/src/opus/src/opus_decoder.c
Line | Count | Source |
1 | | /* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited |
2 | | Copyright (c) 2024 Arm Limited |
3 | | Written by Jean-Marc Valin and Koen Vos */ |
4 | | /* |
5 | | Redistribution and use in source and binary forms, with or without |
6 | | modification, are permitted provided that the following conditions |
7 | | are met: |
8 | | |
9 | | - Redistributions of source code must retain the above copyright |
10 | | notice, this list of conditions and the following disclaimer. |
11 | | |
12 | | - Redistributions in binary form must reproduce the above copyright |
13 | | notice, this list of conditions and the following disclaimer in the |
14 | | documentation and/or other materials provided with the distribution. |
15 | | |
16 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
20 | | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
21 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
23 | | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
24 | | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
25 | | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
26 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | | */ |
28 | | |
29 | | #ifdef HAVE_CONFIG_H |
30 | | # include "config.h" |
31 | | #endif |
32 | | |
33 | | #ifndef OPUS_BUILD |
34 | | # error "OPUS_BUILD _MUST_ be defined to build Opus. This probably means you need other defines as well, as in a config.h. See the included build files for details." |
35 | | #endif |
36 | | |
37 | | #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__) && !defined(OPUS_WILL_BE_SLOW) |
38 | | # pragma message "You appear to be compiling without optimization, if so opus will be very slow." |
39 | | #endif |
40 | | |
41 | | #include <stdarg.h> |
42 | | #include "celt.h" |
43 | | #include "opus.h" |
44 | | #include "entdec.h" |
45 | | #include "modes.h" |
46 | | #include "API.h" |
47 | | #include "stack_alloc.h" |
48 | | #include "float_cast.h" |
49 | | #include "opus_private.h" |
50 | | #include "os_support.h" |
51 | | #include "structs.h" |
52 | | #include "define.h" |
53 | | #include "mathops.h" |
54 | | #include "cpu_support.h" |
55 | | |
56 | | #ifdef ENABLE_DEEP_PLC |
57 | | #include "dred_rdovae_dec_data.h" |
58 | | #include "dred_rdovae_dec.h" |
59 | | #endif |
60 | | |
61 | | #ifdef ENABLE_OSCE |
62 | | #include "osce.h" |
63 | | #endif |
64 | | |
65 | | struct OpusDecoder { |
66 | | int celt_dec_offset; |
67 | | int silk_dec_offset; |
68 | | int channels; |
69 | | opus_int32 Fs; /** Sampling rate (at the API level) */ |
70 | | silk_DecControlStruct DecControl; |
71 | | int decode_gain; |
72 | | int complexity; |
73 | | int ignore_extensions; |
74 | | int arch; |
75 | | #ifdef ENABLE_DEEP_PLC |
76 | | LPCNetPLCState lpcnet; |
77 | | #endif |
78 | | |
79 | | /* Everything beyond this point gets cleared on a reset */ |
80 | | #define OPUS_DECODER_RESET_START stream_channels |
81 | | int stream_channels; |
82 | | |
83 | | int bandwidth; |
84 | | int mode; |
85 | | int prev_mode; |
86 | | int frame_size; |
87 | | int prev_redundancy; |
88 | | int last_packet_duration; |
89 | | #ifndef FIXED_POINT |
90 | | opus_val16 softclip_mem[2]; |
91 | | #endif |
92 | | |
93 | | opus_uint32 rangeFinal; |
94 | | }; |
95 | | |
96 | | #if defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS) |
97 | | static void validate_opus_decoder(OpusDecoder *st) |
98 | 192k | { |
99 | 192k | celt_assert(st->channels == 1 || st->channels == 2); |
100 | | #ifdef ENABLE_QEXT |
101 | | celt_assert(st->Fs == 96000 || st->Fs == 48000 || st->Fs == 24000 || st->Fs == 16000 || st->Fs == 12000 || st->Fs == 8000); |
102 | | #else |
103 | 192k | celt_assert(st->Fs == 48000 || st->Fs == 24000 || st->Fs == 16000 || st->Fs == 12000 || st->Fs == 8000); |
104 | 192k | #endif |
105 | 192k | celt_assert(st->DecControl.API_sampleRate == st->Fs); |
106 | 192k | celt_assert(st->DecControl.internalSampleRate == 0 || st->DecControl.internalSampleRate == 16000 || st->DecControl.internalSampleRate == 12000 || st->DecControl.internalSampleRate == 8000); |
107 | 192k | celt_assert(st->DecControl.nChannelsAPI == st->channels); |
108 | 192k | celt_assert(st->DecControl.nChannelsInternal == 0 || st->DecControl.nChannelsInternal == 1 || st->DecControl.nChannelsInternal == 2); |
109 | 192k | celt_assert(st->DecControl.payloadSize_ms == 0 || st->DecControl.payloadSize_ms == 10 || st->DecControl.payloadSize_ms == 20 || st->DecControl.payloadSize_ms == 40 || st->DecControl.payloadSize_ms == 60); |
110 | 192k | #ifdef OPUS_ARCHMASK |
111 | 192k | celt_assert(st->arch >= 0); |
112 | 192k | celt_assert(st->arch <= OPUS_ARCHMASK); |
113 | 192k | #endif |
114 | 192k | celt_assert(st->stream_channels == 1 || st->stream_channels == 2); |
115 | 192k | } |
116 | 218k | #define VALIDATE_OPUS_DECODER(st) validate_opus_decoder(st) |
117 | | #else |
118 | | #define VALIDATE_OPUS_DECODER(st) |
119 | | #endif |
120 | | |
121 | | int opus_decoder_get_size(int channels) |
122 | 835k | { |
123 | 835k | int silkDecSizeBytes, celtDecSizeBytes; |
124 | 835k | int ret; |
125 | 835k | if (channels<1 || channels > 2) |
126 | 0 | return 0; |
127 | 835k | ret = silk_Get_Decoder_Size( &silkDecSizeBytes ); |
128 | 835k | if(ret) |
129 | 0 | return 0; |
130 | 835k | silkDecSizeBytes = align(silkDecSizeBytes); |
131 | 835k | celtDecSizeBytes = celt_decoder_get_size(channels); |
132 | 835k | return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes; |
133 | 835k | } |
134 | | |
135 | | int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels) |
136 | 189k | { |
137 | 189k | void *silk_dec; |
138 | 189k | CELTDecoder *celt_dec; |
139 | 189k | int ret, silkDecSizeBytes; |
140 | | |
141 | 189k | if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000 |
142 | | #ifdef ENABLE_QEXT |
143 | | &&Fs!=96000 |
144 | | #endif |
145 | 189k | ) |
146 | 189k | || (channels!=1&&channels!=2)) |
147 | 0 | return OPUS_BAD_ARG; |
148 | | |
149 | 189k | OPUS_CLEAR((char*)st, opus_decoder_get_size(channels)); |
150 | | /* Initialize SILK decoder */ |
151 | 189k | ret = silk_Get_Decoder_Size(&silkDecSizeBytes); |
152 | 189k | if (ret) |
153 | 0 | return OPUS_INTERNAL_ERROR; |
154 | | |
155 | 189k | silkDecSizeBytes = align(silkDecSizeBytes); |
156 | 189k | st->silk_dec_offset = align(sizeof(OpusDecoder)); |
157 | 189k | st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes; |
158 | 189k | silk_dec = (char*)st+st->silk_dec_offset; |
159 | 189k | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); |
160 | 189k | st->stream_channels = st->channels = channels; |
161 | 189k | st->complexity = 0; |
162 | | |
163 | 189k | st->Fs = Fs; |
164 | 189k | st->DecControl.API_sampleRate = st->Fs; |
165 | 189k | st->DecControl.nChannelsAPI = st->channels; |
166 | | |
167 | | /* Reset decoder */ |
168 | 189k | ret = silk_InitDecoder( silk_dec ); |
169 | 189k | if(ret)return OPUS_INTERNAL_ERROR; |
170 | | |
171 | | /* Initialize CELT decoder */ |
172 | 189k | ret = celt_decoder_init(celt_dec, Fs, channels); |
173 | 189k | if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR; |
174 | | |
175 | 189k | celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0)); |
176 | | |
177 | 189k | st->prev_mode = 0; |
178 | 189k | st->frame_size = Fs/400; |
179 | | #ifdef ENABLE_DEEP_PLC |
180 | | lpcnet_plc_init( &st->lpcnet); |
181 | | #endif |
182 | 189k | st->arch = opus_select_arch(); |
183 | 189k | return OPUS_OK; |
184 | 189k | } |
185 | | |
186 | | OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error) |
187 | 0 | { |
188 | 0 | int ret; |
189 | 0 | OpusDecoder *st; |
190 | 0 | if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000 |
191 | | #ifdef ENABLE_QEXT |
192 | | &&Fs!=96000 |
193 | | #endif |
194 | 0 | ) |
195 | 0 | || (channels!=1&&channels!=2)) |
196 | 0 | { |
197 | 0 | if (error) |
198 | 0 | *error = OPUS_BAD_ARG; |
199 | 0 | return NULL; |
200 | 0 | } |
201 | 0 | st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels)); |
202 | 0 | if (st == NULL) |
203 | 0 | { |
204 | 0 | if (error) |
205 | 0 | *error = OPUS_ALLOC_FAIL; |
206 | 0 | return NULL; |
207 | 0 | } |
208 | 0 | ret = opus_decoder_init(st, Fs, channels); |
209 | 0 | if (error) |
210 | 0 | *error = ret; |
211 | 0 | if (ret != OPUS_OK) |
212 | 0 | { |
213 | 0 | opus_free(st); |
214 | 0 | st = NULL; |
215 | 0 | } |
216 | 0 | return st; |
217 | 0 | } |
218 | | |
219 | | #ifdef ENABLE_RES24 |
220 | | static void smooth_fade(const opus_res *in1, const opus_res *in2, |
221 | | opus_res *out, int overlap, int channels, |
222 | | const celt_coef *window, opus_int32 Fs) |
223 | 50.7k | { |
224 | 50.7k | int i, c; |
225 | 50.7k | int inc = 48000/Fs; |
226 | 130k | for (c=0;c<channels;c++) |
227 | 79.9k | { |
228 | 6.34M | for (i=0;i<overlap;i++) |
229 | 6.26M | { |
230 | 6.26M | celt_coef w = MULT_COEF(window[i*inc], window[i*inc]); |
231 | 6.26M | out[i*channels+c] = ADD32(MULT_COEF_32(w,in2[i*channels+c]), |
232 | 6.26M | MULT_COEF_32(COEF_ONE-w, in1[i*channels+c])); |
233 | 6.26M | } |
234 | 79.9k | } |
235 | 50.7k | } opus_decoder.c:smooth_fade Line | Count | Source | 223 | 25.3k | { | 224 | 25.3k | int i, c; | 225 | 25.3k | int inc = 48000/Fs; | 226 | 65.3k | for (c=0;c<channels;c++) | 227 | 39.9k | { | 228 | 3.17M | for (i=0;i<overlap;i++) | 229 | 3.13M | { | 230 | 3.13M | celt_coef w = MULT_COEF(window[i*inc], window[i*inc]); | 231 | 3.13M | out[i*channels+c] = ADD32(MULT_COEF_32(w,in2[i*channels+c]), | 232 | 3.13M | MULT_COEF_32(COEF_ONE-w, in1[i*channels+c])); | 233 | 3.13M | } | 234 | 39.9k | } | 235 | 25.3k | } |
opus_decoder.c:smooth_fade Line | Count | Source | 223 | 25.3k | { | 224 | 25.3k | int i, c; | 225 | 25.3k | int inc = 48000/Fs; | 226 | 65.3k | for (c=0;c<channels;c++) | 227 | 39.9k | { | 228 | 3.17M | for (i=0;i<overlap;i++) | 229 | 3.13M | { | 230 | 3.13M | celt_coef w = MULT_COEF(window[i*inc], window[i*inc]); | 231 | 3.13M | out[i*channels+c] = ADD32(MULT_COEF_32(w,in2[i*channels+c]), | 232 | 3.13M | MULT_COEF_32(COEF_ONE-w, in1[i*channels+c])); | 233 | 3.13M | } | 234 | 39.9k | } | 235 | 25.3k | } |
|
236 | | #else |
237 | | static void smooth_fade(const opus_res *in1, const opus_res *in2, |
238 | | opus_res *out, int overlap, int channels, |
239 | | const opus_val16 *window, opus_int32 Fs) |
240 | | { |
241 | | int i, c; |
242 | | int inc = 48000/Fs; |
243 | | for (c=0;c<channels;c++) |
244 | | { |
245 | | for (i=0;i<overlap;i++) |
246 | | { |
247 | | opus_val16 w = MULT16_16_Q15(window[i*inc], window[i*inc]); |
248 | | out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]), |
249 | | Q15ONE-w, in1[i*channels+c]), 15); |
250 | | } |
251 | | } |
252 | | } |
253 | | #endif |
254 | | |
255 | | static int opus_packet_get_mode(const unsigned char *data) |
256 | 285k | { |
257 | 285k | int mode; |
258 | 285k | if (data[0]&0x80) |
259 | 84.2k | { |
260 | 84.2k | mode = MODE_CELT_ONLY; |
261 | 201k | } else if ((data[0]&0x60) == 0x60) |
262 | 30.5k | { |
263 | 30.5k | mode = MODE_HYBRID; |
264 | 170k | } else { |
265 | 170k | mode = MODE_SILK_ONLY; |
266 | 170k | } |
267 | 285k | return mode; |
268 | 285k | } |
269 | | |
270 | | static int opus_decode_frame(OpusDecoder *st, const unsigned char *data, |
271 | | opus_int32 len, opus_res *pcm, int frame_size, int decode_fec ARG_QEXT(opus_extension_data *ext)) |
272 | 3.71M | { |
273 | 3.71M | void *silk_dec; |
274 | 3.71M | CELTDecoder *celt_dec; |
275 | 3.71M | int i, silk_ret=0, celt_ret=0; |
276 | 3.71M | ec_dec dec; |
277 | 3.71M | opus_int32 silk_frame_size; |
278 | 3.71M | int pcm_transition_silk_size; |
279 | 3.71M | VARDECL(opus_res, pcm_transition_silk); |
280 | 3.71M | int pcm_transition_celt_size; |
281 | 3.71M | VARDECL(opus_res, pcm_transition_celt); |
282 | 3.71M | opus_res *pcm_transition=NULL; |
283 | 3.71M | int redundant_audio_size; |
284 | 3.71M | VARDECL(opus_res, redundant_audio); |
285 | | |
286 | 3.71M | int audiosize; |
287 | 3.71M | int mode; |
288 | 3.71M | int bandwidth; |
289 | 3.71M | int transition=0; |
290 | 3.71M | int start_band; |
291 | 3.71M | int redundancy=0; |
292 | 3.71M | int redundancy_bytes = 0; |
293 | 3.71M | int celt_to_silk=0; |
294 | 3.71M | int c; |
295 | 3.71M | int F2_5, F5, F10, F20; |
296 | 3.71M | const celt_coef *window; |
297 | 3.71M | opus_uint32 redundant_rng = 0; |
298 | 3.71M | int celt_accum; |
299 | 3.71M | ALLOC_STACK; |
300 | | |
301 | 3.71M | silk_dec = (char*)st+st->silk_dec_offset; |
302 | 3.71M | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); |
303 | 3.71M | F20 = st->Fs/50; |
304 | 3.71M | F10 = F20>>1; |
305 | 3.71M | F5 = F10>>1; |
306 | 3.71M | F2_5 = F5>>1; |
307 | 3.71M | if (frame_size < F2_5) |
308 | 0 | { |
309 | 0 | RESTORE_STACK; |
310 | 0 | return OPUS_BUFFER_TOO_SMALL; |
311 | 0 | } |
312 | | /* Limit frame_size to avoid excessive stack allocations. */ |
313 | 3.71M | frame_size = IMIN(frame_size, st->Fs/25*3); |
314 | | /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */ |
315 | 3.71M | if (len<=1) |
316 | 3.28M | { |
317 | 3.28M | data = NULL; |
318 | | /* In that case, don't conceal more than what the ToC says */ |
319 | 3.28M | frame_size = IMIN(frame_size, st->frame_size); |
320 | 3.28M | } |
321 | 3.71M | if (data != NULL) |
322 | 428k | { |
323 | 428k | audiosize = st->frame_size; |
324 | 428k | mode = st->mode; |
325 | 428k | bandwidth = st->bandwidth; |
326 | 428k | ec_dec_init(&dec,(unsigned char*)data,len); |
327 | 3.28M | } else { |
328 | 3.28M | audiosize = frame_size; |
329 | | /* Run PLC using last used mode (CELT if we ended with CELT redundancy) */ |
330 | 3.28M | mode = st->prev_redundancy ? MODE_CELT_ONLY : st->prev_mode; |
331 | 3.28M | bandwidth = 0; |
332 | | |
333 | 3.28M | if (mode == 0) |
334 | 3.02M | { |
335 | | /* If we haven't got any packet yet, all we can do is return zeros */ |
336 | 287M | for (i=0;i<audiosize*st->channels;i++) |
337 | 284M | pcm[i] = 0; |
338 | 3.02M | RESTORE_STACK; |
339 | 3.02M | return audiosize; |
340 | 3.02M | } |
341 | | |
342 | | /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT), |
343 | | 10, or 20 (e.g. 12.5 or 30 ms). */ |
344 | 263k | if (audiosize > F20) |
345 | 12.3k | { |
346 | 36.5k | do { |
347 | 36.5k | int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0 ARG_QEXT(NULL)); |
348 | 36.5k | if (ret<0) |
349 | 0 | { |
350 | 0 | RESTORE_STACK; |
351 | 0 | return ret; |
352 | 0 | } |
353 | 36.5k | pcm += ret*st->channels; |
354 | 36.5k | audiosize -= ret; |
355 | 36.5k | } while (audiosize > 0); |
356 | 12.3k | RESTORE_STACK; |
357 | 12.3k | return frame_size; |
358 | 251k | } else if (audiosize < F20) |
359 | 202k | { |
360 | 202k | if (audiosize > F10) |
361 | 0 | audiosize = F10; |
362 | 202k | else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10) |
363 | 0 | audiosize = F5; |
364 | 202k | } |
365 | 263k | } |
366 | | |
367 | | /* In fixed-point, we can tell CELT to do the accumulation on top of the |
368 | | SILK PCM buffer. This saves some stack space. */ |
369 | 680k | celt_accum = (mode != MODE_CELT_ONLY); |
370 | | |
371 | 680k | pcm_transition_silk_size = ALLOC_NONE; |
372 | 680k | pcm_transition_celt_size = ALLOC_NONE; |
373 | 680k | if (data!=NULL && st->prev_mode > 0 && ( |
374 | 215k | (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy) |
375 | 215k | || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) |
376 | 680k | ) |
377 | 412 | { |
378 | 412 | transition = 1; |
379 | | /* Decide where to allocate the stack memory for pcm_transition */ |
380 | 412 | if (mode == MODE_CELT_ONLY) |
381 | 0 | pcm_transition_celt_size = F5*st->channels; |
382 | 412 | else |
383 | 412 | pcm_transition_silk_size = F5*st->channels; |
384 | 412 | } |
385 | 680k | ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_res); |
386 | 680k | if (transition && mode == MODE_CELT_ONLY) |
387 | 0 | { |
388 | 0 | pcm_transition = pcm_transition_celt; |
389 | 0 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); |
390 | 0 | } |
391 | 680k | if (audiosize > frame_size) |
392 | 0 | { |
393 | | /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/ |
394 | 0 | RESTORE_STACK; |
395 | 0 | return OPUS_BAD_ARG; |
396 | 680k | } else { |
397 | 680k | frame_size = audiosize; |
398 | 680k | } |
399 | | |
400 | | /* SILK processing */ |
401 | 680k | if (mode != MODE_CELT_ONLY) |
402 | 264k | { |
403 | 264k | int lost_flag, decoded_samples; |
404 | 264k | opus_res *pcm_ptr; |
405 | 264k | int pcm_too_small; |
406 | 264k | int pcm_silk_size = ALLOC_NONE; |
407 | 264k | VARDECL(opus_res, pcm_silk); |
408 | 264k | pcm_too_small = (frame_size < F10); |
409 | 264k | if (pcm_too_small) |
410 | 0 | pcm_silk_size = F10*st->channels; |
411 | 264k | ALLOC(pcm_silk, pcm_silk_size, opus_res); |
412 | 264k | if (pcm_too_small) |
413 | 0 | pcm_ptr = pcm_silk; |
414 | 264k | else |
415 | 264k | pcm_ptr = pcm; |
416 | | |
417 | 264k | if (st->prev_mode==MODE_CELT_ONLY) |
418 | 412 | silk_ResetDecoder( silk_dec ); |
419 | | |
420 | | /* The SILK PLC cannot produce frames of less than 10 ms */ |
421 | 264k | st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs); |
422 | | |
423 | 264k | if (data != NULL) |
424 | 190k | { |
425 | 190k | st->DecControl.nChannelsInternal = st->stream_channels; |
426 | 190k | if( mode == MODE_SILK_ONLY ) { |
427 | 148k | if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) { |
428 | 68.3k | st->DecControl.internalSampleRate = 8000; |
429 | 80.3k | } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) { |
430 | 47.5k | st->DecControl.internalSampleRate = 12000; |
431 | 47.5k | } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) { |
432 | 32.8k | st->DecControl.internalSampleRate = 16000; |
433 | 32.8k | } else { |
434 | 0 | st->DecControl.internalSampleRate = 16000; |
435 | 0 | celt_assert( 0 ); |
436 | 0 | } |
437 | 148k | } else { |
438 | | /* Hybrid mode */ |
439 | 42.1k | st->DecControl.internalSampleRate = 16000; |
440 | 42.1k | } |
441 | 190k | } |
442 | 264k | st->DecControl.enable_deep_plc = st->complexity >= 5; |
443 | | #ifdef ENABLE_OSCE |
444 | | st->DecControl.osce_method = OSCE_METHOD_NONE; |
445 | | #ifndef DISABLE_LACE |
446 | | if (st->complexity >= 6) {st->DecControl.osce_method = OSCE_METHOD_LACE;} |
447 | | #endif |
448 | | #ifndef DISABLE_NOLACE |
449 | | if (st->complexity >= 7) {st->DecControl.osce_method = OSCE_METHOD_NOLACE;} |
450 | | #endif |
451 | | #ifdef ENABLE_OSCE_BWE |
452 | | if (st->complexity >= 4 && st->DecControl.enable_osce_bwe && |
453 | | st->Fs == 48000 && st->DecControl.internalSampleRate == 16000 && |
454 | | ((mode == MODE_SILK_ONLY) || (data == NULL))) { |
455 | | /* request WB -> FB signal extension */ |
456 | | st->DecControl.osce_extended_mode = OSCE_MODE_SILK_BBWE; |
457 | | } else { |
458 | | /* at this point, mode can only be MODE_SILK_ONLY or MODE_HYBRID */ |
459 | | st->DecControl.osce_extended_mode = mode == MODE_SILK_ONLY ? OSCE_MODE_SILK_ONLY : OSCE_MODE_HYBRID; |
460 | | } |
461 | | if (st->prev_mode == MODE_CELT_ONLY) { |
462 | | /* Update extended mode for CELT->SILK transition */ |
463 | | st->DecControl.prev_osce_extended_mode = OSCE_MODE_CELT_ONLY; |
464 | | } |
465 | | #endif |
466 | | #endif |
467 | | |
468 | 264k | lost_flag = data == NULL ? 1 : 2 * !!decode_fec; |
469 | 264k | decoded_samples = 0; |
470 | 406k | do { |
471 | | /* Call SILK decoder */ |
472 | 406k | int first_frame = decoded_samples == 0; |
473 | 406k | silk_ret = silk_Decode( silk_dec, &st->DecControl, |
474 | 406k | lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, |
475 | | #ifdef ENABLE_DEEP_PLC |
476 | | &st->lpcnet, |
477 | | #endif |
478 | 406k | st->arch ); |
479 | 406k | if( silk_ret ) { |
480 | 0 | if (lost_flag) { |
481 | | /* PLC failure should not be fatal */ |
482 | 0 | silk_frame_size = frame_size; |
483 | 0 | for (i=0;i<frame_size*st->channels;i++) |
484 | 0 | pcm_ptr[i] = 0; |
485 | 0 | } else { |
486 | 0 | RESTORE_STACK; |
487 | 0 | return OPUS_INTERNAL_ERROR; |
488 | 0 | } |
489 | 0 | } |
490 | 406k | pcm_ptr += silk_frame_size * st->channels; |
491 | 406k | decoded_samples += silk_frame_size; |
492 | 406k | } while( decoded_samples < frame_size ); |
493 | 264k | if (pcm_too_small) { |
494 | 0 | OPUS_COPY(pcm, pcm_silk, frame_size*st->channels); |
495 | 0 | } |
496 | 264k | } |
497 | | |
498 | 680k | start_band = 0; |
499 | 680k | if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL |
500 | 154k | && ec_tell(&dec)+17+20*(mode == MODE_HYBRID) <= 8*len) |
501 | 41.3k | { |
502 | | /* Check if we have a redundant 0-8 kHz band */ |
503 | 41.3k | if (mode == MODE_HYBRID) |
504 | 10.5k | redundancy = ec_dec_bit_logp(&dec, 12); |
505 | 30.7k | else |
506 | 30.7k | redundancy = 1; |
507 | 41.3k | if (redundancy) |
508 | 31.2k | { |
509 | 31.2k | celt_to_silk = ec_dec_bit_logp(&dec, 1); |
510 | | /* redundancy_bytes will be at least two, in the non-hybrid |
511 | | case due to the ec_tell() check above */ |
512 | 31.2k | redundancy_bytes = mode==MODE_HYBRID ? |
513 | 455 | (opus_int32)ec_dec_uint(&dec, 256)+2 : |
514 | 31.2k | len-((ec_tell(&dec)+7)>>3); |
515 | 31.2k | len -= redundancy_bytes; |
516 | | /* This is a sanity check. It should never happen for a valid |
517 | | packet, so the exact behaviour is not normative. */ |
518 | 31.2k | if (len*8 < ec_tell(&dec)) |
519 | 293 | { |
520 | 293 | len = 0; |
521 | 293 | redundancy_bytes = 0; |
522 | 293 | redundancy = 0; |
523 | 293 | } |
524 | | /* Shrink decoder because of raw bits */ |
525 | 31.2k | dec.storage -= redundancy_bytes; |
526 | 31.2k | } |
527 | 41.3k | } |
528 | 680k | if (mode != MODE_CELT_ONLY) |
529 | 264k | start_band = 17; |
530 | | |
531 | 680k | if (redundancy) |
532 | 30.9k | { |
533 | 30.9k | transition = 0; |
534 | 30.9k | pcm_transition_silk_size=ALLOC_NONE; |
535 | 30.9k | } |
536 | | |
537 | 680k | ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_res); |
538 | | |
539 | 680k | if (transition && mode != MODE_CELT_ONLY) |
540 | 301 | { |
541 | 301 | pcm_transition = pcm_transition_silk; |
542 | 301 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); |
543 | 301 | } |
544 | | |
545 | | |
546 | 680k | if (bandwidth) |
547 | 428k | { |
548 | 428k | int endband=21; |
549 | | |
550 | 428k | switch(bandwidth) |
551 | 428k | { |
552 | 108k | case OPUS_BANDWIDTH_NARROWBAND: |
553 | 108k | endband = 13; |
554 | 108k | break; |
555 | 47.5k | case OPUS_BANDWIDTH_MEDIUMBAND: |
556 | 158k | case OPUS_BANDWIDTH_WIDEBAND: |
557 | 158k | endband = 17; |
558 | 158k | break; |
559 | 49.8k | case OPUS_BANDWIDTH_SUPERWIDEBAND: |
560 | 49.8k | endband = 19; |
561 | 49.8k | break; |
562 | 112k | case OPUS_BANDWIDTH_FULLBAND: |
563 | 112k | endband = 21; |
564 | 112k | break; |
565 | 0 | default: |
566 | 0 | celt_assert(0); |
567 | 0 | break; |
568 | 428k | } |
569 | 428k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband))); |
570 | 428k | } |
571 | 680k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels))); |
572 | | |
573 | | /* Only allocation memory for redundancy if/when needed */ |
574 | 680k | redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE; |
575 | 680k | ALLOC(redundant_audio, redundant_audio_size, opus_res); |
576 | | |
577 | | /* 5 ms redundant frame for CELT->SILK*/ |
578 | 680k | if (redundancy && celt_to_silk) |
579 | 17.8k | { |
580 | | /* If the previous frame did not use CELT (the first redundancy frame in |
581 | | a transition from SILK may have been lost) then the CELT decoder is |
582 | | stale at this point and the redundancy audio is not useful, however |
583 | | the final range is still needed (for testing), so the redundancy is |
584 | | always decoded but the decoded audio may not be used */ |
585 | 17.8k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); |
586 | 17.8k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, |
587 | 17.8k | redundant_audio, F5, NULL, 0); |
588 | 17.8k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); |
589 | 17.8k | } |
590 | | |
591 | | /* MUST be after PLC */ |
592 | 680k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band))); |
593 | | |
594 | | #ifdef ENABLE_OSCE_BWE |
595 | | if (mode != MODE_SILK_ONLY && st->DecControl.osce_extended_mode != OSCE_MODE_SILK_BBWE) |
596 | | #else |
597 | 680k | if (mode != MODE_SILK_ONLY) |
598 | 485k | #endif |
599 | 485k | { |
600 | 485k | int celt_frame_size = IMIN(F20, frame_size); |
601 | | /* Make sure to discard any previous CELT state */ |
602 | 485k | if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy) |
603 | 485k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); |
604 | | /* Decode CELT */ |
605 | 485k | celt_ret = celt_decode_with_ec_dred(celt_dec, decode_fec ? NULL : data, |
606 | 485k | len, pcm, celt_frame_size, &dec, celt_accum |
607 | | #ifdef ENABLE_DEEP_PLC |
608 | | , &st->lpcnet |
609 | | #endif |
610 | 485k | ARG_QEXT(ext ? ext->data : NULL) ARG_QEXT(ext ? ext->len : 0)); |
611 | 485k | celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&st->rangeFinal)); |
612 | 485k | } else { |
613 | 194k | unsigned char silence[2] = {0xFF, 0xFF}; |
614 | 194k | if (!celt_accum) |
615 | 0 | { |
616 | 0 | for (i=0;i<frame_size*st->channels;i++) |
617 | 0 | pcm[i] = 0; |
618 | 0 | } |
619 | | /* For hybrid -> SILK transitions, we let the CELT MDCT |
620 | | do a fade-out by decoding a silence frame */ |
621 | 194k | if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) ) |
622 | 0 | { |
623 | 0 | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); |
624 | 0 | celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum); |
625 | 0 | } |
626 | 194k | st->rangeFinal = dec.rng; |
627 | 194k | } |
628 | | |
629 | 680k | { |
630 | 680k | const CELTMode *celt_mode; |
631 | 680k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode))); |
632 | 680k | window = celt_mode->window; |
633 | 680k | } |
634 | | |
635 | | /* 5 ms redundant frame for SILK->CELT */ |
636 | 680k | if (redundancy && !celt_to_silk) |
637 | 13.1k | { |
638 | 13.1k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); |
639 | 13.1k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); |
640 | | |
641 | 13.1k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0); |
642 | 13.1k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); |
643 | 13.1k | smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5, |
644 | 13.1k | pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs); |
645 | 13.1k | } |
646 | | /* 5ms redundant frame for CELT->SILK; ignore if the previous frame did not |
647 | | use CELT (the first redundancy frame in a transition from SILK may have |
648 | | been lost) */ |
649 | 680k | if (redundancy && celt_to_silk && (st->prev_mode != MODE_SILK_ONLY || st->prev_redundancy)) |
650 | 11.9k | { |
651 | 30.3k | for (c=0;c<st->channels;c++) |
652 | 18.3k | { |
653 | 1.52M | for (i=0;i<F2_5;i++) |
654 | 1.50M | pcm[st->channels*i+c] = redundant_audio[st->channels*i+c]; |
655 | 18.3k | } |
656 | 11.9k | smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5, |
657 | 11.9k | pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs); |
658 | 11.9k | } |
659 | 680k | if (transition) |
660 | 301 | { |
661 | 301 | if (audiosize >= F5) |
662 | 301 | { |
663 | 32.9k | for (i=0;i<st->channels*F2_5;i++) |
664 | 32.6k | pcm[i] = pcm_transition[i]; |
665 | 301 | smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5, |
666 | 301 | pcm+st->channels*F2_5, F2_5, |
667 | 301 | st->channels, window, st->Fs); |
668 | 301 | } else { |
669 | | /* Not enough time to do a clean transition, but we do it anyway |
670 | | This will not preserve amplitude perfectly and may introduce |
671 | | a bit of temporal aliasing, but it shouldn't be too bad and |
672 | | that's pretty much the best we can do. In any case, generating this |
673 | | transition it pretty silly in the first place */ |
674 | 0 | smooth_fade(pcm_transition, pcm, |
675 | 0 | pcm, F2_5, |
676 | 0 | st->channels, window, st->Fs); |
677 | 0 | } |
678 | 301 | } |
679 | | |
680 | 680k | if(st->decode_gain) |
681 | 0 | { |
682 | 0 | opus_val32 gain; |
683 | 0 | gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain)); |
684 | 0 | for (i=0;i<frame_size*st->channels;i++) |
685 | 0 | { |
686 | 0 | opus_val32 x; |
687 | 0 | #ifdef ENABLE_RES24 |
688 | 0 | x = MULT32_32_Q16(pcm[i],gain); |
689 | | #else |
690 | | x = MULT16_32_P16(pcm[i],gain); |
691 | | #endif |
692 | 0 | pcm[i] = SATURATE(x, 32767); |
693 | 0 | } |
694 | 0 | } |
695 | | |
696 | 680k | if (len <= 1) |
697 | 251k | st->rangeFinal = 0; |
698 | 428k | else |
699 | 428k | st->rangeFinal ^= redundant_rng; |
700 | | |
701 | 680k | st->prev_mode = mode; |
702 | 680k | st->prev_redundancy = redundancy && !celt_to_silk; |
703 | | |
704 | 680k | if (celt_ret>=0) |
705 | 680k | { |
706 | 680k | if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels)) |
707 | 0 | OPUS_PRINT_INT(audiosize); |
708 | 680k | } |
709 | | |
710 | 680k | RESTORE_STACK; |
711 | 680k | return celt_ret < 0 ? celt_ret : audiosize; |
712 | | |
713 | 680k | } opus_decoder.c:opus_decode_frame Line | Count | Source | 272 | 924k | { | 273 | 924k | void *silk_dec; | 274 | 924k | CELTDecoder *celt_dec; | 275 | 924k | int i, silk_ret=0, celt_ret=0; | 276 | 924k | ec_dec dec; | 277 | 924k | opus_int32 silk_frame_size; | 278 | 924k | int pcm_transition_silk_size; | 279 | 924k | VARDECL(opus_res, pcm_transition_silk); | 280 | 924k | int pcm_transition_celt_size; | 281 | 924k | VARDECL(opus_res, pcm_transition_celt); | 282 | 924k | opus_res *pcm_transition=NULL; | 283 | 924k | int redundant_audio_size; | 284 | 924k | VARDECL(opus_res, redundant_audio); | 285 | | | 286 | 924k | int audiosize; | 287 | 924k | int mode; | 288 | 924k | int bandwidth; | 289 | 924k | int transition=0; | 290 | 924k | int start_band; | 291 | 924k | int redundancy=0; | 292 | 924k | int redundancy_bytes = 0; | 293 | 924k | int celt_to_silk=0; | 294 | 924k | int c; | 295 | 924k | int F2_5, F5, F10, F20; | 296 | 924k | const celt_coef *window; | 297 | 924k | opus_uint32 redundant_rng = 0; | 298 | 924k | int celt_accum; | 299 | 924k | ALLOC_STACK; | 300 | | | 301 | 924k | silk_dec = (char*)st+st->silk_dec_offset; | 302 | 924k | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); | 303 | 924k | F20 = st->Fs/50; | 304 | 924k | F10 = F20>>1; | 305 | 924k | F5 = F10>>1; | 306 | 924k | F2_5 = F5>>1; | 307 | 924k | if (frame_size < F2_5) | 308 | 0 | { | 309 | 0 | RESTORE_STACK; | 310 | 0 | return OPUS_BUFFER_TOO_SMALL; | 311 | 0 | } | 312 | | /* Limit frame_size to avoid excessive stack allocations. */ | 313 | 924k | frame_size = IMIN(frame_size, st->Fs/25*3); | 314 | | /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */ | 315 | 924k | if (len<=1) | 316 | 811k | { | 317 | 811k | data = NULL; | 318 | | /* In that case, don't conceal more than what the ToC says */ | 319 | 811k | frame_size = IMIN(frame_size, st->frame_size); | 320 | 811k | } | 321 | 924k | if (data != NULL) | 322 | 113k | { | 323 | 113k | audiosize = st->frame_size; | 324 | 113k | mode = st->mode; | 325 | 113k | bandwidth = st->bandwidth; | 326 | 113k | ec_dec_init(&dec,(unsigned char*)data,len); | 327 | 811k | } else { | 328 | 811k | audiosize = frame_size; | 329 | | /* Run PLC using last used mode (CELT if we ended with CELT redundancy) */ | 330 | 811k | mode = st->prev_redundancy ? MODE_CELT_ONLY : st->prev_mode; | 331 | 811k | bandwidth = 0; | 332 | | | 333 | 811k | if (mode == 0) | 334 | 729k | { | 335 | | /* If we haven't got any packet yet, all we can do is return zeros */ | 336 | 58.5M | for (i=0;i<audiosize*st->channels;i++) | 337 | 57.7M | pcm[i] = 0; | 338 | 729k | RESTORE_STACK; | 339 | 729k | return audiosize; | 340 | 729k | } | 341 | | | 342 | | /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT), | 343 | | 10, or 20 (e.g. 12.5 or 30 ms). */ | 344 | 81.2k | if (audiosize > F20) | 345 | 3.47k | { | 346 | 10.3k | do { | 347 | 10.3k | int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0 ARG_QEXT(NULL)); | 348 | 10.3k | if (ret<0) | 349 | 0 | { | 350 | 0 | RESTORE_STACK; | 351 | 0 | return ret; | 352 | 0 | } | 353 | 10.3k | pcm += ret*st->channels; | 354 | 10.3k | audiosize -= ret; | 355 | 10.3k | } while (audiosize > 0); | 356 | 3.47k | RESTORE_STACK; | 357 | 3.47k | return frame_size; | 358 | 77.8k | } else if (audiosize < F20) | 359 | 64.7k | { | 360 | 64.7k | if (audiosize > F10) | 361 | 0 | audiosize = F10; | 362 | 64.7k | else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10) | 363 | 0 | audiosize = F5; | 364 | 64.7k | } | 365 | 81.2k | } | 366 | | | 367 | | /* In fixed-point, we can tell CELT to do the accumulation on top of the | 368 | | SILK PCM buffer. This saves some stack space. */ | 369 | 190k | celt_accum = (mode != MODE_CELT_ONLY); | 370 | | | 371 | 190k | pcm_transition_silk_size = ALLOC_NONE; | 372 | 190k | pcm_transition_celt_size = ALLOC_NONE; | 373 | 190k | if (data!=NULL && st->prev_mode > 0 && ( | 374 | 58.3k | (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy) | 375 | 58.3k | || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) | 376 | 190k | ) | 377 | 128 | { | 378 | 128 | transition = 1; | 379 | | /* Decide where to allocate the stack memory for pcm_transition */ | 380 | 128 | if (mode == MODE_CELT_ONLY) | 381 | 0 | pcm_transition_celt_size = F5*st->channels; | 382 | 128 | else | 383 | 128 | pcm_transition_silk_size = F5*st->channels; | 384 | 128 | } | 385 | 190k | ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_res); | 386 | 190k | if (transition && mode == MODE_CELT_ONLY) | 387 | 0 | { | 388 | 0 | pcm_transition = pcm_transition_celt; | 389 | 0 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); | 390 | 0 | } | 391 | 190k | if (audiosize > frame_size) | 392 | 0 | { | 393 | | /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/ | 394 | 0 | RESTORE_STACK; | 395 | 0 | return OPUS_BAD_ARG; | 396 | 190k | } else { | 397 | 190k | frame_size = audiosize; | 398 | 190k | } | 399 | | | 400 | | /* SILK processing */ | 401 | 190k | if (mode != MODE_CELT_ONLY) | 402 | 68.3k | { | 403 | 68.3k | int lost_flag, decoded_samples; | 404 | 68.3k | opus_res *pcm_ptr; | 405 | 68.3k | int pcm_too_small; | 406 | 68.3k | int pcm_silk_size = ALLOC_NONE; | 407 | 68.3k | VARDECL(opus_res, pcm_silk); | 408 | 68.3k | pcm_too_small = (frame_size < F10); | 409 | 68.3k | if (pcm_too_small) | 410 | 0 | pcm_silk_size = F10*st->channels; | 411 | 68.3k | ALLOC(pcm_silk, pcm_silk_size, opus_res); | 412 | 68.3k | if (pcm_too_small) | 413 | 0 | pcm_ptr = pcm_silk; | 414 | 68.3k | else | 415 | 68.3k | pcm_ptr = pcm; | 416 | | | 417 | 68.3k | if (st->prev_mode==MODE_CELT_ONLY) | 418 | 128 | silk_ResetDecoder( silk_dec ); | 419 | | | 420 | | /* The SILK PLC cannot produce frames of less than 10 ms */ | 421 | 68.3k | st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs); | 422 | | | 423 | 68.3k | if (data != NULL) | 424 | 46.3k | { | 425 | 46.3k | st->DecControl.nChannelsInternal = st->stream_channels; | 426 | 46.3k | if( mode == MODE_SILK_ONLY ) { | 427 | 35.6k | if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) { | 428 | 14.7k | st->DecControl.internalSampleRate = 8000; | 429 | 20.9k | } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) { | 430 | 13.5k | st->DecControl.internalSampleRate = 12000; | 431 | 13.5k | } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) { | 432 | 7.31k | st->DecControl.internalSampleRate = 16000; | 433 | 7.31k | } else { | 434 | 0 | st->DecControl.internalSampleRate = 16000; | 435 | 0 | celt_assert( 0 ); | 436 | 0 | } | 437 | 35.6k | } else { | 438 | | /* Hybrid mode */ | 439 | 10.6k | st->DecControl.internalSampleRate = 16000; | 440 | 10.6k | } | 441 | 46.3k | } | 442 | 68.3k | st->DecControl.enable_deep_plc = st->complexity >= 5; | 443 | | #ifdef ENABLE_OSCE | 444 | | st->DecControl.osce_method = OSCE_METHOD_NONE; | 445 | | #ifndef DISABLE_LACE | 446 | | if (st->complexity >= 6) {st->DecControl.osce_method = OSCE_METHOD_LACE;} | 447 | | #endif | 448 | | #ifndef DISABLE_NOLACE | 449 | | if (st->complexity >= 7) {st->DecControl.osce_method = OSCE_METHOD_NOLACE;} | 450 | | #endif | 451 | | #ifdef ENABLE_OSCE_BWE | 452 | | if (st->complexity >= 4 && st->DecControl.enable_osce_bwe && | 453 | | st->Fs == 48000 && st->DecControl.internalSampleRate == 16000 && | 454 | | ((mode == MODE_SILK_ONLY) || (data == NULL))) { | 455 | | /* request WB -> FB signal extension */ | 456 | | st->DecControl.osce_extended_mode = OSCE_MODE_SILK_BBWE; | 457 | | } else { | 458 | | /* at this point, mode can only be MODE_SILK_ONLY or MODE_HYBRID */ | 459 | | st->DecControl.osce_extended_mode = mode == MODE_SILK_ONLY ? OSCE_MODE_SILK_ONLY : OSCE_MODE_HYBRID; | 460 | | } | 461 | | if (st->prev_mode == MODE_CELT_ONLY) { | 462 | | /* Update extended mode for CELT->SILK transition */ | 463 | | st->DecControl.prev_osce_extended_mode = OSCE_MODE_CELT_ONLY; | 464 | | } | 465 | | #endif | 466 | | #endif | 467 | | | 468 | 68.3k | lost_flag = data == NULL ? 1 : 2 * !!decode_fec; | 469 | 68.3k | decoded_samples = 0; | 470 | 105k | do { | 471 | | /* Call SILK decoder */ | 472 | 105k | int first_frame = decoded_samples == 0; | 473 | 105k | silk_ret = silk_Decode( silk_dec, &st->DecControl, | 474 | 105k | lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, | 475 | | #ifdef ENABLE_DEEP_PLC | 476 | | &st->lpcnet, | 477 | | #endif | 478 | 105k | st->arch ); | 479 | 105k | if( silk_ret ) { | 480 | 0 | if (lost_flag) { | 481 | | /* PLC failure should not be fatal */ | 482 | 0 | silk_frame_size = frame_size; | 483 | 0 | for (i=0;i<frame_size*st->channels;i++) | 484 | 0 | pcm_ptr[i] = 0; | 485 | 0 | } else { | 486 | 0 | RESTORE_STACK; | 487 | 0 | return OPUS_INTERNAL_ERROR; | 488 | 0 | } | 489 | 0 | } | 490 | 105k | pcm_ptr += silk_frame_size * st->channels; | 491 | 105k | decoded_samples += silk_frame_size; | 492 | 105k | } while( decoded_samples < frame_size ); | 493 | 68.3k | if (pcm_too_small) { | 494 | 0 | OPUS_COPY(pcm, pcm_silk, frame_size*st->channels); | 495 | 0 | } | 496 | 68.3k | } | 497 | | | 498 | 190k | start_band = 0; | 499 | 190k | if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL | 500 | 37.3k | && ec_tell(&dec)+17+20*(mode == MODE_HYBRID) <= 8*len) | 501 | 9.33k | { | 502 | | /* Check if we have a redundant 0-8 kHz band */ | 503 | 9.33k | if (mode == MODE_HYBRID) | 504 | 1.92k | redundancy = ec_dec_bit_logp(&dec, 12); | 505 | 7.41k | else | 506 | 7.41k | redundancy = 1; | 507 | 9.33k | if (redundancy) | 508 | 7.52k | { | 509 | 7.52k | celt_to_silk = ec_dec_bit_logp(&dec, 1); | 510 | | /* redundancy_bytes will be at least two, in the non-hybrid | 511 | | case due to the ec_tell() check above */ | 512 | 7.52k | redundancy_bytes = mode==MODE_HYBRID ? | 513 | 110 | (opus_int32)ec_dec_uint(&dec, 256)+2 : | 514 | 7.52k | len-((ec_tell(&dec)+7)>>3); | 515 | 7.52k | len -= redundancy_bytes; | 516 | | /* This is a sanity check. It should never happen for a valid | 517 | | packet, so the exact behaviour is not normative. */ | 518 | 7.52k | if (len*8 < ec_tell(&dec)) | 519 | 73 | { | 520 | 73 | len = 0; | 521 | 73 | redundancy_bytes = 0; | 522 | 73 | redundancy = 0; | 523 | 73 | } | 524 | | /* Shrink decoder because of raw bits */ | 525 | 7.52k | dec.storage -= redundancy_bytes; | 526 | 7.52k | } | 527 | 9.33k | } | 528 | 190k | if (mode != MODE_CELT_ONLY) | 529 | 68.3k | start_band = 17; | 530 | | | 531 | 190k | if (redundancy) | 532 | 7.45k | { | 533 | 7.45k | transition = 0; | 534 | 7.45k | pcm_transition_silk_size=ALLOC_NONE; | 535 | 7.45k | } | 536 | | | 537 | 190k | ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_res); | 538 | | | 539 | 190k | if (transition && mode != MODE_CELT_ONLY) | 540 | 92 | { | 541 | 92 | pcm_transition = pcm_transition_silk; | 542 | 92 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); | 543 | 92 | } | 544 | | | 545 | | | 546 | 190k | if (bandwidth) | 547 | 113k | { | 548 | 113k | int endband=21; | 549 | | | 550 | 113k | switch(bandwidth) | 551 | 113k | { | 552 | 23.9k | case OPUS_BANDWIDTH_NARROWBAND: | 553 | 23.9k | endband = 13; | 554 | 23.9k | break; | 555 | 13.5k | case OPUS_BANDWIDTH_MEDIUMBAND: | 556 | 41.9k | case OPUS_BANDWIDTH_WIDEBAND: | 557 | 41.9k | endband = 17; | 558 | 41.9k | break; | 559 | 16.7k | case OPUS_BANDWIDTH_SUPERWIDEBAND: | 560 | 16.7k | endband = 19; | 561 | 16.7k | break; | 562 | 30.5k | case OPUS_BANDWIDTH_FULLBAND: | 563 | 30.5k | endband = 21; | 564 | 30.5k | break; | 565 | 0 | default: | 566 | 0 | celt_assert(0); | 567 | 0 | break; | 568 | 113k | } | 569 | 113k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband))); | 570 | 113k | } | 571 | 190k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels))); | 572 | | | 573 | | /* Only allocation memory for redundancy if/when needed */ | 574 | 190k | redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE; | 575 | 190k | ALLOC(redundant_audio, redundant_audio_size, opus_res); | 576 | | | 577 | | /* 5 ms redundant frame for CELT->SILK*/ | 578 | 190k | if (redundancy && celt_to_silk) | 579 | 4.57k | { | 580 | | /* If the previous frame did not use CELT (the first redundancy frame in | 581 | | a transition from SILK may have been lost) then the CELT decoder is | 582 | | stale at this point and the redundancy audio is not useful, however | 583 | | the final range is still needed (for testing), so the redundancy is | 584 | | always decoded but the decoded audio may not be used */ | 585 | 4.57k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 586 | 4.57k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, | 587 | 4.57k | redundant_audio, F5, NULL, 0); | 588 | 4.57k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); | 589 | 4.57k | } | 590 | | | 591 | | /* MUST be after PLC */ | 592 | 190k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band))); | 593 | | | 594 | | #ifdef ENABLE_OSCE_BWE | 595 | | if (mode != MODE_SILK_ONLY && st->DecControl.osce_extended_mode != OSCE_MODE_SILK_BBWE) | 596 | | #else | 597 | 190k | if (mode != MODE_SILK_ONLY) | 598 | 142k | #endif | 599 | 142k | { | 600 | 142k | int celt_frame_size = IMIN(F20, frame_size); | 601 | | /* Make sure to discard any previous CELT state */ | 602 | 142k | if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy) | 603 | 142k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); | 604 | | /* Decode CELT */ | 605 | 142k | celt_ret = celt_decode_with_ec_dred(celt_dec, decode_fec ? NULL : data, | 606 | 142k | len, pcm, celt_frame_size, &dec, celt_accum | 607 | | #ifdef ENABLE_DEEP_PLC | 608 | | , &st->lpcnet | 609 | | #endif | 610 | 142k | ARG_QEXT(ext ? ext->data : NULL) ARG_QEXT(ext ? ext->len : 0)); | 611 | 142k | celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&st->rangeFinal)); | 612 | 142k | } else { | 613 | 48.7k | unsigned char silence[2] = {0xFF, 0xFF}; | 614 | 48.7k | if (!celt_accum) | 615 | 0 | { | 616 | 0 | for (i=0;i<frame_size*st->channels;i++) | 617 | 0 | pcm[i] = 0; | 618 | 0 | } | 619 | | /* For hybrid -> SILK transitions, we let the CELT MDCT | 620 | | do a fade-out by decoding a silence frame */ | 621 | 48.7k | if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) ) | 622 | 0 | { | 623 | 0 | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 624 | 0 | celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum); | 625 | 0 | } | 626 | 48.7k | st->rangeFinal = dec.rng; | 627 | 48.7k | } | 628 | | | 629 | 190k | { | 630 | 190k | const CELTMode *celt_mode; | 631 | 190k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode))); | 632 | 190k | window = celt_mode->window; | 633 | 190k | } | 634 | | | 635 | | /* 5 ms redundant frame for SILK->CELT */ | 636 | 190k | if (redundancy && !celt_to_silk) | 637 | 2.87k | { | 638 | 2.87k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); | 639 | 2.87k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 640 | | | 641 | 2.87k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0); | 642 | 2.87k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); | 643 | 2.87k | smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5, | 644 | 2.87k | pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs); | 645 | 2.87k | } | 646 | | /* 5ms redundant frame for CELT->SILK; ignore if the previous frame did not | 647 | | use CELT (the first redundancy frame in a transition from SILK may have | 648 | | been lost) */ | 649 | 190k | if (redundancy && celt_to_silk && (st->prev_mode != MODE_SILK_ONLY || st->prev_redundancy)) | 650 | 3.12k | { | 651 | 7.69k | for (c=0;c<st->channels;c++) | 652 | 4.56k | { | 653 | 207k | for (i=0;i<F2_5;i++) | 654 | 203k | pcm[st->channels*i+c] = redundant_audio[st->channels*i+c]; | 655 | 4.56k | } | 656 | 3.12k | smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5, | 657 | 3.12k | pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs); | 658 | 3.12k | } | 659 | 190k | if (transition) | 660 | 92 | { | 661 | 92 | if (audiosize >= F5) | 662 | 92 | { | 663 | 6.55k | for (i=0;i<st->channels*F2_5;i++) | 664 | 6.46k | pcm[i] = pcm_transition[i]; | 665 | 92 | smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5, | 666 | 92 | pcm+st->channels*F2_5, F2_5, | 667 | 92 | st->channels, window, st->Fs); | 668 | 92 | } else { | 669 | | /* Not enough time to do a clean transition, but we do it anyway | 670 | | This will not preserve amplitude perfectly and may introduce | 671 | | a bit of temporal aliasing, but it shouldn't be too bad and | 672 | | that's pretty much the best we can do. In any case, generating this | 673 | | transition it pretty silly in the first place */ | 674 | 0 | smooth_fade(pcm_transition, pcm, | 675 | 0 | pcm, F2_5, | 676 | 0 | st->channels, window, st->Fs); | 677 | 0 | } | 678 | 92 | } | 679 | | | 680 | 190k | if(st->decode_gain) | 681 | 0 | { | 682 | 0 | opus_val32 gain; | 683 | 0 | gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain)); | 684 | 0 | for (i=0;i<frame_size*st->channels;i++) | 685 | 0 | { | 686 | 0 | opus_val32 x; | 687 | 0 | #ifdef ENABLE_RES24 | 688 | 0 | x = MULT32_32_Q16(pcm[i],gain); | 689 | | #else | 690 | | x = MULT16_32_P16(pcm[i],gain); | 691 | | #endif | 692 | 0 | pcm[i] = SATURATE(x, 32767); | 693 | 0 | } | 694 | 0 | } | 695 | | | 696 | 190k | if (len <= 1) | 697 | 77.8k | st->rangeFinal = 0; | 698 | 113k | else | 699 | 113k | st->rangeFinal ^= redundant_rng; | 700 | | | 701 | 190k | st->prev_mode = mode; | 702 | 190k | st->prev_redundancy = redundancy && !celt_to_silk; | 703 | | | 704 | 190k | if (celt_ret>=0) | 705 | 190k | { | 706 | 190k | if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels)) | 707 | 0 | OPUS_PRINT_INT(audiosize); | 708 | 190k | } | 709 | | | 710 | 190k | RESTORE_STACK; | 711 | 190k | return celt_ret < 0 ? celt_ret : audiosize; | 712 | | | 713 | 190k | } |
opus_decoder.c:opus_decode_frame Line | Count | Source | 272 | 1.03M | { | 273 | 1.03M | void *silk_dec; | 274 | 1.03M | CELTDecoder *celt_dec; | 275 | 1.03M | int i, silk_ret=0, celt_ret=0; | 276 | 1.03M | ec_dec dec; | 277 | 1.03M | opus_int32 silk_frame_size; | 278 | 1.03M | int pcm_transition_silk_size; | 279 | 1.03M | VARDECL(opus_res, pcm_transition_silk); | 280 | 1.03M | int pcm_transition_celt_size; | 281 | 1.03M | VARDECL(opus_res, pcm_transition_celt); | 282 | 1.03M | opus_res *pcm_transition=NULL; | 283 | 1.03M | int redundant_audio_size; | 284 | 1.03M | VARDECL(opus_res, redundant_audio); | 285 | | | 286 | 1.03M | int audiosize; | 287 | 1.03M | int mode; | 288 | 1.03M | int bandwidth; | 289 | 1.03M | int transition=0; | 290 | 1.03M | int start_band; | 291 | 1.03M | int redundancy=0; | 292 | 1.03M | int redundancy_bytes = 0; | 293 | 1.03M | int celt_to_silk=0; | 294 | 1.03M | int c; | 295 | 1.03M | int F2_5, F5, F10, F20; | 296 | 1.03M | const celt_coef *window; | 297 | 1.03M | opus_uint32 redundant_rng = 0; | 298 | 1.03M | int celt_accum; | 299 | 1.03M | ALLOC_STACK; | 300 | | | 301 | 1.03M | silk_dec = (char*)st+st->silk_dec_offset; | 302 | 1.03M | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); | 303 | 1.03M | F20 = st->Fs/50; | 304 | 1.03M | F10 = F20>>1; | 305 | 1.03M | F5 = F10>>1; | 306 | 1.03M | F2_5 = F5>>1; | 307 | 1.03M | if (frame_size < F2_5) | 308 | 0 | { | 309 | 0 | RESTORE_STACK; | 310 | 0 | return OPUS_BUFFER_TOO_SMALL; | 311 | 0 | } | 312 | | /* Limit frame_size to avoid excessive stack allocations. */ | 313 | 1.03M | frame_size = IMIN(frame_size, st->Fs/25*3); | 314 | | /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */ | 315 | 1.03M | if (len<=1) | 316 | 914k | { | 317 | 914k | data = NULL; | 318 | | /* In that case, don't conceal more than what the ToC says */ | 319 | 914k | frame_size = IMIN(frame_size, st->frame_size); | 320 | 914k | } | 321 | 1.03M | if (data != NULL) | 322 | 116k | { | 323 | 116k | audiosize = st->frame_size; | 324 | 116k | mode = st->mode; | 325 | 116k | bandwidth = st->bandwidth; | 326 | 116k | ec_dec_init(&dec,(unsigned char*)data,len); | 327 | 914k | } else { | 328 | 914k | audiosize = frame_size; | 329 | | /* Run PLC using last used mode (CELT if we ended with CELT redundancy) */ | 330 | 914k | mode = st->prev_redundancy ? MODE_CELT_ONLY : st->prev_mode; | 331 | 914k | bandwidth = 0; | 332 | | | 333 | 914k | if (mode == 0) | 334 | 852k | { | 335 | | /* If we haven't got any packet yet, all we can do is return zeros */ | 336 | 104M | for (i=0;i<audiosize*st->channels;i++) | 337 | 103M | pcm[i] = 0; | 338 | 852k | RESTORE_STACK; | 339 | 852k | return audiosize; | 340 | 852k | } | 341 | | | 342 | | /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT), | 343 | | 10, or 20 (e.g. 12.5 or 30 ms). */ | 344 | 62.0k | if (audiosize > F20) | 345 | 1.75k | { | 346 | 5.15k | do { | 347 | 5.15k | int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0 ARG_QEXT(NULL)); | 348 | 5.15k | if (ret<0) | 349 | 0 | { | 350 | 0 | RESTORE_STACK; | 351 | 0 | return ret; | 352 | 0 | } | 353 | 5.15k | pcm += ret*st->channels; | 354 | 5.15k | audiosize -= ret; | 355 | 5.15k | } while (audiosize > 0); | 356 | 1.75k | RESTORE_STACK; | 357 | 1.75k | return frame_size; | 358 | 60.3k | } else if (audiosize < F20) | 359 | 52.7k | { | 360 | 52.7k | if (audiosize > F10) | 361 | 0 | audiosize = F10; | 362 | 52.7k | else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10) | 363 | 0 | audiosize = F5; | 364 | 52.7k | } | 365 | 62.0k | } | 366 | | | 367 | | /* In fixed-point, we can tell CELT to do the accumulation on top of the | 368 | | SILK PCM buffer. This saves some stack space. */ | 369 | 176k | celt_accum = (mode != MODE_CELT_ONLY); | 370 | | | 371 | 176k | pcm_transition_silk_size = ALLOC_NONE; | 372 | 176k | pcm_transition_celt_size = ALLOC_NONE; | 373 | 176k | if (data!=NULL && st->prev_mode > 0 && ( | 374 | 57.1k | (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy) | 375 | 57.1k | || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) | 376 | 176k | ) | 377 | 79 | { | 378 | 79 | transition = 1; | 379 | | /* Decide where to allocate the stack memory for pcm_transition */ | 380 | 79 | if (mode == MODE_CELT_ONLY) | 381 | 0 | pcm_transition_celt_size = F5*st->channels; | 382 | 79 | else | 383 | 79 | pcm_transition_silk_size = F5*st->channels; | 384 | 79 | } | 385 | 176k | ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_res); | 386 | 176k | if (transition && mode == MODE_CELT_ONLY) | 387 | 0 | { | 388 | 0 | pcm_transition = pcm_transition_celt; | 389 | 0 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); | 390 | 0 | } | 391 | 176k | if (audiosize > frame_size) | 392 | 0 | { | 393 | | /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/ | 394 | 0 | RESTORE_STACK; | 395 | 0 | return OPUS_BAD_ARG; | 396 | 176k | } else { | 397 | 176k | frame_size = audiosize; | 398 | 176k | } | 399 | | | 400 | | /* SILK processing */ | 401 | 176k | if (mode != MODE_CELT_ONLY) | 402 | 64.2k | { | 403 | 64.2k | int lost_flag, decoded_samples; | 404 | 64.2k | opus_res *pcm_ptr; | 405 | 64.2k | int pcm_too_small; | 406 | 64.2k | int pcm_silk_size = ALLOC_NONE; | 407 | 64.2k | VARDECL(opus_res, pcm_silk); | 408 | 64.2k | pcm_too_small = (frame_size < F10); | 409 | 64.2k | if (pcm_too_small) | 410 | 0 | pcm_silk_size = F10*st->channels; | 411 | 64.2k | ALLOC(pcm_silk, pcm_silk_size, opus_res); | 412 | 64.2k | if (pcm_too_small) | 413 | 0 | pcm_ptr = pcm_silk; | 414 | 64.2k | else | 415 | 64.2k | pcm_ptr = pcm; | 416 | | | 417 | 64.2k | if (st->prev_mode==MODE_CELT_ONLY) | 418 | 79 | silk_ResetDecoder( silk_dec ); | 419 | | | 420 | | /* The SILK PLC cannot produce frames of less than 10 ms */ | 421 | 64.2k | st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs); | 422 | | | 423 | 64.2k | if (data != NULL) | 424 | 52.7k | { | 425 | 52.7k | st->DecControl.nChannelsInternal = st->stream_channels; | 426 | 52.7k | if( mode == MODE_SILK_ONLY ) { | 427 | 42.6k | if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) { | 428 | 23.1k | st->DecControl.internalSampleRate = 8000; | 429 | 23.1k | } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) { | 430 | 11.7k | st->DecControl.internalSampleRate = 12000; | 431 | 11.7k | } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) { | 432 | 7.79k | st->DecControl.internalSampleRate = 16000; | 433 | 7.79k | } else { | 434 | 0 | st->DecControl.internalSampleRate = 16000; | 435 | 0 | celt_assert( 0 ); | 436 | 0 | } | 437 | 42.6k | } else { | 438 | | /* Hybrid mode */ | 439 | 10.0k | st->DecControl.internalSampleRate = 16000; | 440 | 10.0k | } | 441 | 52.7k | } | 442 | 64.2k | st->DecControl.enable_deep_plc = st->complexity >= 5; | 443 | | #ifdef ENABLE_OSCE | 444 | | st->DecControl.osce_method = OSCE_METHOD_NONE; | 445 | | #ifndef DISABLE_LACE | 446 | | if (st->complexity >= 6) {st->DecControl.osce_method = OSCE_METHOD_LACE;} | 447 | | #endif | 448 | | #ifndef DISABLE_NOLACE | 449 | | if (st->complexity >= 7) {st->DecControl.osce_method = OSCE_METHOD_NOLACE;} | 450 | | #endif | 451 | | #ifdef ENABLE_OSCE_BWE | 452 | | if (st->complexity >= 4 && st->DecControl.enable_osce_bwe && | 453 | | st->Fs == 48000 && st->DecControl.internalSampleRate == 16000 && | 454 | | ((mode == MODE_SILK_ONLY) || (data == NULL))) { | 455 | | /* request WB -> FB signal extension */ | 456 | | st->DecControl.osce_extended_mode = OSCE_MODE_SILK_BBWE; | 457 | | } else { | 458 | | /* at this point, mode can only be MODE_SILK_ONLY or MODE_HYBRID */ | 459 | | st->DecControl.osce_extended_mode = mode == MODE_SILK_ONLY ? OSCE_MODE_SILK_ONLY : OSCE_MODE_HYBRID; | 460 | | } | 461 | | if (st->prev_mode == MODE_CELT_ONLY) { | 462 | | /* Update extended mode for CELT->SILK transition */ | 463 | | st->DecControl.prev_osce_extended_mode = OSCE_MODE_CELT_ONLY; | 464 | | } | 465 | | #endif | 466 | | #endif | 467 | | | 468 | 64.2k | lost_flag = data == NULL ? 1 : 2 * !!decode_fec; | 469 | 64.2k | decoded_samples = 0; | 470 | 97.4k | do { | 471 | | /* Call SILK decoder */ | 472 | 97.4k | int first_frame = decoded_samples == 0; | 473 | 97.4k | silk_ret = silk_Decode( silk_dec, &st->DecControl, | 474 | 97.4k | lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, | 475 | | #ifdef ENABLE_DEEP_PLC | 476 | | &st->lpcnet, | 477 | | #endif | 478 | 97.4k | st->arch ); | 479 | 97.4k | if( silk_ret ) { | 480 | 0 | if (lost_flag) { | 481 | | /* PLC failure should not be fatal */ | 482 | 0 | silk_frame_size = frame_size; | 483 | 0 | for (i=0;i<frame_size*st->channels;i++) | 484 | 0 | pcm_ptr[i] = 0; | 485 | 0 | } else { | 486 | 0 | RESTORE_STACK; | 487 | 0 | return OPUS_INTERNAL_ERROR; | 488 | 0 | } | 489 | 0 | } | 490 | 97.4k | pcm_ptr += silk_frame_size * st->channels; | 491 | 97.4k | decoded_samples += silk_frame_size; | 492 | 97.4k | } while( decoded_samples < frame_size ); | 493 | 64.2k | if (pcm_too_small) { | 494 | 0 | OPUS_COPY(pcm, pcm_silk, frame_size*st->channels); | 495 | 0 | } | 496 | 64.2k | } | 497 | | | 498 | 176k | start_band = 0; | 499 | 176k | if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL | 500 | 41.2k | && ec_tell(&dec)+17+20*(mode == MODE_HYBRID) <= 8*len) | 501 | 13.1k | { | 502 | | /* Check if we have a redundant 0-8 kHz band */ | 503 | 13.1k | if (mode == MODE_HYBRID) | 504 | 3.12k | redundancy = ec_dec_bit_logp(&dec, 12); | 505 | 10.0k | else | 506 | 10.0k | redundancy = 1; | 507 | 13.1k | if (redundancy) | 508 | 10.1k | { | 509 | 10.1k | celt_to_silk = ec_dec_bit_logp(&dec, 1); | 510 | | /* redundancy_bytes will be at least two, in the non-hybrid | 511 | | case due to the ec_tell() check above */ | 512 | 10.1k | redundancy_bytes = mode==MODE_HYBRID ? | 513 | 75 | (opus_int32)ec_dec_uint(&dec, 256)+2 : | 514 | 10.1k | len-((ec_tell(&dec)+7)>>3); | 515 | 10.1k | len -= redundancy_bytes; | 516 | | /* This is a sanity check. It should never happen for a valid | 517 | | packet, so the exact behaviour is not normative. */ | 518 | 10.1k | if (len*8 < ec_tell(&dec)) | 519 | 42 | { | 520 | 42 | len = 0; | 521 | 42 | redundancy_bytes = 0; | 522 | 42 | redundancy = 0; | 523 | 42 | } | 524 | | /* Shrink decoder because of raw bits */ | 525 | 10.1k | dec.storage -= redundancy_bytes; | 526 | 10.1k | } | 527 | 13.1k | } | 528 | 176k | if (mode != MODE_CELT_ONLY) | 529 | 64.2k | start_band = 17; | 530 | | | 531 | 176k | if (redundancy) | 532 | 10.0k | { | 533 | 10.0k | transition = 0; | 534 | 10.0k | pcm_transition_silk_size=ALLOC_NONE; | 535 | 10.0k | } | 536 | | | 537 | 176k | ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_res); | 538 | | | 539 | 176k | if (transition && mode != MODE_CELT_ONLY) | 540 | 57 | { | 541 | 57 | pcm_transition = pcm_transition_silk; | 542 | 57 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); | 543 | 57 | } | 544 | | | 545 | | | 546 | 176k | if (bandwidth) | 547 | 116k | { | 548 | 116k | int endband=21; | 549 | | | 550 | 116k | switch(bandwidth) | 551 | 116k | { | 552 | 32.1k | case OPUS_BANDWIDTH_NARROWBAND: | 553 | 32.1k | endband = 13; | 554 | 32.1k | break; | 555 | 11.7k | case OPUS_BANDWIDTH_MEDIUMBAND: | 556 | 43.0k | case OPUS_BANDWIDTH_WIDEBAND: | 557 | 43.0k | endband = 17; | 558 | 43.0k | break; | 559 | 8.65k | case OPUS_BANDWIDTH_SUPERWIDEBAND: | 560 | 8.65k | endband = 19; | 561 | 8.65k | break; | 562 | 32.3k | case OPUS_BANDWIDTH_FULLBAND: | 563 | 32.3k | endband = 21; | 564 | 32.3k | break; | 565 | 0 | default: | 566 | 0 | celt_assert(0); | 567 | 0 | break; | 568 | 116k | } | 569 | 116k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband))); | 570 | 116k | } | 571 | 176k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels))); | 572 | | | 573 | | /* Only allocation memory for redundancy if/when needed */ | 574 | 176k | redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE; | 575 | 176k | ALLOC(redundant_audio, redundant_audio_size, opus_res); | 576 | | | 577 | | /* 5 ms redundant frame for CELT->SILK*/ | 578 | 176k | if (redundancy && celt_to_silk) | 579 | 5.43k | { | 580 | | /* If the previous frame did not use CELT (the first redundancy frame in | 581 | | a transition from SILK may have been lost) then the CELT decoder is | 582 | | stale at this point and the redundancy audio is not useful, however | 583 | | the final range is still needed (for testing), so the redundancy is | 584 | | always decoded but the decoded audio may not be used */ | 585 | 5.43k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 586 | 5.43k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, | 587 | 5.43k | redundant_audio, F5, NULL, 0); | 588 | 5.43k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); | 589 | 5.43k | } | 590 | | | 591 | | /* MUST be after PLC */ | 592 | 176k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band))); | 593 | | | 594 | | #ifdef ENABLE_OSCE_BWE | 595 | | if (mode != MODE_SILK_ONLY && st->DecControl.osce_extended_mode != OSCE_MODE_SILK_BBWE) | 596 | | #else | 597 | 176k | if (mode != MODE_SILK_ONLY) | 598 | 126k | #endif | 599 | 126k | { | 600 | 126k | int celt_frame_size = IMIN(F20, frame_size); | 601 | | /* Make sure to discard any previous CELT state */ | 602 | 126k | if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy) | 603 | 126k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); | 604 | | /* Decode CELT */ | 605 | 126k | celt_ret = celt_decode_with_ec_dred(celt_dec, decode_fec ? NULL : data, | 606 | 126k | len, pcm, celt_frame_size, &dec, celt_accum | 607 | | #ifdef ENABLE_DEEP_PLC | 608 | | , &st->lpcnet | 609 | | #endif | 610 | 126k | ARG_QEXT(ext ? ext->data : NULL) ARG_QEXT(ext ? ext->len : 0)); | 611 | 126k | celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&st->rangeFinal)); | 612 | 126k | } else { | 613 | 50.3k | unsigned char silence[2] = {0xFF, 0xFF}; | 614 | 50.3k | if (!celt_accum) | 615 | 0 | { | 616 | 0 | for (i=0;i<frame_size*st->channels;i++) | 617 | 0 | pcm[i] = 0; | 618 | 0 | } | 619 | | /* For hybrid -> SILK transitions, we let the CELT MDCT | 620 | | do a fade-out by decoding a silence frame */ | 621 | 50.3k | if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) ) | 622 | 0 | { | 623 | 0 | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 624 | 0 | celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum); | 625 | 0 | } | 626 | 50.3k | st->rangeFinal = dec.rng; | 627 | 50.3k | } | 628 | | | 629 | 176k | { | 630 | 176k | const CELTMode *celt_mode; | 631 | 176k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode))); | 632 | 176k | window = celt_mode->window; | 633 | 176k | } | 634 | | | 635 | | /* 5 ms redundant frame for SILK->CELT */ | 636 | 176k | if (redundancy && !celt_to_silk) | 637 | 4.62k | { | 638 | 4.62k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); | 639 | 4.62k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 640 | | | 641 | 4.62k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0); | 642 | 4.62k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); | 643 | 4.62k | smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5, | 644 | 4.62k | pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs); | 645 | 4.62k | } | 646 | | /* 5ms redundant frame for CELT->SILK; ignore if the previous frame did not | 647 | | use CELT (the first redundancy frame in a transition from SILK may have | 648 | | been lost) */ | 649 | 176k | if (redundancy && celt_to_silk && (st->prev_mode != MODE_SILK_ONLY || st->prev_redundancy)) | 650 | 3.62k | { | 651 | 9.69k | for (c=0;c<st->channels;c++) | 652 | 6.06k | { | 653 | 736k | for (i=0;i<F2_5;i++) | 654 | 730k | pcm[st->channels*i+c] = redundant_audio[st->channels*i+c]; | 655 | 6.06k | } | 656 | 3.62k | smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5, | 657 | 3.62k | pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs); | 658 | 3.62k | } | 659 | 176k | if (transition) | 660 | 57 | { | 661 | 57 | if (audiosize >= F5) | 662 | 57 | { | 663 | 9.20k | for (i=0;i<st->channels*F2_5;i++) | 664 | 9.15k | pcm[i] = pcm_transition[i]; | 665 | 57 | smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5, | 666 | 57 | pcm+st->channels*F2_5, F2_5, | 667 | 57 | st->channels, window, st->Fs); | 668 | 57 | } else { | 669 | | /* Not enough time to do a clean transition, but we do it anyway | 670 | | This will not preserve amplitude perfectly and may introduce | 671 | | a bit of temporal aliasing, but it shouldn't be too bad and | 672 | | that's pretty much the best we can do. In any case, generating this | 673 | | transition it pretty silly in the first place */ | 674 | 0 | smooth_fade(pcm_transition, pcm, | 675 | 0 | pcm, F2_5, | 676 | 0 | st->channels, window, st->Fs); | 677 | 0 | } | 678 | 57 | } | 679 | | | 680 | 176k | if(st->decode_gain) | 681 | 0 | { | 682 | 0 | opus_val32 gain; | 683 | 0 | gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain)); | 684 | 0 | for (i=0;i<frame_size*st->channels;i++) | 685 | 0 | { | 686 | 0 | opus_val32 x; | 687 | 0 | #ifdef ENABLE_RES24 | 688 | 0 | x = MULT32_32_Q16(pcm[i],gain); | 689 | | #else | 690 | | x = MULT16_32_P16(pcm[i],gain); | 691 | | #endif | 692 | 0 | pcm[i] = SATURATE(x, 32767); | 693 | 0 | } | 694 | 0 | } | 695 | | | 696 | 176k | if (len <= 1) | 697 | 60.3k | st->rangeFinal = 0; | 698 | 116k | else | 699 | 116k | st->rangeFinal ^= redundant_rng; | 700 | | | 701 | 176k | st->prev_mode = mode; | 702 | 176k | st->prev_redundancy = redundancy && !celt_to_silk; | 703 | | | 704 | 176k | if (celt_ret>=0) | 705 | 176k | { | 706 | 176k | if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels)) | 707 | 0 | OPUS_PRINT_INT(audiosize); | 708 | 176k | } | 709 | | | 710 | 176k | RESTORE_STACK; | 711 | 176k | return celt_ret < 0 ? celt_ret : audiosize; | 712 | | | 713 | 176k | } |
opus_decoder.c:opus_decode_frame Line | Count | Source | 272 | 871k | { | 273 | 871k | void *silk_dec; | 274 | 871k | CELTDecoder *celt_dec; | 275 | 871k | int i, silk_ret=0, celt_ret=0; | 276 | 871k | ec_dec dec; | 277 | 871k | opus_int32 silk_frame_size; | 278 | 871k | int pcm_transition_silk_size; | 279 | 871k | VARDECL(opus_res, pcm_transition_silk); | 280 | 871k | int pcm_transition_celt_size; | 281 | 871k | VARDECL(opus_res, pcm_transition_celt); | 282 | 871k | opus_res *pcm_transition=NULL; | 283 | 871k | int redundant_audio_size; | 284 | 871k | VARDECL(opus_res, redundant_audio); | 285 | | | 286 | 871k | int audiosize; | 287 | 871k | int mode; | 288 | 871k | int bandwidth; | 289 | 871k | int transition=0; | 290 | 871k | int start_band; | 291 | 871k | int redundancy=0; | 292 | 871k | int redundancy_bytes = 0; | 293 | 871k | int celt_to_silk=0; | 294 | 871k | int c; | 295 | 871k | int F2_5, F5, F10, F20; | 296 | 871k | const celt_coef *window; | 297 | 871k | opus_uint32 redundant_rng = 0; | 298 | 871k | int celt_accum; | 299 | 871k | ALLOC_STACK; | 300 | | | 301 | 871k | silk_dec = (char*)st+st->silk_dec_offset; | 302 | 871k | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); | 303 | 871k | F20 = st->Fs/50; | 304 | 871k | F10 = F20>>1; | 305 | 871k | F5 = F10>>1; | 306 | 871k | F2_5 = F5>>1; | 307 | 871k | if (frame_size < F2_5) | 308 | 0 | { | 309 | 0 | RESTORE_STACK; | 310 | 0 | return OPUS_BUFFER_TOO_SMALL; | 311 | 0 | } | 312 | | /* Limit frame_size to avoid excessive stack allocations. */ | 313 | 871k | frame_size = IMIN(frame_size, st->Fs/25*3); | 314 | | /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */ | 315 | 871k | if (len<=1) | 316 | 767k | { | 317 | 767k | data = NULL; | 318 | | /* In that case, don't conceal more than what the ToC says */ | 319 | 767k | frame_size = IMIN(frame_size, st->frame_size); | 320 | 767k | } | 321 | 871k | if (data != NULL) | 322 | 103k | { | 323 | 103k | audiosize = st->frame_size; | 324 | 103k | mode = st->mode; | 325 | 103k | bandwidth = st->bandwidth; | 326 | 103k | ec_dec_init(&dec,(unsigned char*)data,len); | 327 | 767k | } else { | 328 | 767k | audiosize = frame_size; | 329 | | /* Run PLC using last used mode (CELT if we ended with CELT redundancy) */ | 330 | 767k | mode = st->prev_redundancy ? MODE_CELT_ONLY : st->prev_mode; | 331 | 767k | bandwidth = 0; | 332 | | | 333 | 767k | if (mode == 0) | 334 | 710k | { | 335 | | /* If we haven't got any packet yet, all we can do is return zeros */ | 336 | 71.7M | for (i=0;i<audiosize*st->channels;i++) | 337 | 71.0M | pcm[i] = 0; | 338 | 710k | RESTORE_STACK; | 339 | 710k | return audiosize; | 340 | 710k | } | 341 | | | 342 | | /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT), | 343 | | 10, or 20 (e.g. 12.5 or 30 ms). */ | 344 | 56.5k | if (audiosize > F20) | 345 | 3.17k | { | 346 | 9.44k | do { | 347 | 9.44k | int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0 ARG_QEXT(NULL)); | 348 | 9.44k | if (ret<0) | 349 | 0 | { | 350 | 0 | RESTORE_STACK; | 351 | 0 | return ret; | 352 | 0 | } | 353 | 9.44k | pcm += ret*st->channels; | 354 | 9.44k | audiosize -= ret; | 355 | 9.44k | } while (audiosize > 0); | 356 | 3.17k | RESTORE_STACK; | 357 | 3.17k | return frame_size; | 358 | 53.4k | } else if (audiosize < F20) | 359 | 41.7k | { | 360 | 41.7k | if (audiosize > F10) | 361 | 0 | audiosize = F10; | 362 | 41.7k | else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10) | 363 | 0 | audiosize = F5; | 364 | 41.7k | } | 365 | 56.5k | } | 366 | | | 367 | | /* In fixed-point, we can tell CELT to do the accumulation on top of the | 368 | | SILK PCM buffer. This saves some stack space. */ | 369 | 157k | celt_accum = (mode != MODE_CELT_ONLY); | 370 | | | 371 | 157k | pcm_transition_silk_size = ALLOC_NONE; | 372 | 157k | pcm_transition_celt_size = ALLOC_NONE; | 373 | 157k | if (data!=NULL && st->prev_mode > 0 && ( | 374 | 49.7k | (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy) | 375 | 49.7k | || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) | 376 | 157k | ) | 377 | 79 | { | 378 | 79 | transition = 1; | 379 | | /* Decide where to allocate the stack memory for pcm_transition */ | 380 | 79 | if (mode == MODE_CELT_ONLY) | 381 | 0 | pcm_transition_celt_size = F5*st->channels; | 382 | 79 | else | 383 | 79 | pcm_transition_silk_size = F5*st->channels; | 384 | 79 | } | 385 | 157k | ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_res); | 386 | 157k | if (transition && mode == MODE_CELT_ONLY) | 387 | 0 | { | 388 | 0 | pcm_transition = pcm_transition_celt; | 389 | 0 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); | 390 | 0 | } | 391 | 157k | if (audiosize > frame_size) | 392 | 0 | { | 393 | | /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/ | 394 | 0 | RESTORE_STACK; | 395 | 0 | return OPUS_BAD_ARG; | 396 | 157k | } else { | 397 | 157k | frame_size = audiosize; | 398 | 157k | } | 399 | | | 400 | | /* SILK processing */ | 401 | 157k | if (mode != MODE_CELT_ONLY) | 402 | 64.9k | { | 403 | 64.9k | int lost_flag, decoded_samples; | 404 | 64.9k | opus_res *pcm_ptr; | 405 | 64.9k | int pcm_too_small; | 406 | 64.9k | int pcm_silk_size = ALLOC_NONE; | 407 | 64.9k | VARDECL(opus_res, pcm_silk); | 408 | 64.9k | pcm_too_small = (frame_size < F10); | 409 | 64.9k | if (pcm_too_small) | 410 | 0 | pcm_silk_size = F10*st->channels; | 411 | 64.9k | ALLOC(pcm_silk, pcm_silk_size, opus_res); | 412 | 64.9k | if (pcm_too_small) | 413 | 0 | pcm_ptr = pcm_silk; | 414 | 64.9k | else | 415 | 64.9k | pcm_ptr = pcm; | 416 | | | 417 | 64.9k | if (st->prev_mode==MODE_CELT_ONLY) | 418 | 79 | silk_ResetDecoder( silk_dec ); | 419 | | | 420 | | /* The SILK PLC cannot produce frames of less than 10 ms */ | 421 | 64.9k | st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs); | 422 | | | 423 | 64.9k | if (data != NULL) | 424 | 49.0k | { | 425 | 49.0k | st->DecControl.nChannelsInternal = st->stream_channels; | 426 | 49.0k | if( mode == MODE_SILK_ONLY ) { | 427 | 39.0k | if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) { | 428 | 18.3k | st->DecControl.internalSampleRate = 8000; | 429 | 20.7k | } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) { | 430 | 13.3k | st->DecControl.internalSampleRate = 12000; | 431 | 13.3k | } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) { | 432 | 7.45k | st->DecControl.internalSampleRate = 16000; | 433 | 7.45k | } else { | 434 | 0 | st->DecControl.internalSampleRate = 16000; | 435 | 0 | celt_assert( 0 ); | 436 | 0 | } | 437 | 39.0k | } else { | 438 | | /* Hybrid mode */ | 439 | 9.95k | st->DecControl.internalSampleRate = 16000; | 440 | 9.95k | } | 441 | 49.0k | } | 442 | 64.9k | st->DecControl.enable_deep_plc = st->complexity >= 5; | 443 | | #ifdef ENABLE_OSCE | 444 | | st->DecControl.osce_method = OSCE_METHOD_NONE; | 445 | | #ifndef DISABLE_LACE | 446 | | if (st->complexity >= 6) {st->DecControl.osce_method = OSCE_METHOD_LACE;} | 447 | | #endif | 448 | | #ifndef DISABLE_NOLACE | 449 | | if (st->complexity >= 7) {st->DecControl.osce_method = OSCE_METHOD_NOLACE;} | 450 | | #endif | 451 | | #ifdef ENABLE_OSCE_BWE | 452 | | if (st->complexity >= 4 && st->DecControl.enable_osce_bwe && | 453 | | st->Fs == 48000 && st->DecControl.internalSampleRate == 16000 && | 454 | | ((mode == MODE_SILK_ONLY) || (data == NULL))) { | 455 | | /* request WB -> FB signal extension */ | 456 | | st->DecControl.osce_extended_mode = OSCE_MODE_SILK_BBWE; | 457 | | } else { | 458 | | /* at this point, mode can only be MODE_SILK_ONLY or MODE_HYBRID */ | 459 | | st->DecControl.osce_extended_mode = mode == MODE_SILK_ONLY ? OSCE_MODE_SILK_ONLY : OSCE_MODE_HYBRID; | 460 | | } | 461 | | if (st->prev_mode == MODE_CELT_ONLY) { | 462 | | /* Update extended mode for CELT->SILK transition */ | 463 | | st->DecControl.prev_osce_extended_mode = OSCE_MODE_CELT_ONLY; | 464 | | } | 465 | | #endif | 466 | | #endif | 467 | | | 468 | 64.9k | lost_flag = data == NULL ? 1 : 2 * !!decode_fec; | 469 | 64.9k | decoded_samples = 0; | 470 | 101k | do { | 471 | | /* Call SILK decoder */ | 472 | 101k | int first_frame = decoded_samples == 0; | 473 | 101k | silk_ret = silk_Decode( silk_dec, &st->DecControl, | 474 | 101k | lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, | 475 | | #ifdef ENABLE_DEEP_PLC | 476 | | &st->lpcnet, | 477 | | #endif | 478 | 101k | st->arch ); | 479 | 101k | if( silk_ret ) { | 480 | 0 | if (lost_flag) { | 481 | | /* PLC failure should not be fatal */ | 482 | 0 | silk_frame_size = frame_size; | 483 | 0 | for (i=0;i<frame_size*st->channels;i++) | 484 | 0 | pcm_ptr[i] = 0; | 485 | 0 | } else { | 486 | 0 | RESTORE_STACK; | 487 | 0 | return OPUS_INTERNAL_ERROR; | 488 | 0 | } | 489 | 0 | } | 490 | 101k | pcm_ptr += silk_frame_size * st->channels; | 491 | 101k | decoded_samples += silk_frame_size; | 492 | 101k | } while( decoded_samples < frame_size ); | 493 | 64.9k | if (pcm_too_small) { | 494 | 0 | OPUS_COPY(pcm, pcm_silk, frame_size*st->channels); | 495 | 0 | } | 496 | 64.9k | } | 497 | | | 498 | 157k | start_band = 0; | 499 | 157k | if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL | 500 | 39.8k | && ec_tell(&dec)+17+20*(mode == MODE_HYBRID) <= 8*len) | 501 | 10.2k | { | 502 | | /* Check if we have a redundant 0-8 kHz band */ | 503 | 10.2k | if (mode == MODE_HYBRID) | 504 | 3.09k | redundancy = ec_dec_bit_logp(&dec, 12); | 505 | 7.18k | else | 506 | 7.18k | redundancy = 1; | 507 | 10.2k | if (redundancy) | 508 | 7.29k | { | 509 | 7.29k | celt_to_silk = ec_dec_bit_logp(&dec, 1); | 510 | | /* redundancy_bytes will be at least two, in the non-hybrid | 511 | | case due to the ec_tell() check above */ | 512 | 7.29k | redundancy_bytes = mode==MODE_HYBRID ? | 513 | 108 | (opus_int32)ec_dec_uint(&dec, 256)+2 : | 514 | 7.29k | len-((ec_tell(&dec)+7)>>3); | 515 | 7.29k | len -= redundancy_bytes; | 516 | | /* This is a sanity check. It should never happen for a valid | 517 | | packet, so the exact behaviour is not normative. */ | 518 | 7.29k | if (len*8 < ec_tell(&dec)) | 519 | 67 | { | 520 | 67 | len = 0; | 521 | 67 | redundancy_bytes = 0; | 522 | 67 | redundancy = 0; | 523 | 67 | } | 524 | | /* Shrink decoder because of raw bits */ | 525 | 7.29k | dec.storage -= redundancy_bytes; | 526 | 7.29k | } | 527 | 10.2k | } | 528 | 157k | if (mode != MODE_CELT_ONLY) | 529 | 64.9k | start_band = 17; | 530 | | | 531 | 157k | if (redundancy) | 532 | 7.22k | { | 533 | 7.22k | transition = 0; | 534 | 7.22k | pcm_transition_silk_size=ALLOC_NONE; | 535 | 7.22k | } | 536 | | | 537 | 157k | ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_res); | 538 | | | 539 | 157k | if (transition && mode != MODE_CELT_ONLY) | 540 | 58 | { | 541 | 58 | pcm_transition = pcm_transition_silk; | 542 | 58 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); | 543 | 58 | } | 544 | | | 545 | | | 546 | 157k | if (bandwidth) | 547 | 103k | { | 548 | 103k | int endband=21; | 549 | | | 550 | 103k | switch(bandwidth) | 551 | 103k | { | 552 | 28.7k | case OPUS_BANDWIDTH_NARROWBAND: | 553 | 28.7k | endband = 13; | 554 | 28.7k | break; | 555 | 13.3k | case OPUS_BANDWIDTH_MEDIUMBAND: | 556 | 36.5k | case OPUS_BANDWIDTH_WIDEBAND: | 557 | 36.5k | endband = 17; | 558 | 36.5k | break; | 559 | 10.0k | case OPUS_BANDWIDTH_SUPERWIDEBAND: | 560 | 10.0k | endband = 19; | 561 | 10.0k | break; | 562 | 28.4k | case OPUS_BANDWIDTH_FULLBAND: | 563 | 28.4k | endband = 21; | 564 | 28.4k | break; | 565 | 0 | default: | 566 | 0 | celt_assert(0); | 567 | 0 | break; | 568 | 103k | } | 569 | 103k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband))); | 570 | 103k | } | 571 | 157k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels))); | 572 | | | 573 | | /* Only allocation memory for redundancy if/when needed */ | 574 | 157k | redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE; | 575 | 157k | ALLOC(redundant_audio, redundant_audio_size, opus_res); | 576 | | | 577 | | /* 5 ms redundant frame for CELT->SILK*/ | 578 | 157k | if (redundancy && celt_to_silk) | 579 | 4.07k | { | 580 | | /* If the previous frame did not use CELT (the first redundancy frame in | 581 | | a transition from SILK may have been lost) then the CELT decoder is | 582 | | stale at this point and the redundancy audio is not useful, however | 583 | | the final range is still needed (for testing), so the redundancy is | 584 | | always decoded but the decoded audio may not be used */ | 585 | 4.07k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 586 | 4.07k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, | 587 | 4.07k | redundant_audio, F5, NULL, 0); | 588 | 4.07k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); | 589 | 4.07k | } | 590 | | | 591 | | /* MUST be after PLC */ | 592 | 157k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band))); | 593 | | | 594 | | #ifdef ENABLE_OSCE_BWE | 595 | | if (mode != MODE_SILK_ONLY && st->DecControl.osce_extended_mode != OSCE_MODE_SILK_BBWE) | 596 | | #else | 597 | 157k | if (mode != MODE_SILK_ONLY) | 598 | 107k | #endif | 599 | 107k | { | 600 | 107k | int celt_frame_size = IMIN(F20, frame_size); | 601 | | /* Make sure to discard any previous CELT state */ | 602 | 107k | if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy) | 603 | 107k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); | 604 | | /* Decode CELT */ | 605 | 107k | celt_ret = celt_decode_with_ec_dred(celt_dec, decode_fec ? NULL : data, | 606 | 107k | len, pcm, celt_frame_size, &dec, celt_accum | 607 | | #ifdef ENABLE_DEEP_PLC | 608 | | , &st->lpcnet | 609 | | #endif | 610 | 107k | ARG_QEXT(ext ? ext->data : NULL) ARG_QEXT(ext ? ext->len : 0)); | 611 | 107k | celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&st->rangeFinal)); | 612 | 107k | } else { | 613 | 50.2k | unsigned char silence[2] = {0xFF, 0xFF}; | 614 | 50.2k | if (!celt_accum) | 615 | 0 | { | 616 | 0 | for (i=0;i<frame_size*st->channels;i++) | 617 | 0 | pcm[i] = 0; | 618 | 0 | } | 619 | | /* For hybrid -> SILK transitions, we let the CELT MDCT | 620 | | do a fade-out by decoding a silence frame */ | 621 | 50.2k | if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) ) | 622 | 0 | { | 623 | 0 | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 624 | 0 | celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum); | 625 | 0 | } | 626 | 50.2k | st->rangeFinal = dec.rng; | 627 | 50.2k | } | 628 | | | 629 | 157k | { | 630 | 157k | const CELTMode *celt_mode; | 631 | 157k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode))); | 632 | 157k | window = celt_mode->window; | 633 | 157k | } | 634 | | | 635 | | /* 5 ms redundant frame for SILK->CELT */ | 636 | 157k | if (redundancy && !celt_to_silk) | 637 | 3.15k | { | 638 | 3.15k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); | 639 | 3.15k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 640 | | | 641 | 3.15k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0); | 642 | 3.15k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); | 643 | 3.15k | smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5, | 644 | 3.15k | pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs); | 645 | 3.15k | } | 646 | | /* 5ms redundant frame for CELT->SILK; ignore if the previous frame did not | 647 | | use CELT (the first redundancy frame in a transition from SILK may have | 648 | | been lost) */ | 649 | 157k | if (redundancy && celt_to_silk && (st->prev_mode != MODE_SILK_ONLY || st->prev_redundancy)) | 650 | 2.71k | { | 651 | 6.58k | for (c=0;c<st->channels;c++) | 652 | 3.86k | { | 653 | 370k | for (i=0;i<F2_5;i++) | 654 | 366k | pcm[st->channels*i+c] = redundant_audio[st->channels*i+c]; | 655 | 3.86k | } | 656 | 2.71k | smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5, | 657 | 2.71k | pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs); | 658 | 2.71k | } | 659 | 157k | if (transition) | 660 | 58 | { | 661 | 58 | if (audiosize >= F5) | 662 | 58 | { | 663 | 8.17k | for (i=0;i<st->channels*F2_5;i++) | 664 | 8.12k | pcm[i] = pcm_transition[i]; | 665 | 58 | smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5, | 666 | 58 | pcm+st->channels*F2_5, F2_5, | 667 | 58 | st->channels, window, st->Fs); | 668 | 58 | } else { | 669 | | /* Not enough time to do a clean transition, but we do it anyway | 670 | | This will not preserve amplitude perfectly and may introduce | 671 | | a bit of temporal aliasing, but it shouldn't be too bad and | 672 | | that's pretty much the best we can do. In any case, generating this | 673 | | transition it pretty silly in the first place */ | 674 | 0 | smooth_fade(pcm_transition, pcm, | 675 | 0 | pcm, F2_5, | 676 | 0 | st->channels, window, st->Fs); | 677 | 0 | } | 678 | 58 | } | 679 | | | 680 | 157k | if(st->decode_gain) | 681 | 0 | { | 682 | 0 | opus_val32 gain; | 683 | 0 | gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain)); | 684 | 0 | for (i=0;i<frame_size*st->channels;i++) | 685 | 0 | { | 686 | 0 | opus_val32 x; | 687 | 0 | #ifdef ENABLE_RES24 | 688 | 0 | x = MULT32_32_Q16(pcm[i],gain); | 689 | | #else | 690 | | x = MULT16_32_P16(pcm[i],gain); | 691 | | #endif | 692 | 0 | pcm[i] = SATURATE(x, 32767); | 693 | 0 | } | 694 | 0 | } | 695 | | | 696 | 157k | if (len <= 1) | 697 | 53.4k | st->rangeFinal = 0; | 698 | 103k | else | 699 | 103k | st->rangeFinal ^= redundant_rng; | 700 | | | 701 | 157k | st->prev_mode = mode; | 702 | 157k | st->prev_redundancy = redundancy && !celt_to_silk; | 703 | | | 704 | 157k | if (celt_ret>=0) | 705 | 157k | { | 706 | 157k | if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels)) | 707 | 0 | OPUS_PRINT_INT(audiosize); | 708 | 157k | } | 709 | | | 710 | 157k | RESTORE_STACK; | 711 | 157k | return celt_ret < 0 ? celt_ret : audiosize; | 712 | | | 713 | 157k | } |
opus_decoder.c:opus_decode_frame Line | Count | Source | 272 | 890k | { | 273 | 890k | void *silk_dec; | 274 | 890k | CELTDecoder *celt_dec; | 275 | 890k | int i, silk_ret=0, celt_ret=0; | 276 | 890k | ec_dec dec; | 277 | 890k | opus_int32 silk_frame_size; | 278 | 890k | int pcm_transition_silk_size; | 279 | 890k | VARDECL(opus_res, pcm_transition_silk); | 280 | 890k | int pcm_transition_celt_size; | 281 | 890k | VARDECL(opus_res, pcm_transition_celt); | 282 | 890k | opus_res *pcm_transition=NULL; | 283 | 890k | int redundant_audio_size; | 284 | 890k | VARDECL(opus_res, redundant_audio); | 285 | | | 286 | 890k | int audiosize; | 287 | 890k | int mode; | 288 | 890k | int bandwidth; | 289 | 890k | int transition=0; | 290 | 890k | int start_band; | 291 | 890k | int redundancy=0; | 292 | 890k | int redundancy_bytes = 0; | 293 | 890k | int celt_to_silk=0; | 294 | 890k | int c; | 295 | 890k | int F2_5, F5, F10, F20; | 296 | 890k | const celt_coef *window; | 297 | 890k | opus_uint32 redundant_rng = 0; | 298 | 890k | int celt_accum; | 299 | 890k | ALLOC_STACK; | 300 | | | 301 | 890k | silk_dec = (char*)st+st->silk_dec_offset; | 302 | 890k | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); | 303 | 890k | F20 = st->Fs/50; | 304 | 890k | F10 = F20>>1; | 305 | 890k | F5 = F10>>1; | 306 | 890k | F2_5 = F5>>1; | 307 | 890k | if (frame_size < F2_5) | 308 | 0 | { | 309 | 0 | RESTORE_STACK; | 310 | 0 | return OPUS_BUFFER_TOO_SMALL; | 311 | 0 | } | 312 | | /* Limit frame_size to avoid excessive stack allocations. */ | 313 | 890k | frame_size = IMIN(frame_size, st->Fs/25*3); | 314 | | /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */ | 315 | 890k | if (len<=1) | 316 | 794k | { | 317 | 794k | data = NULL; | 318 | | /* In that case, don't conceal more than what the ToC says */ | 319 | 794k | frame_size = IMIN(frame_size, st->frame_size); | 320 | 794k | } | 321 | 890k | if (data != NULL) | 322 | 95.3k | { | 323 | 95.3k | audiosize = st->frame_size; | 324 | 95.3k | mode = st->mode; | 325 | 95.3k | bandwidth = st->bandwidth; | 326 | 95.3k | ec_dec_init(&dec,(unsigned char*)data,len); | 327 | 794k | } else { | 328 | 794k | audiosize = frame_size; | 329 | | /* Run PLC using last used mode (CELT if we ended with CELT redundancy) */ | 330 | 794k | mode = st->prev_redundancy ? MODE_CELT_ONLY : st->prev_mode; | 331 | 794k | bandwidth = 0; | 332 | | | 333 | 794k | if (mode == 0) | 334 | 731k | { | 335 | | /* If we haven't got any packet yet, all we can do is return zeros */ | 336 | 52.9M | for (i=0;i<audiosize*st->channels;i++) | 337 | 52.2M | pcm[i] = 0; | 338 | 731k | RESTORE_STACK; | 339 | 731k | return audiosize; | 340 | 731k | } | 341 | | | 342 | | /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT), | 343 | | 10, or 20 (e.g. 12.5 or 30 ms). */ | 344 | 63.8k | if (audiosize > F20) | 345 | 3.92k | { | 346 | 11.6k | do { | 347 | 11.6k | int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0 ARG_QEXT(NULL)); | 348 | 11.6k | if (ret<0) | 349 | 0 | { | 350 | 0 | RESTORE_STACK; | 351 | 0 | return ret; | 352 | 0 | } | 353 | 11.6k | pcm += ret*st->channels; | 354 | 11.6k | audiosize -= ret; | 355 | 11.6k | } while (audiosize > 0); | 356 | 3.92k | RESTORE_STACK; | 357 | 3.92k | return frame_size; | 358 | 59.9k | } else if (audiosize < F20) | 359 | 43.2k | { | 360 | 43.2k | if (audiosize > F10) | 361 | 0 | audiosize = F10; | 362 | 43.2k | else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10) | 363 | 0 | audiosize = F5; | 364 | 43.2k | } | 365 | 63.8k | } | 366 | | | 367 | | /* In fixed-point, we can tell CELT to do the accumulation on top of the | 368 | | SILK PCM buffer. This saves some stack space. */ | 369 | 155k | celt_accum = (mode != MODE_CELT_ONLY); | 370 | | | 371 | 155k | pcm_transition_silk_size = ALLOC_NONE; | 372 | 155k | pcm_transition_celt_size = ALLOC_NONE; | 373 | 155k | if (data!=NULL && st->prev_mode > 0 && ( | 374 | 50.1k | (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy) | 375 | 50.1k | || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) | 376 | 155k | ) | 377 | 126 | { | 378 | 126 | transition = 1; | 379 | | /* Decide where to allocate the stack memory for pcm_transition */ | 380 | 126 | if (mode == MODE_CELT_ONLY) | 381 | 0 | pcm_transition_celt_size = F5*st->channels; | 382 | 126 | else | 383 | 126 | pcm_transition_silk_size = F5*st->channels; | 384 | 126 | } | 385 | 155k | ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_res); | 386 | 155k | if (transition && mode == MODE_CELT_ONLY) | 387 | 0 | { | 388 | 0 | pcm_transition = pcm_transition_celt; | 389 | 0 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); | 390 | 0 | } | 391 | 155k | if (audiosize > frame_size) | 392 | 0 | { | 393 | | /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/ | 394 | 0 | RESTORE_STACK; | 395 | 0 | return OPUS_BAD_ARG; | 396 | 155k | } else { | 397 | 155k | frame_size = audiosize; | 398 | 155k | } | 399 | | | 400 | | /* SILK processing */ | 401 | 155k | if (mode != MODE_CELT_ONLY) | 402 | 66.8k | { | 403 | 66.8k | int lost_flag, decoded_samples; | 404 | 66.8k | opus_res *pcm_ptr; | 405 | 66.8k | int pcm_too_small; | 406 | 66.8k | int pcm_silk_size = ALLOC_NONE; | 407 | 66.8k | VARDECL(opus_res, pcm_silk); | 408 | 66.8k | pcm_too_small = (frame_size < F10); | 409 | 66.8k | if (pcm_too_small) | 410 | 0 | pcm_silk_size = F10*st->channels; | 411 | 66.8k | ALLOC(pcm_silk, pcm_silk_size, opus_res); | 412 | 66.8k | if (pcm_too_small) | 413 | 0 | pcm_ptr = pcm_silk; | 414 | 66.8k | else | 415 | 66.8k | pcm_ptr = pcm; | 416 | | | 417 | 66.8k | if (st->prev_mode==MODE_CELT_ONLY) | 418 | 126 | silk_ResetDecoder( silk_dec ); | 419 | | | 420 | | /* The SILK PLC cannot produce frames of less than 10 ms */ | 421 | 66.8k | st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs); | 422 | | | 423 | 66.8k | if (data != NULL) | 424 | 42.7k | { | 425 | 42.7k | st->DecControl.nChannelsInternal = st->stream_channels; | 426 | 42.7k | if( mode == MODE_SILK_ONLY ) { | 427 | 31.2k | if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) { | 428 | 12.1k | st->DecControl.internalSampleRate = 8000; | 429 | 19.1k | } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) { | 430 | 8.84k | st->DecControl.internalSampleRate = 12000; | 431 | 10.2k | } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) { | 432 | 10.2k | st->DecControl.internalSampleRate = 16000; | 433 | 10.2k | } else { | 434 | 0 | st->DecControl.internalSampleRate = 16000; | 435 | 0 | celt_assert( 0 ); | 436 | 0 | } | 437 | 31.2k | } else { | 438 | | /* Hybrid mode */ | 439 | 11.4k | st->DecControl.internalSampleRate = 16000; | 440 | 11.4k | } | 441 | 42.7k | } | 442 | 66.8k | st->DecControl.enable_deep_plc = st->complexity >= 5; | 443 | | #ifdef ENABLE_OSCE | 444 | | st->DecControl.osce_method = OSCE_METHOD_NONE; | 445 | | #ifndef DISABLE_LACE | 446 | | if (st->complexity >= 6) {st->DecControl.osce_method = OSCE_METHOD_LACE;} | 447 | | #endif | 448 | | #ifndef DISABLE_NOLACE | 449 | | if (st->complexity >= 7) {st->DecControl.osce_method = OSCE_METHOD_NOLACE;} | 450 | | #endif | 451 | | #ifdef ENABLE_OSCE_BWE | 452 | | if (st->complexity >= 4 && st->DecControl.enable_osce_bwe && | 453 | | st->Fs == 48000 && st->DecControl.internalSampleRate == 16000 && | 454 | | ((mode == MODE_SILK_ONLY) || (data == NULL))) { | 455 | | /* request WB -> FB signal extension */ | 456 | | st->DecControl.osce_extended_mode = OSCE_MODE_SILK_BBWE; | 457 | | } else { | 458 | | /* at this point, mode can only be MODE_SILK_ONLY or MODE_HYBRID */ | 459 | | st->DecControl.osce_extended_mode = mode == MODE_SILK_ONLY ? OSCE_MODE_SILK_ONLY : OSCE_MODE_HYBRID; | 460 | | } | 461 | | if (st->prev_mode == MODE_CELT_ONLY) { | 462 | | /* Update extended mode for CELT->SILK transition */ | 463 | | st->DecControl.prev_osce_extended_mode = OSCE_MODE_CELT_ONLY; | 464 | | } | 465 | | #endif | 466 | | #endif | 467 | | | 468 | 66.8k | lost_flag = data == NULL ? 1 : 2 * !!decode_fec; | 469 | 66.8k | decoded_samples = 0; | 470 | 101k | do { | 471 | | /* Call SILK decoder */ | 472 | 101k | int first_frame = decoded_samples == 0; | 473 | 101k | silk_ret = silk_Decode( silk_dec, &st->DecControl, | 474 | 101k | lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, | 475 | | #ifdef ENABLE_DEEP_PLC | 476 | | &st->lpcnet, | 477 | | #endif | 478 | 101k | st->arch ); | 479 | 101k | if( silk_ret ) { | 480 | 0 | if (lost_flag) { | 481 | | /* PLC failure should not be fatal */ | 482 | 0 | silk_frame_size = frame_size; | 483 | 0 | for (i=0;i<frame_size*st->channels;i++) | 484 | 0 | pcm_ptr[i] = 0; | 485 | 0 | } else { | 486 | 0 | RESTORE_STACK; | 487 | 0 | return OPUS_INTERNAL_ERROR; | 488 | 0 | } | 489 | 0 | } | 490 | 101k | pcm_ptr += silk_frame_size * st->channels; | 491 | 101k | decoded_samples += silk_frame_size; | 492 | 101k | } while( decoded_samples < frame_size ); | 493 | 66.8k | if (pcm_too_small) { | 494 | 0 | OPUS_COPY(pcm, pcm_silk, frame_size*st->channels); | 495 | 0 | } | 496 | 66.8k | } | 497 | | | 498 | 155k | start_band = 0; | 499 | 155k | if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL | 500 | 35.8k | && ec_tell(&dec)+17+20*(mode == MODE_HYBRID) <= 8*len) | 501 | 8.60k | { | 502 | | /* Check if we have a redundant 0-8 kHz band */ | 503 | 8.60k | if (mode == MODE_HYBRID) | 504 | 2.44k | redundancy = ec_dec_bit_logp(&dec, 12); | 505 | 6.16k | else | 506 | 6.16k | redundancy = 1; | 507 | 8.60k | if (redundancy) | 508 | 6.32k | { | 509 | 6.32k | celt_to_silk = ec_dec_bit_logp(&dec, 1); | 510 | | /* redundancy_bytes will be at least two, in the non-hybrid | 511 | | case due to the ec_tell() check above */ | 512 | 6.32k | redundancy_bytes = mode==MODE_HYBRID ? | 513 | 162 | (opus_int32)ec_dec_uint(&dec, 256)+2 : | 514 | 6.32k | len-((ec_tell(&dec)+7)>>3); | 515 | 6.32k | len -= redundancy_bytes; | 516 | | /* This is a sanity check. It should never happen for a valid | 517 | | packet, so the exact behaviour is not normative. */ | 518 | 6.32k | if (len*8 < ec_tell(&dec)) | 519 | 111 | { | 520 | 111 | len = 0; | 521 | 111 | redundancy_bytes = 0; | 522 | 111 | redundancy = 0; | 523 | 111 | } | 524 | | /* Shrink decoder because of raw bits */ | 525 | 6.32k | dec.storage -= redundancy_bytes; | 526 | 6.32k | } | 527 | 8.60k | } | 528 | 155k | if (mode != MODE_CELT_ONLY) | 529 | 66.8k | start_band = 17; | 530 | | | 531 | 155k | if (redundancy) | 532 | 6.21k | { | 533 | 6.21k | transition = 0; | 534 | 6.21k | pcm_transition_silk_size=ALLOC_NONE; | 535 | 6.21k | } | 536 | | | 537 | 155k | ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_res); | 538 | | | 539 | 155k | if (transition && mode != MODE_CELT_ONLY) | 540 | 94 | { | 541 | 94 | pcm_transition = pcm_transition_silk; | 542 | 94 | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); | 543 | 94 | } | 544 | | | 545 | | | 546 | 155k | if (bandwidth) | 547 | 95.3k | { | 548 | 95.3k | int endband=21; | 549 | | | 550 | 95.3k | switch(bandwidth) | 551 | 95.3k | { | 552 | 23.1k | case OPUS_BANDWIDTH_NARROWBAND: | 553 | 23.1k | endband = 13; | 554 | 23.1k | break; | 555 | 8.84k | case OPUS_BANDWIDTH_MEDIUMBAND: | 556 | 36.9k | case OPUS_BANDWIDTH_WIDEBAND: | 557 | 36.9k | endband = 17; | 558 | 36.9k | break; | 559 | 14.3k | case OPUS_BANDWIDTH_SUPERWIDEBAND: | 560 | 14.3k | endband = 19; | 561 | 14.3k | break; | 562 | 20.9k | case OPUS_BANDWIDTH_FULLBAND: | 563 | 20.9k | endband = 21; | 564 | 20.9k | break; | 565 | 0 | default: | 566 | 0 | celt_assert(0); | 567 | 0 | break; | 568 | 95.3k | } | 569 | 95.3k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband))); | 570 | 95.3k | } | 571 | 155k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels))); | 572 | | | 573 | | /* Only allocation memory for redundancy if/when needed */ | 574 | 155k | redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE; | 575 | 155k | ALLOC(redundant_audio, redundant_audio_size, opus_res); | 576 | | | 577 | | /* 5 ms redundant frame for CELT->SILK*/ | 578 | 155k | if (redundancy && celt_to_silk) | 579 | 3.73k | { | 580 | | /* If the previous frame did not use CELT (the first redundancy frame in | 581 | | a transition from SILK may have been lost) then the CELT decoder is | 582 | | stale at this point and the redundancy audio is not useful, however | 583 | | the final range is still needed (for testing), so the redundancy is | 584 | | always decoded but the decoded audio may not be used */ | 585 | 3.73k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 586 | 3.73k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, | 587 | 3.73k | redundant_audio, F5, NULL, 0); | 588 | 3.73k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); | 589 | 3.73k | } | 590 | | | 591 | | /* MUST be after PLC */ | 592 | 155k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band))); | 593 | | | 594 | | #ifdef ENABLE_OSCE_BWE | 595 | | if (mode != MODE_SILK_ONLY && st->DecControl.osce_extended_mode != OSCE_MODE_SILK_BBWE) | 596 | | #else | 597 | 155k | if (mode != MODE_SILK_ONLY) | 598 | 109k | #endif | 599 | 109k | { | 600 | 109k | int celt_frame_size = IMIN(F20, frame_size); | 601 | | /* Make sure to discard any previous CELT state */ | 602 | 109k | if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy) | 603 | 109k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); | 604 | | /* Decode CELT */ | 605 | 109k | celt_ret = celt_decode_with_ec_dred(celt_dec, decode_fec ? NULL : data, | 606 | 109k | len, pcm, celt_frame_size, &dec, celt_accum | 607 | | #ifdef ENABLE_DEEP_PLC | 608 | | , &st->lpcnet | 609 | | #endif | 610 | 109k | ARG_QEXT(ext ? ext->data : NULL) ARG_QEXT(ext ? ext->len : 0)); | 611 | 109k | celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&st->rangeFinal)); | 612 | 109k | } else { | 613 | 45.4k | unsigned char silence[2] = {0xFF, 0xFF}; | 614 | 45.4k | if (!celt_accum) | 615 | 0 | { | 616 | 0 | for (i=0;i<frame_size*st->channels;i++) | 617 | 0 | pcm[i] = 0; | 618 | 0 | } | 619 | | /* For hybrid -> SILK transitions, we let the CELT MDCT | 620 | | do a fade-out by decoding a silence frame */ | 621 | 45.4k | if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) ) | 622 | 0 | { | 623 | 0 | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 624 | 0 | celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum); | 625 | 0 | } | 626 | 45.4k | st->rangeFinal = dec.rng; | 627 | 45.4k | } | 628 | | | 629 | 155k | { | 630 | 155k | const CELTMode *celt_mode; | 631 | 155k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode))); | 632 | 155k | window = celt_mode->window; | 633 | 155k | } | 634 | | | 635 | | /* 5 ms redundant frame for SILK->CELT */ | 636 | 155k | if (redundancy && !celt_to_silk) | 637 | 2.47k | { | 638 | 2.47k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); | 639 | 2.47k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); | 640 | | | 641 | 2.47k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0); | 642 | 2.47k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); | 643 | 2.47k | smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5, | 644 | 2.47k | pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs); | 645 | 2.47k | } | 646 | | /* 5ms redundant frame for CELT->SILK; ignore if the previous frame did not | 647 | | use CELT (the first redundancy frame in a transition from SILK may have | 648 | | been lost) */ | 649 | 155k | if (redundancy && celt_to_silk && (st->prev_mode != MODE_SILK_ONLY || st->prev_redundancy)) | 650 | 2.48k | { | 651 | 6.33k | for (c=0;c<st->channels;c++) | 652 | 3.85k | { | 653 | 210k | for (i=0;i<F2_5;i++) | 654 | 206k | pcm[st->channels*i+c] = redundant_audio[st->channels*i+c]; | 655 | 3.85k | } | 656 | 2.48k | smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5, | 657 | 2.48k | pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs); | 658 | 2.48k | } | 659 | 155k | if (transition) | 660 | 94 | { | 661 | 94 | if (audiosize >= F5) | 662 | 94 | { | 663 | 9.03k | for (i=0;i<st->channels*F2_5;i++) | 664 | 8.94k | pcm[i] = pcm_transition[i]; | 665 | 94 | smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5, | 666 | 94 | pcm+st->channels*F2_5, F2_5, | 667 | 94 | st->channels, window, st->Fs); | 668 | 94 | } else { | 669 | | /* Not enough time to do a clean transition, but we do it anyway | 670 | | This will not preserve amplitude perfectly and may introduce | 671 | | a bit of temporal aliasing, but it shouldn't be too bad and | 672 | | that's pretty much the best we can do. In any case, generating this | 673 | | transition it pretty silly in the first place */ | 674 | 0 | smooth_fade(pcm_transition, pcm, | 675 | 0 | pcm, F2_5, | 676 | 0 | st->channels, window, st->Fs); | 677 | 0 | } | 678 | 94 | } | 679 | | | 680 | 155k | if(st->decode_gain) | 681 | 0 | { | 682 | 0 | opus_val32 gain; | 683 | 0 | gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain)); | 684 | 0 | for (i=0;i<frame_size*st->channels;i++) | 685 | 0 | { | 686 | 0 | opus_val32 x; | 687 | 0 | #ifdef ENABLE_RES24 | 688 | 0 | x = MULT32_32_Q16(pcm[i],gain); | 689 | | #else | 690 | | x = MULT16_32_P16(pcm[i],gain); | 691 | | #endif | 692 | 0 | pcm[i] = SATURATE(x, 32767); | 693 | 0 | } | 694 | 0 | } | 695 | | | 696 | 155k | if (len <= 1) | 697 | 60.0k | st->rangeFinal = 0; | 698 | 95.2k | else | 699 | 95.2k | st->rangeFinal ^= redundant_rng; | 700 | | | 701 | 155k | st->prev_mode = mode; | 702 | 155k | st->prev_redundancy = redundancy && !celt_to_silk; | 703 | | | 704 | 155k | if (celt_ret>=0) | 705 | 155k | { | 706 | 155k | if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels)) | 707 | 0 | OPUS_PRINT_INT(audiosize); | 708 | 155k | } | 709 | | | 710 | 155k | RESTORE_STACK; | 711 | 155k | return celt_ret < 0 ? celt_ret : audiosize; | 712 | | | 713 | 155k | } |
|
714 | | |
715 | | int opus_decode_native(OpusDecoder *st, const unsigned char *data, |
716 | | opus_int32 len, opus_res *pcm, int frame_size, int decode_fec, |
717 | | int self_delimited, opus_int32 *packet_offset, int soft_clip, const OpusDRED *dred, opus_int32 dred_offset) |
718 | 218k | { |
719 | 218k | int i, nb_samples; |
720 | 218k | int count, offset; |
721 | 218k | unsigned char toc; |
722 | 218k | int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels; |
723 | | /* 48 x 2.5 ms = 120 ms */ |
724 | 218k | opus_int16 size[48]; |
725 | 218k | const unsigned char *padding; |
726 | 218k | opus_int32 padding_len; |
727 | 218k | OpusExtensionIterator iter; |
728 | 218k | VALIDATE_OPUS_DECODER(st); |
729 | 218k | if (decode_fec<0 || decode_fec>1) |
730 | 0 | return OPUS_BAD_ARG; |
731 | | /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */ |
732 | 218k | if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0) |
733 | 0 | return OPUS_BAD_ARG; |
734 | | #ifdef ENABLE_DRED |
735 | | if (dred != NULL && dred->process_stage == 2) { |
736 | | int F10; |
737 | | int features_per_frame; |
738 | | int needed_feature_frames; |
739 | | int init_frames; |
740 | | lpcnet_plc_fec_clear(&st->lpcnet); |
741 | | F10 = st->Fs/100; |
742 | | /* if blend==0, the last PLC call was "update" and we need to feed two extra 10-ms frames. */ |
743 | | init_frames = (st->lpcnet.blend == 0) ? 2 : 0; |
744 | | features_per_frame = IMAX(1, frame_size/F10); |
745 | | needed_feature_frames = init_frames + features_per_frame; |
746 | | lpcnet_plc_fec_clear(&st->lpcnet); |
747 | | for (i=0;i<needed_feature_frames;i++) { |
748 | | int feature_offset; |
749 | | /* We floor instead of rounding because 5-ms overlap compensates for the missing 0.5 rounding offset. */ |
750 | | feature_offset = init_frames - i - 2 + (int)floor(((float)dred_offset + dred->dred_offset*F10/4)/F10); |
751 | | if (feature_offset <= 4*dred->nb_latents-1 && feature_offset >= 0) { |
752 | | lpcnet_plc_fec_add(&st->lpcnet, dred->fec_features+feature_offset*DRED_NUM_FEATURES); |
753 | | } else { |
754 | | if (feature_offset >= 0) lpcnet_plc_fec_add(&st->lpcnet, NULL); |
755 | | } |
756 | | |
757 | | } |
758 | | } |
759 | | #else |
760 | 218k | (void)dred; |
761 | 218k | (void)dred_offset; |
762 | 218k | #endif |
763 | 218k | if (len==0 || data==NULL) |
764 | 69.2k | { |
765 | 69.2k | int pcm_count=0; |
766 | 1.53M | do { |
767 | 1.53M | int ret; |
768 | 1.53M | ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0 ARG_QEXT(NULL)); |
769 | 1.53M | if (ret<0) |
770 | 0 | return ret; |
771 | 1.53M | pcm_count += ret; |
772 | 1.53M | } while (pcm_count < frame_size); |
773 | 69.2k | celt_assert(pcm_count == frame_size); |
774 | 69.2k | if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels)) |
775 | 0 | OPUS_PRINT_INT(pcm_count); |
776 | 69.2k | st->last_packet_duration = pcm_count; |
777 | 69.2k | return pcm_count; |
778 | 149k | } else if (len<0) |
779 | 0 | return OPUS_BAD_ARG; |
780 | | |
781 | 149k | packet_mode = opus_packet_get_mode(data); |
782 | 149k | packet_bandwidth = opus_packet_get_bandwidth(data); |
783 | 149k | packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs); |
784 | 149k | packet_stream_channels = opus_packet_get_nb_channels(data); |
785 | | |
786 | 149k | count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, |
787 | 149k | size, &offset, packet_offset, &padding, &padding_len); |
788 | 149k | if (st->ignore_extensions) { |
789 | 0 | padding = NULL; |
790 | 0 | padding_len = 0; |
791 | 0 | } |
792 | 149k | if (count<0) |
793 | 0 | return count; |
794 | 149k | opus_extension_iterator_init(&iter, padding, padding_len, count); |
795 | | |
796 | 149k | data += offset; |
797 | | |
798 | 149k | if (decode_fec) |
799 | 30.1k | { |
800 | 30.1k | int duration_copy; |
801 | 30.1k | int ret; |
802 | | /* If no FEC can be present, run the PLC (recursive call) */ |
803 | 30.1k | if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY) |
804 | 1.77k | return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip, NULL, 0); |
805 | | /* Otherwise, run the PLC on everything except the size for which we might have FEC */ |
806 | 28.3k | duration_copy = st->last_packet_duration; |
807 | 28.3k | if (frame_size-packet_frame_size!=0) |
808 | 23.0k | { |
809 | 23.0k | ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip, NULL, 0); |
810 | 23.0k | if (ret<0) |
811 | 0 | { |
812 | 0 | st->last_packet_duration = duration_copy; |
813 | 0 | return ret; |
814 | 0 | } |
815 | 23.0k | celt_assert(ret==frame_size-packet_frame_size); |
816 | 23.0k | } |
817 | | /* Complete with FEC */ |
818 | 28.3k | st->mode = packet_mode; |
819 | 28.3k | st->bandwidth = packet_bandwidth; |
820 | 28.3k | st->frame_size = packet_frame_size; |
821 | 28.3k | st->stream_channels = packet_stream_channels; |
822 | 28.3k | ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size), |
823 | 28.3k | packet_frame_size, 1 ARG_QEXT(NULL)); |
824 | 28.3k | if (ret<0) |
825 | 0 | return ret; |
826 | 28.3k | else { |
827 | 28.3k | if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels)) |
828 | 0 | OPUS_PRINT_INT(frame_size); |
829 | 28.3k | st->last_packet_duration = frame_size; |
830 | 28.3k | return frame_size; |
831 | 28.3k | } |
832 | 28.3k | } |
833 | | |
834 | 119k | if (count*packet_frame_size > frame_size) |
835 | 0 | return OPUS_BUFFER_TOO_SMALL; |
836 | | |
837 | | /* Update the state as the last step to avoid updating it on an invalid packet */ |
838 | 119k | st->mode = packet_mode; |
839 | 119k | st->bandwidth = packet_bandwidth; |
840 | 119k | st->frame_size = packet_frame_size; |
841 | 119k | st->stream_channels = packet_stream_channels; |
842 | | |
843 | 119k | nb_samples=0; |
844 | 499k | for (i=0;i<count;i++) |
845 | 379k | { |
846 | 379k | int ret; |
847 | | #ifdef ENABLE_QEXT |
848 | | opus_extension_data ext; |
849 | | ext.frame = -1; |
850 | | ext.data = NULL; |
851 | | ext.len = 0; |
852 | | ext.id = -1; |
853 | 198k | while (ext.frame < i) { |
854 | 189k | OpusExtensionIterator iter_copy; |
855 | 189k | iter_copy = iter; |
856 | 189k | ret = opus_extension_iterator_find(&iter, &ext, QEXT_EXTENSION_ID); |
857 | 189k | if (ret <= 0) break; |
858 | 9.52k | if (ext.frame > i) iter = iter_copy; |
859 | 9.52k | } |
860 | 189k | if (ext.frame != i) ext.data = NULL; |
861 | | #endif |
862 | 379k | ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0 ARG_QEXT(&ext)); |
863 | 379k | if (ret<0) |
864 | 2 | return ret; |
865 | 379k | celt_assert(ret==packet_frame_size); |
866 | 379k | data += size[i]; |
867 | 379k | nb_samples += ret; |
868 | 379k | } |
869 | 119k | st->last_packet_duration = nb_samples; |
870 | 119k | if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels)) |
871 | 0 | OPUS_PRINT_INT(nb_samples); |
872 | | #ifndef FIXED_POINT |
873 | | if (soft_clip) |
874 | | opus_pcm_soft_clip_impl(pcm, nb_samples, st->channels, st->softclip_mem, st->arch); |
875 | | else |
876 | | st->softclip_mem[0]=st->softclip_mem[1]=0; |
877 | | #endif |
878 | 119k | return nb_samples; |
879 | 119k | } Line | Count | Source | 718 | 99.8k | { | 719 | 99.8k | int i, nb_samples; | 720 | 99.8k | int count, offset; | 721 | 99.8k | unsigned char toc; | 722 | 99.8k | int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels; | 723 | | /* 48 x 2.5 ms = 120 ms */ | 724 | 99.8k | opus_int16 size[48]; | 725 | 99.8k | const unsigned char *padding; | 726 | 99.8k | opus_int32 padding_len; | 727 | 99.8k | OpusExtensionIterator iter; | 728 | 99.8k | VALIDATE_OPUS_DECODER(st); | 729 | 99.8k | if (decode_fec<0 || decode_fec>1) | 730 | 0 | return OPUS_BAD_ARG; | 731 | | /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */ | 732 | 99.8k | if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0) | 733 | 0 | return OPUS_BAD_ARG; | 734 | | #ifdef ENABLE_DRED | 735 | | if (dred != NULL && dred->process_stage == 2) { | 736 | | int F10; | 737 | | int features_per_frame; | 738 | | int needed_feature_frames; | 739 | | int init_frames; | 740 | | lpcnet_plc_fec_clear(&st->lpcnet); | 741 | | F10 = st->Fs/100; | 742 | | /* if blend==0, the last PLC call was "update" and we need to feed two extra 10-ms frames. */ | 743 | | init_frames = (st->lpcnet.blend == 0) ? 2 : 0; | 744 | | features_per_frame = IMAX(1, frame_size/F10); | 745 | | needed_feature_frames = init_frames + features_per_frame; | 746 | | lpcnet_plc_fec_clear(&st->lpcnet); | 747 | | for (i=0;i<needed_feature_frames;i++) { | 748 | | int feature_offset; | 749 | | /* We floor instead of rounding because 5-ms overlap compensates for the missing 0.5 rounding offset. */ | 750 | | feature_offset = init_frames - i - 2 + (int)floor(((float)dred_offset + dred->dred_offset*F10/4)/F10); | 751 | | if (feature_offset <= 4*dred->nb_latents-1 && feature_offset >= 0) { | 752 | | lpcnet_plc_fec_add(&st->lpcnet, dred->fec_features+feature_offset*DRED_NUM_FEATURES); | 753 | | } else { | 754 | | if (feature_offset >= 0) lpcnet_plc_fec_add(&st->lpcnet, NULL); | 755 | | } | 756 | | | 757 | | } | 758 | | } | 759 | | #else | 760 | 99.8k | (void)dred; | 761 | 99.8k | (void)dred_offset; | 762 | 99.8k | #endif | 763 | 99.8k | if (len==0 || data==NULL) | 764 | 32.7k | { | 765 | 32.7k | int pcm_count=0; | 766 | 710k | do { | 767 | 710k | int ret; | 768 | 710k | ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0 ARG_QEXT(NULL)); | 769 | 710k | if (ret<0) | 770 | 0 | return ret; | 771 | 710k | pcm_count += ret; | 772 | 710k | } while (pcm_count < frame_size); | 773 | 32.7k | celt_assert(pcm_count == frame_size); | 774 | 32.7k | if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels)) | 775 | 0 | OPUS_PRINT_INT(pcm_count); | 776 | 32.7k | st->last_packet_duration = pcm_count; | 777 | 32.7k | return pcm_count; | 778 | 67.1k | } else if (len<0) | 779 | 0 | return OPUS_BAD_ARG; | 780 | | | 781 | 67.1k | packet_mode = opus_packet_get_mode(data); | 782 | 67.1k | packet_bandwidth = opus_packet_get_bandwidth(data); | 783 | 67.1k | packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs); | 784 | 67.1k | packet_stream_channels = opus_packet_get_nb_channels(data); | 785 | | | 786 | 67.1k | count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, | 787 | 67.1k | size, &offset, packet_offset, &padding, &padding_len); | 788 | 67.1k | if (st->ignore_extensions) { | 789 | 0 | padding = NULL; | 790 | 0 | padding_len = 0; | 791 | 0 | } | 792 | 67.1k | if (count<0) | 793 | 0 | return count; | 794 | 67.1k | opus_extension_iterator_init(&iter, padding, padding_len, count); | 795 | | | 796 | 67.1k | data += offset; | 797 | | | 798 | 67.1k | if (decode_fec) | 799 | 14.0k | { | 800 | 14.0k | int duration_copy; | 801 | 14.0k | int ret; | 802 | | /* If no FEC can be present, run the PLC (recursive call) */ | 803 | 14.0k | if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY) | 804 | 856 | return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip, NULL, 0); | 805 | | /* Otherwise, run the PLC on everything except the size for which we might have FEC */ | 806 | 13.2k | duration_copy = st->last_packet_duration; | 807 | 13.2k | if (frame_size-packet_frame_size!=0) | 808 | 10.4k | { | 809 | 10.4k | ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip, NULL, 0); | 810 | 10.4k | if (ret<0) | 811 | 0 | { | 812 | 0 | st->last_packet_duration = duration_copy; | 813 | 0 | return ret; | 814 | 0 | } | 815 | 10.4k | celt_assert(ret==frame_size-packet_frame_size); | 816 | 10.4k | } | 817 | | /* Complete with FEC */ | 818 | 13.2k | st->mode = packet_mode; | 819 | 13.2k | st->bandwidth = packet_bandwidth; | 820 | 13.2k | st->frame_size = packet_frame_size; | 821 | 13.2k | st->stream_channels = packet_stream_channels; | 822 | 13.2k | ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size), | 823 | 13.2k | packet_frame_size, 1 ARG_QEXT(NULL)); | 824 | 13.2k | if (ret<0) | 825 | 0 | return ret; | 826 | 13.2k | else { | 827 | 13.2k | if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels)) | 828 | 0 | OPUS_PRINT_INT(frame_size); | 829 | 13.2k | st->last_packet_duration = frame_size; | 830 | 13.2k | return frame_size; | 831 | 13.2k | } | 832 | 13.2k | } | 833 | | | 834 | 53.0k | if (count*packet_frame_size > frame_size) | 835 | 0 | return OPUS_BUFFER_TOO_SMALL; | 836 | | | 837 | | /* Update the state as the last step to avoid updating it on an invalid packet */ | 838 | 53.0k | st->mode = packet_mode; | 839 | 53.0k | st->bandwidth = packet_bandwidth; | 840 | 53.0k | st->frame_size = packet_frame_size; | 841 | 53.0k | st->stream_channels = packet_stream_channels; | 842 | | | 843 | 53.0k | nb_samples=0; | 844 | 243k | for (i=0;i<count;i++) | 845 | 190k | { | 846 | 190k | int ret; | 847 | | #ifdef ENABLE_QEXT | 848 | | opus_extension_data ext; | 849 | | ext.frame = -1; | 850 | | ext.data = NULL; | 851 | | ext.len = 0; | 852 | | ext.id = -1; | 853 | | while (ext.frame < i) { | 854 | | OpusExtensionIterator iter_copy; | 855 | | iter_copy = iter; | 856 | | ret = opus_extension_iterator_find(&iter, &ext, QEXT_EXTENSION_ID); | 857 | | if (ret <= 0) break; | 858 | | if (ext.frame > i) iter = iter_copy; | 859 | | } | 860 | | if (ext.frame != i) ext.data = NULL; | 861 | | #endif | 862 | 190k | ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0 ARG_QEXT(&ext)); | 863 | 190k | if (ret<0) | 864 | 2 | return ret; | 865 | 190k | celt_assert(ret==packet_frame_size); | 866 | 190k | data += size[i]; | 867 | 190k | nb_samples += ret; | 868 | 190k | } | 869 | 53.0k | st->last_packet_duration = nb_samples; | 870 | 53.0k | if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels)) | 871 | 0 | OPUS_PRINT_INT(nb_samples); | 872 | | #ifndef FIXED_POINT | 873 | | if (soft_clip) | 874 | | opus_pcm_soft_clip_impl(pcm, nb_samples, st->channels, st->softclip_mem, st->arch); | 875 | | else | 876 | | st->softclip_mem[0]=st->softclip_mem[1]=0; | 877 | | #endif | 878 | 53.0k | return nb_samples; | 879 | 53.0k | } |
Line | Count | Source | 718 | 119k | { | 719 | 119k | int i, nb_samples; | 720 | 119k | int count, offset; | 721 | 119k | unsigned char toc; | 722 | 119k | int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels; | 723 | | /* 48 x 2.5 ms = 120 ms */ | 724 | 119k | opus_int16 size[48]; | 725 | 119k | const unsigned char *padding; | 726 | 119k | opus_int32 padding_len; | 727 | 119k | OpusExtensionIterator iter; | 728 | 119k | VALIDATE_OPUS_DECODER(st); | 729 | 119k | if (decode_fec<0 || decode_fec>1) | 730 | 0 | return OPUS_BAD_ARG; | 731 | | /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */ | 732 | 119k | if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0) | 733 | 0 | return OPUS_BAD_ARG; | 734 | | #ifdef ENABLE_DRED | 735 | | if (dred != NULL && dred->process_stage == 2) { | 736 | | int F10; | 737 | | int features_per_frame; | 738 | | int needed_feature_frames; | 739 | | int init_frames; | 740 | | lpcnet_plc_fec_clear(&st->lpcnet); | 741 | | F10 = st->Fs/100; | 742 | | /* if blend==0, the last PLC call was "update" and we need to feed two extra 10-ms frames. */ | 743 | | init_frames = (st->lpcnet.blend == 0) ? 2 : 0; | 744 | | features_per_frame = IMAX(1, frame_size/F10); | 745 | | needed_feature_frames = init_frames + features_per_frame; | 746 | | lpcnet_plc_fec_clear(&st->lpcnet); | 747 | | for (i=0;i<needed_feature_frames;i++) { | 748 | | int feature_offset; | 749 | | /* We floor instead of rounding because 5-ms overlap compensates for the missing 0.5 rounding offset. */ | 750 | | feature_offset = init_frames - i - 2 + (int)floor(((float)dred_offset + dred->dred_offset*F10/4)/F10); | 751 | | if (feature_offset <= 4*dred->nb_latents-1 && feature_offset >= 0) { | 752 | | lpcnet_plc_fec_add(&st->lpcnet, dred->fec_features+feature_offset*DRED_NUM_FEATURES); | 753 | | } else { | 754 | | if (feature_offset >= 0) lpcnet_plc_fec_add(&st->lpcnet, NULL); | 755 | | } | 756 | | | 757 | | } | 758 | | } | 759 | | #else | 760 | 119k | (void)dred; | 761 | 119k | (void)dred_offset; | 762 | 119k | #endif | 763 | 119k | if (len==0 || data==NULL) | 764 | 36.5k | { | 765 | 36.5k | int pcm_count=0; | 766 | 820k | do { | 767 | 820k | int ret; | 768 | 820k | ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0 ARG_QEXT(NULL)); | 769 | 820k | if (ret<0) | 770 | 0 | return ret; | 771 | 820k | pcm_count += ret; | 772 | 820k | } while (pcm_count < frame_size); | 773 | 36.5k | celt_assert(pcm_count == frame_size); | 774 | 36.5k | if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels)) | 775 | 0 | OPUS_PRINT_INT(pcm_count); | 776 | 36.5k | st->last_packet_duration = pcm_count; | 777 | 36.5k | return pcm_count; | 778 | 82.4k | } else if (len<0) | 779 | 0 | return OPUS_BAD_ARG; | 780 | | | 781 | 82.4k | packet_mode = opus_packet_get_mode(data); | 782 | 82.4k | packet_bandwidth = opus_packet_get_bandwidth(data); | 783 | 82.4k | packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs); | 784 | 82.4k | packet_stream_channels = opus_packet_get_nb_channels(data); | 785 | | | 786 | 82.4k | count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, | 787 | 82.4k | size, &offset, packet_offset, &padding, &padding_len); | 788 | 82.4k | if (st->ignore_extensions) { | 789 | 0 | padding = NULL; | 790 | 0 | padding_len = 0; | 791 | 0 | } | 792 | 82.4k | if (count<0) | 793 | 0 | return count; | 794 | 82.4k | opus_extension_iterator_init(&iter, padding, padding_len, count); | 795 | | | 796 | 82.4k | data += offset; | 797 | | | 798 | 82.4k | if (decode_fec) | 799 | 16.0k | { | 800 | 16.0k | int duration_copy; | 801 | 16.0k | int ret; | 802 | | /* If no FEC can be present, run the PLC (recursive call) */ | 803 | 16.0k | if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY) | 804 | 918 | return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip, NULL, 0); | 805 | | /* Otherwise, run the PLC on everything except the size for which we might have FEC */ | 806 | 15.1k | duration_copy = st->last_packet_duration; | 807 | 15.1k | if (frame_size-packet_frame_size!=0) | 808 | 12.6k | { | 809 | 12.6k | ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip, NULL, 0); | 810 | 12.6k | if (ret<0) | 811 | 0 | { | 812 | 0 | st->last_packet_duration = duration_copy; | 813 | 0 | return ret; | 814 | 0 | } | 815 | 12.6k | celt_assert(ret==frame_size-packet_frame_size); | 816 | 12.6k | } | 817 | | /* Complete with FEC */ | 818 | 15.1k | st->mode = packet_mode; | 819 | 15.1k | st->bandwidth = packet_bandwidth; | 820 | 15.1k | st->frame_size = packet_frame_size; | 821 | 15.1k | st->stream_channels = packet_stream_channels; | 822 | 15.1k | ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size), | 823 | 15.1k | packet_frame_size, 1 ARG_QEXT(NULL)); | 824 | 15.1k | if (ret<0) | 825 | 0 | return ret; | 826 | 15.1k | else { | 827 | 15.1k | if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels)) | 828 | 0 | OPUS_PRINT_INT(frame_size); | 829 | 15.1k | st->last_packet_duration = frame_size; | 830 | 15.1k | return frame_size; | 831 | 15.1k | } | 832 | 15.1k | } | 833 | | | 834 | 66.3k | if (count*packet_frame_size > frame_size) | 835 | 0 | return OPUS_BUFFER_TOO_SMALL; | 836 | | | 837 | | /* Update the state as the last step to avoid updating it on an invalid packet */ | 838 | 66.3k | st->mode = packet_mode; | 839 | 66.3k | st->bandwidth = packet_bandwidth; | 840 | 66.3k | st->frame_size = packet_frame_size; | 841 | 66.3k | st->stream_channels = packet_stream_channels; | 842 | | | 843 | 66.3k | nb_samples=0; | 844 | 255k | for (i=0;i<count;i++) | 845 | 189k | { | 846 | 189k | int ret; | 847 | 189k | #ifdef ENABLE_QEXT | 848 | 189k | opus_extension_data ext; | 849 | 189k | ext.frame = -1; | 850 | 189k | ext.data = NULL; | 851 | 189k | ext.len = 0; | 852 | 189k | ext.id = -1; | 853 | 198k | while (ext.frame < i) { | 854 | 189k | OpusExtensionIterator iter_copy; | 855 | 189k | iter_copy = iter; | 856 | 189k | ret = opus_extension_iterator_find(&iter, &ext, QEXT_EXTENSION_ID); | 857 | 189k | if (ret <= 0) break; | 858 | 9.52k | if (ext.frame > i) iter = iter_copy; | 859 | 9.52k | } | 860 | 189k | if (ext.frame != i) ext.data = NULL; | 861 | 189k | #endif | 862 | 189k | ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0 ARG_QEXT(&ext)); | 863 | 189k | if (ret<0) | 864 | 0 | return ret; | 865 | 189k | celt_assert(ret==packet_frame_size); | 866 | 189k | data += size[i]; | 867 | 189k | nb_samples += ret; | 868 | 189k | } | 869 | 66.3k | st->last_packet_duration = nb_samples; | 870 | 66.3k | if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels)) | 871 | 0 | OPUS_PRINT_INT(nb_samples); | 872 | | #ifndef FIXED_POINT | 873 | | if (soft_clip) | 874 | | opus_pcm_soft_clip_impl(pcm, nb_samples, st->channels, st->softclip_mem, st->arch); | 875 | | else | 876 | | st->softclip_mem[0]=st->softclip_mem[1]=0; | 877 | | #endif | 878 | 66.3k | return nb_samples; | 879 | 66.3k | } |
|
880 | | |
881 | | #ifdef FIXED_POINT |
882 | 0 | #define OPTIONAL_CLIP 0 |
883 | | #else |
884 | 0 | #define OPTIONAL_CLIP 1 |
885 | | #endif |
886 | | |
887 | | #if defined(FIXED_POINT) && !defined(ENABLE_RES24) |
888 | | int opus_decode(OpusDecoder *st, const unsigned char *data, |
889 | | opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec) |
890 | | { |
891 | | if(frame_size<=0) |
892 | | return OPUS_BAD_ARG; |
893 | | return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
894 | | } |
895 | | #else |
896 | | int opus_decode(OpusDecoder *st, const unsigned char *data, |
897 | | opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec) |
898 | 0 | { |
899 | 0 | VARDECL(opus_res, out); |
900 | 0 | int ret; |
901 | 0 | int nb_samples; |
902 | 0 | ALLOC_STACK; |
903 | |
|
904 | 0 | if(frame_size<=0) |
905 | 0 | { |
906 | 0 | RESTORE_STACK; |
907 | 0 | return OPUS_BAD_ARG; |
908 | 0 | } |
909 | 0 | if (data != NULL && len > 0 && !decode_fec) |
910 | 0 | { |
911 | 0 | nb_samples = opus_decoder_get_nb_samples(st, data, len); |
912 | 0 | if (nb_samples>0) |
913 | 0 | frame_size = IMIN(frame_size, nb_samples); |
914 | 0 | else |
915 | 0 | return OPUS_INVALID_PACKET; |
916 | 0 | } |
917 | 0 | celt_assert(st->channels == 1 || st->channels == 2); |
918 | 0 | ALLOC(out, frame_size*st->channels, opus_res); |
919 | |
|
920 | 0 | ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, OPTIONAL_CLIP, NULL, 0); |
921 | 0 | if (ret > 0) |
922 | 0 | { |
923 | | # if defined(FIXED_POINT) |
924 | | int i; |
925 | 0 | for (i=0;i<ret*st->channels;i++) |
926 | 0 | pcm[i] = RES2INT16(out[i]); |
927 | | # else |
928 | 0 | celt_float2int16(out, pcm, ret*st->channels, st->arch); |
929 | | # endif |
930 | 0 | } |
931 | 0 | RESTORE_STACK; |
932 | 0 | return ret; |
933 | 0 | } Unexecuted instantiation: opus_decode Unexecuted instantiation: opus_decode |
934 | | #endif |
935 | | |
936 | | #if defined(FIXED_POINT) && defined(ENABLE_RES24) |
937 | | int opus_decode24(OpusDecoder *st, const unsigned char *data, |
938 | | opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec) |
939 | 0 | { |
940 | 0 | if(frame_size<=0) |
941 | 0 | return OPUS_BAD_ARG; |
942 | 0 | return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
943 | 0 | } |
944 | | #else |
945 | | int opus_decode24(OpusDecoder *st, const unsigned char *data, |
946 | | opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec) |
947 | 0 | { |
948 | 0 | VARDECL(opus_res, out); |
949 | 0 | int ret, i; |
950 | 0 | int nb_samples; |
951 | 0 | ALLOC_STACK; |
952 | |
|
953 | 0 | if(frame_size<=0) |
954 | 0 | { |
955 | 0 | RESTORE_STACK; |
956 | 0 | return OPUS_BAD_ARG; |
957 | 0 | } |
958 | 0 | if (data != NULL && len > 0 && !decode_fec) |
959 | 0 | { |
960 | 0 | nb_samples = opus_decoder_get_nb_samples(st, data, len); |
961 | 0 | if (nb_samples>0) |
962 | 0 | frame_size = IMIN(frame_size, nb_samples); |
963 | 0 | else |
964 | 0 | return OPUS_INVALID_PACKET; |
965 | 0 | } |
966 | 0 | celt_assert(st->channels == 1 || st->channels == 2); |
967 | 0 | ALLOC(out, frame_size*st->channels, opus_res); |
968 | |
|
969 | 0 | ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
970 | 0 | if (ret > 0) |
971 | 0 | { |
972 | 0 | nb_samples = ret*st->channels; |
973 | 0 | for (i=0;i<nb_samples;i++) |
974 | 0 | pcm[i] = RES2INT24(out[i]); |
975 | 0 | } |
976 | 0 | RESTORE_STACK; |
977 | 0 | return ret; |
978 | 0 | } |
979 | | #endif |
980 | | |
981 | | |
982 | | #ifndef DISABLE_FLOAT_API |
983 | | |
984 | | # if !defined(FIXED_POINT) |
985 | | int opus_decode_float(OpusDecoder *st, const unsigned char *data, |
986 | | opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) |
987 | 0 | { |
988 | 0 | if(frame_size<=0) |
989 | 0 | return OPUS_BAD_ARG; |
990 | 0 | return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
991 | 0 | } |
992 | | # else |
993 | | int opus_decode_float(OpusDecoder *st, const unsigned char *data, |
994 | | opus_int32 len, float *pcm, int frame_size, int decode_fec) |
995 | 0 | { |
996 | 0 | VARDECL(opus_res, out); |
997 | 0 | int ret, i; |
998 | 0 | int nb_samples; |
999 | 0 | ALLOC_STACK; |
1000 | |
|
1001 | 0 | if(frame_size<=0) |
1002 | 0 | { |
1003 | 0 | RESTORE_STACK; |
1004 | 0 | return OPUS_BAD_ARG; |
1005 | 0 | } |
1006 | 0 | if (data != NULL && len > 0 && !decode_fec) |
1007 | 0 | { |
1008 | 0 | nb_samples = opus_decoder_get_nb_samples(st, data, len); |
1009 | 0 | if (nb_samples>0) |
1010 | 0 | frame_size = IMIN(frame_size, nb_samples); |
1011 | 0 | else |
1012 | 0 | return OPUS_INVALID_PACKET; |
1013 | 0 | } |
1014 | 0 | celt_assert(st->channels == 1 || st->channels == 2); |
1015 | 0 | ALLOC(out, frame_size*st->channels, opus_res); |
1016 | |
|
1017 | 0 | ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
1018 | 0 | if (ret > 0) |
1019 | 0 | { |
1020 | 0 | for (i=0;i<ret*st->channels;i++) |
1021 | 0 | pcm[i] = RES2FLOAT(out[i]); |
1022 | 0 | } |
1023 | 0 | RESTORE_STACK; |
1024 | 0 | return ret; |
1025 | 0 | } |
1026 | | # endif |
1027 | | |
1028 | | #endif |
1029 | | |
1030 | | |
1031 | | int opus_decoder_ctl(OpusDecoder *st, int request, ...) |
1032 | 53.6k | { |
1033 | 53.6k | int ret = OPUS_OK; |
1034 | 53.6k | va_list ap; |
1035 | 53.6k | void *silk_dec; |
1036 | 53.6k | CELTDecoder *celt_dec; |
1037 | | |
1038 | 53.6k | silk_dec = (char*)st+st->silk_dec_offset; |
1039 | 53.6k | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); |
1040 | | |
1041 | | |
1042 | 53.6k | va_start(ap, request); |
1043 | | |
1044 | 53.6k | switch (request) |
1045 | 53.6k | { |
1046 | 0 | case OPUS_GET_BANDWIDTH_REQUEST: |
1047 | 0 | { |
1048 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1049 | 0 | if (!value) |
1050 | 0 | { |
1051 | 0 | goto bad_arg; |
1052 | 0 | } |
1053 | 0 | *value = st->bandwidth; |
1054 | 0 | } |
1055 | 0 | break; |
1056 | 0 | case OPUS_SET_COMPLEXITY_REQUEST: |
1057 | 0 | { |
1058 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
1059 | 0 | if(value<0 || value>10) |
1060 | 0 | { |
1061 | 0 | goto bad_arg; |
1062 | 0 | } |
1063 | 0 | st->complexity = value; |
1064 | 0 | celt_decoder_ctl(celt_dec, OPUS_SET_COMPLEXITY(value)); |
1065 | 0 | } |
1066 | 0 | break; |
1067 | 0 | case OPUS_GET_COMPLEXITY_REQUEST: |
1068 | 0 | { |
1069 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1070 | 0 | if (!value) |
1071 | 0 | { |
1072 | 0 | goto bad_arg; |
1073 | 0 | } |
1074 | 0 | *value = st->complexity; |
1075 | 0 | } |
1076 | 0 | break; |
1077 | | #ifdef ENABLE_OSCE_BWE |
1078 | | case OPUS_SET_OSCE_BWE_REQUEST: |
1079 | | { |
1080 | | opus_int32 value = va_arg(ap, opus_int32); |
1081 | | if(value<0 || value>1) |
1082 | | { goto bad_arg; |
1083 | | } |
1084 | | st->DecControl.enable_osce_bwe = value; |
1085 | | |
1086 | | } |
1087 | | break; |
1088 | | case OPUS_GET_OSCE_BWE_REQUEST: |
1089 | | { |
1090 | | opus_int32 *value = va_arg(ap, opus_int32*); |
1091 | | if (!value) |
1092 | | { |
1093 | | goto bad_arg; |
1094 | | } |
1095 | | *value = st->DecControl.enable_osce_bwe; |
1096 | | } |
1097 | | break; |
1098 | | #endif |
1099 | 0 | case OPUS_GET_FINAL_RANGE_REQUEST: |
1100 | 0 | { |
1101 | 0 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
1102 | 0 | if (!value) |
1103 | 0 | { |
1104 | 0 | goto bad_arg; |
1105 | 0 | } |
1106 | 0 | *value = st->rangeFinal; |
1107 | 0 | } |
1108 | 0 | break; |
1109 | 0 | case OPUS_RESET_STATE: |
1110 | 0 | { |
1111 | 0 | OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START, |
1112 | 0 | sizeof(OpusDecoder)- |
1113 | 0 | ((char*)&st->OPUS_DECODER_RESET_START - (char*)st)); |
1114 | |
|
1115 | 0 | celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); |
1116 | 0 | silk_ResetDecoder( silk_dec ); |
1117 | 0 | st->stream_channels = st->channels; |
1118 | 0 | st->frame_size = st->Fs/400; |
1119 | | #ifdef ENABLE_DEEP_PLC |
1120 | | lpcnet_plc_reset( &st->lpcnet ); |
1121 | | #endif |
1122 | 0 | } |
1123 | 0 | break; |
1124 | 53.6k | case OPUS_GET_SAMPLE_RATE_REQUEST: |
1125 | 53.6k | { |
1126 | 53.6k | opus_int32 *value = va_arg(ap, opus_int32*); |
1127 | 53.6k | if (!value) |
1128 | 0 | { |
1129 | 0 | goto bad_arg; |
1130 | 0 | } |
1131 | 53.6k | *value = st->Fs; |
1132 | 53.6k | } |
1133 | 0 | break; |
1134 | 0 | case OPUS_GET_PITCH_REQUEST: |
1135 | 0 | { |
1136 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1137 | 0 | if (!value) |
1138 | 0 | { |
1139 | 0 | goto bad_arg; |
1140 | 0 | } |
1141 | 0 | if (st->prev_mode == MODE_CELT_ONLY) |
1142 | 0 | ret = celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value)); |
1143 | 0 | else |
1144 | 0 | *value = st->DecControl.prevPitchLag; |
1145 | 0 | } |
1146 | 0 | break; |
1147 | 0 | case OPUS_GET_GAIN_REQUEST: |
1148 | 0 | { |
1149 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1150 | 0 | if (!value) |
1151 | 0 | { |
1152 | 0 | goto bad_arg; |
1153 | 0 | } |
1154 | 0 | *value = st->decode_gain; |
1155 | 0 | } |
1156 | 0 | break; |
1157 | 0 | case OPUS_SET_GAIN_REQUEST: |
1158 | 0 | { |
1159 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
1160 | 0 | if (value<-32768 || value>32767) |
1161 | 0 | { |
1162 | 0 | goto bad_arg; |
1163 | 0 | } |
1164 | 0 | st->decode_gain = value; |
1165 | 0 | } |
1166 | 0 | break; |
1167 | 0 | case OPUS_GET_LAST_PACKET_DURATION_REQUEST: |
1168 | 0 | { |
1169 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1170 | 0 | if (!value) |
1171 | 0 | { |
1172 | 0 | goto bad_arg; |
1173 | 0 | } |
1174 | 0 | *value = st->last_packet_duration; |
1175 | 0 | } |
1176 | 0 | break; |
1177 | 0 | case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: |
1178 | 0 | { |
1179 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
1180 | 0 | if(value<0 || value>1) |
1181 | 0 | { |
1182 | 0 | goto bad_arg; |
1183 | 0 | } |
1184 | 0 | ret = celt_decoder_ctl(celt_dec, OPUS_SET_PHASE_INVERSION_DISABLED(value)); |
1185 | 0 | } |
1186 | 0 | break; |
1187 | 0 | case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: |
1188 | 0 | { |
1189 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1190 | 0 | if (!value) |
1191 | 0 | { |
1192 | 0 | goto bad_arg; |
1193 | 0 | } |
1194 | 0 | ret = celt_decoder_ctl(celt_dec, OPUS_GET_PHASE_INVERSION_DISABLED(value)); |
1195 | 0 | } |
1196 | 0 | break; |
1197 | 0 | case OPUS_SET_IGNORE_EXTENSIONS_REQUEST: |
1198 | 0 | { |
1199 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
1200 | 0 | if(value<0 || value>1) |
1201 | 0 | { |
1202 | 0 | goto bad_arg; |
1203 | 0 | } |
1204 | 0 | st->ignore_extensions = value; |
1205 | 0 | } |
1206 | 0 | break; |
1207 | 0 | case OPUS_GET_IGNORE_EXTENSIONS_REQUEST: |
1208 | 0 | { |
1209 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1210 | 0 | if (!value) |
1211 | 0 | { |
1212 | 0 | goto bad_arg; |
1213 | 0 | } |
1214 | 0 | *value = st->ignore_extensions; |
1215 | 0 | } |
1216 | 0 | break; |
1217 | | #ifdef USE_WEIGHTS_FILE |
1218 | | case OPUS_SET_DNN_BLOB_REQUEST: |
1219 | | { |
1220 | | const unsigned char *data = va_arg(ap, const unsigned char *); |
1221 | | opus_int32 len = va_arg(ap, opus_int32); |
1222 | | if(len<0 || data == NULL) |
1223 | | { |
1224 | | goto bad_arg; |
1225 | | } |
1226 | | ret = lpcnet_plc_load_model(&st->lpcnet, data, len); |
1227 | | ret = silk_LoadOSCEModels(silk_dec, data, len) || ret; |
1228 | | } |
1229 | | break; |
1230 | | #endif |
1231 | 0 | default: |
1232 | | /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/ |
1233 | 0 | ret = OPUS_UNIMPLEMENTED; |
1234 | 0 | break; |
1235 | 53.6k | } |
1236 | | |
1237 | 53.6k | va_end(ap); |
1238 | 53.6k | return ret; |
1239 | 0 | bad_arg: |
1240 | 0 | va_end(ap); |
1241 | 0 | return OPUS_BAD_ARG; |
1242 | 53.6k | } |
1243 | | |
1244 | | void opus_decoder_destroy(OpusDecoder *st) |
1245 | 0 | { |
1246 | 0 | opus_free(st); |
1247 | 0 | } |
1248 | | |
1249 | | |
1250 | | int opus_packet_get_bandwidth(const unsigned char *data) |
1251 | 285k | { |
1252 | 285k | int bandwidth; |
1253 | 285k | if (data[0]&0x80) |
1254 | 84.2k | { |
1255 | 84.2k | bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3); |
1256 | 84.2k | if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) |
1257 | 18.9k | bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
1258 | 201k | } else if ((data[0]&0x60) == 0x60) |
1259 | 30.5k | { |
1260 | 30.5k | bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND : |
1261 | 30.5k | OPUS_BANDWIDTH_SUPERWIDEBAND; |
1262 | 170k | } else { |
1263 | 170k | bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3); |
1264 | 170k | } |
1265 | 285k | return bandwidth; |
1266 | 285k | } |
1267 | | |
1268 | | int opus_packet_get_nb_channels(const unsigned char *data) |
1269 | 285k | { |
1270 | 285k | return (data[0]&0x4) ? 2 : 1; |
1271 | 285k | } |
1272 | | |
1273 | | int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) |
1274 | 1.13M | { |
1275 | 1.13M | int count; |
1276 | 1.13M | if (len<1) |
1277 | 0 | return OPUS_BAD_ARG; |
1278 | 1.13M | count = packet[0]&0x3; |
1279 | 1.13M | if (count==0) |
1280 | 756k | return 1; |
1281 | 381k | else if (count!=3) |
1282 | 142k | return 2; |
1283 | 239k | else if (len<2) |
1284 | 301 | return OPUS_INVALID_PACKET; |
1285 | 239k | else |
1286 | 239k | return packet[1]&0x3F; |
1287 | 1.13M | } |
1288 | | |
1289 | | int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len, |
1290 | | opus_int32 Fs) |
1291 | 292k | { |
1292 | 292k | int samples; |
1293 | 292k | int count = opus_packet_get_nb_frames(packet, len); |
1294 | | |
1295 | 292k | if (count<0) |
1296 | 0 | return count; |
1297 | | |
1298 | 292k | samples = count*opus_packet_get_samples_per_frame(packet, Fs); |
1299 | | /* Can't have more than 120 ms */ |
1300 | 292k | if (samples*25 > Fs*3) |
1301 | 0 | return OPUS_INVALID_PACKET; |
1302 | 292k | else |
1303 | 292k | return samples; |
1304 | 292k | } |
1305 | | |
1306 | | int opus_packet_has_lbrr(const unsigned char packet[], opus_int32 len) |
1307 | 0 | { |
1308 | 0 | int ret; |
1309 | 0 | const unsigned char *frames[48]; |
1310 | 0 | opus_int16 size[48]; |
1311 | 0 | int packet_mode, packet_frame_size, packet_stream_channels; |
1312 | 0 | int nb_frames=1; |
1313 | 0 | int lbrr; |
1314 | |
|
1315 | 0 | packet_mode = opus_packet_get_mode(packet); |
1316 | 0 | if (packet_mode == MODE_CELT_ONLY) |
1317 | 0 | return 0; |
1318 | 0 | packet_frame_size = opus_packet_get_samples_per_frame(packet, 48000); |
1319 | 0 | if (packet_frame_size > 960) |
1320 | 0 | nb_frames = packet_frame_size/960; |
1321 | 0 | packet_stream_channels = opus_packet_get_nb_channels(packet); |
1322 | 0 | ret = opus_packet_parse(packet, len, NULL, frames, size, NULL); |
1323 | 0 | if (ret <= 0) |
1324 | 0 | return ret; |
1325 | 0 | if (size[0] == 0) |
1326 | 0 | return 0; |
1327 | 0 | lbrr = (frames[0][0] >> (7-nb_frames)) & 0x1; |
1328 | 0 | if (packet_stream_channels == 2) |
1329 | 0 | lbrr = lbrr || ((frames[0][0] >> (6-2*nb_frames)) & 0x1); |
1330 | 0 | return lbrr; |
1331 | 0 | } |
1332 | | |
1333 | | int opus_decoder_get_nb_samples(const OpusDecoder *dec, |
1334 | | const unsigned char packet[], opus_int32 len) |
1335 | 0 | { |
1336 | 0 | return opus_packet_get_nb_samples(packet, len, dec->Fs); |
1337 | 0 | } |
1338 | | |
1339 | | struct OpusDREDDecoder { |
1340 | | #ifdef ENABLE_DRED |
1341 | | RDOVAEDec model; |
1342 | | #endif |
1343 | | int loaded; |
1344 | | int arch; |
1345 | | opus_uint32 magic; |
1346 | | }; |
1347 | | |
1348 | | #if defined(ENABLE_DRED) && (defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS)) |
1349 | | static void validate_dred_decoder(OpusDREDDecoder *st) |
1350 | | { |
1351 | | celt_assert(st->magic == 0xD8EDDEC0); |
1352 | | #ifdef OPUS_ARCHMASK |
1353 | | celt_assert(st->arch >= 0); |
1354 | | celt_assert(st->arch <= OPUS_ARCHMASK); |
1355 | | #endif |
1356 | | } |
1357 | | #define VALIDATE_DRED_DECODER(st) validate_dred_decoder(st) |
1358 | | #else |
1359 | | #define VALIDATE_DRED_DECODER(st) |
1360 | | #endif |
1361 | | |
1362 | | |
1363 | | int opus_dred_decoder_get_size(void) |
1364 | 0 | { |
1365 | 0 | return sizeof(OpusDREDDecoder); |
1366 | 0 | } |
1367 | | |
1368 | | #ifdef ENABLE_DRED |
1369 | | int dred_decoder_load_model(OpusDREDDecoder *dec, const unsigned char *data, int len) |
1370 | | { |
1371 | | WeightArray *list; |
1372 | | int ret; |
1373 | | parse_weights(&list, data, len); |
1374 | | ret = init_rdovaedec(&dec->model, list); |
1375 | | opus_free(list); |
1376 | | if (ret == 0) dec->loaded = 1; |
1377 | | return (ret == 0) ? OPUS_OK : OPUS_BAD_ARG; |
1378 | | } |
1379 | | #endif |
1380 | | |
1381 | | int opus_dred_decoder_init(OpusDREDDecoder *dec) |
1382 | 0 | { |
1383 | 0 | int ret = 0; |
1384 | 0 | dec->loaded = 0; |
1385 | | #if defined(ENABLE_DRED) && !defined(USE_WEIGHTS_FILE) |
1386 | | ret = init_rdovaedec(&dec->model, rdovaedec_arrays); |
1387 | | if (ret == 0) dec->loaded = 1; |
1388 | | #endif |
1389 | 0 | dec->arch = opus_select_arch(); |
1390 | | /* To make sure nobody forgets to init, use a magic number. */ |
1391 | 0 | dec->magic = 0xD8EDDEC0; |
1392 | 0 | return (ret == 0) ? OPUS_OK : OPUS_UNIMPLEMENTED; |
1393 | 0 | } |
1394 | | |
1395 | | OpusDREDDecoder *opus_dred_decoder_create(int *error) |
1396 | 0 | { |
1397 | 0 | int ret; |
1398 | 0 | OpusDREDDecoder *dec; |
1399 | 0 | dec = (OpusDREDDecoder *)opus_alloc(opus_dred_decoder_get_size()); |
1400 | 0 | if (dec == NULL) |
1401 | 0 | { |
1402 | 0 | if (error) |
1403 | 0 | *error = OPUS_ALLOC_FAIL; |
1404 | 0 | return NULL; |
1405 | 0 | } |
1406 | 0 | ret = opus_dred_decoder_init(dec); |
1407 | 0 | if (error) |
1408 | 0 | *error = ret; |
1409 | 0 | if (ret != OPUS_OK) |
1410 | 0 | { |
1411 | 0 | opus_free(dec); |
1412 | 0 | dec = NULL; |
1413 | 0 | } |
1414 | 0 | return dec; |
1415 | 0 | } |
1416 | | |
1417 | | void opus_dred_decoder_destroy(OpusDREDDecoder *dec) |
1418 | 0 | { |
1419 | 0 | if (dec) dec->magic = 0xDE57801D; |
1420 | 0 | opus_free(dec); |
1421 | 0 | } |
1422 | | |
1423 | | int opus_dred_decoder_ctl(OpusDREDDecoder *dred_dec, int request, ...) |
1424 | 0 | { |
1425 | | #ifdef ENABLE_DRED |
1426 | | int ret = OPUS_OK; |
1427 | | va_list ap; |
1428 | | |
1429 | | va_start(ap, request); |
1430 | | (void)dred_dec; |
1431 | | switch (request) |
1432 | | { |
1433 | | # ifdef USE_WEIGHTS_FILE |
1434 | | case OPUS_SET_DNN_BLOB_REQUEST: |
1435 | | { |
1436 | | const unsigned char *data = va_arg(ap, const unsigned char *); |
1437 | | opus_int32 len = va_arg(ap, opus_int32); |
1438 | | if(len<0 || data == NULL) |
1439 | | { |
1440 | | goto bad_arg; |
1441 | | } |
1442 | | return dred_decoder_load_model(dred_dec, data, len); |
1443 | | } |
1444 | | break; |
1445 | | # endif |
1446 | | default: |
1447 | | /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/ |
1448 | | ret = OPUS_UNIMPLEMENTED; |
1449 | | break; |
1450 | | } |
1451 | | va_end(ap); |
1452 | | return ret; |
1453 | | # ifdef USE_WEIGHTS_FILE |
1454 | | bad_arg: |
1455 | | va_end(ap); |
1456 | | return OPUS_BAD_ARG; |
1457 | | # endif |
1458 | | #else |
1459 | 0 | (void)dred_dec; |
1460 | 0 | (void)request; |
1461 | 0 | return OPUS_UNIMPLEMENTED; |
1462 | 0 | #endif |
1463 | 0 | } |
1464 | | |
1465 | | #ifdef ENABLE_DRED |
1466 | | static int dred_find_payload(const unsigned char *data, opus_int32 len, const unsigned char **payload, int *dred_frame_offset) |
1467 | | { |
1468 | | OpusExtensionIterator iter; |
1469 | | opus_extension_data ext; |
1470 | | const unsigned char *padding; |
1471 | | opus_int32 padding_len; |
1472 | | int nb_frames; |
1473 | | const unsigned char *frames[48]; |
1474 | | opus_int16 size[48]; |
1475 | | int frame_size; |
1476 | | int ret; |
1477 | | |
1478 | | *payload = NULL; |
1479 | | /* Get the padding section of the packet. */ |
1480 | | ret = opus_packet_parse_impl(data, len, 0, NULL, frames, size, NULL, NULL, |
1481 | | &padding, &padding_len); |
1482 | | if (ret < 0) |
1483 | | return ret; |
1484 | | nb_frames = ret; |
1485 | | frame_size = opus_packet_get_samples_per_frame(data, 48000); |
1486 | | opus_extension_iterator_init(&iter, padding, padding_len, nb_frames); |
1487 | | for (;;) { |
1488 | | ret = opus_extension_iterator_find(&iter, &ext, DRED_EXTENSION_ID); |
1489 | | if (ret <= 0) |
1490 | | return ret; |
1491 | | /* DRED position in the packet, in units of 2.5 ms like for the signaled DRED offset. */ |
1492 | | *dred_frame_offset = ext.frame*frame_size/120; |
1493 | | #ifdef DRED_EXPERIMENTAL_VERSION |
1494 | | /* Check that temporary extension type and version match. |
1495 | | This check will be removed once extension is finalized. */ |
1496 | | if (ext.len > DRED_EXPERIMENTAL_BYTES && ext.data[0] == 'D' |
1497 | | && ext.data[1] == DRED_EXPERIMENTAL_VERSION) { |
1498 | | *payload = ext.data+2; |
1499 | | return ext.len-2; |
1500 | | } |
1501 | | #else |
1502 | | if (ext.len > 0) { |
1503 | | *payload = ext.data; |
1504 | | return ext.len; |
1505 | | } |
1506 | | #endif |
1507 | | } |
1508 | | } |
1509 | | #endif |
1510 | | |
1511 | | int opus_dred_get_size(void) |
1512 | 0 | { |
1513 | | #ifdef ENABLE_DRED |
1514 | | return sizeof(OpusDRED); |
1515 | | #else |
1516 | 0 | return 0; |
1517 | 0 | #endif |
1518 | 0 | } |
1519 | | |
1520 | | OpusDRED *opus_dred_alloc(int *error) |
1521 | 0 | { |
1522 | | #ifdef ENABLE_DRED |
1523 | | OpusDRED *dec; |
1524 | | dec = (OpusDRED *)opus_alloc(opus_dred_get_size()); |
1525 | | if (dec == NULL) |
1526 | | { |
1527 | | if (error) |
1528 | | *error = OPUS_ALLOC_FAIL; |
1529 | | return NULL; |
1530 | | } |
1531 | | return dec; |
1532 | | #else |
1533 | 0 | if (error) |
1534 | 0 | *error = OPUS_UNIMPLEMENTED; |
1535 | 0 | return NULL; |
1536 | 0 | #endif |
1537 | 0 | } |
1538 | | |
1539 | | void opus_dred_free(OpusDRED *dec) |
1540 | 0 | { |
1541 | | #ifdef ENABLE_DRED |
1542 | | opus_free(dec); |
1543 | | #else |
1544 | 0 | (void)dec; |
1545 | 0 | #endif |
1546 | 0 | } |
1547 | | |
1548 | | int opus_dred_parse(OpusDREDDecoder *dred_dec, OpusDRED *dred, const unsigned char *data, opus_int32 len, opus_int32 max_dred_samples, opus_int32 sampling_rate, int *dred_end, int defer_processing) |
1549 | 0 | { |
1550 | | #ifdef ENABLE_DRED |
1551 | | const unsigned char *payload; |
1552 | | opus_int32 payload_len; |
1553 | | int dred_frame_offset=0; |
1554 | | VALIDATE_DRED_DECODER(dred_dec); |
1555 | | if (!dred_dec->loaded) return OPUS_UNIMPLEMENTED; |
1556 | | dred->process_stage = -1; |
1557 | | payload_len = dred_find_payload(data, len, &payload, &dred_frame_offset); |
1558 | | if (payload_len < 0) |
1559 | | return payload_len; |
1560 | | if (payload != NULL) |
1561 | | { |
1562 | | int offset; |
1563 | | int min_feature_frames; |
1564 | | offset = 100*max_dred_samples/sampling_rate; |
1565 | | min_feature_frames = IMIN(2 + offset, 2*DRED_NUM_REDUNDANCY_FRAMES); |
1566 | | dred_ec_decode(dred, payload, payload_len, min_feature_frames, dred_frame_offset); |
1567 | | if (!defer_processing) |
1568 | | opus_dred_process(dred_dec, dred, dred); |
1569 | | if (dred_end) *dred_end = IMAX(0, -dred->dred_offset*sampling_rate/400); |
1570 | | return IMAX(0, dred->nb_latents*sampling_rate/25 - dred->dred_offset* sampling_rate/400); |
1571 | | } |
1572 | | if (dred_end) *dred_end = 0; |
1573 | | return 0; |
1574 | | #else |
1575 | 0 | (void)dred_dec; |
1576 | 0 | (void)dred; |
1577 | 0 | (void)data; |
1578 | 0 | (void)len; |
1579 | 0 | (void)max_dred_samples; |
1580 | 0 | (void)sampling_rate; |
1581 | 0 | (void)defer_processing; |
1582 | 0 | (void)dred_end; |
1583 | 0 | return OPUS_UNIMPLEMENTED; |
1584 | 0 | #endif |
1585 | 0 | } |
1586 | | |
1587 | | int opus_dred_process(OpusDREDDecoder *dred_dec, const OpusDRED *src, OpusDRED *dst) |
1588 | 0 | { |
1589 | | #ifdef ENABLE_DRED |
1590 | | if (dred_dec == NULL || src == NULL || dst == NULL || (src->process_stage != 1 && src->process_stage != 2)) |
1591 | | return OPUS_BAD_ARG; |
1592 | | VALIDATE_DRED_DECODER(dred_dec); |
1593 | | if (!dred_dec->loaded) return OPUS_UNIMPLEMENTED; |
1594 | | if (src != dst) |
1595 | | OPUS_COPY(dst, src, 1); |
1596 | | if (dst->process_stage == 2) |
1597 | | return OPUS_OK; |
1598 | | DRED_rdovae_decode_all(&dred_dec->model, dst->fec_features, dst->state, dst->latents, dst->nb_latents, dred_dec->arch); |
1599 | | dst->process_stage = 2; |
1600 | | return OPUS_OK; |
1601 | | #else |
1602 | 0 | (void)dred_dec; |
1603 | 0 | (void)src; |
1604 | 0 | (void)dst; |
1605 | 0 | return OPUS_UNIMPLEMENTED; |
1606 | 0 | #endif |
1607 | 0 | } |
1608 | | |
1609 | | int opus_decoder_dred_decode(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, opus_int16 *pcm, opus_int32 frame_size) |
1610 | 0 | { |
1611 | | #ifdef ENABLE_DRED |
1612 | | VARDECL(float, out); |
1613 | | int ret, i; |
1614 | | ALLOC_STACK; |
1615 | | |
1616 | | if(frame_size<=0) |
1617 | | { |
1618 | | RESTORE_STACK; |
1619 | | return OPUS_BAD_ARG; |
1620 | | } |
1621 | | |
1622 | | celt_assert(st->channels == 1 || st->channels == 2); |
1623 | | ALLOC(out, frame_size*st->channels, float); |
1624 | | |
1625 | | ret = opus_decode_native(st, NULL, 0, out, frame_size, 0, 0, NULL, 1, dred, dred_offset); |
1626 | | if (ret > 0) |
1627 | | { |
1628 | | for (i=0;i<ret*st->channels;i++) |
1629 | | pcm[i] = RES2INT16(out[i]); |
1630 | | } |
1631 | | RESTORE_STACK; |
1632 | | return ret; |
1633 | | #else |
1634 | 0 | (void)st; |
1635 | 0 | (void)dred; |
1636 | 0 | (void)dred_offset; |
1637 | 0 | (void)pcm; |
1638 | 0 | (void)frame_size; |
1639 | 0 | return OPUS_UNIMPLEMENTED; |
1640 | 0 | #endif |
1641 | 0 | } |
1642 | | |
1643 | | int opus_decoder_dred_decode24(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, opus_int32 *pcm, opus_int32 frame_size) |
1644 | 0 | { |
1645 | | #ifdef ENABLE_DRED |
1646 | | VARDECL(float, out); |
1647 | | int ret, i; |
1648 | | ALLOC_STACK; |
1649 | | |
1650 | | if(frame_size<=0) |
1651 | | { |
1652 | | RESTORE_STACK; |
1653 | | return OPUS_BAD_ARG; |
1654 | | } |
1655 | | |
1656 | | celt_assert(st->channels == 1 || st->channels == 2); |
1657 | | ALLOC(out, frame_size*st->channels, float); |
1658 | | |
1659 | | ret = opus_decode_native(st, NULL, 0, out, frame_size, 0, 0, NULL, 1, dred, dred_offset); |
1660 | | if (ret > 0) |
1661 | | { |
1662 | | for (i=0;i<ret*st->channels;i++) |
1663 | | pcm[i] = RES2INT24(out[i]); |
1664 | | } |
1665 | | RESTORE_STACK; |
1666 | | return ret; |
1667 | | #else |
1668 | 0 | (void)st; |
1669 | 0 | (void)dred; |
1670 | 0 | (void)dred_offset; |
1671 | 0 | (void)pcm; |
1672 | 0 | (void)frame_size; |
1673 | 0 | return OPUS_UNIMPLEMENTED; |
1674 | 0 | #endif |
1675 | 0 | } |
1676 | | |
1677 | | int opus_decoder_dred_decode_float(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, float *pcm, opus_int32 frame_size) |
1678 | 0 | { |
1679 | | #ifdef ENABLE_DRED |
1680 | | if(frame_size<=0) |
1681 | | return OPUS_BAD_ARG; |
1682 | | return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, 0, dred, dred_offset); |
1683 | | #else |
1684 | 0 | (void)st; |
1685 | 0 | (void)dred; |
1686 | 0 | (void)dred_offset; |
1687 | 0 | (void)pcm; |
1688 | 0 | (void)frame_size; |
1689 | 0 | return OPUS_UNIMPLEMENTED; |
1690 | 0 | #endif |
1691 | 0 | } |