/src/opus/celt/celt_decoder.c
Line | Count | Source |
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 | 406k | #define PLC_PITCH_LAG_MAX (720) |
63 | | /* The minimum pitch lag to allow in the pitch-based PLC. This corresponds to a |
64 | | pitch of 480 Hz. */ |
65 | 101k | #define PLC_PITCH_LAG_MIN (100) |
66 | | |
67 | | /**********************************************************************/ |
68 | | /* */ |
69 | | /* DECODER */ |
70 | | /* */ |
71 | | /**********************************************************************/ |
72 | 203k | #define DECODE_BUFFER_SIZE DEC_PITCH_BUF_SIZE |
73 | | |
74 | | #define PLC_UPDATE_FRAMES 4 |
75 | | #define PLC_UPDATE_SAMPLES (PLC_UPDATE_FRAMES*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 | | #ifdef ENABLE_QEXT |
93 | | int qext_scale; |
94 | | #endif |
95 | | |
96 | | /* Everything beyond this point gets cleared on a reset */ |
97 | | #define DECODER_RESET_START rng |
98 | | |
99 | | opus_uint32 rng; |
100 | | int error; |
101 | | int last_pitch_index; |
102 | | int loss_duration; |
103 | | int skip_plc; |
104 | | int postfilter_period; |
105 | | int postfilter_period_old; |
106 | | opus_val16 postfilter_gain; |
107 | | opus_val16 postfilter_gain_old; |
108 | | int postfilter_tapset; |
109 | | int postfilter_tapset_old; |
110 | | int prefilter_and_fold; |
111 | | |
112 | | celt_sig preemph_memD[2]; |
113 | | |
114 | | #ifdef ENABLE_DEEP_PLC |
115 | | opus_int16 plc_pcm[PLC_UPDATE_SAMPLES]; |
116 | | int plc_fill; |
117 | | float plc_preemphasis_mem; |
118 | | #endif |
119 | | |
120 | | celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ |
121 | | /* opus_val16 lpc[], Size = channels*CELT_LPC_ORDER */ |
122 | | /* celt_glog oldEBands[], Size = 2*mode->nbEBands */ |
123 | | /* celt_glog oldLogE[], Size = 2*mode->nbEBands */ |
124 | | /* celt_glog oldLogE2[], Size = 2*mode->nbEBands */ |
125 | | /* celt_glog backgroundLogE[], Size = 2*mode->nbEBands */ |
126 | | }; |
127 | | |
128 | | #if defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS) |
129 | | /* Make basic checks on the CELT state to ensure we don't end |
130 | | up writing all over memory. */ |
131 | | void validate_celt_decoder(CELTDecoder *st) |
132 | 516k | { |
133 | | #if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT) |
134 | 265k | celt_assert(st->mode == opus_custom_mode_create(48000, 960, NULL)); |
135 | 265k | celt_assert(st->overlap == 120); |
136 | 265k | celt_assert(st->end <= 21); |
137 | | #else |
138 | | /* From Section 4.3 in the spec: "The normal CELT layer uses 21 of those bands, |
139 | | though Opus Custom (see Section 6.2) may use a different number of bands" |
140 | | |
141 | | Check if it's within the maximum number of Bark frequency bands instead */ |
142 | 250k | celt_assert(st->end <= 25); |
143 | 250k | #endif |
144 | 516k | celt_assert(st->channels == 1 || st->channels == 2); |
145 | 516k | celt_assert(st->stream_channels == 1 || st->stream_channels == 2); |
146 | 516k | celt_assert(st->downsample > 0); |
147 | 516k | celt_assert(st->start == 0 || st->start == 17); |
148 | 516k | celt_assert(st->start < st->end); |
149 | 516k | #ifdef OPUS_ARCHMASK |
150 | 516k | celt_assert(st->arch >= 0); |
151 | 516k | celt_assert(st->arch <= OPUS_ARCHMASK); |
152 | 516k | #endif |
153 | | #ifndef ENABLE_QEXT |
154 | 265k | celt_assert(st->last_pitch_index <= PLC_PITCH_LAG_MAX); |
155 | 265k | celt_assert(st->last_pitch_index >= PLC_PITCH_LAG_MIN || st->last_pitch_index == 0); |
156 | 265k | #endif |
157 | 516k | celt_assert(st->postfilter_period < MAX_PERIOD); |
158 | 516k | celt_assert(st->postfilter_period >= COMBFILTER_MINPERIOD || st->postfilter_period == 0); |
159 | 516k | celt_assert(st->postfilter_period_old < MAX_PERIOD); |
160 | 516k | celt_assert(st->postfilter_period_old >= COMBFILTER_MINPERIOD || st->postfilter_period_old == 0); |
161 | 516k | celt_assert(st->postfilter_tapset <= 2); |
162 | 516k | celt_assert(st->postfilter_tapset >= 0); |
163 | 516k | celt_assert(st->postfilter_tapset_old <= 2); |
164 | 516k | celt_assert(st->postfilter_tapset_old >= 0); |
165 | 516k | } Line | Count | Source | 132 | 265k | { | 133 | 265k | #if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT) | 134 | 265k | celt_assert(st->mode == opus_custom_mode_create(48000, 960, NULL)); | 135 | 265k | celt_assert(st->overlap == 120); | 136 | 265k | celt_assert(st->end <= 21); | 137 | | #else | 138 | | /* From Section 4.3 in the spec: "The normal CELT layer uses 21 of those bands, | 139 | | though Opus Custom (see Section 6.2) may use a different number of bands" | 140 | | | 141 | | Check if it's within the maximum number of Bark frequency bands instead */ | 142 | | celt_assert(st->end <= 25); | 143 | | #endif | 144 | 265k | celt_assert(st->channels == 1 || st->channels == 2); | 145 | 265k | celt_assert(st->stream_channels == 1 || st->stream_channels == 2); | 146 | 265k | celt_assert(st->downsample > 0); | 147 | 265k | celt_assert(st->start == 0 || st->start == 17); | 148 | 265k | celt_assert(st->start < st->end); | 149 | 265k | #ifdef OPUS_ARCHMASK | 150 | 265k | celt_assert(st->arch >= 0); | 151 | 265k | celt_assert(st->arch <= OPUS_ARCHMASK); | 152 | 265k | #endif | 153 | 265k | #ifndef ENABLE_QEXT | 154 | 265k | celt_assert(st->last_pitch_index <= PLC_PITCH_LAG_MAX); | 155 | 265k | celt_assert(st->last_pitch_index >= PLC_PITCH_LAG_MIN || st->last_pitch_index == 0); | 156 | 265k | #endif | 157 | 265k | celt_assert(st->postfilter_period < MAX_PERIOD); | 158 | 265k | celt_assert(st->postfilter_period >= COMBFILTER_MINPERIOD || st->postfilter_period == 0); | 159 | 265k | celt_assert(st->postfilter_period_old < MAX_PERIOD); | 160 | 265k | celt_assert(st->postfilter_period_old >= COMBFILTER_MINPERIOD || st->postfilter_period_old == 0); | 161 | 265k | celt_assert(st->postfilter_tapset <= 2); | 162 | 265k | celt_assert(st->postfilter_tapset >= 0); | 163 | 265k | celt_assert(st->postfilter_tapset_old <= 2); | 164 | 265k | celt_assert(st->postfilter_tapset_old >= 0); | 165 | 265k | } |
Line | Count | Source | 132 | 250k | { | 133 | | #if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT) | 134 | | celt_assert(st->mode == opus_custom_mode_create(48000, 960, NULL)); | 135 | | celt_assert(st->overlap == 120); | 136 | | celt_assert(st->end <= 21); | 137 | | #else | 138 | | /* From Section 4.3 in the spec: "The normal CELT layer uses 21 of those bands, | 139 | | though Opus Custom (see Section 6.2) may use a different number of bands" | 140 | | | 141 | | Check if it's within the maximum number of Bark frequency bands instead */ | 142 | 250k | celt_assert(st->end <= 25); | 143 | 250k | #endif | 144 | 250k | celt_assert(st->channels == 1 || st->channels == 2); | 145 | 250k | celt_assert(st->stream_channels == 1 || st->stream_channels == 2); | 146 | 250k | celt_assert(st->downsample > 0); | 147 | 250k | celt_assert(st->start == 0 || st->start == 17); | 148 | 250k | celt_assert(st->start < st->end); | 149 | 250k | #ifdef OPUS_ARCHMASK | 150 | 250k | celt_assert(st->arch >= 0); | 151 | 250k | celt_assert(st->arch <= OPUS_ARCHMASK); | 152 | 250k | #endif | 153 | | #ifndef ENABLE_QEXT | 154 | | celt_assert(st->last_pitch_index <= PLC_PITCH_LAG_MAX); | 155 | | celt_assert(st->last_pitch_index >= PLC_PITCH_LAG_MIN || st->last_pitch_index == 0); | 156 | | #endif | 157 | 250k | celt_assert(st->postfilter_period < MAX_PERIOD); | 158 | 250k | celt_assert(st->postfilter_period >= COMBFILTER_MINPERIOD || st->postfilter_period == 0); | 159 | 250k | celt_assert(st->postfilter_period_old < MAX_PERIOD); | 160 | 250k | celt_assert(st->postfilter_period_old >= COMBFILTER_MINPERIOD || st->postfilter_period_old == 0); | 161 | 250k | celt_assert(st->postfilter_tapset <= 2); | 162 | 250k | celt_assert(st->postfilter_tapset >= 0); | 163 | 250k | celt_assert(st->postfilter_tapset_old <= 2); | 164 | 250k | celt_assert(st->postfilter_tapset_old >= 0); | 165 | 250k | } |
|
166 | | #endif |
167 | | |
168 | | int celt_decoder_get_size(int channels) |
169 | 833k | { |
170 | | #ifdef ENABLE_QEXT |
171 | | const CELTMode *mode = opus_custom_mode_create(96000, 960, NULL); |
172 | | #else |
173 | 833k | const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL); |
174 | 833k | #endif |
175 | 833k | return opus_custom_decoder_get_size(mode, channels); |
176 | 833k | } |
177 | | |
178 | | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels) |
179 | 1.65M | { |
180 | 1.65M | int size; |
181 | 1.65M | int extra=0; |
182 | | #ifdef ENABLE_QEXT |
183 | | int qext_scale; |
184 | 887k | extra = 2*NB_QEXT_BANDS*sizeof(celt_glog); |
185 | 887k | if (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)) { |
186 | 534k | qext_scale = 2; |
187 | 534k | } else qext_scale = 1; |
188 | | #endif |
189 | 1.65M | size = sizeof(struct CELTDecoder) |
190 | 1.65M | + (channels*(QEXT_SCALE(DECODE_BUFFER_SIZE)+mode->overlap)-1)*sizeof(celt_sig) |
191 | 1.65M | + channels*CELT_LPC_ORDER*sizeof(opus_val16) |
192 | 1.65M | + 4*2*mode->nbEBands*sizeof(celt_glog) |
193 | 1.65M | + extra; |
194 | 1.65M | return size; |
195 | 1.65M | } celt_decoder.c:opus_custom_decoder_get_size Line | Count | Source | 179 | 766k | { | 180 | 766k | int size; | 181 | 766k | int extra=0; | 182 | | #ifdef ENABLE_QEXT | 183 | | int qext_scale; | 184 | | extra = 2*NB_QEXT_BANDS*sizeof(celt_glog); | 185 | | if (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)) { | 186 | | qext_scale = 2; | 187 | | } else qext_scale = 1; | 188 | | #endif | 189 | 766k | size = sizeof(struct CELTDecoder) | 190 | 766k | + (channels*(QEXT_SCALE(DECODE_BUFFER_SIZE)+mode->overlap)-1)*sizeof(celt_sig) | 191 | 766k | + channels*CELT_LPC_ORDER*sizeof(opus_val16) | 192 | 766k | + 4*2*mode->nbEBands*sizeof(celt_glog) | 193 | 766k | + extra; | 194 | 766k | return size; | 195 | 766k | } |
celt_decoder.c:opus_custom_decoder_get_size Line | Count | Source | 179 | 887k | { | 180 | 887k | int size; | 181 | 887k | int extra=0; | 182 | 887k | #ifdef ENABLE_QEXT | 183 | 887k | int qext_scale; | 184 | 887k | extra = 2*NB_QEXT_BANDS*sizeof(celt_glog); | 185 | 887k | if (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)) { | 186 | 534k | qext_scale = 2; | 187 | 534k | } else qext_scale = 1; | 188 | 887k | #endif | 189 | 887k | size = sizeof(struct CELTDecoder) | 190 | 887k | + (channels*(QEXT_SCALE(DECODE_BUFFER_SIZE)+mode->overlap)-1)*sizeof(celt_sig) | 191 | 887k | + channels*CELT_LPC_ORDER*sizeof(opus_val16) | 192 | 887k | + 4*2*mode->nbEBands*sizeof(celt_glog) | 193 | 887k | + extra; | 194 | 887k | return size; | 195 | 887k | } |
|
196 | | |
197 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) |
198 | | CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error) |
199 | | { |
200 | | int ret; |
201 | | CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels)); |
202 | | ret = opus_custom_decoder_init(st, mode, channels); |
203 | | if (ret != OPUS_OK) |
204 | | { |
205 | | opus_custom_decoder_destroy(st); |
206 | | st = NULL; |
207 | | } |
208 | | if (error) |
209 | | *error = ret; |
210 | | return st; |
211 | | } |
212 | | #endif /* CUSTOM_MODES */ |
213 | | |
214 | | int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels) |
215 | 189k | { |
216 | 189k | int ret; |
217 | | #ifdef ENABLE_QEXT |
218 | | if (sampling_rate == 96000) { |
219 | | return opus_custom_decoder_init(st, opus_custom_mode_create(96000, 960, NULL), channels); |
220 | | } |
221 | | #endif |
222 | 189k | ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels); |
223 | 189k | if (ret != OPUS_OK) |
224 | 0 | return ret; |
225 | 189k | st->downsample = resampling_factor(sampling_rate); |
226 | 189k | if (st->downsample==0) |
227 | 0 | return OPUS_BAD_ARG; |
228 | 189k | else |
229 | 189k | return OPUS_OK; |
230 | 189k | } |
231 | | |
232 | | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels) |
233 | 189k | { |
234 | 189k | if (channels < 0 || channels > 2) |
235 | 0 | return OPUS_BAD_ARG; |
236 | | |
237 | 189k | if (st==NULL) |
238 | 0 | return OPUS_ALLOC_FAIL; |
239 | | |
240 | 189k | OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels)); |
241 | | |
242 | 189k | st->mode = mode; |
243 | 189k | st->overlap = mode->overlap; |
244 | 189k | st->stream_channels = st->channels = channels; |
245 | | |
246 | 189k | st->downsample = 1; |
247 | 189k | st->start = 0; |
248 | 189k | st->end = st->mode->effEBands; |
249 | 189k | st->signalling = 1; |
250 | 189k | #ifndef DISABLE_UPDATE_DRAFT |
251 | 189k | st->disable_inv = channels == 1; |
252 | | #else |
253 | | st->disable_inv = 0; |
254 | | #endif |
255 | 189k | st->arch = opus_select_arch(); |
256 | | |
257 | | #ifdef ENABLE_QEXT |
258 | | if (st->mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)) st->qext_scale = 2; |
259 | | else st->qext_scale = 1; |
260 | | #endif |
261 | | |
262 | 189k | opus_custom_decoder_ctl(st, OPUS_RESET_STATE); |
263 | | |
264 | 189k | return OPUS_OK; |
265 | 189k | } |
266 | | |
267 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) |
268 | | void opus_custom_decoder_destroy(CELTDecoder *st) |
269 | | { |
270 | | opus_free(st); |
271 | | } |
272 | | #endif /* CUSTOM_MODES */ |
273 | | |
274 | | #if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT) |
275 | | /* Special case for stereo with no downsampling and no accumulation. This is |
276 | | quite common and we can make it faster by processing both channels in the |
277 | | same loop, reducing overhead due to the dependency loop in the IIR filter. */ |
278 | | static void deemphasis_stereo_simple(celt_sig *in[], opus_res *pcm, int N, const opus_val16 coef0, |
279 | | celt_sig *mem) |
280 | 30.4k | { |
281 | 30.4k | celt_sig * OPUS_RESTRICT x0; |
282 | 30.4k | celt_sig * OPUS_RESTRICT x1; |
283 | 30.4k | celt_sig m0, m1; |
284 | 30.4k | int j; |
285 | 30.4k | x0=in[0]; |
286 | 30.4k | x1=in[1]; |
287 | 30.4k | m0 = mem[0]; |
288 | 30.4k | m1 = mem[1]; |
289 | 7.48M | for (j=0;j<N;j++) |
290 | 7.45M | { |
291 | 7.45M | celt_sig tmp0, tmp1; |
292 | | /* Add VERY_SMALL to x[] first to reduce dependency chain. */ |
293 | 7.45M | tmp0 = SATURATE(x0[j] + VERY_SMALL + m0, SIG_SAT); |
294 | 7.45M | tmp1 = SATURATE(x1[j] + VERY_SMALL + m1, SIG_SAT); |
295 | 7.45M | m0 = MULT16_32_Q15(coef0, tmp0); |
296 | 7.45M | m1 = MULT16_32_Q15(coef0, tmp1); |
297 | 7.45M | pcm[2*j ] = SIG2RES(tmp0); |
298 | 7.45M | pcm[2*j+1] = SIG2RES(tmp1); |
299 | 7.45M | } |
300 | 30.4k | mem[0] = m0; |
301 | 30.4k | mem[1] = m1; |
302 | 30.4k | } celt_decoder.c:deemphasis_stereo_simple Line | Count | Source | 280 | 18.2k | { | 281 | 18.2k | celt_sig * OPUS_RESTRICT x0; | 282 | 18.2k | celt_sig * OPUS_RESTRICT x1; | 283 | 18.2k | celt_sig m0, m1; | 284 | 18.2k | int j; | 285 | 18.2k | x0=in[0]; | 286 | 18.2k | x1=in[1]; | 287 | 18.2k | m0 = mem[0]; | 288 | 18.2k | m1 = mem[1]; | 289 | 3.74M | for (j=0;j<N;j++) | 290 | 3.72M | { | 291 | 3.72M | celt_sig tmp0, tmp1; | 292 | | /* Add VERY_SMALL to x[] first to reduce dependency chain. */ | 293 | 3.72M | tmp0 = SATURATE(x0[j] + VERY_SMALL + m0, SIG_SAT); | 294 | 3.72M | tmp1 = SATURATE(x1[j] + VERY_SMALL + m1, SIG_SAT); | 295 | 3.72M | m0 = MULT16_32_Q15(coef0, tmp0); | 296 | 3.72M | m1 = MULT16_32_Q15(coef0, tmp1); | 297 | 3.72M | pcm[2*j ] = SIG2RES(tmp0); | 298 | 3.72M | pcm[2*j+1] = SIG2RES(tmp1); | 299 | 3.72M | } | 300 | 18.2k | mem[0] = m0; | 301 | 18.2k | mem[1] = m1; | 302 | 18.2k | } |
celt_decoder.c:deemphasis_stereo_simple Line | Count | Source | 280 | 12.2k | { | 281 | 12.2k | celt_sig * OPUS_RESTRICT x0; | 282 | 12.2k | celt_sig * OPUS_RESTRICT x1; | 283 | 12.2k | celt_sig m0, m1; | 284 | 12.2k | int j; | 285 | 12.2k | x0=in[0]; | 286 | 12.2k | x1=in[1]; | 287 | 12.2k | m0 = mem[0]; | 288 | 12.2k | m1 = mem[1]; | 289 | 3.73M | for (j=0;j<N;j++) | 290 | 3.72M | { | 291 | 3.72M | celt_sig tmp0, tmp1; | 292 | | /* Add VERY_SMALL to x[] first to reduce dependency chain. */ | 293 | 3.72M | tmp0 = SATURATE(x0[j] + VERY_SMALL + m0, SIG_SAT); | 294 | 3.72M | tmp1 = SATURATE(x1[j] + VERY_SMALL + m1, SIG_SAT); | 295 | 3.72M | m0 = MULT16_32_Q15(coef0, tmp0); | 296 | 3.72M | m1 = MULT16_32_Q15(coef0, tmp1); | 297 | 3.72M | pcm[2*j ] = SIG2RES(tmp0); | 298 | 3.72M | pcm[2*j+1] = SIG2RES(tmp1); | 299 | 3.72M | } | 300 | 12.2k | mem[0] = m0; | 301 | 12.2k | mem[1] = m1; | 302 | 12.2k | } |
|
303 | | #endif |
304 | | |
305 | | #ifndef RESYNTH |
306 | | static |
307 | | #endif |
308 | | void deemphasis(celt_sig *in[], opus_res *pcm, int N, int C, int downsample, const opus_val16 *coef, |
309 | | celt_sig *mem, int accum) |
310 | 516k | { |
311 | 516k | int c; |
312 | 516k | int Nd; |
313 | 516k | int apply_downsampling=0; |
314 | 516k | opus_val16 coef0; |
315 | 516k | VARDECL(celt_sig, scratch); |
316 | 516k | SAVE_STACK; |
317 | | #if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT) |
318 | | /* Short version for common case. */ |
319 | 265k | if (downsample == 1 && C == 2 && !accum) |
320 | 30.4k | { |
321 | 30.4k | deemphasis_stereo_simple(in, pcm, N, coef[0], mem); |
322 | 30.4k | return; |
323 | 30.4k | } |
324 | 235k | #endif |
325 | 485k | ALLOC(scratch, N, celt_sig); |
326 | 235k | coef0 = coef[0]; |
327 | 235k | Nd = N/downsample; |
328 | 720k | c=0; do { |
329 | 720k | int j; |
330 | 720k | celt_sig * OPUS_RESTRICT x; |
331 | 720k | opus_res * OPUS_RESTRICT y; |
332 | 720k | celt_sig m = mem[c]; |
333 | 720k | x =in[c]; |
334 | 720k | y = pcm+c; |
335 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT) |
336 | 364k | if (coef[1] != 0) |
337 | 100k | { |
338 | 100k | opus_val16 coef1 = coef[1]; |
339 | 100k | opus_val16 coef3 = coef[3]; |
340 | 47.8M | for (j=0;j<N;j++) |
341 | 47.7M | { |
342 | 47.7M | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); |
343 | 47.7M | m = MULT16_32_Q15(coef0, tmp) |
344 | 47.7M | - MULT16_32_Q15(coef1, x[j]); |
345 | 47.7M | tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2); |
346 | 47.7M | scratch[j] = tmp; |
347 | 47.7M | } |
348 | 100k | apply_downsampling=1; |
349 | 100k | } else |
350 | 264k | #endif |
351 | 620k | if (downsample>1) |
352 | 525k | { |
353 | | /* Shortcut for the standard (non-custom modes) case */ |
354 | 145M | for (j=0;j<N;j++) |
355 | 145M | { |
356 | 145M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); |
357 | 145M | m = MULT16_32_Q15(coef0, tmp); |
358 | 145M | scratch[j] = tmp; |
359 | 145M | } |
360 | 525k | apply_downsampling=1; |
361 | 525k | } else { |
362 | | /* Shortcut for the standard (non-custom modes) case */ |
363 | 94.5k | if (accum) |
364 | 26.9k | { |
365 | 14.5M | for (j=0;j<N;j++) |
366 | 14.5M | { |
367 | 14.5M | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); |
368 | 14.5M | m = MULT16_32_Q15(coef0, tmp); |
369 | 14.5M | y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp)); |
370 | 14.5M | } |
371 | 26.9k | } else |
372 | 67.6k | { |
373 | 17.8M | for (j=0;j<N;j++) |
374 | 17.7M | { |
375 | 17.7M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); |
376 | 17.7M | m = MULT16_32_Q15(coef0, tmp); |
377 | 17.7M | y[j*C] = SIG2RES(tmp); |
378 | 17.7M | } |
379 | 67.6k | } |
380 | 94.5k | } |
381 | 720k | mem[c] = m; |
382 | | |
383 | 720k | if (apply_downsampling) |
384 | 626k | { |
385 | | /* Perform down-sampling */ |
386 | 626k | if (accum) |
387 | 72.8k | { |
388 | 19.2M | for (j=0;j<Nd;j++) |
389 | 19.1M | y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample])); |
390 | 72.8k | } else |
391 | 553k | { |
392 | 73.6M | for (j=0;j<Nd;j++) |
393 | 73.1M | y[j*C] = SIG2RES(scratch[j*downsample]); |
394 | 553k | } |
395 | 626k | } |
396 | 720k | } while (++c<C); |
397 | 235k | RESTORE_STACK; |
398 | 235k | } celt_decoder.c:deemphasis Line | Count | Source | 310 | 149k | { | 311 | 149k | int c; | 312 | 149k | int Nd; | 313 | 149k | int apply_downsampling=0; | 314 | 149k | opus_val16 coef0; | 315 | 149k | VARDECL(celt_sig, scratch); | 316 | 149k | SAVE_STACK; | 317 | 149k | #if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT) | 318 | | /* Short version for common case. */ | 319 | 149k | if (downsample == 1 && C == 2 && !accum) | 320 | 18.2k | { | 321 | 18.2k | deemphasis_stereo_simple(in, pcm, N, coef[0], mem); | 322 | 18.2k | return; | 323 | 18.2k | } | 324 | 131k | #endif | 325 | 131k | ALLOC(scratch, N, celt_sig); | 326 | 131k | coef0 = coef[0]; | 327 | 131k | Nd = N/downsample; | 328 | 202k | c=0; do { | 329 | 202k | int j; | 330 | 202k | celt_sig * OPUS_RESTRICT x; | 331 | 202k | opus_res * OPUS_RESTRICT y; | 332 | 202k | celt_sig m = mem[c]; | 333 | 202k | x =in[c]; | 334 | 202k | y = pcm+c; | 335 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT) | 336 | | if (coef[1] != 0) | 337 | | { | 338 | | opus_val16 coef1 = coef[1]; | 339 | | opus_val16 coef3 = coef[3]; | 340 | | for (j=0;j<N;j++) | 341 | | { | 342 | | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); | 343 | | m = MULT16_32_Q15(coef0, tmp) | 344 | | - MULT16_32_Q15(coef1, x[j]); | 345 | | tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2); | 346 | | scratch[j] = tmp; | 347 | | } | 348 | | apply_downsampling=1; | 349 | | } else | 350 | | #endif | 351 | 202k | if (downsample>1) | 352 | 179k | { | 353 | | /* Shortcut for the standard (non-custom modes) case */ | 354 | 45.3M | for (j=0;j<N;j++) | 355 | 45.1M | { | 356 | 45.1M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); | 357 | 45.1M | m = MULT16_32_Q15(coef0, tmp); | 358 | 45.1M | scratch[j] = tmp; | 359 | 45.1M | } | 360 | 179k | apply_downsampling=1; | 361 | 179k | } else { | 362 | | /* Shortcut for the standard (non-custom modes) case */ | 363 | 22.4k | if (accum) | 364 | 8.37k | { | 365 | 4.37M | for (j=0;j<N;j++) | 366 | 4.36M | { | 367 | 4.36M | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); | 368 | 4.36M | m = MULT16_32_Q15(coef0, tmp); | 369 | 4.36M | y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp)); | 370 | 4.36M | } | 371 | 8.37k | } else | 372 | 14.1k | { | 373 | 4.19M | for (j=0;j<N;j++) | 374 | 4.18M | { | 375 | 4.18M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); | 376 | 4.18M | m = MULT16_32_Q15(coef0, tmp); | 377 | 4.18M | y[j*C] = SIG2RES(tmp); | 378 | 4.18M | } | 379 | 14.1k | } | 380 | 22.4k | } | 381 | 202k | mem[c] = m; | 382 | | | 383 | 202k | if (apply_downsampling) | 384 | 179k | { | 385 | | /* Perform down-sampling */ | 386 | 179k | if (accum) | 387 | 19.6k | { | 388 | 3.11M | for (j=0;j<Nd;j++) | 389 | 3.09M | y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample])); | 390 | 19.6k | } else | 391 | 160k | { | 392 | 11.1M | for (j=0;j<Nd;j++) | 393 | 11.0M | y[j*C] = SIG2RES(scratch[j*downsample]); | 394 | 160k | } | 395 | 179k | } | 396 | 202k | } while (++c<C); | 397 | 131k | RESTORE_STACK; | 398 | 131k | } |
celt_decoder.c:deemphasis Line | Count | Source | 310 | 136k | { | 311 | 136k | int c; | 312 | 136k | int Nd; | 313 | 136k | int apply_downsampling=0; | 314 | 136k | opus_val16 coef0; | 315 | 136k | VARDECL(celt_sig, scratch); | 316 | 136k | SAVE_STACK; | 317 | | #if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT) | 318 | | /* Short version for common case. */ | 319 | | if (downsample == 1 && C == 2 && !accum) | 320 | | { | 321 | | deemphasis_stereo_simple(in, pcm, N, coef[0], mem); | 322 | | return; | 323 | | } | 324 | | #endif | 325 | 136k | ALLOC(scratch, N, celt_sig); | 326 | 136k | coef0 = coef[0]; | 327 | 136k | Nd = N/downsample; | 328 | 191k | c=0; do { | 329 | 191k | int j; | 330 | 191k | celt_sig * OPUS_RESTRICT x; | 331 | 191k | opus_res * OPUS_RESTRICT y; | 332 | 191k | celt_sig m = mem[c]; | 333 | 191k | x =in[c]; | 334 | 191k | y = pcm+c; | 335 | 191k | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT) | 336 | 191k | if (coef[1] != 0) | 337 | 56.2k | { | 338 | 56.2k | opus_val16 coef1 = coef[1]; | 339 | 56.2k | opus_val16 coef3 = coef[3]; | 340 | 28.3M | for (j=0;j<N;j++) | 341 | 28.3M | { | 342 | 28.3M | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); | 343 | 28.3M | m = MULT16_32_Q15(coef0, tmp) | 344 | 28.3M | - MULT16_32_Q15(coef1, x[j]); | 345 | 28.3M | tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2); | 346 | 28.3M | scratch[j] = tmp; | 347 | 28.3M | } | 348 | 56.2k | apply_downsampling=1; | 349 | 56.2k | } else | 350 | 135k | #endif | 351 | 135k | if (downsample>1) | 352 | 107k | { | 353 | | /* Shortcut for the standard (non-custom modes) case */ | 354 | 26.7M | for (j=0;j<N;j++) | 355 | 26.6M | { | 356 | 26.6M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); | 357 | 26.6M | m = MULT16_32_Q15(coef0, tmp); | 358 | 26.6M | scratch[j] = tmp; | 359 | 26.6M | } | 360 | 107k | apply_downsampling=1; | 361 | 107k | } else { | 362 | | /* Shortcut for the standard (non-custom modes) case */ | 363 | 28.2k | if (accum) | 364 | 5.81k | { | 365 | 3.21M | for (j=0;j<N;j++) | 366 | 3.20M | { | 367 | 3.20M | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); | 368 | 3.20M | m = MULT16_32_Q15(coef0, tmp); | 369 | 3.20M | y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp)); | 370 | 3.20M | } | 371 | 5.81k | } else | 372 | 22.4k | { | 373 | 5.71M | for (j=0;j<N;j++) | 374 | 5.69M | { | 375 | 5.69M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); | 376 | 5.69M | m = MULT16_32_Q15(coef0, tmp); | 377 | 5.69M | y[j*C] = SIG2RES(tmp); | 378 | 5.69M | } | 379 | 22.4k | } | 380 | 28.2k | } | 381 | 191k | mem[c] = m; | 382 | | | 383 | 191k | if (apply_downsampling) | 384 | 163k | { | 385 | | /* Perform down-sampling */ | 386 | 163k | if (accum) | 387 | 14.7k | { | 388 | 6.78M | for (j=0;j<Nd;j++) | 389 | 6.76M | y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample])); | 390 | 14.7k | } else | 391 | 148k | { | 392 | 28.9M | for (j=0;j<Nd;j++) | 393 | 28.7M | y[j*C] = SIG2RES(scratch[j*downsample]); | 394 | 148k | } | 395 | 163k | } | 396 | 191k | } while (++c<C); | 397 | 136k | RESTORE_STACK; | 398 | 136k | } |
celt_decoder.c:deemphasis Line | Count | Source | 310 | 114k | { | 311 | 114k | int c; | 312 | 114k | int Nd; | 313 | 114k | int apply_downsampling=0; | 314 | 114k | opus_val16 coef0; | 315 | 114k | VARDECL(celt_sig, scratch); | 316 | 114k | SAVE_STACK; | 317 | | #if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT) | 318 | | /* Short version for common case. */ | 319 | | if (downsample == 1 && C == 2 && !accum) | 320 | | { | 321 | | deemphasis_stereo_simple(in, pcm, N, coef[0], mem); | 322 | | return; | 323 | | } | 324 | | #endif | 325 | 114k | ALLOC(scratch, N, celt_sig); | 326 | 114k | coef0 = coef[0]; | 327 | 114k | Nd = N/downsample; | 328 | 172k | c=0; do { | 329 | 172k | int j; | 330 | 172k | celt_sig * OPUS_RESTRICT x; | 331 | 172k | opus_res * OPUS_RESTRICT y; | 332 | 172k | celt_sig m = mem[c]; | 333 | 172k | x =in[c]; | 334 | 172k | y = pcm+c; | 335 | 172k | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT) | 336 | 172k | if (coef[1] != 0) | 337 | 43.8k | { | 338 | 43.8k | opus_val16 coef1 = coef[1]; | 339 | 43.8k | opus_val16 coef3 = coef[3]; | 340 | 19.5M | for (j=0;j<N;j++) | 341 | 19.4M | { | 342 | 19.4M | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); | 343 | 19.4M | m = MULT16_32_Q15(coef0, tmp) | 344 | 19.4M | - MULT16_32_Q15(coef1, x[j]); | 345 | 19.4M | tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2); | 346 | 19.4M | scratch[j] = tmp; | 347 | 19.4M | } | 348 | 43.8k | apply_downsampling=1; | 349 | 43.8k | } else | 350 | 129k | #endif | 351 | 129k | if (downsample>1) | 352 | 105k | { | 353 | | /* Shortcut for the standard (non-custom modes) case */ | 354 | 30.0M | for (j=0;j<N;j++) | 355 | 29.9M | { | 356 | 29.9M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); | 357 | 29.9M | m = MULT16_32_Q15(coef0, tmp); | 358 | 29.9M | scratch[j] = tmp; | 359 | 29.9M | } | 360 | 105k | apply_downsampling=1; | 361 | 105k | } else { | 362 | | /* Shortcut for the standard (non-custom modes) case */ | 363 | 23.1k | if (accum) | 364 | 3.99k | { | 365 | 2.33M | for (j=0;j<N;j++) | 366 | 2.33M | { | 367 | 2.33M | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); | 368 | 2.33M | m = MULT16_32_Q15(coef0, tmp); | 369 | 2.33M | y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp)); | 370 | 2.33M | } | 371 | 3.99k | } else | 372 | 19.1k | { | 373 | 4.34M | for (j=0;j<N;j++) | 374 | 4.32M | { | 375 | 4.32M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); | 376 | 4.32M | m = MULT16_32_Q15(coef0, tmp); | 377 | 4.32M | y[j*C] = SIG2RES(tmp); | 378 | 4.32M | } | 379 | 19.1k | } | 380 | 23.1k | } | 381 | 172k | mem[c] = m; | 382 | | | 383 | 172k | if (apply_downsampling) | 384 | 149k | { | 385 | | /* Perform down-sampling */ | 386 | 149k | if (accum) | 387 | 16.1k | { | 388 | 5.78M | for (j=0;j<Nd;j++) | 389 | 5.77M | y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample])); | 390 | 16.1k | } else | 391 | 133k | { | 392 | 22.6M | for (j=0;j<Nd;j++) | 393 | 22.5M | y[j*C] = SIG2RES(scratch[j*downsample]); | 394 | 133k | } | 395 | 149k | } | 396 | 172k | } while (++c<C); | 397 | 114k | RESTORE_STACK; | 398 | 114k | } |
celt_decoder.c:deemphasis Line | Count | Source | 310 | 115k | { | 311 | 115k | int c; | 312 | 115k | int Nd; | 313 | 115k | int apply_downsampling=0; | 314 | 115k | opus_val16 coef0; | 315 | 115k | VARDECL(celt_sig, scratch); | 316 | 115k | SAVE_STACK; | 317 | 115k | #if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT) | 318 | | /* Short version for common case. */ | 319 | 115k | if (downsample == 1 && C == 2 && !accum) | 320 | 12.2k | { | 321 | 12.2k | deemphasis_stereo_simple(in, pcm, N, coef[0], mem); | 322 | 12.2k | return; | 323 | 12.2k | } | 324 | 103k | #endif | 325 | 103k | ALLOC(scratch, N, celt_sig); | 326 | 103k | coef0 = coef[0]; | 327 | 103k | Nd = N/downsample; | 328 | 153k | c=0; do { | 329 | 153k | int j; | 330 | 153k | celt_sig * OPUS_RESTRICT x; | 331 | 153k | opus_res * OPUS_RESTRICT y; | 332 | 153k | celt_sig m = mem[c]; | 333 | 153k | x =in[c]; | 334 | 153k | y = pcm+c; | 335 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT) | 336 | | if (coef[1] != 0) | 337 | | { | 338 | | opus_val16 coef1 = coef[1]; | 339 | | opus_val16 coef3 = coef[3]; | 340 | | for (j=0;j<N;j++) | 341 | | { | 342 | | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); | 343 | | m = MULT16_32_Q15(coef0, tmp) | 344 | | - MULT16_32_Q15(coef1, x[j]); | 345 | | tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2); | 346 | | scratch[j] = tmp; | 347 | | } | 348 | | apply_downsampling=1; | 349 | | } else | 350 | | #endif | 351 | 153k | if (downsample>1) | 352 | 132k | { | 353 | | /* Shortcut for the standard (non-custom modes) case */ | 354 | 43.4M | for (j=0;j<N;j++) | 355 | 43.3M | { | 356 | 43.3M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); | 357 | 43.3M | m = MULT16_32_Q15(coef0, tmp); | 358 | 43.3M | scratch[j] = tmp; | 359 | 43.3M | } | 360 | 132k | apply_downsampling=1; | 361 | 132k | } else { | 362 | | /* Shortcut for the standard (non-custom modes) case */ | 363 | 20.6k | if (accum) | 364 | 8.73k | { | 365 | 4.63M | for (j=0;j<N;j++) | 366 | 4.62M | { | 367 | 4.62M | celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT); | 368 | 4.62M | m = MULT16_32_Q15(coef0, tmp); | 369 | 4.62M | y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp)); | 370 | 4.62M | } | 371 | 8.73k | } else | 372 | 11.9k | { | 373 | 3.58M | for (j=0;j<N;j++) | 374 | 3.57M | { | 375 | 3.57M | celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT); | 376 | 3.57M | m = MULT16_32_Q15(coef0, tmp); | 377 | 3.57M | y[j*C] = SIG2RES(tmp); | 378 | 3.57M | } | 379 | 11.9k | } | 380 | 20.6k | } | 381 | 153k | mem[c] = m; | 382 | | | 383 | 153k | if (apply_downsampling) | 384 | 132k | { | 385 | | /* Perform down-sampling */ | 386 | 132k | if (accum) | 387 | 22.2k | { | 388 | 3.53M | for (j=0;j<Nd;j++) | 389 | 3.51M | y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample])); | 390 | 22.2k | } else | 391 | 110k | { | 392 | 10.9M | for (j=0;j<Nd;j++) | 393 | 10.8M | y[j*C] = SIG2RES(scratch[j*downsample]); | 394 | 110k | } | 395 | 132k | } | 396 | 153k | } while (++c<C); | 397 | 103k | RESTORE_STACK; | 398 | 103k | } |
|
399 | | |
400 | | #ifndef RESYNTH |
401 | | static |
402 | | #endif |
403 | | void celt_synthesis(const CELTMode *mode, celt_norm *X, celt_sig * out_syn[], |
404 | | celt_glog *oldBandE, int start, int effEnd, int C, int CC, |
405 | | int isTransient, int LM, int downsample, |
406 | | int silence, int arch ARG_QEXT(const CELTMode *qext_mode) ARG_QEXT(const celt_glog *qext_bandLogE) ARG_QEXT(int qext_end)) |
407 | 340k | { |
408 | 340k | int c, i; |
409 | 340k | int M; |
410 | 340k | int b; |
411 | 340k | int B; |
412 | 340k | int N, NB; |
413 | 340k | int shift; |
414 | 340k | int nbEBands; |
415 | 340k | int overlap; |
416 | 340k | VARDECL(celt_sig, freq); |
417 | 340k | SAVE_STACK; |
418 | | |
419 | 340k | overlap = mode->overlap; |
420 | 340k | nbEBands = mode->nbEBands; |
421 | 340k | N = mode->shortMdctSize<<LM; |
422 | 340k | ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */ |
423 | 340k | M = 1<<LM; |
424 | | #ifdef ENABLE_QEXT |
425 | 165k | if (mode->Fs != 96000) qext_end=2; |
426 | | #endif |
427 | | |
428 | 340k | if (isTransient) |
429 | 26.8k | { |
430 | 26.8k | B = M; |
431 | 26.8k | NB = mode->shortMdctSize; |
432 | 26.8k | shift = mode->maxLM; |
433 | 313k | } else { |
434 | 313k | B = 1; |
435 | 313k | NB = mode->shortMdctSize<<LM; |
436 | 313k | shift = mode->maxLM-LM; |
437 | 313k | } |
438 | | |
439 | 340k | if (CC==2&&C==1) |
440 | 105k | { |
441 | | /* Copying a mono streams to two channels */ |
442 | 105k | celt_sig *freq2; |
443 | 105k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, |
444 | 105k | downsample, silence); |
445 | | #ifdef ENABLE_QEXT |
446 | 46.1k | if (qext_mode) |
447 | 828 | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, |
448 | 828 | downsample, silence); |
449 | | #endif |
450 | | /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */ |
451 | 105k | freq2 = out_syn[1]+overlap/2; |
452 | 105k | OPUS_COPY(freq2, freq, N); |
453 | 230k | for (b=0;b<B;b++) |
454 | 125k | clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); |
455 | 230k | for (b=0;b<B;b++) |
456 | 125k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch); |
457 | 235k | } else if (CC==1&&C==2) |
458 | 62.6k | { |
459 | | /* Downmixing a stereo stream to mono */ |
460 | 62.6k | celt_sig *freq2; |
461 | 62.6k | freq2 = out_syn[0]+overlap/2; |
462 | 62.6k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, |
463 | 62.6k | downsample, silence); |
464 | | /* Use the output buffer as temp array before downmixing. */ |
465 | 62.6k | denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M, |
466 | 62.6k | downsample, silence); |
467 | | #ifdef ENABLE_QEXT |
468 | 31.7k | if (qext_mode) |
469 | 3.86k | { |
470 | 3.86k | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, |
471 | 3.86k | downsample, silence); |
472 | 3.86k | denormalise_bands(qext_mode, X+N, freq2, qext_bandLogE+NB_QEXT_BANDS, 0, qext_end, M, |
473 | 3.86k | downsample, silence); |
474 | 3.86k | } |
475 | | #endif |
476 | 26.3M | for (i=0;i<N;i++) |
477 | 26.2M | freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i])); |
478 | 153k | for (b=0;b<B;b++) |
479 | 91.1k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); |
480 | 172k | } else { |
481 | | /* Normal case (mono or stereo) */ |
482 | 235k | c=0; do { |
483 | 235k | denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M, |
484 | 235k | downsample, silence); |
485 | | #ifdef ENABLE_QEXT |
486 | 114k | if (qext_mode) |
487 | 5.24k | denormalise_bands(qext_mode, X+c*N, freq, qext_bandLogE+c*NB_QEXT_BANDS, 0, qext_end, M, |
488 | 5.24k | downsample, silence); |
489 | | #endif |
490 | 520k | for (b=0;b<B;b++) |
491 | 285k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch); |
492 | 235k | } while (++c<CC); |
493 | 172k | } |
494 | | /* Saturate IMDCT output so that we can't overflow in the pitch postfilter |
495 | | or in the */ |
496 | 508k | c=0; do { |
497 | 179M | for (i=0;i<N;i++) |
498 | 178M | out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT); |
499 | 508k | } while (++c<CC); |
500 | 340k | RESTORE_STACK; |
501 | 340k | } celt_decoder.c:celt_synthesis Line | Count | Source | 407 | 94.7k | { | 408 | 94.7k | int c, i; | 409 | 94.7k | int M; | 410 | 94.7k | int b; | 411 | 94.7k | int B; | 412 | 94.7k | int N, NB; | 413 | 94.7k | int shift; | 414 | 94.7k | int nbEBands; | 415 | 94.7k | int overlap; | 416 | 94.7k | VARDECL(celt_sig, freq); | 417 | 94.7k | SAVE_STACK; | 418 | | | 419 | 94.7k | overlap = mode->overlap; | 420 | 94.7k | nbEBands = mode->nbEBands; | 421 | 94.7k | N = mode->shortMdctSize<<LM; | 422 | 94.7k | ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */ | 423 | 94.7k | M = 1<<LM; | 424 | | #ifdef ENABLE_QEXT | 425 | | if (mode->Fs != 96000) qext_end=2; | 426 | | #endif | 427 | | | 428 | 94.7k | if (isTransient) | 429 | 8.09k | { | 430 | 8.09k | B = M; | 431 | 8.09k | NB = mode->shortMdctSize; | 432 | 8.09k | shift = mode->maxLM; | 433 | 86.6k | } else { | 434 | 86.6k | B = 1; | 435 | 86.6k | NB = mode->shortMdctSize<<LM; | 436 | 86.6k | shift = mode->maxLM-LM; | 437 | 86.6k | } | 438 | | | 439 | 94.7k | if (CC==2&&C==1) | 440 | 35.2k | { | 441 | | /* Copying a mono streams to two channels */ | 442 | 35.2k | celt_sig *freq2; | 443 | 35.2k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | 444 | 35.2k | downsample, silence); | 445 | | #ifdef ENABLE_QEXT | 446 | | if (qext_mode) | 447 | | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, | 448 | | downsample, silence); | 449 | | #endif | 450 | | /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */ | 451 | 35.2k | freq2 = out_syn[1]+overlap/2; | 452 | 35.2k | OPUS_COPY(freq2, freq, N); | 453 | 77.5k | for (b=0;b<B;b++) | 454 | 42.3k | clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); | 455 | 77.5k | for (b=0;b<B;b++) | 456 | 42.3k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch); | 457 | 59.5k | } else if (CC==1&&C==2) | 458 | 16.9k | { | 459 | | /* Downmixing a stereo stream to mono */ | 460 | 16.9k | celt_sig *freq2; | 461 | 16.9k | freq2 = out_syn[0]+overlap/2; | 462 | 16.9k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | 463 | 16.9k | downsample, silence); | 464 | | /* Use the output buffer as temp array before downmixing. */ | 465 | 16.9k | denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M, | 466 | 16.9k | downsample, silence); | 467 | | #ifdef ENABLE_QEXT | 468 | | if (qext_mode) | 469 | | { | 470 | | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, | 471 | | downsample, silence); | 472 | | denormalise_bands(qext_mode, X+N, freq2, qext_bandLogE+NB_QEXT_BANDS, 0, qext_end, M, | 473 | | downsample, silence); | 474 | | } | 475 | | #endif | 476 | 6.11M | for (i=0;i<N;i++) | 477 | 6.10M | freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i])); | 478 | 43.1k | for (b=0;b<B;b++) | 479 | 26.2k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); | 480 | 42.5k | } else { | 481 | | /* Normal case (mono or stereo) */ | 482 | 59.9k | c=0; do { | 483 | 59.9k | denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M, | 484 | 59.9k | downsample, silence); | 485 | | #ifdef ENABLE_QEXT | 486 | | if (qext_mode) | 487 | | denormalise_bands(qext_mode, X+c*N, freq, qext_bandLogE+c*NB_QEXT_BANDS, 0, qext_end, M, | 488 | | downsample, silence); | 489 | | #endif | 490 | 136k | for (b=0;b<B;b++) | 491 | 76.6k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch); | 492 | 59.9k | } while (++c<CC); | 493 | 42.5k | } | 494 | | /* Saturate IMDCT output so that we can't overflow in the pitch postfilter | 495 | | or in the */ | 496 | 147k | c=0; do { | 497 | 43.9M | for (i=0;i<N;i++) | 498 | 43.7M | out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT); | 499 | 147k | } while (++c<CC); | 500 | 94.7k | RESTORE_STACK; | 501 | 94.7k | } |
celt_decoder.c:celt_synthesis Line | Count | Source | 407 | 87.8k | { | 408 | 87.8k | int c, i; | 409 | 87.8k | int M; | 410 | 87.8k | int b; | 411 | 87.8k | int B; | 412 | 87.8k | int N, NB; | 413 | 87.8k | int shift; | 414 | 87.8k | int nbEBands; | 415 | 87.8k | int overlap; | 416 | 87.8k | VARDECL(celt_sig, freq); | 417 | 87.8k | SAVE_STACK; | 418 | | | 419 | 87.8k | overlap = mode->overlap; | 420 | 87.8k | nbEBands = mode->nbEBands; | 421 | 87.8k | N = mode->shortMdctSize<<LM; | 422 | 87.8k | ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */ | 423 | 87.8k | M = 1<<LM; | 424 | 87.8k | #ifdef ENABLE_QEXT | 425 | 87.8k | if (mode->Fs != 96000) qext_end=2; | 426 | 87.8k | #endif | 427 | | | 428 | 87.8k | if (isTransient) | 429 | 5.80k | { | 430 | 5.80k | B = M; | 431 | 5.80k | NB = mode->shortMdctSize; | 432 | 5.80k | shift = mode->maxLM; | 433 | 82.0k | } else { | 434 | 82.0k | B = 1; | 435 | 82.0k | NB = mode->shortMdctSize<<LM; | 436 | 82.0k | shift = mode->maxLM-LM; | 437 | 82.0k | } | 438 | | | 439 | 87.8k | if (CC==2&&C==1) | 440 | 20.4k | { | 441 | | /* Copying a mono streams to two channels */ | 442 | 20.4k | celt_sig *freq2; | 443 | 20.4k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | 444 | 20.4k | downsample, silence); | 445 | 20.4k | #ifdef ENABLE_QEXT | 446 | 20.4k | if (qext_mode) | 447 | 361 | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, | 448 | 361 | downsample, silence); | 449 | 20.4k | #endif | 450 | | /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */ | 451 | 20.4k | freq2 = out_syn[1]+overlap/2; | 452 | 20.4k | OPUS_COPY(freq2, freq, N); | 453 | 44.1k | for (b=0;b<B;b++) | 454 | 23.6k | clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); | 455 | 44.1k | for (b=0;b<B;b++) | 456 | 23.6k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch); | 457 | 67.4k | } else if (CC==1&&C==2) | 458 | 17.1k | { | 459 | | /* Downmixing a stereo stream to mono */ | 460 | 17.1k | celt_sig *freq2; | 461 | 17.1k | freq2 = out_syn[0]+overlap/2; | 462 | 17.1k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | 463 | 17.1k | downsample, silence); | 464 | | /* Use the output buffer as temp array before downmixing. */ | 465 | 17.1k | denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M, | 466 | 17.1k | downsample, silence); | 467 | 17.1k | #ifdef ENABLE_QEXT | 468 | 17.1k | if (qext_mode) | 469 | 2.56k | { | 470 | 2.56k | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, | 471 | 2.56k | downsample, silence); | 472 | 2.56k | denormalise_bands(qext_mode, X+N, freq2, qext_bandLogE+NB_QEXT_BANDS, 0, qext_end, M, | 473 | 2.56k | downsample, silence); | 474 | 2.56k | } | 475 | 17.1k | #endif | 476 | 7.66M | for (i=0;i<N;i++) | 477 | 7.64M | freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i])); | 478 | 40.2k | for (b=0;b<B;b++) | 479 | 23.0k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); | 480 | 50.2k | } else { | 481 | | /* Normal case (mono or stereo) */ | 482 | 66.3k | c=0; do { | 483 | 66.3k | denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M, | 484 | 66.3k | downsample, silence); | 485 | 66.3k | #ifdef ENABLE_QEXT | 486 | 66.3k | if (qext_mode) | 487 | 2.08k | denormalise_bands(qext_mode, X+c*N, freq, qext_bandLogE+c*NB_QEXT_BANDS, 0, qext_end, M, | 488 | 2.08k | downsample, silence); | 489 | 66.3k | #endif | 490 | 144k | for (b=0;b<B;b++) | 491 | 78.5k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch); | 492 | 66.3k | } while (++c<CC); | 493 | 50.2k | } | 494 | | /* Saturate IMDCT output so that we can't overflow in the pitch postfilter | 495 | | or in the */ | 496 | 124k | c=0; do { | 497 | 47.9M | for (i=0;i<N;i++) | 498 | 47.8M | out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT); | 499 | 124k | } while (++c<CC); | 500 | 87.8k | RESTORE_STACK; | 501 | 87.8k | } |
celt_decoder.c:celt_synthesis Line | Count | Source | 407 | 77.2k | { | 408 | 77.2k | int c, i; | 409 | 77.2k | int M; | 410 | 77.2k | int b; | 411 | 77.2k | int B; | 412 | 77.2k | int N, NB; | 413 | 77.2k | int shift; | 414 | 77.2k | int nbEBands; | 415 | 77.2k | int overlap; | 416 | 77.2k | VARDECL(celt_sig, freq); | 417 | 77.2k | SAVE_STACK; | 418 | | | 419 | 77.2k | overlap = mode->overlap; | 420 | 77.2k | nbEBands = mode->nbEBands; | 421 | 77.2k | N = mode->shortMdctSize<<LM; | 422 | 77.2k | ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */ | 423 | 77.2k | M = 1<<LM; | 424 | 77.2k | #ifdef ENABLE_QEXT | 425 | 77.2k | if (mode->Fs != 96000) qext_end=2; | 426 | 77.2k | #endif | 427 | | | 428 | 77.2k | if (isTransient) | 429 | 6.19k | { | 430 | 6.19k | B = M; | 431 | 6.19k | NB = mode->shortMdctSize; | 432 | 6.19k | shift = mode->maxLM; | 433 | 71.0k | } else { | 434 | 71.0k | B = 1; | 435 | 71.0k | NB = mode->shortMdctSize<<LM; | 436 | 71.0k | shift = mode->maxLM-LM; | 437 | 71.0k | } | 438 | | | 439 | 77.2k | if (CC==2&&C==1) | 440 | 25.7k | { | 441 | | /* Copying a mono streams to two channels */ | 442 | 25.7k | celt_sig *freq2; | 443 | 25.7k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | 444 | 25.7k | downsample, silence); | 445 | 25.7k | #ifdef ENABLE_QEXT | 446 | 25.7k | if (qext_mode) | 447 | 467 | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, | 448 | 467 | downsample, silence); | 449 | 25.7k | #endif | 450 | | /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */ | 451 | 25.7k | freq2 = out_syn[1]+overlap/2; | 452 | 25.7k | OPUS_COPY(freq2, freq, N); | 453 | 54.4k | for (b=0;b<B;b++) | 454 | 28.7k | clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); | 455 | 54.4k | for (b=0;b<B;b++) | 456 | 28.7k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch); | 457 | 51.5k | } else if (CC==1&&C==2) | 458 | 14.5k | { | 459 | | /* Downmixing a stereo stream to mono */ | 460 | 14.5k | celt_sig *freq2; | 461 | 14.5k | freq2 = out_syn[0]+overlap/2; | 462 | 14.5k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | 463 | 14.5k | downsample, silence); | 464 | | /* Use the output buffer as temp array before downmixing. */ | 465 | 14.5k | denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M, | 466 | 14.5k | downsample, silence); | 467 | 14.5k | #ifdef ENABLE_QEXT | 468 | 14.5k | if (qext_mode) | 469 | 1.30k | { | 470 | 1.30k | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, | 471 | 1.30k | downsample, silence); | 472 | 1.30k | denormalise_bands(qext_mode, X+N, freq2, qext_bandLogE+NB_QEXT_BANDS, 0, qext_end, M, | 473 | 1.30k | downsample, silence); | 474 | 1.30k | } | 475 | 14.5k | #endif | 476 | 6.83M | for (i=0;i<N;i++) | 477 | 6.81M | freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i])); | 478 | 35.3k | for (b=0;b<B;b++) | 479 | 20.7k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); | 480 | 36.9k | } else { | 481 | | /* Normal case (mono or stereo) */ | 482 | 48.0k | c=0; do { | 483 | 48.0k | denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M, | 484 | 48.0k | downsample, silence); | 485 | 48.0k | #ifdef ENABLE_QEXT | 486 | 48.0k | if (qext_mode) | 487 | 3.16k | denormalise_bands(qext_mode, X+c*N, freq, qext_bandLogE+c*NB_QEXT_BANDS, 0, qext_end, M, | 488 | 3.16k | downsample, silence); | 489 | 48.0k | #endif | 490 | 105k | for (b=0;b<B;b++) | 491 | 57.1k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch); | 492 | 48.0k | } while (++c<CC); | 493 | 36.9k | } | 494 | | /* Saturate IMDCT output so that we can't overflow in the pitch postfilter | 495 | | or in the */ | 496 | 114k | c=0; do { | 497 | 43.2M | for (i=0;i<N;i++) | 498 | 43.1M | out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT); | 499 | 114k | } while (++c<CC); | 500 | 77.2k | RESTORE_STACK; | 501 | 77.2k | } |
celt_decoder.c:celt_synthesis Line | Count | Source | 407 | 80.7k | { | 408 | 80.7k | int c, i; | 409 | 80.7k | int M; | 410 | 80.7k | int b; | 411 | 80.7k | int B; | 412 | 80.7k | int N, NB; | 413 | 80.7k | int shift; | 414 | 80.7k | int nbEBands; | 415 | 80.7k | int overlap; | 416 | 80.7k | VARDECL(celt_sig, freq); | 417 | 80.7k | SAVE_STACK; | 418 | | | 419 | 80.7k | overlap = mode->overlap; | 420 | 80.7k | nbEBands = mode->nbEBands; | 421 | 80.7k | N = mode->shortMdctSize<<LM; | 422 | 80.7k | ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */ | 423 | 80.7k | M = 1<<LM; | 424 | | #ifdef ENABLE_QEXT | 425 | | if (mode->Fs != 96000) qext_end=2; | 426 | | #endif | 427 | | | 428 | 80.7k | if (isTransient) | 429 | 6.73k | { | 430 | 6.73k | B = M; | 431 | 6.73k | NB = mode->shortMdctSize; | 432 | 6.73k | shift = mode->maxLM; | 433 | 74.0k | } else { | 434 | 74.0k | B = 1; | 435 | 74.0k | NB = mode->shortMdctSize<<LM; | 436 | 74.0k | shift = mode->maxLM-LM; | 437 | 74.0k | } | 438 | | | 439 | 80.7k | if (CC==2&&C==1) | 440 | 23.7k | { | 441 | | /* Copying a mono streams to two channels */ | 442 | 23.7k | celt_sig *freq2; | 443 | 23.7k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | 444 | 23.7k | downsample, silence); | 445 | | #ifdef ENABLE_QEXT | 446 | | if (qext_mode) | 447 | | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, | 448 | | downsample, silence); | 449 | | #endif | 450 | | /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */ | 451 | 23.7k | freq2 = out_syn[1]+overlap/2; | 452 | 23.7k | OPUS_COPY(freq2, freq, N); | 453 | 54.2k | for (b=0;b<B;b++) | 454 | 30.4k | clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); | 455 | 54.2k | for (b=0;b<B;b++) | 456 | 30.4k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch); | 457 | 56.9k | } else if (CC==1&&C==2) | 458 | 13.9k | { | 459 | | /* Downmixing a stereo stream to mono */ | 460 | 13.9k | celt_sig *freq2; | 461 | 13.9k | freq2 = out_syn[0]+overlap/2; | 462 | 13.9k | denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M, | 463 | 13.9k | downsample, silence); | 464 | | /* Use the output buffer as temp array before downmixing. */ | 465 | 13.9k | denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M, | 466 | 13.9k | downsample, silence); | 467 | | #ifdef ENABLE_QEXT | 468 | | if (qext_mode) | 469 | | { | 470 | | denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M, | 471 | | downsample, silence); | 472 | | denormalise_bands(qext_mode, X+N, freq2, qext_bandLogE+NB_QEXT_BANDS, 0, qext_end, M, | 473 | | downsample, silence); | 474 | | } | 475 | | #endif | 476 | 5.70M | for (i=0;i<N;i++) | 477 | 5.69M | freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i])); | 478 | 35.0k | for (b=0;b<B;b++) | 479 | 21.0k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch); | 480 | 43.0k | } else { | 481 | | /* Normal case (mono or stereo) */ | 482 | 60.7k | c=0; do { | 483 | 60.7k | denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M, | 484 | 60.7k | downsample, silence); | 485 | | #ifdef ENABLE_QEXT | 486 | | if (qext_mode) | 487 | | denormalise_bands(qext_mode, X+c*N, freq, qext_bandLogE+c*NB_QEXT_BANDS, 0, qext_end, M, | 488 | | downsample, silence); | 489 | | #endif | 490 | 134k | for (b=0;b<B;b++) | 491 | 73.3k | clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch); | 492 | 60.7k | } while (++c<CC); | 493 | 43.0k | } | 494 | | /* Saturate IMDCT output so that we can't overflow in the pitch postfilter | 495 | | or in the */ | 496 | 122k | c=0; do { | 497 | 44.0M | for (i=0;i<N;i++) | 498 | 43.9M | out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT); | 499 | 122k | } while (++c<CC); | 500 | 80.7k | RESTORE_STACK; | 501 | 80.7k | } |
|
502 | | |
503 | | static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec) |
504 | 303k | { |
505 | 303k | int i, curr, tf_select; |
506 | 303k | int tf_select_rsv; |
507 | 303k | int tf_changed; |
508 | 303k | int logp; |
509 | 303k | opus_uint32 budget; |
510 | 303k | opus_uint32 tell; |
511 | | |
512 | 303k | budget = dec->storage*8; |
513 | 303k | tell = ec_tell(dec); |
514 | 303k | logp = isTransient ? 2 : 4; |
515 | 303k | tf_select_rsv = LM>0 && tell+logp+1<=budget; |
516 | 303k | budget -= tf_select_rsv; |
517 | 303k | tf_changed = curr = 0; |
518 | 5.21M | for (i=start;i<end;i++) |
519 | 4.91M | { |
520 | 4.91M | if (tell+logp<=budget) |
521 | 1.85M | { |
522 | 1.85M | curr ^= ec_dec_bit_logp(dec, logp); |
523 | 1.85M | tell = ec_tell(dec); |
524 | 1.85M | tf_changed |= curr; |
525 | 1.85M | } |
526 | 4.91M | tf_res[i] = curr; |
527 | 4.91M | logp = isTransient ? 4 : 5; |
528 | 4.91M | } |
529 | 303k | tf_select = 0; |
530 | 303k | if (tf_select_rsv && |
531 | 91.2k | tf_select_table[LM][4*isTransient+0+tf_changed] != |
532 | 91.2k | tf_select_table[LM][4*isTransient+2+tf_changed]) |
533 | 31.8k | { |
534 | 31.8k | tf_select = ec_dec_bit_logp(dec, 1); |
535 | 31.8k | } |
536 | 5.21M | for (i=start;i<end;i++) |
537 | 4.91M | { |
538 | 4.91M | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; |
539 | 4.91M | } |
540 | 303k | } |
541 | | |
542 | | static int celt_plc_pitch_search(CELTDecoder *st, celt_sig *decode_mem[2], int C, int arch) |
543 | 101k | { |
544 | 101k | int pitch_index; |
545 | | #ifdef ENABLE_QEXT |
546 | | int qext_scale; |
547 | | #endif |
548 | 101k | VARDECL( opus_val16, lp_pitch_buf ); |
549 | 101k | SAVE_STACK; |
550 | | #ifdef ENABLE_QEXT |
551 | | qext_scale = st->qext_scale; |
552 | | #endif |
553 | 101k | ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 ); |
554 | 101k | pitch_downsample(decode_mem, lp_pitch_buf, |
555 | 101k | DECODE_BUFFER_SIZE>>1, C, QEXT_SCALE(2), arch); |
556 | 101k | pitch_search(lp_pitch_buf+(PLC_PITCH_LAG_MAX>>1), lp_pitch_buf, |
557 | 101k | DECODE_BUFFER_SIZE-PLC_PITCH_LAG_MAX, |
558 | 101k | PLC_PITCH_LAG_MAX-PLC_PITCH_LAG_MIN, &pitch_index, arch); |
559 | 101k | pitch_index = PLC_PITCH_LAG_MAX-pitch_index; |
560 | 101k | RESTORE_STACK; |
561 | 101k | return QEXT_SCALE(pitch_index); |
562 | 101k | } |
563 | | |
564 | | static void prefilter_and_fold(CELTDecoder * OPUS_RESTRICT st, int N) |
565 | 125k | { |
566 | 125k | int c; |
567 | 125k | int CC; |
568 | 125k | int i; |
569 | 125k | int overlap; |
570 | 125k | celt_sig *decode_mem[2]; |
571 | 125k | const OpusCustomMode *mode; |
572 | 125k | int decode_buffer_size; |
573 | | #ifdef ENABLE_QEXT |
574 | | int qext_scale; |
575 | | #endif |
576 | 125k | VARDECL(opus_val32, etmp); |
577 | 125k | SAVE_STACK |
578 | | #ifdef ENABLE_QEXT |
579 | | qext_scale = st->qext_scale; |
580 | | #endif |
581 | 125k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); |
582 | 125k | mode = st->mode; |
583 | 125k | overlap = st->overlap; |
584 | 125k | CC = st->channels; |
585 | 125k | ALLOC(etmp, overlap, opus_val32); |
586 | 205k | c=0; do { |
587 | 205k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); |
588 | 205k | } while (++c<CC); |
589 | | |
590 | 205k | c=0; do { |
591 | | /* Apply the pre-filter to the MDCT overlap for the next frame because |
592 | | the post-filter will be re-applied in the decoder after the MDCT |
593 | | overlap. */ |
594 | 205k | comb_filter(etmp, decode_mem[c]+decode_buffer_size-N, |
595 | 205k | st->postfilter_period_old, st->postfilter_period, overlap, |
596 | 205k | -st->postfilter_gain_old, -st->postfilter_gain, |
597 | 205k | st->postfilter_tapset_old, st->postfilter_tapset, NULL, 0, st->arch); |
598 | | |
599 | | /* Simulate TDAC on the concealed audio so that it blends with the |
600 | | MDCT of the next frame. */ |
601 | 14.0M | for (i=0;i<overlap/2;i++) |
602 | 13.8M | { |
603 | 13.8M | decode_mem[c][decode_buffer_size-N+i] = |
604 | 13.8M | MULT16_32_Q15(COEF2VAL16(mode->window[i]), etmp[overlap-1-i]) |
605 | 13.8M | + MULT16_32_Q15 (COEF2VAL16(mode->window[overlap-i-1]), etmp[i]); |
606 | 13.8M | } |
607 | 205k | } while (++c<CC); |
608 | 125k | RESTORE_STACK; |
609 | 125k | } celt_decoder.c:prefilter_and_fold Line | Count | Source | 565 | 62.9k | { | 566 | 62.9k | int c; | 567 | 62.9k | int CC; | 568 | 62.9k | int i; | 569 | 62.9k | int overlap; | 570 | 62.9k | celt_sig *decode_mem[2]; | 571 | 62.9k | const OpusCustomMode *mode; | 572 | 62.9k | int decode_buffer_size; | 573 | | #ifdef ENABLE_QEXT | 574 | | int qext_scale; | 575 | | #endif | 576 | 62.9k | VARDECL(opus_val32, etmp); | 577 | 62.9k | SAVE_STACK | 578 | | #ifdef ENABLE_QEXT | 579 | | qext_scale = st->qext_scale; | 580 | | #endif | 581 | 62.9k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 582 | 62.9k | mode = st->mode; | 583 | 62.9k | overlap = st->overlap; | 584 | 62.9k | CC = st->channels; | 585 | 62.9k | ALLOC(etmp, overlap, opus_val32); | 586 | 102k | c=0; do { | 587 | 102k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 588 | 102k | } while (++c<CC); | 589 | | | 590 | 102k | c=0; do { | 591 | | /* Apply the pre-filter to the MDCT overlap for the next frame because | 592 | | the post-filter will be re-applied in the decoder after the MDCT | 593 | | overlap. */ | 594 | 102k | comb_filter(etmp, decode_mem[c]+decode_buffer_size-N, | 595 | 102k | st->postfilter_period_old, st->postfilter_period, overlap, | 596 | 102k | -st->postfilter_gain_old, -st->postfilter_gain, | 597 | 102k | st->postfilter_tapset_old, st->postfilter_tapset, NULL, 0, st->arch); | 598 | | | 599 | | /* Simulate TDAC on the concealed audio so that it blends with the | 600 | | MDCT of the next frame. */ | 601 | 7.01M | for (i=0;i<overlap/2;i++) | 602 | 6.90M | { | 603 | 6.90M | decode_mem[c][decode_buffer_size-N+i] = | 604 | 6.90M | MULT16_32_Q15(COEF2VAL16(mode->window[i]), etmp[overlap-1-i]) | 605 | 6.90M | + MULT16_32_Q15 (COEF2VAL16(mode->window[overlap-i-1]), etmp[i]); | 606 | 6.90M | } | 607 | 102k | } while (++c<CC); | 608 | 62.9k | RESTORE_STACK; | 609 | 62.9k | } |
celt_decoder.c:prefilter_and_fold Line | Count | Source | 565 | 62.9k | { | 566 | 62.9k | int c; | 567 | 62.9k | int CC; | 568 | 62.9k | int i; | 569 | 62.9k | int overlap; | 570 | 62.9k | celt_sig *decode_mem[2]; | 571 | 62.9k | const OpusCustomMode *mode; | 572 | 62.9k | int decode_buffer_size; | 573 | 62.9k | #ifdef ENABLE_QEXT | 574 | 62.9k | int qext_scale; | 575 | 62.9k | #endif | 576 | 62.9k | VARDECL(opus_val32, etmp); | 577 | 62.9k | SAVE_STACK | 578 | 62.9k | #ifdef ENABLE_QEXT | 579 | 62.9k | qext_scale = st->qext_scale; | 580 | 62.9k | #endif | 581 | 62.9k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 582 | 62.9k | mode = st->mode; | 583 | 62.9k | overlap = st->overlap; | 584 | 62.9k | CC = st->channels; | 585 | 62.9k | ALLOC(etmp, overlap, opus_val32); | 586 | 102k | c=0; do { | 587 | 102k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 588 | 102k | } while (++c<CC); | 589 | | | 590 | 102k | c=0; do { | 591 | | /* Apply the pre-filter to the MDCT overlap for the next frame because | 592 | | the post-filter will be re-applied in the decoder after the MDCT | 593 | | overlap. */ | 594 | 102k | comb_filter(etmp, decode_mem[c]+decode_buffer_size-N, | 595 | 102k | st->postfilter_period_old, st->postfilter_period, overlap, | 596 | 102k | -st->postfilter_gain_old, -st->postfilter_gain, | 597 | 102k | st->postfilter_tapset_old, st->postfilter_tapset, NULL, 0, st->arch); | 598 | | | 599 | | /* Simulate TDAC on the concealed audio so that it blends with the | 600 | | MDCT of the next frame. */ | 601 | 7.01M | for (i=0;i<overlap/2;i++) | 602 | 6.90M | { | 603 | 6.90M | decode_mem[c][decode_buffer_size-N+i] = | 604 | 6.90M | MULT16_32_Q15(COEF2VAL16(mode->window[i]), etmp[overlap-1-i]) | 605 | 6.90M | + MULT16_32_Q15 (COEF2VAL16(mode->window[overlap-i-1]), etmp[i]); | 606 | 6.90M | } | 607 | 102k | } while (++c<CC); | 608 | 62.9k | RESTORE_STACK; | 609 | 62.9k | } |
|
610 | | |
611 | | #ifdef ENABLE_DEEP_PLC |
612 | | |
613 | | #define SINC_ORDER 48 |
614 | | /* h=cos(pi/2*abs(sin([-24:24]/48*pi*23./24)).^2); |
615 | | b=sinc([-24:24]/3*1.02).*h; |
616 | | b=b/sum(b); */ |
617 | | static const float sinc_filter[SINC_ORDER+1] = { |
618 | | 4.2931e-05f, -0.000190293f, -0.000816132f, -0.000637162f, 0.00141662f, 0.00354764f, 0.00184368f, -0.00428274f, |
619 | | -0.00856105f, -0.0034003f, 0.00930201f, 0.0159616f, 0.00489785f, -0.0169649f, -0.0259484f, -0.00596856f, |
620 | | 0.0286551f, 0.0405872f, 0.00649994f, -0.0509284f, -0.0716655f, -0.00665212f, 0.134336f, 0.278927f, |
621 | | 0.339995f, 0.278927f, 0.134336f, -0.00665212f, -0.0716655f, -0.0509284f, 0.00649994f, 0.0405872f, |
622 | | 0.0286551f, -0.00596856f, -0.0259484f, -0.0169649f, 0.00489785f, 0.0159616f, 0.00930201f, -0.0034003f, |
623 | | -0.00856105f, -0.00428274f, 0.00184368f, 0.00354764f, 0.00141662f, -0.000637162f, -0.000816132f, -0.000190293f, |
624 | | 4.2931e-05f |
625 | | }; |
626 | | |
627 | | void update_plc_state(LPCNetPLCState *lpcnet, celt_sig *decode_mem[2], float *plc_preemphasis_mem, int CC) |
628 | | { |
629 | | int i; |
630 | | int tmp_read_post, tmp_fec_skip; |
631 | | int offset; |
632 | | celt_sig buf48k[DECODE_BUFFER_SIZE]; |
633 | | opus_int16 buf16k[PLC_UPDATE_SAMPLES]; |
634 | | if (CC == 1) OPUS_COPY(buf48k, decode_mem[0], DECODE_BUFFER_SIZE); |
635 | | else { |
636 | | for (i=0;i<DECODE_BUFFER_SIZE;i++) { |
637 | | buf48k[i] = .5*(decode_mem[0][i] + decode_mem[1][i]); |
638 | | } |
639 | | } |
640 | | /* Down-sample the last 40 ms. */ |
641 | | for (i=1;i<DECODE_BUFFER_SIZE;i++) buf48k[i] += PREEMPHASIS*buf48k[i-1]; |
642 | | *plc_preemphasis_mem = buf48k[DECODE_BUFFER_SIZE-1]; |
643 | | offset = DECODE_BUFFER_SIZE-SINC_ORDER-1 - 3*(PLC_UPDATE_SAMPLES-1); |
644 | | celt_assert(3*(PLC_UPDATE_SAMPLES-1) + SINC_ORDER + offset == DECODE_BUFFER_SIZE-1); |
645 | | for (i=0;i<PLC_UPDATE_SAMPLES;i++) { |
646 | | int j; |
647 | | float sum = 0; |
648 | | for (j=0;j<SINC_ORDER+1;j++) { |
649 | | sum += buf48k[3*i + j + offset]*sinc_filter[j]; |
650 | | } |
651 | | buf16k[i] = float2int(MIN32(32767.f, MAX32(-32767.f, sum))); |
652 | | } |
653 | | tmp_read_post = lpcnet->fec_read_pos; |
654 | | tmp_fec_skip = lpcnet->fec_skip; |
655 | | for (i=0;i<PLC_UPDATE_FRAMES;i++) { |
656 | | lpcnet_plc_update(lpcnet, &buf16k[FRAME_SIZE*i]); |
657 | | } |
658 | | lpcnet->fec_read_pos = tmp_read_post; |
659 | | lpcnet->fec_skip = tmp_fec_skip; |
660 | | } |
661 | | #endif |
662 | | |
663 | | static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, int N, int LM |
664 | | #ifdef ENABLE_DEEP_PLC |
665 | | ,LPCNetPLCState *lpcnet |
666 | | #endif |
667 | | ) |
668 | 303k | { |
669 | 303k | int c; |
670 | 303k | int i; |
671 | 303k | const int C = st->channels; |
672 | 303k | celt_sig *decode_mem[2]; |
673 | 303k | celt_sig *out_syn[2]; |
674 | 303k | opus_val16 *lpc; |
675 | 303k | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; |
676 | 303k | const OpusCustomMode *mode; |
677 | 303k | int nbEBands; |
678 | 303k | int overlap; |
679 | 303k | int start; |
680 | 303k | int loss_duration; |
681 | 303k | int noise_based; |
682 | 303k | const opus_int16 *eBands; |
683 | 303k | int decode_buffer_size; |
684 | 303k | int max_period; |
685 | | #ifdef ENABLE_QEXT |
686 | | int qext_scale; |
687 | | #endif |
688 | 303k | SAVE_STACK; |
689 | | #ifdef ENABLE_QEXT |
690 | | qext_scale = st->qext_scale; |
691 | | #endif |
692 | 303k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); |
693 | 303k | max_period = QEXT_SCALE(MAX_PERIOD); |
694 | 303k | mode = st->mode; |
695 | 303k | nbEBands = mode->nbEBands; |
696 | 303k | overlap = mode->overlap; |
697 | 303k | eBands = mode->eBands; |
698 | | |
699 | 467k | c=0; do { |
700 | 467k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); |
701 | 467k | out_syn[c] = decode_mem[c]+decode_buffer_size-N; |
702 | 467k | } while (++c<C); |
703 | 303k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C); |
704 | 303k | oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER); |
705 | 303k | oldLogE = oldBandE + 2*nbEBands; |
706 | 303k | oldLogE2 = oldLogE + 2*nbEBands; |
707 | 303k | backgroundLogE = oldLogE2 + 2*nbEBands; |
708 | | |
709 | 303k | loss_duration = st->loss_duration; |
710 | 303k | start = st->start; |
711 | | #ifdef ENABLE_DEEP_PLC |
712 | | if (lpcnet != NULL) noise_based = start != 0 || (lpcnet->fec_fill_pos == 0 && (st->skip_plc || loss_duration >= 80)); |
713 | | else |
714 | | #endif |
715 | 303k | noise_based = loss_duration >= 40 || start != 0 || st->skip_plc; |
716 | 303k | if (noise_based) |
717 | 55.9k | { |
718 | | /* Noise-based PLC/CNG */ |
719 | 55.9k | VARDECL(celt_norm, X); |
720 | 55.9k | opus_uint32 seed; |
721 | 55.9k | int end; |
722 | 55.9k | int effEnd; |
723 | 55.9k | celt_glog decay; |
724 | 55.9k | end = st->end; |
725 | 55.9k | effEnd = IMAX(start, IMIN(end, mode->effEBands)); |
726 | | |
727 | 55.9k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
728 | 79.6k | c=0; do { |
729 | 79.6k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, |
730 | 79.6k | decode_buffer_size-N+overlap); |
731 | 79.6k | } while (++c<C); |
732 | | |
733 | 55.9k | if (st->prefilter_and_fold) { |
734 | 1.27k | prefilter_and_fold(st, N); |
735 | 1.27k | } |
736 | | |
737 | | /* Energy decay */ |
738 | 55.9k | decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f); |
739 | 55.9k | c=0; do |
740 | 79.6k | { |
741 | 409k | for (i=start;i<end;i++) |
742 | 329k | oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay); |
743 | 79.6k | } while (++c<C); |
744 | 55.9k | seed = st->rng; |
745 | 135k | for (c=0;c<C;c++) |
746 | 79.6k | { |
747 | 409k | for (i=start;i<effEnd;i++) |
748 | 329k | { |
749 | 329k | int j; |
750 | 329k | int boffs; |
751 | 329k | int blen; |
752 | 329k | boffs = N*c+(eBands[i]<<LM); |
753 | 329k | blen = (eBands[i+1]-eBands[i])<<LM; |
754 | 15.1M | for (j=0;j<blen;j++) |
755 | 14.8M | { |
756 | 14.8M | seed = celt_lcg_rand(seed); |
757 | 14.8M | X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14); |
758 | 14.8M | } |
759 | 329k | renormalise_vector(X+boffs, blen, Q31ONE, st->arch); |
760 | 329k | } |
761 | 79.6k | } |
762 | 55.9k | st->rng = seed; |
763 | | |
764 | 55.9k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch ARG_QEXT(NULL) ARG_QEXT(NULL) ARG_QEXT(0)); |
765 | | |
766 | | /* Run the postfilter with the last parameters. */ |
767 | 79.6k | c=0; do { |
768 | 79.6k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); |
769 | 79.6k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); |
770 | 79.6k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, |
771 | 79.6k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, |
772 | 79.6k | mode->window, overlap, st->arch); |
773 | 79.6k | if (LM!=0) |
774 | 74.9k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, st->postfilter_period, N-mode->shortMdctSize, |
775 | 74.9k | st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, |
776 | 74.9k | mode->window, overlap, st->arch); |
777 | | |
778 | 79.6k | } while (++c<C); |
779 | 55.9k | st->postfilter_period_old = st->postfilter_period; |
780 | 55.9k | st->postfilter_gain_old = st->postfilter_gain; |
781 | 55.9k | st->postfilter_tapset_old = st->postfilter_tapset; |
782 | | |
783 | 55.9k | st->prefilter_and_fold = 0; |
784 | | /* Skip regular PLC until we get two consecutive packets. */ |
785 | 55.9k | st->skip_plc = 1; |
786 | 247k | } else { |
787 | 247k | int exc_length; |
788 | | /* Pitch-based PLC */ |
789 | 247k | const celt_coef *window; |
790 | 247k | opus_val16 *exc; |
791 | 247k | opus_val16 fade = Q15ONE; |
792 | 247k | int pitch_index; |
793 | 247k | VARDECL(opus_val16, _exc); |
794 | 247k | VARDECL(opus_val16, fir_tmp); |
795 | | |
796 | 247k | if (loss_duration == 0) |
797 | 143k | { |
798 | | #ifdef ENABLE_DEEP_PLC |
799 | | if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C); |
800 | | #endif |
801 | 143k | st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch); |
802 | 143k | } else { |
803 | 104k | pitch_index = st->last_pitch_index; |
804 | 104k | fade = QCONST16(.8f,15); |
805 | 104k | } |
806 | | |
807 | | /* We want the excitation for 2 pitch periods in order to look for a |
808 | | decaying signal, but we can't get more than MAX_PERIOD. */ |
809 | 247k | exc_length = IMIN(2*pitch_index, max_period); |
810 | | |
811 | 247k | ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16); |
812 | 247k | ALLOC(fir_tmp, exc_length, opus_val16); |
813 | 247k | exc = _exc+CELT_LPC_ORDER; |
814 | 247k | window = mode->window; |
815 | 388k | c=0; do { |
816 | 388k | opus_val16 decay; |
817 | 388k | opus_val16 attenuation; |
818 | 388k | opus_val32 S1=0; |
819 | 388k | celt_sig *buf; |
820 | 388k | int extrapolation_offset; |
821 | 388k | int extrapolation_len; |
822 | 388k | int j; |
823 | | |
824 | 388k | buf = decode_mem[c]; |
825 | 455M | for (i=0;i<max_period+CELT_LPC_ORDER;i++) |
826 | 454M | exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT); |
827 | | |
828 | 388k | if (loss_duration == 0) |
829 | 223k | { |
830 | 223k | opus_val32 ac[CELT_LPC_ORDER+1]; |
831 | | /* Compute LPC coefficients for the last MAX_PERIOD samples before |
832 | | the first loss so we can work in the excitation-filter domain. */ |
833 | 223k | _celt_autocorr(exc, ac, window, overlap, |
834 | 223k | CELT_LPC_ORDER, max_period, st->arch); |
835 | | /* Add a noise floor of -40 dB. */ |
836 | | #ifdef FIXED_POINT |
837 | 91.5k | ac[0] += SHR32(ac[0],13); |
838 | | #else |
839 | | ac[0] *= 1.0001f; |
840 | | #endif |
841 | | /* Use lag windowing to stabilize the Levinson-Durbin recursion. */ |
842 | 5.59M | for (i=1;i<=CELT_LPC_ORDER;i++) |
843 | 5.37M | { |
844 | | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ |
845 | | #ifdef FIXED_POINT |
846 | 2.19M | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); |
847 | | #else |
848 | | ac[i] -= ac[i]*(0.008f*0.008f)*i*i; |
849 | | #endif |
850 | 5.37M | } |
851 | 223k | _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER); |
852 | | #ifdef FIXED_POINT |
853 | | /* For fixed-point, apply bandwidth expansion until we can guarantee that |
854 | | no overflow can happen in the IIR filter. This means: |
855 | | 32768*sum(abs(filter)) < 2^31 */ |
856 | 111k | while (1) { |
857 | 111k | opus_val16 tmp=Q15ONE; |
858 | 111k | opus_val32 sum=QCONST16(1., SIG_SHIFT); |
859 | 2.77M | for (i=0;i<CELT_LPC_ORDER;i++) |
860 | 2.66M | sum += ABS16(lpc[c*CELT_LPC_ORDER+i]); |
861 | 111k | if (sum < 65535) break; |
862 | 488k | for (i=0;i<CELT_LPC_ORDER;i++) |
863 | 469k | { |
864 | 469k | tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp); |
865 | 469k | lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp); |
866 | 469k | } |
867 | 19.5k | } |
868 | | #endif |
869 | 223k | } |
870 | | /* Initialize the LPC history with the samples just before the start |
871 | | of the region for which we're computing the excitation. */ |
872 | 388k | { |
873 | | /* Compute the excitation for exc_length samples before the loss. We need the copy |
874 | | because celt_fir() cannot filter in-place. */ |
875 | 388k | celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER, |
876 | 388k | fir_tmp, exc_length, CELT_LPC_ORDER, st->arch); |
877 | 388k | OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length); |
878 | 388k | } |
879 | | |
880 | | /* Check if the waveform is decaying, and if so how fast. |
881 | | We do this to avoid adding energy when concealing in a segment |
882 | | with decaying energy. */ |
883 | 388k | { |
884 | 388k | opus_val32 E1=1, E2=1; |
885 | 388k | int decay_length; |
886 | | #ifdef FIXED_POINT |
887 | 158k | int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20); |
888 | | #ifdef ENABLE_QEXT |
889 | 67.2k | if (st->qext_scale==2) shift++; |
890 | | #endif |
891 | | #endif |
892 | 388k | decay_length = exc_length>>1; |
893 | 106M | for (i=0;i<decay_length;i++) |
894 | 105M | { |
895 | 105M | opus_val16 e; |
896 | 105M | e = exc[max_period-decay_length+i]; |
897 | 105M | E1 += SHR32(MULT16_16(e, e), shift); |
898 | 105M | e = exc[max_period-2*decay_length+i]; |
899 | 105M | E2 += SHR32(MULT16_16(e, e), shift); |
900 | 105M | } |
901 | 388k | E1 = MIN32(E1, E2); |
902 | 388k | decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2)); |
903 | 388k | } |
904 | | |
905 | | /* Move the decoder memory one frame to the left to give us room to |
906 | | add the data for the new frame. We ignore the overlap that extends |
907 | | past the end of the buffer, because we aren't going to use it. */ |
908 | 388k | OPUS_MOVE(buf, buf+N, decode_buffer_size-N); |
909 | | |
910 | | /* Extrapolate from the end of the excitation with a period of |
911 | | "pitch_index", scaling down each period by an additional factor of |
912 | | "decay". */ |
913 | 388k | extrapolation_offset = max_period-pitch_index; |
914 | | /* We need to extrapolate enough samples to cover a complete MDCT |
915 | | window (including overlap/2 samples on both sides). */ |
916 | 388k | extrapolation_len = N+overlap; |
917 | | /* We also apply fading if this is not the first loss. */ |
918 | 388k | attenuation = MULT16_16_Q15(fade, decay); |
919 | 141M | for (i=j=0;i<extrapolation_len;i++,j++) |
920 | 141M | { |
921 | 141M | opus_val16 tmp; |
922 | 141M | if (j >= pitch_index) { |
923 | 473k | j -= pitch_index; |
924 | 473k | attenuation = MULT16_16_Q15(attenuation, decay); |
925 | 473k | } |
926 | 141M | buf[decode_buffer_size-N+i] = |
927 | 141M | SHL32(EXTEND32(MULT16_16_Q15(attenuation, |
928 | 141M | exc[extrapolation_offset+j])), SIG_SHIFT); |
929 | | /* Compute the energy of the previously decoded signal whose |
930 | | excitation we're copying. */ |
931 | 141M | tmp = SROUND16( |
932 | 141M | buf[decode_buffer_size-max_period-N+extrapolation_offset+j], |
933 | 141M | SIG_SHIFT); |
934 | 141M | S1 += SHR32(MULT16_16(tmp, tmp), 11); |
935 | 141M | } |
936 | 388k | { |
937 | 388k | opus_val16 lpc_mem[CELT_LPC_ORDER]; |
938 | | /* Copy the last decoded samples (prior to the overlap region) to |
939 | | synthesis filter memory so we can have a continuous signal. */ |
940 | 9.70M | for (i=0;i<CELT_LPC_ORDER;i++) |
941 | 9.31M | lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT); |
942 | | /* Apply the synthesis filter to convert the excitation back into |
943 | | the signal domain. */ |
944 | 388k | celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER, |
945 | 388k | buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER, |
946 | 388k | lpc_mem, st->arch); |
947 | | #ifdef FIXED_POINT |
948 | 54.8M | for (i=0; i < extrapolation_len; i++) |
949 | 54.6M | buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT); |
950 | | #endif |
951 | 388k | } |
952 | | |
953 | | /* Check if the synthesis energy is higher than expected, which can |
954 | | happen with the signal changes during our window. If so, |
955 | | attenuate. */ |
956 | 388k | { |
957 | 388k | opus_val32 S2=0; |
958 | 141M | for (i=0;i<extrapolation_len;i++) |
959 | 141M | { |
960 | 141M | opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT); |
961 | 141M | S2 += SHR32(MULT16_16(tmp, tmp), 11); |
962 | 141M | } |
963 | | /* This checks for an "explosion" in the synthesis. */ |
964 | | #ifdef FIXED_POINT |
965 | 158k | if (!(S1 > SHR32(S2,2))) |
966 | | #else |
967 | | /* The float test is written this way to catch NaNs in the output |
968 | | of the IIR filter at the same time. */ |
969 | 229k | if (!(S1 > 0.2f*S2)) |
970 | 29.6k | #endif |
971 | 121k | { |
972 | 40.7M | for (i=0;i<extrapolation_len;i++) |
973 | 40.5M | buf[decode_buffer_size-N+i] = 0; |
974 | 266k | } else if (S1 < S2) |
975 | 72.3k | { |
976 | 72.3k | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); |
977 | 9.87M | for (i=0;i<overlap;i++) |
978 | 9.80M | { |
979 | 9.80M | opus_val16 tmp_g = Q15ONE |
980 | 9.80M | - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio); |
981 | 9.80M | buf[decode_buffer_size-N+i] = |
982 | 9.80M | MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]); |
983 | 9.80M | } |
984 | 14.1M | for (i=overlap;i<extrapolation_len;i++) |
985 | 14.0M | { |
986 | 14.0M | buf[decode_buffer_size-N+i] = |
987 | 14.0M | MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]); |
988 | 14.0M | } |
989 | 72.3k | } |
990 | 388k | } |
991 | | |
992 | 388k | } while (++c<C); |
993 | | |
994 | | #ifdef ENABLE_DEEP_PLC |
995 | | if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) { |
996 | | float overlap_mem; |
997 | | int samples_needed16k; |
998 | | celt_sig *buf; |
999 | | VARDECL(float, buf_copy); |
1000 | | buf = decode_mem[0]; |
1001 | | ALLOC(buf_copy, C*overlap, float); |
1002 | | c=0; do { |
1003 | | OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap); |
1004 | | } while (++c<C); |
1005 | | |
1006 | | /* Need enough samples from the PLC to cover the frame size, resampling delay, |
1007 | | and the overlap at the end. */ |
1008 | | samples_needed16k = (N+SINC_ORDER+overlap)/3; |
1009 | | if (loss_duration == 0) { |
1010 | | st->plc_fill = 0; |
1011 | | } |
1012 | | while (st->plc_fill < samples_needed16k) { |
1013 | | lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]); |
1014 | | st->plc_fill += FRAME_SIZE; |
1015 | | } |
1016 | | /* Resample to 48 kHz. */ |
1017 | | for (i=0;i<(N+overlap)/3;i++) { |
1018 | | int j; |
1019 | | float sum; |
1020 | | for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j]; |
1021 | | buf[decode_buffer_size-N+3*i] = sum; |
1022 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2]; |
1023 | | buf[decode_buffer_size-N+3*i+1] = sum; |
1024 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1]; |
1025 | | buf[decode_buffer_size-N+3*i+2] = sum; |
1026 | | } |
1027 | | OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3); |
1028 | | st->plc_fill -= N/3; |
1029 | | for (i=0;i<N;i++) { |
1030 | | float tmp = buf[decode_buffer_size-N+i]; |
1031 | | buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem; |
1032 | | st->plc_preemphasis_mem = tmp; |
1033 | | } |
1034 | | overlap_mem = st->plc_preemphasis_mem; |
1035 | | for (i=0;i<overlap;i++) { |
1036 | | float tmp = buf[decode_buffer_size+i]; |
1037 | | buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem; |
1038 | | overlap_mem = tmp; |
1039 | | } |
1040 | | /* For now, we just do mono PLC. */ |
1041 | | if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap); |
1042 | | c=0; do { |
1043 | | /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */ |
1044 | | if (loss_duration == 0) { |
1045 | | for (i=0;i<overlap;i++) decode_mem[c][decode_buffer_size-N+i] = (1-window[i])*buf_copy[c*overlap+i] + (window[i])*decode_mem[c][decode_buffer_size-N+i]; |
1046 | | } |
1047 | | } while (++c<C); |
1048 | | } |
1049 | | #endif |
1050 | 247k | st->prefilter_and_fold = 1; |
1051 | 247k | } |
1052 | | |
1053 | | /* Saturate to something large to avoid wrap-around. */ |
1054 | 303k | st->loss_duration = IMIN(10000, loss_duration+(1<<LM)); |
1055 | | |
1056 | 303k | RESTORE_STACK; |
1057 | 303k | } celt_decoder.c:celt_decode_lost Line | Count | Source | 668 | 67.1k | { | 669 | 67.1k | int c; | 670 | 67.1k | int i; | 671 | 67.1k | const int C = st->channels; | 672 | 67.1k | celt_sig *decode_mem[2]; | 673 | 67.1k | celt_sig *out_syn[2]; | 674 | 67.1k | opus_val16 *lpc; | 675 | 67.1k | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | 676 | 67.1k | const OpusCustomMode *mode; | 677 | 67.1k | int nbEBands; | 678 | 67.1k | int overlap; | 679 | 67.1k | int start; | 680 | 67.1k | int loss_duration; | 681 | 67.1k | int noise_based; | 682 | 67.1k | const opus_int16 *eBands; | 683 | 67.1k | int decode_buffer_size; | 684 | 67.1k | int max_period; | 685 | | #ifdef ENABLE_QEXT | 686 | | int qext_scale; | 687 | | #endif | 688 | 67.1k | SAVE_STACK; | 689 | | #ifdef ENABLE_QEXT | 690 | | qext_scale = st->qext_scale; | 691 | | #endif | 692 | 67.1k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 693 | 67.1k | max_period = QEXT_SCALE(MAX_PERIOD); | 694 | 67.1k | mode = st->mode; | 695 | 67.1k | nbEBands = mode->nbEBands; | 696 | 67.1k | overlap = mode->overlap; | 697 | 67.1k | eBands = mode->eBands; | 698 | | | 699 | 109k | c=0; do { | 700 | 109k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 701 | 109k | out_syn[c] = decode_mem[c]+decode_buffer_size-N; | 702 | 109k | } while (++c<C); | 703 | 67.1k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C); | 704 | 67.1k | oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER); | 705 | 67.1k | oldLogE = oldBandE + 2*nbEBands; | 706 | 67.1k | oldLogE2 = oldLogE + 2*nbEBands; | 707 | 67.1k | backgroundLogE = oldLogE2 + 2*nbEBands; | 708 | | | 709 | 67.1k | loss_duration = st->loss_duration; | 710 | 67.1k | start = st->start; | 711 | | #ifdef ENABLE_DEEP_PLC | 712 | | if (lpcnet != NULL) noise_based = start != 0 || (lpcnet->fec_fill_pos == 0 && (st->skip_plc || loss_duration >= 80)); | 713 | | else | 714 | | #endif | 715 | 67.1k | noise_based = loss_duration >= 40 || start != 0 || st->skip_plc; | 716 | 67.1k | if (noise_based) | 717 | 12.2k | { | 718 | | /* Noise-based PLC/CNG */ | 719 | 12.2k | VARDECL(celt_norm, X); | 720 | 12.2k | opus_uint32 seed; | 721 | 12.2k | int end; | 722 | 12.2k | int effEnd; | 723 | 12.2k | celt_glog decay; | 724 | 12.2k | end = st->end; | 725 | 12.2k | effEnd = IMAX(start, IMIN(end, mode->effEBands)); | 726 | | | 727 | 12.2k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | 728 | 17.6k | c=0; do { | 729 | 17.6k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, | 730 | 17.6k | decode_buffer_size-N+overlap); | 731 | 17.6k | } while (++c<C); | 732 | | | 733 | 12.2k | if (st->prefilter_and_fold) { | 734 | 337 | prefilter_and_fold(st, N); | 735 | 337 | } | 736 | | | 737 | | /* Energy decay */ | 738 | 12.2k | decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f); | 739 | 12.2k | c=0; do | 740 | 17.6k | { | 741 | 89.8k | for (i=start;i<end;i++) | 742 | 72.2k | oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay); | 743 | 17.6k | } while (++c<C); | 744 | 12.2k | seed = st->rng; | 745 | 29.8k | for (c=0;c<C;c++) | 746 | 17.6k | { | 747 | 89.8k | for (i=start;i<effEnd;i++) | 748 | 72.2k | { | 749 | 72.2k | int j; | 750 | 72.2k | int boffs; | 751 | 72.2k | int blen; | 752 | 72.2k | boffs = N*c+(eBands[i]<<LM); | 753 | 72.2k | blen = (eBands[i+1]-eBands[i])<<LM; | 754 | 3.13M | for (j=0;j<blen;j++) | 755 | 3.06M | { | 756 | 3.06M | seed = celt_lcg_rand(seed); | 757 | 3.06M | X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14); | 758 | 3.06M | } | 759 | 72.2k | renormalise_vector(X+boffs, blen, Q31ONE, st->arch); | 760 | 72.2k | } | 761 | 17.6k | } | 762 | 12.2k | st->rng = seed; | 763 | | | 764 | 12.2k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch ARG_QEXT(NULL) ARG_QEXT(NULL) ARG_QEXT(0)); | 765 | | | 766 | | /* Run the postfilter with the last parameters. */ | 767 | 17.6k | c=0; do { | 768 | 17.6k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | 769 | 17.6k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | 770 | 17.6k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | 771 | 17.6k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | 772 | 17.6k | mode->window, overlap, st->arch); | 773 | 17.6k | if (LM!=0) | 774 | 16.5k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, st->postfilter_period, N-mode->shortMdctSize, | 775 | 16.5k | st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, | 776 | 16.5k | mode->window, overlap, st->arch); | 777 | | | 778 | 17.6k | } while (++c<C); | 779 | 12.2k | st->postfilter_period_old = st->postfilter_period; | 780 | 12.2k | st->postfilter_gain_old = st->postfilter_gain; | 781 | 12.2k | st->postfilter_tapset_old = st->postfilter_tapset; | 782 | | | 783 | 12.2k | st->prefilter_and_fold = 0; | 784 | | /* Skip regular PLC until we get two consecutive packets. */ | 785 | 12.2k | st->skip_plc = 1; | 786 | 54.8k | } else { | 787 | 54.8k | int exc_length; | 788 | | /* Pitch-based PLC */ | 789 | 54.8k | const celt_coef *window; | 790 | 54.8k | opus_val16 *exc; | 791 | 54.8k | opus_val16 fade = Q15ONE; | 792 | 54.8k | int pitch_index; | 793 | 54.8k | VARDECL(opus_val16, _exc); | 794 | 54.8k | VARDECL(opus_val16, fir_tmp); | 795 | | | 796 | 54.8k | if (loss_duration == 0) | 797 | 31.9k | { | 798 | | #ifdef ENABLE_DEEP_PLC | 799 | | if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C); | 800 | | #endif | 801 | 31.9k | st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch); | 802 | 31.9k | } else { | 803 | 22.9k | pitch_index = st->last_pitch_index; | 804 | 22.9k | fade = QCONST16(.8f,15); | 805 | 22.9k | } | 806 | | | 807 | | /* We want the excitation for 2 pitch periods in order to look for a | 808 | | decaying signal, but we can't get more than MAX_PERIOD. */ | 809 | 54.8k | exc_length = IMIN(2*pitch_index, max_period); | 810 | | | 811 | 54.8k | ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16); | 812 | 54.8k | ALLOC(fir_tmp, exc_length, opus_val16); | 813 | 54.8k | exc = _exc+CELT_LPC_ORDER; | 814 | 54.8k | window = mode->window; | 815 | 91.6k | c=0; do { | 816 | 91.6k | opus_val16 decay; | 817 | 91.6k | opus_val16 attenuation; | 818 | 91.6k | opus_val32 S1=0; | 819 | 91.6k | celt_sig *buf; | 820 | 91.6k | int extrapolation_offset; | 821 | 91.6k | int extrapolation_len; | 822 | 91.6k | int j; | 823 | | | 824 | 91.6k | buf = decode_mem[c]; | 825 | 96.1M | for (i=0;i<max_period+CELT_LPC_ORDER;i++) | 826 | 96.0M | exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT); | 827 | | | 828 | 91.6k | if (loss_duration == 0) | 829 | 54.0k | { | 830 | 54.0k | opus_val32 ac[CELT_LPC_ORDER+1]; | 831 | | /* Compute LPC coefficients for the last MAX_PERIOD samples before | 832 | | the first loss so we can work in the excitation-filter domain. */ | 833 | 54.0k | _celt_autocorr(exc, ac, window, overlap, | 834 | 54.0k | CELT_LPC_ORDER, max_period, st->arch); | 835 | | /* Add a noise floor of -40 dB. */ | 836 | 54.0k | #ifdef FIXED_POINT | 837 | 54.0k | ac[0] += SHR32(ac[0],13); | 838 | | #else | 839 | | ac[0] *= 1.0001f; | 840 | | #endif | 841 | | /* Use lag windowing to stabilize the Levinson-Durbin recursion. */ | 842 | 1.35M | for (i=1;i<=CELT_LPC_ORDER;i++) | 843 | 1.29M | { | 844 | | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ | 845 | 1.29M | #ifdef FIXED_POINT | 846 | 1.29M | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); | 847 | | #else | 848 | | ac[i] -= ac[i]*(0.008f*0.008f)*i*i; | 849 | | #endif | 850 | 1.29M | } | 851 | 54.0k | _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER); | 852 | 54.0k | #ifdef FIXED_POINT | 853 | | /* For fixed-point, apply bandwidth expansion until we can guarantee that | 854 | | no overflow can happen in the IIR filter. This means: | 855 | | 32768*sum(abs(filter)) < 2^31 */ | 856 | 59.0k | while (1) { | 857 | 59.0k | opus_val16 tmp=Q15ONE; | 858 | 59.0k | opus_val32 sum=QCONST16(1., SIG_SHIFT); | 859 | 1.47M | for (i=0;i<CELT_LPC_ORDER;i++) | 860 | 1.41M | sum += ABS16(lpc[c*CELT_LPC_ORDER+i]); | 861 | 59.0k | if (sum < 65535) break; | 862 | 125k | for (i=0;i<CELT_LPC_ORDER;i++) | 863 | 120k | { | 864 | 120k | tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp); | 865 | 120k | lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp); | 866 | 120k | } | 867 | 5.02k | } | 868 | 54.0k | #endif | 869 | 54.0k | } | 870 | | /* Initialize the LPC history with the samples just before the start | 871 | | of the region for which we're computing the excitation. */ | 872 | 91.6k | { | 873 | | /* Compute the excitation for exc_length samples before the loss. We need the copy | 874 | | because celt_fir() cannot filter in-place. */ | 875 | 91.6k | celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER, | 876 | 91.6k | fir_tmp, exc_length, CELT_LPC_ORDER, st->arch); | 877 | 91.6k | OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length); | 878 | 91.6k | } | 879 | | | 880 | | /* Check if the waveform is decaying, and if so how fast. | 881 | | We do this to avoid adding energy when concealing in a segment | 882 | | with decaying energy. */ | 883 | 91.6k | { | 884 | 91.6k | opus_val32 E1=1, E2=1; | 885 | 91.6k | int decay_length; | 886 | 91.6k | #ifdef FIXED_POINT | 887 | 91.6k | int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20); | 888 | | #ifdef ENABLE_QEXT | 889 | | if (st->qext_scale==2) shift++; | 890 | | #endif | 891 | 91.6k | #endif | 892 | 91.6k | decay_length = exc_length>>1; | 893 | 22.9M | for (i=0;i<decay_length;i++) | 894 | 22.8M | { | 895 | 22.8M | opus_val16 e; | 896 | 22.8M | e = exc[max_period-decay_length+i]; | 897 | 22.8M | E1 += SHR32(MULT16_16(e, e), shift); | 898 | 22.8M | e = exc[max_period-2*decay_length+i]; | 899 | 22.8M | E2 += SHR32(MULT16_16(e, e), shift); | 900 | 22.8M | } | 901 | 91.6k | E1 = MIN32(E1, E2); | 902 | 91.6k | decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2)); | 903 | 91.6k | } | 904 | | | 905 | | /* Move the decoder memory one frame to the left to give us room to | 906 | | add the data for the new frame. We ignore the overlap that extends | 907 | | past the end of the buffer, because we aren't going to use it. */ | 908 | 91.6k | OPUS_MOVE(buf, buf+N, decode_buffer_size-N); | 909 | | | 910 | | /* Extrapolate from the end of the excitation with a period of | 911 | | "pitch_index", scaling down each period by an additional factor of | 912 | | "decay". */ | 913 | 91.6k | extrapolation_offset = max_period-pitch_index; | 914 | | /* We need to extrapolate enough samples to cover a complete MDCT | 915 | | window (including overlap/2 samples on both sides). */ | 916 | 91.6k | extrapolation_len = N+overlap; | 917 | | /* We also apply fading if this is not the first loss. */ | 918 | 91.6k | attenuation = MULT16_16_Q15(fade, decay); | 919 | 28.4M | for (i=j=0;i<extrapolation_len;i++,j++) | 920 | 28.4M | { | 921 | 28.4M | opus_val16 tmp; | 922 | 28.4M | if (j >= pitch_index) { | 923 | 103k | j -= pitch_index; | 924 | 103k | attenuation = MULT16_16_Q15(attenuation, decay); | 925 | 103k | } | 926 | 28.4M | buf[decode_buffer_size-N+i] = | 927 | 28.4M | SHL32(EXTEND32(MULT16_16_Q15(attenuation, | 928 | 28.4M | exc[extrapolation_offset+j])), SIG_SHIFT); | 929 | | /* Compute the energy of the previously decoded signal whose | 930 | | excitation we're copying. */ | 931 | 28.4M | tmp = SROUND16( | 932 | 28.4M | buf[decode_buffer_size-max_period-N+extrapolation_offset+j], | 933 | 28.4M | SIG_SHIFT); | 934 | 28.4M | S1 += SHR32(MULT16_16(tmp, tmp), 11); | 935 | 28.4M | } | 936 | 91.6k | { | 937 | 91.6k | opus_val16 lpc_mem[CELT_LPC_ORDER]; | 938 | | /* Copy the last decoded samples (prior to the overlap region) to | 939 | | synthesis filter memory so we can have a continuous signal. */ | 940 | 2.29M | for (i=0;i<CELT_LPC_ORDER;i++) | 941 | 2.19M | lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT); | 942 | | /* Apply the synthesis filter to convert the excitation back into | 943 | | the signal domain. */ | 944 | 91.6k | celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER, | 945 | 91.6k | buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER, | 946 | 91.6k | lpc_mem, st->arch); | 947 | 91.6k | #ifdef FIXED_POINT | 948 | 28.4M | for (i=0; i < extrapolation_len; i++) | 949 | 28.4M | buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT); | 950 | 91.6k | #endif | 951 | 91.6k | } | 952 | | | 953 | | /* Check if the synthesis energy is higher than expected, which can | 954 | | happen with the signal changes during our window. If so, | 955 | | attenuate. */ | 956 | 91.6k | { | 957 | 91.6k | opus_val32 S2=0; | 958 | 28.4M | for (i=0;i<extrapolation_len;i++) | 959 | 28.4M | { | 960 | 28.4M | opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT); | 961 | 28.4M | S2 += SHR32(MULT16_16(tmp, tmp), 11); | 962 | 28.4M | } | 963 | | /* This checks for an "explosion" in the synthesis. */ | 964 | 91.6k | #ifdef FIXED_POINT | 965 | 91.6k | if (!(S1 > SHR32(S2,2))) | 966 | | #else | 967 | | /* The float test is written this way to catch NaNs in the output | 968 | | of the IIR filter at the same time. */ | 969 | | if (!(S1 > 0.2f*S2)) | 970 | | #endif | 971 | 51.6k | { | 972 | 14.8M | for (i=0;i<extrapolation_len;i++) | 973 | 14.8M | buf[decode_buffer_size-N+i] = 0; | 974 | 51.6k | } else if (S1 < S2) | 975 | 21.0k | { | 976 | 21.0k | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); | 977 | 2.54M | for (i=0;i<overlap;i++) | 978 | 2.52M | { | 979 | 2.52M | opus_val16 tmp_g = Q15ONE | 980 | 2.52M | - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio); | 981 | 2.52M | buf[decode_buffer_size-N+i] = | 982 | 2.52M | MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]); | 983 | 2.52M | } | 984 | 4.26M | for (i=overlap;i<extrapolation_len;i++) | 985 | 4.24M | { | 986 | 4.24M | buf[decode_buffer_size-N+i] = | 987 | 4.24M | MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]); | 988 | 4.24M | } | 989 | 21.0k | } | 990 | 91.6k | } | 991 | | | 992 | 91.6k | } while (++c<C); | 993 | | | 994 | | #ifdef ENABLE_DEEP_PLC | 995 | | if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) { | 996 | | float overlap_mem; | 997 | | int samples_needed16k; | 998 | | celt_sig *buf; | 999 | | VARDECL(float, buf_copy); | 1000 | | buf = decode_mem[0]; | 1001 | | ALLOC(buf_copy, C*overlap, float); | 1002 | | c=0; do { | 1003 | | OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap); | 1004 | | } while (++c<C); | 1005 | | | 1006 | | /* Need enough samples from the PLC to cover the frame size, resampling delay, | 1007 | | and the overlap at the end. */ | 1008 | | samples_needed16k = (N+SINC_ORDER+overlap)/3; | 1009 | | if (loss_duration == 0) { | 1010 | | st->plc_fill = 0; | 1011 | | } | 1012 | | while (st->plc_fill < samples_needed16k) { | 1013 | | lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]); | 1014 | | st->plc_fill += FRAME_SIZE; | 1015 | | } | 1016 | | /* Resample to 48 kHz. */ | 1017 | | for (i=0;i<(N+overlap)/3;i++) { | 1018 | | int j; | 1019 | | float sum; | 1020 | | for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j]; | 1021 | | buf[decode_buffer_size-N+3*i] = sum; | 1022 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2]; | 1023 | | buf[decode_buffer_size-N+3*i+1] = sum; | 1024 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1]; | 1025 | | buf[decode_buffer_size-N+3*i+2] = sum; | 1026 | | } | 1027 | | OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3); | 1028 | | st->plc_fill -= N/3; | 1029 | | for (i=0;i<N;i++) { | 1030 | | float tmp = buf[decode_buffer_size-N+i]; | 1031 | | buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem; | 1032 | | st->plc_preemphasis_mem = tmp; | 1033 | | } | 1034 | | overlap_mem = st->plc_preemphasis_mem; | 1035 | | for (i=0;i<overlap;i++) { | 1036 | | float tmp = buf[decode_buffer_size+i]; | 1037 | | buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem; | 1038 | | overlap_mem = tmp; | 1039 | | } | 1040 | | /* For now, we just do mono PLC. */ | 1041 | | if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap); | 1042 | | c=0; do { | 1043 | | /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */ | 1044 | | if (loss_duration == 0) { | 1045 | | for (i=0;i<overlap;i++) decode_mem[c][decode_buffer_size-N+i] = (1-window[i])*buf_copy[c*overlap+i] + (window[i])*decode_mem[c][decode_buffer_size-N+i]; | 1046 | | } | 1047 | | } while (++c<C); | 1048 | | } | 1049 | | #endif | 1050 | 54.8k | st->prefilter_and_fold = 1; | 1051 | 54.8k | } | 1052 | | | 1053 | | /* Saturate to something large to avoid wrap-around. */ | 1054 | 67.1k | st->loss_duration = IMIN(10000, loss_duration+(1<<LM)); | 1055 | | | 1056 | 67.1k | RESTORE_STACK; | 1057 | 67.1k | } |
celt_decoder.c:celt_decode_lost Line | Count | Source | 668 | 54.2k | { | 669 | 54.2k | int c; | 670 | 54.2k | int i; | 671 | 54.2k | const int C = st->channels; | 672 | 54.2k | celt_sig *decode_mem[2]; | 673 | 54.2k | celt_sig *out_syn[2]; | 674 | 54.2k | opus_val16 *lpc; | 675 | 54.2k | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | 676 | 54.2k | const OpusCustomMode *mode; | 677 | 54.2k | int nbEBands; | 678 | 54.2k | int overlap; | 679 | 54.2k | int start; | 680 | 54.2k | int loss_duration; | 681 | 54.2k | int noise_based; | 682 | 54.2k | const opus_int16 *eBands; | 683 | 54.2k | int decode_buffer_size; | 684 | 54.2k | int max_period; | 685 | 54.2k | #ifdef ENABLE_QEXT | 686 | 54.2k | int qext_scale; | 687 | 54.2k | #endif | 688 | 54.2k | SAVE_STACK; | 689 | 54.2k | #ifdef ENABLE_QEXT | 690 | 54.2k | qext_scale = st->qext_scale; | 691 | 54.2k | #endif | 692 | 54.2k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 693 | 54.2k | max_period = QEXT_SCALE(MAX_PERIOD); | 694 | 54.2k | mode = st->mode; | 695 | 54.2k | nbEBands = mode->nbEBands; | 696 | 54.2k | overlap = mode->overlap; | 697 | 54.2k | eBands = mode->eBands; | 698 | | | 699 | 75.8k | c=0; do { | 700 | 75.8k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 701 | 75.8k | out_syn[c] = decode_mem[c]+decode_buffer_size-N; | 702 | 75.8k | } while (++c<C); | 703 | 54.2k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C); | 704 | 54.2k | oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER); | 705 | 54.2k | oldLogE = oldBandE + 2*nbEBands; | 706 | 54.2k | oldLogE2 = oldLogE + 2*nbEBands; | 707 | 54.2k | backgroundLogE = oldLogE2 + 2*nbEBands; | 708 | | | 709 | 54.2k | loss_duration = st->loss_duration; | 710 | 54.2k | start = st->start; | 711 | | #ifdef ENABLE_DEEP_PLC | 712 | | if (lpcnet != NULL) noise_based = start != 0 || (lpcnet->fec_fill_pos == 0 && (st->skip_plc || loss_duration >= 80)); | 713 | | else | 714 | | #endif | 715 | 54.2k | noise_based = loss_duration >= 40 || start != 0 || st->skip_plc; | 716 | 54.2k | if (noise_based) | 717 | 5.88k | { | 718 | | /* Noise-based PLC/CNG */ | 719 | 5.88k | VARDECL(celt_norm, X); | 720 | 5.88k | opus_uint32 seed; | 721 | 5.88k | int end; | 722 | 5.88k | int effEnd; | 723 | 5.88k | celt_glog decay; | 724 | 5.88k | end = st->end; | 725 | 5.88k | effEnd = IMAX(start, IMIN(end, mode->effEBands)); | 726 | | | 727 | 5.88k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | 728 | 8.56k | c=0; do { | 729 | 8.56k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, | 730 | 8.56k | decode_buffer_size-N+overlap); | 731 | 8.56k | } while (++c<C); | 732 | | | 733 | 5.88k | if (st->prefilter_and_fold) { | 734 | 138 | prefilter_and_fold(st, N); | 735 | 138 | } | 736 | | | 737 | | /* Energy decay */ | 738 | 5.88k | decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f); | 739 | 5.88k | c=0; do | 740 | 8.56k | { | 741 | 42.8k | for (i=start;i<end;i++) | 742 | 34.2k | oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay); | 743 | 8.56k | } while (++c<C); | 744 | 5.88k | seed = st->rng; | 745 | 14.4k | for (c=0;c<C;c++) | 746 | 8.56k | { | 747 | 42.8k | for (i=start;i<effEnd;i++) | 748 | 34.2k | { | 749 | 34.2k | int j; | 750 | 34.2k | int boffs; | 751 | 34.2k | int blen; | 752 | 34.2k | boffs = N*c+(eBands[i]<<LM); | 753 | 34.2k | blen = (eBands[i+1]-eBands[i])<<LM; | 754 | 1.30M | for (j=0;j<blen;j++) | 755 | 1.27M | { | 756 | 1.27M | seed = celt_lcg_rand(seed); | 757 | 1.27M | X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14); | 758 | 1.27M | } | 759 | 34.2k | renormalise_vector(X+boffs, blen, Q31ONE, st->arch); | 760 | 34.2k | } | 761 | 8.56k | } | 762 | 5.88k | st->rng = seed; | 763 | | | 764 | 5.88k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch ARG_QEXT(NULL) ARG_QEXT(NULL) ARG_QEXT(0)); | 765 | | | 766 | | /* Run the postfilter with the last parameters. */ | 767 | 8.56k | c=0; do { | 768 | 8.56k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | 769 | 8.56k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | 770 | 8.56k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | 771 | 8.56k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | 772 | 8.56k | mode->window, overlap, st->arch); | 773 | 8.56k | if (LM!=0) | 774 | 7.88k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, st->postfilter_period, N-mode->shortMdctSize, | 775 | 7.88k | st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, | 776 | 7.88k | mode->window, overlap, st->arch); | 777 | | | 778 | 8.56k | } while (++c<C); | 779 | 5.88k | st->postfilter_period_old = st->postfilter_period; | 780 | 5.88k | st->postfilter_gain_old = st->postfilter_gain; | 781 | 5.88k | st->postfilter_tapset_old = st->postfilter_tapset; | 782 | | | 783 | 5.88k | st->prefilter_and_fold = 0; | 784 | | /* Skip regular PLC until we get two consecutive packets. */ | 785 | 5.88k | st->skip_plc = 1; | 786 | 48.3k | } else { | 787 | 48.3k | int exc_length; | 788 | | /* Pitch-based PLC */ | 789 | 48.3k | const celt_coef *window; | 790 | 48.3k | opus_val16 *exc; | 791 | 48.3k | opus_val16 fade = Q15ONE; | 792 | 48.3k | int pitch_index; | 793 | 48.3k | VARDECL(opus_val16, _exc); | 794 | 48.3k | VARDECL(opus_val16, fir_tmp); | 795 | | | 796 | 48.3k | if (loss_duration == 0) | 797 | 28.2k | { | 798 | | #ifdef ENABLE_DEEP_PLC | 799 | | if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C); | 800 | | #endif | 801 | 28.2k | st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch); | 802 | 28.2k | } else { | 803 | 20.1k | pitch_index = st->last_pitch_index; | 804 | 20.1k | fade = QCONST16(.8f,15); | 805 | 20.1k | } | 806 | | | 807 | | /* We want the excitation for 2 pitch periods in order to look for a | 808 | | decaying signal, but we can't get more than MAX_PERIOD. */ | 809 | 48.3k | exc_length = IMIN(2*pitch_index, max_period); | 810 | | | 811 | 48.3k | ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16); | 812 | 48.3k | ALLOC(fir_tmp, exc_length, opus_val16); | 813 | 48.3k | exc = _exc+CELT_LPC_ORDER; | 814 | 48.3k | window = mode->window; | 815 | 67.2k | c=0; do { | 816 | 67.2k | opus_val16 decay; | 817 | 67.2k | opus_val16 attenuation; | 818 | 67.2k | opus_val32 S1=0; | 819 | 67.2k | celt_sig *buf; | 820 | 67.2k | int extrapolation_offset; | 821 | 67.2k | int extrapolation_len; | 822 | 67.2k | int j; | 823 | | | 824 | 67.2k | buf = decode_mem[c]; | 825 | 89.1M | for (i=0;i<max_period+CELT_LPC_ORDER;i++) | 826 | 89.1M | exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT); | 827 | | | 828 | 67.2k | if (loss_duration == 0) | 829 | 37.4k | { | 830 | 37.4k | opus_val32 ac[CELT_LPC_ORDER+1]; | 831 | | /* Compute LPC coefficients for the last MAX_PERIOD samples before | 832 | | the first loss so we can work in the excitation-filter domain. */ | 833 | 37.4k | _celt_autocorr(exc, ac, window, overlap, | 834 | 37.4k | CELT_LPC_ORDER, max_period, st->arch); | 835 | | /* Add a noise floor of -40 dB. */ | 836 | 37.4k | #ifdef FIXED_POINT | 837 | 37.4k | ac[0] += SHR32(ac[0],13); | 838 | | #else | 839 | | ac[0] *= 1.0001f; | 840 | | #endif | 841 | | /* Use lag windowing to stabilize the Levinson-Durbin recursion. */ | 842 | 937k | for (i=1;i<=CELT_LPC_ORDER;i++) | 843 | 899k | { | 844 | | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ | 845 | 899k | #ifdef FIXED_POINT | 846 | 899k | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); | 847 | | #else | 848 | | ac[i] -= ac[i]*(0.008f*0.008f)*i*i; | 849 | | #endif | 850 | 899k | } | 851 | 37.4k | _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER); | 852 | 37.4k | #ifdef FIXED_POINT | 853 | | /* For fixed-point, apply bandwidth expansion until we can guarantee that | 854 | | no overflow can happen in the IIR filter. This means: | 855 | | 32768*sum(abs(filter)) < 2^31 */ | 856 | 52.0k | while (1) { | 857 | 52.0k | opus_val16 tmp=Q15ONE; | 858 | 52.0k | opus_val32 sum=QCONST16(1., SIG_SHIFT); | 859 | 1.30M | for (i=0;i<CELT_LPC_ORDER;i++) | 860 | 1.24M | sum += ABS16(lpc[c*CELT_LPC_ORDER+i]); | 861 | 52.0k | if (sum < 65535) break; | 862 | 363k | for (i=0;i<CELT_LPC_ORDER;i++) | 863 | 348k | { | 864 | 348k | tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp); | 865 | 348k | lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp); | 866 | 348k | } | 867 | 14.5k | } | 868 | 37.4k | #endif | 869 | 37.4k | } | 870 | | /* Initialize the LPC history with the samples just before the start | 871 | | of the region for which we're computing the excitation. */ | 872 | 67.2k | { | 873 | | /* Compute the excitation for exc_length samples before the loss. We need the copy | 874 | | because celt_fir() cannot filter in-place. */ | 875 | 67.2k | celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER, | 876 | 67.2k | fir_tmp, exc_length, CELT_LPC_ORDER, st->arch); | 877 | 67.2k | OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length); | 878 | 67.2k | } | 879 | | | 880 | | /* Check if the waveform is decaying, and if so how fast. | 881 | | We do this to avoid adding energy when concealing in a segment | 882 | | with decaying energy. */ | 883 | 67.2k | { | 884 | 67.2k | opus_val32 E1=1, E2=1; | 885 | 67.2k | int decay_length; | 886 | 67.2k | #ifdef FIXED_POINT | 887 | 67.2k | int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20); | 888 | 67.2k | #ifdef ENABLE_QEXT | 889 | 67.2k | if (st->qext_scale==2) shift++; | 890 | 67.2k | #endif | 891 | 67.2k | #endif | 892 | 67.2k | decay_length = exc_length>>1; | 893 | 20.4M | for (i=0;i<decay_length;i++) | 894 | 20.3M | { | 895 | 20.3M | opus_val16 e; | 896 | 20.3M | e = exc[max_period-decay_length+i]; | 897 | 20.3M | E1 += SHR32(MULT16_16(e, e), shift); | 898 | 20.3M | e = exc[max_period-2*decay_length+i]; | 899 | 20.3M | E2 += SHR32(MULT16_16(e, e), shift); | 900 | 20.3M | } | 901 | 67.2k | E1 = MIN32(E1, E2); | 902 | 67.2k | decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2)); | 903 | 67.2k | } | 904 | | | 905 | | /* Move the decoder memory one frame to the left to give us room to | 906 | | add the data for the new frame. We ignore the overlap that extends | 907 | | past the end of the buffer, because we aren't going to use it. */ | 908 | 67.2k | OPUS_MOVE(buf, buf+N, decode_buffer_size-N); | 909 | | | 910 | | /* Extrapolate from the end of the excitation with a period of | 911 | | "pitch_index", scaling down each period by an additional factor of | 912 | | "decay". */ | 913 | 67.2k | extrapolation_offset = max_period-pitch_index; | 914 | | /* We need to extrapolate enough samples to cover a complete MDCT | 915 | | window (including overlap/2 samples on both sides). */ | 916 | 67.2k | extrapolation_len = N+overlap; | 917 | | /* We also apply fading if this is not the first loss. */ | 918 | 67.2k | attenuation = MULT16_16_Q15(fade, decay); | 919 | 26.3M | for (i=j=0;i<extrapolation_len;i++,j++) | 920 | 26.2M | { | 921 | 26.2M | opus_val16 tmp; | 922 | 26.2M | if (j >= pitch_index) { | 923 | 84.4k | j -= pitch_index; | 924 | 84.4k | attenuation = MULT16_16_Q15(attenuation, decay); | 925 | 84.4k | } | 926 | 26.2M | buf[decode_buffer_size-N+i] = | 927 | 26.2M | SHL32(EXTEND32(MULT16_16_Q15(attenuation, | 928 | 26.2M | exc[extrapolation_offset+j])), SIG_SHIFT); | 929 | | /* Compute the energy of the previously decoded signal whose | 930 | | excitation we're copying. */ | 931 | 26.2M | tmp = SROUND16( | 932 | 26.2M | buf[decode_buffer_size-max_period-N+extrapolation_offset+j], | 933 | 26.2M | SIG_SHIFT); | 934 | 26.2M | S1 += SHR32(MULT16_16(tmp, tmp), 11); | 935 | 26.2M | } | 936 | 67.2k | { | 937 | 67.2k | opus_val16 lpc_mem[CELT_LPC_ORDER]; | 938 | | /* Copy the last decoded samples (prior to the overlap region) to | 939 | | synthesis filter memory so we can have a continuous signal. */ | 940 | 1.68M | for (i=0;i<CELT_LPC_ORDER;i++) | 941 | 1.61M | lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT); | 942 | | /* Apply the synthesis filter to convert the excitation back into | 943 | | the signal domain. */ | 944 | 67.2k | celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER, | 945 | 67.2k | buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER, | 946 | 67.2k | lpc_mem, st->arch); | 947 | 67.2k | #ifdef FIXED_POINT | 948 | 26.3M | for (i=0; i < extrapolation_len; i++) | 949 | 26.2M | buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT); | 950 | 67.2k | #endif | 951 | 67.2k | } | 952 | | | 953 | | /* Check if the synthesis energy is higher than expected, which can | 954 | | happen with the signal changes during our window. If so, | 955 | | attenuate. */ | 956 | 67.2k | { | 957 | 67.2k | opus_val32 S2=0; | 958 | 26.3M | for (i=0;i<extrapolation_len;i++) | 959 | 26.2M | { | 960 | 26.2M | opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT); | 961 | 26.2M | S2 += SHR32(MULT16_16(tmp, tmp), 11); | 962 | 26.2M | } | 963 | | /* This checks for an "explosion" in the synthesis. */ | 964 | 67.2k | #ifdef FIXED_POINT | 965 | 67.2k | if (!(S1 > SHR32(S2,2))) | 966 | | #else | 967 | | /* The float test is written this way to catch NaNs in the output | 968 | | of the IIR filter at the same time. */ | 969 | | if (!(S1 > 0.2f*S2)) | 970 | | #endif | 971 | 40.3k | { | 972 | 14.8M | for (i=0;i<extrapolation_len;i++) | 973 | 14.8M | buf[decode_buffer_size-N+i] = 0; | 974 | 40.3k | } else if (S1 < S2) | 975 | 10.9k | { | 976 | 10.9k | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); | 977 | 1.66M | for (i=0;i<overlap;i++) | 978 | 1.65M | { | 979 | 1.65M | opus_val16 tmp_g = Q15ONE | 980 | 1.65M | - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio); | 981 | 1.65M | buf[decode_buffer_size-N+i] = | 982 | 1.65M | MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]); | 983 | 1.65M | } | 984 | 2.53M | for (i=overlap;i<extrapolation_len;i++) | 985 | 2.52M | { | 986 | 2.52M | buf[decode_buffer_size-N+i] = | 987 | 2.52M | MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]); | 988 | 2.52M | } | 989 | 10.9k | } | 990 | 67.2k | } | 991 | | | 992 | 67.2k | } while (++c<C); | 993 | | | 994 | | #ifdef ENABLE_DEEP_PLC | 995 | | if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) { | 996 | | float overlap_mem; | 997 | | int samples_needed16k; | 998 | | celt_sig *buf; | 999 | | VARDECL(float, buf_copy); | 1000 | | buf = decode_mem[0]; | 1001 | | ALLOC(buf_copy, C*overlap, float); | 1002 | | c=0; do { | 1003 | | OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap); | 1004 | | } while (++c<C); | 1005 | | | 1006 | | /* Need enough samples from the PLC to cover the frame size, resampling delay, | 1007 | | and the overlap at the end. */ | 1008 | | samples_needed16k = (N+SINC_ORDER+overlap)/3; | 1009 | | if (loss_duration == 0) { | 1010 | | st->plc_fill = 0; | 1011 | | } | 1012 | | while (st->plc_fill < samples_needed16k) { | 1013 | | lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]); | 1014 | | st->plc_fill += FRAME_SIZE; | 1015 | | } | 1016 | | /* Resample to 48 kHz. */ | 1017 | | for (i=0;i<(N+overlap)/3;i++) { | 1018 | | int j; | 1019 | | float sum; | 1020 | | for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j]; | 1021 | | buf[decode_buffer_size-N+3*i] = sum; | 1022 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2]; | 1023 | | buf[decode_buffer_size-N+3*i+1] = sum; | 1024 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1]; | 1025 | | buf[decode_buffer_size-N+3*i+2] = sum; | 1026 | | } | 1027 | | OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3); | 1028 | | st->plc_fill -= N/3; | 1029 | | for (i=0;i<N;i++) { | 1030 | | float tmp = buf[decode_buffer_size-N+i]; | 1031 | | buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem; | 1032 | | st->plc_preemphasis_mem = tmp; | 1033 | | } | 1034 | | overlap_mem = st->plc_preemphasis_mem; | 1035 | | for (i=0;i<overlap;i++) { | 1036 | | float tmp = buf[decode_buffer_size+i]; | 1037 | | buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem; | 1038 | | overlap_mem = tmp; | 1039 | | } | 1040 | | /* For now, we just do mono PLC. */ | 1041 | | if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap); | 1042 | | c=0; do { | 1043 | | /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */ | 1044 | | if (loss_duration == 0) { | 1045 | | for (i=0;i<overlap;i++) decode_mem[c][decode_buffer_size-N+i] = (1-window[i])*buf_copy[c*overlap+i] + (window[i])*decode_mem[c][decode_buffer_size-N+i]; | 1046 | | } | 1047 | | } while (++c<C); | 1048 | | } | 1049 | | #endif | 1050 | 48.3k | st->prefilter_and_fold = 1; | 1051 | 48.3k | } | 1052 | | | 1053 | | /* Saturate to something large to avoid wrap-around. */ | 1054 | 54.2k | st->loss_duration = IMIN(10000, loss_duration+(1<<LM)); | 1055 | | | 1056 | 54.2k | RESTORE_STACK; | 1057 | 54.2k | } |
celt_decoder.c:celt_decode_lost Line | Count | Source | 668 | 91.1k | { | 669 | 91.1k | int c; | 670 | 91.1k | int i; | 671 | 91.1k | const int C = st->channels; | 672 | 91.1k | celt_sig *decode_mem[2]; | 673 | 91.1k | celt_sig *out_syn[2]; | 674 | 91.1k | opus_val16 *lpc; | 675 | 91.1k | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | 676 | 91.1k | const OpusCustomMode *mode; | 677 | 91.1k | int nbEBands; | 678 | 91.1k | int overlap; | 679 | 91.1k | int start; | 680 | 91.1k | int loss_duration; | 681 | 91.1k | int noise_based; | 682 | 91.1k | const opus_int16 *eBands; | 683 | 91.1k | int decode_buffer_size; | 684 | 91.1k | int max_period; | 685 | 91.1k | #ifdef ENABLE_QEXT | 686 | 91.1k | int qext_scale; | 687 | 91.1k | #endif | 688 | 91.1k | SAVE_STACK; | 689 | 91.1k | #ifdef ENABLE_QEXT | 690 | 91.1k | qext_scale = st->qext_scale; | 691 | 91.1k | #endif | 692 | 91.1k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 693 | 91.1k | max_period = QEXT_SCALE(MAX_PERIOD); | 694 | 91.1k | mode = st->mode; | 695 | 91.1k | nbEBands = mode->nbEBands; | 696 | 91.1k | overlap = mode->overlap; | 697 | 91.1k | eBands = mode->eBands; | 698 | | | 699 | 141k | c=0; do { | 700 | 141k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 701 | 141k | out_syn[c] = decode_mem[c]+decode_buffer_size-N; | 702 | 141k | } while (++c<C); | 703 | 91.1k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C); | 704 | 91.1k | oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER); | 705 | 91.1k | oldLogE = oldBandE + 2*nbEBands; | 706 | 91.1k | oldLogE2 = oldLogE + 2*nbEBands; | 707 | 91.1k | backgroundLogE = oldLogE2 + 2*nbEBands; | 708 | | | 709 | 91.1k | loss_duration = st->loss_duration; | 710 | 91.1k | start = st->start; | 711 | | #ifdef ENABLE_DEEP_PLC | 712 | | if (lpcnet != NULL) noise_based = start != 0 || (lpcnet->fec_fill_pos == 0 && (st->skip_plc || loss_duration >= 80)); | 713 | | else | 714 | | #endif | 715 | 91.1k | noise_based = loss_duration >= 40 || start != 0 || st->skip_plc; | 716 | 91.1k | if (noise_based) | 717 | 18.8k | { | 718 | | /* Noise-based PLC/CNG */ | 719 | 18.8k | VARDECL(celt_norm, X); | 720 | 18.8k | opus_uint32 seed; | 721 | 18.8k | int end; | 722 | 18.8k | int effEnd; | 723 | 18.8k | celt_glog decay; | 724 | 18.8k | end = st->end; | 725 | 18.8k | effEnd = IMAX(start, IMIN(end, mode->effEBands)); | 726 | | | 727 | 18.8k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | 728 | 26.7k | c=0; do { | 729 | 26.7k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, | 730 | 26.7k | decode_buffer_size-N+overlap); | 731 | 26.7k | } while (++c<C); | 732 | | | 733 | 18.8k | if (st->prefilter_and_fold) { | 734 | 398 | prefilter_and_fold(st, N); | 735 | 398 | } | 736 | | | 737 | | /* Energy decay */ | 738 | 18.8k | decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f); | 739 | 18.8k | c=0; do | 740 | 26.7k | { | 741 | 138k | for (i=start;i<end;i++) | 742 | 111k | oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay); | 743 | 26.7k | } while (++c<C); | 744 | 18.8k | seed = st->rng; | 745 | 45.6k | for (c=0;c<C;c++) | 746 | 26.7k | { | 747 | 138k | for (i=start;i<effEnd;i++) | 748 | 111k | { | 749 | 111k | int j; | 750 | 111k | int boffs; | 751 | 111k | int blen; | 752 | 111k | boffs = N*c+(eBands[i]<<LM); | 753 | 111k | blen = (eBands[i+1]-eBands[i])<<LM; | 754 | 5.36M | for (j=0;j<blen;j++) | 755 | 5.24M | { | 756 | 5.24M | seed = celt_lcg_rand(seed); | 757 | 5.24M | X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14); | 758 | 5.24M | } | 759 | 111k | renormalise_vector(X+boffs, blen, Q31ONE, st->arch); | 760 | 111k | } | 761 | 26.7k | } | 762 | 18.8k | st->rng = seed; | 763 | | | 764 | 18.8k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch ARG_QEXT(NULL) ARG_QEXT(NULL) ARG_QEXT(0)); | 765 | | | 766 | | /* Run the postfilter with the last parameters. */ | 767 | 26.7k | c=0; do { | 768 | 26.7k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | 769 | 26.7k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | 770 | 26.7k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | 771 | 26.7k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | 772 | 26.7k | mode->window, overlap, st->arch); | 773 | 26.7k | if (LM!=0) | 774 | 25.2k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, st->postfilter_period, N-mode->shortMdctSize, | 775 | 25.2k | st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, | 776 | 25.2k | mode->window, overlap, st->arch); | 777 | | | 778 | 26.7k | } while (++c<C); | 779 | 18.8k | st->postfilter_period_old = st->postfilter_period; | 780 | 18.8k | st->postfilter_gain_old = st->postfilter_gain; | 781 | 18.8k | st->postfilter_tapset_old = st->postfilter_tapset; | 782 | | | 783 | 18.8k | st->prefilter_and_fold = 0; | 784 | | /* Skip regular PLC until we get two consecutive packets. */ | 785 | 18.8k | st->skip_plc = 1; | 786 | 72.2k | } else { | 787 | 72.2k | int exc_length; | 788 | | /* Pitch-based PLC */ | 789 | 72.2k | const celt_coef *window; | 790 | 72.2k | opus_val16 *exc; | 791 | 72.2k | opus_val16 fade = Q15ONE; | 792 | 72.2k | int pitch_index; | 793 | 72.2k | VARDECL(opus_val16, _exc); | 794 | 72.2k | VARDECL(opus_val16, fir_tmp); | 795 | | | 796 | 72.2k | if (loss_duration == 0) | 797 | 41.4k | { | 798 | | #ifdef ENABLE_DEEP_PLC | 799 | | if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C); | 800 | | #endif | 801 | 41.4k | st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch); | 802 | 41.4k | } else { | 803 | 30.7k | pitch_index = st->last_pitch_index; | 804 | 30.7k | fade = QCONST16(.8f,15); | 805 | 30.7k | } | 806 | | | 807 | | /* We want the excitation for 2 pitch periods in order to look for a | 808 | | decaying signal, but we can't get more than MAX_PERIOD. */ | 809 | 72.2k | exc_length = IMIN(2*pitch_index, max_period); | 810 | | | 811 | 72.2k | ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16); | 812 | 72.2k | ALLOC(fir_tmp, exc_length, opus_val16); | 813 | 72.2k | exc = _exc+CELT_LPC_ORDER; | 814 | 72.2k | window = mode->window; | 815 | 114k | c=0; do { | 816 | 114k | opus_val16 decay; | 817 | 114k | opus_val16 attenuation; | 818 | 114k | opus_val32 S1=0; | 819 | 114k | celt_sig *buf; | 820 | 114k | int extrapolation_offset; | 821 | 114k | int extrapolation_len; | 822 | 114k | int j; | 823 | | | 824 | 114k | buf = decode_mem[c]; | 825 | 135M | for (i=0;i<max_period+CELT_LPC_ORDER;i++) | 826 | 134M | exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT); | 827 | | | 828 | 114k | if (loss_duration == 0) | 829 | 66.1k | { | 830 | 66.1k | opus_val32 ac[CELT_LPC_ORDER+1]; | 831 | | /* Compute LPC coefficients for the last MAX_PERIOD samples before | 832 | | the first loss so we can work in the excitation-filter domain. */ | 833 | 66.1k | _celt_autocorr(exc, ac, window, overlap, | 834 | 66.1k | CELT_LPC_ORDER, max_period, st->arch); | 835 | | /* Add a noise floor of -40 dB. */ | 836 | | #ifdef FIXED_POINT | 837 | | ac[0] += SHR32(ac[0],13); | 838 | | #else | 839 | 66.1k | ac[0] *= 1.0001f; | 840 | 66.1k | #endif | 841 | | /* Use lag windowing to stabilize the Levinson-Durbin recursion. */ | 842 | 1.65M | for (i=1;i<=CELT_LPC_ORDER;i++) | 843 | 1.58M | { | 844 | | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ | 845 | | #ifdef FIXED_POINT | 846 | | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); | 847 | | #else | 848 | 1.58M | ac[i] -= ac[i]*(0.008f*0.008f)*i*i; | 849 | 1.58M | #endif | 850 | 1.58M | } | 851 | 66.1k | _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER); | 852 | | #ifdef FIXED_POINT | 853 | | /* For fixed-point, apply bandwidth expansion until we can guarantee that | 854 | | no overflow can happen in the IIR filter. This means: | 855 | | 32768*sum(abs(filter)) < 2^31 */ | 856 | | while (1) { | 857 | | opus_val16 tmp=Q15ONE; | 858 | | opus_val32 sum=QCONST16(1., SIG_SHIFT); | 859 | | for (i=0;i<CELT_LPC_ORDER;i++) | 860 | | sum += ABS16(lpc[c*CELT_LPC_ORDER+i]); | 861 | | if (sum < 65535) break; | 862 | | for (i=0;i<CELT_LPC_ORDER;i++) | 863 | | { | 864 | | tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp); | 865 | | lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp); | 866 | | } | 867 | | } | 868 | | #endif | 869 | 66.1k | } | 870 | | /* Initialize the LPC history with the samples just before the start | 871 | | of the region for which we're computing the excitation. */ | 872 | 114k | { | 873 | | /* Compute the excitation for exc_length samples before the loss. We need the copy | 874 | | because celt_fir() cannot filter in-place. */ | 875 | 114k | celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER, | 876 | 114k | fir_tmp, exc_length, CELT_LPC_ORDER, st->arch); | 877 | 114k | OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length); | 878 | 114k | } | 879 | | | 880 | | /* Check if the waveform is decaying, and if so how fast. | 881 | | We do this to avoid adding energy when concealing in a segment | 882 | | with decaying energy. */ | 883 | 114k | { | 884 | 114k | opus_val32 E1=1, E2=1; | 885 | 114k | int decay_length; | 886 | | #ifdef FIXED_POINT | 887 | | int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20); | 888 | | #ifdef ENABLE_QEXT | 889 | | if (st->qext_scale==2) shift++; | 890 | | #endif | 891 | | #endif | 892 | 114k | decay_length = exc_length>>1; | 893 | 31.4M | for (i=0;i<decay_length;i++) | 894 | 31.3M | { | 895 | 31.3M | opus_val16 e; | 896 | 31.3M | e = exc[max_period-decay_length+i]; | 897 | 31.3M | E1 += SHR32(MULT16_16(e, e), shift); | 898 | 31.3M | e = exc[max_period-2*decay_length+i]; | 899 | 31.3M | E2 += SHR32(MULT16_16(e, e), shift); | 900 | 31.3M | } | 901 | 114k | E1 = MIN32(E1, E2); | 902 | 114k | decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2)); | 903 | 114k | } | 904 | | | 905 | | /* Move the decoder memory one frame to the left to give us room to | 906 | | add the data for the new frame. We ignore the overlap that extends | 907 | | past the end of the buffer, because we aren't going to use it. */ | 908 | 114k | OPUS_MOVE(buf, buf+N, decode_buffer_size-N); | 909 | | | 910 | | /* Extrapolate from the end of the excitation with a period of | 911 | | "pitch_index", scaling down each period by an additional factor of | 912 | | "decay". */ | 913 | 114k | extrapolation_offset = max_period-pitch_index; | 914 | | /* We need to extrapolate enough samples to cover a complete MDCT | 915 | | window (including overlap/2 samples on both sides). */ | 916 | 114k | extrapolation_len = N+overlap; | 917 | | /* We also apply fading if this is not the first loss. */ | 918 | 114k | attenuation = MULT16_16_Q15(fade, decay); | 919 | 43.4M | for (i=j=0;i<extrapolation_len;i++,j++) | 920 | 43.3M | { | 921 | 43.3M | opus_val16 tmp; | 922 | 43.3M | if (j >= pitch_index) { | 923 | 142k | j -= pitch_index; | 924 | 142k | attenuation = MULT16_16_Q15(attenuation, decay); | 925 | 142k | } | 926 | 43.3M | buf[decode_buffer_size-N+i] = | 927 | 43.3M | SHL32(EXTEND32(MULT16_16_Q15(attenuation, | 928 | 43.3M | exc[extrapolation_offset+j])), SIG_SHIFT); | 929 | | /* Compute the energy of the previously decoded signal whose | 930 | | excitation we're copying. */ | 931 | 43.3M | tmp = SROUND16( | 932 | 43.3M | buf[decode_buffer_size-max_period-N+extrapolation_offset+j], | 933 | 43.3M | SIG_SHIFT); | 934 | 43.3M | S1 += SHR32(MULT16_16(tmp, tmp), 11); | 935 | 43.3M | } | 936 | 114k | { | 937 | 114k | opus_val16 lpc_mem[CELT_LPC_ORDER]; | 938 | | /* Copy the last decoded samples (prior to the overlap region) to | 939 | | synthesis filter memory so we can have a continuous signal. */ | 940 | 2.86M | for (i=0;i<CELT_LPC_ORDER;i++) | 941 | 2.74M | lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT); | 942 | | /* Apply the synthesis filter to convert the excitation back into | 943 | | the signal domain. */ | 944 | 114k | celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER, | 945 | 114k | buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER, | 946 | 114k | lpc_mem, st->arch); | 947 | | #ifdef FIXED_POINT | 948 | | for (i=0; i < extrapolation_len; i++) | 949 | | buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT); | 950 | | #endif | 951 | 114k | } | 952 | | | 953 | | /* Check if the synthesis energy is higher than expected, which can | 954 | | happen with the signal changes during our window. If so, | 955 | | attenuate. */ | 956 | 114k | { | 957 | 114k | opus_val32 S2=0; | 958 | 43.4M | for (i=0;i<extrapolation_len;i++) | 959 | 43.3M | { | 960 | 43.3M | opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT); | 961 | 43.3M | S2 += SHR32(MULT16_16(tmp, tmp), 11); | 962 | 43.3M | } | 963 | | /* This checks for an "explosion" in the synthesis. */ | 964 | | #ifdef FIXED_POINT | 965 | | if (!(S1 > SHR32(S2,2))) | 966 | | #else | 967 | | /* The float test is written this way to catch NaNs in the output | 968 | | of the IIR filter at the same time. */ | 969 | 114k | if (!(S1 > 0.2f*S2)) | 970 | 14.8k | #endif | 971 | 14.8k | { | 972 | 5.49M | for (i=0;i<extrapolation_len;i++) | 973 | 5.48M | buf[decode_buffer_size-N+i] = 0; | 974 | 99.7k | } else if (S1 < S2) | 975 | 20.1k | { | 976 | 20.1k | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); | 977 | 2.83M | for (i=0;i<overlap;i++) | 978 | 2.81M | { | 979 | 2.81M | opus_val16 tmp_g = Q15ONE | 980 | 2.81M | - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio); | 981 | 2.81M | buf[decode_buffer_size-N+i] = | 982 | 2.81M | MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]); | 983 | 2.81M | } | 984 | 3.67M | for (i=overlap;i<extrapolation_len;i++) | 985 | 3.65M | { | 986 | 3.65M | buf[decode_buffer_size-N+i] = | 987 | 3.65M | MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]); | 988 | 3.65M | } | 989 | 20.1k | } | 990 | 114k | } | 991 | | | 992 | 114k | } while (++c<C); | 993 | | | 994 | | #ifdef ENABLE_DEEP_PLC | 995 | | if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) { | 996 | | float overlap_mem; | 997 | | int samples_needed16k; | 998 | | celt_sig *buf; | 999 | | VARDECL(float, buf_copy); | 1000 | | buf = decode_mem[0]; | 1001 | | ALLOC(buf_copy, C*overlap, float); | 1002 | | c=0; do { | 1003 | | OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap); | 1004 | | } while (++c<C); | 1005 | | | 1006 | | /* Need enough samples from the PLC to cover the frame size, resampling delay, | 1007 | | and the overlap at the end. */ | 1008 | | samples_needed16k = (N+SINC_ORDER+overlap)/3; | 1009 | | if (loss_duration == 0) { | 1010 | | st->plc_fill = 0; | 1011 | | } | 1012 | | while (st->plc_fill < samples_needed16k) { | 1013 | | lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]); | 1014 | | st->plc_fill += FRAME_SIZE; | 1015 | | } | 1016 | | /* Resample to 48 kHz. */ | 1017 | | for (i=0;i<(N+overlap)/3;i++) { | 1018 | | int j; | 1019 | | float sum; | 1020 | | for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j]; | 1021 | | buf[decode_buffer_size-N+3*i] = sum; | 1022 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2]; | 1023 | | buf[decode_buffer_size-N+3*i+1] = sum; | 1024 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1]; | 1025 | | buf[decode_buffer_size-N+3*i+2] = sum; | 1026 | | } | 1027 | | OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3); | 1028 | | st->plc_fill -= N/3; | 1029 | | for (i=0;i<N;i++) { | 1030 | | float tmp = buf[decode_buffer_size-N+i]; | 1031 | | buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem; | 1032 | | st->plc_preemphasis_mem = tmp; | 1033 | | } | 1034 | | overlap_mem = st->plc_preemphasis_mem; | 1035 | | for (i=0;i<overlap;i++) { | 1036 | | float tmp = buf[decode_buffer_size+i]; | 1037 | | buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem; | 1038 | | overlap_mem = tmp; | 1039 | | } | 1040 | | /* For now, we just do mono PLC. */ | 1041 | | if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap); | 1042 | | c=0; do { | 1043 | | /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */ | 1044 | | if (loss_duration == 0) { | 1045 | | for (i=0;i<overlap;i++) decode_mem[c][decode_buffer_size-N+i] = (1-window[i])*buf_copy[c*overlap+i] + (window[i])*decode_mem[c][decode_buffer_size-N+i]; | 1046 | | } | 1047 | | } while (++c<C); | 1048 | | } | 1049 | | #endif | 1050 | 72.2k | st->prefilter_and_fold = 1; | 1051 | 72.2k | } | 1052 | | | 1053 | | /* Saturate to something large to avoid wrap-around. */ | 1054 | 91.1k | st->loss_duration = IMIN(10000, loss_duration+(1<<LM)); | 1055 | | | 1056 | 91.1k | RESTORE_STACK; | 1057 | 91.1k | } |
celt_decoder.c:celt_decode_lost Line | Count | Source | 668 | 91.1k | { | 669 | 91.1k | int c; | 670 | 91.1k | int i; | 671 | 91.1k | const int C = st->channels; | 672 | 91.1k | celt_sig *decode_mem[2]; | 673 | 91.1k | celt_sig *out_syn[2]; | 674 | 91.1k | opus_val16 *lpc; | 675 | 91.1k | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | 676 | 91.1k | const OpusCustomMode *mode; | 677 | 91.1k | int nbEBands; | 678 | 91.1k | int overlap; | 679 | 91.1k | int start; | 680 | 91.1k | int loss_duration; | 681 | 91.1k | int noise_based; | 682 | 91.1k | const opus_int16 *eBands; | 683 | 91.1k | int decode_buffer_size; | 684 | 91.1k | int max_period; | 685 | | #ifdef ENABLE_QEXT | 686 | | int qext_scale; | 687 | | #endif | 688 | 91.1k | SAVE_STACK; | 689 | | #ifdef ENABLE_QEXT | 690 | | qext_scale = st->qext_scale; | 691 | | #endif | 692 | 91.1k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 693 | 91.1k | max_period = QEXT_SCALE(MAX_PERIOD); | 694 | 91.1k | mode = st->mode; | 695 | 91.1k | nbEBands = mode->nbEBands; | 696 | 91.1k | overlap = mode->overlap; | 697 | 91.1k | eBands = mode->eBands; | 698 | | | 699 | 141k | c=0; do { | 700 | 141k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 701 | 141k | out_syn[c] = decode_mem[c]+decode_buffer_size-N; | 702 | 141k | } while (++c<C); | 703 | 91.1k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C); | 704 | 91.1k | oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER); | 705 | 91.1k | oldLogE = oldBandE + 2*nbEBands; | 706 | 91.1k | oldLogE2 = oldLogE + 2*nbEBands; | 707 | 91.1k | backgroundLogE = oldLogE2 + 2*nbEBands; | 708 | | | 709 | 91.1k | loss_duration = st->loss_duration; | 710 | 91.1k | start = st->start; | 711 | | #ifdef ENABLE_DEEP_PLC | 712 | | if (lpcnet != NULL) noise_based = start != 0 || (lpcnet->fec_fill_pos == 0 && (st->skip_plc || loss_duration >= 80)); | 713 | | else | 714 | | #endif | 715 | 91.1k | noise_based = loss_duration >= 40 || start != 0 || st->skip_plc; | 716 | 91.1k | if (noise_based) | 717 | 18.8k | { | 718 | | /* Noise-based PLC/CNG */ | 719 | 18.8k | VARDECL(celt_norm, X); | 720 | 18.8k | opus_uint32 seed; | 721 | 18.8k | int end; | 722 | 18.8k | int effEnd; | 723 | 18.8k | celt_glog decay; | 724 | 18.8k | end = st->end; | 725 | 18.8k | effEnd = IMAX(start, IMIN(end, mode->effEBands)); | 726 | | | 727 | 18.8k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | 728 | 26.7k | c=0; do { | 729 | 26.7k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, | 730 | 26.7k | decode_buffer_size-N+overlap); | 731 | 26.7k | } while (++c<C); | 732 | | | 733 | 18.8k | if (st->prefilter_and_fold) { | 734 | 398 | prefilter_and_fold(st, N); | 735 | 398 | } | 736 | | | 737 | | /* Energy decay */ | 738 | 18.8k | decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f); | 739 | 18.8k | c=0; do | 740 | 26.7k | { | 741 | 138k | for (i=start;i<end;i++) | 742 | 111k | oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay); | 743 | 26.7k | } while (++c<C); | 744 | 18.8k | seed = st->rng; | 745 | 45.6k | for (c=0;c<C;c++) | 746 | 26.7k | { | 747 | 138k | for (i=start;i<effEnd;i++) | 748 | 111k | { | 749 | 111k | int j; | 750 | 111k | int boffs; | 751 | 111k | int blen; | 752 | 111k | boffs = N*c+(eBands[i]<<LM); | 753 | 111k | blen = (eBands[i+1]-eBands[i])<<LM; | 754 | 5.36M | for (j=0;j<blen;j++) | 755 | 5.24M | { | 756 | 5.24M | seed = celt_lcg_rand(seed); | 757 | 5.24M | X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14); | 758 | 5.24M | } | 759 | 111k | renormalise_vector(X+boffs, blen, Q31ONE, st->arch); | 760 | 111k | } | 761 | 26.7k | } | 762 | 18.8k | st->rng = seed; | 763 | | | 764 | 18.8k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch ARG_QEXT(NULL) ARG_QEXT(NULL) ARG_QEXT(0)); | 765 | | | 766 | | /* Run the postfilter with the last parameters. */ | 767 | 26.7k | c=0; do { | 768 | 26.7k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | 769 | 26.7k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | 770 | 26.7k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | 771 | 26.7k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | 772 | 26.7k | mode->window, overlap, st->arch); | 773 | 26.7k | if (LM!=0) | 774 | 25.2k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, st->postfilter_period, N-mode->shortMdctSize, | 775 | 25.2k | st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, | 776 | 25.2k | mode->window, overlap, st->arch); | 777 | | | 778 | 26.7k | } while (++c<C); | 779 | 18.8k | st->postfilter_period_old = st->postfilter_period; | 780 | 18.8k | st->postfilter_gain_old = st->postfilter_gain; | 781 | 18.8k | st->postfilter_tapset_old = st->postfilter_tapset; | 782 | | | 783 | 18.8k | st->prefilter_and_fold = 0; | 784 | | /* Skip regular PLC until we get two consecutive packets. */ | 785 | 18.8k | st->skip_plc = 1; | 786 | 72.2k | } else { | 787 | 72.2k | int exc_length; | 788 | | /* Pitch-based PLC */ | 789 | 72.2k | const celt_coef *window; | 790 | 72.2k | opus_val16 *exc; | 791 | 72.2k | opus_val16 fade = Q15ONE; | 792 | 72.2k | int pitch_index; | 793 | 72.2k | VARDECL(opus_val16, _exc); | 794 | 72.2k | VARDECL(opus_val16, fir_tmp); | 795 | | | 796 | 72.2k | if (loss_duration == 0) | 797 | 41.4k | { | 798 | | #ifdef ENABLE_DEEP_PLC | 799 | | if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C); | 800 | | #endif | 801 | 41.4k | st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch); | 802 | 41.4k | } else { | 803 | 30.7k | pitch_index = st->last_pitch_index; | 804 | 30.7k | fade = QCONST16(.8f,15); | 805 | 30.7k | } | 806 | | | 807 | | /* We want the excitation for 2 pitch periods in order to look for a | 808 | | decaying signal, but we can't get more than MAX_PERIOD. */ | 809 | 72.2k | exc_length = IMIN(2*pitch_index, max_period); | 810 | | | 811 | 72.2k | ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16); | 812 | 72.2k | ALLOC(fir_tmp, exc_length, opus_val16); | 813 | 72.2k | exc = _exc+CELT_LPC_ORDER; | 814 | 72.2k | window = mode->window; | 815 | 114k | c=0; do { | 816 | 114k | opus_val16 decay; | 817 | 114k | opus_val16 attenuation; | 818 | 114k | opus_val32 S1=0; | 819 | 114k | celt_sig *buf; | 820 | 114k | int extrapolation_offset; | 821 | 114k | int extrapolation_len; | 822 | 114k | int j; | 823 | | | 824 | 114k | buf = decode_mem[c]; | 825 | 135M | for (i=0;i<max_period+CELT_LPC_ORDER;i++) | 826 | 134M | exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT); | 827 | | | 828 | 114k | if (loss_duration == 0) | 829 | 66.1k | { | 830 | 66.1k | opus_val32 ac[CELT_LPC_ORDER+1]; | 831 | | /* Compute LPC coefficients for the last MAX_PERIOD samples before | 832 | | the first loss so we can work in the excitation-filter domain. */ | 833 | 66.1k | _celt_autocorr(exc, ac, window, overlap, | 834 | 66.1k | CELT_LPC_ORDER, max_period, st->arch); | 835 | | /* Add a noise floor of -40 dB. */ | 836 | | #ifdef FIXED_POINT | 837 | | ac[0] += SHR32(ac[0],13); | 838 | | #else | 839 | 66.1k | ac[0] *= 1.0001f; | 840 | 66.1k | #endif | 841 | | /* Use lag windowing to stabilize the Levinson-Durbin recursion. */ | 842 | 1.65M | for (i=1;i<=CELT_LPC_ORDER;i++) | 843 | 1.58M | { | 844 | | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ | 845 | | #ifdef FIXED_POINT | 846 | | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); | 847 | | #else | 848 | 1.58M | ac[i] -= ac[i]*(0.008f*0.008f)*i*i; | 849 | 1.58M | #endif | 850 | 1.58M | } | 851 | 66.1k | _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER); | 852 | | #ifdef FIXED_POINT | 853 | | /* For fixed-point, apply bandwidth expansion until we can guarantee that | 854 | | no overflow can happen in the IIR filter. This means: | 855 | | 32768*sum(abs(filter)) < 2^31 */ | 856 | | while (1) { | 857 | | opus_val16 tmp=Q15ONE; | 858 | | opus_val32 sum=QCONST16(1., SIG_SHIFT); | 859 | | for (i=0;i<CELT_LPC_ORDER;i++) | 860 | | sum += ABS16(lpc[c*CELT_LPC_ORDER+i]); | 861 | | if (sum < 65535) break; | 862 | | for (i=0;i<CELT_LPC_ORDER;i++) | 863 | | { | 864 | | tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp); | 865 | | lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp); | 866 | | } | 867 | | } | 868 | | #endif | 869 | 66.1k | } | 870 | | /* Initialize the LPC history with the samples just before the start | 871 | | of the region for which we're computing the excitation. */ | 872 | 114k | { | 873 | | /* Compute the excitation for exc_length samples before the loss. We need the copy | 874 | | because celt_fir() cannot filter in-place. */ | 875 | 114k | celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER, | 876 | 114k | fir_tmp, exc_length, CELT_LPC_ORDER, st->arch); | 877 | 114k | OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length); | 878 | 114k | } | 879 | | | 880 | | /* Check if the waveform is decaying, and if so how fast. | 881 | | We do this to avoid adding energy when concealing in a segment | 882 | | with decaying energy. */ | 883 | 114k | { | 884 | 114k | opus_val32 E1=1, E2=1; | 885 | 114k | int decay_length; | 886 | | #ifdef FIXED_POINT | 887 | | int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20); | 888 | | #ifdef ENABLE_QEXT | 889 | | if (st->qext_scale==2) shift++; | 890 | | #endif | 891 | | #endif | 892 | 114k | decay_length = exc_length>>1; | 893 | 31.4M | for (i=0;i<decay_length;i++) | 894 | 31.3M | { | 895 | 31.3M | opus_val16 e; | 896 | 31.3M | e = exc[max_period-decay_length+i]; | 897 | 31.3M | E1 += SHR32(MULT16_16(e, e), shift); | 898 | 31.3M | e = exc[max_period-2*decay_length+i]; | 899 | 31.3M | E2 += SHR32(MULT16_16(e, e), shift); | 900 | 31.3M | } | 901 | 114k | E1 = MIN32(E1, E2); | 902 | 114k | decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2)); | 903 | 114k | } | 904 | | | 905 | | /* Move the decoder memory one frame to the left to give us room to | 906 | | add the data for the new frame. We ignore the overlap that extends | 907 | | past the end of the buffer, because we aren't going to use it. */ | 908 | 114k | OPUS_MOVE(buf, buf+N, decode_buffer_size-N); | 909 | | | 910 | | /* Extrapolate from the end of the excitation with a period of | 911 | | "pitch_index", scaling down each period by an additional factor of | 912 | | "decay". */ | 913 | 114k | extrapolation_offset = max_period-pitch_index; | 914 | | /* We need to extrapolate enough samples to cover a complete MDCT | 915 | | window (including overlap/2 samples on both sides). */ | 916 | 114k | extrapolation_len = N+overlap; | 917 | | /* We also apply fading if this is not the first loss. */ | 918 | 114k | attenuation = MULT16_16_Q15(fade, decay); | 919 | 43.4M | for (i=j=0;i<extrapolation_len;i++,j++) | 920 | 43.3M | { | 921 | 43.3M | opus_val16 tmp; | 922 | 43.3M | if (j >= pitch_index) { | 923 | 142k | j -= pitch_index; | 924 | 142k | attenuation = MULT16_16_Q15(attenuation, decay); | 925 | 142k | } | 926 | 43.3M | buf[decode_buffer_size-N+i] = | 927 | 43.3M | SHL32(EXTEND32(MULT16_16_Q15(attenuation, | 928 | 43.3M | exc[extrapolation_offset+j])), SIG_SHIFT); | 929 | | /* Compute the energy of the previously decoded signal whose | 930 | | excitation we're copying. */ | 931 | 43.3M | tmp = SROUND16( | 932 | 43.3M | buf[decode_buffer_size-max_period-N+extrapolation_offset+j], | 933 | 43.3M | SIG_SHIFT); | 934 | 43.3M | S1 += SHR32(MULT16_16(tmp, tmp), 11); | 935 | 43.3M | } | 936 | 114k | { | 937 | 114k | opus_val16 lpc_mem[CELT_LPC_ORDER]; | 938 | | /* Copy the last decoded samples (prior to the overlap region) to | 939 | | synthesis filter memory so we can have a continuous signal. */ | 940 | 2.86M | for (i=0;i<CELT_LPC_ORDER;i++) | 941 | 2.74M | lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT); | 942 | | /* Apply the synthesis filter to convert the excitation back into | 943 | | the signal domain. */ | 944 | 114k | celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER, | 945 | 114k | buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER, | 946 | 114k | lpc_mem, st->arch); | 947 | | #ifdef FIXED_POINT | 948 | | for (i=0; i < extrapolation_len; i++) | 949 | | buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT); | 950 | | #endif | 951 | 114k | } | 952 | | | 953 | | /* Check if the synthesis energy is higher than expected, which can | 954 | | happen with the signal changes during our window. If so, | 955 | | attenuate. */ | 956 | 114k | { | 957 | 114k | opus_val32 S2=0; | 958 | 43.4M | for (i=0;i<extrapolation_len;i++) | 959 | 43.3M | { | 960 | 43.3M | opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT); | 961 | 43.3M | S2 += SHR32(MULT16_16(tmp, tmp), 11); | 962 | 43.3M | } | 963 | | /* This checks for an "explosion" in the synthesis. */ | 964 | | #ifdef FIXED_POINT | 965 | | if (!(S1 > SHR32(S2,2))) | 966 | | #else | 967 | | /* The float test is written this way to catch NaNs in the output | 968 | | of the IIR filter at the same time. */ | 969 | 114k | if (!(S1 > 0.2f*S2)) | 970 | 14.8k | #endif | 971 | 14.8k | { | 972 | 5.49M | for (i=0;i<extrapolation_len;i++) | 973 | 5.48M | buf[decode_buffer_size-N+i] = 0; | 974 | 99.7k | } else if (S1 < S2) | 975 | 20.1k | { | 976 | 20.1k | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); | 977 | 2.83M | for (i=0;i<overlap;i++) | 978 | 2.81M | { | 979 | 2.81M | opus_val16 tmp_g = Q15ONE | 980 | 2.81M | - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio); | 981 | 2.81M | buf[decode_buffer_size-N+i] = | 982 | 2.81M | MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]); | 983 | 2.81M | } | 984 | 3.67M | for (i=overlap;i<extrapolation_len;i++) | 985 | 3.65M | { | 986 | 3.65M | buf[decode_buffer_size-N+i] = | 987 | 3.65M | MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]); | 988 | 3.65M | } | 989 | 20.1k | } | 990 | 114k | } | 991 | | | 992 | 114k | } while (++c<C); | 993 | | | 994 | | #ifdef ENABLE_DEEP_PLC | 995 | | if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) { | 996 | | float overlap_mem; | 997 | | int samples_needed16k; | 998 | | celt_sig *buf; | 999 | | VARDECL(float, buf_copy); | 1000 | | buf = decode_mem[0]; | 1001 | | ALLOC(buf_copy, C*overlap, float); | 1002 | | c=0; do { | 1003 | | OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap); | 1004 | | } while (++c<C); | 1005 | | | 1006 | | /* Need enough samples from the PLC to cover the frame size, resampling delay, | 1007 | | and the overlap at the end. */ | 1008 | | samples_needed16k = (N+SINC_ORDER+overlap)/3; | 1009 | | if (loss_duration == 0) { | 1010 | | st->plc_fill = 0; | 1011 | | } | 1012 | | while (st->plc_fill < samples_needed16k) { | 1013 | | lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]); | 1014 | | st->plc_fill += FRAME_SIZE; | 1015 | | } | 1016 | | /* Resample to 48 kHz. */ | 1017 | | for (i=0;i<(N+overlap)/3;i++) { | 1018 | | int j; | 1019 | | float sum; | 1020 | | for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j]; | 1021 | | buf[decode_buffer_size-N+3*i] = sum; | 1022 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2]; | 1023 | | buf[decode_buffer_size-N+3*i+1] = sum; | 1024 | | for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1]; | 1025 | | buf[decode_buffer_size-N+3*i+2] = sum; | 1026 | | } | 1027 | | OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3); | 1028 | | st->plc_fill -= N/3; | 1029 | | for (i=0;i<N;i++) { | 1030 | | float tmp = buf[decode_buffer_size-N+i]; | 1031 | | buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem; | 1032 | | st->plc_preemphasis_mem = tmp; | 1033 | | } | 1034 | | overlap_mem = st->plc_preemphasis_mem; | 1035 | | for (i=0;i<overlap;i++) { | 1036 | | float tmp = buf[decode_buffer_size+i]; | 1037 | | buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem; | 1038 | | overlap_mem = tmp; | 1039 | | } | 1040 | | /* For now, we just do mono PLC. */ | 1041 | | if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap); | 1042 | | c=0; do { | 1043 | | /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */ | 1044 | | if (loss_duration == 0) { | 1045 | | for (i=0;i<overlap;i++) decode_mem[c][decode_buffer_size-N+i] = (1-window[i])*buf_copy[c*overlap+i] + (window[i])*decode_mem[c][decode_buffer_size-N+i]; | 1046 | | } | 1047 | | } while (++c<C); | 1048 | | } | 1049 | | #endif | 1050 | 72.2k | st->prefilter_and_fold = 1; | 1051 | 72.2k | } | 1052 | | | 1053 | | /* Saturate to something large to avoid wrap-around. */ | 1054 | 91.1k | st->loss_duration = IMIN(10000, loss_duration+(1<<LM)); | 1055 | | | 1056 | 91.1k | RESTORE_STACK; | 1057 | 91.1k | } |
|
1058 | | |
1059 | | #ifdef ENABLE_QEXT |
1060 | 5.87k | static void decode_qext_stereo_params(ec_dec *ec, int qext_end, int *qext_intensity, int *qext_dual_stereo) { |
1061 | 5.87k | *qext_intensity = ec_dec_uint(ec, qext_end+1); |
1062 | 5.87k | if (*qext_intensity != 0) *qext_dual_stereo = ec_dec_bit_logp(ec, 1); |
1063 | 3.05k | else *qext_dual_stereo = 0; |
1064 | 5.87k | } |
1065 | | #endif |
1066 | | |
1067 | | int celt_decode_with_ec_dred(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, |
1068 | | int len, opus_res * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum |
1069 | | #ifdef ENABLE_DEEP_PLC |
1070 | | ,LPCNetPLCState *lpcnet |
1071 | | #endif |
1072 | | ARG_QEXT(const unsigned char *qext_payload) ARG_QEXT(int qext_payload_len) |
1073 | | ) |
1074 | 1.03M | { |
1075 | 1.03M | int c, i, N; |
1076 | 1.03M | int spread_decision; |
1077 | 1.03M | opus_int32 bits; |
1078 | 1.03M | ec_dec _dec; |
1079 | 1.03M | VARDECL(celt_norm, X); |
1080 | 1.03M | VARDECL(int, fine_quant); |
1081 | 1.03M | VARDECL(int, pulses); |
1082 | 1.03M | VARDECL(int, cap); |
1083 | 1.03M | VARDECL(int, offsets); |
1084 | 1.03M | VARDECL(int, fine_priority); |
1085 | 1.03M | VARDECL(int, tf_res); |
1086 | 1.03M | VARDECL(unsigned char, collapse_masks); |
1087 | 1.03M | celt_sig *decode_mem[2]; |
1088 | 1.03M | celt_sig *out_syn[2]; |
1089 | 1.03M | opus_val16 *lpc; |
1090 | 1.03M | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; |
1091 | | |
1092 | 1.03M | int shortBlocks; |
1093 | 1.03M | int isTransient; |
1094 | 1.03M | int intra_ener; |
1095 | 1.03M | const int CC = st->channels; |
1096 | 1.03M | int LM, M; |
1097 | 1.03M | int start; |
1098 | 1.03M | int end; |
1099 | 1.03M | int effEnd; |
1100 | 1.03M | int codedBands; |
1101 | 1.03M | int alloc_trim; |
1102 | 1.03M | int postfilter_pitch; |
1103 | 1.03M | opus_val16 postfilter_gain; |
1104 | 1.03M | int intensity=0; |
1105 | 1.03M | int dual_stereo=0; |
1106 | 1.03M | opus_int32 total_bits; |
1107 | 1.03M | opus_int32 balance; |
1108 | 1.03M | opus_int32 tell; |
1109 | 1.03M | int dynalloc_logp; |
1110 | 1.03M | int postfilter_tapset; |
1111 | 1.03M | int anti_collapse_rsv; |
1112 | 1.03M | int anti_collapse_on=0; |
1113 | 1.03M | int silence; |
1114 | 1.03M | int C = st->stream_channels; |
1115 | 1.03M | const OpusCustomMode *mode; |
1116 | 1.03M | int nbEBands; |
1117 | 1.03M | int overlap; |
1118 | 1.03M | const opus_int16 *eBands; |
1119 | 1.03M | celt_glog max_background_increase; |
1120 | 1.03M | int decode_buffer_size; |
1121 | | #ifdef ENABLE_QEXT |
1122 | | opus_int32 qext_bits; |
1123 | | ec_dec ext_dec; |
1124 | | int qext_bytes=0; |
1125 | | int qext_end=0; |
1126 | | int qext_intensity=0; |
1127 | | int qext_dual_stereo=0; |
1128 | | VARDECL(int, extra_quant); |
1129 | | VARDECL(int, extra_pulses); |
1130 | | const CELTMode *qext_mode = NULL; |
1131 | 501k | CELTMode qext_mode_struct; |
1132 | | celt_glog *qext_oldBandE=NULL; |
1133 | | int qext_scale; |
1134 | | #else |
1135 | 301k | # define qext_bytes 0 |
1136 | | #endif |
1137 | 1.03M | ALLOC_STACK; |
1138 | | #ifdef ENABLE_QEXT |
1139 | | qext_scale = st->qext_scale; |
1140 | | #endif |
1141 | 1.03M | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); |
1142 | | |
1143 | 1.03M | VALIDATE_CELT_DECODER(st); |
1144 | 1.03M | mode = st->mode; |
1145 | 1.03M | nbEBands = mode->nbEBands; |
1146 | 1.03M | overlap = mode->overlap; |
1147 | 1.03M | eBands = mode->eBands; |
1148 | 1.03M | start = st->start; |
1149 | 1.03M | end = st->end; |
1150 | 1.03M | frame_size *= st->downsample; |
1151 | | |
1152 | 1.03M | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC); |
1153 | 1.03M | oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER); |
1154 | 1.03M | oldLogE = oldBandE + 2*nbEBands; |
1155 | 1.03M | oldLogE2 = oldLogE + 2*nbEBands; |
1156 | 1.03M | backgroundLogE = oldLogE2 + 2*nbEBands; |
1157 | | |
1158 | | #ifdef ENABLE_QEXT |
1159 | 501k | if (qext_payload) { |
1160 | 24.2k | ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len); |
1161 | 24.2k | qext_bytes = qext_payload_len; |
1162 | 476k | } else { |
1163 | 476k | ec_dec_init(&ext_dec, NULL, 0); |
1164 | 476k | } |
1165 | | #endif |
1166 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) |
1167 | | if (st->signalling && data!=NULL) |
1168 | | { |
1169 | | int data0=data[0]; |
1170 | | /* Convert "standard mode" to Opus header */ |
1171 | | # ifndef ENABLE_QEXT |
1172 | | if (mode->Fs==48000 && mode->shortMdctSize==120) |
1173 | | # endif |
1174 | | { |
1175 | | data0 = fromOpus(data0); |
1176 | | if (data0<0) |
1177 | | return OPUS_INVALID_PACKET; |
1178 | | } |
1179 | | st->end = end = IMAX(1, mode->effEBands-2*(data0>>5)); |
1180 | | LM = (data0>>3)&0x3; |
1181 | | C = 1 + ((data0>>2)&0x1); |
1182 | | if ((data[0] & 0x03) == 0x03) { |
1183 | | data++; |
1184 | | len--; |
1185 | | if (len<=0) |
1186 | | return OPUS_INVALID_PACKET; |
1187 | | if (data[0] & 0x40) { |
1188 | | int p; |
1189 | | int padding=0; |
1190 | | data++; |
1191 | | len--; |
1192 | | do { |
1193 | | int tmp; |
1194 | | if (len<=0) |
1195 | | return OPUS_INVALID_PACKET; |
1196 | | p = *data++; |
1197 | | len--; |
1198 | | tmp = p==255 ? 254: p; |
1199 | | len -= tmp; |
1200 | | padding += tmp; |
1201 | | } while (p==255); |
1202 | | padding--; |
1203 | | if (len <= 0 || padding<0) return OPUS_INVALID_PACKET; |
1204 | | #ifdef ENABLE_QEXT |
1205 | | qext_bytes = padding; |
1206 | | if (data[len] != QEXT_EXTENSION_ID<<1) |
1207 | | qext_bytes=0; |
1208 | | ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes); |
1209 | | #endif |
1210 | | } |
1211 | | } else |
1212 | | { |
1213 | | data++; |
1214 | | len--; |
1215 | | } |
1216 | | if (LM>mode->maxLM) |
1217 | | return OPUS_INVALID_PACKET; |
1218 | | if (frame_size < mode->shortMdctSize<<LM) |
1219 | | return OPUS_BUFFER_TOO_SMALL; |
1220 | | else |
1221 | | frame_size = mode->shortMdctSize<<LM; |
1222 | | } else { |
1223 | | #else |
1224 | 1.03M | { |
1225 | 1.03M | #endif |
1226 | 1.90M | for (LM=0;LM<=mode->maxLM;LM++) |
1227 | 1.90M | if (mode->shortMdctSize<<LM==frame_size) |
1228 | 1.03M | break; |
1229 | 1.03M | if (LM>mode->maxLM) |
1230 | 0 | return OPUS_BAD_ARG; |
1231 | 1.03M | } |
1232 | 1.03M | M=1<<LM; |
1233 | | |
1234 | 1.03M | if (len<0 || len>1275 || pcm==NULL) |
1235 | 0 | return OPUS_BAD_ARG; |
1236 | | |
1237 | 1.03M | N = M*mode->shortMdctSize; |
1238 | 1.56M | c=0; do { |
1239 | 1.56M | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); |
1240 | 1.56M | out_syn[c] = decode_mem[c]+decode_buffer_size-N; |
1241 | 1.56M | } while (++c<CC); |
1242 | | |
1243 | 1.03M | effEnd = end; |
1244 | 1.03M | if (effEnd > mode->effEBands) |
1245 | 0 | effEnd = mode->effEBands; |
1246 | | |
1247 | 1.03M | if (data == NULL || len<=1) |
1248 | 425k | { |
1249 | 425k | celt_decode_lost(st, N, LM |
1250 | | #ifdef ENABLE_DEEP_PLC |
1251 | | , lpcnet |
1252 | | #endif |
1253 | 425k | ); |
1254 | 425k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); |
1255 | 425k | RESTORE_STACK; |
1256 | 425k | return frame_size/st->downsample; |
1257 | 425k | } |
1258 | | #ifdef ENABLE_DEEP_PLC |
1259 | | else { |
1260 | | /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */ |
1261 | | if (lpcnet) lpcnet->blend = 0; |
1262 | | } |
1263 | | #endif |
1264 | | |
1265 | | /* Check if there are at least two packets received consecutively before |
1266 | | * turning on the pitch-based PLC */ |
1267 | 607k | if (st->loss_duration == 0) st->skip_plc = 0; |
1268 | | |
1269 | 607k | if (dec == NULL) |
1270 | 61.8k | { |
1271 | 61.8k | ec_dec_init(&_dec,(unsigned char*)data,len); |
1272 | 61.8k | dec = &_dec; |
1273 | 61.8k | } |
1274 | | |
1275 | 607k | if (C==1) |
1276 | 389k | { |
1277 | 8.56M | for (i=0;i<nbEBands;i++) |
1278 | 8.17M | oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]); |
1279 | 389k | } |
1280 | | |
1281 | 607k | total_bits = len*8; |
1282 | 607k | tell = ec_tell(dec); |
1283 | | |
1284 | 607k | if (tell >= total_bits) |
1285 | 47.4k | silence = 1; |
1286 | 559k | else if (tell==1) |
1287 | 537k | silence = ec_dec_bit_logp(dec, 15); |
1288 | 22.7k | else |
1289 | 22.7k | silence = 0; |
1290 | 607k | if (silence) |
1291 | 71.2k | { |
1292 | | /* Pretend we've read all the remaining bits */ |
1293 | 71.2k | tell = len*8; |
1294 | 71.2k | dec->nbits_total+=tell-ec_tell(dec); |
1295 | 71.2k | } |
1296 | | |
1297 | 607k | postfilter_gain = 0; |
1298 | 607k | postfilter_pitch = 0; |
1299 | 607k | postfilter_tapset = 0; |
1300 | 607k | if (start==0 && tell+16 <= total_bits) |
1301 | 365k | { |
1302 | 365k | if(ec_dec_bit_logp(dec, 1)) |
1303 | 106k | { |
1304 | 106k | int qg, octave; |
1305 | 106k | octave = ec_dec_uint(dec, 6); |
1306 | 106k | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; |
1307 | 106k | qg = ec_dec_bits(dec, 3); |
1308 | 106k | if (ec_tell(dec)+2<=total_bits) |
1309 | 106k | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); |
1310 | 106k | postfilter_gain = QCONST16(.09375f,15)*(qg+1); |
1311 | 106k | } |
1312 | 365k | tell = ec_tell(dec); |
1313 | 365k | } |
1314 | | |
1315 | 607k | if (LM > 0 && tell+3 <= total_bits) |
1316 | 284k | { |
1317 | 284k | isTransient = ec_dec_bit_logp(dec, 3); |
1318 | 284k | tell = ec_tell(dec); |
1319 | 284k | } |
1320 | 323k | else |
1321 | 323k | isTransient = 0; |
1322 | | |
1323 | 607k | if (isTransient) |
1324 | 53.6k | shortBlocks = M; |
1325 | 553k | else |
1326 | 553k | shortBlocks = 0; |
1327 | | |
1328 | | /* Decode the global flags (first symbols in the stream) */ |
1329 | 607k | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; |
1330 | | /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the |
1331 | | risk of getting loud artifacts. */ |
1332 | 607k | if (!intra_ener && st->loss_duration != 0) { |
1333 | 121k | c=0; do |
1334 | 242k | { |
1335 | 242k | celt_glog safety = 0; |
1336 | 242k | int missing = IMIN(10, st->loss_duration>>LM); |
1337 | 242k | if (LM==0) safety = GCONST(1.5f); |
1338 | 27.4k | else if (LM==1) safety = GCONST(.5f); |
1339 | 4.41M | for (i=start;i<end;i++) |
1340 | 4.16M | { |
1341 | 4.16M | if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) { |
1342 | | /* If energy is going down already, continue the trend. */ |
1343 | 1.77M | opus_val32 slope; |
1344 | 1.77M | opus_val32 E0, E1, E2; |
1345 | 1.77M | E0 = oldBandE[c*nbEBands+i]; |
1346 | 1.77M | E1 = oldLogE[c*nbEBands+i]; |
1347 | 1.77M | E2 = oldLogE2[c*nbEBands+i]; |
1348 | 1.77M | slope = MAX32(E1 - E0, HALF32(E2 - E0)); |
1349 | 1.77M | slope = MING(slope, GCONST(2.f)); |
1350 | 1.77M | E0 -= MAX32(0, (1+missing)*slope); |
1351 | 1.77M | oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0); |
1352 | 2.39M | } else { |
1353 | | /* Otherwise take the min of the last frames. */ |
1354 | 2.39M | oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]); |
1355 | 2.39M | } |
1356 | | /* Shorter frames have more natural fluctuations -- play it safe. */ |
1357 | 4.16M | oldBandE[c*nbEBands+i] -= safety; |
1358 | 4.16M | } |
1359 | 242k | } while (++c<2); |
1360 | 121k | } |
1361 | | /* Get band energies */ |
1362 | 607k | unquant_coarse_energy(mode, start, end, oldBandE, |
1363 | 607k | intra_ener, dec, C, LM); |
1364 | | |
1365 | 607k | ALLOC(tf_res, nbEBands, int); |
1366 | 607k | tf_decode(start, end, isTransient, tf_res, LM, dec); |
1367 | | |
1368 | 607k | tell = ec_tell(dec); |
1369 | 607k | spread_decision = SPREAD_NORMAL; |
1370 | 607k | if (tell+4 <= total_bits) |
1371 | 238k | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); |
1372 | | |
1373 | 607k | ALLOC(cap, nbEBands, int); |
1374 | | |
1375 | 607k | init_caps(mode,cap,LM,C); |
1376 | | |
1377 | 607k | ALLOC(offsets, nbEBands, int); |
1378 | | |
1379 | 607k | dynalloc_logp = 6; |
1380 | 607k | total_bits<<=BITRES; |
1381 | 607k | tell = ec_tell_frac(dec); |
1382 | 10.4M | for (i=start;i<end;i++) |
1383 | 9.82M | { |
1384 | 9.82M | int width, quanta; |
1385 | 9.82M | int dynalloc_loop_logp; |
1386 | 9.82M | int boost; |
1387 | 9.82M | width = C*(eBands[i+1]-eBands[i])<<LM; |
1388 | | /* quanta is 6 bits, but no more than 1 bit/sample |
1389 | | and no less than 1/8 bit/sample */ |
1390 | 9.82M | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); |
1391 | 9.82M | dynalloc_loop_logp = dynalloc_logp; |
1392 | 9.82M | boost = 0; |
1393 | 10.0M | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) |
1394 | 3.67M | { |
1395 | 3.67M | int flag; |
1396 | 3.67M | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); |
1397 | 3.67M | tell = ec_tell_frac(dec); |
1398 | 3.67M | if (!flag) |
1399 | 3.47M | break; |
1400 | 203k | boost += quanta; |
1401 | 203k | total_bits -= quanta; |
1402 | 203k | dynalloc_loop_logp = 1; |
1403 | 203k | } |
1404 | 9.82M | offsets[i] = boost; |
1405 | | /* Making dynalloc more likely */ |
1406 | 9.82M | if (boost>0) |
1407 | 59.1k | dynalloc_logp = IMAX(2, dynalloc_logp-1); |
1408 | 9.82M | } |
1409 | | |
1410 | 607k | ALLOC(fine_quant, nbEBands, int); |
1411 | 607k | alloc_trim = tell+(6<<BITRES) <= total_bits ? |
1412 | 396k | ec_dec_icdf(dec, trim_icdf, 7) : 5; |
1413 | | |
1414 | 607k | bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; |
1415 | 607k | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; |
1416 | 607k | bits -= anti_collapse_rsv; |
1417 | | |
1418 | 607k | ALLOC(pulses, nbEBands, int); |
1419 | 607k | ALLOC(fine_priority, nbEBands, int); |
1420 | | |
1421 | 607k | codedBands = clt_compute_allocation(mode, start, end, offsets, cap, |
1422 | 607k | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, |
1423 | 607k | fine_quant, fine_priority, C, LM, dec, 0, 0, 0); |
1424 | | |
1425 | 607k | unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C); |
1426 | | |
1427 | 607k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
1428 | | |
1429 | | #ifdef ENABLE_QEXT |
1430 | 305k | if (qext_bytes && end == nbEBands && |
1431 | 15.8k | ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90)) |
1432 | 15.8k | || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) { |
1433 | 15.8k | int qext_intra_ener; |
1434 | 15.8k | qext_oldBandE = backgroundLogE + 2*nbEBands; |
1435 | 15.8k | compute_qext_mode(&qext_mode_struct, mode); |
1436 | 15.8k | qext_mode = &qext_mode_struct; |
1437 | 15.8k | qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2; |
1438 | 15.8k | if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo); |
1439 | 15.8k | qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0; |
1440 | 15.8k | unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE, |
1441 | 15.8k | qext_intra_ener, &ext_dec, C, LM); |
1442 | 15.8k | } |
1443 | 305k | ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int); |
1444 | 305k | ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int); |
1445 | 305k | qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; |
1446 | | clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL, |
1447 | | qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0); |
1448 | 305k | if (qext_bytes > 0) { |
1449 | 21.1k | unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C); |
1450 | 21.1k | } |
1451 | | #endif |
1452 | | |
1453 | 910k | c=0; do { |
1454 | 910k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap); |
1455 | 910k | } while (++c<CC); |
1456 | | |
1457 | | /* Decode fixed codebook */ |
1458 | 607k | ALLOC(collapse_masks, C*nbEBands, unsigned char); |
1459 | | |
1460 | 607k | quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks, |
1461 | 607k | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, |
1462 | 607k | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0, |
1463 | 607k | st->arch, st->disable_inv |
1464 | 607k | ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses) |
1465 | 607k | ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap)); |
1466 | | |
1467 | | #ifdef ENABLE_QEXT |
1468 | 305k | if (qext_mode) { |
1469 | 15.8k | VARDECL(int, zeros); |
1470 | 15.8k | VARDECL(unsigned char, qext_collapse_masks); |
1471 | 15.8k | ec_dec dummy_dec; |
1472 | 15.8k | int ext_balance; |
1473 | 15.8k | ALLOC(zeros, nbEBands, int); |
1474 | 15.8k | ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char); |
1475 | 15.8k | ec_dec_init(&dummy_dec, NULL, 0); |
1476 | 15.8k | OPUS_CLEAR(zeros, end); |
1477 | 15.8k | ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec); |
1478 | 105k | for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES); |
1479 | 15.8k | unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C); |
1480 | 15.8k | quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks, |
1481 | 15.8k | NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros, |
1482 | 15.8k | qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0, |
1483 | 15.8k | st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL); |
1484 | 15.8k | } |
1485 | | #endif |
1486 | | |
1487 | 607k | if (anti_collapse_rsv > 0) |
1488 | 17.9k | { |
1489 | 17.9k | anti_collapse_on = ec_dec_bits(dec, 1); |
1490 | 17.9k | } |
1491 | 607k | unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE, |
1492 | 607k | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); |
1493 | 607k | if (anti_collapse_on) |
1494 | 13.4k | anti_collapse(mode, X, collapse_masks, LM, C, N, |
1495 | 13.4k | start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch); |
1496 | | |
1497 | 607k | if (silence) |
1498 | 71.2k | { |
1499 | 2.54M | for (i=0;i<C*nbEBands;i++) |
1500 | 2.47M | oldBandE[i] = -GCONST(28.f); |
1501 | 71.2k | } |
1502 | 607k | if (st->prefilter_and_fold) { |
1503 | 124k | prefilter_and_fold(st, N); |
1504 | 124k | } |
1505 | 607k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, |
1506 | 607k | C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end)); |
1507 | | |
1508 | 910k | c=0; do { |
1509 | 910k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); |
1510 | 910k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); |
1511 | 910k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, |
1512 | 910k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, |
1513 | 910k | mode->window, overlap, st->arch); |
1514 | 910k | if (LM!=0) |
1515 | 487k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize, |
1516 | 487k | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, |
1517 | 487k | mode->window, overlap, st->arch); |
1518 | | |
1519 | 910k | } while (++c<CC); |
1520 | 607k | st->postfilter_period_old = st->postfilter_period; |
1521 | 607k | st->postfilter_gain_old = st->postfilter_gain; |
1522 | 607k | st->postfilter_tapset_old = st->postfilter_tapset; |
1523 | 607k | st->postfilter_period = postfilter_pitch; |
1524 | 607k | st->postfilter_gain = postfilter_gain; |
1525 | 607k | st->postfilter_tapset = postfilter_tapset; |
1526 | 607k | if (LM!=0) |
1527 | 338k | { |
1528 | 338k | st->postfilter_period_old = st->postfilter_period; |
1529 | 338k | st->postfilter_gain_old = st->postfilter_gain; |
1530 | 338k | st->postfilter_tapset_old = st->postfilter_tapset; |
1531 | 338k | } |
1532 | | |
1533 | 607k | if (C==1) |
1534 | 389k | OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands); |
1535 | | |
1536 | 607k | if (!isTransient) |
1537 | 553k | { |
1538 | 553k | OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands); |
1539 | 553k | OPUS_COPY(oldLogE, oldBandE, 2*nbEBands); |
1540 | 553k | } else { |
1541 | 2.30M | for (i=0;i<2*nbEBands;i++) |
1542 | 2.25M | oldLogE[i] = MING(oldLogE[i], oldBandE[i]); |
1543 | 53.6k | } |
1544 | | /* In normal circumstances, we only allow the noise floor to increase by |
1545 | | up to 2.4 dB/second, but when we're in DTX we give the weight of |
1546 | | all missing packets to the update packet. */ |
1547 | 607k | max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f); |
1548 | 26.1M | for (i=0;i<2*nbEBands;i++) |
1549 | 25.5M | backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]); |
1550 | | /* In case start or end were to change */ |
1551 | 607k | c=0; do |
1552 | 1.21M | { |
1553 | 3.60M | for (i=0;i<start;i++) |
1554 | 2.38M | { |
1555 | 2.38M | oldBandE[c*nbEBands+i]=0; |
1556 | 2.38M | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); |
1557 | 2.38M | } |
1558 | 4.69M | for (i=end;i<nbEBands;i++) |
1559 | 3.47M | { |
1560 | 3.47M | oldBandE[c*nbEBands+i]=0; |
1561 | 3.47M | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); |
1562 | 3.47M | } |
1563 | 1.21M | } while (++c<2); |
1564 | 607k | st->rng = dec->rng; |
1565 | | #ifdef ENABLE_QEXT |
1566 | 305k | if (qext_bytes) st->rng = st->rng ^ ext_dec.rng; |
1567 | | #endif |
1568 | | |
1569 | 607k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); |
1570 | 607k | st->loss_duration = 0; |
1571 | 607k | st->prefilter_and_fold = 0; |
1572 | 607k | RESTORE_STACK; |
1573 | 607k | if (ec_tell(dec) > 8*len) |
1574 | 10 | return OPUS_INTERNAL_ERROR; |
1575 | | #ifdef ENABLE_QEXT |
1576 | 305k | if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes) |
1577 | 0 | return OPUS_INTERNAL_ERROR; |
1578 | 305k | #endif |
1579 | 607k | if(ec_get_error(dec)) |
1580 | 8.60k | st->error = 1; |
1581 | 607k | return frame_size/st->downsample; |
1582 | 305k | } Line | Count | Source | 1074 | 265k | { | 1075 | 265k | int c, i, N; | 1076 | 265k | int spread_decision; | 1077 | 265k | opus_int32 bits; | 1078 | 265k | ec_dec _dec; | 1079 | 265k | VARDECL(celt_norm, X); | 1080 | 265k | VARDECL(int, fine_quant); | 1081 | 265k | VARDECL(int, pulses); | 1082 | 265k | VARDECL(int, cap); | 1083 | 265k | VARDECL(int, offsets); | 1084 | 265k | VARDECL(int, fine_priority); | 1085 | 265k | VARDECL(int, tf_res); | 1086 | 265k | VARDECL(unsigned char, collapse_masks); | 1087 | 265k | celt_sig *decode_mem[2]; | 1088 | 265k | celt_sig *out_syn[2]; | 1089 | 265k | opus_val16 *lpc; | 1090 | 265k | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | 1091 | | | 1092 | 265k | int shortBlocks; | 1093 | 265k | int isTransient; | 1094 | 265k | int intra_ener; | 1095 | 265k | const int CC = st->channels; | 1096 | 265k | int LM, M; | 1097 | 265k | int start; | 1098 | 265k | int end; | 1099 | 265k | int effEnd; | 1100 | 265k | int codedBands; | 1101 | 265k | int alloc_trim; | 1102 | 265k | int postfilter_pitch; | 1103 | 265k | opus_val16 postfilter_gain; | 1104 | 265k | int intensity=0; | 1105 | 265k | int dual_stereo=0; | 1106 | 265k | opus_int32 total_bits; | 1107 | 265k | opus_int32 balance; | 1108 | 265k | opus_int32 tell; | 1109 | 265k | int dynalloc_logp; | 1110 | 265k | int postfilter_tapset; | 1111 | 265k | int anti_collapse_rsv; | 1112 | 265k | int anti_collapse_on=0; | 1113 | 265k | int silence; | 1114 | 265k | int C = st->stream_channels; | 1115 | 265k | const OpusCustomMode *mode; | 1116 | 265k | int nbEBands; | 1117 | 265k | int overlap; | 1118 | 265k | const opus_int16 *eBands; | 1119 | 265k | celt_glog max_background_increase; | 1120 | 265k | int decode_buffer_size; | 1121 | | #ifdef ENABLE_QEXT | 1122 | | opus_int32 qext_bits; | 1123 | | ec_dec ext_dec; | 1124 | | int qext_bytes=0; | 1125 | | int qext_end=0; | 1126 | | int qext_intensity=0; | 1127 | | int qext_dual_stereo=0; | 1128 | | VARDECL(int, extra_quant); | 1129 | | VARDECL(int, extra_pulses); | 1130 | | const CELTMode *qext_mode = NULL; | 1131 | | CELTMode qext_mode_struct; | 1132 | | celt_glog *qext_oldBandE=NULL; | 1133 | | int qext_scale; | 1134 | | #else | 1135 | 265k | # define qext_bytes 0 | 1136 | 265k | #endif | 1137 | 265k | ALLOC_STACK; | 1138 | | #ifdef ENABLE_QEXT | 1139 | | qext_scale = st->qext_scale; | 1140 | | #endif | 1141 | 265k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 1142 | | | 1143 | 265k | VALIDATE_CELT_DECODER(st); | 1144 | 265k | mode = st->mode; | 1145 | 265k | nbEBands = mode->nbEBands; | 1146 | 265k | overlap = mode->overlap; | 1147 | 265k | eBands = mode->eBands; | 1148 | 265k | start = st->start; | 1149 | 265k | end = st->end; | 1150 | 265k | frame_size *= st->downsample; | 1151 | | | 1152 | 265k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC); | 1153 | 265k | oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER); | 1154 | 265k | oldLogE = oldBandE + 2*nbEBands; | 1155 | 265k | oldLogE2 = oldLogE + 2*nbEBands; | 1156 | 265k | backgroundLogE = oldLogE2 + 2*nbEBands; | 1157 | | | 1158 | | #ifdef ENABLE_QEXT | 1159 | | if (qext_payload) { | 1160 | | ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len); | 1161 | | qext_bytes = qext_payload_len; | 1162 | | } else { | 1163 | | ec_dec_init(&ext_dec, NULL, 0); | 1164 | | } | 1165 | | #endif | 1166 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) | 1167 | | if (st->signalling && data!=NULL) | 1168 | | { | 1169 | | int data0=data[0]; | 1170 | | /* Convert "standard mode" to Opus header */ | 1171 | | # ifndef ENABLE_QEXT | 1172 | | if (mode->Fs==48000 && mode->shortMdctSize==120) | 1173 | | # endif | 1174 | | { | 1175 | | data0 = fromOpus(data0); | 1176 | | if (data0<0) | 1177 | | return OPUS_INVALID_PACKET; | 1178 | | } | 1179 | | st->end = end = IMAX(1, mode->effEBands-2*(data0>>5)); | 1180 | | LM = (data0>>3)&0x3; | 1181 | | C = 1 + ((data0>>2)&0x1); | 1182 | | if ((data[0] & 0x03) == 0x03) { | 1183 | | data++; | 1184 | | len--; | 1185 | | if (len<=0) | 1186 | | return OPUS_INVALID_PACKET; | 1187 | | if (data[0] & 0x40) { | 1188 | | int p; | 1189 | | int padding=0; | 1190 | | data++; | 1191 | | len--; | 1192 | | do { | 1193 | | int tmp; | 1194 | | if (len<=0) | 1195 | | return OPUS_INVALID_PACKET; | 1196 | | p = *data++; | 1197 | | len--; | 1198 | | tmp = p==255 ? 254: p; | 1199 | | len -= tmp; | 1200 | | padding += tmp; | 1201 | | } while (p==255); | 1202 | | padding--; | 1203 | | if (len <= 0 || padding<0) return OPUS_INVALID_PACKET; | 1204 | | #ifdef ENABLE_QEXT | 1205 | | qext_bytes = padding; | 1206 | | if (data[len] != QEXT_EXTENSION_ID<<1) | 1207 | | qext_bytes=0; | 1208 | | ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes); | 1209 | | #endif | 1210 | | } | 1211 | | } else | 1212 | | { | 1213 | | data++; | 1214 | | len--; | 1215 | | } | 1216 | | if (LM>mode->maxLM) | 1217 | | return OPUS_INVALID_PACKET; | 1218 | | if (frame_size < mode->shortMdctSize<<LM) | 1219 | | return OPUS_BUFFER_TOO_SMALL; | 1220 | | else | 1221 | | frame_size = mode->shortMdctSize<<LM; | 1222 | | } else { | 1223 | | #else | 1224 | 265k | { | 1225 | 265k | #endif | 1226 | 507k | for (LM=0;LM<=mode->maxLM;LM++) | 1227 | 507k | if (mode->shortMdctSize<<LM==frame_size) | 1228 | 265k | break; | 1229 | 265k | if (LM>mode->maxLM) | 1230 | 0 | return OPUS_BAD_ARG; | 1231 | 265k | } | 1232 | 265k | M=1<<LM; | 1233 | | | 1234 | 265k | if (len<0 || len>1275 || pcm==NULL) | 1235 | 0 | return OPUS_BAD_ARG; | 1236 | | | 1237 | 265k | N = M*mode->shortMdctSize; | 1238 | 417k | c=0; do { | 1239 | 417k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 1240 | 417k | out_syn[c] = decode_mem[c]+decode_buffer_size-N; | 1241 | 417k | } while (++c<CC); | 1242 | | | 1243 | 265k | effEnd = end; | 1244 | 265k | if (effEnd > mode->effEBands) | 1245 | 0 | effEnd = mode->effEBands; | 1246 | | | 1247 | 265k | if (data == NULL || len<=1) | 1248 | 114k | { | 1249 | 114k | celt_decode_lost(st, N, LM | 1250 | | #ifdef ENABLE_DEEP_PLC | 1251 | | , lpcnet | 1252 | | #endif | 1253 | 114k | ); | 1254 | 114k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | 1255 | 114k | RESTORE_STACK; | 1256 | 114k | return frame_size/st->downsample; | 1257 | 114k | } | 1258 | | #ifdef ENABLE_DEEP_PLC | 1259 | | else { | 1260 | | /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */ | 1261 | | if (lpcnet) lpcnet->blend = 0; | 1262 | | } | 1263 | | #endif | 1264 | | | 1265 | | /* Check if there are at least two packets received consecutively before | 1266 | | * turning on the pitch-based PLC */ | 1267 | 150k | if (st->loss_duration == 0) st->skip_plc = 0; | 1268 | | | 1269 | 150k | if (dec == NULL) | 1270 | 13.6k | { | 1271 | 13.6k | ec_dec_init(&_dec,(unsigned char*)data,len); | 1272 | 13.6k | dec = &_dec; | 1273 | 13.6k | } | 1274 | | | 1275 | 150k | if (C==1) | 1276 | 96.0k | { | 1277 | 2.11M | for (i=0;i<nbEBands;i++) | 1278 | 2.01M | oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]); | 1279 | 96.0k | } | 1280 | | | 1281 | 150k | total_bits = len*8; | 1282 | 150k | tell = ec_tell(dec); | 1283 | | | 1284 | 150k | if (tell >= total_bits) | 1285 | 13.0k | silence = 1; | 1286 | 137k | else if (tell==1) | 1287 | 133k | silence = ec_dec_bit_logp(dec, 15); | 1288 | 4.80k | else | 1289 | 4.80k | silence = 0; | 1290 | 150k | if (silence) | 1291 | 19.9k | { | 1292 | | /* Pretend we've read all the remaining bits */ | 1293 | 19.9k | tell = len*8; | 1294 | 19.9k | dec->nbits_total+=tell-ec_tell(dec); | 1295 | 19.9k | } | 1296 | | | 1297 | 150k | postfilter_gain = 0; | 1298 | 150k | postfilter_pitch = 0; | 1299 | 150k | postfilter_tapset = 0; | 1300 | 150k | if (start==0 && tell+16 <= total_bits) | 1301 | 78.1k | { | 1302 | 78.1k | if(ec_dec_bit_logp(dec, 1)) | 1303 | 23.2k | { | 1304 | 23.2k | int qg, octave; | 1305 | 23.2k | octave = ec_dec_uint(dec, 6); | 1306 | 23.2k | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; | 1307 | 23.2k | qg = ec_dec_bits(dec, 3); | 1308 | 23.2k | if (ec_tell(dec)+2<=total_bits) | 1309 | 23.2k | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); | 1310 | 23.2k | postfilter_gain = QCONST16(.09375f,15)*(qg+1); | 1311 | 23.2k | } | 1312 | 78.1k | tell = ec_tell(dec); | 1313 | 78.1k | } | 1314 | | | 1315 | 150k | if (LM > 0 && tell+3 <= total_bits) | 1316 | 66.8k | { | 1317 | 66.8k | isTransient = ec_dec_bit_logp(dec, 3); | 1318 | 66.8k | tell = ec_tell(dec); | 1319 | 66.8k | } | 1320 | 84.0k | else | 1321 | 84.0k | isTransient = 0; | 1322 | | | 1323 | 150k | if (isTransient) | 1324 | 14.8k | shortBlocks = M; | 1325 | 136k | else | 1326 | 136k | shortBlocks = 0; | 1327 | | | 1328 | | /* Decode the global flags (first symbols in the stream) */ | 1329 | 150k | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; | 1330 | | /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the | 1331 | | risk of getting loud artifacts. */ | 1332 | 150k | if (!intra_ener && st->loss_duration != 0) { | 1333 | 30.0k | c=0; do | 1334 | 60.1k | { | 1335 | 60.1k | celt_glog safety = 0; | 1336 | 60.1k | int missing = IMIN(10, st->loss_duration>>LM); | 1337 | 60.1k | if (LM==0) safety = GCONST(1.5f); | 1338 | 6.91k | else if (LM==1) safety = GCONST(.5f); | 1339 | 1.09M | for (i=start;i<end;i++) | 1340 | 1.03M | { | 1341 | 1.03M | if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) { | 1342 | | /* If energy is going down already, continue the trend. */ | 1343 | 438k | opus_val32 slope; | 1344 | 438k | opus_val32 E0, E1, E2; | 1345 | 438k | E0 = oldBandE[c*nbEBands+i]; | 1346 | 438k | E1 = oldLogE[c*nbEBands+i]; | 1347 | 438k | E2 = oldLogE2[c*nbEBands+i]; | 1348 | 438k | slope = MAX32(E1 - E0, HALF32(E2 - E0)); | 1349 | 438k | slope = MING(slope, GCONST(2.f)); | 1350 | 438k | E0 -= MAX32(0, (1+missing)*slope); | 1351 | 438k | oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0); | 1352 | 596k | } else { | 1353 | | /* Otherwise take the min of the last frames. */ | 1354 | 596k | oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]); | 1355 | 596k | } | 1356 | | /* Shorter frames have more natural fluctuations -- play it safe. */ | 1357 | 1.03M | oldBandE[c*nbEBands+i] -= safety; | 1358 | 1.03M | } | 1359 | 60.1k | } while (++c<2); | 1360 | 30.0k | } | 1361 | | /* Get band energies */ | 1362 | 150k | unquant_coarse_energy(mode, start, end, oldBandE, | 1363 | 150k | intra_ener, dec, C, LM); | 1364 | | | 1365 | 150k | ALLOC(tf_res, nbEBands, int); | 1366 | 150k | tf_decode(start, end, isTransient, tf_res, LM, dec); | 1367 | | | 1368 | 150k | tell = ec_tell(dec); | 1369 | 150k | spread_decision = SPREAD_NORMAL; | 1370 | 150k | if (tell+4 <= total_bits) | 1371 | 48.5k | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); | 1372 | | | 1373 | 150k | ALLOC(cap, nbEBands, int); | 1374 | | | 1375 | 150k | init_caps(mode,cap,LM,C); | 1376 | | | 1377 | 150k | ALLOC(offsets, nbEBands, int); | 1378 | | | 1379 | 150k | dynalloc_logp = 6; | 1380 | 150k | total_bits<<=BITRES; | 1381 | 150k | tell = ec_tell_frac(dec); | 1382 | 2.57M | for (i=start;i<end;i++) | 1383 | 2.42M | { | 1384 | 2.42M | int width, quanta; | 1385 | 2.42M | int dynalloc_loop_logp; | 1386 | 2.42M | int boost; | 1387 | 2.42M | width = C*(eBands[i+1]-eBands[i])<<LM; | 1388 | | /* quanta is 6 bits, but no more than 1 bit/sample | 1389 | | and no less than 1/8 bit/sample */ | 1390 | 2.42M | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); | 1391 | 2.42M | dynalloc_loop_logp = dynalloc_logp; | 1392 | 2.42M | boost = 0; | 1393 | 2.46M | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) | 1394 | 721k | { | 1395 | 721k | int flag; | 1396 | 721k | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); | 1397 | 721k | tell = ec_tell_frac(dec); | 1398 | 721k | if (!flag) | 1399 | 675k | break; | 1400 | 45.6k | boost += quanta; | 1401 | 45.6k | total_bits -= quanta; | 1402 | 45.6k | dynalloc_loop_logp = 1; | 1403 | 45.6k | } | 1404 | 2.42M | offsets[i] = boost; | 1405 | | /* Making dynalloc more likely */ | 1406 | 2.42M | if (boost>0) | 1407 | 12.0k | dynalloc_logp = IMAX(2, dynalloc_logp-1); | 1408 | 2.42M | } | 1409 | | | 1410 | 150k | ALLOC(fine_quant, nbEBands, int); | 1411 | 150k | alloc_trim = tell+(6<<BITRES) <= total_bits ? | 1412 | 108k | ec_dec_icdf(dec, trim_icdf, 7) : 5; | 1413 | | | 1414 | 150k | bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; | 1415 | 150k | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; | 1416 | 150k | bits -= anti_collapse_rsv; | 1417 | | | 1418 | 150k | ALLOC(pulses, nbEBands, int); | 1419 | 150k | ALLOC(fine_priority, nbEBands, int); | 1420 | | | 1421 | 150k | codedBands = clt_compute_allocation(mode, start, end, offsets, cap, | 1422 | 150k | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, | 1423 | 150k | fine_quant, fine_priority, C, LM, dec, 0, 0, 0); | 1424 | | | 1425 | 150k | unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C); | 1426 | | | 1427 | 150k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | 1428 | | | 1429 | | #ifdef ENABLE_QEXT | 1430 | | if (qext_bytes && end == nbEBands && | 1431 | | ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90)) | 1432 | | || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) { | 1433 | | int qext_intra_ener; | 1434 | | qext_oldBandE = backgroundLogE + 2*nbEBands; | 1435 | | compute_qext_mode(&qext_mode_struct, mode); | 1436 | | qext_mode = &qext_mode_struct; | 1437 | | qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2; | 1438 | | if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo); | 1439 | | qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0; | 1440 | | unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE, | 1441 | | qext_intra_ener, &ext_dec, C, LM); | 1442 | | } | 1443 | | ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int); | 1444 | | ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int); | 1445 | | qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; | 1446 | | clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL, | 1447 | | qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0); | 1448 | | if (qext_bytes > 0) { | 1449 | | unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C); | 1450 | | } | 1451 | | #endif | 1452 | | | 1453 | 233k | c=0; do { | 1454 | 233k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap); | 1455 | 233k | } while (++c<CC); | 1456 | | | 1457 | | /* Decode fixed codebook */ | 1458 | 150k | ALLOC(collapse_masks, C*nbEBands, unsigned char); | 1459 | | | 1460 | 150k | quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks, | 1461 | 150k | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, | 1462 | 150k | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0, | 1463 | 150k | st->arch, st->disable_inv | 1464 | 150k | ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses) | 1465 | 150k | ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap)); | 1466 | | | 1467 | | #ifdef ENABLE_QEXT | 1468 | | if (qext_mode) { | 1469 | | VARDECL(int, zeros); | 1470 | | VARDECL(unsigned char, qext_collapse_masks); | 1471 | | ec_dec dummy_dec; | 1472 | | int ext_balance; | 1473 | | ALLOC(zeros, nbEBands, int); | 1474 | | ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char); | 1475 | | ec_dec_init(&dummy_dec, NULL, 0); | 1476 | | OPUS_CLEAR(zeros, end); | 1477 | | ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec); | 1478 | | for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES); | 1479 | | unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C); | 1480 | | quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks, | 1481 | | NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros, | 1482 | | qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0, | 1483 | | st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL); | 1484 | | } | 1485 | | #endif | 1486 | | | 1487 | 150k | if (anti_collapse_rsv > 0) | 1488 | 4.39k | { | 1489 | 4.39k | anti_collapse_on = ec_dec_bits(dec, 1); | 1490 | 4.39k | } | 1491 | 150k | unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE, | 1492 | 150k | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); | 1493 | 150k | if (anti_collapse_on) | 1494 | 3.21k | anti_collapse(mode, X, collapse_masks, LM, C, N, | 1495 | 3.21k | start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch); | 1496 | | | 1497 | 150k | if (silence) | 1498 | 19.9k | { | 1499 | 675k | for (i=0;i<C*nbEBands;i++) | 1500 | 655k | oldBandE[i] = -GCONST(28.f); | 1501 | 19.9k | } | 1502 | 150k | if (st->prefilter_and_fold) { | 1503 | 31.2k | prefilter_and_fold(st, N); | 1504 | 31.2k | } | 1505 | 150k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, | 1506 | 150k | C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end)); | 1507 | | | 1508 | 233k | c=0; do { | 1509 | 233k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | 1510 | 233k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | 1511 | 233k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | 1512 | 233k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | 1513 | 233k | mode->window, overlap, st->arch); | 1514 | 233k | if (LM!=0) | 1515 | 118k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize, | 1516 | 118k | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, | 1517 | 118k | mode->window, overlap, st->arch); | 1518 | | | 1519 | 233k | } while (++c<CC); | 1520 | 150k | st->postfilter_period_old = st->postfilter_period; | 1521 | 150k | st->postfilter_gain_old = st->postfilter_gain; | 1522 | 150k | st->postfilter_tapset_old = st->postfilter_tapset; | 1523 | 150k | st->postfilter_period = postfilter_pitch; | 1524 | 150k | st->postfilter_gain = postfilter_gain; | 1525 | 150k | st->postfilter_tapset = postfilter_tapset; | 1526 | 150k | if (LM!=0) | 1527 | 82.0k | { | 1528 | 82.0k | st->postfilter_period_old = st->postfilter_period; | 1529 | 82.0k | st->postfilter_gain_old = st->postfilter_gain; | 1530 | 82.0k | st->postfilter_tapset_old = st->postfilter_tapset; | 1531 | 82.0k | } | 1532 | | | 1533 | 150k | if (C==1) | 1534 | 96.0k | OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands); | 1535 | | | 1536 | 150k | if (!isTransient) | 1537 | 136k | { | 1538 | 136k | OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands); | 1539 | 136k | OPUS_COPY(oldLogE, oldBandE, 2*nbEBands); | 1540 | 136k | } else { | 1541 | 637k | for (i=0;i<2*nbEBands;i++) | 1542 | 623k | oldLogE[i] = MING(oldLogE[i], oldBandE[i]); | 1543 | 14.8k | } | 1544 | | /* In normal circumstances, we only allow the noise floor to increase by | 1545 | | up to 2.4 dB/second, but when we're in DTX we give the weight of | 1546 | | all missing packets to the update packet. */ | 1547 | 150k | max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f); | 1548 | 6.48M | for (i=0;i<2*nbEBands;i++) | 1549 | 6.33M | backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]); | 1550 | | /* In case start or end were to change */ | 1551 | 150k | c=0; do | 1552 | 301k | { | 1553 | 908k | for (i=0;i<start;i++) | 1554 | 606k | { | 1555 | 606k | oldBandE[c*nbEBands+i]=0; | 1556 | 606k | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); | 1557 | 606k | } | 1558 | 1.18M | for (i=end;i<nbEBands;i++) | 1559 | 884k | { | 1560 | 884k | oldBandE[c*nbEBands+i]=0; | 1561 | 884k | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); | 1562 | 884k | } | 1563 | 301k | } while (++c<2); | 1564 | 150k | st->rng = dec->rng; | 1565 | | #ifdef ENABLE_QEXT | 1566 | | if (qext_bytes) st->rng = st->rng ^ ext_dec.rng; | 1567 | | #endif | 1568 | | | 1569 | 150k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | 1570 | 150k | st->loss_duration = 0; | 1571 | 150k | st->prefilter_and_fold = 0; | 1572 | 150k | RESTORE_STACK; | 1573 | 150k | if (ec_tell(dec) > 8*len) | 1574 | 4 | return OPUS_INTERNAL_ERROR; | 1575 | | #ifdef ENABLE_QEXT | 1576 | | if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes) | 1577 | | return OPUS_INTERNAL_ERROR; | 1578 | | #endif | 1579 | 150k | if(ec_get_error(dec)) | 1580 | 1.79k | st->error = 1; | 1581 | 150k | return frame_size/st->downsample; | 1582 | 150k | } |
Line | Count | Source | 1074 | 250k | { | 1075 | 250k | int c, i, N; | 1076 | 250k | int spread_decision; | 1077 | 250k | opus_int32 bits; | 1078 | 250k | ec_dec _dec; | 1079 | 250k | VARDECL(celt_norm, X); | 1080 | 250k | VARDECL(int, fine_quant); | 1081 | 250k | VARDECL(int, pulses); | 1082 | 250k | VARDECL(int, cap); | 1083 | 250k | VARDECL(int, offsets); | 1084 | 250k | VARDECL(int, fine_priority); | 1085 | 250k | VARDECL(int, tf_res); | 1086 | 250k | VARDECL(unsigned char, collapse_masks); | 1087 | 250k | celt_sig *decode_mem[2]; | 1088 | 250k | celt_sig *out_syn[2]; | 1089 | 250k | opus_val16 *lpc; | 1090 | 250k | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | 1091 | | | 1092 | 250k | int shortBlocks; | 1093 | 250k | int isTransient; | 1094 | 250k | int intra_ener; | 1095 | 250k | const int CC = st->channels; | 1096 | 250k | int LM, M; | 1097 | 250k | int start; | 1098 | 250k | int end; | 1099 | 250k | int effEnd; | 1100 | 250k | int codedBands; | 1101 | 250k | int alloc_trim; | 1102 | 250k | int postfilter_pitch; | 1103 | 250k | opus_val16 postfilter_gain; | 1104 | 250k | int intensity=0; | 1105 | 250k | int dual_stereo=0; | 1106 | 250k | opus_int32 total_bits; | 1107 | 250k | opus_int32 balance; | 1108 | 250k | opus_int32 tell; | 1109 | 250k | int dynalloc_logp; | 1110 | 250k | int postfilter_tapset; | 1111 | 250k | int anti_collapse_rsv; | 1112 | 250k | int anti_collapse_on=0; | 1113 | 250k | int silence; | 1114 | 250k | int C = st->stream_channels; | 1115 | 250k | const OpusCustomMode *mode; | 1116 | 250k | int nbEBands; | 1117 | 250k | int overlap; | 1118 | 250k | const opus_int16 *eBands; | 1119 | 250k | celt_glog max_background_increase; | 1120 | 250k | int decode_buffer_size; | 1121 | 250k | #ifdef ENABLE_QEXT | 1122 | 250k | opus_int32 qext_bits; | 1123 | 250k | ec_dec ext_dec; | 1124 | 250k | int qext_bytes=0; | 1125 | 250k | int qext_end=0; | 1126 | 250k | int qext_intensity=0; | 1127 | 250k | int qext_dual_stereo=0; | 1128 | 250k | VARDECL(int, extra_quant); | 1129 | 250k | VARDECL(int, extra_pulses); | 1130 | 250k | const CELTMode *qext_mode = NULL; | 1131 | 250k | CELTMode qext_mode_struct; | 1132 | 250k | celt_glog *qext_oldBandE=NULL; | 1133 | 250k | int qext_scale; | 1134 | | #else | 1135 | | # define qext_bytes 0 | 1136 | | #endif | 1137 | 250k | ALLOC_STACK; | 1138 | 250k | #ifdef ENABLE_QEXT | 1139 | 250k | qext_scale = st->qext_scale; | 1140 | 250k | #endif | 1141 | 250k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 1142 | | | 1143 | 250k | VALIDATE_CELT_DECODER(st); | 1144 | 250k | mode = st->mode; | 1145 | 250k | nbEBands = mode->nbEBands; | 1146 | 250k | overlap = mode->overlap; | 1147 | 250k | eBands = mode->eBands; | 1148 | 250k | start = st->start; | 1149 | 250k | end = st->end; | 1150 | 250k | frame_size *= st->downsample; | 1151 | | | 1152 | 250k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC); | 1153 | 250k | oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER); | 1154 | 250k | oldLogE = oldBandE + 2*nbEBands; | 1155 | 250k | oldLogE2 = oldLogE + 2*nbEBands; | 1156 | 250k | backgroundLogE = oldLogE2 + 2*nbEBands; | 1157 | | | 1158 | 250k | #ifdef ENABLE_QEXT | 1159 | 250k | if (qext_payload) { | 1160 | 12.1k | ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len); | 1161 | 12.1k | qext_bytes = qext_payload_len; | 1162 | 238k | } else { | 1163 | 238k | ec_dec_init(&ext_dec, NULL, 0); | 1164 | 238k | } | 1165 | 250k | #endif | 1166 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) | 1167 | | if (st->signalling && data!=NULL) | 1168 | | { | 1169 | | int data0=data[0]; | 1170 | | /* Convert "standard mode" to Opus header */ | 1171 | | # ifndef ENABLE_QEXT | 1172 | | if (mode->Fs==48000 && mode->shortMdctSize==120) | 1173 | | # endif | 1174 | | { | 1175 | | data0 = fromOpus(data0); | 1176 | | if (data0<0) | 1177 | | return OPUS_INVALID_PACKET; | 1178 | | } | 1179 | | st->end = end = IMAX(1, mode->effEBands-2*(data0>>5)); | 1180 | | LM = (data0>>3)&0x3; | 1181 | | C = 1 + ((data0>>2)&0x1); | 1182 | | if ((data[0] & 0x03) == 0x03) { | 1183 | | data++; | 1184 | | len--; | 1185 | | if (len<=0) | 1186 | | return OPUS_INVALID_PACKET; | 1187 | | if (data[0] & 0x40) { | 1188 | | int p; | 1189 | | int padding=0; | 1190 | | data++; | 1191 | | len--; | 1192 | | do { | 1193 | | int tmp; | 1194 | | if (len<=0) | 1195 | | return OPUS_INVALID_PACKET; | 1196 | | p = *data++; | 1197 | | len--; | 1198 | | tmp = p==255 ? 254: p; | 1199 | | len -= tmp; | 1200 | | padding += tmp; | 1201 | | } while (p==255); | 1202 | | padding--; | 1203 | | if (len <= 0 || padding<0) return OPUS_INVALID_PACKET; | 1204 | | #ifdef ENABLE_QEXT | 1205 | | qext_bytes = padding; | 1206 | | if (data[len] != QEXT_EXTENSION_ID<<1) | 1207 | | qext_bytes=0; | 1208 | | ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes); | 1209 | | #endif | 1210 | | } | 1211 | | } else | 1212 | | { | 1213 | | data++; | 1214 | | len--; | 1215 | | } | 1216 | | if (LM>mode->maxLM) | 1217 | | return OPUS_INVALID_PACKET; | 1218 | | if (frame_size < mode->shortMdctSize<<LM) | 1219 | | return OPUS_BUFFER_TOO_SMALL; | 1220 | | else | 1221 | | frame_size = mode->shortMdctSize<<LM; | 1222 | | } else { | 1223 | | #else | 1224 | 250k | { | 1225 | 250k | #endif | 1226 | 446k | for (LM=0;LM<=mode->maxLM;LM++) | 1227 | 446k | if (mode->shortMdctSize<<LM==frame_size) | 1228 | 250k | break; | 1229 | 250k | if (LM>mode->maxLM) | 1230 | 0 | return OPUS_BAD_ARG; | 1231 | 250k | } | 1232 | 250k | M=1<<LM; | 1233 | | | 1234 | 250k | if (len<0 || len>1275 || pcm==NULL) | 1235 | 0 | return OPUS_BAD_ARG; | 1236 | | | 1237 | 250k | N = M*mode->shortMdctSize; | 1238 | 364k | c=0; do { | 1239 | 364k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 1240 | 364k | out_syn[c] = decode_mem[c]+decode_buffer_size-N; | 1241 | 364k | } while (++c<CC); | 1242 | | | 1243 | 250k | effEnd = end; | 1244 | 250k | if (effEnd > mode->effEBands) | 1245 | 0 | effEnd = mode->effEBands; | 1246 | | | 1247 | 250k | if (data == NULL || len<=1) | 1248 | 97.8k | { | 1249 | 97.8k | celt_decode_lost(st, N, LM | 1250 | | #ifdef ENABLE_DEEP_PLC | 1251 | | , lpcnet | 1252 | | #endif | 1253 | 97.8k | ); | 1254 | 97.8k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | 1255 | 97.8k | RESTORE_STACK; | 1256 | 97.8k | return frame_size/st->downsample; | 1257 | 97.8k | } | 1258 | | #ifdef ENABLE_DEEP_PLC | 1259 | | else { | 1260 | | /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */ | 1261 | | if (lpcnet) lpcnet->blend = 0; | 1262 | | } | 1263 | | #endif | 1264 | | | 1265 | | /* Check if there are at least two packets received consecutively before | 1266 | | * turning on the pitch-based PLC */ | 1267 | 152k | if (st->loss_duration == 0) st->skip_plc = 0; | 1268 | | | 1269 | 152k | if (dec == NULL) | 1270 | 17.2k | { | 1271 | 17.2k | ec_dec_init(&_dec,(unsigned char*)data,len); | 1272 | 17.2k | dec = &_dec; | 1273 | 17.2k | } | 1274 | | | 1275 | 152k | if (C==1) | 1276 | 98.6k | { | 1277 | 2.17M | for (i=0;i<nbEBands;i++) | 1278 | 2.07M | oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]); | 1279 | 98.6k | } | 1280 | | | 1281 | 152k | total_bits = len*8; | 1282 | 152k | tell = ec_tell(dec); | 1283 | | | 1284 | 152k | if (tell >= total_bits) | 1285 | 10.6k | silence = 1; | 1286 | 142k | else if (tell==1) | 1287 | 135k | silence = ec_dec_bit_logp(dec, 15); | 1288 | 6.56k | else | 1289 | 6.56k | silence = 0; | 1290 | 152k | if (silence) | 1291 | 15.7k | { | 1292 | | /* Pretend we've read all the remaining bits */ | 1293 | 15.7k | tell = len*8; | 1294 | 15.7k | dec->nbits_total+=tell-ec_tell(dec); | 1295 | 15.7k | } | 1296 | | | 1297 | 152k | postfilter_gain = 0; | 1298 | 152k | postfilter_pitch = 0; | 1299 | 152k | postfilter_tapset = 0; | 1300 | 152k | if (start==0 && tell+16 <= total_bits) | 1301 | 104k | { | 1302 | 104k | if(ec_dec_bit_logp(dec, 1)) | 1303 | 30.2k | { | 1304 | 30.2k | int qg, octave; | 1305 | 30.2k | octave = ec_dec_uint(dec, 6); | 1306 | 30.2k | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; | 1307 | 30.2k | qg = ec_dec_bits(dec, 3); | 1308 | 30.2k | if (ec_tell(dec)+2<=total_bits) | 1309 | 30.2k | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); | 1310 | 30.2k | postfilter_gain = QCONST16(.09375f,15)*(qg+1); | 1311 | 30.2k | } | 1312 | 104k | tell = ec_tell(dec); | 1313 | 104k | } | 1314 | | | 1315 | 152k | if (LM > 0 && tell+3 <= total_bits) | 1316 | 75.2k | { | 1317 | 75.2k | isTransient = ec_dec_bit_logp(dec, 3); | 1318 | 75.2k | tell = ec_tell(dec); | 1319 | 75.2k | } | 1320 | 77.5k | else | 1321 | 77.5k | isTransient = 0; | 1322 | | | 1323 | 152k | if (isTransient) | 1324 | 12.0k | shortBlocks = M; | 1325 | 140k | else | 1326 | 140k | shortBlocks = 0; | 1327 | | | 1328 | | /* Decode the global flags (first symbols in the stream) */ | 1329 | 152k | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; | 1330 | | /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the | 1331 | | risk of getting loud artifacts. */ | 1332 | 152k | if (!intra_ener && st->loss_duration != 0) { | 1333 | 30.5k | c=0; do | 1334 | 61.0k | { | 1335 | 61.0k | celt_glog safety = 0; | 1336 | 61.0k | int missing = IMIN(10, st->loss_duration>>LM); | 1337 | 61.0k | if (LM==0) safety = GCONST(1.5f); | 1338 | 6.79k | else if (LM==1) safety = GCONST(.5f); | 1339 | 1.10M | for (i=start;i<end;i++) | 1340 | 1.04M | { | 1341 | 1.04M | if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) { | 1342 | | /* If energy is going down already, continue the trend. */ | 1343 | 449k | opus_val32 slope; | 1344 | 449k | opus_val32 E0, E1, E2; | 1345 | 449k | E0 = oldBandE[c*nbEBands+i]; | 1346 | 449k | E1 = oldLogE[c*nbEBands+i]; | 1347 | 449k | E2 = oldLogE2[c*nbEBands+i]; | 1348 | 449k | slope = MAX32(E1 - E0, HALF32(E2 - E0)); | 1349 | 449k | slope = MING(slope, GCONST(2.f)); | 1350 | 449k | E0 -= MAX32(0, (1+missing)*slope); | 1351 | 449k | oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0); | 1352 | 599k | } else { | 1353 | | /* Otherwise take the min of the last frames. */ | 1354 | 599k | oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]); | 1355 | 599k | } | 1356 | | /* Shorter frames have more natural fluctuations -- play it safe. */ | 1357 | 1.04M | oldBandE[c*nbEBands+i] -= safety; | 1358 | 1.04M | } | 1359 | 61.0k | } while (++c<2); | 1360 | 30.5k | } | 1361 | | /* Get band energies */ | 1362 | 152k | unquant_coarse_energy(mode, start, end, oldBandE, | 1363 | 152k | intra_ener, dec, C, LM); | 1364 | | | 1365 | 152k | ALLOC(tf_res, nbEBands, int); | 1366 | 152k | tf_decode(start, end, isTransient, tf_res, LM, dec); | 1367 | | | 1368 | 152k | tell = ec_tell(dec); | 1369 | 152k | spread_decision = SPREAD_NORMAL; | 1370 | 152k | if (tell+4 <= total_bits) | 1371 | 70.5k | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); | 1372 | | | 1373 | 152k | ALLOC(cap, nbEBands, int); | 1374 | | | 1375 | 152k | init_caps(mode,cap,LM,C); | 1376 | | | 1377 | 152k | ALLOC(offsets, nbEBands, int); | 1378 | | | 1379 | 152k | dynalloc_logp = 6; | 1380 | 152k | total_bits<<=BITRES; | 1381 | 152k | tell = ec_tell_frac(dec); | 1382 | 2.64M | for (i=start;i<end;i++) | 1383 | 2.48M | { | 1384 | 2.48M | int width, quanta; | 1385 | 2.48M | int dynalloc_loop_logp; | 1386 | 2.48M | int boost; | 1387 | 2.48M | width = C*(eBands[i+1]-eBands[i])<<LM; | 1388 | | /* quanta is 6 bits, but no more than 1 bit/sample | 1389 | | and no less than 1/8 bit/sample */ | 1390 | 2.48M | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); | 1391 | 2.48M | dynalloc_loop_logp = dynalloc_logp; | 1392 | 2.48M | boost = 0; | 1393 | 2.54M | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) | 1394 | 1.11M | { | 1395 | 1.11M | int flag; | 1396 | 1.11M | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); | 1397 | 1.11M | tell = ec_tell_frac(dec); | 1398 | 1.11M | if (!flag) | 1399 | 1.06M | break; | 1400 | 55.9k | boost += quanta; | 1401 | 55.9k | total_bits -= quanta; | 1402 | 55.9k | dynalloc_loop_logp = 1; | 1403 | 55.9k | } | 1404 | 2.48M | offsets[i] = boost; | 1405 | | /* Making dynalloc more likely */ | 1406 | 2.48M | if (boost>0) | 1407 | 17.5k | dynalloc_logp = IMAX(2, dynalloc_logp-1); | 1408 | 2.48M | } | 1409 | | | 1410 | 152k | ALLOC(fine_quant, nbEBands, int); | 1411 | 152k | alloc_trim = tell+(6<<BITRES) <= total_bits ? | 1412 | 89.3k | ec_dec_icdf(dec, trim_icdf, 7) : 5; | 1413 | | | 1414 | 152k | bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; | 1415 | 152k | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; | 1416 | 152k | bits -= anti_collapse_rsv; | 1417 | | | 1418 | 152k | ALLOC(pulses, nbEBands, int); | 1419 | 152k | ALLOC(fine_priority, nbEBands, int); | 1420 | | | 1421 | 152k | codedBands = clt_compute_allocation(mode, start, end, offsets, cap, | 1422 | 152k | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, | 1423 | 152k | fine_quant, fine_priority, C, LM, dec, 0, 0, 0); | 1424 | | | 1425 | 152k | unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C); | 1426 | | | 1427 | 152k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | 1428 | | | 1429 | 152k | #ifdef ENABLE_QEXT | 1430 | 152k | if (qext_bytes && end == nbEBands && | 1431 | 7.92k | ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90)) | 1432 | 7.92k | || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) { | 1433 | 7.92k | int qext_intra_ener; | 1434 | 7.92k | qext_oldBandE = backgroundLogE + 2*nbEBands; | 1435 | 7.92k | compute_qext_mode(&qext_mode_struct, mode); | 1436 | 7.92k | qext_mode = &qext_mode_struct; | 1437 | 7.92k | qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2; | 1438 | 7.92k | if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo); | 1439 | 7.92k | qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0; | 1440 | 7.92k | unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE, | 1441 | 7.92k | qext_intra_ener, &ext_dec, C, LM); | 1442 | 7.92k | } | 1443 | 152k | ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int); | 1444 | 152k | ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int); | 1445 | 152k | qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; | 1446 | 152k | clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL, | 1447 | 152k | qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0); | 1448 | 152k | if (qext_bytes > 0) { | 1449 | 10.5k | unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C); | 1450 | 10.5k | } | 1451 | 152k | #endif | 1452 | | | 1453 | 221k | c=0; do { | 1454 | 221k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap); | 1455 | 221k | } while (++c<CC); | 1456 | | | 1457 | | /* Decode fixed codebook */ | 1458 | 152k | ALLOC(collapse_masks, C*nbEBands, unsigned char); | 1459 | | | 1460 | 152k | quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks, | 1461 | 152k | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, | 1462 | 152k | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0, | 1463 | 152k | st->arch, st->disable_inv | 1464 | 152k | ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses) | 1465 | 152k | ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap)); | 1466 | | | 1467 | 152k | #ifdef ENABLE_QEXT | 1468 | 152k | if (qext_mode) { | 1469 | 7.92k | VARDECL(int, zeros); | 1470 | 7.92k | VARDECL(unsigned char, qext_collapse_masks); | 1471 | 7.92k | ec_dec dummy_dec; | 1472 | 7.92k | int ext_balance; | 1473 | 7.92k | ALLOC(zeros, nbEBands, int); | 1474 | 7.92k | ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char); | 1475 | 7.92k | ec_dec_init(&dummy_dec, NULL, 0); | 1476 | 7.92k | OPUS_CLEAR(zeros, end); | 1477 | 7.92k | ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec); | 1478 | 52.9k | for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES); | 1479 | 7.92k | unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C); | 1480 | 7.92k | quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks, | 1481 | 7.92k | NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros, | 1482 | 7.92k | qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0, | 1483 | 7.92k | st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL); | 1484 | 7.92k | } | 1485 | 152k | #endif | 1486 | | | 1487 | 152k | if (anti_collapse_rsv > 0) | 1488 | 4.57k | { | 1489 | 4.57k | anti_collapse_on = ec_dec_bits(dec, 1); | 1490 | 4.57k | } | 1491 | 152k | unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE, | 1492 | 152k | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); | 1493 | 152k | if (anti_collapse_on) | 1494 | 3.50k | anti_collapse(mode, X, collapse_masks, LM, C, N, | 1495 | 3.50k | start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch); | 1496 | | | 1497 | 152k | if (silence) | 1498 | 15.7k | { | 1499 | 597k | for (i=0;i<C*nbEBands;i++) | 1500 | 581k | oldBandE[i] = -GCONST(28.f); | 1501 | 15.7k | } | 1502 | 152k | if (st->prefilter_and_fold) { | 1503 | 30.7k | prefilter_and_fold(st, N); | 1504 | 30.7k | } | 1505 | 152k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, | 1506 | 152k | C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end)); | 1507 | | | 1508 | 221k | c=0; do { | 1509 | 221k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | 1510 | 221k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | 1511 | 221k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | 1512 | 221k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | 1513 | 221k | mode->window, overlap, st->arch); | 1514 | 221k | if (LM!=0) | 1515 | 125k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize, | 1516 | 125k | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, | 1517 | 125k | mode->window, overlap, st->arch); | 1518 | | | 1519 | 221k | } while (++c<CC); | 1520 | 152k | st->postfilter_period_old = st->postfilter_period; | 1521 | 152k | st->postfilter_gain_old = st->postfilter_gain; | 1522 | 152k | st->postfilter_tapset_old = st->postfilter_tapset; | 1523 | 152k | st->postfilter_period = postfilter_pitch; | 1524 | 152k | st->postfilter_gain = postfilter_gain; | 1525 | 152k | st->postfilter_tapset = postfilter_tapset; | 1526 | 152k | if (LM!=0) | 1527 | 87.2k | { | 1528 | 87.2k | st->postfilter_period_old = st->postfilter_period; | 1529 | 87.2k | st->postfilter_gain_old = st->postfilter_gain; | 1530 | 87.2k | st->postfilter_tapset_old = st->postfilter_tapset; | 1531 | 87.2k | } | 1532 | | | 1533 | 152k | if (C==1) | 1534 | 98.6k | OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands); | 1535 | | | 1536 | 152k | if (!isTransient) | 1537 | 140k | { | 1538 | 140k | OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands); | 1539 | 140k | OPUS_COPY(oldLogE, oldBandE, 2*nbEBands); | 1540 | 140k | } else { | 1541 | 516k | for (i=0;i<2*nbEBands;i++) | 1542 | 504k | oldLogE[i] = MING(oldLogE[i], oldBandE[i]); | 1543 | 12.0k | } | 1544 | | /* In normal circumstances, we only allow the noise floor to increase by | 1545 | | up to 2.4 dB/second, but when we're in DTX we give the weight of | 1546 | | all missing packets to the update packet. */ | 1547 | 152k | max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f); | 1548 | 6.56M | for (i=0;i<2*nbEBands;i++) | 1549 | 6.41M | backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]); | 1550 | | /* In case start or end were to change */ | 1551 | 152k | c=0; do | 1552 | 305k | { | 1553 | 892k | for (i=0;i<start;i++) | 1554 | 586k | { | 1555 | 586k | oldBandE[c*nbEBands+i]=0; | 1556 | 586k | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); | 1557 | 586k | } | 1558 | 1.15M | for (i=end;i<nbEBands;i++) | 1559 | 853k | { | 1560 | 853k | oldBandE[c*nbEBands+i]=0; | 1561 | 853k | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); | 1562 | 853k | } | 1563 | 305k | } while (++c<2); | 1564 | 152k | st->rng = dec->rng; | 1565 | 152k | #ifdef ENABLE_QEXT | 1566 | 152k | if (qext_bytes) st->rng = st->rng ^ ext_dec.rng; | 1567 | 152k | #endif | 1568 | | | 1569 | 152k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | 1570 | 152k | st->loss_duration = 0; | 1571 | 152k | st->prefilter_and_fold = 0; | 1572 | 152k | RESTORE_STACK; | 1573 | 152k | if (ec_tell(dec) > 8*len) | 1574 | 1 | return OPUS_INTERNAL_ERROR; | 1575 | 152k | #ifdef ENABLE_QEXT | 1576 | 152k | if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes) | 1577 | 0 | return OPUS_INTERNAL_ERROR; | 1578 | 152k | #endif | 1579 | 152k | if(ec_get_error(dec)) | 1580 | 2.50k | st->error = 1; | 1581 | 152k | return frame_size/st->downsample; | 1582 | 152k | } |
Line | Count | Source | 1074 | 250k | { | 1075 | 250k | int c, i, N; | 1076 | 250k | int spread_decision; | 1077 | 250k | opus_int32 bits; | 1078 | 250k | ec_dec _dec; | 1079 | 250k | VARDECL(celt_norm, X); | 1080 | 250k | VARDECL(int, fine_quant); | 1081 | 250k | VARDECL(int, pulses); | 1082 | 250k | VARDECL(int, cap); | 1083 | 250k | VARDECL(int, offsets); | 1084 | 250k | VARDECL(int, fine_priority); | 1085 | 250k | VARDECL(int, tf_res); | 1086 | 250k | VARDECL(unsigned char, collapse_masks); | 1087 | 250k | celt_sig *decode_mem[2]; | 1088 | 250k | celt_sig *out_syn[2]; | 1089 | 250k | opus_val16 *lpc; | 1090 | 250k | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | 1091 | | | 1092 | 250k | int shortBlocks; | 1093 | 250k | int isTransient; | 1094 | 250k | int intra_ener; | 1095 | 250k | const int CC = st->channels; | 1096 | 250k | int LM, M; | 1097 | 250k | int start; | 1098 | 250k | int end; | 1099 | 250k | int effEnd; | 1100 | 250k | int codedBands; | 1101 | 250k | int alloc_trim; | 1102 | 250k | int postfilter_pitch; | 1103 | 250k | opus_val16 postfilter_gain; | 1104 | 250k | int intensity=0; | 1105 | 250k | int dual_stereo=0; | 1106 | 250k | opus_int32 total_bits; | 1107 | 250k | opus_int32 balance; | 1108 | 250k | opus_int32 tell; | 1109 | 250k | int dynalloc_logp; | 1110 | 250k | int postfilter_tapset; | 1111 | 250k | int anti_collapse_rsv; | 1112 | 250k | int anti_collapse_on=0; | 1113 | 250k | int silence; | 1114 | 250k | int C = st->stream_channels; | 1115 | 250k | const OpusCustomMode *mode; | 1116 | 250k | int nbEBands; | 1117 | 250k | int overlap; | 1118 | 250k | const opus_int16 *eBands; | 1119 | 250k | celt_glog max_background_increase; | 1120 | 250k | int decode_buffer_size; | 1121 | 250k | #ifdef ENABLE_QEXT | 1122 | 250k | opus_int32 qext_bits; | 1123 | 250k | ec_dec ext_dec; | 1124 | 250k | int qext_bytes=0; | 1125 | 250k | int qext_end=0; | 1126 | 250k | int qext_intensity=0; | 1127 | 250k | int qext_dual_stereo=0; | 1128 | 250k | VARDECL(int, extra_quant); | 1129 | 250k | VARDECL(int, extra_pulses); | 1130 | 250k | const CELTMode *qext_mode = NULL; | 1131 | 250k | CELTMode qext_mode_struct; | 1132 | 250k | celt_glog *qext_oldBandE=NULL; | 1133 | 250k | int qext_scale; | 1134 | | #else | 1135 | | # define qext_bytes 0 | 1136 | | #endif | 1137 | 250k | ALLOC_STACK; | 1138 | 250k | #ifdef ENABLE_QEXT | 1139 | 250k | qext_scale = st->qext_scale; | 1140 | 250k | #endif | 1141 | 250k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 1142 | | | 1143 | 250k | VALIDATE_CELT_DECODER(st); | 1144 | 250k | mode = st->mode; | 1145 | 250k | nbEBands = mode->nbEBands; | 1146 | 250k | overlap = mode->overlap; | 1147 | 250k | eBands = mode->eBands; | 1148 | 250k | start = st->start; | 1149 | 250k | end = st->end; | 1150 | 250k | frame_size *= st->downsample; | 1151 | | | 1152 | 250k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC); | 1153 | 250k | oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER); | 1154 | 250k | oldLogE = oldBandE + 2*nbEBands; | 1155 | 250k | oldLogE2 = oldLogE + 2*nbEBands; | 1156 | 250k | backgroundLogE = oldLogE2 + 2*nbEBands; | 1157 | | | 1158 | 250k | #ifdef ENABLE_QEXT | 1159 | 250k | if (qext_payload) { | 1160 | 12.1k | ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len); | 1161 | 12.1k | qext_bytes = qext_payload_len; | 1162 | 238k | } else { | 1163 | 238k | ec_dec_init(&ext_dec, NULL, 0); | 1164 | 238k | } | 1165 | 250k | #endif | 1166 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) | 1167 | | if (st->signalling && data!=NULL) | 1168 | | { | 1169 | | int data0=data[0]; | 1170 | | /* Convert "standard mode" to Opus header */ | 1171 | | # ifndef ENABLE_QEXT | 1172 | | if (mode->Fs==48000 && mode->shortMdctSize==120) | 1173 | | # endif | 1174 | | { | 1175 | | data0 = fromOpus(data0); | 1176 | | if (data0<0) | 1177 | | return OPUS_INVALID_PACKET; | 1178 | | } | 1179 | | st->end = end = IMAX(1, mode->effEBands-2*(data0>>5)); | 1180 | | LM = (data0>>3)&0x3; | 1181 | | C = 1 + ((data0>>2)&0x1); | 1182 | | if ((data[0] & 0x03) == 0x03) { | 1183 | | data++; | 1184 | | len--; | 1185 | | if (len<=0) | 1186 | | return OPUS_INVALID_PACKET; | 1187 | | if (data[0] & 0x40) { | 1188 | | int p; | 1189 | | int padding=0; | 1190 | | data++; | 1191 | | len--; | 1192 | | do { | 1193 | | int tmp; | 1194 | | if (len<=0) | 1195 | | return OPUS_INVALID_PACKET; | 1196 | | p = *data++; | 1197 | | len--; | 1198 | | tmp = p==255 ? 254: p; | 1199 | | len -= tmp; | 1200 | | padding += tmp; | 1201 | | } while (p==255); | 1202 | | padding--; | 1203 | | if (len <= 0 || padding<0) return OPUS_INVALID_PACKET; | 1204 | | #ifdef ENABLE_QEXT | 1205 | | qext_bytes = padding; | 1206 | | if (data[len] != QEXT_EXTENSION_ID<<1) | 1207 | | qext_bytes=0; | 1208 | | ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes); | 1209 | | #endif | 1210 | | } | 1211 | | } else | 1212 | | { | 1213 | | data++; | 1214 | | len--; | 1215 | | } | 1216 | | if (LM>mode->maxLM) | 1217 | | return OPUS_INVALID_PACKET; | 1218 | | if (frame_size < mode->shortMdctSize<<LM) | 1219 | | return OPUS_BUFFER_TOO_SMALL; | 1220 | | else | 1221 | | frame_size = mode->shortMdctSize<<LM; | 1222 | | } else { | 1223 | | #else | 1224 | 250k | { | 1225 | 250k | #endif | 1226 | 446k | for (LM=0;LM<=mode->maxLM;LM++) | 1227 | 446k | if (mode->shortMdctSize<<LM==frame_size) | 1228 | 250k | break; | 1229 | 250k | if (LM>mode->maxLM) | 1230 | 0 | return OPUS_BAD_ARG; | 1231 | 250k | } | 1232 | 250k | M=1<<LM; | 1233 | | | 1234 | 250k | if (len<0 || len>1275 || pcm==NULL) | 1235 | 0 | return OPUS_BAD_ARG; | 1236 | | | 1237 | 250k | N = M*mode->shortMdctSize; | 1238 | 364k | c=0; do { | 1239 | 364k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 1240 | 364k | out_syn[c] = decode_mem[c]+decode_buffer_size-N; | 1241 | 364k | } while (++c<CC); | 1242 | | | 1243 | 250k | effEnd = end; | 1244 | 250k | if (effEnd > mode->effEBands) | 1245 | 0 | effEnd = mode->effEBands; | 1246 | | | 1247 | 250k | if (data == NULL || len<=1) | 1248 | 97.8k | { | 1249 | 97.8k | celt_decode_lost(st, N, LM | 1250 | | #ifdef ENABLE_DEEP_PLC | 1251 | | , lpcnet | 1252 | | #endif | 1253 | 97.8k | ); | 1254 | 97.8k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | 1255 | 97.8k | RESTORE_STACK; | 1256 | 97.8k | return frame_size/st->downsample; | 1257 | 97.8k | } | 1258 | | #ifdef ENABLE_DEEP_PLC | 1259 | | else { | 1260 | | /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */ | 1261 | | if (lpcnet) lpcnet->blend = 0; | 1262 | | } | 1263 | | #endif | 1264 | | | 1265 | | /* Check if there are at least two packets received consecutively before | 1266 | | * turning on the pitch-based PLC */ | 1267 | 152k | if (st->loss_duration == 0) st->skip_plc = 0; | 1268 | | | 1269 | 152k | if (dec == NULL) | 1270 | 17.2k | { | 1271 | 17.2k | ec_dec_init(&_dec,(unsigned char*)data,len); | 1272 | 17.2k | dec = &_dec; | 1273 | 17.2k | } | 1274 | | | 1275 | 152k | if (C==1) | 1276 | 98.6k | { | 1277 | 2.17M | for (i=0;i<nbEBands;i++) | 1278 | 2.07M | oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]); | 1279 | 98.6k | } | 1280 | | | 1281 | 152k | total_bits = len*8; | 1282 | 152k | tell = ec_tell(dec); | 1283 | | | 1284 | 152k | if (tell >= total_bits) | 1285 | 10.6k | silence = 1; | 1286 | 142k | else if (tell==1) | 1287 | 135k | silence = ec_dec_bit_logp(dec, 15); | 1288 | 6.56k | else | 1289 | 6.56k | silence = 0; | 1290 | 152k | if (silence) | 1291 | 15.7k | { | 1292 | | /* Pretend we've read all the remaining bits */ | 1293 | 15.7k | tell = len*8; | 1294 | 15.7k | dec->nbits_total+=tell-ec_tell(dec); | 1295 | 15.7k | } | 1296 | | | 1297 | 152k | postfilter_gain = 0; | 1298 | 152k | postfilter_pitch = 0; | 1299 | 152k | postfilter_tapset = 0; | 1300 | 152k | if (start==0 && tell+16 <= total_bits) | 1301 | 104k | { | 1302 | 104k | if(ec_dec_bit_logp(dec, 1)) | 1303 | 30.2k | { | 1304 | 30.2k | int qg, octave; | 1305 | 30.2k | octave = ec_dec_uint(dec, 6); | 1306 | 30.2k | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; | 1307 | 30.2k | qg = ec_dec_bits(dec, 3); | 1308 | 30.2k | if (ec_tell(dec)+2<=total_bits) | 1309 | 30.2k | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); | 1310 | 30.2k | postfilter_gain = QCONST16(.09375f,15)*(qg+1); | 1311 | 30.2k | } | 1312 | 104k | tell = ec_tell(dec); | 1313 | 104k | } | 1314 | | | 1315 | 152k | if (LM > 0 && tell+3 <= total_bits) | 1316 | 75.2k | { | 1317 | 75.2k | isTransient = ec_dec_bit_logp(dec, 3); | 1318 | 75.2k | tell = ec_tell(dec); | 1319 | 75.2k | } | 1320 | 77.5k | else | 1321 | 77.5k | isTransient = 0; | 1322 | | | 1323 | 152k | if (isTransient) | 1324 | 12.0k | shortBlocks = M; | 1325 | 140k | else | 1326 | 140k | shortBlocks = 0; | 1327 | | | 1328 | | /* Decode the global flags (first symbols in the stream) */ | 1329 | 152k | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; | 1330 | | /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the | 1331 | | risk of getting loud artifacts. */ | 1332 | 152k | if (!intra_ener && st->loss_duration != 0) { | 1333 | 30.5k | c=0; do | 1334 | 61.0k | { | 1335 | 61.0k | celt_glog safety = 0; | 1336 | 61.0k | int missing = IMIN(10, st->loss_duration>>LM); | 1337 | 61.0k | if (LM==0) safety = GCONST(1.5f); | 1338 | 6.79k | else if (LM==1) safety = GCONST(.5f); | 1339 | 1.10M | for (i=start;i<end;i++) | 1340 | 1.04M | { | 1341 | 1.04M | if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) { | 1342 | | /* If energy is going down already, continue the trend. */ | 1343 | 449k | opus_val32 slope; | 1344 | 449k | opus_val32 E0, E1, E2; | 1345 | 449k | E0 = oldBandE[c*nbEBands+i]; | 1346 | 449k | E1 = oldLogE[c*nbEBands+i]; | 1347 | 449k | E2 = oldLogE2[c*nbEBands+i]; | 1348 | 449k | slope = MAX32(E1 - E0, HALF32(E2 - E0)); | 1349 | 449k | slope = MING(slope, GCONST(2.f)); | 1350 | 449k | E0 -= MAX32(0, (1+missing)*slope); | 1351 | 449k | oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0); | 1352 | 599k | } else { | 1353 | | /* Otherwise take the min of the last frames. */ | 1354 | 599k | oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]); | 1355 | 599k | } | 1356 | | /* Shorter frames have more natural fluctuations -- play it safe. */ | 1357 | 1.04M | oldBandE[c*nbEBands+i] -= safety; | 1358 | 1.04M | } | 1359 | 61.0k | } while (++c<2); | 1360 | 30.5k | } | 1361 | | /* Get band energies */ | 1362 | 152k | unquant_coarse_energy(mode, start, end, oldBandE, | 1363 | 152k | intra_ener, dec, C, LM); | 1364 | | | 1365 | 152k | ALLOC(tf_res, nbEBands, int); | 1366 | 152k | tf_decode(start, end, isTransient, tf_res, LM, dec); | 1367 | | | 1368 | 152k | tell = ec_tell(dec); | 1369 | 152k | spread_decision = SPREAD_NORMAL; | 1370 | 152k | if (tell+4 <= total_bits) | 1371 | 70.5k | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); | 1372 | | | 1373 | 152k | ALLOC(cap, nbEBands, int); | 1374 | | | 1375 | 152k | init_caps(mode,cap,LM,C); | 1376 | | | 1377 | 152k | ALLOC(offsets, nbEBands, int); | 1378 | | | 1379 | 152k | dynalloc_logp = 6; | 1380 | 152k | total_bits<<=BITRES; | 1381 | 152k | tell = ec_tell_frac(dec); | 1382 | 2.64M | for (i=start;i<end;i++) | 1383 | 2.48M | { | 1384 | 2.48M | int width, quanta; | 1385 | 2.48M | int dynalloc_loop_logp; | 1386 | 2.48M | int boost; | 1387 | 2.48M | width = C*(eBands[i+1]-eBands[i])<<LM; | 1388 | | /* quanta is 6 bits, but no more than 1 bit/sample | 1389 | | and no less than 1/8 bit/sample */ | 1390 | 2.48M | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); | 1391 | 2.48M | dynalloc_loop_logp = dynalloc_logp; | 1392 | 2.48M | boost = 0; | 1393 | 2.54M | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) | 1394 | 1.11M | { | 1395 | 1.11M | int flag; | 1396 | 1.11M | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); | 1397 | 1.11M | tell = ec_tell_frac(dec); | 1398 | 1.11M | if (!flag) | 1399 | 1.06M | break; | 1400 | 55.9k | boost += quanta; | 1401 | 55.9k | total_bits -= quanta; | 1402 | 55.9k | dynalloc_loop_logp = 1; | 1403 | 55.9k | } | 1404 | 2.48M | offsets[i] = boost; | 1405 | | /* Making dynalloc more likely */ | 1406 | 2.48M | if (boost>0) | 1407 | 17.5k | dynalloc_logp = IMAX(2, dynalloc_logp-1); | 1408 | 2.48M | } | 1409 | | | 1410 | 152k | ALLOC(fine_quant, nbEBands, int); | 1411 | 152k | alloc_trim = tell+(6<<BITRES) <= total_bits ? | 1412 | 89.3k | ec_dec_icdf(dec, trim_icdf, 7) : 5; | 1413 | | | 1414 | 152k | bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; | 1415 | 152k | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; | 1416 | 152k | bits -= anti_collapse_rsv; | 1417 | | | 1418 | 152k | ALLOC(pulses, nbEBands, int); | 1419 | 152k | ALLOC(fine_priority, nbEBands, int); | 1420 | | | 1421 | 152k | codedBands = clt_compute_allocation(mode, start, end, offsets, cap, | 1422 | 152k | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, | 1423 | 152k | fine_quant, fine_priority, C, LM, dec, 0, 0, 0); | 1424 | | | 1425 | 152k | unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C); | 1426 | | | 1427 | 152k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | 1428 | | | 1429 | 152k | #ifdef ENABLE_QEXT | 1430 | 152k | if (qext_bytes && end == nbEBands && | 1431 | 7.92k | ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90)) | 1432 | 7.92k | || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) { | 1433 | 7.92k | int qext_intra_ener; | 1434 | 7.92k | qext_oldBandE = backgroundLogE + 2*nbEBands; | 1435 | 7.92k | compute_qext_mode(&qext_mode_struct, mode); | 1436 | 7.92k | qext_mode = &qext_mode_struct; | 1437 | 7.92k | qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2; | 1438 | 7.92k | if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo); | 1439 | 7.92k | qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0; | 1440 | 7.92k | unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE, | 1441 | 7.92k | qext_intra_ener, &ext_dec, C, LM); | 1442 | 7.92k | } | 1443 | 152k | ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int); | 1444 | 152k | ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int); | 1445 | 152k | qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; | 1446 | 152k | clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL, | 1447 | 152k | qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0); | 1448 | 152k | if (qext_bytes > 0) { | 1449 | 10.5k | unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C); | 1450 | 10.5k | } | 1451 | 152k | #endif | 1452 | | | 1453 | 221k | c=0; do { | 1454 | 221k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap); | 1455 | 221k | } while (++c<CC); | 1456 | | | 1457 | | /* Decode fixed codebook */ | 1458 | 152k | ALLOC(collapse_masks, C*nbEBands, unsigned char); | 1459 | | | 1460 | 152k | quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks, | 1461 | 152k | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, | 1462 | 152k | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0, | 1463 | 152k | st->arch, st->disable_inv | 1464 | 152k | ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses) | 1465 | 152k | ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap)); | 1466 | | | 1467 | 152k | #ifdef ENABLE_QEXT | 1468 | 152k | if (qext_mode) { | 1469 | 7.92k | VARDECL(int, zeros); | 1470 | 7.92k | VARDECL(unsigned char, qext_collapse_masks); | 1471 | 7.92k | ec_dec dummy_dec; | 1472 | 7.92k | int ext_balance; | 1473 | 7.92k | ALLOC(zeros, nbEBands, int); | 1474 | 7.92k | ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char); | 1475 | 7.92k | ec_dec_init(&dummy_dec, NULL, 0); | 1476 | 7.92k | OPUS_CLEAR(zeros, end); | 1477 | 7.92k | ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec); | 1478 | 52.9k | for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES); | 1479 | 7.92k | unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C); | 1480 | 7.92k | quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks, | 1481 | 7.92k | NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros, | 1482 | 7.92k | qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0, | 1483 | 7.92k | st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL); | 1484 | 7.92k | } | 1485 | 152k | #endif | 1486 | | | 1487 | 152k | if (anti_collapse_rsv > 0) | 1488 | 4.57k | { | 1489 | 4.57k | anti_collapse_on = ec_dec_bits(dec, 1); | 1490 | 4.57k | } | 1491 | 152k | unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE, | 1492 | 152k | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); | 1493 | 152k | if (anti_collapse_on) | 1494 | 3.50k | anti_collapse(mode, X, collapse_masks, LM, C, N, | 1495 | 3.50k | start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch); | 1496 | | | 1497 | 152k | if (silence) | 1498 | 15.7k | { | 1499 | 597k | for (i=0;i<C*nbEBands;i++) | 1500 | 581k | oldBandE[i] = -GCONST(28.f); | 1501 | 15.7k | } | 1502 | 152k | if (st->prefilter_and_fold) { | 1503 | 30.7k | prefilter_and_fold(st, N); | 1504 | 30.7k | } | 1505 | 152k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, | 1506 | 152k | C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end)); | 1507 | | | 1508 | 221k | c=0; do { | 1509 | 221k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | 1510 | 221k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | 1511 | 221k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | 1512 | 221k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | 1513 | 221k | mode->window, overlap, st->arch); | 1514 | 221k | if (LM!=0) | 1515 | 125k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize, | 1516 | 125k | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, | 1517 | 125k | mode->window, overlap, st->arch); | 1518 | | | 1519 | 221k | } while (++c<CC); | 1520 | 152k | st->postfilter_period_old = st->postfilter_period; | 1521 | 152k | st->postfilter_gain_old = st->postfilter_gain; | 1522 | 152k | st->postfilter_tapset_old = st->postfilter_tapset; | 1523 | 152k | st->postfilter_period = postfilter_pitch; | 1524 | 152k | st->postfilter_gain = postfilter_gain; | 1525 | 152k | st->postfilter_tapset = postfilter_tapset; | 1526 | 152k | if (LM!=0) | 1527 | 87.2k | { | 1528 | 87.2k | st->postfilter_period_old = st->postfilter_period; | 1529 | 87.2k | st->postfilter_gain_old = st->postfilter_gain; | 1530 | 87.2k | st->postfilter_tapset_old = st->postfilter_tapset; | 1531 | 87.2k | } | 1532 | | | 1533 | 152k | if (C==1) | 1534 | 98.6k | OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands); | 1535 | | | 1536 | 152k | if (!isTransient) | 1537 | 140k | { | 1538 | 140k | OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands); | 1539 | 140k | OPUS_COPY(oldLogE, oldBandE, 2*nbEBands); | 1540 | 140k | } else { | 1541 | 516k | for (i=0;i<2*nbEBands;i++) | 1542 | 504k | oldLogE[i] = MING(oldLogE[i], oldBandE[i]); | 1543 | 12.0k | } | 1544 | | /* In normal circumstances, we only allow the noise floor to increase by | 1545 | | up to 2.4 dB/second, but when we're in DTX we give the weight of | 1546 | | all missing packets to the update packet. */ | 1547 | 152k | max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f); | 1548 | 6.56M | for (i=0;i<2*nbEBands;i++) | 1549 | 6.41M | backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]); | 1550 | | /* In case start or end were to change */ | 1551 | 152k | c=0; do | 1552 | 305k | { | 1553 | 892k | for (i=0;i<start;i++) | 1554 | 586k | { | 1555 | 586k | oldBandE[c*nbEBands+i]=0; | 1556 | 586k | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); | 1557 | 586k | } | 1558 | 1.15M | for (i=end;i<nbEBands;i++) | 1559 | 853k | { | 1560 | 853k | oldBandE[c*nbEBands+i]=0; | 1561 | 853k | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); | 1562 | 853k | } | 1563 | 305k | } while (++c<2); | 1564 | 152k | st->rng = dec->rng; | 1565 | 152k | #ifdef ENABLE_QEXT | 1566 | 152k | if (qext_bytes) st->rng = st->rng ^ ext_dec.rng; | 1567 | 152k | #endif | 1568 | | | 1569 | 152k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | 1570 | 152k | st->loss_duration = 0; | 1571 | 152k | st->prefilter_and_fold = 0; | 1572 | 152k | RESTORE_STACK; | 1573 | 152k | if (ec_tell(dec) > 8*len) | 1574 | 1 | return OPUS_INTERNAL_ERROR; | 1575 | 152k | #ifdef ENABLE_QEXT | 1576 | 152k | if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes) | 1577 | 0 | return OPUS_INTERNAL_ERROR; | 1578 | 152k | #endif | 1579 | 152k | if(ec_get_error(dec)) | 1580 | 2.50k | st->error = 1; | 1581 | 152k | return frame_size/st->downsample; | 1582 | 152k | } |
Line | Count | Source | 1074 | 265k | { | 1075 | 265k | int c, i, N; | 1076 | 265k | int spread_decision; | 1077 | 265k | opus_int32 bits; | 1078 | 265k | ec_dec _dec; | 1079 | 265k | VARDECL(celt_norm, X); | 1080 | 265k | VARDECL(int, fine_quant); | 1081 | 265k | VARDECL(int, pulses); | 1082 | 265k | VARDECL(int, cap); | 1083 | 265k | VARDECL(int, offsets); | 1084 | 265k | VARDECL(int, fine_priority); | 1085 | 265k | VARDECL(int, tf_res); | 1086 | 265k | VARDECL(unsigned char, collapse_masks); | 1087 | 265k | celt_sig *decode_mem[2]; | 1088 | 265k | celt_sig *out_syn[2]; | 1089 | 265k | opus_val16 *lpc; | 1090 | 265k | celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | 1091 | | | 1092 | 265k | int shortBlocks; | 1093 | 265k | int isTransient; | 1094 | 265k | int intra_ener; | 1095 | 265k | const int CC = st->channels; | 1096 | 265k | int LM, M; | 1097 | 265k | int start; | 1098 | 265k | int end; | 1099 | 265k | int effEnd; | 1100 | 265k | int codedBands; | 1101 | 265k | int alloc_trim; | 1102 | 265k | int postfilter_pitch; | 1103 | 265k | opus_val16 postfilter_gain; | 1104 | 265k | int intensity=0; | 1105 | 265k | int dual_stereo=0; | 1106 | 265k | opus_int32 total_bits; | 1107 | 265k | opus_int32 balance; | 1108 | 265k | opus_int32 tell; | 1109 | 265k | int dynalloc_logp; | 1110 | 265k | int postfilter_tapset; | 1111 | 265k | int anti_collapse_rsv; | 1112 | 265k | int anti_collapse_on=0; | 1113 | 265k | int silence; | 1114 | 265k | int C = st->stream_channels; | 1115 | 265k | const OpusCustomMode *mode; | 1116 | 265k | int nbEBands; | 1117 | 265k | int overlap; | 1118 | 265k | const opus_int16 *eBands; | 1119 | 265k | celt_glog max_background_increase; | 1120 | 265k | int decode_buffer_size; | 1121 | | #ifdef ENABLE_QEXT | 1122 | | opus_int32 qext_bits; | 1123 | | ec_dec ext_dec; | 1124 | | int qext_bytes=0; | 1125 | | int qext_end=0; | 1126 | | int qext_intensity=0; | 1127 | | int qext_dual_stereo=0; | 1128 | | VARDECL(int, extra_quant); | 1129 | | VARDECL(int, extra_pulses); | 1130 | | const CELTMode *qext_mode = NULL; | 1131 | | CELTMode qext_mode_struct; | 1132 | | celt_glog *qext_oldBandE=NULL; | 1133 | | int qext_scale; | 1134 | | #else | 1135 | 265k | # define qext_bytes 0 | 1136 | 265k | #endif | 1137 | 265k | ALLOC_STACK; | 1138 | | #ifdef ENABLE_QEXT | 1139 | | qext_scale = st->qext_scale; | 1140 | | #endif | 1141 | 265k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 1142 | | | 1143 | 265k | VALIDATE_CELT_DECODER(st); | 1144 | 265k | mode = st->mode; | 1145 | 265k | nbEBands = mode->nbEBands; | 1146 | 265k | overlap = mode->overlap; | 1147 | 265k | eBands = mode->eBands; | 1148 | 265k | start = st->start; | 1149 | 265k | end = st->end; | 1150 | 265k | frame_size *= st->downsample; | 1151 | | | 1152 | 265k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC); | 1153 | 265k | oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER); | 1154 | 265k | oldLogE = oldBandE + 2*nbEBands; | 1155 | 265k | oldLogE2 = oldLogE + 2*nbEBands; | 1156 | 265k | backgroundLogE = oldLogE2 + 2*nbEBands; | 1157 | | | 1158 | | #ifdef ENABLE_QEXT | 1159 | | if (qext_payload) { | 1160 | | ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len); | 1161 | | qext_bytes = qext_payload_len; | 1162 | | } else { | 1163 | | ec_dec_init(&ext_dec, NULL, 0); | 1164 | | } | 1165 | | #endif | 1166 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) | 1167 | | if (st->signalling && data!=NULL) | 1168 | | { | 1169 | | int data0=data[0]; | 1170 | | /* Convert "standard mode" to Opus header */ | 1171 | | # ifndef ENABLE_QEXT | 1172 | | if (mode->Fs==48000 && mode->shortMdctSize==120) | 1173 | | # endif | 1174 | | { | 1175 | | data0 = fromOpus(data0); | 1176 | | if (data0<0) | 1177 | | return OPUS_INVALID_PACKET; | 1178 | | } | 1179 | | st->end = end = IMAX(1, mode->effEBands-2*(data0>>5)); | 1180 | | LM = (data0>>3)&0x3; | 1181 | | C = 1 + ((data0>>2)&0x1); | 1182 | | if ((data[0] & 0x03) == 0x03) { | 1183 | | data++; | 1184 | | len--; | 1185 | | if (len<=0) | 1186 | | return OPUS_INVALID_PACKET; | 1187 | | if (data[0] & 0x40) { | 1188 | | int p; | 1189 | | int padding=0; | 1190 | | data++; | 1191 | | len--; | 1192 | | do { | 1193 | | int tmp; | 1194 | | if (len<=0) | 1195 | | return OPUS_INVALID_PACKET; | 1196 | | p = *data++; | 1197 | | len--; | 1198 | | tmp = p==255 ? 254: p; | 1199 | | len -= tmp; | 1200 | | padding += tmp; | 1201 | | } while (p==255); | 1202 | | padding--; | 1203 | | if (len <= 0 || padding<0) return OPUS_INVALID_PACKET; | 1204 | | #ifdef ENABLE_QEXT | 1205 | | qext_bytes = padding; | 1206 | | if (data[len] != QEXT_EXTENSION_ID<<1) | 1207 | | qext_bytes=0; | 1208 | | ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes); | 1209 | | #endif | 1210 | | } | 1211 | | } else | 1212 | | { | 1213 | | data++; | 1214 | | len--; | 1215 | | } | 1216 | | if (LM>mode->maxLM) | 1217 | | return OPUS_INVALID_PACKET; | 1218 | | if (frame_size < mode->shortMdctSize<<LM) | 1219 | | return OPUS_BUFFER_TOO_SMALL; | 1220 | | else | 1221 | | frame_size = mode->shortMdctSize<<LM; | 1222 | | } else { | 1223 | | #else | 1224 | 265k | { | 1225 | 265k | #endif | 1226 | 507k | for (LM=0;LM<=mode->maxLM;LM++) | 1227 | 507k | if (mode->shortMdctSize<<LM==frame_size) | 1228 | 265k | break; | 1229 | 265k | if (LM>mode->maxLM) | 1230 | 0 | return OPUS_BAD_ARG; | 1231 | 265k | } | 1232 | 265k | M=1<<LM; | 1233 | | | 1234 | 265k | if (len<0 || len>1275 || pcm==NULL) | 1235 | 0 | return OPUS_BAD_ARG; | 1236 | | | 1237 | 265k | N = M*mode->shortMdctSize; | 1238 | 417k | c=0; do { | 1239 | 417k | decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap); | 1240 | 417k | out_syn[c] = decode_mem[c]+decode_buffer_size-N; | 1241 | 417k | } while (++c<CC); | 1242 | | | 1243 | 265k | effEnd = end; | 1244 | 265k | if (effEnd > mode->effEBands) | 1245 | 0 | effEnd = mode->effEBands; | 1246 | | | 1247 | 265k | if (data == NULL || len<=1) | 1248 | 114k | { | 1249 | 114k | celt_decode_lost(st, N, LM | 1250 | | #ifdef ENABLE_DEEP_PLC | 1251 | | , lpcnet | 1252 | | #endif | 1253 | 114k | ); | 1254 | 114k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | 1255 | 114k | RESTORE_STACK; | 1256 | 114k | return frame_size/st->downsample; | 1257 | 114k | } | 1258 | | #ifdef ENABLE_DEEP_PLC | 1259 | | else { | 1260 | | /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */ | 1261 | | if (lpcnet) lpcnet->blend = 0; | 1262 | | } | 1263 | | #endif | 1264 | | | 1265 | | /* Check if there are at least two packets received consecutively before | 1266 | | * turning on the pitch-based PLC */ | 1267 | 150k | if (st->loss_duration == 0) st->skip_plc = 0; | 1268 | | | 1269 | 150k | if (dec == NULL) | 1270 | 13.6k | { | 1271 | 13.6k | ec_dec_init(&_dec,(unsigned char*)data,len); | 1272 | 13.6k | dec = &_dec; | 1273 | 13.6k | } | 1274 | | | 1275 | 150k | if (C==1) | 1276 | 96.0k | { | 1277 | 2.11M | for (i=0;i<nbEBands;i++) | 1278 | 2.01M | oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]); | 1279 | 96.0k | } | 1280 | | | 1281 | 150k | total_bits = len*8; | 1282 | 150k | tell = ec_tell(dec); | 1283 | | | 1284 | 150k | if (tell >= total_bits) | 1285 | 13.0k | silence = 1; | 1286 | 137k | else if (tell==1) | 1287 | 133k | silence = ec_dec_bit_logp(dec, 15); | 1288 | 4.80k | else | 1289 | 4.80k | silence = 0; | 1290 | 150k | if (silence) | 1291 | 19.9k | { | 1292 | | /* Pretend we've read all the remaining bits */ | 1293 | 19.9k | tell = len*8; | 1294 | 19.9k | dec->nbits_total+=tell-ec_tell(dec); | 1295 | 19.9k | } | 1296 | | | 1297 | 150k | postfilter_gain = 0; | 1298 | 150k | postfilter_pitch = 0; | 1299 | 150k | postfilter_tapset = 0; | 1300 | 150k | if (start==0 && tell+16 <= total_bits) | 1301 | 78.1k | { | 1302 | 78.1k | if(ec_dec_bit_logp(dec, 1)) | 1303 | 23.2k | { | 1304 | 23.2k | int qg, octave; | 1305 | 23.2k | octave = ec_dec_uint(dec, 6); | 1306 | 23.2k | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; | 1307 | 23.2k | qg = ec_dec_bits(dec, 3); | 1308 | 23.2k | if (ec_tell(dec)+2<=total_bits) | 1309 | 23.2k | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); | 1310 | 23.2k | postfilter_gain = QCONST16(.09375f,15)*(qg+1); | 1311 | 23.2k | } | 1312 | 78.1k | tell = ec_tell(dec); | 1313 | 78.1k | } | 1314 | | | 1315 | 150k | if (LM > 0 && tell+3 <= total_bits) | 1316 | 66.8k | { | 1317 | 66.8k | isTransient = ec_dec_bit_logp(dec, 3); | 1318 | 66.8k | tell = ec_tell(dec); | 1319 | 66.8k | } | 1320 | 84.0k | else | 1321 | 84.0k | isTransient = 0; | 1322 | | | 1323 | 150k | if (isTransient) | 1324 | 14.8k | shortBlocks = M; | 1325 | 136k | else | 1326 | 136k | shortBlocks = 0; | 1327 | | | 1328 | | /* Decode the global flags (first symbols in the stream) */ | 1329 | 150k | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; | 1330 | | /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the | 1331 | | risk of getting loud artifacts. */ | 1332 | 150k | if (!intra_ener && st->loss_duration != 0) { | 1333 | 30.0k | c=0; do | 1334 | 60.1k | { | 1335 | 60.1k | celt_glog safety = 0; | 1336 | 60.1k | int missing = IMIN(10, st->loss_duration>>LM); | 1337 | 60.1k | if (LM==0) safety = GCONST(1.5f); | 1338 | 6.91k | else if (LM==1) safety = GCONST(.5f); | 1339 | 1.09M | for (i=start;i<end;i++) | 1340 | 1.03M | { | 1341 | 1.03M | if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) { | 1342 | | /* If energy is going down already, continue the trend. */ | 1343 | 438k | opus_val32 slope; | 1344 | 438k | opus_val32 E0, E1, E2; | 1345 | 438k | E0 = oldBandE[c*nbEBands+i]; | 1346 | 438k | E1 = oldLogE[c*nbEBands+i]; | 1347 | 438k | E2 = oldLogE2[c*nbEBands+i]; | 1348 | 438k | slope = MAX32(E1 - E0, HALF32(E2 - E0)); | 1349 | 438k | slope = MING(slope, GCONST(2.f)); | 1350 | 438k | E0 -= MAX32(0, (1+missing)*slope); | 1351 | 438k | oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0); | 1352 | 596k | } else { | 1353 | | /* Otherwise take the min of the last frames. */ | 1354 | 596k | oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]); | 1355 | 596k | } | 1356 | | /* Shorter frames have more natural fluctuations -- play it safe. */ | 1357 | 1.03M | oldBandE[c*nbEBands+i] -= safety; | 1358 | 1.03M | } | 1359 | 60.1k | } while (++c<2); | 1360 | 30.0k | } | 1361 | | /* Get band energies */ | 1362 | 150k | unquant_coarse_energy(mode, start, end, oldBandE, | 1363 | 150k | intra_ener, dec, C, LM); | 1364 | | | 1365 | 150k | ALLOC(tf_res, nbEBands, int); | 1366 | 150k | tf_decode(start, end, isTransient, tf_res, LM, dec); | 1367 | | | 1368 | 150k | tell = ec_tell(dec); | 1369 | 150k | spread_decision = SPREAD_NORMAL; | 1370 | 150k | if (tell+4 <= total_bits) | 1371 | 48.5k | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); | 1372 | | | 1373 | 150k | ALLOC(cap, nbEBands, int); | 1374 | | | 1375 | 150k | init_caps(mode,cap,LM,C); | 1376 | | | 1377 | 150k | ALLOC(offsets, nbEBands, int); | 1378 | | | 1379 | 150k | dynalloc_logp = 6; | 1380 | 150k | total_bits<<=BITRES; | 1381 | 150k | tell = ec_tell_frac(dec); | 1382 | 2.57M | for (i=start;i<end;i++) | 1383 | 2.42M | { | 1384 | 2.42M | int width, quanta; | 1385 | 2.42M | int dynalloc_loop_logp; | 1386 | 2.42M | int boost; | 1387 | 2.42M | width = C*(eBands[i+1]-eBands[i])<<LM; | 1388 | | /* quanta is 6 bits, but no more than 1 bit/sample | 1389 | | and no less than 1/8 bit/sample */ | 1390 | 2.42M | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); | 1391 | 2.42M | dynalloc_loop_logp = dynalloc_logp; | 1392 | 2.42M | boost = 0; | 1393 | 2.46M | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) | 1394 | 721k | { | 1395 | 721k | int flag; | 1396 | 721k | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); | 1397 | 721k | tell = ec_tell_frac(dec); | 1398 | 721k | if (!flag) | 1399 | 675k | break; | 1400 | 45.6k | boost += quanta; | 1401 | 45.6k | total_bits -= quanta; | 1402 | 45.6k | dynalloc_loop_logp = 1; | 1403 | 45.6k | } | 1404 | 2.42M | offsets[i] = boost; | 1405 | | /* Making dynalloc more likely */ | 1406 | 2.42M | if (boost>0) | 1407 | 12.0k | dynalloc_logp = IMAX(2, dynalloc_logp-1); | 1408 | 2.42M | } | 1409 | | | 1410 | 150k | ALLOC(fine_quant, nbEBands, int); | 1411 | 150k | alloc_trim = tell+(6<<BITRES) <= total_bits ? | 1412 | 108k | ec_dec_icdf(dec, trim_icdf, 7) : 5; | 1413 | | | 1414 | 150k | bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; | 1415 | 150k | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; | 1416 | 150k | bits -= anti_collapse_rsv; | 1417 | | | 1418 | 150k | ALLOC(pulses, nbEBands, int); | 1419 | 150k | ALLOC(fine_priority, nbEBands, int); | 1420 | | | 1421 | 150k | codedBands = clt_compute_allocation(mode, start, end, offsets, cap, | 1422 | 150k | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, | 1423 | 150k | fine_quant, fine_priority, C, LM, dec, 0, 0, 0); | 1424 | | | 1425 | 150k | unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C); | 1426 | | | 1427 | 150k | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | 1428 | | | 1429 | | #ifdef ENABLE_QEXT | 1430 | | if (qext_bytes && end == nbEBands && | 1431 | | ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90)) | 1432 | | || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) { | 1433 | | int qext_intra_ener; | 1434 | | qext_oldBandE = backgroundLogE + 2*nbEBands; | 1435 | | compute_qext_mode(&qext_mode_struct, mode); | 1436 | | qext_mode = &qext_mode_struct; | 1437 | | qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2; | 1438 | | if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo); | 1439 | | qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0; | 1440 | | unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE, | 1441 | | qext_intra_ener, &ext_dec, C, LM); | 1442 | | } | 1443 | | ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int); | 1444 | | ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int); | 1445 | | qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1; | 1446 | | clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL, | 1447 | | qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0); | 1448 | | if (qext_bytes > 0) { | 1449 | | unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C); | 1450 | | } | 1451 | | #endif | 1452 | | | 1453 | 233k | c=0; do { | 1454 | 233k | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap); | 1455 | 233k | } while (++c<CC); | 1456 | | | 1457 | | /* Decode fixed codebook */ | 1458 | 150k | ALLOC(collapse_masks, C*nbEBands, unsigned char); | 1459 | | | 1460 | 150k | quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks, | 1461 | 150k | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, | 1462 | 150k | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0, | 1463 | 150k | st->arch, st->disable_inv | 1464 | 150k | ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses) | 1465 | 150k | ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap)); | 1466 | | | 1467 | | #ifdef ENABLE_QEXT | 1468 | | if (qext_mode) { | 1469 | | VARDECL(int, zeros); | 1470 | | VARDECL(unsigned char, qext_collapse_masks); | 1471 | | ec_dec dummy_dec; | 1472 | | int ext_balance; | 1473 | | ALLOC(zeros, nbEBands, int); | 1474 | | ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char); | 1475 | | ec_dec_init(&dummy_dec, NULL, 0); | 1476 | | OPUS_CLEAR(zeros, end); | 1477 | | ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec); | 1478 | | for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES); | 1479 | | unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C); | 1480 | | quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks, | 1481 | | NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros, | 1482 | | qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0, | 1483 | | st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL); | 1484 | | } | 1485 | | #endif | 1486 | | | 1487 | 150k | if (anti_collapse_rsv > 0) | 1488 | 4.39k | { | 1489 | 4.39k | anti_collapse_on = ec_dec_bits(dec, 1); | 1490 | 4.39k | } | 1491 | 150k | unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE, | 1492 | 150k | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); | 1493 | 150k | if (anti_collapse_on) | 1494 | 3.21k | anti_collapse(mode, X, collapse_masks, LM, C, N, | 1495 | 3.21k | start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch); | 1496 | | | 1497 | 150k | if (silence) | 1498 | 19.9k | { | 1499 | 675k | for (i=0;i<C*nbEBands;i++) | 1500 | 655k | oldBandE[i] = -GCONST(28.f); | 1501 | 19.9k | } | 1502 | 150k | if (st->prefilter_and_fold) { | 1503 | 31.2k | prefilter_and_fold(st, N); | 1504 | 31.2k | } | 1505 | 150k | celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, | 1506 | 150k | C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end)); | 1507 | | | 1508 | 233k | c=0; do { | 1509 | 233k | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | 1510 | 233k | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | 1511 | 233k | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | 1512 | 233k | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | 1513 | 233k | mode->window, overlap, st->arch); | 1514 | 233k | if (LM!=0) | 1515 | 118k | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize, | 1516 | 118k | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, | 1517 | 118k | mode->window, overlap, st->arch); | 1518 | | | 1519 | 233k | } while (++c<CC); | 1520 | 150k | st->postfilter_period_old = st->postfilter_period; | 1521 | 150k | st->postfilter_gain_old = st->postfilter_gain; | 1522 | 150k | st->postfilter_tapset_old = st->postfilter_tapset; | 1523 | 150k | st->postfilter_period = postfilter_pitch; | 1524 | 150k | st->postfilter_gain = postfilter_gain; | 1525 | 150k | st->postfilter_tapset = postfilter_tapset; | 1526 | 150k | if (LM!=0) | 1527 | 82.0k | { | 1528 | 82.0k | st->postfilter_period_old = st->postfilter_period; | 1529 | 82.0k | st->postfilter_gain_old = st->postfilter_gain; | 1530 | 82.0k | st->postfilter_tapset_old = st->postfilter_tapset; | 1531 | 82.0k | } | 1532 | | | 1533 | 150k | if (C==1) | 1534 | 96.0k | OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands); | 1535 | | | 1536 | 150k | if (!isTransient) | 1537 | 136k | { | 1538 | 136k | OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands); | 1539 | 136k | OPUS_COPY(oldLogE, oldBandE, 2*nbEBands); | 1540 | 136k | } else { | 1541 | 637k | for (i=0;i<2*nbEBands;i++) | 1542 | 623k | oldLogE[i] = MING(oldLogE[i], oldBandE[i]); | 1543 | 14.8k | } | 1544 | | /* In normal circumstances, we only allow the noise floor to increase by | 1545 | | up to 2.4 dB/second, but when we're in DTX we give the weight of | 1546 | | all missing packets to the update packet. */ | 1547 | 150k | max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f); | 1548 | 6.48M | for (i=0;i<2*nbEBands;i++) | 1549 | 6.33M | backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]); | 1550 | | /* In case start or end were to change */ | 1551 | 150k | c=0; do | 1552 | 301k | { | 1553 | 908k | for (i=0;i<start;i++) | 1554 | 606k | { | 1555 | 606k | oldBandE[c*nbEBands+i]=0; | 1556 | 606k | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); | 1557 | 606k | } | 1558 | 1.18M | for (i=end;i<nbEBands;i++) | 1559 | 884k | { | 1560 | 884k | oldBandE[c*nbEBands+i]=0; | 1561 | 884k | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f); | 1562 | 884k | } | 1563 | 301k | } while (++c<2); | 1564 | 150k | st->rng = dec->rng; | 1565 | | #ifdef ENABLE_QEXT | 1566 | | if (qext_bytes) st->rng = st->rng ^ ext_dec.rng; | 1567 | | #endif | 1568 | | | 1569 | 150k | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum); | 1570 | 150k | st->loss_duration = 0; | 1571 | 150k | st->prefilter_and_fold = 0; | 1572 | 150k | RESTORE_STACK; | 1573 | 150k | if (ec_tell(dec) > 8*len) | 1574 | 4 | return OPUS_INTERNAL_ERROR; | 1575 | | #ifdef ENABLE_QEXT | 1576 | | if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes) | 1577 | | return OPUS_INTERNAL_ERROR; | 1578 | | #endif | 1579 | 150k | if(ec_get_error(dec)) | 1580 | 1.79k | st->error = 1; | 1581 | 150k | return frame_size/st->downsample; | 1582 | 150k | } |
|
1583 | | |
1584 | | int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, |
1585 | | int len, opus_res * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum) |
1586 | 61.8k | { |
1587 | 61.8k | return celt_decode_with_ec_dred(st, data, len, pcm, frame_size, dec, accum |
1588 | | #ifdef ENABLE_DEEP_PLC |
1589 | | , NULL |
1590 | | #endif |
1591 | 61.8k | ARG_QEXT(NULL) ARG_QEXT(0) |
1592 | 61.8k | ); |
1593 | 61.8k | } Line | Count | Source | 1586 | 30.9k | { | 1587 | 30.9k | return celt_decode_with_ec_dred(st, data, len, pcm, frame_size, dec, accum | 1588 | | #ifdef ENABLE_DEEP_PLC | 1589 | | , NULL | 1590 | | #endif | 1591 | 30.9k | ARG_QEXT(NULL) ARG_QEXT(0) | 1592 | 30.9k | ); | 1593 | 30.9k | } |
Line | Count | Source | 1586 | 30.9k | { | 1587 | 30.9k | return celt_decode_with_ec_dred(st, data, len, pcm, frame_size, dec, accum | 1588 | | #ifdef ENABLE_DEEP_PLC | 1589 | | , NULL | 1590 | | #endif | 1591 | 30.9k | ARG_QEXT(NULL) ARG_QEXT(0) | 1592 | 30.9k | ); | 1593 | 30.9k | } |
|
1594 | | |
1595 | | #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) |
1596 | | |
1597 | | #if defined(FIXED_POINT) && !defined(ENABLE_RES24) |
1598 | | int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) |
1599 | | { |
1600 | | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0); |
1601 | | } |
1602 | | #else |
1603 | | int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) |
1604 | | { |
1605 | | int j, ret, C, N; |
1606 | | VARDECL(opus_res, out); |
1607 | | ALLOC_STACK; |
1608 | | |
1609 | | if (pcm==NULL) |
1610 | | return OPUS_BAD_ARG; |
1611 | | |
1612 | | C = st->channels; |
1613 | | N = frame_size; |
1614 | | |
1615 | | ALLOC(out, C*N, opus_res); |
1616 | | ret = celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0); |
1617 | | if (ret>0) |
1618 | | for (j=0;j<C*ret;j++) |
1619 | | pcm[j]=RES2INT16(out[j]); |
1620 | | |
1621 | | RESTORE_STACK; |
1622 | | return ret; |
1623 | | } |
1624 | | #endif |
1625 | | |
1626 | | #if defined(FIXED_POINT) && defined(ENABLE_RES24) |
1627 | | int opus_custom_decode24(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int32 * OPUS_RESTRICT pcm, int frame_size) |
1628 | | { |
1629 | | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0); |
1630 | | } |
1631 | | #else |
1632 | | int opus_custom_decode24(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int32 * OPUS_RESTRICT pcm, int frame_size) |
1633 | | { |
1634 | | int j, ret, C, N; |
1635 | | VARDECL(opus_res, out); |
1636 | | ALLOC_STACK; |
1637 | | |
1638 | | if (pcm==NULL) |
1639 | | return OPUS_BAD_ARG; |
1640 | | |
1641 | | C = st->channels; |
1642 | | N = frame_size; |
1643 | | |
1644 | | ALLOC(out, C*N, opus_res); |
1645 | | ret = celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0); |
1646 | | if (ret>0) |
1647 | | for (j=0;j<C*ret;j++) |
1648 | | pcm[j]=RES2INT24(out[j]); |
1649 | | |
1650 | | RESTORE_STACK; |
1651 | | return ret; |
1652 | | } |
1653 | | #endif |
1654 | | |
1655 | | |
1656 | | #ifndef DISABLE_FLOAT_API |
1657 | | |
1658 | | # if !defined(FIXED_POINT) |
1659 | | int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) |
1660 | | { |
1661 | | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0); |
1662 | | } |
1663 | | # else |
1664 | | int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) |
1665 | | { |
1666 | | int j, ret, C, N; |
1667 | | VARDECL(opus_res, out); |
1668 | | ALLOC_STACK; |
1669 | | |
1670 | | if (pcm==NULL) |
1671 | | return OPUS_BAD_ARG; |
1672 | | |
1673 | | C = st->channels; |
1674 | | N = frame_size; |
1675 | | |
1676 | | ALLOC(out, C*N, opus_res); |
1677 | | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0); |
1678 | | if (ret>0) |
1679 | | for (j=0;j<C*ret;j++) |
1680 | | pcm[j]=RES2FLOAT(out[j]); |
1681 | | |
1682 | | RESTORE_STACK; |
1683 | | return ret; |
1684 | | } |
1685 | | # endif |
1686 | | |
1687 | | #endif |
1688 | | |
1689 | | #endif /* CUSTOM_MODES */ |
1690 | | |
1691 | | int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...) |
1692 | 7.67M | { |
1693 | 7.67M | va_list ap; |
1694 | | |
1695 | 7.67M | va_start(ap, request); |
1696 | 7.67M | switch (request) |
1697 | 7.67M | { |
1698 | 0 | case OPUS_SET_COMPLEXITY_REQUEST: |
1699 | 0 | { |
1700 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
1701 | 0 | if(value<0 || value>10) |
1702 | 0 | { |
1703 | 0 | goto bad_arg; |
1704 | 0 | } |
1705 | 0 | st->complexity = value; |
1706 | 0 | } |
1707 | 0 | break; |
1708 | 0 | case OPUS_GET_COMPLEXITY_REQUEST: |
1709 | 0 | { |
1710 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1711 | 0 | if (!value) |
1712 | 0 | { |
1713 | 0 | goto bad_arg; |
1714 | 0 | } |
1715 | 0 | *value = st->complexity; |
1716 | 0 | } |
1717 | 0 | break; |
1718 | 1.42M | case CELT_SET_START_BAND_REQUEST: |
1719 | 1.42M | { |
1720 | 1.42M | opus_int32 value = va_arg(ap, opus_int32); |
1721 | 1.42M | if (value<0 || value>=st->mode->nbEBands) |
1722 | 0 | goto bad_arg; |
1723 | 1.42M | st->start = value; |
1724 | 1.42M | } |
1725 | 0 | break; |
1726 | 856k | case CELT_SET_END_BAND_REQUEST: |
1727 | 856k | { |
1728 | 856k | opus_int32 value = va_arg(ap, opus_int32); |
1729 | 856k | if (value<1 || value>st->mode->nbEBands) |
1730 | 0 | goto bad_arg; |
1731 | 856k | st->end = value; |
1732 | 856k | } |
1733 | 0 | break; |
1734 | 1.35M | case CELT_SET_CHANNELS_REQUEST: |
1735 | 1.35M | { |
1736 | 1.35M | opus_int32 value = va_arg(ap, opus_int32); |
1737 | 1.35M | if (value<1 || value>2) |
1738 | 0 | goto bad_arg; |
1739 | 1.35M | st->stream_channels = value; |
1740 | 1.35M | } |
1741 | 0 | break; |
1742 | 0 | case CELT_GET_AND_CLEAR_ERROR_REQUEST: |
1743 | 0 | { |
1744 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1745 | 0 | if (value==NULL) |
1746 | 0 | goto bad_arg; |
1747 | 0 | *value=st->error; |
1748 | 0 | st->error = 0; |
1749 | 0 | } |
1750 | 0 | break; |
1751 | 0 | case OPUS_GET_LOOKAHEAD_REQUEST: |
1752 | 0 | { |
1753 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1754 | 0 | if (value==NULL) |
1755 | 0 | goto bad_arg; |
1756 | 0 | *value = st->overlap/st->downsample; |
1757 | 0 | } |
1758 | 0 | break; |
1759 | 834k | case OPUS_RESET_STATE: |
1760 | 834k | { |
1761 | 834k | int i; |
1762 | 834k | opus_val16 *lpc; |
1763 | 834k | celt_glog *oldBandE, *oldLogE, *oldLogE2; |
1764 | 834k | int decode_buffer_size; |
1765 | | #ifdef ENABLE_QEXT |
1766 | | int qext_scale = st->qext_scale; |
1767 | | #endif |
1768 | 834k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); |
1769 | 834k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+st->overlap)*st->channels); |
1770 | 834k | oldBandE = (celt_glog*)(lpc+st->channels*CELT_LPC_ORDER); |
1771 | 834k | oldLogE = oldBandE + 2*st->mode->nbEBands; |
1772 | 834k | oldLogE2 = oldLogE + 2*st->mode->nbEBands; |
1773 | 834k | OPUS_CLEAR((char*)&st->DECODER_RESET_START, |
1774 | 834k | opus_custom_decoder_get_size(st->mode, st->channels)- |
1775 | 834k | ((char*)&st->DECODER_RESET_START - (char*)st)); |
1776 | 35.8M | for (i=0;i<2*st->mode->nbEBands;i++) |
1777 | 35.0M | oldLogE[i]=oldLogE2[i]=-GCONST(28.f); |
1778 | 834k | st->skip_plc = 1; |
1779 | 834k | } |
1780 | 834k | break; |
1781 | 0 | case OPUS_GET_PITCH_REQUEST: |
1782 | 0 | { |
1783 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1784 | 0 | if (value==NULL) |
1785 | 0 | goto bad_arg; |
1786 | 0 | *value = st->postfilter_period; |
1787 | 0 | } |
1788 | 0 | break; |
1789 | 1.35M | case CELT_GET_MODE_REQUEST: |
1790 | 1.35M | { |
1791 | 1.35M | const CELTMode ** value = va_arg(ap, const CELTMode**); |
1792 | 1.35M | if (value==0) |
1793 | 0 | goto bad_arg; |
1794 | 1.35M | *value=st->mode; |
1795 | 1.35M | } |
1796 | 0 | break; |
1797 | 808k | case CELT_SET_SIGNALLING_REQUEST: |
1798 | 808k | { |
1799 | 808k | opus_int32 value = va_arg(ap, opus_int32); |
1800 | 808k | st->signalling = value; |
1801 | 808k | } |
1802 | 808k | break; |
1803 | 1.03M | case OPUS_GET_FINAL_RANGE_REQUEST: |
1804 | 1.03M | { |
1805 | 1.03M | opus_uint32 * value = va_arg(ap, opus_uint32 *); |
1806 | 1.03M | if (value==0) |
1807 | 0 | goto bad_arg; |
1808 | 1.03M | *value=st->rng; |
1809 | 1.03M | } |
1810 | 0 | break; |
1811 | 0 | case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: |
1812 | 0 | { |
1813 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
1814 | 0 | if(value<0 || value>1) |
1815 | 0 | { |
1816 | 0 | goto bad_arg; |
1817 | 0 | } |
1818 | 0 | st->disable_inv = value; |
1819 | 0 | } |
1820 | 0 | break; |
1821 | 0 | case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: |
1822 | 0 | { |
1823 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1824 | 0 | if (!value) |
1825 | 0 | { |
1826 | 0 | goto bad_arg; |
1827 | 0 | } |
1828 | 0 | *value = st->disable_inv; |
1829 | 0 | } |
1830 | 0 | break; |
1831 | 0 | default: |
1832 | 0 | goto bad_request; |
1833 | 7.67M | } |
1834 | 7.67M | va_end(ap); |
1835 | 7.67M | return OPUS_OK; |
1836 | 0 | bad_arg: |
1837 | 0 | va_end(ap); |
1838 | 0 | return OPUS_BAD_ARG; |
1839 | 0 | bad_request: |
1840 | 0 | va_end(ap); |
1841 | 0 | return OPUS_UNIMPLEMENTED; |
1842 | 7.67M | } Line | Count | Source | 1692 | 3.83M | { | 1693 | 3.83M | va_list ap; | 1694 | | | 1695 | 3.83M | va_start(ap, request); | 1696 | 3.83M | switch (request) | 1697 | 3.83M | { | 1698 | 0 | case OPUS_SET_COMPLEXITY_REQUEST: | 1699 | 0 | { | 1700 | 0 | opus_int32 value = va_arg(ap, opus_int32); | 1701 | 0 | if(value<0 || value>10) | 1702 | 0 | { | 1703 | 0 | goto bad_arg; | 1704 | 0 | } | 1705 | 0 | st->complexity = value; | 1706 | 0 | } | 1707 | 0 | break; | 1708 | 0 | case OPUS_GET_COMPLEXITY_REQUEST: | 1709 | 0 | { | 1710 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1711 | 0 | if (!value) | 1712 | 0 | { | 1713 | 0 | goto bad_arg; | 1714 | 0 | } | 1715 | 0 | *value = st->complexity; | 1716 | 0 | } | 1717 | 0 | break; | 1718 | 710k | case CELT_SET_START_BAND_REQUEST: | 1719 | 710k | { | 1720 | 710k | opus_int32 value = va_arg(ap, opus_int32); | 1721 | 710k | if (value<0 || value>=st->mode->nbEBands) | 1722 | 0 | goto bad_arg; | 1723 | 710k | st->start = value; | 1724 | 710k | } | 1725 | 0 | break; | 1726 | 428k | case CELT_SET_END_BAND_REQUEST: | 1727 | 428k | { | 1728 | 428k | opus_int32 value = va_arg(ap, opus_int32); | 1729 | 428k | if (value<1 || value>st->mode->nbEBands) | 1730 | 0 | goto bad_arg; | 1731 | 428k | st->end = value; | 1732 | 428k | } | 1733 | 0 | break; | 1734 | 679k | case CELT_SET_CHANNELS_REQUEST: | 1735 | 679k | { | 1736 | 679k | opus_int32 value = va_arg(ap, opus_int32); | 1737 | 679k | if (value<1 || value>2) | 1738 | 0 | goto bad_arg; | 1739 | 679k | st->stream_channels = value; | 1740 | 679k | } | 1741 | 0 | break; | 1742 | 0 | case CELT_GET_AND_CLEAR_ERROR_REQUEST: | 1743 | 0 | { | 1744 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1745 | 0 | if (value==NULL) | 1746 | 0 | goto bad_arg; | 1747 | 0 | *value=st->error; | 1748 | 0 | st->error = 0; | 1749 | 0 | } | 1750 | 0 | break; | 1751 | 0 | case OPUS_GET_LOOKAHEAD_REQUEST: | 1752 | 0 | { | 1753 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1754 | 0 | if (value==NULL) | 1755 | 0 | goto bad_arg; | 1756 | 0 | *value = st->overlap/st->downsample; | 1757 | 0 | } | 1758 | 0 | break; | 1759 | 417k | case OPUS_RESET_STATE: | 1760 | 417k | { | 1761 | 417k | int i; | 1762 | 417k | opus_val16 *lpc; | 1763 | 417k | celt_glog *oldBandE, *oldLogE, *oldLogE2; | 1764 | 417k | int decode_buffer_size; | 1765 | | #ifdef ENABLE_QEXT | 1766 | | int qext_scale = st->qext_scale; | 1767 | | #endif | 1768 | 417k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 1769 | 417k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+st->overlap)*st->channels); | 1770 | 417k | oldBandE = (celt_glog*)(lpc+st->channels*CELT_LPC_ORDER); | 1771 | 417k | oldLogE = oldBandE + 2*st->mode->nbEBands; | 1772 | 417k | oldLogE2 = oldLogE + 2*st->mode->nbEBands; | 1773 | 417k | OPUS_CLEAR((char*)&st->DECODER_RESET_START, | 1774 | 417k | opus_custom_decoder_get_size(st->mode, st->channels)- | 1775 | 417k | ((char*)&st->DECODER_RESET_START - (char*)st)); | 1776 | 17.9M | for (i=0;i<2*st->mode->nbEBands;i++) | 1777 | 17.5M | oldLogE[i]=oldLogE2[i]=-GCONST(28.f); | 1778 | 417k | st->skip_plc = 1; | 1779 | 417k | } | 1780 | 417k | break; | 1781 | 0 | case OPUS_GET_PITCH_REQUEST: | 1782 | 0 | { | 1783 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1784 | 0 | if (value==NULL) | 1785 | 0 | goto bad_arg; | 1786 | 0 | *value = st->postfilter_period; | 1787 | 0 | } | 1788 | 0 | break; | 1789 | 679k | case CELT_GET_MODE_REQUEST: | 1790 | 679k | { | 1791 | 679k | const CELTMode ** value = va_arg(ap, const CELTMode**); | 1792 | 679k | if (value==0) | 1793 | 0 | goto bad_arg; | 1794 | 679k | *value=st->mode; | 1795 | 679k | } | 1796 | 0 | break; | 1797 | 404k | case CELT_SET_SIGNALLING_REQUEST: | 1798 | 404k | { | 1799 | 404k | opus_int32 value = va_arg(ap, opus_int32); | 1800 | 404k | st->signalling = value; | 1801 | 404k | } | 1802 | 404k | break; | 1803 | 516k | case OPUS_GET_FINAL_RANGE_REQUEST: | 1804 | 516k | { | 1805 | 516k | opus_uint32 * value = va_arg(ap, opus_uint32 *); | 1806 | 516k | if (value==0) | 1807 | 0 | goto bad_arg; | 1808 | 516k | *value=st->rng; | 1809 | 516k | } | 1810 | 0 | break; | 1811 | 0 | case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: | 1812 | 0 | { | 1813 | 0 | opus_int32 value = va_arg(ap, opus_int32); | 1814 | 0 | if(value<0 || value>1) | 1815 | 0 | { | 1816 | 0 | goto bad_arg; | 1817 | 0 | } | 1818 | 0 | st->disable_inv = value; | 1819 | 0 | } | 1820 | 0 | break; | 1821 | 0 | case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: | 1822 | 0 | { | 1823 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1824 | 0 | if (!value) | 1825 | 0 | { | 1826 | 0 | goto bad_arg; | 1827 | 0 | } | 1828 | 0 | *value = st->disable_inv; | 1829 | 0 | } | 1830 | 0 | break; | 1831 | 0 | default: | 1832 | 0 | goto bad_request; | 1833 | 3.83M | } | 1834 | 3.83M | va_end(ap); | 1835 | 3.83M | return OPUS_OK; | 1836 | 0 | bad_arg: | 1837 | 0 | va_end(ap); | 1838 | 0 | return OPUS_BAD_ARG; | 1839 | 0 | bad_request: | 1840 | 0 | va_end(ap); | 1841 | 0 | return OPUS_UNIMPLEMENTED; | 1842 | 3.83M | } |
Line | Count | Source | 1692 | 3.83M | { | 1693 | 3.83M | va_list ap; | 1694 | | | 1695 | 3.83M | va_start(ap, request); | 1696 | 3.83M | switch (request) | 1697 | 3.83M | { | 1698 | 0 | case OPUS_SET_COMPLEXITY_REQUEST: | 1699 | 0 | { | 1700 | 0 | opus_int32 value = va_arg(ap, opus_int32); | 1701 | 0 | if(value<0 || value>10) | 1702 | 0 | { | 1703 | 0 | goto bad_arg; | 1704 | 0 | } | 1705 | 0 | st->complexity = value; | 1706 | 0 | } | 1707 | 0 | break; | 1708 | 0 | case OPUS_GET_COMPLEXITY_REQUEST: | 1709 | 0 | { | 1710 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1711 | 0 | if (!value) | 1712 | 0 | { | 1713 | 0 | goto bad_arg; | 1714 | 0 | } | 1715 | 0 | *value = st->complexity; | 1716 | 0 | } | 1717 | 0 | break; | 1718 | 710k | case CELT_SET_START_BAND_REQUEST: | 1719 | 710k | { | 1720 | 710k | opus_int32 value = va_arg(ap, opus_int32); | 1721 | 710k | if (value<0 || value>=st->mode->nbEBands) | 1722 | 0 | goto bad_arg; | 1723 | 710k | st->start = value; | 1724 | 710k | } | 1725 | 0 | break; | 1726 | 428k | case CELT_SET_END_BAND_REQUEST: | 1727 | 428k | { | 1728 | 428k | opus_int32 value = va_arg(ap, opus_int32); | 1729 | 428k | if (value<1 || value>st->mode->nbEBands) | 1730 | 0 | goto bad_arg; | 1731 | 428k | st->end = value; | 1732 | 428k | } | 1733 | 0 | break; | 1734 | 679k | case CELT_SET_CHANNELS_REQUEST: | 1735 | 679k | { | 1736 | 679k | opus_int32 value = va_arg(ap, opus_int32); | 1737 | 679k | if (value<1 || value>2) | 1738 | 0 | goto bad_arg; | 1739 | 679k | st->stream_channels = value; | 1740 | 679k | } | 1741 | 0 | break; | 1742 | 0 | case CELT_GET_AND_CLEAR_ERROR_REQUEST: | 1743 | 0 | { | 1744 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1745 | 0 | if (value==NULL) | 1746 | 0 | goto bad_arg; | 1747 | 0 | *value=st->error; | 1748 | 0 | st->error = 0; | 1749 | 0 | } | 1750 | 0 | break; | 1751 | 0 | case OPUS_GET_LOOKAHEAD_REQUEST: | 1752 | 0 | { | 1753 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1754 | 0 | if (value==NULL) | 1755 | 0 | goto bad_arg; | 1756 | 0 | *value = st->overlap/st->downsample; | 1757 | 0 | } | 1758 | 0 | break; | 1759 | 417k | case OPUS_RESET_STATE: | 1760 | 417k | { | 1761 | 417k | int i; | 1762 | 417k | opus_val16 *lpc; | 1763 | 417k | celt_glog *oldBandE, *oldLogE, *oldLogE2; | 1764 | 417k | int decode_buffer_size; | 1765 | 417k | #ifdef ENABLE_QEXT | 1766 | 417k | int qext_scale = st->qext_scale; | 1767 | 417k | #endif | 1768 | 417k | decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE); | 1769 | 417k | lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+st->overlap)*st->channels); | 1770 | 417k | oldBandE = (celt_glog*)(lpc+st->channels*CELT_LPC_ORDER); | 1771 | 417k | oldLogE = oldBandE + 2*st->mode->nbEBands; | 1772 | 417k | oldLogE2 = oldLogE + 2*st->mode->nbEBands; | 1773 | 417k | OPUS_CLEAR((char*)&st->DECODER_RESET_START, | 1774 | 417k | opus_custom_decoder_get_size(st->mode, st->channels)- | 1775 | 417k | ((char*)&st->DECODER_RESET_START - (char*)st)); | 1776 | 17.9M | for (i=0;i<2*st->mode->nbEBands;i++) | 1777 | 17.5M | oldLogE[i]=oldLogE2[i]=-GCONST(28.f); | 1778 | 417k | st->skip_plc = 1; | 1779 | 417k | } | 1780 | 417k | break; | 1781 | 0 | case OPUS_GET_PITCH_REQUEST: | 1782 | 0 | { | 1783 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1784 | 0 | if (value==NULL) | 1785 | 0 | goto bad_arg; | 1786 | 0 | *value = st->postfilter_period; | 1787 | 0 | } | 1788 | 0 | break; | 1789 | 679k | case CELT_GET_MODE_REQUEST: | 1790 | 679k | { | 1791 | 679k | const CELTMode ** value = va_arg(ap, const CELTMode**); | 1792 | 679k | if (value==0) | 1793 | 0 | goto bad_arg; | 1794 | 679k | *value=st->mode; | 1795 | 679k | } | 1796 | 0 | break; | 1797 | 404k | case CELT_SET_SIGNALLING_REQUEST: | 1798 | 404k | { | 1799 | 404k | opus_int32 value = va_arg(ap, opus_int32); | 1800 | 404k | st->signalling = value; | 1801 | 404k | } | 1802 | 404k | break; | 1803 | 516k | case OPUS_GET_FINAL_RANGE_REQUEST: | 1804 | 516k | { | 1805 | 516k | opus_uint32 * value = va_arg(ap, opus_uint32 *); | 1806 | 516k | if (value==0) | 1807 | 0 | goto bad_arg; | 1808 | 516k | *value=st->rng; | 1809 | 516k | } | 1810 | 0 | break; | 1811 | 0 | case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: | 1812 | 0 | { | 1813 | 0 | opus_int32 value = va_arg(ap, opus_int32); | 1814 | 0 | if(value<0 || value>1) | 1815 | 0 | { | 1816 | 0 | goto bad_arg; | 1817 | 0 | } | 1818 | 0 | st->disable_inv = value; | 1819 | 0 | } | 1820 | 0 | break; | 1821 | 0 | case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: | 1822 | 0 | { | 1823 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); | 1824 | 0 | if (!value) | 1825 | 0 | { | 1826 | 0 | goto bad_arg; | 1827 | 0 | } | 1828 | 0 | *value = st->disable_inv; | 1829 | 0 | } | 1830 | 0 | break; | 1831 | 0 | default: | 1832 | 0 | goto bad_request; | 1833 | 3.83M | } | 1834 | 3.83M | va_end(ap); | 1835 | 3.83M | return OPUS_OK; | 1836 | 0 | bad_arg: | 1837 | 0 | va_end(ap); | 1838 | 0 | return OPUS_BAD_ARG; | 1839 | 0 | bad_request: | 1840 | 0 | va_end(ap); | 1841 | 0 | return OPUS_UNIMPLEMENTED; | 1842 | 3.83M | } |
|