avcodec/ac3enc: Move transient PutBitContext to stack

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2024-05-17 19:21:34 +02:00
parent e863cbceae
commit 59b1838e09
3 changed files with 159 additions and 157 deletions

View file

@ -1634,63 +1634,63 @@ static void ac3_quantize_mantissas(AC3EncodeContext *s)
/* /*
* Write the AC-3 frame header to the output bitstream. * Write the AC-3 frame header to the output bitstream.
*/ */
static void ac3_output_frame_header(AC3EncodeContext *s) static void ac3_output_frame_header(AC3EncodeContext *s, PutBitContext *pb)
{ {
AC3EncOptions *opt = &s->options; AC3EncOptions *opt = &s->options;
put_bits(&s->pb, 16, 0x0b77); /* frame header */ put_bits(pb, 16, 0x0b77); /* frame header */
put_bits(&s->pb, 16, 0); /* crc1: will be filled later */ put_bits(pb, 16, 0); /* crc1: will be filled later */
put_bits(&s->pb, 2, s->bit_alloc.sr_code); put_bits(pb, 2, s->bit_alloc.sr_code);
put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min) / 2); put_bits(pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
put_bits(&s->pb, 5, s->bitstream_id); put_bits(pb, 5, s->bitstream_id);
put_bits(&s->pb, 3, s->bitstream_mode); put_bits(pb, 3, s->bitstream_mode);
put_bits(&s->pb, 3, s->channel_mode); put_bits(pb, 3, s->channel_mode);
if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO) if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
put_bits(&s->pb, 2, s->center_mix_level); put_bits(pb, 2, s->center_mix_level);
if (s->channel_mode & 0x04) if (s->channel_mode & 0x04)
put_bits(&s->pb, 2, s->surround_mix_level); put_bits(pb, 2, s->surround_mix_level);
if (s->channel_mode == AC3_CHMODE_STEREO) if (s->channel_mode == AC3_CHMODE_STEREO)
put_bits(&s->pb, 2, opt->dolby_surround_mode); put_bits(pb, 2, opt->dolby_surround_mode);
put_bits(&s->pb, 1, s->lfe_on); /* LFE */ put_bits(pb, 1, s->lfe_on); /* LFE */
put_bits(&s->pb, 5, -opt->dialogue_level); put_bits(pb, 5, -opt->dialogue_level);
put_bits(&s->pb, 1, 0); /* no compression control word */ put_bits(pb, 1, 0); /* no compression control word */
put_bits(&s->pb, 1, 0); /* no lang code */ put_bits(pb, 1, 0); /* no lang code */
put_bits(&s->pb, 1, opt->audio_production_info); put_bits(pb, 1, opt->audio_production_info);
if (opt->audio_production_info) { if (opt->audio_production_info) {
put_bits(&s->pb, 5, opt->mixing_level - 80); put_bits(pb, 5, opt->mixing_level - 80);
put_bits(&s->pb, 2, opt->room_type); put_bits(pb, 2, opt->room_type);
} }
put_bits(&s->pb, 1, opt->copyright); put_bits(pb, 1, opt->copyright);
put_bits(&s->pb, 1, opt->original); put_bits(pb, 1, opt->original);
if (s->bitstream_id == 6) { if (s->bitstream_id == 6) {
/* alternate bit stream syntax */ /* alternate bit stream syntax */
put_bits(&s->pb, 1, opt->extended_bsi_1); put_bits(pb, 1, opt->extended_bsi_1);
if (opt->extended_bsi_1) { if (opt->extended_bsi_1) {
put_bits(&s->pb, 2, opt->preferred_stereo_downmix); put_bits(pb, 2, opt->preferred_stereo_downmix);
put_bits(&s->pb, 3, s->ltrt_center_mix_level); put_bits(pb, 3, s->ltrt_center_mix_level);
put_bits(&s->pb, 3, s->ltrt_surround_mix_level); put_bits(pb, 3, s->ltrt_surround_mix_level);
put_bits(&s->pb, 3, s->loro_center_mix_level); put_bits(pb, 3, s->loro_center_mix_level);
put_bits(&s->pb, 3, s->loro_surround_mix_level); put_bits(pb, 3, s->loro_surround_mix_level);
} }
put_bits(&s->pb, 1, opt->extended_bsi_2); put_bits(pb, 1, opt->extended_bsi_2);
if (opt->extended_bsi_2) { if (opt->extended_bsi_2) {
put_bits(&s->pb, 2, opt->dolby_surround_ex_mode); put_bits(pb, 2, opt->dolby_surround_ex_mode);
put_bits(&s->pb, 2, opt->dolby_headphone_mode); put_bits(pb, 2, opt->dolby_headphone_mode);
put_bits(&s->pb, 1, opt->ad_converter_type); put_bits(pb, 1, opt->ad_converter_type);
put_bits(&s->pb, 9, 0); /* xbsi2 and encinfo : reserved */ put_bits(pb, 9, 0); /* xbsi2 and encinfo : reserved */
} }
} else { } else {
put_bits(&s->pb, 1, 0); /* no time code 1 */ put_bits(pb, 1, 0); /* no time code 1 */
put_bits(&s->pb, 1, 0); /* no time code 2 */ put_bits(pb, 1, 0); /* no time code 2 */
} }
put_bits(&s->pb, 1, 0); /* no additional bit stream info */ put_bits(pb, 1, 0); /* no additional bit stream info */
} }
/* /*
* Write one audio block to the output bitstream. * Write one audio block to the output bitstream.
*/ */
static void output_audio_block(AC3EncodeContext *s, int blk) static void output_audio_block(AC3EncodeContext *s, PutBitContext *pb, int blk)
{ {
int ch, i, baie, bnd, got_cpl, av_uninit(ch0); int ch, i, baie, bnd, got_cpl, av_uninit(ch0);
AC3Block *block = &s->blocks[blk]; AC3Block *block = &s->blocks[blk];
@ -1698,48 +1698,48 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
/* block switching */ /* block switching */
if (!s->eac3) { if (!s->eac3) {
for (ch = 0; ch < s->fbw_channels; ch++) for (ch = 0; ch < s->fbw_channels; ch++)
put_bits(&s->pb, 1, 0); put_bits(pb, 1, 0);
} }
/* dither flags */ /* dither flags */
if (!s->eac3) { if (!s->eac3) {
for (ch = 0; ch < s->fbw_channels; ch++) for (ch = 0; ch < s->fbw_channels; ch++)
put_bits(&s->pb, 1, 1); put_bits(pb, 1, 1);
} }
/* dynamic range codes */ /* dynamic range codes */
put_bits(&s->pb, 1, 0); put_bits(pb, 1, 0);
/* spectral extension */ /* spectral extension */
if (s->eac3) if (s->eac3)
put_bits(&s->pb, 1, 0); put_bits(pb, 1, 0);
/* channel coupling */ /* channel coupling */
if (!s->eac3) if (!s->eac3)
put_bits(&s->pb, 1, block->new_cpl_strategy); put_bits(pb, 1, block->new_cpl_strategy);
if (block->new_cpl_strategy) { if (block->new_cpl_strategy) {
if (!s->eac3) if (!s->eac3)
put_bits(&s->pb, 1, block->cpl_in_use); put_bits(pb, 1, block->cpl_in_use);
if (block->cpl_in_use) { if (block->cpl_in_use) {
int start_sub, end_sub; int start_sub, end_sub;
if (s->eac3) if (s->eac3)
put_bits(&s->pb, 1, 0); /* enhanced coupling */ put_bits(pb, 1, 0); /* enhanced coupling */
if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO) { if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO) {
for (ch = 1; ch <= s->fbw_channels; ch++) for (ch = 1; ch <= s->fbw_channels; ch++)
put_bits(&s->pb, 1, block->channel_in_cpl[ch]); put_bits(pb, 1, block->channel_in_cpl[ch]);
} }
if (s->channel_mode == AC3_CHMODE_STEREO) if (s->channel_mode == AC3_CHMODE_STEREO)
put_bits(&s->pb, 1, 0); /* phase flags in use */ put_bits(pb, 1, 0); /* phase flags in use */
start_sub = (s->start_freq[CPL_CH] - 37) / 12; start_sub = (s->start_freq[CPL_CH] - 37) / 12;
end_sub = (s->cpl_end_freq - 37) / 12; end_sub = (s->cpl_end_freq - 37) / 12;
put_bits(&s->pb, 4, start_sub); put_bits(pb, 4, start_sub);
put_bits(&s->pb, 4, end_sub - 3); put_bits(pb, 4, end_sub - 3);
/* coupling band structure */ /* coupling band structure */
if (s->eac3) { if (s->eac3) {
put_bits(&s->pb, 1, 0); /* use default */ put_bits(pb, 1, 0); /* use default */
} else { } else {
for (bnd = start_sub+1; bnd < end_sub; bnd++) for (bnd = start_sub+1; bnd < end_sub; bnd++)
put_bits(&s->pb, 1, ff_eac3_default_cpl_band_struct[bnd]); put_bits(pb, 1, ff_eac3_default_cpl_band_struct[bnd]);
} }
} }
} }
@ -1749,12 +1749,12 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
for (ch = 1; ch <= s->fbw_channels; ch++) { for (ch = 1; ch <= s->fbw_channels; ch++) {
if (block->channel_in_cpl[ch]) { if (block->channel_in_cpl[ch]) {
if (!s->eac3 || block->new_cpl_coords[ch] != 2) if (!s->eac3 || block->new_cpl_coords[ch] != 2)
put_bits(&s->pb, 1, block->new_cpl_coords[ch]); put_bits(pb, 1, block->new_cpl_coords[ch]);
if (block->new_cpl_coords[ch]) { if (block->new_cpl_coords[ch]) {
put_bits(&s->pb, 2, block->cpl_master_exp[ch]); put_bits(pb, 2, block->cpl_master_exp[ch]);
for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
put_bits(&s->pb, 4, block->cpl_coord_exp [ch][bnd]); put_bits(pb, 4, block->cpl_coord_exp [ch][bnd]);
put_bits(&s->pb, 4, block->cpl_coord_mant[ch][bnd]); put_bits(pb, 4, block->cpl_coord_mant[ch][bnd]);
} }
} }
} }
@ -1764,26 +1764,26 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
/* stereo rematrixing */ /* stereo rematrixing */
if (s->channel_mode == AC3_CHMODE_STEREO) { if (s->channel_mode == AC3_CHMODE_STEREO) {
if (!s->eac3 || blk > 0) if (!s->eac3 || blk > 0)
put_bits(&s->pb, 1, block->new_rematrixing_strategy); put_bits(pb, 1, block->new_rematrixing_strategy);
if (block->new_rematrixing_strategy) { if (block->new_rematrixing_strategy) {
/* rematrixing flags */ /* rematrixing flags */
for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++)
put_bits(&s->pb, 1, block->rematrixing_flags[bnd]); put_bits(pb, 1, block->rematrixing_flags[bnd]);
} }
} }
/* exponent strategy */ /* exponent strategy */
if (!s->eac3) { if (!s->eac3) {
for (ch = !block->cpl_in_use; ch <= s->fbw_channels; ch++) for (ch = !block->cpl_in_use; ch <= s->fbw_channels; ch++)
put_bits(&s->pb, 2, s->exp_strategy[ch][blk]); put_bits(pb, 2, s->exp_strategy[ch][blk]);
if (s->lfe_on) if (s->lfe_on)
put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]); put_bits(pb, 1, s->exp_strategy[s->lfe_channel][blk]);
} }
/* bandwidth */ /* bandwidth */
for (ch = 1; ch <= s->fbw_channels; ch++) { for (ch = 1; ch <= s->fbw_channels; ch++) {
if (s->exp_strategy[ch][blk] != EXP_REUSE && !block->channel_in_cpl[ch]) if (s->exp_strategy[ch][blk] != EXP_REUSE && !block->channel_in_cpl[ch])
put_bits(&s->pb, 6, s->bandwidth_code); put_bits(pb, 6, s->bandwidth_code);
} }
/* exponents */ /* exponents */
@ -1795,58 +1795,58 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
continue; continue;
/* DC exponent */ /* DC exponent */
put_bits(&s->pb, 4, block->grouped_exp[ch][0] >> cpl); put_bits(pb, 4, block->grouped_exp[ch][0] >> cpl);
/* exponent groups */ /* exponent groups */
nb_groups = exponent_group_tab[cpl][s->exp_strategy[ch][blk]-1][block->end_freq[ch]-s->start_freq[ch]]; nb_groups = exponent_group_tab[cpl][s->exp_strategy[ch][blk]-1][block->end_freq[ch]-s->start_freq[ch]];
for (i = 1; i <= nb_groups; i++) for (i = 1; i <= nb_groups; i++)
put_bits(&s->pb, 7, block->grouped_exp[ch][i]); put_bits(pb, 7, block->grouped_exp[ch][i]);
/* gain range info */ /* gain range info */
if (ch != s->lfe_channel && !cpl) if (ch != s->lfe_channel && !cpl)
put_bits(&s->pb, 2, 0); put_bits(pb, 2, 0);
} }
/* bit allocation info */ /* bit allocation info */
if (!s->eac3) { if (!s->eac3) {
baie = (blk == 0); baie = (blk == 0);
put_bits(&s->pb, 1, baie); put_bits(pb, 1, baie);
if (baie) { if (baie) {
put_bits(&s->pb, 2, s->slow_decay_code); put_bits(pb, 2, s->slow_decay_code);
put_bits(&s->pb, 2, s->fast_decay_code); put_bits(pb, 2, s->fast_decay_code);
put_bits(&s->pb, 2, s->slow_gain_code); put_bits(pb, 2, s->slow_gain_code);
put_bits(&s->pb, 2, s->db_per_bit_code); put_bits(pb, 2, s->db_per_bit_code);
put_bits(&s->pb, 3, s->floor_code); put_bits(pb, 3, s->floor_code);
} }
} }
/* snr offset */ /* snr offset */
if (!s->eac3) { if (!s->eac3) {
put_bits(&s->pb, 1, block->new_snr_offsets); put_bits(pb, 1, block->new_snr_offsets);
if (block->new_snr_offsets) { if (block->new_snr_offsets) {
put_bits(&s->pb, 6, s->coarse_snr_offset); put_bits(pb, 6, s->coarse_snr_offset);
for (ch = !block->cpl_in_use; ch <= s->channels; ch++) { for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
put_bits(&s->pb, 4, s->fine_snr_offset[ch]); put_bits(pb, 4, s->fine_snr_offset[ch]);
put_bits(&s->pb, 3, s->fast_gain_code[ch]); put_bits(pb, 3, s->fast_gain_code[ch]);
} }
} }
} else { } else {
put_bits(&s->pb, 1, 0); /* no converter snr offset */ put_bits(pb, 1, 0); /* no converter snr offset */
} }
/* coupling leak */ /* coupling leak */
if (block->cpl_in_use) { if (block->cpl_in_use) {
if (!s->eac3 || block->new_cpl_leak != 2) if (!s->eac3 || block->new_cpl_leak != 2)
put_bits(&s->pb, 1, block->new_cpl_leak); put_bits(pb, 1, block->new_cpl_leak);
if (block->new_cpl_leak) { if (block->new_cpl_leak) {
put_bits(&s->pb, 3, s->bit_alloc.cpl_fast_leak); put_bits(pb, 3, s->bit_alloc.cpl_fast_leak);
put_bits(&s->pb, 3, s->bit_alloc.cpl_slow_leak); put_bits(pb, 3, s->bit_alloc.cpl_slow_leak);
} }
} }
if (!s->eac3) { if (!s->eac3) {
put_bits(&s->pb, 1, 0); /* no delta bit allocation */ put_bits(pb, 1, 0); /* no delta bit allocation */
put_bits(&s->pb, 1, 0); /* no data to skip */ put_bits(pb, 1, 0); /* no data to skip */
} }
/* mantissas */ /* mantissas */
@ -1864,13 +1864,13 @@ static void output_audio_block(AC3EncodeContext *s, int blk)
b = s->ref_bap[ch][blk][i]; b = s->ref_bap[ch][blk][i];
switch (b) { switch (b) {
case 0: break; case 0: break;
case 1: if (q != 128) put_bits (&s->pb, 5, q); break; case 1: if (q != 128) put_bits (pb, 5, q); break;
case 2: if (q != 128) put_bits (&s->pb, 7, q); break; case 2: if (q != 128) put_bits (pb, 7, q); break;
case 3: put_sbits(&s->pb, 3, q); break; case 3: put_sbits(pb, 3, q); break;
case 4: if (q != 128) put_bits (&s->pb, 7, q); break; case 4: if (q != 128) put_bits (pb, 7, q); break;
case 14: put_sbits(&s->pb, 14, q); break; case 14: put_sbits(pb, 14, q); break;
case 15: put_sbits(&s->pb, 16, q); break; case 15: put_sbits(pb, 16, q); break;
default: put_sbits(&s->pb, b-1, q); break; default: put_sbits(pb, b-1, q); break;
} }
} }
if (ch == CPL_CH) if (ch == CPL_CH)
@ -1917,7 +1917,7 @@ static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
/* /*
* Fill the end of the frame with 0's and compute the two CRCs. * Fill the end of the frame with 0's and compute the two CRCs.
*/ */
static void output_frame_end(AC3EncodeContext *s) static void output_frame_end(AC3EncodeContext *s, PutBitContext *pb)
{ {
const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI); const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
int frame_size_58, pad_bytes, crc1, crc2, crc_inv; int frame_size_58, pad_bytes, crc1, crc2, crc_inv;
@ -1926,13 +1926,13 @@ static void output_frame_end(AC3EncodeContext *s)
frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1; frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
/* pad the remainder of the frame with zeros */ /* pad the remainder of the frame with zeros */
av_assert2(s->frame_size * 8 - put_bits_count(&s->pb) >= 18); av_assert2(s->frame_size * 8 - put_bits_count(pb) >= 18);
flush_put_bits(&s->pb); flush_put_bits(pb);
frame = s->pb.buf; frame = pb->buf;
pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2; pad_bytes = s->frame_size - (put_bits_ptr(pb) - frame) - 2;
av_assert2(pad_bytes >= 0); av_assert2(pad_bytes >= 0);
if (pad_bytes > 0) if (pad_bytes > 0)
memset(put_bits_ptr(&s->pb), 0, pad_bytes); memset(put_bits_ptr(pb), 0, pad_bytes);
if (s->eac3) { if (s->eac3) {
/* compute crc2 */ /* compute crc2 */
@ -1969,16 +1969,17 @@ static void output_frame_end(AC3EncodeContext *s)
*/ */
static void ac3_output_frame(AC3EncodeContext *s, unsigned char *frame) static void ac3_output_frame(AC3EncodeContext *s, unsigned char *frame)
{ {
PutBitContext pb;
int blk; int blk;
init_put_bits(&s->pb, frame, s->frame_size); init_put_bits(&pb, frame, s->frame_size);
s->output_frame_header(s); s->output_frame_header(s, &pb);
for (blk = 0; blk < s->num_blocks; blk++) for (blk = 0; blk < s->num_blocks; blk++)
output_audio_block(s, blk); output_audio_block(s, &pb, blk);
output_frame_end(s); output_frame_end(s, &pb);
} }
int ff_ac3_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, int ff_ac3_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,

View file

@ -41,7 +41,6 @@
#include "codec_internal.h" #include "codec_internal.h"
#include "mathops.h" #include "mathops.h"
#include "me_cmp.h" #include "me_cmp.h"
#include "put_bits.h"
#include "audiodsp.h" #include "audiodsp.h"
#ifndef AC3ENC_FLOAT #ifndef AC3ENC_FLOAT
@ -151,6 +150,8 @@ typedef struct AC3Block {
int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant) int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
} AC3Block; } AC3Block;
struct PutBitContext;
/** /**
* AC-3 encoder private context. * AC-3 encoder private context.
*/ */
@ -158,7 +159,6 @@ typedef struct AC3EncodeContext {
AVClass *av_class; ///< AVClass used for AVOption AVClass *av_class; ///< AVClass used for AVOption
AC3EncOptions options; ///< encoding options AC3EncOptions options; ///< encoding options
AVCodecContext *avctx; ///< parent AVCodecContext AVCodecContext *avctx; ///< parent AVCodecContext
PutBitContext pb; ///< bitstream writer context
AudioDSPContext adsp; AudioDSPContext adsp;
#if AC3ENC_FLOAT #if AC3ENC_FLOAT
AVFloatDSPContext *fdsp; AVFloatDSPContext *fdsp;
@ -256,7 +256,7 @@ typedef struct AC3EncodeContext {
void (*encode_frame)(struct AC3EncodeContext *s, uint8_t * const *samples); void (*encode_frame)(struct AC3EncodeContext *s, uint8_t * const *samples);
/* AC-3 vs. E-AC-3 function pointers */ /* AC-3 vs. E-AC-3 function pointers */
void (*output_frame_header)(struct AC3EncodeContext *s); void (*output_frame_header)(struct AC3EncodeContext *s, struct PutBitContext *pb);
union { union {
DECLARE_ALIGNED(32, float, mdct_window_float)[AC3_BLOCK_SIZE]; DECLARE_ALIGNED(32, float, mdct_window_float)[AC3_BLOCK_SIZE];

View file

@ -32,6 +32,7 @@
#include "codec_internal.h" #include "codec_internal.h"
#include "eac3enc.h" #include "eac3enc.h"
#include "eac3_data.h" #include "eac3_data.h"
#include "put_bits.h"
static const AVClass eac3enc_class = { static const AVClass eac3enc_class = {
@ -129,124 +130,124 @@ void ff_eac3_set_cpl_states(AC3EncodeContext *s)
/** /**
* Write the E-AC-3 frame header to the output bitstream. * Write the E-AC-3 frame header to the output bitstream.
*/ */
static void eac3_output_frame_header(AC3EncodeContext *s) static void eac3_output_frame_header(AC3EncodeContext *s, PutBitContext *pb)
{ {
int blk, ch; int blk, ch;
AC3EncOptions *opt = &s->options; AC3EncOptions *opt = &s->options;
put_bits(&s->pb, 16, 0x0b77); /* sync word */ put_bits(pb, 16, 0x0b77); /* sync word */
/* BSI header */ /* BSI header */
put_bits(&s->pb, 2, 0); /* stream type = independent */ put_bits(pb, 2, 0); /* stream type = independent */
put_bits(&s->pb, 3, 0); /* substream id = 0 */ put_bits(pb, 3, 0); /* substream id = 0 */
put_bits(&s->pb, 11, (s->frame_size / 2) - 1); /* frame size */ put_bits(pb, 11, (s->frame_size / 2) - 1); /* frame size */
put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */ put_bits(pb, 2, s->bit_alloc.sr_code); /* sample rate code */
put_bits(&s->pb, 2, s->num_blks_code); /* number of blocks */ put_bits(pb, 2, s->num_blks_code); /* number of blocks */
put_bits(&s->pb, 3, s->channel_mode); /* audio coding mode */ put_bits(pb, 3, s->channel_mode); /* audio coding mode */
put_bits(&s->pb, 1, s->lfe_on); /* LFE channel indicator */ put_bits(pb, 1, s->lfe_on); /* LFE channel indicator */
put_bits(&s->pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */ put_bits(pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
put_bits(&s->pb, 5, -opt->dialogue_level); /* dialogue normalization level */ put_bits(pb, 5, -opt->dialogue_level); /* dialogue normalization level */
put_bits(&s->pb, 1, 0); /* no compression gain */ put_bits(pb, 1, 0); /* no compression gain */
/* mixing metadata*/ /* mixing metadata*/
put_bits(&s->pb, 1, opt->eac3_mixing_metadata); put_bits(pb, 1, opt->eac3_mixing_metadata);
if (opt->eac3_mixing_metadata) { if (opt->eac3_mixing_metadata) {
if (s->channel_mode > AC3_CHMODE_STEREO) if (s->channel_mode > AC3_CHMODE_STEREO)
put_bits(&s->pb, 2, opt->preferred_stereo_downmix); put_bits(pb, 2, opt->preferred_stereo_downmix);
if (s->has_center) { if (s->has_center) {
put_bits(&s->pb, 3, s->ltrt_center_mix_level); put_bits(pb, 3, s->ltrt_center_mix_level);
put_bits(&s->pb, 3, s->loro_center_mix_level); put_bits(pb, 3, s->loro_center_mix_level);
} }
if (s->has_surround) { if (s->has_surround) {
put_bits(&s->pb, 3, s->ltrt_surround_mix_level); put_bits(pb, 3, s->ltrt_surround_mix_level);
put_bits(&s->pb, 3, s->loro_surround_mix_level); put_bits(pb, 3, s->loro_surround_mix_level);
} }
if (s->lfe_on) if (s->lfe_on)
put_bits(&s->pb, 1, 0); put_bits(pb, 1, 0);
put_bits(&s->pb, 1, 0); /* no program scale */ put_bits(pb, 1, 0); /* no program scale */
put_bits(&s->pb, 1, 0); /* no ext program scale */ put_bits(pb, 1, 0); /* no ext program scale */
put_bits(&s->pb, 2, 0); /* no mixing parameters */ put_bits(pb, 2, 0); /* no mixing parameters */
if (s->channel_mode < AC3_CHMODE_STEREO) if (s->channel_mode < AC3_CHMODE_STEREO)
put_bits(&s->pb, 1, 0); /* no pan info */ put_bits(pb, 1, 0); /* no pan info */
put_bits(&s->pb, 1, 0); /* no frame mix config info */ put_bits(pb, 1, 0); /* no frame mix config info */
} }
/* info metadata*/ /* info metadata*/
put_bits(&s->pb, 1, opt->eac3_info_metadata); put_bits(pb, 1, opt->eac3_info_metadata);
if (opt->eac3_info_metadata) { if (opt->eac3_info_metadata) {
put_bits(&s->pb, 3, s->bitstream_mode); put_bits(pb, 3, s->bitstream_mode);
put_bits(&s->pb, 1, opt->copyright); put_bits(pb, 1, opt->copyright);
put_bits(&s->pb, 1, opt->original); put_bits(pb, 1, opt->original);
if (s->channel_mode == AC3_CHMODE_STEREO) { if (s->channel_mode == AC3_CHMODE_STEREO) {
put_bits(&s->pb, 2, opt->dolby_surround_mode); put_bits(pb, 2, opt->dolby_surround_mode);
put_bits(&s->pb, 2, opt->dolby_headphone_mode); put_bits(pb, 2, opt->dolby_headphone_mode);
} }
if (s->channel_mode >= AC3_CHMODE_2F2R) if (s->channel_mode >= AC3_CHMODE_2F2R)
put_bits(&s->pb, 2, opt->dolby_surround_ex_mode); put_bits(pb, 2, opt->dolby_surround_ex_mode);
put_bits(&s->pb, 1, opt->audio_production_info); put_bits(pb, 1, opt->audio_production_info);
if (opt->audio_production_info) { if (opt->audio_production_info) {
put_bits(&s->pb, 5, opt->mixing_level - 80); put_bits(pb, 5, opt->mixing_level - 80);
put_bits(&s->pb, 2, opt->room_type); put_bits(pb, 2, opt->room_type);
put_bits(&s->pb, 1, opt->ad_converter_type); put_bits(pb, 1, opt->ad_converter_type);
} }
put_bits(&s->pb, 1, 0); put_bits(pb, 1, 0);
} }
if (s->num_blocks != 6) if (s->num_blocks != 6)
put_bits(&s->pb, 1, !(s->avctx->frame_num % 6)); /* converter sync flag */ put_bits(pb, 1, !(s->avctx->frame_num % 6)); /* converter sync flag */
put_bits(&s->pb, 1, 0); /* no additional bit stream info */ put_bits(pb, 1, 0); /* no additional bit stream info */
/* frame header */ /* frame header */
if (s->num_blocks == 6) { if (s->num_blocks == 6) {
put_bits(&s->pb, 1, !s->use_frame_exp_strategy); /* exponent strategy syntax */ put_bits(pb, 1, !s->use_frame_exp_strategy); /* exponent strategy syntax */
put_bits(&s->pb, 1, 0); /* aht enabled = no */ put_bits(pb, 1, 0); /* aht enabled = no */
} }
put_bits(&s->pb, 2, 0); /* snr offset strategy = 1 */ put_bits(pb, 2, 0); /* snr offset strategy = 1 */
put_bits(&s->pb, 1, 0); /* transient pre-noise processing enabled = no */ put_bits(pb, 1, 0); /* transient pre-noise processing enabled = no */
put_bits(&s->pb, 1, 0); /* block switch syntax enabled = no */ put_bits(pb, 1, 0); /* block switch syntax enabled = no */
put_bits(&s->pb, 1, 0); /* dither flag syntax enabled = no */ put_bits(pb, 1, 0); /* dither flag syntax enabled = no */
put_bits(&s->pb, 1, 0); /* bit allocation model syntax enabled = no */ put_bits(pb, 1, 0); /* bit allocation model syntax enabled = no */
put_bits(&s->pb, 1, 0); /* fast gain codes enabled = no */ put_bits(pb, 1, 0); /* fast gain codes enabled = no */
put_bits(&s->pb, 1, 0); /* dba syntax enabled = no */ put_bits(pb, 1, 0); /* dba syntax enabled = no */
put_bits(&s->pb, 1, 0); /* skip field syntax enabled = no */ put_bits(pb, 1, 0); /* skip field syntax enabled = no */
put_bits(&s->pb, 1, 0); /* spx enabled = no */ put_bits(pb, 1, 0); /* spx enabled = no */
/* coupling strategy use flags */ /* coupling strategy use flags */
if (s->channel_mode > AC3_CHMODE_MONO) { if (s->channel_mode > AC3_CHMODE_MONO) {
put_bits(&s->pb, 1, s->blocks[0].cpl_in_use); put_bits(pb, 1, s->blocks[0].cpl_in_use);
for (blk = 1; blk < s->num_blocks; blk++) { for (blk = 1; blk < s->num_blocks; blk++) {
AC3Block *block = &s->blocks[blk]; AC3Block *block = &s->blocks[blk];
put_bits(&s->pb, 1, block->new_cpl_strategy); put_bits(pb, 1, block->new_cpl_strategy);
if (block->new_cpl_strategy) if (block->new_cpl_strategy)
put_bits(&s->pb, 1, block->cpl_in_use); put_bits(pb, 1, block->cpl_in_use);
} }
} }
/* exponent strategy */ /* exponent strategy */
if (s->use_frame_exp_strategy) { if (s->use_frame_exp_strategy) {
for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
put_bits(&s->pb, 5, s->frame_exp_strategy[ch]); put_bits(pb, 5, s->frame_exp_strategy[ch]);
} else { } else {
for (blk = 0; blk < s->num_blocks; blk++) for (blk = 0; blk < s->num_blocks; blk++)
for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
put_bits(&s->pb, 2, s->exp_strategy[ch][blk]); put_bits(pb, 2, s->exp_strategy[ch][blk]);
} }
if (s->lfe_on) { if (s->lfe_on) {
for (blk = 0; blk < s->num_blocks; blk++) for (blk = 0; blk < s->num_blocks; blk++)
put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]); put_bits(pb, 1, s->exp_strategy[s->lfe_channel][blk]);
} }
/* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */ /* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */
if (s->num_blocks != 6) { if (s->num_blocks != 6) {
put_bits(&s->pb, 1, 0); put_bits(pb, 1, 0);
} else { } else {
for (ch = 1; ch <= s->fbw_channels; ch++) { for (ch = 1; ch <= s->fbw_channels; ch++) {
if (s->use_frame_exp_strategy) if (s->use_frame_exp_strategy)
put_bits(&s->pb, 5, s->frame_exp_strategy[ch]); put_bits(pb, 5, s->frame_exp_strategy[ch]);
else else
put_bits(&s->pb, 5, 0); put_bits(pb, 5, 0);
} }
} }
/* snr offsets */ /* snr offsets */
put_bits(&s->pb, 6, s->coarse_snr_offset); put_bits(pb, 6, s->coarse_snr_offset);
put_bits(&s->pb, 4, s->fine_snr_offset[1]); put_bits(pb, 4, s->fine_snr_offset[1]);
/* block start info */ /* block start info */
if (s->num_blocks > 1) if (s->num_blocks > 1)
put_bits(&s->pb, 1, 0); put_bits(pb, 1, 0);
} }
static av_cold int eac3_encode_init(AVCodecContext *avctx) static av_cold int eac3_encode_init(AVCodecContext *avctx)