#include "libavutil/attributes.h"
#include "libavutil/avassert.h"
#include "libavutil/channel_layout.h"
+#include "libavutil/crc.h"
#include "libavutil/float_dsp.h"
#include "libavutil/libm.h"
#include "avcodec.h"
int err_recognition;
AVCodecContext* avctx;
MPADSPContext mpadsp;
- AVFloatDSPContext *fdsp;
+ void (*butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len);
AVFrame *frame;
+ uint32_t crc;
} MPADecodeContext;
#define HEADER_SIZE 4
static INTFLOAT is_table_lsf[2][2][16];
static INTFLOAT csa_table[8][4];
-static int16_t division_tab3[1<<6 ];
-static int16_t division_tab5[1<<8 ];
-static int16_t division_tab9[1<<11];
+static int16_t division_tab3[1 << 6 ];
+static int16_t division_tab5[1 << 8 ];
+static int16_t division_tab9[1 << 11];
static int16_t * const division_tabs[4] = {
division_tab3, division_tab5, NULL, division_tab9
#endif
if (e > (SUINT)31)
return 0;
- m = (m + ((1U << e)>>1)) >> e;
+ m = (m + ((1U << e) >> 1)) >> e;
return m;
}
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++];
+ tmp_bits [(x << 5) | y | ((x && y) << 4)]= h->bits [j ];
+ tmp_codes[(x << 5) | y | ((x && y) << 4)]= h->codes[j++];
}
}
for (i = 0; i < 4; i++) {
if (ff_mpa_quant_bits[i] < 0) {
- for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
+ for (j = 0; j < (1 << (-ff_mpa_quant_bits[i] + 1)); j++) {
int val1, val2, val3, steps;
int val = j;
steps = ff_mpa_quant_steps[i];
}
}
-#if USE_FLOATS
-static av_cold int decode_close(AVCodecContext * avctx)
-{
- MPADecodeContext *s = avctx->priv_data;
- av_freep(&s->fdsp);
-
- return 0;
-}
-#endif
-
static av_cold int decode_init(AVCodecContext * avctx)
{
static int initialized_tables = 0;
s->avctx = avctx;
#if USE_FLOATS
- s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
- if (!s->fdsp)
- return AVERROR(ENOMEM);
+ {
+ AVFloatDSPContext *fdsp;
+ fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
+ if (!fdsp)
+ return AVERROR(ENOMEM);
+ s->butterflies_float = fdsp->butterflies_float;
+ av_free(fdsp);
+ }
#endif
ff_mpadsp_init(&s->mpadsp);
out[11] = in0 + in5;
}
+static int handle_crc(MPADecodeContext *s, int sec_len)
+{
+ if (s->error_protection && (s->err_recognition & AV_EF_CRCCHECK)) {
+ const uint8_t *buf = s->gb.buffer - HEADER_SIZE;
+ int sec_byte_len = sec_len >> 3;
+ int sec_rem_bits = sec_len & 7;
+ const AVCRC *crc_tab = av_crc_get_table(AV_CRC_16_ANSI);
+ uint8_t tmp_buf[4];
+ uint32_t crc_val = av_crc(crc_tab, UINT16_MAX, &buf[2], 2);
+ crc_val = av_crc(crc_tab, crc_val, &buf[6], sec_byte_len);
+
+ AV_WB32(tmp_buf,
+ ((buf[6 + sec_byte_len] & (0xFF00 >> sec_rem_bits)) << 24) +
+ ((s->crc << 16) >> sec_rem_bits));
+
+ crc_val = av_crc(crc_tab, crc_val, tmp_buf, 3);
+
+ 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 */
static int 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;
if (s->mode == MPA_JSTEREO)
bound = (s->mode_ext + 1) * 4;
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
int scale, qindex, bits, steps, k, l, m, b;
+ int ret;
/* select decoding table */
table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
}
}
+ 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++) {
int mant, scale0, scale1;
scale0 = scale_factors[0][i][k];
scale1 = scale_factors[1][i][k];
- qindex = alloc_table[j+b];
+ qindex = alloc_table[j + b];
bits = ff_mpa_quant_bits[qindex];
if (bits < 0) {
/* 3 values at the same time */
y = get_vlc2(&s->gb, vlc->table, 7, 3);
if (!y) {
- g->sb_hybrid[s_index ] =
- g->sb_hybrid[s_index+1] = 0;
+ g->sb_hybrid[s_index ] =
+ g->sb_hybrid[s_index + 1] = 0;
s_index += 2;
continue;
}
v = l3_unscale(y, exponent);
if (get_bits1(&s->gb))
v = -v;
- g->sb_hybrid[s_index+1] = v;
+ g->sb_hybrid[s_index + 1] = v;
}
} else {
x = y >> 5;
code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
ff_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
- g->sb_hybrid[s_index+0] =
- g->sb_hybrid[s_index+1] =
- g->sb_hybrid[s_index+2] =
- g->sb_hybrid[s_index+3] = 0;
+ g->sb_hybrid[s_index + 0] =
+ g->sb_hybrid[s_index + 1] =
+ g->sb_hybrid[s_index + 2] =
+ g->sb_hybrid[s_index + 3] = 0;
while (code) {
static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
int v;
/* NOTE: the 1/sqrt(2) normalization factor is included in the
global gain */
#if USE_FLOATS
- s->fdsp->butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
+ s->butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
#else
tab0 = g0->sb_hybrid;
tab1 = g1->sb_hybrid;
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);
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++) {
if (!s->adu_mode) {
int skip;
- const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
+ const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb) >> 3);
s->extrasize = av_clip((get_bits_left(&s->gb) >> 3) - s->extrasize, 0,
FFMAX(0, LAST_BUF_SIZE - s->last_buf_size));
av_assert1((get_bits_count(&s->gb) & 7) == 0);
OUT_INT *samples_ptr;
init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
-
- /* skip error protection field */
if (s->error_protection)
- skip_bits(&s->gb, 16);
+ s->crc = get_bits(&s->gb, 16);
switch(s->layer) {
case 1:
align_get_bits(&s->gb);
i = (get_bits_left(&s->gb) >> 3) - s->extrasize;
if (i >= 0 && i <= BACKSTEP_SIZE) {
- memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
+ memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb) >> 3), i);
s->last_buf_size=i;
} else
av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
return AVERROR_INVALIDDATA;
header = AV_RB32(buf);
- if (header>>8 == AV_RB32("TAG")>>8) {
+ if (header >> 8 == AV_RB32("TAG") >> 8) {
av_log(avctx, AV_LOG_DEBUG, "discarding ID3 tag\n");
return buf_size + skipped;
}
MP3On4DecodeContext *s = avctx->priv_data;
int i;
- if (s->mp3decctx[0])
- av_freep(&s->mp3decctx[0]->fdsp);
-
for (i = 0; i < s->frames; i++)
av_freep(&s->mp3decctx[i]);
{
MP3On4DecodeContext *s = avctx->priv_data;
MPEG4AudioConfig cfg;
- int i;
+ int i, ret;
if ((avctx->extradata_size < 2) || !avctx->extradata) {
av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
return AVERROR_INVALIDDATA;
}
- avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
- avctx->extradata_size * 8, 1);
+ avpriv_mpeg4audio_get_config2(&cfg, avctx->extradata,
+ avctx->extradata_size, 1, avctx);
if (!cfg.chan_config || cfg.chan_config > 7) {
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
return AVERROR_INVALIDDATA;
// Allocate zeroed memory for the first decoder context
s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
if (!s->mp3decctx[0])
- goto alloc_fail;
+ return AVERROR(ENOMEM);
// Put decoder context in place to make init_decode() happy
avctx->priv_data = s->mp3decctx[0];
- decode_init(avctx);
+ 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).
for (i = 1; i < s->frames; i++) {
s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
if (!s->mp3decctx[i])
- goto alloc_fail;
+ 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]->fdsp = s->mp3decctx[0]->fdsp;
+ s->mp3decctx[i]->butterflies_float = s->mp3decctx[0]->butterflies_float;
}
return 0;
-alloc_fail:
- decode_close_mp3on4(avctx);
- return AVERROR(ENOMEM);
}