/src/ffmpeg/libavcodec/eac3enc.c
Line | Count | Source |
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 "libavutil/thread.h" |
31 | | #include "ac3enc.h" |
32 | | #include "codec_internal.h" |
33 | | #include "eac3enc.h" |
34 | | #include "eac3_data.h" |
35 | | #include "put_bits.h" |
36 | | |
37 | | |
38 | | static const AVClass eac3enc_class = { |
39 | | .class_name = "E-AC-3 Encoder", |
40 | | .item_name = av_default_item_name, |
41 | | .option = &ff_ac3_enc_options[2], /* First two options are AC-3 only. */ |
42 | | .version = LIBAVUTIL_VERSION_INT, |
43 | | }; |
44 | | |
45 | | /** |
46 | | * LUT for finding a matching frame exponent strategy index from a set of |
47 | | * exponent strategies for a single channel across all 6 blocks. |
48 | | */ |
49 | | static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4]; |
50 | | |
51 | | |
52 | | /** |
53 | | * Initialize E-AC-3 exponent tables. |
54 | | */ |
55 | | static av_cold void eac3_exponent_init(void) |
56 | 0 | { |
57 | 0 | int i; |
58 | |
|
59 | 0 | memset(eac3_frame_expstr_index_tab, -1, sizeof(eac3_frame_expstr_index_tab)); |
60 | 0 | for (i = 0; i < 32; i++) { |
61 | 0 | eac3_frame_expstr_index_tab[ff_eac3_frm_expstr[i][0]-1] |
62 | 0 | [ff_eac3_frm_expstr[i][1]] |
63 | 0 | [ff_eac3_frm_expstr[i][2]] |
64 | 0 | [ff_eac3_frm_expstr[i][3]] |
65 | 0 | [ff_eac3_frm_expstr[i][4]] |
66 | 0 | [ff_eac3_frm_expstr[i][5]] = i; |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | |
71 | | void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s) |
72 | 0 | { |
73 | 0 | int ch; |
74 | |
|
75 | 0 | if (s->num_blocks < 6) { |
76 | 0 | s->use_frame_exp_strategy = 0; |
77 | 0 | return; |
78 | 0 | } |
79 | | |
80 | 0 | s->use_frame_exp_strategy = 1; |
81 | 0 | for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) { |
82 | 0 | int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1] |
83 | 0 | [s->exp_strategy[ch][1]] |
84 | 0 | [s->exp_strategy[ch][2]] |
85 | 0 | [s->exp_strategy[ch][3]] |
86 | 0 | [s->exp_strategy[ch][4]] |
87 | 0 | [s->exp_strategy[ch][5]]; |
88 | 0 | if (expstr < 0) { |
89 | 0 | s->use_frame_exp_strategy = 0; |
90 | 0 | break; |
91 | 0 | } |
92 | 0 | s->frame_exp_strategy[ch] = expstr; |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | | |
97 | | |
98 | | void ff_eac3_set_cpl_states(AC3EncodeContext *s) |
99 | 0 | { |
100 | 0 | int ch, blk; |
101 | 0 | int first_cpl_coords[AC3_MAX_CHANNELS]; |
102 | | |
103 | | /* set first cpl coords */ |
104 | 0 | for (ch = 1; ch <= s->fbw_channels; ch++) |
105 | 0 | first_cpl_coords[ch] = 1; |
106 | 0 | for (blk = 0; blk < s->num_blocks; blk++) { |
107 | 0 | AC3Block *block = &s->blocks[blk]; |
108 | 0 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
109 | 0 | if (block->channel_in_cpl[ch]) { |
110 | 0 | if (first_cpl_coords[ch]) { |
111 | 0 | block->new_cpl_coords[ch] = 2; |
112 | 0 | first_cpl_coords[ch] = 0; |
113 | 0 | } |
114 | 0 | } else { |
115 | 0 | first_cpl_coords[ch] = 1; |
116 | 0 | } |
117 | 0 | } |
118 | 0 | } |
119 | | |
120 | | /* set first cpl leak */ |
121 | 0 | for (blk = 0; blk < s->num_blocks; blk++) { |
122 | 0 | AC3Block *block = &s->blocks[blk]; |
123 | 0 | if (block->cpl_in_use) { |
124 | 0 | block->new_cpl_leak = 2; |
125 | 0 | break; |
126 | 0 | } |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | | /** |
131 | | * Write the E-AC-3 frame header to the output bitstream. |
132 | | */ |
133 | | static void eac3_output_frame_header(AC3EncodeContext *s, PutBitContext *pb) |
134 | 0 | { |
135 | 0 | int blk, ch; |
136 | 0 | AC3EncOptions *opt = &s->options; |
137 | |
|
138 | 0 | put_bits_assume_flushed(pb); |
139 | |
|
140 | 0 | put_bits(pb, 16, 0x0b77); /* sync word */ |
141 | | |
142 | | /* BSI header */ |
143 | 0 | put_bits(pb, 2, 0); /* stream type = independent */ |
144 | 0 | put_bits(pb, 3, 0); /* substream id = 0 */ |
145 | 0 | put_bits(pb, 11, (s->frame_size / 2) - 1); /* frame size */ |
146 | 0 | put_bits(pb, 2, s->bit_alloc.sr_code); /* sample rate code */ |
147 | 0 | put_bits(pb, 2, s->num_blks_code); /* number of blocks */ |
148 | 0 | put_bits(pb, 3, s->channel_mode); /* audio coding mode */ |
149 | 0 | put_bits(pb, 1, s->lfe_on); /* LFE channel indicator */ |
150 | 0 | put_bits(pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */ |
151 | 0 | put_bits(pb, 5, -opt->dialogue_level); /* dialogue normalization level */ |
152 | 0 | put_bits(pb, 1, 0); /* no compression gain */ |
153 | | /* mixing metadata*/ |
154 | 0 | put_bits(pb, 1, opt->eac3_mixing_metadata); |
155 | 0 | if (opt->eac3_mixing_metadata) { |
156 | 0 | if (s->channel_mode > AC3_CHMODE_STEREO) |
157 | 0 | put_bits(pb, 2, opt->preferred_stereo_downmix); |
158 | 0 | if (s->has_center) { |
159 | 0 | put_bits(pb, 3, s->ltrt_center_mix_level); |
160 | 0 | put_bits(pb, 3, s->loro_center_mix_level); |
161 | 0 | } |
162 | 0 | if (s->has_surround) { |
163 | 0 | put_bits(pb, 3, s->ltrt_surround_mix_level); |
164 | 0 | put_bits(pb, 3, s->loro_surround_mix_level); |
165 | 0 | } |
166 | 0 | if (s->lfe_on) |
167 | 0 | put_bits(pb, 1, 0); |
168 | 0 | put_bits(pb, 1, 0); /* no program scale */ |
169 | 0 | put_bits(pb, 1, 0); /* no ext program scale */ |
170 | 0 | put_bits(pb, 2, 0); /* no mixing parameters */ |
171 | 0 | if (s->channel_mode < AC3_CHMODE_STEREO) |
172 | 0 | put_bits(pb, 1, 0); /* no pan info */ |
173 | 0 | put_bits(pb, 1, 0); /* no frame mix config info */ |
174 | 0 | } |
175 | | /* info metadata*/ |
176 | 0 | put_bits(pb, 1, opt->eac3_info_metadata); |
177 | 0 | if (opt->eac3_info_metadata) { |
178 | 0 | put_bits(pb, 3, s->bitstream_mode); |
179 | 0 | put_bits(pb, 1, opt->copyright); |
180 | 0 | put_bits(pb, 1, opt->original); |
181 | 0 | if (s->channel_mode == AC3_CHMODE_STEREO) { |
182 | 0 | put_bits(pb, 2, opt->dolby_surround_mode); |
183 | 0 | put_bits(pb, 2, opt->dolby_headphone_mode); |
184 | 0 | } |
185 | 0 | if (s->channel_mode >= AC3_CHMODE_2F2R) |
186 | 0 | put_bits(pb, 2, opt->dolby_surround_ex_mode); |
187 | 0 | put_bits(pb, 1, opt->audio_production_info); |
188 | 0 | if (opt->audio_production_info) { |
189 | 0 | put_bits(pb, 5, opt->mixing_level - 80); |
190 | 0 | put_bits(pb, 2, opt->room_type); |
191 | 0 | put_bits(pb, 1, opt->ad_converter_type); |
192 | 0 | } |
193 | 0 | put_bits(pb, 1, 0); |
194 | 0 | } |
195 | 0 | if (s->num_blocks != 6) |
196 | 0 | put_bits(pb, 1, !(s->avctx->frame_num % 6)); /* converter sync flag */ |
197 | 0 | put_bits(pb, 1, 0); /* no additional bit stream info */ |
198 | | |
199 | | /* frame header */ |
200 | 0 | if (s->num_blocks == 6) { |
201 | 0 | put_bits(pb, 1, !s->use_frame_exp_strategy); /* exponent strategy syntax */ |
202 | 0 | put_bits(pb, 1, 0); /* aht enabled = no */ |
203 | 0 | } |
204 | 0 | put_bits(pb, 2, 0); /* snr offset strategy = 1 */ |
205 | 0 | put_bits(pb, 1, 0); /* transient pre-noise processing enabled = no */ |
206 | 0 | put_bits(pb, 1, 0); /* block switch syntax enabled = no */ |
207 | 0 | put_bits(pb, 1, 0); /* dither flag syntax enabled = no */ |
208 | 0 | put_bits(pb, 1, 0); /* bit allocation model syntax enabled = no */ |
209 | 0 | put_bits(pb, 1, 0); /* fast gain codes enabled = no */ |
210 | 0 | put_bits(pb, 1, 0); /* dba syntax enabled = no */ |
211 | 0 | put_bits(pb, 1, 0); /* skip field syntax enabled = no */ |
212 | 0 | put_bits(pb, 1, 0); /* spx enabled = no */ |
213 | | /* coupling strategy use flags */ |
214 | 0 | if (s->channel_mode > AC3_CHMODE_MONO) { |
215 | 0 | put_bits(pb, 1, s->blocks[0].cpl_in_use); |
216 | 0 | for (blk = 1; blk < s->num_blocks; blk++) { |
217 | 0 | AC3Block *block = &s->blocks[blk]; |
218 | 0 | put_bits(pb, 1, block->new_cpl_strategy); |
219 | 0 | if (block->new_cpl_strategy) |
220 | 0 | put_bits(pb, 1, block->cpl_in_use); |
221 | 0 | } |
222 | 0 | } |
223 | | /* exponent strategy */ |
224 | 0 | if (s->use_frame_exp_strategy) { |
225 | 0 | for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) |
226 | 0 | put_bits(pb, 5, s->frame_exp_strategy[ch]); |
227 | 0 | } else { |
228 | 0 | for (blk = 0; blk < s->num_blocks; blk++) |
229 | 0 | for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) |
230 | 0 | put_bits(pb, 2, s->exp_strategy[ch][blk]); |
231 | 0 | } |
232 | 0 | if (s->lfe_on) { |
233 | 0 | for (blk = 0; blk < s->num_blocks; blk++) |
234 | 0 | put_bits(pb, 1, s->exp_strategy[s->lfe_channel][blk]); |
235 | 0 | } |
236 | | /* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */ |
237 | 0 | if (s->num_blocks != 6) { |
238 | 0 | put_bits(pb, 1, 0); |
239 | 0 | } else { |
240 | 0 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
241 | 0 | if (s->use_frame_exp_strategy) |
242 | 0 | put_bits(pb, 5, s->frame_exp_strategy[ch]); |
243 | 0 | else |
244 | 0 | put_bits(pb, 5, 0); |
245 | 0 | } |
246 | 0 | } |
247 | | /* snr offsets */ |
248 | 0 | put_bits(pb, 6, s->coarse_snr_offset); |
249 | 0 | put_bits(pb, 4, s->fine_snr_offset[1]); |
250 | | /* block start info */ |
251 | 0 | if (s->num_blocks > 1) |
252 | 0 | put_bits(pb, 1, 0); |
253 | 0 | } |
254 | | |
255 | | static av_cold int eac3_encode_init(AVCodecContext *avctx) |
256 | 0 | { |
257 | 0 | static AVOnce init_static_once = AV_ONCE_INIT; |
258 | 0 | AC3EncodeContext *s = avctx->priv_data; |
259 | |
|
260 | 0 | s->eac3 = 1; |
261 | 0 | s->output_frame_header = eac3_output_frame_header; |
262 | |
|
263 | 0 | ff_thread_once(&init_static_once, eac3_exponent_init); |
264 | |
|
265 | 0 | return ff_ac3_float_encode_init(avctx); |
266 | 0 | } |
267 | | |
268 | | const FFCodec ff_eac3_encoder = { |
269 | | .p.name = "eac3", |
270 | | CODEC_LONG_NAME("ATSC A/52 E-AC-3"), |
271 | | .p.type = AVMEDIA_TYPE_AUDIO, |
272 | | .p.id = AV_CODEC_ID_EAC3, |
273 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
274 | | .priv_data_size = sizeof(AC3EncodeContext), |
275 | | .init = eac3_encode_init, |
276 | | FF_CODEC_ENCODE_CB(ff_ac3_encode_frame), |
277 | | .close = ff_ac3_encode_close, |
278 | | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP), |
279 | | .p.priv_class = &eac3enc_class, |
280 | | CODEC_SAMPLERATES_ARRAY(ff_ac3_sample_rate_tab), |
281 | | CODEC_CH_LAYOUTS_ARRAY(ff_ac3_ch_layouts), |
282 | | .defaults = ff_ac3_enc_defaults, |
283 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
284 | | }; |