#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;
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)
g->table_select[i] = get_bits(&s->gb, 5);
for(i=0;i<3;i++)
g->subblock_gain[i] = get_bits(&s->gb, 3);
- /* compute huffman coded region sizes */
- 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);
+ ff_init_short_region(s, g);
} else {
- int region_address1, region_address2, l;
+ int region_address1, region_address2;
g->block_type = 0;
g->switch_point = 0;
for(i=0;i<3;i++)
region_address2 = get_bits(&s->gb, 3);
dprintf(s->avctx, "region1=%d region2=%d\n",
region_address1, region_address2);
- g->region_size[0] =
- band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
- l = region_address1 + region_address2 + 2;
- /* should not overflow */
- if (l > 22)
- l = 22;
- g->region_size[1] =
- band_index_long[s->sample_rate_index][l] >> 1;
- }
- /* convert region offsets to region sizes and truncate
- size to big_values */
- g->region_size[2] = (576 / 2);
- j = 0;
- for(i=0;i<3;i++) {
- k = FFMIN(g->region_size[i], g->big_values);
- g->region_size[i] = k - j;
- j = k;
- }
-
- /* compute band indexes */
- 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;
+ ff_init_long_region(s, g, region_address1, region_address2);
}
+ ff_region_offset2size(g);
+ ff_compute_band_indexes(s, g);
g->preflag = 0;
if (!s->lsf)
dprintf(s->avctx, "frame %d:\n", s->frame_count);
switch(s->layer) {
case 1:
+ s->avctx->frame_size = 384;
nb_frames = mp_decode_layer1(s);
break;
case 2:
+ s->avctx->frame_size = 1152;
nb_frames = mp_decode_layer2(s);
break;
case 3:
+ s->avctx->frame_size = s->lsf ? 576 : 1152;
default:
nb_frames = mp_decode_layer3(s);
static int decode_frame(AVCodecContext * avctx,
void *data, int *data_size,
- uint8_t * buf, int buf_size)
+ const uint8_t * buf, int buf_size)
{
MPADecodeContext *s = avctx->priv_data;
uint32_t header;
avctx->channels = s->nb_channels;
avctx->bit_rate = s->bit_rate;
avctx->sub_id = s->layer;
- switch(s->layer) {
- case 1:
- avctx->frame_size = 384;
- break;
- case 2:
- avctx->frame_size = 1152;
- break;
- case 3:
- if (s->lsf)
- avctx->frame_size = 576;
- else
- avctx->frame_size = 1152;
- break;
- }
if(s->frame_size<=0 || s->frame_size > buf_size){
av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
#ifdef CONFIG_MP3ADU_DECODER
static int decode_frame_adu(AVCodecContext * avctx,
void *data, int *data_size,
- uint8_t * buf, int buf_size)
+ const uint8_t * buf, int buf_size)
{
MPADecodeContext *s = avctx->priv_data;
uint32_t header;
avctx->bit_rate = s->bit_rate;
avctx->sub_id = s->layer;
- avctx->frame_size=s->frame_size = len;
+ s->frame_size = len;
if (avctx->parse_only) {
out_size = buf_size;
#endif /* CONFIG_MP3ADU_DECODER */
#ifdef CONFIG_MP3ON4_DECODER
+
+/**
+ * Context for MP3On4 decoder
+ */
+typedef struct MP3On4DecodeContext {
+ int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
+ int syncword; ///< syncword patch
+ const uint8_t *coff; ///< channels 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) */
-static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
-static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
+static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
-static int chan_offset[9][5] = {
+static const uint8_t chan_offset[8][5] = {
{0},
{0}, // C
{0}, // FLR
{4,0,2}, // C FLR BLRS
{4,0,2,5}, // C FLR BLRS LFE
{4,0,2,6,5}, // C FLR BLRS BLR LFE
- {0,2} // FLR BLRS
};
static int decode_init_mp3on4(AVCodecContext * avctx)
{
MP3On4DecodeContext *s = avctx->priv_data;
+ MPEG4AudioConfig cfg;
int i;
if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
return -1;
}
- s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
- s->frames = mp3Frames[s->chan_cfg];
- if(!s->frames) {
+ ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
+ if (!cfg.chan_config || cfg.chan_config > 7) {
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
return -1;
}
- avctx->channels = mp3Channels[s->chan_cfg];
+ s->frames = mp3Frames[cfg.chan_config];
+ s->coff = chan_offset[cfg.chan_config];
+ avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
+
+ if (cfg.sample_rate < 16000)
+ s->syncword = 0xffe00000;
+ else
+ s->syncword = 0xfff00000;
/* 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 inited here copying data from the first context
+ * 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));
static int decode_frame_mp3on4(AVCodecContext * avctx,
void *data, int *data_size,
- uint8_t * buf, int buf_size)
+ const uint8_t * buf, int buf_size)
{
MP3On4DecodeContext *s = avctx->priv_data;
MPADecodeContext *m;
- int len, out_size = 0;
+ int fsize, len = buf_size, out_size = 0;
uint32_t header;
OUT_INT *out_samples = data;
OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
OUT_INT *outptr, *bp;
- int fsize;
- unsigned char *start2 = buf, *start;
- int fr, i, j, n;
- int off = avctx->channels;
- int *coff = chan_offset[s->chan_cfg];
-
- len = buf_size;
+ int fr, j, n;
+ *data_size = 0;
// Discard too short frames
- if (buf_size < HEADER_SIZE) {
- *data_size = 0;
- return buf_size;
- }
+ if (buf_size < HEADER_SIZE)
+ return -1;
// If only one decoder interleave is not needed
outptr = s->frames == 1 ? out_samples : decoded_buf;
+ avctx->bit_rate = 0;
+
for (fr = 0; fr < s->frames; fr++) {
- start = start2;
- fsize = (start[0] << 4) | (start[1] >> 4);
- start2 += fsize;
- if (fsize > len)
- fsize = len;
- len -= fsize;
- if (fsize > MPA_MAX_CODED_FRAME_SIZE)
- fsize = MPA_MAX_CODED_FRAME_SIZE;
+ fsize = AV_RB16(buf) >> 4;
+ fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
m = s->mp3decctx[fr];
assert (m != NULL);
- // Get header
- header = AV_RB32(start) | 0xfff00000;
+ header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
- if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
- *data_size = 0;
- return buf_size;
- }
+ if (ff_mpa_check_header(header) < 0) // Bad header, discard block
+ break;
ff_mpegaudio_decode_header(m, header);
- mp_decode_frame(m, decoded_buf, start, fsize);
+ out_size += mp_decode_frame(m, outptr, buf, fsize);
+ buf += fsize;
+ len -= fsize;
- n = MPA_FRAME_SIZE * m->nb_channels;
- out_size += n * sizeof(OUT_INT);
if(s->frames > 1) {
+ n = m->avctx->frame_size*m->nb_channels;
/* interleave output data */
- bp = out_samples + coff[fr];
+ bp = out_samples + s->coff[fr];
if(m->nb_channels == 1) {
for(j = 0; j < n; j++) {
*bp = decoded_buf[j];
- bp += off;
+ bp += avctx->channels;
}
} else {
for(j = 0; j < n; j++) {
bp[0] = decoded_buf[j++];
bp[1] = decoded_buf[j];
- bp += off;
+ bp += avctx->channels;
}
}
}
+ avctx->bit_rate += m->bit_rate;
}
/* update codec info */
avctx->sample_rate = s->mp3decctx[0]->sample_rate;
- avctx->frame_size= buf_size;
- avctx->bit_rate = 0;
- for (i = 0; i < s->frames; i++)
- avctx->bit_rate += s->mp3decctx[i]->bit_rate;
*data_size = out_size;
return buf_size;
decode_frame,
CODEC_CAP_PARSE_ONLY,
.flush= flush,
+ .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
};
#endif
#ifdef CONFIG_MP3_DECODER
decode_frame,
CODEC_CAP_PARSE_ONLY,
.flush= flush,
+ .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
};
#endif
#ifdef CONFIG_MP3ADU_DECODER
decode_frame_adu,
CODEC_CAP_PARSE_ONLY,
.flush= flush,
+ .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
};
#endif
#ifdef CONFIG_MP3ON4_DECODER
decode_close_mp3on4,
decode_frame_mp3on4,
.flush= flush,
+ .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
};
#endif