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