/* * 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
*/
/* layer 3 "granule" */ typedefstruct 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];
DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
} GranuleDef;
typedefstruct MPADecodeContext {
MPA_DECODE_HEADER
uint8_t last_buf[LAST_BUF_SIZE]; int last_buf_size; int extrasize; /* next header (used in free format parsing) */
uint32_t free_format_next_header;
GetBitContext gb;
GetBitContext in_gb;
DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2]; int synth_buf_offset[MPA_MAX_CHANNELS];
DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
GranuleDef granules[2][2]; /* Used in Layer 3 */ int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3 int dither_state; int err_recognition;
AVCodecContext* avctx;
MPADSPContext mpadsp; void (*butterflies_float)(float *restrict v1, float *restrict v2, int len);
AVFrame *frame;
uint32_t crc;
} MPADecodeContext;
/** * Convert region offsets to region sizes and truncate * size to big_values.
*/ staticvoid 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;
}
}
staticvoid init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
{ int l;
g->region_size[0] = ff_band_index_long[s->sample_rate_index][ra1 + 1]; /* should not overflow */
l = FFMIN(ra1 + ra2 + 2, 22);
g->region_size[1] = ff_band_index_long[s->sample_rate_index][ l];
}
staticvoid compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
{ if (g->block_type == 2) { if (g->switch_point) { if(s->sample_rate_index == 8)
avpriv_request_sample(s->avctx, "switch point in 8khz"); /* if switched mode, we handle the 36 first samples as long blocks. For 8000Hz, we handle the 72 first
exponents as long blocks */ if (s->sample_rate_index <= 2)
g->long_end = 8; else
g->long_end = 6;
/* layer 1 unscaling */ /* n = number of bits of the mantissa minus 1 */ staticinlineint l1_unscale(int n, int mant, int scale_factor)
{ int shift, mod;
int64_t val;
shift = ff_scale_factor_modshift[scale_factor];
mod = shift & 3;
shift >>= 2;
val = MUL64((int)(mant + (-1U << 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);
}
staticinlineint l2_unscale_group(int steps, int mant, int scale_factor)
{ int shift, mod, val;
if (crc_val) {
av_log(s->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc_val); if (s->err_recognition & AV_EF_EXPLODE) return AVERROR_INVALIDDATA;
}
} return 0;
}
/* return the number of decoded frames */ staticint mp_decode_layer1(MPADecodeContext *s)
{ int bound, i, v, n, ch, j, mant;
uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT]; int ret;
ret = handle_crc(s, (s->nb_channels == 1) ? 8*16 : 8*32); if (ret < 0) return ret;
/* allocation bits */ for (i = 0; i < bound; i++) { for (ch = 0; ch < s->nb_channels; ch++) {
allocation[ch][i] = get_bits(&s->gb, 4);
}
} for (i = bound; i < SBLIMIT; i++)
allocation[0][i] = get_bits(&s->gb, 4);
/* scale factors */ for (i = 0; i < bound; i++) { for (ch = 0; ch < s->nb_channels; ch++) { if (allocation[ch][i])
scale_factors[ch][i] = get_bits(&s->gb, 6);
}
} for (i = bound; i < SBLIMIT; i++) { if (allocation[0][i]) {
scale_factors[0][i] = get_bits(&s->gb, 6);
scale_factors[1][i] = get_bits(&s->gb, 6);
}
}
/* compute samples */ for (j = 0; j < 12; j++) { for (i = 0; i < bound; i++) { for (ch = 0; ch < s->nb_channels; ch++) {
n = allocation[ch][i]; if (n) {
mant = get_bits(&s->gb, n + 1);
v = l1_unscale(n, mant, scale_factors[ch][i]);
} else {
v = 0;
}
s->sb_samples[ch][j][i] = v;
}
} for (i = bound; i < SBLIMIT; i++) {
n = allocation[0][i]; if (n) {
mant = get_bits(&s->gb, n + 1);
v = l1_unscale(n, mant, scale_factors[0][i]);
s->sb_samples[0][j][i] = v;
v = l1_unscale(n, mant, scale_factors[1][i]);
s->sb_samples[1][j][i] = v;
} else {
s->sb_samples[0][j][i] = 0;
s->sb_samples[1][j][i] = 0;
}
}
} return 12;
}
staticint mp_decode_layer2(MPADecodeContext *s)
{ int sblimit; /* number of used subbands */ constunsignedchar *alloc_table; int table, bit_alloc_bits, i, j, ch, bound, v; unsignedchar bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; unsignedchar scale_code[MPA_MAX_CHANNELS][SBLIMIT]; unsignedchar scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf; int scale, qindex, bits, steps, k, l, m, b; int ret;
/* parse bit allocation */
j = 0; for (i = 0; i < bound; i++) {
bit_alloc_bits = alloc_table[j]; for (ch = 0; ch < s->nb_channels; ch++)
bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
j += 1 << bit_alloc_bits;
} for (i = bound; i < sblimit; i++) {
bit_alloc_bits = alloc_table[j];
v = get_bits(&s->gb, bit_alloc_bits);
bit_alloc[0][i] = v;
bit_alloc[1][i] = v;
j += 1 << bit_alloc_bits;
}
/* scale codes */ for (i = 0; i < sblimit; i++) { for (ch = 0; ch < s->nb_channels; ch++) { if (bit_alloc[ch][i])
scale_code[ch][i] = get_bits(&s->gb, 2);
}
}
ret = handle_crc(s, get_bits_count(&s->gb) - 16); if (ret < 0) return ret;
/* scale factors */ for (i = 0; i < sblimit; i++) { for (ch = 0; ch < s->nb_channels; ch++) { if (bit_alloc[ch][i]) {
sf = scale_factors[ch][i]; switch (scale_code[ch][i]) { default: case 0:
sf[0] = get_bits(&s->gb, 6);
sf[1] = get_bits(&s->gb, 6);
sf[2] = get_bits(&s->gb, 6); break; case 2:
sf[0] = get_bits(&s->gb, 6);
sf[1] = sf[0];
sf[2] = sf[0]; break; case 1:
sf[0] = get_bits(&s->gb, 6);
sf[2] = get_bits(&s->gb, 6);
sf[1] = sf[0]; break; case 3:
sf[0] = get_bits(&s->gb, 6);
sf[2] = get_bits(&s->gb, 6);
sf[1] = sf[2]; break;
}
}
}
}
/* samples */ for (k = 0; k < 3; k++) { for (l = 0; l < 12; l += 3) {
j = 0; for (i = 0; i < bound; i++) {
bit_alloc_bits = alloc_table[j]; for (ch = 0; ch < s->nb_channels; ch++) {
b = bit_alloc[ch][i]; if (b) {
scale = scale_factors[ch][i][k];
qindex = alloc_table[j+b];
bits = ff_mpa_quant_bits[qindex]; if (bits < 0) { int v2; /* 3 values at the same time */
v = get_bits(&s->gb, -bits);
v2 = ff_division_tabs[qindex][v];
steps = ff_mpa_quant_steps[qindex];
s->sb_samples[ch][k * 12 + l + 0][i] =
l2_unscale_group(steps, v2 & 15, scale);
s->sb_samples[ch][k * 12 + l + 1][i] =
l2_unscale_group(steps, (v2 >> 4) & 15, scale);
s->sb_samples[ch][k * 12 + l + 2][i] =
l2_unscale_group(steps, v2 >> 8 , scale);
} else { for (m = 0; m < 3; m++) {
v = get_bits(&s->gb, bits);
v = l1_unscale(bits - 1, v, scale);
s->sb_samples[ch][k * 12 + l + m][i] = v;
}
}
} else {
s->sb_samples[ch][k * 12 + l + 0][i] = 0;
s->sb_samples[ch][k * 12 + l + 1][i] = 0;
s->sb_samples[ch][k * 12 + l + 2][i] = 0;
}
} /* next subband in alloc table */
j += 1 << bit_alloc_bits;
} /* XXX: find a way to avoid this duplication of code */ for (i = bound; i < sblimit; i++) {
bit_alloc_bits = alloc_table[j];
b = bit_alloc[0][i]; if (b) { int mant, scale0, scale1;
scale0 = scale_factors[0][i][k];
scale1 = scale_factors[1][i][k];
qindex = alloc_table[j + b];
bits = ff_mpa_quant_bits[qindex]; if (bits < 0) { /* 3 values at the same time */
v = get_bits(&s->gb, -bits);
steps = ff_mpa_quant_steps[qindex];
mant = v % steps;
v = v / steps;
s->sb_samples[0][k * 12 + l + 0][i] =
l2_unscale_group(steps, mant, scale0);
s->sb_samples[1][k * 12 + l + 0][i] =
l2_unscale_group(steps, mant, scale1);
mant = v % steps;
v = v / steps;
s->sb_samples[0][k * 12 + l + 1][i] =
l2_unscale_group(steps, mant, scale0);
s->sb_samples[1][k * 12 + l + 1][i] =
l2_unscale_group(steps, mant, scale1);
s->sb_samples[0][k * 12 + l + 2][i] =
l2_unscale_group(steps, v, scale0);
s->sb_samples[1][k * 12 + l + 2][i] =
l2_unscale_group(steps, v, scale1);
} else { for (m = 0; m < 3; m++) {
mant = get_bits(&s->gb, bits);
s->sb_samples[0][k * 12 + l + m][i] =
l1_unscale(bits - 1, mant, scale0);
s->sb_samples[1][k * 12 + l + m][i] =
l1_unscale(bits - 1, mant, scale1);
}
}
} else {
s->sb_samples[0][k * 12 + l + 0][i] = 0;
s->sb_samples[0][k * 12 + l + 1][i] = 0;
s->sb_samples[0][k * 12 + l + 2][i] = 0;
s->sb_samples[1][k * 12 + l + 0][i] = 0;
s->sb_samples[1][k * 12 + l + 1][i] = 0;
s->sb_samples[1][k * 12 + l + 2][i] = 0;
} /* next subband in alloc table */
j += 1 << bit_alloc_bits;
} /* fill remaining samples to zero */ for (i = sblimit; i < SBLIMIT; i++) { for (ch = 0; ch < s->nb_channels; ch++) {
s->sb_samples[ch][k * 12 + l + 0][i] = 0;
s->sb_samples[ch][k * 12 + l + 1][i] = 0;
s->sb_samples[ch][k * 12 + l + 2][i] = 0;
}
}
}
} return 3 * 12;
}
#define SPLIT(dst,sf,n) \ if (n == 3) { \ int m = (sf * 171) >> 9; \
dst = sf - 3 * m; \
sf = m; \
} elseif (n == 4) { \
dst = sf & 3; \
sf >>= 2; \
} elseif (n == 5) { \ int m = (sf * 205) >> 10; \
dst = sf - 5 * m; \
sf = m; \
} elseif (n == 6) { \ int m = (sf * 171) >> 10; \
dst = sf - 6 * m; \
sf = m; \
} else { \
dst = 0; \
}
static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2, int n3)
{
SPLIT(slen[3], sf, n3)
SPLIT(slen[2], sf, n2)
SPLIT(slen[1], sf, n1)
slen[0] = sf;
}
staticvoid exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
int16_t *exponents)
{ const uint8_t *bstab, *pretab; int len, i, j, k, l, v0, shift, gain, gains[3];
int16_t *exp_ptr;
i = get_bits_count(&s->gb);
switch_buffer(s, &i, &end_pos, &end_pos2);
return 0;
}
/* Reorder short blocks from bitstream order to interleaved order. It would be faster to do it in parsing, but the code would be far more
complicated */ staticvoid reorder_block(MPADecodeContext *s, GranuleDef *g)
{ int i, j, len;
INTFLOAT *ptr, *dst, *ptr1;
INTFLOAT tmp[576];
non_zero_found_short[0] = 0;
non_zero_found_short[1] = 0;
non_zero_found_short[2] = 0;
k = (13 - g1->short_start) * 3 + g1->long_end - 3; for (i = 12; i >= g1->short_start; i--) { /* for last band, use previous scale factor */ if (i != 11)
k -= 3;
len = ff_band_size_short[s->sample_rate_index][i]; for (l = 2; l >= 0; l--) {
tab0 -= len;
tab1 -= len; if (!non_zero_found_short[l]) { /* test if non zero band. if so, stop doing i-stereo */ for (j = 0; j < len; j++) { if (tab1[j] != 0) {
non_zero_found_short[l] = 1; goto found1;
}
}
sf = g1->scale_factors[k + l]; if (sf >= sf_max) goto found1;
v1 = is_tab[0][sf];
v2 = is_tab[1][sf]; for (j = 0; j < len; j++) {
tmp0 = tab0[j];
tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
}
} else {
found1: if (s->mode_ext & MODE_EXT_MS_STEREO) { /* lower part of the spectrum : do ms stereo
if enabled */ for (j = 0; j < len; j++) {
tmp0 = tab0[j];
tmp1 = tab1[j];
tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
}
}
}
}
}
staticvoid compute_antialias(MPADecodeContext *s, GranuleDef *g)
{
INTFLOAT *ptr; int n, i;
/* we antialias only "long" bands */ if (g->block_type == 2) { if (!g->switch_point) return; /* XXX: check this for 8000Hz case */
n = 1;
} else {
n = SBLIMIT - 1;
}
ptr = g->sb_hybrid + 18; for (i = n; i > 0; i--) {
AA(0);
AA(1);
AA(2);
AA(3);
AA(4);
AA(5);
AA(6);
AA(7);
/* main layer3 decoding function */ staticint mp_decode_layer3(MPADecodeContext *s)
{ int nb_granules, main_data_begin; int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
GranuleDef *g;
int16_t exponents[576]; //FIXME try INTFLOAT int ret;
/* read side info */ if (s->lsf) {
ret = handle_crc(s, ((s->nb_channels == 1) ? 8*9 : 8*17));
main_data_begin = get_bits(&s->gb, 8);
skip_bits(&s->gb, s->nb_channels);
nb_granules = 1;
} else {
ret = handle_crc(s, ((s->nb_channels == 1) ? 8*17 : 8*32));
main_data_begin = get_bits(&s->gb, 9); if (s->nb_channels == 2)
skip_bits(&s->gb, 3); else
skip_bits(&s->gb, 5);
nb_granules = 2; for (ch = 0; ch < s->nb_channels; ch++) {
s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
}
} if (ret < 0) return ret;
for (gr = 0; gr < nb_granules; gr++) { for (ch = 0; ch < s->nb_channels; ch++) {
ff_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
g = &s->granules[ch][gr];
g->part2_3_length = get_bits(&s->gb, 12);
g->big_values = get_bits(&s->gb, 9); if (g->big_values > 288) {
av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n"); return AVERROR_INVALIDDATA;
}
g->global_gain = get_bits(&s->gb, 8); /* if MS stereo only is selected, we precompute the
1/sqrt(2) renormalization factor */ if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
MODE_EXT_MS_STEREO)
g->global_gain -= 2; if (s->lsf)
g->scalefac_compress = get_bits(&s->gb, 9); else
g->scalefac_compress = get_bits(&s->gb, 4);
blocksplit_flag = get_bits1(&s->gb); if (blocksplit_flag) {
g->block_type = get_bits(&s->gb, 2); if (g->block_type == 0) {
av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n"); return AVERROR_INVALIDDATA;
}
g->switch_point = get_bits1(&s->gb); for (i = 0; i < 2; i++)
g->table_select[i] = get_bits(&s->gb, 5); for (i = 0; i < 3; i++)
g->subblock_gain[i] = get_bits(&s->gb, 3);
init_short_region(s, g);
} else { int region_address1, region_address2;
g->block_type = 0;
g->switch_point = 0; for (i = 0; i < 3; i++)
g->table_select[i] = get_bits(&s->gb, 5); /* compute huffman coded region sizes */
region_address1 = get_bits(&s->gb, 4);
region_address2 = get_bits(&s->gb, 3);
ff_dlog(s->avctx, "region1=%d region2=%d\n",
region_address1, region_address2);
init_long_region(s, g, region_address1, region_address2);
}
region_offset2size(g);
compute_band_indexes(s, g);
ret = mp_decode_frame(s, NULL, buf, buf_size); if (ret >= 0) {
s->frame->nb_samples = avctx->frame_size;
*got_frame_ptr = 1;
avctx->sample_rate = s->sample_rate; //FIXME maybe move the other codec info stuff from above here too
} else {
av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n"); /* Only return an error if the bad frame makes up the whole packet or * the error is related to buffer management. * If there is more data in the packet, just consume the bad frame * instead of returning an error, which would discard the whole
* packet. */
*got_frame_ptr = 0; if (buf_size == avpkt->size || ret != AVERROR_INVALIDDATA) return ret;
}
s->frame_size = 0; return buf_size + skipped;
}
/** * Context for MP3On4 decoder
*/ typedefstruct MP3On4DecodeContext { int frames; ///< number of mp3 frames per block (number of mp3 decoder instances) int syncword; ///< syncword patch const uint8_t *coff; ///< channel offsets in output buffer
MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
} MP3On4DecodeContext;
#include"mpeg4audio.h"
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
/* Init the first mp3 decoder in standard way, so that all tables get builded * We replace avctx->priv_data with the context of the first decoder so that * decode_init() does not have to be changed. * Other decoders will be initialized here copying data from the first context
*/ // Allocate zeroed memory for the first decoder context
s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext)); if (!s->mp3decctx[0]) return AVERROR(ENOMEM); // Put decoder context in place to make init_decode() happy
avctx->priv_data = s->mp3decctx[0];
ret = decode_init(avctx); // Restore mp3on4 context pointer
avctx->priv_data = s; if (ret < 0) return ret;
s->mp3decctx[0]->adu_mode = 1; // Set adu mode
/* Create a separate codec/context for each frame (first is already ok). * Each frame is 1 or 2 channels - up to 5 frames allowed
*/ for (i = 1; i < s->frames; i++) {
s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext)); if (!s->mp3decctx[i]) return AVERROR(ENOMEM);
s->mp3decctx[i]->adu_mode = 1;
s->mp3decctx[i]->avctx = avctx;
s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
s->mp3decctx[i]->butterflies_float = s->mp3decctx[0]->butterflies_float;
}
return 0;
}
staticvoid flush_mp3on4(AVCodecContext *avctx)
{ int i;
MP3On4DecodeContext *s = avctx->priv_data;
for (i = 0; i < s->frames; i++)
mp_flush(s->mp3decctx[i]);
}
staticint decode_frame_mp3on4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
{ const uint8_t *buf = avpkt->data; int buf_size = avpkt->size;
MP3On4DecodeContext *s = avctx->priv_data;
MPADecodeContext *m; int fsize, len = buf_size, out_size = 0;
uint32_t header;
OUT_INT **out_samples;
OUT_INT *outptr[2]; int fr, ch, ret;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.