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