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
26 * @file libavcodec/qdm2.c
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
41 #include "mpegaudio.h"
49 #define SOFTCLIP_THRESHOLD 27600
50 #define HARDCLIP_THRESHOLD 35716
53 #define QDM2_LIST_ADD(list, size, packet) \
56 list[size - 1].next = &list[size]; \
58 list[size].packet = packet; \
59 list[size].next = NULL; \
63 // Result is 8, 16 or 30
64 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
66 #define FIX_NOISE_IDX(noise_idx) \
67 if ((noise_idx) >= 3840) \
68 (noise_idx) -= 3840; \
70 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
72 #define BITS_LEFT(length,gb) ((length) - get_bits_count ((gb)))
74 #define SAMPLES_NEEDED \
75 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
77 #define SAMPLES_NEEDED_2(why) \
78 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
81 typedef int8_t sb_int8_array
[2][30][64];
87 int type
; ///< subpacket type
88 unsigned int size
; ///< subpacket size
89 const uint8_t *data
; ///< pointer to subpacket data (points to input data buffer, it's not a private copy)
93 * A node in the subpacket list
95 typedef struct QDM2SubPNode
{
96 QDM2SubPacket
*packet
; ///< packet
97 struct QDM2SubPNode
*next
; ///< pointer to next packet in the list, NULL if leaf node
107 QDM2Complex
*complex;
125 DECLARE_ALIGNED_16(QDM2Complex
, complex[MPA_MAX_CHANNELS
][256]);
129 * QDM2 decoder context
132 /// Parameters from codec header, do not change during playback
133 int nb_channels
; ///< number of channels
134 int channels
; ///< number of channels
135 int group_size
; ///< size of frame group (16 frames per group)
136 int fft_size
; ///< size of FFT, in complex numbers
137 int checksum_size
; ///< size of data block, used also for checksum
139 /// Parameters built from header parameters, do not change during playback
140 int group_order
; ///< order of frame group
141 int fft_order
; ///< order of FFT (actually fftorder+1)
142 int fft_frame_size
; ///< size of fft frame, in components (1 comples = re + im)
143 int frame_size
; ///< size of data frame
145 int sub_sampling
; ///< subsampling: 0=25%, 1=50%, 2=100% */
146 int coeff_per_sb_select
; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
147 int cm_table_select
; ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
149 /// Packets and packet lists
150 QDM2SubPacket sub_packets
[16]; ///< the packets themselves
151 QDM2SubPNode sub_packet_list_A
[16]; ///< list of all packets
152 QDM2SubPNode sub_packet_list_B
[16]; ///< FFT packets B are on list
153 int sub_packets_B
; ///< number of packets on 'B' list
154 QDM2SubPNode sub_packet_list_C
[16]; ///< packets with errors?
155 QDM2SubPNode sub_packet_list_D
[16]; ///< DCT packets
158 FFTTone fft_tones
[1000];
161 FFTCoefficient fft_coefs
[1000];
163 int fft_coefs_min_index
[5];
164 int fft_coefs_max_index
[5];
165 int fft_level_exp
[6];
166 RDFTContext rdft_ctx
;
170 const uint8_t *compressed_data
;
172 float output_buffer
[1024];
175 DECLARE_ALIGNED_16(MPA_INT
, synth_buf
[MPA_MAX_CHANNELS
][512*2]);
176 int synth_buf_offset
[MPA_MAX_CHANNELS
];
177 DECLARE_ALIGNED_16(int32_t, sb_samples
[MPA_MAX_CHANNELS
][128][SBLIMIT
]);
179 /// Mixed temporary data used in decoding
180 float tone_level
[MPA_MAX_CHANNELS
][30][64];
181 int8_t coding_method
[MPA_MAX_CHANNELS
][30][64];
182 int8_t quantized_coeffs
[MPA_MAX_CHANNELS
][10][8];
183 int8_t tone_level_idx_base
[MPA_MAX_CHANNELS
][30][8];
184 int8_t tone_level_idx_hi1
[MPA_MAX_CHANNELS
][3][8][8];
185 int8_t tone_level_idx_mid
[MPA_MAX_CHANNELS
][26][8];
186 int8_t tone_level_idx_hi2
[MPA_MAX_CHANNELS
][26];
187 int8_t tone_level_idx
[MPA_MAX_CHANNELS
][30][64];
188 int8_t tone_level_idx_temp
[MPA_MAX_CHANNELS
][30][64];
191 int has_errors
; ///< packet has errors
192 int superblocktype_2_3
; ///< select fft tables and some algorithm based on superblock type
193 int do_synth_filter
; ///< used to perform or skip synthesis filter
196 int noise_idx
; ///< index for dithering noise table
200 static uint8_t empty_buffer
[FF_INPUT_BUFFER_PADDING_SIZE
];
202 static VLC vlc_tab_level
;
203 static VLC vlc_tab_diff
;
204 static VLC vlc_tab_run
;
205 static VLC fft_level_exp_alt_vlc
;
206 static VLC fft_level_exp_vlc
;
207 static VLC fft_stereo_exp_vlc
;
208 static VLC fft_stereo_phase_vlc
;
209 static VLC vlc_tab_tone_level_idx_hi1
;
210 static VLC vlc_tab_tone_level_idx_mid
;
211 static VLC vlc_tab_tone_level_idx_hi2
;
212 static VLC vlc_tab_type30
;
213 static VLC vlc_tab_type34
;
214 static VLC vlc_tab_fft_tone_offset
[5];
216 static uint16_t softclip_table
[HARDCLIP_THRESHOLD
- SOFTCLIP_THRESHOLD
+ 1];
217 static float noise_table
[4096];
218 static uint8_t random_dequant_index
[256][5];
219 static uint8_t random_dequant_type24
[128][3];
220 static float noise_samples
[128];
222 static DECLARE_ALIGNED_16(MPA_INT
, mpa_window
[512]);
225 static av_cold
void softclip_table_init(void) {
227 double dfl
= SOFTCLIP_THRESHOLD
- 32767;
228 float delta
= 1.0 / -dfl
;
229 for (i
= 0; i
< HARDCLIP_THRESHOLD
- SOFTCLIP_THRESHOLD
+ 1; i
++)
230 softclip_table
[i
] = SOFTCLIP_THRESHOLD
- ((int)(sin((float)i
* delta
) * dfl
) & 0x0000FFFF);
234 // random generated table
235 static av_cold
void rnd_table_init(void) {
239 uint64_t random_seed
= 0;
240 float delta
= 1.0 / 16384.0;
241 for(i
= 0; i
< 4096 ;i
++) {
242 random_seed
= random_seed
* 214013 + 2531011;
243 noise_table
[i
] = (delta
* (float)(((int32_t)random_seed
>> 16) & 0x00007FFF)- 1.0) * 1.3;
246 for (i
= 0; i
< 256 ;i
++) {
249 for (j
= 0; j
< 5 ;j
++) {
250 random_dequant_index
[i
][j
] = (uint8_t)((ldw
/ random_seed
) & 0xFF);
251 ldw
= (uint32_t)ldw
% (uint32_t)random_seed
;
252 tmp64_1
= (random_seed
* 0x55555556);
253 hdw
= (uint32_t)(tmp64_1
>> 32);
254 random_seed
= (uint64_t)(hdw
+ (ldw
>> 31));
257 for (i
= 0; i
< 128 ;i
++) {
260 for (j
= 0; j
< 3 ;j
++) {
261 random_dequant_type24
[i
][j
] = (uint8_t)((ldw
/ random_seed
) & 0xFF);
262 ldw
= (uint32_t)ldw
% (uint32_t)random_seed
;
263 tmp64_1
= (random_seed
* 0x66666667);
264 hdw
= (uint32_t)(tmp64_1
>> 33);
265 random_seed
= hdw
+ (ldw
>> 31);
271 static av_cold
void init_noise_samples(void) {
274 float delta
= 1.0 / 16384.0;
275 for (i
= 0; i
< 128;i
++) {
276 random_seed
= random_seed
* 214013 + 2531011;
277 noise_samples
[i
] = (delta
* (float)((random_seed
>> 16) & 0x00007fff) - 1.0);
281 static const uint16_t qdm2_vlc_offs
[] = {
282 0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
285 static av_cold
void qdm2_init_vlc(void)
287 static int vlcs_initialized
= 0;
288 static VLC_TYPE qdm2_table
[3838][2];
290 if (!vlcs_initialized
) {
292 vlc_tab_level
.table
= &qdm2_table
[qdm2_vlc_offs
[0]];
293 vlc_tab_level
.table_allocated
= qdm2_vlc_offs
[1] - qdm2_vlc_offs
[0];
294 init_vlc (&vlc_tab_level
, 8, 24,
295 vlc_tab_level_huffbits
, 1, 1,
296 vlc_tab_level_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
298 vlc_tab_diff
.table
= &qdm2_table
[qdm2_vlc_offs
[1]];
299 vlc_tab_diff
.table_allocated
= qdm2_vlc_offs
[2] - qdm2_vlc_offs
[1];
300 init_vlc (&vlc_tab_diff
, 8, 37,
301 vlc_tab_diff_huffbits
, 1, 1,
302 vlc_tab_diff_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
304 vlc_tab_run
.table
= &qdm2_table
[qdm2_vlc_offs
[2]];
305 vlc_tab_run
.table_allocated
= qdm2_vlc_offs
[3] - qdm2_vlc_offs
[2];
306 init_vlc (&vlc_tab_run
, 5, 6,
307 vlc_tab_run_huffbits
, 1, 1,
308 vlc_tab_run_huffcodes
, 1, 1, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
310 fft_level_exp_alt_vlc
.table
= &qdm2_table
[qdm2_vlc_offs
[3]];
311 fft_level_exp_alt_vlc
.table_allocated
= qdm2_vlc_offs
[4] - qdm2_vlc_offs
[3];
312 init_vlc (&fft_level_exp_alt_vlc
, 8, 28,
313 fft_level_exp_alt_huffbits
, 1, 1,
314 fft_level_exp_alt_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
317 fft_level_exp_vlc
.table
= &qdm2_table
[qdm2_vlc_offs
[4]];
318 fft_level_exp_vlc
.table_allocated
= qdm2_vlc_offs
[5] - qdm2_vlc_offs
[4];
319 init_vlc (&fft_level_exp_vlc
, 8, 20,
320 fft_level_exp_huffbits
, 1, 1,
321 fft_level_exp_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
323 fft_stereo_exp_vlc
.table
= &qdm2_table
[qdm2_vlc_offs
[5]];
324 fft_stereo_exp_vlc
.table_allocated
= qdm2_vlc_offs
[6] - qdm2_vlc_offs
[5];
325 init_vlc (&fft_stereo_exp_vlc
, 6, 7,
326 fft_stereo_exp_huffbits
, 1, 1,
327 fft_stereo_exp_huffcodes
, 1, 1, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
329 fft_stereo_phase_vlc
.table
= &qdm2_table
[qdm2_vlc_offs
[6]];
330 fft_stereo_phase_vlc
.table_allocated
= qdm2_vlc_offs
[7] - qdm2_vlc_offs
[6];
331 init_vlc (&fft_stereo_phase_vlc
, 6, 9,
332 fft_stereo_phase_huffbits
, 1, 1,
333 fft_stereo_phase_huffcodes
, 1, 1, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
335 vlc_tab_tone_level_idx_hi1
.table
= &qdm2_table
[qdm2_vlc_offs
[7]];
336 vlc_tab_tone_level_idx_hi1
.table_allocated
= qdm2_vlc_offs
[8] - qdm2_vlc_offs
[7];
337 init_vlc (&vlc_tab_tone_level_idx_hi1
, 8, 20,
338 vlc_tab_tone_level_idx_hi1_huffbits
, 1, 1,
339 vlc_tab_tone_level_idx_hi1_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
341 vlc_tab_tone_level_idx_mid
.table
= &qdm2_table
[qdm2_vlc_offs
[8]];
342 vlc_tab_tone_level_idx_mid
.table_allocated
= qdm2_vlc_offs
[9] - qdm2_vlc_offs
[8];
343 init_vlc (&vlc_tab_tone_level_idx_mid
, 8, 24,
344 vlc_tab_tone_level_idx_mid_huffbits
, 1, 1,
345 vlc_tab_tone_level_idx_mid_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
347 vlc_tab_tone_level_idx_hi2
.table
= &qdm2_table
[qdm2_vlc_offs
[9]];
348 vlc_tab_tone_level_idx_hi2
.table_allocated
= qdm2_vlc_offs
[10] - qdm2_vlc_offs
[9];
349 init_vlc (&vlc_tab_tone_level_idx_hi2
, 8, 24,
350 vlc_tab_tone_level_idx_hi2_huffbits
, 1, 1,
351 vlc_tab_tone_level_idx_hi2_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
353 vlc_tab_type30
.table
= &qdm2_table
[qdm2_vlc_offs
[10]];
354 vlc_tab_type30
.table_allocated
= qdm2_vlc_offs
[11] - qdm2_vlc_offs
[10];
355 init_vlc (&vlc_tab_type30
, 6, 9,
356 vlc_tab_type30_huffbits
, 1, 1,
357 vlc_tab_type30_huffcodes
, 1, 1, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
359 vlc_tab_type34
.table
= &qdm2_table
[qdm2_vlc_offs
[11]];
360 vlc_tab_type34
.table_allocated
= qdm2_vlc_offs
[12] - qdm2_vlc_offs
[11];
361 init_vlc (&vlc_tab_type34
, 5, 10,
362 vlc_tab_type34_huffbits
, 1, 1,
363 vlc_tab_type34_huffcodes
, 1, 1, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
365 vlc_tab_fft_tone_offset
[0].table
= &qdm2_table
[qdm2_vlc_offs
[12]];
366 vlc_tab_fft_tone_offset
[0].table_allocated
= qdm2_vlc_offs
[13] - qdm2_vlc_offs
[12];
367 init_vlc (&vlc_tab_fft_tone_offset
[0], 8, 23,
368 vlc_tab_fft_tone_offset_0_huffbits
, 1, 1,
369 vlc_tab_fft_tone_offset_0_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
371 vlc_tab_fft_tone_offset
[1].table
= &qdm2_table
[qdm2_vlc_offs
[13]];
372 vlc_tab_fft_tone_offset
[1].table_allocated
= qdm2_vlc_offs
[14] - qdm2_vlc_offs
[13];
373 init_vlc (&vlc_tab_fft_tone_offset
[1], 8, 28,
374 vlc_tab_fft_tone_offset_1_huffbits
, 1, 1,
375 vlc_tab_fft_tone_offset_1_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
377 vlc_tab_fft_tone_offset
[2].table
= &qdm2_table
[qdm2_vlc_offs
[14]];
378 vlc_tab_fft_tone_offset
[2].table_allocated
= qdm2_vlc_offs
[15] - qdm2_vlc_offs
[14];
379 init_vlc (&vlc_tab_fft_tone_offset
[2], 8, 32,
380 vlc_tab_fft_tone_offset_2_huffbits
, 1, 1,
381 vlc_tab_fft_tone_offset_2_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
383 vlc_tab_fft_tone_offset
[3].table
= &qdm2_table
[qdm2_vlc_offs
[15]];
384 vlc_tab_fft_tone_offset
[3].table_allocated
= qdm2_vlc_offs
[16] - qdm2_vlc_offs
[15];
385 init_vlc (&vlc_tab_fft_tone_offset
[3], 8, 35,
386 vlc_tab_fft_tone_offset_3_huffbits
, 1, 1,
387 vlc_tab_fft_tone_offset_3_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
389 vlc_tab_fft_tone_offset
[4].table
= &qdm2_table
[qdm2_vlc_offs
[16]];
390 vlc_tab_fft_tone_offset
[4].table_allocated
= qdm2_vlc_offs
[17] - qdm2_vlc_offs
[16];
391 init_vlc (&vlc_tab_fft_tone_offset
[4], 8, 38,
392 vlc_tab_fft_tone_offset_4_huffbits
, 1, 1,
393 vlc_tab_fft_tone_offset_4_huffcodes
, 2, 2, INIT_VLC_USE_NEW_STATIC
| INIT_VLC_LE
);
400 /* for floating point to fixed point conversion */
401 static const float f2i_scale
= (float) (1 << (FRAC_BITS
- 15));
404 static int qdm2_get_vlc (GetBitContext
*gb
, VLC
*vlc
, int flag
, int depth
)
408 value
= get_vlc2(gb
, vlc
->table
, vlc
->bits
, depth
);
410 /* stage-2, 3 bits exponent escape sequence */
412 value
= get_bits (gb
, get_bits (gb
, 3) + 1);
414 /* stage-3, optional */
416 int tmp
= vlc_stage3_values
[value
];
418 if ((value
& ~3) > 0)
419 tmp
+= get_bits (gb
, (value
>> 2));
427 static int qdm2_get_se_vlc (VLC
*vlc
, GetBitContext
*gb
, int depth
)
429 int value
= qdm2_get_vlc (gb
, vlc
, 0, depth
);
431 return (value
& 1) ? ((value
+ 1) >> 1) : -(value
>> 1);
438 * @param data pointer to data to be checksum'ed
439 * @param length data length
440 * @param value checksum value
442 * @return 0 if checksum is OK
444 static uint16_t qdm2_packet_checksum (const uint8_t *data
, int length
, int value
) {
447 for (i
=0; i
< length
; i
++)
450 return (uint16_t)(value
& 0xffff);
455 * Fills a QDM2SubPacket structure with packet type, size, and data pointer.
457 * @param gb bitreader context
458 * @param sub_packet packet under analysis
460 static void qdm2_decode_sub_packet_header (GetBitContext
*gb
, QDM2SubPacket
*sub_packet
)
462 sub_packet
->type
= get_bits (gb
, 8);
464 if (sub_packet
->type
== 0) {
465 sub_packet
->size
= 0;
466 sub_packet
->data
= NULL
;
468 sub_packet
->size
= get_bits (gb
, 8);
470 if (sub_packet
->type
& 0x80) {
471 sub_packet
->size
<<= 8;
472 sub_packet
->size
|= get_bits (gb
, 8);
473 sub_packet
->type
&= 0x7f;
476 if (sub_packet
->type
== 0x7f)
477 sub_packet
->type
|= (get_bits (gb
, 8) << 8);
479 sub_packet
->data
= &gb
->buffer
[get_bits_count(gb
) / 8]; // FIXME: this depends on bitreader internal data
482 av_log(NULL
,AV_LOG_DEBUG
,"Subpacket: type=%d size=%d start_offs=%x\n",
483 sub_packet
->type
, sub_packet
->size
, get_bits_count(gb
) / 8);
488 * Return node pointer to first packet of requested type in list.
490 * @param list list of subpackets to be scanned
491 * @param type type of searched subpacket
492 * @return node pointer for subpacket if found, else NULL
494 static QDM2SubPNode
* qdm2_search_subpacket_type_in_list (QDM2SubPNode
*list
, int type
)
496 while (list
!= NULL
&& list
->packet
!= NULL
) {
497 if (list
->packet
->type
== type
)
506 * Replaces 8 elements with their average value.
507 * Called by qdm2_decode_superblock before starting subblock decoding.
511 static void average_quantized_coeffs (QDM2Context
*q
)
513 int i
, j
, n
, ch
, sum
;
515 n
= coeff_per_sb_for_avg
[q
->coeff_per_sb_select
][QDM2_SB_USED(q
->sub_sampling
) - 1] + 1;
517 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
518 for (i
= 0; i
< n
; i
++) {
521 for (j
= 0; j
< 8; j
++)
522 sum
+= q
->quantized_coeffs
[ch
][i
][j
];
528 for (j
=0; j
< 8; j
++)
529 q
->quantized_coeffs
[ch
][i
][j
] = sum
;
535 * Build subband samples with noise weighted by q->tone_level.
536 * Called by synthfilt_build_sb_samples.
539 * @param sb subband index
541 static void build_sb_samples_from_noise (QDM2Context
*q
, int sb
)
545 FIX_NOISE_IDX(q
->noise_idx
);
550 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
551 for (j
= 0; j
< 64; j
++) {
552 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);
553 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);
559 * Called while processing data from subpackets 11 and 12.
560 * Used after making changes to coding_method array.
562 * @param sb subband index
563 * @param channels number of channels
564 * @param coding_method q->coding_method[0][0][0]
566 static void fix_coding_method_array (int sb
, int channels
, sb_int8_array coding_method
)
571 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};
573 for (ch
= 0; ch
< channels
; ch
++) {
574 for (j
= 0; j
< 64; ) {
575 if((coding_method
[ch
][sb
][j
] - 8) > 22) {
579 switch (switchtable
[coding_method
[ch
][sb
][j
]-8]) {
580 case 0: run
= 10; case_val
= 10; break;
581 case 1: run
= 1; case_val
= 16; break;
582 case 2: run
= 5; case_val
= 24; break;
583 case 3: run
= 3; case_val
= 30; break;
584 case 4: run
= 1; case_val
= 30; break;
585 case 5: run
= 1; case_val
= 8; break;
586 default: run
= 1; case_val
= 8; break;
589 for (k
= 0; k
< run
; k
++)
591 if (coding_method
[ch
][sb
+ (j
+ k
) / 64][(j
+ k
) % 64] > coding_method
[ch
][sb
][j
])
594 //not debugged, almost never used
595 memset(&coding_method
[ch
][sb
][j
+ k
], case_val
, k
* sizeof(int8_t));
596 memset(&coding_method
[ch
][sb
][j
+ k
], case_val
, 3 * sizeof(int8_t));
605 * Related to synthesis filter
606 * Called by process_subpacket_10
609 * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10
611 static void fill_tone_level_array (QDM2Context
*q
, int flag
)
613 int i
, sb
, ch
, sb_used
;
616 // This should never happen
617 if (q
->nb_channels
<= 0)
620 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
621 for (sb
= 0; sb
< 30; sb
++)
622 for (i
= 0; i
< 8; i
++) {
623 if ((tab
=coeff_per_sb_for_dequant
[q
->coeff_per_sb_select
][sb
]) < (last_coeff
[q
->coeff_per_sb_select
] - 1))
624 tmp
= q
->quantized_coeffs
[ch
][tab
+ 1][i
] * dequant_table
[q
->coeff_per_sb_select
][tab
+ 1][sb
]+
625 q
->quantized_coeffs
[ch
][tab
][i
] * dequant_table
[q
->coeff_per_sb_select
][tab
][sb
];
627 tmp
= q
->quantized_coeffs
[ch
][tab
][i
] * dequant_table
[q
->coeff_per_sb_select
][tab
][sb
];
630 q
->tone_level_idx_base
[ch
][sb
][i
] = (tmp
/ 256) & 0xff;
633 sb_used
= QDM2_SB_USED(q
->sub_sampling
);
635 if ((q
->superblocktype_2_3
!= 0) && !flag
) {
636 for (sb
= 0; sb
< sb_used
; sb
++)
637 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
638 for (i
= 0; i
< 64; i
++) {
639 q
->tone_level_idx
[ch
][sb
][i
] = q
->tone_level_idx_base
[ch
][sb
][i
/ 8];
640 if (q
->tone_level_idx
[ch
][sb
][i
] < 0)
641 q
->tone_level
[ch
][sb
][i
] = 0;
643 q
->tone_level
[ch
][sb
][i
] = fft_tone_level_table
[0][q
->tone_level_idx
[ch
][sb
][i
] & 0x3f];
646 tab
= q
->superblocktype_2_3
? 0 : 1;
647 for (sb
= 0; sb
< sb_used
; sb
++) {
648 if ((sb
>= 4) && (sb
<= 23)) {
649 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
650 for (i
= 0; i
< 64; i
++) {
651 tmp
= q
->tone_level_idx_base
[ch
][sb
][i
/ 8] -
652 q
->tone_level_idx_hi1
[ch
][sb
/ 8][i
/ 8][i
% 8] -
653 q
->tone_level_idx_mid
[ch
][sb
- 4][i
/ 8] -
654 q
->tone_level_idx_hi2
[ch
][sb
- 4];
655 q
->tone_level_idx
[ch
][sb
][i
] = tmp
& 0xff;
656 if ((tmp
< 0) || (!q
->superblocktype_2_3
&& !tmp
))
657 q
->tone_level
[ch
][sb
][i
] = 0;
659 q
->tone_level
[ch
][sb
][i
] = fft_tone_level_table
[tab
][tmp
& 0x3f];
663 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
664 for (i
= 0; i
< 64; i
++) {
665 tmp
= q
->tone_level_idx_base
[ch
][sb
][i
/ 8] -
666 q
->tone_level_idx_hi1
[ch
][2][i
/ 8][i
% 8] -
667 q
->tone_level_idx_hi2
[ch
][sb
- 4];
668 q
->tone_level_idx
[ch
][sb
][i
] = tmp
& 0xff;
669 if ((tmp
< 0) || (!q
->superblocktype_2_3
&& !tmp
))
670 q
->tone_level
[ch
][sb
][i
] = 0;
672 q
->tone_level
[ch
][sb
][i
] = fft_tone_level_table
[tab
][tmp
& 0x3f];
675 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
676 for (i
= 0; i
< 64; i
++) {
677 tmp
= q
->tone_level_idx
[ch
][sb
][i
] = q
->tone_level_idx_base
[ch
][sb
][i
/ 8];
678 if ((tmp
< 0) || (!q
->superblocktype_2_3
&& !tmp
))
679 q
->tone_level
[ch
][sb
][i
] = 0;
681 q
->tone_level
[ch
][sb
][i
] = fft_tone_level_table
[tab
][tmp
& 0x3f];
693 * Related to synthesis filter
694 * Called by process_subpacket_11
695 * c is built with data from subpacket 11
696 * Most of this function is used only if superblock_type_2_3 == 0, never seen it in samples
698 * @param tone_level_idx
699 * @param tone_level_idx_temp
700 * @param coding_method q->coding_method[0][0][0]
701 * @param nb_channels number of channels
702 * @param c coming from subpacket 11, passed as 8*c
703 * @param superblocktype_2_3 flag based on superblock packet type
704 * @param cm_table_select q->cm_table_select
706 static void fill_coding_method_array (sb_int8_array tone_level_idx
, sb_int8_array tone_level_idx_temp
,
707 sb_int8_array coding_method
, int nb_channels
,
708 int c
, int superblocktype_2_3
, int cm_table_select
)
711 int tmp
, acc
, esp_40
, comp
;
712 int add1
, add2
, add3
, add4
;
715 // This should never happen
716 if (nb_channels
<= 0)
719 if (!superblocktype_2_3
) {
720 /* This case is untested, no samples available */
722 for (ch
= 0; ch
< nb_channels
; ch
++)
723 for (sb
= 0; sb
< 30; sb
++) {
724 for (j
= 1; j
< 63; j
++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
725 add1
= tone_level_idx
[ch
][sb
][j
] - 10;
728 add2
= add3
= add4
= 0;
730 add2
= tone_level_idx
[ch
][sb
- 2][j
] + tone_level_idx_offset_table
[sb
][0] - 6;
735 add3
= tone_level_idx
[ch
][sb
- 1][j
] + tone_level_idx_offset_table
[sb
][1] - 6;
740 add4
= tone_level_idx
[ch
][sb
+ 1][j
] + tone_level_idx_offset_table
[sb
][3] - 6;
744 tmp
= tone_level_idx
[ch
][sb
][j
+ 1] * 2 - add4
- add3
- add2
- add1
;
747 tone_level_idx_temp
[ch
][sb
][j
+ 1] = tmp
& 0xff;
749 tone_level_idx_temp
[ch
][sb
][0] = tone_level_idx_temp
[ch
][sb
][1];
752 for (ch
= 0; ch
< nb_channels
; ch
++)
753 for (sb
= 0; sb
< 30; sb
++)
754 for (j
= 0; j
< 64; j
++)
755 acc
+= tone_level_idx_temp
[ch
][sb
][j
];
757 multres
= 0x66666667 * (acc
* 10);
758 esp_40
= (multres
>> 32) / 8 + ((multres
& 0xffffffff) >> 31);
759 for (ch
= 0; ch
< nb_channels
; ch
++)
760 for (sb
= 0; sb
< 30; sb
++)
761 for (j
= 0; j
< 64; j
++) {
762 comp
= tone_level_idx_temp
[ch
][sb
][j
]* esp_40
* 10;
765 comp
/= 256; // signed shift
793 coding_method
[ch
][sb
][j
] = ((tmp
& 0xfffa) + 30 )& 0xff;
795 for (sb
= 0; sb
< 30; sb
++)
796 fix_coding_method_array(sb
, nb_channels
, coding_method
);
797 for (ch
= 0; ch
< nb_channels
; ch
++)
798 for (sb
= 0; sb
< 30; sb
++)
799 for (j
= 0; j
< 64; j
++)
801 if (coding_method
[ch
][sb
][j
] < 10)
802 coding_method
[ch
][sb
][j
] = 10;
805 if (coding_method
[ch
][sb
][j
] < 16)
806 coding_method
[ch
][sb
][j
] = 16;
808 if (coding_method
[ch
][sb
][j
] < 30)
809 coding_method
[ch
][sb
][j
] = 30;
812 } else { // superblocktype_2_3 != 0
813 for (ch
= 0; ch
< nb_channels
; ch
++)
814 for (sb
= 0; sb
< 30; sb
++)
815 for (j
= 0; j
< 64; j
++)
816 coding_method
[ch
][sb
][j
] = coding_method_table
[cm_table_select
][sb
];
825 * Called by process_subpacket_11 to process more data from subpacket 11 with sb 0-8
826 * Called by process_subpacket_12 to process data from subpacket 12 with sb 8-sb_used
829 * @param gb bitreader context
830 * @param length packet length in bits
831 * @param sb_min lower subband processed (sb_min included)
832 * @param sb_max higher subband processed (sb_max excluded)
834 static void synthfilt_build_sb_samples (QDM2Context
*q
, GetBitContext
*gb
, int length
, int sb_min
, int sb_max
)
836 int sb
, j
, k
, n
, ch
, run
, channels
;
837 int joined_stereo
, zero_encoding
, chs
;
839 float type34_div
= 0;
840 float type34_predictor
;
841 float samples
[10], sign_bits
[16];
844 // If no data use noise
845 for (sb
=sb_min
; sb
< sb_max
; sb
++)
846 build_sb_samples_from_noise (q
, sb
);
851 for (sb
= sb_min
; sb
< sb_max
; sb
++) {
852 FIX_NOISE_IDX(q
->noise_idx
);
854 channels
= q
->nb_channels
;
856 if (q
->nb_channels
<= 1 || sb
< 12)
861 joined_stereo
= (BITS_LEFT(length
,gb
) >= 1) ? get_bits1 (gb
) : 0;
864 if (BITS_LEFT(length
,gb
) >= 16)
865 for (j
= 0; j
< 16; j
++)
866 sign_bits
[j
] = get_bits1 (gb
);
868 for (j
= 0; j
< 64; j
++)
869 if (q
->coding_method
[1][sb
][j
] > q
->coding_method
[0][sb
][j
])
870 q
->coding_method
[0][sb
][j
] = q
->coding_method
[1][sb
][j
];
872 fix_coding_method_array(sb
, q
->nb_channels
, q
->coding_method
);
876 for (ch
= 0; ch
< channels
; ch
++) {
877 zero_encoding
= (BITS_LEFT(length
,gb
) >= 1) ? get_bits1(gb
) : 0;
878 type34_predictor
= 0.0;
881 for (j
= 0; j
< 128; ) {
882 switch (q
->coding_method
[ch
][sb
][j
/ 2]) {
884 if (BITS_LEFT(length
,gb
) >= 10) {
886 for (k
= 0; k
< 5; k
++) {
887 if ((j
+ 2 * k
) >= 128)
889 samples
[2 * k
] = get_bits1(gb
) ? dequant_1bit
[joined_stereo
][2 * get_bits1(gb
)] : 0;
893 for (k
= 0; k
< 5; k
++)
894 samples
[2 * k
] = dequant_1bit
[joined_stereo
][random_dequant_index
[n
][k
]];
896 for (k
= 0; k
< 5; k
++)
897 samples
[2 * k
+ 1] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
899 for (k
= 0; k
< 10; k
++)
900 samples
[k
] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
906 if (BITS_LEFT(length
,gb
) >= 1) {
911 f
-= noise_samples
[((sb
+ 1) * (j
+5 * ch
+ 1)) & 127] * 9.0 / 40.0;
914 samples
[0] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
920 if (BITS_LEFT(length
,gb
) >= 10) {
922 for (k
= 0; k
< 5; k
++) {
925 samples
[k
] = (get_bits1(gb
) == 0) ? 0 : dequant_1bit
[joined_stereo
][2 * get_bits1(gb
)];
928 n
= get_bits (gb
, 8);
929 for (k
= 0; k
< 5; k
++)
930 samples
[k
] = dequant_1bit
[joined_stereo
][random_dequant_index
[n
][k
]];
933 for (k
= 0; k
< 5; k
++)
934 samples
[k
] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
940 if (BITS_LEFT(length
,gb
) >= 7) {
942 for (k
= 0; k
< 3; k
++)
943 samples
[k
] = (random_dequant_type24
[n
][k
] - 2.0) * 0.5;
945 for (k
= 0; k
< 3; k
++)
946 samples
[k
] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
952 if (BITS_LEFT(length
,gb
) >= 4)
953 samples
[0] = type30_dequant
[qdm2_get_vlc(gb
, &vlc_tab_type30
, 0, 1)];
955 samples
[0] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
961 if (BITS_LEFT(length
,gb
) >= 7) {
963 type34_div
= (float)(1 << get_bits(gb
, 2));
964 samples
[0] = ((float)get_bits(gb
, 5) - 16.0) / 15.0;
965 type34_predictor
= samples
[0];
968 samples
[0] = type34_delta
[qdm2_get_vlc(gb
, &vlc_tab_type34
, 0, 1)] / type34_div
+ type34_predictor
;
969 type34_predictor
= samples
[0];
972 samples
[0] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
978 samples
[0] = SB_DITHERING_NOISE(sb
,q
->noise_idx
);
984 float tmp
[10][MPA_MAX_CHANNELS
];
986 for (k
= 0; k
< run
; k
++) {
987 tmp
[k
][0] = samples
[k
];
988 tmp
[k
][1] = (sign_bits
[(j
+ k
) / 8]) ? -samples
[k
] : samples
[k
];
990 for (chs
= 0; chs
< q
->nb_channels
; chs
++)
991 for (k
= 0; k
< run
; k
++)
993 q
->sb_samples
[chs
][j
+ k
][sb
] = (int32_t)(f2i_scale
* q
->tone_level
[chs
][sb
][((j
+ k
)/2)] * tmp
[k
][chs
] + .5);
995 for (k
= 0; k
< run
; k
++)
997 q
->sb_samples
[ch
][j
+ k
][sb
] = (int32_t)(f2i_scale
* q
->tone_level
[ch
][sb
][(j
+ k
)/2] * samples
[k
] + .5);
1008 * Init the first element of a channel in quantized_coeffs with data from packet 10 (quantized_coeffs[ch][0]).
1009 * This is similar to process_subpacket_9, but for a single channel and for element [0]
1010 * same VLC tables as process_subpacket_9 are used.
1013 * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
1014 * @param gb bitreader context
1015 * @param length packet length in bits
1017 static void init_quantized_coeffs_elem0 (int8_t *quantized_coeffs
, GetBitContext
*gb
, int length
)
1019 int i
, k
, run
, level
, diff
;
1021 if (BITS_LEFT(length
,gb
) < 16)
1023 level
= qdm2_get_vlc(gb
, &vlc_tab_level
, 0, 2);
1025 quantized_coeffs
[0] = level
;
1027 for (i
= 0; i
< 7; ) {
1028 if (BITS_LEFT(length
,gb
) < 16)
1030 run
= qdm2_get_vlc(gb
, &vlc_tab_run
, 0, 1) + 1;
1032 if (BITS_LEFT(length
,gb
) < 16)
1034 diff
= qdm2_get_se_vlc(&vlc_tab_diff
, gb
, 2);
1036 for (k
= 1; k
<= run
; k
++)
1037 quantized_coeffs
[i
+ k
] = (level
+ ((k
* diff
) / run
));
1046 * Related to synthesis filter, process data from packet 10
1047 * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
1048 * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with data from packet 10
1051 * @param gb bitreader context
1052 * @param length packet length in bits
1054 static void init_tone_level_dequantization (QDM2Context
*q
, GetBitContext
*gb
, int length
)
1056 int sb
, j
, k
, n
, ch
;
1058 for (ch
= 0; ch
< q
->nb_channels
; ch
++) {
1059 init_quantized_coeffs_elem0(q
->quantized_coeffs
[ch
][0], gb
, length
);
1061 if (BITS_LEFT(length
,gb
) < 16) {
1062 memset(q
->quantized_coeffs
[ch
][0], 0, 8);
1067 n
= q
->sub_sampling
+ 1;
1069 for (sb
= 0; sb
< n
; sb
++)
1070 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
1071 for (j
= 0; j
< 8; j
++) {
1072 if (BITS_LEFT(length
,gb
) < 1)
1074 if (get_bits1(gb
)) {
1075 for (k
=0; k
< 8; k
++) {
1076 if (BITS_LEFT(length
,gb
) < 16)
1078 q
->tone_level_idx_hi1
[ch
][sb
][j
][k
] = qdm2_get_vlc(gb
, &vlc_tab_tone_level_idx_hi1
, 0, 2);
1081 for (k
=0; k
< 8; k
++)
1082 q
->tone_level_idx_hi1
[ch
][sb
][j
][k
] = 0;
1086 n
= QDM2_SB_USED(q
->sub_sampling
) - 4;
1088 for (sb
= 0; sb
< n
; sb
++)
1089 for (ch
= 0; ch
< q
->nb_channels
; ch
++) {
1090 if (BITS_LEFT(length
,gb
) < 16)
1092 q
->tone_level_idx_hi2
[ch
][sb
] = qdm2_get_vlc(gb
, &vlc_tab_tone_level_idx_hi2
, 0, 2);
1094 q
->tone_level_idx_hi2
[ch
][sb
] -= 16;
1096 for (j
= 0; j
< 8; j
++)
1097 q
->tone_level_idx_mid
[ch
][sb
][j
] = -16;
1100 n
= QDM2_SB_USED(q
->sub_sampling
) - 5;
1102 for (sb
= 0; sb
< n
; sb
++)
1103 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
1104 for (j
= 0; j
< 8; j
++) {
1105 if (BITS_LEFT(length
,gb
) < 16)
1107 q
->tone_level_idx_mid
[ch
][sb
][j
] = qdm2_get_vlc(gb
, &vlc_tab_tone_level_idx_mid
, 0, 2) - 32;
1112 * Process subpacket 9, init quantized_coeffs with data from it
1115 * @param node pointer to node with packet
1117 static void process_subpacket_9 (QDM2Context
*q
, QDM2SubPNode
*node
)
1120 int i
, j
, k
, n
, ch
, run
, level
, diff
;
1122 init_get_bits(&gb
, node
->packet
->data
, node
->packet
->size
*8);
1124 n
= coeff_per_sb_for_avg
[q
->coeff_per_sb_select
][QDM2_SB_USED(q
->sub_sampling
) - 1] + 1; // same as averagesomething function
1126 for (i
= 1; i
< n
; i
++)
1127 for (ch
=0; ch
< q
->nb_channels
; ch
++) {
1128 level
= qdm2_get_vlc(&gb
, &vlc_tab_level
, 0, 2);
1129 q
->quantized_coeffs
[ch
][i
][0] = level
;
1131 for (j
= 0; j
< (8 - 1); ) {
1132 run
= qdm2_get_vlc(&gb
, &vlc_tab_run
, 0, 1) + 1;
1133 diff
= qdm2_get_se_vlc(&vlc_tab_diff
, &gb
, 2);
1135 for (k
= 1; k
<= run
; k
++)
1136 q
->quantized_coeffs
[ch
][i
][j
+ k
] = (level
+ ((k
*diff
) / run
));
1143 for (ch
= 0; ch
< q
->nb_channels
; ch
++)
1144 for (i
= 0; i
< 8; i
++)
1145 q
->quantized_coeffs
[ch
][0][i
] = 0;
1150 * Process subpacket 10 if not null, else
1153 * @param node pointer to node with packet
1154 * @param length packet length in bits
1156 static void process_subpacket_10 (QDM2Context
*q
, QDM2SubPNode
*node
, int length
)
1160 init_get_bits(&gb
, ((node
== NULL
) ? empty_buffer
: node
->packet
->data
), ((node
== NULL
) ? 0 : node
->packet
->size
*8));
1163 init_tone_level_dequantization(q
, &gb
, length
);
1164 fill_tone_level_array(q
, 1);
1166 fill_tone_level_array(q
, 0);
1172 * Process subpacket 11
1175 * @param node pointer to node with packet
1176 * @param length packet length in bit
1178 static void process_subpacket_11 (QDM2Context
*q
, QDM2SubPNode
*node
, int length
)
1182 init_get_bits(&gb
, ((node
== NULL
) ? empty_buffer
: node
->packet
->data
), ((node
== NULL
) ? 0 : node
->packet
->size
*8));
1184 int c
= get_bits (&gb
, 13);
1187 fill_coding_method_array (q
->tone_level_idx
, q
->tone_level_idx_temp
, q
->coding_method
,
1188 q
->nb_channels
, 8*c
, q
->superblocktype_2_3
, q
->cm_table_select
);
1191 synthfilt_build_sb_samples(q
, &gb
, length
, 0, 8);
1196 * Process subpacket 12
1199 * @param node pointer to node with packet
1200 * @param length packet length in bits
1202 static void process_subpacket_12 (QDM2Context
*q
, QDM2SubPNode
*node
, int length
)
1206 init_get_bits(&gb
, ((node
== NULL
) ? empty_buffer
: node
->packet
->data
), ((node
== NULL
) ? 0 : node
->packet
->size
*8));
1207 synthfilt_build_sb_samples(q
, &gb
, length
, 8, QDM2_SB_USED(q
->sub_sampling
));
1211 * Process new subpackets for synthesis filter
1214 * @param list list with synthesis filter packets (list D)
1216 static void process_synthesis_subpackets (QDM2Context
*q
, QDM2SubPNode
*list
)
1218 QDM2SubPNode
*nodes
[4];
1220 nodes
[0] = qdm2_search_subpacket_type_in_list(list
, 9);
1221 if (nodes
[0] != NULL
)
1222 process_subpacket_9(q
, nodes
[0]);
1224 nodes
[1] = qdm2_search_subpacket_type_in_list(list
, 10);
1225 if (nodes
[1] != NULL
)
1226 process_subpacket_10(q
, nodes
[1], nodes
[1]->packet
->size
<< 3);
1228 process_subpacket_10(q
, NULL
, 0);
1230 nodes
[2] = qdm2_search_subpacket_type_in_list(list
, 11);
1231 if (nodes
[0] != NULL
&& nodes
[1] != NULL
&& nodes
[2] != NULL
)
1232 process_subpacket_11(q
, nodes
[2], (nodes
[2]->packet
->size
<< 3));
1234 process_subpacket_11(q
, NULL
, 0);
1236 nodes
[3] = qdm2_search_subpacket_type_in_list(list
, 12);
1237 if (nodes
[0] != NULL
&& nodes
[1] != NULL
&& nodes
[3] != NULL
)
1238 process_subpacket_12(q
, nodes
[3], (nodes
[3]->packet
->size
<< 3));
1240 process_subpacket_12(q
, NULL
, 0);
1245 * Decode superblock, fill packet lists.
1249 static void qdm2_decode_super_block (QDM2Context
*q
)
1252 QDM2SubPacket header
, *packet
;
1253 int i
, packet_bytes
, sub_packet_size
, sub_packets_D
;
1254 unsigned int next_index
= 0;
1256 memset(q
->tone_level_idx_hi1
, 0, sizeof(q
->tone_level_idx_hi1
));
1257 memset(q
->tone_level_idx_mid
, 0, sizeof(q
->tone_level_idx_mid
));
1258 memset(q
->tone_level_idx_hi2
, 0, sizeof(q
->tone_level_idx_hi2
));
1260 q
->sub_packets_B
= 0;
1263 average_quantized_coeffs(q
); // average elements in quantized_coeffs[max_ch][10][8]
1265 init_get_bits(&gb
, q
->compressed_data
, q
->compressed_size
*8);
1266 qdm2_decode_sub_packet_header(&gb
, &header
);
1268 if (header
.type
< 2 || header
.type
>= 8) {
1270 av_log(NULL
,AV_LOG_ERROR
,"bad superblock type\n");
1274 q
->superblocktype_2_3
= (header
.type
== 2 || header
.type
== 3);
1275 packet_bytes
= (q
->compressed_size
- get_bits_count(&gb
) / 8);
1277 init_get_bits(&gb
, header
.data
, header
.size
*8);
1279 if (header
.type
== 2 || header
.type
== 4 || header
.type
== 5) {
1280 int csum
= 257 * get_bits(&gb
, 8) + 2 * get_bits(&gb
, 8);
1282 csum
= qdm2_packet_checksum(q
->compressed_data
, q
->checksum_size
, csum
);
1286 av_log(NULL
,AV_LOG_ERROR
,"bad packet checksum\n");
1291 q
->sub_packet_list_B
[0].packet
= NULL
;
1292 q
->sub_packet_list_D
[0].packet
= NULL
;
1294 for (i
= 0; i
< 6; i
++)
1295 if (--q
->fft_level_exp
[i
] < 0)
1296 q
->fft_level_exp
[i
] = 0;
1298 for (i
= 0; packet_bytes
> 0; i
++) {
1301 q
->sub_packet_list_A
[i
].next
= NULL
;
1304 q
->sub_packet_list_A
[i
- 1].next
= &q
->sub_packet_list_A
[i
];
1306 /* seek to next block */
1307 init_get_bits(&gb
, header
.data
, header
.size
*8);
1308 skip_bits(&gb
, next_index
*8);
1310 if (next_index
>= header
.size
)
1314 /* decode subpacket */
1315 packet
= &q
->sub_packets
[i
];
1316 qdm2_decode_sub_packet_header(&gb
, packet
);
1317 next_index
= packet
->size
+ get_bits_count(&gb
) / 8;
1318 sub_packet_size
= ((packet
->size
> 0xff) ? 1 : 0) + packet
->size
+ 2;
1320 if (packet
->type
== 0)
1323 if (sub_packet_size
> packet_bytes
) {
1324 if (packet
->type
!= 10 && packet
->type
!= 11 && packet
->type
!= 12)
1326 packet
->size
+= packet_bytes
- sub_packet_size
;
1329 packet_bytes
-= sub_packet_size
;
1331 /* add subpacket to 'all subpackets' list */
1332 q
->sub_packet_list_A
[i
].packet
= packet
;
1334 /* add subpacket to related list */
1335 if (packet
->type
== 8) {
1336 SAMPLES_NEEDED_2("packet type 8");
1338 } else if (packet
->type
>= 9 && packet
->type
<= 12) {
1339 /* packets for MPEG Audio like Synthesis Filter */
1340 QDM2_LIST_ADD(q
->sub_packet_list_D
, sub_packets_D
, packet
);
1341 } else if (packet
->type
== 13) {
1342 for (j
= 0; j
< 6; j
++)
1343 q
->fft_level_exp
[j
] = get_bits(&gb
, 6);
1344 } else if (packet
->type
== 14) {
1345 for (j
= 0; j
< 6; j
++)
1346 q
->fft_level_exp
[j
] = qdm2_get_vlc(&gb
, &fft_level_exp_vlc
, 0, 2);
1347 } else if (packet
->type
== 15) {
1348 SAMPLES_NEEDED_2("packet type 15")
1350 } else if (packet
->type
>= 16 && packet
->type
< 48 && !fft_subpackets
[packet
->type
- 16]) {
1351 /* packets for FFT */
1352 QDM2_LIST_ADD(q
->sub_packet_list_B
, q
->sub_packets_B
, packet
);
1354 } // Packet bytes loop
1356 /* **************************************************************** */
1357 if (q
->sub_packet_list_D
[0].packet
!= NULL
) {
1358 process_synthesis_subpackets(q
, q
->sub_packet_list_D
);
1359 q
->do_synth_filter
= 1;
1360 } else if (q
->do_synth_filter
) {
1361 process_subpacket_10(q
, NULL
, 0);
1362 process_subpacket_11(q
, NULL
, 0);
1363 process_subpacket_12(q
, NULL
, 0);
1365 /* **************************************************************** */
1369 static void qdm2_fft_init_coefficient (QDM2Context
*q
, int sub_packet
,
1370 int offset
, int duration
, int channel
,
1373 if (q
->fft_coefs_min_index
[duration
] < 0)
1374 q
->fft_coefs_min_index
[duration
] = q
->fft_coefs_index
;
1376 q
->fft_coefs
[q
->fft_coefs_index
].sub_packet
= ((sub_packet
>= 16) ? (sub_packet
- 16) : sub_packet
);
1377 q
->fft_coefs
[q
->fft_coefs_index
].channel
= channel
;
1378 q
->fft_coefs
[q
->fft_coefs_index
].offset
= offset
;
1379 q
->fft_coefs
[q
->fft_coefs_index
].exp
= exp
;
1380 q
->fft_coefs
[q
->fft_coefs_index
].phase
= phase
;
1381 q
->fft_coefs_index
++;
1385 static void qdm2_fft_decode_tones (QDM2Context
*q
, int duration
, GetBitContext
*gb
, int b
)
1387 int channel
, stereo
, phase
, exp
;
1388 int local_int_4
, local_int_8
, stereo_phase
, local_int_10
;
1389 int local_int_14
, stereo_exp
, local_int_20
, local_int_28
;
1395 local_int_8
= (4 - duration
);
1396 local_int_10
= 1 << (q
->group_order
- duration
- 1);
1400 if (q
->superblocktype_2_3
) {
1401 while ((n
= qdm2_get_vlc(gb
, &vlc_tab_fft_tone_offset
[local_int_8
], 1, 2)) < 2) {
1404 local_int_4
+= local_int_10
;
1405 local_int_28
+= (1 << local_int_8
);
1407 local_int_4
+= 8*local_int_10
;
1408 local_int_28
+= (8 << local_int_8
);
1413 offset
+= qdm2_get_vlc(gb
, &vlc_tab_fft_tone_offset
[local_int_8
], 1, 2);
1414 while (offset
>= (local_int_10
- 1)) {
1415 offset
+= (1 - (local_int_10
- 1));
1416 local_int_4
+= local_int_10
;
1417 local_int_28
+= (1 << local_int_8
);
1421 if (local_int_4
>= q
->group_size
)
1424 local_int_14
= (offset
>> local_int_8
);
1426 if (q
->nb_channels
> 1) {
1427 channel
= get_bits1(gb
);
1428 stereo
= get_bits1(gb
);
1434 exp
= qdm2_get_vlc(gb
, (b
? &fft_level_exp_vlc
: &fft_level_exp_alt_vlc
), 0, 2);
1435 exp
+= q
->fft_level_exp
[fft_level_index_table
[local_int_14
]];
1436 exp
= (exp
< 0) ? 0 : exp
;
1438 phase
= get_bits(gb
, 3);
1443 stereo_exp
= (exp
- qdm2_get_vlc(gb
, &fft_stereo_exp_vlc
, 0, 1));
1444 stereo_phase
= (phase
- qdm2_get_vlc(gb
, &fft_stereo_phase_vlc
, 0, 1));
1445 if (stereo_phase
< 0)
1449 if (q
->frequency_range
> (local_int_14
+ 1)) {
1450 int sub_packet
= (local_int_20
+ local_int_28
);
1452 qdm2_fft_init_coefficient(q
, sub_packet
, offset
, duration
, channel
, exp
, phase
);
1454 qdm2_fft_init_coefficient(q
, sub_packet
, offset
, duration
, (1 - channel
), stereo_exp
, stereo_phase
);
1462 static void qdm2_decode_fft_packets (QDM2Context
*q
)
1464 int i
, j
, min
, max
, value
, type
, unknown_flag
;
1467 if (q
->sub_packet_list_B
[0].packet
== NULL
)
1470 /* reset minimum indexes for FFT coefficients */
1471 q
->fft_coefs_index
= 0;
1472 for (i
=0; i
< 5; i
++)
1473 q
->fft_coefs_min_index
[i
] = -1;
1475 /* process subpackets ordered by type, largest type first */
1476 for (i
= 0, max
= 256; i
< q
->sub_packets_B
; i
++) {
1477 QDM2SubPacket
*packet
= NULL
;
1479 /* find subpacket with largest type less than max */
1480 for (j
= 0, min
= 0; j
< q
->sub_packets_B
; j
++) {
1481 value
= q
->sub_packet_list_B
[j
].packet
->type
;
1482 if (value
> min
&& value
< max
) {
1484 packet
= q
->sub_packet_list_B
[j
].packet
;
1490 /* check for errors (?) */
1494 if (i
== 0 && (packet
->type
< 16 || packet
->type
>= 48 || fft_subpackets
[packet
->type
- 16]))
1497 /* decode FFT tones */
1498 init_get_bits (&gb
, packet
->data
, packet
->size
*8);
1500 if (packet
->type
>= 32 && packet
->type
< 48 && !fft_subpackets
[packet
->type
- 16])
1505 type
= packet
->type
;
1507 if ((type
>= 17 && type
< 24) || (type
>= 33 && type
< 40)) {
1508 int duration
= q
->sub_sampling
+ 5 - (type
& 15);
1510 if (duration
>= 0 && duration
< 4)
1511 qdm2_fft_decode_tones(q
, duration
, &gb
, unknown_flag
);
1512 } else if (type
== 31) {
1513 for (j
=0; j
< 4; j
++)
1514 qdm2_fft_decode_tones(q
, j
, &gb
, unknown_flag
);
1515 } else if (type
== 46) {
1516 for (j
=0; j
< 6; j
++)
1517 q
->fft_level_exp
[j
] = get_bits(&gb
, 6);
1518 for (j
=0; j
< 4; j
++)
1519 qdm2_fft_decode_tones(q
, j
, &gb
, unknown_flag
);
1521 } // Loop on B packets
1523 /* calculate maximum indexes for FFT coefficients */
1524 for (i
= 0, j
= -1; i
< 5; i
++)
1525 if (q
->fft_coefs_min_index
[i
] >= 0) {
1527 q
->fft_coefs_max_index
[j
] = q
->fft_coefs_min_index
[i
];
1531 q
->fft_coefs_max_index
[j
] = q
->fft_coefs_index
;
1535 static void qdm2_fft_generate_tone (QDM2Context
*q
, FFTTone
*tone
)
1540 const double iscale
= 2.0*M_PI
/ 512.0;
1542 tone
->phase
+= tone
->phase_shift
;
1544 /* calculate current level (maximum amplitude) of tone */
1545 level
= fft_tone_envelope_table
[tone
->duration
][tone
->time_index
] * tone
->level
;
1546 c
.im
= level
* sin(tone
->phase
*iscale
);
1547 c
.re
= level
* cos(tone
->phase
*iscale
);
1549 /* generate FFT coefficients for tone */
1550 if (tone
->duration
>= 3 || tone
->cutoff
>= 3) {
1551 tone
->complex[0].im
+= c
.im
;
1552 tone
->complex[0].re
+= c
.re
;
1553 tone
->complex[1].im
-= c
.im
;
1554 tone
->complex[1].re
-= c
.re
;
1556 f
[1] = -tone
->table
[4];
1557 f
[0] = tone
->table
[3] - tone
->table
[0];
1558 f
[2] = 1.0 - tone
->table
[2] - tone
->table
[3];
1559 f
[3] = tone
->table
[1] + tone
->table
[4] - 1.0;
1560 f
[4] = tone
->table
[0] - tone
->table
[1];
1561 f
[5] = tone
->table
[2];
1562 for (i
= 0; i
< 2; i
++) {
1563 tone
->complex[fft_cutoff_index_table
[tone
->cutoff
][i
]].re
+= c
.re
* f
[i
];
1564 tone
->complex[fft_cutoff_index_table
[tone
->cutoff
][i
]].im
+= c
.im
*((tone
->cutoff
<= i
) ? -f
[i
] : f
[i
]);
1566 for (i
= 0; i
< 4; i
++) {
1567 tone
->complex[i
].re
+= c
.re
* f
[i
+2];
1568 tone
->complex[i
].im
+= c
.im
* f
[i
+2];
1572 /* copy the tone if it has not yet died out */
1573 if (++tone
->time_index
< ((1 << (5 - tone
->duration
)) - 1)) {
1574 memcpy(&q
->fft_tones
[q
->fft_tone_end
], tone
, sizeof(FFTTone
));
1575 q
->fft_tone_end
= (q
->fft_tone_end
+ 1) % 1000;
1580 static void qdm2_fft_tone_synthesizer (QDM2Context
*q
, int sub_packet
)
1583 const double iscale
= 0.25 * M_PI
;
1585 for (ch
= 0; ch
< q
->channels
; ch
++) {
1586 memset(q
->fft
.complex[ch
], 0, q
->fft_size
* sizeof(QDM2Complex
));
1590 /* apply FFT tones with duration 4 (1 FFT period) */
1591 if (q
->fft_coefs_min_index
[4] >= 0)
1592 for (i
= q
->fft_coefs_min_index
[4]; i
< q
->fft_coefs_max_index
[4]; i
++) {
1596 if (q
->fft_coefs
[i
].sub_packet
!= sub_packet
)
1599 ch
= (q
->channels
== 1) ? 0 : q
->fft_coefs
[i
].channel
;
1600 level
= (q
->fft_coefs
[i
].exp
< 0) ? 0.0 : fft_tone_level_table
[q
->superblocktype_2_3
? 0 : 1][q
->fft_coefs
[i
].exp
& 63];
1602 c
.re
= level
* cos(q
->fft_coefs
[i
].phase
* iscale
);
1603 c
.im
= level
* sin(q
->fft_coefs
[i
].phase
* iscale
);
1604 q
->fft
.complex[ch
][q
->fft_coefs
[i
].offset
+ 0].re
+= c
.re
;
1605 q
->fft
.complex[ch
][q
->fft_coefs
[i
].offset
+ 0].im
+= c
.im
;
1606 q
->fft
.complex[ch
][q
->fft_coefs
[i
].offset
+ 1].re
-= c
.re
;
1607 q
->fft
.complex[ch
][q
->fft_coefs
[i
].offset
+ 1].im
-= c
.im
;
1610 /* generate existing FFT tones */
1611 for (i
= q
->fft_tone_end
; i
!= q
->fft_tone_start
; ) {
1612 qdm2_fft_generate_tone(q
, &q
->fft_tones
[q
->fft_tone_start
]);
1613 q
->fft_tone_start
= (q
->fft_tone_start
+ 1) % 1000;
1616 /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1617 for (i
= 0; i
< 4; i
++)
1618 if (q
->fft_coefs_min_index
[i
] >= 0) {
1619 for (j
= q
->fft_coefs_min_index
[i
]; j
< q
->fft_coefs_max_index
[i
]; j
++) {
1623 if (q
->fft_coefs
[j
].sub_packet
!= sub_packet
)
1627 offset
= q
->fft_coefs
[j
].offset
>> four_i
;
1628 ch
= (q
->channels
== 1) ? 0 : q
->fft_coefs
[j
].channel
;
1630 if (offset
< q
->frequency_range
) {
1632 tone
.cutoff
= offset
;
1634 tone
.cutoff
= (offset
>= 60) ? 3 : 2;
1636 tone
.level
= (q
->fft_coefs
[j
].exp
< 0) ? 0.0 : fft_tone_level_table
[q
->superblocktype_2_3
? 0 : 1][q
->fft_coefs
[j
].exp
& 63];
1637 tone
.complex = &q
->fft
.complex[ch
][offset
];
1638 tone
.table
= fft_tone_sample_table
[i
][q
->fft_coefs
[j
].offset
- (offset
<< four_i
)];
1639 tone
.phase
= 64 * q
->fft_coefs
[j
].phase
- (offset
<< 8) - 128;
1640 tone
.phase_shift
= (2 * q
->fft_coefs
[j
].offset
+ 1) << (7 - four_i
);
1642 tone
.time_index
= 0;
1644 qdm2_fft_generate_tone(q
, &tone
);
1647 q
->fft_coefs_min_index
[i
] = j
;
1652 static void qdm2_calculate_fft (QDM2Context
*q
, int channel
, int sub_packet
)
1654 const float gain
= (q
->channels
== 1 && q
->nb_channels
== 2) ? 0.5f
: 1.0f
;
1656 q
->fft
.complex[channel
][0].re
*= 2.0f
;
1657 q
->fft
.complex[channel
][0].im
= 0.0f
;
1658 ff_rdft_calc(&q
->rdft_ctx
, (FFTSample
*)q
->fft
.complex[channel
]);
1659 /* add samples to output buffer */
1660 for (i
= 0; i
< ((q
->fft_frame_size
+ 15) & ~15); i
++)
1661 q
->output_buffer
[q
->channels
* i
+ channel
] += ((float *) q
->fft
.complex[channel
])[i
] * gain
;
1667 * @param index subpacket number
1669 static void qdm2_synthesis_filter (QDM2Context
*q
, int index
)
1671 OUT_INT samples
[MPA_MAX_CHANNELS
* MPA_FRAME_SIZE
];
1672 int i
, k
, ch
, sb_used
, sub_sampling
, dither_state
= 0;
1674 /* copy sb_samples */
1675 sb_used
= QDM2_SB_USED(q
->sub_sampling
);
1677 for (ch
= 0; ch
< q
->channels
; ch
++)
1678 for (i
= 0; i
< 8; i
++)
1679 for (k
=sb_used
; k
< SBLIMIT
; k
++)
1680 q
->sb_samples
[ch
][(8 * index
) + i
][k
] = 0;
1682 for (ch
= 0; ch
< q
->nb_channels
; ch
++) {
1683 OUT_INT
*samples_ptr
= samples
+ ch
;
1685 for (i
= 0; i
< 8; i
++) {
1686 ff_mpa_synth_filter(q
->synth_buf
[ch
], &(q
->synth_buf_offset
[ch
]),
1687 mpa_window
, &dither_state
,
1688 samples_ptr
, q
->nb_channels
,
1689 q
->sb_samples
[ch
][(8 * index
) + i
]);
1690 samples_ptr
+= 32 * q
->nb_channels
;
1694 /* add samples to output buffer */
1695 sub_sampling
= (4 >> q
->sub_sampling
);
1697 for (ch
= 0; ch
< q
->channels
; ch
++)
1698 for (i
= 0; i
< q
->frame_size
; i
++)
1699 q
->output_buffer
[q
->channels
* i
+ ch
] += (float)(samples
[q
->nb_channels
* sub_sampling
* i
+ ch
] >> (sizeof(OUT_INT
)*8-16));
1704 * Init static data (does not depend on specific file)
1708 static av_cold
void qdm2_init(QDM2Context
*q
) {
1709 static int initialized
= 0;
1711 if (initialized
!= 0)
1716 ff_mpa_synth_init(mpa_window
);
1717 softclip_table_init();
1719 init_noise_samples();
1721 av_log(NULL
, AV_LOG_DEBUG
, "init done\n");
1726 static void dump_context(QDM2Context
*q
)
1729 #define PRINT(a,b) av_log(NULL,AV_LOG_DEBUG," %s = %d\n", a, b);
1730 PRINT("compressed_data",q
->compressed_data
);
1731 PRINT("compressed_size",q
->compressed_size
);
1732 PRINT("frame_size",q
->frame_size
);
1733 PRINT("checksum_size",q
->checksum_size
);
1734 PRINT("channels",q
->channels
);
1735 PRINT("nb_channels",q
->nb_channels
);
1736 PRINT("fft_frame_size",q
->fft_frame_size
);
1737 PRINT("fft_size",q
->fft_size
);
1738 PRINT("sub_sampling",q
->sub_sampling
);
1739 PRINT("fft_order",q
->fft_order
);
1740 PRINT("group_order",q
->group_order
);
1741 PRINT("group_size",q
->group_size
);
1742 PRINT("sub_packet",q
->sub_packet
);
1743 PRINT("frequency_range",q
->frequency_range
);
1744 PRINT("has_errors",q
->has_errors
);
1745 PRINT("fft_tone_end",q
->fft_tone_end
);
1746 PRINT("fft_tone_start",q
->fft_tone_start
);
1747 PRINT("fft_coefs_index",q
->fft_coefs_index
);
1748 PRINT("coeff_per_sb_select",q
->coeff_per_sb_select
);
1749 PRINT("cm_table_select",q
->cm_table_select
);
1750 PRINT("noise_idx",q
->noise_idx
);
1752 for (i
= q
->fft_tone_start
; i
< q
->fft_tone_end
; i
++)
1754 FFTTone
*t
= &q
->fft_tones
[i
];
1756 av_log(NULL
,AV_LOG_DEBUG
,"Tone (%d) dump:\n", i
);
1757 av_log(NULL
,AV_LOG_DEBUG
," level = %f\n", t
->level
);
1758 // PRINT(" level", t->level);
1759 PRINT(" phase", t
->phase
);
1760 PRINT(" phase_shift", t
->phase_shift
);
1761 PRINT(" duration", t
->duration
);
1762 PRINT(" samples_im", t
->samples_im
);
1763 PRINT(" samples_re", t
->samples_re
);
1764 PRINT(" table", t
->table
);
1772 * Init parameters from codec extradata
1774 static av_cold
int qdm2_decode_init(AVCodecContext
*avctx
)
1776 QDM2Context
*s
= avctx
->priv_data
;
1779 int tmp_val
, tmp
, size
;
1781 /* extradata parsing
1790 32 size (including this field)
1792 32 type (=QDM2 or QDMC)
1794 32 size (including this field, in bytes)
1795 32 tag (=QDCA) // maybe mandatory parameters
1798 32 samplerate (=44100)
1800 32 block size (=4096)
1801 32 frame size (=256) (for one channel)
1802 32 packet size (=1300)
1804 32 size (including this field, in bytes)
1805 32 tag (=QDCP) // maybe some tuneable parameters
1815 if (!avctx
->extradata
|| (avctx
->extradata_size
< 48)) {
1816 av_log(avctx
, AV_LOG_ERROR
, "extradata missing or truncated\n");
1820 extradata
= avctx
->extradata
;
1821 extradata_size
= avctx
->extradata_size
;
1823 while (extradata_size
> 7) {
1824 if (!memcmp(extradata
, "frmaQDM", 7))
1830 if (extradata_size
< 12) {
1831 av_log(avctx
, AV_LOG_ERROR
, "not enough extradata (%i)\n",
1836 if (memcmp(extradata
, "frmaQDM", 7)) {
1837 av_log(avctx
, AV_LOG_ERROR
, "invalid headers, QDM? not found\n");
1841 if (extradata
[7] == 'C') {
1843 av_log(avctx
, AV_LOG_ERROR
, "stream is QDMC version 1, which is not supported\n");
1848 extradata_size
-= 8;
1850 size
= AV_RB32(extradata
);
1852 if(size
> extradata_size
){
1853 av_log(avctx
, AV_LOG_ERROR
, "extradata size too small, %i < %i\n",
1854 extradata_size
, size
);
1859 av_log(avctx
, AV_LOG_DEBUG
, "size: %d\n", size
);
1860 if (AV_RB32(extradata
) != MKBETAG('Q','D','C','A')) {
1861 av_log(avctx
, AV_LOG_ERROR
, "invalid extradata, expecting QDCA\n");
1867 avctx
->channels
= s
->nb_channels
= s
->channels
= AV_RB32(extradata
);
1870 avctx
->sample_rate
= AV_RB32(extradata
);
1873 avctx
->bit_rate
= AV_RB32(extradata
);
1876 s
->group_size
= AV_RB32(extradata
);
1879 s
->fft_size
= AV_RB32(extradata
);
1882 s
->checksum_size
= AV_RB32(extradata
);
1884 s
->fft_order
= av_log2(s
->fft_size
) + 1;
1885 s
->fft_frame_size
= 2 * s
->fft_size
; // complex has two floats
1887 // something like max decodable tones
1888 s
->group_order
= av_log2(s
->group_size
) + 1;
1889 s
->frame_size
= s
->group_size
/ 16; // 16 iterations per super block
1891 s
->sub_sampling
= s
->fft_order
- 7;
1892 s
->frequency_range
= 255 / (1 << (2 - s
->sub_sampling
));
1894 switch ((s
->sub_sampling
* 2 + s
->channels
- 1)) {
1895 case 0: tmp
= 40; break;
1896 case 1: tmp
= 48; break;
1897 case 2: tmp
= 56; break;
1898 case 3: tmp
= 72; break;
1899 case 4: tmp
= 80; break;
1900 case 5: tmp
= 100;break;
1901 default: tmp
=s
->sub_sampling
; break;
1904 if ((tmp
* 1000) < avctx
->bit_rate
) tmp_val
= 1;
1905 if ((tmp
* 1440) < avctx
->bit_rate
) tmp_val
= 2;
1906 if ((tmp
* 1760) < avctx
->bit_rate
) tmp_val
= 3;
1907 if ((tmp
* 2240) < avctx
->bit_rate
) tmp_val
= 4;
1908 s
->cm_table_select
= tmp_val
;
1910 if (s
->sub_sampling
== 0)
1913 tmp
= ((-(s
->sub_sampling
-1)) & 8000) + 20000;
1920 s
->coeff_per_sb_select
= 0;
1921 else if (tmp
<= 16000)
1922 s
->coeff_per_sb_select
= 1;
1924 s
->coeff_per_sb_select
= 2;
1926 // Fail on unknown fft order
1927 if ((s
->fft_order
< 7) || (s
->fft_order
> 9)) {
1928 av_log(avctx
, AV_LOG_ERROR
, "Unknown FFT order (%d), contact the developers!\n", s
->fft_order
);
1932 ff_rdft_init(&s
->rdft_ctx
, s
->fft_order
, IRDFT
);
1936 avctx
->sample_fmt
= SAMPLE_FMT_S16
;
1943 static av_cold
int qdm2_decode_close(AVCodecContext
*avctx
)
1945 QDM2Context
*s
= avctx
->priv_data
;
1947 ff_rdft_end(&s
->rdft_ctx
);
1953 static void qdm2_decode (QDM2Context
*q
, const uint8_t *in
, int16_t *out
)
1956 const int frame_size
= (q
->frame_size
* q
->channels
);
1958 /* select input buffer */
1959 q
->compressed_data
= in
;
1960 q
->compressed_size
= q
->checksum_size
;
1964 /* copy old block, clear new block of output samples */
1965 memmove(q
->output_buffer
, &q
->output_buffer
[frame_size
], frame_size
* sizeof(float));
1966 memset(&q
->output_buffer
[frame_size
], 0, frame_size
* sizeof(float));
1968 /* decode block of QDM2 compressed data */
1969 if (q
->sub_packet
== 0) {
1970 q
->has_errors
= 0; // zero it for a new super block
1971 av_log(NULL
,AV_LOG_DEBUG
,"Superblock follows\n");
1972 qdm2_decode_super_block(q
);
1975 /* parse subpackets */
1976 if (!q
->has_errors
) {
1977 if (q
->sub_packet
== 2)
1978 qdm2_decode_fft_packets(q
);
1980 qdm2_fft_tone_synthesizer(q
, q
->sub_packet
);
1983 /* sound synthesis stage 1 (FFT) */
1984 for (ch
= 0; ch
< q
->channels
; ch
++) {
1985 qdm2_calculate_fft(q
, ch
, q
->sub_packet
);
1987 if (!q
->has_errors
&& q
->sub_packet_list_C
[0].packet
!= NULL
) {
1988 SAMPLES_NEEDED_2("has errors, and C list is not empty")
1993 /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1994 if (!q
->has_errors
&& q
->do_synth_filter
)
1995 qdm2_synthesis_filter(q
, q
->sub_packet
);
1997 q
->sub_packet
= (q
->sub_packet
+ 1) % 16;
1999 /* clip and convert output float[] to 16bit signed samples */
2000 for (i
= 0; i
< frame_size
; i
++) {
2001 int value
= (int)q
->output_buffer
[i
];
2003 if (value
> SOFTCLIP_THRESHOLD
)
2004 value
= (value
> HARDCLIP_THRESHOLD
) ? 32767 : softclip_table
[ value
- SOFTCLIP_THRESHOLD
];
2005 else if (value
< -SOFTCLIP_THRESHOLD
)
2006 value
= (value
< -HARDCLIP_THRESHOLD
) ? -32767 : -softclip_table
[-value
- SOFTCLIP_THRESHOLD
];
2013 static int qdm2_decode_frame(AVCodecContext
*avctx
,
2014 void *data
, int *data_size
,
2017 const uint8_t *buf
= avpkt
->data
;
2018 int buf_size
= avpkt
->size
;
2019 QDM2Context
*s
= avctx
->priv_data
;
2023 if(buf_size
< s
->checksum_size
)
2026 *data_size
= s
->channels
* s
->frame_size
* sizeof(int16_t);
2028 av_log(avctx
, AV_LOG_DEBUG
, "decode(%d): %p[%d] -> %p[%d]\n",
2029 buf_size
, buf
, s
->checksum_size
, data
, *data_size
);
2031 qdm2_decode(s
, buf
, data
);
2033 // reading only when next superblock found
2034 if (s
->sub_packet
== 0) {
2035 return s
->checksum_size
;
2041 AVCodec qdm2_decoder
=
2044 .type
= CODEC_TYPE_AUDIO
,
2045 .id
= CODEC_ID_QDM2
,
2046 .priv_data_size
= sizeof(QDM2Context
),
2047 .init
= qdm2_decode_init
,
2048 .close
= qdm2_decode_close
,
2049 .decode
= qdm2_decode_frame
,
2050 .long_name
= NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),