/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 | 179k | { |
99 | 179k | 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 | 179k | celt_assert(st->Fs == 48000 || st->Fs == 24000 || st->Fs == 16000 || st->Fs == 12000 || st->Fs == 8000); |
104 | 179k | #endif |
105 | 179k | celt_assert(st->DecControl.API_sampleRate == st->Fs); |
106 | 179k | celt_assert(st->DecControl.internalSampleRate == 0 || st->DecControl.internalSampleRate == 16000 || st->DecControl.internalSampleRate == 12000 || st->DecControl.internalSampleRate == 8000); |
107 | 179k | celt_assert(st->DecControl.nChannelsAPI == st->channels); |
108 | 179k | celt_assert(st->DecControl.nChannelsInternal == 0 || st->DecControl.nChannelsInternal == 1 || st->DecControl.nChannelsInternal == 2); |
109 | 179k | 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 | 179k | #ifdef OPUS_ARCHMASK |
111 | 179k | celt_assert(st->arch >= 0); |
112 | 179k | celt_assert(st->arch <= OPUS_ARCHMASK); |
113 | 179k | #endif |
114 | 179k | celt_assert(st->stream_channels == 1 || st->stream_channels == 2); |
115 | 179k | } |
116 | 179k | #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 | 1.12M | { |
123 | 1.12M | int silkDecSizeBytes, celtDecSizeBytes; |
124 | 1.12M | int ret; |
125 | 1.12M | if (channels<1 || channels > 2) |
126 | 0 | return 0; |
127 | 1.12M | ret = silk_Get_Decoder_Size( &silkDecSizeBytes ); |
128 | 1.12M | if(ret) |
129 | 0 | return 0; |
130 | 1.12M | silkDecSizeBytes = align(silkDecSizeBytes); |
131 | 1.12M | celtDecSizeBytes = celt_decoder_get_size(channels); |
132 | 1.12M | return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes; |
133 | 1.12M | } |
134 | | |
135 | | int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels) |
136 | 47.4k | { |
137 | 47.4k | void *silk_dec; |
138 | 47.4k | CELTDecoder *celt_dec; |
139 | 47.4k | int ret, silkDecSizeBytes; |
140 | | |
141 | 47.4k | if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000 |
142 | | #ifdef ENABLE_QEXT |
143 | | &&Fs!=96000 |
144 | | #endif |
145 | 47.4k | ) |
146 | 47.4k | || (channels!=1&&channels!=2)) |
147 | 0 | return OPUS_BAD_ARG; |
148 | | |
149 | 47.4k | OPUS_CLEAR((char*)st, opus_decoder_get_size(channels)); |
150 | | /* Initialize SILK decoder */ |
151 | 47.4k | ret = silk_Get_Decoder_Size(&silkDecSizeBytes); |
152 | 47.4k | if (ret) |
153 | 0 | return OPUS_INTERNAL_ERROR; |
154 | | |
155 | 47.4k | silkDecSizeBytes = align(silkDecSizeBytes); |
156 | 47.4k | st->silk_dec_offset = align(sizeof(OpusDecoder)); |
157 | 47.4k | st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes; |
158 | 47.4k | silk_dec = (char*)st+st->silk_dec_offset; |
159 | 47.4k | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); |
160 | 47.4k | st->stream_channels = st->channels = channels; |
161 | 47.4k | st->complexity = 0; |
162 | | |
163 | 47.4k | st->Fs = Fs; |
164 | 47.4k | st->DecControl.API_sampleRate = st->Fs; |
165 | 47.4k | st->DecControl.nChannelsAPI = st->channels; |
166 | | |
167 | | /* Reset decoder */ |
168 | 47.4k | ret = silk_InitDecoder( silk_dec ); |
169 | 47.4k | if(ret)return OPUS_INTERNAL_ERROR; |
170 | | |
171 | | /* Initialize CELT decoder */ |
172 | 47.4k | ret = celt_decoder_init(celt_dec, Fs, channels); |
173 | 47.4k | if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR; |
174 | | |
175 | 47.4k | celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0)); |
176 | | |
177 | 47.4k | st->prev_mode = 0; |
178 | 47.4k | st->frame_size = Fs/400; |
179 | | #ifdef ENABLE_DEEP_PLC |
180 | | lpcnet_plc_init( &st->lpcnet); |
181 | | #endif |
182 | 47.4k | st->arch = opus_select_arch(); |
183 | 47.4k | return OPUS_OK; |
184 | 47.4k | } |
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 | 16.2k | { |
224 | 16.2k | int i, c; |
225 | 16.2k | int inc = 48000/Fs; |
226 | 45.8k | for (c=0;c<channels;c++) |
227 | 29.6k | { |
228 | 3.58M | for (i=0;i<overlap;i++) |
229 | 3.55M | { |
230 | 3.55M | celt_coef w = MULT_COEF(window[i*inc], window[i*inc]); |
231 | 3.55M | out[i*channels+c] = ADD32(MULT_COEF_32(w,in2[i*channels+c]), |
232 | 3.55M | MULT_COEF_32(COEF_ONE-w, in1[i*channels+c])); |
233 | 3.55M | } |
234 | 29.6k | } |
235 | 16.2k | } |
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 celt_coef *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 = COEF2VAL16(window[i*inc]); |
248 | | w = MULT16_16_Q15(w, w); |
249 | | out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]), |
250 | | Q15ONE-w, in1[i*channels+c]), 15); |
251 | | } |
252 | | } |
253 | | } |
254 | | #endif |
255 | | |
256 | | static int opus_packet_get_mode(const unsigned char *data) |
257 | 179k | { |
258 | 179k | int mode; |
259 | 179k | if (data[0]&0x80) |
260 | 24.2k | { |
261 | 24.2k | mode = MODE_CELT_ONLY; |
262 | 155k | } else if ((data[0]&0x60) == 0x60) |
263 | 12.9k | { |
264 | 12.9k | mode = MODE_HYBRID; |
265 | 142k | } else { |
266 | 142k | mode = MODE_SILK_ONLY; |
267 | 142k | } |
268 | 179k | return mode; |
269 | 179k | } |
270 | | |
271 | | static int opus_decode_frame(OpusDecoder *st, const unsigned char *data, |
272 | | opus_int32 len, opus_res *pcm, int frame_size, int decode_fec ARG_QEXT(opus_extension_data *ext)) |
273 | 369k | { |
274 | 369k | void *silk_dec; |
275 | 369k | CELTDecoder *celt_dec; |
276 | 369k | int i, silk_ret=0, celt_ret=0; |
277 | 369k | ec_dec dec; |
278 | 369k | opus_int32 silk_frame_size; |
279 | 369k | int pcm_transition_silk_size; |
280 | 369k | VARDECL(opus_res, pcm_transition_silk); |
281 | 369k | int pcm_transition_celt_size; |
282 | 369k | VARDECL(opus_res, pcm_transition_celt); |
283 | 369k | opus_res *pcm_transition=NULL; |
284 | 369k | int redundant_audio_size; |
285 | 369k | VARDECL(opus_res, redundant_audio); |
286 | | |
287 | 369k | int audiosize; |
288 | 369k | int mode; |
289 | 369k | int bandwidth; |
290 | 369k | int transition=0; |
291 | 369k | int start_band; |
292 | 369k | int redundancy=0; |
293 | 369k | int redundancy_bytes = 0; |
294 | 369k | int celt_to_silk=0; |
295 | 369k | int c; |
296 | 369k | int F2_5, F5, F10, F20; |
297 | 369k | const celt_coef *window; |
298 | 369k | opus_uint32 redundant_rng = 0; |
299 | 369k | int celt_accum; |
300 | 369k | ALLOC_STACK; |
301 | | |
302 | 369k | silk_dec = (char*)st+st->silk_dec_offset; |
303 | 369k | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); |
304 | 369k | F20 = st->Fs/50; |
305 | 369k | F10 = F20>>1; |
306 | 369k | F5 = F10>>1; |
307 | 369k | F2_5 = F5>>1; |
308 | 369k | if (frame_size < F2_5) |
309 | 0 | { |
310 | 0 | RESTORE_STACK; |
311 | 0 | return OPUS_BUFFER_TOO_SMALL; |
312 | 0 | } |
313 | | /* Limit frame_size to avoid excessive stack allocations. */ |
314 | 369k | frame_size = IMIN(frame_size, st->Fs/25*3); |
315 | | /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */ |
316 | 369k | if (len<=1) |
317 | 253k | { |
318 | 253k | data = NULL; |
319 | | /* In that case, don't conceal more than what the ToC says */ |
320 | 253k | frame_size = IMIN(frame_size, st->frame_size); |
321 | 253k | } |
322 | 369k | if (data != NULL) |
323 | 115k | { |
324 | 115k | audiosize = st->frame_size; |
325 | 115k | mode = st->mode; |
326 | 115k | bandwidth = st->bandwidth; |
327 | 115k | ec_dec_init(&dec,(unsigned char*)data,len); |
328 | 253k | } else { |
329 | 253k | audiosize = frame_size; |
330 | | /* Run PLC using last used mode (CELT if we ended with CELT redundancy) */ |
331 | 253k | mode = st->prev_redundancy ? MODE_CELT_ONLY : st->prev_mode; |
332 | 253k | bandwidth = 0; |
333 | | |
334 | 253k | if (mode == 0) |
335 | 70.4k | { |
336 | | /* If we haven't got any packet yet, all we can do is return zeros */ |
337 | 70.7M | for (i=0;i<audiosize*st->channels;i++) |
338 | 70.6M | pcm[i] = 0; |
339 | 70.4k | RESTORE_STACK; |
340 | 70.4k | return audiosize; |
341 | 70.4k | } |
342 | | |
343 | | /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT), |
344 | | 10, or 20 (e.g. 12.5 or 30 ms). */ |
345 | 183k | if (audiosize > F20) |
346 | 9.34k | { |
347 | 22.2k | do { |
348 | 22.2k | int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0 ARG_QEXT(NULL)); |
349 | 22.2k | if (ret<0) |
350 | 0 | { |
351 | 0 | RESTORE_STACK; |
352 | 0 | return ret; |
353 | 0 | } |
354 | 22.2k | pcm += ret*st->channels; |
355 | 22.2k | audiosize -= ret; |
356 | 22.2k | } while (audiosize > 0); |
357 | 9.34k | RESTORE_STACK; |
358 | 9.34k | return frame_size; |
359 | 173k | } else if (audiosize < F20) |
360 | 144k | { |
361 | 144k | if (audiosize > F10) |
362 | 0 | audiosize = F10; |
363 | 144k | else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10) |
364 | 0 | audiosize = F5; |
365 | 144k | } |
366 | 183k | } |
367 | | |
368 | | /* In fixed-point, we can tell CELT to do the accumulation on top of the |
369 | | SILK PCM buffer. This saves some stack space. */ |
370 | 289k | celt_accum = (mode != MODE_CELT_ONLY); |
371 | | |
372 | 289k | pcm_transition_silk_size = ALLOC_NONE; |
373 | 289k | pcm_transition_celt_size = ALLOC_NONE; |
374 | 289k | if (data!=NULL && st->prev_mode > 0 && ( |
375 | 85.5k | (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy) |
376 | 82.1k | || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) |
377 | 289k | ) |
378 | 8.06k | { |
379 | 8.06k | transition = 1; |
380 | | /* Decide where to allocate the stack memory for pcm_transition */ |
381 | 8.06k | if (mode == MODE_CELT_ONLY) |
382 | 3.44k | pcm_transition_celt_size = F5*st->channels; |
383 | 4.61k | else |
384 | 4.61k | pcm_transition_silk_size = F5*st->channels; |
385 | 8.06k | } |
386 | 289k | ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_res); |
387 | 289k | if (transition && mode == MODE_CELT_ONLY) |
388 | 3.44k | { |
389 | 3.44k | pcm_transition = pcm_transition_celt; |
390 | 3.44k | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); |
391 | 3.44k | } |
392 | 289k | if (audiosize > frame_size) |
393 | 0 | { |
394 | | /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/ |
395 | 0 | RESTORE_STACK; |
396 | 0 | return OPUS_BAD_ARG; |
397 | 289k | } else { |
398 | 289k | frame_size = audiosize; |
399 | 289k | } |
400 | | |
401 | | /* SILK processing */ |
402 | 289k | if (mode != MODE_CELT_ONLY) |
403 | 158k | { |
404 | 158k | int lost_flag, decoded_samples; |
405 | 158k | opus_res *pcm_ptr; |
406 | 158k | int pcm_too_small; |
407 | 158k | int pcm_silk_size = ALLOC_NONE; |
408 | 158k | VARDECL(opus_res, pcm_silk); |
409 | 158k | pcm_too_small = (frame_size < F10); |
410 | 158k | if (pcm_too_small) |
411 | 11.2k | pcm_silk_size = F10*st->channels; |
412 | 158k | ALLOC(pcm_silk, pcm_silk_size, opus_res); |
413 | 158k | if (pcm_too_small) |
414 | 11.2k | pcm_ptr = pcm_silk; |
415 | 147k | else |
416 | 147k | pcm_ptr = pcm; |
417 | | |
418 | 158k | if (st->prev_mode==MODE_CELT_ONLY) |
419 | 4.61k | silk_ResetDecoder( silk_dec ); |
420 | | |
421 | | /* The SILK PLC cannot produce frames of less than 10 ms */ |
422 | 158k | st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs); |
423 | | |
424 | 158k | if (data != NULL) |
425 | 71.7k | { |
426 | 71.7k | st->DecControl.nChannelsInternal = st->stream_channels; |
427 | 71.7k | if( mode == MODE_SILK_ONLY ) { |
428 | 47.1k | if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) { |
429 | 35.3k | st->DecControl.internalSampleRate = 8000; |
430 | 35.3k | } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) { |
431 | 5.76k | st->DecControl.internalSampleRate = 12000; |
432 | 6.09k | } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) { |
433 | 6.09k | st->DecControl.internalSampleRate = 16000; |
434 | 6.09k | } else { |
435 | 0 | st->DecControl.internalSampleRate = 16000; |
436 | 0 | celt_assert( 0 ); |
437 | 0 | } |
438 | 47.1k | } else { |
439 | | /* Hybrid mode */ |
440 | 24.5k | st->DecControl.internalSampleRate = 16000; |
441 | 24.5k | } |
442 | 71.7k | } |
443 | 158k | st->DecControl.enable_deep_plc = st->complexity >= 5; |
444 | | #ifdef ENABLE_OSCE |
445 | | st->DecControl.osce_method = OSCE_METHOD_NONE; |
446 | | #ifndef DISABLE_LACE |
447 | | if (st->complexity >= 6) {st->DecControl.osce_method = OSCE_METHOD_LACE;} |
448 | | #endif |
449 | | #ifndef DISABLE_NOLACE |
450 | | if (st->complexity >= 7) {st->DecControl.osce_method = OSCE_METHOD_NOLACE;} |
451 | | #endif |
452 | | #ifdef ENABLE_OSCE_BWE |
453 | | if (st->complexity >= 4 && st->DecControl.enable_osce_bwe && |
454 | | st->Fs == 48000 && st->DecControl.internalSampleRate == 16000 && |
455 | | ((mode == MODE_SILK_ONLY) || (data == NULL))) { |
456 | | /* request WB -> FB signal extension */ |
457 | | st->DecControl.osce_extended_mode = OSCE_MODE_SILK_BBWE; |
458 | | } else { |
459 | | /* at this point, mode can only be MODE_SILK_ONLY or MODE_HYBRID */ |
460 | | st->DecControl.osce_extended_mode = mode == MODE_SILK_ONLY ? OSCE_MODE_SILK_ONLY : OSCE_MODE_HYBRID; |
461 | | } |
462 | | if (st->prev_mode == MODE_CELT_ONLY) { |
463 | | /* Update extended mode for CELT->SILK transition */ |
464 | | st->DecControl.prev_osce_extended_mode = OSCE_MODE_CELT_ONLY; |
465 | | } |
466 | | #endif |
467 | | #endif |
468 | | |
469 | 158k | lost_flag = data == NULL ? 1 : 2 * !!decode_fec; |
470 | 158k | decoded_samples = 0; |
471 | 179k | do { |
472 | | /* Call SILK decoder */ |
473 | 179k | int first_frame = decoded_samples == 0; |
474 | 179k | silk_ret = silk_Decode( silk_dec, &st->DecControl, |
475 | 179k | lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, |
476 | | #ifdef ENABLE_DEEP_PLC |
477 | | &st->lpcnet, |
478 | | #endif |
479 | 179k | st->arch ); |
480 | 179k | if( silk_ret ) { |
481 | 0 | if (lost_flag) { |
482 | | /* PLC failure should not be fatal */ |
483 | 0 | silk_frame_size = frame_size; |
484 | 0 | for (i=0;i<frame_size*st->channels;i++) |
485 | 0 | pcm_ptr[i] = 0; |
486 | 0 | } else { |
487 | 0 | RESTORE_STACK; |
488 | 0 | return OPUS_INTERNAL_ERROR; |
489 | 0 | } |
490 | 0 | } |
491 | 179k | pcm_ptr += silk_frame_size * st->channels; |
492 | 179k | decoded_samples += silk_frame_size; |
493 | 179k | } while( decoded_samples < frame_size ); |
494 | 158k | if (pcm_too_small) { |
495 | 11.2k | OPUS_COPY(pcm, pcm_silk, frame_size*st->channels); |
496 | 11.2k | } |
497 | 158k | } |
498 | | #ifdef ENABLE_OSCE_BWE |
499 | | else { |
500 | | /* CELT-only frames never enter the SILK section above, so the extended |
501 | | mode would otherwise retain a stale value (e.g. OSCE_MODE_SILK_BBWE |
502 | | from a preceding WB SILK frame), which incorrectly skips the CELT |
503 | | decode below and desyncs the range decoder. */ |
504 | | st->DecControl.osce_extended_mode = OSCE_MODE_CELT_ONLY; |
505 | | } |
506 | | #endif |
507 | | |
508 | 289k | start_band = 0; |
509 | 289k | if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL |
510 | 71.7k | && ec_tell(&dec)+17+20*(mode == MODE_HYBRID) <= 8*len) |
511 | 16.4k | { |
512 | | /* Check if we have a redundant 0-8 kHz band */ |
513 | 16.4k | if (mode == MODE_HYBRID) |
514 | 5.14k | redundancy = ec_dec_bit_logp(&dec, 12); |
515 | 11.2k | else |
516 | 11.2k | redundancy = 1; |
517 | 16.4k | if (redundancy) |
518 | 11.8k | { |
519 | 11.8k | celt_to_silk = ec_dec_bit_logp(&dec, 1); |
520 | | /* redundancy_bytes will be at least two, in the non-hybrid |
521 | | case due to the ec_tell() check above */ |
522 | 11.8k | redundancy_bytes = mode==MODE_HYBRID ? |
523 | 594 | (opus_int32)ec_dec_uint(&dec, 256)+2 : |
524 | 11.8k | len-((ec_tell(&dec)+7)>>3); |
525 | 11.8k | len -= redundancy_bytes; |
526 | | /* This is a sanity check. It should never happen for a valid |
527 | | packet, so the exact behaviour is not normative. */ |
528 | 11.8k | if (len*8 < ec_tell(&dec)) |
529 | 394 | { |
530 | 394 | len = 0; |
531 | 394 | redundancy_bytes = 0; |
532 | 394 | redundancy = 0; |
533 | 394 | } |
534 | | /* Shrink decoder because of raw bits */ |
535 | 11.8k | dec.storage -= redundancy_bytes; |
536 | 11.8k | } |
537 | 16.4k | } |
538 | 289k | if (mode != MODE_CELT_ONLY) |
539 | 158k | start_band = 17; |
540 | | |
541 | 289k | if (redundancy) |
542 | 11.4k | { |
543 | 11.4k | transition = 0; |
544 | 11.4k | pcm_transition_silk_size=ALLOC_NONE; |
545 | 11.4k | } |
546 | | |
547 | 289k | ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_res); |
548 | | |
549 | 289k | if (transition && mode != MODE_CELT_ONLY) |
550 | 2.79k | { |
551 | 2.79k | pcm_transition = pcm_transition_silk; |
552 | 2.79k | opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL)); |
553 | 2.79k | } |
554 | | |
555 | | |
556 | 289k | if (bandwidth) |
557 | 115k | { |
558 | 115k | int endband=21; |
559 | | |
560 | 115k | switch(bandwidth) |
561 | 115k | { |
562 | 50.0k | case OPUS_BANDWIDTH_NARROWBAND: |
563 | 50.0k | endband = 13; |
564 | 50.0k | break; |
565 | 5.76k | case OPUS_BANDWIDTH_MEDIUMBAND: |
566 | 32.4k | case OPUS_BANDWIDTH_WIDEBAND: |
567 | 32.4k | endband = 17; |
568 | 32.4k | break; |
569 | 13.3k | case OPUS_BANDWIDTH_SUPERWIDEBAND: |
570 | 13.3k | endband = 19; |
571 | 13.3k | break; |
572 | 20.0k | case OPUS_BANDWIDTH_FULLBAND: |
573 | 20.0k | endband = 21; |
574 | 20.0k | break; |
575 | 0 | default: |
576 | 0 | celt_assert(0); |
577 | 0 | break; |
578 | 115k | } |
579 | 115k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband))); |
580 | 115k | } |
581 | 289k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels))); |
582 | | |
583 | | /* Only allocation memory for redundancy if/when needed */ |
584 | 289k | redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE; |
585 | 289k | ALLOC(redundant_audio, redundant_audio_size, opus_res); |
586 | | |
587 | | /* 5 ms redundant frame for CELT->SILK*/ |
588 | 289k | if (redundancy && celt_to_silk) |
589 | 5.48k | { |
590 | | /* If the previous frame did not use CELT (the first redundancy frame in |
591 | | a transition from SILK may have been lost) then the CELT decoder is |
592 | | stale at this point and the redundancy audio is not useful, however |
593 | | the final range is still needed (for testing), so the redundancy is |
594 | | always decoded but the decoded audio may not be used */ |
595 | 5.48k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); |
596 | 5.48k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, |
597 | 5.48k | redundant_audio, F5, NULL, 0); |
598 | 5.48k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); |
599 | 5.48k | } |
600 | | |
601 | | /* MUST be after PLC */ |
602 | 289k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band))); |
603 | | |
604 | | #ifdef ENABLE_OSCE_BWE |
605 | | if (mode != MODE_SILK_ONLY && st->DecControl.osce_extended_mode != OSCE_MODE_SILK_BBWE) |
606 | | #else |
607 | 289k | if (mode != MODE_SILK_ONLY) |
608 | 178k | #endif |
609 | 178k | { |
610 | 178k | int celt_frame_size = IMIN(F20, frame_size); |
611 | | /* Make sure to discard any previous CELT state */ |
612 | 178k | if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy) |
613 | 178k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); |
614 | | /* Decode CELT */ |
615 | 178k | celt_ret = celt_decode_with_ec_dred(celt_dec, decode_fec ? NULL : data, |
616 | 178k | len, pcm, celt_frame_size, &dec, celt_accum |
617 | | #ifdef ENABLE_DEEP_PLC |
618 | | , &st->lpcnet |
619 | | #endif |
620 | 178k | ARG_QEXT(ext ? ext->data : NULL) ARG_QEXT(ext ? ext->len : 0)); |
621 | 178k | celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&st->rangeFinal)); |
622 | 178k | } else { |
623 | 110k | unsigned char silence[2] = {0xFF, 0xFF}; |
624 | 110k | if (!celt_accum) |
625 | 0 | { |
626 | 0 | for (i=0;i<frame_size*st->channels;i++) |
627 | 0 | pcm[i] = 0; |
628 | 0 | } |
629 | | /* For hybrid -> SILK transitions, we let the CELT MDCT |
630 | | do a fade-out by decoding a silence frame */ |
631 | 110k | if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) ) |
632 | 1.24k | { |
633 | 1.24k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); |
634 | 1.24k | celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum); |
635 | 1.24k | } |
636 | 110k | st->rangeFinal = dec.rng; |
637 | 110k | } |
638 | | |
639 | 289k | { |
640 | 289k | const CELTMode *celt_mode; |
641 | 289k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode))); |
642 | 289k | window = celt_mode->window; |
643 | 289k | } |
644 | | |
645 | | /* 5 ms redundant frame for SILK->CELT */ |
646 | 289k | if (redundancy && !celt_to_silk) |
647 | 6.00k | { |
648 | 6.00k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE)); |
649 | 6.00k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0))); |
650 | | |
651 | 6.00k | celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0); |
652 | 6.00k | MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng))); |
653 | 6.00k | smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5, |
654 | 6.00k | pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs); |
655 | 6.00k | } |
656 | | /* 5ms redundant frame for CELT->SILK; ignore if the previous frame did not |
657 | | use CELT (the first redundancy frame in a transition from SILK may have |
658 | | been lost) */ |
659 | 289k | if (redundancy && celt_to_silk && (st->prev_mode != MODE_SILK_ONLY || st->prev_redundancy)) |
660 | 4.01k | { |
661 | 11.5k | for (c=0;c<st->channels;c++) |
662 | 7.51k | { |
663 | 909k | for (i=0;i<F2_5;i++) |
664 | 902k | pcm[st->channels*i+c] = redundant_audio[st->channels*i+c]; |
665 | 7.51k | } |
666 | 4.01k | smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5, |
667 | 4.01k | pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs); |
668 | 4.01k | } |
669 | 289k | if (transition) |
670 | 6.23k | { |
671 | 6.23k | if (audiosize >= F5) |
672 | 4.65k | { |
673 | 1.00M | for (i=0;i<st->channels*F2_5;i++) |
674 | 997k | pcm[i] = pcm_transition[i]; |
675 | 4.65k | smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5, |
676 | 4.65k | pcm+st->channels*F2_5, F2_5, |
677 | 4.65k | st->channels, window, st->Fs); |
678 | 4.65k | } else { |
679 | | /* Not enough time to do a clean transition, but we do it anyway |
680 | | This will not preserve amplitude perfectly and may introduce |
681 | | a bit of temporal aliasing, but it shouldn't be too bad and |
682 | | that's pretty much the best we can do. In any case, generating this |
683 | | transition it pretty silly in the first place */ |
684 | 1.57k | smooth_fade(pcm_transition, pcm, |
685 | 1.57k | pcm, F2_5, |
686 | 1.57k | st->channels, window, st->Fs); |
687 | 1.57k | } |
688 | 6.23k | } |
689 | | |
690 | 289k | if(st->decode_gain) |
691 | 2.88k | { |
692 | 2.88k | opus_val32 gain; |
693 | 2.88k | gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain)); |
694 | 3.10M | for (i=0;i<frame_size*st->channels;i++) |
695 | 3.10M | { |
696 | 3.10M | opus_val32 x; |
697 | 3.10M | #ifdef ENABLE_RES24 |
698 | 3.10M | x = MULT32_32_Q16(pcm[i],gain); |
699 | | #else |
700 | | x = MULT16_32_P16(pcm[i],gain); |
701 | | #endif |
702 | 3.10M | pcm[i] = SATURATE(x, 32767); |
703 | 3.10M | } |
704 | 2.88k | } |
705 | | |
706 | 289k | if (len <= 1) |
707 | 174k | st->rangeFinal = 0; |
708 | 115k | else |
709 | 115k | st->rangeFinal ^= redundant_rng; |
710 | | |
711 | 289k | st->prev_mode = mode; |
712 | 289k | st->prev_redundancy = redundancy && !celt_to_silk; |
713 | | |
714 | 289k | if (celt_ret>=0) |
715 | 289k | { |
716 | 289k | if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels)) |
717 | 0 | OPUS_PRINT_INT(audiosize); |
718 | 289k | } |
719 | | |
720 | 289k | RESTORE_STACK; |
721 | 289k | return celt_ret < 0 ? celt_ret : audiosize; |
722 | | |
723 | 289k | } |
724 | | |
725 | | int opus_decode_native(OpusDecoder *st, const unsigned char *data, |
726 | | opus_int32 len, opus_res *pcm, int frame_size, int decode_fec, |
727 | | int self_delimited, opus_int32 *packet_offset, int soft_clip, const OpusDRED *dred, opus_int32 dred_offset) |
728 | 179k | { |
729 | 179k | int i, nb_samples; |
730 | 179k | int count, offset; |
731 | 179k | unsigned char toc; |
732 | 179k | int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels; |
733 | | /* 48 x 2.5 ms = 120 ms */ |
734 | 179k | opus_int16 size[48]; |
735 | 179k | const unsigned char *padding; |
736 | 179k | opus_int32 padding_len; |
737 | 179k | OpusExtensionIterator iter; |
738 | 179k | VALIDATE_OPUS_DECODER(st); |
739 | 179k | if (decode_fec<0 || decode_fec>1) |
740 | 0 | return OPUS_BAD_ARG; |
741 | | /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */ |
742 | 179k | if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0) |
743 | 0 | return OPUS_BAD_ARG; |
744 | | #ifdef ENABLE_DRED |
745 | | if (dred != NULL && dred->process_stage != 2) |
746 | | return OPUS_BAD_ARG; |
747 | | if (dred != NULL && dred->process_stage == 2) { |
748 | | int F10; |
749 | | int features_per_frame; |
750 | | int needed_feature_frames; |
751 | | int init_frames; |
752 | | lpcnet_plc_fec_clear(&st->lpcnet); |
753 | | F10 = st->Fs/100; |
754 | | /* if blend==0, the last PLC call was "update" and we need to feed two extra 10-ms frames. */ |
755 | | init_frames = (st->lpcnet.blend == 0) ? 2 : 0; |
756 | | features_per_frame = IMAX(1, frame_size/F10); |
757 | | needed_feature_frames = init_frames + features_per_frame; |
758 | | for (i=0;i<needed_feature_frames;i++) { |
759 | | int feature_offset; |
760 | | /* We floor instead of rounding because 5-ms overlap compensates for the missing 0.5 rounding offset. */ |
761 | | feature_offset = init_frames - i - 2 + (int)floor(((float)dred_offset + dred->dred_offset*F10/4)/F10); |
762 | | if (feature_offset <= 4*dred->nb_latents-1 && feature_offset >= 0) { |
763 | | lpcnet_plc_fec_add(&st->lpcnet, dred->fec_features+feature_offset*DRED_NUM_FEATURES); |
764 | | } else { |
765 | | if (feature_offset >= 0) lpcnet_plc_fec_add(&st->lpcnet, NULL); |
766 | | } |
767 | | |
768 | | } |
769 | | } |
770 | | #else |
771 | 179k | (void)dred; |
772 | 179k | (void)dred_offset; |
773 | 179k | #endif |
774 | 179k | if (len==0 || data==NULL) |
775 | 0 | { |
776 | 0 | int pcm_count=0; |
777 | 0 | do { |
778 | 0 | int ret; |
779 | 0 | ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0 ARG_QEXT(NULL)); |
780 | 0 | if (ret<0) |
781 | 0 | return ret; |
782 | 0 | pcm_count += ret; |
783 | 0 | } while (pcm_count < frame_size); |
784 | 0 | celt_assert(pcm_count == frame_size); |
785 | 0 | if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels)) |
786 | 0 | OPUS_PRINT_INT(pcm_count); |
787 | 0 | st->last_packet_duration = pcm_count; |
788 | 0 | return pcm_count; |
789 | 179k | } else if (len<0) |
790 | 0 | return OPUS_BAD_ARG; |
791 | | |
792 | 179k | packet_mode = opus_packet_get_mode(data); |
793 | 179k | packet_bandwidth = opus_packet_get_bandwidth(data); |
794 | 179k | packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs); |
795 | 179k | packet_stream_channels = opus_packet_get_nb_channels(data); |
796 | | |
797 | 179k | count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, |
798 | 179k | size, &offset, packet_offset, &padding, &padding_len); |
799 | 179k | if (st->ignore_extensions) { |
800 | 0 | padding = NULL; |
801 | 0 | padding_len = 0; |
802 | 0 | } |
803 | 179k | if (count<0) |
804 | 0 | return count; |
805 | 179k | opus_extension_iterator_init(&iter, padding, padding_len, count); |
806 | | |
807 | 179k | data += offset; |
808 | | |
809 | 179k | if (decode_fec) |
810 | 0 | { |
811 | 0 | int duration_copy; |
812 | 0 | int ret; |
813 | | /* If no FEC can be present, run the PLC (recursive call) */ |
814 | 0 | if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY) |
815 | 0 | return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip, NULL, 0); |
816 | | /* Otherwise, run the PLC on everything except the size for which we might have FEC */ |
817 | 0 | duration_copy = st->last_packet_duration; |
818 | 0 | if (frame_size-packet_frame_size!=0) |
819 | 0 | { |
820 | 0 | ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip, NULL, 0); |
821 | 0 | if (ret<0) |
822 | 0 | { |
823 | 0 | st->last_packet_duration = duration_copy; |
824 | 0 | return ret; |
825 | 0 | } |
826 | 0 | celt_assert(ret==frame_size-packet_frame_size); |
827 | 0 | } |
828 | | /* Complete with FEC */ |
829 | 0 | st->mode = packet_mode; |
830 | 0 | st->bandwidth = packet_bandwidth; |
831 | 0 | st->frame_size = packet_frame_size; |
832 | 0 | st->stream_channels = packet_stream_channels; |
833 | 0 | ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size), |
834 | 0 | packet_frame_size, 1 ARG_QEXT(NULL)); |
835 | 0 | if (ret<0) |
836 | 0 | return ret; |
837 | 0 | else { |
838 | 0 | if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels)) |
839 | 0 | OPUS_PRINT_INT(frame_size); |
840 | 0 | st->last_packet_duration = frame_size; |
841 | 0 | return frame_size; |
842 | 0 | } |
843 | 0 | } |
844 | | |
845 | 179k | if (count*packet_frame_size > frame_size) |
846 | 0 | return OPUS_BUFFER_TOO_SMALL; |
847 | | |
848 | | /* Update the state as the last step to avoid updating it on an invalid packet */ |
849 | 179k | st->mode = packet_mode; |
850 | 179k | st->bandwidth = packet_bandwidth; |
851 | 179k | st->frame_size = packet_frame_size; |
852 | 179k | st->stream_channels = packet_stream_channels; |
853 | | |
854 | 179k | nb_samples=0; |
855 | 520k | for (i=0;i<count;i++) |
856 | 340k | { |
857 | 340k | int ret; |
858 | | #ifdef ENABLE_QEXT |
859 | | opus_extension_data ext; |
860 | | ext.frame = -1; |
861 | | ext.data = NULL; |
862 | | ext.len = 0; |
863 | | ext.id = -1; |
864 | | while (ext.frame < i) { |
865 | | OpusExtensionIterator iter_copy; |
866 | | iter_copy = iter; |
867 | | ret = opus_extension_iterator_find(&iter, &ext, QEXT_EXTENSION_ID); |
868 | | if (ret <= 0) break; |
869 | | if (ext.frame > i) iter = iter_copy; |
870 | | } |
871 | | if (ext.frame != i) ext.data = NULL; |
872 | | #endif |
873 | 340k | ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0 ARG_QEXT(&ext)); |
874 | 340k | if (ret<0) |
875 | 0 | return ret; |
876 | 340k | celt_assert(ret==packet_frame_size); |
877 | 340k | data += size[i]; |
878 | 340k | nb_samples += ret; |
879 | 340k | } |
880 | 179k | st->last_packet_duration = nb_samples; |
881 | 179k | if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels)) |
882 | 0 | OPUS_PRINT_INT(nb_samples); |
883 | 179k | #ifndef FIXED_POINT |
884 | 179k | if (soft_clip) |
885 | 179k | opus_pcm_soft_clip_impl(pcm, nb_samples, st->channels, st->softclip_mem, st->arch); |
886 | 0 | else |
887 | 0 | st->softclip_mem[0]=st->softclip_mem[1]=0; |
888 | 179k | #endif |
889 | 179k | return nb_samples; |
890 | 179k | } |
891 | | |
892 | | #ifdef FIXED_POINT |
893 | | #define OPTIONAL_CLIP 0 |
894 | | #else |
895 | 0 | #define OPTIONAL_CLIP 1 |
896 | | #endif |
897 | | |
898 | | #if defined(FIXED_POINT) && !defined(ENABLE_RES24) |
899 | | int opus_decode(OpusDecoder *st, const unsigned char *data, |
900 | | opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec) |
901 | | { |
902 | | if(frame_size<=0) |
903 | | return OPUS_BAD_ARG; |
904 | | return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
905 | | } |
906 | | #else |
907 | | int opus_decode(OpusDecoder *st, const unsigned char *data, |
908 | | opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec) |
909 | 0 | { |
910 | 0 | VARDECL(opus_res, out); |
911 | 0 | int ret; |
912 | 0 | int nb_samples; |
913 | 0 | ALLOC_STACK; |
914 | |
|
915 | 0 | if(frame_size<=0) |
916 | 0 | { |
917 | 0 | RESTORE_STACK; |
918 | 0 | return OPUS_BAD_ARG; |
919 | 0 | } |
920 | 0 | if (data != NULL && len > 0 && !decode_fec) |
921 | 0 | { |
922 | 0 | nb_samples = opus_decoder_get_nb_samples(st, data, len); |
923 | 0 | if (nb_samples>0) |
924 | 0 | frame_size = IMIN(frame_size, nb_samples); |
925 | 0 | else |
926 | 0 | return OPUS_INVALID_PACKET; |
927 | 0 | } |
928 | 0 | celt_assert(st->channels == 1 || st->channels == 2); |
929 | 0 | ALLOC(out, frame_size*st->channels, opus_res); |
930 | |
|
931 | 0 | ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, OPTIONAL_CLIP, NULL, 0); |
932 | 0 | if (ret > 0) |
933 | 0 | { |
934 | | # if defined(FIXED_POINT) |
935 | | int i; |
936 | | for (i=0;i<ret*st->channels;i++) |
937 | | pcm[i] = RES2INT16(out[i]); |
938 | | # else |
939 | 0 | celt_float2int16(out, pcm, ret*st->channels, st->arch); |
940 | 0 | # endif |
941 | 0 | } |
942 | 0 | RESTORE_STACK; |
943 | 0 | return ret; |
944 | 0 | } |
945 | | #endif |
946 | | |
947 | | #if defined(FIXED_POINT) && defined(ENABLE_RES24) |
948 | | int opus_decode24(OpusDecoder *st, const unsigned char *data, |
949 | | opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec) |
950 | | { |
951 | | if(frame_size<=0) |
952 | | return OPUS_BAD_ARG; |
953 | | return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
954 | | } |
955 | | #else |
956 | | int opus_decode24(OpusDecoder *st, const unsigned char *data, |
957 | | opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec) |
958 | 0 | { |
959 | 0 | VARDECL(opus_res, out); |
960 | 0 | int ret, i; |
961 | 0 | int nb_samples; |
962 | 0 | ALLOC_STACK; |
963 | |
|
964 | 0 | if(frame_size<=0) |
965 | 0 | { |
966 | 0 | RESTORE_STACK; |
967 | 0 | return OPUS_BAD_ARG; |
968 | 0 | } |
969 | 0 | if (data != NULL && len > 0 && !decode_fec) |
970 | 0 | { |
971 | 0 | nb_samples = opus_decoder_get_nb_samples(st, data, len); |
972 | 0 | if (nb_samples>0) |
973 | 0 | frame_size = IMIN(frame_size, nb_samples); |
974 | 0 | else |
975 | 0 | return OPUS_INVALID_PACKET; |
976 | 0 | } |
977 | 0 | celt_assert(st->channels == 1 || st->channels == 2); |
978 | 0 | ALLOC(out, frame_size*st->channels, opus_res); |
979 | |
|
980 | 0 | ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
981 | 0 | if (ret > 0) |
982 | 0 | { |
983 | 0 | nb_samples = ret*st->channels; |
984 | 0 | for (i=0;i<nb_samples;i++) |
985 | 0 | pcm[i] = RES2INT24(out[i]); |
986 | 0 | } |
987 | 0 | RESTORE_STACK; |
988 | 0 | return ret; |
989 | 0 | } |
990 | | #endif |
991 | | |
992 | | |
993 | | #ifndef DISABLE_FLOAT_API |
994 | | |
995 | | # if !defined(FIXED_POINT) |
996 | | int opus_decode_float(OpusDecoder *st, const unsigned char *data, |
997 | | opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) |
998 | 0 | { |
999 | 0 | if(frame_size<=0) |
1000 | 0 | return OPUS_BAD_ARG; |
1001 | 0 | return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
1002 | 0 | } |
1003 | | # else |
1004 | | int opus_decode_float(OpusDecoder *st, const unsigned char *data, |
1005 | | opus_int32 len, float *pcm, int frame_size, int decode_fec) |
1006 | | { |
1007 | | VARDECL(opus_res, out); |
1008 | | int ret, i; |
1009 | | int nb_samples; |
1010 | | ALLOC_STACK; |
1011 | | |
1012 | | if(frame_size<=0) |
1013 | | { |
1014 | | RESTORE_STACK; |
1015 | | return OPUS_BAD_ARG; |
1016 | | } |
1017 | | if (data != NULL && len > 0 && !decode_fec) |
1018 | | { |
1019 | | nb_samples = opus_decoder_get_nb_samples(st, data, len); |
1020 | | if (nb_samples>0) |
1021 | | frame_size = IMIN(frame_size, nb_samples); |
1022 | | else |
1023 | | return OPUS_INVALID_PACKET; |
1024 | | } |
1025 | | celt_assert(st->channels == 1 || st->channels == 2); |
1026 | | ALLOC(out, frame_size*st->channels, opus_res); |
1027 | | |
1028 | | ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0, NULL, 0); |
1029 | | if (ret > 0) |
1030 | | { |
1031 | | for (i=0;i<ret*st->channels;i++) |
1032 | | pcm[i] = RES2FLOAT(out[i]); |
1033 | | } |
1034 | | RESTORE_STACK; |
1035 | | return ret; |
1036 | | } |
1037 | | # endif |
1038 | | |
1039 | | #endif |
1040 | | |
1041 | | |
1042 | | int opus_decoder_ctl(OpusDecoder *st, int request, ...) |
1043 | 1.55M | { |
1044 | 1.55M | int ret = OPUS_OK; |
1045 | 1.55M | va_list ap; |
1046 | 1.55M | void *silk_dec; |
1047 | 1.55M | CELTDecoder *celt_dec; |
1048 | | |
1049 | 1.55M | silk_dec = (char*)st+st->silk_dec_offset; |
1050 | 1.55M | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); |
1051 | | |
1052 | | |
1053 | 1.55M | va_start(ap, request); |
1054 | | |
1055 | 1.55M | switch (request) |
1056 | 1.55M | { |
1057 | 0 | case OPUS_GET_BANDWIDTH_REQUEST: |
1058 | 0 | { |
1059 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1060 | 0 | if (!value) |
1061 | 0 | { |
1062 | 0 | goto bad_arg; |
1063 | 0 | } |
1064 | 0 | *value = st->bandwidth; |
1065 | 0 | } |
1066 | 0 | break; |
1067 | 0 | case OPUS_SET_COMPLEXITY_REQUEST: |
1068 | 0 | { |
1069 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
1070 | 0 | if(value<0 || value>10) |
1071 | 0 | { |
1072 | 0 | goto bad_arg; |
1073 | 0 | } |
1074 | 0 | st->complexity = value; |
1075 | 0 | celt_decoder_ctl(celt_dec, OPUS_SET_COMPLEXITY(value)); |
1076 | 0 | } |
1077 | 0 | break; |
1078 | 0 | case OPUS_GET_COMPLEXITY_REQUEST: |
1079 | 0 | { |
1080 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1081 | 0 | if (!value) |
1082 | 0 | { |
1083 | 0 | goto bad_arg; |
1084 | 0 | } |
1085 | 0 | *value = st->complexity; |
1086 | 0 | } |
1087 | 0 | break; |
1088 | | #ifdef ENABLE_OSCE_BWE |
1089 | | case OPUS_SET_OSCE_BWE_REQUEST: |
1090 | | { |
1091 | | opus_int32 value = va_arg(ap, opus_int32); |
1092 | | if(value<0 || value>1) |
1093 | | { goto bad_arg; |
1094 | | } |
1095 | | st->DecControl.enable_osce_bwe = value; |
1096 | | |
1097 | | } |
1098 | | break; |
1099 | | case OPUS_GET_OSCE_BWE_REQUEST: |
1100 | | { |
1101 | | opus_int32 *value = va_arg(ap, opus_int32*); |
1102 | | if (!value) |
1103 | | { |
1104 | | goto bad_arg; |
1105 | | } |
1106 | | *value = st->DecControl.enable_osce_bwe; |
1107 | | } |
1108 | | break; |
1109 | | #endif |
1110 | 0 | case OPUS_GET_FINAL_RANGE_REQUEST: |
1111 | 0 | { |
1112 | 0 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
1113 | 0 | if (!value) |
1114 | 0 | { |
1115 | 0 | goto bad_arg; |
1116 | 0 | } |
1117 | 0 | *value = st->rangeFinal; |
1118 | 0 | } |
1119 | 0 | break; |
1120 | 1.24M | case OPUS_RESET_STATE: |
1121 | 1.24M | { |
1122 | 1.24M | OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START, |
1123 | 1.24M | sizeof(OpusDecoder)- |
1124 | 1.24M | ((char*)&st->OPUS_DECODER_RESET_START - (char*)st)); |
1125 | | |
1126 | 1.24M | celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); |
1127 | 1.24M | silk_ResetDecoder( silk_dec ); |
1128 | 1.24M | st->stream_channels = st->channels; |
1129 | 1.24M | st->frame_size = st->Fs/400; |
1130 | | #ifdef ENABLE_DEEP_PLC |
1131 | | lpcnet_plc_reset( &st->lpcnet ); |
1132 | | #endif |
1133 | 1.24M | } |
1134 | 1.24M | break; |
1135 | 215k | case OPUS_GET_SAMPLE_RATE_REQUEST: |
1136 | 215k | { |
1137 | 215k | opus_int32 *value = va_arg(ap, opus_int32*); |
1138 | 215k | if (!value) |
1139 | 0 | { |
1140 | 0 | goto bad_arg; |
1141 | 0 | } |
1142 | 215k | *value = st->Fs; |
1143 | 215k | } |
1144 | 0 | break; |
1145 | 0 | case OPUS_GET_PITCH_REQUEST: |
1146 | 0 | { |
1147 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1148 | 0 | if (!value) |
1149 | 0 | { |
1150 | 0 | goto bad_arg; |
1151 | 0 | } |
1152 | 0 | if (st->prev_mode == MODE_CELT_ONLY) |
1153 | 0 | ret = celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value)); |
1154 | 0 | else |
1155 | 0 | *value = st->DecControl.prevPitchLag; |
1156 | 0 | } |
1157 | 0 | break; |
1158 | 0 | case OPUS_GET_GAIN_REQUEST: |
1159 | 0 | { |
1160 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1161 | 0 | if (!value) |
1162 | 0 | { |
1163 | 0 | goto bad_arg; |
1164 | 0 | } |
1165 | 0 | *value = st->decode_gain; |
1166 | 0 | } |
1167 | 0 | break; |
1168 | 47.4k | case OPUS_SET_GAIN_REQUEST: |
1169 | 47.4k | { |
1170 | 47.4k | opus_int32 value = va_arg(ap, opus_int32); |
1171 | 47.4k | if (value<-32768 || value>32767) |
1172 | 0 | { |
1173 | 0 | goto bad_arg; |
1174 | 0 | } |
1175 | 47.4k | st->decode_gain = value; |
1176 | 47.4k | } |
1177 | 0 | break; |
1178 | 0 | case OPUS_GET_LAST_PACKET_DURATION_REQUEST: |
1179 | 0 | { |
1180 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1181 | 0 | if (!value) |
1182 | 0 | { |
1183 | 0 | goto bad_arg; |
1184 | 0 | } |
1185 | 0 | *value = st->last_packet_duration; |
1186 | 0 | } |
1187 | 0 | break; |
1188 | 47.4k | case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: |
1189 | 47.4k | { |
1190 | 47.4k | opus_int32 value = va_arg(ap, opus_int32); |
1191 | 47.4k | if(value<0 || value>1) |
1192 | 0 | { |
1193 | 0 | goto bad_arg; |
1194 | 0 | } |
1195 | 47.4k | ret = celt_decoder_ctl(celt_dec, OPUS_SET_PHASE_INVERSION_DISABLED(value)); |
1196 | 47.4k | } |
1197 | 0 | break; |
1198 | 0 | case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: |
1199 | 0 | { |
1200 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1201 | 0 | if (!value) |
1202 | 0 | { |
1203 | 0 | goto bad_arg; |
1204 | 0 | } |
1205 | 0 | ret = celt_decoder_ctl(celt_dec, OPUS_GET_PHASE_INVERSION_DISABLED(value)); |
1206 | 0 | } |
1207 | 0 | break; |
1208 | 0 | case OPUS_SET_IGNORE_EXTENSIONS_REQUEST: |
1209 | 0 | { |
1210 | 0 | opus_int32 value = va_arg(ap, opus_int32); |
1211 | 0 | if(value<0 || value>1) |
1212 | 0 | { |
1213 | 0 | goto bad_arg; |
1214 | 0 | } |
1215 | 0 | st->ignore_extensions = value; |
1216 | 0 | } |
1217 | 0 | break; |
1218 | 0 | case OPUS_GET_IGNORE_EXTENSIONS_REQUEST: |
1219 | 0 | { |
1220 | 0 | opus_int32 *value = va_arg(ap, opus_int32*); |
1221 | 0 | if (!value) |
1222 | 0 | { |
1223 | 0 | goto bad_arg; |
1224 | 0 | } |
1225 | 0 | *value = st->ignore_extensions; |
1226 | 0 | } |
1227 | 0 | break; |
1228 | | #ifdef USE_WEIGHTS_FILE |
1229 | | case OPUS_SET_DNN_BLOB_REQUEST: |
1230 | | { |
1231 | | const unsigned char *data = va_arg(ap, const unsigned char *); |
1232 | | opus_int32 len = va_arg(ap, opus_int32); |
1233 | | if(len<0 || data == NULL) |
1234 | | { |
1235 | | goto bad_arg; |
1236 | | } |
1237 | | ret = lpcnet_plc_load_model(&st->lpcnet, data, len); |
1238 | | ret = silk_LoadOSCEModels(silk_dec, data, len) || ret; |
1239 | | } |
1240 | | break; |
1241 | | #endif |
1242 | 0 | default: |
1243 | | /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/ |
1244 | 0 | ret = OPUS_UNIMPLEMENTED; |
1245 | 0 | break; |
1246 | 1.55M | } |
1247 | | |
1248 | 1.55M | va_end(ap); |
1249 | 1.55M | return ret; |
1250 | 0 | bad_arg: |
1251 | 0 | va_end(ap); |
1252 | 0 | return OPUS_BAD_ARG; |
1253 | 1.55M | } |
1254 | | |
1255 | | void opus_decoder_destroy(OpusDecoder *st) |
1256 | 0 | { |
1257 | 0 | opus_free(st); |
1258 | 0 | } |
1259 | | |
1260 | | |
1261 | | int opus_packet_get_bandwidth(const unsigned char *data) |
1262 | 179k | { |
1263 | 179k | int bandwidth; |
1264 | 179k | if (data[0]&0x80) |
1265 | 24.2k | { |
1266 | 24.2k | bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3); |
1267 | 24.2k | if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) |
1268 | 7.94k | bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
1269 | 155k | } else if ((data[0]&0x60) == 0x60) |
1270 | 12.9k | { |
1271 | 12.9k | bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND : |
1272 | 12.9k | OPUS_BANDWIDTH_SUPERWIDEBAND; |
1273 | 142k | } else { |
1274 | 142k | bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3); |
1275 | 142k | } |
1276 | 179k | return bandwidth; |
1277 | 179k | } |
1278 | | |
1279 | | int opus_packet_get_nb_channels(const unsigned char *data) |
1280 | 179k | { |
1281 | 179k | return (data[0]&0x4) ? 2 : 1; |
1282 | 179k | } |
1283 | | |
1284 | | int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) |
1285 | 190k | { |
1286 | 190k | int count; |
1287 | 190k | if (len<1) |
1288 | 0 | return OPUS_BAD_ARG; |
1289 | 190k | count = packet[0]&0x3; |
1290 | 190k | if (count==0) |
1291 | 95.2k | return 1; |
1292 | 95.6k | else if (count!=3) |
1293 | 80.7k | return 2; |
1294 | 14.9k | else if (len<2) |
1295 | 0 | return OPUS_INVALID_PACKET; |
1296 | 14.9k | else |
1297 | 14.9k | return packet[1]&0x3F; |
1298 | 190k | } |
1299 | | |
1300 | | int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len, |
1301 | | opus_int32 Fs) |
1302 | 190k | { |
1303 | 190k | int samples; |
1304 | 190k | int count = opus_packet_get_nb_frames(packet, len); |
1305 | | |
1306 | 190k | if (count<0) |
1307 | 0 | return count; |
1308 | | |
1309 | 190k | samples = count*opus_packet_get_samples_per_frame(packet, Fs); |
1310 | | /* Can't have more than 120 ms */ |
1311 | 190k | if (samples*25 > Fs*3) |
1312 | 0 | return OPUS_INVALID_PACKET; |
1313 | 190k | else |
1314 | 190k | return samples; |
1315 | 190k | } |
1316 | | |
1317 | | int opus_packet_has_lbrr(const unsigned char packet[], opus_int32 len) |
1318 | 0 | { |
1319 | 0 | int ret; |
1320 | 0 | const unsigned char *frames[48]; |
1321 | 0 | opus_int16 size[48]; |
1322 | 0 | int packet_mode, packet_frame_size, packet_stream_channels; |
1323 | 0 | int nb_frames=1; |
1324 | 0 | int lbrr; |
1325 | |
|
1326 | 0 | packet_mode = opus_packet_get_mode(packet); |
1327 | 0 | if (packet_mode == MODE_CELT_ONLY) |
1328 | 0 | return 0; |
1329 | 0 | packet_frame_size = opus_packet_get_samples_per_frame(packet, 48000); |
1330 | 0 | if (packet_frame_size > 960) |
1331 | 0 | nb_frames = packet_frame_size/960; |
1332 | 0 | packet_stream_channels = opus_packet_get_nb_channels(packet); |
1333 | 0 | ret = opus_packet_parse(packet, len, NULL, frames, size, NULL); |
1334 | 0 | if (ret <= 0) |
1335 | 0 | return ret; |
1336 | 0 | if (size[0] == 0) |
1337 | 0 | return 0; |
1338 | 0 | lbrr = (frames[0][0] >> (7-nb_frames)) & 0x1; |
1339 | 0 | if (packet_stream_channels == 2) |
1340 | 0 | lbrr = lbrr || ((frames[0][0] >> (6-2*nb_frames)) & 0x1); |
1341 | 0 | return lbrr; |
1342 | 0 | } |
1343 | | |
1344 | | int opus_decoder_get_nb_samples(const OpusDecoder *dec, |
1345 | | const unsigned char packet[], opus_int32 len) |
1346 | 0 | { |
1347 | 0 | return opus_packet_get_nb_samples(packet, len, dec->Fs); |
1348 | 0 | } |
1349 | | |
1350 | | struct OpusDREDDecoder { |
1351 | | #ifdef ENABLE_DRED |
1352 | | RDOVAEDec model; |
1353 | | #endif |
1354 | | int loaded; |
1355 | | int arch; |
1356 | | opus_uint32 magic; |
1357 | | }; |
1358 | | |
1359 | | #if defined(ENABLE_DRED) && (defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS)) |
1360 | | static void validate_dred_decoder(OpusDREDDecoder *st) |
1361 | | { |
1362 | | celt_assert(st->magic == 0xD8EDDEC0); |
1363 | | #ifdef OPUS_ARCHMASK |
1364 | | celt_assert(st->arch >= 0); |
1365 | | celt_assert(st->arch <= OPUS_ARCHMASK); |
1366 | | #endif |
1367 | | } |
1368 | | #define VALIDATE_DRED_DECODER(st) validate_dred_decoder(st) |
1369 | | #else |
1370 | | #define VALIDATE_DRED_DECODER(st) |
1371 | | #endif |
1372 | | |
1373 | | |
1374 | | int opus_dred_decoder_get_size(void) |
1375 | 0 | { |
1376 | 0 | return sizeof(OpusDREDDecoder); |
1377 | 0 | } |
1378 | | |
1379 | | #ifdef ENABLE_DRED |
1380 | | int dred_decoder_load_model(OpusDREDDecoder *dec, const unsigned char *data, int len) |
1381 | | { |
1382 | | WeightArray *list; |
1383 | | int ret; |
1384 | | parse_weights(&list, data, len); |
1385 | | ret = init_rdovaedec(&dec->model, list); |
1386 | | opus_free(list); |
1387 | | if (ret == 0) dec->loaded = 1; |
1388 | | return (ret == 0) ? OPUS_OK : OPUS_BAD_ARG; |
1389 | | } |
1390 | | #endif |
1391 | | |
1392 | | int opus_dred_decoder_init(OpusDREDDecoder *dec) |
1393 | 0 | { |
1394 | 0 | int ret = 0; |
1395 | 0 | dec->loaded = 0; |
1396 | | #if defined(ENABLE_DRED) && !defined(USE_WEIGHTS_FILE) |
1397 | | ret = init_rdovaedec(&dec->model, rdovaedec_arrays); |
1398 | | if (ret == 0) dec->loaded = 1; |
1399 | | #endif |
1400 | 0 | dec->arch = opus_select_arch(); |
1401 | | /* To make sure nobody forgets to init, use a magic number. */ |
1402 | 0 | dec->magic = 0xD8EDDEC0; |
1403 | 0 | return (ret == 0) ? OPUS_OK : OPUS_UNIMPLEMENTED; |
1404 | 0 | } |
1405 | | |
1406 | | OpusDREDDecoder *opus_dred_decoder_create(int *error) |
1407 | 0 | { |
1408 | 0 | int ret; |
1409 | 0 | OpusDREDDecoder *dec; |
1410 | 0 | dec = (OpusDREDDecoder *)opus_alloc(opus_dred_decoder_get_size()); |
1411 | 0 | if (dec == NULL) |
1412 | 0 | { |
1413 | 0 | if (error) |
1414 | 0 | *error = OPUS_ALLOC_FAIL; |
1415 | 0 | return NULL; |
1416 | 0 | } |
1417 | 0 | ret = opus_dred_decoder_init(dec); |
1418 | 0 | if (error) |
1419 | 0 | *error = ret; |
1420 | 0 | if (ret != OPUS_OK) |
1421 | 0 | { |
1422 | 0 | opus_free(dec); |
1423 | 0 | dec = NULL; |
1424 | 0 | } |
1425 | 0 | return dec; |
1426 | 0 | } |
1427 | | |
1428 | | void opus_dred_decoder_destroy(OpusDREDDecoder *dec) |
1429 | 0 | { |
1430 | 0 | if (dec) dec->magic = 0xDE57801D; |
1431 | 0 | opus_free(dec); |
1432 | 0 | } |
1433 | | |
1434 | | int opus_dred_decoder_ctl(OpusDREDDecoder *dred_dec, int request, ...) |
1435 | 0 | { |
1436 | | #ifdef ENABLE_DRED |
1437 | | int ret = OPUS_OK; |
1438 | | va_list ap; |
1439 | | |
1440 | | va_start(ap, request); |
1441 | | (void)dred_dec; |
1442 | | switch (request) |
1443 | | { |
1444 | | # ifdef USE_WEIGHTS_FILE |
1445 | | case OPUS_SET_DNN_BLOB_REQUEST: |
1446 | | { |
1447 | | const unsigned char *data = va_arg(ap, const unsigned char *); |
1448 | | opus_int32 len = va_arg(ap, opus_int32); |
1449 | | if(len<0 || data == NULL) |
1450 | | { |
1451 | | goto bad_arg; |
1452 | | } |
1453 | | ret = dred_decoder_load_model(dred_dec, data, len); |
1454 | | } |
1455 | | break; |
1456 | | # endif |
1457 | | default: |
1458 | | /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/ |
1459 | | ret = OPUS_UNIMPLEMENTED; |
1460 | | break; |
1461 | | } |
1462 | | va_end(ap); |
1463 | | return ret; |
1464 | | # ifdef USE_WEIGHTS_FILE |
1465 | | bad_arg: |
1466 | | va_end(ap); |
1467 | | return OPUS_BAD_ARG; |
1468 | | # endif |
1469 | | #else |
1470 | 0 | (void)dred_dec; |
1471 | 0 | (void)request; |
1472 | 0 | return OPUS_UNIMPLEMENTED; |
1473 | 0 | #endif |
1474 | 0 | } |
1475 | | |
1476 | | #ifdef ENABLE_DRED |
1477 | | static int dred_find_payload(const unsigned char *data, opus_int32 len, const unsigned char **payload, int *dred_frame_offset) |
1478 | | { |
1479 | | OpusExtensionIterator iter; |
1480 | | opus_extension_data ext; |
1481 | | const unsigned char *padding; |
1482 | | opus_int32 padding_len; |
1483 | | int nb_frames; |
1484 | | const unsigned char *frames[48]; |
1485 | | opus_int16 size[48]; |
1486 | | int frame_size; |
1487 | | int ret; |
1488 | | |
1489 | | *payload = NULL; |
1490 | | /* Get the padding section of the packet. */ |
1491 | | ret = opus_packet_parse_impl(data, len, 0, NULL, frames, size, NULL, NULL, |
1492 | | &padding, &padding_len); |
1493 | | if (ret < 0) |
1494 | | return ret; |
1495 | | nb_frames = ret; |
1496 | | frame_size = opus_packet_get_samples_per_frame(data, 48000); |
1497 | | opus_extension_iterator_init(&iter, padding, padding_len, nb_frames); |
1498 | | for (;;) { |
1499 | | ret = opus_extension_iterator_find(&iter, &ext, DRED_EXTENSION_ID); |
1500 | | if (ret <= 0) |
1501 | | return ret; |
1502 | | /* DRED position in the packet, in units of 2.5 ms like for the signaled DRED offset. */ |
1503 | | *dred_frame_offset = ext.frame*frame_size/120; |
1504 | | #ifdef DRED_EXPERIMENTAL_VERSION |
1505 | | /* Check that temporary extension type and version match. |
1506 | | This check will be removed once extension is finalized. */ |
1507 | | if (ext.len > DRED_EXPERIMENTAL_BYTES && ext.data[0] == 'D' |
1508 | | && ext.data[1] == DRED_EXPERIMENTAL_VERSION) { |
1509 | | *payload = ext.data+2; |
1510 | | return ext.len-2; |
1511 | | } |
1512 | | #else |
1513 | | if (ext.len > 0) { |
1514 | | *payload = ext.data; |
1515 | | return ext.len; |
1516 | | } |
1517 | | #endif |
1518 | | } |
1519 | | } |
1520 | | #endif |
1521 | | |
1522 | | int opus_dred_get_size(void) |
1523 | 0 | { |
1524 | | #ifdef ENABLE_DRED |
1525 | | return sizeof(OpusDRED); |
1526 | | #else |
1527 | 0 | return 0; |
1528 | 0 | #endif |
1529 | 0 | } |
1530 | | |
1531 | | OpusDRED *opus_dred_alloc(int *error) |
1532 | 0 | { |
1533 | | #ifdef ENABLE_DRED |
1534 | | OpusDRED *dec; |
1535 | | dec = (OpusDRED *)opus_alloc(opus_dred_get_size()); |
1536 | | if (dec == NULL) |
1537 | | { |
1538 | | if (error) |
1539 | | *error = OPUS_ALLOC_FAIL; |
1540 | | return NULL; |
1541 | | } |
1542 | | OPUS_CLEAR(dec, 1); |
1543 | | return dec; |
1544 | | #else |
1545 | 0 | if (error) |
1546 | 0 | *error = OPUS_UNIMPLEMENTED; |
1547 | 0 | return NULL; |
1548 | 0 | #endif |
1549 | 0 | } |
1550 | | |
1551 | | void opus_dred_free(OpusDRED *dec) |
1552 | 0 | { |
1553 | | #ifdef ENABLE_DRED |
1554 | | opus_free(dec); |
1555 | | #else |
1556 | 0 | (void)dec; |
1557 | 0 | #endif |
1558 | 0 | } |
1559 | | |
1560 | | 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) |
1561 | 0 | { |
1562 | | #ifdef ENABLE_DRED |
1563 | | const unsigned char *payload; |
1564 | | opus_int32 payload_len; |
1565 | | int dred_frame_offset=0; |
1566 | | VALIDATE_DRED_DECODER(dred_dec); |
1567 | | if (!dred_dec->loaded) return OPUS_UNIMPLEMENTED; |
1568 | | dred->process_stage = -1; |
1569 | | payload_len = dred_find_payload(data, len, &payload, &dred_frame_offset); |
1570 | | if (payload_len < 0) |
1571 | | return payload_len; |
1572 | | if (payload != NULL) |
1573 | | { |
1574 | | int max_dred_features; |
1575 | | max_dred_features = (100*max_dred_samples + sampling_rate - 1)/sampling_rate; |
1576 | | dred_ec_decode(dred, payload, payload_len, max_dred_features, dred_frame_offset); |
1577 | | if (!defer_processing) |
1578 | | opus_dred_process(dred_dec, dred, dred); |
1579 | | if (dred_end) *dred_end = IMAX(0, -dred->dred_offset*sampling_rate/400); |
1580 | | return IMAX(0, dred->nb_latents*sampling_rate/25 - dred->dred_offset* sampling_rate/400); |
1581 | | } |
1582 | | if (dred_end) *dred_end = 0; |
1583 | | return 0; |
1584 | | #else |
1585 | 0 | (void)dred_dec; |
1586 | 0 | (void)dred; |
1587 | 0 | (void)data; |
1588 | 0 | (void)len; |
1589 | 0 | (void)max_dred_samples; |
1590 | 0 | (void)sampling_rate; |
1591 | 0 | (void)defer_processing; |
1592 | 0 | (void)dred_end; |
1593 | 0 | return OPUS_UNIMPLEMENTED; |
1594 | 0 | #endif |
1595 | 0 | } |
1596 | | |
1597 | | int opus_dred_process(OpusDREDDecoder *dred_dec, const OpusDRED *src, OpusDRED *dst) |
1598 | 0 | { |
1599 | | #ifdef ENABLE_DRED |
1600 | | if (dred_dec == NULL || src == NULL || dst == NULL || (src->process_stage != 1 && src->process_stage != 2)) |
1601 | | return OPUS_BAD_ARG; |
1602 | | VALIDATE_DRED_DECODER(dred_dec); |
1603 | | if (!dred_dec->loaded) return OPUS_UNIMPLEMENTED; |
1604 | | if (src != dst) |
1605 | | OPUS_COPY(dst, src, 1); |
1606 | | if (dst->process_stage == 2) |
1607 | | return OPUS_OK; |
1608 | | celt_assert(dst->nb_latents >= 0 && dst->nb_latents <= DRED_NUM_REDUNDANCY_FRAMES/2); |
1609 | | DRED_rdovae_decode_all(&dred_dec->model, dst->fec_features, dst->state, dst->latents, dst->nb_latents, dred_dec->arch); |
1610 | | dst->process_stage = 2; |
1611 | | return OPUS_OK; |
1612 | | #else |
1613 | 0 | (void)dred_dec; |
1614 | 0 | (void)src; |
1615 | 0 | (void)dst; |
1616 | 0 | return OPUS_UNIMPLEMENTED; |
1617 | 0 | #endif |
1618 | 0 | } |
1619 | | |
1620 | | int opus_decoder_dred_decode(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, opus_int16 *pcm, opus_int32 frame_size) |
1621 | 0 | { |
1622 | | #ifdef ENABLE_DRED |
1623 | | VARDECL(float, out); |
1624 | | int ret, i; |
1625 | | ALLOC_STACK; |
1626 | | if(frame_size<=0) |
1627 | | { |
1628 | | RESTORE_STACK; |
1629 | | return OPUS_BAD_ARG; |
1630 | | } |
1631 | | |
1632 | | celt_assert(st->channels == 1 || st->channels == 2); |
1633 | | ALLOC(out, frame_size*st->channels, float); |
1634 | | |
1635 | | ret = opus_decode_native(st, NULL, 0, out, frame_size, 0, 0, NULL, 1, dred, dred_offset); |
1636 | | if (ret > 0) |
1637 | | { |
1638 | | for (i=0;i<ret*st->channels;i++) |
1639 | | pcm[i] = RES2INT16(out[i]); |
1640 | | } |
1641 | | RESTORE_STACK; |
1642 | | return ret; |
1643 | | #else |
1644 | 0 | (void)st; |
1645 | 0 | (void)dred; |
1646 | 0 | (void)dred_offset; |
1647 | 0 | (void)pcm; |
1648 | 0 | (void)frame_size; |
1649 | 0 | return OPUS_UNIMPLEMENTED; |
1650 | 0 | #endif |
1651 | 0 | } |
1652 | | |
1653 | | int opus_decoder_dred_decode24(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, opus_int32 *pcm, opus_int32 frame_size) |
1654 | 0 | { |
1655 | | #ifdef ENABLE_DRED |
1656 | | VARDECL(float, out); |
1657 | | int ret, i; |
1658 | | ALLOC_STACK; |
1659 | | |
1660 | | if(frame_size<=0) |
1661 | | { |
1662 | | RESTORE_STACK; |
1663 | | return OPUS_BAD_ARG; |
1664 | | } |
1665 | | |
1666 | | celt_assert(st->channels == 1 || st->channels == 2); |
1667 | | ALLOC(out, frame_size*st->channels, float); |
1668 | | |
1669 | | ret = opus_decode_native(st, NULL, 0, out, frame_size, 0, 0, NULL, 1, dred, dred_offset); |
1670 | | if (ret > 0) |
1671 | | { |
1672 | | for (i=0;i<ret*st->channels;i++) |
1673 | | pcm[i] = RES2INT24(out[i]); |
1674 | | } |
1675 | | RESTORE_STACK; |
1676 | | return ret; |
1677 | | #else |
1678 | 0 | (void)st; |
1679 | 0 | (void)dred; |
1680 | 0 | (void)dred_offset; |
1681 | 0 | (void)pcm; |
1682 | 0 | (void)frame_size; |
1683 | 0 | return OPUS_UNIMPLEMENTED; |
1684 | 0 | #endif |
1685 | 0 | } |
1686 | | |
1687 | | int opus_decoder_dred_decode_float(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, float *pcm, opus_int32 frame_size) |
1688 | 0 | { |
1689 | | #ifdef ENABLE_DRED |
1690 | | if(frame_size<=0) |
1691 | | return OPUS_BAD_ARG; |
1692 | | return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, 0, dred, dred_offset); |
1693 | | #else |
1694 | 0 | (void)st; |
1695 | 0 | (void)dred; |
1696 | 0 | (void)dred_offset; |
1697 | 0 | (void)pcm; |
1698 | 0 | (void)frame_size; |
1699 | 0 | return OPUS_UNIMPLEMENTED; |
1700 | 0 | #endif |
1701 | 0 | } |