📄 mpegaudiodec.c
字号:
/* * MPEG Audio decoder * Copyright (c) 2001, 2002 Fabrice Bellard. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//** * @file mpegaudiodec.c * MPEG Audio decoder. */ //#define DEBUG#include "avcodec.h"#include "mpegaudio.h"/* * TODO: * - in low precision mode, use more 16 bit multiplies in synth filter * - test lsf / mpeg25 extensively. *//* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg audio decoder */#ifdef CONFIG_MPEGAUDIO_HP#define USE_HIGHPRECISION#endif#ifdef USE_HIGHPRECISION#define FRAC_BITS 23 /* fractional bits for sb_samples and dct */#define WFRAC_BITS 16 /* fractional bits for window */#else#define FRAC_BITS 15 /* fractional bits for sb_samples and dct */#define WFRAC_BITS 14 /* fractional bits for window */#endif#define FRAC_ONE (1 << FRAC_BITS)#define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)#define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))#define FIX(a) ((int)((a) * FRAC_ONE))/* WARNING: only correct for posititive numbers */#define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)#if FRAC_BITS <= 15typedef int16_t MPA_INT;#elsetypedef int32_t MPA_INT;#endif/****************/#define HEADER_SIZE 4#define BACKSTEP_SIZE 512typedef struct MPADecodeContext { uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */ int inbuf_index; uint8_t *inbuf_ptr, *inbuf; int frame_size; int free_format_frame_size; /* frame size in case of free format (zero if currently unknown) */ /* next header (used in free format parsing) */ uint32_t free_format_next_header; int error_protection; int layer; int sample_rate; int sample_rate_index; /* between 0 and 8 */ int bit_rate; int old_frame_size; GetBitContext gb; int nb_channels; int mode; int mode_ext; int lsf; MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16))); int synth_buf_offset[MPA_MAX_CHANNELS]; int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16))); int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */#ifdef DEBUG int frame_count;#endif} MPADecodeContext;/* layer 3 "granule" */typedef struct GranuleDef { uint8_t scfsi; int part2_3_length; int big_values; int global_gain; int scalefac_compress; uint8_t block_type; uint8_t switch_point; int table_select[3]; int subblock_gain[3]; uint8_t scalefac_scale; uint8_t count1table_select; int region_size[3]; /* number of huffman codes in each region */ int preflag; int short_start, long_end; /* long/short band indexes */ uint8_t scale_factors[40]; int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */} GranuleDef;#define MODE_EXT_MS_STEREO 2#define MODE_EXT_I_STEREO 1/* layer 3 huffman tables */typedef struct HuffTable { int xsize; const uint8_t *bits; const uint16_t *codes;} HuffTable;#include "mpegaudiodectab.h"/* vlc structure for decoding layer 3 huffman tables */static VLC huff_vlc[16]; static uint8_t *huff_code_table[16];static VLC huff_quad_vlc[2];/* computed from band_size_long */static uint16_t band_index_long[9][23];/* XXX: free when all decoders are closed */#define TABLE_4_3_SIZE (8191 + 16)static int8_t *table_4_3_exp;#if FRAC_BITS <= 15static uint16_t *table_4_3_value;#elsestatic uint32_t *table_4_3_value;#endif/* intensity stereo coef table */static int32_t is_table[2][16];static int32_t is_table_lsf[2][2][16];static int32_t csa_table[8][2];static int32_t mdct_win[8][36];/* lower 2 bits: modulo 3, higher bits: shift */static uint16_t scale_factor_modshift[64];/* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */static int32_t scale_factor_mult[15][3];/* mult table for layer 2 group quantization */#define SCALE_GEN(v) \{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }static int32_t scale_factor_mult2[3][3] = { SCALE_GEN(4.0 / 3.0), /* 3 steps */ SCALE_GEN(4.0 / 5.0), /* 5 steps */ SCALE_GEN(4.0 / 9.0), /* 9 steps */};/* 2^(n/4) */static uint32_t scale_factor_mult3[4] = { FIXR(1.0), FIXR(1.18920711500272106671), FIXR(1.41421356237309504880), FIXR(1.68179283050742908605),};static MPA_INT window[512] __attribute__((aligned(16))); /* layer 1 unscaling *//* n = number of bits of the mantissa minus 1 */static inline int l1_unscale(int n, int mant, int scale_factor){ int shift, mod; int64_t val; shift = scale_factor_modshift[scale_factor]; mod = shift & 3; shift >>= 2; val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]); shift += n; /* NOTE: at this point, 1 <= shift >= 21 + 15 */ return (int)((val + (1LL << (shift - 1))) >> shift);}static inline int l2_unscale_group(int steps, int mant, int scale_factor){ int shift, mod, val; shift = scale_factor_modshift[scale_factor]; mod = shift & 3; shift >>= 2; val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod]; /* NOTE: at this point, 0 <= shift <= 21 */ if (shift > 0) val = (val + (1 << (shift - 1))) >> shift; return val;}/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */static inline int l3_unscale(int value, int exponent){#if FRAC_BITS <= 15 unsigned int m;#else uint64_t m;#endif int e; e = table_4_3_exp[value]; e += (exponent >> 2); e = FRAC_BITS - e;#if FRAC_BITS <= 15 if (e > 31) e = 31;#endif m = table_4_3_value[value];#if FRAC_BITS <= 15 m = (m * scale_factor_mult3[exponent & 3]); m = (m + (1 << (e-1))) >> e; return m;#else m = MUL64(m, scale_factor_mult3[exponent & 3]); m = (m + (uint64_t_C(1) << (e-1))) >> e; return m;#endif}/* all integer n^(4/3) computation code */#define DEV_ORDER 13#define POW_FRAC_BITS 24#define POW_FRAC_ONE (1 << POW_FRAC_BITS)#define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)static int dev_4_3_coefs[DEV_ORDER];static int pow_mult3[3] = { POW_FIX(1.0), POW_FIX(1.25992104989487316476), POW_FIX(1.58740105196819947474),};static void int_pow_init(void){ int i, a; a = POW_FIX(1.0); for(i=0;i<DEV_ORDER;i++) { a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1); dev_4_3_coefs[i] = a; }}/* return the mantissa and the binary exponent */static int int_pow(int i, int *exp_ptr){ int e, er, eq, j; int a, a1; /* renormalize */ a = i; e = POW_FRAC_BITS; while (a < (1 << (POW_FRAC_BITS - 1))) { a = a << 1; e--; } a -= (1 << POW_FRAC_BITS); a1 = 0; for(j = DEV_ORDER - 1; j >= 0; j--) a1 = POW_MULL(a, dev_4_3_coefs[j] + a1); a = (1 << POW_FRAC_BITS) + a1; /* exponent compute (exact) */ e = e * 4; er = e % 3; eq = e / 3; a = POW_MULL(a, pow_mult3[er]); while (a >= 2 * POW_FRAC_ONE) { a = a >> 1; eq++; } /* convert to float */ while (a < POW_FRAC_ONE) { a = a << 1; eq--; } /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */#if POW_FRAC_BITS > FRAC_BITS a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS); /* correct overflow */ if (a >= 2 * (1 << FRAC_BITS)) { a = a >> 1; eq++; }#endif *exp_ptr = eq; return a;}static int decode_init(AVCodecContext * avctx){ MPADecodeContext *s = avctx->priv_data; static int init=0; int i, j, k; if (!init && !avctx->parse_only) { /* scale factors table for layer 1/2 */ for(i=0;i<64;i++) { int shift, mod; /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */ shift = (i / 3); mod = i % 3; scale_factor_modshift[i] = mod | (shift << 2); } /* scale factor multiply for layer 1 */ for(i=0;i<15;i++) { int n, norm; n = i + 2; norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1); scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm); scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm); scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm); dprintf("%d: norm=%x s=%x %x %x\n", i, norm, scale_factor_mult[i][0], scale_factor_mult[i][1], scale_factor_mult[i][2]); } /* window */ /* max = 18760, max sum over all 16 coefs : 44736 */ for(i=0;i<257;i++) { int v; v = mpa_enwindow[i];#if WFRAC_BITS < 16 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);#endif window[i] = v; if ((i & 63) != 0) v = -v; if (i != 0) window[512 - i] = v; } /* huffman decode tables */ huff_code_table[0] = NULL; for(i=1;i<16;i++) { const HuffTable *h = &mpa_huff_tables[i]; int xsize, x, y; unsigned int n; uint8_t *code_table; xsize = h->xsize; n = xsize * xsize; /* XXX: fail test */ init_vlc(&huff_vlc[i], 8, n, h->bits, 1, 1, h->codes, 2, 2); code_table = av_mallocz(n); j = 0; for(x=0;x<xsize;x++) { for(y=0;y<xsize;y++) code_table[j++] = (x << 4) | y; } huff_code_table[i] = code_table; } for(i=0;i<2;i++) { init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1); } for(i=0;i<9;i++) { k = 0; for(j=0;j<22;j++) { band_index_long[i][j] = k; k += band_size_long[i][j]; } band_index_long[i][22] = k; } /* compute n ^ (4/3) and store it in mantissa/exp format */ if (!av_mallocz_static(&table_4_3_exp, TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]))) return -1; if (!av_mallocz_static(&table_4_3_value, TABLE_4_3_SIZE * sizeof(table_4_3_value[0]))) return -1; int_pow_init(); for(i=1;i<TABLE_4_3_SIZE;i++) { int e, m; m = int_pow(i, &e);#if 0 /* test code */ { double f, fm; int e1, m1; f = pow((double)i, 4.0 / 3.0); fm = frexp(f, &e1); m1 = FIXR(2 * fm);#if FRAC_BITS <= 15 if ((unsigned short)m1 != m1) { m1 = m1 >> 1; e1++; }#endif e1--; if (m != m1 || e != e1) { printf("%4d: m=%x m1=%x e=%d e1=%d\n", i, m, m1, e, e1); } }#endif /* normalized to FRAC_BITS */ table_4_3_value[i] = m; table_4_3_exp[i] = e; } for(i=0;i<7;i++) { float f; int v; if (i != 6) { f = tan((double)i * M_PI / 12.0); v = FIXR(f / (1.0 + f)); } else { v = FIXR(1.0); } is_table[0][i] = v; is_table[1][6 - i] = v; } /* invalid values */ for(i=7;i<16;i++) is_table[0][i] = is_table[1][i] = 0.0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -