781999aa101cb1ed755acbab8b5fcbfaab79320f
[libav.git] / libavcodec / qdm2.c
1 /*
2 * QDM2 compatible decoder
3 * Copyright (c) 2003 Ewald Snel
4 * Copyright (c) 2005 Benjamin Larsson
5 * Copyright (c) 2005 Alex Beregszaszi
6 * Copyright (c) 2005 Roberto Togni
7 *
8 * This file is part of Libav.
9 *
10 * Libav is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * Libav is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * QDM2 decoder
28 * @author Ewald Snel, Benjamin Larsson, Alex Beregszaszi, Roberto Togni
29 *
30 * The decoder is not perfect yet, there are still some distortions
31 * especially on files encoded with 16 or 8 subbands.
32 */
33
34 #include <math.h>
35 #include <stddef.h>
36 #include <stdio.h>
37
38 #include "libavutil/channel_layout.h"
39
40 #define BITSTREAM_READER_LE
41 #include "avcodec.h"
42 #include "bitstream.h"
43 #include "internal.h"
44 #include "mpegaudio.h"
45 #include "mpegaudiodsp.h"
46 #include "rdft.h"
47
48 #include "qdm2data.h"
49 #include "qdm2_tablegen.h"
50
51
52 #define QDM2_LIST_ADD(list, size, packet) \
53 do { \
54 if (size > 0) { \
55 list[size - 1].next = &list[size]; \
56 } \
57 list[size].packet = packet; \
58 list[size].next = NULL; \
59 size++; \
60 } while(0)
61
62 // Result is 8, 16 or 30
63 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
64
65 #define FIX_NOISE_IDX(noise_idx) \
66 if ((noise_idx) >= 3840) \
67 (noise_idx) -= 3840; \
68
69 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
70
71 #define SAMPLES_NEEDED \
72 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
73
74 #define SAMPLES_NEEDED_2(why) \
75 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
76
77 #define QDM2_MAX_FRAME_SIZE 512
78
79 typedef int8_t sb_int8_array[2][30][64];
80
81 /**
82 * Subpacket
83 */
84 typedef struct QDM2SubPacket {
85 int type; ///< subpacket type
86 unsigned int size; ///< subpacket size
87 const uint8_t *data; ///< pointer to subpacket data (points to input data buffer, it's not a private copy)
88 } QDM2SubPacket;
89
90 /**
91 * A node in the subpacket list
92 */
93 typedef struct QDM2SubPNode {
94 QDM2SubPacket *packet; ///< packet
95 struct QDM2SubPNode *next; ///< pointer to next packet in the list, NULL if leaf node
96 } QDM2SubPNode;
97
98 typedef struct QDM2Complex {
99 float re;
100 float im;
101 } QDM2Complex;
102
103 typedef struct FFTTone {
104 float level;
105 QDM2Complex *complex;
106 const float *table;
107 int phase;
108 int phase_shift;
109 int duration;
110 short time_index;
111 short cutoff;
112 } FFTTone;
113
114 typedef struct FFTCoefficient {
115 int16_t sub_packet;
116 uint8_t channel;
117 int16_t offset;
118 int16_t exp;
119 uint8_t phase;
120 } FFTCoefficient;
121
122 typedef struct QDM2FFT {
123 DECLARE_ALIGNED(32, QDM2Complex, complex)[MPA_MAX_CHANNELS][256];
124 } QDM2FFT;
125
126 /**
127 * QDM2 decoder context
128 */
129 typedef struct QDM2Context {
130 /// Parameters from codec header, do not change during playback
131 int nb_channels; ///< number of channels
132 int channels; ///< number of channels
133 int group_size; ///< size of frame group (16 frames per group)
134 int fft_size; ///< size of FFT, in complex numbers
135 int checksum_size; ///< size of data block, used also for checksum
136
137 /// Parameters built from header parameters, do not change during playback
138 int group_order; ///< order of frame group
139 int fft_order; ///< order of FFT (actually fftorder+1)
140 int frame_size; ///< size of data frame
141 int frequency_range;
142 int sub_sampling; ///< subsampling: 0=25%, 1=50%, 2=100% */
143 int coeff_per_sb_select; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
144 int cm_table_select; ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
145
146 /// Packets and packet lists
147 QDM2SubPacket sub_packets[16]; ///< the packets themselves
148 QDM2SubPNode sub_packet_list_A[16]; ///< list of all packets
149 QDM2SubPNode sub_packet_list_B[16]; ///< FFT packets B are on list
150 int sub_packets_B; ///< number of packets on 'B' list
151 QDM2SubPNode sub_packet_list_C[16]; ///< packets with errors?
152 QDM2SubPNode sub_packet_list_D[16]; ///< DCT packets
153
154 /// FFT and tones
155 FFTTone fft_tones[1000];
156 int fft_tone_start;
157 int fft_tone_end;
158 FFTCoefficient fft_coefs[1000];
159 int fft_coefs_index;
160 int fft_coefs_min_index[5];
161 int fft_coefs_max_index[5];
162 int fft_level_exp[6];
163 RDFTContext rdft_ctx;
164 QDM2FFT fft;
165
166 /// I/O data
167 const uint8_t *compressed_data;
168 int compressed_size;
169 float output_buffer[QDM2_MAX_FRAME_SIZE * 2];
170
171 /// Synthesis filter
172 MPADSPContext mpadsp;
173 DECLARE_ALIGNED(32, float, synth_buf)[MPA_MAX_CHANNELS][512*2];
174 int synth_buf_offset[MPA_MAX_CHANNELS];
175 DECLARE_ALIGNED(32, float, sb_samples)[MPA_MAX_CHANNELS][128][SBLIMIT];
176 DECLARE_ALIGNED(32, float, samples)[MPA_MAX_CHANNELS * MPA_FRAME_SIZE];
177
178 /// Mixed temporary data used in decoding
179 float tone_level[MPA_MAX_CHANNELS][30][64];
180 int8_t coding_method[MPA_MAX_CHANNELS][30][64];
181 int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8];
182 int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8];
183 int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8];
184 int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8];
185 int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26];
186 int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64];
187 int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64];
188
189 // Flags
190 int has_errors; ///< packet has errors
191 int superblocktype_2_3; ///< select fft tables and some algorithm based on superblock type
192 int do_synth_filter; ///< used to perform or skip synthesis filter
193
194 int sub_packet;
195 int noise_idx; ///< index for dithering noise table
196 } QDM2Context;
197
198
199 static VLC vlc_tab_level;
200 static VLC vlc_tab_diff;
201 static VLC vlc_tab_run;
202 static VLC fft_level_exp_alt_vlc;
203 static VLC fft_level_exp_vlc;
204 static VLC fft_stereo_exp_vlc;
205 static VLC fft_stereo_phase_vlc;
206 static VLC vlc_tab_tone_level_idx_hi1;
207 static VLC vlc_tab_tone_level_idx_mid;
208 static VLC vlc_tab_tone_level_idx_hi2;
209 static VLC vlc_tab_type30;
210 static VLC vlc_tab_type34;
211 static VLC vlc_tab_fft_tone_offset[5];
212
213 static const uint16_t qdm2_vlc_offs[] = {
214 0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
215 };
216
217 static const int switchtable[23] = {
218 0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
219 };
220
221 static av_cold void qdm2_init_vlc(void)
222 {
223 static VLC_TYPE qdm2_table[3838][2];
224
225 vlc_tab_level.table = &qdm2_table[qdm2_vlc_offs[0]];
226 vlc_tab_level.table_allocated = qdm2_vlc_offs[1] - qdm2_vlc_offs[0];
227 init_vlc(&vlc_tab_level, 8, 24,
228 vlc_tab_level_huffbits, 1, 1,
229 vlc_tab_level_huffcodes, 2, 2,
230 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
231
232 vlc_tab_diff.table = &qdm2_table[qdm2_vlc_offs[1]];
233 vlc_tab_diff.table_allocated = qdm2_vlc_offs[2] - qdm2_vlc_offs[1];
234 init_vlc(&vlc_tab_diff, 8, 37,
235 vlc_tab_diff_huffbits, 1, 1,
236 vlc_tab_diff_huffcodes, 2, 2,
237 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
238
239 vlc_tab_run.table = &qdm2_table[qdm2_vlc_offs[2]];
240 vlc_tab_run.table_allocated = qdm2_vlc_offs[3] - qdm2_vlc_offs[2];
241 init_vlc(&vlc_tab_run, 5, 6,
242 vlc_tab_run_huffbits, 1, 1,
243 vlc_tab_run_huffcodes, 1, 1,
244 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
245
246 fft_level_exp_alt_vlc.table = &qdm2_table[qdm2_vlc_offs[3]];
247 fft_level_exp_alt_vlc.table_allocated = qdm2_vlc_offs[4] -
248 qdm2_vlc_offs[3];
249 init_vlc(&fft_level_exp_alt_vlc, 8, 28,
250 fft_level_exp_alt_huffbits, 1, 1,
251 fft_level_exp_alt_huffcodes, 2, 2,
252 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
253
254 fft_level_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[4]];
255 fft_level_exp_vlc.table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
256 init_vlc(&fft_level_exp_vlc, 8, 20,
257 fft_level_exp_huffbits, 1, 1,
258 fft_level_exp_huffcodes, 2, 2,
259 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
260
261 fft_stereo_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[5]];
262 fft_stereo_exp_vlc.table_allocated = qdm2_vlc_offs[6] -
263 qdm2_vlc_offs[5];
264 init_vlc(&fft_stereo_exp_vlc, 6, 7,
265 fft_stereo_exp_huffbits, 1, 1,
266 fft_stereo_exp_huffcodes, 1, 1,
267 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
268
269 fft_stereo_phase_vlc.table = &qdm2_table[qdm2_vlc_offs[6]];
270 fft_stereo_phase_vlc.table_allocated = qdm2_vlc_offs[7] -
271 qdm2_vlc_offs[6];
272 init_vlc(&fft_stereo_phase_vlc, 6, 9,
273 fft_stereo_phase_huffbits, 1, 1,
274 fft_stereo_phase_huffcodes, 1, 1,
275 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
276
277 vlc_tab_tone_level_idx_hi1.table =
278 &qdm2_table[qdm2_vlc_offs[7]];
279 vlc_tab_tone_level_idx_hi1.table_allocated = qdm2_vlc_offs[8] -
280 qdm2_vlc_offs[7];
281 init_vlc(&vlc_tab_tone_level_idx_hi1, 8, 20,
282 vlc_tab_tone_level_idx_hi1_huffbits, 1, 1,
283 vlc_tab_tone_level_idx_hi1_huffcodes, 2, 2,
284 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
285
286 vlc_tab_tone_level_idx_mid.table =
287 &qdm2_table[qdm2_vlc_offs[8]];
288 vlc_tab_tone_level_idx_mid.table_allocated = qdm2_vlc_offs[9] -
289 qdm2_vlc_offs[8];
290 init_vlc(&vlc_tab_tone_level_idx_mid, 8, 24,
291 vlc_tab_tone_level_idx_mid_huffbits, 1, 1,
292 vlc_tab_tone_level_idx_mid_huffcodes, 2, 2,
293 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
294
295 vlc_tab_tone_level_idx_hi2.table =
296 &qdm2_table[qdm2_vlc_offs[9]];
297 vlc_tab_tone_level_idx_hi2.table_allocated = qdm2_vlc_offs[10] -
298 qdm2_vlc_offs[9];
299 init_vlc(&vlc_tab_tone_level_idx_hi2, 8, 24,
300 vlc_tab_tone_level_idx_hi2_huffbits, 1, 1,
301 vlc_tab_tone_level_idx_hi2_huffcodes, 2, 2,
302 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
303
304 vlc_tab_type30.table = &qdm2_table[qdm2_vlc_offs[10]];
305 vlc_tab_type30.table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
306 init_vlc(&vlc_tab_type30, 6, 9,
307 vlc_tab_type30_huffbits, 1, 1,
308 vlc_tab_type30_huffcodes, 1, 1,
309 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
310
311 vlc_tab_type34.table = &qdm2_table[qdm2_vlc_offs[11]];
312 vlc_tab_type34.table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11];
313 init_vlc(&vlc_tab_type34, 5, 10,
314 vlc_tab_type34_huffbits, 1, 1,
315 vlc_tab_type34_huffcodes, 1, 1,
316 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
317
318 vlc_tab_fft_tone_offset[0].table =
319 &qdm2_table[qdm2_vlc_offs[12]];
320 vlc_tab_fft_tone_offset[0].table_allocated = qdm2_vlc_offs[13] -
321 qdm2_vlc_offs[12];
322 init_vlc(&vlc_tab_fft_tone_offset[0], 8, 23,
323 vlc_tab_fft_tone_offset_0_huffbits, 1, 1,
324 vlc_tab_fft_tone_offset_0_huffcodes, 2, 2,
325 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
326
327 vlc_tab_fft_tone_offset[1].table =
328 &qdm2_table[qdm2_vlc_offs[13]];
329 vlc_tab_fft_tone_offset[1].table_allocated = qdm2_vlc_offs[14] -
330 qdm2_vlc_offs[13];
331 init_vlc(&vlc_tab_fft_tone_offset[1], 8, 28,
332 vlc_tab_fft_tone_offset_1_huffbits, 1, 1,
333 vlc_tab_fft_tone_offset_1_huffcodes, 2, 2,
334 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
335
336 vlc_tab_fft_tone_offset[2].table =
337 &qdm2_table[qdm2_vlc_offs[14]];
338 vlc_tab_fft_tone_offset[2].table_allocated = qdm2_vlc_offs[15] -
339 qdm2_vlc_offs[14];
340 init_vlc(&vlc_tab_fft_tone_offset[2], 8, 32,
341 vlc_tab_fft_tone_offset_2_huffbits, 1, 1,
342 vlc_tab_fft_tone_offset_2_huffcodes, 2, 2,
343 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
344
345 vlc_tab_fft_tone_offset[3].table =
346 &qdm2_table[qdm2_vlc_offs[15]];
347 vlc_tab_fft_tone_offset[3].table_allocated = qdm2_vlc_offs[16] -
348 qdm2_vlc_offs[15];
349 init_vlc(&vlc_tab_fft_tone_offset[3], 8, 35,
350 vlc_tab_fft_tone_offset_3_huffbits, 1, 1,
351 vlc_tab_fft_tone_offset_3_huffcodes, 2, 2,
352 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
353
354 vlc_tab_fft_tone_offset[4].table =
355 &qdm2_table[qdm2_vlc_offs[16]];
356 vlc_tab_fft_tone_offset[4].table_allocated = qdm2_vlc_offs[17] -
357 qdm2_vlc_offs[16];
358 init_vlc(&vlc_tab_fft_tone_offset[4], 8, 38,
359 vlc_tab_fft_tone_offset_4_huffbits, 1, 1,
360 vlc_tab_fft_tone_offset_4_huffcodes, 2, 2,
361 INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
362 }
363
364 static int qdm2_get_vlc(BitstreamContext *bc, VLC *vlc, int flag, int depth)
365 {
366 int value;
367
368 value = bitstream_read_vlc(bc, vlc->table, vlc->bits, depth);
369
370 /* stage-2, 3 bits exponent escape sequence */
371 if (value-- == 0)
372 value = bitstream_read(bc, bitstream_read(bc, 3) + 1);
373
374 /* stage-3, optional */
375 if (flag) {
376 int tmp = vlc_stage3_values[value];
377
378 if ((value & ~3) > 0)
379 tmp += bitstream_read(bc, value >> 2);
380 value = tmp;
381 }
382
383 return value;
384 }
385
386 static int qdm2_get_se_vlc(VLC *vlc, BitstreamContext *bc, int depth)
387 {
388 int value = qdm2_get_vlc(bc, vlc, 0, depth);
389
390 return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
391 }
392
393 /**
394 * QDM2 checksum
395 *
396 * @param data pointer to data to be checksummed
397 * @param length data length
398 * @param value checksum value
399 *
400 * @return 0 if checksum is OK
401 */
402 static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
403 {
404 int i;
405
406 for (i = 0; i < length; i++)
407 value -= data[i];
408
409 return (uint16_t)(value & 0xffff);
410 }
411
412 /**
413 * Fill a QDM2SubPacket structure with packet type, size, and data pointer.
414 *
415 * @param bc bitreader context
416 * @param sub_packet packet under analysis
417 */
418 static void qdm2_decode_sub_packet_header(BitstreamContext *bc,
419 QDM2SubPacket *sub_packet)
420 {
421 sub_packet->type = bitstream_read(bc, 8);
422
423 if (sub_packet->type == 0) {
424 sub_packet->size = 0;
425 sub_packet->data = NULL;
426 } else {
427 sub_packet->size = bitstream_read(bc, 8);
428
429 if (sub_packet->type & 0x80) {
430 sub_packet->size <<= 8;
431 sub_packet->size |= bitstream_read(bc, 8);
432 sub_packet->type &= 0x7f;
433 }
434
435 if (sub_packet->type == 0x7f)
436 sub_packet->type |= bitstream_read(bc, 8) << 8;
437
438 // FIXME: this depends on bitreader-internal data
439 sub_packet->data = &bc->buffer[bitstream_tell(bc) / 8];
440 }
441
442 av_log(NULL, AV_LOG_DEBUG, "Subpacket: type=%d size=%d start_offs=%x\n",
443 sub_packet->type, sub_packet->size, bitstream_tell(bc) / 8);
444 }
445
446 /**
447 * Return node pointer to first packet of requested type in list.
448 *
449 * @param list list of subpackets to be scanned
450 * @param type type of searched subpacket
451 * @return node pointer for subpacket if found, else NULL
452 */
453 static QDM2SubPNode *qdm2_search_subpacket_type_in_list(QDM2SubPNode *list,
454 int type)
455 {
456 while (list && list->packet) {
457 if (list->packet->type == type)
458 return list;
459 list = list->next;
460 }
461 return NULL;
462 }
463
464 /**
465 * Replace 8 elements with their average value.
466 * Called by qdm2_decode_superblock before starting subblock decoding.
467 *
468 * @param q context
469 */
470 static void average_quantized_coeffs(QDM2Context *q)
471 {
472 int i, j, n, ch, sum;
473
474 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
475
476 for (ch = 0; ch < q->nb_channels; ch++)
477 for (i = 0; i < n; i++) {
478 sum = 0;
479
480 for (j = 0; j < 8; j++)
481 sum += q->quantized_coeffs[ch][i][j];
482
483 sum /= 8;
484 if (sum > 0)
485 sum--;
486
487 for (j = 0; j < 8; j++)
488 q->quantized_coeffs[ch][i][j] = sum;
489 }
490 }
491
492 /**
493 * Build subband samples with noise weighted by q->tone_level.
494 * Called by synthfilt_build_sb_samples.
495 *
496 * @param q context
497 * @param sb subband index
498 */
499 static void build_sb_samples_from_noise(QDM2Context *q, int sb)
500 {
501 int ch, j;
502
503 FIX_NOISE_IDX(q->noise_idx);
504
505 if (!q->nb_channels)
506 return;
507
508 for (ch = 0; ch < q->nb_channels; ch++) {
509 for (j = 0; j < 64; j++) {
510 q->sb_samples[ch][j * 2][sb] =
511 SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
512 q->sb_samples[ch][j * 2 + 1][sb] =
513 SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
514 }
515 }
516 }
517
518 /**
519 * Called while processing data from subpackets 11 and 12.
520 * Used after making changes to coding_method array.
521 *
522 * @param sb subband index
523 * @param channels number of channels
524 * @param coding_method q->coding_method[0][0][0]
525 */
526 static int fix_coding_method_array(int sb, int channels,
527 sb_int8_array coding_method)
528 {
529 int j, k;
530 int ch;
531 int run, case_val;
532
533 for (ch = 0; ch < channels; ch++) {
534 for (j = 0; j < 64; ) {
535 if (coding_method[ch][sb][j] < 8)
536 return -1;
537 if ((coding_method[ch][sb][j] - 8) > 22) {
538 run = 1;
539 case_val = 8;
540 } else {
541 switch (switchtable[coding_method[ch][sb][j] - 8]) {
542 case 0: run = 10;
543 case_val = 10;
544 break;
545 case 1: run = 1;
546 case_val = 16;
547 break;
548 case 2: run = 5;
549 case_val = 24;
550 break;
551 case 3: run = 3;
552 case_val = 30;
553 break;
554 case 4: run = 1;
555 case_val = 30;
556 break;
557 case 5: run = 1;
558 case_val = 8;
559 break;
560 default: run = 1;
561 case_val = 8;
562 break;
563 }
564 }
565 for (k = 0; k < run; k++) {
566 if (j + k < 128) {
567 if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j]) {
568 if (k > 0) {
569 SAMPLES_NEEDED
570 //not debugged, almost never used
571 memset(&coding_method[ch][sb][j + k], case_val,
572 k *sizeof(int8_t));
573 memset(&coding_method[ch][sb][j + k], case_val,
574 3 * sizeof(int8_t));
575 }
576 }
577 }
578 }
579 j += run;
580 }
581 }
582 return 0;
583 }
584
585 /**
586 * Related to synthesis filter
587 * Called by process_subpacket_10
588 *
589 * @param q context
590 * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10
591 */
592 static void fill_tone_level_array(QDM2Context *q, int flag)
593 {
594 int i, sb, ch, sb_used;
595 int tmp, tab;
596
597 for (ch = 0; ch < q->nb_channels; ch++)
598 for (sb = 0; sb < 30; sb++)
599 for (i = 0; i < 8; i++) {
600 if ((tab=coeff_per_sb_for_dequant[q->coeff_per_sb_select][sb]) < (last_coeff[q->coeff_per_sb_select] - 1))
601 tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
602 q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
603 else
604 tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
605 if(tmp < 0)
606 tmp += 0xff;
607 q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
608 }
609
610 sb_used = QDM2_SB_USED(q->sub_sampling);
611
612 if ((q->superblocktype_2_3 != 0) && !flag) {
613 for (sb = 0; sb < sb_used; sb++)
614 for (ch = 0; ch < q->nb_channels; ch++)
615 for (i = 0; i < 64; i++) {
616 q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
617 if (q->tone_level_idx[ch][sb][i] < 0)
618 q->tone_level[ch][sb][i] = 0;
619 else
620 q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
621 }
622 } else {
623 tab = q->superblocktype_2_3 ? 0 : 1;
624 for (sb = 0; sb < sb_used; sb++) {
625 if ((sb >= 4) && (sb <= 23)) {
626 for (ch = 0; ch < q->nb_channels; ch++)
627 for (i = 0; i < 64; i++) {
628 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
629 q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
630 q->tone_level_idx_mid[ch][sb - 4][i / 8] -
631 q->tone_level_idx_hi2[ch][sb - 4];
632 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
633 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
634 q->tone_level[ch][sb][i] = 0;
635 else
636 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
637 }
638 } else {
639 if (sb > 4) {
640 for (ch = 0; ch < q->nb_channels; ch++)
641 for (i = 0; i < 64; i++) {
642 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
643 q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
644 q->tone_level_idx_hi2[ch][sb - 4];
645 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
646 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
647 q->tone_level[ch][sb][i] = 0;
648 else
649 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
650 }
651 } else {
652 for (ch = 0; ch < q->nb_channels; ch++)
653 for (i = 0; i < 64; i++) {
654 tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
655 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
656 q->tone_level[ch][sb][i] = 0;
657 else
658 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
659 }
660 }
661 }
662 }
663 }
664 }
665
666 /**
667 * Related to synthesis filter
668 * Called by process_subpacket_11
669 * c is built with data from subpacket 11
670 * Most of this function is used only if superblock_type_2_3 == 0,
671 * never seen it in samples.
672 *
673 * @param tone_level_idx
674 * @param tone_level_idx_temp
675 * @param coding_method q->coding_method[0][0][0]
676 * @param nb_channels number of channels
677 * @param c coming from subpacket 11, passed as 8*c
678 * @param superblocktype_2_3 flag based on superblock packet type
679 * @param cm_table_select q->cm_table_select
680 */
681 static void fill_coding_method_array(sb_int8_array tone_level_idx,
682 sb_int8_array tone_level_idx_temp,
683 sb_int8_array coding_method,
684 int nb_channels,
685 int c, int superblocktype_2_3,
686 int cm_table_select)
687 {
688 int ch, sb, j;
689 int tmp, acc, esp_40, comp;
690 int add1, add2, add3, add4;
691 int64_t multres;
692
693 if (!superblocktype_2_3) {
694 /* This case is untested, no samples available */
695 SAMPLES_NEEDED
696 for (ch = 0; ch < nb_channels; ch++)
697 for (sb = 0; sb < 30; sb++) {
698 for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
699 add1 = tone_level_idx[ch][sb][j] - 10;
700 if (add1 < 0)
701 add1 = 0;
702 add2 = add3 = add4 = 0;
703 if (sb > 1) {
704 add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
705 if (add2 < 0)
706 add2 = 0;
707 }
708 if (sb > 0) {
709 add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
710 if (add3 < 0)
711 add3 = 0;
712 }
713 if (sb < 29) {
714 add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
715 if (add4 < 0)
716 add4 = 0;
717 }
718 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
719 if (tmp < 0)
720 tmp = 0;
721 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
722 }
723 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
724 }
725
726 acc = 0;
727 for (ch = 0; ch < nb_channels; ch++)
728 for (sb = 0; sb < 30; sb++)
729 for (j = 0; j < 64; j++)
730 acc += tone_level_idx_temp[ch][sb][j];
731
732 multres = 0x66666667LL * (acc * 10);
733 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
734 for (ch = 0; ch < nb_channels; ch++)
735 for (sb = 0; sb < 30; sb++)
736 for (j = 0; j < 64; j++) {
737 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
738 if (comp < 0)
739 comp += 0xff;
740 comp /= 256; // signed shift
741 switch(sb) {
742 case 0:
743 if (comp < 30)
744 comp = 30;
745 comp += 15;
746 break;
747 case 1:
748 if (comp < 24)
749 comp = 24;
750 comp += 10;
751 break;
752 case 2:
753 case 3:
754 case 4:
755 if (comp < 16)
756 comp = 16;
757 }
758 if (comp <= 5)
759 tmp = 0;
760 else if (comp <= 10)
761 tmp = 10;
762 else if (comp <= 16)
763 tmp = 16;
764 else if (comp <= 24)
765 tmp = -1;
766 else
767 tmp = 0;
768 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
769 }
770 for (sb = 0; sb < 30; sb++)
771 fix_coding_method_array(sb, nb_channels, coding_method);
772 for (ch = 0; ch < nb_channels; ch++)
773 for (sb = 0; sb < 30; sb++)
774 for (j = 0; j < 64; j++)
775 if (sb >= 10) {
776 if (coding_method[ch][sb][j] < 10)
777 coding_method[ch][sb][j] = 10;
778 } else {
779 if (sb >= 2) {
780 if (coding_method[ch][sb][j] < 16)
781 coding_method[ch][sb][j] = 16;
782 } else {
783 if (coding_method[ch][sb][j] < 30)
784 coding_method[ch][sb][j] = 30;
785 }
786 }
787 } else { // superblocktype_2_3 != 0
788 for (ch = 0; ch < nb_channels; ch++)
789 for (sb = 0; sb < 30; sb++)
790 for (j = 0; j < 64; j++)
791 coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
792 }
793 }
794
795 /**
796 * Called by process_subpacket_11 to process more data from subpacket 11
797 * with sb 0-8.
798 * Called by process_subpacket_12 to process data from subpacket 12 with
799 * sb 8-sb_used.
800 *
801 * @param q context
802 * @param bc bitreader context
803 * @param length packet length in bits
804 * @param sb_min lower subband processed (sb_min included)
805 * @param sb_max higher subband processed (sb_max excluded)
806 */
807 static void synthfilt_build_sb_samples(QDM2Context *q, BitstreamContext *bc,
808 int length, int sb_min, int sb_max)
809 {
810 int sb, j, k, n, ch, run, channels;
811 int joined_stereo, zero_encoding;
812 int type34_first;
813 float type34_div = 0;
814 float type34_predictor;
815 float samples[10], sign_bits[16];
816
817 if (length == 0) {
818 // If no data use noise
819 for (sb=sb_min; sb < sb_max; sb++)
820 build_sb_samples_from_noise(q, sb);
821
822 return;
823 }
824
825 for (sb = sb_min; sb < sb_max; sb++) {
826 channels = q->nb_channels;
827
828 if (q->nb_channels <= 1 || sb < 12)
829 joined_stereo = 0;
830 else if (sb >= 24)
831 joined_stereo = 1;
832 else
833 joined_stereo = (bitstream_bits_left(bc) >= 1) ? bitstream_read_bit(bc) : 0;
834
835 if (joined_stereo) {
836 if (bitstream_bits_left(bc) >= 16)
837 for (j = 0; j < 16; j++)
838 sign_bits[j] = bitstream_read_bit(bc);
839
840 for (j = 0; j < 64; j++)
841 if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
842 q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
843
844 if (fix_coding_method_array(sb, q->nb_channels,
845 q->coding_method)) {
846 build_sb_samples_from_noise(q, sb);
847 continue;
848 }
849 channels = 1;
850 }
851
852 for (ch = 0; ch < channels; ch++) {
853 FIX_NOISE_IDX(q->noise_idx);
854 zero_encoding = (bitstream_bits_left(bc) >= 1) ? bitstream_read_bit(bc) : 0;
855 type34_predictor = 0.0;
856 type34_first = 1;
857
858 for (j = 0; j < 128; ) {
859 switch (q->coding_method[ch][sb][j / 2]) {
860 case 8:
861 if (bitstream_bits_left(bc) >= 10) {
862 if (zero_encoding) {
863 for (k = 0; k < 5; k++) {
864 if ((j + 2 * k) >= 128)
865 break;
866 samples[2 * k] = bitstream_read_bit(bc) ? dequant_1bit[joined_stereo][2 * bitstream_read_bit(bc)] : 0;
867 }
868 } else {
869 n = bitstream_read(bc, 8);
870 for (k = 0; k < 5; k++)
871 samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
872 }
873 for (k = 0; k < 5; k++)
874 samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
875 } else {
876 for (k = 0; k < 10; k++)
877 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
878 }
879 run = 10;
880 break;
881
882 case 10:
883 if (bitstream_bits_left(bc) >= 1) {
884 float f = 0.81;
885
886 if (bitstream_read_bit(bc))
887 f = -f;
888 f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
889 samples[0] = f;
890 } else {
891 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
892 }
893 run = 1;
894 break;
895
896 case 16:
897 if (bitstream_bits_left(bc) >= 10) {
898 if (zero_encoding) {
899 for (k = 0; k < 5; k++) {
900 if ((j + k) >= 128)
901 break;
902 samples[k] = (bitstream_read_bit(bc) == 0) ? 0 : dequant_1bit[joined_stereo][2 * bitstream_read_bit(bc)];
903 }
904 } else {
905 n = bitstream_read (bc, 8);
906 for (k = 0; k < 5; k++)
907 samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
908 }
909 } else {
910 for (k = 0; k < 5; k++)
911 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
912 }
913 run = 5;
914 break;
915
916 case 24:
917 if (bitstream_bits_left(bc) >= 7) {
918 n = bitstream_read(bc, 7);
919 for (k = 0; k < 3; k++)
920 samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
921 } else {
922 for (k = 0; k < 3; k++)
923 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
924 }
925 run = 3;
926 break;
927
928 case 30:
929 if (bitstream_bits_left(bc) >= 4) {
930 unsigned index = qdm2_get_vlc(bc, &vlc_tab_type30, 0, 1);
931 if (index < FF_ARRAY_ELEMS(type30_dequant)) {
932 samples[0] = type30_dequant[index];
933 } else
934 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
935 } else
936 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
937
938 run = 1;
939 break;
940
941 case 34:
942 if (bitstream_bits_left(bc) >= 7) {
943 if (type34_first) {
944 type34_div = (float)(1 << bitstream_read(bc, 2));
945 samples[0] = ((float)bitstream_read(bc, 5) - 16.0) / 15.0;
946 type34_predictor = samples[0];
947 type34_first = 0;
948 } else {
949 unsigned index = qdm2_get_vlc(bc, &vlc_tab_type34, 0, 1);
950 if (index < FF_ARRAY_ELEMS(type34_delta)) {
951 samples[0] = type34_delta[index] / type34_div + type34_predictor;
952 type34_predictor = samples[0];
953 } else
954 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
955 }
956 } else {
957 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
958 }
959 run = 1;
960 break;
961
962 default:
963 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
964 run = 1;
965 break;
966 }
967
968 if (joined_stereo) {
969 for (k = 0; k < run && j + k < 128; k++) {
970 q->sb_samples[0][j + k][sb] =
971 q->tone_level[0][sb][(j + k) / 2] * samples[k];
972 if (q->nb_channels == 2) {
973 if (sign_bits[(j + k) / 8])
974 q->sb_samples[1][j + k][sb] =
975 q->tone_level[1][sb][(j + k) / 2] * -samples[k];
976 else
977 q->sb_samples[1][j + k][sb] =
978 q->tone_level[1][sb][(j + k) / 2] * samples[k];
979 }
980 }
981 } else {
982 for (k = 0; k < run; k++)
983 if ((j + k) < 128)
984 q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
985 }
986
987 j += run;
988 } // j loop
989 } // channel loop
990 } // subband loop
991 }
992
993 /**
994 * Init the first element of a channel in quantized_coeffs with data
995 * from packet 10 (quantized_coeffs[ch][0]).
996 * This is similar to process_subpacket_9, but for a single channel
997 * and for element [0]
998 * same VLC tables as process_subpacket_9 are used.
999 *
1000 * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
1001 * @param bc bitreader context
1002 */
1003 static void init_quantized_coeffs_elem0(int8_t *quantized_coeffs,
1004 BitstreamContext *bc)
1005 {
1006 int i, k, run, level, diff;
1007
1008 if (bitstream_bits_left(bc) < 16)
1009 return;
1010 level = qdm2_get_vlc(bc, &vlc_tab_level, 0, 2);
1011
1012 quantized_coeffs[0] = level;
1013
1014 for (i = 0; i < 7; ) {
1015 if (bitstream_bits_left(bc) < 16)
1016 break;
1017 run = qdm2_get_vlc(bc, &vlc_tab_run, 0, 1) + 1;
1018
1019 if (bitstream_bits_left(bc) < 16)
1020 break;
1021 diff = qdm2_get_se_vlc(&vlc_tab_diff, bc, 2);
1022
1023 for (k = 1; k <= run; k++)
1024 quantized_coeffs[i + k] = (level + ((k * diff) / run));
1025
1026 level += diff;
1027 i += run;
1028 }
1029 }
1030
1031 /**
1032 * Related to synthesis filter, process data from packet 10
1033 * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
1034 * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with
1035 * data from packet 10
1036 *
1037 * @param q context
1038 * @param bc bitreader context
1039 */
1040 static void init_tone_level_dequantization(QDM2Context *q, BitstreamContext *bc)
1041 {
1042 int sb, j, k, n, ch;
1043
1044 for (ch = 0; ch < q->nb_channels; ch++) {
1045 init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], bc);
1046
1047 if (bitstream_bits_left(bc) < 16) {
1048 memset(q->quantized_coeffs[ch][0], 0, 8);
1049 break;
1050 }
1051 }
1052
1053 n = q->sub_sampling + 1;
1054
1055 for (sb = 0; sb < n; sb++)
1056 for (ch = 0; ch < q->nb_channels; ch++)
1057 for (j = 0; j < 8; j++) {
1058 if (bitstream_bits_left(bc) < 1)
1059 break;
1060 if (bitstream_read_bit(bc)) {
1061 for (k=0; k < 8; k++) {
1062 if (bitstream_bits_left(bc) < 16)
1063 break;
1064 q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(bc, &vlc_tab_tone_level_idx_hi1, 0, 2);
1065 }
1066 } else {
1067 for (k=0; k < 8; k++)
1068 q->tone_level_idx_hi1[ch][sb][j][k] = 0;
1069 }
1070 }
1071
1072 n = QDM2_SB_USED(q->sub_sampling) - 4;
1073
1074 for (sb = 0; sb < n; sb++)
1075 for (ch = 0; ch < q->nb_channels; ch++) {
1076 if (bitstream_bits_left(bc) < 16)
1077 break;
1078 q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(bc, &vlc_tab_tone_level_idx_hi2, 0, 2);
1079 if (sb > 19)
1080 q->tone_level_idx_hi2[ch][sb] -= 16;
1081 else
1082 for (j = 0; j < 8; j++)
1083 q->tone_level_idx_mid[ch][sb][j] = -16;
1084 }
1085
1086 n = QDM2_SB_USED(q->sub_sampling) - 5;
1087
1088 for (sb = 0; sb < n; sb++)
1089 for (ch = 0; ch < q->nb_channels; ch++)
1090 for (j = 0; j < 8; j++) {
1091 if (bitstream_bits_left(bc) < 16)
1092 break;
1093 q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(bc, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
1094 }
1095 }
1096
1097 /**
1098 * Process subpacket 9, init quantized_coeffs with data from it
1099 *
1100 * @param q context
1101 * @param node pointer to node with packet
1102 */
1103 static void process_subpacket_9(QDM2Context *q, QDM2SubPNode *node)
1104 {
1105 BitstreamContext bc;
1106 int i, j, k, n, ch, run, level, diff;
1107
1108 bitstream_init(&bc, node->packet->data, node->packet->size * 8);
1109
1110 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
1111
1112 for (i = 1; i < n; i++)
1113 for (ch = 0; ch < q->nb_channels; ch++) {
1114 level = qdm2_get_vlc(&bc, &vlc_tab_level, 0, 2);
1115 q->quantized_coeffs[ch][i][0] = level;
1116
1117 for (j = 0; j < (8 - 1); ) {
1118 run = qdm2_get_vlc(&bc, &vlc_tab_run, 0, 1) + 1;
1119 diff = qdm2_get_se_vlc(&vlc_tab_diff, &bc, 2);
1120
1121 for (k = 1; k <= run; k++)
1122 q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run));
1123
1124 level += diff;
1125 j += run;
1126 }
1127 }
1128
1129 for (ch = 0; ch < q->nb_channels; ch++)
1130 for (i = 0; i < 8; i++)
1131 q->quantized_coeffs[ch][0][i] = 0;
1132 }
1133
1134 /**
1135 * Process subpacket 10 if not null, else
1136 *
1137 * @param q context
1138 * @param node pointer to node with packet
1139 */
1140 static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node)
1141 {
1142 BitstreamContext bc;
1143
1144 if (node) {
1145 bitstream_init(&bc, node->packet->data, node->packet->size * 8);
1146 init_tone_level_dequantization(q, &bc);
1147 fill_tone_level_array(q, 1);
1148 } else {
1149 fill_tone_level_array(q, 0);
1150 }
1151 }
1152
1153 /**
1154 * Process subpacket 11
1155 *
1156 * @param q context
1157 * @param node pointer to node with packet
1158 */
1159 static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node)
1160 {
1161 BitstreamContext bc;
1162 int length = 0;
1163
1164 if (node) {
1165 length = node->packet->size * 8;
1166 bitstream_init(&bc, node->packet->data, length);
1167 }
1168
1169 if (length >= 32) {
1170 int c = bitstream_read(&bc, 13);
1171
1172 if (c > 3)
1173 fill_coding_method_array(q->tone_level_idx,
1174 q->tone_level_idx_temp, q->coding_method,
1175 q->nb_channels, 8 * c,
1176 q->superblocktype_2_3, q->cm_table_select);
1177 }
1178
1179 synthfilt_build_sb_samples(q, &bc, length, 0, 8);
1180 }
1181
1182 /**
1183 * Process subpacket 12
1184 *
1185 * @param q context
1186 * @param node pointer to node with packet
1187 */
1188 static void process_subpacket_12(QDM2Context *q, QDM2SubPNode *node)
1189 {
1190 BitstreamContext bc;
1191 int length = 0;
1192
1193 if (node) {
1194 length = node->packet->size * 8;
1195 bitstream_init(&bc, node->packet->data, length);
1196 }
1197
1198 synthfilt_build_sb_samples(q, &bc, length, 8, QDM2_SB_USED(q->sub_sampling));
1199 }
1200
1201 /*
1202 * Process new subpackets for synthesis filter
1203 *
1204 * @param q context
1205 * @param list list with synthesis filter packets (list D)
1206 */
1207 static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list)
1208 {
1209 QDM2SubPNode *nodes[4];
1210
1211 nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
1212 if (nodes[0])
1213 process_subpacket_9(q, nodes[0]);
1214
1215 nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
1216 if (nodes[1])
1217 process_subpacket_10(q, nodes[1]);
1218 else
1219 process_subpacket_10(q, NULL);
1220
1221 nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
1222 if (nodes[0] && nodes[1] && nodes[2])
1223 process_subpacket_11(q, nodes[2]);
1224 else
1225 process_subpacket_11(q, NULL);
1226
1227 nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
1228 if (nodes[0] && nodes[1] && nodes[3])
1229 process_subpacket_12(q, nodes[3]);
1230 else
1231 process_subpacket_12(q, NULL);
1232 }
1233
1234 /*
1235 * Decode superblock, fill packet lists.
1236 *
1237 * @param q context
1238 */
1239 static void qdm2_decode_super_block(QDM2Context *q)
1240 {
1241 BitstreamContext bc;
1242 QDM2SubPacket header, *packet;
1243 int i, packet_bytes, sub_packet_size, sub_packets_D;
1244 unsigned int next_index = 0;
1245
1246 memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
1247 memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
1248 memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
1249
1250 q->sub_packets_B = 0;
1251 sub_packets_D = 0;
1252
1253 average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
1254
1255 bitstream_init(&bc, q->compressed_data, q->compressed_size * 8);
1256 qdm2_decode_sub_packet_header(&bc, &header);
1257
1258 if (header.type < 2 || header.type >= 8) {
1259 q->has_errors = 1;
1260 av_log(NULL, AV_LOG_ERROR, "bad superblock type\n");
1261 return;
1262 }
1263
1264 q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
1265 packet_bytes = (q->compressed_size - bitstream_tell(&bc) / 8);
1266
1267 bitstream_init(&bc, header.data, header.size * 8);
1268
1269 if (header.type == 2 || header.type == 4 || header.type == 5) {
1270 int csum = 257 * bitstream_read(&bc, 8);
1271 csum += 2 * bitstream_read(&bc, 8);
1272
1273 csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
1274
1275 if (csum != 0) {
1276 q->has_errors = 1;
1277 av_log(NULL, AV_LOG_ERROR, "bad packet checksum\n");
1278 return;
1279 }
1280 }
1281
1282 q->sub_packet_list_B[0].packet = NULL;
1283 q->sub_packet_list_D[0].packet = NULL;
1284
1285 for (i = 0; i < 6; i++)
1286 if (--q->fft_level_exp[i] < 0)
1287 q->fft_level_exp[i] = 0;
1288
1289 for (i = 0; packet_bytes > 0; i++) {
1290 int j;
1291
1292 if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
1293 SAMPLES_NEEDED_2("too many packet bytes");
1294 return;
1295 }
1296
1297 q->sub_packet_list_A[i].next = NULL;
1298
1299 if (i > 0) {
1300 q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
1301
1302 /* seek to next block */
1303 bitstream_init(&bc, header.data, header.size * 8);
1304 bitstream_skip(&bc, next_index * 8);
1305
1306 if (next_index >= header.size)
1307 break;
1308 }
1309
1310 /* decode subpacket */
1311 packet = &q->sub_packets[i];
1312 qdm2_decode_sub_packet_header(&bc, packet);
1313 next_index = packet->size + bitstream_tell(&bc) / 8;
1314 sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
1315
1316 if (packet->type == 0)
1317 break;
1318
1319 if (sub_packet_size > packet_bytes) {
1320 if (packet->type != 10 && packet->type != 11 && packet->type != 12)
1321 break;
1322 packet->size += packet_bytes - sub_packet_size;
1323 }
1324
1325 packet_bytes -= sub_packet_size;
1326
1327 /* add subpacket to 'all subpackets' list */
1328 q->sub_packet_list_A[i].packet = packet;
1329
1330 /* add subpacket to related list */
1331 if (packet->type == 8) {
1332 SAMPLES_NEEDED_2("packet type 8");
1333 return;
1334 } else if (packet->type >= 9 && packet->type <= 12) {
1335 /* packets for MPEG Audio like Synthesis Filter */
1336 QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
1337 } else if (packet->type == 13) {
1338 for (j = 0; j < 6; j++)
1339 q->fft_level_exp[j] = bitstream_read(&bc, 6);
1340 } else if (packet->type == 14) {
1341 for (j = 0; j < 6; j++)
1342 q->fft_level_exp[j] = qdm2_get_vlc(&bc, &fft_level_exp_vlc, 0, 2);
1343 } else if (packet->type == 15) {
1344 SAMPLES_NEEDED_2("packet type 15")
1345 return;
1346 } else if (packet->type >= 16 && packet->type < 48 &&
1347 !fft_subpackets[packet->type - 16]) {
1348 /* packets for FFT */
1349 QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);
1350 }
1351 } // Packet bytes loop
1352
1353 if (q->sub_packet_list_D[0].packet) {
1354 process_synthesis_subpackets(q, q->sub_packet_list_D);
1355 q->do_synth_filter = 1;
1356 } else if (q->do_synth_filter) {
1357 process_subpacket_10(q, NULL);
1358 process_subpacket_11(q, NULL);
1359 process_subpacket_12(q, NULL);
1360 }
1361 }
1362
1363 static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet,
1364 int offset, int duration, int channel,
1365 int exp, int phase)
1366 {
1367 if (q->fft_coefs_min_index[duration] < 0)
1368 q->fft_coefs_min_index[duration] = q->fft_coefs_index;
1369
1370 q->fft_coefs[q->fft_coefs_index].sub_packet =
1371 ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1372 q->fft_coefs[q->fft_coefs_index].channel = channel;
1373 q->fft_coefs[q->fft_coefs_index].offset = offset;
1374 q->fft_coefs[q->fft_coefs_index].exp = exp;
1375 q->fft_coefs[q->fft_coefs_index].phase = phase;
1376 q->fft_coefs_index++;
1377 }
1378
1379 static void qdm2_fft_decode_tones(QDM2Context *q, int duration,
1380 BitstreamContext *bc, int b)
1381 {
1382 int channel, stereo, phase, exp;
1383 int local_int_4, local_int_8, stereo_phase, local_int_10;
1384 int local_int_14, stereo_exp, local_int_20, local_int_28;
1385 int n, offset;
1386
1387 local_int_4 = 0;
1388 local_int_28 = 0;
1389 local_int_20 = 2;
1390 local_int_8 = (4 - duration);
1391 local_int_10 = 1 << (q->group_order - duration - 1);
1392 offset = 1;
1393
1394 while (1) {
1395 if (q->superblocktype_2_3) {
1396 while ((n = qdm2_get_vlc(bc, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1397 offset = 1;
1398 if (n == 0) {
1399 local_int_4 += local_int_10;
1400 local_int_28 += (1 << local_int_8);
1401 } else {
1402 local_int_4 += 8 * local_int_10;
1403 local_int_28 += (8 << local_int_8);
1404 }
1405 }
1406 offset += (n - 2);
1407 } else {
1408 offset += qdm2_get_vlc(bc, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1409 while (offset >= (local_int_10 - 1)) {
1410 offset += (1 - (local_int_10 - 1));
1411 local_int_4 += local_int_10;
1412 local_int_28 += (1 << local_int_8);
1413 }
1414 }
1415
1416 if (local_int_4 >= q->group_size)
1417 return;
1418
1419 local_int_14 = (offset >> local_int_8);
1420 if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
1421 return;
1422
1423 if (q->nb_channels > 1) {
1424 channel = bitstream_read_bit(bc);
1425 stereo = bitstream_read_bit(bc);
1426 } else {
1427 channel = 0;
1428 stereo = 0;
1429 }
1430
1431 exp = qdm2_get_vlc(bc, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1432 exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
1433 exp = (exp < 0) ? 0 : exp;
1434
1435 phase = bitstream_read(bc, 3);
1436 stereo_exp = 0;
1437 stereo_phase = 0;
1438
1439 if (stereo) {
1440 stereo_exp = (exp - qdm2_get_vlc(bc, &fft_stereo_exp_vlc, 0, 1));
1441 stereo_phase = (phase - qdm2_get_vlc(bc, &fft_stereo_phase_vlc, 0, 1));
1442 if (stereo_phase < 0)
1443 stereo_phase += 8;
1444 }
1445
1446 if (q->frequency_range > (local_int_14 + 1)) {
1447 int sub_packet = (local_int_20 + local_int_28);
1448
1449 qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1450 channel, exp, phase);
1451 if (stereo)
1452 qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1453 1 - channel,
1454 stereo_exp, stereo_phase);
1455 }
1456 offset++;
1457 }
1458 }
1459
1460 static void qdm2_decode_fft_packets(QDM2Context *q)
1461 {
1462 int i, j, min, max, value, type, unknown_flag;
1463 BitstreamContext bc;
1464
1465 if (!q->sub_packet_list_B[0].packet)
1466 return;
1467
1468 /* reset minimum indexes for FFT coefficients */
1469 q->fft_coefs_index = 0;
1470 for (i = 0; i < 5; i++)
1471 q->fft_coefs_min_index[i] = -1;
1472
1473 /* process subpackets ordered by type, largest type first */
1474 for (i = 0, max = 256; i < q->sub_packets_B; i++) {
1475 QDM2SubPacket *packet = NULL;
1476
1477 /* find subpacket with largest type less than max */
1478 for (j = 0, min = 0; j < q->sub_packets_B; j++) {
1479 value = q->sub_packet_list_B[j].packet->type;
1480 if (value > min && value < max) {
1481 min = value;
1482 packet = q->sub_packet_list_B[j].packet;
1483 }
1484 }
1485
1486 max = min;
1487
1488 /* check for errors (?) */
1489 if (!packet)
1490 return;
1491
1492 if (i == 0 &&
1493 (packet->type < 16 || packet->type >= 48 ||
1494 fft_subpackets[packet->type - 16]))
1495 return;
1496
1497 /* decode FFT tones */
1498 bitstream_init(&bc, packet->data, packet->size * 8);
1499
1500 if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
1501 unknown_flag = 1;
1502 else
1503 unknown_flag = 0;
1504
1505 type = packet->type;
1506
1507 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1508 int duration = q->sub_sampling + 5 - (type & 15);
1509
1510 if (duration >= 0 && duration < 4)
1511 qdm2_fft_decode_tones(q, duration, &bc, unknown_flag);
1512 } else if (type == 31) {
1513 for (j = 0; j < 4; j++)
1514 qdm2_fft_decode_tones(q, j, &bc, unknown_flag);
1515 } else if (type == 46) {
1516 for (j = 0; j < 6; j++)
1517 q->fft_level_exp[j] = bitstream_read(&bc, 6);
1518 for (j = 0; j < 4; j++)
1519 qdm2_fft_decode_tones(q, j, &bc, unknown_flag);
1520 }
1521 } // Loop on B packets
1522
1523 /* calculate maximum indexes for FFT coefficients */
1524 for (i = 0, j = -1; i < 5; i++)
1525 if (q->fft_coefs_min_index[i] >= 0) {
1526 if (j >= 0)
1527 q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i];
1528 j = i;
1529 }
1530 if (j >= 0)
1531 q->fft_coefs_max_index[j] = q->fft_coefs_index;
1532 }
1533
1534 static void qdm2_fft_generate_tone(QDM2Context *q, FFTTone *tone)
1535 {
1536 float level, f[6];
1537 int i;
1538 QDM2Complex c;
1539 const double iscale = 2.0 * M_PI / 512.0;
1540
1541 tone->phase += tone->phase_shift;
1542
1543 /* calculate current level (maximum amplitude) of tone */
1544 level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
1545 c.im = level * sin(tone->phase * iscale);
1546 c.re = level * cos(tone->phase * iscale);
1547
1548 /* generate FFT coefficients for tone */
1549 if (tone->duration >= 3 || tone->cutoff >= 3) {
1550 tone->complex[0].im += c.im;
1551 tone->complex[0].re += c.re;
1552 tone->complex[1].im -= c.im;
1553 tone->complex[1].re -= c.re;
1554 } else {
1555 f[1] = -tone->table[4];
1556 f[0] = tone->table[3] - tone->table[0];
1557 f[2] = 1.0 - tone->table[2] - tone->table[3];
1558 f[3] = tone->table[1] + tone->table[4] - 1.0;
1559 f[4] = tone->table[0] - tone->table[1];
1560 f[5] = tone->table[2];
1561 for (i = 0; i < 2; i++) {
1562 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re +=
1563 c.re * f[i];
1564 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im +=
1565 c.im * ((tone->cutoff <= i) ? -f[i] : f[i]);
1566 }
1567 for (i = 0; i < 4; i++) {
1568 tone->complex[i].re += c.re * f[i + 2];
1569 tone->complex[i].im += c.im * f[i + 2];
1570 }
1571 }
1572
1573 /* copy the tone if it has not yet died out */
1574 if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
1575 memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
1576 q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
1577 }
1578 }
1579
1580 static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
1581 {
1582 int i, j, ch;
1583 const double iscale = 0.25 * M_PI;
1584
1585 for (ch = 0; ch < q->channels; ch++) {
1586 memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
1587 }
1588
1589
1590 /* apply FFT tones with duration 4 (1 FFT period) */
1591 if (q->fft_coefs_min_index[4] >= 0)
1592 for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
1593 float level;
1594 QDM2Complex c;
1595
1596 if (q->fft_coefs[i].sub_packet != sub_packet)
1597 break;
1598
1599 ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
1600 level = (q->fft_coefs[i].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[i].exp & 63];
1601
1602 c.re = level * cos(q->fft_coefs[i].phase * iscale);
1603 c.im = level * sin(q->fft_coefs[i].phase * iscale);
1604 q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
1605 q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
1606 q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
1607 q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
1608 }
1609
1610 /* generate existing FFT tones */
1611 for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
1612 qdm2_fft_generate_tone(q, &q->fft_tones[q->fft_tone_start]);
1613 q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
1614 }
1615
1616 /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1617 for (i = 0; i < 4; i++)
1618 if (q->fft_coefs_min_index[i] >= 0) {
1619 for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
1620 int offset, four_i;
1621 FFTTone tone;
1622
1623 if (q->fft_coefs[j].sub_packet != sub_packet)
1624 break;
1625
1626 four_i = (4 - i);
1627 offset = q->fft_coefs[j].offset >> four_i;
1628 ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
1629
1630 if (offset < q->frequency_range) {
1631 if (offset < 2)
1632 tone.cutoff = offset;
1633 else
1634 tone.cutoff = (offset >= 60) ? 3 : 2;
1635
1636 tone.level = (q->fft_coefs[j].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[j].exp & 63];
1637 tone.complex = &q->fft.complex[ch][offset];
1638 tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
1639 tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
1640 tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
1641 tone.duration = i;
1642 tone.time_index = 0;
1643
1644 qdm2_fft_generate_tone(q, &tone);
1645 }
1646 }
1647 q->fft_coefs_min_index[i] = j;
1648 }
1649 }
1650
1651 static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
1652 {
1653 const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
1654 float *out = q->output_buffer + channel;
1655 int i;
1656 q->fft.complex[channel][0].re *= 2.0f;
1657 q->fft.complex[channel][0].im = 0.0f;
1658 q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
1659 /* add samples to output buffer */
1660 for (i = 0; i < FFALIGN(q->fft_size, 8); i++) {
1661 out[0] += q->fft.complex[channel][i].re * gain;
1662 out[q->channels] += q->fft.complex[channel][i].im * gain;
1663 out += 2 * q->channels;
1664 }
1665 }
1666
1667 /**
1668 * @param q context
1669 * @param index subpacket number
1670 */
1671 static void qdm2_synthesis_filter(QDM2Context *q, int index)
1672 {
1673 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1674
1675 /* copy sb_samples */
1676 sb_used = QDM2_SB_USED(q->sub_sampling);
1677
1678 for (ch = 0; ch < q->channels; ch++)
1679 for (i = 0; i < 8; i++)
1680 for (k = sb_used; k < SBLIMIT; k++)
1681 q->sb_samples[ch][(8 * index) + i][k] = 0;
1682
1683 for (ch = 0; ch < q->nb_channels; ch++) {
1684 float *samples_ptr = q->samples + ch;
1685
1686 for (i = 0; i < 8; i++) {
1687 ff_mpa_synth_filter_float(&q->mpadsp,
1688 q->synth_buf[ch], &(q->synth_buf_offset[ch]),
1689 ff_mpa_synth_window_float, &dither_state,
1690 samples_ptr, q->nb_channels,
1691 q->sb_samples[ch][(8 * index) + i]);
1692 samples_ptr += 32 * q->nb_channels;
1693 }
1694 }
1695
1696 /* add samples to output buffer */
1697 sub_sampling = (4 >> q->sub_sampling);
1698
1699 for (ch = 0; ch < q->channels; ch++)
1700 for (i = 0; i < q->frame_size; i++)
1701 q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
1702 }
1703
1704 /**
1705 * Init static data (does not depend on specific file)
1706 *
1707 * @param q context
1708 */
1709 static av_cold void qdm2_init_static_data(AVCodec *codec) {
1710 qdm2_init_vlc();
1711 ff_mpa_synth_init_float(ff_mpa_synth_window_float);
1712 softclip_table_init();
1713 rnd_table_init();
1714 init_noise_samples();
1715 }
1716
1717 /**
1718 * Init parameters from codec extradata
1719 */
1720 static av_cold int qdm2_decode_init(AVCodecContext *avctx)
1721 {
1722 QDM2Context *s = avctx->priv_data;
1723 uint8_t *extradata;
1724 int extradata_size;
1725 int tmp_val, tmp, size;
1726
1727 /* extradata parsing
1728
1729 Structure:
1730 wave {
1731 frma (QDM2)
1732 QDCA
1733 QDCP
1734 }
1735
1736 32 size (including this field)
1737 32 tag (=frma)
1738 32 type (=QDM2 or QDMC)
1739
1740 32 size (including this field, in bytes)
1741 32 tag (=QDCA) // maybe mandatory parameters
1742 32 unknown (=1)
1743 32 channels (=2)
1744 32 samplerate (=44100)
1745 32 bitrate (=96000)
1746 32 block size (=4096)
1747 32 frame size (=256) (for one channel)
1748 32 packet size (=1300)
1749
1750 32 size (including this field, in bytes)
1751 32 tag (=QDCP) // maybe some tuneable parameters
1752 32 float1 (=1.0)
1753 32 zero ?
1754 32 float2 (=1.0)
1755 32 float3 (=1.0)
1756 32 unknown (27)
1757 32 unknown (8)
1758 32 zero ?
1759 */
1760
1761 if (!avctx->extradata || (avctx->extradata_size < 48)) {
1762 av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
1763 return AVERROR_INVALIDDATA;
1764 }
1765
1766 extradata = avctx->extradata;
1767 extradata_size = avctx->extradata_size;
1768
1769 while (extradata_size > 7) {
1770 if (!memcmp(extradata, "frmaQDM", 7))
1771 break;
1772 extradata++;
1773 extradata_size--;
1774 }
1775
1776 if (extradata_size < 12) {
1777 av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
1778 extradata_size);
1779 return AVERROR_INVALIDDATA;
1780 }
1781
1782 if (memcmp(extradata, "frmaQDM", 7)) {
1783 av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
1784 return AVERROR_INVALIDDATA;
1785 }
1786
1787 if (extradata[7] == 'C') {
1788 // s->is_qdmc = 1;
1789 avpriv_report_missing_feature(avctx, "QDMC version 1");
1790 return AVERROR_PATCHWELCOME;
1791 }
1792
1793 extradata += 8;
1794 extradata_size -= 8;
1795
1796 size = AV_RB32(extradata);
1797
1798 if(size > extradata_size){
1799 av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
1800 extradata_size, size);
1801 return AVERROR_INVALIDDATA;
1802 }
1803
1804 extradata += 4;
1805 av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
1806 if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
1807 av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
1808 return AVERROR_INVALIDDATA;
1809 }
1810
1811 extradata += 8;
1812
1813 avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
1814 extradata += 4;
1815 if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS)
1816 return AVERROR_INVALIDDATA;
1817 avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
1818 AV_CH_LAYOUT_MONO;
1819
1820 avctx->sample_rate = AV_RB32(extradata);
1821 extradata += 4;
1822
1823 avctx->bit_rate = AV_RB32(extradata);
1824 extradata += 4;
1825
1826 s->group_size = AV_RB32(extradata);
1827 extradata += 4;
1828
1829 s->fft_size = AV_RB32(extradata);
1830 extradata += 4;
1831
1832 s->checksum_size = AV_RB32(extradata);
1833 if (s->checksum_size >= 1U << 28) {
1834 av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
1835 return AVERROR_INVALIDDATA;
1836 }
1837
1838 s->fft_order = av_log2(s->fft_size) + 1;
1839
1840 // something like max decodable tones
1841 s->group_order = av_log2(s->group_size) + 1;
1842 s->frame_size = s->group_size / 16; // 16 iterations per super block
1843 if (s->frame_size > QDM2_MAX_FRAME_SIZE)
1844 return AVERROR_INVALIDDATA;
1845
1846 s->sub_sampling = s->fft_order - 7;
1847 s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
1848
1849 switch ((s->sub_sampling * 2 + s->channels - 1)) {
1850 case 0: tmp = 40; break;
1851 case 1: tmp = 48; break;
1852 case 2: tmp = 56; break;
1853 case 3: tmp = 72; break;
1854 case 4: tmp = 80; break;
1855 case 5: tmp = 100;break;
1856 default: tmp=s->sub_sampling; break;
1857 }
1858 tmp_val = 0;
1859 if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
1860 if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
1861 if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
1862 if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
1863 s->cm_table_select = tmp_val;
1864
1865 if (s->sub_sampling == 0)
1866 tmp = 7999;
1867 else
1868 tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;
1869 /*
1870 0: 7999 -> 0
1871 1: 20000 -> 2
1872 2: 28000 -> 2
1873 */
1874 if (tmp < 8000)
1875 s->coeff_per_sb_select = 0;
1876 else if (tmp <= 16000)
1877 s->coeff_per_sb_select = 1;
1878 else
1879 s->coeff_per_sb_select = 2;
1880
1881 // Fail on unknown fft order
1882 if ((s->fft_order < 7) || (s->fft_order > 9)) {
1883 avpriv_request_sample(avctx, "Unknown FFT order %d", s->fft_order);
1884 return AVERROR_PATCHWELCOME;
1885 }
1886 if (s->fft_size != (1 << (s->fft_order - 1))) {
1887 av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
1888 return AVERROR_INVALIDDATA;
1889 }
1890
1891 ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);
1892 ff_mpadsp_init(&s->mpadsp);
1893
1894 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1895
1896 return 0;
1897 }
1898
1899 static av_cold int qdm2_decode_close(AVCodecContext *avctx)
1900 {
1901 QDM2Context *s = avctx->priv_data;
1902
1903 ff_rdft_end(&s->rdft_ctx);
1904
1905 return 0;
1906 }
1907
1908 static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
1909 {
1910 int ch, i;
1911 const int frame_size = (q->frame_size * q->channels);
1912
1913 /* select input buffer */
1914 q->compressed_data = in;
1915 q->compressed_size = q->checksum_size;
1916
1917 /* copy old block, clear new block of output samples */
1918 memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
1919 memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
1920
1921 /* decode block of QDM2 compressed data */
1922 if (q->sub_packet == 0) {
1923 q->has_errors = 0; // zero it for a new super block
1924 av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
1925 qdm2_decode_super_block(q);
1926 }
1927
1928 /* parse subpackets */
1929 if (!q->has_errors) {
1930 if (q->sub_packet == 2)
1931 qdm2_decode_fft_packets(q);
1932
1933 qdm2_fft_tone_synthesizer(q, q->sub_packet);
1934 }
1935
1936 /* sound synthesis stage 1 (FFT) */
1937 for (ch = 0; ch < q->channels; ch++) {
1938 qdm2_calculate_fft(q, ch, q->sub_packet);
1939
1940 if (!q->has_errors && q->sub_packet_list_C[0].packet) {
1941 SAMPLES_NEEDED_2("has errors, and C list is not empty")
1942 return -1;
1943 }
1944 }
1945
1946 /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1947 if (!q->has_errors && q->do_synth_filter)
1948 qdm2_synthesis_filter(q, q->sub_packet);
1949
1950 q->sub_packet = (q->sub_packet + 1) % 16;
1951
1952 /* clip and convert output float[] to 16-bit signed samples */
1953 for (i = 0; i < frame_size; i++) {
1954 int value = (int)q->output_buffer[i];
1955
1956 if (value > SOFTCLIP_THRESHOLD)
1957 value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
1958 else if (value < -SOFTCLIP_THRESHOLD)
1959 value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
1960
1961 out[i] = value;
1962 }
1963
1964 return 0;
1965 }
1966
1967 static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
1968 int *got_frame_ptr, AVPacket *avpkt)
1969 {
1970 AVFrame *frame = data;
1971 const uint8_t *buf = avpkt->data;
1972 int buf_size = avpkt->size;
1973 QDM2Context *s = avctx->priv_data;
1974 int16_t *out;
1975 int i, ret;
1976
1977 if(!buf)
1978 return 0;
1979 if(buf_size < s->checksum_size)
1980 return -1;
1981
1982 /* get output buffer */
1983 frame->nb_samples = 16 * s->frame_size;
1984 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1985 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1986 return ret;
1987 }
1988 out = (int16_t *)frame->data[0];
1989
1990 for (i = 0; i < 16; i++) {
1991 if ((ret = qdm2_decode(s, buf, out)) < 0)
1992 return ret;
1993 out += s->channels * s->frame_size;
1994 }
1995
1996 *got_frame_ptr = 1;
1997
1998 return s->checksum_size;
1999 }
2000
2001 AVCodec ff_qdm2_decoder = {
2002 .name = "qdm2",
2003 .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
2004 .type = AVMEDIA_TYPE_AUDIO,
2005 .id = AV_CODEC_ID_QDM2,
2006 .priv_data_size = sizeof(QDM2Context),
2007 .init = qdm2_decode_init,
2008 .init_static_data = qdm2_init_static_data,
2009 .close = qdm2_decode_close,
2010 .decode = qdm2_decode_frame,
2011 .capabilities = AV_CODEC_CAP_DR1,
2012 };