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