Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2007-2008 CSIRO |
Jean-Marc Valin | 190b782 | 2010-08-27 15:03:20 -0400 | [diff] [blame] | 2 | Copyright (c) 2007-2010 Xiph.Org Foundation |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 3 | Copyright (c) 2008 Gregory Maxwell |
Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 4 | Written by Jean-Marc Valin and Gregory Maxwell */ |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 5 | /* |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions |
| 8 | are met: |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 9 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 10 | - Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 12 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 13 | - Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in the |
| 15 | documentation and/or other materials provided with the distribution. |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 16 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 21 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include "config.h" |
| 32 | #endif |
| 33 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 34 | #define CELT_C |
| 35 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 36 | #include "os_support.h" |
Jean-Marc Valin | f02ba11 | 2007-11-30 01:10:42 +1100 | [diff] [blame] | 37 | #include "mdct.h" |
| 38 | #include <math.h> |
Jean-Marc Valin | 013c31d | 2007-11-30 11:36:46 +1100 | [diff] [blame] | 39 | #include "celt.h" |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 40 | #include "pitch.h" |
Jean-Marc Valin | 991c0f0 | 2007-11-30 16:07:46 +1100 | [diff] [blame] | 41 | #include "bands.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 42 | #include "modes.h" |
Jean-Marc Valin | 6238bc0 | 2008-01-28 22:28:54 +1100 | [diff] [blame] | 43 | #include "entcode.h" |
Jean-Marc Valin | 98d2a49 | 2007-12-07 22:46:47 +1100 | [diff] [blame] | 44 | #include "quant_bands.h" |
Jean-Marc Valin | 4fbd18d | 2008-01-17 14:07:55 +1100 | [diff] [blame] | 45 | #include "rate.h" |
Jean-Marc Valin | c7e0b76 | 2008-03-16 07:55:29 +1100 | [diff] [blame] | 46 | #include "stack_alloc.h" |
Jean-Marc Valin | e4aeb47 | 2008-06-29 12:06:05 +1000 | [diff] [blame] | 47 | #include "mathops.h" |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 48 | #include "float_cast.h" |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 49 | #include <stdarg.h> |
Jean-Marc Valin | 6c3788c | 2010-06-20 22:48:50 -0400 | [diff] [blame] | 50 | #include "plc.h" |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 51 | #include "vq.h" |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 52 | |
Timothy B. Terriberry | 845dfa1 | 2011-01-02 16:53:28 -0800 | [diff] [blame] | 53 | static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0}; |
| 54 | /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */ |
| 55 | static const unsigned char spread_icdf[4] = {25, 23, 2, 0}; |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 56 | |
Jean-Marc Valin | 2ce5c63 | 2011-01-17 20:50:18 -0500 | [diff] [blame] | 57 | static const unsigned char tapset_icdf[3]={2,1,0}; |
| 58 | |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 59 | static const unsigned char toOpusTable[20] = { |
| 60 | 0xE0, 0xE8, 0xF0, 0xF8, |
| 61 | 0xC0, 0xC8, 0xD0, 0xD8, |
| 62 | 0xA0, 0xA8, 0xB0, 0xB8, |
| 63 | 0x00, 0x00, 0x00, 0x00, |
| 64 | 0x80, 0x88, 0x90, 0x98, |
| 65 | }; |
| 66 | |
| 67 | static const unsigned char fromOpusTable[16] = { |
| 68 | 0x80, 0x88, 0x90, 0x98, |
| 69 | 0x40, 0x48, 0x50, 0x58, |
| 70 | 0x20, 0x28, 0x30, 0x38, |
| 71 | 0x00, 0x08, 0x10, 0x18 |
| 72 | }; |
| 73 | |
| 74 | static inline int toOpus(unsigned char c) |
| 75 | { |
| 76 | int ret=0; |
| 77 | if (c<0xA0) |
| 78 | ret = toOpusTable[c>>3]; |
| 79 | if (ret == 0) |
| 80 | return -1; |
| 81 | else |
| 82 | return ret|(c&0x7); |
| 83 | } |
| 84 | |
| 85 | static inline int fromOpus(unsigned char c) |
| 86 | { |
| 87 | if (c<0x80) |
| 88 | return -1; |
| 89 | else |
| 90 | return fromOpusTable[(c>>3)-16] | (c&0x7); |
| 91 | } |
| 92 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 93 | #define COMBFILTER_MAXPERIOD 1024 |
Jean-Marc Valin | d121260 | 2011-01-25 13:11:36 -0500 | [diff] [blame] | 94 | #define COMBFILTER_MINPERIOD 15 |
Jean-Marc Valin | 6bf0462 | 2010-09-30 10:16:22 -0400 | [diff] [blame] | 95 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 96 | static int resampling_factor(opus_int32 rate) |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 97 | { |
| 98 | int ret; |
| 99 | switch (rate) |
| 100 | { |
| 101 | case 48000: |
| 102 | ret = 1; |
| 103 | break; |
| 104 | case 24000: |
| 105 | ret = 2; |
| 106 | break; |
| 107 | case 16000: |
| 108 | ret = 3; |
| 109 | break; |
| 110 | case 12000: |
| 111 | ret = 4; |
| 112 | break; |
| 113 | case 8000: |
| 114 | ret = 6; |
| 115 | break; |
| 116 | default: |
| 117 | ret = 0; |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 118 | break; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 119 | } |
| 120 | return ret; |
| 121 | } |
| 122 | |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 123 | /** Encoder state |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 124 | @brief Encoder state |
| 125 | */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 126 | struct CELTEncoder { |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 127 | const CELTMode *mode; /**< Mode used by the encoder */ |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 128 | int overlap; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 129 | int channels; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 130 | int stream_channels; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 131 | |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 132 | int force_intra; |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 133 | int clip; |
Jean-Marc Valin | d539c6b | 2011-02-03 13:36:03 -0500 | [diff] [blame] | 134 | int disable_pf; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 135 | int complexity; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 136 | int upsample; |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 137 | int start, end; |
| 138 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 139 | opus_int32 bitrate; |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 140 | int vbr; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 141 | int signalling; |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 142 | int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */ |
Jean-Marc Valin | 6965388 | 2011-04-21 10:41:13 -0400 | [diff] [blame] | 143 | int loss_rate; |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 144 | |
| 145 | /* Everything beyond this point gets cleared on a reset */ |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 146 | #define ENCODER_RESET_START rng |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 147 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 148 | opus_uint32 rng; |
Timothy B. Terriberry | 320cf2e | 2010-12-17 05:52:06 -0800 | [diff] [blame] | 149 | int spread_decision; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 150 | opus_val32 delayedIntra; |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 151 | int tonal_average; |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 152 | int lastCodedBands; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 153 | int hf_average; |
| 154 | int tapset_decision; |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 155 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 156 | int prefilter_period; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 157 | opus_val16 prefilter_gain; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 158 | int prefilter_tapset; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 159 | #ifdef RESYNTH |
| 160 | int prefilter_period_old; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 161 | opus_val16 prefilter_gain_old; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 162 | int prefilter_tapset_old; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 163 | #endif |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 164 | int consec_transient; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 165 | |
Gregory Maxwell | 86dd984 | 2011-08-12 19:03:29 -0400 | [diff] [blame] | 166 | opus_val32 preemph_memE[2]; |
| 167 | opus_val32 preemph_memD[2]; |
| 168 | |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 169 | /* VBR-related parameters */ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 170 | opus_int32 vbr_reservoir; |
| 171 | opus_int32 vbr_drift; |
| 172 | opus_int32 vbr_offset; |
| 173 | opus_int32 vbr_count; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 174 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 175 | #ifdef RESYNTH |
| 176 | celt_sig syn_mem[2][2*MAX_PERIOD]; |
| 177 | #endif |
| 178 | |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 179 | celt_sig in_mem[1]; /* Size = channels*mode->overlap */ |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 180 | /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */ |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 181 | /* celt_sig overlap_mem[], Size = channels*mode->overlap */ |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 182 | /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */ |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 183 | }; |
| 184 | |
Jean-Marc Valin | 8cf29f0 | 2011-01-30 23:38:28 -0500 | [diff] [blame] | 185 | int celt_encoder_get_size(int channels) |
| 186 | { |
| 187 | CELTMode *mode = celt_mode_create(48000, 960, NULL); |
| 188 | return celt_encoder_get_size_custom(mode, channels); |
| 189 | } |
| 190 | |
| 191 | int celt_encoder_get_size_custom(const CELTMode *mode, int channels) |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 192 | { |
| 193 | int size = sizeof(struct CELTEncoder) |
| 194 | + (2*channels*mode->overlap-1)*sizeof(celt_sig) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 195 | + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 196 | + 3*channels*mode->nbEBands*sizeof(opus_val16); |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 197 | return size; |
| 198 | } |
| 199 | |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 200 | CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error) |
| 201 | { |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 202 | int ret; |
Jean-Marc Valin | 07f8840 | 2011-08-29 15:08:51 -0400 | [diff] [blame] | 203 | CELTEncoder *st = (CELTEncoder *)opus_alloc(celt_encoder_get_size_custom(mode, channels)); |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 204 | /* init will handle the NULL case */ |
| 205 | ret = celt_encoder_init_custom(st, mode, channels); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 206 | if (ret != OPUS_OK) |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 207 | { |
| 208 | celt_encoder_destroy(st); |
| 209 | st = NULL; |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 210 | if (error) |
| 211 | *error = ret; |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 212 | } |
| 213 | return st; |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 214 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 215 | |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 216 | int celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels) |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 217 | { |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 218 | int ret; |
| 219 | ret = celt_encoder_init_custom(st, celt_mode_create(48000, 960, NULL), channels); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 220 | if (ret != OPUS_OK) |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 221 | return ret; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 222 | st->upsample = resampling_factor(sampling_rate); |
| 223 | if (st->upsample==0) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 224 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 225 | else |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 226 | return OPUS_OK; |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 229 | int celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int channels) |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 230 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 231 | if (channels < 0 || channels > 2) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 232 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 233 | |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 234 | if (st==NULL || mode==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 235 | return OPUS_ALLOC_FAIL; |
Jean-Marc Valin | 6d3829f | 2010-08-27 17:52:38 -0400 | [diff] [blame] | 236 | |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 237 | OPUS_CLEAR((char*)st, celt_encoder_get_size_custom(mode, channels)); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 238 | |
Jean-Marc Valin | 73e51b3 | 2007-12-05 17:48:24 +1100 | [diff] [blame] | 239 | st->mode = mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 240 | st->overlap = mode->overlap; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 241 | st->stream_channels = st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 242 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 243 | st->upsample = 1; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 244 | st->start = 0; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 245 | st->end = st->mode->effEBands; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 246 | st->signalling = 1; |
| 247 | |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 248 | st->constrained_vbr = 1; |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 249 | st->clip = 1; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 250 | |
Jean-Marc Valin | 5e36e19 | 2011-08-16 09:12:14 -0400 | [diff] [blame] | 251 | st->bitrate = -1; |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 252 | st->vbr = 0; |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 253 | st->vbr_offset = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 254 | st->force_intra = 0; |
Gregory Maxwell | 8842fde | 2009-05-04 15:58:40 -0400 | [diff] [blame] | 255 | st->delayedIntra = 1; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 256 | st->tonal_average = 256; |
Timothy B. Terriberry | 320cf2e | 2010-12-17 05:52:06 -0800 | [diff] [blame] | 257 | st->spread_decision = SPREAD_NORMAL; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 258 | st->hf_average = 0; |
| 259 | st->tapset_decision = 0; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 260 | st->complexity = 5; |
Jean-Marc Valin | a76a0b2 | 2008-01-17 22:43:05 +1100 | [diff] [blame] | 261 | |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 262 | return OPUS_OK; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 263 | } |
| 264 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 265 | void celt_encoder_destroy(CELTEncoder *st) |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 266 | { |
Jean-Marc Valin | 07f8840 | 2011-08-29 15:08:51 -0400 | [diff] [blame] | 267 | opus_free(st); |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 268 | } |
| 269 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 270 | static inline opus_val16 SIG2WORD16(celt_sig x) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 271 | { |
| 272 | #ifdef FIXED_POINT |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 273 | x = PSHR32(x, SIG_SHIFT); |
Jean-Marc Valin | abdfc38 | 2008-04-18 15:57:18 +1000 | [diff] [blame] | 274 | x = MAX32(x, -32768); |
| 275 | x = MIN32(x, 32767); |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 276 | return EXTRACT16(x); |
| 277 | #else |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 278 | return (opus_val16)x; |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 279 | #endif |
| 280 | } |
| 281 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 282 | static int transient_analysis(const opus_val32 * restrict in, int len, int C, |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 283 | int overlap) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 284 | { |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 285 | int i; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 286 | VARDECL(opus_val16, tmp); |
| 287 | opus_val32 mem0=0,mem1=0; |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 288 | int is_transient = 0; |
| 289 | int block; |
| 290 | int N; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 291 | VARDECL(opus_val16, bins); |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 292 | SAVE_STACK; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 293 | ALLOC(tmp, len, opus_val16); |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 294 | |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 295 | block = overlap/2; |
| 296 | N=len/block; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 297 | ALLOC(bins, N, opus_val16); |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 298 | if (C==1) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 299 | { |
| 300 | for (i=0;i<len;i++) |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 301 | tmp[i] = SHR32(in[i],SIG_SHIFT); |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 302 | } else { |
| 303 | for (i=0;i<len;i++) |
Jean-Marc Valin | 933dd83 | 2010-10-24 00:08:16 -0400 | [diff] [blame] | 304 | tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1); |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 305 | } |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 306 | |
| 307 | /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */ |
| 308 | for (i=0;i<len;i++) |
| 309 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 310 | opus_val32 x,y; |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 311 | x = tmp[i]; |
| 312 | y = ADD32(mem0, x); |
| 313 | #ifdef FIXED_POINT |
| 314 | mem0 = mem1 + y - SHL32(x,1); |
| 315 | mem1 = x - SHR32(y,1); |
| 316 | #else |
| 317 | mem0 = mem1 + y - 2*x; |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 318 | mem1 = x - .5f*y; |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 319 | #endif |
| 320 | tmp[i] = EXTRACT16(SHR(y,2)); |
| 321 | } |
| 322 | /* First few samples are bad because we don't propagate the memory */ |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 323 | for (i=0;i<12;i++) |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 324 | tmp[i] = 0; |
| 325 | |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 326 | for (i=0;i<N;i++) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 327 | { |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 328 | int j; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 329 | opus_val16 max_abs=0; |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 330 | for (j=0;j<block;j++) |
Jean-Marc Valin | 168888f | 2011-03-02 17:26:48 -0500 | [diff] [blame] | 331 | max_abs = MAX16(max_abs, ABS16(tmp[i*block+j])); |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 332 | bins[i] = max_abs; |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 333 | } |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 334 | for (i=0;i<N;i++) |
Jean-Marc Valin | 4a8c1f1 | 2010-10-13 18:03:50 -0400 | [diff] [blame] | 335 | { |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 336 | int j; |
| 337 | int conseq=0; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 338 | opus_val16 t1, t2, t3; |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 339 | |
| 340 | t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]); |
| 341 | t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]); |
| 342 | t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]); |
| 343 | for (j=0;j<i;j++) |
| 344 | { |
| 345 | if (bins[j] < t1) |
| 346 | conseq++; |
| 347 | if (bins[j] < t2) |
| 348 | conseq++; |
| 349 | else |
| 350 | conseq = 0; |
| 351 | } |
| 352 | if (conseq>=3) |
| 353 | is_transient=1; |
| 354 | conseq = 0; |
| 355 | for (j=i+1;j<N;j++) |
| 356 | { |
| 357 | if (bins[j] < t3) |
| 358 | conseq++; |
| 359 | else |
| 360 | conseq = 0; |
| 361 | } |
| 362 | if (conseq>=7) |
| 363 | is_transient=1; |
Jean-Marc Valin | 4a8c1f1 | 2010-10-13 18:03:50 -0400 | [diff] [blame] | 364 | } |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 365 | RESTORE_STACK; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 366 | #ifdef FUZZING |
| 367 | is_transient = rand()&0x1; |
| 368 | #endif |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 369 | return is_transient; |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 370 | } |
| 371 | |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 372 | /** Apply window and compute the MDCT for all sub-frames and |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 373 | all channels in a frame */ |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 374 | static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * restrict in, celt_sig * restrict out, int _C, int LM) |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 375 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 376 | const int C = CHANNELS(_C); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 377 | if (C==1 && !shortBlocks) |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 378 | { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 379 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | a6b4e25 | 2011-08-15 10:20:06 -0400 | [diff] [blame] | 380 | clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1); |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 381 | } else { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 382 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | ce4dd36 | 2010-05-07 07:45:18 -0400 | [diff] [blame] | 383 | int N = mode->shortMdctSize<<LM; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 384 | int B = 1; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 385 | int b, c; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 386 | if (shortBlocks) |
| 387 | { |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 388 | N = mode->shortMdctSize; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 389 | B = shortBlocks; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 390 | } |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 391 | c=0; do { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 392 | for (b=0;b<B;b++) |
| 393 | { |
Jean-Marc Valin | a6b4e25 | 2011-08-15 10:20:06 -0400 | [diff] [blame] | 394 | /* Interleaving the sub-frames while doing the MDCTs */ |
| 395 | clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 396 | } |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 397 | } while (++c<C); |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 398 | } |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 399 | } |
| 400 | |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 401 | /** Compute the IMDCT and apply window for all sub-frames and |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 402 | all channels in a frame */ |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 403 | static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X, |
Jean-Marc Valin | 7a08ddd | 2010-10-18 14:55:42 -0400 | [diff] [blame] | 404 | celt_sig * restrict out_mem[], |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 405 | celt_sig * restrict overlap_mem[], int _C, int LM) |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 406 | { |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 407 | int c; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 408 | const int C = CHANNELS(_C); |
Jean-Marc Valin | ce4dd36 | 2010-05-07 07:45:18 -0400 | [diff] [blame] | 409 | const int N = mode->shortMdctSize<<LM; |
Jean-Marc Valin | b18ec0b | 2008-04-11 04:07:52 +1000 | [diff] [blame] | 410 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 411 | VARDECL(opus_val32, x); |
| 412 | SAVE_STACK; |
| 413 | |
| 414 | ALLOC(x, N+overlap, opus_val32); |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 415 | c=0; do { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 416 | int j; |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 417 | int b; |
| 418 | int N2 = N; |
| 419 | int B = 1; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 420 | |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 421 | if (shortBlocks) |
| 422 | { |
| 423 | N2 = mode->shortMdctSize; |
| 424 | B = shortBlocks; |
| 425 | } |
| 426 | /* Prevents problems from the imdct doing the overlap-add */ |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 427 | OPUS_CLEAR(x, overlap); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 428 | |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 429 | for (b=0;b<B;b++) |
| 430 | { |
| 431 | /* IMDCT on the interleaved the sub-frames */ |
| 432 | clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B); |
| 433 | } |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 434 | |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 435 | for (j=0;j<overlap;j++) |
| 436 | out_mem[c][j] = x[j] + overlap_mem[c][j]; |
| 437 | for (;j<N;j++) |
| 438 | out_mem[c][j] = x[j]; |
| 439 | for (j=0;j<overlap;j++) |
| 440 | overlap_mem[c][j] = x[N+j]; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 441 | } while (++c<C); |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 442 | RESTORE_STACK; |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 443 | } |
| 444 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 445 | static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int _C, int downsample, const opus_val16 *coef, celt_sig *mem) |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 446 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 447 | const int C = CHANNELS(_C); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 448 | int c; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 449 | int count=0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 450 | c=0; do { |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 451 | int j; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 452 | celt_sig * restrict x; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 453 | opus_val16 * restrict y; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 454 | celt_sig m = mem[c]; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 455 | x =in[c]; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 456 | y = pcm+c; |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 457 | for (j=0;j<N;j++) |
| 458 | { |
Jean-Marc Valin | af1fce9 | 2010-07-16 11:05:06 -0400 | [diff] [blame] | 459 | celt_sig tmp = *x + m; |
| 460 | m = MULT16_32_Q15(coef[0], tmp) |
| 461 | - MULT16_32_Q15(coef[1], *x); |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 462 | tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2); |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame] | 463 | x++; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 464 | /* Technically the store could be moved outside of the if because |
| 465 | the stores we don't want will just be overwritten */ |
| 466 | if (++count==downsample) |
| 467 | { |
| 468 | *y = SCALEOUT(SIG2WORD16(tmp)); |
| 469 | y+=C; |
| 470 | count=0; |
| 471 | } |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 472 | } |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 473 | mem[c] = m; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 474 | } while (++c<C); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 475 | } |
| 476 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 477 | static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N, |
| 478 | opus_val16 g0, opus_val16 g1, int tapset0, int tapset1, |
| 479 | const opus_val16 *window, int overlap) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 480 | { |
| 481 | int i; |
| 482 | /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */ |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 483 | opus_val16 g00, g01, g02, g10, g11, g12; |
| 484 | static const opus_val16 gains[3][3] = { |
Jean-Marc Valin | 61f4041 | 2011-01-27 17:14:33 -0500 | [diff] [blame] | 485 | {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)}, |
| 486 | {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)}, |
| 487 | {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}}; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 488 | g00 = MULT16_16_Q15(g0, gains[tapset0][0]); |
| 489 | g01 = MULT16_16_Q15(g0, gains[tapset0][1]); |
| 490 | g02 = MULT16_16_Q15(g0, gains[tapset0][2]); |
| 491 | g10 = MULT16_16_Q15(g1, gains[tapset1][0]); |
| 492 | g11 = MULT16_16_Q15(g1, gains[tapset1][1]); |
| 493 | g12 = MULT16_16_Q15(g1, gains[tapset1][2]); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 494 | for (i=0;i<overlap;i++) |
| 495 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 496 | opus_val16 f; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 497 | f = MULT16_16_Q15(window[i],window[i]); |
| 498 | y[i] = x[i] |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 499 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0]) |
| 500 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1]) |
| 501 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1]) |
| 502 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2]) |
| 503 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2]) |
| 504 | + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1]) |
| 505 | + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1]) |
| 506 | + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1]) |
| 507 | + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2]) |
| 508 | + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 509 | |
| 510 | } |
| 511 | for (i=overlap;i<N;i++) |
| 512 | y[i] = x[i] |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 513 | + MULT16_32_Q15(g10,x[i-T1]) |
| 514 | + MULT16_32_Q15(g11,x[i-T1-1]) |
| 515 | + MULT16_32_Q15(g11,x[i-T1+1]) |
| 516 | + MULT16_32_Q15(g12,x[i-T1-2]) |
| 517 | + MULT16_32_Q15(g12,x[i-T1+2]); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 518 | } |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 519 | |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 520 | static const signed char tf_select_table[4][8] = { |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 521 | {0, -1, 0, -1, 0,-1, 0,-1}, |
Jean-Marc Valin | 41a15e6 | 2010-12-27 16:33:38 -0500 | [diff] [blame] | 522 | {0, -1, 0, -2, 1, 0, 1,-1}, |
| 523 | {0, -2, 0, -3, 2, 0, 1,-1}, |
Jean-Marc Valin | 2b13401 | 2011-01-12 16:13:46 -0500 | [diff] [blame] | 524 | {0, -2, 0, -3, 3, 0, 1,-1}, |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 525 | }; |
| 526 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 527 | static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, int width) |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 528 | { |
| 529 | int i, j; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 530 | static const opus_val16 sqrtM_1[4] = {Q15ONE, QCONST16(.70710678f,15), QCONST16(0.5f,15), QCONST16(0.35355339f,15)}; |
| 531 | opus_val32 L1; |
| 532 | opus_val16 bias; |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 533 | L1=0; |
| 534 | for (i=0;i<1<<LM;i++) |
| 535 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 536 | opus_val32 L2 = 0; |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 537 | for (j=0;j<N>>LM;j++) |
| 538 | L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]); |
| 539 | L1 += celt_sqrt(L2); |
| 540 | } |
| 541 | L1 = MULT16_32_Q15(sqrtM_1[LM], L1); |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 542 | if (width==1) |
| 543 | bias = QCONST16(.12f,15)*LM; |
| 544 | else if (width==2) |
| 545 | bias = QCONST16(.05f,15)*LM; |
| 546 | else |
| 547 | bias = QCONST16(.02f,15)*LM; |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 548 | L1 = MAC16_32_Q15(L1, bias, L1); |
| 549 | return L1; |
| 550 | } |
| 551 | |
Gregory Maxwell | 06d57b2 | 2011-08-01 22:02:25 -0400 | [diff] [blame] | 552 | static int tf_analysis(const CELTMode *m, int len, int C, int isTransient, |
| 553 | int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM, |
| 554 | int *tf_sum) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 555 | { |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 556 | int i; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 557 | VARDECL(int, metric); |
| 558 | int cost0; |
| 559 | int cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 560 | VARDECL(int, path0); |
| 561 | VARDECL(int, path1); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 562 | VARDECL(celt_norm, tmp); |
| 563 | int lambda; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 564 | int tf_select=0; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 565 | SAVE_STACK; |
| 566 | |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 567 | if (nbCompressedBytes<15*C) |
| 568 | { |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 569 | *tf_sum = 0; |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 570 | for (i=0;i<len;i++) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 571 | tf_res[i] = isTransient; |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 572 | return 0; |
| 573 | } |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 574 | if (nbCompressedBytes<40) |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 575 | lambda = 12; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 576 | else if (nbCompressedBytes<60) |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 577 | lambda = 6; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 578 | else if (nbCompressedBytes<100) |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 579 | lambda = 4; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 580 | else |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 581 | lambda = 3; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 582 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 583 | ALLOC(metric, len, int); |
| 584 | ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm); |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 585 | ALLOC(path0, len, int); |
| 586 | ALLOC(path1, len, int); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 587 | |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 588 | *tf_sum = 0; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 589 | for (i=0;i<len;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 590 | { |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 591 | int j, k, N; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 592 | opus_val32 L1, best_L1; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 593 | int best_level=0; |
| 594 | N = (m->eBands[i+1]-m->eBands[i])<<LM; |
| 595 | for (j=0;j<N;j++) |
| 596 | tmp[j] = X[j+(m->eBands[i]<<LM)]; |
Jean-Marc Valin | 20e4c6a | 2010-12-21 22:27:08 -0500 | [diff] [blame] | 597 | /* Just add the right channel if we're in stereo */ |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 598 | if (C==2) |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 599 | for (j=0;j<N;j++) |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 600 | tmp[j] = ADD16(tmp[j],X[N0+j+(m->eBands[i]<<LM)]); |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 601 | L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 602 | best_L1 = L1; |
| 603 | /*printf ("%f ", L1);*/ |
| 604 | for (k=0;k<LM;k++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 605 | { |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 606 | int B; |
| 607 | |
| 608 | if (isTransient) |
| 609 | B = (LM-k-1); |
| 610 | else |
| 611 | B = k+1; |
| 612 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 613 | if (isTransient) |
| 614 | haar1(tmp, N>>(LM-k), 1<<(LM-k)); |
| 615 | else |
| 616 | haar1(tmp, N>>k, 1<<k); |
| 617 | |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 618 | L1 = l1_metric(tmp, N, B, N>>LM); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 619 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 620 | if (L1 < best_L1) |
| 621 | { |
| 622 | best_L1 = L1; |
| 623 | best_level = k+1; |
| 624 | } |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 625 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 626 | /*printf ("%d ", isTransient ? LM-best_level : best_level);*/ |
| 627 | if (isTransient) |
| 628 | metric[i] = best_level; |
| 629 | else |
| 630 | metric[i] = -best_level; |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 631 | *tf_sum += metric[i]; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 632 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 633 | /*printf("\n");*/ |
Jean-Marc Valin | 27169ca | 2011-05-16 14:10:04 -0400 | [diff] [blame] | 634 | /* TODO: Detect the extreme transients that require tf_select = 1 */ |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 635 | tf_select = 0; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 636 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 637 | cost0 = 0; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 638 | cost1 = isTransient ? 0 : lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 639 | /* Viterbi forward pass */ |
| 640 | for (i=1;i<len;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 641 | { |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 642 | int curr0, curr1; |
| 643 | int from0, from1; |
Jean-Marc Valin | 581fdba | 2010-05-28 06:56:23 -0400 | [diff] [blame] | 644 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 645 | from0 = cost0; |
| 646 | from1 = cost1 + lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 647 | if (from0 < from1) |
| 648 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 649 | curr0 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 650 | path0[i]= 0; |
| 651 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 652 | curr0 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 653 | path0[i]= 1; |
| 654 | } |
| 655 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 656 | from0 = cost0 + lambda; |
| 657 | from1 = cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 658 | if (from0 < from1) |
| 659 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 660 | curr1 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 661 | path1[i]= 0; |
| 662 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 663 | curr1 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 664 | path1[i]= 1; |
| 665 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 666 | cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]); |
| 667 | cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]); |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 668 | } |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 669 | tf_res[len-1] = cost0 < cost1 ? 0 : 1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 670 | /* Viterbi backward pass to check the decisions */ |
| 671 | for (i=len-2;i>=0;i--) |
| 672 | { |
| 673 | if (tf_res[i+1] == 1) |
| 674 | tf_res[i] = path1[i+1]; |
| 675 | else |
| 676 | tf_res[i] = path0[i+1]; |
| 677 | } |
Jean-Marc Valin | 71ae6d4 | 2010-06-27 21:55:08 -0400 | [diff] [blame] | 678 | RESTORE_STACK; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 679 | #ifdef FUZZING |
| 680 | tf_select = rand()&0x1; |
| 681 | tf_res[0] = rand()&0x1; |
| 682 | for (i=1;i<len;i++) |
| 683 | tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0); |
| 684 | #endif |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 685 | return tf_select; |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 686 | } |
| 687 | |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 688 | static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc) |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 689 | { |
| 690 | int curr, i; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 691 | int tf_select_rsv; |
| 692 | int tf_changed; |
| 693 | int logp; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 694 | opus_uint32 budget; |
| 695 | opus_uint32 tell; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 696 | budget = enc->storage*8; |
| 697 | tell = ec_tell(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 698 | logp = isTransient ? 2 : 4; |
| 699 | /* Reserve space to code the tf_select decision. */ |
| 700 | tf_select_rsv = LM>0 && tell+logp+1 <= budget; |
| 701 | budget -= tf_select_rsv; |
| 702 | curr = tf_changed = 0; |
| 703 | for (i=start;i<end;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 704 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 705 | if (tell+logp<=budget) |
| 706 | { |
| 707 | ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 708 | tell = ec_tell(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 709 | curr = tf_res[i]; |
| 710 | tf_changed |= curr; |
| 711 | } |
| 712 | else |
| 713 | tf_res[i] = curr; |
| 714 | logp = isTransient ? 4 : 5; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 715 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 716 | /* Only code tf_select if it would actually make a difference. */ |
| 717 | if (tf_select_rsv && |
| 718 | tf_select_table[LM][4*isTransient+0+tf_changed]!= |
| 719 | tf_select_table[LM][4*isTransient+2+tf_changed]) |
| 720 | ec_enc_bit_logp(enc, tf_select, 1); |
| 721 | else |
| 722 | tf_select = 0; |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 723 | for (i=start;i<end;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 724 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 725 | /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/ |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 726 | } |
| 727 | |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 728 | static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 729 | { |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 730 | int i, curr, tf_select; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 731 | int tf_select_rsv; |
| 732 | int tf_changed; |
| 733 | int logp; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 734 | opus_uint32 budget; |
| 735 | opus_uint32 tell; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 736 | |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 737 | budget = dec->storage*8; |
| 738 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 739 | logp = isTransient ? 2 : 4; |
| 740 | tf_select_rsv = LM>0 && tell+logp+1<=budget; |
| 741 | budget -= tf_select_rsv; |
| 742 | tf_changed = curr = 0; |
| 743 | for (i=start;i<end;i++) |
Timothy B. Terriberry | 509ad20 | 2010-12-27 18:20:20 -0800 | [diff] [blame] | 744 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 745 | if (tell+logp<=budget) |
| 746 | { |
| 747 | curr ^= ec_dec_bit_logp(dec, logp); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 748 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 749 | tf_changed |= curr; |
| 750 | } |
| 751 | tf_res[i] = curr; |
| 752 | logp = isTransient ? 4 : 5; |
| 753 | } |
| 754 | tf_select = 0; |
| 755 | if (tf_select_rsv && |
| 756 | tf_select_table[LM][4*isTransient+0+tf_changed] != |
| 757 | tf_select_table[LM][4*isTransient+2+tf_changed]) |
| 758 | { |
| 759 | tf_select = ec_dec_bit_logp(dec, 1); |
| 760 | } |
| 761 | for (i=start;i<end;i++) |
| 762 | { |
| 763 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; |
Timothy B. Terriberry | 509ad20 | 2010-12-27 18:20:20 -0800 | [diff] [blame] | 764 | } |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 765 | } |
| 766 | |
Timothy B. Terriberry | ce6d090 | 2011-02-01 17:41:12 -0800 | [diff] [blame] | 767 | static void init_caps(const CELTMode *m,int *cap,int LM,int C) |
| 768 | { |
| 769 | int i; |
| 770 | for (i=0;i<m->nbEBands;i++) |
| 771 | { |
| 772 | int N; |
| 773 | N=(m->eBands[i+1]-m->eBands[i])<<LM; |
| 774 | cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2; |
| 775 | } |
| 776 | } |
| 777 | |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 778 | static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X, |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 779 | const opus_val16 *bandLogE, int end, int LM, int C, int N0) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 780 | { |
| 781 | int i; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 782 | opus_val32 diff=0; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 783 | int c; |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 784 | int trim_index = 5; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 785 | if (C==2) |
| 786 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 787 | opus_val16 sum = 0; /* Q10 */ |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 788 | /* Compute inter-channel correlation for low frequencies */ |
| 789 | for (i=0;i<8;i++) |
| 790 | { |
| 791 | int j; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 792 | opus_val32 partial = 0; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 793 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) |
| 794 | partial = MAC16_16(partial, X[j], X[N0+j]); |
| 795 | sum = ADD16(sum, EXTRACT16(SHR32(partial, 18))); |
| 796 | } |
| 797 | sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum); |
| 798 | /*printf ("%f\n", sum);*/ |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 799 | if (sum > QCONST16(.995f,10)) |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 800 | trim_index-=4; |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 801 | else if (sum > QCONST16(.92f,10)) |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 802 | trim_index-=3; |
| 803 | else if (sum > QCONST16(.85f,10)) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 804 | trim_index-=2; |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 805 | else if (sum > QCONST16(.8f,10)) |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 806 | trim_index-=1; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 807 | } |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 808 | |
| 809 | /* Estimate spectral tilt */ |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 810 | c=0; do { |
Gregory Maxwell | 7007f1b | 2011-02-08 16:17:47 -0500 | [diff] [blame] | 811 | for (i=0;i<end-1;i++) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 812 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 813 | diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands); |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 814 | } |
Gregory Maxwell | f93d81b | 2011-08-07 00:57:31 -0400 | [diff] [blame] | 815 | } while (++c<C); |
Jean-Marc Valin | 817220e | 2011-08-08 11:33:17 -0400 | [diff] [blame] | 816 | /* We divide by two here to avoid making the tilt larger for stereo as a |
| 817 | result of a bug in the loop above */ |
| 818 | diff /= 2*C*(end-1); |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 819 | /*printf("%f\n", diff);*/ |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 820 | if (diff > QCONST16(2.f, DB_SHIFT)) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 821 | trim_index--; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 822 | if (diff > QCONST16(8.f, DB_SHIFT)) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 823 | trim_index--; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 824 | if (diff < -QCONST16(4.f, DB_SHIFT)) |
Jean-Marc Valin | 9037757 | 2010-10-22 15:12:01 -0400 | [diff] [blame] | 825 | trim_index++; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 826 | if (diff < -QCONST16(10.f, DB_SHIFT)) |
| 827 | trim_index++; |
| 828 | |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 829 | if (trim_index<0) |
| 830 | trim_index = 0; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 831 | if (trim_index>10) |
| 832 | trim_index = 10; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 833 | #ifdef FUZZING |
| 834 | trim_index = rand()%11; |
| 835 | #endif |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 836 | return trim_index; |
| 837 | } |
| 838 | |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 839 | static int stereo_analysis(const CELTMode *m, const celt_norm *X, |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 840 | int LM, int N0) |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 841 | { |
| 842 | int i; |
| 843 | int thetas; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 844 | opus_val32 sumLR = EPSILON, sumMS = EPSILON; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 845 | |
| 846 | /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */ |
| 847 | for (i=0;i<13;i++) |
| 848 | { |
| 849 | int j; |
| 850 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) |
| 851 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 852 | opus_val16 L, R, M, S; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 853 | L = X[j]; |
| 854 | R = X[N0+j]; |
| 855 | M = L+R; |
| 856 | S = L-R; |
| 857 | sumLR += EXTEND32(ABS16(L)) + EXTEND32(ABS16(R)); |
| 858 | sumMS += EXTEND32(ABS16(M)) + EXTEND32(ABS16(S)); |
| 859 | } |
| 860 | } |
| 861 | sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS); |
| 862 | thetas = 13; |
| 863 | /* We don't need thetas for lower bands with LM<=1 */ |
| 864 | if (LM<=1) |
| 865 | thetas -= 8; |
| 866 | return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS) |
| 867 | > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR); |
| 868 | } |
| 869 | |
Jean-Marc Valin | 3a8f04d | 2011-02-02 23:02:25 -0500 | [diff] [blame] | 870 | CELT_STATIC |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 871 | int celt_encode_with_ec(CELTEncoder * restrict st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 872 | { |
Jean-Marc Valin | 7a08ddd | 2010-10-18 14:55:42 -0400 | [diff] [blame] | 873 | int i, c, N; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 874 | opus_int32 bits; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 875 | ec_enc _enc; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 876 | VARDECL(celt_sig, in); |
| 877 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 878 | VARDECL(celt_norm, X); |
| 879 | VARDECL(celt_ener, bandE); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 880 | VARDECL(opus_val16, bandLogE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 881 | VARDECL(int, fine_quant); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 882 | VARDECL(opus_val16, error); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 883 | VARDECL(int, pulses); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 884 | VARDECL(int, cap); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 885 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 886 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 887 | VARDECL(int, tf_res); |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 888 | VARDECL(unsigned char, collapse_masks); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 889 | celt_sig *prefilter_mem; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 890 | opus_val16 *oldBandE, *oldLogE, *oldLogE2; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 891 | int shortBlocks=0; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 892 | int isTransient=0; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 893 | const int CC = CHANNELS(st->channels); |
| 894 | const int C = CHANNELS(st->stream_channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 895 | int LM, M; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 896 | int tf_select; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 897 | int nbFilledBytes, nbAvailableBytes; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 898 | int effEnd; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 899 | int codedBands; |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 900 | int tf_sum; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 901 | int alloc_trim; |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 902 | int pitch_index=COMBFILTER_MINPERIOD; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 903 | opus_val16 gain1 = 0; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 904 | int intensity=0; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 905 | int dual_stereo=0; |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 906 | int effectiveBytes; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 907 | opus_val16 pf_threshold; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 908 | int dynalloc_logp; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 909 | opus_int32 vbr_rate; |
| 910 | opus_int32 total_bits; |
| 911 | opus_int32 total_boost; |
| 912 | opus_int32 balance; |
| 913 | opus_int32 tell; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 914 | int prefilter_tapset=0; |
| 915 | int pf_on; |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 916 | int anti_collapse_rsv; |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 917 | int anti_collapse_on=0; |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 918 | int silence=0; |
Jean-Marc Valin | f62b3bb | 2011-03-09 11:56:29 -0500 | [diff] [blame] | 919 | ALLOC_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 920 | |
Jean-Marc Valin | b3dae4b | 2011-02-04 21:50:54 -0500 | [diff] [blame] | 921 | if (nbCompressedBytes<2 || pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 922 | return OPUS_BAD_ARG; |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 923 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 924 | frame_size *= st->upsample; |
Gregory Maxwell | 95becbe | 2011-02-03 21:06:43 -0500 | [diff] [blame] | 925 | for (LM=0;LM<=st->mode->maxLM;LM++) |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 926 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 927 | break; |
Gregory Maxwell | 95becbe | 2011-02-03 21:06:43 -0500 | [diff] [blame] | 928 | if (LM>st->mode->maxLM) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 929 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 930 | M=1<<LM; |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 931 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 932 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 933 | prefilter_mem = st->in_mem+CC*(st->overlap); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 934 | oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD)); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 935 | oldLogE = oldBandE + CC*st->mode->nbEBands; |
| 936 | oldLogE2 = oldLogE + CC*st->mode->nbEBands; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 937 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 938 | if (enc==NULL) |
| 939 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 940 | tell=1; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 941 | nbFilledBytes=0; |
| 942 | } else { |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 943 | tell=ec_tell(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 944 | nbFilledBytes=(tell+4)>>3; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 945 | } |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 946 | |
| 947 | if (st->signalling && enc==NULL) |
| 948 | { |
| 949 | int tmp = (st->mode->effEBands-st->end)>>1; |
| 950 | st->end = IMAX(1, st->mode->effEBands-tmp); |
| 951 | compressed[0] = tmp<<5; |
| 952 | compressed[0] |= LM<<3; |
| 953 | compressed[0] |= (C==2)<<2; |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 954 | /* Convert "standard mode" to Opus header */ |
| 955 | if (st->mode->Fs==48000 && st->mode->shortMdctSize==120) |
| 956 | { |
| 957 | int c0 = toOpus(compressed[0]); |
| 958 | if (c0<0) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 959 | return OPUS_BAD_ARG; |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 960 | compressed[0] = c0; |
| 961 | } |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 962 | compressed++; |
| 963 | nbCompressedBytes--; |
| 964 | } |
| 965 | |
| 966 | /* Can't produce more than 1275 output bytes */ |
| 967 | nbCompressedBytes = IMIN(nbCompressedBytes,1275); |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 968 | nbAvailableBytes = nbCompressedBytes - nbFilledBytes; |
| 969 | |
Jean-Marc Valin | 66ff26f | 2011-08-15 22:37:11 -0400 | [diff] [blame] | 970 | if (st->vbr && st->bitrate!=-1) |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 971 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 972 | opus_int32 den=st->mode->Fs>>BITRES; |
Jean-Marc Valin | 23340e2 | 2011-02-03 23:21:00 -0500 | [diff] [blame] | 973 | vbr_rate=(st->bitrate*frame_size+(den>>1))/den; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 974 | if (st->signalling) |
| 975 | vbr_rate -= 8<<BITRES; |
Jean-Marc Valin | 3beb70e | 2011-03-01 18:08:15 -0500 | [diff] [blame] | 976 | effectiveBytes = vbr_rate>>(3+BITRES); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 977 | } else { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 978 | opus_int32 tmp; |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 979 | vbr_rate = 0; |
| 980 | tmp = st->bitrate*frame_size; |
| 981 | if (tell>1) |
| 982 | tmp += tell; |
Jean-Marc Valin | 66ff26f | 2011-08-15 22:37:11 -0400 | [diff] [blame] | 983 | if (st->bitrate!=-1) |
| 984 | nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes, |
| 985 | (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling)); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 986 | effectiveBytes = nbCompressedBytes; |
| 987 | } |
| 988 | |
| 989 | if (enc==NULL) |
| 990 | { |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 991 | ec_enc_init(&_enc, compressed, nbCompressedBytes); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 992 | enc = &_enc; |
| 993 | } |
| 994 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 995 | if (vbr_rate>0) |
| 996 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 997 | /* Computes the max bit-rate allowed in VBR mode to avoid violating the |
| 998 | target rate and buffering. |
| 999 | We must do this up front so that bust-prevention logic triggers |
| 1000 | correctly if we don't have enough bits. */ |
| 1001 | if (st->constrained_vbr) |
| 1002 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1003 | opus_int32 vbr_bound; |
| 1004 | opus_int32 max_allowed; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1005 | /* We could use any multiple of vbr_rate as bound (depending on the |
| 1006 | delay). |
Gregory Maxwell | 420c325 | 2011-01-27 22:35:50 -0500 | [diff] [blame] | 1007 | This is clamped to ensure we use at least two bytes if the encoder |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1008 | was entirely empty, but to allow 0 in hybrid mode. */ |
| 1009 | vbr_bound = vbr_rate; |
Gregory Maxwell | 420c325 | 2011-01-27 22:35:50 -0500 | [diff] [blame] | 1010 | max_allowed = IMIN(IMAX(tell==1?2:0, |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 1011 | (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)), |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1012 | nbAvailableBytes); |
| 1013 | if(max_allowed < nbAvailableBytes) |
| 1014 | { |
| 1015 | nbCompressedBytes = nbFilledBytes+max_allowed; |
| 1016 | nbAvailableBytes = max_allowed; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1017 | ec_enc_shrink(enc, nbCompressedBytes); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1018 | } |
| 1019 | } |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1020 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1021 | total_bits = nbCompressedBytes*8; |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1022 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1023 | effEnd = st->end; |
| 1024 | if (effEnd > st->mode->effEBands) |
| 1025 | effEnd = st->mode->effEBands; |
| 1026 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1027 | ALLOC(in, CC*(N+st->overlap), celt_sig); |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1028 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1029 | /* Find pitch period and gain */ |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 1030 | { |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1031 | VARDECL(celt_sig, _pre); |
| 1032 | celt_sig *pre[2]; |
| 1033 | SAVE_STACK; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1034 | ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1035 | |
| 1036 | pre[0] = _pre; |
| 1037 | pre[1] = _pre + (N+COMBFILTER_MAXPERIOD); |
| 1038 | |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1039 | silence = 1; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1040 | c=0; do { |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1041 | int count = 0; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1042 | const opus_val16 * restrict pcmp = pcm+c; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1043 | celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap; |
| 1044 | |
| 1045 | for (i=0;i<N;i++) |
| 1046 | { |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1047 | celt_sig x, tmp; |
| 1048 | |
| 1049 | x = SCALEIN(*pcmp); |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 1050 | #ifndef FIXED_POINT |
Gregory Maxwell | 58ecb1a | 2011-05-09 13:16:30 -0400 | [diff] [blame] | 1051 | if (!(x==x)) |
| 1052 | x = 0; |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 1053 | if (st->clip) |
| 1054 | x = MAX32(-65536.f, MIN32(65536.f,x)); |
| 1055 | #endif |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1056 | if (++count==st->upsample) |
| 1057 | { |
| 1058 | count=0; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1059 | pcmp+=CC; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1060 | } else { |
| 1061 | x = 0; |
| 1062 | } |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1063 | /* Apply pre-emphasis */ |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1064 | tmp = MULT16_16(st->mode->preemph[2], x); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1065 | *inp = tmp + st->preemph_memE[c]; |
| 1066 | st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp) |
| 1067 | - MULT16_32_Q15(st->mode->preemph[0], tmp); |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1068 | silence = silence && *inp == 0; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1069 | inp++; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1070 | } |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1071 | OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD); |
| 1072 | OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1073 | } while (++c<CC); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1074 | |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 1075 | #ifdef FUZZING |
| 1076 | if ((rand()&0x3F)==0) |
| 1077 | silence = 1; |
| 1078 | #endif |
Jean-Marc Valin | 65d35a3 | 2011-01-26 22:04:59 -0500 | [diff] [blame] | 1079 | if (tell==1) |
| 1080 | ec_enc_bit_logp(enc, silence, 15); |
| 1081 | else |
| 1082 | silence=0; |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1083 | if (silence) |
| 1084 | { |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1085 | /*In VBR mode there is no need to send more than the minimum. */ |
| 1086 | if (vbr_rate>0) |
| 1087 | { |
Jean-Marc Valin | 65d35a3 | 2011-01-26 22:04:59 -0500 | [diff] [blame] | 1088 | effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2); |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1089 | total_bits=nbCompressedBytes*8; |
| 1090 | nbAvailableBytes=2; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1091 | ec_enc_shrink(enc, nbCompressedBytes); |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1092 | } |
Jean-Marc Valin | 13a7c26 | 2011-01-26 10:58:33 -0500 | [diff] [blame] | 1093 | /* Pretend we've filled all the remaining bits with zeros |
| 1094 | (that's what the initialiser did anyway) */ |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1095 | tell = nbCompressedBytes*8; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1096 | enc->nbits_total+=tell-ec_tell(enc); |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1097 | } |
Jean-Marc Valin | d539c6b | 2011-02-03 13:36:03 -0500 | [diff] [blame] | 1098 | if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1099 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1100 | VARDECL(opus_val16, pitch_buf); |
| 1101 | ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1102 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1103 | pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC); |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 1104 | pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N, |
Jean-Marc Valin | e3e2c26 | 2011-01-26 13:09:53 -0500 | [diff] [blame] | 1105 | COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1106 | pitch_index = COMBFILTER_MAXPERIOD-pitch_index; |
| 1107 | |
| 1108 | gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD, |
| 1109 | N, &pitch_index, st->prefilter_period, st->prefilter_gain); |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1110 | if (pitch_index > COMBFILTER_MAXPERIOD-2) |
| 1111 | pitch_index = COMBFILTER_MAXPERIOD-2; |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 1112 | gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1); |
Jean-Marc Valin | 6965388 | 2011-04-21 10:41:13 -0400 | [diff] [blame] | 1113 | if (st->loss_rate>2) |
| 1114 | gain1 = HALF32(gain1); |
| 1115 | if (st->loss_rate>4) |
| 1116 | gain1 = HALF32(gain1); |
| 1117 | if (st->loss_rate>8) |
| 1118 | gain1 = 0; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 1119 | prefilter_tapset = st->tapset_decision; |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 1120 | } else { |
| 1121 | gain1 = 0; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1122 | } |
Jean-Marc Valin | 6ba0b35 | 2010-12-20 11:40:30 -0500 | [diff] [blame] | 1123 | |
| 1124 | /* Gain threshold for enabling the prefilter/postfilter */ |
| 1125 | pf_threshold = QCONST16(.2f,15); |
| 1126 | |
| 1127 | /* Adjusting the threshold based on rate and continuity */ |
| 1128 | if (abs(pitch_index-st->prefilter_period)*10>pitch_index) |
| 1129 | pf_threshold += QCONST16(.2f,15); |
| 1130 | if (nbAvailableBytes<25) |
| 1131 | pf_threshold += QCONST16(.1f,15); |
| 1132 | if (nbAvailableBytes<35) |
| 1133 | pf_threshold += QCONST16(.1f,15); |
| 1134 | if (st->prefilter_gain > QCONST16(.4f,15)) |
| 1135 | pf_threshold -= QCONST16(.1f,15); |
| 1136 | if (st->prefilter_gain > QCONST16(.55f,15)) |
| 1137 | pf_threshold -= QCONST16(.1f,15); |
| 1138 | |
| 1139 | /* Hard threshold at 0.2 */ |
| 1140 | pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15)); |
| 1141 | if (gain1<pf_threshold) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1142 | { |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1143 | if(st->start==0 && tell+16<=total_bits) |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1144 | ec_enc_bit_logp(enc, 0, 1); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1145 | gain1 = 0; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 1146 | pf_on = 0; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1147 | } else { |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1148 | /*This block is not gated by a total bits check only because |
| 1149 | of the nbAvailableBytes check above.*/ |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1150 | int qg; |
| 1151 | int octave; |
Jean-Marc Valin | 6ba0b35 | 2010-12-20 11:40:30 -0500 | [diff] [blame] | 1152 | |
Jean-Marc Valin | b417d83 | 2011-01-27 17:19:49 -0500 | [diff] [blame] | 1153 | if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15)) |
Jean-Marc Valin | 6ba0b35 | 2010-12-20 11:40:30 -0500 | [diff] [blame] | 1154 | gain1=st->prefilter_gain; |
| 1155 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1156 | #ifdef FIXED_POINT |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1157 | qg = ((gain1+1536)>>10)/3-1; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1158 | #else |
Gregory Maxwell | 662587d | 2011-08-01 20:41:54 -0400 | [diff] [blame] | 1159 | qg = (int)floor(.5f+gain1*32/3)-1; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1160 | #endif |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1161 | qg = IMAX(0, IMIN(7, qg)); |
Timothy B. Terriberry | e86fb26 | 2010-12-17 14:50:19 -0800 | [diff] [blame] | 1162 | ec_enc_bit_logp(enc, 1, 1); |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1163 | pitch_index += 1; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1164 | octave = EC_ILOG(pitch_index)-5; |
| 1165 | ec_enc_uint(enc, octave, 6); |
| 1166 | ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave); |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1167 | pitch_index -= 1; |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1168 | ec_enc_bits(enc, qg, 3); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1169 | if (ec_tell(enc)+2<=total_bits) |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1170 | ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2); |
| 1171 | else |
| 1172 | prefilter_tapset = 0; |
| 1173 | gain1 = QCONST16(0.09375f,15)*(qg+1); |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 1174 | pf_on = 1; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1175 | } |
| 1176 | /*printf("%d %f\n", pitch_index, gain1);*/ |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1177 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1178 | c=0; do { |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 1179 | int offset = st->mode->shortMdctSize-st->mode->overlap; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1180 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1181 | OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap); |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 1182 | if (offset) |
| 1183 | comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD, |
| 1184 | st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain, |
| 1185 | st->prefilter_tapset, st->prefilter_tapset, NULL, 0); |
| 1186 | |
| 1187 | comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset, |
| 1188 | st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1, |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1189 | st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap); |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1190 | OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1191 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1192 | if (N>COMBFILTER_MAXPERIOD) |
| 1193 | { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1194 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1195 | } else { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1196 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N); |
| 1197 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1198 | } |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1199 | } while (++c<CC); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1200 | |
| 1201 | RESTORE_STACK; |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 1202 | } |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 1203 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1204 | isTransient = 0; |
| 1205 | shortBlocks = 0; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1206 | if (LM>0 && ec_tell(enc)+3<=total_bits) |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1207 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1208 | if (st->complexity > 1) |
| 1209 | { |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1210 | isTransient = transient_analysis(in, N+st->overlap, CC, |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 1211 | st->overlap); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1212 | if (isTransient) |
| 1213 | shortBlocks = M; |
| 1214 | } |
| 1215 | ec_enc_bit_logp(enc, isTransient, 3); |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1216 | } |
Jean-Marc Valin | c5f2a9d | 2008-10-26 22:00:26 -0400 | [diff] [blame] | 1217 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1218 | ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 1219 | ALLOC(bandE,st->mode->nbEBands*CC, celt_ener); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1220 | ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16); |
Jean-Marc Valin | 32ec58c | 2009-05-01 21:28:58 -0400 | [diff] [blame] | 1221 | /* Compute MDCTs */ |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1222 | compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM); |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 1223 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1224 | if (CC==2&&C==1) |
| 1225 | { |
| 1226 | for (i=0;i<N;i++) |
| 1227 | freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i])); |
| 1228 | } |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1229 | if (st->upsample != 1) |
| 1230 | { |
| 1231 | c=0; do |
| 1232 | { |
| 1233 | int bound = N/st->upsample; |
| 1234 | for (i=0;i<bound;i++) |
| 1235 | freq[c*N+i] *= st->upsample; |
| 1236 | for (;i<N;i++) |
| 1237 | freq[c*N+i] = 0; |
| 1238 | } while (++c<C); |
| 1239 | } |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1240 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
Jean-Marc Valin | 8d4ac15 | 2008-02-29 17:24:02 +1100 | [diff] [blame] | 1241 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1242 | compute_band_energies(st->mode, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 504fb3c | 2010-08-06 15:56:22 -0400 | [diff] [blame] | 1243 | |
| 1244 | amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1245 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 1246 | /* Band normalisation */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1247 | normalise_bands(st->mode, freq, X, bandE, effEnd, C, M); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 1248 | |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 1249 | ALLOC(tf_res, st->mode->nbEBands, int); |
Gregory Maxwell | 06d57b2 | 2011-08-01 22:02:25 -0400 | [diff] [blame] | 1250 | tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum); |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 1251 | for (i=effEnd;i<st->end;i++) |
| 1252 | tf_res[i] = tf_res[effEnd-1]; |
| 1253 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1254 | ALLOC(error, C*st->mode->nbEBands, opus_val16); |
Jean-Marc Valin | 5e7f02d | 2010-08-08 09:48:22 -0400 | [diff] [blame] | 1255 | quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE, |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1256 | oldBandE, total_bits, error, enc, |
Timothy B. Terriberry | ef2e650 | 2010-11-09 01:43:18 -0800 | [diff] [blame] | 1257 | C, LM, nbAvailableBytes, st->force_intra, |
Jean-Marc Valin | eda2dee | 2011-04-21 16:04:27 -0400 | [diff] [blame] | 1258 | &st->delayedIntra, st->complexity >= 4, st->loss_rate); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1259 | |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1260 | tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc); |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1261 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1262 | st->spread_decision = SPREAD_NORMAL; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1263 | if (ec_tell(enc)+4<=total_bits) |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 1264 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1265 | if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C) |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1266 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1267 | if (st->complexity == 0) |
| 1268 | st->spread_decision = SPREAD_NONE; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1269 | } else { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1270 | st->spread_decision = spreading_decision(st->mode, X, |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 1271 | &st->tonal_average, st->spread_decision, &st->hf_average, |
| 1272 | &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M); |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1273 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1274 | ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5); |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 1275 | } |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1276 | |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 1277 | ALLOC(cap, st->mode->nbEBands, int); |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1278 | ALLOC(offsets, st->mode->nbEBands, int); |
| 1279 | |
Timothy B. Terriberry | ce6d090 | 2011-02-01 17:41:12 -0800 | [diff] [blame] | 1280 | init_caps(st->mode,cap,LM,C); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 1281 | for (i=0;i<st->mode->nbEBands;i++) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1282 | offsets[i] = 0; |
| 1283 | /* Dynamic allocation code */ |
| 1284 | /* Make sure that dynamic allocation can't make us bust the budget */ |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1285 | if (effectiveBytes > 50 && LM>=1) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1286 | { |
| 1287 | int t1, t2; |
| 1288 | if (LM <= 1) |
| 1289 | { |
| 1290 | t1 = 3; |
| 1291 | t2 = 5; |
| 1292 | } else { |
| 1293 | t1 = 2; |
| 1294 | t2 = 4; |
| 1295 | } |
Gregory Maxwell | 7007f1b | 2011-02-08 16:17:47 -0500 | [diff] [blame] | 1296 | for (i=st->start+1;i<st->end-1;i++) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1297 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1298 | opus_val32 d2; |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 1299 | d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1]; |
| 1300 | if (C==2) |
| 1301 | d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]- |
| 1302 | bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]); |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 1303 | #ifdef FUZZING |
| 1304 | if((rand()&0xF)==0) |
| 1305 | { |
| 1306 | offsets[i] += 1; |
| 1307 | if((rand()&0x3)==0) |
| 1308 | offsets[i] += 1+(rand()&0x3); |
| 1309 | } |
| 1310 | #else |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 1311 | if (d2 > SHL16(t1,DB_SHIFT)) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1312 | offsets[i] += 1; |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 1313 | if (d2 > SHL16(t2,DB_SHIFT)) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1314 | offsets[i] += 1; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 1315 | #endif |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1316 | } |
| 1317 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1318 | dynalloc_logp = 6; |
| 1319 | total_bits<<=BITRES; |
| 1320 | total_boost = 0; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1321 | tell = ec_tell_frac(enc); |
Timothy B. Terriberry | d6f6157 | 2010-12-30 09:04:16 -0800 | [diff] [blame] | 1322 | for (i=st->start;i<st->end;i++) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1323 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1324 | int width, quanta; |
| 1325 | int dynalloc_loop_logp; |
| 1326 | int boost; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1327 | int j; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1328 | width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM; |
| 1329 | /* quanta is 6 bits, but no more than 1 bit/sample |
| 1330 | and no less than 1/8 bit/sample */ |
| 1331 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); |
| 1332 | dynalloc_loop_logp = dynalloc_logp; |
| 1333 | boost = 0; |
| 1334 | for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 1335 | && boost < cap[i]; j++) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1336 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1337 | int flag; |
| 1338 | flag = j<offsets[i]; |
| 1339 | ec_enc_bit_logp(enc, flag, dynalloc_loop_logp); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1340 | tell = ec_tell_frac(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1341 | if (!flag) |
| 1342 | break; |
| 1343 | boost += quanta; |
| 1344 | total_boost += quanta; |
| 1345 | dynalloc_loop_logp = 1; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1346 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1347 | /* Making dynalloc more likely */ |
| 1348 | if (j) |
| 1349 | dynalloc_logp = IMAX(2, dynalloc_logp-1); |
| 1350 | offsets[i] = boost; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1351 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1352 | alloc_trim = 5; |
| 1353 | if (tell+(6<<BITRES) <= total_bits - total_boost) |
| 1354 | { |
| 1355 | alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE, |
Gregory Maxwell | 7007f1b | 2011-02-08 16:17:47 -0500 | [diff] [blame] | 1356 | st->end, LM, C, N); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1357 | ec_enc_icdf(enc, alloc_trim, trim_icdf, 7); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1358 | tell = ec_tell_frac(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1359 | } |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1360 | |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1361 | /* Variable bitrate */ |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1362 | if (vbr_rate>0) |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1363 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1364 | opus_val16 alpha; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1365 | opus_int32 delta; |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1366 | /* The target rate in 8th bits per frame */ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1367 | opus_int32 target; |
| 1368 | opus_int32 min_allowed; |
Gregory Maxwell | de0b977 | 2011-08-23 02:00:55 -0400 | [diff] [blame] | 1369 | int lm_diff = st->mode->maxLM - LM; |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1370 | |
Gregory Maxwell | de0b977 | 2011-08-23 02:00:55 -0400 | [diff] [blame] | 1371 | target = vbr_rate + (st->vbr_offset>>lm_diff) - ((40*C+20)<<BITRES); |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1372 | |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1373 | /* Shortblocks get a large boost in bitrate, but since they |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 1374 | are uncommon long blocks are not greatly affected */ |
| 1375 | if (shortBlocks || tf_sum < -2*(st->end-st->start)) |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1376 | target = 7*target/4; |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 1377 | else if (tf_sum < -(st->end-st->start)) |
| 1378 | target = 3*target/2; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1379 | else if (M > 1) |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 1380 | target-=(target+14)/28; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1381 | |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1382 | /* The current offset is removed from the target and the space used |
| 1383 | so far is added*/ |
Jean-Marc Valin | a4badac | 2010-12-03 15:20:11 -0500 | [diff] [blame] | 1384 | target=target+tell; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1385 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1386 | /* In VBR mode the frame size must not be reduced so much that it would |
| 1387 | result in the encoder running out of bits. |
| 1388 | The margin of 2 bytes ensures that none of the bust-prevention logic |
| 1389 | in the decoder will have triggered so far. */ |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 1390 | min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes; |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1391 | |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 1392 | nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3); |
Gregory Maxwell | d85018c | 2011-01-13 14:38:24 -0500 | [diff] [blame] | 1393 | nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes); |
| 1394 | nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes; |
Jean-Marc Valin | 6b56526 | 2011-01-12 11:27:03 -0500 | [diff] [blame] | 1395 | |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1396 | /* By how much did we "miss" the target on that frame */ |
| 1397 | delta = target - vbr_rate; |
| 1398 | |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1399 | target=nbAvailableBytes<<(BITRES+3); |
| 1400 | |
Gregory Maxwell | fdd8675 | 2011-04-13 17:08:22 -0400 | [diff] [blame] | 1401 | /*If the frame is silent we don't adjust our drift, otherwise |
| 1402 | the encoder will shoot to very high rates after hitting a |
| 1403 | span of silence, but we do allow the bitres to refill. |
| 1404 | This means that we'll undershoot our target in CVBR/VBR modes |
| 1405 | on files with lots of silence. */ |
| 1406 | if(silence) |
| 1407 | { |
| 1408 | nbAvailableBytes = 2; |
| 1409 | target = 2*8<<BITRES; |
| 1410 | delta = 0; |
| 1411 | } |
| 1412 | |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1413 | if (st->vbr_count < 970) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1414 | { |
| 1415 | st->vbr_count++; |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1416 | alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16)); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1417 | } else |
| 1418 | alpha = QCONST16(.001f,15); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1419 | /* How many bits have we used in excess of what we're allowed */ |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 1420 | if (st->constrained_vbr) |
| 1421 | st->vbr_reservoir += target - vbr_rate; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1422 | /*printf ("%d\n", st->vbr_reservoir);*/ |
| 1423 | |
| 1424 | /* Compute the offset we need to apply in order to reach the target */ |
Gregory Maxwell | de0b977 | 2011-08-23 02:00:55 -0400 | [diff] [blame] | 1425 | st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta<<lm_diff)-st->vbr_offset-st->vbr_drift); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1426 | st->vbr_offset = -st->vbr_drift; |
| 1427 | /*printf ("%d\n", st->vbr_drift);*/ |
| 1428 | |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 1429 | if (st->constrained_vbr && st->vbr_reservoir < 0) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1430 | { |
| 1431 | /* We're under the min value -- increase rate */ |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1432 | int adjust = (-st->vbr_reservoir)/(8<<BITRES); |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1433 | /* Unless we're just coding silence */ |
| 1434 | nbAvailableBytes += silence?0:adjust; |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1435 | st->vbr_reservoir = 0; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1436 | /*printf ("+%d\n", adjust);*/ |
| 1437 | } |
Gregory Maxwell | d85018c | 2011-01-13 14:38:24 -0500 | [diff] [blame] | 1438 | nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1439 | /* This moves the raw bits to take into account the new compressed size */ |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1440 | ec_enc_shrink(enc, nbCompressedBytes); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1441 | } |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1442 | if (C==2) |
| 1443 | { |
| 1444 | int effectiveRate; |
| 1445 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1446 | /* Always use MS for 2.5 ms frames until we can do a better analysis */ |
| 1447 | if (LM!=0) |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 1448 | dual_stereo = stereo_analysis(st->mode, X, LM, N); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1449 | |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1450 | /* Account for coarse energy */ |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1451 | effectiveRate = (8*effectiveBytes - 80)>>LM; |
| 1452 | |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1453 | /* effectiveRate in kb/s */ |
| 1454 | effectiveRate = 2*effectiveRate/5; |
| 1455 | if (effectiveRate<35) |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 1456 | intensity = 8; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1457 | else if (effectiveRate<50) |
| 1458 | intensity = 12; |
| 1459 | else if (effectiveRate<68) |
| 1460 | intensity = 16; |
| 1461 | else if (effectiveRate<84) |
| 1462 | intensity = 18; |
| 1463 | else if (effectiveRate<102) |
| 1464 | intensity = 19; |
| 1465 | else if (effectiveRate<130) |
| 1466 | intensity = 20; |
| 1467 | else |
| 1468 | intensity = 100; |
| 1469 | intensity = IMIN(st->end,IMAX(st->start, intensity)); |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1470 | } |
| 1471 | |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1472 | /* Bit allocation */ |
| 1473 | ALLOC(fine_quant, st->mode->nbEBands, int); |
| 1474 | ALLOC(pulses, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1475 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 1476 | |
Timothy B. Terriberry | 285bc37 | 2011-02-06 13:29:00 -0800 | [diff] [blame] | 1477 | /* bits = packet size - where we are - safety*/ |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 1478 | bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1; |
| 1479 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 1480 | bits -= anti_collapse_rsv; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 1481 | codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 1482 | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, |
| 1483 | fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands); |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 1484 | st->lastCodedBands = codedBands; |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 1485 | |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 1486 | quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1487 | |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 1488 | #ifdef MEASURE_NORM_MSE |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 1489 | float X0[3000]; |
| 1490 | float bandE0[60]; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1491 | c=0; do |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 1492 | for (i=0;i<N;i++) |
| 1493 | X0[i+c*N] = X[i+c*N]; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1494 | while (++c<C); |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 1495 | for (i=0;i<C*st->mode->nbEBands;i++) |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 1496 | bandE0[i] = bandE[i]; |
| 1497 | #endif |
| 1498 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 1499 | /* Residual quantisation */ |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 1500 | ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char); |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 1501 | quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, |
Gregory Maxwell | d830d08 | 2011-08-15 16:05:40 -0400 | [diff] [blame] | 1502 | bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 1503 | nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng); |
Jean-Marc Valin | 3971053 | 2009-06-09 00:10:32 -0400 | [diff] [blame] | 1504 | |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 1505 | if (anti_collapse_rsv > 0) |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1506 | { |
| 1507 | anti_collapse_on = st->consec_transient<2; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 1508 | #ifdef FUZZING |
| 1509 | anti_collapse_on = rand()&0x1; |
| 1510 | #endif |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1511 | ec_enc_bits(enc, anti_collapse_on, 1); |
| 1512 | } |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1513 | quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C); |
Jean-Marc Valin | 3971053 | 2009-06-09 00:10:32 -0400 | [diff] [blame] | 1514 | |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1515 | if (silence) |
| 1516 | { |
| 1517 | for (i=0;i<C*st->mode->nbEBands;i++) |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1518 | oldBandE[i] = -QCONST16(28.f,DB_SHIFT); |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1519 | } |
| 1520 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1521 | #ifdef RESYNTH |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 1522 | /* Re-synthesis of the coded audio if required */ |
Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 1523 | { |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1524 | celt_sig *out_mem[2]; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1525 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1526 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1527 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); |
Jean-Marc Valin | 4b000c3 | 2011-01-26 20:30:21 -0500 | [diff] [blame] | 1528 | if (silence) |
| 1529 | { |
| 1530 | for (i=0;i<C*st->mode->nbEBands;i++) |
| 1531 | bandE[i] = 0; |
| 1532 | } |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 1533 | |
| 1534 | #ifdef MEASURE_NORM_MSE |
| 1535 | measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C); |
| 1536 | #endif |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1537 | if (anti_collapse_on) |
| 1538 | { |
Timothy B. Terriberry | 682b6cf | 2011-01-31 13:34:54 -0800 | [diff] [blame] | 1539 | anti_collapse(st->mode, X, collapse_masks, LM, C, CC, N, |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 1540 | st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1541 | } |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 1542 | |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1543 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1544 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1545 | |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1546 | OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1547 | if (CC==2) |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1548 | OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1549 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1550 | c=0; do |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 1551 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 1552 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1553 | while (++c<C); |
| 1554 | c=0; do |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 1555 | for (i=M*st->mode->eBands[st->end];i<N;i++) |
| 1556 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1557 | while (++c<C); |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 1558 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1559 | if (CC==2&&C==1) |
| 1560 | { |
| 1561 | for (i=0;i<N;i++) |
| 1562 | freq[N+i] = freq[i]; |
| 1563 | } |
| 1564 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1565 | out_mem[0] = st->syn_mem[0]+MAX_PERIOD; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1566 | if (CC==2) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1567 | out_mem[1] = st->syn_mem[1]+MAX_PERIOD; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1568 | |
Gregory Maxwell | a45724e | 2011-07-29 11:53:45 -0400 | [diff] [blame] | 1569 | overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD; |
| 1570 | if (CC==2) |
| 1571 | overlap_mem[1] = overlap_mem[0] + st->overlap; |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1572 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1573 | compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 1574 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1575 | c=0; do { |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1576 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
| 1577 | st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD); |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 1578 | comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize, |
Jean-Marc Valin | a14e86d | 2011-02-11 16:27:27 -0500 | [diff] [blame] | 1579 | st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset, |
| 1580 | st->mode->window, st->overlap); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1581 | if (LM!=0) |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 1582 | comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize, |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1583 | st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset, |
| 1584 | st->mode->window, st->mode->overlap); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1585 | } while (++c<CC); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1586 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1587 | deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1588 | st->prefilter_period_old = st->prefilter_period; |
| 1589 | st->prefilter_gain_old = st->prefilter_gain; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1590 | st->prefilter_tapset_old = st->prefilter_tapset; |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 1591 | } |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1592 | #endif |
| 1593 | |
| 1594 | st->prefilter_period = pitch_index; |
| 1595 | st->prefilter_gain = gain1; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1596 | st->prefilter_tapset = prefilter_tapset; |
Jean-Marc Valin | a14e86d | 2011-02-11 16:27:27 -0500 | [diff] [blame] | 1597 | #ifdef RESYNTH |
| 1598 | if (LM!=0) |
| 1599 | { |
| 1600 | st->prefilter_period_old = st->prefilter_period; |
| 1601 | st->prefilter_gain_old = st->prefilter_gain; |
| 1602 | st->prefilter_tapset_old = st->prefilter_tapset; |
| 1603 | } |
| 1604 | #endif |
Gregory Maxwell | 54547f1 | 2009-02-16 18:56:44 -0500 | [diff] [blame] | 1605 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1606 | if (CC==2&&C==1) { |
| 1607 | for (i=0;i<st->mode->nbEBands;i++) |
| 1608 | oldBandE[st->mode->nbEBands+i]=oldBandE[i]; |
| 1609 | } |
| 1610 | |
Jean-Marc Valin | 5677e34 | 2011-01-13 16:15:53 -0500 | [diff] [blame] | 1611 | /* In case start or end were to change */ |
Jean-Marc Valin | 5c2ac2b | 2011-01-22 14:48:20 -0500 | [diff] [blame] | 1612 | c=0; do |
| 1613 | { |
| 1614 | for (i=0;i<st->start;i++) |
| 1615 | oldBandE[c*st->mode->nbEBands+i]=0; |
| 1616 | for (i=st->end;i<st->mode->nbEBands;i++) |
| 1617 | oldBandE[c*st->mode->nbEBands+i]=0; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1618 | } while (++c<CC); |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 1619 | if (!isTransient) |
| 1620 | { |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1621 | for (i=0;i<CC*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 1622 | oldLogE2[i] = oldLogE[i]; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1623 | for (i=0;i<CC*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 1624 | oldLogE[i] = oldBandE[i]; |
Jean-Marc Valin | 47e905d | 2011-01-27 18:05:47 -0500 | [diff] [blame] | 1625 | } else { |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1626 | for (i=0;i<CC*st->mode->nbEBands;i++) |
Jean-Marc Valin | 47e905d | 2011-01-27 18:05:47 -0500 | [diff] [blame] | 1627 | oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 1628 | } |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1629 | if (isTransient) |
| 1630 | st->consec_transient++; |
| 1631 | else |
| 1632 | st->consec_transient=0; |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 1633 | st->rng = enc->rng; |
Jean-Marc Valin | 5677e34 | 2011-01-13 16:15:53 -0500 | [diff] [blame] | 1634 | |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 1635 | /* If there's any room left (can only happen for very high rates), |
Jean-Marc Valin | 13a7c26 | 2011-01-26 10:58:33 -0500 | [diff] [blame] | 1636 | it's already filled with zeros */ |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1637 | ec_enc_done(enc); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1638 | |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 1639 | if (st->signalling) |
| 1640 | nbCompressedBytes++; |
| 1641 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1642 | RESTORE_STACK; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1643 | if (ec_get_error(enc)) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1644 | return OPUS_INTERNAL_ERROR; |
Jean-Marc Valin | 9d785af | 2010-07-18 09:42:05 -0400 | [diff] [blame] | 1645 | else |
| 1646 | return nbCompressedBytes; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 1647 | } |
| 1648 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1649 | |
| 1650 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1651 | #ifdef FIXED_POINT |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1652 | int celt_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1653 | { |
| 1654 | return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL); |
| 1655 | } |
| 1656 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1657 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1658 | int celt_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1659 | { |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 1660 | int j, ret, C, N; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1661 | VARDECL(opus_int16, in); |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 1662 | ALLOC_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1663 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1664 | if (pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1665 | return OPUS_BAD_ARG; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1666 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1667 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 1668 | N = frame_size; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1669 | ALLOC(in, C*N, opus_int16); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1670 | |
| 1671 | for (j=0;j<C*N;j++) |
| 1672 | in[j] = FLOAT2INT16(pcm[j]); |
| 1673 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1674 | ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1675 | #ifdef RESYNTH |
| 1676 | for (j=0;j<C*N;j++) |
| 1677 | ((float*)pcm)[j]=in[j]*(1.f/32768.f); |
| 1678 | #endif |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1679 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1680 | return ret; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1681 | } |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1682 | #endif /* DISABLE_FLOAT_API */ |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1683 | #else |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1684 | |
| 1685 | int celt_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1686 | { |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 1687 | int j, ret, C, N; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1688 | VARDECL(celt_sig, in); |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 1689 | ALLOC_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1690 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1691 | if (pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1692 | return OPUS_BAD_ARG; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1693 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1694 | C=CHANNELS(st->channels); |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 1695 | N=frame_size; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1696 | ALLOC(in, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1697 | for (j=0;j<C*N;j++) { |
| 1698 | in[j] = SCALEOUT(pcm[j]); |
| 1699 | } |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1700 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1701 | ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1702 | #ifdef RESYNTH |
| 1703 | for (j=0;j<C*N;j++) |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1704 | ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1705 | #endif |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1706 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1707 | return ret; |
| 1708 | } |
Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 1709 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1710 | int celt_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1711 | { |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1712 | return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL); |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1713 | } |
| 1714 | |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1715 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1716 | #endif |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1717 | int celt_encoder_ctl(CELTEncoder * restrict st, int request, ...) |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1718 | { |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1719 | va_list ap; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1720 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1721 | va_start(ap, request); |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1722 | switch (request) |
| 1723 | { |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1724 | case OPUS_SET_COMPLEXITY_REQUEST: |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1725 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1726 | int value = va_arg(ap, opus_int32); |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1727 | if (value<0 || value>10) |
| 1728 | goto bad_arg; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1729 | st->complexity = value; |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1730 | } |
| 1731 | break; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1732 | case CELT_SET_START_BAND_REQUEST: |
| 1733 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1734 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1735 | if (value<0 || value>=st->mode->nbEBands) |
| 1736 | goto bad_arg; |
| 1737 | st->start = value; |
| 1738 | } |
| 1739 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1740 | case CELT_SET_END_BAND_REQUEST: |
| 1741 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1742 | opus_int32 value = va_arg(ap, opus_int32); |
Timothy B. Terriberry | 8893e53 | 2010-12-30 08:56:49 -0800 | [diff] [blame] | 1743 | if (value<1 || value>st->mode->nbEBands) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1744 | goto bad_arg; |
| 1745 | st->end = value; |
| 1746 | } |
| 1747 | break; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1748 | case CELT_SET_PREDICTION_REQUEST: |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1749 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1750 | int value = va_arg(ap, opus_int32); |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1751 | if (value<0 || value>2) |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1752 | goto bad_arg; |
Jean-Marc Valin | d539c6b | 2011-02-03 13:36:03 -0500 | [diff] [blame] | 1753 | st->disable_pf = value<=1; |
| 1754 | st->force_intra = value==0; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1755 | } |
| 1756 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1757 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: |
Jean-Marc Valin | 6965388 | 2011-04-21 10:41:13 -0400 | [diff] [blame] | 1758 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1759 | int value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 6965388 | 2011-04-21 10:41:13 -0400 | [diff] [blame] | 1760 | if (value<0 || value>100) |
| 1761 | goto bad_arg; |
| 1762 | st->loss_rate = value; |
| 1763 | } |
| 1764 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1765 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: |
Jean-Marc Valin | 79b34eb | 2010-12-05 17:22:06 -0500 | [diff] [blame] | 1766 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1767 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 79b34eb | 2010-12-05 17:22:06 -0500 | [diff] [blame] | 1768 | st->constrained_vbr = value; |
| 1769 | } |
| 1770 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1771 | case OPUS_SET_VBR_REQUEST: |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1772 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1773 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1774 | st->vbr = value; |
| 1775 | } |
| 1776 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1777 | case OPUS_SET_BITRATE_REQUEST: |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1778 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1779 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 66ff26f | 2011-08-15 22:37:11 -0400 | [diff] [blame] | 1780 | if (value<=500 && value!=-1) |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1781 | goto bad_arg; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 1782 | value = IMIN(value, 260000*st->channels); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1783 | st->bitrate = value; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1784 | } |
| 1785 | break; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1786 | case CELT_SET_CHANNELS_REQUEST: |
| 1787 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1788 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1789 | if (value<1 || value>2) |
| 1790 | goto bad_arg; |
| 1791 | st->stream_channels = value; |
| 1792 | } |
| 1793 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1794 | case OPUS_RESET_STATE: |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1795 | { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1796 | OPUS_CLEAR((char*)&st->ENCODER_RESET_START, |
Jean-Marc Valin | 8cf29f0 | 2011-01-30 23:38:28 -0500 | [diff] [blame] | 1797 | celt_encoder_get_size_custom(st->mode, st->channels)- |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1798 | ((char*)&st->ENCODER_RESET_START - (char*)st)); |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1799 | st->vbr_offset = 0; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1800 | st->delayedIntra = 1; |
Timothy B. Terriberry | 320cf2e | 2010-12-17 05:52:06 -0800 | [diff] [blame] | 1801 | st->spread_decision = SPREAD_NORMAL; |
Gregory Maxwell | ec8008b | 2011-08-30 16:50:01 -0400 | [diff] [blame] | 1802 | st->tonal_average = 256; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1803 | } |
| 1804 | break; |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 1805 | case CELT_SET_INPUT_CLIPPING_REQUEST: |
| 1806 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1807 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 1808 | st->clip = value; |
| 1809 | } |
| 1810 | break; |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 1811 | #ifdef OPUS_BUILD |
| 1812 | case CELT_SET_SIGNALLING_REQUEST: |
| 1813 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1814 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 1815 | st->signalling = value; |
| 1816 | } |
| 1817 | break; |
| 1818 | case CELT_GET_MODE_REQUEST: |
| 1819 | { |
| 1820 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 1821 | if (value==0) |
| 1822 | goto bad_arg; |
| 1823 | *value=st->mode; |
| 1824 | } |
| 1825 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1826 | case OPUS_GET_FINAL_RANGE_REQUEST: |
Jean-Marc Valin | 06cee2b | 2011-08-19 16:11:41 -0400 | [diff] [blame] | 1827 | { |
| 1828 | opus_uint32 * value = va_arg(ap, opus_uint32 *); |
| 1829 | if (value==0) |
| 1830 | goto bad_arg; |
| 1831 | *value=st->rng; |
| 1832 | } |
| 1833 | break; |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 1834 | #endif |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1835 | default: |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1836 | goto bad_request; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1837 | } |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1838 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1839 | return OPUS_OK; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1840 | bad_arg: |
| 1841 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1842 | return OPUS_BAD_ARG; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1843 | bad_request: |
| 1844 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1845 | return OPUS_UNIMPLEMENTED; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1846 | } |
| 1847 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 1848 | /**********************************************************************/ |
| 1849 | /* */ |
| 1850 | /* DECODER */ |
| 1851 | /* */ |
| 1852 | /**********************************************************************/ |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1853 | #define DECODE_BUFFER_SIZE 2048 |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1854 | |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1855 | /** Decoder state |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 1856 | @brief Decoder state |
| 1857 | */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1858 | struct CELTDecoder { |
| 1859 | const CELTMode *mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1860 | int overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1861 | int channels; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 1862 | int stream_channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1863 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1864 | int downsample; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1865 | int start, end; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 1866 | int signalling; |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1867 | |
| 1868 | /* Everything beyond this point gets cleared on a reset */ |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 1869 | #define DECODER_RESET_START rng |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1870 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1871 | opus_uint32 rng; |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 1872 | int error; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1873 | int last_pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1874 | int loss_count; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1875 | int postfilter_period; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1876 | int postfilter_period_old; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1877 | opus_val16 postfilter_gain; |
| 1878 | opus_val16 postfilter_gain_old; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1879 | int postfilter_tapset; |
| 1880 | int postfilter_tapset_old; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1881 | |
| 1882 | celt_sig preemph_memD[2]; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1883 | |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 1884 | celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1885 | /* opus_val16 lpc[], Size = channels*LPC_ORDER */ |
| 1886 | /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */ |
| 1887 | /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */ |
| 1888 | /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */ |
| 1889 | /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1890 | }; |
| 1891 | |
Jean-Marc Valin | 8cf29f0 | 2011-01-30 23:38:28 -0500 | [diff] [blame] | 1892 | int celt_decoder_get_size(int channels) |
| 1893 | { |
| 1894 | const CELTMode *mode = celt_mode_create(48000, 960, NULL); |
| 1895 | return celt_decoder_get_size_custom(mode, channels); |
| 1896 | } |
| 1897 | |
| 1898 | int celt_decoder_get_size_custom(const CELTMode *mode, int channels) |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1899 | { |
| 1900 | int size = sizeof(struct CELTDecoder) |
| 1901 | + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig) |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1902 | + channels*LPC_ORDER*sizeof(opus_val16) |
| 1903 | + 4*2*mode->nbEBands*sizeof(opus_val16); |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1904 | return size; |
| 1905 | } |
| 1906 | |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 1907 | CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error) |
| 1908 | { |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1909 | int ret; |
Jean-Marc Valin | 07f8840 | 2011-08-29 15:08:51 -0400 | [diff] [blame] | 1910 | CELTDecoder *st = (CELTDecoder *)opus_alloc(celt_decoder_get_size_custom(mode, channels)); |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1911 | ret = celt_decoder_init_custom(st, mode, channels); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1912 | if (ret != OPUS_OK) |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 1913 | { |
| 1914 | celt_decoder_destroy(st); |
| 1915 | st = NULL; |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1916 | if (error) |
| 1917 | *error = ret; |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 1918 | } |
| 1919 | return st; |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 1920 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1921 | |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1922 | int celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels) |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 1923 | { |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1924 | int ret; |
| 1925 | ret = celt_decoder_init_custom(st, celt_mode_create(48000, 960, NULL), channels); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1926 | if (ret != OPUS_OK) |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1927 | return ret; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1928 | st->downsample = resampling_factor(sampling_rate); |
| 1929 | if (st->downsample==0) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1930 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1931 | else |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1932 | return OPUS_OK; |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 1933 | } |
| 1934 | |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1935 | int celt_decoder_init_custom(CELTDecoder *st, const CELTMode *mode, int channels) |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 1936 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1937 | if (channels < 0 || channels > 2) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1938 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1939 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1940 | if (st==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1941 | return OPUS_ALLOC_FAIL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1942 | |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1943 | OPUS_CLEAR((char*)st, celt_decoder_get_size_custom(mode, channels)); |
Jean-Marc Valin | 6d3829f | 2010-08-27 17:52:38 -0400 | [diff] [blame] | 1944 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1945 | st->mode = mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1946 | st->overlap = mode->overlap; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 1947 | st->stream_channels = st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1948 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1949 | st->downsample = 1; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1950 | st->start = 0; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 1951 | st->end = st->mode->effEBands; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 1952 | st->signalling = 1; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1953 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1954 | st->loss_count = 0; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1955 | |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 1956 | return OPUS_OK; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1957 | } |
| 1958 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 1959 | void celt_decoder_destroy(CELTDecoder *st) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1960 | { |
Jean-Marc Valin | 07f8840 | 2011-08-29 15:08:51 -0400 | [diff] [blame] | 1961 | opus_free(st); |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1962 | } |
| 1963 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1964 | static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM) |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1965 | { |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1966 | int c; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1967 | int pitch_index; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1968 | int overlap = st->mode->overlap; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1969 | opus_val16 fade = Q15ONE; |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1970 | int i, len; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1971 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1972 | int offset; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1973 | celt_sig *out_mem[2]; |
| 1974 | celt_sig *decode_mem[2]; |
| 1975 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1976 | opus_val16 *lpc; |
| 1977 | opus_val32 *out_syn[2]; |
| 1978 | opus_val16 *oldBandE, *oldLogE2, *backgroundLogE; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 1979 | int plc=1; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1980 | SAVE_STACK; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1981 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1982 | c=0; do { |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1983 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); |
| 1984 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 1985 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1986 | } while (++c<C); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1987 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C); |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 1988 | oldBandE = lpc+C*LPC_ORDER; |
| 1989 | oldLogE2 = oldBandE + C*st->mode->nbEBands; |
| 1990 | backgroundLogE = oldLogE2 + C*st->mode->nbEBands; |
| 1991 | |
| 1992 | out_syn[0] = out_mem[0]+MAX_PERIOD-N; |
| 1993 | if (C==2) |
| 1994 | out_syn[1] = out_mem[1]+MAX_PERIOD-N; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1995 | |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1996 | len = N+st->mode->overlap; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1997 | |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 1998 | if (st->loss_count >= 5) |
| 1999 | { |
| 2000 | VARDECL(celt_sig, freq); |
| 2001 | VARDECL(celt_norm, X); |
| 2002 | VARDECL(celt_ener, bandE); |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2003 | opus_uint32 seed; |
Jean-Marc Valin | 1a8bf37 | 2011-03-16 22:03:15 -0400 | [diff] [blame] | 2004 | int effEnd; |
| 2005 | |
| 2006 | effEnd = st->end; |
| 2007 | if (effEnd > st->mode->effEBands) |
| 2008 | effEnd = st->mode->effEBands; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2009 | |
| 2010 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 2011 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 2012 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener); |
| 2013 | |
| 2014 | log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C); |
| 2015 | |
| 2016 | seed = st->rng; |
Timothy B. Terriberry | 115fa35 | 2011-03-02 15:28:08 -0800 | [diff] [blame] | 2017 | for (c=0;c<C;c++) |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2018 | { |
Jean-Marc Valin | 1a8bf37 | 2011-03-16 22:03:15 -0400 | [diff] [blame] | 2019 | for (i=0;i<(st->mode->eBands[st->start]<<LM);i++) |
| 2020 | X[c*N+i] = 0; |
Timothy B. Terriberry | 115fa35 | 2011-03-02 15:28:08 -0800 | [diff] [blame] | 2021 | for (i=0;i<st->mode->effEBands;i++) |
| 2022 | { |
| 2023 | int j; |
| 2024 | int boffs; |
| 2025 | int blen; |
| 2026 | boffs = N*c+(st->mode->eBands[i]<<LM); |
| 2027 | blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM; |
| 2028 | for (j=0;j<blen;j++) |
| 2029 | { |
| 2030 | seed = lcg_rand(seed); |
Gregory Maxwell | ec8008b | 2011-08-30 16:50:01 -0400 | [diff] [blame] | 2031 | X[boffs+j] = (celt_norm)((opus_int32)seed>>20); |
Timothy B. Terriberry | 115fa35 | 2011-03-02 15:28:08 -0800 | [diff] [blame] | 2032 | } |
| 2033 | renormalise_vector(X+boffs, blen, Q15ONE); |
| 2034 | } |
Jean-Marc Valin | 1a8bf37 | 2011-03-16 22:03:15 -0400 | [diff] [blame] | 2035 | for (i=(st->mode->eBands[st->end]<<LM);i<N;i++) |
| 2036 | X[c*N+i] = 0; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2037 | } |
| 2038 | st->rng = seed; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2039 | |
Timothy B. Terriberry | 115fa35 | 2011-03-02 15:28:08 -0800 | [diff] [blame] | 2040 | denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM); |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2041 | |
Jean-Marc Valin | 1a8bf37 | 2011-03-16 22:03:15 -0400 | [diff] [blame] | 2042 | c=0; do |
| 2043 | for (i=0;i<st->mode->eBands[st->start]<<LM;i++) |
| 2044 | freq[c*N+i] = 0; |
| 2045 | while (++c<C); |
| 2046 | c=0; do { |
| 2047 | int bound = st->mode->eBands[effEnd]<<LM; |
| 2048 | if (st->downsample!=1) |
| 2049 | bound = IMIN(bound, N/st->downsample); |
| 2050 | for (i=bound;i<N;i++) |
| 2051 | freq[c*N+i] = 0; |
| 2052 | } while (++c<C); |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2053 | compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM); |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2054 | plc = 0; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2055 | } else if (st->loss_count == 0) |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 2056 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2057 | opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1]; |
Jean-Marc Valin | 5f3d1af | 2011-05-13 17:24:25 -0400 | [diff] [blame] | 2058 | /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this |
| 2059 | search by using only part of the decode buffer */ |
| 2060 | int poffset = 720; |
| 2061 | pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C); |
| 2062 | /* Max pitch is 100 samples (480 Hz) */ |
| 2063 | pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset, |
| 2064 | poffset-100, &pitch_index); |
| 2065 | pitch_index = poffset-pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 2066 | st->last_pitch_index = pitch_index; |
| 2067 | } else { |
| 2068 | pitch_index = st->last_pitch_index; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2069 | fade = QCONST16(.8f,15); |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 2070 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 2071 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2072 | if (plc) |
| 2073 | { |
| 2074 | c=0; do { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2075 | VARDECL(opus_val32, e); |
| 2076 | opus_val16 exc[MAX_PERIOD]; |
| 2077 | opus_val32 ac[LPC_ORDER+1]; |
| 2078 | opus_val16 decay = 1; |
| 2079 | opus_val32 S1=0; |
Gregory Maxwell | 06d57b2 | 2011-08-01 22:02:25 -0400 | [diff] [blame] | 2080 | opus_val16 mem[LPC_ORDER]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2081 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2082 | ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32); |
Jean-Marc Valin | f54a0a3 | 2011-05-13 17:36:31 -0400 | [diff] [blame] | 2083 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2084 | offset = MAX_PERIOD-pitch_index; |
| 2085 | for (i=0;i<MAX_PERIOD;i++) |
| 2086 | exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 2087 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2088 | if (st->loss_count == 0) |
| 2089 | { |
| 2090 | _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap, |
| 2091 | LPC_ORDER, MAX_PERIOD); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 2092 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2093 | /* Noise floor -40 dB */ |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2094 | #ifdef FIXED_POINT |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2095 | ac[0] += SHR32(ac[0],13); |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2096 | #else |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2097 | ac[0] *= 1.0001f; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2098 | #endif |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2099 | /* Lag windowing */ |
| 2100 | for (i=1;i<=LPC_ORDER;i++) |
| 2101 | { |
| 2102 | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2103 | #ifdef FIXED_POINT |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2104 | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2105 | #else |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2106 | ac[i] -= ac[i]*(.008f*i)*(.008f*i); |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2107 | #endif |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER); |
| 2111 | } |
| 2112 | for (i=0;i<LPC_ORDER;i++) |
| 2113 | mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT); |
| 2114 | fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem); |
| 2115 | /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/ |
| 2116 | /* Check if the waveform is decaying (and if so how fast) */ |
| 2117 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2118 | opus_val32 E1=1, E2=1; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2119 | int period; |
| 2120 | if (pitch_index <= MAX_PERIOD/2) |
| 2121 | period = pitch_index; |
| 2122 | else |
| 2123 | period = MAX_PERIOD/2; |
| 2124 | for (i=0;i<period;i++) |
| 2125 | { |
| 2126 | E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8); |
| 2127 | E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8); |
| 2128 | } |
| 2129 | if (E1 > E2) |
| 2130 | E1 = E2; |
| 2131 | decay = celt_sqrt(frac_div32(SHR(E1,1),E2)); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 2132 | } |
| 2133 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2134 | /* Copy excitation, taking decay into account */ |
| 2135 | for (i=0;i<len+st->mode->overlap;i++) |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2136 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2137 | opus_val16 tmp; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2138 | if (offset+i >= MAX_PERIOD) |
| 2139 | { |
| 2140 | offset -= pitch_index; |
| 2141 | decay = MULT16_16_Q15(decay, decay); |
| 2142 | } |
| 2143 | e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT); |
| 2144 | tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT); |
| 2145 | S1 += SHR32(MULT16_16(tmp,tmp),8); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2146 | } |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2147 | for (i=0;i<LPC_ORDER;i++) |
| 2148 | mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT); |
| 2149 | for (i=0;i<len+st->mode->overlap;i++) |
| 2150 | e[i] = MULT16_32_Q15(fade, e[i]); |
| 2151 | iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2152 | |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2153 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2154 | opus_val32 S2=0; |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 2155 | for (i=0;i<len+overlap;i++) |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2156 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2157 | opus_val16 tmp = ROUND16(e[i],SIG_SHIFT); |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2158 | S2 += SHR32(MULT16_16(tmp,tmp),8); |
| 2159 | } |
| 2160 | /* This checks for an "explosion" in the synthesis */ |
| 2161 | #ifdef FIXED_POINT |
| 2162 | if (!(S1 > SHR32(S2,2))) |
| 2163 | #else |
| 2164 | /* Float test is written this way to catch NaNs at the same time */ |
| 2165 | if (!(S1 > 0.2f*S2)) |
| 2166 | #endif |
| 2167 | { |
| 2168 | for (i=0;i<len+overlap;i++) |
| 2169 | e[i] = 0; |
| 2170 | } else if (S1 < S2) |
| 2171 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2172 | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2173 | for (i=0;i<len+overlap;i++) |
| 2174 | e[i] = MULT16_32_Q15(ratio, e[i]); |
| 2175 | } |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 2176 | } |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2177 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2178 | /* Apply post-filter to the MDCT overlap of the previous frame */ |
| 2179 | comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap, |
| 2180 | st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, |
| 2181 | NULL, 0); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2182 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2183 | for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++) |
| 2184 | out_mem[c][i] = out_mem[c][N+i]; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2185 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2186 | /* Apply TDAC to the concealed audio so that it blends with the |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2187 | previous and next frames */ |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2188 | for (i=0;i<overlap/2;i++) |
| 2189 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2190 | opus_val32 tmp; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2191 | tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) + |
| 2192 | MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]); |
| 2193 | out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp); |
| 2194 | out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp); |
| 2195 | } |
| 2196 | for (i=0;i<N;i++) |
| 2197 | out_mem[c][MAX_PERIOD-N+i] = e[i]; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2198 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2199 | /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */ |
| 2200 | comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap, |
| 2201 | -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, |
| 2202 | NULL, 0); |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2203 | for (i=0;i<overlap;i++) |
| 2204 | out_mem[c][MAX_PERIOD+i] = e[i]; |
| 2205 | } while (++c<C); |
| 2206 | } |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 2207 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 2208 | deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2209 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 2210 | st->loss_count++; |
| 2211 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 2212 | RESTORE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 2213 | } |
| 2214 | |
Jean-Marc Valin | 51c7862 | 2011-02-03 00:43:37 -0500 | [diff] [blame] | 2215 | CELT_STATIC |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2216 | int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, opus_val16 * restrict pcm, int frame_size, ec_dec *dec) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 2217 | { |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 2218 | int c, i, N; |
Timothy B. Terriberry | 320cf2e | 2010-12-17 05:52:06 -0800 | [diff] [blame] | 2219 | int spread_decision; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2220 | opus_int32 bits; |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 2221 | ec_dec _dec; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 2222 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 2223 | VARDECL(celt_norm, X); |
| 2224 | VARDECL(celt_ener, bandE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2225 | VARDECL(int, fine_quant); |
| 2226 | VARDECL(int, pulses); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2227 | VARDECL(int, cap); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2228 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 2229 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 2230 | VARDECL(int, tf_res); |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2231 | VARDECL(unsigned char, collapse_masks); |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2232 | celt_sig *out_mem[2]; |
| 2233 | celt_sig *decode_mem[2]; |
| 2234 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 2235 | celt_sig *out_syn[2]; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2236 | opus_val16 *lpc; |
| 2237 | opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 2238 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 2239 | int shortBlocks; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 2240 | int isTransient; |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 2241 | int intra_ener; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2242 | const int CC = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 2243 | int LM, M; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 2244 | int effEnd; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 2245 | int codedBands; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 2246 | int alloc_trim; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2247 | int postfilter_pitch; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2248 | opus_val16 postfilter_gain; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 2249 | int intensity=0; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 2250 | int dual_stereo=0; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2251 | opus_int32 total_bits; |
| 2252 | opus_int32 balance; |
| 2253 | opus_int32 tell; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2254 | int dynalloc_logp; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2255 | int postfilter_tapset; |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2256 | int anti_collapse_rsv; |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 2257 | int anti_collapse_on=0; |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2258 | int silence; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2259 | int C = CHANNELS(st->stream_channels); |
Jean-Marc Valin | f62b3bb | 2011-03-09 11:56:29 -0500 | [diff] [blame] | 2260 | ALLOC_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 2261 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 2262 | frame_size *= st->downsample; |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 2263 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2264 | c=0; do { |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2265 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); |
| 2266 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 2267 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2268 | } while (++c<CC); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2269 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC); |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2270 | oldBandE = lpc+LPC_ORDER; |
| 2271 | oldLogE = oldBandE + 2*st->mode->nbEBands; |
| 2272 | oldLogE2 = oldLogE + 2*st->mode->nbEBands; |
| 2273 | backgroundLogE = oldLogE2 + 2*st->mode->nbEBands; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2274 | |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2275 | if (st->signalling && data!=NULL) |
| 2276 | { |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 2277 | int data0=data[0]; |
| 2278 | /* Convert "standard mode" to Opus header */ |
| 2279 | if (st->mode->Fs==48000 && st->mode->shortMdctSize==120) |
| 2280 | { |
| 2281 | data0 = fromOpus(data0); |
| 2282 | if (data0<0) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2283 | return OPUS_CORRUPTED_DATA; |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 2284 | } |
| 2285 | st->end = IMAX(1, st->mode->effEBands-2*(data0>>5)); |
| 2286 | LM = (data0>>3)&0x3; |
| 2287 | C = 1 + ((data0>>2)&0x1); |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2288 | data++; |
| 2289 | len--; |
| 2290 | if (LM>st->mode->maxLM) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2291 | return OPUS_CORRUPTED_DATA; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2292 | if (frame_size < st->mode->shortMdctSize<<LM) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2293 | return OPUS_BUFFER_TOO_SMALL; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2294 | else |
| 2295 | frame_size = st->mode->shortMdctSize<<LM; |
| 2296 | } else { |
| 2297 | for (LM=0;LM<=st->mode->maxLM;LM++) |
| 2298 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 2299 | break; |
| 2300 | if (LM>st->mode->maxLM) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2301 | return OPUS_BAD_ARG; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2302 | } |
| 2303 | M=1<<LM; |
| 2304 | |
| 2305 | if (len<0 || len>1275 || pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2306 | return OPUS_BAD_ARG; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2307 | |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 2308 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 0141723 | 2008-03-03 13:59:55 +1100 | [diff] [blame] | 2309 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 2310 | effEnd = st->end; |
| 2311 | if (effEnd > st->mode->effEBands) |
| 2312 | effEnd = st->mode->effEBands; |
| 2313 | |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2314 | ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 2315 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 2316 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener); |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2317 | c=0; do |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2318 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 2319 | X[c*N+i] = 0; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2320 | while (++c<C); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2321 | c=0; do |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 2322 | for (i=M*st->mode->eBands[effEnd];i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 2323 | X[c*N+i] = 0; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2324 | while (++c<C); |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2325 | |
Jean-Marc Valin | 65d35a3 | 2011-01-26 22:04:59 -0500 | [diff] [blame] | 2326 | if (data == NULL || len<=1) |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 2327 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 2328 | celt_decode_lost(st, pcm, N, LM); |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 2329 | RESTORE_STACK; |
Jean-Marc Valin | 37e788c | 2011-03-16 20:56:28 -0400 | [diff] [blame] | 2330 | return frame_size/st->downsample; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 2331 | } |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2332 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 2333 | if (dec == NULL) |
| 2334 | { |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2335 | ec_dec_init(&_dec,(unsigned char*)data,len); |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 2336 | dec = &_dec; |
| 2337 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 2338 | |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2339 | if (C<CC) |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2340 | { |
| 2341 | for (i=0;i<st->mode->nbEBands;i++) |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2342 | oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]); |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2343 | } |
| 2344 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2345 | total_bits = len*8; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2346 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2347 | |
Jean-Marc Valin | a0653ed | 2011-07-05 13:18:59 -0400 | [diff] [blame] | 2348 | if (tell >= total_bits) |
| 2349 | silence = 1; |
| 2350 | else if (tell==1) |
Jean-Marc Valin | 65d35a3 | 2011-01-26 22:04:59 -0500 | [diff] [blame] | 2351 | silence = ec_dec_bit_logp(dec, 15); |
| 2352 | else |
| 2353 | silence = 0; |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2354 | if (silence) |
| 2355 | { |
Jean-Marc Valin | 13a7c26 | 2011-01-26 10:58:33 -0500 | [diff] [blame] | 2356 | /* Pretend we've read all the remaining bits */ |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2357 | tell = len*8; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2358 | dec->nbits_total+=tell-ec_tell(dec); |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2359 | } |
| 2360 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2361 | postfilter_gain = 0; |
| 2362 | postfilter_pitch = 0; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2363 | postfilter_tapset = 0; |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 2364 | if (st->start==0 && tell+16 <= total_bits) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2365 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2366 | if(ec_dec_bit_logp(dec, 1)) |
| 2367 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2368 | int qg, octave; |
| 2369 | octave = ec_dec_uint(dec, 6); |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2370 | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 2371 | qg = ec_dec_bits(dec, 3); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2372 | if (ec_tell(dec)+2<=total_bits) |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 2373 | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); |
| 2374 | postfilter_gain = QCONST16(.09375f,15)*(qg+1); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2375 | } |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2376 | tell = ec_tell(dec); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2377 | } |
| 2378 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2379 | if (LM > 0 && tell+3 <= total_bits) |
| 2380 | { |
Timothy B. Terriberry | e86fb26 | 2010-12-17 14:50:19 -0800 | [diff] [blame] | 2381 | isTransient = ec_dec_bit_logp(dec, 3); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2382 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2383 | } |
Jean-Marc Valin | f9fdbff | 2010-09-05 21:02:38 -0400 | [diff] [blame] | 2384 | else |
| 2385 | isTransient = 0; |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 2386 | |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 2387 | if (isTransient) |
| 2388 | shortBlocks = M; |
| 2389 | else |
| 2390 | shortBlocks = 0; |
| 2391 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2392 | /* Decode the global flags (first symbols in the stream) */ |
| 2393 | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; |
| 2394 | /* Get band energies */ |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 2395 | unquant_coarse_energy(st->mode, st->start, st->end, oldBandE, |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2396 | intra_ener, dec, C, LM); |
| 2397 | |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 2398 | ALLOC(tf_res, st->mode->nbEBands, int); |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 2399 | tf_decode(st->start, st->end, isTransient, tf_res, LM, dec); |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 2400 | |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2401 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2402 | spread_decision = SPREAD_NORMAL; |
| 2403 | if (tell+4 <= total_bits) |
| 2404 | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 2405 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2406 | ALLOC(pulses, st->mode->nbEBands, int); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2407 | ALLOC(cap, st->mode->nbEBands, int); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2408 | ALLOC(offsets, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 2409 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2410 | |
Timothy B. Terriberry | ce6d090 | 2011-02-01 17:41:12 -0800 | [diff] [blame] | 2411 | init_caps(st->mode,cap,LM,C); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2412 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2413 | dynalloc_logp = 6; |
| 2414 | total_bits<<=BITRES; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2415 | tell = ec_tell_frac(dec); |
Timothy B. Terriberry | d6f6157 | 2010-12-30 09:04:16 -0800 | [diff] [blame] | 2416 | for (i=st->start;i<st->end;i++) |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 2417 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2418 | int width, quanta; |
| 2419 | int dynalloc_loop_logp; |
| 2420 | int boost; |
| 2421 | width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM; |
| 2422 | /* quanta is 6 bits, but no more than 1 bit/sample |
| 2423 | and no less than 1/8 bit/sample */ |
| 2424 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); |
| 2425 | dynalloc_loop_logp = dynalloc_logp; |
| 2426 | boost = 0; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2427 | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 2428 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2429 | int flag; |
| 2430 | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2431 | tell = ec_tell_frac(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2432 | if (!flag) |
| 2433 | break; |
| 2434 | boost += quanta; |
| 2435 | total_bits -= quanta; |
| 2436 | dynalloc_loop_logp = 1; |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 2437 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2438 | offsets[i] = boost; |
| 2439 | /* Making dynalloc more likely */ |
| 2440 | if (boost>0) |
| 2441 | dynalloc_logp = IMAX(2, dynalloc_logp-1); |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 2442 | } |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2443 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 2444 | ALLOC(fine_quant, st->mode->nbEBands, int); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2445 | alloc_trim = tell+(6<<BITRES) <= total_bits ? |
| 2446 | ec_dec_icdf(dec, trim_icdf, 7) : 5; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 2447 | |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 2448 | bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1; |
| 2449 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2450 | bits -= anti_collapse_rsv; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2451 | codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 2452 | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, |
| 2453 | fine_quant, fine_priority, C, LM, dec, 0, 0); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2454 | |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 2455 | unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C); |
Jean-Marc Valin | 827f931 | 2008-05-06 23:21:55 +1000 | [diff] [blame] | 2456 | |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 2457 | /* Decode fixed codebook */ |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 2458 | ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char); |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2459 | quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, |
Gregory Maxwell | d830d08 | 2011-08-15 16:05:40 -0400 | [diff] [blame] | 2460 | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 2461 | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng); |
Jean-Marc Valin | 746b2a8 | 2010-05-14 22:12:33 -0400 | [diff] [blame] | 2462 | |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2463 | if (anti_collapse_rsv > 0) |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 2464 | { |
| 2465 | anti_collapse_on = ec_dec_bits(dec, 1); |
| 2466 | } |
| 2467 | |
Jean-Marc Valin | e3e2c26 | 2011-01-26 13:09:53 -0500 | [diff] [blame] | 2468 | unquant_energy_finalise(st->mode, st->start, st->end, oldBandE, |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2469 | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 2470 | |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 2471 | if (anti_collapse_on) |
Gregory Maxwell | b288c50 | 2011-08-12 14:22:16 -0400 | [diff] [blame] | 2472 | anti_collapse(st->mode, X, collapse_masks, LM, C, CC, N, |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 2473 | st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 2474 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 2475 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 2476 | |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2477 | if (silence) |
| 2478 | { |
| 2479 | for (i=0;i<C*st->mode->nbEBands;i++) |
| 2480 | { |
| 2481 | bandE[i] = 0; |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 2482 | oldBandE[i] = -QCONST16(28.f,DB_SHIFT); |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2483 | } |
| 2484 | } |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 2485 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 2486 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 2487 | |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 2488 | OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N); |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2489 | if (CC==2) |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 2490 | OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 2491 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2492 | c=0; do |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2493 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 2494 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2495 | while (++c<C); |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 2496 | c=0; do { |
| 2497 | int bound = M*st->mode->eBands[effEnd]; |
| 2498 | if (st->downsample!=1) |
| 2499 | bound = IMIN(bound, N/st->downsample); |
Jean-Marc Valin | 23340e2 | 2011-02-03 23:21:00 -0500 | [diff] [blame] | 2500 | for (i=bound;i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 2501 | freq[c*N+i] = 0; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 2502 | } while (++c<C); |
Jean-Marc Valin | 3a0bc3d | 2010-02-21 15:10:22 -0500 | [diff] [blame] | 2503 | |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2504 | out_syn[0] = out_mem[0]+MAX_PERIOD-N; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2505 | if (CC==2) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2506 | out_syn[1] = out_mem[1]+MAX_PERIOD-N; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 2507 | |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2508 | if (CC==2&&C==1) |
| 2509 | { |
| 2510 | for (i=0;i<N;i++) |
| 2511 | freq[N+i] = freq[i]; |
| 2512 | } |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2513 | if (CC==1&&C==2) |
| 2514 | { |
| 2515 | for (i=0;i<N;i++) |
| 2516 | freq[i] = HALF32(ADD32(freq[i],freq[N+i])); |
| 2517 | } |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2518 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 2519 | /* Compute inverse MDCTs */ |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2520 | compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 2521 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2522 | c=0; do { |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 2523 | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); |
| 2524 | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 2525 | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize, |
Jean-Marc Valin | a14e86d | 2011-02-11 16:27:27 -0500 | [diff] [blame] | 2526 | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, |
| 2527 | st->mode->window, st->overlap); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 2528 | if (LM!=0) |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 2529 | comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize, |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2530 | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, |
| 2531 | st->mode->window, st->mode->overlap); |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 2532 | |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2533 | } while (++c<CC); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 2534 | st->postfilter_period_old = st->postfilter_period; |
| 2535 | st->postfilter_gain_old = st->postfilter_gain; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2536 | st->postfilter_tapset_old = st->postfilter_tapset; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2537 | st->postfilter_period = postfilter_pitch; |
| 2538 | st->postfilter_gain = postfilter_gain; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2539 | st->postfilter_tapset = postfilter_tapset; |
Jean-Marc Valin | a14e86d | 2011-02-11 16:27:27 -0500 | [diff] [blame] | 2540 | if (LM!=0) |
| 2541 | { |
| 2542 | st->postfilter_period_old = st->postfilter_period; |
| 2543 | st->postfilter_gain_old = st->postfilter_gain; |
| 2544 | st->postfilter_tapset_old = st->postfilter_tapset; |
| 2545 | } |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2546 | |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2547 | if (C==1) { |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2548 | for (i=0;i<st->mode->nbEBands;i++) |
| 2549 | oldBandE[st->mode->nbEBands+i]=oldBandE[i]; |
| 2550 | } |
| 2551 | |
Jean-Marc Valin | 5677e34 | 2011-01-13 16:15:53 -0500 | [diff] [blame] | 2552 | /* In case start or end were to change */ |
Jean-Marc Valin | 5c2ac2b | 2011-01-22 14:48:20 -0500 | [diff] [blame] | 2553 | c=0; do |
| 2554 | { |
| 2555 | for (i=0;i<st->start;i++) |
| 2556 | oldBandE[c*st->mode->nbEBands+i]=0; |
| 2557 | for (i=st->end;i<st->mode->nbEBands;i++) |
| 2558 | oldBandE[c*st->mode->nbEBands+i]=0; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2559 | } while (++c<2); |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2560 | if (!isTransient) |
| 2561 | { |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2562 | for (i=0;i<2*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2563 | oldLogE2[i] = oldLogE[i]; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2564 | for (i=0;i<2*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2565 | oldLogE[i] = oldBandE[i]; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2566 | for (i=0;i<2*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2567 | backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]); |
Jean-Marc Valin | 47e905d | 2011-01-27 18:05:47 -0500 | [diff] [blame] | 2568 | } else { |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2569 | for (i=0;i<2*st->mode->nbEBands;i++) |
Jean-Marc Valin | 47e905d | 2011-01-27 18:05:47 -0500 | [diff] [blame] | 2570 | oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2571 | } |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 2572 | st->rng = dec->rng; |
Jean-Marc Valin | 5677e34 | 2011-01-13 16:15:53 -0500 | [diff] [blame] | 2573 | |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2574 | deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2575 | st->loss_count = 0; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 2576 | RESTORE_STACK; |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 2577 | if (ec_tell(dec) > 8*len) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2578 | return OPUS_INTERNAL_ERROR; |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 2579 | if(ec_get_error(dec)) |
| 2580 | st->error = 1; |
| 2581 | return frame_size/st->downsample; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 2582 | } |
| 2583 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2584 | |
| 2585 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2586 | #ifdef FIXED_POINT |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2587 | int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size) |
| 2588 | { |
| 2589 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); |
| 2590 | } |
| 2591 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2592 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2593 | int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2594 | { |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 2595 | int j, ret, C, N; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2596 | VARDECL(opus_int16, out); |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 2597 | ALLOC_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 2598 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 2599 | if (pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2600 | return OPUS_BAD_ARG; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 2601 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 2602 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 2603 | N = frame_size; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2604 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2605 | ALLOC(out, C*N, opus_int16); |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2606 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2607 | if (ret>0) |
| 2608 | for (j=0;j<C*ret;j++) |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 2609 | pcm[j]=out[j]*(1.f/32768.f); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2610 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 2611 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2612 | return ret; |
| 2613 | } |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2614 | #endif /* DISABLE_FLOAT_API */ |
| 2615 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2616 | #else |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2617 | |
| 2618 | int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size) |
| 2619 | { |
| 2620 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); |
| 2621 | } |
| 2622 | |
| 2623 | int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2624 | { |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 2625 | int j, ret, C, N; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 2626 | VARDECL(celt_sig, out); |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 2627 | ALLOC_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 2628 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 2629 | if (pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2630 | return OPUS_BAD_ARG; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 2631 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 2632 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 2633 | N = frame_size; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 2634 | ALLOC(out, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2635 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2636 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2637 | |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2638 | if (ret>0) |
| 2639 | for (j=0;j<C*ret;j++) |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 2640 | pcm[j] = FLOAT2INT16 (out[j]); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2641 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 2642 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2643 | return ret; |
| 2644 | } |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2645 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2646 | #endif |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2647 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 2648 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2649 | int celt_decoder_ctl(CELTDecoder * restrict st, int request, ...) |
| 2650 | { |
| 2651 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 2652 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2653 | va_start(ap, request); |
| 2654 | switch (request) |
| 2655 | { |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2656 | case CELT_SET_START_BAND_REQUEST: |
| 2657 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2658 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2659 | if (value<0 || value>=st->mode->nbEBands) |
| 2660 | goto bad_arg; |
| 2661 | st->start = value; |
| 2662 | } |
| 2663 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 2664 | case CELT_SET_END_BAND_REQUEST: |
| 2665 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2666 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | 1928f8d | 2011-02-08 22:32:56 -0500 | [diff] [blame] | 2667 | if (value<1 || value>st->mode->nbEBands) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 2668 | goto bad_arg; |
| 2669 | st->end = value; |
| 2670 | } |
| 2671 | break; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2672 | case CELT_SET_CHANNELS_REQUEST: |
| 2673 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2674 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2675 | if (value<1 || value>2) |
| 2676 | goto bad_arg; |
| 2677 | st->stream_channels = value; |
| 2678 | } |
Jean-Marc Valin | dd2973d | 2011-03-11 17:46:02 -0500 | [diff] [blame] | 2679 | break; |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 2680 | case CELT_GET_AND_CLEAR_ERROR_REQUEST: |
| 2681 | { |
| 2682 | int *value = va_arg(ap, int*); |
| 2683 | if (value==NULL) |
| 2684 | goto bad_arg; |
| 2685 | *value=st->error; |
| 2686 | st->error = 0; |
| 2687 | } |
| 2688 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2689 | case OPUS_GET_LOOKAHEAD_REQUEST: |
Jean-Marc Valin | ff96b16 | 2011-03-21 11:32:50 -0400 | [diff] [blame] | 2690 | { |
| 2691 | int *value = va_arg(ap, int*); |
| 2692 | if (value==NULL) |
| 2693 | goto bad_arg; |
| 2694 | *value = st->overlap/st->downsample; |
| 2695 | } |
| 2696 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2697 | case OPUS_RESET_STATE: |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2698 | { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 2699 | OPUS_CLEAR((char*)&st->DECODER_RESET_START, |
Jean-Marc Valin | 8cf29f0 | 2011-01-30 23:38:28 -0500 | [diff] [blame] | 2700 | celt_decoder_get_size_custom(st->mode, st->channels)- |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 2701 | ((char*)&st->DECODER_RESET_START - (char*)st)); |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2702 | } |
| 2703 | break; |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 2704 | #ifdef OPUS_BUILD |
| 2705 | case CELT_GET_MODE_REQUEST: |
| 2706 | { |
| 2707 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 2708 | if (value==0) |
| 2709 | goto bad_arg; |
| 2710 | *value=st->mode; |
| 2711 | } |
| 2712 | break; |
| 2713 | case CELT_SET_SIGNALLING_REQUEST: |
| 2714 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2715 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 2716 | st->signalling = value; |
| 2717 | } |
| 2718 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2719 | case OPUS_GET_FINAL_RANGE_REQUEST: |
Jean-Marc Valin | 06cee2b | 2011-08-19 16:11:41 -0400 | [diff] [blame] | 2720 | { |
| 2721 | opus_uint32 * value = va_arg(ap, opus_uint32 *); |
| 2722 | if (value==0) |
| 2723 | goto bad_arg; |
| 2724 | *value=st->rng; |
| 2725 | } |
| 2726 | break; |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 2727 | #endif |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2728 | default: |
| 2729 | goto bad_request; |
| 2730 | } |
| 2731 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2732 | return OPUS_OK; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2733 | bad_arg: |
| 2734 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2735 | return OPUS_BAD_ARG; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2736 | bad_request: |
| 2737 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame^] | 2738 | return OPUS_UNIMPLEMENTED; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2739 | } |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 2740 | |
| 2741 | const char *celt_strerror(int error) |
| 2742 | { |
| 2743 | static const char *error_strings[8] = { |
| 2744 | "success", |
| 2745 | "invalid argument", |
Jean-Marc Valin | c5f9990 | 2011-03-18 15:47:53 -0400 | [diff] [blame] | 2746 | "buffer too small", |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 2747 | "internal error", |
| 2748 | "corrupted stream", |
| 2749 | "request not implemented", |
| 2750 | "invalid state", |
| 2751 | "memory allocation failed" |
| 2752 | }; |
| 2753 | if (error > 0 || error < -7) |
| 2754 | return "unknown error"; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2755 | else |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 2756 | return error_strings[-error]; |
| 2757 | } |