Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * MPEG Audio decoder | |
ff4ec49e | 3 | * Copyright (c) 2001, 2002 Fabrice Bellard. |
de6d9b64 | 4 | * |
ff4ec49e FB |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2 of the License, or (at your option) any later version. | |
de6d9b64 | 9 | * |
ff4ec49e | 10 | * This library is distributed in the hope that it will be useful, |
de6d9b64 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ff4ec49e FB |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. | |
de6d9b64 | 14 | * |
ff4ec49e FB |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the Free Software | |
5509bffa | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de6d9b64 | 18 | */ |
983e3246 MN |
19 | |
20 | /** | |
21 | * @file mpegaudiodec.c | |
22 | * MPEG Audio decoder. | |
115329f1 | 23 | */ |
983e3246 | 24 | |
239c2f4c | 25 | //#define DEBUG |
de6d9b64 | 26 | #include "avcodec.h" |
caa336b4 | 27 | #include "bitstream.h" |
84f986c0 | 28 | #include "dsputil.h" |
de6d9b64 FB |
29 | |
30 | /* | |
239c2f4c FB |
31 | * TODO: |
32 | * - in low precision mode, use more 16 bit multiplies in synth filter | |
33 | * - test lsf / mpeg25 extensively. | |
de6d9b64 FB |
34 | */ |
35 | ||
239c2f4c FB |
36 | /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg |
37 | audio decoder */ | |
81552334 | 38 | #ifdef CONFIG_MPEGAUDIO_HP |
5ab1972b | 39 | # define USE_HIGHPRECISION |
81552334 | 40 | #endif |
239c2f4c | 41 | |
d9b1c197 | 42 | #include "mpegaudio.h" |
a3a5f4d6 | 43 | |
239c2f4c FB |
44 | #define FRAC_ONE (1 << FRAC_BITS) |
45 | ||
47a0cd74 | 46 | #ifdef ARCH_X86 |
f617aded MN |
47 | # define MULL(ra, rb) \ |
48 | ({ int rt, dummy; asm (\ | |
49 | "imull %3 \n\t"\ | |
50 | "shrdl %4, %%edx, %%eax \n\t"\ | |
51 | : "=a"(rt), "=d"(dummy)\ | |
52 | : "a" (ra), "rm" (rb), "i"(FRAC_BITS));\ | |
53 | rt; }) | |
47a0cd74 MN |
54 | # define MUL64(ra, rb) \ |
55 | ({ int64_t rt; asm ("imull %2\n\t" : "=A"(rt) : "a" (ra), "g" (rb)); rt; }) | |
56 | # define MULH(ra, rb) \ | |
57 | ({ int rt, dummy; asm ("imull %3\n\t" : "=d"(rt), "=a"(dummy): "a" (ra), "rm" (rb)); rt; }) | |
69fd15f8 MN |
58 | #elif defined(ARCH_ARMV4L) |
59 | # define MULL(a, b) \ | |
60 | ({ int lo, hi;\ | |
61 | asm("smull %0, %1, %2, %3 \n\t"\ | |
acba3238 AJ |
62 | "mov %0, %0, lsr %4\n\t"\ |
63 | "add %1, %0, %1, lsl %5\n\t"\ | |
64 | : "=&r"(lo), "=&r"(hi)\ | |
69fd15f8 MN |
65 | : "r"(b), "r"(a), "i"(FRAC_BITS), "i"(32-FRAC_BITS));\ |
66 | hi; }) | |
67 | # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b)) | |
acba3238 | 68 | # define MULH(a, b) ({ int lo, hi; asm ("smull %0, %1, %2, %3" : "=&r"(lo), "=&r"(hi) : "r"(b), "r"(a)); hi; }) |
47a0cd74 MN |
69 | #else |
70 | # define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS) | |
71 | # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b)) | |
72 | //#define MULH(a,b) (((int64_t)(a) * (int64_t)(b))>>32) //gcc 3.4 creates an incredibly bloated mess out of this | |
73 | static always_inline int MULH(int a, int b){ | |
74 | return ((int64_t)(a) * (int64_t)(b))>>32; | |
75 | } | |
76 | #endif | |
239c2f4c FB |
77 | #define FIX(a) ((int)((a) * FRAC_ONE)) |
78 | /* WARNING: only correct for posititive numbers */ | |
79 | #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5)) | |
80 | #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS) | |
81 | ||
711ae726 | 82 | #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5)) |
711ae726 | 83 | |
239c2f4c FB |
84 | /****************/ |
85 | ||
de6d9b64 FB |
86 | #define HEADER_SIZE 4 |
87 | #define BACKSTEP_SIZE 512 | |
88 | ||
a1e257b2 MN |
89 | struct GranuleDef; |
90 | ||
de6d9b64 | 91 | typedef struct MPADecodeContext { |
bb270c08 | 92 | uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */ |
de6d9b64 | 93 | int inbuf_index; |
0c1a9eda | 94 | uint8_t *inbuf_ptr, *inbuf; |
de6d9b64 | 95 | int frame_size; |
239c2f4c FB |
96 | int free_format_frame_size; /* frame size in case of free format |
97 | (zero if currently unknown) */ | |
98 | /* next header (used in free format parsing) */ | |
115329f1 | 99 | uint32_t free_format_next_header; |
de6d9b64 FB |
100 | int error_protection; |
101 | int layer; | |
102 | int sample_rate; | |
239c2f4c | 103 | int sample_rate_index; /* between 0 and 8 */ |
de6d9b64 FB |
104 | int bit_rate; |
105 | int old_frame_size; | |
106 | GetBitContext gb; | |
239c2f4c FB |
107 | int nb_channels; |
108 | int mode; | |
109 | int mode_ext; | |
110 | int lsf; | |
a05c8d71 | 111 | MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16))); |
239c2f4c | 112 | int synth_buf_offset[MPA_MAX_CHANNELS]; |
a05c8d71 | 113 | int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16))); |
0c1a9eda | 114 | int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */ |
239c2f4c FB |
115 | #ifdef DEBUG |
116 | int frame_count; | |
117 | #endif | |
a1e257b2 | 118 | void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g); |
1ede228a | 119 | int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3 |
a7a85899 | 120 | unsigned int dither_state; |
de6d9b64 FB |
121 | } MPADecodeContext; |
122 | ||
d2a7718d RT |
123 | /** |
124 | * Context for MP3On4 decoder | |
125 | */ | |
126 | typedef struct MP3On4DecodeContext { | |
127 | int frames; ///< number of mp3 frames per block (number of mp3 decoder instances) | |
128 | int chan_cfg; ///< channel config number | |
129 | MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance | |
130 | } MP3On4DecodeContext; | |
131 | ||
239c2f4c FB |
132 | /* layer 3 "granule" */ |
133 | typedef struct GranuleDef { | |
0c1a9eda | 134 | uint8_t scfsi; |
239c2f4c FB |
135 | int part2_3_length; |
136 | int big_values; | |
137 | int global_gain; | |
138 | int scalefac_compress; | |
0c1a9eda ZK |
139 | uint8_t block_type; |
140 | uint8_t switch_point; | |
239c2f4c FB |
141 | int table_select[3]; |
142 | int subblock_gain[3]; | |
0c1a9eda ZK |
143 | uint8_t scalefac_scale; |
144 | uint8_t count1table_select; | |
239c2f4c FB |
145 | int region_size[3]; /* number of huffman codes in each region */ |
146 | int preflag; | |
147 | int short_start, long_end; /* long/short band indexes */ | |
0c1a9eda ZK |
148 | uint8_t scale_factors[40]; |
149 | int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */ | |
239c2f4c | 150 | } GranuleDef; |
de6d9b64 | 151 | |
239c2f4c FB |
152 | #define MODE_EXT_MS_STEREO 2 |
153 | #define MODE_EXT_I_STEREO 1 | |
154 | ||
155 | /* layer 3 huffman tables */ | |
156 | typedef struct HuffTable { | |
157 | int xsize; | |
0c1a9eda ZK |
158 | const uint8_t *bits; |
159 | const uint16_t *codes; | |
239c2f4c FB |
160 | } HuffTable; |
161 | ||
162 | #include "mpegaudiodectab.h" | |
163 | ||
a1e257b2 MN |
164 | static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g); |
165 | static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g); | |
166 | ||
239c2f4c | 167 | /* vlc structure for decoding layer 3 huffman tables */ |
115329f1 | 168 | static VLC huff_vlc[16]; |
239c2f4c FB |
169 | static VLC huff_quad_vlc[2]; |
170 | /* computed from band_size_long */ | |
0c1a9eda | 171 | static uint16_t band_index_long[9][23]; |
239c2f4c | 172 | /* XXX: free when all decoders are closed */ |
d04728bb | 173 | #define TABLE_4_3_SIZE (8191 + 16)*4 |
0c1a9eda | 174 | static int8_t *table_4_3_exp; |
0c1a9eda | 175 | static uint32_t *table_4_3_value; |
f8347c41 | 176 | static uint32_t exp_table[512]; |
55017f35 | 177 | static uint32_t expval_table[512][16]; |
239c2f4c | 178 | /* intensity stereo coef table */ |
0c1a9eda ZK |
179 | static int32_t is_table[2][16]; |
180 | static int32_t is_table_lsf[2][2][16]; | |
a1e257b2 MN |
181 | static int32_t csa_table[8][4]; |
182 | static float csa_table_float[8][4]; | |
0c1a9eda | 183 | static int32_t mdct_win[8][36]; |
239c2f4c FB |
184 | |
185 | /* lower 2 bits: modulo 3, higher bits: shift */ | |
0c1a9eda | 186 | static uint16_t scale_factor_modshift[64]; |
239c2f4c | 187 | /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */ |
0c1a9eda | 188 | static int32_t scale_factor_mult[15][3]; |
239c2f4c FB |
189 | /* mult table for layer 2 group quantization */ |
190 | ||
191 | #define SCALE_GEN(v) \ | |
192 | { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) } | |
193 | ||
c26ae41d | 194 | static const int32_t scale_factor_mult2[3][3] = { |
81552334 FB |
195 | SCALE_GEN(4.0 / 3.0), /* 3 steps */ |
196 | SCALE_GEN(4.0 / 5.0), /* 5 steps */ | |
197 | SCALE_GEN(4.0 / 9.0), /* 9 steps */ | |
239c2f4c FB |
198 | }; |
199 | ||
bf1f4da0 | 200 | void ff_mpa_synth_init(MPA_INT *window); |
a05c8d71 | 201 | static MPA_INT window[512] __attribute__((aligned(16))); |
115329f1 | 202 | |
239c2f4c FB |
203 | /* layer 1 unscaling */ |
204 | /* n = number of bits of the mantissa minus 1 */ | |
205 | static inline int l1_unscale(int n, int mant, int scale_factor) | |
206 | { | |
207 | int shift, mod; | |
0c1a9eda | 208 | int64_t val; |
239c2f4c FB |
209 | |
210 | shift = scale_factor_modshift[scale_factor]; | |
211 | mod = shift & 3; | |
212 | shift >>= 2; | |
213 | val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]); | |
214 | shift += n; | |
81552334 FB |
215 | /* NOTE: at this point, 1 <= shift >= 21 + 15 */ |
216 | return (int)((val + (1LL << (shift - 1))) >> shift); | |
239c2f4c FB |
217 | } |
218 | ||
219 | static inline int l2_unscale_group(int steps, int mant, int scale_factor) | |
220 | { | |
221 | int shift, mod, val; | |
222 | ||
223 | shift = scale_factor_modshift[scale_factor]; | |
224 | mod = shift & 3; | |
225 | shift >>= 2; | |
81552334 FB |
226 | |
227 | val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod]; | |
228 | /* NOTE: at this point, 0 <= shift <= 21 */ | |
229 | if (shift > 0) | |
230 | val = (val + (1 << (shift - 1))) >> shift; | |
231 | return val; | |
239c2f4c FB |
232 | } |
233 | ||
234 | /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */ | |
235 | static inline int l3_unscale(int value, int exponent) | |
236 | { | |
239c2f4c | 237 | unsigned int m; |
239c2f4c FB |
238 | int e; |
239 | ||
d04728bb MN |
240 | e = table_4_3_exp [4*value + (exponent&3)]; |
241 | m = table_4_3_value[4*value + (exponent&3)]; | |
242 | e -= (exponent >> 2); | |
243 | assert(e>=1); | |
239c2f4c | 244 | if (e > 31) |
b696d2a6 | 245 | return 0; |
239c2f4c | 246 | m = (m + (1 << (e-1))) >> e; |
d04728bb | 247 | |
239c2f4c | 248 | return m; |
239c2f4c FB |
249 | } |
250 | ||
f9ed4f88 FB |
251 | /* all integer n^(4/3) computation code */ |
252 | #define DEV_ORDER 13 | |
253 | ||
254 | #define POW_FRAC_BITS 24 | |
255 | #define POW_FRAC_ONE (1 << POW_FRAC_BITS) | |
256 | #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE)) | |
0c1a9eda | 257 | #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS) |
f9ed4f88 FB |
258 | |
259 | static int dev_4_3_coefs[DEV_ORDER]; | |
260 | ||
88730be6 | 261 | #if 0 /* unused */ |
f9ed4f88 FB |
262 | static int pow_mult3[3] = { |
263 | POW_FIX(1.0), | |
264 | POW_FIX(1.25992104989487316476), | |
265 | POW_FIX(1.58740105196819947474), | |
266 | }; | |
88730be6 | 267 | #endif |
f9ed4f88 FB |
268 | |
269 | static void int_pow_init(void) | |
270 | { | |
271 | int i, a; | |
272 | ||
273 | a = POW_FIX(1.0); | |
274 | for(i=0;i<DEV_ORDER;i++) { | |
275 | a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1); | |
276 | dev_4_3_coefs[i] = a; | |
277 | } | |
278 | } | |
279 | ||
88730be6 | 280 | #if 0 /* unused, remove? */ |
f9ed4f88 FB |
281 | /* return the mantissa and the binary exponent */ |
282 | static int int_pow(int i, int *exp_ptr) | |
283 | { | |
284 | int e, er, eq, j; | |
285 | int a, a1; | |
115329f1 | 286 | |
f9ed4f88 FB |
287 | /* renormalize */ |
288 | a = i; | |
289 | e = POW_FRAC_BITS; | |
290 | while (a < (1 << (POW_FRAC_BITS - 1))) { | |
291 | a = a << 1; | |
292 | e--; | |
293 | } | |
294 | a -= (1 << POW_FRAC_BITS); | |
295 | a1 = 0; | |
296 | for(j = DEV_ORDER - 1; j >= 0; j--) | |
297 | a1 = POW_MULL(a, dev_4_3_coefs[j] + a1); | |
298 | a = (1 << POW_FRAC_BITS) + a1; | |
299 | /* exponent compute (exact) */ | |
300 | e = e * 4; | |
301 | er = e % 3; | |
302 | eq = e / 3; | |
303 | a = POW_MULL(a, pow_mult3[er]); | |
304 | while (a >= 2 * POW_FRAC_ONE) { | |
305 | a = a >> 1; | |
306 | eq++; | |
307 | } | |
308 | /* convert to float */ | |
309 | while (a < POW_FRAC_ONE) { | |
310 | a = a << 1; | |
311 | eq--; | |
312 | } | |
59d3e367 | 313 | /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */ |
81552334 | 314 | #if POW_FRAC_BITS > FRAC_BITS |
59d3e367 FB |
315 | a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS); |
316 | /* correct overflow */ | |
317 | if (a >= 2 * (1 << FRAC_BITS)) { | |
318 | a = a >> 1; | |
319 | eq++; | |
320 | } | |
321 | #endif | |
f9ed4f88 | 322 | *exp_ptr = eq; |
f9ed4f88 | 323 | return a; |
f9ed4f88 | 324 | } |
88730be6 | 325 | #endif |
de6d9b64 FB |
326 | |
327 | static int decode_init(AVCodecContext * avctx) | |
328 | { | |
329 | MPADecodeContext *s = avctx->priv_data; | |
b587a7cb | 330 | static int init=0; |
239c2f4c | 331 | int i, j, k; |
de6d9b64 | 332 | |
e6885654 | 333 | #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT) |
a3a5f4d6 MN |
334 | avctx->sample_fmt= SAMPLE_FMT_S32; |
335 | #else | |
336 | avctx->sample_fmt= SAMPLE_FMT_S16; | |
115329f1 DB |
337 | #endif |
338 | ||
ce4a29c0 | 339 | if(avctx->antialias_algo != FF_AA_FLOAT) |
ac806113 MN |
340 | s->compute_antialias= compute_antialias_integer; |
341 | else | |
342 | s->compute_antialias= compute_antialias_float; | |
343 | ||
8c5b5683 | 344 | if (!init && !avctx->parse_only) { |
239c2f4c FB |
345 | /* scale factors table for layer 1/2 */ |
346 | for(i=0;i<64;i++) { | |
347 | int shift, mod; | |
348 | /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */ | |
81552334 | 349 | shift = (i / 3); |
239c2f4c | 350 | mod = i % 3; |
239c2f4c FB |
351 | scale_factor_modshift[i] = mod | (shift << 2); |
352 | } | |
353 | ||
354 | /* scale factor multiply for layer 1 */ | |
355 | for(i=0;i<15;i++) { | |
356 | int n, norm; | |
357 | n = i + 2; | |
0c1a9eda | 358 | norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1); |
81552334 FB |
359 | scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm); |
360 | scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm); | |
361 | scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm); | |
239c2f4c | 362 | dprintf("%d: norm=%x s=%x %x %x\n", |
115329f1 | 363 | i, norm, |
239c2f4c FB |
364 | scale_factor_mult[i][0], |
365 | scale_factor_mult[i][1], | |
366 | scale_factor_mult[i][2]); | |
367 | } | |
115329f1 | 368 | |
bb270c08 | 369 | ff_mpa_synth_init(window); |
115329f1 | 370 | |
239c2f4c | 371 | /* huffman decode tables */ |
239c2f4c FB |
372 | for(i=1;i<16;i++) { |
373 | const HuffTable *h = &mpa_huff_tables[i]; | |
bb270c08 | 374 | int xsize, x, y; |
5c91a675 | 375 | unsigned int n; |
c7aa3696 MN |
376 | uint8_t tmp_bits [256]; |
377 | uint16_t tmp_codes[256]; | |
378 | ||
379 | memset(tmp_bits , 0, sizeof(tmp_bits )); | |
380 | memset(tmp_codes, 0, sizeof(tmp_codes)); | |
239c2f4c FB |
381 | |
382 | xsize = h->xsize; | |
383 | n = xsize * xsize; | |
115329f1 | 384 | |
239c2f4c FB |
385 | j = 0; |
386 | for(x=0;x<xsize;x++) { | |
c7aa3696 MN |
387 | for(y=0;y<xsize;y++){ |
388 | tmp_bits [(x << 4) | y]= h->bits [j ]; | |
389 | tmp_codes[(x << 4) | y]= h->codes[j++]; | |
390 | } | |
239c2f4c | 391 | } |
c7aa3696 MN |
392 | |
393 | /* XXX: fail test */ | |
1f1d675b | 394 | init_vlc(&huff_vlc[i], 7, 256, |
c7aa3696 | 395 | tmp_bits, 1, 1, tmp_codes, 2, 2, 1); |
239c2f4c FB |
396 | } |
397 | for(i=0;i<2;i++) { | |
115329f1 | 398 | init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, |
073c2593 | 399 | mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1); |
239c2f4c FB |
400 | } |
401 | ||
402 | for(i=0;i<9;i++) { | |
403 | k = 0; | |
404 | for(j=0;j<22;j++) { | |
405 | band_index_long[i][j] = k; | |
406 | k += band_size_long[i][j]; | |
407 | } | |
408 | band_index_long[i][22] = k; | |
409 | } | |
410 | ||
bb270c08 DB |
411 | /* compute n ^ (4/3) and store it in mantissa/exp format */ |
412 | table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0])); | |
8d1f2ba5 | 413 | if(!table_4_3_exp) |
bb270c08 DB |
414 | return -1; |
415 | table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0])); | |
8d1f2ba5 | 416 | if(!table_4_3_value) |
239c2f4c | 417 | return -1; |
115329f1 | 418 | |
f9ed4f88 | 419 | int_pow_init(); |
239c2f4c | 420 | for(i=1;i<TABLE_4_3_SIZE;i++) { |
d04728bb | 421 | double f, fm; |
239c2f4c | 422 | int e, m; |
d04728bb MN |
423 | f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25); |
424 | fm = frexp(f, &e); | |
f86f4481 | 425 | m = (uint32_t)(fm*(1LL<<31) + 0.5); |
44f1698a | 426 | e+= FRAC_BITS - 31 + 5; |
d04728bb | 427 | |
239c2f4c FB |
428 | /* normalized to FRAC_BITS */ |
429 | table_4_3_value[i] = m; | |
d04728bb MN |
430 | // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0)); |
431 | table_4_3_exp[i] = -e; | |
239c2f4c | 432 | } |
55017f35 MN |
433 | for(i=0; i<512*16; i++){ |
434 | int exponent= (i>>4)-400; | |
435 | double f= pow(i&15, 4.0 / 3.0) * pow(2, exponent*0.25 + FRAC_BITS + 5); | |
436 | expval_table[exponent+400][i&15]= lrintf(f); | |
437 | if((i&15)==1) | |
438 | exp_table[exponent+400]= lrintf(f); | |
f8347c41 | 439 | } |
115329f1 | 440 | |
239c2f4c FB |
441 | for(i=0;i<7;i++) { |
442 | float f; | |
443 | int v; | |
444 | if (i != 6) { | |
445 | f = tan((double)i * M_PI / 12.0); | |
446 | v = FIXR(f / (1.0 + f)); | |
447 | } else { | |
448 | v = FIXR(1.0); | |
449 | } | |
450 | is_table[0][i] = v; | |
451 | is_table[1][6 - i] = v; | |
452 | } | |
453 | /* invalid values */ | |
454 | for(i=7;i<16;i++) | |
455 | is_table[0][i] = is_table[1][i] = 0.0; | |
456 | ||
457 | for(i=0;i<16;i++) { | |
458 | double f; | |
459 | int e, k; | |
460 | ||
461 | for(j=0;j<2;j++) { | |
462 | e = -(j + 1) * ((i + 1) >> 1); | |
463 | f = pow(2.0, e / 4.0); | |
464 | k = i & 1; | |
465 | is_table_lsf[j][k ^ 1][i] = FIXR(f); | |
466 | is_table_lsf[j][k][i] = FIXR(1.0); | |
115329f1 | 467 | dprintf("is_table_lsf %d %d: %x %x\n", |
239c2f4c FB |
468 | i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]); |
469 | } | |
470 | } | |
471 | ||
472 | for(i=0;i<8;i++) { | |
473 | float ci, cs, ca; | |
474 | ci = ci_table[i]; | |
475 | cs = 1.0 / sqrt(1.0 + ci * ci); | |
476 | ca = cs * ci; | |
ce4a29c0 MN |
477 | csa_table[i][0] = FIXHR(cs/4); |
478 | csa_table[i][1] = FIXHR(ca/4); | |
479 | csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4); | |
115329f1 | 480 | csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4); |
a1e257b2 MN |
481 | csa_table_float[i][0] = cs; |
482 | csa_table_float[i][1] = ca; | |
483 | csa_table_float[i][2] = ca + cs; | |
115329f1 | 484 | csa_table_float[i][3] = ca - cs; |
a1e257b2 | 485 | // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca)); |
711ae726 | 486 | // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs); |
239c2f4c FB |
487 | } |
488 | ||
489 | /* compute mdct windows */ | |
490 | for(i=0;i<36;i++) { | |
711ae726 MN |
491 | for(j=0; j<4; j++){ |
492 | double d; | |
115329f1 | 493 | |
125d6246 MN |
494 | if(j==2 && i%3 != 1) |
495 | continue; | |
115329f1 | 496 | |
711ae726 MN |
497 | d= sin(M_PI * (i + 0.5) / 36.0); |
498 | if(j==1){ | |
499 | if (i>=30) d= 0; | |
500 | else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0); | |
501 | else if(i>=18) d= 1; | |
502 | }else if(j==3){ | |
503 | if (i< 6) d= 0; | |
504 | else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0); | |
505 | else if(i< 18) d= 1; | |
506 | } | |
507 | //merge last stage of imdct into the window coefficients | |
125d6246 MN |
508 | d*= 0.5 / cos(M_PI*(2*i + 19)/72); |
509 | ||
510 | if(j==2) | |
511 | mdct_win[j][i/3] = FIXHR((d / (1<<5))); | |
512 | else | |
513 | mdct_win[j][i ] = FIXHR((d / (1<<5))); | |
711ae726 MN |
514 | // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5)); |
515 | } | |
239c2f4c FB |
516 | } |
517 | ||
239c2f4c FB |
518 | /* NOTE: we do frequency inversion adter the MDCT by changing |
519 | the sign of the right window coefs */ | |
520 | for(j=0;j<4;j++) { | |
521 | for(i=0;i<36;i+=2) { | |
522 | mdct_win[j + 4][i] = mdct_win[j][i]; | |
523 | mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1]; | |
524 | } | |
525 | } | |
526 | ||
527 | #if defined(DEBUG) | |
528 | for(j=0;j<8;j++) { | |
267f7edc | 529 | av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j); |
239c2f4c | 530 | for(i=0;i<36;i++) |
267f7edc SH |
531 | av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE); |
532 | av_log(avctx, AV_LOG_DEBUG, "\n"); | |
239c2f4c FB |
533 | } |
534 | #endif | |
de6d9b64 | 535 | init = 1; |
de6d9b64 FB |
536 | } |
537 | ||
538 | s->inbuf_index = 0; | |
539 | s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE]; | |
540 | s->inbuf_ptr = s->inbuf; | |
239c2f4c FB |
541 | #ifdef DEBUG |
542 | s->frame_count = 0; | |
543 | #endif | |
1ede228a RT |
544 | if (avctx->codec_id == CODEC_ID_MP3ADU) |
545 | s->adu_mode = 1; | |
de6d9b64 FB |
546 | return 0; |
547 | } | |
548 | ||
ef9f7306 | 549 | /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */ |
239c2f4c FB |
550 | |
551 | /* cos(i*pi/64) */ | |
552 | ||
0bd2483a MN |
553 | #define COS0_0 FIXHR(0.50060299823519630134/2) |
554 | #define COS0_1 FIXHR(0.50547095989754365998/2) | |
555 | #define COS0_2 FIXHR(0.51544730992262454697/2) | |
556 | #define COS0_3 FIXHR(0.53104259108978417447/2) | |
557 | #define COS0_4 FIXHR(0.55310389603444452782/2) | |
558 | #define COS0_5 FIXHR(0.58293496820613387367/2) | |
559 | #define COS0_6 FIXHR(0.62250412303566481615/2) | |
560 | #define COS0_7 FIXHR(0.67480834145500574602/2) | |
561 | #define COS0_8 FIXHR(0.74453627100229844977/2) | |
562 | #define COS0_9 FIXHR(0.83934964541552703873/2) | |
563 | #define COS0_10 FIXHR(0.97256823786196069369/2) | |
564 | #define COS0_11 FIXHR(1.16943993343288495515/4) | |
565 | #define COS0_12 FIXHR(1.48416461631416627724/4) | |
566 | #define COS0_13 FIXHR(2.05778100995341155085/8) | |
567 | #define COS0_14 FIXHR(3.40760841846871878570/8) | |
568 | #define COS0_15 FIXHR(10.19000812354805681150/32) | |
569 | ||
570 | #define COS1_0 FIXHR(0.50241928618815570551/2) | |
571 | #define COS1_1 FIXHR(0.52249861493968888062/2) | |
572 | #define COS1_2 FIXHR(0.56694403481635770368/2) | |
573 | #define COS1_3 FIXHR(0.64682178335999012954/2) | |
574 | #define COS1_4 FIXHR(0.78815462345125022473/2) | |
575 | #define COS1_5 FIXHR(1.06067768599034747134/4) | |
576 | #define COS1_6 FIXHR(1.72244709823833392782/4) | |
577 | #define COS1_7 FIXHR(5.10114861868916385802/16) | |
578 | ||
579 | #define COS2_0 FIXHR(0.50979557910415916894/2) | |
580 | #define COS2_1 FIXHR(0.60134488693504528054/2) | |
581 | #define COS2_2 FIXHR(0.89997622313641570463/2) | |
582 | #define COS2_3 FIXHR(2.56291544774150617881/8) | |
583 | ||
584 | #define COS3_0 FIXHR(0.54119610014619698439/2) | |
585 | #define COS3_1 FIXHR(1.30656296487637652785/4) | |
586 | ||
587 | #define COS4_0 FIXHR(0.70710678118654752439/2) | |
239c2f4c FB |
588 | |
589 | /* butterfly operator */ | |
0bd2483a | 590 | #define BF(a, b, c, s)\ |
239c2f4c FB |
591 | {\ |
592 | tmp0 = tab[a] + tab[b];\ | |
593 | tmp1 = tab[a] - tab[b];\ | |
594 | tab[a] = tmp0;\ | |
0bd2483a | 595 | tab[b] = MULH(tmp1<<(s), c);\ |
239c2f4c FB |
596 | } |
597 | ||
598 | #define BF1(a, b, c, d)\ | |
599 | {\ | |
0bd2483a MN |
600 | BF(a, b, COS4_0, 1);\ |
601 | BF(c, d,-COS4_0, 1);\ | |
239c2f4c FB |
602 | tab[c] += tab[d];\ |
603 | } | |
604 | ||
605 | #define BF2(a, b, c, d)\ | |
606 | {\ | |
0bd2483a MN |
607 | BF(a, b, COS4_0, 1);\ |
608 | BF(c, d,-COS4_0, 1);\ | |
239c2f4c FB |
609 | tab[c] += tab[d];\ |
610 | tab[a] += tab[c];\ | |
611 | tab[c] += tab[b];\ | |
612 | tab[b] += tab[d];\ | |
613 | } | |
614 | ||
615 | #define ADD(a, b) tab[a] += tab[b] | |
616 | ||
617 | /* DCT32 without 1/sqrt(2) coef zero scaling. */ | |
0c1a9eda | 618 | static void dct32(int32_t *out, int32_t *tab) |
239c2f4c FB |
619 | { |
620 | int tmp0, tmp1; | |
621 | ||
622 | /* pass 1 */ | |
0bd2483a MN |
623 | BF( 0, 31, COS0_0 , 1); |
624 | BF(15, 16, COS0_15, 5); | |
120aad7f | 625 | /* pass 2 */ |
0bd2483a MN |
626 | BF( 0, 15, COS1_0 , 1); |
627 | BF(16, 31,-COS1_0 , 1); | |
120aad7f | 628 | /* pass 1 */ |
0bd2483a MN |
629 | BF( 7, 24, COS0_7 , 1); |
630 | BF( 8, 23, COS0_8 , 1); | |
120aad7f | 631 | /* pass 2 */ |
0bd2483a MN |
632 | BF( 7, 8, COS1_7 , 4); |
633 | BF(23, 24,-COS1_7 , 4); | |
120aad7f | 634 | /* pass 3 */ |
0bd2483a MN |
635 | BF( 0, 7, COS2_0 , 1); |
636 | BF( 8, 15,-COS2_0 , 1); | |
637 | BF(16, 23, COS2_0 , 1); | |
638 | BF(24, 31,-COS2_0 , 1); | |
120aad7f | 639 | /* pass 1 */ |
0bd2483a MN |
640 | BF( 3, 28, COS0_3 , 1); |
641 | BF(12, 19, COS0_12, 2); | |
239c2f4c | 642 | /* pass 2 */ |
0bd2483a MN |
643 | BF( 3, 12, COS1_3 , 1); |
644 | BF(19, 28,-COS1_3 , 1); | |
120aad7f | 645 | /* pass 1 */ |
0bd2483a MN |
646 | BF( 4, 27, COS0_4 , 1); |
647 | BF(11, 20, COS0_11, 2); | |
120aad7f | 648 | /* pass 2 */ |
0bd2483a MN |
649 | BF( 4, 11, COS1_4 , 1); |
650 | BF(20, 27,-COS1_4 , 1); | |
120aad7f | 651 | /* pass 3 */ |
0bd2483a MN |
652 | BF( 3, 4, COS2_3 , 3); |
653 | BF(11, 12,-COS2_3 , 3); | |
654 | BF(19, 20, COS2_3 , 3); | |
655 | BF(27, 28,-COS2_3 , 3); | |
120aad7f | 656 | /* pass 4 */ |
0bd2483a MN |
657 | BF( 0, 3, COS3_0 , 1); |
658 | BF( 4, 7,-COS3_0 , 1); | |
659 | BF( 8, 11, COS3_0 , 1); | |
660 | BF(12, 15,-COS3_0 , 1); | |
661 | BF(16, 19, COS3_0 , 1); | |
662 | BF(20, 23,-COS3_0 , 1); | |
663 | BF(24, 27, COS3_0 , 1); | |
664 | BF(28, 31,-COS3_0 , 1); | |
115329f1 | 665 | |
120aad7f MN |
666 | |
667 | ||
668 | /* pass 1 */ | |
0bd2483a MN |
669 | BF( 1, 30, COS0_1 , 1); |
670 | BF(14, 17, COS0_14, 3); | |
120aad7f | 671 | /* pass 2 */ |
0bd2483a MN |
672 | BF( 1, 14, COS1_1 , 1); |
673 | BF(17, 30,-COS1_1 , 1); | |
120aad7f | 674 | /* pass 1 */ |
0bd2483a MN |
675 | BF( 6, 25, COS0_6 , 1); |
676 | BF( 9, 22, COS0_9 , 1); | |
120aad7f | 677 | /* pass 2 */ |
0bd2483a MN |
678 | BF( 6, 9, COS1_6 , 2); |
679 | BF(22, 25,-COS1_6 , 2); | |
239c2f4c | 680 | /* pass 3 */ |
0bd2483a MN |
681 | BF( 1, 6, COS2_1 , 1); |
682 | BF( 9, 14,-COS2_1 , 1); | |
683 | BF(17, 22, COS2_1 , 1); | |
684 | BF(25, 30,-COS2_1 , 1); | |
239c2f4c | 685 | |
120aad7f | 686 | /* pass 1 */ |
0bd2483a MN |
687 | BF( 2, 29, COS0_2 , 1); |
688 | BF(13, 18, COS0_13, 3); | |
120aad7f | 689 | /* pass 2 */ |
0bd2483a MN |
690 | BF( 2, 13, COS1_2 , 1); |
691 | BF(18, 29,-COS1_2 , 1); | |
120aad7f | 692 | /* pass 1 */ |
0bd2483a MN |
693 | BF( 5, 26, COS0_5 , 1); |
694 | BF(10, 21, COS0_10, 1); | |
120aad7f | 695 | /* pass 2 */ |
0bd2483a MN |
696 | BF( 5, 10, COS1_5 , 2); |
697 | BF(21, 26,-COS1_5 , 2); | |
120aad7f | 698 | /* pass 3 */ |
0bd2483a MN |
699 | BF( 2, 5, COS2_2 , 1); |
700 | BF(10, 13,-COS2_2 , 1); | |
701 | BF(18, 21, COS2_2 , 1); | |
702 | BF(26, 29,-COS2_2 , 1); | |
239c2f4c | 703 | /* pass 4 */ |
0bd2483a MN |
704 | BF( 1, 2, COS3_1 , 2); |
705 | BF( 5, 6,-COS3_1 , 2); | |
706 | BF( 9, 10, COS3_1 , 2); | |
707 | BF(13, 14,-COS3_1 , 2); | |
708 | BF(17, 18, COS3_1 , 2); | |
709 | BF(21, 22,-COS3_1 , 2); | |
710 | BF(25, 26, COS3_1 , 2); | |
711 | BF(29, 30,-COS3_1 , 2); | |
115329f1 | 712 | |
239c2f4c | 713 | /* pass 5 */ |
0bd2483a MN |
714 | BF1( 0, 1, 2, 3); |
715 | BF2( 4, 5, 6, 7); | |
716 | BF1( 8, 9, 10, 11); | |
239c2f4c FB |
717 | BF2(12, 13, 14, 15); |
718 | BF1(16, 17, 18, 19); | |
719 | BF2(20, 21, 22, 23); | |
720 | BF1(24, 25, 26, 27); | |
721 | BF2(28, 29, 30, 31); | |
115329f1 | 722 | |
239c2f4c | 723 | /* pass 6 */ |
115329f1 | 724 | |
239c2f4c FB |
725 | ADD( 8, 12); |
726 | ADD(12, 10); | |
727 | ADD(10, 14); | |
728 | ADD(14, 9); | |
729 | ADD( 9, 13); | |
730 | ADD(13, 11); | |
731 | ADD(11, 15); | |
732 | ||
733 | out[ 0] = tab[0]; | |
734 | out[16] = tab[1]; | |
735 | out[ 8] = tab[2]; | |
736 | out[24] = tab[3]; | |
737 | out[ 4] = tab[4]; | |
738 | out[20] = tab[5]; | |
739 | out[12] = tab[6]; | |
740 | out[28] = tab[7]; | |
741 | out[ 2] = tab[8]; | |
742 | out[18] = tab[9]; | |
743 | out[10] = tab[10]; | |
744 | out[26] = tab[11]; | |
745 | out[ 6] = tab[12]; | |
746 | out[22] = tab[13]; | |
747 | out[14] = tab[14]; | |
748 | out[30] = tab[15]; | |
115329f1 | 749 | |
239c2f4c FB |
750 | ADD(24, 28); |
751 | ADD(28, 26); | |
752 | ADD(26, 30); | |
753 | ADD(30, 25); | |
754 | ADD(25, 29); | |
755 | ADD(29, 27); | |
756 | ADD(27, 31); | |
757 | ||
758 | out[ 1] = tab[16] + tab[24]; | |
759 | out[17] = tab[17] + tab[25]; | |
760 | out[ 9] = tab[18] + tab[26]; | |
761 | out[25] = tab[19] + tab[27]; | |
762 | out[ 5] = tab[20] + tab[28]; | |
763 | out[21] = tab[21] + tab[29]; | |
764 | out[13] = tab[22] + tab[30]; | |
765 | out[29] = tab[23] + tab[31]; | |
766 | out[ 3] = tab[24] + tab[20]; | |
767 | out[19] = tab[25] + tab[21]; | |
768 | out[11] = tab[26] + tab[22]; | |
769 | out[27] = tab[27] + tab[23]; | |
770 | out[ 7] = tab[28] + tab[18]; | |
771 | out[23] = tab[29] + tab[19]; | |
772 | out[15] = tab[30] + tab[17]; | |
773 | out[31] = tab[31]; | |
774 | } | |
775 | ||
239c2f4c FB |
776 | #if FRAC_BITS <= 15 |
777 | ||
a7a85899 | 778 | static inline int round_sample(int *sum) |
8c5b5683 FB |
779 | { |
780 | int sum1; | |
a7a85899 MN |
781 | sum1 = (*sum) >> OUT_SHIFT; |
782 | *sum &= (1<<OUT_SHIFT)-1; | |
a3a5f4d6 MN |
783 | if (sum1 < OUT_MIN) |
784 | sum1 = OUT_MIN; | |
785 | else if (sum1 > OUT_MAX) | |
786 | sum1 = OUT_MAX; | |
8c5b5683 | 787 | return sum1; |
239c2f4c FB |
788 | } |
789 | ||
5ab1972b MN |
790 | # if defined(ARCH_POWERPC_405) |
791 | /* signed 16x16 -> 32 multiply add accumulate */ | |
792 | # define MACS(rt, ra, rb) \ | |
793 | asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb)); | |
794 | ||
795 | /* signed 16x16 -> 32 multiply */ | |
796 | # define MULS(ra, rb) \ | |
797 | ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; }) | |
798 | # else | |
799 | /* signed 16x16 -> 32 multiply add accumulate */ | |
800 | # define MACS(rt, ra, rb) rt += (ra) * (rb) | |
801 | ||
802 | /* signed 16x16 -> 32 multiply */ | |
803 | # define MULS(ra, rb) ((ra) * (rb)) | |
804 | # endif | |
8c5b5683 FB |
805 | #else |
806 | ||
115329f1 | 807 | static inline int round_sample(int64_t *sum) |
8c5b5683 FB |
808 | { |
809 | int sum1; | |
a7a85899 MN |
810 | sum1 = (int)((*sum) >> OUT_SHIFT); |
811 | *sum &= (1<<OUT_SHIFT)-1; | |
a3a5f4d6 MN |
812 | if (sum1 < OUT_MIN) |
813 | sum1 = OUT_MIN; | |
814 | else if (sum1 > OUT_MAX) | |
815 | sum1 = OUT_MAX; | |
8c5b5683 | 816 | return sum1; |
239c2f4c FB |
817 | } |
818 | ||
47a0cd74 | 819 | # define MULS(ra, rb) MUL64(ra, rb) |
8c5b5683 FB |
820 | #endif |
821 | ||
822 | #define SUM8(sum, op, w, p) \ | |
239c2f4c | 823 | { \ |
8c5b5683 FB |
824 | sum op MULS((w)[0 * 64], p[0 * 64]);\ |
825 | sum op MULS((w)[1 * 64], p[1 * 64]);\ | |
826 | sum op MULS((w)[2 * 64], p[2 * 64]);\ | |
827 | sum op MULS((w)[3 * 64], p[3 * 64]);\ | |
828 | sum op MULS((w)[4 * 64], p[4 * 64]);\ | |
829 | sum op MULS((w)[5 * 64], p[5 * 64]);\ | |
830 | sum op MULS((w)[6 * 64], p[6 * 64]);\ | |
831 | sum op MULS((w)[7 * 64], p[7 * 64]);\ | |
832 | } | |
833 | ||
834 | #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \ | |
835 | { \ | |
836 | int tmp;\ | |
837 | tmp = p[0 * 64];\ | |
838 | sum1 op1 MULS((w1)[0 * 64], tmp);\ | |
839 | sum2 op2 MULS((w2)[0 * 64], tmp);\ | |
840 | tmp = p[1 * 64];\ | |
841 | sum1 op1 MULS((w1)[1 * 64], tmp);\ | |
842 | sum2 op2 MULS((w2)[1 * 64], tmp);\ | |
843 | tmp = p[2 * 64];\ | |
844 | sum1 op1 MULS((w1)[2 * 64], tmp);\ | |
845 | sum2 op2 MULS((w2)[2 * 64], tmp);\ | |
846 | tmp = p[3 * 64];\ | |
847 | sum1 op1 MULS((w1)[3 * 64], tmp);\ | |
848 | sum2 op2 MULS((w2)[3 * 64], tmp);\ | |
849 | tmp = p[4 * 64];\ | |
850 | sum1 op1 MULS((w1)[4 * 64], tmp);\ | |
851 | sum2 op2 MULS((w2)[4 * 64], tmp);\ | |
852 | tmp = p[5 * 64];\ | |
853 | sum1 op1 MULS((w1)[5 * 64], tmp);\ | |
854 | sum2 op2 MULS((w2)[5 * 64], tmp);\ | |
855 | tmp = p[6 * 64];\ | |
856 | sum1 op1 MULS((w1)[6 * 64], tmp);\ | |
857 | sum2 op2 MULS((w2)[6 * 64], tmp);\ | |
858 | tmp = p[7 * 64];\ | |
859 | sum1 op1 MULS((w1)[7 * 64], tmp);\ | |
860 | sum2 op2 MULS((w2)[7 * 64], tmp);\ | |
239c2f4c FB |
861 | } |
862 | ||
bf1f4da0 AB |
863 | void ff_mpa_synth_init(MPA_INT *window) |
864 | { | |
865 | int i; | |
866 | ||
867 | /* max = 18760, max sum over all 16 coefs : 44736 */ | |
868 | for(i=0;i<257;i++) { | |
869 | int v; | |
870 | v = mpa_enwindow[i]; | |
871 | #if WFRAC_BITS < 16 | |
872 | v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS); | |
873 | #endif | |
874 | window[i] = v; | |
875 | if ((i & 63) != 0) | |
876 | v = -v; | |
877 | if (i != 0) | |
878 | window[512 - i] = v; | |
115329f1 | 879 | } |
bf1f4da0 | 880 | } |
239c2f4c FB |
881 | |
882 | /* 32 sub band synthesis filter. Input: 32 sub band samples, Output: | |
883 | 32 samples. */ | |
884 | /* XXX: optimize by avoiding ring buffer usage */ | |
bf1f4da0 | 885 | void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, |
bb270c08 | 886 | MPA_INT *window, int *dither_state, |
115329f1 | 887 | OUT_INT *samples, int incr, |
0c1a9eda | 888 | int32_t sb_samples[SBLIMIT]) |
239c2f4c | 889 | { |
0c1a9eda | 890 | int32_t tmp[32]; |
8c5b5683 | 891 | register MPA_INT *synth_buf; |
491c4a10 | 892 | register const MPA_INT *w, *w2, *p; |
239c2f4c | 893 | int j, offset, v; |
a3a5f4d6 | 894 | OUT_INT *samples2; |
239c2f4c | 895 | #if FRAC_BITS <= 15 |
8c5b5683 | 896 | int sum, sum2; |
239c2f4c | 897 | #else |
8c5b5683 | 898 | int64_t sum, sum2; |
239c2f4c | 899 | #endif |
bf1f4da0 | 900 | |
239c2f4c | 901 | dct32(tmp, sb_samples); |
115329f1 | 902 | |
bf1f4da0 AB |
903 | offset = *synth_buf_offset; |
904 | synth_buf = synth_buf_ptr + offset; | |
239c2f4c FB |
905 | |
906 | for(j=0;j<32;j++) { | |
907 | v = tmp[j]; | |
908 | #if FRAC_BITS <= 15 | |
81552334 FB |
909 | /* NOTE: can cause a loss in precision if very high amplitude |
910 | sound */ | |
239c2f4c FB |
911 | if (v > 32767) |
912 | v = 32767; | |
913 | else if (v < -32768) | |
914 | v = -32768; | |
915 | #endif | |
916 | synth_buf[j] = v; | |
917 | } | |
918 | /* copy to avoid wrap */ | |
919 | memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT)); | |
920 | ||
8c5b5683 | 921 | samples2 = samples + 31 * incr; |
239c2f4c | 922 | w = window; |
8c5b5683 FB |
923 | w2 = window + 31; |
924 | ||
093c6e50 | 925 | sum = *dither_state; |
8c5b5683 FB |
926 | p = synth_buf + 16; |
927 | SUM8(sum, +=, w, p); | |
928 | p = synth_buf + 48; | |
929 | SUM8(sum, -=, w + 32, p); | |
a7a85899 | 930 | *samples = round_sample(&sum); |
8c5b5683 | 931 | samples += incr; |
239c2f4c FB |
932 | w++; |
933 | ||
8c5b5683 FB |
934 | /* we calculate two samples at the same time to avoid one memory |
935 | access per two sample */ | |
936 | for(j=1;j<16;j++) { | |
8c5b5683 FB |
937 | sum2 = 0; |
938 | p = synth_buf + 16 + j; | |
939 | SUM8P2(sum, +=, sum2, -=, w, w2, p); | |
940 | p = synth_buf + 48 - j; | |
941 | SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p); | |
942 | ||
a7a85899 | 943 | *samples = round_sample(&sum); |
8c5b5683 | 944 | samples += incr; |
a7a85899 MN |
945 | sum += sum2; |
946 | *samples2 = round_sample(&sum); | |
8c5b5683 | 947 | samples2 -= incr; |
239c2f4c | 948 | w++; |
8c5b5683 | 949 | w2--; |
239c2f4c | 950 | } |
115329f1 | 951 | |
8c5b5683 | 952 | p = synth_buf + 32; |
8c5b5683 | 953 | SUM8(sum, -=, w + 32, p); |
a7a85899 | 954 | *samples = round_sample(&sum); |
093c6e50 | 955 | *dither_state= sum; |
8c5b5683 | 956 | |
239c2f4c | 957 | offset = (offset - 32) & 511; |
bf1f4da0 | 958 | *synth_buf_offset = offset; |
239c2f4c FB |
959 | } |
960 | ||
125d6246 MN |
961 | #define C3 FIXHR(0.86602540378443864676/2) |
962 | ||
963 | /* 0.5 / cos(pi*(2*i+1)/36) */ | |
964 | static const int icos36[9] = { | |
965 | FIXR(0.50190991877167369479), | |
966 | FIXR(0.51763809020504152469), //0 | |
967 | FIXR(0.55168895948124587824), | |
968 | FIXR(0.61038729438072803416), | |
969 | FIXR(0.70710678118654752439), //1 | |
970 | FIXR(0.87172339781054900991), | |
971 | FIXR(1.18310079157624925896), | |
972 | FIXR(1.93185165257813657349), //2 | |
973 | FIXR(5.73685662283492756461), | |
974 | }; | |
239c2f4c | 975 | |
eb644776 MN |
976 | /* 0.5 / cos(pi*(2*i+1)/36) */ |
977 | static const int icos36h[9] = { | |
978 | FIXHR(0.50190991877167369479/2), | |
979 | FIXHR(0.51763809020504152469/2), //0 | |
980 | FIXHR(0.55168895948124587824/2), | |
981 | FIXHR(0.61038729438072803416/2), | |
982 | FIXHR(0.70710678118654752439/2), //1 | |
983 | FIXHR(0.87172339781054900991/2), | |
984 | FIXHR(1.18310079157624925896/4), | |
985 | FIXHR(1.93185165257813657349/4), //2 | |
986 | // FIXHR(5.73685662283492756461), | |
987 | }; | |
988 | ||
239c2f4c FB |
989 | /* 12 points IMDCT. We compute it "by hand" by factorizing obvious |
990 | cases. */ | |
991 | static void imdct12(int *out, int *in) | |
992 | { | |
125d6246 | 993 | int in0, in1, in2, in3, in4, in5, t1, t2; |
44f1698a MN |
994 | |
995 | in0= in[0*3]; | |
996 | in1= in[1*3] + in[0*3]; | |
997 | in2= in[2*3] + in[1*3]; | |
998 | in3= in[3*3] + in[2*3]; | |
999 | in4= in[4*3] + in[3*3]; | |
1000 | in5= in[5*3] + in[4*3]; | |
125d6246 MN |
1001 | in5 += in3; |
1002 | in3 += in1; | |
1003 | ||
1004 | in2= MULH(2*in2, C3); | |
eb644776 | 1005 | in3= MULH(4*in3, C3); |
115329f1 | 1006 | |
125d6246 | 1007 | t1 = in0 - in4; |
eb644776 | 1008 | t2 = MULH(2*(in1 - in5), icos36h[4]); |
125d6246 | 1009 | |
115329f1 | 1010 | out[ 7]= |
125d6246 MN |
1011 | out[10]= t1 + t2; |
1012 | out[ 1]= | |
1013 | out[ 4]= t1 - t2; | |
1014 | ||
1015 | in0 += in4>>1; | |
1016 | in4 = in0 + in2; | |
eb644776 MN |
1017 | in5 += 2*in1; |
1018 | in1 = MULH(in5 + in3, icos36h[1]); | |
115329f1 | 1019 | out[ 8]= |
eb644776 | 1020 | out[ 9]= in4 + in1; |
125d6246 | 1021 | out[ 2]= |
eb644776 | 1022 | out[ 3]= in4 - in1; |
115329f1 | 1023 | |
125d6246 | 1024 | in0 -= in2; |
eb644776 | 1025 | in5 = MULH(2*(in5 - in3), icos36h[7]); |
125d6246 | 1026 | out[ 0]= |
eb644776 | 1027 | out[ 5]= in0 - in5; |
125d6246 | 1028 | out[ 6]= |
eb644776 | 1029 | out[11]= in0 + in5; |
239c2f4c FB |
1030 | } |
1031 | ||
239c2f4c | 1032 | /* cos(pi*i/18) */ |
711ae726 MN |
1033 | #define C1 FIXHR(0.98480775301220805936/2) |
1034 | #define C2 FIXHR(0.93969262078590838405/2) | |
1035 | #define C3 FIXHR(0.86602540378443864676/2) | |
1036 | #define C4 FIXHR(0.76604444311897803520/2) | |
1037 | #define C5 FIXHR(0.64278760968653932632/2) | |
1038 | #define C6 FIXHR(0.5/2) | |
1039 | #define C7 FIXHR(0.34202014332566873304/2) | |
1040 | #define C8 FIXHR(0.17364817766693034885/2) | |
1041 | ||
239c2f4c | 1042 | |
239c2f4c | 1043 | /* using Lee like decomposition followed by hand coded 9 points DCT */ |
711ae726 | 1044 | static void imdct36(int *out, int *buf, int *in, int *win) |
239c2f4c FB |
1045 | { |
1046 | int i, j, t0, t1, t2, t3, s0, s1, s2, s3; | |
1047 | int tmp[18], *tmp1, *in1; | |
239c2f4c FB |
1048 | |
1049 | for(i=17;i>=1;i--) | |
1050 | in[i] += in[i-1]; | |
1051 | for(i=17;i>=3;i-=2) | |
1052 | in[i] += in[i-2]; | |
1053 | ||
1054 | for(j=0;j<2;j++) { | |
1055 | tmp1 = tmp + j; | |
1056 | in1 = in + j; | |
711ae726 MN |
1057 | #if 0 |
1058 | //more accurate but slower | |
1059 | int64_t t0, t1, t2, t3; | |
1060 | t2 = in1[2*4] + in1[2*8] - in1[2*2]; | |
115329f1 | 1061 | |
711ae726 MN |
1062 | t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32; |
1063 | t1 = in1[2*0] - in1[2*6]; | |
1064 | tmp1[ 6] = t1 - (t2>>1); | |
1065 | tmp1[16] = t1 + t2; | |
1066 | ||
1067 | t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2); | |
1068 | t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8); | |
1069 | t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4); | |
115329f1 | 1070 | |
711ae726 MN |
1071 | tmp1[10] = (t3 - t0 - t2) >> 32; |
1072 | tmp1[ 2] = (t3 + t0 + t1) >> 32; | |
1073 | tmp1[14] = (t3 + t2 - t1) >> 32; | |
115329f1 | 1074 | |
711ae726 MN |
1075 | tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3); |
1076 | t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1); | |
1077 | t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7); | |
1078 | t0 = MUL64(2*in1[2*3], C3); | |
1079 | ||
1080 | t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5); | |
1081 | ||
1082 | tmp1[ 0] = (t2 + t3 + t0) >> 32; | |
1083 | tmp1[12] = (t2 + t1 - t0) >> 32; | |
1084 | tmp1[ 8] = (t3 - t1 - t0) >> 32; | |
1085 | #else | |
1086 | t2 = in1[2*4] + in1[2*8] - in1[2*2]; | |
115329f1 | 1087 | |
711ae726 MN |
1088 | t3 = in1[2*0] + (in1[2*6]>>1); |
1089 | t1 = in1[2*0] - in1[2*6]; | |
1090 | tmp1[ 6] = t1 - (t2>>1); | |
1091 | tmp1[16] = t1 + t2; | |
1092 | ||
1093 | t0 = MULH(2*(in1[2*2] + in1[2*4]), C2); | |
1094 | t1 = MULH( in1[2*4] - in1[2*8] , -2*C8); | |
1095 | t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4); | |
115329f1 | 1096 | |
711ae726 MN |
1097 | tmp1[10] = t3 - t0 - t2; |
1098 | tmp1[ 2] = t3 + t0 + t1; | |
1099 | tmp1[14] = t3 + t2 - t1; | |
115329f1 | 1100 | |
711ae726 MN |
1101 | tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3); |
1102 | t2 = MULH(2*(in1[2*1] + in1[2*5]), C1); | |
1103 | t3 = MULH( in1[2*5] - in1[2*7] , -2*C7); | |
1104 | t0 = MULH(2*in1[2*3], C3); | |
239c2f4c | 1105 | |
711ae726 MN |
1106 | t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5); |
1107 | ||
1108 | tmp1[ 0] = t2 + t3 + t0; | |
1109 | tmp1[12] = t2 + t1 - t0; | |
1110 | tmp1[ 8] = t3 - t1 - t0; | |
1111 | #endif | |
239c2f4c FB |
1112 | } |
1113 | ||
1114 | i = 0; | |
1115 | for(j=0;j<4;j++) { | |
1116 | t0 = tmp[i]; | |
1117 | t1 = tmp[i + 2]; | |
1118 | s0 = t1 + t0; | |
1119 | s2 = t1 - t0; | |
1120 | ||
1121 | t2 = tmp[i + 1]; | |
1122 | t3 = tmp[i + 3]; | |
eb644776 | 1123 | s1 = MULH(2*(t3 + t2), icos36h[j]); |
239c2f4c | 1124 | s3 = MULL(t3 - t2, icos36[8 - j]); |
115329f1 | 1125 | |
44f1698a MN |
1126 | t0 = s0 + s1; |
1127 | t1 = s0 - s1; | |
125d6246 | 1128 | out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j]; |
711ae726 MN |
1129 | out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j]; |
1130 | buf[9 + j] = MULH(t0, win[18 + 9 + j]); | |
1131 | buf[8 - j] = MULH(t0, win[18 + 8 - j]); | |
115329f1 | 1132 | |
44f1698a MN |
1133 | t0 = s2 + s3; |
1134 | t1 = s2 - s3; | |
125d6246 | 1135 | out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j]; |
711ae726 MN |
1136 | out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j]; |
1137 | buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]); | |
1138 | buf[ + j] = MULH(t0, win[18 + j]); | |
239c2f4c FB |
1139 | i += 4; |
1140 | } | |
1141 | ||
1142 | s0 = tmp[16]; | |
eb644776 | 1143 | s1 = MULH(2*tmp[17], icos36h[4]); |
44f1698a MN |
1144 | t0 = s0 + s1; |
1145 | t1 = s0 - s1; | |
125d6246 | 1146 | out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4]; |
711ae726 MN |
1147 | out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4]; |
1148 | buf[9 + 4] = MULH(t0, win[18 + 9 + 4]); | |
1149 | buf[8 - 4] = MULH(t0, win[18 + 8 - 4]); | |
239c2f4c FB |
1150 | } |
1151 | ||
de6d9b64 | 1152 | /* header decoding. MUST check the header before because no |
239c2f4c FB |
1153 | consistency check is done there. Return 1 if free format found and |
1154 | that the frame size must be computed externally */ | |
0c1a9eda | 1155 | static int decode_header(MPADecodeContext *s, uint32_t header) |
de6d9b64 | 1156 | { |
239c2f4c FB |
1157 | int sample_rate, frame_size, mpeg25, padding; |
1158 | int sample_rate_index, bitrate_index; | |
de6d9b64 | 1159 | if (header & (1<<20)) { |
239c2f4c FB |
1160 | s->lsf = (header & (1<<19)) ? 0 : 1; |
1161 | mpeg25 = 0; | |
de6d9b64 | 1162 | } else { |
239c2f4c FB |
1163 | s->lsf = 1; |
1164 | mpeg25 = 1; | |
de6d9b64 | 1165 | } |
115329f1 | 1166 | |
de6d9b64 FB |
1167 | s->layer = 4 - ((header >> 17) & 3); |
1168 | /* extract frequency */ | |
239c2f4c FB |
1169 | sample_rate_index = (header >> 10) & 3; |
1170 | sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25); | |
1171 | sample_rate_index += 3 * (s->lsf + mpeg25); | |
1172 | s->sample_rate_index = sample_rate_index; | |
1173 | s->error_protection = ((header >> 16) & 1) ^ 1; | |
81552334 | 1174 | s->sample_rate = sample_rate; |
de6d9b64 | 1175 | |
239c2f4c FB |
1176 | bitrate_index = (header >> 12) & 0xf; |
1177 | padding = (header >> 9) & 1; | |
1178 | //extension = (header >> 8) & 1; | |
1179 | s->mode = (header >> 6) & 3; | |
1180 | s->mode_ext = (header >> 4) & 3; | |
1181 | //copyright = (header >> 3) & 1; | |
1182 | //original = (header >> 2) & 1; | |
1183 | //emphasis = header & 3; | |
de6d9b64 | 1184 | |
239c2f4c FB |
1185 | if (s->mode == MPA_MONO) |
1186 | s->nb_channels = 1; | |
1187 | else | |
1188 | s->nb_channels = 2; | |
115329f1 | 1189 | |
239c2f4c FB |
1190 | if (bitrate_index != 0) { |
1191 | frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index]; | |
1192 | s->bit_rate = frame_size * 1000; | |
1193 | switch(s->layer) { | |
1194 | case 1: | |
1195 | frame_size = (frame_size * 12000) / sample_rate; | |
1196 | frame_size = (frame_size + padding) * 4; | |
1197 | break; | |
1198 | case 2: | |
1199 | frame_size = (frame_size * 144000) / sample_rate; | |
1200 | frame_size += padding; | |
1201 | break; | |
1202 | default: | |
1203 | case 3: | |
1204 | frame_size = (frame_size * 144000) / (sample_rate << s->lsf); | |
1205 | frame_size += padding; | |
1206 | break; | |
1207 | } | |
1208 | s->frame_size = frame_size; | |
1209 | } else { | |
1210 | /* if no frame size computed, signal it */ | |
1211 | if (!s->free_format_frame_size) | |
1212 | return 1; | |
1213 | /* free format: compute bitrate and real frame size from the | |
1214 | frame size we extracted by reading the bitstream */ | |
1215 | s->frame_size = s->free_format_frame_size; | |
1216 | switch(s->layer) { | |
1217 | case 1: | |
1218 | s->frame_size += padding * 4; | |
1219 | s->bit_rate = (s->frame_size * sample_rate) / 48000; | |
1220 | break; | |
1221 | case 2: | |
1222 | s->frame_size += padding; | |
1223 | s->bit_rate = (s->frame_size * sample_rate) / 144000; | |
1224 | break; | |
1225 | default: | |
1226 | case 3: | |
1227 | s->frame_size += padding; | |
1228 | s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000; | |
1229 | break; | |
1230 | } | |
de6d9b64 | 1231 | } |
115329f1 | 1232 | |
fad9f495 | 1233 | #if defined(DEBUG) |
267f7edc | 1234 | dprintf("layer%d, %d Hz, %d kbits/s, ", |
239c2f4c FB |
1235 | s->layer, s->sample_rate, s->bit_rate); |
1236 | if (s->nb_channels == 2) { | |
1237 | if (s->layer == 3) { | |
1238 | if (s->mode_ext & MODE_EXT_MS_STEREO) | |
267f7edc | 1239 | dprintf("ms-"); |
239c2f4c | 1240 | if (s->mode_ext & MODE_EXT_I_STEREO) |
267f7edc | 1241 | dprintf("i-"); |
239c2f4c | 1242 | } |
267f7edc | 1243 | dprintf("stereo"); |
239c2f4c | 1244 | } else { |
267f7edc | 1245 | dprintf("mono"); |
239c2f4c | 1246 | } |
267f7edc | 1247 | dprintf("\n"); |
de6d9b64 | 1248 | #endif |
239c2f4c | 1249 | return 0; |
de6d9b64 FB |
1250 | } |
1251 | ||
8c5b5683 | 1252 | /* useful helper to get mpeg audio stream infos. Return -1 if error in |
962d6ae6 FB |
1253 | header, otherwise the coded frame size in bytes */ |
1254 | int mpa_decode_header(AVCodecContext *avctx, uint32_t head) | |
8c5b5683 FB |
1255 | { |
1256 | MPADecodeContext s1, *s = &s1; | |
2caa92d9 | 1257 | memset( s, 0, sizeof(MPADecodeContext) ); |
8c5b5683 | 1258 | |
a7a85899 | 1259 | if (ff_mpa_check_header(head) != 0) |
8c5b5683 FB |
1260 | return -1; |
1261 | ||
1262 | if (decode_header(s, head) != 0) { | |
1263 | return -1; | |
1264 | } | |
1265 | ||
1266 | switch(s->layer) { | |
1267 | case 1: | |
962d6ae6 | 1268 | avctx->frame_size = 384; |
8c5b5683 FB |
1269 | break; |
1270 | case 2: | |
962d6ae6 | 1271 | avctx->frame_size = 1152; |
8c5b5683 FB |
1272 | break; |
1273 | default: | |
1274 | case 3: | |
1275 | if (s->lsf) | |
962d6ae6 | 1276 | avctx->frame_size = 576; |
8c5b5683 | 1277 | else |
962d6ae6 | 1278 | avctx->frame_size = 1152; |
8c5b5683 FB |
1279 | break; |
1280 | } | |
1281 | ||
962d6ae6 FB |
1282 | avctx->sample_rate = s->sample_rate; |
1283 | avctx->channels = s->nb_channels; | |
1284 | avctx->bit_rate = s->bit_rate; | |
1285 | avctx->sub_id = s->layer; | |
1286 | return s->frame_size; | |
8c5b5683 FB |
1287 | } |
1288 | ||
239c2f4c FB |
1289 | /* return the number of decoded frames */ |
1290 | static int mp_decode_layer1(MPADecodeContext *s) | |
de6d9b64 | 1291 | { |
239c2f4c | 1292 | int bound, i, v, n, ch, j, mant; |
0c1a9eda ZK |
1293 | uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT]; |
1294 | uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT]; | |
239c2f4c | 1295 | |
115329f1 | 1296 | if (s->mode == MPA_JSTEREO) |
239c2f4c FB |
1297 | bound = (s->mode_ext + 1) * 4; |
1298 | else | |
1299 | bound = SBLIMIT; | |
1300 | ||
1301 | /* allocation bits */ | |
1302 | for(i=0;i<bound;i++) { | |
1303 | for(ch=0;ch<s->nb_channels;ch++) { | |
1304 | allocation[ch][i] = get_bits(&s->gb, 4); | |
1305 | } | |
1306 | } | |
1307 | for(i=bound;i<SBLIMIT;i++) { | |
1308 | allocation[0][i] = get_bits(&s->gb, 4); | |
1309 | } | |
1310 | ||
1311 | /* scale factors */ | |
1312 | for(i=0;i<bound;i++) { | |
1313 | for(ch=0;ch<s->nb_channels;ch++) { | |
1314 | if (allocation[ch][i]) | |
1315 | scale_factors[ch][i] = get_bits(&s->gb, 6); | |
1316 | } | |
1317 | } | |
1318 | for(i=bound;i<SBLIMIT;i++) { | |
1319 | if (allocation[0][i]) { | |
1320 | scale_factors[0][i] = get_bits(&s->gb, 6); | |
1321 | scale_factors[1][i] = get_bits(&s->gb, 6); | |
1322 | } | |
1323 | } | |
115329f1 | 1324 | |
239c2f4c FB |
1325 | /* compute samples */ |
1326 | for(j=0;j<12;j++) { | |
1327 | for(i=0;i<bound;i++) { | |
1328 | for(ch=0;ch<s->nb_channels;ch++) { | |
1329 | n = allocation[ch][i]; | |
1330 | if (n) { | |
1331 | mant = get_bits(&s->gb, n + 1); | |
1332 | v = l1_unscale(n, mant, scale_factors[ch][i]); | |
1333 | } else { | |
1334 | v = 0; | |
1335 | } | |
1336 | s->sb_samples[ch][j][i] = v; | |
1337 | } | |
1338 | } | |
1339 | for(i=bound;i<SBLIMIT;i++) { | |
1340 | n = allocation[0][i]; | |
1341 | if (n) { | |
1342 | mant = get_bits(&s->gb, n + 1); | |
1343 | v = l1_unscale(n, mant, scale_factors[0][i]); | |
1344 | s->sb_samples[0][j][i] = v; | |
1345 | v = l1_unscale(n, mant, scale_factors[1][i]); | |
1346 | s->sb_samples[1][j][i] = v; | |
1347 | } else { | |
1348 | s->sb_samples[0][j][i] = 0; | |
1349 | s->sb_samples[1][j][i] = 0; | |
1350 | } | |
1351 | } | |
1352 | } | |
1353 | return 12; | |
1354 | } | |
1355 | ||
1356 | /* bitrate is in kb/s */ | |
1357 | int l2_select_table(int bitrate, int nb_channels, int freq, int lsf) | |
1358 | { | |
1359 | int ch_bitrate, table; | |
115329f1 | 1360 | |
239c2f4c FB |
1361 | ch_bitrate = bitrate / nb_channels; |
1362 | if (!lsf) { | |
1363 | if ((freq == 48000 && ch_bitrate >= 56) || | |
115329f1 | 1364 | (ch_bitrate >= 56 && ch_bitrate <= 80)) |
239c2f4c | 1365 | table = 0; |
115329f1 | 1366 | else if (freq != 48000 && ch_bitrate >= 96) |
239c2f4c | 1367 | table = 1; |
115329f1 | 1368 | else if (freq != 32000 && ch_bitrate <= 48) |
239c2f4c | 1369 | table = 2; |
115329f1 | 1370 | else |
239c2f4c FB |
1371 | table = 3; |
1372 | } else { | |
1373 | table = 4; | |
1374 | } | |
1375 | return table; | |
1376 | } | |
de6d9b64 | 1377 | |
239c2f4c FB |
1378 | static int mp_decode_layer2(MPADecodeContext *s) |
1379 | { | |
1380 | int sblimit; /* number of used subbands */ | |
1381 | const unsigned char *alloc_table; | |
1382 | int table, bit_alloc_bits, i, j, ch, bound, v; | |
1383 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; | |
1384 | unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; | |
1385 | unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf; | |
1386 | int scale, qindex, bits, steps, k, l, m, b; | |
de6d9b64 | 1387 | |
239c2f4c | 1388 | /* select decoding table */ |
115329f1 | 1389 | table = l2_select_table(s->bit_rate / 1000, s->nb_channels, |
239c2f4c FB |
1390 | s->sample_rate, s->lsf); |
1391 | sblimit = sblimit_table[table]; | |
1392 | alloc_table = alloc_tables[table]; | |
1393 | ||
115329f1 | 1394 | if (s->mode == MPA_JSTEREO) |
239c2f4c FB |
1395 | bound = (s->mode_ext + 1) * 4; |
1396 | else | |
1397 | bound = sblimit; | |
1398 | ||
1399 | dprintf("bound=%d sblimit=%d\n", bound, sblimit); | |
2caa92d9 MN |
1400 | |
1401 | /* sanity check */ | |
1402 | if( bound > sblimit ) bound = sblimit; | |
1403 | ||
239c2f4c FB |
1404 | /* parse bit allocation */ |
1405 | j = 0; | |
1406 | for(i=0;i<bound;i++) { | |
1407 | bit_alloc_bits = alloc_table[j]; | |
1408 | for(ch=0;ch<s->nb_channels;ch++) { | |
1409 | bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits); | |
1410 | } | |
1411 | j += 1 << bit_alloc_bits; | |
1412 | } | |
1413 | for(i=bound;i<sblimit;i++) { | |
1414 | bit_alloc_bits = alloc_table[j]; | |
1415 | v = get_bits(&s->gb, bit_alloc_bits); | |
1416 | bit_alloc[0][i] = v; | |
1417 | bit_alloc[1][i] = v; | |
1418 | j += 1 << bit_alloc_bits; | |
de6d9b64 | 1419 | } |
239c2f4c FB |
1420 | |
1421 | #ifdef DEBUG | |
1422 | { | |
1423 | for(ch=0;ch<s->nb_channels;ch++) { | |
1424 | for(i=0;i<sblimit;i++) | |
267f7edc SH |
1425 | dprintf(" %d", bit_alloc[ch][i]); |
1426 | dprintf("\n"); | |
239c2f4c FB |
1427 | } |
1428 | } | |
1429 | #endif | |
1430 | ||
1431 | /* scale codes */ | |
1432 | for(i=0;i<sblimit;i++) { | |
1433 | for(ch=0;ch<s->nb_channels;ch++) { | |
115329f1 | 1434 | if (bit_alloc[ch][i]) |
239c2f4c FB |
1435 | scale_code[ch][i] = get_bits(&s->gb, 2); |
1436 | } | |
1437 | } | |
115329f1 | 1438 | |
239c2f4c FB |
1439 | /* scale factors */ |
1440 | for(i=0;i<sblimit;i++) { | |
1441 | for(ch=0;ch<s->nb_channels;ch++) { | |
1442 | if (bit_alloc[ch][i]) { | |
1443 | sf = scale_factors[ch][i]; | |
1444 | switch(scale_code[ch][i]) { | |
1445 | default: | |
1446 | case 0: | |
1447 | sf[0] = get_bits(&s->gb, 6); | |
1448 | sf[1] = get_bits(&s->gb, 6); | |
1449 | sf[2] = get_bits(&s->gb, 6); | |
1450 | break; | |
1451 | case 2: | |
1452 | sf[0] = get_bits(&s->gb, 6); | |
1453 | sf[1] = sf[0]; | |
1454 | sf[2] = sf[0]; | |
1455 | break; | |
1456 | case 1: | |
1457 | sf[0] = get_bits(&s->gb, 6); | |
1458 | sf[2] = get_bits(&s->gb, 6); | |
1459 | sf[1] = sf[0]; | |
1460 | break; | |
1461 | case 3: | |
1462 | sf[0] = get_bits(&s->gb, 6); | |
1463 | sf[2] = get_bits(&s->gb, 6); | |
1464 | sf[1] = sf[2]; | |
1465 | break; | |
1466 | } | |
1467 | } | |
1468 | } | |
1469 | } | |
1470 | ||
1471 | #ifdef DEBUG | |
1472 | for(ch=0;ch<s->nb_channels;ch++) { | |
1473 | for(i=0;i<sblimit;i++) { | |
1474 | if (bit_alloc[ch][i]) { | |
1475 | sf = scale_factors[ch][i]; | |
267f7edc | 1476 | dprintf(" %d %d %d", sf[0], sf[1], sf[2]); |
239c2f4c | 1477 | } else { |
267f7edc | 1478 | dprintf(" -"); |
239c2f4c FB |
1479 | } |
1480 | } | |
267f7edc | 1481 | dprintf("\n"); |
239c2f4c FB |
1482 | } |
1483 | #endif | |
1484 | ||
1485 | /* samples */ | |
1486 | for(k=0;k<3;k++) { | |
1487 | for(l=0;l<12;l+=3) { | |
1488 | j = 0; | |
1489 | for(i=0;i<bound;i++) { | |
1490 | bit_alloc_bits = alloc_table[j]; | |
1491 | for(ch=0;ch<s->nb_channels;ch++) { | |
1492 | b = bit_alloc[ch][i]; | |
1493 | if (b) { | |
1494 | scale = scale_factors[ch][i][k]; | |
1495 | qindex = alloc_table[j+b]; | |
1496 | bits = quant_bits[qindex]; | |
1497 | if (bits < 0) { | |
1498 | /* 3 values at the same time */ | |
1499 | v = get_bits(&s->gb, -bits); | |
1500 | steps = quant_steps[qindex]; | |
115329f1 | 1501 | s->sb_samples[ch][k * 12 + l + 0][i] = |
239c2f4c FB |
1502 | l2_unscale_group(steps, v % steps, scale); |
1503 | v = v / steps; | |
115329f1 | 1504 | s->sb_samples[ch][k * 12 + l + 1][i] = |
239c2f4c FB |
1505 | l2_unscale_group(steps, v % steps, scale); |
1506 | v = v / steps; | |
115329f1 | 1507 | s->sb_samples[ch][k * 12 + l + 2][i] = |
239c2f4c FB |
1508 | l2_unscale_group(steps, v, scale); |
1509 | } else { | |
1510 | for(m=0;m<3;m++) { | |
1511 | v = get_bits(&s->gb, bits); | |
1512 | v = l1_unscale(bits - 1, v, scale); | |
1513 | s->sb_samples[ch][k * 12 + l + m][i] = v; | |
1514 | } | |
1515 | } | |
1516 | } else { | |
1517 | s->sb_samples[ch][k * 12 + l + 0][i] = 0; | |
1518 | s->sb_samples[ch][k * 12 + l + 1][i] = 0; | |
1519 | s->sb_samples[ch][k * 12 + l + 2][i] = 0; | |
1520 | } | |
1521 | } | |
1522 | /* next subband in alloc table */ | |
115329f1 | 1523 | j += 1 << bit_alloc_bits; |
239c2f4c FB |
1524 | } |
1525 | /* XXX: find a way to avoid this duplication of code */ | |
1526 | for(i=bound;i<sblimit;i++) { | |
1527 | bit_alloc_bits = alloc_table[j]; | |
1528 | b = bit_alloc[0][i]; | |
1529 | if (b) { | |
1530 | int mant, scale0, scale1; | |
1531 | scale0 = scale_factors[0][i][k]; | |
1532 | scale1 = scale_factors[1][i][k]; | |
1533 | qindex = alloc_table[j+b]; | |
1534 | bits = quant_bits[qindex]; | |
1535 | if (bits < 0) { | |
1536 | /* 3 values at the same time */ | |
1537 | v = get_bits(&s->gb, -bits); | |
1538 | steps = quant_steps[qindex]; | |
1539 | mant = v % steps; | |
1540 | v = v / steps; | |
115329f1 | 1541 | s->sb_samples[0][k * 12 + l + 0][i] = |
239c2f4c | 1542 | l2_unscale_group(steps, mant, scale0); |
115329f1 | 1543 | s->sb_samples[1][k * 12 + l + 0][i] = |
239c2f4c FB |
1544 | l2_unscale_group(steps, mant, scale1); |
1545 | mant = v % steps; | |
1546 | v = v / steps; | |
115329f1 | 1547 | s->sb_samples[0][k * 12 + l + 1][i] = |
239c2f4c | 1548 | l2_unscale_group(steps, mant, scale0); |
115329f1 | 1549 | s->sb_samples[1][k * 12 + l + 1][i] = |
239c2f4c | 1550 | l2_unscale_group(steps, mant, scale1); |
115329f1 | 1551 | s->sb_samples[0][k * 12 + l + 2][i] = |
239c2f4c | 1552 | l2_unscale_group(steps, v, scale0); |
115329f1 | 1553 | s->sb_samples[1][k * 12 + l + 2][i] = |
239c2f4c FB |
1554 | l2_unscale_group(steps, v, scale1); |
1555 | } else { | |
1556 | for(m=0;m<3;m++) { | |
1557 | mant = get_bits(&s->gb, bits); | |
115329f1 | 1558 | s->sb_samples[0][k * 12 + l + m][i] = |
239c2f4c | 1559 | l1_unscale(bits - 1, mant, scale0); |
115329f1 | 1560 | s->sb_samples[1][k * 12 + l + m][i] = |
239c2f4c FB |
1561 | l1_unscale(bits - 1, mant, scale1); |
1562 | } | |
1563 | } | |
1564 | } else { | |
1565 | s->sb_samples[0][k * 12 + l + 0][i] = 0; | |
1566 | s->sb_samples[0][k * 12 + l + 1][i] = 0; | |
1567 | s->sb_samples[0][k * 12 + l + 2][i] = 0; | |
1568 | s->sb_samples[1][k * 12 + l + 0][i] = 0; | |
1569 | s->sb_samples[1][k * 12 + l + 1][i] = 0; | |
1570 | s->sb_samples[1][k * 12 + l + 2][i] = 0; | |
1571 | } | |
1572 | /* next subband in alloc table */ | |
115329f1 | 1573 | j += 1 << bit_alloc_bits; |
239c2f4c FB |
1574 | } |
1575 | /* fill remaining samples to zero */ | |
1576 | for(i=sblimit;i<SBLIMIT;i++) { | |
1577 | for(ch=0;ch<s->nb_channels;ch++) { | |
1578 | s->sb_samples[ch][k * 12 + l + 0][i] = 0; | |
1579 | s->sb_samples[ch][k * 12 + l + 1][i] = 0; | |
1580 | s->sb_samples[ch][k * 12 + l + 2][i] = 0; | |
1581 | } | |
1582 | } | |
1583 | } | |
1584 | } | |
1585 | return 3 * 12; | |
de6d9b64 FB |
1586 | } |
1587 | ||
1588 | /* | |
239c2f4c | 1589 | * Seek back in the stream for backstep bytes (at most 511 bytes) |
de6d9b64 | 1590 | */ |
5c91a675 | 1591 | static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep) |
de6d9b64 | 1592 | { |
0c1a9eda | 1593 | uint8_t *ptr; |
de6d9b64 FB |
1594 | |
1595 | /* compute current position in stream */ | |
228ef9dd | 1596 | ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3)); |
8db1a1dd | 1597 | |
de6d9b64 FB |
1598 | /* copy old data before current one */ |
1599 | ptr -= backstep; | |
115329f1 | 1600 | memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + |
239c2f4c | 1601 | BACKSTEP_SIZE + s->old_frame_size - backstep, backstep); |
de6d9b64 | 1602 | /* init get bits again */ |
68f593b4 | 1603 | init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8); |
de6d9b64 | 1604 | |
239c2f4c FB |
1605 | /* prepare next buffer */ |
1606 | s->inbuf_index ^= 1; | |
1607 | s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE]; | |
1608 | s->old_frame_size = s->frame_size; | |
1609 | } | |
1610 | ||
1611 | static inline void lsf_sf_expand(int *slen, | |
1612 | int sf, int n1, int n2, int n3) | |
1613 | { | |
1614 | if (n3) { | |
1615 | slen[3] = sf % n3; | |
1616 | sf /= n3; | |
1617 | } else { | |
1618 | slen[3] = 0; | |
1619 | } | |
1620 | if (n2) { | |
1621 | slen[2] = sf % n2; | |
1622 | sf /= n2; | |
1623 | } else { | |
1624 | slen[2] = 0; | |
1625 | } | |
1626 | slen[1] = sf % n1; | |
1627 | sf /= n1; | |
1628 | slen[0] = sf; | |
1629 | } | |
1630 | ||
115329f1 | 1631 | static void exponents_from_scale_factors(MPADecodeContext *s, |
239c2f4c | 1632 | GranuleDef *g, |
0c1a9eda | 1633 | int16_t *exponents) |
239c2f4c | 1634 | { |
0c1a9eda | 1635 | const uint8_t *bstab, *pretab; |
239c2f4c | 1636 | int len, i, j, k, l, v0, shift, gain, gains[3]; |
0c1a9eda | 1637 | int16_t *exp_ptr; |
239c2f4c FB |
1638 | |
1639 | exp_ptr = exponents; | |
1640 | gain = g->global_gain - 210; | |
1641 | shift = g->scalefac_scale + 1; | |
1642 | ||
1643 | bstab = band_size_long[s->sample_rate_index]; | |
1644 | pretab = mpa_pretab[g->preflag]; | |
1645 | for(i=0;i<g->long_end;i++) { | |
1646 | v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift); | |
1647 | len = bstab[i]; | |
1648 | for(j=len;j>0;j--) | |
1649 | *exp_ptr++ = v0; | |
1650 | } | |
1651 | ||
1652 | if (g->short_start < 13) { | |
1653 | bstab = band_size_short[s->sample_rate_index]; | |
1654 | gains[0] = gain - (g->subblock_gain[0] << 3); | |
1655 | gains[1] = gain - (g->subblock_gain[1] << 3); | |
1656 | gains[2] = gain - (g->subblock_gain[2] << 3); | |
1657 | k = g->long_end; | |
1658 | for(i=g->short_start;i<13;i++) { | |
1659 | len = bstab[i]; | |
1660 | for(l=0;l<3;l++) { | |
1661 | v0 = gains[l] - (g->scale_factors[k++] << shift); | |
1662 | for(j=len;j>0;j--) | |
1663 | *exp_ptr++ = v0; | |
1664 | } | |
1665 | } | |
1666 | } | |
1667 | } | |
1668 | ||
1669 | /* handle n = 0 too */ | |
1670 | static inline int get_bitsz(GetBitContext *s, int n) | |
1671 | { | |
1672 | if (n == 0) | |
1673 | return 0; | |
1674 | else | |
1675 | return get_bits(s, n); | |
1676 | } | |
1677 | ||
1678 | static int huffman_decode(MPADecodeContext *s, GranuleDef *g, | |
0c1a9eda | 1679 | int16_t *exponents, int end_pos) |
239c2f4c FB |
1680 | { |
1681 | int s_index; | |
1682 | int linbits, code, x, y, l, v, i, j, k, pos; | |
a08a502e | 1683 | int last_pos; |
239c2f4c | 1684 | VLC *vlc; |
239c2f4c FB |
1685 | |
1686 | /* low frequencies (called big values) */ | |
1687 | s_index = 0; | |
1688 | for(i=0;i<3;i++) { | |
1689 | j = g->region_size[i]; | |
1690 | if (j == 0) | |
1691 | continue; | |
1692 | /* select vlc table */ | |
1693 | k = g->table_select[i]; | |
1694 | l = mpa_huff_data[k][0]; | |
1695 | linbits = mpa_huff_data[k][1]; | |
1696 | vlc = &huff_vlc[l]; | |
239c2f4c | 1697 | |
daf4cd9a MN |
1698 | if(!l){ |
1699 | memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*j); | |
1700 | s_index += 2*j; | |
1701 | continue; | |
1702 | } | |
1703 | ||
239c2f4c FB |
1704 | /* read huffcode and compute each couple */ |
1705 | for(;j>0;j--) { | |
1706 | if (get_bits_count(&s->gb) >= end_pos) | |
1707 | break; | |
1f1d675b | 1708 | y = get_vlc2(&s->gb, vlc->table, 7, 3); |
3328ec63 MN |
1709 | |
1710 | if(!y){ | |
1711 | g->sb_hybrid[s_index ] = | |
1712 | g->sb_hybrid[s_index+1] = 0; | |
1713 | s_index += 2; | |
1714 | continue; | |
1715 | } | |
1716 | ||
daf4cd9a MN |
1717 | x = y >> 4; |
1718 | y = y & 0x0f; | |
1719 | ||
115329f1 | 1720 | dprintf("region=%d n=%d x=%d y=%d exp=%d\n", |
239c2f4c FB |
1721 | i, g->region_size[i] - j, x, y, exponents[s_index]); |
1722 | if (x) { | |
55017f35 | 1723 | #if 0 |
239c2f4c FB |
1724 | if (x == 15) |
1725 | x += get_bitsz(&s->gb, linbits); | |
1726 | v = l3_unscale(x, exponents[s_index]); | |
55017f35 MN |
1727 | #else |
1728 | if (x < 15){ | |
1729 | v = expval_table[ exponents[s_index] + 400 ][ x ]; | |
1730 | }else{ | |
1731 | x += get_bitsz(&s->gb, linbits); | |
1732 | v = l3_unscale(x, exponents[s_index]); | |
1733 | } | |
1734 | #endif | |
239c2f4c FB |
1735 | if (get_bits1(&s->gb)) |
1736 | v = -v; | |
1737 | } else { | |
1738 | v = 0; | |
1739 | } | |
1740 | g->sb_hybrid[s_index++] = v; | |
1741 | if (y) { | |
55017f35 | 1742 | #if 0 |
239c2f4c FB |
1743 | if (y == 15) |
1744 | y += get_bitsz(&s->gb, linbits); | |
1745 | v = l3_unscale(y, exponents[s_index]); | |
55017f35 MN |
1746 | #else |
1747 | if (y < 15){ | |
1748 | v = expval_table[ exponents[s_index] + 400 ][ y ]; | |
1749 | }else{ | |
1750 | y += get_bitsz(&s->gb, linbits); | |
1751 | v = l3_unscale(y, exponents[s_index]); | |
1752 | } | |
1753 | #endif | |
239c2f4c FB |
1754 | if (get_bits1(&s->gb)) |
1755 | v = -v; | |
1756 | } else { | |
1757 | v = 0; | |
1758 | } | |
1759 | g->sb_hybrid[s_index++] = v; | |
1760 | } | |
1761 | } | |
115329f1 | 1762 | |
239c2f4c FB |
1763 | /* high frequencies */ |
1764 | vlc = &huff_quad_vlc[g->count1table_select]; | |
a08a502e | 1765 | last_pos=0; |
239c2f4c FB |
1766 | while (s_index <= 572) { |
1767 | pos = get_bits_count(&s->gb); | |
1768 | if (pos >= end_pos) { | |
a08a502e | 1769 | if (pos > end_pos && last_pos){ |
239c2f4c FB |
1770 | /* some encoders generate an incorrect size for this |
1771 | part. We must go back into the data */ | |
1772 | s_index -= 4; | |
7ca1ed0d MN |
1773 | init_get_bits(&s->gb, s->gb.buffer + 4*(last_pos>>5), s->gb.size_in_bits - (last_pos&(~31))); |
1774 | skip_bits(&s->gb, last_pos&31); | |
239c2f4c FB |
1775 | } |
1776 | break; | |
1777 | } | |
a08a502e | 1778 | last_pos= pos; |
8db1a1dd | 1779 | |
3c693e77 | 1780 | code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1); |
239c2f4c | 1781 | dprintf("t=%d code=%d\n", g->count1table_select, code); |
3c693e77 MN |
1782 | g->sb_hybrid[s_index+0]= |
1783 | g->sb_hybrid[s_index+1]= | |
1784 | g->sb_hybrid[s_index+2]= | |
1785 | g->sb_hybrid[s_index+3]= 0; | |
1786 | while(code){ | |
1787 | const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0}; | |
1788 | int pos= s_index+idxtab[code]; | |
1789 | code ^= 8>>idxtab[code]; | |
f8347c41 | 1790 | v = exp_table[ exponents[pos] + 400]; |
3c693e77 MN |
1791 | if(get_bits1(&s->gb)) |
1792 | v = -v; | |
1793 | g->sb_hybrid[pos] = v; | |
239c2f4c | 1794 | } |
3c693e77 | 1795 | s_index+=4; |
239c2f4c | 1796 | } |
daf4cd9a | 1797 | memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index)); |
de6d9b64 FB |
1798 | return 0; |
1799 | } | |
1800 | ||
239c2f4c FB |
1801 | /* Reorder short blocks from bitstream order to interleaved order. It |
1802 | would be faster to do it in parsing, but the code would be far more | |
1803 | complicated */ | |
1804 | static void reorder_block(MPADecodeContext *s, GranuleDef *g) | |
1805 | { | |
6430ce0f | 1806 | int i, j, len; |
0c1a9eda ZK |
1807 | int32_t *ptr, *dst, *ptr1; |
1808 | int32_t tmp[576]; | |
239c2f4c FB |
1809 | |
1810 | if (g->block_type != 2) | |
1811 | return; | |
1812 | ||
1813 | if (g->switch_point) { | |
1814 | if (s->sample_rate_index != 8) { | |
1815 | ptr = g->sb_hybrid + 36; | |
1816 | } else { | |
1817 | ptr = g->sb_hybrid + 48; | |
1818 | } | |
1819 | } else { | |
1820 | ptr = g->sb_hybrid; | |
1821 | } | |
115329f1 | 1822 | |
239c2f4c FB |
1823 | for(i=g->short_start;i<13;i++) { |
1824 | len = band_size_short[s->sample_rate_index][i]; | |
1825 | ptr1 = ptr; | |
6430ce0f MN |
1826 | dst = tmp; |
1827 | for(j=len;j>0;j--) { | |
1828 | *dst++ = ptr[0*len]; | |
1829 | *dst++ = ptr[1*len]; | |
1830 | *dst++ = ptr[2*len]; | |
1831 | ptr++; | |
239c2f4c | 1832 | } |
6430ce0f MN |
1833 | ptr+=2*len; |
1834 | memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1)); | |
239c2f4c FB |
1835 | } |
1836 | } | |
1837 | ||
1838 | #define ISQRT2 FIXR(0.70710678118654752440) | |
1839 | ||
1840 | static void compute_stereo(MPADecodeContext *s, | |
1841 | GranuleDef *g0, GranuleDef *g1) | |
1842 | { | |
1843 | int i, j, k, l; | |
0c1a9eda | 1844 | int32_t v1, v2; |
239c2f4c | 1845 | int sf_max, tmp0, tmp1, sf, len, non_zero_found; |
0c1a9eda ZK |
1846 | int32_t (*is_tab)[16]; |
1847 | int32_t *tab0, *tab1; | |
239c2f4c FB |
1848 | int non_zero_found_short[3]; |
1849 | ||
1850 | /* intensity stereo */ | |
1851 | if (s->mode_ext & MODE_EXT_I_STEREO) { | |
1852 | if (!s->lsf) { | |
1853 | is_tab = is_table; | |
1854 | sf_max = 7; | |
1855 | } else { | |
1856 | is_tab = is_table_lsf[g1->scalefac_compress & 1]; | |
1857 | sf_max = 16; | |
1858 | } | |
115329f1 | 1859 | |
239c2f4c FB |
1860 | tab0 = g0->sb_hybrid + 576; |
1861 | tab1 = g1->sb_hybrid + 576; | |
1862 | ||
1863 | non_zero_found_short[0] = 0; | |
1864 | non_zero_found_short[1] = 0; | |
1865 | non_zero_found_short[2] = 0; | |
1866 | k = (13 - g1->short_start) * 3 + g1->long_end - 3; | |
1867 | for(i = 12;i >= g1->short_start;i--) { | |
1868 | /* for last band, use previous scale factor */ | |
1869 | if (i != 11) | |
1870 | k -= 3; | |
1871 | len = band_size_short[s->sample_rate_index][i]; | |
1872 | for(l=2;l>=0;l--) { | |
1873 | tab0 -= len; | |
1874 | tab1 -= len; | |
1875 | if (!non_zero_found_short[l]) { | |
1876 | /* test if non zero band. if so, stop doing i-stereo */ | |
1877 | for(j=0;j<len;j++) { | |
1878 | if (tab1[j] != 0) { | |
1879 | non_zero_found_short[l] = 1; | |
1880 | goto found1; | |
1881 | } | |
1882 | } | |
1883 | sf = g1->scale_factors[k + l]; | |
1884 | if (sf >= sf_max) | |
1885 | goto found1; | |
1886 | ||
1887 | v1 = is_tab[0][sf]; | |
1888 | v2 = is_tab[1][sf]; | |
1889 | for(j=0;j<len;j++) { | |
1890 | tmp0 = tab0[j]; | |
1891 | tab0[j] = MULL(tmp0, v1); | |
1892 | tab1[j] = MULL(tmp0, v2); | |
1893 | } | |
1894 | } else { | |
1895 | found1: | |
1896 | if (s->mode_ext & MODE_EXT_MS_STEREO) { | |
1897 | /* lower part of the spectrum : do ms stereo | |
1898 | if enabled */ | |
1899 | for(j=0;j<len;j++) { | |
1900 | tmp0 = tab0[j]; | |
1901 | tmp1 = tab1[j]; | |
1902 | tab0[j] = MULL(tmp0 + tmp1, ISQRT2); | |
1903 | tab1[j] = MULL(tmp0 - tmp1, ISQRT2); | |
1904 | } | |
1905 | } | |
1906 | } | |
1907 | } | |
1908 | } | |
1909 | ||
115329f1 DB |
1910 | non_zero_found = non_zero_found_short[0] | |
1911 | non_zero_found_short[1] | | |
239c2f4c FB |
1912 | non_zero_found_short[2]; |
1913 | ||
1914 | for(i = g1->long_end - 1;i >= 0;i--) { | |
1915 | len = band_size_long[s->sample_rate_index][i]; | |
1916 | tab0 -= len; | |
1917 | tab1 -= len; | |
1918 | /* test if non zero band. if so, stop doing i-stereo */ | |
1919 | if (!non_zero_found) { | |
1920 | for(j=0;j<len;j++) { | |
1921 | if (tab1[j] != 0) { | |
1922 | non_zero_found = 1; | |
1923 | goto found2; | |
1924 | } | |
1925 | } | |
1926 | /* for last band, use previous scale factor */ | |
1927 | k = (i == 21) ? 20 : i; | |
1928 | sf = g1->scale_factors[k]; | |
1929 | if (sf >= sf_max) | |
1930 | goto found2; | |
1931 | v1 = is_tab[0][sf]; | |
1932 | v2 = is_tab[1][sf]; | |
1933 | for(j=0;j<len;j++) { | |
1934 | tmp0 = tab0[j]; | |
1935 | tab0[j] = MULL(tmp0, v1); | |
1936 | tab1[j] = MULL(tmp0, v2); | |
1937 | } | |
1938 | } else { | |
1939 | found2: | |
1940 | if (s->mode_ext & MODE_EXT_MS_STEREO) { | |
1941 | /* lower part of the spectrum : do ms stereo | |
1942 | if enabled */ | |
1943 | for(j=0;j<len;j++) { | |
1944 | tmp0 = tab0[j]; | |
1945 | tmp1 = tab1[j]; | |
1946 | tab0[j] = MULL(tmp0 + tmp1, ISQRT2); | |
1947 | tab1[j] = MULL(tmp0 - tmp1, ISQRT2); | |
1948 | } | |
1949 | } | |
1950 | } | |
1951 | } | |
1952 | } else if (s->mode_ext & MODE_EXT_MS_STEREO) { | |
1953 | /* ms stereo ONLY */ | |
1954 | /* NOTE: the 1/sqrt(2) normalization factor is included in the | |
1955 | global gain */ | |
1956 | tab0 = g0->sb_hybrid; | |
1957 | tab1 = g1->sb_hybrid; | |
1958 | for(i=0;i<576;i++) { | |
1959 | tmp0 = tab0[i]; | |
1960 | tmp1 = tab1[i]; | |
1961 | tab0[i] = tmp0 + tmp1; | |
1962 | tab1[i] = tmp0 - tmp1; | |
1963 | } | |
1964 | } | |
1965 | } | |
1966 | ||
a1e257b2 | 1967 | static void compute_antialias_integer(MPADecodeContext *s, |
239c2f4c FB |
1968 | GranuleDef *g) |
1969 | { | |
ce4a29c0 MN |
1970 | int32_t *ptr, *csa; |
1971 | int n, i; | |
239c2f4c FB |
1972 | |
1973 | /* we antialias only "long" bands */ | |
1974 | if (g->block_type == 2) { | |
1975 | if (!g->switch_point) | |
1976 | return; | |
1977 | /* XXX: check this for 8000Hz case */ | |
1978 | n = 1; | |
1979 | } else { | |
1980 | n = SBLIMIT - 1; | |
1981 | } | |
115329f1 | 1982 | |
239c2f4c FB |
1983 | ptr = g->sb_hybrid + 18; |
1984 | for(i = n;i > 0;i--) { | |
ce4a29c0 MN |
1985 | int tmp0, tmp1, tmp2; |
1986 | csa = &csa_table[0][0]; | |
1987 | #define INT_AA(j) \ | |
44f1698a MN |
1988 | tmp0 = ptr[-1-j];\ |
1989 | tmp1 = ptr[ j];\ | |
ce4a29c0 | 1990 | tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\ |
44f1698a MN |
1991 | ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\ |
1992 | ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j])); | |
ce4a29c0 MN |
1993 | |
1994 | INT_AA(0) | |
1995 | INT_AA(1) | |
1996 | INT_AA(2) | |
1997 | INT_AA(3) | |
1998 | INT_AA(4) | |
1999 | INT_AA(5) | |
2000 | INT_AA(6) | |
2001 | INT_AA(7) | |
115329f1 DB |
2002 | |
2003 | ptr += 18; | |
a1e257b2 MN |
2004 | } |
2005 | } | |
2006 | ||
2007 | static void compute_antialias_float(MPADecodeContext *s, | |
2008 | GranuleDef *g) | |
2009 | { | |
ce4a29c0 MN |
2010 | int32_t *ptr; |
2011 | int n, i; | |
a1e257b2 MN |
2012 | |
2013 | /* we antialias only "long" bands */ | |
2014 | if (g->block_type == 2) { | |
2015 | if (!g->switch_point) | |
2016 | return; | |
2017 | /* XXX: check this for 8000Hz case */ | |
2018 | n = 1; | |
2019 | } else { | |
2020 | n = SBLIMIT - 1; | |
2021 | } | |
115329f1 | 2022 | |
a1e257b2 MN |
2023 | ptr = g->sb_hybrid + 18; |
2024 | for(i = n;i > 0;i--) { | |
ce4a29c0 | 2025 | float tmp0, tmp1; |
115329f1 | 2026 | float *csa = &csa_table_float[0][0]; |
ce4a29c0 MN |
2027 | #define FLOAT_AA(j)\ |
2028 | tmp0= ptr[-1-j];\ | |
2029 | tmp1= ptr[ j];\ | |
2030 | ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\ | |
2031 | ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]); | |
115329f1 | 2032 | |
ce4a29c0 MN |
2033 | FLOAT_AA(0) |
2034 | FLOAT_AA(1) | |
2035 | FLOAT_AA(2) | |
2036 | FLOAT_AA(3) | |
2037 | FLOAT_AA(4) | |
2038 | FLOAT_AA(5) | |
2039 | FLOAT_AA(6) | |
2040 | FLOAT_AA(7) | |
2041 | ||
115329f1 | 2042 | ptr += 18; |
239c2f4c FB |
2043 | } |
2044 | } | |
2045 | ||
2046 | static void compute_imdct(MPADecodeContext *s, | |
115329f1 | 2047 | GranuleDef *g, |
0c1a9eda ZK |
2048 | int32_t *sb_samples, |
2049 | int32_t *mdct_buf) | |
239c2f4c | 2050 | { |
125d6246 | 2051 | int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1; |
0c1a9eda | 2052 | int32_t out2[12]; |
125d6246 | 2053 | int i, j, mdct_long_end, v, sblimit; |
239c2f4c FB |
2054 | |
2055 | /* find last non zero block */ | |
2056 | ptr = g->sb_hybrid + 576; | |
2057 | ptr1 = g->sb_hybrid + 2 * 18; | |
2058 | while (ptr >= ptr1) { | |
2059 | ptr -= 6; | |
2060 | v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5]; | |
2061 | if (v != 0) | |
2062 | break; | |
2063 | } | |
2064 | sblimit = ((ptr - g->sb_hybrid) / 18) + 1; | |
2065 | ||
2066 | if (g->block_type == 2) { | |
2067 | /* XXX: check for 8000 Hz */ | |
2068 | if (g->switch_point) | |
2069 | mdct_long_end = 2; | |
2070 | else | |
2071 | mdct_long_end = 0; | |
2072 | } else { | |
2073 | mdct_long_end = sblimit; | |
2074 | } | |
2075 | ||
2076 | buf = mdct_buf; | |
2077 | ptr = g->sb_hybrid; | |
2078 | for(j=0;j<mdct_long_end;j++) { | |
239c2f4c FB |
2079 | /* apply window & overlap with previous buffer */ |
2080 | out_ptr = sb_samples + j; | |
2081 | /* select window */ | |
2082 | if (g->switch_point && j < 2) | |
2083 | win1 = mdct_win[0]; | |
2084 | else | |
2085 | win1 = mdct_win[g->block_type]; | |
2086 | /* select frequency inversion */ | |
2087 | win = win1 + ((4 * 36) & -(j & 1)); | |
711ae726 MN |
2088 | imdct36(out_ptr, buf, ptr, win); |
2089 | out_ptr += 18*SBLIMIT; | |
239c2f4c FB |
2090 | ptr += 18; |
2091 | buf += 18; | |
2092 | } | |
2093 | for(j=mdct_long_end;j<sblimit;j++) { | |
239c2f4c FB |
2094 | /* select frequency inversion */ |
2095 | win = mdct_win[2] + ((4 * 36) & -(j & 1)); | |
239c2f4c | 2096 | out_ptr = sb_samples + j; |
115329f1 | 2097 | |
125d6246 MN |
2098 | for(i=0; i<6; i++){ |
2099 | *out_ptr = buf[i]; | |
2100 | out_ptr += SBLIMIT; | |
2101 | } | |
2102 | imdct12(out2, ptr + 0); | |
2103 | for(i=0;i<6;i++) { | |
2104 | *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1]; | |
2105 | buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]); | |
239c2f4c FB |
2106 | out_ptr += SBLIMIT; |
2107 | } | |
125d6246 MN |
2108 | imdct12(out2, ptr + 1); |
2109 | for(i=0;i<6;i++) { | |
2110 | *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2]; | |
2111 | buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]); | |
2112 | out_ptr += SBLIMIT; | |
2113 | } | |
2114 | imdct12(out2, ptr + 2); | |
2115 | for(i=0;i<6;i++) { | |
2116 | buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0]; | |
2117 | buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]); | |
2118 | buf[i + 6*2] = 0; | |
2119 | } | |
239c2f4c FB |
2120 | ptr += 18; |
2121 | buf += 18; | |
2122 | } | |
2123 | /* zero bands */ | |
2124 | for(j=sblimit;j<SBLIMIT;j++) { | |
2125 | /* overlap */ | |
2126 | out_ptr = sb_samples + j; | |
2127 | for(i=0;i<18;i++) { | |
2128 | *out_ptr = buf[i]; | |
2129 | buf[i] = 0; | |
2130 | out_ptr += SBLIMIT; | |
2131 | } | |
2132 | buf += 18; | |
2133 | } | |
2134 | } | |
2135 | ||
747a67fb | 2136 | #if defined(DEBUG) |
0c1a9eda | 2137 | void sample_dump(int fnum, int32_t *tab, int n) |
239c2f4c FB |
2138 | { |
2139 | static FILE *files[16], *f; | |
2140 | char buf[512]; | |
81552334 | 2141 | int i; |
0c1a9eda | 2142 | int32_t v; |
115329f1 | 2143 | |
239c2f4c FB |
2144 | f = files[fnum]; |
2145 | if (!f) { | |
115329f1 DB |
2146 | snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm", |
2147 | fnum, | |
81552334 FB |
2148 | #ifdef USE_HIGHPRECISION |
2149 | "hp" | |
2150 | #else | |
2151 | "lp" | |
2152 | #endif | |
2153 | ); | |
239c2f4c FB |
2154 | f = fopen(buf, "w"); |
2155 | if (!f) | |
2156 | return; | |
2157 | files[fnum] = f; | |
2158 | } | |
115329f1 | 2159 | |
239c2f4c | 2160 | if (fnum == 0) { |
239c2f4c | 2161 | static int pos = 0; |
84af4a7e | 2162 | av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos); |
239c2f4c | 2163 | for(i=0;i<n;i++) { |
84af4a7e | 2164 | av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE); |
239c2f4c | 2165 | if ((i % 18) == 17) |
84af4a7e | 2166 | av_log(NULL, AV_LOG_DEBUG, "\n"); |
239c2f4c FB |
2167 | } |
2168 | pos += n; | |
2169 | } | |
81552334 FB |
2170 | for(i=0;i<n;i++) { |
2171 | /* normalize to 23 frac bits */ | |
2172 | v = tab[i] << (23 - FRAC_BITS); | |
0c1a9eda | 2173 | fwrite(&v, 1, sizeof(int32_t), f); |
81552334 | 2174 | } |
239c2f4c FB |
2175 | } |
2176 | #endif | |
2177 | ||
2178 | ||
2179 | /* main layer3 decoding function */ | |
2180 | static int mp_decode_layer3(MPADecodeContext *s) | |
2181 | { | |
2182 | int nb_granules, main_data_begin, private_bits; | |
2183 | int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left; | |
2184 | GranuleDef granules[2][2], *g; | |
0c1a9eda | 2185 | int16_t exponents[576]; |
239c2f4c FB |
2186 | |
2187 | /* read side info */ | |
2188 | if (s->lsf) { | |
2189 | main_data_begin = get_bits(&s->gb, 8); | |
a753e55b | 2190 | private_bits = get_bits(&s->gb, s->nb_channels); |
239c2f4c FB |
2191 | nb_granules = 1; |
2192 | } else { | |
2193 | main_data_begin = get_bits(&s->gb, 9); | |
2194 | if (s->nb_channels == 2) | |
2195 | private_bits = get_bits(&s->gb, 3); | |
2196 | else | |
2197 | private_bits = get_bits(&s->gb, 5); | |
2198 | nb_granules = 2; | |
2199 | for(ch=0;ch<s->nb_channels;ch++) { | |
2200 | granules[ch][0].scfsi = 0; /* all scale factors are transmitted */ | |
2201 | granules[ch][1].scfsi = get_bits(&s->gb, 4); | |
2202 | } | |
2203 | } | |
115329f1 | 2204 | |
239c2f4c FB |
2205 | for(gr=0;gr<nb_granules;gr++) { |
2206 | for(ch=0;ch<s->nb_channels;ch++) { | |
2207 | dprintf("gr=%d ch=%d: side_info\n", gr, ch); | |
2208 | g = &granules[ch][gr]; | |
2209 | g->part2_3_length = get_bits(&s->gb, 12); | |
2210 | g->big_values = get_bits(&s->gb, 9); | |
2211 | g->global_gain = get_bits(&s->gb, 8); | |
2212 | /* if MS stereo only is selected, we precompute the | |
2213 | 1/sqrt(2) renormalization factor */ | |
115329f1 | 2214 | if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) == |
239c2f4c FB |
2215 | MODE_EXT_MS_STEREO) |
2216 | g->global_gain -= 2; | |
2217 | if (s->lsf) | |
2218 | g->scalefac_compress = get_bits(&s->gb, 9); | |
2219 | else | |
2220 | g->scalefac_compress = get_bits(&s->gb, 4); | |
2221 | blocksplit_flag = get_bits(&s->gb, 1); | |
2222 | if (blocksplit_flag) { | |
2223 | g->block_type = get_bits(&s->gb, 2); | |
2224 | if (g->block_type == 0) | |
2225 | return -1; | |
2226 | g->switch_point = get_bits(&s->gb, 1); | |
2227 | for(i=0;i<2;i++) | |
2228 | g->table_select[i] = get_bits(&s->gb, 5); | |
115329f1 | 2229 | for(i=0;i<3;i++) |
239c2f4c FB |
2230 | g->subblock_gain[i] = get_bits(&s->gb, 3); |
2231 | /* compute huffman coded region sizes */ | |
2232 | if (g->block_type == 2) | |
2233 | g->region_size[0] = (36 / 2); | |
2234 | else { | |
115329f1 | 2235 | if (s->sample_rate_index <= 2) |
239c2f4c | 2236 | g->region_size[0] = (36 / 2); |
115329f1 | 2237 | else if (s->sample_rate_index != 8) |
239c2f4c FB |
2238 | g->region_size[0] = (54 / 2); |
2239 | else | |
2240 | g->region_size[0] = (108 / 2); | |
2241 | } | |
2242 | g->region_size[1] = (576 / 2); | |
2243 | } else { | |
2244 | int region_address1, region_address2, l; | |
2245 | g->block_type = 0; | |
2246 | g->switch_point = 0; | |
2247 | for(i=0;i<3;i++) | |
2248 | g->table_select[i] = get_bits(&s->gb, 5); | |
2249 | /* compute huffman coded region sizes */ | |
2250 | region_address1 = get_bits(&s->gb, 4); | |
2251 | region_address2 = get_bits(&s->gb, 3); | |
115329f1 | 2252 | dprintf("region1=%d region2=%d\n", |
239c2f4c | 2253 | region_address1, region_address2); |
115329f1 | 2254 | g->region_size[0] = |
239c2f4c FB |
2255 | band_index_long[s->sample_rate_index][region_address1 + 1] >> 1; |
2256 | l = region_address1 + region_address2 + 2; | |
2257 | /* should not overflow */ | |
2258 | if (l > 22) | |
2259 | l = 22; | |
115329f1 | 2260 | g->region_size[1] = |
239c2f4c FB |
2261 | band_index_long[s->sample_rate_index][l] >> 1; |
2262 | } | |
2263 | /* convert region offsets to region sizes and truncate | |
2264 | size to big_values */ | |
2265 | g->region_size[2] = (576 / 2); | |
2266 | j = 0; | |
2267 | for(i=0;i<3;i++) { | |
c7aa3696 | 2268 | k = FFMIN(g->region_size[i], g->big_values); |
239c2f4c FB |
2269 | g->region_size[i] = k - j; |
2270 | j = k; | |
2271 | } | |
2272 | ||
2273 | /* compute band indexes */ | |
2274 | if (g->block_type == 2) { | |
2275 | if (g->switch_point) { | |
2276 | /* if switched mode, we handle the 36 first samples as | |
2277 | long blocks. For 8000Hz, we handle the 48 first | |
2278 | exponents as long blocks (XXX: check this!) */ | |
2279 | if (s->sample_rate_index <= 2) | |
2280 | g->long_end = 8; | |
2281 | else if (s->sample_rate_index != 8) | |
2282 | g->long_end = 6; | |
2283 | else | |
2284 | g->long_end = 4; /* 8000 Hz */ | |
115329f1 | 2285 | |
c7aa3696 | 2286 | g->short_start = 2 + (s->sample_rate_index != 8); |
239c2f4c FB |
2287 | } else { |
2288 | g->long_end = 0; | |
2289 | g->short_start = 0; | |
2290 | } | |
2291 | } else { | |
2292 | g->short_start = 13; | |
2293 | g->long_end = 22; | |
2294 | } | |
115329f1 | 2295 | |
239c2f4c FB |
2296 | g->preflag = 0; |
2297 | if (!s->lsf) | |
2298 | g->preflag = get_bits(&s->gb, 1); | |
2299 | g->scalefac_scale = get_bits(&s->gb, 1); | |
2300 | g->count1table_select = get_bits(&s->gb, 1); | |
2301 | dprintf("block_type=%d switch_point=%d\n", | |
2302 | g->block_type, g->switch_point); | |
2303 | } | |
2304 | } | |
2305 | ||
1ede228a | 2306 | if (!s->adu_mode) { |
239c2f4c FB |
2307 | /* now we get bits from the main_data_begin offset */ |
2308 | dprintf("seekback: %d\n", main_data_begin); | |
2309 | seek_to_maindata(s, main_data_begin); | |
1ede228a | 2310 | } |
239c2f4c FB |
2311 | |
2312 | for(gr=0;gr<nb_granules;gr++) { | |
2313 | for(ch=0;ch<s->nb_channels;ch++) { | |
2314 | g = &granules[ch][gr]; | |
115329f1 | 2315 | |
239c2f4c | 2316 | bits_pos = get_bits_count(&s->gb); |
115329f1 | 2317 | |
239c2f4c | 2318 | if (!s->lsf) { |
0c1a9eda | 2319 | uint8_t *sc; |
239c2f4c FB |
2320 | int slen, slen1, slen2; |
2321 | ||
2322 | /* MPEG1 scale factors */ | |
2323 | slen1 = slen_table[0][g->scalefac_compress]; | |
2324 | slen2 = slen_table[1][g->scalefac_compress]; | |
2325 | dprintf("slen1=%d slen2=%d\n", slen1, slen2); | |
2326 | if (g->block_type == 2) { | |
2327 | n = g->switch_point ? 17 : 18; | |
2328 | j = 0; | |
e90a7270 MN |
2329 | if(slen1){ |
2330 | for(i=0;i<n;i++) | |
2331 | g->scale_factors[j++] = get_bits(&s->gb, slen1); | |
2332 | }else{ | |
2333 | for(i=0;i<n;i++) | |
2334 | g->scale_factors[j++] = 0; | |
2335 | } | |
2336 | if(slen2){ | |
2337 | for(i=0;i<18;i++) | |
2338 | g->scale_factors[j++] = get_bits(&s->gb, slen2); | |
2339 | for(i=0;i<3;i++) | |
2340 | g->scale_factors[j++] = 0; | |
2341 | }else{ | |
2342 | for(i=0;i<21;i++) | |
2343 | g->scale_factors[j++] = 0; | |
2344 | } | |
239c2f4c FB |
2345 | } else { |
2346 | sc = granules[ch][0].scale_factors; | |
2347 | j = 0; | |
2348 | for(k=0;k<4;k++) { | |
2349 | n = (k == 0 ? 6 : 5); | |
2350 | if ((g->scfsi & (0x8 >> k)) == 0) { | |
2351 | slen = (k < 2) ? slen1 : slen2; | |
e90a7270 MN |
2352 | if(slen){ |
2353 | for(i=0;i<n;i++) | |
2354 | g->scale_factors[j++] = get_bits(&s->gb, slen); | |
2355 | }else{ | |
2356 | for(i=0;i<n;i++) | |
2357 | g->scale_factors[j++] = 0; | |
2358 | } | |
239c2f4c FB |
2359 | } else { |
2360 | /* simply copy from last granule */ | |
2361 | for(i=0;i<n;i++) { | |
2362 | g->scale_factors[j] = sc[j]; | |
2363 | j++; | |
2364 | } | |
2365 | } | |
2366 | } | |
2367 | g->scale_factors[j++] = 0; | |
2368 | } | |
747a67fb | 2369 | #if defined(DEBUG) |
239c2f4c | 2370 | { |
267f7edc | 2371 | dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n", |
239c2f4c FB |
2372 | g->scfsi, gr, ch); |
2373 | for(i=0;i<j;i++) | |
267f7edc SH |
2374 | dprintf(" %d", g->scale_factors[i]); |
2375 | dprintf("\n"); | |
239c2f4c FB |
2376 | } |
2377 | #endif | |
2378 | } else { | |
2379 | int tindex, tindex2, slen[4], sl, sf; | |
2380 | ||
2381 | /* LSF scale factors */ | |
2382 | if (g->block_type == 2) { | |
2383 | tindex = g->switch_point ? 2 : 1; | |
2384 | } else { | |
2385 | tindex = 0; | |
2386 | } | |
2387 | sf = g->scalefac_compress; | |
2388 | if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) { | |
2389 | /* intensity stereo case */ | |
2390 | sf >>= 1; | |
2391 | if (sf < 180) { | |
2392 | lsf_sf_expand(slen, sf, 6, 6, 0); | |
2393 | tindex2 = 3; | |
2394 | } else if (sf < 244) { | |
2395 | lsf_sf_expand(slen, sf - 180, 4, 4, 0); | |
2396 | tindex2 = 4; | |
2397 | } else { | |
2398 | lsf_sf_expand(slen, sf - 244, 3, 0, 0); | |
2399 | tindex2 = 5; | |
2400 | } | |
2401 | } else { | |
2402 | /* normal case */ | |
2403 | if (sf < 400) { | |
2404 | lsf_sf_expand(slen, sf, 5, 4, 4); | |
2405 | tindex2 = 0; | |
2406 | } else if (sf < 500) { | |
2407 | lsf_sf_expand(slen, sf - 400, 5, 4, 0); | |
2408 | tindex2 = 1; | |
2409 | } else { | |
2410 | lsf_sf_expand(slen, sf - 500, 3, 0, 0); | |
2411 | tindex2 = 2; | |
2412 | g->preflag = 1; | |
2413 | } | |
2414 | } | |
2415 | ||
2416 | j = 0; | |
2417 | for(k=0;k<4;k++) { | |
2418 | n = lsf_nsf_table[tindex2][tindex][k]; | |
2419 | sl = slen[k]; | |
92c5cb62 | 2420 | if(sl){ |
e90a7270 MN |
2421 | for(i=0;i<n;i++) |
2422 | g->scale_factors[j++] = get_bits(&s->gb, sl); | |
2423 | }else{ | |
2424 | for(i=0;i<n;i++) | |
2425 | g->scale_factors[j++] = 0; | |
2426 | } | |
239c2f4c FB |
2427 | } |
2428 | /* XXX: should compute exact size */ | |
2429 | for(;j<40;j++) | |
2430 | g->scale_factors[j] = 0; | |
747a67fb | 2431 | #if defined(DEBUG) |
239c2f4c | 2432 | { |
267f7edc | 2433 | dprintf("gr=%d ch=%d scale_factors:\n", |
239c2f4c FB |
2434 | gr, ch); |
2435 | for(i=0;i<40;i++) | |
267f7edc SH |
2436 | dprintf(" %d", g->scale_factors[i]); |
2437 | dprintf("\n"); | |
239c2f4c FB |
2438 | } |
2439 | #endif | |
2440 | } | |
2441 | ||
2442 | exponents_from_scale_factors(s, g, exponents); | |
2443 | ||
2444 | /* read Huffman coded residue */ | |
2445 | if (huffman_decode(s, g, exponents, | |
2446 | bits_pos + g->part2_3_length) < 0) | |
2447 | return -1; | |
747a67fb FB |
2448 | #if defined(DEBUG) |
2449 | sample_dump(0, g->sb_hybrid, 576); | |
239c2f4c FB |
2450 | #endif |
2451 | ||
2452 | /* skip extension bits */ | |
2453 | bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos); | |
2454 | if (bits_left < 0) { | |
2455 | dprintf("bits_left=%d\n", bits_left); | |
2456 | return -1; | |
2457 | } | |
2458 | while (bits_left >= 16) { | |
2459 | skip_bits(&s->gb, 16); | |
2460 | bits_left -= 16; | |
2461 | } | |
2462 | if (bits_left > 0) | |
2463 | skip_bits(&s->gb, bits_left); | |
2464 | } /* ch */ | |
2465 | ||
2466 | if (s->nb_channels == 2) | |
2467 | compute_stereo(s, &granules[0][gr], &granules[1][gr]); | |
2468 | ||
2469 | for(ch=0;ch<s->nb_channels;ch++) { | |
2470 | g = &granules[ch][gr]; | |
2471 | ||
2472 | reorder_block(s, g); | |
747a67fb | 2473 | #if defined(DEBUG) |
239c2f4c FB |
2474 | sample_dump(0, g->sb_hybrid, 576); |
2475 | #endif | |
a1e257b2 | 2476 | s->compute_antialias(s, g); |
81552334 | 2477 | #if defined(DEBUG) |
239c2f4c FB |
2478 | sample_dump(1, g->sb_hybrid, 576); |
2479 | #endif | |
115329f1 | 2480 | compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); |
81552334 | 2481 | #if defined(DEBUG) |
239c2f4c FB |
2482 | sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576); |
2483 | #endif | |
2484 | } | |
2485 | } /* gr */ | |
2486 | return nb_granules * 18; | |
2487 | } | |
2488 | ||
115329f1 | 2489 | static int mp_decode_frame(MPADecodeContext *s, |
a3a5f4d6 | 2490 | OUT_INT *samples) |
239c2f4c FB |
2491 | { |
2492 | int i, nb_frames, ch; | |
a3a5f4d6 | 2493 | OUT_INT *samples_ptr; |
239c2f4c | 2494 | |
115329f1 | 2495 | init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, |
68f593b4 | 2496 | (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8); |
115329f1 | 2497 | |
239c2f4c FB |
2498 | /* skip error protection field */ |
2499 | if (s->error_protection) | |
2500 | get_bits(&s->gb, 16); | |
2501 | ||
2502 | dprintf("frame %d:\n", s->frame_count); | |
2503 | switch(s->layer) { | |
2504 | case 1: | |
2505 | nb_frames = mp_decode_layer1(s); | |
2506 | break; | |
2507 | case 2: | |
2508 | nb_frames = mp_decode_layer2(s); | |
2509 | break; | |
2510 | case 3: | |
2511 | default: | |
2512 | nb_frames = mp_decode_layer3(s); | |
2513 | break; | |
2514 | } | |
2515 | #if defined(DEBUG) | |
2516 | for(i=0;i<nb_frames;i++) { | |
2517 | for(ch=0;ch<s->nb_channels;ch++) { | |
2518 | int j; | |
267f7edc | 2519 | dprintf("%d-%d:", i, ch); |
239c2f4c | 2520 | for(j=0;j<SBLIMIT;j++) |
267f7edc SH |
2521 | dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE); |
2522 | dprintf("\n"); | |
239c2f4c FB |
2523 | } |
2524 | } | |
2525 | #endif | |
2526 | /* apply the synthesis filter */ | |
2527 | for(ch=0;ch<s->nb_channels;ch++) { | |
2528 | samples_ptr = samples + ch; | |
2529 | for(i=0;i<nb_frames;i++) { | |
bf1f4da0 | 2530 | ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]), |
bb270c08 DB |
2531 | window, &s->dither_state, |
2532 | samples_ptr, s->nb_channels, | |
239c2f4c FB |
2533 | s->sb_samples[ch][i]); |
2534 | samples_ptr += 32 * s->nb_channels; | |
2535 | } | |
2536 | } | |
2537 | #ifdef DEBUG | |
115329f1 | 2538 | s->frame_count++; |
239c2f4c | 2539 | #endif |
a3a5f4d6 | 2540 | return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels; |
239c2f4c FB |
2541 | } |
2542 | ||
de6d9b64 | 2543 | static int decode_frame(AVCodecContext * avctx, |
bb270c08 DB |
2544 | void *data, int *data_size, |
2545 | uint8_t * buf, int buf_size) | |
de6d9b64 FB |
2546 | { |
2547 | MPADecodeContext *s = avctx->priv_data; | |
0c1a9eda ZK |
2548 | uint32_t header; |
2549 | uint8_t *buf_ptr; | |
de6d9b64 | 2550 | int len, out_size; |
a3a5f4d6 | 2551 | OUT_INT *out_samples = data; |
de6d9b64 | 2552 | |
de6d9b64 FB |
2553 | buf_ptr = buf; |
2554 | while (buf_size > 0) { | |
bb270c08 DB |
2555 | len = s->inbuf_ptr - s->inbuf; |
2556 | if (s->frame_size == 0) { | |
239c2f4c FB |
2557 | /* special case for next header for first frame in free |
2558 | format case (XXX: find a simpler method) */ | |
2559 | if (s->free_format_next_header != 0) { | |
2560 | s->inbuf[0] = s->free_format_next_header >> 24; | |
2561 | s->inbuf[1] = s->free_format_next_header >> 16; | |
2562 | s->inbuf[2] = s->free_format_next_header >> 8; | |
2563 | s->inbuf[3] = s->free_format_next_header; | |
2564 | s->inbuf_ptr = s->inbuf + 4; | |
2565 | s->free_format_next_header = 0; | |
2566 | goto got_header; | |
2567 | } | |
bb270c08 | 2568 | /* no header seen : find one. We need at least HEADER_SIZE |
239c2f4c | 2569 | bytes to parse it */ |
bb270c08 DB |
2570 | len = HEADER_SIZE - len; |
2571 | if (len > buf_size) | |
2572 | len = buf_size; | |
2573 | if (len > 0) { | |
2574 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
2575 | buf_ptr += len; | |
2576 | buf_size -= len; | |
2577 | s->inbuf_ptr += len; | |
2578 | } | |
2579 | if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) { | |
239c2f4c | 2580 | got_header: |
bb270c08 DB |
2581 | header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | |
2582 | (s->inbuf[2] << 8) | s->inbuf[3]; | |
92d24f49 | 2583 | |
bb270c08 DB |
2584 | if (ff_mpa_check_header(header) < 0) { |
2585 | /* no sync found : move by one byte (inefficient, but simple!) */ | |
2586 | memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
2587 | s->inbuf_ptr--; | |
239c2f4c FB |
2588 | dprintf("skip %x\n", header); |
2589 | /* reset free format frame size to give a chance | |
2590 | to get a new bitrate */ | |
2591 | s->free_format_frame_size = 0; | |
bb270c08 DB |
2592 | } else { |
2593 | if (decode_header(s, header) == 1) { | |
81552334 | 2594 | /* free format: prepare to compute frame size */ |
bb270c08 | 2595 | s->frame_size = -1; |
239c2f4c | 2596 | } |
81552334 FB |
2597 | /* update codec info */ |
2598 | avctx->sample_rate = s->sample_rate; | |
2599 | avctx->channels = s->nb_channels; | |
2600 | avctx->bit_rate = s->bit_rate; | |
98ce5991 | 2601 | avctx->sub_id = s->layer; |
8c5b5683 FB |
2602 | switch(s->layer) { |
2603 | case 1: | |
2604 | avctx->frame_size = 384; | |
2605 | break; | |
2606 | case 2: | |
2607 | avctx->frame_size = 1152; | |
2608 | break; | |
2609 | case 3: | |
2610 | if (s->lsf) | |
2611 | avctx->frame_size = 576; | |
2612 | else | |
2613 | avctx->frame_size = 1152; | |
2614 | break; | |
2615 | } | |
bb270c08 DB |
2616 | } |
2617 | } | |
239c2f4c FB |
2618 | } else if (s->frame_size == -1) { |
2619 | /* free format : find next sync to compute frame size */ | |
bb270c08 DB |
2620 | len = MPA_MAX_CODED_FRAME_SIZE - len; |
2621 | if (len > buf_size) | |
2622 | len = buf_size; | |
239c2f4c | 2623 | if (len == 0) { |
bb270c08 | 2624 | /* frame too long: resync */ |
239c2f4c | 2625 | s->frame_size = 0; |
bb270c08 DB |
2626 | memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); |
2627 | s->inbuf_ptr--; | |
239c2f4c | 2628 | } else { |
0c1a9eda ZK |
2629 | uint8_t *p, *pend; |
2630 | uint32_t header1; | |
239c2f4c FB |
2631 | int padding; |
2632 | ||
2633 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
2634 | /* check for header */ | |
2635 | p = s->inbuf_ptr - 3; | |
2636 | pend = s->inbuf_ptr + len - 4; | |
2637 | while (p <= pend) { | |
2638 | header = (p[0] << 24) | (p[1] << 16) | | |
2639 | (p[2] << 8) | p[3]; | |
2640 | header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
2641 | (s->inbuf[2] << 8) | s->inbuf[3]; | |
2642 | /* check with high probability that we have a | |
2643 | valid header */ | |
2644 | if ((header & SAME_HEADER_MASK) == | |
2645 | (header1 & SAME_HEADER_MASK)) { | |
2646 | /* header found: update pointers */ | |
2647 | len = (p + 4) - s->inbuf_ptr; | |
2648 | buf_ptr += len; | |
2649 | buf_size -= len; | |
2650 | s->inbuf_ptr = p; | |
2651 | /* compute frame size */ | |
2652 | s->free_format_next_header = header; | |
2653 | s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
2654 | padding = (header1 >> 9) & 1; | |
2655 | if (s->layer == 1) | |
2656 | s->free_format_frame_size -= padding * 4; | |
2657 | else | |
2658 | s->free_format_frame_size -= padding; | |
115329f1 | 2659 | dprintf("free frame size=%d padding=%d\n", |
239c2f4c FB |
2660 | s->free_format_frame_size, padding); |
2661 | decode_header(s, header1); | |
2662 | goto next_data; | |
2663 | } | |
2664 | p++; | |
2665 | } | |
2666 | /* not found: simply increase pointers */ | |
2667 | buf_ptr += len; | |
2668 | s->inbuf_ptr += len; | |
2669 | buf_size -= len; | |
2670 | } | |
bb270c08 | 2671 | } else if (len < s->frame_size) { |
de5123dc ZK |
2672 | if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) |
2673 | s->frame_size = MPA_MAX_CODED_FRAME_SIZE; | |
bb270c08 DB |
2674 | len = s->frame_size - len; |
2675 | if (len > buf_size) | |
2676 | len = buf_size; | |
2677 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
2678 | buf_ptr += len; | |
2679 | s->inbuf_ptr += len; | |
2680 | buf_size -= len; | |
2681 | } | |
8c5b5683 | 2682 | next_data: |
115329f1 | 2683 | if (s->frame_size > 0 && |
8c5b5683 FB |
2684 | (s->inbuf_ptr - s->inbuf) >= s->frame_size) { |
2685 | if (avctx->parse_only) { | |
2686 | /* simply return the frame data */ | |
2687 | *(uint8_t **)data = s->inbuf; | |
2688 | out_size = s->inbuf_ptr - s->inbuf; | |
2689 | } else { | |
2690 | out_size = mp_decode_frame(s, out_samples); | |
2691 | } | |
bb270c08 DB |
2692 | s->inbuf_ptr = s->inbuf; |
2693 | s->frame_size = 0; | |
02af2269 | 2694 | if(out_size>=0) |
bb270c08 | 2695 | *data_size = out_size; |
02af2269 MN |
2696 | else |
2697 | av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed | |
bb270c08 DB |
2698 | break; |
2699 | } | |
de6d9b64 FB |
2700 | } |
2701 | return buf_ptr - buf; | |
2702 | } | |
2703 | ||
1ede228a RT |
2704 | |
2705 | static int decode_frame_adu(AVCodecContext * avctx, | |
bb270c08 DB |
2706 | void *data, int *data_size, |
2707 | uint8_t * buf, int buf_size) | |
1ede228a RT |
2708 | { |
2709 | MPADecodeContext *s = avctx->priv_data; | |
2710 | uint32_t header; | |
2711 | int len, out_size; | |
a3a5f4d6 | 2712 | OUT_INT *out_samples = data; |
1ede228a RT |
2713 | |
2714 | len = buf_size; | |
2715 | ||
2716 | // Discard too short frames | |
2717 | if (buf_size < HEADER_SIZE) { | |
2718 | *data_size = 0; | |
2719 | return buf_size; | |
2720 | } | |
2721 | ||
2722 | ||
2723 | if (len > MPA_MAX_CODED_FRAME_SIZE) | |
2724 | len = MPA_MAX_CODED_FRAME_SIZE; | |
2725 | ||
2726 | memcpy(s->inbuf, buf, len); | |
2727 | s->inbuf_ptr = s->inbuf + len; | |
2728 | ||
2729 | // Get header and restore sync word | |
2730 | header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
2731 | (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000; | |
2732 | ||
a7a85899 | 2733 | if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame |
1ede228a RT |
2734 | *data_size = 0; |
2735 | return buf_size; | |
2736 | } | |
2737 | ||
2738 | decode_header(s, header); | |
2739 | /* update codec info */ | |
2740 | avctx->sample_rate = s->sample_rate; | |
2741 | avctx->channels = s->nb_channels; | |
2742 | avctx->bit_rate = s->bit_rate; | |
2743 | avctx->sub_id = s->layer; | |
2744 | ||
2745 | avctx->frame_size=s->frame_size = len; | |
2746 | ||
2747 | if (avctx->parse_only) { | |
2748 | /* simply return the frame data */ | |
2749 | *(uint8_t **)data = s->inbuf; | |
2750 | out_size = s->inbuf_ptr - s->inbuf; | |
2751 | } else { | |
2752 | out_size = mp_decode_frame(s, out_samples); | |
2753 | } | |
2754 | ||
2755 | *data_size = out_size; | |
2756 | return buf_size; | |
2757 | } | |
2758 | ||
2759 | ||
d2a7718d RT |
2760 | /* Next 3 arrays are indexed by channel config number (passed via codecdata) */ |
2761 | static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */ | |
2762 | static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */ | |
2763 | /* offsets into output buffer, assume output order is FL FR BL BR C LFE */ | |
2764 | static int chan_offset[9][5] = { | |
2765 | {0}, | |
2766 | {0}, // C | |
2767 | {0}, // FLR | |
2768 | {2,0}, // C FLR | |
2769 | {2,0,3}, // C FLR BS | |
2770 | {4,0,2}, // C FLR BLRS | |
2771 | {4,0,2,5}, // C FLR BLRS LFE | |
2772 | {4,0,2,6,5}, // C FLR BLRS BLR LFE | |
2773 | {0,2} // FLR BLRS | |
2774 | }; | |
2775 | ||
2776 | ||
2777 | static int decode_init_mp3on4(AVCodecContext * avctx) | |
2778 | { | |
2779 | MP3On4DecodeContext *s = avctx->priv_data; | |
2780 | int i; | |
2781 | ||
2782 | if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) { | |
2783 | av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n"); | |
2784 | return -1; | |
2785 | } | |
2786 | ||
2787 | s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f; | |
2788 | s->frames = mp3Frames[s->chan_cfg]; | |
2789 | if(!s->frames) { | |
2790 | av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n"); | |
2791 | return -1; | |
2792 | } | |
2793 | avctx->channels = mp3Channels[s->chan_cfg]; | |
2794 | ||
2795 | /* Init the first mp3 decoder in standard way, so that all tables get builded | |
2796 | * We replace avctx->priv_data with the context of the first decoder so that | |
2797 | * decode_init() does not have to be changed. | |
2798 | * Other decoders will be inited here copying data from the first context | |
2799 | */ | |
2800 | // Allocate zeroed memory for the first decoder context | |
2801 | s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext)); | |
2802 | // Put decoder context in place to make init_decode() happy | |
2803 | avctx->priv_data = s->mp3decctx[0]; | |
2804 | decode_init(avctx); | |
2805 | // Restore mp3on4 context pointer | |
2806 | avctx->priv_data = s; | |
2807 | s->mp3decctx[0]->adu_mode = 1; // Set adu mode | |
2808 | ||
2809 | /* Create a separate codec/context for each frame (first is already ok). | |
2810 | * Each frame is 1 or 2 channels - up to 5 frames allowed | |
2811 | */ | |
2812 | for (i = 1; i < s->frames; i++) { | |
2813 | s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext)); | |
2814 | s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias; | |
2815 | s->mp3decctx[i]->inbuf = &s->mp3decctx[i]->inbuf1[0][BACKSTEP_SIZE]; | |
2816 | s->mp3decctx[i]->inbuf_ptr = s->mp3decctx[i]->inbuf; | |
2817 | s->mp3decctx[i]->adu_mode = 1; | |
2818 | } | |
2819 | ||
2820 | return 0; | |
2821 | } | |
2822 | ||
2823 | ||
2824 | static int decode_close_mp3on4(AVCodecContext * avctx) | |
2825 | { | |
2826 | MP3On4DecodeContext *s = avctx->priv_data; | |
2827 | int i; | |
2828 | ||
2829 | for (i = 0; i < s->frames; i++) | |
2830 | if (s->mp3decctx[i]) | |
2831 | av_free(s->mp3decctx[i]); | |
2832 | ||
2833 | return 0; | |
2834 | } | |
2835 | ||
2836 | ||
2837 | static int decode_frame_mp3on4(AVCodecContext * avctx, | |
bb270c08 DB |
2838 | void *data, int *data_size, |
2839 | uint8_t * buf, int buf_size) | |
d2a7718d RT |
2840 | { |
2841 | MP3On4DecodeContext *s = avctx->priv_data; | |
2842 | MPADecodeContext *m; | |
2843 | int len, out_size = 0; | |
2844 | uint32_t header; | |
2845 | OUT_INT *out_samples = data; | |
2846 | OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS]; | |
2847 | OUT_INT *outptr, *bp; | |
2848 | int fsize; | |
2849 | unsigned char *start2 = buf, *start; | |
2850 | int fr, i, j, n; | |
2851 | int off = avctx->channels; | |
2852 | int *coff = chan_offset[s->chan_cfg]; | |
2853 | ||
2854 | len = buf_size; | |
2855 | ||
2856 | // Discard too short frames | |
2857 | if (buf_size < HEADER_SIZE) { | |
2858 | *data_size = 0; | |
2859 | return buf_size; | |
2860 | } | |
2861 | ||
2862 | // If only one decoder interleave is not needed | |
2863 | outptr = s->frames == 1 ? out_samples : decoded_buf; | |
2864 | ||
2865 | for (fr = 0; fr < s->frames; fr++) { | |
2866 | start = start2; | |
2867 | fsize = (start[0] << 4) | (start[1] >> 4); | |
2868 | start2 += fsize; | |
2869 | if (fsize > len) | |
2870 | fsize = len; | |
2871 | len -= fsize; | |
2872 | if (fsize > MPA_MAX_CODED_FRAME_SIZE) | |
2873 | fsize = MPA_MAX_CODED_FRAME_SIZE; | |
2874 | m = s->mp3decctx[fr]; | |
2875 | assert (m != NULL); | |
2876 | /* copy original to new */ | |
2877 | m->inbuf_ptr = m->inbuf + fsize; | |
2878 | memcpy(m->inbuf, start, fsize); | |
2879 | ||
2880 | // Get header | |
2881 | header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) | | |
2882 | (m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000; | |
2883 | ||
2884 | if (ff_mpa_check_header(header) < 0) { // Bad header, discard block | |
2885 | *data_size = 0; | |
2886 | return buf_size; | |
2887 | } | |
2888 | ||
2889 | decode_header(m, header); | |
2890 | mp_decode_frame(m, decoded_buf); | |
2891 | ||
2892 | n = MPA_FRAME_SIZE * m->nb_channels; | |
2893 | out_size += n * sizeof(OUT_INT); | |
2894 | if(s->frames > 1) { | |
2895 | /* interleave output data */ | |
2896 | bp = out_samples + coff[fr]; | |
2897 | if(m->nb_channels == 1) { | |
2898 | for(j = 0; j < n; j++) { | |
2899 | *bp = decoded_buf[j]; | |
2900 | bp += off; | |
2901 | } | |
2902 | } else { | |
2903 | for(j = 0; j < n; j++) { | |
2904 | bp[0] = decoded_buf[j++]; | |
2905 | bp[1] = decoded_buf[j]; | |
2906 | bp += off; | |
2907 | } | |
2908 | } | |
2909 | } | |
2910 | } | |
2911 | ||
2912 | /* update codec info */ | |
2913 | avctx->sample_rate = s->mp3decctx[0]->sample_rate; | |
2914 | avctx->frame_size= buf_size; | |
2915 | avctx->bit_rate = 0; | |
2916 | for (i = 0; i < s->frames; i++) | |
2917 | avctx->bit_rate += s->mp3decctx[i]->bit_rate; | |
2918 | ||
2919 | *data_size = out_size; | |
2920 | return buf_size; | |
2921 | } | |
2922 | ||
2923 | ||
4b1f4f23 | 2924 | AVCodec mp2_decoder = |
de6d9b64 | 2925 | { |
4b1f4f23 | 2926 | "mp2", |
de6d9b64 FB |
2927 | CODEC_TYPE_AUDIO, |
2928 | CODEC_ID_MP2, | |
2929 | sizeof(MPADecodeContext), | |
2930 | decode_init, | |
2931 | NULL, | |
2932 | NULL, | |
2933 | decode_frame, | |
8c5b5683 | 2934 | CODEC_CAP_PARSE_ONLY, |
de6d9b64 | 2935 | }; |
4b1f4f23 J |
2936 | |
2937 | AVCodec mp3_decoder = | |
2938 | { | |
2939 | "mp3", | |
2940 | CODEC_TYPE_AUDIO, | |
80783dc2 | 2941 | CODEC_ID_MP3, |
4b1f4f23 J |
2942 | sizeof(MPADecodeContext), |
2943 | decode_init, | |
2944 | NULL, | |
2945 | NULL, | |
2946 | decode_frame, | |
8c5b5683 | 2947 | CODEC_CAP_PARSE_ONLY, |
4b1f4f23 | 2948 | }; |
1ede228a RT |
2949 | |
2950 | AVCodec mp3adu_decoder = | |
2951 | { | |
2952 | "mp3adu", | |
2953 | CODEC_TYPE_AUDIO, | |
2954 | CODEC_ID_MP3ADU, | |
2955 | sizeof(MPADecodeContext), | |
2956 | decode_init, | |
2957 | NULL, | |
2958 | NULL, | |
2959 | decode_frame_adu, | |
2960 | CODEC_CAP_PARSE_ONLY, | |
2961 | }; | |
d2a7718d RT |
2962 | |
2963 | AVCodec mp3on4_decoder = | |
2964 | { | |
2965 | "mp3on4", | |
2966 | CODEC_TYPE_AUDIO, | |
2967 | CODEC_ID_MP3ON4, | |
2968 | sizeof(MP3On4DecodeContext), | |
2969 | decode_init_mp3on4, | |
2970 | NULL, | |
2971 | decode_close_mp3on4, | |
2972 | decode_frame_mp3on4, | |
2973 | 0 | |
2974 | }; |