Revision 06e7fb82 libavcodec/mpegaudiodec.c

View differences:

libavcodec/mpegaudiodec.c
85 85

  
86 86
#define HEADER_SIZE 4
87 87
#define BACKSTEP_SIZE 512
88
#define EXTRABYTES 24
88 89

  
89 90
struct GranuleDef;
90 91

  
91 92
typedef struct MPADecodeContext {
92
    uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE];        /* input buffer */
93
    int inbuf_index;
94
    uint8_t *inbuf_ptr, *inbuf;
93
    DECLARE_ALIGNED_8(uint8_t, last_buf[BACKSTEP_SIZE + EXTRABYTES + MPA_MAX_CODED_FRAME_SIZE]); //FIXME we dont need that much
94
    int last_buf_size;
95 95
    int frame_size;
96 96
    int free_format_frame_size; /* frame size in case of free format
97 97
                                   (zero if currently unknown) */
......
104 104
    int bit_rate;
105 105
    int old_frame_size;
106 106
    GetBitContext gb;
107
    GetBitContext in_gb;
107 108
    int nb_channels;
108 109
    int mode;
109 110
    int mode_ext;
......
535 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 539
#ifdef DEBUG
542 540
    s->frame_count = 0;
543 541
#endif
......
1585 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 1586
static inline void lsf_sf_expand(int *slen,
1612 1587
                                 int sf, int n1, int n2, int n3)
1613 1588
{
......
1676 1651
}
1677 1652

  
1678 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 1656
    int s_index;
1682 1657
    int i;
1683
    int last_pos;
1658
    int last_pos, bits_left;
1684 1659
    VLC *vlc;
1660
    int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1685 1661

  
1686 1662
    /* low frequencies (called big values) */
1687 1663
    s_index = 0;
......
1705 1681
        /* read huffcode and compute each couple */
1706 1682
        for(;j>0;j--) {
1707 1683
            int exponent, x, y, v;
1708

  
1709
            if (get_bits_count(&s->gb) >= end_pos)
1710
                break;
1684
            int pos= get_bits_count(&s->gb);
1685

  
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 1700
            y = get_vlc2(&s->gb, vlc->table, 7, 3);
1712 1701

  
1713 1702
            if(!y){
......
1768 1757
        int pos, code;
1769 1758
        pos = get_bits_count(&s->gb);
1770 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 1771
                /* some encoders generate an incorrect size for this
1773 1772
                   part. We must go back into the data */
1774 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)));
1776
                skip_bits(&s->gb, last_pos&31);
1774
                skip_bits_long(&s->gb, last_pos - pos);
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 1780
        last_pos= pos;
1781 1781

  
......
1799 1799
        s_index+=4;
1800 1800
    }
1801 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 1812
    return 0;
1803 1813
}
1804 1814

  
......
2184 2194
static int mp_decode_layer3(MPADecodeContext *s)
2185 2195
{
2186 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 2198
    GranuleDef granules[2][2], *g;
2189 2199
    int16_t exponents[576];
2190 2200

  
......
2308 2318
    }
2309 2319

  
2310 2320
  if (!s->adu_mode) {
2321
    const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2311 2322
    /* now we get bits from the main_data_begin offset */
2312 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 2335
    for(gr=0;gr<nb_granules;gr++) {
......
2452 2471
#if defined(DEBUG)
2453 2472
            sample_dump(0, g->sb_hybrid, 576);
2454 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 2474
        } /* ch */
2469 2475

  
2470 2476
        if (s->nb_channels == 2)
......
2491 2497
}
2492 2498

  
2493 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 2502
    int i, nb_frames, ch;
2497 2503
    OUT_INT *samples_ptr;
2498 2504

  
2499
    init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2500
                  (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2505
    init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2501 2506

  
2502 2507
    /* skip error protection field */
2503 2508
    if (s->error_protection)
......
2514 2519
    case 3:
2515 2520
    default:
2516 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 2530
        break;
2518 2531
    }
2519 2532
#if defined(DEBUG)
......
2550 2563
{
2551 2564
    MPADecodeContext *s = avctx->priv_data;
2552 2565
    uint32_t header;
2553
    uint8_t *buf_ptr;
2554
    int len, out_size;
2566
    int out_size;
2555 2567
    OUT_INT *out_samples = data;
2556 2568

  
2557
    buf_ptr = buf;
2558
    while (buf_size > 0) {
2559
        len = s->inbuf_ptr - s->inbuf;
2560
        if (s->frame_size == 0) {
2561
            /* special case for next header for first frame in free
2562
               format case (XXX: find a simpler method) */
2563
            if (s->free_format_next_header != 0) {
2564
                s->inbuf[0] = s->free_format_next_header >> 24;
2565
                s->inbuf[1] = s->free_format_next_header >> 16;
2566
                s->inbuf[2] = s->free_format_next_header >> 8;
2567
                s->inbuf[3] = s->free_format_next_header;
2568
                s->inbuf_ptr = s->inbuf + 4;
2569
                s->free_format_next_header = 0;
2570
                goto got_header;
2571
            }
2572
            /* no header seen : find one. We need at least HEADER_SIZE
2573
               bytes to parse it */
2574
            len = HEADER_SIZE - len;
2575
            if (len > buf_size)
2576
                len = buf_size;
2577
            if (len > 0) {
2578
                memcpy(s->inbuf_ptr, buf_ptr, len);
2579
                buf_ptr += len;
2580
                buf_size -= len;
2581
                s->inbuf_ptr += len;
2582
            }
2583
            if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2584
            got_header:
2585
                header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2586
                    (s->inbuf[2] << 8) | s->inbuf[3];
2587

  
2588
                if (ff_mpa_check_header(header) < 0) {
2589
                    /* no sync found : move by one byte (inefficient, but simple!) */
2590
                    memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2591
                    s->inbuf_ptr--;
2592
                    dprintf("skip %x\n", header);
2593
                    /* reset free format frame size to give a chance
2594
                       to get a new bitrate */
2595
                    s->free_format_frame_size = 0;
2596
                } else {
2597
                    if (decode_header(s, header) == 1) {
2598
                        /* free format: prepare to compute frame size */
2599
                        s->frame_size = -1;
2600
                    }
2601
                    /* update codec info */
2602
                    avctx->sample_rate = s->sample_rate;
2603
                    avctx->channels = s->nb_channels;
2604
                    avctx->bit_rate = s->bit_rate;
2605
                    avctx->sub_id = s->layer;
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
        }
2569
retry:
2570
    if(buf_size < HEADER_SIZE)
2571
        return -1;
2572

  
2573
    header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2574
    if(ff_mpa_check_header(header) < 0){
2575
        buf++;
2576
//        buf_size--;
2577
        av_log(avctx, AV_LOG_ERROR, "header missing skiping one byte\n");
2578
        goto retry;
2579
    }
2580

  
2581
    if (decode_header(s, header) == 1) {
2582
        /* free format: prepare to compute frame size */
2583
        s->frame_size = -1;
2584
        return -1;
2585
    }
2586
    /* update codec info */
2587
    avctx->sample_rate = s->sample_rate;
2588
    avctx->channels = s->nb_channels;
2589
    avctx->bit_rate = s->bit_rate;
2590
    avctx->sub_id = s->layer;
2591
    switch(s->layer) {
2592
    case 1:
2593
        avctx->frame_size = 384;
2594
        break;
2595
    case 2:
2596
        avctx->frame_size = 1152;
2597
        break;
2598
    case 3:
2599
        if (s->lsf)
2600
            avctx->frame_size = 576;
2601
        else
2602
            avctx->frame_size = 1152;
2603
        break;
2604
    }
2605

  
2606
    if(s->frame_size<=0 || s->frame_size < buf_size){
2607
        av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2608
        return -1;
2704 2609
    }
2705
    return buf_ptr - buf;
2610

  
2611
    out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2612
    if(out_size>=0)
2613
        *data_size = out_size;
2614
    else
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
2616
    s->frame_size = 0;
2617
    return buf_size;
2706 2618
}
2707 2619

  
2708 2620

  
......
2727 2639
    if (len > MPA_MAX_CODED_FRAME_SIZE)
2728 2640
        len = MPA_MAX_CODED_FRAME_SIZE;
2729 2641

  
2730
    memcpy(s->inbuf, buf, len);
2731
    s->inbuf_ptr = s->inbuf + len;
2732

  
2733 2642
    // Get header and restore sync word
2734
    header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2735
              (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
2643
    header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2736 2644

  
2737 2645
    if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2738 2646
        *data_size = 0;
......
2749 2657
    avctx->frame_size=s->frame_size = len;
2750 2658

  
2751 2659
    if (avctx->parse_only) {
2752
        /* simply return the frame data */
2753
        *(uint8_t **)data = s->inbuf;
2754
        out_size = s->inbuf_ptr - s->inbuf;
2660
        out_size = buf_size;
2755 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 2665
    *data_size = out_size;
......
2816 2722
    for (i = 1; i < s->frames; i++) {
2817 2723
        s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2818 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 2725
        s->mp3decctx[i]->adu_mode = 1;
2822 2726
    }
2823 2727

  
......
2877 2781
            fsize = MPA_MAX_CODED_FRAME_SIZE;
2878 2782
        m = s->mp3decctx[fr];
2879 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 2785
        // Get header
2885
        header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) |
2886
                  (m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000;
2786
        header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2887 2787

  
2888 2788
        if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2889 2789
            *data_size = 0;
......
2891 2791
        }
2892 2792

  
2893 2793
        decode_header(m, header);
2894
        mp_decode_frame(m, decoded_buf);
2794
        mp_decode_frame(m, decoded_buf, start, fsize);
2895 2795

  
2896 2796
        n = MPA_FRAME_SIZE * m->nb_channels;
2897 2797
        out_size += n * sizeof(OUT_INT);

Also available in: Unified diff