| File: | root/firefox-clang/media/libopus/celt/celt_decoder.c |
| Warning: | line 360, column 25 The right operand of '*' is a garbage value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* Copyright (c) 2007-2008 CSIRO | |||
| 2 | Copyright (c) 2007-2010 Xiph.Org Foundation | |||
| 3 | Copyright (c) 2008 Gregory Maxwell | |||
| 4 | Written by Jean-Marc Valin and Gregory Maxwell */ | |||
| 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: | |||
| 9 | ||||
| 10 | - Redistributions of source code must retain the above copyright | |||
| 11 | notice, this list of conditions and the following disclaimer. | |||
| 12 | ||||
| 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. | |||
| 16 | ||||
| 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 COPYRIGHT OWNER | |||
| 21 | OR 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 | ||||
| 30 | #ifdef HAVE_CONFIG_H | |||
| 31 | #include "config.h" | |||
| 32 | #endif | |||
| 33 | ||||
| 34 | #define CELT_DECODER_C | |||
| 35 | ||||
| 36 | #include "cpu_support.h" | |||
| 37 | #include "os_support.h" | |||
| 38 | #include "mdct.h" | |||
| 39 | #include <math.h> | |||
| 40 | #include "celt.h" | |||
| 41 | #include "pitch.h" | |||
| 42 | #include "bands.h" | |||
| 43 | #include "modes.h" | |||
| 44 | #include "entcode.h" | |||
| 45 | #include "quant_bands.h" | |||
| 46 | #include "rate.h" | |||
| 47 | #include "stack_alloc.h" | |||
| 48 | #include "mathops.h" | |||
| 49 | #include "float_cast.h" | |||
| 50 | #include <stdarg.h> | |||
| 51 | #include "celt_lpc.h" | |||
| 52 | #include "vq.h" | |||
| 53 | ||||
| 54 | #ifdef ENABLE_DEEP_PLC | |||
| 55 | #include "lpcnet.h" | |||
| 56 | #include "lpcnet_private.h" | |||
| 57 | #endif | |||
| 58 | ||||
| 59 | /* The maximum pitch lag to allow in the pitch-based PLC. It's possible to save | |||
| 60 | CPU time in the PLC pitch search by making this smaller than MAX_PERIOD. The | |||
| 61 | current value corresponds to a pitch of 66.67 Hz. */ | |||
| 62 | #define PLC_PITCH_LAG_MAX(720) (720) | |||
| 63 | /* The minimum pitch lag to allow in the pitch-based PLC. This corresponds to a | |||
| 64 | pitch of 480 Hz. */ | |||
| 65 | #define PLC_PITCH_LAG_MIN(100) (100) | |||
| 66 | ||||
| 67 | /**********************************************************************/ | |||
| 68 | /* */ | |||
| 69 | /* DECODER */ | |||
| 70 | /* */ | |||
| 71 | /**********************************************************************/ | |||
| 72 | #define DECODE_BUFFER_SIZE2048 2048 | |||
| 73 | ||||
| 74 | #define PLC_UPDATE_FRAMES4 4 | |||
| 75 | #define PLC_UPDATE_SAMPLES(4*FRAME_SIZE) (PLC_UPDATE_FRAMES4*FRAME_SIZE) | |||
| 76 | ||||
| 77 | /** Decoder state | |||
| 78 | @brief Decoder state | |||
| 79 | */ | |||
| 80 | struct OpusCustomDecoder { | |||
| 81 | const OpusCustomMode *mode; | |||
| 82 | int overlap; | |||
| 83 | int channels; | |||
| 84 | int stream_channels; | |||
| 85 | ||||
| 86 | int downsample; | |||
| 87 | int start, end; | |||
| 88 | int signalling; | |||
| 89 | int disable_inv; | |||
| 90 | int complexity; | |||
| 91 | int arch; | |||
| 92 | ||||
| 93 | /* Everything beyond this point gets cleared on a reset */ | |||
| 94 | #define DECODER_RESET_STARTrng rng | |||
| 95 | ||||
| 96 | opus_uint32 rng; | |||
| 97 | int error; | |||
| 98 | int last_pitch_index; | |||
| 99 | int loss_duration; | |||
| 100 | int skip_plc; | |||
| 101 | int postfilter_period; | |||
| 102 | int postfilter_period_old; | |||
| 103 | opus_val16 postfilter_gain; | |||
| 104 | opus_val16 postfilter_gain_old; | |||
| 105 | int postfilter_tapset; | |||
| 106 | int postfilter_tapset_old; | |||
| 107 | int prefilter_and_fold; | |||
| 108 | ||||
| 109 | celt_sig preemph_memD[2]; | |||
| 110 | ||||
| 111 | #ifdef ENABLE_DEEP_PLC | |||
| 112 | opus_int16 plc_pcm[PLC_UPDATE_SAMPLES(4*FRAME_SIZE)]; | |||
| 113 | int plc_fill; | |||
| 114 | float plc_preemphasis_mem; | |||
| 115 | #endif | |||
| 116 | ||||
| 117 | celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ | |||
| 118 | /* opus_val16 lpc[], Size = channels*CELT_LPC_ORDER */ | |||
| 119 | /* celt_glog oldEBands[], Size = 2*mode->nbEBands */ | |||
| 120 | /* celt_glog oldLogE[], Size = 2*mode->nbEBands */ | |||
| 121 | /* celt_glog oldLogE2[], Size = 2*mode->nbEBands */ | |||
| 122 | /* celt_glog backgroundLogE[], Size = 2*mode->nbEBands */ | |||
| 123 | }; | |||
| 124 | ||||
| 125 | #if defined(ENABLE_HARDENING1) || defined(ENABLE_ASSERTIONS1) | |||
| 126 | /* Make basic checks on the CELT state to ensure we don't end | |||
| 127 | up writing all over memory. */ | |||
| 128 | void validate_celt_decoder(CELTDecoderOpusCustomDecoder *st) | |||
| 129 | { | |||
| 130 | #ifndef CUSTOM_MODES | |||
| 131 | celt_assert(st->mode == opus_custom_mode_create(48000, 960, NULL)){if (!(st->mode == opus_custom_mode_create(48000, 960, ((void *)0)))) {celt_fatal("assertion failed: " "st->mode == opus_custom_mode_create(48000, 960, NULL)" , "/root/firefox-clang/media/libopus/celt/celt_decoder.c", 131 );}}; | |||
| 132 | celt_assert(st->overlap == 120){if (!(st->overlap == 120)) {celt_fatal("assertion failed: " "st->overlap == 120", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 132);}}; | |||
| 133 | celt_assert(st->end <= 21){if (!(st->end <= 21)) {celt_fatal("assertion failed: " "st->end <= 21", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 133);}}; | |||
| 134 | #else | |||
| 135 | /* From Section 4.3 in the spec: "The normal CELT layer uses 21 of those bands, | |||
| 136 | though Opus Custom (see Section 6.2) may use a different number of bands" | |||
| 137 | ||||
| 138 | Check if it's within the maximum number of Bark frequency bands instead */ | |||
| 139 | celt_assert(st->end <= 25){if (!(st->end <= 25)) {celt_fatal("assertion failed: " "st->end <= 25", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 139);}}; | |||
| 140 | #endif | |||
| 141 | celt_assert(st->channels == 1 || st->channels == 2){if (!(st->channels == 1 || st->channels == 2)) {celt_fatal ("assertion failed: " "st->channels == 1 || st->channels == 2" , "/root/firefox-clang/media/libopus/celt/celt_decoder.c", 141 );}}; | |||
| 142 | celt_assert(st->stream_channels == 1 || st->stream_channels == 2){if (!(st->stream_channels == 1 || st->stream_channels == 2)) {celt_fatal("assertion failed: " "st->stream_channels == 1 || st->stream_channels == 2" , "/root/firefox-clang/media/libopus/celt/celt_decoder.c", 142 );}}; | |||
| 143 | celt_assert(st->downsample > 0){if (!(st->downsample > 0)) {celt_fatal("assertion failed: " "st->downsample > 0", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 143);}}; | |||
| 144 | celt_assert(st->start == 0 || st->start == 17){if (!(st->start == 0 || st->start == 17)) {celt_fatal( "assertion failed: " "st->start == 0 || st->start == 17" , "/root/firefox-clang/media/libopus/celt/celt_decoder.c", 144 );}}; | |||
| 145 | celt_assert(st->start < st->end){if (!(st->start < st->end)) {celt_fatal("assertion failed: " "st->start < st->end", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 145);}}; | |||
| 146 | #ifdef OPUS_ARCHMASK7 | |||
| 147 | celt_assert(st->arch >= 0){if (!(st->arch >= 0)) {celt_fatal("assertion failed: " "st->arch >= 0", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 147);}}; | |||
| 148 | celt_assert(st->arch <= OPUS_ARCHMASK){if (!(st->arch <= 7)) {celt_fatal("assertion failed: " "st->arch <= OPUS_ARCHMASK", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 148);}}; | |||
| 149 | #endif | |||
| 150 | celt_assert(st->last_pitch_index <= PLC_PITCH_LAG_MAX){if (!(st->last_pitch_index <= (720))) {celt_fatal("assertion failed: " "st->last_pitch_index <= PLC_PITCH_LAG_MAX", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 150);}}; | |||
| 151 | celt_assert(st->last_pitch_index >= PLC_PITCH_LAG_MIN || st->last_pitch_index == 0){if (!(st->last_pitch_index >= (100) || st->last_pitch_index == 0)) {celt_fatal("assertion failed: " "st->last_pitch_index >= PLC_PITCH_LAG_MIN || st->last_pitch_index == 0" , "/root/firefox-clang/media/libopus/celt/celt_decoder.c", 151 );}}; | |||
| 152 | celt_assert(st->postfilter_period < MAX_PERIOD){if (!(st->postfilter_period < 1024)) {celt_fatal("assertion failed: " "st->postfilter_period < MAX_PERIOD", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 152);}}; | |||
| 153 | celt_assert(st->postfilter_period >= COMBFILTER_MINPERIOD || st->postfilter_period == 0){if (!(st->postfilter_period >= 15 || st->postfilter_period == 0)) {celt_fatal("assertion failed: " "st->postfilter_period >= COMBFILTER_MINPERIOD || st->postfilter_period == 0" , "/root/firefox-clang/media/libopus/celt/celt_decoder.c", 153 );}}; | |||
| 154 | celt_assert(st->postfilter_period_old < MAX_PERIOD){if (!(st->postfilter_period_old < 1024)) {celt_fatal("assertion failed: " "st->postfilter_period_old < MAX_PERIOD", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 154);}}; | |||
| 155 | celt_assert(st->postfilter_period_old >= COMBFILTER_MINPERIOD || st->postfilter_period_old == 0){if (!(st->postfilter_period_old >= 15 || st->postfilter_period_old == 0)) {celt_fatal("assertion failed: " "st->postfilter_period_old >= COMBFILTER_MINPERIOD || st->postfilter_period_old == 0" , "/root/firefox-clang/media/libopus/celt/celt_decoder.c", 155 );}}; | |||
| 156 | celt_assert(st->postfilter_tapset <= 2){if (!(st->postfilter_tapset <= 2)) {celt_fatal("assertion failed: " "st->postfilter_tapset <= 2", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 156);}}; | |||
| 157 | celt_assert(st->postfilter_tapset >= 0){if (!(st->postfilter_tapset >= 0)) {celt_fatal("assertion failed: " "st->postfilter_tapset >= 0", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 157);}}; | |||
| 158 | celt_assert(st->postfilter_tapset_old <= 2){if (!(st->postfilter_tapset_old <= 2)) {celt_fatal("assertion failed: " "st->postfilter_tapset_old <= 2", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 158);}}; | |||
| 159 | celt_assert(st->postfilter_tapset_old >= 0){if (!(st->postfilter_tapset_old >= 0)) {celt_fatal("assertion failed: " "st->postfilter_tapset_old >= 0", "/root/firefox-clang/media/libopus/celt/celt_decoder.c" , 159);}}; | |||
| 160 | } | |||
| 161 | #endif | |||
| 162 | ||||
| 163 | int celt_decoder_get_size(int channels) | |||
| 164 | { | |||
| 165 | const CELTModeOpusCustomMode *mode = opus_custom_mode_create(48000, 960, NULL((void*)0)); | |||
| 166 | return opus_custom_decoder_get_size(mode, channels); | |||
| 167 | } | |||
| 168 | ||||
| 169 | OPUS_CUSTOM_NOSTATICstatic inline int opus_custom_decoder_get_size(const CELTModeOpusCustomMode *mode, int channels) | |||
| 170 | { | |||
| 171 | int size = sizeof(struct CELTDecoderOpusCustomDecoder) | |||
| 172 | + (channels*(DECODE_BUFFER_SIZE2048+mode->overlap)-1)*sizeof(celt_sig) | |||
| 173 | + channels*CELT_LPC_ORDER24*sizeof(opus_val16) | |||
| 174 | + 4*2*mode->nbEBands*sizeof(celt_glog); | |||
| 175 | return size; | |||
| 176 | } | |||
| 177 | ||||
| 178 | #ifdef CUSTOM_MODES | |||
| 179 | CELTDecoderOpusCustomDecoder *opus_custom_decoder_create(const CELTModeOpusCustomMode *mode, int channels, int *error) | |||
| 180 | { | |||
| 181 | int ret; | |||
| 182 | CELTDecoderOpusCustomDecoder *st = (CELTDecoderOpusCustomDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels)); | |||
| 183 | ret = opus_custom_decoder_init(st, mode, channels); | |||
| 184 | if (ret != OPUS_OK0) | |||
| 185 | { | |||
| 186 | opus_custom_decoder_destroy(st); | |||
| 187 | st = NULL((void*)0); | |||
| 188 | } | |||
| 189 | if (error) | |||
| 190 | *error = ret; | |||
| 191 | return st; | |||
| 192 | } | |||
| 193 | #endif /* CUSTOM_MODES */ | |||
| 194 | ||||
| 195 | int celt_decoder_init(CELTDecoderOpusCustomDecoder *st, opus_int32 sampling_rate, int channels) | |||
| 196 | { | |||
| 197 | int ret; | |||
| 198 | ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL((void*)0)), channels); | |||
| 199 | if (ret != OPUS_OK0) | |||
| 200 | return ret; | |||
| 201 | st->downsample = resampling_factor(sampling_rate); | |||
| 202 | if (st->downsample==0) | |||
| 203 | return OPUS_BAD_ARG-1; | |||
| 204 | else | |||
| 205 | return OPUS_OK0; | |||
| 206 | } | |||
| 207 | ||||
| 208 | OPUS_CUSTOM_NOSTATICstatic inline int opus_custom_decoder_init(CELTDecoderOpusCustomDecoder *st, const CELTModeOpusCustomMode *mode, int channels) | |||
| 209 | { | |||
| 210 | if (channels < 0 || channels > 2) | |||
| 211 | return OPUS_BAD_ARG-1; | |||
| 212 | ||||
| 213 | if (st==NULL((void*)0)) | |||
| 214 | return OPUS_ALLOC_FAIL-7; | |||
| 215 | ||||
| 216 | OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels))(memset(((char*)st), 0, (opus_custom_decoder_get_size(mode, channels ))*sizeof(*((char*)st)))); | |||
| 217 | ||||
| 218 | st->mode = mode; | |||
| 219 | st->overlap = mode->overlap; | |||
| 220 | st->stream_channels = st->channels = channels; | |||
| 221 | ||||
| 222 | st->downsample = 1; | |||
| 223 | st->start = 0; | |||
| 224 | st->end = st->mode->effEBands; | |||
| 225 | st->signalling = 1; | |||
| 226 | #ifndef DISABLE_UPDATE_DRAFT | |||
| 227 | st->disable_inv = channels == 1; | |||
| 228 | #else | |||
| 229 | st->disable_inv = 0; | |||
| 230 | #endif | |||
| 231 | st->arch = opus_select_arch(); | |||
| 232 | ||||
| 233 | opus_custom_decoder_ctl(st, OPUS_RESET_STATE4028); | |||
| 234 | ||||
| 235 | return OPUS_OK0; | |||
| 236 | } | |||
| 237 | ||||
| 238 | #ifdef CUSTOM_MODES | |||
| 239 | void opus_custom_decoder_destroy(CELTDecoderOpusCustomDecoder *st) | |||
| 240 | { | |||
| 241 | opus_free(st); | |||
| 242 | } | |||
| 243 | #endif /* CUSTOM_MODES */ | |||
| 244 | ||||
| 245 | #ifndef CUSTOM_MODES | |||
| 246 | /* Special case for stereo with no downsampling and no accumulation. This is | |||
| 247 | quite common and we can make it faster by processing both channels in the | |||
| 248 | same loop, reducing overhead due to the dependency loop in the IIR filter. */ | |||
| 249 | static void deemphasis_stereo_simple(celt_sig *in[], opus_res *pcm, int N, const opus_val16 coef0, | |||
| 250 | celt_sig *mem) | |||
| 251 | { | |||
| 252 | celt_sig * OPUS_RESTRICTrestrict x0; | |||
| 253 | celt_sig * OPUS_RESTRICTrestrict x1; | |||
| 254 | celt_sig m0, m1; | |||
| 255 | int j; | |||
| 256 | x0=in[0]; | |||
| 257 | x1=in[1]; | |||
| 258 | m0 = mem[0]; | |||
| 259 | m1 = mem[1]; | |||
| 260 | for (j=0;j<N;j++) | |||
| 261 | { | |||
| 262 | celt_sig tmp0, tmp1; | |||
| 263 | /* Add VERY_SMALL to x[] first to reduce dependency chain. */ | |||
| 264 | tmp0 = SATURATE(x0[j] + VERY_SMALL + m0, SIG_SAT)(x0[j] + 1e-30f + m0); | |||
| 265 | tmp1 = SATURATE(x1[j] + VERY_SMALL + m1, SIG_SAT)(x1[j] + 1e-30f + m1); | |||
| 266 | m0 = MULT16_32_Q15(coef0, tmp0)((coef0)*(tmp0)); | |||
| 267 | m1 = MULT16_32_Q15(coef0, tmp1)((coef0)*(tmp1)); | |||
| 268 | pcm[2*j ] = SIG2RES(tmp0)((1/32768.f)*(tmp0)); | |||
| 269 | pcm[2*j+1] = SIG2RES(tmp1)((1/32768.f)*(tmp1)); | |||
| 270 | } | |||
| 271 | mem[0] = m0; | |||
| 272 | mem[1] = m1; | |||
| 273 | } | |||
| 274 | #endif | |||
| 275 | ||||
| 276 | #ifndef RESYNTH | |||
| 277 | static | |||
| 278 | #endif | |||
| 279 | void deemphasis(celt_sig *in[], opus_res *pcm, int N, int C, int downsample, const opus_val16 *coef, | |||
| 280 | celt_sig *mem, int accum) | |||
| 281 | { | |||
| 282 | int c; | |||
| 283 | int Nd; | |||
| 284 | int apply_downsampling=0; | |||
| 285 | opus_val16 coef0; | |||
| 286 | VARDECL(celt_sig, scratch)celt_sig *scratch; | |||
| 287 | SAVE_STACK; | |||
| 288 | #ifndef CUSTOM_MODES | |||
| 289 | /* Short version for common case. */ | |||
| 290 | if (downsample == 1 && C == 2 && !accum) | |||
| 291 | { | |||
| 292 | deemphasis_stereo_simple(in, pcm, N, coef[0], mem); | |||
| 293 | return; | |||
| 294 | } | |||
| 295 | #endif | |||
| 296 | ALLOC(scratch, N, celt_sig)scratch = ((celt_sig*)__builtin_alloca (sizeof(celt_sig)*(N)) ); | |||
| 297 | coef0 = coef[0]; | |||
| 298 | Nd = N/downsample; | |||
| 299 | c=0; do { | |||
| 300 | int j; | |||
| 301 | celt_sig * OPUS_RESTRICTrestrict x; | |||
| 302 | opus_res * OPUS_RESTRICTrestrict y; | |||
| 303 | celt_sig m = mem[c]; | |||
| 304 | x =in[c]; | |||
| 305 | y = pcm+c; | |||
| 306 | #ifdef CUSTOM_MODES | |||
| 307 | if (coef[1] != 0) | |||
| 308 | { | |||
| 309 | opus_val16 coef1 = coef[1]; | |||
| 310 | opus_val16 coef3 = coef[3]; | |||
| 311 | for (j=0;j<N;j++) | |||
| 312 | { | |||
| 313 | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT)(x[j] + m + 1e-30f); | |||
| 314 | m = MULT16_32_Q15(coef0, tmp)((coef0)*(tmp)) | |||
| 315 | - MULT16_32_Q15(coef1, x[j])((coef1)*(x[j])); | |||
| 316 | tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2)(((coef3)*(tmp))); | |||
| 317 | scratch[j] = tmp; | |||
| 318 | } | |||
| 319 | apply_downsampling=1; | |||
| 320 | } else | |||
| 321 | #endif | |||
| 322 | if (downsample>1) | |||
| 323 | { | |||
| 324 | /* Shortcut for the standard (non-custom modes) case */ | |||
| 325 | for (j=0;j<N;j++) | |||
| 326 | { | |||
| 327 | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT)(x[j] + 1e-30f + m); | |||
| 328 | m = MULT16_32_Q15(coef0, tmp)((coef0)*(tmp)); | |||
| 329 | scratch[j] = tmp; | |||
| 330 | } | |||
| 331 | apply_downsampling=1; | |||
| 332 | } else { | |||
| 333 | /* Shortcut for the standard (non-custom modes) case */ | |||
| 334 | if (accum) | |||
| 335 | { | |||
| 336 | for (j=0;j<N;j++) | |||
| 337 | { | |||
| 338 | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT)(x[j] + m + 1e-30f); | |||
| 339 | m = MULT16_32_Q15(coef0, tmp)((coef0)*(tmp)); | |||
| 340 | y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp))((y[j*C])+(((1/32768.f)*(tmp)))); | |||
| 341 | } | |||
| 342 | } else | |||
| 343 | { | |||
| 344 | for (j=0;j<N;j++) | |||
| 345 | { | |||
| 346 | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT)(x[j] + 1e-30f + m); | |||
| 347 | m = MULT16_32_Q15(coef0, tmp)((coef0)*(tmp)); | |||
| 348 | y[j*C] = SIG2RES(tmp)((1/32768.f)*(tmp)); | |||
| 349 | } | |||
| 350 | } | |||
| 351 | } | |||
| 352 | mem[c] = m; | |||
| 353 | ||||
| 354 | if (apply_downsampling
| |||
| 355 | { | |||
| 356 | /* Perform down-sampling */ | |||
| 357 | if (accum) | |||
| 358 | { | |||
| 359 | for (j=0;j<Nd;j++) | |||
| 360 | y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample]))((y[j*C])+(((1/32768.f)*(scratch[j*downsample])))); | |||
| ||||
| 361 | } else | |||
| 362 | { | |||
| 363 | for (j=0;j<Nd;j++) | |||
| 364 | y[j*C] = SIG2RES(scratch[j*downsample])((1/32768.f)*(scratch[j*downsample])); | |||
| 365 | } | |||
| 366 | } | |||
| 367 | } while (++c<C); | |||
| 368 | RESTORE_STACK; | |||
| 369 | } | |||
| 370 | ||||
| 371 | #ifndef RESYNTH | |||
| 372 | static | |||
| 373 | #endif | |||
| 374 | void celt_synthesis(const CELTModeOpusCustomMode *mode, celt_norm *X, celt_sig * out_syn[], | |||
| 375 | celt_glog *oldBandE, int start, int effEnd, int C, int CC, | |||
| 376 | int isTransient, int LM, int downsample, | |||
| 377 | int silence, int arch) | |||
| 378 | { | |||
| 379 | int c, i; | |||
| 380 | int M; | |||
| 381 | int b; | |||
| 382 | int B; | |||
| 383 | int N, NB; | |||
| 384 | int shift; | |||
| 385 | int nbEBands; | |||
| 386 | int overlap; | |||
| 387 | VARDECL(celt_sig, freq)celt_sig *freq; | |||
| 388 | SAVE_STACK; | |||
| 389 | ||||
| 390 | overlap = mode->overlap; | |||
| 391 | nbEBands = mode->nbEBands; | |||
| 392 | N = mode->shortMdctSize<<LM; | |||
| 393 | ALLOC(freq, N, celt_sig)freq = ((celt_sig*)__builtin_alloca (sizeof(celt_sig)*(N))); /**< Interleaved signal MDCTs */ | |||
| 394 | M = 1<<LM; | |||
| 395 | ||||
| 396 | if (isTransient) | |||
| 397 | { | |||
| 398 | B = M; | |||
| 399 | NB = mode->shortMdctSize; | |||
| 400 | shift = mode->maxLM; | |||
| 401 | } else { | |||
| 402 | B = 1; | |||
| 403 | NB = mode->shortMdctSize<<LM; | |||
| 404 | shift = mode->maxLM-LM; | |||
| 405 | } | |||
| 406 | ||||
| 407 | if (CC==2&&C==1) | |||
| 408 | { | |||
| 409 | /* Copying a mono streams to two channels */ | |||
| 410 | celt_sig *freq2; | |||
| 411 | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | |||
| 412 | downsample, silence); | |||
| 413 | /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */ | |||
| 414 | freq2 = out_syn[1]+overlap/2; | |||
| 415 | OPUS_COPY(freq2, freq, N)(memcpy((freq2), (freq), (N)*sizeof(*(freq2)) + 0*((freq2)-(freq )) )); | |||
| 416 | for (b=0;b<B;b++) | |||
| 417 | clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch)clt_mdct_backward_c(&mode->mdct, &freq2[b], out_syn [0]+NB*b, mode->window, overlap, shift, B, arch); | |||
| 418 | for (b=0;b<B;b++) | |||
| 419 | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch)clt_mdct_backward_c(&mode->mdct, &freq[b], out_syn [1]+NB*b, mode->window, overlap, shift, B, arch); | |||
| 420 | } else if (CC==1&&C==2) | |||
| 421 | { | |||
| 422 | /* Downmixing a stereo stream to mono */ | |||
| 423 | celt_sig *freq2; | |||
| 424 | freq2 = out_syn[0]+overlap/2; | |||
| 425 | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | |||
| 426 | downsample, silence); | |||
| 427 | /* Use the output buffer as temp array before downmixing. */ | |||
| 428 | denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M, | |||
| 429 | downsample, silence); | |||
| 430 | for (i=0;i<N;i++) | |||
| 431 | freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i]))(((.5f*(freq[i])))+((.5f*(freq2[i])))); | |||
| 432 | for (b=0;b<B;b++) | |||
| 433 | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch)clt_mdct_backward_c(&mode->mdct, &freq[b], out_syn [0]+NB*b, mode->window, overlap, shift, B, arch); | |||
| 434 | } else { | |||
| 435 | /* Normal case (mono or stereo) */ | |||
| 436 | c=0; do { | |||
| 437 | denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M, | |||
| 438 | downsample, silence); | |||
| 439 | for (b=0;b<B;b++) | |||
| 440 | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch)clt_mdct_backward_c(&mode->mdct, &freq[b], out_syn [c]+NB*b, mode->window, overlap, shift, B, arch); | |||
| 441 | } while (++c<CC); | |||
| 442 | } | |||
| 443 | /* Saturate IMDCT output so that we can't overflow in the pitch postfilter | |||
| 444 | or in the */ | |||
| 445 | c=0; do { | |||
| 446 | for (i=0;i<N;i++) | |||
| 447 | out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT)(out_syn[c][i]); | |||
| 448 | } while (++c<CC); | |||
| 449 | RESTORE_STACK; | |||
| 450 | } | |||
| 451 | ||||
| 452 | static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec) | |||
| 453 | { | |||
| 454 | int i, curr, tf_select; | |||
| 455 | int tf_select_rsv; | |||
| 456 | int tf_changed; | |||
| 457 | int logp; | |||
| 458 | opus_uint32 budget; | |||
| 459 | opus_uint32 tell; | |||
| 460 | ||||
| 461 | budget = dec->storage*8; | |||
| 462 | tell = ec_tell(dec); | |||
| 463 | logp = isTransient ? 2 : 4; | |||
| 464 | tf_select_rsv = LM>0 && tell+logp+1<=budget; | |||
| 465 | budget -= tf_select_rsv; | |||
| 466 | tf_changed = curr = 0; | |||
| 467 | for (i=start;i<end;i++) | |||
| 468 | { | |||
| 469 | if (tell+logp<=budget) | |||
| 470 | { | |||
| 471 | curr ^= ec_dec_bit_logp(dec, logp); | |||
| 472 | tell = ec_tell(dec); | |||
| 473 | tf_changed |= curr; | |||
| 474 | } | |||
| 475 | tf_res[i] = curr; | |||
| 476 | logp = isTransient ? 4 : 5; | |||
| 477 | } | |||
| 478 | tf_select = 0; | |||
| 479 | if (tf_select_rsv && | |||
| 480 | tf_select_table[LM][4*isTransient+0+tf_changed] != | |||
| 481 | tf_select_table[LM][4*isTransient+2+tf_changed]) | |||
| 482 | { | |||
| 483 | tf_select = ec_dec_bit_logp(dec, 1); | |||
| 484 | } | |||
| 485 | for (i=start;i<end;i++) | |||
| 486 | { | |||
| 487 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; | |||
| 488 | } | |||
| 489 | } | |||
| 490 | ||||
| 491 | static int celt_plc_pitch_search(celt_sig *decode_mem[2], int C, int arch) | |||
| 492 | { | |||
| 493 | int pitch_index; | |||
| 494 | VARDECL( opus_val16, lp_pitch_buf )opus_val16 *lp_pitch_buf; | |||
| 495 | SAVE_STACK; | |||
| 496 | ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 )lp_pitch_buf = ((opus_val16*)__builtin_alloca (sizeof(opus_val16 )*(2048>>1))); | |||
| 497 | pitch_downsample(decode_mem, lp_pitch_buf, | |||
| 498 | DECODE_BUFFER_SIZE2048, C, arch); | |||
| 499 | pitch_search(lp_pitch_buf+(PLC_PITCH_LAG_MAX(720)>>1), lp_pitch_buf, | |||
| 500 | DECODE_BUFFER_SIZE2048-PLC_PITCH_LAG_MAX(720), | |||
| 501 | PLC_PITCH_LAG_MAX(720)-PLC_PITCH_LAG_MIN(100), &pitch_index, arch); | |||
| 502 | pitch_index = PLC_PITCH_LAG_MAX(720)-pitch_index; | |||
| 503 | RESTORE_STACK; | |||
| 504 | return pitch_index; | |||
| 505 | } | |||
| 506 | ||||
| 507 | static void prefilter_and_fold(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, int N) | |||
| 508 | { | |||
| 509 | int c; | |||
| 510 | int CC; | |||
| 511 | int i; | |||
| 512 | int overlap; | |||
| 513 | celt_sig *decode_mem[2]; | |||
| 514 | const OpusCustomMode *mode; | |||
| 515 | VARDECL(opus_val32, etmp)opus_val32 *etmp; | |||
| 516 | mode = st->mode; | |||
| 517 | overlap = st->overlap; | |||
| 518 | CC = st->channels; | |||
| 519 | ALLOC(etmp, overlap, opus_val32)etmp = ((opus_val32*)__builtin_alloca (sizeof(opus_val32)*(overlap ))); | |||
| 520 | c=0; do { | |||
| 521 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE2048+overlap); | |||
| 522 | } while (++c<CC); | |||
| 523 | ||||
| 524 | c=0; do { | |||
| 525 | /* Apply the pre-filter to the MDCT overlap for the next frame because | |||
| 526 | the post-filter will be re-applied in the decoder after the MDCT | |||
| 527 | overlap. */ | |||
| 528 | comb_filter(etmp, decode_mem[c]+DECODE_BUFFER_SIZE2048-N, | |||
| 529 | st->postfilter_period_old, st->postfilter_period, overlap, | |||
| 530 | -st->postfilter_gain_old, -st->postfilter_gain, | |||
| 531 | st->postfilter_tapset_old, st->postfilter_tapset, NULL((void*)0), 0, st->arch); | |||
| 532 | ||||
| 533 | /* Simulate TDAC on the concealed audio so that it blends with the | |||
| 534 | MDCT of the next frame. */ | |||
| 535 | for (i=0;i<overlap/2;i++) | |||
| 536 | { | |||
| 537 | decode_mem[c][DECODE_BUFFER_SIZE2048-N+i] = | |||
| 538 | MULT16_32_Q15(COEF2VAL16(mode->window[i]), etmp[overlap-1-i])(((mode->window[i]))*(etmp[overlap-1-i])) | |||
| 539 | + MULT16_32_Q15 (COEF2VAL16(mode->window[overlap-i-1]), etmp[i])(((mode->window[overlap-i-1]))*(etmp[i])); | |||
| 540 | } | |||
| 541 | } while (++c<CC); | |||
| 542 | } | |||
| 543 | ||||
| 544 | #ifdef ENABLE_DEEP_PLC | |||
| 545 | ||||
| 546 | #define SINC_ORDER 48 | |||
| 547 | /* h=cos(pi/2*abs(sin([-24:24]/48*pi*23./24)).^2); | |||
| 548 | b=sinc([-24:24]/3*1.02).*h; | |||
| 549 | b=b/sum(b); */ | |||
| 550 | static const float sinc_filter[SINC_ORDER+1] = { | |||
| 551 | 4.2931e-05f, -0.000190293f, -0.000816132f, -0.000637162f, 0.00141662f, 0.00354764f, 0.00184368f, -0.00428274f, | |||
| 552 | -0.00856105f, -0.0034003f, 0.00930201f, 0.0159616f, 0.00489785f, -0.0169649f, -0.0259484f, -0.00596856f, | |||
| 553 | 0.0286551f, 0.0405872f, 0.00649994f, -0.0509284f, -0.0716655f, -0.00665212f, 0.134336f, 0.278927f, | |||
| 554 | 0.339995f, 0.278927f, 0.134336f, -0.00665212f, -0.0716655f, -0.0509284f, 0.00649994f, 0.0405872f, | |||
| 555 | 0.0286551f, -0.00596856f, -0.0259484f, -0.0169649f, 0.00489785f, 0.0159616f, 0.00930201f, -0.0034003f, | |||
| 556 | -0.00856105f, -0.00428274f, 0.00184368f, 0.00354764f, 0.00141662f, -0.000637162f, -0.000816132f, -0.000190293f, | |||
| 557 | 4.2931e-05f | |||
| 558 | }; | |||
| 559 | ||||
| 560 | void update_plc_state(LPCNetPLCState *lpcnet, celt_sig *decode_mem[2], float *plc_preemphasis_mem, int CC) | |||
| 561 | { | |||
| 562 | int i; | |||
| 563 | int tmp_read_post, tmp_fec_skip; | |||
| 564 | int offset; | |||
| 565 | celt_sig buf48k[DECODE_BUFFER_SIZE2048]; | |||
| 566 | opus_int16 buf16k[PLC_UPDATE_SAMPLES(4*FRAME_SIZE)]; | |||
| 567 | if (CC == 1) OPUS_COPY(buf48k, decode_mem[0], DECODE_BUFFER_SIZE)(memcpy((buf48k), (decode_mem[0]), (2048)*sizeof(*(buf48k)) + 0*((buf48k)-(decode_mem[0])) )); | |||
| 568 | else { | |||
| 569 | for (i=0;i<DECODE_BUFFER_SIZE2048;i++) { | |||
| 570 | buf48k[i] = .5*(decode_mem[0][i] + decode_mem[1][i]); | |||
| 571 | } | |||
| 572 | } | |||
| 573 | /* Down-sample the last 40 ms. */ | |||
| 574 | for (i=1;i<DECODE_BUFFER_SIZE2048;i++) buf48k[i] += PREEMPHASIS*buf48k[i-1]; | |||
| 575 | *plc_preemphasis_mem = buf48k[DECODE_BUFFER_SIZE2048-1]; | |||
| 576 | offset = DECODE_BUFFER_SIZE2048-SINC_ORDER-1 - 3*(PLC_UPDATE_SAMPLES(4*FRAME_SIZE)-1); | |||
| 577 | celt_assert(3*(PLC_UPDATE_SAMPLES-1) + SINC_ORDER + offset == DECODE_BUFFER_SIZE-1){if (!(3*((4*FRAME_SIZE)-1) + SINC_ORDER + offset == 2048 -1) ) {celt_fatal("assertion failed: " "3*(PLC_UPDATE_SAMPLES-1) + SINC_ORDER + offset == DECODE_BUFFER_SIZE-1" , "/root/firefox-clang/media/libopus/celt/celt_decoder.c", 577 );}}; | |||
| 578 | for (i=0;i<PLC_UPDATE_SAMPLES(4*FRAME_SIZE);i++) { | |||
| 579 | int j; | |||
| 580 | float sum = 0; | |||
| 581 | for (j=0;j<SINC_ORDER+1;j++) { | |||
| 582 | sum += buf48k[3*i + j + offset]*sinc_filter[j]; | |||
| 583 | } | |||
| 584 | buf16k[i] = float2int(MIN32(32767.f, MAX32(-32767.f, sum))((32767.f) < (((-32767.f) > (sum) ? (-32767.f) : (sum)) ) ? (32767.f) : (((-32767.f) > (sum) ? (-32767.f) : (sum)) ))); | |||
| 585 | } | |||
| 586 | tmp_read_post = lpcnet->fec_read_pos; | |||
| 587 | tmp_fec_skip = lpcnet->fec_skip; | |||
| 588 | for (i=0;i<PLC_UPDATE_FRAMES4;i++) { | |||
| 589 | lpcnet_plc_update(lpcnet, &buf16k[FRAME_SIZE*i]); | |||
| 590 | } | |||
| 591 | lpcnet->fec_read_pos = tmp_read_post; | |||
| 592 | lpcnet->fec_skip = tmp_fec_skip; | |||
| 593 | } | |||
| 594 | #endif | |||
| 595 | ||||
| 596 | static void celt_decode_lost(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, int N, int LM | |||
| 597 | #ifdef ENABLE_DEEP_PLC | |||
| 598 | ,LPCNetPLCState *lpcnet | |||
| 599 | #endif | |||
| 600 | ) | |||
| 601 | { | |||
| 602 | int c; | |||
| 603 | int i; | |||
| 604 | const int C = st->channels; | |||
| 605 | celt_sig *decode_mem[2]; | |||
| 606 | celt_sig *out_syn[2]; | |||
| 607 | opus_val16 *lpc; | |||
| 608 | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | |||
| 609 | const OpusCustomMode *mode; | |||
| 610 | int nbEBands; | |||
| 611 | int overlap; | |||
| 612 | int start; | |||
| 613 | int loss_duration; | |||
| 614 | int noise_based; | |||
| 615 | const opus_int16 *eBands; | |||
| 616 | SAVE_STACK; | |||
| 617 | ||||
| 618 | mode = st->mode; | |||
| 619 | nbEBands = mode->nbEBands; | |||
| 620 | overlap = mode->overlap; | |||
| 621 | eBands = mode->eBands; | |||
| 622 | ||||
| 623 | c=0; do { | |||
| 624 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE2048+overlap); | |||
| 625 | out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE2048-N; | |||
| 626 | } while (++c<C); | |||
| 627 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE2048+overlap)*C); | |||
| 628 | oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER24); | |||
| 629 | oldLogE = oldBandE + 2*nbEBands; | |||
| 630 | oldLogE2 = oldLogE + 2*nbEBands; | |||
| 631 | backgroundLogE = oldLogE2 + 2*nbEBands; | |||
| 632 | ||||
| 633 | loss_duration = st->loss_duration; | |||
| 634 | start = st->start; | |||
| 635 | #ifdef ENABLE_DEEP_PLC | |||
| 636 | if (lpcnet != NULL((void*)0)) noise_based = start != 0 || (lpcnet->fec_fill_pos == 0 && (st->skip_plc || loss_duration >= 80)); | |||
| 637 | else | |||
| 638 | #endif | |||
| 639 | noise_based = loss_duration >= 40 || start != 0 || st->skip_plc; | |||
| 640 | if (noise_based) | |||
| 641 | { | |||
| 642 | /* Noise-based PLC/CNG */ | |||
| 643 | VARDECL(celt_norm, X)celt_norm *X; | |||
| 644 | opus_uint32 seed; | |||
| 645 | int end; | |||
| 646 | int effEnd; | |||
| 647 | celt_glog decay; | |||
| 648 | end = st->end; | |||
| 649 | effEnd = IMAX(start, IMIN(end, mode->effEBands))((start) > (((end) < (mode->effEBands) ? (end) : (mode ->effEBands))) ? (start) : (((end) < (mode->effEBands ) ? (end) : (mode->effEBands)))); | |||
| 650 | ||||
| 651 | ALLOC(X, C*N, celt_norm)X = ((celt_norm*)__builtin_alloca (sizeof(celt_norm)*(C*N))); /**< Interleaved normalised MDCTs */ | |||
| 652 | c=0; do { | |||
| 653 | OPUS_MOVE(decode_mem[c], decode_mem[c]+N,(memmove((decode_mem[c]), (decode_mem[c]+N), (2048 -N+overlap )*sizeof(*(decode_mem[c])) + 0*((decode_mem[c])-(decode_mem[c ]+N)) )) | |||
| 654 | DECODE_BUFFER_SIZE-N+overlap)(memmove((decode_mem[c]), (decode_mem[c]+N), (2048 -N+overlap )*sizeof(*(decode_mem[c])) + 0*((decode_mem[c])-(decode_mem[c ]+N)) )); | |||
| 655 | } while (++c<C); | |||
| 656 | ||||
| 657 | if (st->prefilter_and_fold) { | |||
| 658 | prefilter_and_fold(st, N); | |||
| 659 | } | |||
| 660 | ||||
| 661 | /* Energy decay */ | |||
| 662 | decay = loss_duration==0 ? GCONST(1.5f)(1.5f) : GCONST(.5f)(.5f); | |||
| 663 | c=0; do | |||
| 664 | { | |||
| 665 | for (i=start;i<end;i++) | |||
| 666 | oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay)((backgroundLogE[c*nbEBands+i]) > (oldBandE[c*nbEBands+i] - decay) ? (backgroundLogE[c*nbEBands+i]) : (oldBandE[c*nbEBands +i] - decay)); | |||
| 667 | } while (++c<C); | |||
| 668 | seed = st->rng; | |||
| 669 | for (c=0;c<C;c++) | |||
| 670 | { | |||
| 671 | for (i=start;i<effEnd;i++) | |||
| 672 | { | |||
| 673 | int j; | |||
| 674 | int boffs; | |||
| 675 | int blen; | |||
| 676 | boffs = N*c+(eBands[i]<<LM); | |||
| 677 | blen = (eBands[i+1]-eBands[i])<<LM; | |||
| 678 | for (j=0;j<blen;j++) | |||
| 679 | { | |||
| 680 | seed = celt_lcg_rand(seed); | |||
| 681 | X[boffs+j] = (celt_norm)((opus_int32)seed>>20); | |||
| 682 | } | |||
| 683 | renormalise_vector(X+boffs, blen, Q31ONE1.0f, st->arch); | |||
| 684 | } | |||
| 685 | } | |||
| 686 | st->rng = seed; | |||
| 687 | ||||
| 688 | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch); | |||
| 689 | st->prefilter_and_fold = 0; | |||
| 690 | /* Skip regular PLC until we get two consecutive packets. */ | |||
| 691 | st->skip_plc = 1; | |||
| 692 | } else { | |||
| 693 | int exc_length; | |||
| 694 | /* Pitch-based PLC */ | |||
| 695 | const celt_coef *window; | |||
| 696 | opus_val16 *exc; | |||
| 697 | opus_val16 fade = Q15ONE1.0f; | |||
| 698 | int pitch_index; | |||
| 699 | VARDECL(opus_val16, _exc)opus_val16 *_exc; | |||
| 700 | VARDECL(opus_val16, fir_tmp)opus_val16 *fir_tmp; | |||
| 701 | ||||
| 702 | if (loss_duration == 0) | |||
| 703 | { | |||
| 704 | #ifdef ENABLE_DEEP_PLC | |||
| 705 | if (lpcnet != NULL((void*)0) && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C); | |||
| 706 | #endif | |||
| 707 | st->last_pitch_index = pitch_index = celt_plc_pitch_search(decode_mem, C, st->arch); | |||
| 708 | } else { | |||
| 709 | pitch_index = st->last_pitch_index; | |||
| 710 | fade = QCONST16(.8f,15)(.8f); | |||
| 711 | } | |||
| 712 | ||||
| 713 | /* We want the excitation for 2 pitch periods in order to look for a | |||
| 714 | decaying signal, but we can't get more than MAX_PERIOD. */ | |||
| 715 | exc_length = IMIN(2*pitch_index, MAX_PERIOD)((2*pitch_index) < (1024) ? (2*pitch_index) : (1024)); | |||
| 716 | ||||
| 717 | ALLOC(_exc, MAX_PERIOD+CELT_LPC_ORDER, opus_val16)_exc = ((opus_val16*)__builtin_alloca (sizeof(opus_val16)*(1024 +24))); | |||
| 718 | ALLOC(fir_tmp, exc_length, opus_val16)fir_tmp = ((opus_val16*)__builtin_alloca (sizeof(opus_val16)* (exc_length))); | |||
| 719 | exc = _exc+CELT_LPC_ORDER24; | |||
| 720 | window = mode->window; | |||
| 721 | c=0; do { | |||
| 722 | opus_val16 decay; | |||
| 723 | opus_val16 attenuation; | |||
| 724 | opus_val32 S1=0; | |||
| 725 | celt_sig *buf; | |||
| 726 | int extrapolation_offset; | |||
| 727 | int extrapolation_len; | |||
| 728 | int j; | |||
| 729 | ||||
| 730 | buf = decode_mem[c]; | |||
| 731 | for (i=0;i<MAX_PERIOD1024+CELT_LPC_ORDER24;i++) | |||
| 732 | exc[i-CELT_LPC_ORDER24] = SROUND16(buf[DECODE_BUFFER_SIZE-MAX_PERIOD-CELT_LPC_ORDER+i], SIG_SHIFT)(buf[2048 -1024 -24 +i]); | |||
| 733 | ||||
| 734 | if (loss_duration == 0) | |||
| 735 | { | |||
| 736 | opus_val32 ac[CELT_LPC_ORDER24+1]; | |||
| 737 | /* Compute LPC coefficients for the last MAX_PERIOD samples before | |||
| 738 | the first loss so we can work in the excitation-filter domain. */ | |||
| 739 | _celt_autocorr(exc, ac, window, overlap, | |||
| 740 | CELT_LPC_ORDER24, MAX_PERIOD1024, st->arch); | |||
| 741 | /* Add a noise floor of -40 dB. */ | |||
| 742 | #ifdef FIXED_POINT | |||
| 743 | ac[0] += SHR32(ac[0],13)(ac[0]); | |||
| 744 | #else | |||
| 745 | ac[0] *= 1.0001f; | |||
| 746 | #endif | |||
| 747 | /* Use lag windowing to stabilize the Levinson-Durbin recursion. */ | |||
| 748 | for (i=1;i<=CELT_LPC_ORDER24;i++) | |||
| 749 | { | |||
| 750 | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ | |||
| 751 | #ifdef FIXED_POINT | |||
| 752 | ac[i] -= MULT16_32_Q15(2*i*i, ac[i])((2*i*i)*(ac[i])); | |||
| 753 | #else | |||
| 754 | ac[i] -= ac[i]*(0.008f*0.008f)*i*i; | |||
| 755 | #endif | |||
| 756 | } | |||
| 757 | _celt_lpc(lpc+c*CELT_LPC_ORDER24, ac, CELT_LPC_ORDER24); | |||
| 758 | #ifdef FIXED_POINT | |||
| 759 | /* For fixed-point, apply bandwidth expansion until we can guarantee that | |||
| 760 | no overflow can happen in the IIR filter. This means: | |||
| 761 | 32768*sum(abs(filter)) < 2^31 */ | |||
| 762 | while (1) { | |||
| 763 | opus_val16 tmp=Q15ONE1.0f; | |||
| 764 | opus_val32 sum=QCONST16(1., SIG_SHIFT)(1.); | |||
| 765 | for (i=0;i<CELT_LPC_ORDER24;i++) | |||
| 766 | sum += ABS16(lpc[c*CELT_LPC_ORDER+i])((float)fabs(lpc[c*24 +i])); | |||
| 767 | if (sum < 65535) break; | |||
| 768 | for (i=0;i<CELT_LPC_ORDER24;i++) | |||
| 769 | { | |||
| 770 | tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp)(((.99f))*(tmp)); | |||
| 771 | lpc[c*CELT_LPC_ORDER24+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp)((lpc[c*24 +i])*(tmp)); | |||
| 772 | } | |||
| 773 | } | |||
| 774 | #endif | |||
| 775 | } | |||
| 776 | /* Initialize the LPC history with the samples just before the start | |||
| 777 | of the region for which we're computing the excitation. */ | |||
| 778 | { | |||
| 779 | /* Compute the excitation for exc_length samples before the loss. We need the copy | |||
| 780 | because celt_fir() cannot filter in-place. */ | |||
| 781 | celt_fir(exc+MAX_PERIOD-exc_length, lpc+c*CELT_LPC_ORDER,(celt_fir_c(exc+1024 -exc_length, lpc+c*24, fir_tmp, exc_length , 24, st->arch)) | |||
| 782 | fir_tmp, exc_length, CELT_LPC_ORDER, st->arch)(celt_fir_c(exc+1024 -exc_length, lpc+c*24, fir_tmp, exc_length , 24, st->arch)); | |||
| 783 | OPUS_COPY(exc+MAX_PERIOD-exc_length, fir_tmp, exc_length)(memcpy((exc+1024 -exc_length), (fir_tmp), (exc_length)*sizeof (*(exc+1024 -exc_length)) + 0*((exc+1024 -exc_length)-(fir_tmp )) )); | |||
| 784 | } | |||
| 785 | ||||
| 786 | /* Check if the waveform is decaying, and if so how fast. | |||
| 787 | We do this to avoid adding energy when concealing in a segment | |||
| 788 | with decaying energy. */ | |||
| 789 | { | |||
| 790 | opus_val32 E1=1, E2=1; | |||
| 791 | int decay_length; | |||
| 792 | #ifdef FIXED_POINT | |||
| 793 | int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[MAX_PERIOD-exc_length], exc_length))-20)((0) > (2*celt_zlog2(celt_maxabs16(&exc[1024 -exc_length ], exc_length))-20) ? (0) : (2*celt_zlog2(celt_maxabs16(& exc[1024 -exc_length], exc_length))-20)); | |||
| 794 | #endif | |||
| 795 | decay_length = exc_length>>1; | |||
| 796 | for (i=0;i<decay_length;i++) | |||
| 797 | { | |||
| 798 | opus_val16 e; | |||
| 799 | e = exc[MAX_PERIOD1024-decay_length+i]; | |||
| 800 | E1 += SHR32(MULT16_16(e, e), shift)(((opus_val32)(e)*(opus_val32)(e))); | |||
| 801 | e = exc[MAX_PERIOD1024-2*decay_length+i]; | |||
| 802 | E2 += SHR32(MULT16_16(e, e), shift)(((opus_val32)(e)*(opus_val32)(e))); | |||
| 803 | } | |||
| 804 | E1 = MIN32(E1, E2)((E1) < (E2) ? (E1) : (E2)); | |||
| 805 | decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2))((float)sqrt(((float)((E1))/(E2)))); | |||
| 806 | } | |||
| 807 | ||||
| 808 | /* Move the decoder memory one frame to the left to give us room to | |||
| 809 | add the data for the new frame. We ignore the overlap that extends | |||
| 810 | past the end of the buffer, because we aren't going to use it. */ | |||
| 811 | OPUS_MOVE(buf, buf+N, DECODE_BUFFER_SIZE-N)(memmove((buf), (buf+N), (2048 -N)*sizeof(*(buf)) + 0*((buf)- (buf+N)) )); | |||
| 812 | ||||
| 813 | /* Extrapolate from the end of the excitation with a period of | |||
| 814 | "pitch_index", scaling down each period by an additional factor of | |||
| 815 | "decay". */ | |||
| 816 | extrapolation_offset = MAX_PERIOD1024-pitch_index; | |||
| 817 | /* We need to extrapolate enough samples to cover a complete MDCT | |||
| 818 | window (including overlap/2 samples on both sides). */ | |||
| 819 | extrapolation_len = N+overlap; | |||
| 820 | /* We also apply fading if this is not the first loss. */ | |||
| 821 | attenuation = MULT16_16_Q15(fade, decay)((fade)*(decay)); | |||
| 822 | for (i=j=0;i<extrapolation_len;i++,j++) | |||
| 823 | { | |||
| 824 | opus_val16 tmp; | |||
| 825 | if (j >= pitch_index) { | |||
| 826 | j -= pitch_index; | |||
| 827 | attenuation = MULT16_16_Q15(attenuation, decay)((attenuation)*(decay)); | |||
| 828 | } | |||
| 829 | buf[DECODE_BUFFER_SIZE2048-N+i] = | |||
| 830 | SHL32(EXTEND32(MULT16_16_Q15(attenuation,((((attenuation)*(exc[extrapolation_offset+j])))) | |||
| 831 | exc[extrapolation_offset+j])), SIG_SHIFT)((((attenuation)*(exc[extrapolation_offset+j])))); | |||
| 832 | /* Compute the energy of the previously decoded signal whose | |||
| 833 | excitation we're copying. */ | |||
| 834 | tmp = SROUND16((buf[2048 -1024 -N+extrapolation_offset+j]) | |||
| 835 | buf[DECODE_BUFFER_SIZE-MAX_PERIOD-N+extrapolation_offset+j],(buf[2048 -1024 -N+extrapolation_offset+j]) | |||
| 836 | SIG_SHIFT)(buf[2048 -1024 -N+extrapolation_offset+j]); | |||
| 837 | S1 += SHR32(MULT16_16(tmp, tmp), 10)(((opus_val32)(tmp)*(opus_val32)(tmp))); | |||
| 838 | } | |||
| 839 | { | |||
| 840 | opus_val16 lpc_mem[CELT_LPC_ORDER24]; | |||
| 841 | /* Copy the last decoded samples (prior to the overlap region) to | |||
| 842 | synthesis filter memory so we can have a continuous signal. */ | |||
| 843 | for (i=0;i<CELT_LPC_ORDER24;i++) | |||
| 844 | lpc_mem[i] = SROUND16(buf[DECODE_BUFFER_SIZE-N-1-i], SIG_SHIFT)(buf[2048 -N-1-i]); | |||
| 845 | /* Apply the synthesis filter to convert the excitation back into | |||
| 846 | the signal domain. */ | |||
| 847 | celt_iir(buf+DECODE_BUFFER_SIZE2048-N, lpc+c*CELT_LPC_ORDER24, | |||
| 848 | buf+DECODE_BUFFER_SIZE2048-N, extrapolation_len, CELT_LPC_ORDER24, | |||
| 849 | lpc_mem, st->arch); | |||
| 850 | #ifdef FIXED_POINT | |||
| 851 | for (i=0; i < extrapolation_len; i++) | |||
| 852 | buf[DECODE_BUFFER_SIZE2048-N+i] = SATURATE(buf[DECODE_BUFFER_SIZE-N+i], SIG_SAT)(buf[2048 -N+i]); | |||
| 853 | #endif | |||
| 854 | } | |||
| 855 | ||||
| 856 | /* Check if the synthesis energy is higher than expected, which can | |||
| 857 | happen with the signal changes during our window. If so, | |||
| 858 | attenuate. */ | |||
| 859 | { | |||
| 860 | opus_val32 S2=0; | |||
| 861 | for (i=0;i<extrapolation_len;i++) | |||
| 862 | { | |||
| 863 | opus_val16 tmp = SROUND16(buf[DECODE_BUFFER_SIZE-N+i], SIG_SHIFT)(buf[2048 -N+i]); | |||
| 864 | S2 += SHR32(MULT16_16(tmp, tmp), 10)(((opus_val32)(tmp)*(opus_val32)(tmp))); | |||
| 865 | } | |||
| 866 | /* This checks for an "explosion" in the synthesis. */ | |||
| 867 | #ifdef FIXED_POINT | |||
| 868 | if (!(S1 > SHR32(S2,2)(S2))) | |||
| 869 | #else | |||
| 870 | /* The float test is written this way to catch NaNs in the output | |||
| 871 | of the IIR filter at the same time. */ | |||
| 872 | if (!(S1 > 0.2f*S2)) | |||
| 873 | #endif | |||
| 874 | { | |||
| 875 | for (i=0;i<extrapolation_len;i++) | |||
| 876 | buf[DECODE_BUFFER_SIZE2048-N+i] = 0; | |||
| 877 | } else if (S1 < S2) | |||
| 878 | { | |||
| 879 | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1))((float)sqrt(((float)((S1)+1)/(S2+1)))); | |||
| 880 | for (i=0;i<overlap;i++) | |||
| 881 | { | |||
| 882 | opus_val16 tmp_g = Q15ONE1.0f | |||
| 883 | - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio)(((window[i]))*(1.0f -ratio)); | |||
| 884 | buf[DECODE_BUFFER_SIZE2048-N+i] = | |||
| 885 | MULT16_32_Q15(tmp_g, buf[DECODE_BUFFER_SIZE-N+i])((tmp_g)*(buf[2048 -N+i])); | |||
| 886 | } | |||
| 887 | for (i=overlap;i<extrapolation_len;i++) | |||
| 888 | { | |||
| 889 | buf[DECODE_BUFFER_SIZE2048-N+i] = | |||
| 890 | MULT16_32_Q15(ratio, buf[DECODE_BUFFER_SIZE-N+i])((ratio)*(buf[2048 -N+i])); | |||
| 891 | } | |||
| 892 | } | |||
| 893 | } | |||
| 894 | ||||
| 895 | } while (++c<C); | |||
| 896 | ||||
| 897 | #ifdef ENABLE_DEEP_PLC | |||
| 898 | if (lpcnet != NULL((void*)0) && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) { | |||
| 899 | float overlap_mem; | |||
| 900 | int samples_needed16k; | |||
| 901 | celt_sig *buf; | |||
| 902 | VARDECL(float, buf_copy)float *buf_copy; | |||
| 903 | buf = decode_mem[0]; | |||
| 904 | ALLOC(buf_copy, C*overlap, float)buf_copy = ((float*)__builtin_alloca (sizeof(float)*(C*overlap ))); | |||
| 905 | c=0; do { | |||
| 906 | OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][DECODE_BUFFER_SIZE-N], overlap)(memcpy((buf_copy+c*overlap), (&decode_mem[c][2048 -N]), ( overlap)*sizeof(*(buf_copy+c*overlap)) + 0*((buf_copy+c*overlap )-(&decode_mem[c][2048 -N])) )); | |||
| 907 | } while (++c<C); | |||
| 908 | ||||
| 909 | /* Need enough samples from the PLC to cover the frame size, resampling delay, | |||
| 910 | and the overlap at the end. */ | |||
| 911 | samples_needed16k = (N+SINC_ORDER+overlap)/3; | |||
| 912 | if (loss_duration == 0) { | |||
| 913 | st->plc_fill = 0; | |||
| 914 | } | |||
| 915 | while (st->plc_fill < samples_needed16k) { | |||
| 916 | lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]); | |||
| 917 | st->plc_fill += FRAME_SIZE; | |||
| 918 | } | |||
| 919 | /* Resample to 48 kHz. */ | |||
| 920 | for (i=0;i<(N+overlap)/3;i++) { | |||
| 921 | int j; | |||
| 922 | float sum; | |||
| 923 | for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j]; | |||
| 924 | buf[DECODE_BUFFER_SIZE2048-N+3*i] = sum; | |||
| 925 | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2]; | |||
| 926 | buf[DECODE_BUFFER_SIZE2048-N+3*i+1] = sum; | |||
| 927 | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1]; | |||
| 928 | buf[DECODE_BUFFER_SIZE2048-N+3*i+2] = sum; | |||
| 929 | } | |||
| 930 | OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3)(memmove((st->plc_pcm), (&st->plc_pcm[N/3]), (st-> plc_fill-N/3)*sizeof(*(st->plc_pcm)) + 0*((st->plc_pcm) -(&st->plc_pcm[N/3])) )); | |||
| 931 | st->plc_fill -= N/3; | |||
| 932 | for (i=0;i<N;i++) { | |||
| 933 | float tmp = buf[DECODE_BUFFER_SIZE2048-N+i]; | |||
| 934 | buf[DECODE_BUFFER_SIZE2048-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem; | |||
| 935 | st->plc_preemphasis_mem = tmp; | |||
| 936 | } | |||
| 937 | overlap_mem = st->plc_preemphasis_mem; | |||
| 938 | for (i=0;i<overlap;i++) { | |||
| 939 | float tmp = buf[DECODE_BUFFER_SIZE2048+i]; | |||
| 940 | buf[DECODE_BUFFER_SIZE2048+i] -= PREEMPHASIS*overlap_mem; | |||
| 941 | overlap_mem = tmp; | |||
| 942 | } | |||
| 943 | /* For now, we just do mono PLC. */ | |||
| 944 | if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], DECODE_BUFFER_SIZE+overlap)(memcpy((decode_mem[1]), (decode_mem[0]), (2048 +overlap)*sizeof (*(decode_mem[1])) + 0*((decode_mem[1])-(decode_mem[0])) )); | |||
| 945 | c=0; do { | |||
| 946 | /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */ | |||
| 947 | if (loss_duration == 0) { | |||
| 948 | for (i=0;i<overlap;i++) decode_mem[c][DECODE_BUFFER_SIZE2048-N+i] = (1-window[i])*buf_copy[c*overlap+i] + (window[i])*decode_mem[c][DECODE_BUFFER_SIZE2048-N+i]; | |||
| 949 | } | |||
| 950 | } while (++c<C); | |||
| 951 | } | |||
| 952 | #endif | |||
| 953 | st->prefilter_and_fold = 1; | |||
| 954 | } | |||
| 955 | ||||
| 956 | /* Saturate to soemthing large to avoid wrap-around. */ | |||
| 957 | st->loss_duration = IMIN(10000, loss_duration+(1<<LM))((10000) < (loss_duration+(1<<LM)) ? (10000) : (loss_duration +(1<<LM))); | |||
| 958 | ||||
| 959 | RESTORE_STACK; | |||
| 960 | } | |||
| 961 | ||||
| 962 | int celt_decode_with_ec_dred(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, const unsigned char *data, | |||
| 963 | int len, opus_res * OPUS_RESTRICTrestrict pcm, int frame_size, ec_dec *dec, int accum | |||
| 964 | #ifdef ENABLE_DEEP_PLC | |||
| 965 | ,LPCNetPLCState *lpcnet | |||
| 966 | #endif | |||
| 967 | ) | |||
| 968 | { | |||
| 969 | int c, i, N; | |||
| 970 | int spread_decision; | |||
| 971 | opus_int32 bits; | |||
| 972 | ec_dec _dec; | |||
| 973 | VARDECL(celt_norm, X)celt_norm *X; | |||
| 974 | VARDECL(int, fine_quant)int *fine_quant; | |||
| 975 | VARDECL(int, pulses)int *pulses; | |||
| 976 | VARDECL(int, cap)int *cap; | |||
| 977 | VARDECL(int, offsets)int *offsets; | |||
| 978 | VARDECL(int, fine_priority)int *fine_priority; | |||
| 979 | VARDECL(int, tf_res)int *tf_res; | |||
| 980 | VARDECL(unsigned char, collapse_masks)unsigned char *collapse_masks; | |||
| 981 | celt_sig *decode_mem[2]; | |||
| 982 | celt_sig *out_syn[2]; | |||
| 983 | opus_val16 *lpc; | |||
| 984 | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | |||
| 985 | ||||
| 986 | int shortBlocks; | |||
| 987 | int isTransient; | |||
| 988 | int intra_ener; | |||
| 989 | const int CC = st->channels; | |||
| 990 | int LM, M; | |||
| 991 | int start; | |||
| 992 | int end; | |||
| 993 | int effEnd; | |||
| 994 | int codedBands; | |||
| 995 | int alloc_trim; | |||
| 996 | int postfilter_pitch; | |||
| 997 | opus_val16 postfilter_gain; | |||
| 998 | int intensity=0; | |||
| 999 | int dual_stereo=0; | |||
| 1000 | opus_int32 total_bits; | |||
| 1001 | opus_int32 balance; | |||
| 1002 | opus_int32 tell; | |||
| 1003 | int dynalloc_logp; | |||
| 1004 | int postfilter_tapset; | |||
| 1005 | int anti_collapse_rsv; | |||
| 1006 | int anti_collapse_on=0; | |||
| 1007 | int silence; | |||
| 1008 | int C = st->stream_channels; | |||
| 1009 | const OpusCustomMode *mode; | |||
| 1010 | int nbEBands; | |||
| 1011 | int overlap; | |||
| 1012 | const opus_int16 *eBands; | |||
| 1013 | celt_glog max_background_increase; | |||
| 1014 | ALLOC_STACK; | |||
| 1015 | ||||
| 1016 | VALIDATE_CELT_DECODER(st)validate_celt_decoder(st); | |||
| 1017 | mode = st->mode; | |||
| 1018 | nbEBands = mode->nbEBands; | |||
| 1019 | overlap = mode->overlap; | |||
| 1020 | eBands = mode->eBands; | |||
| 1021 | start = st->start; | |||
| 1022 | end = st->end; | |||
| 1023 | frame_size *= st->downsample; | |||
| 1024 | ||||
| 1025 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE2048+overlap)*CC); | |||
| 1026 | oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER24); | |||
| 1027 | oldLogE = oldBandE + 2*nbEBands; | |||
| 1028 | oldLogE2 = oldLogE + 2*nbEBands; | |||
| 1029 | backgroundLogE = oldLogE2 + 2*nbEBands; | |||
| 1030 | ||||
| 1031 | #ifdef CUSTOM_MODES | |||
| 1032 | if (st->signalling && data!=NULL((void*)0)) | |||
| 1033 | { | |||
| 1034 | int data0=data[0]; | |||
| 1035 | /* Convert "standard mode" to Opus header */ | |||
| 1036 | if (mode->Fs==48000 && mode->shortMdctSize==120) | |||
| 1037 | { | |||
| 1038 | data0 = fromOpus(data0); | |||
| 1039 | if (data0<0) | |||
| 1040 | return OPUS_INVALID_PACKET-4; | |||
| 1041 | } | |||
| 1042 | st->end = end = IMAX(1, mode->effEBands-2*(data0>>5))((1) > (mode->effEBands-2*(data0>>5)) ? (1) : (mode ->effEBands-2*(data0>>5))); | |||
| 1043 | LM = (data0>>3)&0x3; | |||
| 1044 | C = 1 + ((data0>>2)&0x1); | |||
| 1045 | if ((data[0] & 0x03) == 0x03) { | |||
| 1046 | data++; | |||
| 1047 | len--; | |||
| 1048 | if (len<=0) | |||
| 1049 | return OPUS_INVALID_PACKET-4; | |||
| 1050 | if (data[0] & 0x40) { | |||
| 1051 | int p; | |||
| 1052 | int padding=0; | |||
| 1053 | data++; | |||
| 1054 | len--; | |||
| 1055 | do { | |||
| 1056 | int tmp; | |||
| 1057 | if (len<=0) | |||
| 1058 | return OPUS_INVALID_PACKET-4; | |||
| 1059 | p = *data++; | |||
| 1060 | len--; | |||
| 1061 | tmp = p==255 ? 254: p; | |||
| 1062 | len -= tmp; | |||
| 1063 | padding += tmp; | |||
| 1064 | } while (p==255); | |||
| 1065 | padding--; | |||
| 1066 | if (len <= 0 || padding<0) return OPUS_INVALID_PACKET-4; | |||
| 1067 | } | |||
| 1068 | } else | |||
| 1069 | { | |||
| 1070 | data++; | |||
| 1071 | len--; | |||
| 1072 | } | |||
| 1073 | if (LM>mode->maxLM) | |||
| 1074 | return OPUS_INVALID_PACKET-4; | |||
| 1075 | if (frame_size < mode->shortMdctSize<<LM) | |||
| 1076 | return OPUS_BUFFER_TOO_SMALL-2; | |||
| 1077 | else | |||
| 1078 | frame_size = mode->shortMdctSize<<LM; | |||
| 1079 | } else { | |||
| 1080 | #else | |||
| 1081 | { | |||
| 1082 | #endif | |||
| 1083 | for (LM=0;LM<=mode->maxLM;LM++) | |||
| ||||
| 1084 | if (mode->shortMdctSize<<LM==frame_size) | |||
| 1085 | break; | |||
| 1086 | if (LM
| |||
| 1087 | return OPUS_BAD_ARG-1; | |||
| 1088 | } | |||
| 1089 | M=1<<LM; | |||
| 1090 | ||||
| 1091 | if (len<0 || len>1275 || pcm==NULL((void*)0)) | |||
| 1092 | return OPUS_BAD_ARG-1; | |||
| 1093 | ||||
| 1094 | N = M*mode->shortMdctSize; | |||
| 1095 | c=0; do { | |||
| 1096 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE2048+overlap); | |||
| 1097 | out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE2048-N; | |||
| 1098 | } while (++c<CC); | |||
| 1099 | ||||
| 1100 | effEnd = end; | |||
| 1101 | if (effEnd > mode->effEBands) | |||
| 1102 | effEnd = mode->effEBands; | |||
| 1103 | ||||
| 1104 | if (data == NULL((void*)0) || len<=1) | |||
| 1105 | { | |||
| 1106 | celt_decode_lost(st, N, LM | |||
| 1107 | #ifdef ENABLE_DEEP_PLC | |||
| 1108 | , lpcnet | |||
| 1109 | #endif | |||
| 1110 | ); | |||
| 1111 | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | |||
| 1112 | RESTORE_STACK; | |||
| 1113 | return frame_size/st->downsample; | |||
| 1114 | } | |||
| 1115 | #ifdef ENABLE_DEEP_PLC | |||
| 1116 | else { | |||
| 1117 | /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */ | |||
| 1118 | if (lpcnet) lpcnet->blend = 0; | |||
| 1119 | } | |||
| 1120 | #endif | |||
| 1121 | ||||
| 1122 | /* Check if there are at least two packets received consecutively before | |||
| 1123 | * turning on the pitch-based PLC */ | |||
| 1124 | if (st->loss_duration == 0) st->skip_plc = 0; | |||
| 1125 | ||||
| 1126 | if (dec == NULL((void*)0)) | |||
| 1127 | { | |||
| 1128 | ec_dec_init(&_dec,(unsigned char*)data,len); | |||
| 1129 | dec = &_dec; | |||
| 1130 | } | |||
| 1131 | ||||
| 1132 | if (C==1) | |||
| 1133 | { | |||
| 1134 | for (i=0;i<nbEBands;i++) | |||
| 1135 | oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i])((oldBandE[i]) > (oldBandE[nbEBands+i]) ? (oldBandE[i]) : ( oldBandE[nbEBands+i])); | |||
| 1136 | } | |||
| 1137 | ||||
| 1138 | total_bits = len*8; | |||
| 1139 | tell = ec_tell(dec); | |||
| 1140 | ||||
| 1141 | if (tell >= total_bits) | |||
| 1142 | silence = 1; | |||
| 1143 | else if (tell==1) | |||
| 1144 | silence = ec_dec_bit_logp(dec, 15); | |||
| 1145 | else | |||
| 1146 | silence = 0; | |||
| 1147 | if (silence) | |||
| 1148 | { | |||
| 1149 | /* Pretend we've read all the remaining bits */ | |||
| 1150 | tell = len*8; | |||
| 1151 | dec->nbits_total+=tell-ec_tell(dec); | |||
| 1152 | } | |||
| 1153 | ||||
| 1154 | postfilter_gain = 0; | |||
| 1155 | postfilter_pitch = 0; | |||
| 1156 | postfilter_tapset = 0; | |||
| 1157 | if (start==0 && tell+16 <= total_bits) | |||
| 1158 | { | |||
| 1159 | if(ec_dec_bit_logp(dec, 1)) | |||
| 1160 | { | |||
| 1161 | int qg, octave; | |||
| 1162 | octave = ec_dec_uint(dec, 6); | |||
| 1163 | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; | |||
| 1164 | qg = ec_dec_bits(dec, 3); | |||
| 1165 | if (ec_tell(dec)+2<=total_bits) | |||
| 1166 | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); | |||
| 1167 | postfilter_gain = QCONST16(.09375f,15)(.09375f)*(qg+1); | |||
| 1168 | } | |||
| 1169 | tell = ec_tell(dec); | |||
| 1170 | } | |||
| 1171 | ||||
| 1172 | if (LM > 0 && tell+3 <= total_bits) | |||
| 1173 | { | |||
| 1174 | isTransient = ec_dec_bit_logp(dec, 3); | |||
| 1175 | tell = ec_tell(dec); | |||
| 1176 | } | |||
| 1177 | else | |||
| 1178 | isTransient = 0; | |||
| 1179 | ||||
| 1180 | if (isTransient) | |||
| 1181 | shortBlocks = M; | |||
| 1182 | else | |||
| 1183 | shortBlocks = 0; | |||
| 1184 | ||||
| 1185 | /* Decode the global flags (first symbols in the stream) */ | |||
| 1186 | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; | |||
| 1187 | /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the | |||
| 1188 | risk of getting loud artifacts. */ | |||
| 1189 | if (!intra_ener && st->loss_duration != 0) { | |||
| 1190 | c=0; do | |||
| 1191 | { | |||
| 1192 | celt_glog safety = 0; | |||
| 1193 | int missing = IMIN(10, st->loss_duration>>LM)((10) < (st->loss_duration>>LM) ? (10) : (st-> loss_duration>>LM)); | |||
| 1194 | if (LM==0) safety = GCONST(1.5f)(1.5f); | |||
| 1195 | else if (LM==1) safety = GCONST(.5f)(.5f); | |||
| 1196 | for (i=start;i<end;i++) | |||
| 1197 | { | |||
| 1198 | if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])((oldLogE[c*nbEBands+i]) > (oldLogE2[c*nbEBands+i]) ? (oldLogE [c*nbEBands+i]) : (oldLogE2[c*nbEBands+i]))) { | |||
| 1199 | /* If energy is going down already, continue the trend. */ | |||
| 1200 | opus_val32 slope; | |||
| 1201 | opus_val32 E0, E1, E2; | |||
| 1202 | E0 = oldBandE[c*nbEBands+i]; | |||
| 1203 | E1 = oldLogE[c*nbEBands+i]; | |||
| 1204 | E2 = oldLogE2[c*nbEBands+i]; | |||
| 1205 | slope = MAX32(E1 - E0, HALF32(E2 - E0))((E1 - E0) > ((.5f*(E2 - E0))) ? (E1 - E0) : ((.5f*(E2 - E0 )))); | |||
| 1206 | slope = MING(slope, GCONST(2.f))((slope) < ((2.f)) ? (slope) : ((2.f))); | |||
| 1207 | E0 -= MAX32(0, (1+missing)*slope)((0) > ((1+missing)*slope) ? (0) : ((1+missing)*slope)); | |||
| 1208 | oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0)((-(20.f)) > (E0) ? (-(20.f)) : (E0)); | |||
| 1209 | } else { | |||
| 1210 | /* Otherwise take the min of the last frames. */ | |||
| 1211 | oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i])((((oldBandE[c*nbEBands+i]) < (oldLogE[c*nbEBands+i]) ? (oldBandE [c*nbEBands+i]) : (oldLogE[c*nbEBands+i]))) < (oldLogE2[c* nbEBands+i]) ? (((oldBandE[c*nbEBands+i]) < (oldLogE[c*nbEBands +i]) ? (oldBandE[c*nbEBands+i]) : (oldLogE[c*nbEBands+i]))) : (oldLogE2[c*nbEBands+i])); | |||
| 1212 | } | |||
| 1213 | /* Shorter frames have more natural fluctuations -- play it safe. */ | |||
| 1214 | oldBandE[c*nbEBands+i] -= safety; | |||
| 1215 | } | |||
| 1216 | } while (++c<2); | |||
| 1217 | } | |||
| 1218 | /* Get band energies */ | |||
| 1219 | unquant_coarse_energy(mode, start, end, oldBandE, | |||
| 1220 | intra_ener, dec, C, LM); | |||
| 1221 | ||||
| 1222 | ALLOC(tf_res, nbEBands, int)tf_res = ((int*)__builtin_alloca (sizeof(int)*(nbEBands))); | |||
| 1223 | tf_decode(start, end, isTransient, tf_res, LM, dec); | |||
| 1224 | ||||
| 1225 | tell = ec_tell(dec); | |||
| 1226 | spread_decision = SPREAD_NORMAL(2); | |||
| 1227 | if (tell+4 <= total_bits) | |||
| 1228 | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); | |||
| 1229 | ||||
| 1230 | ALLOC(cap, nbEBands, int)cap = ((int*)__builtin_alloca (sizeof(int)*(nbEBands))); | |||
| 1231 | ||||
| 1232 | init_caps(mode,cap,LM,C); | |||
| 1233 | ||||
| 1234 | ALLOC(offsets, nbEBands, int)offsets = ((int*)__builtin_alloca (sizeof(int)*(nbEBands))); | |||
| 1235 | ||||
| 1236 | dynalloc_logp = 6; | |||
| 1237 | total_bits<<=BITRES3; | |||
| 1238 | tell = ec_tell_frac(dec); | |||
| 1239 | for (i=start;i<end;i++) | |||
| 1240 | { | |||
| 1241 | int width, quanta; | |||
| 1242 | int dynalloc_loop_logp; | |||
| 1243 | int boost; | |||
| 1244 | width = C*(eBands[i+1]-eBands[i])<<LM; | |||
| 1245 | /* quanta is 6 bits, but no more than 1 bit/sample | |||
| 1246 | and no less than 1/8 bit/sample */ | |||
| 1247 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width))((width<<3) < (((6<<3) > (width) ? (6<< 3) : (width))) ? (width<<3) : (((6<<3) > (width ) ? (6<<3) : (width)))); | |||
| 1248 | dynalloc_loop_logp = dynalloc_logp; | |||
| 1249 | boost = 0; | |||
| 1250 | while (tell+(dynalloc_loop_logp<<BITRES3) < total_bits && boost < cap[i]) | |||
| 1251 | { | |||
| 1252 | int flag; | |||
| 1253 | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); | |||
| 1254 | tell = ec_tell_frac(dec); | |||
| 1255 | if (!flag) | |||
| 1256 | break; | |||
| 1257 | boost += quanta; | |||
| 1258 | total_bits -= quanta; | |||
| 1259 | dynalloc_loop_logp = 1; | |||
| 1260 | } | |||
| 1261 | offsets[i] = boost; | |||
| 1262 | /* Making dynalloc more likely */ | |||
| 1263 | if (boost>0) | |||
| 1264 | dynalloc_logp = IMAX(2, dynalloc_logp-1)((2) > (dynalloc_logp-1) ? (2) : (dynalloc_logp-1)); | |||
| 1265 | } | |||
| 1266 | ||||
| 1267 | ALLOC(fine_quant, nbEBands, int)fine_quant = ((int*)__builtin_alloca (sizeof(int)*(nbEBands)) ); | |||
| 1268 | alloc_trim = tell+(6<<BITRES3) <= total_bits ? | |||
| 1269 | ec_dec_icdf(dec, trim_icdf, 7) : 5; | |||
| 1270 | ||||
| 1271 | bits = (((opus_int32)len*8)<<BITRES3) - (opus_int32)ec_tell_frac(dec) - 1; | |||
| 1272 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES3) ? (1<<BITRES3) : 0; | |||
| 1273 | bits -= anti_collapse_rsv; | |||
| 1274 | ||||
| 1275 | ALLOC(pulses, nbEBands, int)pulses = ((int*)__builtin_alloca (sizeof(int)*(nbEBands))); | |||
| 1276 | ALLOC(fine_priority, nbEBands, int)fine_priority = ((int*)__builtin_alloca (sizeof(int)*(nbEBands ))); | |||
| 1277 | ||||
| 1278 | codedBands = clt_compute_allocation(mode, start, end, offsets, cap, | |||
| 1279 | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, | |||
| 1280 | fine_quant, fine_priority, C, LM, dec, 0, 0, 0); | |||
| 1281 | ||||
| 1282 | unquant_fine_energy(mode, start, end, oldBandE, fine_quant, dec, C); | |||
| 1283 | ||||
| 1284 | c=0; do { | |||
| 1285 | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap)(memmove((decode_mem[c]), (decode_mem[c]+N), (2048 -N+overlap )*sizeof(*(decode_mem[c])) + 0*((decode_mem[c])-(decode_mem[c ]+N)) )); | |||
| 1286 | } while (++c<CC); | |||
| 1287 | ||||
| 1288 | /* Decode fixed codebook */ | |||
| 1289 | ALLOC(collapse_masks, C*nbEBands, unsigned char)collapse_masks = ((unsigned char*)__builtin_alloca (sizeof(unsigned char)*(C*nbEBands))); | |||
| 1290 | ||||
| 1291 | ALLOC(X, C*N, celt_norm)X = ((celt_norm*)__builtin_alloca (sizeof(celt_norm)*(C*N))); /**< Interleaved normalised MDCTs */ | |||
| 1292 | ||||
| 1293 | quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL((void*)0), collapse_masks, | |||
| 1294 | NULL((void*)0), pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, | |||
| 1295 | len*(8<<BITRES3)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0, | |||
| 1296 | st->arch, st->disable_inv); | |||
| 1297 | ||||
| 1298 | if (anti_collapse_rsv > 0) | |||
| 1299 | { | |||
| 1300 | anti_collapse_on = ec_dec_bits(dec, 1); | |||
| 1301 | } | |||
| 1302 | ||||
| 1303 | unquant_energy_finalise(mode, start, end, oldBandE, | |||
| 1304 | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); | |||
| 1305 | ||||
| 1306 | if (anti_collapse_on) | |||
| 1307 | anti_collapse(mode, X, collapse_masks, LM, C, N, | |||
| 1308 | start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch); | |||
| 1309 | ||||
| 1310 | if (silence) | |||
| 1311 | { | |||
| 1312 | for (i=0;i<C*nbEBands;i++) | |||
| 1313 | oldBandE[i] = -GCONST(28.f)(28.f); | |||
| 1314 | } | |||
| 1315 | if (st->prefilter_and_fold) { | |||
| 1316 | prefilter_and_fold(st, N); | |||
| 1317 | } | |||
| 1318 | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, | |||
| 1319 | C, CC, isTransient, LM, st->downsample, silence, st->arch); | |||
| 1320 | ||||
| 1321 | c=0; do { | |||
| 1322 | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD)((st->postfilter_period) > (15) ? (st->postfilter_period ) : (15)); | |||
| 1323 | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD)((st->postfilter_period_old) > (15) ? (st->postfilter_period_old ) : (15)); | |||
| 1324 | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | |||
| 1325 | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | |||
| 1326 | mode->window, overlap, st->arch); | |||
| 1327 | if (LM!=0) | |||
| 1328 | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize, | |||
| 1329 | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, | |||
| 1330 | mode->window, overlap, st->arch); | |||
| 1331 | ||||
| 1332 | } while (++c<CC); | |||
| 1333 | st->postfilter_period_old = st->postfilter_period; | |||
| 1334 | st->postfilter_gain_old = st->postfilter_gain; | |||
| 1335 | st->postfilter_tapset_old = st->postfilter_tapset; | |||
| 1336 | st->postfilter_period = postfilter_pitch; | |||
| 1337 | st->postfilter_gain = postfilter_gain; | |||
| 1338 | st->postfilter_tapset = postfilter_tapset; | |||
| 1339 | if (LM!=0) | |||
| 1340 | { | |||
| 1341 | st->postfilter_period_old = st->postfilter_period; | |||
| 1342 | st->postfilter_gain_old = st->postfilter_gain; | |||
| 1343 | st->postfilter_tapset_old = st->postfilter_tapset; | |||
| 1344 | } | |||
| 1345 | ||||
| 1346 | if (C==1) | |||
| 1347 | OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands)(memcpy((&oldBandE[nbEBands]), (oldBandE), (nbEBands)*sizeof (*(&oldBandE[nbEBands])) + 0*((&oldBandE[nbEBands])-( oldBandE)) )); | |||
| 1348 | ||||
| 1349 | if (!isTransient) | |||
| 1350 | { | |||
| 1351 | OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands)(memcpy((oldLogE2), (oldLogE), (2*nbEBands)*sizeof(*(oldLogE2 )) + 0*((oldLogE2)-(oldLogE)) )); | |||
| 1352 | OPUS_COPY(oldLogE, oldBandE, 2*nbEBands)(memcpy((oldLogE), (oldBandE), (2*nbEBands)*sizeof(*(oldLogE) ) + 0*((oldLogE)-(oldBandE)) )); | |||
| 1353 | } else { | |||
| 1354 | for (i=0;i<2*nbEBands;i++) | |||
| 1355 | oldLogE[i] = MING(oldLogE[i], oldBandE[i])((oldLogE[i]) < (oldBandE[i]) ? (oldLogE[i]) : (oldBandE[i ])); | |||
| 1356 | } | |||
| 1357 | /* In normal circumstances, we only allow the noise floor to increase by | |||
| 1358 | up to 2.4 dB/second, but when we're in DTX we give the weight of | |||
| 1359 | all missing packets to the update packet. */ | |||
| 1360 | max_background_increase = IMIN(160, st->loss_duration+M)((160) < (st->loss_duration+M) ? (160) : (st->loss_duration +M))*GCONST(0.001f)(0.001f); | |||
| 1361 | for (i=0;i<2*nbEBands;i++) | |||
| 1362 | backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i])((backgroundLogE[i] + max_background_increase) < (oldBandE [i]) ? (backgroundLogE[i] + max_background_increase) : (oldBandE [i])); | |||
| 1363 | /* In case start or end were to change */ | |||
| 1364 | c=0; do | |||
| 1365 | { | |||
| 1366 | for (i=0;i<start;i++) | |||
| 1367 | { | |||
| 1368 | oldBandE[c*nbEBands+i]=0; | |||
| 1369 | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f)(28.f); | |||
| 1370 | } | |||
| 1371 | for (i=end;i<nbEBands;i++) | |||
| 1372 | { | |||
| 1373 | oldBandE[c*nbEBands+i]=0; | |||
| 1374 | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f)(28.f); | |||
| 1375 | } | |||
| 1376 | } while (++c<2); | |||
| 1377 | st->rng = dec->rng; | |||
| 1378 | ||||
| 1379 | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | |||
| 1380 | st->loss_duration = 0; | |||
| 1381 | st->prefilter_and_fold = 0; | |||
| 1382 | RESTORE_STACK; | |||
| 1383 | if (ec_tell(dec) > 8*len) | |||
| 1384 | return OPUS_INTERNAL_ERROR-3; | |||
| 1385 | if(ec_get_error(dec)) | |||
| 1386 | st->error = 1; | |||
| 1387 | return frame_size/st->downsample; | |||
| 1388 | } | |||
| 1389 | ||||
| 1390 | int celt_decode_with_ec(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, const unsigned char *data, | |||
| 1391 | int len, opus_res * OPUS_RESTRICTrestrict pcm, int frame_size, ec_dec *dec, int accum) | |||
| 1392 | { | |||
| 1393 | return celt_decode_with_ec_dred(st, data, len, pcm, frame_size, dec, accum | |||
| 1394 | #ifdef ENABLE_DEEP_PLC | |||
| 1395 | , NULL((void*)0) | |||
| 1396 | #endif | |||
| 1397 | ); | |||
| 1398 | } | |||
| 1399 | ||||
| 1400 | #ifdef CUSTOM_MODES | |||
| 1401 | ||||
| 1402 | #if defined(FIXED_POINT) && !defined(ENABLE_RES24) | |||
| 1403 | int opus_custom_decode(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICTrestrict pcm, int frame_size) | |||
| 1404 | { | |||
| 1405 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL((void*)0), 0); | |||
| 1406 | } | |||
| 1407 | #else | |||
| 1408 | int opus_custom_decode(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICTrestrict pcm, int frame_size) | |||
| 1409 | { | |||
| 1410 | int j, ret, C, N; | |||
| 1411 | VARDECL(opus_res, out)opus_res *out; | |||
| 1412 | ALLOC_STACK; | |||
| 1413 | ||||
| 1414 | if (pcm==NULL((void*)0)) | |||
| 1415 | return OPUS_BAD_ARG-1; | |||
| 1416 | ||||
| 1417 | C = st->channels; | |||
| 1418 | N = frame_size; | |||
| 1419 | ||||
| 1420 | ALLOC(out, C*N, opus_res)out = ((opus_res*)__builtin_alloca (sizeof(opus_res)*(C*N))); | |||
| 1421 | ret = celt_decode_with_ec(st, data, len, out, frame_size, NULL((void*)0), 0); | |||
| 1422 | if (ret>0) | |||
| 1423 | for (j=0;j<C*ret;j++) | |||
| 1424 | pcm[j]=RES2INT16(out[j])FLOAT2INT16(out[j]); | |||
| 1425 | ||||
| 1426 | RESTORE_STACK; | |||
| 1427 | return ret; | |||
| 1428 | } | |||
| 1429 | #endif | |||
| 1430 | ||||
| 1431 | #if defined(FIXED_POINT) && defined(ENABLE_RES24) | |||
| 1432 | int opus_custom_decode24(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, const unsigned char *data, int len, opus_int32 * OPUS_RESTRICTrestrict pcm, int frame_size) | |||
| 1433 | { | |||
| 1434 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL((void*)0), 0); | |||
| 1435 | } | |||
| 1436 | #else | |||
| 1437 | int opus_custom_decode24(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, const unsigned char *data, int len, opus_int32 * OPUS_RESTRICTrestrict pcm, int frame_size) | |||
| 1438 | { | |||
| 1439 | int j, ret, C, N; | |||
| 1440 | VARDECL(opus_res, out)opus_res *out; | |||
| 1441 | ALLOC_STACK; | |||
| 1442 | ||||
| 1443 | if (pcm==NULL((void*)0)) | |||
| 1444 | return OPUS_BAD_ARG-1; | |||
| 1445 | ||||
| 1446 | C = st->channels; | |||
| 1447 | N = frame_size; | |||
| 1448 | ||||
| 1449 | ALLOC(out, C*N, opus_res)out = ((opus_res*)__builtin_alloca (sizeof(opus_res)*(C*N))); | |||
| 1450 | ret = celt_decode_with_ec(st, data, len, out, frame_size, NULL((void*)0), 0); | |||
| 1451 | if (ret>0) | |||
| 1452 | for (j=0;j<C*ret;j++) | |||
| 1453 | pcm[j]=RES2INT24(out[j])float2int(32768.f*256.f*(out[j])); | |||
| 1454 | ||||
| 1455 | RESTORE_STACK; | |||
| 1456 | return ret; | |||
| 1457 | } | |||
| 1458 | #endif | |||
| 1459 | ||||
| 1460 | ||||
| 1461 | #ifndef DISABLE_FLOAT_API | |||
| 1462 | ||||
| 1463 | # if !defined(FIXED_POINT) | |||
| 1464 | int opus_custom_decode_float(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, const unsigned char *data, int len, float * OPUS_RESTRICTrestrict pcm, int frame_size) | |||
| 1465 | { | |||
| 1466 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL((void*)0), 0); | |||
| 1467 | } | |||
| 1468 | # else | |||
| 1469 | int opus_custom_decode_float(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, const unsigned char *data, int len, float * OPUS_RESTRICTrestrict pcm, int frame_size) | |||
| 1470 | { | |||
| 1471 | int j, ret, C, N; | |||
| 1472 | VARDECL(opus_res, out)opus_res *out; | |||
| 1473 | ALLOC_STACK; | |||
| 1474 | ||||
| 1475 | if (pcm==NULL((void*)0)) | |||
| 1476 | return OPUS_BAD_ARG-1; | |||
| 1477 | ||||
| 1478 | C = st->channels; | |||
| 1479 | N = frame_size; | |||
| 1480 | ||||
| 1481 | ALLOC(out, C*N, opus_res)out = ((opus_res*)__builtin_alloca (sizeof(opus_res)*(C*N))); | |||
| 1482 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL((void*)0), 0); | |||
| 1483 | if (ret>0) | |||
| 1484 | for (j=0;j<C*ret;j++) | |||
| 1485 | pcm[j]=RES2FLOAT(out[j])(out[j]); | |||
| 1486 | ||||
| 1487 | RESTORE_STACK; | |||
| 1488 | return ret; | |||
| 1489 | } | |||
| 1490 | # endif | |||
| 1491 | ||||
| 1492 | #endif | |||
| 1493 | ||||
| 1494 | #endif /* CUSTOM_MODES */ | |||
| 1495 | ||||
| 1496 | int opus_custom_decoder_ctl(CELTDecoderOpusCustomDecoder * OPUS_RESTRICTrestrict st, int request, ...) | |||
| 1497 | { | |||
| 1498 | va_list ap; | |||
| 1499 | ||||
| 1500 | va_start(ap, request)__builtin_va_start(ap, request); | |||
| 1501 | switch (request) | |||
| 1502 | { | |||
| 1503 | case OPUS_SET_COMPLEXITY_REQUEST4010: | |||
| 1504 | { | |||
| 1505 | opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32); | |||
| 1506 | if(value<0 || value>10) | |||
| 1507 | { | |||
| 1508 | goto bad_arg; | |||
| 1509 | } | |||
| 1510 | st->complexity = value; | |||
| 1511 | } | |||
| 1512 | break; | |||
| 1513 | case OPUS_GET_COMPLEXITY_REQUEST4011: | |||
| 1514 | { | |||
| 1515 | opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*); | |||
| 1516 | if (!value) | |||
| 1517 | { | |||
| 1518 | goto bad_arg; | |||
| 1519 | } | |||
| 1520 | *value = st->complexity; | |||
| 1521 | } | |||
| 1522 | break; | |||
| 1523 | case CELT_SET_START_BAND_REQUEST10010: | |||
| 1524 | { | |||
| 1525 | opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32); | |||
| 1526 | if (value<0 || value>=st->mode->nbEBands) | |||
| 1527 | goto bad_arg; | |||
| 1528 | st->start = value; | |||
| 1529 | } | |||
| 1530 | break; | |||
| 1531 | case CELT_SET_END_BAND_REQUEST10012: | |||
| 1532 | { | |||
| 1533 | opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32); | |||
| 1534 | if (value<1 || value>st->mode->nbEBands) | |||
| 1535 | goto bad_arg; | |||
| 1536 | st->end = value; | |||
| 1537 | } | |||
| 1538 | break; | |||
| 1539 | case CELT_SET_CHANNELS_REQUEST10008: | |||
| 1540 | { | |||
| 1541 | opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32); | |||
| 1542 | if (value<1 || value>2) | |||
| 1543 | goto bad_arg; | |||
| 1544 | st->stream_channels = value; | |||
| 1545 | } | |||
| 1546 | break; | |||
| 1547 | case CELT_GET_AND_CLEAR_ERROR_REQUEST10007: | |||
| 1548 | { | |||
| 1549 | opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*); | |||
| 1550 | if (value==NULL((void*)0)) | |||
| 1551 | goto bad_arg; | |||
| 1552 | *value=st->error; | |||
| 1553 | st->error = 0; | |||
| 1554 | } | |||
| 1555 | break; | |||
| 1556 | case OPUS_GET_LOOKAHEAD_REQUEST4027: | |||
| 1557 | { | |||
| 1558 | opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*); | |||
| 1559 | if (value==NULL((void*)0)) | |||
| 1560 | goto bad_arg; | |||
| 1561 | *value = st->overlap/st->downsample; | |||
| 1562 | } | |||
| 1563 | break; | |||
| 1564 | case OPUS_RESET_STATE4028: | |||
| 1565 | { | |||
| 1566 | int i; | |||
| 1567 | opus_val16 *lpc; | |||
| 1568 | celt_glog *oldBandE, *oldLogE, *oldLogE2; | |||
| 1569 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE2048+st->overlap)*st->channels); | |||
| 1570 | oldBandE = (celt_glog*)(lpc+st->channels*CELT_LPC_ORDER24); | |||
| 1571 | oldLogE = oldBandE + 2*st->mode->nbEBands; | |||
| 1572 | oldLogE2 = oldLogE + 2*st->mode->nbEBands; | |||
| 1573 | OPUS_CLEAR((char*)&st->DECODER_RESET_START,(memset(((char*)&st->rng), 0, (opus_custom_decoder_get_size (st->mode, st->channels)- ((char*)&st->rng - (char *)st))*sizeof(*((char*)&st->rng)))) | |||
| 1574 | opus_custom_decoder_get_size(st->mode, st->channels)-(memset(((char*)&st->rng), 0, (opus_custom_decoder_get_size (st->mode, st->channels)- ((char*)&st->rng - (char *)st))*sizeof(*((char*)&st->rng)))) | |||
| 1575 | ((char*)&st->DECODER_RESET_START - (char*)st))(memset(((char*)&st->rng), 0, (opus_custom_decoder_get_size (st->mode, st->channels)- ((char*)&st->rng - (char *)st))*sizeof(*((char*)&st->rng)))); | |||
| 1576 | for (i=0;i<2*st->mode->nbEBands;i++) | |||
| 1577 | oldLogE[i]=oldLogE2[i]=-GCONST(28.f)(28.f); | |||
| 1578 | st->skip_plc = 1; | |||
| 1579 | } | |||
| 1580 | break; | |||
| 1581 | case OPUS_GET_PITCH_REQUEST4033: | |||
| 1582 | { | |||
| 1583 | opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*); | |||
| 1584 | if (value==NULL((void*)0)) | |||
| 1585 | goto bad_arg; | |||
| 1586 | *value = st->postfilter_period; | |||
| 1587 | } | |||
| 1588 | break; | |||
| 1589 | case CELT_GET_MODE_REQUEST10015: | |||
| 1590 | { | |||
| 1591 | const CELTModeOpusCustomMode ** value = va_arg(ap, const CELTMode**)__builtin_va_arg(ap, const OpusCustomMode**); | |||
| 1592 | if (value==0) | |||
| 1593 | goto bad_arg; | |||
| 1594 | *value=st->mode; | |||
| 1595 | } | |||
| 1596 | break; | |||
| 1597 | case CELT_SET_SIGNALLING_REQUEST10016: | |||
| 1598 | { | |||
| 1599 | opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32); | |||
| 1600 | st->signalling = value; | |||
| 1601 | } | |||
| 1602 | break; | |||
| 1603 | case OPUS_GET_FINAL_RANGE_REQUEST4031: | |||
| 1604 | { | |||
| 1605 | opus_uint32 * value = va_arg(ap, opus_uint32 *)__builtin_va_arg(ap, opus_uint32 *); | |||
| 1606 | if (value==0) | |||
| 1607 | goto bad_arg; | |||
| 1608 | *value=st->rng; | |||
| 1609 | } | |||
| 1610 | break; | |||
| 1611 | case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST4046: | |||
| 1612 | { | |||
| 1613 | opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32); | |||
| 1614 | if(value<0 || value>1) | |||
| 1615 | { | |||
| 1616 | goto bad_arg; | |||
| 1617 | } | |||
| 1618 | st->disable_inv = value; | |||
| 1619 | } | |||
| 1620 | break; | |||
| 1621 | case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST4047: | |||
| 1622 | { | |||
| 1623 | opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*); | |||
| 1624 | if (!value) | |||
| 1625 | { | |||
| 1626 | goto bad_arg; | |||
| 1627 | } | |||
| 1628 | *value = st->disable_inv; | |||
| 1629 | } | |||
| 1630 | break; | |||
| 1631 | default: | |||
| 1632 | goto bad_request; | |||
| 1633 | } | |||
| 1634 | va_end(ap)__builtin_va_end(ap); | |||
| 1635 | return OPUS_OK0; | |||
| 1636 | bad_arg: | |||
| 1637 | va_end(ap)__builtin_va_end(ap); | |||
| 1638 | return OPUS_BAD_ARG-1; | |||
| 1639 | bad_request: | |||
| 1640 | va_end(ap)__builtin_va_end(ap); | |||
| 1641 | return OPUS_UNIMPLEMENTED-5; | |||
| 1642 | } |