blob: dfd1dca9a18f505cc0340338d45707228186774a [file] [log] [blame]
Gregory Maxwellae231142011-07-30 08:18:48 -04001/***********************************************************************
2Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3Redistribution and use in source and binary forms, with or without
Jean-Marc Valinae00e602012-04-20 16:31:04 -04004modification, are permitted provided that the following conditions
5are met:
Gregory Maxwellae231142011-07-30 08:18:48 -04006- Redistributions of source code must retain the above copyright notice,
7this list of conditions and the following disclaimer.
8- Redistributions in binary form must reproduce the above copyright
9notice, this list of conditions and the following disclaimer in the
10documentation and/or other materials provided with the distribution.
Ralph Gilesf2446c22013-09-16 14:40:04 -070011- Neither the name of Internet Society, IETF or IETF Trust, nor the
Jean-Marc Valinae00e602012-04-20 16:31:04 -040012names of specific contributors, may be used to endorse or promote
13products derived from this software without specific prior written
14permission.
Timothy B. Terriberry80ad3832013-05-19 18:00:39 -070015THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Jean-Marc Valinae00e602012-04-20 16:31:04 -040016AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25POSSIBILITY OF SUCH DAMAGE.
Gregory Maxwellae231142011-07-30 08:18:48 -040026***********************************************************************/
27
Jean-Marc Valin5a484122011-08-15 10:49:53 -040028#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
Jean-Marc Valin1c2f5632011-09-16 01:16:53 -070032#include "main.h"
Gregory Maxwellae231142011-07-30 08:18:48 -040033
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -070034/*#define silk_enc_map(a) ((a) > 0 ? 1 : 0)*/
35/*#define silk_dec_map(a) ((a) > 0 ? 1 : -1)*/
Gregory Maxwellae231142011-07-30 08:18:48 -040036/* shifting avoids if-statement */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -070037#define silk_enc_map(a) ( silk_RSHIFT( (a), 15 ) + 1 )
38#define silk_dec_map(a) ( silk_LSHIFT( (a), 1 ) - 1 )
Gregory Maxwellae231142011-07-30 08:18:48 -040039
40/* Encodes signs of excitation */
41void silk_encode_signs(
42 ec_enc *psRangeEnc, /* I/O Compressor data structure */
Koen Vosacc7a6c2011-10-28 19:44:26 -040043 const opus_int8 pulses[], /* I pulse signal */
44 opus_int length, /* I length of input */
45 const opus_int signalType, /* I Signal type */
46 const opus_int quantOffsetType, /* I Quantization offset type */
47 const opus_int sum_pulses[ MAX_NB_SHELL_BLOCKS ] /* I Sum of absolute pulses per block */
Gregory Maxwellae231142011-07-30 08:18:48 -040048)
49{
50 opus_int i, j, p;
51 opus_uint8 icdf[ 2 ];
52 const opus_int8 *q_ptr;
53 const opus_uint8 *icdf_ptr;
54
55 icdf[ 1 ] = 0;
56 q_ptr = pulses;
Timothy B. Terriberry748c9602011-09-17 12:44:19 -070057 i = silk_SMULBB( 7, silk_ADD_LSHIFT( quantOffsetType, signalType, 1 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -040058 icdf_ptr = &silk_sign_iCDF[ i ];
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -070059 length = silk_RSHIFT( length + SHELL_CODEC_FRAME_LENGTH/2, LOG2_SHELL_CODEC_FRAME_LENGTH );
Gregory Maxwellae231142011-07-30 08:18:48 -040060 for( i = 0; i < length; i++ ) {
61 p = sum_pulses[ i ];
62 if( p > 0 ) {
Timothy B. Terriberry748c9602011-09-17 12:44:19 -070063 icdf[ 0 ] = icdf_ptr[ silk_min( p & 0x1F, 6 ) ];
Gregory Maxwellae231142011-07-30 08:18:48 -040064 for( j = 0; j < SHELL_CODEC_FRAME_LENGTH; j++ ) {
65 if( q_ptr[ j ] != 0 ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -070066 ec_enc_icdf( psRangeEnc, silk_enc_map( q_ptr[ j ]), icdf, 8 );
Gregory Maxwellae231142011-07-30 08:18:48 -040067 }
68 }
69 }
70 q_ptr += SHELL_CODEC_FRAME_LENGTH;
71 }
72}
73
74/* Decodes signs of excitation */
75void silk_decode_signs(
76 ec_dec *psRangeDec, /* I/O Compressor data structure */
Jean-Marc Valindce69d22014-01-06 21:59:48 -050077 opus_int16 pulses[], /* I/O pulse signal */
Koen Vosacc7a6c2011-10-28 19:44:26 -040078 opus_int length, /* I length of input */
79 const opus_int signalType, /* I Signal type */
80 const opus_int quantOffsetType, /* I Quantization offset type */
81 const opus_int sum_pulses[ MAX_NB_SHELL_BLOCKS ] /* I Sum of absolute pulses per block */
Gregory Maxwellae231142011-07-30 08:18:48 -040082)
83{
84 opus_int i, j, p;
85 opus_uint8 icdf[ 2 ];
Jean-Marc Valindce69d22014-01-06 21:59:48 -050086 opus_int16 *q_ptr;
Gregory Maxwellae231142011-07-30 08:18:48 -040087 const opus_uint8 *icdf_ptr;
88
89 icdf[ 1 ] = 0;
90 q_ptr = pulses;
Timothy B. Terriberry748c9602011-09-17 12:44:19 -070091 i = silk_SMULBB( 7, silk_ADD_LSHIFT( quantOffsetType, signalType, 1 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -040092 icdf_ptr = &silk_sign_iCDF[ i ];
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -070093 length = silk_RSHIFT( length + SHELL_CODEC_FRAME_LENGTH/2, LOG2_SHELL_CODEC_FRAME_LENGTH );
Gregory Maxwellae231142011-07-30 08:18:48 -040094 for( i = 0; i < length; i++ ) {
95 p = sum_pulses[ i ];
96 if( p > 0 ) {
Timothy B. Terriberry748c9602011-09-17 12:44:19 -070097 icdf[ 0 ] = icdf_ptr[ silk_min( p & 0x1F, 6 ) ];
Gregory Maxwellae231142011-07-30 08:18:48 -040098 for( j = 0; j < SHELL_CODEC_FRAME_LENGTH; j++ ) {
99 if( q_ptr[ j ] > 0 ) {
100 /* attach sign */
101#if 0
102 /* conditional implementation */
103 if( ec_dec_icdf( psRangeDec, icdf, 8 ) == 0 ) {
104 q_ptr[ j ] = -q_ptr[ j ];
105 }
106#else
107 /* implementation with shift, subtraction, multiplication */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700108 q_ptr[ j ] *= silk_dec_map( ec_dec_icdf( psRangeDec, icdf, 8 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400109#endif
110 }
111 }
112 }
113 q_ptr += SHELL_CODEC_FRAME_LENGTH;
114 }
115}