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
8 * This file is part of FFmpeg.
10 * FFmpeg 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.
15 * FFmpeg 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.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 * @author Ewald Snel, Benjamin Larsson, Alex Beregszaszi, Roberto Togni
30 * The decoder is not perfect yet, there are still some distortions
31 * especially on files encoded with 16 or 8 subbands.
38 #define BITSTREAM_READER_LE
39 #include "libavutil/audioconvert.h"
44 #include "mpegaudiodsp.h"
45 #include "mpegaudio.h"
48 #include "qdm2_tablegen.h"
54 #define QDM2_LIST_ADD(list, size, packet) \
57 list[size - 1].next = &list[size]; \
59 list[size].packet = packet; \
60 list[size].next = NULL; \
64 // Result is 8, 16 or 30
65 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
67 #define FIX_NOISE_IDX(noise_idx) \
68 if ((noise_idx) >= 3840) \
69 (noise_idx) -= 3840; \
71 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
73 #define SAMPLES_NEEDED \
74 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
76 #define SAMPLES_NEEDED_2(why) \
77 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
79 #define QDM2_MAX_FRAME_SIZE 512
81 typedef int8_t sb_int8_array[2][30][64];
87 int type; ///< subpacket type
88 unsigned int size; ///< subpacket size
89 const uint8_t *data; ///< pointer to subpacket data (points to input data buffer, it's not a private copy)
93 * A node in the subpacket list
95 typedef struct QDM2SubPNode {
96 QDM2SubPacket *packet; ///< packet
97 struct QDM2SubPNode *next; ///< pointer to next packet in the list, NULL if leaf node
107 QDM2Complex *complex;
125 DECLARE_ALIGNED(32, QDM2Complex, complex)[MPA_MAX_CHANNELS][256];
129 * QDM2 decoder context
134 /// Parameters from codec header, do not change during playback
135 int nb_channels; ///< number of channels
136 int channels; ///< number of channels
137 int group_size; ///< size of frame group (16 frames per group)
138 int fft_size; ///< size of FFT, in complex numbers
139 int checksum_size; ///< size of data block, used also for checksum
141 /// Parameters built from header parameters, do not change during playback
142 int group_order; ///< order of frame group
143 int fft_order; ///< order of FFT (actually fftorder+1)
144 int frame_size; ///< size of data frame
146 int sub_sampling; ///< subsampling: 0=25%, 1=50%, 2=100% */
147 int coeff_per_sb_select; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
148 int cm_table_select; ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
150 /// Packets and packet lists
151 QDM2SubPacket sub_packets[16]; ///< the packets themselves
152 QDM2SubPNode sub_packet_list_A[16]; ///< list of all packets
153 QDM2SubPNode sub_packet_list_B[16]; ///< FFT packets B are on list
154 int sub_packets_B; ///< number of packets on 'B' list
155 QDM2SubPNode sub_packet_list_C[16]; ///< packets with errors?
156 QDM2SubPNode sub_packet_list_D[16]; ///< DCT packets
159 FFTTone fft_tones[1000];
162 FFTCoefficient fft_coefs[1000];
164 int fft_coefs_min_index[5];
165 int fft_coefs_max_index[5];
166 int fft_level_exp[6];
167 RDFTContext rdft_ctx;
171 const uint8_t *compressed_data;
173 float output_buffer[QDM2_MAX_FRAME_SIZE * MPA_MAX_CHANNELS * 2];
176 MPADSPContext mpadsp;
177 DECLARE_ALIGNED(32, float, synth_buf)[MPA_MAX_CHANNELS][512*2];
178 int synth_buf_offset[MPA_MAX_CHANNELS];
179 DECLARE_ALIGNED(32, float, sb_samples)[MPA_MAX_CHANNELS][128][SBLIMIT];
180 DECLARE_ALIGNED(32, float, samples)[MPA_MAX_CHANNELS * MPA_FRAME_SIZE];
182 /// Mixed temporary data used in decoding
183 float tone_level[MPA_MAX_CHANNELS][30][64];
184 int8_t coding_method[MPA_MAX_CHANNELS][30][64];
185 int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8];
186 int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8];
187 int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8];
188 int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8];
189 int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26];
190 int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64];
191 int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64];
194 int has_errors; ///< packet has errors
195 int superblocktype_2_3; ///< select fft tables and some algorithm based on superblock type
196 int do_synth_filter; ///< used to perform or skip synthesis filter
199 int noise_idx; ///< index for dithering noise table
203 static VLC vlc_tab_level;
204 static VLC vlc_tab_diff;
205 static VLC vlc_tab_run;
206 static VLC fft_level_exp_alt_vlc;
207 static VLC fft_level_exp_vlc;
208 static VLC fft_stereo_exp_vlc;
209 static VLC fft_stereo_phase_vlc;
210 static VLC vlc_tab_tone_level_idx_hi1;
211 static VLC vlc_tab_tone_level_idx_mid;
212 static VLC vlc_tab_tone_level_idx_hi2;
213 static VLC vlc_tab_type30;
214 static VLC vlc_tab_type34;
215 static VLC vlc_tab_fft_tone_offset[5];
217 static const uint16_t qdm2_vlc_offs[] = {
218 0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
221 static av_cold void qdm2_init_vlc(void)
223 static int vlcs_initialized = 0;
224 static VLC_TYPE qdm2_table[3838][2];
226 if (!vlcs_initialized) {
228 vlc_tab_level.table = &qdm2_table[qdm2_vlc_offs[0]];
229 vlc_tab_level.table_allocated = qdm2_vlc_offs[1] - qdm2_vlc_offs[0];
230 init_vlc (&vlc_tab_level, 8, 24,
231 vlc_tab_level_huffbits, 1, 1,
232 vlc_tab_level_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
234 vlc_tab_diff.table = &qdm2_table[qdm2_vlc_offs[1]];
235 vlc_tab_diff.table_allocated = qdm2_vlc_offs[2] - qdm2_vlc_offs[1];
236 init_vlc (&vlc_tab_diff, 8, 37,
237 vlc_tab_diff_huffbits, 1, 1,
238 vlc_tab_diff_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
240 vlc_tab_run.table = &qdm2_table[qdm2_vlc_offs[2]];
241 vlc_tab_run.table_allocated = qdm2_vlc_offs[3] - qdm2_vlc_offs[2];
242 init_vlc (&vlc_tab_run, 5, 6,
243 vlc_tab_run_huffbits, 1, 1,
244 vlc_tab_run_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
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] - qdm2_vlc_offs[3];
248 init_vlc (&fft_level_exp_alt_vlc, 8, 28,
249 fft_level_exp_alt_huffbits, 1, 1,
250 fft_level_exp_alt_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
253 fft_level_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[4]];
254 fft_level_exp_vlc.table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
255 init_vlc (&fft_level_exp_vlc, 8, 20,
256 fft_level_exp_huffbits, 1, 1,
257 fft_level_exp_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
259 fft_stereo_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[5]];
260 fft_stereo_exp_vlc.table_allocated = qdm2_vlc_offs[6] - qdm2_vlc_offs[5];
261 init_vlc (&fft_stereo_exp_vlc, 6, 7,
262 fft_stereo_exp_huffbits, 1, 1,
263 fft_stereo_exp_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
265 fft_stereo_phase_vlc.table = &qdm2_table[qdm2_vlc_offs[6]];
266 fft_stereo_phase_vlc.table_allocated = qdm2_vlc_offs[7] - qdm2_vlc_offs[6];
267 init_vlc (&fft_stereo_phase_vlc, 6, 9,
268 fft_stereo_phase_huffbits, 1, 1,
269 fft_stereo_phase_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
271 vlc_tab_tone_level_idx_hi1.table = &qdm2_table[qdm2_vlc_offs[7]];
272 vlc_tab_tone_level_idx_hi1.table_allocated = qdm2_vlc_offs[8] - qdm2_vlc_offs[7];
273 init_vlc (&vlc_tab_tone_level_idx_hi1, 8, 20,
274 vlc_tab_tone_level_idx_hi1_huffbits, 1, 1,
275 vlc_tab_tone_level_idx_hi1_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
277 vlc_tab_tone_level_idx_mid.table = &qdm2_table[qdm2_vlc_offs[8]];
278 vlc_tab_tone_level_idx_mid.table_allocated = qdm2_vlc_offs[9] - qdm2_vlc_offs[8];
279 init_vlc (&vlc_tab_tone_level_idx_mid, 8, 24,
280 vlc_tab_tone_level_idx_mid_huffbits, 1, 1,
281 vlc_tab_tone_level_idx_mid_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
283 vlc_tab_tone_level_idx_hi2.table = &qdm2_table[qdm2_vlc_offs[9]];
284 vlc_tab_tone_level_idx_hi2.table_allocated = qdm2_vlc_offs[10] - qdm2_vlc_offs[9];
285 init_vlc (&vlc_tab_tone_level_idx_hi2, 8, 24,
286 vlc_tab_tone_level_idx_hi2_huffbits, 1, 1,
287 vlc_tab_tone_level_idx_hi2_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
289 vlc_tab_type30.table = &qdm2_table[qdm2_vlc_offs[10]];
290 vlc_tab_type30.table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
291 init_vlc (&vlc_tab_type30, 6, 9,
292 vlc_tab_type30_huffbits, 1, 1,
293 vlc_tab_type30_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
295 vlc_tab_type34.table = &qdm2_table[qdm2_vlc_offs[11]];
296 vlc_tab_type34.table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11];
297 init_vlc (&vlc_tab_type34, 5, 10,
298 vlc_tab_type34_huffbits, 1, 1,
299 vlc_tab_type34_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
301 vlc_tab_fft_tone_offset[0].table = &qdm2_table[qdm2_vlc_offs[12]];
302 vlc_tab_fft_tone_offset[0].table_allocated = qdm2_vlc_offs[13] - qdm2_vlc_offs[12];
303 init_vlc (&vlc_tab_fft_tone_offset[0], 8, 23,
304 vlc_tab_fft_tone_offset_0_huffbits, 1, 1,
305 vlc_tab_fft_tone_offset_0_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
307 vlc_tab_fft_tone_offset[1].table = &qdm2_table[qdm2_vlc_offs[13]];
308 vlc_tab_fft_tone_offset[1].table_allocated = qdm2_vlc_offs[14] - qdm2_vlc_offs[13];
309 init_vlc (&vlc_tab_fft_tone_offset[1], 8, 28,
310 vlc_tab_fft_tone_offset_1_huffbits, 1, 1,
311 vlc_tab_fft_tone_offset_1_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
313 vlc_tab_fft_tone_offset[2].table = &qdm2_table[qdm2_vlc_offs[14]];
314 vlc_tab_fft_tone_offset[2].table_allocated = qdm2_vlc_offs[15] - qdm2_vlc_offs[14];
315 init_vlc (&vlc_tab_fft_tone_offset[2], 8, 32,
316 vlc_tab_fft_tone_offset_2_huffbits, 1, 1,
317 vlc_tab_fft_tone_offset_2_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
319 vlc_tab_fft_tone_offset[3].table = &qdm2_table[qdm2_vlc_offs[15]];
320 vlc_tab_fft_tone_offset[3].table_allocated = qdm2_vlc_offs[16] - qdm2_vlc_offs[15];
321 init_vlc (&vlc_tab_fft_tone_offset[3], 8, 35,
322 vlc_tab_fft_tone_offset_3_huffbits, 1, 1,
323 vlc_tab_fft_tone_offset_3_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
325 vlc_tab_fft_tone_offset[4].table = &qdm2_table[qdm2_vlc_offs[16]];
326 vlc_tab_fft_tone_offset[4].table_allocated = qdm2_vlc_offs[17] - qdm2_vlc_offs[16];
327 init_vlc (&vlc_tab_fft_tone_offset[4], 8, 38,
328 vlc_tab_fft_tone_offset_4_huffbits, 1, 1,
329 vlc_tab_fft_tone_offset_4_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
335 static int qdm2_get_vlc (GetBitContext *gb, VLC *vlc, int flag, int depth)
339 value = get_vlc2(gb, vlc->table, vlc->bits, depth);
341 /* stage-2, 3 bits exponent escape sequence */
343 value = get_bits (gb, get_bits (gb, 3) + 1);
345 /* stage-3, optional */
350 av_log(0, AV_LOG_ERROR, "value %d in qdm2_get_vlc too large\n", value);
354 tmp= vlc_stage3_values[value];
356 if ((value & ~3) > 0)
357 tmp += get_bits (gb, (value >> 2));
365 static int qdm2_get_se_vlc (VLC *vlc, GetBitContext *gb, int depth)
367 int value = qdm2_get_vlc (gb, vlc, 0, depth);
369 return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
376 * @param data pointer to data to be checksum'ed
377 * @param length data length
378 * @param value checksum value
380 * @return 0 if checksum is OK
382 static uint16_t qdm2_packet_checksum (const uint8_t *data, int length, int value) {
385 for (i=0; i < length; i++)
388 return (uint16_t)(value & 0xffff);
393 * Fill a QDM2SubPacket structure with packet type, size, and data pointer.
395 * @param gb bitreader context
396 * @param sub_packet packet under analysis
398 static void qdm2_decode_sub_packet_header (GetBitContext *gb, QDM2SubPacket *sub_packet)
400 sub_packet->type = get_bits (gb, 8);
402 if (sub_packet->type == 0) {
403 sub_packet->size = 0;
404 sub_packet->data = NULL;
406 sub_packet->size = get_bits (gb, 8);
408 if (sub_packet->type & 0x80) {
409 sub_packet->size <<= 8;
410 sub_packet->size |= get_bits (gb, 8);
411 sub_packet->type &= 0x7f;
414 if (sub_packet->type == 0x7f)
415 sub_packet->type |= (get_bits (gb, 8) << 8);
417 sub_packet->data = &gb->buffer[get_bits_count(gb) / 8]; // FIXME: this depends on bitreader internal data
420 av_log(NULL,AV_LOG_DEBUG,"Subpacket: type=%d size=%d start_offs=%x\n",
421 sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);
426 * Return node pointer to first packet of requested type in list.
428 * @param list list of subpackets to be scanned
429 * @param type type of searched subpacket
430 * @return node pointer for subpacket if found, else NULL
432 static QDM2SubPNode* qdm2_search_subpacket_type_in_list (QDM2SubPNode *list, int type)
434 while (list != NULL && list->packet != NULL) {
435 if (list->packet->type == type)
444 * Replace 8 elements with their average value.
445 * Called by qdm2_decode_superblock before starting subblock decoding.
449 static void average_quantized_coeffs (QDM2Context *q)
451 int i, j, n, ch, sum;
453 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
455 for (ch = 0; ch < q->nb_channels; ch++)
456 for (i = 0; i < n; i++) {
459 for (j = 0; j < 8; j++)
460 sum += q->quantized_coeffs[ch][i][j];
466 for (j=0; j < 8; j++)
467 q->quantized_coeffs[ch][i][j] = sum;
473 * Build subband samples with noise weighted by q->tone_level.
474 * Called by synthfilt_build_sb_samples.
477 * @param sb subband index
479 static void build_sb_samples_from_noise (QDM2Context *q, int sb)
483 FIX_NOISE_IDX(q->noise_idx);
488 for (ch = 0; ch < q->nb_channels; ch++)
489 for (j = 0; j < 64; j++) {
490 q->sb_samples[ch][j * 2][sb] = SB_DITHERING_NOISE(sb,q->noise_idx) * q->tone_level[ch][sb][j];
491 q->sb_samples[ch][j * 2 + 1][sb] = SB_DITHERING_NOISE(sb,q->noise_idx) * q->tone_level[ch][sb][j];
497 * Called while processing data from subpackets 11 and 12.
498 * Used after making changes to coding_method array.
500 * @param sb subband index
501 * @param channels number of channels
502 * @param coding_method q->coding_method[0][0][0]
504 static void fix_coding_method_array (int sb, int channels, sb_int8_array coding_method)
509 static const int switchtable[23] = {0,5,1,5,5,5,5,5,2,5,5,5,5,5,5,5,3,5,5,5,5,5,4};
511 for (ch = 0; ch < channels; ch++) {
512 for (j = 0; j < 64; ) {
513 if((coding_method[ch][sb][j] - 8) > 22) {
517 switch (switchtable[coding_method[ch][sb][j]-8]) {
518 case 0: run = 10; case_val = 10; break;
519 case 1: run = 1; case_val = 16; break;
520 case 2: run = 5; case_val = 24; break;
521 case 3: run = 3; case_val = 30; break;
522 case 4: run = 1; case_val = 30; break;
523 case 5: run = 1; case_val = 8; break;
524 default: run = 1; case_val = 8; break;
527 for (k = 0; k < run; k++)
529 if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j])
532 //not debugged, almost never used
533 memset(&coding_method[ch][sb][j + k], case_val, k * sizeof(int8_t));
534 memset(&coding_method[ch][sb][j + k], case_val, 3 * sizeof(int8_t));
543 * Related to synthesis filter
544 * Called by process_subpacket_10
547 * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10
549 static void fill_tone_level_array (QDM2Context *q, int flag)
551 int i, sb, ch, sb_used;
554 for (ch = 0; ch < q->nb_channels; ch++)
555 for (sb = 0; sb < 30; sb++)
556 for (i = 0; i < 8; i++) {
557 if ((tab=coeff_per_sb_for_dequant[q->coeff_per_sb_select][sb]) < (last_coeff[q->coeff_per_sb_select] - 1))
558 tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
559 q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
561 tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
564 q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
567 sb_used = QDM2_SB_USED(q->sub_sampling);
569 if ((q->superblocktype_2_3 != 0) && !flag) {
570 for (sb = 0; sb < sb_used; sb++)
571 for (ch = 0; ch < q->nb_channels; ch++)
572 for (i = 0; i < 64; i++) {
573 q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
574 if (q->tone_level_idx[ch][sb][i] < 0)
575 q->tone_level[ch][sb][i] = 0;
577 q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
580 tab = q->superblocktype_2_3 ? 0 : 1;
581 for (sb = 0; sb < sb_used; sb++) {
582 if ((sb >= 4) && (sb <= 23)) {
583 for (ch = 0; ch < q->nb_channels; ch++)
584 for (i = 0; i < 64; i++) {
585 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
586 q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
587 q->tone_level_idx_mid[ch][sb - 4][i / 8] -
588 q->tone_level_idx_hi2[ch][sb - 4];
589 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
590 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
591 q->tone_level[ch][sb][i] = 0;
593 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
597 for (ch = 0; ch < q->nb_channels; ch++)
598 for (i = 0; i < 64; i++) {
599 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
600 q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
601 q->tone_level_idx_hi2[ch][sb - 4];
602 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
603 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
604 q->tone_level[ch][sb][i] = 0;
606 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
609 for (ch = 0; ch < q->nb_channels; ch++)
610 for (i = 0; i < 64; i++) {
611 tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
612 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
613 q->tone_level[ch][sb][i] = 0;
615 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
627 * Related to synthesis filter
628 * Called by process_subpacket_11
629 * c is built with data from subpacket 11
630 * Most of this function is used only if superblock_type_2_3 == 0, never seen it in samples
632 * @param tone_level_idx
633 * @param tone_level_idx_temp
634 * @param coding_method q->coding_method[0][0][0]
635 * @param nb_channels number of channels
636 * @param c coming from subpacket 11, passed as 8*c
637 * @param superblocktype_2_3 flag based on superblock packet type
638 * @param cm_table_select q->cm_table_select
640 static void fill_coding_method_array (sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp,
641 sb_int8_array coding_method, int nb_channels,
642 int c, int superblocktype_2_3, int cm_table_select)
645 int tmp, acc, esp_40, comp;
646 int add1, add2, add3, add4;
649 if (!superblocktype_2_3) {
650 /* This case is untested, no samples available */
652 for (ch = 0; ch < nb_channels; ch++)
653 for (sb = 0; sb < 30; sb++) {
654 for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
655 add1 = tone_level_idx[ch][sb][j] - 10;
658 add2 = add3 = add4 = 0;
660 add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
665 add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
670 add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
674 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
677 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
679 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
682 for (ch = 0; ch < nb_channels; ch++)
683 for (sb = 0; sb < 30; sb++)
684 for (j = 0; j < 64; j++)
685 acc += tone_level_idx_temp[ch][sb][j];
687 multres = 0x66666667 * (acc * 10);
688 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
689 for (ch = 0; ch < nb_channels; ch++)
690 for (sb = 0; sb < 30; sb++)
691 for (j = 0; j < 64; j++) {
692 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
695 comp /= 256; // signed shift
723 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
725 for (sb = 0; sb < 30; sb++)
726 fix_coding_method_array(sb, nb_channels, coding_method);
727 for (ch = 0; ch < nb_channels; ch++)
728 for (sb = 0; sb < 30; sb++)
729 for (j = 0; j < 64; j++)
731 if (coding_method[ch][sb][j] < 10)
732 coding_method[ch][sb][j] = 10;
735 if (coding_method[ch][sb][j] < 16)
736 coding_method[ch][sb][j] = 16;
738 if (coding_method[ch][sb][j] < 30)
739 coding_method[ch][sb][j] = 30;
742 } else { // superblocktype_2_3 != 0
743 for (ch = 0; ch < nb_channels; ch++)
744 for (sb = 0; sb < 30; sb++)
745 for (j = 0; j < 64; j++)
746 coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
755 * Called by process_subpacket_11 to process more data from subpacket 11 with sb 0-8
756 * Called by process_subpacket_12 to process data from subpacket 12 with sb 8-sb_used
759 * @param gb bitreader context
760 * @param length packet length in bits
761 * @param sb_min lower subband processed (sb_min included)
762 * @param sb_max higher subband processed (sb_max excluded)
764 static int synthfilt_build_sb_samples (QDM2Context *q, GetBitContext *gb, int length, int sb_min, int sb_max)
766 int sb, j, k, n, ch, run, channels;
767 int joined_stereo, zero_encoding, chs;
769 float type34_div = 0;
770 float type34_predictor;
771 float samples[10], sign_bits[16];
774 // If no data use noise
775 for (sb=sb_min; sb < sb_max; sb++)
776 build_sb_samples_from_noise (q, sb);
781 for (sb = sb_min; sb < sb_max; sb++) {
782 FIX_NOISE_IDX(q->noise_idx);
784 channels = q->nb_channels;
786 if (q->nb_channels <= 1 || sb < 12)
791 joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1 (gb) : 0;
794 if (get_bits_left(gb) >= 16)
795 for (j = 0; j < 16; j++)
796 sign_bits[j] = get_bits1 (gb);
798 for (j = 0; j < 64; j++)
799 if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
800 q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
802 fix_coding_method_array(sb, q->nb_channels, q->coding_method);
806 for (ch = 0; ch < channels; ch++) {
807 zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
808 type34_predictor = 0.0;
811 for (j = 0; j < 128; ) {
812 switch (q->coding_method[ch][sb][j / 2]) {
814 if (get_bits_left(gb) >= 10) {
816 for (k = 0; k < 5; k++) {
817 if ((j + 2 * k) >= 128)
819 samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
823 for (k = 0; k < 5; k++)
824 samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
826 for (k = 0; k < 5; k++)
827 samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
829 for (k = 0; k < 10; k++)
830 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
836 if (get_bits_left(gb) >= 1) {
841 f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
844 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
850 if (get_bits_left(gb) >= 10) {
852 for (k = 0; k < 5; k++) {
855 samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
858 n = get_bits (gb, 8);
859 for (k = 0; k < 5; k++)
860 samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
863 for (k = 0; k < 5; k++)
864 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
870 if (get_bits_left(gb) >= 7) {
872 for (k = 0; k < 3; k++)
873 samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
875 for (k = 0; k < 3; k++)
876 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
882 if (get_bits_left(gb) >= 4) {
883 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
884 if (index >= FF_ARRAY_ELEMS(type30_dequant)) {
885 av_log(NULL, AV_LOG_ERROR, "index %d out of type30_dequant array\n", index);
886 return AVERROR_INVALIDDATA;
888 samples[0] = type30_dequant[index];
890 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
896 if (get_bits_left(gb) >= 7) {
898 type34_div = (float)(1 << get_bits(gb, 2));
899 samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
900 type34_predictor = samples[0];
903 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
904 if (index >= FF_ARRAY_ELEMS(type34_delta)) {
905 av_log(NULL, AV_LOG_ERROR, "index %d out of type34_delta array\n", index);
906 return AVERROR_INVALIDDATA;
908 samples[0] = type34_delta[index] / type34_div + type34_predictor;
909 type34_predictor = samples[0];
912 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
918 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
924 float tmp[10][MPA_MAX_CHANNELS];
926 for (k = 0; k < run; k++) {
927 tmp[k][0] = samples[k];
928 tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
930 for (chs = 0; chs < q->nb_channels; chs++)
931 for (k = 0; k < run; k++)
933 q->sb_samples[chs][j + k][sb] = q->tone_level[chs][sb][((j + k)/2)] * tmp[k][chs];
935 for (k = 0; k < run; k++)
937 q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
949 * Init the first element of a channel in quantized_coeffs with data from packet 10 (quantized_coeffs[ch][0]).
950 * This is similar to process_subpacket_9, but for a single channel and for element [0]
951 * same VLC tables as process_subpacket_9 are used.
953 * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
954 * @param gb bitreader context
956 static int init_quantized_coeffs_elem0 (int8_t *quantized_coeffs, GetBitContext *gb)
958 int i, k, run, level, diff;
960 if (get_bits_left(gb) < 16)
962 level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
964 quantized_coeffs[0] = level;
966 for (i = 0; i < 7; ) {
967 if (get_bits_left(gb) < 16)
969 run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
974 if (get_bits_left(gb) < 16)
976 diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
978 for (k = 1; k <= run; k++)
979 quantized_coeffs[i + k] = (level + ((k * diff) / run));
989 * Related to synthesis filter, process data from packet 10
990 * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
991 * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with data from packet 10
994 * @param gb bitreader context
996 static void init_tone_level_dequantization (QDM2Context *q, GetBitContext *gb)
1000 for (ch = 0; ch < q->nb_channels; ch++) {
1001 init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], gb);
1003 if (get_bits_left(gb) < 16) {
1004 memset(q->quantized_coeffs[ch][0], 0, 8);
1009 n = q->sub_sampling + 1;
1011 for (sb = 0; sb < n; sb++)
1012 for (ch = 0; ch < q->nb_channels; ch++)
1013 for (j = 0; j < 8; j++) {
1014 if (get_bits_left(gb) < 1)
1016 if (get_bits1(gb)) {
1017 for (k=0; k < 8; k++) {
1018 if (get_bits_left(gb) < 16)
1020 q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
1023 for (k=0; k < 8; k++)
1024 q->tone_level_idx_hi1[ch][sb][j][k] = 0;
1028 n = QDM2_SB_USED(q->sub_sampling) - 4;
1030 for (sb = 0; sb < n; sb++)
1031 for (ch = 0; ch < q->nb_channels; ch++) {
1032 if (get_bits_left(gb) < 16)
1034 q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2);
1036 q->tone_level_idx_hi2[ch][sb] -= 16;
1038 for (j = 0; j < 8; j++)
1039 q->tone_level_idx_mid[ch][sb][j] = -16;
1042 n = QDM2_SB_USED(q->sub_sampling) - 5;
1044 for (sb = 0; sb < n; sb++)
1045 for (ch = 0; ch < q->nb_channels; ch++)
1046 for (j = 0; j < 8; j++) {
1047 if (get_bits_left(gb) < 16)
1049 q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
1054 * Process subpacket 9, init quantized_coeffs with data from it
1057 * @param node pointer to node with packet
1059 static int process_subpacket_9 (QDM2Context *q, QDM2SubPNode *node)
1062 int i, j, k, n, ch, run, level, diff;
1064 init_get_bits(&gb, node->packet->data, node->packet->size*8);
1066 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1; // same as averagesomething function
1068 for (i = 1; i < n; i++)
1069 for (ch=0; ch < q->nb_channels; ch++) {
1070 level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
1071 q->quantized_coeffs[ch][i][0] = level;
1073 for (j = 0; j < (8 - 1); ) {
1074 run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
1075 diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
1080 for (k = 1; k <= run; k++)
1081 q->quantized_coeffs[ch][i][j + k] = (level + ((k*diff) / run));
1088 for (ch = 0; ch < q->nb_channels; ch++)
1089 for (i = 0; i < 8; i++)
1090 q->quantized_coeffs[ch][0][i] = 0;
1097 * Process subpacket 10 if not null, else
1100 * @param node pointer to node with packet
1102 static void process_subpacket_10 (QDM2Context *q, QDM2SubPNode *node)
1107 init_get_bits(&gb, node->packet->data, node->packet->size * 8);
1108 init_tone_level_dequantization(q, &gb);
1109 fill_tone_level_array(q, 1);
1111 fill_tone_level_array(q, 0);
1117 * Process subpacket 11
1120 * @param node pointer to node with packet
1122 static void process_subpacket_11 (QDM2Context *q, QDM2SubPNode *node)
1128 length = node->packet->size * 8;
1129 init_get_bits(&gb, node->packet->data, length);
1133 int c = get_bits (&gb, 13);
1136 fill_coding_method_array (q->tone_level_idx, q->tone_level_idx_temp, q->coding_method,
1137 q->nb_channels, 8*c, q->superblocktype_2_3, q->cm_table_select);
1140 synthfilt_build_sb_samples(q, &gb, length, 0, 8);
1145 * Process subpacket 12
1148 * @param node pointer to node with packet
1150 static void process_subpacket_12 (QDM2Context *q, QDM2SubPNode *node)
1156 length = node->packet->size * 8;
1157 init_get_bits(&gb, node->packet->data, length);
1160 synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
1164 * Process new subpackets for synthesis filter
1167 * @param list list with synthesis filter packets (list D)
1169 static void process_synthesis_subpackets (QDM2Context *q, QDM2SubPNode *list)
1171 QDM2SubPNode *nodes[4];
1173 nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
1174 if (nodes[0] != NULL)
1175 process_subpacket_9(q, nodes[0]);
1177 nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
1178 if (nodes[1] != NULL)
1179 process_subpacket_10(q, nodes[1]);
1181 process_subpacket_10(q, NULL);
1183 nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
1184 if (nodes[0] != NULL && nodes[1] != NULL && nodes[2] != NULL)
1185 process_subpacket_11(q, nodes[2]);
1187 process_subpacket_11(q, NULL);
1189 nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
1190 if (nodes[0] != NULL && nodes[1] != NULL && nodes[3] != NULL)
1191 process_subpacket_12(q, nodes[3]);
1193 process_subpacket_12(q, NULL);
1198 * Decode superblock, fill packet lists.
1202 static void qdm2_decode_super_block (QDM2Context *q)
1205 QDM2SubPacket header, *packet;
1206 int i, packet_bytes, sub_packet_size, sub_packets_D;
1207 unsigned int next_index = 0;
1209 memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
1210 memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
1211 memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
1213 q->sub_packets_B = 0;
1216 average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
1218 init_get_bits(&gb, q->compressed_data, q->compressed_size*8);
1219 qdm2_decode_sub_packet_header(&gb, &header);
1221 if (header.type < 2 || header.type >= 8) {
1223 av_log(NULL,AV_LOG_ERROR,"bad superblock type\n");
1227 q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
1228 packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
1230 init_get_bits(&gb, header.data, header.size*8);
1232 if (header.type == 2 || header.type == 4 || header.type == 5) {
1233 int csum = 257 * get_bits(&gb, 8);
1234 csum += 2 * get_bits(&gb, 8);
1236 csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
1240 av_log(NULL,AV_LOG_ERROR,"bad packet checksum\n");
1245 q->sub_packet_list_B[0].packet = NULL;
1246 q->sub_packet_list_D[0].packet = NULL;
1248 for (i = 0; i < 6; i++)
1249 if (--q->fft_level_exp[i] < 0)
1250 q->fft_level_exp[i] = 0;
1252 for (i = 0; packet_bytes > 0; i++) {
1255 q->sub_packet_list_A[i].next = NULL;
1258 q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
1260 /* seek to next block */
1261 init_get_bits(&gb, header.data, header.size*8);
1262 skip_bits(&gb, next_index*8);
1264 if (next_index >= header.size)
1268 /* decode subpacket */
1269 packet = &q->sub_packets[i];
1270 qdm2_decode_sub_packet_header(&gb, packet);
1271 next_index = packet->size + get_bits_count(&gb) / 8;
1272 sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
1274 if (packet->type == 0)
1277 if (sub_packet_size > packet_bytes) {
1278 if (packet->type != 10 && packet->type != 11 && packet->type != 12)
1280 packet->size += packet_bytes - sub_packet_size;
1283 packet_bytes -= sub_packet_size;
1285 /* add subpacket to 'all subpackets' list */
1286 q->sub_packet_list_A[i].packet = packet;
1288 /* add subpacket to related list */
1289 if (packet->type == 8) {
1290 SAMPLES_NEEDED_2("packet type 8");
1292 } else if (packet->type >= 9 && packet->type <= 12) {
1293 /* packets for MPEG Audio like Synthesis Filter */
1294 QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
1295 } else if (packet->type == 13) {
1296 for (j = 0; j < 6; j++)
1297 q->fft_level_exp[j] = get_bits(&gb, 6);
1298 } else if (packet->type == 14) {
1299 for (j = 0; j < 6; j++)
1300 q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
1301 } else if (packet->type == 15) {
1302 SAMPLES_NEEDED_2("packet type 15")
1304 } else if (packet->type >= 16 && packet->type < 48 && !fft_subpackets[packet->type - 16]) {
1305 /* packets for FFT */
1306 QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);
1308 } // Packet bytes loop
1310 /* **************************************************************** */
1311 if (q->sub_packet_list_D[0].packet != NULL) {
1312 process_synthesis_subpackets(q, q->sub_packet_list_D);
1313 q->do_synth_filter = 1;
1314 } else if (q->do_synth_filter) {
1315 process_subpacket_10(q, NULL);
1316 process_subpacket_11(q, NULL);
1317 process_subpacket_12(q, NULL);
1319 /* **************************************************************** */
1323 static void qdm2_fft_init_coefficient (QDM2Context *q, int sub_packet,
1324 int offset, int duration, int channel,
1327 if (q->fft_coefs_min_index[duration] < 0)
1328 q->fft_coefs_min_index[duration] = q->fft_coefs_index;
1330 q->fft_coefs[q->fft_coefs_index].sub_packet = ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1331 q->fft_coefs[q->fft_coefs_index].channel = channel;
1332 q->fft_coefs[q->fft_coefs_index].offset = offset;
1333 q->fft_coefs[q->fft_coefs_index].exp = exp;
1334 q->fft_coefs[q->fft_coefs_index].phase = phase;
1335 q->fft_coefs_index++;
1339 static void qdm2_fft_decode_tones (QDM2Context *q, int duration, GetBitContext *gb, int b)
1341 int channel, stereo, phase, exp;
1342 int local_int_4, local_int_8, stereo_phase, local_int_10;
1343 int local_int_14, stereo_exp, local_int_20, local_int_28;
1349 local_int_8 = (4 - duration);
1350 local_int_10 = 1 << (q->group_order - duration - 1);
1353 while (get_bits_left(gb)>0) {
1354 if (q->superblocktype_2_3) {
1355 while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1356 if (get_bits_left(gb)<0) {
1357 if(local_int_4 < q->group_size)
1358 av_log(0, AV_LOG_ERROR, "overread in qdm2_fft_decode_tones()\n");
1363 local_int_4 += local_int_10;
1364 local_int_28 += (1 << local_int_8);
1366 local_int_4 += 8*local_int_10;
1367 local_int_28 += (8 << local_int_8);
1372 offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1373 while (offset >= (local_int_10 - 1)) {
1374 offset += (1 - (local_int_10 - 1));
1375 local_int_4 += local_int_10;
1376 local_int_28 += (1 << local_int_8);
1380 if (local_int_4 >= q->group_size)
1383 local_int_14 = (offset >> local_int_8);
1384 if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
1387 if (q->nb_channels > 1) {
1388 channel = get_bits1(gb);
1389 stereo = get_bits1(gb);
1395 exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1396 exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
1397 exp = (exp < 0) ? 0 : exp;
1399 phase = get_bits(gb, 3);
1404 stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1405 stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1406 if (stereo_phase < 0)
1410 if (q->frequency_range > (local_int_14 + 1)) {
1411 int sub_packet = (local_int_20 + local_int_28);
1413 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, channel, exp, phase);
1415 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, (1 - channel), stereo_exp, stereo_phase);
1423 static void qdm2_decode_fft_packets (QDM2Context *q)
1425 int i, j, min, max, value, type, unknown_flag;
1428 if (q->sub_packet_list_B[0].packet == NULL)
1431 /* reset minimum indexes for FFT coefficients */
1432 q->fft_coefs_index = 0;
1433 for (i=0; i < 5; i++)
1434 q->fft_coefs_min_index[i] = -1;
1436 /* process subpackets ordered by type, largest type first */
1437 for (i = 0, max = 256; i < q->sub_packets_B; i++) {
1438 QDM2SubPacket *packet= NULL;
1440 /* find subpacket with largest type less than max */
1441 for (j = 0, min = 0; j < q->sub_packets_B; j++) {
1442 value = q->sub_packet_list_B[j].packet->type;
1443 if (value > min && value < max) {
1445 packet = q->sub_packet_list_B[j].packet;
1451 /* check for errors (?) */
1455 if (i == 0 && (packet->type < 16 || packet->type >= 48 || fft_subpackets[packet->type - 16]))
1458 /* decode FFT tones */
1459 init_get_bits (&gb, packet->data, packet->size*8);
1461 if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
1466 type = packet->type;
1468 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1469 int duration = q->sub_sampling + 5 - (type & 15);
1471 if (duration >= 0 && duration < 4)
1472 qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
1473 } else if (type == 31) {
1474 for (j=0; j < 4; j++)
1475 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1476 } else if (type == 46) {
1477 for (j=0; j < 6; j++)
1478 q->fft_level_exp[j] = get_bits(&gb, 6);
1479 for (j=0; j < 4; j++)
1480 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1482 } // Loop on B packets
1484 /* calculate maximum indexes for FFT coefficients */
1485 for (i = 0, j = -1; i < 5; i++)
1486 if (q->fft_coefs_min_index[i] >= 0) {
1488 q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i];
1492 q->fft_coefs_max_index[j] = q->fft_coefs_index;
1496 static void qdm2_fft_generate_tone (QDM2Context *q, FFTTone *tone)
1501 const double iscale = 2.0*M_PI / 512.0;
1503 tone->phase += tone->phase_shift;
1505 /* calculate current level (maximum amplitude) of tone */
1506 level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
1507 c.im = level * sin(tone->phase*iscale);
1508 c.re = level * cos(tone->phase*iscale);
1510 /* generate FFT coefficients for tone */
1511 if (tone->duration >= 3 || tone->cutoff >= 3) {
1512 tone->complex[0].im += c.im;
1513 tone->complex[0].re += c.re;
1514 tone->complex[1].im -= c.im;
1515 tone->complex[1].re -= c.re;
1517 f[1] = -tone->table[4];
1518 f[0] = tone->table[3] - tone->table[0];
1519 f[2] = 1.0 - tone->table[2] - tone->table[3];
1520 f[3] = tone->table[1] + tone->table[4] - 1.0;
1521 f[4] = tone->table[0] - tone->table[1];
1522 f[5] = tone->table[2];
1523 for (i = 0; i < 2; i++) {
1524 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re += c.re * f[i];
1525 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im += c.im *((tone->cutoff <= i) ? -f[i] : f[i]);
1527 for (i = 0; i < 4; i++) {
1528 tone->complex[i].re += c.re * f[i+2];
1529 tone->complex[i].im += c.im * f[i+2];
1533 /* copy the tone if it has not yet died out */
1534 if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
1535 memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
1536 q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
1541 static void qdm2_fft_tone_synthesizer (QDM2Context *q, int sub_packet)
1544 const double iscale = 0.25 * M_PI;
1546 for (ch = 0; ch < q->channels; ch++) {
1547 memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
1551 /* apply FFT tones with duration 4 (1 FFT period) */
1552 if (q->fft_coefs_min_index[4] >= 0)
1553 for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
1557 if (q->fft_coefs[i].sub_packet != sub_packet)
1560 ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
1561 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];
1563 c.re = level * cos(q->fft_coefs[i].phase * iscale);
1564 c.im = level * sin(q->fft_coefs[i].phase * iscale);
1565 q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
1566 q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
1567 q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
1568 q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
1571 /* generate existing FFT tones */
1572 for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
1573 qdm2_fft_generate_tone(q, &q->fft_tones[q->fft_tone_start]);
1574 q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
1577 /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1578 for (i = 0; i < 4; i++)
1579 if (q->fft_coefs_min_index[i] >= 0) {
1580 for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
1584 if (q->fft_coefs[j].sub_packet != sub_packet)
1588 offset = q->fft_coefs[j].offset >> four_i;
1589 ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
1591 if (offset < q->frequency_range) {
1593 tone.cutoff = offset;
1595 tone.cutoff = (offset >= 60) ? 3 : 2;
1597 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];
1598 tone.complex = &q->fft.complex[ch][offset];
1599 tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
1600 tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
1601 tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
1603 tone.time_index = 0;
1605 qdm2_fft_generate_tone(q, &tone);
1608 q->fft_coefs_min_index[i] = j;
1613 static void qdm2_calculate_fft (QDM2Context *q, int channel, int sub_packet)
1615 const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
1616 float *out = q->output_buffer + channel;
1618 q->fft.complex[channel][0].re *= 2.0f;
1619 q->fft.complex[channel][0].im = 0.0f;
1620 q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
1621 /* add samples to output buffer */
1622 for (i = 0; i < FFALIGN(q->fft_size, 8); i++) {
1623 out[0] += q->fft.complex[channel][i].re * gain;
1624 out[q->channels] += q->fft.complex[channel][i].im * gain;
1625 out += 2 * q->channels;
1632 * @param index subpacket number
1634 static void qdm2_synthesis_filter (QDM2Context *q, int index)
1636 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1638 /* copy sb_samples */
1639 sb_used = QDM2_SB_USED(q->sub_sampling);
1641 for (ch = 0; ch < q->channels; ch++)
1642 for (i = 0; i < 8; i++)
1643 for (k=sb_used; k < SBLIMIT; k++)
1644 q->sb_samples[ch][(8 * index) + i][k] = 0;
1646 for (ch = 0; ch < q->nb_channels; ch++) {
1647 float *samples_ptr = q->samples + ch;
1649 for (i = 0; i < 8; i++) {
1650 ff_mpa_synth_filter_float(&q->mpadsp,
1651 q->synth_buf[ch], &(q->synth_buf_offset[ch]),
1652 ff_mpa_synth_window_float, &dither_state,
1653 samples_ptr, q->nb_channels,
1654 q->sb_samples[ch][(8 * index) + i]);
1655 samples_ptr += 32 * q->nb_channels;
1659 /* add samples to output buffer */
1660 sub_sampling = (4 >> q->sub_sampling);
1662 for (ch = 0; ch < q->channels; ch++)
1663 for (i = 0; i < q->frame_size; i++)
1664 q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
1669 * Init static data (does not depend on specific file)
1673 static av_cold void qdm2_init(QDM2Context *q) {
1674 static int initialized = 0;
1676 if (initialized != 0)
1681 ff_mpa_synth_init_float(ff_mpa_synth_window_float);
1682 softclip_table_init();
1684 init_noise_samples();
1686 av_log(NULL, AV_LOG_DEBUG, "init done\n");
1691 * Init parameters from codec extradata
1693 static av_cold int qdm2_decode_init(AVCodecContext *avctx)
1695 QDM2Context *s = avctx->priv_data;
1698 int tmp_val, tmp, size;
1700 /* extradata parsing
1709 32 size (including this field)
1711 32 type (=QDM2 or QDMC)
1713 32 size (including this field, in bytes)
1714 32 tag (=QDCA) // maybe mandatory parameters
1717 32 samplerate (=44100)
1719 32 block size (=4096)
1720 32 frame size (=256) (for one channel)
1721 32 packet size (=1300)
1723 32 size (including this field, in bytes)
1724 32 tag (=QDCP) // maybe some tuneable parameters
1734 if (!avctx->extradata || (avctx->extradata_size < 48)) {
1735 av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
1739 extradata = avctx->extradata;
1740 extradata_size = avctx->extradata_size;
1742 while (extradata_size > 7) {
1743 if (!memcmp(extradata, "frmaQDM", 7))
1749 if (extradata_size < 12) {
1750 av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
1755 if (memcmp(extradata, "frmaQDM", 7)) {
1756 av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
1760 if (extradata[7] == 'C') {
1762 av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n");
1767 extradata_size -= 8;
1769 size = AV_RB32(extradata);
1771 if(size > extradata_size){
1772 av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
1773 extradata_size, size);
1778 av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
1779 if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
1780 av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
1786 avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
1788 if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS) {
1789 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
1790 return AVERROR_INVALIDDATA;
1792 avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
1795 avctx->sample_rate = AV_RB32(extradata);
1798 avctx->bit_rate = AV_RB32(extradata);
1801 s->group_size = AV_RB32(extradata);
1804 s->fft_size = AV_RB32(extradata);
1807 s->checksum_size = AV_RB32(extradata);
1808 if (s->checksum_size >= 1U << 28) {
1809 av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
1810 return AVERROR_INVALIDDATA;
1813 s->fft_order = av_log2(s->fft_size) + 1;
1815 // something like max decodable tones
1816 s->group_order = av_log2(s->group_size) + 1;
1817 s->frame_size = s->group_size / 16; // 16 iterations per super block
1819 if (s->frame_size > QDM2_MAX_FRAME_SIZE)
1820 return AVERROR_INVALIDDATA;
1822 s->sub_sampling = s->fft_order - 7;
1823 s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
1825 switch ((s->sub_sampling * 2 + s->channels - 1)) {
1826 case 0: tmp = 40; break;
1827 case 1: tmp = 48; break;
1828 case 2: tmp = 56; break;
1829 case 3: tmp = 72; break;
1830 case 4: tmp = 80; break;
1831 case 5: tmp = 100;break;
1832 default: tmp=s->sub_sampling; break;
1835 if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
1836 if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
1837 if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
1838 if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
1839 s->cm_table_select = tmp_val;
1841 if (s->sub_sampling == 0)
1844 tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;
1851 s->coeff_per_sb_select = 0;
1852 else if (tmp <= 16000)
1853 s->coeff_per_sb_select = 1;
1855 s->coeff_per_sb_select = 2;
1857 // Fail on unknown fft order
1858 if ((s->fft_order < 7) || (s->fft_order > 9)) {
1859 av_log(avctx, AV_LOG_ERROR, "Unknown FFT order (%d), contact the developers!\n", s->fft_order);
1863 ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);
1864 ff_mpadsp_init(&s->mpadsp);
1868 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1870 avcodec_get_frame_defaults(&s->frame);
1871 avctx->coded_frame = &s->frame;
1877 static av_cold int qdm2_decode_close(AVCodecContext *avctx)
1879 QDM2Context *s = avctx->priv_data;
1881 ff_rdft_end(&s->rdft_ctx);
1887 static int qdm2_decode (QDM2Context *q, const uint8_t *in, int16_t *out)
1890 const int frame_size = (q->frame_size * q->channels);
1892 if((unsigned)frame_size > FF_ARRAY_ELEMS(q->output_buffer)/2)
1895 /* select input buffer */
1896 q->compressed_data = in;
1897 q->compressed_size = q->checksum_size;
1899 /* copy old block, clear new block of output samples */
1900 memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
1901 memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
1903 /* decode block of QDM2 compressed data */
1904 if (q->sub_packet == 0) {
1905 q->has_errors = 0; // zero it for a new super block
1906 av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
1907 qdm2_decode_super_block(q);
1910 /* parse subpackets */
1911 if (!q->has_errors) {
1912 if (q->sub_packet == 2)
1913 qdm2_decode_fft_packets(q);
1915 qdm2_fft_tone_synthesizer(q, q->sub_packet);
1918 /* sound synthesis stage 1 (FFT) */
1919 for (ch = 0; ch < q->channels; ch++) {
1920 qdm2_calculate_fft(q, ch, q->sub_packet);
1922 if (!q->has_errors && q->sub_packet_list_C[0].packet != NULL) {
1923 SAMPLES_NEEDED_2("has errors, and C list is not empty")
1928 /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1929 if (!q->has_errors && q->do_synth_filter)
1930 qdm2_synthesis_filter(q, q->sub_packet);
1932 q->sub_packet = (q->sub_packet + 1) % 16;
1934 /* clip and convert output float[] to 16bit signed samples */
1935 for (i = 0; i < frame_size; i++) {
1936 int value = (int)q->output_buffer[i];
1938 if (value > SOFTCLIP_THRESHOLD)
1939 value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
1940 else if (value < -SOFTCLIP_THRESHOLD)
1941 value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
1950 static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
1951 int *got_frame_ptr, AVPacket *avpkt)
1953 const uint8_t *buf = avpkt->data;
1954 int buf_size = avpkt->size;
1955 QDM2Context *s = avctx->priv_data;
1961 if(buf_size < s->checksum_size)
1964 /* get output buffer */
1965 s->frame.nb_samples = 16 * s->frame_size;
1966 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
1967 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1970 out = (int16_t *)s->frame.data[0];
1972 for (i = 0; i < 16; i++) {
1973 if (qdm2_decode(s, buf, out) < 0)
1975 out += s->channels * s->frame_size;
1979 *(AVFrame *)data = s->frame;
1981 return s->checksum_size;
1984 AVCodec ff_qdm2_decoder =
1987 .type = AVMEDIA_TYPE_AUDIO,
1988 .id = AV_CODEC_ID_QDM2,
1989 .priv_data_size = sizeof(QDM2Context),
1990 .init = qdm2_decode_init,
1991 .close = qdm2_decode_close,
1992 .decode = qdm2_decode_frame,
1993 .capabilities = CODEC_CAP_DR1,
1994 .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),