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