📄 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 "bitstream.h"#include "mpegaudio.h"#include "dsputil.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#if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)typedef int32_t OUT_INT;#define OUT_MAX INT32_MAX#define OUT_MIN INT32_MIN#define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 31)#elsetypedef int16_t OUT_INT;#define OUT_MAX INT16_MAX#define OUT_MIN INT16_MIN#define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)#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)#define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))//#define MULH(a,b) (((int64_t)(a) * (int64_t)(b))>>32) //gcc 3.4 creates an incredibly bloated mess out of thisstatic always_inline int MULH(int a, int b){ return ((int64_t)(a) * (int64_t)(b))>>32;}#if FRAC_BITS <= 15typedef int16_t MPA_INT;#elsetypedef int32_t MPA_INT;#endif/****************/#define HEADER_SIZE 4#define BACKSTEP_SIZE 512struct GranuleDef;typedef 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 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g); int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3 unsigned int dither_state;} MPADecodeContext;/** * Context for MP3On4 decoder */typedef struct MP3On4DecodeContext { int frames; ///< number of mp3 frames per block (number of mp3 decoder instances) int chan_cfg; ///< channel config number MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance} MP3On4DecodeContext;/* 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"static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);/* 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)*4static int8_t *table_4_3_exp;static uint32_t *table_4_3_value;/* 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][4];static float csa_table_float[8][4];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 const 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 */};void ff_mpa_synth_init(MPA_INT *window);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){ unsigned int m; int e; e = table_4_3_exp [4*value + (exponent&3)]; m = table_4_3_value[4*value + (exponent&3)]; e -= (exponent >> 2); assert(e>=1); if (e > 31) return 0; m = (m + (1 << (e-1))) >> e; return m;}/* 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];#if 0 /* unused */static int pow_mult3[3] = { POW_FIX(1.0), POW_FIX(1.25992104989487316476), POW_FIX(1.58740105196819947474),};#endifstatic 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; }}#if 0 /* unused, remove? *//* 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;}#endifstatic int decode_init(AVCodecContext * avctx){ MPADecodeContext *s = avctx->priv_data; static int init=0; int i, j, k;#if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT) avctx->sample_fmt= SAMPLE_FMT_S32;#else avctx->sample_fmt= SAMPLE_FMT_S16;#endif if(avctx->antialias_algo != FF_AA_FLOAT) s->compute_antialias= compute_antialias_integer; else s->compute_antialias= compute_antialias_float; 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]); } ff_mpa_synth_init(window); /* 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, 1); 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, 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 */ table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0])); if(!table_4_3_exp) return -1; table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0])); if(!table_4_3_value) return -1; int_pow_init(); for(i=1;i<TABLE_4_3_SIZE;i++) { double f, fm; int e, m; f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25); fm = frexp(f, &e); m = (uint32_t)(fm*(1LL<<31) + 0.5); e+= FRAC_BITS - 31 + 5; /* normalized to FRAC_BITS */ table_4_3_value[i] = m;// av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0)); 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 + -