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