3 * Copyright (c) 2011 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #define CONFIG_AC3ENC_FLOAT 1
29 #include "libavutil/attributes.h"
32 #include "eac3_data.h"
35 #define AC3ENC_TYPE AC3ENC_TYPE_EAC3
36 #include "ac3enc_opts_template.c"
38 static const AVClass eac3enc_class = {
39 .class_name = "E-AC-3 Encoder",
40 .item_name = av_default_item_name,
41 .option = ac3_options,
42 .version = LIBAVUTIL_VERSION_INT,
47 * LUT for finding a matching frame exponent strategy index from a set of
48 * exponent strategies for a single channel across all 6 blocks.
50 static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
53 av_cold void ff_eac3_exponent_init(void)
57 memset(eac3_frame_expstr_index_tab, -1, sizeof(eac3_frame_expstr_index_tab));
58 for (i = 0; i < 32; i++) {
59 eac3_frame_expstr_index_tab[ff_eac3_frm_expstr[i][0]-1]
60 [ff_eac3_frm_expstr[i][1]]
61 [ff_eac3_frm_expstr[i][2]]
62 [ff_eac3_frm_expstr[i][3]]
63 [ff_eac3_frm_expstr[i][4]]
64 [ff_eac3_frm_expstr[i][5]] = i;
69 void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
73 if (s->num_blocks < 6) {
74 s->use_frame_exp_strategy = 0;
78 s->use_frame_exp_strategy = 1;
79 for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
80 int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
81 [s->exp_strategy[ch][1]]
82 [s->exp_strategy[ch][2]]
83 [s->exp_strategy[ch][3]]
84 [s->exp_strategy[ch][4]]
85 [s->exp_strategy[ch][5]];
87 s->use_frame_exp_strategy = 0;
90 s->frame_exp_strategy[ch] = expstr;
96 void ff_eac3_set_cpl_states(AC3EncodeContext *s)
99 int first_cpl_coords[AC3_MAX_CHANNELS];
101 /* set first cpl coords */
102 for (ch = 1; ch <= s->fbw_channels; ch++)
103 first_cpl_coords[ch] = 1;
104 for (blk = 0; blk < s->num_blocks; blk++) {
105 AC3Block *block = &s->blocks[blk];
106 for (ch = 1; ch <= s->fbw_channels; ch++) {
107 if (block->channel_in_cpl[ch]) {
108 if (first_cpl_coords[ch]) {
109 block->new_cpl_coords[ch] = 2;
110 first_cpl_coords[ch] = 0;
113 first_cpl_coords[ch] = 1;
118 /* set first cpl leak */
119 for (blk = 0; blk < s->num_blocks; blk++) {
120 AC3Block *block = &s->blocks[blk];
121 if (block->cpl_in_use) {
122 block->new_cpl_leak = 2;
129 void ff_eac3_output_frame_header(AC3EncodeContext *s)
132 AC3EncOptions *opt = &s->options;
134 put_bits(&s->pb, 16, 0x0b77); /* sync word */
137 put_bits(&s->pb, 2, 0); /* stream type = independent */
138 put_bits(&s->pb, 3, 0); /* substream id = 0 */
139 put_bits(&s->pb, 11, (s->frame_size / 2) - 1); /* frame size */
140 if (s->bit_alloc.sr_shift) {
141 put_bits(&s->pb, 2, 0x3); /* fscod2 */
142 put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
144 put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
145 put_bits(&s->pb, 2, s->num_blks_code); /* number of blocks */
147 put_bits(&s->pb, 3, s->channel_mode); /* audio coding mode */
148 put_bits(&s->pb, 1, s->lfe_on); /* LFE channel indicator */
149 put_bits(&s->pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
150 put_bits(&s->pb, 5, -opt->dialogue_level); /* dialogue normalization level */
151 put_bits(&s->pb, 1, 0); /* no compression gain */
153 put_bits(&s->pb, 1, opt->eac3_mixing_metadata);
154 if (opt->eac3_mixing_metadata) {
155 if (s->channel_mode > AC3_CHMODE_STEREO)
156 put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
158 put_bits(&s->pb, 3, s->ltrt_center_mix_level);
159 put_bits(&s->pb, 3, s->loro_center_mix_level);
161 if (s->has_surround) {
162 put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
163 put_bits(&s->pb, 3, s->loro_surround_mix_level);
166 put_bits(&s->pb, 1, 0);
167 put_bits(&s->pb, 1, 0); /* no program scale */
168 put_bits(&s->pb, 1, 0); /* no ext program scale */
169 put_bits(&s->pb, 2, 0); /* no mixing parameters */
170 if (s->channel_mode < AC3_CHMODE_STEREO)
171 put_bits(&s->pb, 1, 0); /* no pan info */
172 put_bits(&s->pb, 1, 0); /* no frame mix config info */
175 put_bits(&s->pb, 1, opt->eac3_info_metadata);
176 if (opt->eac3_info_metadata) {
177 put_bits(&s->pb, 3, s->bitstream_mode);
178 put_bits(&s->pb, 1, opt->copyright);
179 put_bits(&s->pb, 1, opt->original);
180 if (s->channel_mode == AC3_CHMODE_STEREO) {
181 put_bits(&s->pb, 2, opt->dolby_surround_mode);
182 put_bits(&s->pb, 2, opt->dolby_headphone_mode);
184 if (s->channel_mode >= AC3_CHMODE_2F2R)
185 put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
186 put_bits(&s->pb, 1, opt->audio_production_info);
187 if (opt->audio_production_info) {
188 put_bits(&s->pb, 5, opt->mixing_level - 80);
189 put_bits(&s->pb, 2, opt->room_type);
190 put_bits(&s->pb, 1, opt->ad_converter_type);
192 put_bits(&s->pb, 1, 0);
194 if (s->num_blocks != 6)
195 put_bits(&s->pb, 1, !(s->avctx->frame_number % 6)); /* converter sync flag */
196 put_bits(&s->pb, 1, 0); /* no additional bit stream info */
199 if (s->num_blocks == 6) {
200 put_bits(&s->pb, 1, !s->use_frame_exp_strategy);/* exponent strategy syntax */
201 put_bits(&s->pb, 1, 0); /* aht enabled = no */
203 put_bits(&s->pb, 2, 0); /* snr offset strategy = 1 */
204 put_bits(&s->pb, 1, 0); /* transient pre-noise processing enabled = no */
205 put_bits(&s->pb, 1, 0); /* block switch syntax enabled = no */
206 put_bits(&s->pb, 1, 0); /* dither flag syntax enabled = no */
207 put_bits(&s->pb, 1, 0); /* bit allocation model syntax enabled = no */
208 put_bits(&s->pb, 1, 0); /* fast gain codes enabled = no */
209 put_bits(&s->pb, 1, 0); /* dba syntax enabled = no */
210 put_bits(&s->pb, 1, 0); /* skip field syntax enabled = no */
211 put_bits(&s->pb, 1, 0); /* spx enabled = no */
212 /* coupling strategy use flags */
213 if (s->channel_mode > AC3_CHMODE_MONO) {
214 put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
215 for (blk = 1; blk < s->num_blocks; blk++) {
216 AC3Block *block = &s->blocks[blk];
217 put_bits(&s->pb, 1, block->new_cpl_strategy);
218 if (block->new_cpl_strategy)
219 put_bits(&s->pb, 1, block->cpl_in_use);
222 /* exponent strategy */
223 if (s->use_frame_exp_strategy) {
224 for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
225 put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
227 for (blk = 0; blk < s->num_blocks; blk++)
228 for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
229 put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
232 for (blk = 0; blk < s->num_blocks; blk++)
233 put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
235 /* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */
236 if (s->num_blocks != 6) {
237 put_bits(&s->pb, 1, 0);
239 for (ch = 1; ch <= s->fbw_channels; ch++) {
240 if (s->use_frame_exp_strategy)
241 put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
243 put_bits(&s->pb, 5, 0);
247 put_bits(&s->pb, 6, s->coarse_snr_offset);
248 put_bits(&s->pb, 4, s->fine_snr_offset[1]);
249 /* block start info */
250 if (s->num_blocks > 1)
251 put_bits(&s->pb, 1, 0);
255 AVCodec ff_eac3_encoder = {
257 .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
258 .type = AVMEDIA_TYPE_AUDIO,
259 .id = AV_CODEC_ID_EAC3,
260 .priv_data_size = sizeof(AC3EncodeContext),
261 .init = ff_ac3_float_encode_init,
262 .encode2 = ff_ac3_float_encode_frame,
263 .close = ff_ac3_encode_close,
264 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
265 AV_SAMPLE_FMT_NONE },
266 .priv_class = &eac3enc_class,
267 .channel_layouts = ff_ac3_channel_layouts,
268 .defaults = ac3_defaults,