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