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
29 * The decoder is not perfect yet, there are still some distortions
30 * especially on files encoded with 16 or 8 subbands.
37 #define ALT_BITSTREAM_READER_LE
42 #include "mpegaudio.h"
45 #include "qdm2_tablegen.h"
51 #define QDM2_LIST_ADD(list, size, packet) \
54 list[size - 1].next = &list[size]; \
56 list[size].packet = packet; \
57 list[size].next = NULL; \
61 // Result is 8, 16 or 30
62 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
64 #define FIX_NOISE_IDX(noise_idx) \
65 if ((noise_idx) >= 3840) \
66 (noise_idx) -= 3840; \
68 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
70 #define BITS_LEFT(length,gb) ((length) - get_bits_count ((gb)))
72 #define SAMPLES_NEEDED \
73 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
75 #define SAMPLES_NEEDED_2(why) \
76 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
79 typedef int8_t sb_int8_array[2][30][64];
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)
91 * A node in the subpacket list
93 typedef struct QDM2SubPNode {
94 QDM2SubPacket *packet; ///< packet
95 struct QDM2SubPNode *next; ///< pointer to next packet in the list, NULL if leaf node
105 QDM2Complex *complex;
123 DECLARE_ALIGNED(16, QDM2Complex, complex)[MPA_MAX_CHANNELS][256];
127 * QDM2 decoder context
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
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 fft_frame_size; ///< size of fft frame, in components (1 comples = re + im)
141 int frame_size; ///< size of data frame
143 int sub_sampling; ///< subsampling: 0=25%, 1=50%, 2=100% */
144 int coeff_per_sb_select; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
145 int cm_table_select; ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
147 /// Packets and packet lists
148 QDM2SubPacket sub_packets[16]; ///< the packets themselves
149 QDM2SubPNode sub_packet_list_A[16]; ///< list of all packets
150 QDM2SubPNode sub_packet_list_B[16]; ///< FFT packets B are on list
151 int sub_packets_B; ///< number of packets on 'B' list
152 QDM2SubPNode sub_packet_list_C[16]; ///< packets with errors?
153 QDM2SubPNode sub_packet_list_D[16]; ///< DCT packets
156 FFTTone fft_tones[1000];
159 FFTCoefficient fft_coefs[1000];
161 int fft_coefs_min_index[5];
162 int fft_coefs_max_index[5];
163 int fft_level_exp[6];
164 RDFTContext rdft_ctx;
168 const uint8_t *compressed_data;
170 float output_buffer[1024];
173 DECLARE_ALIGNED(16, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512*2];
174 int synth_buf_offset[MPA_MAX_CHANNELS];
175 DECLARE_ALIGNED(16, int32_t, sb_samples)[MPA_MAX_CHANNELS][128][SBLIMIT];
177 /// Mixed temporary data used in decoding
178 float tone_level[MPA_MAX_CHANNELS][30][64];
179 int8_t coding_method[MPA_MAX_CHANNELS][30][64];
180 int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8];
181 int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8];
182 int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8];
183 int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8];
184 int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26];
185 int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64];
186 int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64];
189 int has_errors; ///< packet has errors
190 int superblocktype_2_3; ///< select fft tables and some algorithm based on superblock type
191 int do_synth_filter; ///< used to perform or skip synthesis filter
194 int noise_idx; ///< index for dithering noise table
198 static uint8_t empty_buffer[FF_INPUT_BUFFER_PADDING_SIZE];
200 static VLC vlc_tab_level;
201 static VLC vlc_tab_diff;
202 static VLC vlc_tab_run;
203 static VLC fft_level_exp_alt_vlc;
204 static VLC fft_level_exp_vlc;
205 static VLC fft_stereo_exp_vlc;
206 static VLC fft_stereo_phase_vlc;
207 static VLC vlc_tab_tone_level_idx_hi1;
208 static VLC vlc_tab_tone_level_idx_mid;
209 static VLC vlc_tab_tone_level_idx_hi2;
210 static VLC vlc_tab_type30;
211 static VLC vlc_tab_type34;
212 static VLC vlc_tab_fft_tone_offset[5];
214 static const uint16_t qdm2_vlc_offs[] = {
215 0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
218 static av_cold void qdm2_init_vlc(void)
220 static int vlcs_initialized = 0;
221 static VLC_TYPE qdm2_table[3838][2];
223 if (!vlcs_initialized) {
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, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
231 vlc_tab_diff.table = &qdm2_table[qdm2_vlc_offs[1]];
232 vlc_tab_diff.table_allocated = qdm2_vlc_offs[2] - qdm2_vlc_offs[1];
233 init_vlc (&vlc_tab_diff, 8, 37,
234 vlc_tab_diff_huffbits, 1, 1,
235 vlc_tab_diff_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
237 vlc_tab_run.table = &qdm2_table[qdm2_vlc_offs[2]];
238 vlc_tab_run.table_allocated = qdm2_vlc_offs[3] - qdm2_vlc_offs[2];
239 init_vlc (&vlc_tab_run, 5, 6,
240 vlc_tab_run_huffbits, 1, 1,
241 vlc_tab_run_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
243 fft_level_exp_alt_vlc.table = &qdm2_table[qdm2_vlc_offs[3]];
244 fft_level_exp_alt_vlc.table_allocated = qdm2_vlc_offs[4] - qdm2_vlc_offs[3];
245 init_vlc (&fft_level_exp_alt_vlc, 8, 28,
246 fft_level_exp_alt_huffbits, 1, 1,
247 fft_level_exp_alt_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
250 fft_level_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[4]];
251 fft_level_exp_vlc.table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
252 init_vlc (&fft_level_exp_vlc, 8, 20,
253 fft_level_exp_huffbits, 1, 1,
254 fft_level_exp_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
256 fft_stereo_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[5]];
257 fft_stereo_exp_vlc.table_allocated = qdm2_vlc_offs[6] - qdm2_vlc_offs[5];
258 init_vlc (&fft_stereo_exp_vlc, 6, 7,
259 fft_stereo_exp_huffbits, 1, 1,
260 fft_stereo_exp_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
262 fft_stereo_phase_vlc.table = &qdm2_table[qdm2_vlc_offs[6]];
263 fft_stereo_phase_vlc.table_allocated = qdm2_vlc_offs[7] - qdm2_vlc_offs[6];
264 init_vlc (&fft_stereo_phase_vlc, 6, 9,
265 fft_stereo_phase_huffbits, 1, 1,
266 fft_stereo_phase_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
268 vlc_tab_tone_level_idx_hi1.table = &qdm2_table[qdm2_vlc_offs[7]];
269 vlc_tab_tone_level_idx_hi1.table_allocated = qdm2_vlc_offs[8] - qdm2_vlc_offs[7];
270 init_vlc (&vlc_tab_tone_level_idx_hi1, 8, 20,
271 vlc_tab_tone_level_idx_hi1_huffbits, 1, 1,
272 vlc_tab_tone_level_idx_hi1_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
274 vlc_tab_tone_level_idx_mid.table = &qdm2_table[qdm2_vlc_offs[8]];
275 vlc_tab_tone_level_idx_mid.table_allocated = qdm2_vlc_offs[9] - qdm2_vlc_offs[8];
276 init_vlc (&vlc_tab_tone_level_idx_mid, 8, 24,
277 vlc_tab_tone_level_idx_mid_huffbits, 1, 1,
278 vlc_tab_tone_level_idx_mid_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
280 vlc_tab_tone_level_idx_hi2.table = &qdm2_table[qdm2_vlc_offs[9]];
281 vlc_tab_tone_level_idx_hi2.table_allocated = qdm2_vlc_offs[10] - qdm2_vlc_offs[9];
282 init_vlc (&vlc_tab_tone_level_idx_hi2, 8, 24,
283 vlc_tab_tone_level_idx_hi2_huffbits, 1, 1,
284 vlc_tab_tone_level_idx_hi2_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
286 vlc_tab_type30.table = &qdm2_table[qdm2_vlc_offs[10]];
287 vlc_tab_type30.table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
288 init_vlc (&vlc_tab_type30, 6, 9,
289 vlc_tab_type30_huffbits, 1, 1,
290 vlc_tab_type30_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
292 vlc_tab_type34.table = &qdm2_table[qdm2_vlc_offs[11]];
293 vlc_tab_type34.table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11];
294 init_vlc (&vlc_tab_type34, 5, 10,
295 vlc_tab_type34_huffbits, 1, 1,
296 vlc_tab_type34_huffcodes, 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
298 vlc_tab_fft_tone_offset[0].table = &qdm2_table[qdm2_vlc_offs[12]];
299 vlc_tab_fft_tone_offset[0].table_allocated = qdm2_vlc_offs[13] - qdm2_vlc_offs[12];
300 init_vlc (&vlc_tab_fft_tone_offset[0], 8, 23,
301 vlc_tab_fft_tone_offset_0_huffbits, 1, 1,
302 vlc_tab_fft_tone_offset_0_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
304 vlc_tab_fft_tone_offset[1].table = &qdm2_table[qdm2_vlc_offs[13]];
305 vlc_tab_fft_tone_offset[1].table_allocated = qdm2_vlc_offs[14] - qdm2_vlc_offs[13];
306 init_vlc (&vlc_tab_fft_tone_offset[1], 8, 28,
307 vlc_tab_fft_tone_offset_1_huffbits, 1, 1,
308 vlc_tab_fft_tone_offset_1_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
310 vlc_tab_fft_tone_offset[2].table = &qdm2_table[qdm2_vlc_offs[14]];
311 vlc_tab_fft_tone_offset[2].table_allocated = qdm2_vlc_offs[15] - qdm2_vlc_offs[14];
312 init_vlc (&vlc_tab_fft_tone_offset[2], 8, 32,
313 vlc_tab_fft_tone_offset_2_huffbits, 1, 1,
314 vlc_tab_fft_tone_offset_2_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
316 vlc_tab_fft_tone_offset[3].table = &qdm2_table[qdm2_vlc_offs[15]];
317 vlc_tab_fft_tone_offset[3].table_allocated = qdm2_vlc_offs[16] - qdm2_vlc_offs[15];
318 init_vlc (&vlc_tab_fft_tone_offset[3], 8, 35,
319 vlc_tab_fft_tone_offset_3_huffbits, 1, 1,
320 vlc_tab_fft_tone_offset_3_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
322 vlc_tab_fft_tone_offset[4].table = &qdm2_table[qdm2_vlc_offs[16]];
323 vlc_tab_fft_tone_offset[4].table_allocated = qdm2_vlc_offs[17] - qdm2_vlc_offs[16];
324 init_vlc (&vlc_tab_fft_tone_offset[4], 8, 38,
325 vlc_tab_fft_tone_offset_4_huffbits, 1, 1,
326 vlc_tab_fft_tone_offset_4_huffcodes, 2, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
333 /* for floating point to fixed point conversion */
334 static const float f2i_scale = (float) (1 << (FRAC_BITS - 15));
337 static int qdm2_get_vlc (GetBitContext *gb, VLC *vlc, int flag, int depth)
341 value = get_vlc2(gb, vlc->table, vlc->bits, depth);
343 /* stage-2, 3 bits exponent escape sequence */
345 value = get_bits (gb, get_bits (gb, 3) + 1);
347 /* stage-3, optional */
349 int tmp = vlc_stage3_values[value];
351 if ((value & ~3) > 0)
352 tmp += get_bits (gb, (value >> 2));
360 static int qdm2_get_se_vlc (VLC *vlc, GetBitContext *gb, int depth)
362 int value = qdm2_get_vlc (gb, vlc, 0, depth);
364 return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
371 * @param data pointer to data to be checksum'ed
372 * @param length data length
373 * @param value checksum value
375 * @return 0 if checksum is OK
377 static uint16_t qdm2_packet_checksum (const uint8_t *data, int length, int value) {
380 for (i=0; i < length; i++)
383 return (uint16_t)(value & 0xffff);
388 * Fill a QDM2SubPacket structure with packet type, size, and data pointer.
390 * @param gb bitreader context
391 * @param sub_packet packet under analysis
393 static void qdm2_decode_sub_packet_header (GetBitContext *gb, QDM2SubPacket *sub_packet)
395 sub_packet->type = get_bits (gb, 8);
397 if (sub_packet->type == 0) {
398 sub_packet->size = 0;
399 sub_packet->data = NULL;
401 sub_packet->size = get_bits (gb, 8);
403 if (sub_packet->type & 0x80) {
404 sub_packet->size <<= 8;
405 sub_packet->size |= get_bits (gb, 8);
406 sub_packet->type &= 0x7f;
409 if (sub_packet->type == 0x7f)
410 sub_packet->type |= (get_bits (gb, 8) << 8);
412 sub_packet->data = &gb->buffer[get_bits_count(gb) / 8]; // FIXME: this depends on bitreader internal data
415 av_log(NULL,AV_LOG_DEBUG,"Subpacket: type=%d size=%d start_offs=%x\n",
416 sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);
421 * Return node pointer to first packet of requested type in list.
423 * @param list list of subpackets to be scanned
424 * @param type type of searched subpacket
425 * @return node pointer for subpacket if found, else NULL
427 static QDM2SubPNode* qdm2_search_subpacket_type_in_list (QDM2SubPNode *list, int type)
429 while (list != NULL && list->packet != NULL) {
430 if (list->packet->type == type)
439 * Replace 8 elements with their average value.
440 * Called by qdm2_decode_superblock before starting subblock decoding.
444 static void average_quantized_coeffs (QDM2Context *q)
446 int i, j, n, ch, sum;
448 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
450 for (ch = 0; ch < q->nb_channels; ch++)
451 for (i = 0; i < n; i++) {
454 for (j = 0; j < 8; j++)
455 sum += q->quantized_coeffs[ch][i][j];
461 for (j=0; j < 8; j++)
462 q->quantized_coeffs[ch][i][j] = sum;
468 * Build subband samples with noise weighted by q->tone_level.
469 * Called by synthfilt_build_sb_samples.
472 * @param sb subband index
474 static void build_sb_samples_from_noise (QDM2Context *q, int sb)
478 FIX_NOISE_IDX(q->noise_idx);
483 for (ch = 0; ch < q->nb_channels; ch++)
484 for (j = 0; j < 64; j++) {
485 q->sb_samples[ch][j * 2][sb] = (int32_t)(f2i_scale * SB_DITHERING_NOISE(sb,q->noise_idx) * q->tone_level[ch][sb][j] + .5);
486 q->sb_samples[ch][j * 2 + 1][sb] = (int32_t)(f2i_scale * SB_DITHERING_NOISE(sb,q->noise_idx) * q->tone_level[ch][sb][j] + .5);
492 * Called while processing data from subpackets 11 and 12.
493 * Used after making changes to coding_method array.
495 * @param sb subband index
496 * @param channels number of channels
497 * @param coding_method q->coding_method[0][0][0]
499 static void fix_coding_method_array (int sb, int channels, sb_int8_array coding_method)
504 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};
506 for (ch = 0; ch < channels; ch++) {
507 for (j = 0; j < 64; ) {
508 if((coding_method[ch][sb][j] - 8) > 22) {
512 switch (switchtable[coding_method[ch][sb][j]-8]) {
513 case 0: run = 10; case_val = 10; break;
514 case 1: run = 1; case_val = 16; break;
515 case 2: run = 5; case_val = 24; break;
516 case 3: run = 3; case_val = 30; break;
517 case 4: run = 1; case_val = 30; break;
518 case 5: run = 1; case_val = 8; break;
519 default: run = 1; case_val = 8; break;
522 for (k = 0; k < run; k++)
524 if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j])
527 //not debugged, almost never used
528 memset(&coding_method[ch][sb][j + k], case_val, k * sizeof(int8_t));
529 memset(&coding_method[ch][sb][j + k], case_val, 3 * sizeof(int8_t));
538 * Related to synthesis filter
539 * Called by process_subpacket_10
542 * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10
544 static void fill_tone_level_array (QDM2Context *q, int flag)
546 int i, sb, ch, sb_used;
549 // This should never happen
550 if (q->nb_channels <= 0)
553 for (ch = 0; ch < q->nb_channels; ch++)
554 for (sb = 0; sb < 30; sb++)
555 for (i = 0; i < 8; i++) {
556 if ((tab=coeff_per_sb_for_dequant[q->coeff_per_sb_select][sb]) < (last_coeff[q->coeff_per_sb_select] - 1))
557 tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
558 q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
560 tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
563 q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
566 sb_used = QDM2_SB_USED(q->sub_sampling);
568 if ((q->superblocktype_2_3 != 0) && !flag) {
569 for (sb = 0; sb < sb_used; sb++)
570 for (ch = 0; ch < q->nb_channels; ch++)
571 for (i = 0; i < 64; i++) {
572 q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
573 if (q->tone_level_idx[ch][sb][i] < 0)
574 q->tone_level[ch][sb][i] = 0;
576 q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
579 tab = q->superblocktype_2_3 ? 0 : 1;
580 for (sb = 0; sb < sb_used; sb++) {
581 if ((sb >= 4) && (sb <= 23)) {
582 for (ch = 0; ch < q->nb_channels; ch++)
583 for (i = 0; i < 64; i++) {
584 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
585 q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
586 q->tone_level_idx_mid[ch][sb - 4][i / 8] -
587 q->tone_level_idx_hi2[ch][sb - 4];
588 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
589 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
590 q->tone_level[ch][sb][i] = 0;
592 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
596 for (ch = 0; ch < q->nb_channels; ch++)
597 for (i = 0; i < 64; i++) {
598 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
599 q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
600 q->tone_level_idx_hi2[ch][sb - 4];
601 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
602 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
603 q->tone_level[ch][sb][i] = 0;
605 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
608 for (ch = 0; ch < q->nb_channels; ch++)
609 for (i = 0; i < 64; i++) {
610 tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
611 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
612 q->tone_level[ch][sb][i] = 0;
614 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
626 * Related to synthesis filter
627 * Called by process_subpacket_11
628 * c is built with data from subpacket 11
629 * Most of this function is used only if superblock_type_2_3 == 0, never seen it in samples
631 * @param tone_level_idx
632 * @param tone_level_idx_temp
633 * @param coding_method q->coding_method[0][0][0]
634 * @param nb_channels number of channels
635 * @param c coming from subpacket 11, passed as 8*c
636 * @param superblocktype_2_3 flag based on superblock packet type
637 * @param cm_table_select q->cm_table_select
639 static void fill_coding_method_array (sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp,
640 sb_int8_array coding_method, int nb_channels,
641 int c, int superblocktype_2_3, int cm_table_select)
644 int tmp, acc, esp_40, comp;
645 int add1, add2, add3, add4;
648 // This should never happen
649 if (nb_channels <= 0)
652 if (!superblocktype_2_3) {
653 /* This case is untested, no samples available */
655 for (ch = 0; ch < nb_channels; ch++)
656 for (sb = 0; sb < 30; sb++) {
657 for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
658 add1 = tone_level_idx[ch][sb][j] - 10;
661 add2 = add3 = add4 = 0;
663 add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
668 add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
673 add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
677 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
680 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
682 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
685 for (ch = 0; ch < nb_channels; ch++)
686 for (sb = 0; sb < 30; sb++)
687 for (j = 0; j < 64; j++)
688 acc += tone_level_idx_temp[ch][sb][j];
690 multres = 0x66666667 * (acc * 10);
691 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
692 for (ch = 0; ch < nb_channels; ch++)
693 for (sb = 0; sb < 30; sb++)
694 for (j = 0; j < 64; j++) {
695 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
698 comp /= 256; // signed shift
726 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
728 for (sb = 0; sb < 30; sb++)
729 fix_coding_method_array(sb, nb_channels, coding_method);
730 for (ch = 0; ch < nb_channels; ch++)
731 for (sb = 0; sb < 30; sb++)
732 for (j = 0; j < 64; j++)
734 if (coding_method[ch][sb][j] < 10)
735 coding_method[ch][sb][j] = 10;
738 if (coding_method[ch][sb][j] < 16)
739 coding_method[ch][sb][j] = 16;
741 if (coding_method[ch][sb][j] < 30)
742 coding_method[ch][sb][j] = 30;
745 } else { // superblocktype_2_3 != 0
746 for (ch = 0; ch < nb_channels; ch++)
747 for (sb = 0; sb < 30; sb++)
748 for (j = 0; j < 64; j++)
749 coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
758 * Called by process_subpacket_11 to process more data from subpacket 11 with sb 0-8
759 * Called by process_subpacket_12 to process data from subpacket 12 with sb 8-sb_used
762 * @param gb bitreader context
763 * @param length packet length in bits
764 * @param sb_min lower subband processed (sb_min included)
765 * @param sb_max higher subband processed (sb_max excluded)
767 static void synthfilt_build_sb_samples (QDM2Context *q, GetBitContext *gb, int length, int sb_min, int sb_max)
769 int sb, j, k, n, ch, run, channels;
770 int joined_stereo, zero_encoding, chs;
772 float type34_div = 0;
773 float type34_predictor;
774 float samples[10], sign_bits[16];
777 // If no data use noise
778 for (sb=sb_min; sb < sb_max; sb++)
779 build_sb_samples_from_noise (q, sb);
784 for (sb = sb_min; sb < sb_max; sb++) {
785 FIX_NOISE_IDX(q->noise_idx);
787 channels = q->nb_channels;
789 if (q->nb_channels <= 1 || sb < 12)
794 joined_stereo = (BITS_LEFT(length,gb) >= 1) ? get_bits1 (gb) : 0;
797 if (BITS_LEFT(length,gb) >= 16)
798 for (j = 0; j < 16; j++)
799 sign_bits[j] = get_bits1 (gb);
801 for (j = 0; j < 64; j++)
802 if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
803 q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
805 fix_coding_method_array(sb, q->nb_channels, q->coding_method);
809 for (ch = 0; ch < channels; ch++) {
810 zero_encoding = (BITS_LEFT(length,gb) >= 1) ? get_bits1(gb) : 0;
811 type34_predictor = 0.0;
814 for (j = 0; j < 128; ) {
815 switch (q->coding_method[ch][sb][j / 2]) {
817 if (BITS_LEFT(length,gb) >= 10) {
819 for (k = 0; k < 5; k++) {
820 if ((j + 2 * k) >= 128)
822 samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
826 for (k = 0; k < 5; k++)
827 samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
829 for (k = 0; k < 5; k++)
830 samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
832 for (k = 0; k < 10; k++)
833 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
839 if (BITS_LEFT(length,gb) >= 1) {
844 f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
847 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
853 if (BITS_LEFT(length,gb) >= 10) {
855 for (k = 0; k < 5; k++) {
858 samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
861 n = get_bits (gb, 8);
862 for (k = 0; k < 5; k++)
863 samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
866 for (k = 0; k < 5; k++)
867 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
873 if (BITS_LEFT(length,gb) >= 7) {
875 for (k = 0; k < 3; k++)
876 samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
878 for (k = 0; k < 3; k++)
879 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
885 if (BITS_LEFT(length,gb) >= 4)
886 samples[0] = type30_dequant[qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1)];
888 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
894 if (BITS_LEFT(length,gb) >= 7) {
896 type34_div = (float)(1 << get_bits(gb, 2));
897 samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
898 type34_predictor = samples[0];
901 samples[0] = type34_delta[qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1)] / type34_div + type34_predictor;
902 type34_predictor = samples[0];
905 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
911 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
917 float tmp[10][MPA_MAX_CHANNELS];
919 for (k = 0; k < run; k++) {
920 tmp[k][0] = samples[k];
921 tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
923 for (chs = 0; chs < q->nb_channels; chs++)
924 for (k = 0; k < run; k++)
926 q->sb_samples[chs][j + k][sb] = (int32_t)(f2i_scale * q->tone_level[chs][sb][((j + k)/2)] * tmp[k][chs] + .5);
928 for (k = 0; k < run; k++)
930 q->sb_samples[ch][j + k][sb] = (int32_t)(f2i_scale * q->tone_level[ch][sb][(j + k)/2] * samples[k] + .5);
941 * Init the first element of a channel in quantized_coeffs with data from packet 10 (quantized_coeffs[ch][0]).
942 * This is similar to process_subpacket_9, but for a single channel and for element [0]
943 * same VLC tables as process_subpacket_9 are used.
945 * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
946 * @param gb bitreader context
947 * @param length packet length in bits
949 static void init_quantized_coeffs_elem0 (int8_t *quantized_coeffs, GetBitContext *gb, int length)
951 int i, k, run, level, diff;
953 if (BITS_LEFT(length,gb) < 16)
955 level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
957 quantized_coeffs[0] = level;
959 for (i = 0; i < 7; ) {
960 if (BITS_LEFT(length,gb) < 16)
962 run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
964 if (BITS_LEFT(length,gb) < 16)
966 diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
968 for (k = 1; k <= run; k++)
969 quantized_coeffs[i + k] = (level + ((k * diff) / run));
978 * Related to synthesis filter, process data from packet 10
979 * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
980 * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with data from packet 10
983 * @param gb bitreader context
984 * @param length packet length in bits
986 static void init_tone_level_dequantization (QDM2Context *q, GetBitContext *gb, int length)
990 for (ch = 0; ch < q->nb_channels; ch++) {
991 init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], gb, length);
993 if (BITS_LEFT(length,gb) < 16) {
994 memset(q->quantized_coeffs[ch][0], 0, 8);
999 n = q->sub_sampling + 1;
1001 for (sb = 0; sb < n; sb++)
1002 for (ch = 0; ch < q->nb_channels; ch++)
1003 for (j = 0; j < 8; j++) {
1004 if (BITS_LEFT(length,gb) < 1)
1006 if (get_bits1(gb)) {
1007 for (k=0; k < 8; k++) {
1008 if (BITS_LEFT(length,gb) < 16)
1010 q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
1013 for (k=0; k < 8; k++)
1014 q->tone_level_idx_hi1[ch][sb][j][k] = 0;
1018 n = QDM2_SB_USED(q->sub_sampling) - 4;
1020 for (sb = 0; sb < n; sb++)
1021 for (ch = 0; ch < q->nb_channels; ch++) {
1022 if (BITS_LEFT(length,gb) < 16)
1024 q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2);
1026 q->tone_level_idx_hi2[ch][sb] -= 16;
1028 for (j = 0; j < 8; j++)
1029 q->tone_level_idx_mid[ch][sb][j] = -16;
1032 n = QDM2_SB_USED(q->sub_sampling) - 5;
1034 for (sb = 0; sb < n; sb++)
1035 for (ch = 0; ch < q->nb_channels; ch++)
1036 for (j = 0; j < 8; j++) {
1037 if (BITS_LEFT(length,gb) < 16)
1039 q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
1044 * Process subpacket 9, init quantized_coeffs with data from it
1047 * @param node pointer to node with packet
1049 static void process_subpacket_9 (QDM2Context *q, QDM2SubPNode *node)
1052 int i, j, k, n, ch, run, level, diff;
1054 init_get_bits(&gb, node->packet->data, node->packet->size*8);
1056 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1; // same as averagesomething function
1058 for (i = 1; i < n; i++)
1059 for (ch=0; ch < q->nb_channels; ch++) {
1060 level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
1061 q->quantized_coeffs[ch][i][0] = level;
1063 for (j = 0; j < (8 - 1); ) {
1064 run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
1065 diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
1067 for (k = 1; k <= run; k++)
1068 q->quantized_coeffs[ch][i][j + k] = (level + ((k*diff) / run));
1075 for (ch = 0; ch < q->nb_channels; ch++)
1076 for (i = 0; i < 8; i++)
1077 q->quantized_coeffs[ch][0][i] = 0;
1082 * Process subpacket 10 if not null, else
1085 * @param node pointer to node with packet
1086 * @param length packet length in bits
1088 static void process_subpacket_10 (QDM2Context *q, QDM2SubPNode *node, int length)
1092 init_get_bits(&gb, ((node == NULL) ? empty_buffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8));
1095 init_tone_level_dequantization(q, &gb, length);
1096 fill_tone_level_array(q, 1);
1098 fill_tone_level_array(q, 0);
1104 * Process subpacket 11
1107 * @param node pointer to node with packet
1108 * @param length packet length in bit
1110 static void process_subpacket_11 (QDM2Context *q, QDM2SubPNode *node, int length)
1114 init_get_bits(&gb, ((node == NULL) ? empty_buffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8));
1116 int c = get_bits (&gb, 13);
1119 fill_coding_method_array (q->tone_level_idx, q->tone_level_idx_temp, q->coding_method,
1120 q->nb_channels, 8*c, q->superblocktype_2_3, q->cm_table_select);
1123 synthfilt_build_sb_samples(q, &gb, length, 0, 8);
1128 * Process subpacket 12
1131 * @param node pointer to node with packet
1132 * @param length packet length in bits
1134 static void process_subpacket_12 (QDM2Context *q, QDM2SubPNode *node, int length)
1138 init_get_bits(&gb, ((node == NULL) ? empty_buffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8));
1139 synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
1143 * Process new subpackets for synthesis filter
1146 * @param list list with synthesis filter packets (list D)
1148 static void process_synthesis_subpackets (QDM2Context *q, QDM2SubPNode *list)
1150 QDM2SubPNode *nodes[4];
1152 nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
1153 if (nodes[0] != NULL)
1154 process_subpacket_9(q, nodes[0]);
1156 nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
1157 if (nodes[1] != NULL)
1158 process_subpacket_10(q, nodes[1], nodes[1]->packet->size << 3);
1160 process_subpacket_10(q, NULL, 0);
1162 nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
1163 if (nodes[0] != NULL && nodes[1] != NULL && nodes[2] != NULL)
1164 process_subpacket_11(q, nodes[2], (nodes[2]->packet->size << 3));
1166 process_subpacket_11(q, NULL, 0);
1168 nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
1169 if (nodes[0] != NULL && nodes[1] != NULL && nodes[3] != NULL)
1170 process_subpacket_12(q, nodes[3], (nodes[3]->packet->size << 3));
1172 process_subpacket_12(q, NULL, 0);
1177 * Decode superblock, fill packet lists.
1181 static void qdm2_decode_super_block (QDM2Context *q)
1184 QDM2SubPacket header, *packet;
1185 int i, packet_bytes, sub_packet_size, sub_packets_D;
1186 unsigned int next_index = 0;
1188 memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
1189 memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
1190 memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
1192 q->sub_packets_B = 0;
1195 average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
1197 init_get_bits(&gb, q->compressed_data, q->compressed_size*8);
1198 qdm2_decode_sub_packet_header(&gb, &header);
1200 if (header.type < 2 || header.type >= 8) {
1202 av_log(NULL,AV_LOG_ERROR,"bad superblock type\n");
1206 q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
1207 packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
1209 init_get_bits(&gb, header.data, header.size*8);
1211 if (header.type == 2 || header.type == 4 || header.type == 5) {
1212 int csum = 257 * get_bits(&gb, 8);
1213 csum += 2 * get_bits(&gb, 8);
1215 csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
1219 av_log(NULL,AV_LOG_ERROR,"bad packet checksum\n");
1224 q->sub_packet_list_B[0].packet = NULL;
1225 q->sub_packet_list_D[0].packet = NULL;
1227 for (i = 0; i < 6; i++)
1228 if (--q->fft_level_exp[i] < 0)
1229 q->fft_level_exp[i] = 0;
1231 for (i = 0; packet_bytes > 0; i++) {
1234 q->sub_packet_list_A[i].next = NULL;
1237 q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
1239 /* seek to next block */
1240 init_get_bits(&gb, header.data, header.size*8);
1241 skip_bits(&gb, next_index*8);
1243 if (next_index >= header.size)
1247 /* decode subpacket */
1248 packet = &q->sub_packets[i];
1249 qdm2_decode_sub_packet_header(&gb, packet);
1250 next_index = packet->size + get_bits_count(&gb) / 8;
1251 sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
1253 if (packet->type == 0)
1256 if (sub_packet_size > packet_bytes) {
1257 if (packet->type != 10 && packet->type != 11 && packet->type != 12)
1259 packet->size += packet_bytes - sub_packet_size;
1262 packet_bytes -= sub_packet_size;
1264 /* add subpacket to 'all subpackets' list */
1265 q->sub_packet_list_A[i].packet = packet;
1267 /* add subpacket to related list */
1268 if (packet->type == 8) {
1269 SAMPLES_NEEDED_2("packet type 8");
1271 } else if (packet->type >= 9 && packet->type <= 12) {
1272 /* packets for MPEG Audio like Synthesis Filter */
1273 QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
1274 } else if (packet->type == 13) {
1275 for (j = 0; j < 6; j++)
1276 q->fft_level_exp[j] = get_bits(&gb, 6);
1277 } else if (packet->type == 14) {
1278 for (j = 0; j < 6; j++)
1279 q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
1280 } else if (packet->type == 15) {
1281 SAMPLES_NEEDED_2("packet type 15")
1283 } else if (packet->type >= 16 && packet->type < 48 && !fft_subpackets[packet->type - 16]) {
1284 /* packets for FFT */
1285 QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);
1287 } // Packet bytes loop
1289 /* **************************************************************** */
1290 if (q->sub_packet_list_D[0].packet != NULL) {
1291 process_synthesis_subpackets(q, q->sub_packet_list_D);
1292 q->do_synth_filter = 1;
1293 } else if (q->do_synth_filter) {
1294 process_subpacket_10(q, NULL, 0);
1295 process_subpacket_11(q, NULL, 0);
1296 process_subpacket_12(q, NULL, 0);
1298 /* **************************************************************** */
1302 static void qdm2_fft_init_coefficient (QDM2Context *q, int sub_packet,
1303 int offset, int duration, int channel,
1306 if (q->fft_coefs_min_index[duration] < 0)
1307 q->fft_coefs_min_index[duration] = q->fft_coefs_index;
1309 q->fft_coefs[q->fft_coefs_index].sub_packet = ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1310 q->fft_coefs[q->fft_coefs_index].channel = channel;
1311 q->fft_coefs[q->fft_coefs_index].offset = offset;
1312 q->fft_coefs[q->fft_coefs_index].exp = exp;
1313 q->fft_coefs[q->fft_coefs_index].phase = phase;
1314 q->fft_coefs_index++;
1318 static void qdm2_fft_decode_tones (QDM2Context *q, int duration, GetBitContext *gb, int b)
1320 int channel, stereo, phase, exp;
1321 int local_int_4, local_int_8, stereo_phase, local_int_10;
1322 int local_int_14, stereo_exp, local_int_20, local_int_28;
1328 local_int_8 = (4 - duration);
1329 local_int_10 = 1 << (q->group_order - duration - 1);
1333 if (q->superblocktype_2_3) {
1334 while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1337 local_int_4 += local_int_10;
1338 local_int_28 += (1 << local_int_8);
1340 local_int_4 += 8*local_int_10;
1341 local_int_28 += (8 << local_int_8);
1346 offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1347 while (offset >= (local_int_10 - 1)) {
1348 offset += (1 - (local_int_10 - 1));
1349 local_int_4 += local_int_10;
1350 local_int_28 += (1 << local_int_8);
1354 if (local_int_4 >= q->group_size)
1357 local_int_14 = (offset >> local_int_8);
1359 if (q->nb_channels > 1) {
1360 channel = get_bits1(gb);
1361 stereo = get_bits1(gb);
1367 exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1368 exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
1369 exp = (exp < 0) ? 0 : exp;
1371 phase = get_bits(gb, 3);
1376 stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1377 stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1378 if (stereo_phase < 0)
1382 if (q->frequency_range > (local_int_14 + 1)) {
1383 int sub_packet = (local_int_20 + local_int_28);
1385 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, channel, exp, phase);
1387 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, (1 - channel), stereo_exp, stereo_phase);
1395 static void qdm2_decode_fft_packets (QDM2Context *q)
1397 int i, j, min, max, value, type, unknown_flag;
1400 if (q->sub_packet_list_B[0].packet == NULL)
1403 /* reset minimum indexes for FFT coefficients */
1404 q->fft_coefs_index = 0;
1405 for (i=0; i < 5; i++)
1406 q->fft_coefs_min_index[i] = -1;
1408 /* process subpackets ordered by type, largest type first */
1409 for (i = 0, max = 256; i < q->sub_packets_B; i++) {
1410 QDM2SubPacket *packet= NULL;
1412 /* find subpacket with largest type less than max */
1413 for (j = 0, min = 0; j < q->sub_packets_B; j++) {
1414 value = q->sub_packet_list_B[j].packet->type;
1415 if (value > min && value < max) {
1417 packet = q->sub_packet_list_B[j].packet;
1423 /* check for errors (?) */
1427 if (i == 0 && (packet->type < 16 || packet->type >= 48 || fft_subpackets[packet->type - 16]))
1430 /* decode FFT tones */
1431 init_get_bits (&gb, packet->data, packet->size*8);
1433 if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
1438 type = packet->type;
1440 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1441 int duration = q->sub_sampling + 5 - (type & 15);
1443 if (duration >= 0 && duration < 4)
1444 qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
1445 } else if (type == 31) {
1446 for (j=0; j < 4; j++)
1447 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1448 } else if (type == 46) {
1449 for (j=0; j < 6; j++)
1450 q->fft_level_exp[j] = get_bits(&gb, 6);
1451 for (j=0; j < 4; j++)
1452 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1454 } // Loop on B packets
1456 /* calculate maximum indexes for FFT coefficients */
1457 for (i = 0, j = -1; i < 5; i++)
1458 if (q->fft_coefs_min_index[i] >= 0) {
1460 q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i];
1464 q->fft_coefs_max_index[j] = q->fft_coefs_index;
1468 static void qdm2_fft_generate_tone (QDM2Context *q, FFTTone *tone)
1473 const double iscale = 2.0*M_PI / 512.0;
1475 tone->phase += tone->phase_shift;
1477 /* calculate current level (maximum amplitude) of tone */
1478 level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
1479 c.im = level * sin(tone->phase*iscale);
1480 c.re = level * cos(tone->phase*iscale);
1482 /* generate FFT coefficients for tone */
1483 if (tone->duration >= 3 || tone->cutoff >= 3) {
1484 tone->complex[0].im += c.im;
1485 tone->complex[0].re += c.re;
1486 tone->complex[1].im -= c.im;
1487 tone->complex[1].re -= c.re;
1489 f[1] = -tone->table[4];
1490 f[0] = tone->table[3] - tone->table[0];
1491 f[2] = 1.0 - tone->table[2] - tone->table[3];
1492 f[3] = tone->table[1] + tone->table[4] - 1.0;
1493 f[4] = tone->table[0] - tone->table[1];
1494 f[5] = tone->table[2];
1495 for (i = 0; i < 2; i++) {
1496 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re += c.re * f[i];
1497 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im += c.im *((tone->cutoff <= i) ? -f[i] : f[i]);
1499 for (i = 0; i < 4; i++) {
1500 tone->complex[i].re += c.re * f[i+2];
1501 tone->complex[i].im += c.im * f[i+2];
1505 /* copy the tone if it has not yet died out */
1506 if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
1507 memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
1508 q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
1513 static void qdm2_fft_tone_synthesizer (QDM2Context *q, int sub_packet)
1516 const double iscale = 0.25 * M_PI;
1518 for (ch = 0; ch < q->channels; ch++) {
1519 memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
1523 /* apply FFT tones with duration 4 (1 FFT period) */
1524 if (q->fft_coefs_min_index[4] >= 0)
1525 for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
1529 if (q->fft_coefs[i].sub_packet != sub_packet)
1532 ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
1533 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];
1535 c.re = level * cos(q->fft_coefs[i].phase * iscale);
1536 c.im = level * sin(q->fft_coefs[i].phase * iscale);
1537 q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
1538 q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
1539 q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
1540 q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
1543 /* generate existing FFT tones */
1544 for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
1545 qdm2_fft_generate_tone(q, &q->fft_tones[q->fft_tone_start]);
1546 q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
1549 /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1550 for (i = 0; i < 4; i++)
1551 if (q->fft_coefs_min_index[i] >= 0) {
1552 for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
1556 if (q->fft_coefs[j].sub_packet != sub_packet)
1560 offset = q->fft_coefs[j].offset >> four_i;
1561 ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
1563 if (offset < q->frequency_range) {
1565 tone.cutoff = offset;
1567 tone.cutoff = (offset >= 60) ? 3 : 2;
1569 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];
1570 tone.complex = &q->fft.complex[ch][offset];
1571 tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
1572 tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
1573 tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
1575 tone.time_index = 0;
1577 qdm2_fft_generate_tone(q, &tone);
1580 q->fft_coefs_min_index[i] = j;
1585 static void qdm2_calculate_fft (QDM2Context *q, int channel, int sub_packet)
1587 const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
1589 q->fft.complex[channel][0].re *= 2.0f;
1590 q->fft.complex[channel][0].im = 0.0f;
1591 q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
1592 /* add samples to output buffer */
1593 for (i = 0; i < ((q->fft_frame_size + 15) & ~15); i++)
1594 q->output_buffer[q->channels * i + channel] += ((float *) q->fft.complex[channel])[i] * gain;
1600 * @param index subpacket number
1602 static void qdm2_synthesis_filter (QDM2Context *q, int index)
1604 OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE];
1605 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1607 /* copy sb_samples */
1608 sb_used = QDM2_SB_USED(q->sub_sampling);
1610 for (ch = 0; ch < q->channels; ch++)
1611 for (i = 0; i < 8; i++)
1612 for (k=sb_used; k < SBLIMIT; k++)
1613 q->sb_samples[ch][(8 * index) + i][k] = 0;
1615 for (ch = 0; ch < q->nb_channels; ch++) {
1616 OUT_INT *samples_ptr = samples + ch;
1618 for (i = 0; i < 8; i++) {
1619 ff_mpa_synth_filter(q->synth_buf[ch], &(q->synth_buf_offset[ch]),
1620 ff_mpa_synth_window, &dither_state,
1621 samples_ptr, q->nb_channels,
1622 q->sb_samples[ch][(8 * index) + i]);
1623 samples_ptr += 32 * q->nb_channels;
1627 /* add samples to output buffer */
1628 sub_sampling = (4 >> q->sub_sampling);
1630 for (ch = 0; ch < q->channels; ch++)
1631 for (i = 0; i < q->frame_size; i++)
1632 q->output_buffer[q->channels * i + ch] += (float)(samples[q->nb_channels * sub_sampling * i + ch] >> (sizeof(OUT_INT)*8-16));
1637 * Init static data (does not depend on specific file)
1641 static av_cold void qdm2_init(QDM2Context *q) {
1642 static int initialized = 0;
1644 if (initialized != 0)
1649 ff_mpa_synth_init(ff_mpa_synth_window);
1650 softclip_table_init();
1652 init_noise_samples();
1654 av_log(NULL, AV_LOG_DEBUG, "init done\n");
1659 static void dump_context(QDM2Context *q)
1662 #define PRINT(a,b) av_log(NULL,AV_LOG_DEBUG," %s = %d\n", a, b);
1663 PRINT("compressed_data",q->compressed_data);
1664 PRINT("compressed_size",q->compressed_size);
1665 PRINT("frame_size",q->frame_size);
1666 PRINT("checksum_size",q->checksum_size);
1667 PRINT("channels",q->channels);
1668 PRINT("nb_channels",q->nb_channels);
1669 PRINT("fft_frame_size",q->fft_frame_size);
1670 PRINT("fft_size",q->fft_size);
1671 PRINT("sub_sampling",q->sub_sampling);
1672 PRINT("fft_order",q->fft_order);
1673 PRINT("group_order",q->group_order);
1674 PRINT("group_size",q->group_size);
1675 PRINT("sub_packet",q->sub_packet);
1676 PRINT("frequency_range",q->frequency_range);
1677 PRINT("has_errors",q->has_errors);
1678 PRINT("fft_tone_end",q->fft_tone_end);
1679 PRINT("fft_tone_start",q->fft_tone_start);
1680 PRINT("fft_coefs_index",q->fft_coefs_index);
1681 PRINT("coeff_per_sb_select",q->coeff_per_sb_select);
1682 PRINT("cm_table_select",q->cm_table_select);
1683 PRINT("noise_idx",q->noise_idx);
1685 for (i = q->fft_tone_start; i < q->fft_tone_end; i++)
1687 FFTTone *t = &q->fft_tones[i];
1689 av_log(NULL,AV_LOG_DEBUG,"Tone (%d) dump:\n", i);
1690 av_log(NULL,AV_LOG_DEBUG," level = %f\n", t->level);
1691 // PRINT(" level", t->level);
1692 PRINT(" phase", t->phase);
1693 PRINT(" phase_shift", t->phase_shift);
1694 PRINT(" duration", t->duration);
1695 PRINT(" samples_im", t->samples_im);
1696 PRINT(" samples_re", t->samples_re);
1697 PRINT(" table", t->table);
1705 * Init parameters from codec extradata
1707 static av_cold int qdm2_decode_init(AVCodecContext *avctx)
1709 QDM2Context *s = avctx->priv_data;
1712 int tmp_val, tmp, size;
1714 /* extradata parsing
1723 32 size (including this field)
1725 32 type (=QDM2 or QDMC)
1727 32 size (including this field, in bytes)
1728 32 tag (=QDCA) // maybe mandatory parameters
1731 32 samplerate (=44100)
1733 32 block size (=4096)
1734 32 frame size (=256) (for one channel)
1735 32 packet size (=1300)
1737 32 size (including this field, in bytes)
1738 32 tag (=QDCP) // maybe some tuneable parameters
1748 if (!avctx->extradata || (avctx->extradata_size < 48)) {
1749 av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
1753 extradata = avctx->extradata;
1754 extradata_size = avctx->extradata_size;
1756 while (extradata_size > 7) {
1757 if (!memcmp(extradata, "frmaQDM", 7))
1763 if (extradata_size < 12) {
1764 av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
1769 if (memcmp(extradata, "frmaQDM", 7)) {
1770 av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
1774 if (extradata[7] == 'C') {
1776 av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n");
1781 extradata_size -= 8;
1783 size = AV_RB32(extradata);
1785 if(size > extradata_size){
1786 av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
1787 extradata_size, size);
1792 av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
1793 if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
1794 av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
1800 avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
1803 avctx->sample_rate = AV_RB32(extradata);
1806 avctx->bit_rate = AV_RB32(extradata);
1809 s->group_size = AV_RB32(extradata);
1812 s->fft_size = AV_RB32(extradata);
1815 s->checksum_size = AV_RB32(extradata);
1817 s->fft_order = av_log2(s->fft_size) + 1;
1818 s->fft_frame_size = 2 * s->fft_size; // complex has two floats
1820 // something like max decodable tones
1821 s->group_order = av_log2(s->group_size) + 1;
1822 s->frame_size = s->group_size / 16; // 16 iterations per super block
1824 s->sub_sampling = s->fft_order - 7;
1825 s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
1827 switch ((s->sub_sampling * 2 + s->channels - 1)) {
1828 case 0: tmp = 40; break;
1829 case 1: tmp = 48; break;
1830 case 2: tmp = 56; break;
1831 case 3: tmp = 72; break;
1832 case 4: tmp = 80; break;
1833 case 5: tmp = 100;break;
1834 default: tmp=s->sub_sampling; break;
1837 if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
1838 if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
1839 if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
1840 if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
1841 s->cm_table_select = tmp_val;
1843 if (s->sub_sampling == 0)
1846 tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;
1853 s->coeff_per_sb_select = 0;
1854 else if (tmp <= 16000)
1855 s->coeff_per_sb_select = 1;
1857 s->coeff_per_sb_select = 2;
1859 // Fail on unknown fft order
1860 if ((s->fft_order < 7) || (s->fft_order > 9)) {
1861 av_log(avctx, AV_LOG_ERROR, "Unknown FFT order (%d), contact the developers!\n", s->fft_order);
1865 ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);
1869 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1876 static av_cold int qdm2_decode_close(AVCodecContext *avctx)
1878 QDM2Context *s = avctx->priv_data;
1880 ff_rdft_end(&s->rdft_ctx);
1886 static int qdm2_decode (QDM2Context *q, const uint8_t *in, int16_t *out)
1889 const int frame_size = (q->frame_size * q->channels);
1891 /* select input buffer */
1892 q->compressed_data = in;
1893 q->compressed_size = q->checksum_size;
1897 /* copy old block, clear new block of output samples */
1898 memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
1899 memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
1901 /* decode block of QDM2 compressed data */
1902 if (q->sub_packet == 0) {
1903 q->has_errors = 0; // zero it for a new super block
1904 av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
1905 qdm2_decode_super_block(q);
1908 /* parse subpackets */
1909 if (!q->has_errors) {
1910 if (q->sub_packet == 2)
1911 qdm2_decode_fft_packets(q);
1913 qdm2_fft_tone_synthesizer(q, q->sub_packet);
1916 /* sound synthesis stage 1 (FFT) */
1917 for (ch = 0; ch < q->channels; ch++) {
1918 qdm2_calculate_fft(q, ch, q->sub_packet);
1920 if (!q->has_errors && q->sub_packet_list_C[0].packet != NULL) {
1921 SAMPLES_NEEDED_2("has errors, and C list is not empty")
1926 /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1927 if (!q->has_errors && q->do_synth_filter)
1928 qdm2_synthesis_filter(q, q->sub_packet);
1930 q->sub_packet = (q->sub_packet + 1) % 16;
1932 /* clip and convert output float[] to 16bit signed samples */
1933 for (i = 0; i < frame_size; i++) {
1934 int value = (int)q->output_buffer[i];
1936 if (value > SOFTCLIP_THRESHOLD)
1937 value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
1938 else if (value < -SOFTCLIP_THRESHOLD)
1939 value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
1948 static int qdm2_decode_frame(AVCodecContext *avctx,
1949 void *data, int *data_size,
1952 const uint8_t *buf = avpkt->data;
1953 int buf_size = avpkt->size;
1954 QDM2Context *s = avctx->priv_data;
1955 int16_t *out = data;
1960 if(buf_size < s->checksum_size)
1963 av_log(avctx, AV_LOG_DEBUG, "decode(%d): %p[%d] -> %p[%d]\n",
1964 buf_size, buf, s->checksum_size, data, *data_size);
1966 for (i = 0; i < 16; i++) {
1967 if (qdm2_decode(s, buf, out) < 0)
1969 out += s->channels * s->frame_size;
1972 *data_size = (uint8_t*)out - (uint8_t*)data;
1974 return s->checksum_size;
1977 AVCodec ff_qdm2_decoder =
1980 .type = AVMEDIA_TYPE_AUDIO,
1981 .id = CODEC_ID_QDM2,
1982 .priv_data_size = sizeof(QDM2Context),
1983 .init = qdm2_decode_init,
1984 .close = qdm2_decode_close,
1985 .decode = qdm2_decode_frame,
1986 .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),