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