1 /*
2 * E-AC-3 encoder
3 * Copyright (c) 2011 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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.
11 *
12 * FFmpeg 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * E-AC-3 encoder
25 */
26
27 #define AC3ENC_FLOAT 1
28
29 #include "libavutil/attributes.h"
30 #include "ac3enc.h"
31 #include "eac3enc.h"
32 #include "eac3_data.h"
33
34
35 static const AVClass eac3enc_class = {
36 .class_name = "E-AC-3 Encoder",
37 .item_name = av_default_item_name,
38 .option = &ff_ac3_enc_options[2], /* First two options are AC-3 only. */
39 .version = LIBAVUTIL_VERSION_INT,
40 };
41
42 /**
43 * LUT for finding a matching frame exponent strategy index from a set of
44 * exponent strategies for a single channel across all 6 blocks.
45 */
46 static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
47
48
ff_eac3_exponent_init(void)49 av_cold void ff_eac3_exponent_init(void)
50 {
51 int i;
52
53 memset(eac3_frame_expstr_index_tab, -1, sizeof(eac3_frame_expstr_index_tab));
54 for (i = 0; i < 32; i++) {
55 eac3_frame_expstr_index_tab[ff_eac3_frm_expstr[i][0]-1]
56 [ff_eac3_frm_expstr[i][1]]
57 [ff_eac3_frm_expstr[i][2]]
58 [ff_eac3_frm_expstr[i][3]]
59 [ff_eac3_frm_expstr[i][4]]
60 [ff_eac3_frm_expstr[i][5]] = i;
61 }
62 }
63
64
ff_eac3_get_frame_exp_strategy(AC3EncodeContext * s)65 void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
66 {
67 int ch;
68
69 if (s->num_blocks < 6) {
70 s->use_frame_exp_strategy = 0;
71 return;
72 }
73
74 s->use_frame_exp_strategy = 1;
75 for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
76 int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
77 [s->exp_strategy[ch][1]]
78 [s->exp_strategy[ch][2]]
79 [s->exp_strategy[ch][3]]
80 [s->exp_strategy[ch][4]]
81 [s->exp_strategy[ch][5]];
82 if (expstr < 0) {
83 s->use_frame_exp_strategy = 0;
84 break;
85 }
86 s->frame_exp_strategy[ch] = expstr;
87 }
88 }
89
90
91
ff_eac3_set_cpl_states(AC3EncodeContext * s)92 void ff_eac3_set_cpl_states(AC3EncodeContext *s)
93 {
94 int ch, blk;
95 int first_cpl_coords[AC3_MAX_CHANNELS];
96
97 /* set first cpl coords */
98 for (ch = 1; ch <= s->fbw_channels; ch++)
99 first_cpl_coords[ch] = 1;
100 for (blk = 0; blk < s->num_blocks; blk++) {
101 AC3Block *block = &s->blocks[blk];
102 for (ch = 1; ch <= s->fbw_channels; ch++) {
103 if (block->channel_in_cpl[ch]) {
104 if (first_cpl_coords[ch]) {
105 block->new_cpl_coords[ch] = 2;
106 first_cpl_coords[ch] = 0;
107 }
108 } else {
109 first_cpl_coords[ch] = 1;
110 }
111 }
112 }
113
114 /* set first cpl leak */
115 for (blk = 0; blk < s->num_blocks; blk++) {
116 AC3Block *block = &s->blocks[blk];
117 if (block->cpl_in_use) {
118 block->new_cpl_leak = 2;
119 break;
120 }
121 }
122 }
123
124
ff_eac3_output_frame_header(AC3EncodeContext * s)125 void ff_eac3_output_frame_header(AC3EncodeContext *s)
126 {
127 int blk, ch;
128 AC3EncOptions *opt = &s->options;
129
130 put_bits(&s->pb, 16, 0x0b77); /* sync word */
131
132 /* BSI header */
133 put_bits(&s->pb, 2, 0); /* stream type = independent */
134 put_bits(&s->pb, 3, 0); /* substream id = 0 */
135 put_bits(&s->pb, 11, (s->frame_size / 2) - 1); /* frame size */
136 if (s->bit_alloc.sr_shift) {
137 put_bits(&s->pb, 2, 0x3); /* fscod2 */
138 put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
139 } else {
140 put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
141 put_bits(&s->pb, 2, s->num_blks_code); /* number of blocks */
142 }
143 put_bits(&s->pb, 3, s->channel_mode); /* audio coding mode */
144 put_bits(&s->pb, 1, s->lfe_on); /* LFE channel indicator */
145 put_bits(&s->pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
146 put_bits(&s->pb, 5, -opt->dialogue_level); /* dialogue normalization level */
147 put_bits(&s->pb, 1, 0); /* no compression gain */
148 /* mixing metadata*/
149 put_bits(&s->pb, 1, opt->eac3_mixing_metadata);
150 if (opt->eac3_mixing_metadata) {
151 if (s->channel_mode > AC3_CHMODE_STEREO)
152 put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
153 if (s->has_center) {
154 put_bits(&s->pb, 3, s->ltrt_center_mix_level);
155 put_bits(&s->pb, 3, s->loro_center_mix_level);
156 }
157 if (s->has_surround) {
158 put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
159 put_bits(&s->pb, 3, s->loro_surround_mix_level);
160 }
161 if (s->lfe_on)
162 put_bits(&s->pb, 1, 0);
163 put_bits(&s->pb, 1, 0); /* no program scale */
164 put_bits(&s->pb, 1, 0); /* no ext program scale */
165 put_bits(&s->pb, 2, 0); /* no mixing parameters */
166 if (s->channel_mode < AC3_CHMODE_STEREO)
167 put_bits(&s->pb, 1, 0); /* no pan info */
168 put_bits(&s->pb, 1, 0); /* no frame mix config info */
169 }
170 /* info metadata*/
171 put_bits(&s->pb, 1, opt->eac3_info_metadata);
172 if (opt->eac3_info_metadata) {
173 put_bits(&s->pb, 3, s->bitstream_mode);
174 put_bits(&s->pb, 1, opt->copyright);
175 put_bits(&s->pb, 1, opt->original);
176 if (s->channel_mode == AC3_CHMODE_STEREO) {
177 put_bits(&s->pb, 2, opt->dolby_surround_mode);
178 put_bits(&s->pb, 2, opt->dolby_headphone_mode);
179 }
180 if (s->channel_mode >= AC3_CHMODE_2F2R)
181 put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
182 put_bits(&s->pb, 1, opt->audio_production_info);
183 if (opt->audio_production_info) {
184 put_bits(&s->pb, 5, opt->mixing_level - 80);
185 put_bits(&s->pb, 2, opt->room_type);
186 put_bits(&s->pb, 1, opt->ad_converter_type);
187 }
188 put_bits(&s->pb, 1, 0);
189 }
190 if (s->num_blocks != 6)
191 put_bits(&s->pb, 1, !(s->avctx->frame_number % 6)); /* converter sync flag */
192 put_bits(&s->pb, 1, 0); /* no additional bit stream info */
193
194 /* frame header */
195 if (s->num_blocks == 6) {
196 put_bits(&s->pb, 1, !s->use_frame_exp_strategy);/* exponent strategy syntax */
197 put_bits(&s->pb, 1, 0); /* aht enabled = no */
198 }
199 put_bits(&s->pb, 2, 0); /* snr offset strategy = 1 */
200 put_bits(&s->pb, 1, 0); /* transient pre-noise processing enabled = no */
201 put_bits(&s->pb, 1, 0); /* block switch syntax enabled = no */
202 put_bits(&s->pb, 1, 0); /* dither flag syntax enabled = no */
203 put_bits(&s->pb, 1, 0); /* bit allocation model syntax enabled = no */
204 put_bits(&s->pb, 1, 0); /* fast gain codes enabled = no */
205 put_bits(&s->pb, 1, 0); /* dba syntax enabled = no */
206 put_bits(&s->pb, 1, 0); /* skip field syntax enabled = no */
207 put_bits(&s->pb, 1, 0); /* spx enabled = no */
208 /* coupling strategy use flags */
209 if (s->channel_mode > AC3_CHMODE_MONO) {
210 put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
211 for (blk = 1; blk < s->num_blocks; blk++) {
212 AC3Block *block = &s->blocks[blk];
213 put_bits(&s->pb, 1, block->new_cpl_strategy);
214 if (block->new_cpl_strategy)
215 put_bits(&s->pb, 1, block->cpl_in_use);
216 }
217 }
218 /* exponent strategy */
219 if (s->use_frame_exp_strategy) {
220 for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
221 put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
222 } else {
223 for (blk = 0; blk < s->num_blocks; blk++)
224 for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
225 put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
226 }
227 if (s->lfe_on) {
228 for (blk = 0; blk < s->num_blocks; blk++)
229 put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
230 }
231 /* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */
232 if (s->num_blocks != 6) {
233 put_bits(&s->pb, 1, 0);
234 } else {
235 for (ch = 1; ch <= s->fbw_channels; ch++) {
236 if (s->use_frame_exp_strategy)
237 put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
238 else
239 put_bits(&s->pb, 5, 0);
240 }
241 }
242 /* snr offsets */
243 put_bits(&s->pb, 6, s->coarse_snr_offset);
244 put_bits(&s->pb, 4, s->fine_snr_offset[1]);
245 /* block start info */
246 if (s->num_blocks > 1)
247 put_bits(&s->pb, 1, 0);
248 }
249
250
251 AVCodec ff_eac3_encoder = {
252 .name = "eac3",
253 .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
254 .type = AVMEDIA_TYPE_AUDIO,
255 .id = AV_CODEC_ID_EAC3,
256 .priv_data_size = sizeof(AC3EncodeContext),
257 .init = ff_ac3_float_encode_init,
258 .encode2 = ff_ac3_float_encode_frame,
259 .close = ff_ac3_encode_close,
260 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
261 AV_SAMPLE_FMT_NONE },
262 .priv_class = &eac3enc_class,
263 .supported_samplerates = ff_ac3_sample_rate_tab,
264 .channel_layouts = ff_ac3_channel_layouts,
265 .defaults = ff_ac3_enc_defaults,
266 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
267 };
268