comparison mpegaudiodec.c @ 3634:fa4beebb74a6 libavcodec

remove duplicated parser, people who pass random gibblets of the bitstream into the decoder will have to pass it through a AVParser like for all other codecs too remove silly internal buffering architecture (removes 1 memcpy() of the bitstream) mp3on4 and mp3adu untested
author michael
date Sat, 26 Aug 2006 12:19:05 +0000
parents 8624a5c2824d
children 0087dc6b306c
comparison
equal deleted inserted replaced
3633:f9660d3a9975 3634:fa4beebb74a6
83 83
84 /****************/ 84 /****************/
85 85
86 #define HEADER_SIZE 4 86 #define HEADER_SIZE 4
87 #define BACKSTEP_SIZE 512 87 #define BACKSTEP_SIZE 512
88 #define EXTRABYTES 24
88 89
89 struct GranuleDef; 90 struct GranuleDef;
90 91
91 typedef struct MPADecodeContext { 92 typedef struct MPADecodeContext {
92 uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */ 93 DECLARE_ALIGNED_8(uint8_t, last_buf[BACKSTEP_SIZE + EXTRABYTES + MPA_MAX_CODED_FRAME_SIZE]); //FIXME we dont need that much
93 int inbuf_index; 94 int last_buf_size;
94 uint8_t *inbuf_ptr, *inbuf;
95 int frame_size; 95 int frame_size;
96 int free_format_frame_size; /* frame size in case of free format 96 int free_format_frame_size; /* frame size in case of free format
97 (zero if currently unknown) */ 97 (zero if currently unknown) */
98 /* next header (used in free format parsing) */ 98 /* next header (used in free format parsing) */
99 uint32_t free_format_next_header; 99 uint32_t free_format_next_header;
102 int sample_rate; 102 int sample_rate;
103 int sample_rate_index; /* between 0 and 8 */ 103 int sample_rate_index; /* between 0 and 8 */
104 int bit_rate; 104 int bit_rate;
105 int old_frame_size; 105 int old_frame_size;
106 GetBitContext gb; 106 GetBitContext gb;
107 GetBitContext in_gb;
107 int nb_channels; 108 int nb_channels;
108 int mode; 109 int mode;
109 int mode_ext; 110 int mode_ext;
110 int lsf; 111 int lsf;
111 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16))); 112 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
533 } 534 }
534 #endif 535 #endif
535 init = 1; 536 init = 1;
536 } 537 }
537 538
538 s->inbuf_index = 0;
539 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
540 s->inbuf_ptr = s->inbuf;
541 #ifdef DEBUG 539 #ifdef DEBUG
542 s->frame_count = 0; 540 s->frame_count = 0;
543 #endif 541 #endif
544 if (avctx->codec_id == CODEC_ID_MP3ADU) 542 if (avctx->codec_id == CODEC_ID_MP3ADU)
545 s->adu_mode = 1; 543 s->adu_mode = 1;
1583 } 1581 }
1584 } 1582 }
1585 return 3 * 12; 1583 return 3 * 12;
1586 } 1584 }
1587 1585
1588 /*
1589 * Seek back in the stream for backstep bytes (at most 511 bytes)
1590 */
1591 static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1592 {
1593 uint8_t *ptr;
1594
1595 /* compute current position in stream */
1596 ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1597
1598 /* copy old data before current one */
1599 ptr -= backstep;
1600 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1601 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1602 /* init get bits again */
1603 init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1604
1605 /* prepare next buffer */
1606 s->inbuf_index ^= 1;
1607 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1608 s->old_frame_size = s->frame_size;
1609 }
1610
1611 static inline void lsf_sf_expand(int *slen, 1586 static inline void lsf_sf_expand(int *slen,
1612 int sf, int n1, int n2, int n3) 1587 int sf, int n1, int n2, int n3)
1613 { 1588 {
1614 if (n3) { 1589 if (n3) {
1615 slen[3] = sf % n3; 1590 slen[3] = sf % n3;
1674 else 1649 else
1675 return get_bits(s, n); 1650 return get_bits(s, n);
1676 } 1651 }
1677 1652
1678 static int huffman_decode(MPADecodeContext *s, GranuleDef *g, 1653 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1679 int16_t *exponents, int end_pos) 1654 int16_t *exponents, int end_pos2)
1680 { 1655 {
1681 int s_index; 1656 int s_index;
1682 int i; 1657 int i;
1683 int last_pos; 1658 int last_pos, bits_left;
1684 VLC *vlc; 1659 VLC *vlc;
1660 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1685 1661
1686 /* low frequencies (called big values) */ 1662 /* low frequencies (called big values) */
1687 s_index = 0; 1663 s_index = 0;
1688 for(i=0;i<3;i++) { 1664 for(i=0;i<3;i++) {
1689 int j, k, l, linbits; 1665 int j, k, l, linbits;
1703 } 1679 }
1704 1680
1705 /* read huffcode and compute each couple */ 1681 /* read huffcode and compute each couple */
1706 for(;j>0;j--) { 1682 for(;j>0;j--) {
1707 int exponent, x, y, v; 1683 int exponent, x, y, v;
1708 1684 int pos= get_bits_count(&s->gb);
1709 if (get_bits_count(&s->gb) >= end_pos) 1685
1710 break; 1686 if (pos >= end_pos){
1687 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1688 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1689 s->gb= s->in_gb;
1690 s->in_gb.buffer=NULL;
1691 assert((get_bits_count(&s->gb) & 7) == 0);
1692 skip_bits_long(&s->gb, pos - end_pos);
1693 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1694 pos= get_bits_count(&s->gb);
1695 }
1696 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1697 if(pos >= end_pos)
1698 break;
1699 }
1711 y = get_vlc2(&s->gb, vlc->table, 7, 3); 1700 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1712 1701
1713 if(!y){ 1702 if(!y){
1714 g->sb_hybrid[s_index ] = 1703 g->sb_hybrid[s_index ] =
1715 g->sb_hybrid[s_index+1] = 0; 1704 g->sb_hybrid[s_index+1] = 0;
1766 last_pos=0; 1755 last_pos=0;
1767 while (s_index <= 572) { 1756 while (s_index <= 572) {
1768 int pos, code; 1757 int pos, code;
1769 pos = get_bits_count(&s->gb); 1758 pos = get_bits_count(&s->gb);
1770 if (pos >= end_pos) { 1759 if (pos >= end_pos) {
1771 if (pos > end_pos && last_pos){ 1760 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1761 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1762 s->gb= s->in_gb;
1763 s->in_gb.buffer=NULL;
1764 assert((get_bits_count(&s->gb) & 7) == 0);
1765 skip_bits_long(&s->gb, pos - end_pos);
1766 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1767 pos= get_bits_count(&s->gb);
1768 }
1769 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1770 if (pos > end_pos && last_pos){ //FIXME last_pos is messed if we switch buffers
1772 /* some encoders generate an incorrect size for this 1771 /* some encoders generate an incorrect size for this
1773 part. We must go back into the data */ 1772 part. We must go back into the data */
1774 s_index -= 4; 1773 s_index -= 4;
1775 init_get_bits(&s->gb, s->gb.buffer + 4*(last_pos>>5), s->gb.size_in_bits - (last_pos&(~31))); 1774 skip_bits_long(&s->gb, last_pos - pos);
1776 skip_bits(&s->gb, last_pos&31); 1775 av_log(NULL, AV_LOG_ERROR, "overread, skip %d\n", last_pos&7);
1777 } 1776 }
1778 break; 1777 if(pos >= end_pos)
1778 break;
1779 } 1779 }
1780 last_pos= pos; 1780 last_pos= pos;
1781 1781
1782 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1); 1782 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1783 dprintf("t=%d code=%d\n", g->count1table_select, code); 1783 dprintf("t=%d code=%d\n", g->count1table_select, code);
1797 g->sb_hybrid[pos] = v; 1797 g->sb_hybrid[pos] = v;
1798 } 1798 }
1799 s_index+=4; 1799 s_index+=4;
1800 } 1800 }
1801 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index)); 1801 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1802
1803 /* skip extension bits */
1804 bits_left = end_pos - get_bits_count(&s->gb);
1805 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1806 if (bits_left < 0) {
1807 dprintf("bits_left=%d\n", bits_left);
1808 return -1;
1809 }
1810 skip_bits_long(&s->gb, bits_left);
1811
1802 return 0; 1812 return 0;
1803 } 1813 }
1804 1814
1805 /* Reorder short blocks from bitstream order to interleaved order. It 1815 /* Reorder short blocks from bitstream order to interleaved order. It
1806 would be faster to do it in parsing, but the code would be far more 1816 would be faster to do it in parsing, but the code would be far more
2182 2192
2183 /* main layer3 decoding function */ 2193 /* main layer3 decoding function */
2184 static int mp_decode_layer3(MPADecodeContext *s) 2194 static int mp_decode_layer3(MPADecodeContext *s)
2185 { 2195 {
2186 int nb_granules, main_data_begin, private_bits; 2196 int nb_granules, main_data_begin, private_bits;
2187 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left; 2197 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2188 GranuleDef granules[2][2], *g; 2198 GranuleDef granules[2][2], *g;
2189 int16_t exponents[576]; 2199 int16_t exponents[576];
2190 2200
2191 /* read side info */ 2201 /* read side info */
2192 if (s->lsf) { 2202 if (s->lsf) {
2306 g->block_type, g->switch_point); 2316 g->block_type, g->switch_point);
2307 } 2317 }
2308 } 2318 }
2309 2319
2310 if (!s->adu_mode) { 2320 if (!s->adu_mode) {
2321 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2311 /* now we get bits from the main_data_begin offset */ 2322 /* now we get bits from the main_data_begin offset */
2312 dprintf("seekback: %d\n", main_data_begin); 2323 dprintf("seekback: %d\n", main_data_begin);
2313 seek_to_maindata(s, main_data_begin); 2324 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2325 if(main_data_begin > s->last_buf_size)
2326 s->last_buf_size= main_data_begin;
2327
2328 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2329 s->in_gb= s->gb;
2330 init_get_bits(&s->gb, s->last_buf + s->last_buf_size - main_data_begin, main_data_begin*8);
2331 /* prepare next buffer */
2332 s->old_frame_size = s->frame_size;
2314 } 2333 }
2315 2334
2316 for(gr=0;gr<nb_granules;gr++) { 2335 for(gr=0;gr<nb_granules;gr++) {
2317 for(ch=0;ch<s->nb_channels;ch++) { 2336 for(ch=0;ch<s->nb_channels;ch++) {
2318 g = &granules[ch][gr]; 2337 g = &granules[ch][gr];
2450 bits_pos + g->part2_3_length) < 0) 2469 bits_pos + g->part2_3_length) < 0)
2451 return -1; 2470 return -1;
2452 #if defined(DEBUG) 2471 #if defined(DEBUG)
2453 sample_dump(0, g->sb_hybrid, 576); 2472 sample_dump(0, g->sb_hybrid, 576);
2454 #endif 2473 #endif
2455
2456 /* skip extension bits */
2457 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2458 if (bits_left < 0) {
2459 dprintf("bits_left=%d\n", bits_left);
2460 return -1;
2461 }
2462 while (bits_left >= 16) {
2463 skip_bits(&s->gb, 16);
2464 bits_left -= 16;
2465 }
2466 if (bits_left > 0)
2467 skip_bits(&s->gb, bits_left);
2468 } /* ch */ 2474 } /* ch */
2469 2475
2470 if (s->nb_channels == 2) 2476 if (s->nb_channels == 2)
2471 compute_stereo(s, &granules[0][gr], &granules[1][gr]); 2477 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2472 2478
2489 } /* gr */ 2495 } /* gr */
2490 return nb_granules * 18; 2496 return nb_granules * 18;
2491 } 2497 }
2492 2498
2493 static int mp_decode_frame(MPADecodeContext *s, 2499 static int mp_decode_frame(MPADecodeContext *s,
2494 OUT_INT *samples) 2500 OUT_INT *samples, const uint8_t *buf, int buf_size)
2495 { 2501 {
2496 int i, nb_frames, ch; 2502 int i, nb_frames, ch;
2497 OUT_INT *samples_ptr; 2503 OUT_INT *samples_ptr;
2498 2504
2499 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, 2505 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2500 (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2501 2506
2502 /* skip error protection field */ 2507 /* skip error protection field */
2503 if (s->error_protection) 2508 if (s->error_protection)
2504 get_bits(&s->gb, 16); 2509 get_bits(&s->gb, 16);
2505 2510
2512 nb_frames = mp_decode_layer2(s); 2517 nb_frames = mp_decode_layer2(s);
2513 break; 2518 break;
2514 case 3: 2519 case 3:
2515 default: 2520 default:
2516 nb_frames = mp_decode_layer3(s); 2521 nb_frames = mp_decode_layer3(s);
2522
2523 if(s->in_gb.buffer)
2524 s->gb= s->in_gb;
2525 align_get_bits(&s->gb);
2526 assert((get_bits_count(&s->gb) & 7) == 0);
2527 s->last_buf_size= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2528 memcpy(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), s->last_buf_size);
2529
2517 break; 2530 break;
2518 } 2531 }
2519 #if defined(DEBUG) 2532 #if defined(DEBUG)
2520 for(i=0;i<nb_frames;i++) { 2533 for(i=0;i<nb_frames;i++) {
2521 for(ch=0;ch<s->nb_channels;ch++) { 2534 for(ch=0;ch<s->nb_channels;ch++) {
2548 void *data, int *data_size, 2561 void *data, int *data_size,
2549 uint8_t * buf, int buf_size) 2562 uint8_t * buf, int buf_size)
2550 { 2563 {
2551 MPADecodeContext *s = avctx->priv_data; 2564 MPADecodeContext *s = avctx->priv_data;
2552 uint32_t header; 2565 uint32_t header;
2553 uint8_t *buf_ptr; 2566 int out_size;
2554 int len, out_size;
2555 OUT_INT *out_samples = data; 2567 OUT_INT *out_samples = data;
2556 2568
2557 buf_ptr = buf; 2569 retry:
2558 while (buf_size > 0) { 2570 if(buf_size < HEADER_SIZE)
2559 len = s->inbuf_ptr - s->inbuf; 2571 return -1;
2560 if (s->frame_size == 0) { 2572
2561 /* special case for next header for first frame in free 2573 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2562 format case (XXX: find a simpler method) */ 2574 if(ff_mpa_check_header(header) < 0){
2563 if (s->free_format_next_header != 0) { 2575 buf++;
2564 s->inbuf[0] = s->free_format_next_header >> 24; 2576 // buf_size--;
2565 s->inbuf[1] = s->free_format_next_header >> 16; 2577 av_log(avctx, AV_LOG_ERROR, "header missing skiping one byte\n");
2566 s->inbuf[2] = s->free_format_next_header >> 8; 2578 goto retry;
2567 s->inbuf[3] = s->free_format_next_header; 2579 }
2568 s->inbuf_ptr = s->inbuf + 4; 2580
2569 s->free_format_next_header = 0; 2581 if (decode_header(s, header) == 1) {
2570 goto got_header; 2582 /* free format: prepare to compute frame size */
2571 } 2583 s->frame_size = -1;
2572 /* no header seen : find one. We need at least HEADER_SIZE 2584 return -1;
2573 bytes to parse it */ 2585 }
2574 len = HEADER_SIZE - len; 2586 /* update codec info */
2575 if (len > buf_size) 2587 avctx->sample_rate = s->sample_rate;
2576 len = buf_size; 2588 avctx->channels = s->nb_channels;
2577 if (len > 0) { 2589 avctx->bit_rate = s->bit_rate;
2578 memcpy(s->inbuf_ptr, buf_ptr, len); 2590 avctx->sub_id = s->layer;
2579 buf_ptr += len; 2591 switch(s->layer) {
2580 buf_size -= len; 2592 case 1:
2581 s->inbuf_ptr += len; 2593 avctx->frame_size = 384;
2582 } 2594 break;
2583 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) { 2595 case 2:
2584 got_header: 2596 avctx->frame_size = 1152;
2585 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | 2597 break;
2586 (s->inbuf[2] << 8) | s->inbuf[3]; 2598 case 3:
2587 2599 if (s->lsf)
2588 if (ff_mpa_check_header(header) < 0) { 2600 avctx->frame_size = 576;
2589 /* no sync found : move by one byte (inefficient, but simple!) */ 2601 else
2590 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); 2602 avctx->frame_size = 1152;
2591 s->inbuf_ptr--; 2603 break;
2592 dprintf("skip %x\n", header); 2604 }
2593 /* reset free format frame size to give a chance 2605
2594 to get a new bitrate */ 2606 if(s->frame_size<=0 || s->frame_size < buf_size){
2595 s->free_format_frame_size = 0; 2607 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2596 } else { 2608 return -1;
2597 if (decode_header(s, header) == 1) { 2609 }
2598 /* free format: prepare to compute frame size */ 2610
2599 s->frame_size = -1; 2611 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2600 } 2612 if(out_size>=0)
2601 /* update codec info */ 2613 *data_size = out_size;
2602 avctx->sample_rate = s->sample_rate; 2614 else
2603 avctx->channels = s->nb_channels; 2615 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2604 avctx->bit_rate = s->bit_rate; 2616 s->frame_size = 0;
2605 avctx->sub_id = s->layer; 2617 return buf_size;
2606 switch(s->layer) {
2607 case 1:
2608 avctx->frame_size = 384;
2609 break;
2610 case 2:
2611 avctx->frame_size = 1152;
2612 break;
2613 case 3:
2614 if (s->lsf)
2615 avctx->frame_size = 576;
2616 else
2617 avctx->frame_size = 1152;
2618 break;
2619 }
2620 }
2621 }
2622 } else if (s->frame_size == -1) {
2623 /* free format : find next sync to compute frame size */
2624 len = MPA_MAX_CODED_FRAME_SIZE - len;
2625 if (len > buf_size)
2626 len = buf_size;
2627 if (len == 0) {
2628 /* frame too long: resync */
2629 s->frame_size = 0;
2630 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2631 s->inbuf_ptr--;
2632 } else {
2633 uint8_t *p, *pend;
2634 uint32_t header1;
2635 int padding;
2636
2637 memcpy(s->inbuf_ptr, buf_ptr, len);
2638 /* check for header */
2639 p = s->inbuf_ptr - 3;
2640 pend = s->inbuf_ptr + len - 4;
2641 while (p <= pend) {
2642 header = (p[0] << 24) | (p[1] << 16) |
2643 (p[2] << 8) | p[3];
2644 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2645 (s->inbuf[2] << 8) | s->inbuf[3];
2646 /* check with high probability that we have a
2647 valid header */
2648 if ((header & SAME_HEADER_MASK) ==
2649 (header1 & SAME_HEADER_MASK)) {
2650 /* header found: update pointers */
2651 len = (p + 4) - s->inbuf_ptr;
2652 buf_ptr += len;
2653 buf_size -= len;
2654 s->inbuf_ptr = p;
2655 /* compute frame size */
2656 s->free_format_next_header = header;
2657 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2658 padding = (header1 >> 9) & 1;
2659 if (s->layer == 1)
2660 s->free_format_frame_size -= padding * 4;
2661 else
2662 s->free_format_frame_size -= padding;
2663 dprintf("free frame size=%d padding=%d\n",
2664 s->free_format_frame_size, padding);
2665 decode_header(s, header1);
2666 goto next_data;
2667 }
2668 p++;
2669 }
2670 /* not found: simply increase pointers */
2671 buf_ptr += len;
2672 s->inbuf_ptr += len;
2673 buf_size -= len;
2674 }
2675 } else if (len < s->frame_size) {
2676 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2677 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2678 len = s->frame_size - len;
2679 if (len > buf_size)
2680 len = buf_size;
2681 memcpy(s->inbuf_ptr, buf_ptr, len);
2682 buf_ptr += len;
2683 s->inbuf_ptr += len;
2684 buf_size -= len;
2685 }
2686 next_data:
2687 if (s->frame_size > 0 &&
2688 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2689 if (avctx->parse_only) {
2690 /* simply return the frame data */
2691 *(uint8_t **)data = s->inbuf;
2692 out_size = s->inbuf_ptr - s->inbuf;
2693 } else {
2694 out_size = mp_decode_frame(s, out_samples);
2695 }
2696 s->inbuf_ptr = s->inbuf;
2697 s->frame_size = 0;
2698 if(out_size>=0)
2699 *data_size = out_size;
2700 else
2701 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2702 break;
2703 }
2704 }
2705 return buf_ptr - buf;
2706 } 2618 }
2707 2619
2708 2620
2709 static int decode_frame_adu(AVCodecContext * avctx, 2621 static int decode_frame_adu(AVCodecContext * avctx,
2710 void *data, int *data_size, 2622 void *data, int *data_size,
2725 2637
2726 2638
2727 if (len > MPA_MAX_CODED_FRAME_SIZE) 2639 if (len > MPA_MAX_CODED_FRAME_SIZE)
2728 len = MPA_MAX_CODED_FRAME_SIZE; 2640 len = MPA_MAX_CODED_FRAME_SIZE;
2729 2641
2730 memcpy(s->inbuf, buf, len);
2731 s->inbuf_ptr = s->inbuf + len;
2732
2733 // Get header and restore sync word 2642 // Get header and restore sync word
2734 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | 2643 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2735 (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
2736 2644
2737 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame 2645 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2738 *data_size = 0; 2646 *data_size = 0;
2739 return buf_size; 2647 return buf_size;
2740 } 2648 }
2747 avctx->sub_id = s->layer; 2655 avctx->sub_id = s->layer;
2748 2656
2749 avctx->frame_size=s->frame_size = len; 2657 avctx->frame_size=s->frame_size = len;
2750 2658
2751 if (avctx->parse_only) { 2659 if (avctx->parse_only) {
2752 /* simply return the frame data */ 2660 out_size = buf_size;
2753 *(uint8_t **)data = s->inbuf;
2754 out_size = s->inbuf_ptr - s->inbuf;
2755 } else { 2661 } else {
2756 out_size = mp_decode_frame(s, out_samples); 2662 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2757 } 2663 }
2758 2664
2759 *data_size = out_size; 2665 *data_size = out_size;
2760 return buf_size; 2666 return buf_size;
2761 } 2667 }
2814 * Each frame is 1 or 2 channels - up to 5 frames allowed 2720 * Each frame is 1 or 2 channels - up to 5 frames allowed
2815 */ 2721 */
2816 for (i = 1; i < s->frames; i++) { 2722 for (i = 1; i < s->frames; i++) {
2817 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext)); 2723 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2818 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias; 2724 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2819 s->mp3decctx[i]->inbuf = &s->mp3decctx[i]->inbuf1[0][BACKSTEP_SIZE];
2820 s->mp3decctx[i]->inbuf_ptr = s->mp3decctx[i]->inbuf;
2821 s->mp3decctx[i]->adu_mode = 1; 2725 s->mp3decctx[i]->adu_mode = 1;
2822 } 2726 }
2823 2727
2824 return 0; 2728 return 0;
2825 } 2729 }
2875 len -= fsize; 2779 len -= fsize;
2876 if (fsize > MPA_MAX_CODED_FRAME_SIZE) 2780 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2877 fsize = MPA_MAX_CODED_FRAME_SIZE; 2781 fsize = MPA_MAX_CODED_FRAME_SIZE;
2878 m = s->mp3decctx[fr]; 2782 m = s->mp3decctx[fr];
2879 assert (m != NULL); 2783 assert (m != NULL);
2880 /* copy original to new */
2881 m->inbuf_ptr = m->inbuf + fsize;
2882 memcpy(m->inbuf, start, fsize);
2883 2784
2884 // Get header 2785 // Get header
2885 header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) | 2786 header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2886 (m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000;
2887 2787
2888 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block 2788 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2889 *data_size = 0; 2789 *data_size = 0;
2890 return buf_size; 2790 return buf_size;
2891 } 2791 }
2892 2792
2893 decode_header(m, header); 2793 decode_header(m, header);
2894 mp_decode_frame(m, decoded_buf); 2794 mp_decode_frame(m, decoded_buf, start, fsize);
2895 2795
2896 n = MPA_FRAME_SIZE * m->nb_channels; 2796 n = MPA_FRAME_SIZE * m->nb_channels;
2897 out_size += n * sizeof(OUT_INT); 2797 out_size += n * sizeof(OUT_INT);
2898 if(s->frames > 1) { 2798 if(s->frames > 1) {
2899 /* interleave output data */ 2799 /* interleave output data */