Newer
Older
Fabrice Bellard
committed
if (blocksplit_flag) {
g->block_type = get_bits(&s->gb, 2);
av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
Fabrice Bellard
committed
return -1;
g->switch_point = get_bits1(&s->gb);
Fabrice Bellard
committed
for(i=0;i<2;i++)
g->table_select[i] = get_bits(&s->gb, 5);
Fabrice Bellard
committed
g->subblock_gain[i] = get_bits(&s->gb, 3);
ff_init_short_region(s, g);
Fabrice Bellard
committed
} else {
int region_address1, region_address2;
Fabrice Bellard
committed
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);
Fabrice Bellard
committed
region_address1, region_address2);
ff_init_long_region(s, g, region_address1, region_address2);
Fabrice Bellard
committed
}
ff_region_offset2size(g);
ff_compute_band_indexes(s, g);
Fabrice Bellard
committed
g->preflag = 0;
if (!s->lsf)
g->preflag = get_bits1(&s->gb);
g->scalefac_scale = get_bits1(&s->gb);
g->count1table_select = get_bits1(&s->gb);
dprintf(s->avctx, "block_type=%d switch_point=%d\n",
Fabrice Bellard
committed
g->block_type, g->switch_point);
}
}
Michael Niedermayer
committed
const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
Fabrice Bellard
committed
/* now we get bits from the main_data_begin offset */
dprintf(s->avctx, "seekback: %d\n", main_data_begin);
Michael Niedermayer
committed
//av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
s->in_gb= s->gb;
init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
Fabrice Bellard
committed
for(gr=0;gr<nb_granules;gr++) {
for(ch=0;ch<s->nb_channels;ch++) {
av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
main_data_begin, s->last_buf_size, gr);
skip_bits_long(&s->gb, g->part2_3_length);
memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
s->gb= s->in_gb;
s->in_gb.buffer=NULL;
}
continue;
}
Fabrice Bellard
committed
bits_pos = get_bits_count(&s->gb);
Fabrice Bellard
committed
if (!s->lsf) {
Fabrice Bellard
committed
int slen, slen1, slen2;
/* MPEG1 scale factors */
slen1 = slen_table[0][g->scalefac_compress];
slen2 = slen_table[1][g->scalefac_compress];
dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
Fabrice Bellard
committed
if (g->block_type == 2) {
n = g->switch_point ? 17 : 18;
j = 0;
if(slen1){
for(i=0;i<n;i++)
g->scale_factors[j++] = get_bits(&s->gb, slen1);
}else{
for(i=0;i<n;i++)
g->scale_factors[j++] = 0;
}
if(slen2){
for(i=0;i<18;i++)
g->scale_factors[j++] = get_bits(&s->gb, slen2);
for(i=0;i<3;i++)
g->scale_factors[j++] = 0;
}else{
for(i=0;i<21;i++)
g->scale_factors[j++] = 0;
}
Fabrice Bellard
committed
} else {
sc = s->granules[ch][0].scale_factors;
Fabrice Bellard
committed
j = 0;
for(k=0;k<4;k++) {
n = (k == 0 ? 6 : 5);
if ((g->scfsi & (0x8 >> k)) == 0) {
slen = (k < 2) ? slen1 : slen2;
if(slen){
for(i=0;i<n;i++)
g->scale_factors[j++] = get_bits(&s->gb, slen);
}else{
for(i=0;i<n;i++)
g->scale_factors[j++] = 0;
}
Fabrice Bellard
committed
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
} else {
/* simply copy from last granule */
for(i=0;i<n;i++) {
g->scale_factors[j] = sc[j];
j++;
}
}
}
g->scale_factors[j++] = 0;
}
} else {
int tindex, tindex2, slen[4], sl, sf;
/* LSF scale factors */
if (g->block_type == 2) {
tindex = g->switch_point ? 2 : 1;
} else {
tindex = 0;
}
sf = g->scalefac_compress;
if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
/* intensity stereo case */
sf >>= 1;
if (sf < 180) {
lsf_sf_expand(slen, sf, 6, 6, 0);
tindex2 = 3;
} else if (sf < 244) {
lsf_sf_expand(slen, sf - 180, 4, 4, 0);
tindex2 = 4;
} else {
lsf_sf_expand(slen, sf - 244, 3, 0, 0);
tindex2 = 5;
}
} else {
/* normal case */
if (sf < 400) {
lsf_sf_expand(slen, sf, 5, 4, 4);
tindex2 = 0;
} else if (sf < 500) {
lsf_sf_expand(slen, sf - 400, 5, 4, 0);
tindex2 = 1;
} else {
lsf_sf_expand(slen, sf - 500, 3, 0, 0);
tindex2 = 2;
g->preflag = 1;
}
}
j = 0;
for(k=0;k<4;k++) {
n = lsf_nsf_table[tindex2][tindex][k];
sl = slen[k];
for(i=0;i<n;i++)
g->scale_factors[j++] = get_bits(&s->gb, sl);
}else{
for(i=0;i<n;i++)
g->scale_factors[j++] = 0;
}
Fabrice Bellard
committed
}
/* XXX: should compute exact size */
for(;j<40;j++)
g->scale_factors[j] = 0;
}
exponents_from_scale_factors(s, g, exponents);
/* read Huffman coded residue */
huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
Fabrice Bellard
committed
} /* ch */
if (s->nb_channels == 2)
compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
Fabrice Bellard
committed
for(ch=0;ch<s->nb_channels;ch++) {
Fabrice Bellard
committed
reorder_block(s, g);
compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
Fabrice Bellard
committed
}
} /* gr */
if(get_bits_count(&s->gb)<0)
skip_bits_long(&s->gb, -get_bits_count(&s->gb));
Fabrice Bellard
committed
return nb_granules * 18;
}
static int mp_decode_frame(MPADecodeContext *s,
Michael Niedermayer
committed
OUT_INT *samples, const uint8_t *buf, int buf_size)
Fabrice Bellard
committed
{
int i, nb_frames, ch;
OUT_INT *samples_ptr;
Fabrice Bellard
committed
Michael Niedermayer
committed
init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
Fabrice Bellard
committed
/* skip error protection field */
if (s->error_protection)
Fabrice Bellard
committed
dprintf(s->avctx, "frame %d:\n", s->frame_count);
Fabrice Bellard
committed
switch(s->layer) {
case 1:
Fabrice Bellard
committed
nb_frames = mp_decode_layer1(s);
break;
case 2:
Fabrice Bellard
committed
nb_frames = mp_decode_layer2(s);
break;
case 3:
Fabrice Bellard
committed
default:
nb_frames = mp_decode_layer3(s);
Michael Niedermayer
committed
s->last_buf_size=0;
if(s->in_gb.buffer){
align_get_bits(&s->gb);
i= get_bits_left(&s->gb)>>3;
memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
s->last_buf_size=i;
av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
Michael Niedermayer
committed
s->gb= s->in_gb;
Michael Niedermayer
committed
align_get_bits(&s->gb);
assert((get_bits_count(&s->gb) & 7) == 0);
i= get_bits_left(&s->gb)>>3;
if(i<0)
av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
}
assert(i <= buf_size - HEADER_SIZE && i>= 0);
memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
Michael Niedermayer
committed
Fabrice Bellard
committed
break;
}
Michael Niedermayer
committed
Fabrice Bellard
committed
/* apply the synthesis filter */
for(ch=0;ch<s->nb_channels;ch++) {
samples_ptr = samples + ch;
for(i=0;i<nb_frames;i++) {
RENAME(ff_mpa_synth_filter)(
#if CONFIG_FLOAT
s,
#endif
s->synth_buf[ch], &(s->synth_buf_offset[ch]),
RENAME(ff_mpa_synth_window), &s->dither_state,
samples_ptr, s->nb_channels,
Fabrice Bellard
committed
s->sb_samples[ch][i]);
samples_ptr += 32 * s->nb_channels;
}
}
Michael Niedermayer
committed
return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
Fabrice Bellard
committed
}
void *data, int *data_size,
Thilo Borgmann
committed
AVPacket *avpkt)
Thilo Borgmann
committed
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
Michael Niedermayer
committed
int out_size;
OUT_INT *out_samples = data;
Michael Niedermayer
committed
if(buf_size < HEADER_SIZE)
return -1;
Michael Niedermayer
committed
if(ff_mpa_check_header(header) < 0){
av_log(avctx, AV_LOG_ERROR, "Header missing\n");
return -1;
Michael Niedermayer
committed
}
if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
Michael Niedermayer
committed
/* free format: prepare to compute frame size */
s->frame_size = -1;
return -1;
}
/* update codec info */
avctx->channels = s->nb_channels;
avctx->bit_rate = s->bit_rate;
avctx->sub_id = s->layer;
if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
return -1;
*data_size = 0;
if(s->frame_size<=0 || s->frame_size > buf_size){
Michael Niedermayer
committed
av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
return -1;
}else if(s->frame_size < buf_size){
av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
Michael Niedermayer
committed
buf_size= s->frame_size;
Michael Niedermayer
committed
out_size = mp_decode_frame(s, out_samples, buf, buf_size);
Michael Niedermayer
committed
if(out_size>=0){
Michael Niedermayer
committed
*data_size = out_size;
Michael Niedermayer
committed
avctx->sample_rate = s->sample_rate;
//FIXME maybe move the other codec info stuff from above here too
}else
av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
Michael Niedermayer
committed
s->frame_size = 0;
return buf_size;
static void flush(AVCodecContext *avctx){
MPADecodeContext *s = avctx->priv_data;
memset(s->synth_buf, 0, sizeof(s->synth_buf));
s->last_buf_size= 0;
}
#if CONFIG_MP3ADU_DECODER
static int decode_frame_adu(AVCodecContext * avctx,
void *data, int *data_size,
Thilo Borgmann
committed
AVPacket *avpkt)
Thilo Borgmann
committed
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
MPADecodeContext *s = avctx->priv_data;
uint32_t header;
int len, out_size;
OUT_INT *out_samples = data;
len = buf_size;
// Discard too short frames
if (buf_size < HEADER_SIZE) {
*data_size = 0;
return buf_size;
}
if (len > MPA_MAX_CODED_FRAME_SIZE)
len = MPA_MAX_CODED_FRAME_SIZE;
// Get header and restore sync word
if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
*data_size = 0;
return buf_size;
}
ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
/* update codec info */
avctx->sample_rate = s->sample_rate;
avctx->channels = s->nb_channels;
avctx->bit_rate = s->bit_rate;
avctx->sub_id = s->layer;
s->frame_size = len;
Michael Niedermayer
committed
out_size = buf_size;
Michael Niedermayer
committed
out_size = mp_decode_frame(s, out_samples, buf, buf_size);
}
*data_size = out_size;
return buf_size;
}
#endif /* CONFIG_MP3ADU_DECODER */
#if 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;
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
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 const uint8_t chan_offset[8][5] = {
{0},
{0}, // C
{0}, // FLR
{2,0}, // C FLR
{2,0,3}, // C FLR BS
{4,0,2}, // C FLR BLRS
{4,0,2,5}, // C FLR BLRS LFE
{4,0,2,6,5}, // C FLR BLRS BLR LFE
};
static int decode_init_mp3on4(AVCodecContext * avctx)
{
MP3On4DecodeContext *s = avctx->priv_data;
int i;
if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
return -1;
}
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;
}
s->frames = mp3Frames[cfg.chan_config];
s->coff = chan_offset[cfg.chan_config];
avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
Baptiste Coudurier
committed
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 initialized here copying data from the first context
*/
// Allocate zeroed memory for the first decoder context
s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
// Put decoder context in place to make init_decode() happy
avctx->priv_data = s->mp3decctx[0];
decode_init(avctx);
// Restore mp3on4 context pointer
avctx->priv_data = s;
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));
s->mp3decctx[i]->adu_mode = 1;
}
return 0;
}
static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
{
MP3On4DecodeContext *s = avctx->priv_data;
int i;
for (i = 0; i < s->frames; i++)
if (s->mp3decctx[i])
av_free(s->mp3decctx[i]);
return 0;
}
static int decode_frame_mp3on4(AVCodecContext * avctx,
void *data, int *data_size,
Thilo Borgmann
committed
AVPacket *avpkt)
{
Thilo Borgmann
committed
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 = data;
OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
OUT_INT *outptr, *bp;
if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
return -1;
// Discard too short frames
if (buf_size < HEADER_SIZE)
return -1;
// If only one decoder interleave is not needed
outptr = s->frames == 1 ? out_samples : decoded_buf;
for (fr = 0; fr < s->frames; fr++) {
fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
m = s->mp3decctx[fr];
assert (m != NULL);
Baptiste Coudurier
committed
header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
if (ff_mpa_check_header(header) < 0) // Bad header, discard block
break;
ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
out_size += mp_decode_frame(m, outptr, buf, fsize);
if(s->frames > 1) {
n = m->avctx->frame_size*m->nb_channels;
/* interleave output data */
bp = out_samples + s->coff[fr];
if(m->nb_channels == 1) {
for(j = 0; j < n; j++) {
*bp = decoded_buf[j];
}
} else {
for(j = 0; j < n; j++) {
bp[0] = decoded_buf[j++];
bp[1] = decoded_buf[j];
}
}
}
}
/* update codec info */
avctx->sample_rate = s->mp3decctx[0]->sample_rate;
*data_size = out_size;
return buf_size;
}
#endif /* CONFIG_MP3ON4_DECODER */
#if CONFIG_MP1_DECODER
Michael Niedermayer
committed
AVCodec mp1_decoder =
{
"mp1",
AVMEDIA_TYPE_AUDIO,
Michael Niedermayer
committed
CODEC_ID_MP1,
sizeof(MPADecodeContext),
decode_init,
NULL,
NULL,
decode_frame,
CODEC_CAP_PARSE_ONLY,
.flush= flush,
.long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
};
#endif
#if CONFIG_MP2_DECODER
AVMEDIA_TYPE_AUDIO,
CODEC_ID_MP2,
sizeof(MPADecodeContext),
decode_init,
NULL,
NULL,
decode_frame,
Fabrice Bellard
committed
CODEC_CAP_PARSE_ONLY,
Brian Brice
committed
.flush= flush,
Stefano Sabatini
committed
.long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
#if CONFIG_MP3_DECODER
AVCodec mp3_decoder =
{
"mp3",
AVMEDIA_TYPE_AUDIO,
sizeof(MPADecodeContext),
decode_init,
NULL,
NULL,
decode_frame,
Fabrice Bellard
committed
CODEC_CAP_PARSE_ONLY,
Stefano Sabatini
committed
.long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
#if CONFIG_MP3ADU_DECODER
AVCodec mp3adu_decoder =
{
"mp3adu",
AVMEDIA_TYPE_AUDIO,
CODEC_ID_MP3ADU,
sizeof(MPADecodeContext),
decode_init,
NULL,
NULL,
decode_frame_adu,
CODEC_CAP_PARSE_ONLY,
Stefano Sabatini
committed
.long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
#if CONFIG_MP3ON4_DECODER
AVCodec mp3on4_decoder =
{
"mp3on4",
AVMEDIA_TYPE_AUDIO,
CODEC_ID_MP3ON4,
sizeof(MP3On4DecodeContext),
decode_init_mp3on4,
NULL,
decode_close_mp3on4,
decode_frame_mp3on4,
Stefano Sabatini
committed
.long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
};