Newer
Older
INT_AA(0)
INT_AA(1)
INT_AA(2)
INT_AA(3)
INT_AA(4)
INT_AA(5)
INT_AA(6)
INT_AA(7)
Michael Niedermayer
committed
}
}
static void compute_antialias_float(MPADecodeContext *s,
GranuleDef *g)
{
Michael Niedermayer
committed
/* 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;
}
Michael Niedermayer
committed
ptr = g->sb_hybrid + 18;
for(i = n;i > 0;i--) {
float *csa = &csa_table_float[0][0];
#define FLOAT_AA(j)\
tmp0= ptr[-1-j];\
tmp1= ptr[ j];\
ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
FLOAT_AA(0)
FLOAT_AA(1)
FLOAT_AA(2)
FLOAT_AA(3)
FLOAT_AA(4)
FLOAT_AA(5)
FLOAT_AA(6)
FLOAT_AA(7)
Fabrice Bellard
committed
}
}
static void compute_imdct(MPADecodeContext *s,
int32_t *sb_samples,
int32_t *mdct_buf)
Fabrice Bellard
committed
{
int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
Fabrice Bellard
committed
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
/* find last non zero block */
ptr = g->sb_hybrid + 576;
ptr1 = g->sb_hybrid + 2 * 18;
while (ptr >= ptr1) {
ptr -= 6;
v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
if (v != 0)
break;
}
sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
if (g->block_type == 2) {
/* XXX: check for 8000 Hz */
if (g->switch_point)
mdct_long_end = 2;
else
mdct_long_end = 0;
} else {
mdct_long_end = sblimit;
}
buf = mdct_buf;
ptr = g->sb_hybrid;
for(j=0;j<mdct_long_end;j++) {
/* apply window & overlap with previous buffer */
out_ptr = sb_samples + j;
/* select window */
if (g->switch_point && j < 2)
win1 = mdct_win[0];
else
win1 = mdct_win[g->block_type];
/* select frequency inversion */
win = win1 + ((4 * 36) & -(j & 1));
imdct36(out_ptr, buf, ptr, win);
out_ptr += 18*SBLIMIT;
Fabrice Bellard
committed
ptr += 18;
buf += 18;
}
for(j=mdct_long_end;j<sblimit;j++) {
/* select frequency inversion */
win = mdct_win[2] + ((4 * 36) & -(j & 1));
out_ptr = sb_samples + j;
for(i=0; i<6; i++){
*out_ptr = buf[i];
out_ptr += SBLIMIT;
}
imdct12(out2, ptr + 0);
for(i=0;i<6;i++) {
*out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
Fabrice Bellard
committed
out_ptr += SBLIMIT;
}
imdct12(out2, ptr + 1);
for(i=0;i<6;i++) {
*out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
out_ptr += SBLIMIT;
}
imdct12(out2, ptr + 2);
for(i=0;i<6;i++) {
buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
buf[i + 6*2] = 0;
}
Fabrice Bellard
committed
ptr += 18;
buf += 18;
}
/* zero bands */
for(j=sblimit;j<SBLIMIT;j++) {
/* overlap */
out_ptr = sb_samples + j;
for(i=0;i<18;i++) {
*out_ptr = buf[i];
buf[i] = 0;
out_ptr += SBLIMIT;
}
buf += 18;
}
}
void sample_dump(int fnum, int32_t *tab, int n)
Fabrice Bellard
committed
{
static FILE *files[16], *f;
char buf[512];
Fabrice Bellard
committed
int i;
Fabrice Bellard
committed
f = files[fnum];
if (!f) {
snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
fnum,
Fabrice Bellard
committed
#ifdef USE_HIGHPRECISION
"hp"
#else
"lp"
#endif
);
Fabrice Bellard
committed
f = fopen(buf, "w");
if (!f)
return;
files[fnum] = f;
}
Fabrice Bellard
committed
if (fnum == 0) {
static int pos = 0;
av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
Fabrice Bellard
committed
for(i=0;i<n;i++) {
av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
Fabrice Bellard
committed
if ((i % 18) == 17)
Fabrice Bellard
committed
}
pos += n;
}
Fabrice Bellard
committed
for(i=0;i<n;i++) {
/* normalize to 23 frac bits */
v = tab[i] << (23 - FRAC_BITS);
Fabrice Bellard
committed
}
Fabrice Bellard
committed
}
#endif
/* main layer3 decoding function */
static int mp_decode_layer3(MPADecodeContext *s)
{
int nb_granules, main_data_begin, private_bits;
int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
GranuleDef granules[2][2], *g;
Fabrice Bellard
committed
/* read side info */
if (s->lsf) {
main_data_begin = get_bits(&s->gb, 8);
Fabrice Bellard
committed
nb_granules = 1;
} else {
main_data_begin = get_bits(&s->gb, 9);
if (s->nb_channels == 2)
private_bits = get_bits(&s->gb, 3);
else
private_bits = get_bits(&s->gb, 5);
nb_granules = 2;
for(ch=0;ch<s->nb_channels;ch++) {
granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
granules[ch][1].scfsi = get_bits(&s->gb, 4);
}
}
Fabrice Bellard
committed
for(gr=0;gr<nb_granules;gr++) {
for(ch=0;ch<s->nb_channels;ch++) {
dprintf("gr=%d ch=%d: side_info\n", gr, ch);
g = &granules[ch][gr];
g->part2_3_length = get_bits(&s->gb, 12);
g->big_values = get_bits(&s->gb, 9);
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)) ==
Fabrice Bellard
committed
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_bits(&s->gb, 1);
if (blocksplit_flag) {
g->block_type = get_bits(&s->gb, 2);
if (g->block_type == 0)
return -1;
g->switch_point = get_bits(&s->gb, 1);
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);
/* compute huffman coded region sizes */
if (g->block_type == 2)
g->region_size[0] = (36 / 2);
else {
if (s->sample_rate_index <= 2)
Fabrice Bellard
committed
g->region_size[0] = (36 / 2);
else if (s->sample_rate_index != 8)
Fabrice Bellard
committed
g->region_size[0] = (54 / 2);
else
g->region_size[0] = (108 / 2);
}
g->region_size[1] = (576 / 2);
} else {
int region_address1, region_address2, l;
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);
dprintf("region1=%d region2=%d\n",
Fabrice Bellard
committed
region_address1, region_address2);
Fabrice Bellard
committed
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;
Fabrice Bellard
committed
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++) {
Fabrice Bellard
committed
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 */
Fabrice Bellard
committed
} else {
g->long_end = 0;
g->short_start = 0;
}
} else {
g->short_start = 13;
g->long_end = 22;
}
Fabrice Bellard
committed
g->preflag = 0;
if (!s->lsf)
g->preflag = get_bits(&s->gb, 1);
g->scalefac_scale = get_bits(&s->gb, 1);
g->count1table_select = get_bits(&s->gb, 1);
dprintf("block_type=%d switch_point=%d\n",
g->block_type, g->switch_point);
}
}
Fabrice Bellard
committed
/* now we get bits from the main_data_begin offset */
dprintf("seekback: %d\n", main_data_begin);
seek_to_maindata(s, main_data_begin);
Fabrice Bellard
committed
for(gr=0;gr<nb_granules;gr++) {
for(ch=0;ch<s->nb_channels;ch++) {
g = &granules[ch][gr];
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("slen1=%d slen2=%d\n", slen1, slen2);
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 = granules[ch][0].scale_factors;
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
} else {
/* simply copy from last granule */
for(i=0;i<n;i++) {
g->scale_factors[j] = sc[j];
j++;
}
}
}
g->scale_factors[j++] = 0;
}
Fabrice Bellard
committed
{
dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
Fabrice Bellard
committed
g->scfsi, gr, ch);
for(i=0;i<j;i++)
dprintf(" %d", g->scale_factors[i]);
dprintf("\n");
Fabrice Bellard
committed
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
}
#endif
} 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;
Fabrice Bellard
committed
{
dprintf("gr=%d ch=%d scale_factors:\n",
Fabrice Bellard
committed
gr, ch);
for(i=0;i<40;i++)
dprintf(" %d", g->scale_factors[i]);
dprintf("\n");
Fabrice Bellard
committed
}
#endif
}
exponents_from_scale_factors(s, g, exponents);
/* read Huffman coded residue */
if (huffman_decode(s, g, exponents,
bits_pos + g->part2_3_length) < 0)
return -1;
#if defined(DEBUG)
sample_dump(0, g->sb_hybrid, 576);
Fabrice Bellard
committed
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
#endif
/* skip extension bits */
bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
if (bits_left < 0) {
dprintf("bits_left=%d\n", bits_left);
return -1;
}
while (bits_left >= 16) {
skip_bits(&s->gb, 16);
bits_left -= 16;
}
if (bits_left > 0)
skip_bits(&s->gb, bits_left);
} /* ch */
if (s->nb_channels == 2)
compute_stereo(s, &granules[0][gr], &granules[1][gr]);
for(ch=0;ch<s->nb_channels;ch++) {
g = &granules[ch][gr];
reorder_block(s, g);
Fabrice Bellard
committed
sample_dump(0, g->sb_hybrid, 576);
#endif
Michael Niedermayer
committed
s->compute_antialias(s, g);
Fabrice Bellard
committed
#if defined(DEBUG)
Fabrice Bellard
committed
sample_dump(1, g->sb_hybrid, 576);
#endif
compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
Fabrice Bellard
committed
#if defined(DEBUG)
Fabrice Bellard
committed
sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
#endif
}
} /* gr */
return nb_granules * 18;
}
static int mp_decode_frame(MPADecodeContext *s,
OUT_INT *samples)
Fabrice Bellard
committed
{
int i, nb_frames, ch;
OUT_INT *samples_ptr;
Fabrice Bellard
committed
init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
Michael Niedermayer
committed
(s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
Fabrice Bellard
committed
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
/* skip error protection field */
if (s->error_protection)
get_bits(&s->gb, 16);
dprintf("frame %d:\n", s->frame_count);
switch(s->layer) {
case 1:
nb_frames = mp_decode_layer1(s);
break;
case 2:
nb_frames = mp_decode_layer2(s);
break;
case 3:
default:
nb_frames = mp_decode_layer3(s);
break;
}
#if defined(DEBUG)
for(i=0;i<nb_frames;i++) {
for(ch=0;ch<s->nb_channels;ch++) {
int j;
Fabrice Bellard
committed
for(j=0;j<SBLIMIT;j++)
dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
dprintf("\n");
Fabrice Bellard
committed
}
}
#endif
/* apply the synthesis filter */
for(ch=0;ch<s->nb_channels;ch++) {
samples_ptr = samples + ch;
for(i=0;i<nb_frames;i++) {
ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
window, &s->dither_state,
samples_ptr, s->nb_channels,
Fabrice Bellard
committed
s->sb_samples[ch][i]);
samples_ptr += 32 * s->nb_channels;
}
}
#ifdef DEBUG
Fabrice Bellard
committed
#endif
return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
Fabrice Bellard
committed
}
void *data, int *data_size,
uint8_t * buf, int buf_size)
uint32_t header;
uint8_t *buf_ptr;
OUT_INT *out_samples = data;
len = s->inbuf_ptr - s->inbuf;
if (s->frame_size == 0) {
Fabrice Bellard
committed
/* special case for next header for first frame in free
format case (XXX: find a simpler method) */
if (s->free_format_next_header != 0) {
s->inbuf[0] = s->free_format_next_header >> 24;
s->inbuf[1] = s->free_format_next_header >> 16;
s->inbuf[2] = s->free_format_next_header >> 8;
s->inbuf[3] = s->free_format_next_header;
s->inbuf_ptr = s->inbuf + 4;
s->free_format_next_header = 0;
goto got_header;
}
/* no header seen : find one. We need at least HEADER_SIZE
Fabrice Bellard
committed
bytes to parse it */
len = HEADER_SIZE - len;
if (len > buf_size)
len = buf_size;
if (len > 0) {
memcpy(s->inbuf_ptr, buf_ptr, len);
buf_ptr += len;
buf_size -= len;
s->inbuf_ptr += len;
}
if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
Fabrice Bellard
committed
got_header:
header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
(s->inbuf[2] << 8) | s->inbuf[3];
if (ff_mpa_check_header(header) < 0) {
/* no sync found : move by one byte (inefficient, but simple!) */
memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
s->inbuf_ptr--;
Fabrice Bellard
committed
dprintf("skip %x\n", header);
/* reset free format frame size to give a chance
to get a new bitrate */
s->free_format_frame_size = 0;
} else {
if (decode_header(s, header) == 1) {
Fabrice Bellard
committed
/* free format: prepare to compute frame size */
Fabrice Bellard
committed
}
Fabrice Bellard
committed
/* update codec info */
avctx->sample_rate = s->sample_rate;
avctx->channels = s->nb_channels;
avctx->bit_rate = s->bit_rate;
Fabrice Bellard
committed
avctx->sub_id = s->layer;
Fabrice Bellard
committed
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;
}
Fabrice Bellard
committed
} else if (s->frame_size == -1) {
/* free format : find next sync to compute frame size */
len = MPA_MAX_CODED_FRAME_SIZE - len;
if (len > buf_size)
len = buf_size;
Fabrice Bellard
committed
if (len == 0) {
/* frame too long: resync */
Fabrice Bellard
committed
s->frame_size = 0;
memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
s->inbuf_ptr--;
Fabrice Bellard
committed
} else {
uint8_t *p, *pend;
uint32_t header1;
Fabrice Bellard
committed
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
int padding;
memcpy(s->inbuf_ptr, buf_ptr, len);
/* check for header */
p = s->inbuf_ptr - 3;
pend = s->inbuf_ptr + len - 4;
while (p <= pend) {
header = (p[0] << 24) | (p[1] << 16) |
(p[2] << 8) | p[3];
header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
(s->inbuf[2] << 8) | s->inbuf[3];
/* check with high probability that we have a
valid header */
if ((header & SAME_HEADER_MASK) ==
(header1 & SAME_HEADER_MASK)) {
/* header found: update pointers */
len = (p + 4) - s->inbuf_ptr;
buf_ptr += len;
buf_size -= len;
s->inbuf_ptr = p;
/* compute frame size */
s->free_format_next_header = header;
s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
padding = (header1 >> 9) & 1;
if (s->layer == 1)
s->free_format_frame_size -= padding * 4;
else
s->free_format_frame_size -= padding;
dprintf("free frame size=%d padding=%d\n",
Fabrice Bellard
committed
s->free_format_frame_size, padding);
decode_header(s, header1);
goto next_data;
}
p++;
}
/* not found: simply increase pointers */
buf_ptr += len;
s->inbuf_ptr += len;
buf_size -= len;
}
} else if (len < s->frame_size) {
if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
len = s->frame_size - len;
if (len > buf_size)
len = buf_size;
memcpy(s->inbuf_ptr, buf_ptr, len);
buf_ptr += len;
s->inbuf_ptr += len;
buf_size -= len;
}
Fabrice Bellard
committed
next_data:
Fabrice Bellard
committed
(s->inbuf_ptr - s->inbuf) >= s->frame_size) {
if (avctx->parse_only) {
/* simply return the frame data */
*(uint8_t **)data = s->inbuf;
out_size = s->inbuf_ptr - s->inbuf;
} else {
out_size = mp_decode_frame(s, out_samples);
}
s->inbuf_ptr = s->inbuf;
s->frame_size = 0;
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
static int decode_frame_adu(AVCodecContext * avctx,
void *data, int *data_size,
uint8_t * buf, int buf_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;
memcpy(s->inbuf, buf, len);
s->inbuf_ptr = s->inbuf + len;
// Get header and restore sync word
header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
(s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
*data_size = 0;
return buf_size;
}
decode_header(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;
avctx->frame_size=s->frame_size = len;
if (avctx->parse_only) {
/* simply return the frame data */
*(uint8_t **)data = s->inbuf;
out_size = s->inbuf_ptr - s->inbuf;
} else {
out_size = mp_decode_frame(s, out_samples);
}
*data_size = out_size;
return buf_size;
}
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
/* 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 */
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
static int chan_offset[9][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
{0,2} // FLR BLRS
};
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;
}
s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
s->frames = mp3Frames[s->chan_cfg];
if(!s->frames) {
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
return -1;
}
avctx->channels = mp3Channels[s->chan_cfg];
/* 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
*/
// 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]->compute_antialias = s->mp3decctx[0]->compute_antialias;
s->mp3decctx[i]->inbuf = &s->mp3decctx[i]->inbuf1[0][BACKSTEP_SIZE];
s->mp3decctx[i]->inbuf_ptr = s->mp3decctx[i]->inbuf;
s->mp3decctx[i]->adu_mode = 1;
}
return 0;
}
static 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,
uint8_t * buf, int buf_size)
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
{
MP3On4DecodeContext *s = avctx->priv_data;
MPADecodeContext *m;
int len, 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;
// Discard too short frames
if (buf_size < HEADER_SIZE) {
*data_size = 0;
return buf_size;
}
// If only one decoder interleave is not needed
outptr = s->frames == 1 ? out_samples : decoded_buf;
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;
m = s->mp3decctx[fr];
assert (m != NULL);
/* copy original to new */
m->inbuf_ptr = m->inbuf + fsize;
memcpy(m->inbuf, start, fsize);
// Get header
header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) |
(m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000;
if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
*data_size = 0;
return buf_size;
}
decode_header(m, header);
mp_decode_frame(m, decoded_buf);
n = MPA_FRAME_SIZE * m->nb_channels;
out_size += n * sizeof(OUT_INT);
if(s->frames > 1) {
/* interleave output data */
bp = out_samples + coff[fr];
if(m->nb_channels == 1) {
for(j = 0; j < n; j++) {
*bp = decoded_buf[j];
bp += off;
}
} else {
for(j = 0; j < n; j++) {
bp[0] = decoded_buf[j++];
bp[1] = decoded_buf[j];
bp += off;
}
}
}
}
/* 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;
}
CODEC_TYPE_AUDIO,
CODEC_ID_MP2,
sizeof(MPADecodeContext),
decode_init,
NULL,
NULL,
decode_frame,
Fabrice Bellard
committed
CODEC_CAP_PARSE_ONLY,
AVCodec mp3_decoder =
{
"mp3",
CODEC_TYPE_AUDIO,
sizeof(MPADecodeContext),
decode_init,
NULL,
NULL,
decode_frame,
Fabrice Bellard
committed
CODEC_CAP_PARSE_ONLY,
AVCodec mp3adu_decoder =
{
"mp3adu",
CODEC_TYPE_AUDIO,
CODEC_ID_MP3ADU,
sizeof(MPADecodeContext),
decode_init,
NULL,
NULL,
decode_frame_adu,
CODEC_CAP_PARSE_ONLY,
};
AVCodec mp3on4_decoder =
{
"mp3on4",
CODEC_TYPE_AUDIO,
CODEC_ID_MP3ON4,
sizeof(MP3On4DecodeContext),
decode_init_mp3on4,
NULL,
decode_close_mp3on4,
decode_frame_mp3on4,
0
};