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