Revision 171bc51c

View differences:

libavcodec/ac3enc.c
59 59
 * Data for a single audio block.
60 60
 */
61 61
typedef struct AC3Block {
62
    uint8_t  **bap;                             ///< bap for each channel in this block
62 63
    int32_t  mdct_coef[AC3_MAX_CHANNELS][AC3_MAX_COEFS];
63 64
    uint8_t  exp[AC3_MAX_CHANNELS][AC3_MAX_COEFS];
64 65
    uint8_t  exp_strategy[AC3_MAX_CHANNELS];
......
121 122

  
122 123
    int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE];
123 124
    int16_t windowed_samples[AC3_WINDOW_SIZE];
124
    uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
125
    uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
125
    uint8_t *bap_buffer;
126
    uint8_t *bap1_buffer;
126 127
} AC3EncodeContext;
127 128

  
128 129

  
......
908 909

  
909 910

  
910 911
/**
912
 * Ensure that bap for each block and channel point to the current bap_buffer.
913
 * They may have been switched during the bit allocation search.
914
 */
915
static void reset_block_bap(AC3EncodeContext *s)
916
{
917
    int blk, ch;
918
    if (s->blocks[0].bap[0] == s->bap_buffer)
919
        return;
920
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
921
        for (ch = 0; ch < s->channels; ch++) {
922
            s->blocks[blk].bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
923
        }
924
    }
925
}
926

  
927

  
928
/**
911 929
 * Run the bit allocation with a given SNR offset.
912 930
 * This calculates the bit allocation pointers that will be used to determine
913 931
 * the quantization of each mantissa.
......
915 933
 *         is used.
916 934
 */
917 935
static int bit_alloc(AC3EncodeContext *s,
918
                     uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
919 936
                     int snr_offset)
920 937
{
921 938
    int blk, ch;
......
923 940

  
924 941
    snr_offset = (snr_offset - 240) << 2;
925 942

  
943
    reset_block_bap(s);
926 944
    mantissa_bits = 0;
927 945
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
928 946
        AC3Block *block = &s->blocks[blk];
......
933 951
            ff_ac3_bit_alloc_calc_bap(block->mask[ch], block->psd[ch], 0,
934 952
                                      s->nb_coefs[ch], snr_offset,
935 953
                                      s->bit_alloc.floor, ff_ac3_bap_tab,
936
                                      bap[blk][ch]);
937
            mantissa_bits += compute_mantissa_size(s, bap[blk][ch], s->nb_coefs[ch]);
954
                                      block->bap[ch]);
955
            mantissa_bits += compute_mantissa_size(s, block->bap[ch], s->nb_coefs[ch]);
938 956
        }
939 957
    }
940 958
    return mantissa_bits;
......
956 974
    snr_offset = s->coarse_snr_offset << 4;
957 975

  
958 976
    while (snr_offset >= 0 &&
959
           bit_alloc(s, s->bap, snr_offset) > bits_left) {
977
           bit_alloc(s, snr_offset) > bits_left) {
960 978
        snr_offset -= 64;
961 979
    }
962 980
    if (snr_offset < 0)
963 981
        return AVERROR(EINVAL);
964 982

  
983
    FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
965 984
    while (snr_offset + 64 <= 1023 &&
966
           bit_alloc(s, s->bap1, snr_offset + 64) <= bits_left) {
985
           bit_alloc(s, snr_offset + 64) <= bits_left) {
967 986
        snr_offset += 64;
968
        memcpy(s->bap, s->bap1, sizeof(s->bap1));
987
        FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
969 988
    }
970 989
    while (snr_offset + 16 <= 1023 &&
971
           bit_alloc(s, s->bap1, snr_offset + 16) <= bits_left) {
990
           bit_alloc(s, snr_offset + 16) <= bits_left) {
972 991
        snr_offset += 16;
973
        memcpy(s->bap, s->bap1, sizeof(s->bap1));
992
        FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
974 993
    }
975 994
    while (snr_offset + 4 <= 1023 &&
976
           bit_alloc(s, s->bap1, snr_offset + 4) <= bits_left) {
995
           bit_alloc(s, snr_offset + 4) <= bits_left) {
977 996
        snr_offset += 4;
978
        memcpy(s->bap, s->bap1, sizeof(s->bap1));
997
        FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
979 998
    }
980 999
    while (snr_offset + 1 <= 1023 &&
981
           bit_alloc(s, s->bap1, snr_offset + 1) <= bits_left) {
1000
           bit_alloc(s, snr_offset + 1) <= bits_left) {
982 1001
        snr_offset++;
983
        memcpy(s->bap, s->bap1, sizeof(s->bap1));
1002
        FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
984 1003
    }
1004
    FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1005
    reset_block_bap(s);
985 1006

  
986 1007
    s->coarse_snr_offset = snr_offset >> 4;
987 1008
    for (ch = 0; ch < s->channels; ch++)
......
1160 1181

  
1161 1182
        for (ch = 0; ch < s->channels; ch++) {
1162 1183
            quantize_mantissas_blk_ch(s, block->mdct_coef[ch], block->exp_shift[ch],
1163
                                      block->encoded_exp[ch], s->bap[blk][ch],
1184
                                      block->encoded_exp[ch], block->bap[ch],
1164 1185
                                      block->qmant[ch], s->nb_coefs[ch]);
1165 1186
        }
1166 1187
    }
......
1299 1320
        int b, q;
1300 1321
        for (i = 0; i < s->nb_coefs[ch]; i++) {
1301 1322
            q = block->qmant[ch][i];
1302
            b = s->bap[block_num][ch][i];
1323
            b = block->bap[ch][i];
1303 1324
            switch (b) {
1304 1325
            case 0:                                         break;
1305 1326
            case 1: if (q != 128) put_bits(&s->pb,   5, q); break;
......
1443 1464
 */
1444 1465
static av_cold int ac3_encode_close(AVCodecContext *avctx)
1445 1466
{
1467
    int blk;
1468
    AC3EncodeContext *s = avctx->priv_data;
1469

  
1470
    av_freep(&s->bap_buffer);
1471
    av_freep(&s->bap1_buffer);
1472
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1473
        AC3Block *block = &s->blocks[blk];
1474
        av_freep(&block->bap);
1475
    }
1476

  
1446 1477
    av_freep(&avctx->coded_frame);
1447 1478
    return 0;
1448 1479
}
......
1573 1604
}
1574 1605

  
1575 1606

  
1607
static av_cold int allocate_buffers(AVCodecContext *avctx)
1608
{
1609
    int blk;
1610
    AC3EncodeContext *s = avctx->priv_data;
1611

  
1612
    FF_ALLOC_OR_GOTO(avctx, s->bap_buffer,  AC3_MAX_BLOCKS * s->channels *
1613
                     AC3_MAX_COEFS * sizeof(*s->bap_buffer),  alloc_fail);
1614
    FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, AC3_MAX_BLOCKS * s->channels *
1615
                     AC3_MAX_COEFS * sizeof(*s->bap1_buffer), alloc_fail);
1616
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1617
        AC3Block *block = &s->blocks[blk];
1618
        FF_ALLOC_OR_GOTO(avctx, block->bap, s->channels * sizeof(*block->bap),
1619
                         alloc_fail);
1620
    }
1621
    s->blocks[0].bap[0] = NULL;
1622
    reset_block_bap(s);
1623

  
1624
    return 0;
1625
alloc_fail:
1626
    return AVERROR(ENOMEM);
1627
}
1628

  
1629

  
1576 1630
/**
1577 1631
 * Initialize the encoder.
1578 1632
 */
......
1603 1657

  
1604 1658
    mdct_init(9);
1605 1659

  
1660
    ret = allocate_buffers(avctx);
1661
    if (ret) {
1662
        ac3_encode_close(avctx);
1663
        return ret;
1664
    }
1665

  
1606 1666
    avctx->coded_frame= avcodec_alloc_frame();
1607 1667

  
1608 1668
    return 0;

Also available in: Unified diff