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