📄 mpegaudiodec.c
字号:
/* * MPEG Audio decoder * Copyright (c) 2001, 2002 Fabrice Bellard. * * This file is part of FFmpeg. * * FFmpeg 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.1 of the License, or (at your option) any later version. * * FFmpeg 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 FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *//** * @file mpegaudiodec.c * MPEG Audio decoder. *///#define DEBUG#include "avcodec.h"#include "bitstream.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#include "mpegaudio.h"#include "mpegaudiodecheader.h"#include "mathops.h"/* 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 HEADER_SIZE 4/** * 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;#include "mpegaudiodata.h"#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 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[TABLE_4_3_SIZE];static uint32_t table_4_3_value[TABLE_4_3_SIZE];static uint32_t exp_table[512];static uint32_t expval_table[512][16];/* 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 */};static DECLARE_ALIGNED_16(MPA_INT, window[512]);/** * Convert region offsets to region sizes and truncate * size to big_values. */void ff_region_offset2size(GranuleDef *g){ int i, k, j=0; g->region_size[2] = (576 / 2); for(i=0;i<3;i++) { k = FFMIN(g->region_size[i], g->big_values); g->region_size[i] = k - j; j = k; }}void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){ if (g->block_type == 2) g->region_size[0] = (36 / 2); else { if (s->sample_rate_index <= 2) g->region_size[0] = (36 / 2); else if (s->sample_rate_index != 8) g->region_size[0] = (54 / 2); else g->region_size[0] = (108 / 2); } g->region_size[1] = (576 / 2);}void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){ int l; g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1; /* should not overflow */ l = FFMIN(ra1 + ra2 + 2, 22); g->region_size[1] = band_index_long[s->sample_rate_index][l] >> 1;}void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){ if (g->block_type == 2) { if (g->switch_point) { /* if switched mode, we handle the 36 first samples as long blocks. For 8000Hz, we handle the 48 first exponents as long blocks (XXX: check this!) */ if (s->sample_rate_index <= 2) g->long_end = 8; else if (s->sample_rate_index != 8) g->long_end = 6; else g->long_end = 4; /* 8000 Hz */ g->short_start = 2 + (s->sample_rate_index != 8); } else { g->long_end = 0; g->short_start = 0; } } else { g->short_start = 13; g->long_end = 22; }}/* 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; s->avctx = avctx;#if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT) avctx->sample_fmt= SAMPLE_FMT_S32;#else avctx->sample_fmt= SAMPLE_FMT_S16;#endif s->error_resilience= avctx->error_resilience; 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_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(avctx, "%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 */ for(i=1;i<16;i++) { const HuffTable *h = &mpa_huff_tables[i]; int xsize, x, y; unsigned int n; uint8_t tmp_bits [512]; uint16_t tmp_codes[512]; memset(tmp_bits , 0, sizeof(tmp_bits )); memset(tmp_codes, 0, sizeof(tmp_codes)); xsize = h->xsize; n = xsize * xsize; j = 0; for(x=0;x<xsize;x++) { for(y=0;y<xsize;y++){ tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ]; tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++]; } } /* XXX: fail test */ init_vlc(&huff_vlc[i], 7, 512, tmp_bits, 1, 1, tmp_codes, 2, 2, 1); } 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 */ 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 - 100; /* 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<512*16; i++){ int exponent= (i>>4); double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5); expval_table[exponent][i&15]= llrint(f); if((i&15)==1) exp_table[exponent]= llrint(f); } 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 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -