- init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
-
- put_bits(&s->pb, 16, 0x0b77); /* frame header */
- put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
- put_bits(&s->pb, 2, s->bit_alloc.sr_code);
- put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min));
- put_bits(&s->pb, 5, s->bitstream_id);
- put_bits(&s->pb, 3, s->bitstream_mode);
- put_bits(&s->pb, 3, s->channel_mode);
- if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
- put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
- if (s->channel_mode & 0x04)
- put_bits(&s->pb, 2, 1); /* XXX -6 dB */
- if (s->channel_mode == AC3_CHMODE_STEREO)
- put_bits(&s->pb, 2, 0); /* surround not indicated */
- put_bits(&s->pb, 1, s->lfe_on); /* LFE */
- put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */
- put_bits(&s->pb, 1, 0); /* no compression control word */
- put_bits(&s->pb, 1, 0); /* no lang code */
- put_bits(&s->pb, 1, 0); /* no audio production info */
- put_bits(&s->pb, 1, 0); /* no copyright */
- put_bits(&s->pb, 1, 1); /* original bitstream */
- put_bits(&s->pb, 1, 0); /* no time code 1 */
- put_bits(&s->pb, 1, 0); /* no time code 2 */
- put_bits(&s->pb, 1, 0); /* no additional bit stream info */
-}
+ int blk, ch;
+ int frame_bits = 0;
+
+ for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+ uint8_t *exp_strategy = s->blocks[blk].exp_strategy;
+ for (ch = 0; ch < s->fbw_channels; ch++) {
+ if (exp_strategy[ch] != EXP_REUSE)
+ frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
+ }
+ }
+ s->frame_bits = s->frame_bits_fixed + frame_bits;
+}
+
+
+/**
+ * Calculate the number of bits needed to encode a set of mantissas.
+ */
+static int compute_mantissa_size(int mant_cnt[5], uint8_t *bap, int nb_coefs)
+{
+ int bits, b, i;
+
+ bits = 0;
+ for (i = 0; i < nb_coefs; i++) {
+ b = bap[i];
+ if (b <= 4) {
+ // bap=1 to bap=4 will be counted in compute_mantissa_size_final
+ mant_cnt[b]++;
+ } else if (b <= 13) {
+ // bap=5 to bap=13 use (bap-1) bits
+ bits += b - 1;
+ } else {
+ // bap=14 uses 14 bits and bap=15 uses 16 bits
+ bits += (b == 14) ? 14 : 16;
+ }
+ }
+ return bits;
+}
+
+
+/**
+ * Finalize the mantissa bit count by adding in the grouped mantissas.
+ */
+static int compute_mantissa_size_final(int mant_cnt[5])
+{
+ // bap=1 : 3 mantissas in 5 bits
+ int bits = (mant_cnt[1] / 3) * 5;
+ // bap=2 : 3 mantissas in 7 bits
+ // bap=4 : 2 mantissas in 7 bits
+ bits += ((mant_cnt[2] / 3) + (mant_cnt[4] >> 1)) * 7;
+ // bap=3 : each mantissa is 3 bits
+ bits += mant_cnt[3] * 3;
+ return bits;
+}
+
+
+/**
+ * Calculate masking curve based on the final exponents.
+ * Also calculate the power spectral densities to use in future calculations.
+ */
+static void bit_alloc_masking(AC3EncodeContext *s)
+{
+ int blk, ch;
+
+ for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+ AC3Block *block = &s->blocks[blk];
+ for (ch = 0; ch < s->channels; ch++) {
+ /* We only need psd and mask for calculating bap.
+ Since we currently do not calculate bap when exponent
+ strategy is EXP_REUSE we do not need to calculate psd or mask. */
+ if (block->exp_strategy[ch] != EXP_REUSE) {
+ ff_ac3_bit_alloc_calc_psd(block->exp[ch], 0,
+ s->nb_coefs[ch],
+ block->psd[ch], block->band_psd[ch]);
+ ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
+ 0, s->nb_coefs[ch],
+ ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
+ ch == s->lfe_channel,
+ DBA_NONE, 0, NULL, NULL, NULL,
+ block->mask[ch]);
+ }
+ }
+ }
+}
+
+
+/**
+ * Ensure that bap for each block and channel point to the current bap_buffer.
+ * They may have been switched during the bit allocation search.
+ */
+static void reset_block_bap(AC3EncodeContext *s)
+{
+ int blk, ch;
+ if (s->blocks[0].bap[0] == s->bap_buffer)
+ return;
+ for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+ for (ch = 0; ch < s->channels; ch++) {
+ s->blocks[blk].bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
+ }
+ }
+}
+
+
+/**
+ * Run the bit allocation with a given SNR offset.
+ * This calculates the bit allocation pointers that will be used to determine
+ * the quantization of each mantissa.
+ * @return the number of bits needed for mantissas if the given SNR offset is
+ * is used.
+ */
+static int bit_alloc(AC3EncodeContext *s, int snr_offset)
+{
+ int blk, ch;
+ int mantissa_bits;
+ int mant_cnt[5];
+
+ snr_offset = (snr_offset - 240) << 2;
+
+ reset_block_bap(s);
+ mantissa_bits = 0;
+ for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+ AC3Block *block = &s->blocks[blk];
+ // initialize grouped mantissa counts. these are set so that they are
+ // padded to the next whole group size when bits are counted in
+ // compute_mantissa_size_final
+ mant_cnt[0] = mant_cnt[3] = 0;
+ mant_cnt[1] = mant_cnt[2] = 2;
+ mant_cnt[4] = 1;
+ for (ch = 0; ch < s->channels; ch++) {
+ /* Currently the only bit allocation parameters which vary across
+ blocks within a frame are the exponent values. We can take
+ advantage of that by reusing the bit allocation pointers
+ whenever we reuse exponents. */
+ if (block->exp_strategy[ch] == EXP_REUSE) {
+ memcpy(block->bap[ch], s->blocks[blk-1].bap[ch], AC3_MAX_COEFS);
+ } else {
+ ff_ac3_bit_alloc_calc_bap(block->mask[ch], block->psd[ch], 0,
+ s->nb_coefs[ch], snr_offset,
+ s->bit_alloc.floor, ff_ac3_bap_tab,
+ block->bap[ch]);
+ }
+ mantissa_bits += compute_mantissa_size(mant_cnt, block->bap[ch], s->nb_coefs[ch]);
+ }
+ mantissa_bits += compute_mantissa_size_final(mant_cnt);
+ }
+ return mantissa_bits;
+}
+
+
+/**
+ * Constant bitrate bit allocation search.
+ * Find the largest SNR offset that will allow data to fit in the frame.
+ */
+static int cbr_bit_allocation(AC3EncodeContext *s)
+{
+ int ch;
+ int bits_left;
+ int snr_offset, snr_incr;
+
+ bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
+
+ snr_offset = s->coarse_snr_offset << 4;
+
+ while (snr_offset >= 0 &&
+ bit_alloc(s, snr_offset) > bits_left) {
+ snr_offset -= 64;
+ }
+ if (snr_offset < 0)
+ return AVERROR(EINVAL);
+
+ FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
+ for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
+ while (snr_offset + 64 <= 1023 &&
+ bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
+ snr_offset += snr_incr;
+ FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
+ }
+ }
+ FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
+ reset_block_bap(s);
+
+ s->coarse_snr_offset = snr_offset >> 4;
+ for (ch = 0; ch < s->channels; ch++)
+ s->fine_snr_offset[ch] = snr_offset & 0xF;
+
+ return 0;
+}
+
+
+/**
+ * Downgrade exponent strategies to reduce the bits used by the exponents.
+ * This is a fallback for when bit allocation fails with the normal exponent
+ * strategies. Each time this function is run it only downgrades the
+ * strategy in 1 channel of 1 block.
+ * @return non-zero if downgrade was unsuccessful
+ */
+static int downgrade_exponents(AC3EncodeContext *s)
+{
+ int ch, blk;
+
+ for (ch = 0; ch < s->fbw_channels; ch++) {
+ for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
+ if (s->blocks[blk].exp_strategy[ch] == EXP_D15) {
+ s->blocks[blk].exp_strategy[ch] = EXP_D25;
+ return 0;
+ }
+ }
+ }
+ for (ch = 0; ch < s->fbw_channels; ch++) {
+ for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
+ if (s->blocks[blk].exp_strategy[ch] == EXP_D25) {
+ s->blocks[blk].exp_strategy[ch] = EXP_D45;
+ return 0;
+ }
+ }
+ }
+ for (ch = 0; ch < s->fbw_channels; ch++) {
+ /* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if
+ the block number > 0 */
+ for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) {
+ if (s->blocks[blk].exp_strategy[ch] > EXP_REUSE) {
+ s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
+ return 0;
+ }
+ }
+ }
+ return -1;
+}
+
+
+/**
+ * Reduce the bandwidth to reduce the number of bits used for a given SNR offset.
+ * This is a second fallback for when bit allocation still fails after exponents
+ * have been downgraded.
+ * @return non-zero if bandwidth reduction was unsuccessful
+ */
+static int reduce_bandwidth(AC3EncodeContext *s, int min_bw_code)
+{
+ int ch;
+
+ if (s->bandwidth_code[0] > min_bw_code) {
+ for (ch = 0; ch < s->fbw_channels; ch++) {
+ s->bandwidth_code[ch]--;
+ s->nb_coefs[ch] = s->bandwidth_code[ch] * 3 + 73;
+ }
+ return 0;
+ }
+ return -1;
+}
+
+
+/**
+ * Perform bit allocation search.
+ * Finds the SNR offset value that maximizes quality and fits in the specified
+ * frame size. Output is the SNR offset and a set of bit allocation pointers
+ * used to quantize the mantissas.
+ */
+static int compute_bit_allocation(AC3EncodeContext *s)
+{
+ int ret;
+
+ count_frame_bits(s);
+
+ bit_alloc_masking(s);
+
+ ret = cbr_bit_allocation(s);
+ while (ret) {
+ /* fallback 1: downgrade exponents */
+ if (!downgrade_exponents(s)) {
+ extract_exponents(s);
+ encode_exponents(s);
+ group_exponents(s);
+ ret = compute_bit_allocation(s);
+ continue;
+ }
+
+ /* fallback 2: reduce bandwidth */
+ /* only do this if the user has not specified a specific cutoff
+ frequency */
+ if (!s->cutoff && !reduce_bandwidth(s, 0)) {
+ process_exponents(s);
+ ret = compute_bit_allocation(s);
+ continue;
+ }
+
+ /* fallbacks were not enough... */
+ break;
+ }
+
+ return ret;
+}