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
39 #include "bitstream.h"
42 #ifdef CONFIG_MPEGAUDIO_HP
43 #define USE_HIGHPRECISION
46 #include "mpegaudio.h"
54 #define SOFTCLIP_THRESHOLD 27600
55 #define HARDCLIP_THRESHOLD 35716
58 #define QDM2_LIST_ADD(list, size, packet) \
61 list[size - 1].next = &list[size]; \
63 list[size].packet = packet; \
64 list[size].next = NULL; \
68 // Result is 8, 16 or 30
69 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
71 #define FIX_NOISE_IDX(noise_idx) \
72 if ((noise_idx) >= 3840) \
73 (noise_idx) -= 3840; \
75 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
77 #define BITS_LEFT(length,gb) ((length) - get_bits_count ((gb)))
79 #define SAMPLES_NEEDED \
80 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
82 #define SAMPLES_NEEDED_2(why) \
83 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
86 typedef int8_t sb_int8_array
[2][30][64];
92 int type
; ///< subpacket type
93 unsigned int size
; ///< subpacket size
94 const uint8_t *data
; ///< pointer to subpacket data (points to input data buffer, it's not a private copy)
98 * A node in the subpacket list
100 typedef struct QDM2SubPNode
{
101 QDM2SubPacket
*packet
; ///< packet
102 struct QDM2SubPNode
*next
; ///< pointer to next packet in the list, NULL if leaf node
131 DECLARE_ALIGNED_16(QDM2Complex
, complex[256 + 1]);
132 float samples_im
[MPA_MAX_CHANNELS
][256];
133 float samples_re
[MPA_MAX_CHANNELS
][256];
137 * QDM2 decoder context
140 /// Parameters from codec header, do not change during playback
141 int nb_channels
; ///< number of channels
142 int channels
; ///< number of channels
143 int group_size
; ///< size of frame group (16 frames per group)
144 int fft_size
; ///< size of FFT, in complex numbers
145 int checksum_size
; ///< size of data block, used also for checksum
147 /// Parameters built from header parameters, do not change during playback
148 int group_order
; ///< order of frame group
149 int fft_order
; ///< order of FFT (actually fftorder+1)
150 int fft_frame_size
; ///< size of fft frame, in components (1 comples = re + im)
151 int frame_size
; ///< size of data frame
153 int sub_sampling
; ///< subsampling: 0=25%, 1=50%, 2=100% */
154 int coeff_per_sb_select
; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
155 int cm_table_select
; ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
157 /// Packets and packet lists
158 QDM2SubPacket sub_packets
[16]; ///< the packets themselves
159 QDM2SubPNode sub_packet_list_A
[16]; ///< list of all packets
160 QDM2SubPNode sub_packet_list_B
[16]; ///< FFT packets B are on list
161 int sub_packets_B
; ///< number of packets on 'B' list
162 QDM2SubPNode sub_packet_list_C
[16]; ///< packets with errors?
163 QDM2SubPNode sub_packet_list_D
[16]; ///< DCT packets
166 FFTTone fft_tones
[1000];
169 FFTCoefficient fft_coefs
[1000];
171 int fft_coefs_min_index
[5];
172 int fft_coefs_max_index
[5];
173 int fft_level_exp
[6];
175 FFTComplex exptab
[128];
179 const uint8_t *compressed_data
;
181 float output_buffer
[1024];
184 DECLARE_ALIGNED_16(MPA_INT
, synth_buf
[MPA_MAX_CHANNELS
][512*2]);
185 int synth_buf_offset
[MPA_MAX_CHANNELS
];
186 DECLARE_ALIGNED_16(int32_t, sb_samples
[MPA_MAX_CHANNELS
][128][SBLIMIT
]);
188 /// Mixed temporary data used in decoding
189 float tone_level
[MPA_MAX_CHANNELS
][30][64];
190 int8_t coding_method
[MPA_MAX_CHANNELS
][30][64];
191 int8_t quantized_coeffs
[MPA_MAX_CHANNELS
][10][8];
192 int8_t tone_level_idx_base
[MPA_MAX_CHANNELS
][30][8];
193 int8_t tone_level_idx_hi1
[MPA_MAX_CHANNELS
][3][8][8];
194 int8_t tone_level_idx_mid
[MPA_MAX_CHANNELS
][26][8];
195 int8_t tone_level_idx_hi2
[MPA_MAX_CHANNELS
][26];
196 int8_t tone_level_idx
[MPA_MAX_CHANNELS
][30][64];
197 int8_t tone_level_idx_temp
[MPA_MAX_CHANNELS
][30][64];
200 int has_errors
; ///< packet has errors
201 int superblocktype_2_3
; ///< select fft tables and some algorithm based on superblock type
202 int do_synth_filter
; ///< used to perform or skip synthesis filter
205 int noise_idx
; ///< index for dithering noise table
209 static uint8_t empty_buffer
[FF_INPUT_BUFFER_PADDING_SIZE
];
211 static VLC vlc_tab_level
;
212 static VLC vlc_tab_diff
;
213 static VLC vlc_tab_run
;
214 static VLC fft_level_exp_alt_vlc
;
215 static VLC fft_level_exp_vlc
;
216 static VLC fft_stereo_exp_vlc
;
217 static VLC fft_stereo_phase_vlc
;
218 static VLC vlc_tab_tone_level_idx_hi1
;
219 static VLC vlc_tab_tone_level_idx_mid
;
220 static VLC vlc_tab_tone_level_idx_hi2
;
221 static VLC vlc_tab_type30
;
222 static VLC vlc_tab_type34
;
223 static VLC vlc_tab_fft_tone_offset
[5];
225 static uint16_t softclip_table
[HARDCLIP_THRESHOLD
- SOFTCLIP_THRESHOLD
+ 1];
226 static float noise_table
[4096];
227 static uint8_t random_dequant_index
[256][5];
228 static uint8_t random_dequant_type24
[128][3];
229 static float noise_samples
[128];
231 static DECLARE_ALIGNED_16(MPA_INT
, mpa_window
[512]);
234 static void softclip_table_init(void) {
236 double dfl
= SOFTCLIP_THRESHOLD
- 32767;
237 float delta
= 1.0 / -dfl
;
238 for (i
= 0; i
< HARDCLIP_THRESHOLD
- SOFTCLIP_THRESHOLD
+ 1; i
++)
239 softclip_table
[i
] = SOFTCLIP_THRESHOLD
- ((int)(sin((float)i
* delta
) * dfl
) & 0x0000FFFF);
243 // random generated table
244 static void rnd_table_init(void) {
248 uint64_t random_seed
= 0;
249 float delta
= 1.0 / 16384.0;
250 for(i
= 0; i
< 4096 ;i
++) {
251 random_seed
= random_seed
* 214013 + 2531011;
252 noise_table
[i
] = (delta
* (float)(((int32_t)random_seed
>> 16) & 0x00007FFF)- 1.0) * 1.3;
255 for (i
= 0; i
< 256 ;i
++) {
258 for (j
= 0; j
< 5 ;j
++) {
259 random_dequant_index
[i
][j
] = (uint8_t)((ldw
/ random_seed
) & 0xFF);
260 ldw
= (uint32_t)ldw
% (uint32_t)random_seed
;
261 tmp64_1
= (random_seed
* 0x55555556);
262 hdw
= (uint32_t)(tmp64_1
>> 32);
263 random_seed
= (uint64_t)(hdw
+ (ldw
>> 31));
266 for (i
= 0; i
< 128 ;i
++) {
269 for (j
= 0; j
< 3 ;j
++) {
270 random_dequant_type24
[i
][j
] = (uint8_t)((ldw
/ random_seed
) & 0xFF);
271 ldw
= (uint32_t)ldw
% (uint32_t)random_seed
;
272 tmp64_1
= (random_seed
* 0x66666667);
273 hdw
= (uint32_t)(tmp64_1
>> 33);
274 random_seed
= hdw
+ (ldw
>> 31);
280 static void init_noise_samples(void) {
283 float delta
= 1.0 / 16384.0;
284 for (i
= 0; i
< 128;i
++) {
285 random_seed
= random_seed
* 214013 + 2531011;
286 noise_samples
[i
] = (delta
* (float)((random_seed
>> 16) & 0x00007fff) - 1.0);
291 static void qdm2_init_vlc(void)
293 init_vlc (&vlc_tab_level
, 8, 24,
294 vlc_tab_level_huffbits
, 1, 1,
295 vlc_tab_level_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
297 init_vlc (&vlc_tab_diff
, 8, 37,
298 vlc_tab_diff_huffbits
, 1, 1,
299 vlc_tab_diff_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
301 init_vlc (&vlc_tab_run
, 5, 6,
302 vlc_tab_run_huffbits
, 1, 1,
303 vlc_tab_run_huffcodes
, 1, 1, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
305 init_vlc (&fft_level_exp_alt_vlc
, 8, 28,
306 fft_level_exp_alt_huffbits
, 1, 1,
307 fft_level_exp_alt_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
309 init_vlc (&fft_level_exp_vlc
, 8, 20,
310 fft_level_exp_huffbits
, 1, 1,
311 fft_level_exp_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
313 init_vlc (&fft_stereo_exp_vlc
, 6, 7,
314 fft_stereo_exp_huffbits
, 1, 1,
315 fft_stereo_exp_huffcodes
, 1, 1, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
317 init_vlc (&fft_stereo_phase_vlc
, 6, 9,
318 fft_stereo_phase_huffbits
, 1, 1,
319 fft_stereo_phase_huffcodes
, 1, 1, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
321 init_vlc (&vlc_tab_tone_level_idx_hi1
, 8, 20,
322 vlc_tab_tone_level_idx_hi1_huffbits
, 1, 1,
323 vlc_tab_tone_level_idx_hi1_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
325 init_vlc (&vlc_tab_tone_level_idx_mid
, 8, 24,
326 vlc_tab_tone_level_idx_mid_huffbits
, 1, 1,
327 vlc_tab_tone_level_idx_mid_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
329 init_vlc (&vlc_tab_tone_level_idx_hi2
, 8, 24,
330 vlc_tab_tone_level_idx_hi2_huffbits
, 1, 1,
331 vlc_tab_tone_level_idx_hi2_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
333 init_vlc (&vlc_tab_type30
, 6, 9,
334 vlc_tab_type30_huffbits
, 1, 1,
335 vlc_tab_type30_huffcodes
, 1, 1, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
337 init_vlc (&vlc_tab_type34
, 5, 10,
338 vlc_tab_type34_huffbits
, 1, 1,
339 vlc_tab_type34_huffcodes
, 1, 1, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
341 init_vlc (&vlc_tab_fft_tone_offset
[0], 8, 23,
342 vlc_tab_fft_tone_offset_0_huffbits
, 1, 1,
343 vlc_tab_fft_tone_offset_0_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
345 init_vlc (&vlc_tab_fft_tone_offset
[1], 8, 28,
346 vlc_tab_fft_tone_offset_1_huffbits
, 1, 1,
347 vlc_tab_fft_tone_offset_1_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
349 init_vlc (&vlc_tab_fft_tone_offset
[2], 8, 32,
350 vlc_tab_fft_tone_offset_2_huffbits
, 1, 1,
351 vlc_tab_fft_tone_offset_2_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
353 init_vlc (&vlc_tab_fft_tone_offset
[3], 8, 35,
354 vlc_tab_fft_tone_offset_3_huffbits
, 1, 1,
355 vlc_tab_fft_tone_offset_3_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
357 init_vlc (&vlc_tab_fft_tone_offset
[4], 8, 38,
358 vlc_tab_fft_tone_offset_4_huffbits
, 1, 1,
359 vlc_tab_fft_tone_offset_4_huffcodes
, 2, 2, INIT_VLC_USE_STATIC
| INIT_VLC_LE
);
363 /* for floating point to fixed point conversion */
364 static float f2i_scale
= (float) (1 << (FRAC_BITS
- 15));
367 static int qdm2_get_vlc (GetBitContext
*gb
, VLC
*vlc
, int flag
, int depth
)
371 value
= get_vlc2(gb
, vlc
->table
, vlc
->bits
, depth
);
373 /* stage-2, 3 bits exponent escape sequence */
375 value
= get_bits (gb
, get_bits (gb
, 3) + 1);
377 /* stage-3, optional */
379 int tmp
= vlc_stage3_values
[value
];
381 if ((value
& ~3) > 0)
382 tmp
+= get_bits (gb
, (value
>> 2));
390 static int qdm2_get_se_vlc (VLC
*vlc
, GetBitContext
*gb
, int depth
)
392 int value
= qdm2_get_vlc (gb
, vlc
, 0, depth
);
394 return (value
& 1) ? ((value
+ 1) >> 1) : -(value
>> 1);
401 * @param data pointer to data to be checksum'ed
402 * @param length data length
403 * @param value checksum value
405 * @return 0 if checksum is OK
407 static uint16_t qdm2_packet_checksum (const uint8_t *data
, int length
, int value
) {
410 for (i
=0; i
< length
; i
++)
413 return (uint16_t)(value
& 0xffff);
418 * Fills a QDM2SubPacket structure with packet type, size, and data pointer.
420 * @param gb bitreader context
421 * @param sub_packet packet under analysis
423 static void qdm2_decode_sub_packet_header (GetBitContext
*gb
, QDM2SubPacket
*sub_packet
)
425 sub_packet
->type
= get_bits (gb
, 8);
427 if (sub_packet
->type
== 0) {
428 sub_packet
->size
= 0;
429 sub_packet
->data
= NULL
;
431 sub_packet
->size
= get_bits (gb
, 8);
433 if (sub_packet
->type
& 0x80) {
434 sub_packet
->size
<<= 8;
435 sub_packet
->size
|= get_bits (gb
, 8);
436 sub_packet
->type
&= 0x7f;
439 if (sub_packet
->type
== 0x7f)
440 sub_packet
->type
|= (get_bits (gb
, 8) << 8);
442 sub_packet
->data
= &gb
->buffer
[get_bits_count(gb
) / 8]; // FIXME: this depends on bitreader internal data
445 av_log(NULL
,AV_LOG_DEBUG
,"Subpacket: type=%d size=%d start_offs=%x\n",
446 sub_packet
->type
, sub_packet
->size
, get_bits_count(gb
) / 8);
451 * Return node pointer to first packet of requested type in list.
453 * @param list list of subpackets to be scanned
454 * @param type type of searched subpacket
455 * @return node pointer for subpacket if found, else NULL
457 static QDM2SubPNode
* qdm2_search_subpacket_type_in_list (QDM2SubPNode
*list
, int type
)
459 while (list
!= NULL
&& list
->packet
!= NULL
) {
460 if (list
->packet
->type
== type
)
469 * Replaces 8 elements with their average value.
470 * Called by qdm2_decode_superblock before starting subblock decoding.
474 static void average_quantized_coeffs (QDM2Context
*q
)
476 int i
, j
, n
, ch
, sum
;
478 n
= coeff_per_sb_for_avg
[q
->coeff_per_sb_select
][QDM2_SB_USED(q
->sub_sampling
) - 1] + 1;
480 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
481 for (i
= 0; i
< n
; i
++) {
484 for (j
= 0; j
< 8; j
++)
485 sum
+= q
->quantized_coeffs
[ch
][i
][j
];
491 for (j
=0; j
< 8; j
++)
492 q
->quantized_coeffs
[ch
][i
][j
] = sum
;
498 * Build subband samples with noise weighted by q->tone_level.
499 * Called by synthfilt_build_sb_samples.
502 * @param sb subband index
504 static void build_sb_samples_from_noise (QDM2Context
*q
, int sb
)
508 FIX_NOISE_IDX(q
->noise_idx
);
513 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
514 for (j
= 0; j
< 64; j
++) {
515 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);
516 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);
522 * Called while processing data from subpackets 11 and 12.
523 * Used after making changes to coding_method array.
525 * @param sb subband index
526 * @param channels number of channels
527 * @param coding_method q->coding_method[0][0][0]
529 static void fix_coding_method_array (int sb
, int channels
, sb_int8_array coding_method
)
534 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};
536 for (ch
= 0; ch
< channels
; ch
++) {
537 for (j
= 0; j
< 64; ) {
538 if((coding_method
[ch
][sb
][j
] - 8) > 22) {
542 switch (switchtable
[coding_method
[ch
][sb
][j
]-8]) {
543 case 0: run
= 10; case_val
= 10; break;
544 case 1: run
= 1; case_val
= 16; break;
545 case 2: run
= 5; case_val
= 24; break;
546 case 3: run
= 3; case_val
= 30; break;
547 case 4: run
= 1; case_val
= 30; break;
548 case 5: run
= 1; case_val
= 8; break;
549 default: run
= 1; case_val
= 8; break;
552 for (k
= 0; k
< run
; k
++)
554 if (coding_method
[ch
][sb
+ (j
+ k
) / 64][(j
+ k
) % 64] > coding_method
[ch
][sb
][j
])
557 //not debugged, almost never used
558 memset(&coding_method
[ch
][sb
][j
+ k
], case_val
, k
* sizeof(int8_t));
559 memset(&coding_method
[ch
][sb
][j
+ k
], case_val
, 3 * sizeof(int8_t));
568 * Related to synthesis filter
569 * Called by process_subpacket_10
572 * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10
574 static void fill_tone_level_array (QDM2Context
*q
, int flag
)
576 int i
, sb
, ch
, sb_used
;
579 // This should never happen
580 if (q
->nb_channels
<= 0)
583 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
584 for (sb
= 0; sb
< 30; sb
++)
585 for (i
= 0; i
< 8; i
++) {
586 if ((tab
=coeff_per_sb_for_dequant
[q
->coeff_per_sb_select
][sb
]) < (last_coeff
[q
->coeff_per_sb_select
] - 1))
587 tmp
= q
->quantized_coeffs
[ch
][tab
+ 1][i
] * dequant_table
[q
->coeff_per_sb_select
][tab
+ 1][sb
]+
588 q
->quantized_coeffs
[ch
][tab
][i
] * dequant_table
[q
->coeff_per_sb_select
][tab
][sb
];
590 tmp
= q
->quantized_coeffs
[ch
][tab
][i
] * dequant_table
[q
->coeff_per_sb_select
][tab
][sb
];
593 q
->tone_level_idx_base
[ch
][sb
][i
] = (tmp
/ 256) & 0xff;
596 sb_used
= QDM2_SB_USED(q
->sub_sampling
);
598 if ((q
->superblocktype_2_3
!= 0) && !flag
) {
599 for (sb
= 0; sb
< sb_used
; sb
++)
600 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
601 for (i
= 0; i
< 64; i
++) {
602 q
->tone_level_idx
[ch
][sb
][i
] = q
->tone_level_idx_base
[ch
][sb
][i
/ 8];
603 if (q
->tone_level_idx
[ch
][sb
][i
] < 0)
604 q
->tone_level
[ch
][sb
][i
] = 0;
606 q
->tone_level
[ch
][sb
][i
] = fft_tone_level_table
[0][q
->tone_level_idx
[ch
][sb
][i
] & 0x3f];
609 tab
= q
->superblocktype_2_3
? 0 : 1;
610 for (sb
= 0; sb
< sb_used
; sb
++) {
611 if ((sb
>= 4) && (sb
<= 23)) {
612 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
613 for (i
= 0; i
< 64; i
++) {
614 tmp
= q
->tone_level_idx_base
[ch
][sb
][i
/ 8] -
615 q
->tone_level_idx_hi1
[ch
][sb
/ 8][i
/ 8][i
% 8] -
616 q
->tone_level_idx_mid
[ch
][sb
- 4][i
/ 8] -
617 q
->tone_level_idx_hi2
[ch
][sb
- 4];
618 q
->tone_level_idx
[ch
][sb
][i
] = tmp
& 0xff;
619 if ((tmp
< 0) || (!q
->superblocktype_2_3
&& !tmp
))
620 q
->tone_level
[ch
][sb
][i
] = 0;
622 q
->tone_level
[ch
][sb
][i
] = fft_tone_level_table
[tab
][tmp
& 0x3f];
626 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
627 for (i
= 0; i
< 64; i
++) {
628 tmp
= q
->tone_level_idx_base
[ch
][sb
][i
/ 8] -
629 q
->tone_level_idx_hi1
[ch
][2][i
/ 8][i
% 8] -
630 q
->tone_level_idx_hi2
[ch
][sb
- 4];
631 q
->tone_level_idx
[ch
][sb
][i
] = tmp
& 0xff;
632 if ((tmp
< 0) || (!q
->superblocktype_2_3
&& !tmp
))
633 q
->tone_level
[ch
][sb
][i
] = 0;
635 q
->tone_level
[ch
][sb
][i
] = fft_tone_level_table
[tab
][tmp
& 0x3f];
638 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
639 for (i
= 0; i
< 64; i
++) {
640 tmp
= q
->tone_level_idx
[ch
][sb
][i
] = q
->tone_level_idx_base
[ch
][sb
][i
/ 8];
641 if ((tmp
< 0) || (!q
->superblocktype_2_3
&& !tmp
))
642 q
->tone_level
[ch
][sb
][i
] = 0;
644 q
->tone_level
[ch
][sb
][i
] = fft_tone_level_table
[tab
][tmp
& 0x3f];
656 * Related to synthesis filter
657 * Called by process_subpacket_11
658 * c is built with data from subpacket 11
659 * Most of this function is used only if superblock_type_2_3 == 0, never seen it in samples
661 * @param tone_level_idx
662 * @param tone_level_idx_temp
663 * @param coding_method q->coding_method[0][0][0]
664 * @param nb_channels number of channels
665 * @param c coming from subpacket 11, passed as 8*c
666 * @param superblocktype_2_3 flag based on superblock packet type
667 * @param cm_table_select q->cm_table_select
669 static void fill_coding_method_array (sb_int8_array tone_level_idx
, sb_int8_array tone_level_idx_temp
,
670 sb_int8_array coding_method
, int nb_channels
,
671 int c
, int superblocktype_2_3
, int cm_table_select
)
674 int tmp
, acc
, esp_40
, comp
;
675 int add1
, add2
, add3
, add4
;
678 // This should never happen
679 if (nb_channels
<= 0)
682 if (!superblocktype_2_3
) {
683 /* This case is untested, no samples available */
685 for (ch
= 0; ch
< nb_channels
; ch
++)
686 for (sb
= 0; sb
< 30; sb
++) {
687 for (j
= 1; j
< 64; j
++) {
688 add1
= tone_level_idx
[ch
][sb
][j
] - 10;
691 add2
= add3
= add4
= 0;
693 add2
= tone_level_idx
[ch
][sb
- 2][j
] + tone_level_idx_offset_table
[sb
][0] - 6;
698 add3
= tone_level_idx
[ch
][sb
- 1][j
] + tone_level_idx_offset_table
[sb
][1] - 6;
703 add4
= tone_level_idx
[ch
][sb
+ 1][j
] + tone_level_idx_offset_table
[sb
][3] - 6;
707 tmp
= tone_level_idx
[ch
][sb
][j
+ 1] * 2 - add4
- add3
- add2
- add1
;
710 tone_level_idx_temp
[ch
][sb
][j
+ 1] = tmp
& 0xff;
712 tone_level_idx_temp
[ch
][sb
][0] = tone_level_idx_temp
[ch
][sb
][1];
715 for (ch
= 0; ch
< nb_channels
; ch
++)
716 for (sb
= 0; sb
< 30; sb
++)
717 for (j
= 0; j
< 64; j
++)
718 acc
+= tone_level_idx_temp
[ch
][sb
][j
];
720 tmp
= c
* 256 / (acc
& 0xffff);
721 multres
= 0x66666667 * (acc
* 10);
722 esp_40
= (multres
>> 32) / 8 + ((multres
& 0xffffffff) >> 31);
723 for (ch
= 0; ch
< nb_channels
; ch
++)
724 for (sb
= 0; sb
< 30; sb
++)
725 for (j
= 0; j
< 64; j
++) {
726 comp
= tone_level_idx_temp
[ch
][sb
][j
]* esp_40
* 10;
729 comp
/= 256; // signed shift
757 coding_method
[ch
][sb
][j
] = ((tmp
& 0xfffa) + 30 )& 0xff;
759 for (sb
= 0; sb
< 30; sb
++)
760 fix_coding_method_array(sb
, nb_channels
, coding_method
);
761 for (ch
= 0; ch
< nb_channels
; ch
++)
762 for (sb
= 0; sb
< 30; sb
++)
763 for (j
= 0; j
< 64; j
++)
765 if (coding_method
[ch
][sb
][j
] < 10)
766 coding_method
[ch
][sb
][j
] = 10;
769 if (coding_method
[ch
][sb
][j
] < 16)
770 coding_method
[ch
][sb
][j
] = 16;
772 if (coding_method
[ch
][sb
][j
] < 30)
773 coding_method
[ch
][sb
][j
] = 30;
776 } else { // superblocktype_2_3 != 0
777 for (ch
= 0; ch
< nb_channels
; ch
++)
778 for (sb
= 0; sb
< 30; sb
++)
779 for (j
= 0; j
< 64; j
++)
780 coding_method
[ch
][sb
][j
] = coding_method_table
[cm_table_select
][sb
];
789 * Called by process_subpacket_11 to process more data from subpacket 11 with sb 0-8
790 * Called by process_subpacket_12 to process data from subpacket 12 with sb 8-sb_used
793 * @param gb bitreader context
794 * @param length packet length in bits
795 * @param sb_min lower subband processed (sb_min included)
796 * @param sb_max higher subband processed (sb_max excluded)
798 static void synthfilt_build_sb_samples (QDM2Context
*q
, GetBitContext
*gb
, int length
, int sb_min
, int sb_max
)
800 int sb
, j
, k
, n
, ch
, run
, channels
;
801 int joined_stereo
, zero_encoding
, chs
;
803 float type34_div
= 0;
804 float type34_predictor
;
805 float samples
[10], sign_bits
[16];
808 // If no data use noise
809 for (sb
=sb_min
; sb
< sb_max
; sb
++)
810 build_sb_samples_from_noise (q
, sb
);
815 for (sb
= sb_min
; sb
< sb_max
; sb
++) {
816 FIX_NOISE_IDX(q
->noise_idx
);
818 channels
= q
->nb_channels
;
820 if (q
->nb_channels
<= 1 || sb
< 12)
825 joined_stereo
= (BITS_LEFT(length
,gb
) >= 1) ? get_bits1 (gb
) : 0;
828 if (BITS_LEFT(length
,gb
) >= 16)
829 for (j
= 0; j
< 16; j
++)
830 sign_bits
[j
] = get_bits1 (gb
);
832 for (j
= 0; j
< 64; j
++)
833 if (q
->coding_method
[1][sb
][j
] > q
->coding_method
[0][sb
][j
])
834 q
->coding_method
[0][sb
][j
] = q
->coding_method
[1][sb
][j
];
836 fix_coding_method_array(sb
, q
->nb_channels
, q
->coding_method
);
840 for (ch
= 0; ch
< channels
; ch
++) {
841 zero_encoding
= (BITS_LEFT(length
,gb
) >= 1) ? get_bits1(gb
) : 0;
842 type34_predictor
= 0.0;
845 for (j
= 0; j
< 128; ) {
846 switch (q
->coding_method
[ch
][sb
][j
/ 2]) {
848 if (BITS_LEFT(length
,gb
) >= 10) {
850 for (k
= 0; k
< 5; k
++) {
851 if ((j
+ 2 * k
) >= 128)
853 samples
[2 * k
] = get_bits1(gb
) ? dequant_1bit
[joined_stereo
][2 * get_bits1(gb
)] : 0;
857 for (k
= 0; k
< 5; k
++)
858 samples
[2 * k
] = dequant_1bit
[joined_stereo
][random_dequant_index
[n
][k
]];
860 for (k
= 0; k
< 5; k
++)
861 samples
[2 * k
+ 1] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
863 for (k
= 0; k
< 10; k
++)
864 samples
[k
] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
870 if (BITS_LEFT(length
,gb
) >= 1) {
875 f
-= noise_samples
[((sb
+ 1) * (j
+5 * ch
+ 1)) & 127] * 9.0 / 40.0;
878 samples
[0] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
884 if (BITS_LEFT(length
,gb
) >= 10) {
886 for (k
= 0; k
< 5; k
++) {
889 samples
[k
] = (get_bits1(gb
) == 0) ? 0 : dequant_1bit
[joined_stereo
][2 * get_bits1(gb
)];
892 n
= get_bits (gb
, 8);
893 for (k
= 0; k
< 5; k
++)
894 samples
[k
] = dequant_1bit
[joined_stereo
][random_dequant_index
[n
][k
]];
897 for (k
= 0; k
< 5; k
++)
898 samples
[k
] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
904 if (BITS_LEFT(length
,gb
) >= 7) {
906 for (k
= 0; k
< 3; k
++)
907 samples
[k
] = (random_dequant_type24
[n
][k
] - 2.0) * 0.5;
909 for (k
= 0; k
< 3; k
++)
910 samples
[k
] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
916 if (BITS_LEFT(length
,gb
) >= 4)
917 samples
[0] = type30_dequant
[qdm2_get_vlc(gb
, &vlc_tab_type30
, 0, 1)];
919 samples
[0] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
925 if (BITS_LEFT(length
,gb
) >= 7) {
927 type34_div
= (float)(1 << get_bits(gb
, 2));
928 samples
[0] = ((float)get_bits(gb
, 5) - 16.0) / 15.0;
929 type34_predictor
= samples
[0];
932 samples
[0] = type34_delta
[qdm2_get_vlc(gb
, &vlc_tab_type34
, 0, 1)] / type34_div
+ type34_predictor
;
933 type34_predictor
= samples
[0];
936 samples
[0] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
942 samples
[0] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
948 float tmp
[10][MPA_MAX_CHANNELS
];
950 for (k
= 0; k
< run
; k
++) {
951 tmp
[k
][0] = samples
[k
];
952 tmp
[k
][1] = (sign_bits
[(j
+ k
) / 8]) ? -samples
[k
] : samples
[k
];
954 for (chs
= 0; chs
< q
->nb_channels
; chs
++)
955 for (k
= 0; k
< run
; k
++)
957 q
->sb_samples
[chs
][j
+ k
][sb
] = (int32_t)(f2i_scale
* q
->tone_level
[chs
][sb
][((j
+ k
)/2)] * tmp
[k
][chs
] + .5);
959 for (k
= 0; k
< run
; k
++)
961 q
->sb_samples
[ch
][j
+ k
][sb
] = (int32_t)(f2i_scale
* q
->tone_level
[ch
][sb
][(j
+ k
)/2] * samples
[k
] + .5);
972 * Init the first element of a channel in quantized_coeffs with data from packet 10 (quantized_coeffs[ch][0]).
973 * This is similar to process_subpacket_9, but for a single channel and for element [0]
974 * same VLC tables as process_subpacket_9 are used.
977 * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
978 * @param gb bitreader context
979 * @param length packet length in bits
981 static void init_quantized_coeffs_elem0 (int8_t *quantized_coeffs
, GetBitContext
*gb
, int length
)
983 int i
, k
, run
, level
, diff
;
985 if (BITS_LEFT(length
,gb
) < 16)
987 level
= qdm2_get_vlc(gb
, &vlc_tab_level
, 0, 2);
989 quantized_coeffs
[0] = level
;
991 for (i
= 0; i
< 7; ) {
992 if (BITS_LEFT(length
,gb
) < 16)
994 run
= qdm2_get_vlc(gb
, &vlc_tab_run
, 0, 1) + 1;
996 if (BITS_LEFT(length
,gb
) < 16)
998 diff
= qdm2_get_se_vlc(&vlc_tab_diff
, gb
, 2);
1000 for (k
= 1; k
<= run
; k
++)
1001 quantized_coeffs
[i
+ k
] = (level
+ ((k
* diff
) / run
));
1010 * Related to synthesis filter, process data from packet 10
1011 * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
1012 * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with data from packet 10
1015 * @param gb bitreader context
1016 * @param length packet length in bits
1018 static void init_tone_level_dequantization (QDM2Context
*q
, GetBitContext
*gb
, int length
)
1020 int sb
, j
, k
, n
, ch
;
1022 for (ch
= 0; ch
< q
->nb_channels
; ch
++) {
1023 init_quantized_coeffs_elem0(q
->quantized_coeffs
[ch
][0], gb
, length
);
1025 if (BITS_LEFT(length
,gb
) < 16) {
1026 memset(q
->quantized_coeffs
[ch
][0], 0, 8);
1031 n
= q
->sub_sampling
+ 1;
1033 for (sb
= 0; sb
< n
; sb
++)
1034 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
1035 for (j
= 0; j
< 8; j
++) {
1036 if (BITS_LEFT(length
,gb
) < 1)
1038 if (get_bits1(gb
)) {
1039 for (k
=0; k
< 8; k
++) {
1040 if (BITS_LEFT(length
,gb
) < 16)
1042 q
->tone_level_idx_hi1
[ch
][sb
][j
][k
] = qdm2_get_vlc(gb
, &vlc_tab_tone_level_idx_hi1
, 0, 2);
1045 for (k
=0; k
< 8; k
++)
1046 q
->tone_level_idx_hi1
[ch
][sb
][j
][k
] = 0;
1050 n
= QDM2_SB_USED(q
->sub_sampling
) - 4;
1052 for (sb
= 0; sb
< n
; sb
++)
1053 for (ch
= 0; ch
< q
->nb_channels
; ch
++) {
1054 if (BITS_LEFT(length
,gb
) < 16)
1056 q
->tone_level_idx_hi2
[ch
][sb
] = qdm2_get_vlc(gb
, &vlc_tab_tone_level_idx_hi2
, 0, 2);
1058 q
->tone_level_idx_hi2
[ch
][sb
] -= 16;
1060 for (j
= 0; j
< 8; j
++)
1061 q
->tone_level_idx_mid
[ch
][sb
][j
] = -16;
1064 n
= QDM2_SB_USED(q
->sub_sampling
) - 5;
1066 for (sb
= 0; sb
< n
; sb
++)
1067 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
1068 for (j
= 0; j
< 8; j
++) {
1069 if (BITS_LEFT(length
,gb
) < 16)
1071 q
->tone_level_idx_mid
[ch
][sb
][j
] = qdm2_get_vlc(gb
, &vlc_tab_tone_level_idx_mid
, 0, 2) - 32;
1076 * Process subpacket 9, init quantized_coeffs with data from it
1079 * @param node pointer to node with packet
1081 static void process_subpacket_9 (QDM2Context
*q
, QDM2SubPNode
*node
)
1084 int i
, j
, k
, n
, ch
, run
, level
, diff
;
1086 init_get_bits(&gb
, node
->packet
->data
, node
->packet
->size
*8);
1088 n
= coeff_per_sb_for_avg
[q
->coeff_per_sb_select
][QDM2_SB_USED(q
->sub_sampling
) - 1] + 1; // same as averagesomething function
1090 for (i
= 1; i
< n
; i
++)
1091 for (ch
=0; ch
< q
->nb_channels
; ch
++) {
1092 level
= qdm2_get_vlc(&gb
, &vlc_tab_level
, 0, 2);
1093 q
->quantized_coeffs
[ch
][i
][0] = level
;
1095 for (j
= 0; j
< (8 - 1); ) {
1096 run
= qdm2_get_vlc(&gb
, &vlc_tab_run
, 0, 1) + 1;
1097 diff
= qdm2_get_se_vlc(&vlc_tab_diff
, &gb
, 2);
1099 for (k
= 1; k
<= run
; k
++)
1100 q
->quantized_coeffs
[ch
][i
][j
+ k
] = (level
+ ((k
*diff
) / run
));
1107 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
1108 for (i
= 0; i
< 8; i
++)
1109 q
->quantized_coeffs
[ch
][0][i
] = 0;
1114 * Process subpacket 10 if not null, else
1117 * @param node pointer to node with packet
1118 * @param length packet length in bits
1120 static void process_subpacket_10 (QDM2Context
*q
, QDM2SubPNode
*node
, int length
)
1124 init_get_bits(&gb
, ((node
== NULL
) ? empty_buffer
: node
->packet
->data
), ((node
== NULL
) ? 0 : node
->packet
->size
*8));
1127 init_tone_level_dequantization(q
, &gb
, length
);
1128 fill_tone_level_array(q
, 1);
1130 fill_tone_level_array(q
, 0);
1136 * Process subpacket 11
1139 * @param node pointer to node with packet
1140 * @param length packet length in bit
1142 static void process_subpacket_11 (QDM2Context
*q
, QDM2SubPNode
*node
, int length
)
1146 init_get_bits(&gb
, ((node
== NULL
) ? empty_buffer
: node
->packet
->data
), ((node
== NULL
) ? 0 : node
->packet
->size
*8));
1148 int c
= get_bits (&gb
, 13);
1151 fill_coding_method_array (q
->tone_level_idx
, q
->tone_level_idx_temp
, q
->coding_method
,
1152 q
->nb_channels
, 8*c
, q
->superblocktype_2_3
, q
->cm_table_select
);
1155 synthfilt_build_sb_samples(q
, &gb
, length
, 0, 8);
1160 * Process subpacket 12
1163 * @param node pointer to node with packet
1164 * @param length packet length in bits
1166 static void process_subpacket_12 (QDM2Context
*q
, QDM2SubPNode
*node
, int length
)
1170 init_get_bits(&gb
, ((node
== NULL
) ? empty_buffer
: node
->packet
->data
), ((node
== NULL
) ? 0 : node
->packet
->size
*8));
1171 synthfilt_build_sb_samples(q
, &gb
, length
, 8, QDM2_SB_USED(q
->sub_sampling
));
1175 * Process new subpackets for synthesis filter
1178 * @param list list with synthesis filter packets (list D)
1180 static void process_synthesis_subpackets (QDM2Context
*q
, QDM2SubPNode
*list
)
1182 QDM2SubPNode
*nodes
[4];
1184 nodes
[0] = qdm2_search_subpacket_type_in_list(list
, 9);
1185 if (nodes
[0] != NULL
)
1186 process_subpacket_9(q
, nodes
[0]);
1188 nodes
[1] = qdm2_search_subpacket_type_in_list(list
, 10);
1189 if (nodes
[1] != NULL
)
1190 process_subpacket_10(q
, nodes
[1], nodes
[1]->packet
->size
<< 3);
1192 process_subpacket_10(q
, NULL
, 0);
1194 nodes
[2] = qdm2_search_subpacket_type_in_list(list
, 11);
1195 if (nodes
[0] != NULL
&& nodes
[1] != NULL
&& nodes
[2] != NULL
)
1196 process_subpacket_11(q
, nodes
[2], (nodes
[2]->packet
->size
<< 3));
1198 process_subpacket_11(q
, NULL
, 0);
1200 nodes
[3] = qdm2_search_subpacket_type_in_list(list
, 12);
1201 if (nodes
[0] != NULL
&& nodes
[1] != NULL
&& nodes
[3] != NULL
)
1202 process_subpacket_12(q
, nodes
[3], (nodes
[3]->packet
->size
<< 3));
1204 process_subpacket_12(q
, NULL
, 0);
1209 * Decode superblock, fill packet lists.
1213 static void qdm2_decode_super_block (QDM2Context
*q
)
1216 QDM2SubPacket header
, *packet
;
1217 int i
, packet_bytes
, sub_packet_size
, sub_packets_D
;
1218 unsigned int next_index
= 0;
1220 memset(q
->tone_level_idx_hi1
, 0, sizeof(q
->tone_level_idx_hi1
));
1221 memset(q
->tone_level_idx_mid
, 0, sizeof(q
->tone_level_idx_mid
));
1222 memset(q
->tone_level_idx_hi2
, 0, sizeof(q
->tone_level_idx_hi2
));
1224 q
->sub_packets_B
= 0;
1227 average_quantized_coeffs(q
); // average elements in quantized_coeffs[max_ch][10][8]
1229 init_get_bits(&gb
, q
->compressed_data
, q
->compressed_size
*8);
1230 qdm2_decode_sub_packet_header(&gb
, &header
);
1232 if (header
.type
< 2 || header
.type
>= 8) {
1234 av_log(NULL
,AV_LOG_ERROR
,"bad superblock type\n");
1238 q
->superblocktype_2_3
= (header
.type
== 2 || header
.type
== 3);
1239 packet_bytes
= (q
->compressed_size
- get_bits_count(&gb
) / 8);
1241 init_get_bits(&gb
, header
.data
, header
.size
*8);
1243 if (header
.type
== 2 || header
.type
== 4 || header
.type
== 5) {
1244 int csum
= 257 * get_bits(&gb
, 8) + 2 * get_bits(&gb
, 8);
1246 csum
= qdm2_packet_checksum(q
->compressed_data
, q
->checksum_size
, csum
);
1250 av_log(NULL
,AV_LOG_ERROR
,"bad packet checksum\n");
1255 q
->sub_packet_list_B
[0].packet
= NULL
;
1256 q
->sub_packet_list_D
[0].packet
= NULL
;
1258 for (i
= 0; i
< 6; i
++)
1259 if (--q
->fft_level_exp
[i
] < 0)
1260 q
->fft_level_exp
[i
] = 0;
1262 for (i
= 0; packet_bytes
> 0; i
++) {
1265 q
->sub_packet_list_A
[i
].next
= NULL
;
1268 q
->sub_packet_list_A
[i
- 1].next
= &q
->sub_packet_list_A
[i
];
1270 /* seek to next block */
1271 init_get_bits(&gb
, header
.data
, header
.size
*8);
1272 skip_bits(&gb
, next_index
*8);
1274 if (next_index
>= header
.size
)
1278 /* decode subpacket */
1279 packet
= &q
->sub_packets
[i
];
1280 qdm2_decode_sub_packet_header(&gb
, packet
);
1281 next_index
= packet
->size
+ get_bits_count(&gb
) / 8;
1282 sub_packet_size
= ((packet
->size
> 0xff) ? 1 : 0) + packet
->size
+ 2;
1284 if (packet
->type
== 0)
1287 if (sub_packet_size
> packet_bytes
) {
1288 if (packet
->type
!= 10 && packet
->type
!= 11 && packet
->type
!= 12)
1290 packet
->size
+= packet_bytes
- sub_packet_size
;
1293 packet_bytes
-= sub_packet_size
;
1295 /* add subpacket to 'all subpackets' list */
1296 q
->sub_packet_list_A
[i
].packet
= packet
;
1298 /* add subpacket to related list */
1299 if (packet
->type
== 8) {
1300 SAMPLES_NEEDED_2("packet type 8");
1302 } else if (packet
->type
>= 9 && packet
->type
<= 12) {
1303 /* packets for MPEG Audio like Synthesis Filter */
1304 QDM2_LIST_ADD(q
->sub_packet_list_D
, sub_packets_D
, packet
);
1305 } else if (packet
->type
== 13) {
1306 for (j
= 0; j
< 6; j
++)
1307 q
->fft_level_exp
[j
] = get_bits(&gb
, 6);
1308 } else if (packet
->type
== 14) {
1309 for (j
= 0; j
< 6; j
++)
1310 q
->fft_level_exp
[j
] = qdm2_get_vlc(&gb
, &fft_level_exp_vlc
, 0, 2);
1311 } else if (packet
->type
== 15) {
1312 SAMPLES_NEEDED_2("packet type 15")
1314 } else if (packet
->type
>= 16 && packet
->type
< 48 && !fft_subpackets
[packet
->type
- 16]) {
1315 /* packets for FFT */
1316 QDM2_LIST_ADD(q
->sub_packet_list_B
, q
->sub_packets_B
, packet
);
1318 } // Packet bytes loop
1320 /* **************************************************************** */
1321 if (q
->sub_packet_list_D
[0].packet
!= NULL
) {
1322 process_synthesis_subpackets(q
, q
->sub_packet_list_D
);
1323 q
->do_synth_filter
= 1;
1324 } else if (q
->do_synth_filter
) {
1325 process_subpacket_10(q
, NULL
, 0);
1326 process_subpacket_11(q
, NULL
, 0);
1327 process_subpacket_12(q
, NULL
, 0);
1329 /* **************************************************************** */
1333 static void qdm2_fft_init_coefficient (QDM2Context
*q
, int sub_packet
,
1334 int offset
, int duration
, int channel
,
1337 if (q
->fft_coefs_min_index
[duration
] < 0)
1338 q
->fft_coefs_min_index
[duration
] = q
->fft_coefs_index
;
1340 q
->fft_coefs
[q
->fft_coefs_index
].sub_packet
= ((sub_packet
>= 16) ? (sub_packet
- 16) : sub_packet
);
1341 q
->fft_coefs
[q
->fft_coefs_index
].channel
= channel
;
1342 q
->fft_coefs
[q
->fft_coefs_index
].offset
= offset
;
1343 q
->fft_coefs
[q
->fft_coefs_index
].exp
= exp
;
1344 q
->fft_coefs
[q
->fft_coefs_index
].phase
= phase
;
1345 q
->fft_coefs_index
++;
1349 static void qdm2_fft_decode_tones (QDM2Context
*q
, int duration
, GetBitContext
*gb
, int b
)
1351 int channel
, stereo
, phase
, exp
;
1352 int local_int_4
, local_int_8
, stereo_phase
, local_int_10
;
1353 int local_int_14
, stereo_exp
, local_int_20
, local_int_28
;
1359 local_int_8
= (4 - duration
);
1360 local_int_10
= 1 << (q
->group_order
- duration
- 1);
1364 if (q
->superblocktype_2_3
) {
1365 while ((n
= qdm2_get_vlc(gb
, &vlc_tab_fft_tone_offset
[local_int_8
], 1, 2)) < 2) {
1368 local_int_4
+= local_int_10
;
1369 local_int_28
+= (1 << local_int_8
);
1371 local_int_4
+= 8*local_int_10
;
1372 local_int_28
+= (8 << local_int_8
);
1377 offset
+= qdm2_get_vlc(gb
, &vlc_tab_fft_tone_offset
[local_int_8
], 1, 2);
1378 while (offset
>= (local_int_10
- 1)) {
1379 offset
+= (1 - (local_int_10
- 1));
1380 local_int_4
+= local_int_10
;
1381 local_int_28
+= (1 << local_int_8
);
1385 if (local_int_4
>= q
->group_size
)
1388 local_int_14
= (offset
>> local_int_8
);
1390 if (q
->nb_channels
> 1) {
1391 channel
= get_bits1(gb
);
1392 stereo
= get_bits1(gb
);
1398 exp
= qdm2_get_vlc(gb
, (b
? &fft_level_exp_vlc
: &fft_level_exp_alt_vlc
), 0, 2);
1399 exp
+= q
->fft_level_exp
[fft_level_index_table
[local_int_14
]];
1400 exp
= (exp
< 0) ? 0 : exp
;
1402 phase
= get_bits(gb
, 3);
1407 stereo_exp
= (exp
- qdm2_get_vlc(gb
, &fft_stereo_exp_vlc
, 0, 1));
1408 stereo_phase
= (phase
- qdm2_get_vlc(gb
, &fft_stereo_phase_vlc
, 0, 1));
1409 if (stereo_phase
< 0)
1413 if (q
->frequency_range
> (local_int_14
+ 1)) {
1414 int sub_packet
= (local_int_20
+ local_int_28
);
1416 qdm2_fft_init_coefficient(q
, sub_packet
, offset
, duration
, channel
, exp
, phase
);
1418 qdm2_fft_init_coefficient(q
, sub_packet
, offset
, duration
, (1 - channel
), stereo_exp
, stereo_phase
);
1426 static void qdm2_decode_fft_packets (QDM2Context
*q
)
1428 int i
, j
, min
, max
, value
, type
, unknown_flag
;
1431 if (q
->sub_packet_list_B
[0].packet
== NULL
)
1434 /* reset minimum indices for FFT coefficients */
1435 q
->fft_coefs_index
= 0;
1436 for (i
=0; i
< 5; i
++)
1437 q
->fft_coefs_min_index
[i
] = -1;
1439 /* process subpackets ordered by type, largest type first */
1440 for (i
= 0, max
= 256; i
< q
->sub_packets_B
; i
++) {
1441 QDM2SubPacket
*packet
;
1443 /* find subpacket with largest type less than max */
1444 for (j
= 0, min
= 0, packet
= NULL
; j
< q
->sub_packets_B
; j
++) {
1445 value
= q
->sub_packet_list_B
[j
].packet
->type
;
1446 if (value
> min
&& value
< max
) {
1448 packet
= q
->sub_packet_list_B
[j
].packet
;
1454 /* 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 indices 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
->samples_im
[0] += c
.im
;
1513 tone
->samples_re
[0] += c
.re
;
1514 tone
->samples_im
[1] -= c
.im
;
1515 tone
->samples_re
[1] -= 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
->samples_re
[fft_cutoff_index_table
[tone
->cutoff
][i
]] += c
.re
* f
[i
];
1525 tone
->samples_im
[fft_cutoff_index_table
[tone
->cutoff
][i
]] += c
.im
*((tone
->cutoff
<= i
) ? -f
[i
] : f
[i
]);
1527 for (i
= 0; i
< 4; i
++) {
1528 tone
->samples_re
[i
] += c
.re
* f
[i
+2];
1529 tone
->samples_im
[i
] += 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
.samples_im
[ch
], 0, q
->fft_size
* sizeof(float));
1548 memset(q
->fft
.samples_re
[ch
], 0, q
->fft_size
* sizeof(float));
1552 /* apply FFT tones with duration 4 (1 FFT period) */
1553 if (q
->fft_coefs_min_index
[4] >= 0)
1554 for (i
= q
->fft_coefs_min_index
[4]; i
< q
->fft_coefs_max_index
[4]; i
++) {
1558 if (q
->fft_coefs
[i
].sub_packet
!= sub_packet
)
1561 ch
= (q
->channels
== 1) ? 0 : q
->fft_coefs
[i
].channel
;
1562 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];
1564 c
.re
= level
* cos(q
->fft_coefs
[i
].phase
* iscale
);
1565 c
.im
= level
* sin(q
->fft_coefs
[i
].phase
* iscale
);
1566 q
->fft
.samples_re
[ch
][q
->fft_coefs
[i
].offset
+ 0] += c
.re
;
1567 q
->fft
.samples_im
[ch
][q
->fft_coefs
[i
].offset
+ 0] += c
.im
;
1568 q
->fft
.samples_re
[ch
][q
->fft_coefs
[i
].offset
+ 1] -= c
.re
;
1569 q
->fft
.samples_im
[ch
][q
->fft_coefs
[i
].offset
+ 1] -= c
.im
;
1572 /* generate existing FFT tones */
1573 for (i
= q
->fft_tone_end
; i
!= q
->fft_tone_start
; ) {
1574 qdm2_fft_generate_tone(q
, &q
->fft_tones
[q
->fft_tone_start
]);
1575 q
->fft_tone_start
= (q
->fft_tone_start
+ 1) % 1000;
1578 /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1579 for (i
= 0; i
< 4; i
++)
1580 if (q
->fft_coefs_min_index
[i
] >= 0) {
1581 for (j
= q
->fft_coefs_min_index
[i
]; j
< q
->fft_coefs_max_index
[i
]; j
++) {
1585 if (q
->fft_coefs
[j
].sub_packet
!= sub_packet
)
1589 offset
= q
->fft_coefs
[j
].offset
>> four_i
;
1590 ch
= (q
->channels
== 1) ? 0 : q
->fft_coefs
[j
].channel
;
1592 if (offset
< q
->frequency_range
) {
1594 tone
.cutoff
= offset
;
1596 tone
.cutoff
= (offset
>= 60) ? 3 : 2;
1598 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];
1599 tone
.samples_im
= &q
->fft
.samples_im
[ch
][offset
];
1600 tone
.samples_re
= &q
->fft
.samples_re
[ch
][offset
];
1601 tone
.table
= fft_tone_sample_table
[i
][q
->fft_coefs
[j
].offset
- (offset
<< four_i
)];
1602 tone
.phase
= 64 * q
->fft_coefs
[j
].phase
- (offset
<< 8) - 128;
1603 tone
.phase_shift
= (2 * q
->fft_coefs
[j
].offset
+ 1) << (7 - four_i
);
1605 tone
.time_index
= 0;
1607 qdm2_fft_generate_tone(q
, &tone
);
1610 q
->fft_coefs_min_index
[i
] = j
;
1615 static void qdm2_calculate_fft (QDM2Context
*q
, int channel
, int sub_packet
)
1617 const int n
= 1 << (q
->fft_order
- 1);
1618 const int n2
= n
>> 1;
1619 const float gain
= (q
->channels
== 1 && q
->nb_channels
== 2) ? 0.25f
: 0.50f
;
1620 float c
, s
, f0
, f1
, f2
, f3
;
1623 /* prerotation (or something like that) */
1624 for (i
=1; i
< n2
; i
++) {
1626 c
= q
->exptab
[i
].re
;
1627 s
= -q
->exptab
[i
].im
;
1628 f0
= (q
->fft
.samples_re
[channel
][i
] - q
->fft
.samples_re
[channel
][j
]) * gain
;
1629 f1
= (q
->fft
.samples_im
[channel
][i
] + q
->fft
.samples_im
[channel
][j
]) * gain
;
1630 f2
= (q
->fft
.samples_re
[channel
][i
] + q
->fft
.samples_re
[channel
][j
]) * gain
;
1631 f3
= (q
->fft
.samples_im
[channel
][i
] - q
->fft
.samples_im
[channel
][j
]) * gain
;
1632 q
->fft
.complex[i
].re
= s
* f0
- c
* f1
+ f2
;
1633 q
->fft
.complex[i
].im
= c
* f0
+ s
* f1
+ f3
;
1634 q
->fft
.complex[j
].re
= -s
* f0
+ c
* f1
+ f2
;
1635 q
->fft
.complex[j
].im
= c
* f0
+ s
* f1
- f3
;
1638 q
->fft
.complex[ 0].re
= q
->fft
.samples_re
[channel
][ 0] * gain
* 2.0;
1639 q
->fft
.complex[ 0].im
= q
->fft
.samples_re
[channel
][ 0] * gain
* 2.0;
1640 q
->fft
.complex[n2
].re
= q
->fft
.samples_re
[channel
][n2
] * gain
* 2.0;
1641 q
->fft
.complex[n2
].im
= -q
->fft
.samples_im
[channel
][n2
] * gain
* 2.0;
1643 ff_fft_permute(&q
->fft_ctx
, (FFTComplex
*) q
->fft
.complex);
1644 ff_fft_calc (&q
->fft_ctx
, (FFTComplex
*) q
->fft
.complex);
1645 /* add samples to output buffer */
1646 for (i
= 0; i
< ((q
->fft_frame_size
+ 15) & ~15); i
++)
1647 q
->output_buffer
[q
->channels
* i
+ channel
] += ((float *) q
->fft
.complex)[i
];
1653 * @param index subpacket number
1655 static void qdm2_synthesis_filter (QDM2Context
*q
, int index
)
1657 OUT_INT samples
[MPA_MAX_CHANNELS
* MPA_FRAME_SIZE
];
1658 int i
, k
, ch
, sb_used
, sub_sampling
, dither_state
= 0;
1660 /* copy sb_samples */
1661 sb_used
= QDM2_SB_USED(q
->sub_sampling
);
1663 for (ch
= 0; ch
< q
->channels
; ch
++)
1664 for (i
= 0; i
< 8; i
++)
1665 for (k
=sb_used
; k
< SBLIMIT
; k
++)
1666 q
->sb_samples
[ch
][(8 * index
) + i
][k
] = 0;
1668 for (ch
= 0; ch
< q
->nb_channels
; ch
++) {
1669 OUT_INT
*samples_ptr
= samples
+ ch
;
1671 for (i
= 0; i
< 8; i
++) {
1672 ff_mpa_synth_filter(q
->synth_buf
[ch
], &(q
->synth_buf_offset
[ch
]),
1673 mpa_window
, &dither_state
,
1674 samples_ptr
, q
->nb_channels
,
1675 q
->sb_samples
[ch
][(8 * index
) + i
]);
1676 samples_ptr
+= 32 * q
->nb_channels
;
1680 /* add samples to output buffer */
1681 sub_sampling
= (4 >> q
->sub_sampling
);
1683 for (ch
= 0; ch
< q
->channels
; ch
++)
1684 for (i
= 0; i
< q
->frame_size
; i
++)
1685 q
->output_buffer
[q
->channels
* i
+ ch
] += (float)(samples
[q
->nb_channels
* sub_sampling
* i
+ ch
] >> (sizeof(OUT_INT
)*8-16));
1690 * Init static data (does not depend on specific file)
1694 static void qdm2_init(QDM2Context
*q
) {
1695 static int initialized
= 0;
1697 if (initialized
!= 0)
1702 ff_mpa_synth_init(mpa_window
);
1703 softclip_table_init();
1705 init_noise_samples();
1707 av_log(NULL
, AV_LOG_DEBUG
, "init done\n");
1712 static void dump_context(QDM2Context
*q
)
1715 #define PRINT(a,b) av_log(NULL,AV_LOG_DEBUG," %s = %d\n", a, b);
1716 PRINT("compressed_data",q
->compressed_data
);
1717 PRINT("compressed_size",q
->compressed_size
);
1718 PRINT("frame_size",q
->frame_size
);
1719 PRINT("checksum_size",q
->checksum_size
);
1720 PRINT("channels",q
->channels
);
1721 PRINT("nb_channels",q
->nb_channels
);
1722 PRINT("fft_frame_size",q
->fft_frame_size
);
1723 PRINT("fft_size",q
->fft_size
);
1724 PRINT("sub_sampling",q
->sub_sampling
);
1725 PRINT("fft_order",q
->fft_order
);
1726 PRINT("group_order",q
->group_order
);
1727 PRINT("group_size",q
->group_size
);
1728 PRINT("sub_packet",q
->sub_packet
);
1729 PRINT("frequency_range",q
->frequency_range
);
1730 PRINT("has_errors",q
->has_errors
);
1731 PRINT("fft_tone_end",q
->fft_tone_end
);
1732 PRINT("fft_tone_start",q
->fft_tone_start
);
1733 PRINT("fft_coefs_index",q
->fft_coefs_index
);
1734 PRINT("coeff_per_sb_select",q
->coeff_per_sb_select
);
1735 PRINT("cm_table_select",q
->cm_table_select
);
1736 PRINT("noise_idx",q
->noise_idx
);
1738 for (i
= q
->fft_tone_start
; i
< q
->fft_tone_end
; i
++)
1740 FFTTone
*t
= &q
->fft_tones
[i
];
1742 av_log(NULL
,AV_LOG_DEBUG
,"Tone (%d) dump:\n", i
);
1743 av_log(NULL
,AV_LOG_DEBUG
," level = %f\n", t
->level
);
1744 // PRINT(" level", t->level);
1745 PRINT(" phase", t
->phase
);
1746 PRINT(" phase_shift", t
->phase_shift
);
1747 PRINT(" duration", t
->duration
);
1748 PRINT(" samples_im", t
->samples_im
);
1749 PRINT(" samples_re", t
->samples_re
);
1750 PRINT(" table", t
->table
);
1758 * Init parameters from codec extradata
1760 static int qdm2_decode_init(AVCodecContext
*avctx
)
1762 QDM2Context
*s
= avctx
->priv_data
;
1765 int tmp_val
, tmp
, size
;
1769 /* extradata parsing
1778 32 size (including this field)
1780 32 type (=QDM2 or QDMC)
1782 32 size (including this field, in bytes)
1783 32 tag (=QDCA) // maybe mandatory parameters
1786 32 samplerate (=44100)
1788 32 block size (=4096)
1789 32 frame size (=256) (for one channel)
1790 32 packet size (=1300)
1792 32 size (including this field, in bytes)
1793 32 tag (=QDCP) // maybe some tuneable parameters
1803 if (!avctx
->extradata
|| (avctx
->extradata_size
< 48)) {
1804 av_log(avctx
, AV_LOG_ERROR
, "extradata missing or truncated\n");
1808 extradata
= avctx
->extradata
;
1809 extradata_size
= avctx
->extradata_size
;
1811 while (extradata_size
> 7) {
1812 if (!memcmp(extradata
, "frmaQDM", 7))
1818 if (extradata_size
< 12) {
1819 av_log(avctx
, AV_LOG_ERROR
, "not enough extradata (%i)\n",
1824 if (memcmp(extradata
, "frmaQDM", 7)) {
1825 av_log(avctx
, AV_LOG_ERROR
, "invalid headers, QDM? not found\n");
1829 if (extradata
[7] == 'C') {
1831 av_log(avctx
, AV_LOG_ERROR
, "stream is QDMC version 1, which is not supported\n");
1836 extradata_size
-= 8;
1838 size
= AV_RB32(extradata
);
1840 if(size
> extradata_size
){
1841 av_log(avctx
, AV_LOG_ERROR
, "extradata size too small, %i < %i\n",
1842 extradata_size
, size
);
1847 av_log(avctx
, AV_LOG_DEBUG
, "size: %d\n", size
);
1848 if (AV_RB32(extradata
) != MKBETAG('Q','D','C','A')) {
1849 av_log(avctx
, AV_LOG_ERROR
, "invalid extradata, expecting QDCA\n");
1855 avctx
->channels
= s
->nb_channels
= s
->channels
= AV_RB32(extradata
);
1858 avctx
->sample_rate
= AV_RB32(extradata
);
1861 avctx
->bit_rate
= AV_RB32(extradata
);
1864 s
->group_size
= AV_RB32(extradata
);
1867 s
->fft_size
= AV_RB32(extradata
);
1870 s
->checksum_size
= AV_RB32(extradata
);
1873 s
->fft_order
= av_log2(s
->fft_size
) + 1;
1874 s
->fft_frame_size
= 2 * s
->fft_size
; // complex has two floats
1876 // something like max decodable tones
1877 s
->group_order
= av_log2(s
->group_size
) + 1;
1878 s
->frame_size
= s
->group_size
/ 16; // 16 iterations per super block
1880 s
->sub_sampling
= s
->fft_order
- 7;
1881 s
->frequency_range
= 255 / (1 << (2 - s
->sub_sampling
));
1883 switch ((s
->sub_sampling
* 2 + s
->channels
- 1)) {
1884 case 0: tmp
= 40; break;
1885 case 1: tmp
= 48; break;
1886 case 2: tmp
= 56; break;
1887 case 3: tmp
= 72; break;
1888 case 4: tmp
= 80; break;
1889 case 5: tmp
= 100;break;
1890 default: tmp
=s
->sub_sampling
; break;
1893 if ((tmp
* 1000) < avctx
->bit_rate
) tmp_val
= 1;
1894 if ((tmp
* 1440) < avctx
->bit_rate
) tmp_val
= 2;
1895 if ((tmp
* 1760) < avctx
->bit_rate
) tmp_val
= 3;
1896 if ((tmp
* 2240) < avctx
->bit_rate
) tmp_val
= 4;
1897 s
->cm_table_select
= tmp_val
;
1899 if (s
->sub_sampling
== 0)
1902 tmp
= ((-(s
->sub_sampling
-1)) & 8000) + 20000;
1909 s
->coeff_per_sb_select
= 0;
1910 else if (tmp
<= 16000)
1911 s
->coeff_per_sb_select
= 1;
1913 s
->coeff_per_sb_select
= 2;
1915 // Fail on unknown fft order, if it's > 9 it can overflow s->exptab[]
1916 if ((s
->fft_order
< 7) || (s
->fft_order
> 9)) {
1917 av_log(avctx
, AV_LOG_ERROR
, "Unknown FFT order (%d), contact the developers!\n", s
->fft_order
);
1921 ff_fft_init(&s
->fft_ctx
, s
->fft_order
- 1, 1);
1923 for (i
= 1; i
< (1 << (s
->fft_order
- 2)); i
++) {
1924 alpha
= 2 * M_PI
* (float)i
/ (float)(1 << (s
->fft_order
- 1));
1925 s
->exptab
[i
].re
= cos(alpha
);
1926 s
->exptab
[i
].im
= sin(alpha
);
1936 static int qdm2_decode_close(AVCodecContext
*avctx
)
1938 QDM2Context
*s
= avctx
->priv_data
;
1940 ff_fft_end(&s
->fft_ctx
);
1946 static void qdm2_decode (QDM2Context
*q
, const uint8_t *in
, int16_t *out
)
1949 const int frame_size
= (q
->frame_size
* q
->channels
);
1951 /* select input buffer */
1952 q
->compressed_data
= in
;
1953 q
->compressed_size
= q
->checksum_size
;
1957 /* copy old block, clear new block of output samples */
1958 memmove(q
->output_buffer
, &q
->output_buffer
[frame_size
], frame_size
* sizeof(float));
1959 memset(&q
->output_buffer
[frame_size
], 0, frame_size
* sizeof(float));
1961 /* decode block of QDM2 compressed data */
1962 if (q
->sub_packet
== 0) {
1963 q
->has_errors
= 0; // zero it for a new super block
1964 av_log(NULL
,AV_LOG_DEBUG
,"Superblock follows\n");
1965 qdm2_decode_super_block(q
);
1968 /* parse subpackets */
1969 if (!q
->has_errors
) {
1970 if (q
->sub_packet
== 2)
1971 qdm2_decode_fft_packets(q
);
1973 qdm2_fft_tone_synthesizer(q
, q
->sub_packet
);
1976 /* sound synthesis stage 1 (FFT) */
1977 for (ch
= 0; ch
< q
->channels
; ch
++) {
1978 qdm2_calculate_fft(q
, ch
, q
->sub_packet
);
1980 if (!q
->has_errors
&& q
->sub_packet_list_C
[0].packet
!= NULL
) {
1981 SAMPLES_NEEDED_2("has errors, and C list is not empty")
1986 /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1987 if (!q
->has_errors
&& q
->do_synth_filter
)
1988 qdm2_synthesis_filter(q
, q
->sub_packet
);
1990 q
->sub_packet
= (q
->sub_packet
+ 1) % 16;
1992 /* clip and convert output float[] to 16bit signed samples */
1993 for (i
= 0; i
< frame_size
; i
++) {
1994 int value
= (int)q
->output_buffer
[i
];
1996 if (value
> SOFTCLIP_THRESHOLD
)
1997 value
= (value
> HARDCLIP_THRESHOLD
) ? 32767 : softclip_table
[ value
- SOFTCLIP_THRESHOLD
];
1998 else if (value
< -SOFTCLIP_THRESHOLD
)
1999 value
= (value
< -HARDCLIP_THRESHOLD
) ? -32767 : -softclip_table
[-value
- SOFTCLIP_THRESHOLD
];
2006 static int qdm2_decode_frame(AVCodecContext
*avctx
,
2007 void *data
, int *data_size
,
2008 const uint8_t *buf
, int buf_size
)
2010 QDM2Context
*s
= avctx
->priv_data
;
2014 if(buf_size
< s
->checksum_size
)
2017 *data_size
= s
->channels
* s
->frame_size
* sizeof(int16_t);
2019 av_log(avctx
, AV_LOG_DEBUG
, "decode(%d): %p[%d] -> %p[%d]\n",
2020 buf_size
, buf
, s
->checksum_size
, data
, *data_size
);
2022 qdm2_decode(s
, buf
, data
);
2024 // reading only when next superblock found
2025 if (s
->sub_packet
== 0) {
2026 return s
->checksum_size
;
2032 AVCodec qdm2_decoder
=
2035 .type
= CODEC_TYPE_AUDIO
,
2036 .id
= CODEC_ID_QDM2
,
2037 .priv_data_size
= sizeof(QDM2Context
),
2038 .init
= qdm2_decode_init
,
2039 .close
= qdm2_decode_close
,
2040 .decode
= qdm2_decode_frame
,
2041 .long_name
= "QDesign Music Codec 2",