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