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