FFmpeg
ac3enc.c
Go to the documentation of this file.
1 /*
2  * The simplest AC-3 encoder
3  * Copyright (c) 2000 Fabrice Bellard
4  * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
5  * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 /**
25  * @file
26  * The simplest AC-3 encoder.
27  */
28 
29 #include <stdint.h>
30 
31 #include "libavutil/attributes.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/avstring.h"
35 #include "libavutil/crc.h"
36 #include "libavutil/emms.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mem_internal.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/thread.h"
41 #include "avcodec.h"
42 #include "codec_internal.h"
43 #include "config_components.h"
44 #include "encode.h"
45 #include "me_cmp.h"
46 #include "put_bits.h"
47 #include "audiodsp.h"
48 #include "ac3dsp.h"
49 #include "ac3.h"
50 #include "ac3defs.h"
51 #include "ac3tab.h"
52 #include "ac3enc.h"
53 #include "eac3enc.h"
54 
55 typedef struct AC3Mant {
56  int16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4
57  int mant1_cnt, mant2_cnt, mant4_cnt; ///< mantissa counts for bap=1,2,4
58 } AC3Mant;
59 
60 #define CMIXLEV_NUM_OPTIONS 3
61 static const float cmixlev_options[CMIXLEV_NUM_OPTIONS] = {
63 };
64 
65 #define SURMIXLEV_NUM_OPTIONS 3
68 };
69 
70 #define EXTMIXLEV_NUM_OPTIONS 8
74 };
75 
76 /* The first two options apply only to the AC-3 encoders;
77  * the rest is also valid for EAC-3. When modifying it,
78  * it might be necessary to adapt said offset in eac3enc.c. */
79 #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
80 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
82 /* AC-3 downmix levels */
83 {"center_mixlev", "Center Mix Level", OFFSET(center_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = LEVEL_MINUS_4POINT5DB }, 0.0, 1.0, AC3ENC_PARAM},
84 {"surround_mixlev", "Surround Mix Level", OFFSET(surround_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = LEVEL_MINUS_6DB }, 0.0, 1.0, AC3ENC_PARAM},
85 /* audio production information */
86 {"mixing_level", "Mixing Level", OFFSET(mixing_level), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, 111, AC3ENC_PARAM},
87 {"room_type", "Room Type", OFFSET(room_type), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_SMALL_ROOM, AC3ENC_PARAM, .unit = "room_type"},
88  {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "room_type"},
89  {"large", "Large Room", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_LARGE_ROOM }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "room_type"},
90  {"small", "Small Room", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_SMALL_ROOM }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "room_type"},
91 /* Metadata Options */
92 {"per_frame_metadata", "Allow Changing Metadata Per-Frame", OFFSET(allow_per_frame_metadata), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AC3ENC_PARAM},
93 {"copyright", "Copyright Bit", OFFSET(copyright), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, 1, AC3ENC_PARAM},
94 {"dialnorm", "Dialogue Level (dB)", OFFSET(dialogue_level), AV_OPT_TYPE_INT, {.i64 = -31 }, -31, -1, AC3ENC_PARAM},
95 {"dsur_mode", "Dolby Surround Mode", OFFSET(dolby_surround_mode), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_ON, AC3ENC_PARAM, .unit = "dsur_mode"},
96  {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dsur_mode"},
97  {"on", "Dolby Surround Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_ON }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dsur_mode"},
98  {"off", "Not Dolby Surround Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_OFF }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dsur_mode"},
99 {"original", "Original Bit Stream", OFFSET(original), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, 1, AC3ENC_PARAM},
100 /* extended bitstream information */
101 {"dmix_mode", "Preferred Stereo Downmix Mode", OFFSET(preferred_stereo_downmix), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_DOWNMIX_DPLII, AC3ENC_PARAM, .unit = "dmix_mode"},
102  {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dmix_mode"},
103  {"ltrt", "Lt/Rt Downmix Preferred", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_DOWNMIX_LTRT }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dmix_mode"},
104  {"loro", "Lo/Ro Downmix Preferred", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_DOWNMIX_LORO }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dmix_mode"},
105  {"dplii", "Dolby Pro Logic II Downmix Preferred", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_DOWNMIX_DPLII }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dmix_mode"},
106 {"ltrt_cmixlev", "Lt/Rt Center Mix Level", OFFSET(ltrt_center_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
107 {"ltrt_surmixlev", "Lt/Rt Surround Mix Level", OFFSET(ltrt_surround_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
108 {"loro_cmixlev", "Lo/Ro Center Mix Level", OFFSET(loro_center_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
109 {"loro_surmixlev", "Lo/Ro Surround Mix Level", OFFSET(loro_surround_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
110 {"dsurex_mode", "Dolby Surround EX Mode", OFFSET(dolby_surround_ex_mode), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_DSUREX_DPLIIZ, AC3ENC_PARAM, .unit = "dsurex_mode"},
111  {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dsurex_mode"},
112  {"on", "Dolby Surround EX Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_ON }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dsurex_mode"},
113  {"off", "Not Dolby Surround EX Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_OFF }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dsurex_mode"},
114  {"dpliiz", "Dolby Pro Logic IIz-encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_DSUREX_DPLIIZ }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dsurex_mode"},
115 {"dheadphone_mode", "Dolby Headphone Mode", OFFSET(dolby_headphone_mode), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_ON, AC3ENC_PARAM, .unit = "dheadphone_mode"},
116  {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dheadphone_mode"},
117  {"on", "Dolby Headphone Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_ON }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dheadphone_mode"},
118  {"off", "Not Dolby Headphone Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_OFF }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "dheadphone_mode"},
119 {"ad_conv_type", "A/D Converter Type", OFFSET(ad_converter_type), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_ADCONV_HDCD, AC3ENC_PARAM, .unit = "ad_conv_type"},
120  {"standard", "Standard (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_ADCONV_STANDARD }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "ad_conv_type"},
121  {"hdcd", "HDCD", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_ADCONV_HDCD }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "ad_conv_type"},
122 /* Other Encoding Options */
123 {"stereo_rematrixing", "Stereo Rematrixing", OFFSET(stereo_rematrixing), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, AC3ENC_PARAM},
124 {"channel_coupling", "Channel Coupling", OFFSET(channel_coupling), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_AUTO }, AC3ENC_OPT_AUTO, AC3ENC_OPT_ON, AC3ENC_PARAM, .unit = "channel_coupling"},
125  {"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_AUTO }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "channel_coupling"},
126 {"cpl_start_band", "Coupling Start Band", OFFSET(cpl_start), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_AUTO }, AC3ENC_OPT_AUTO, 15, AC3ENC_PARAM, .unit = "cpl_start_band"},
127  {"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_AUTO }, INT_MIN, INT_MAX, AC3ENC_PARAM, .unit = "cpl_start_band"},
128 {NULL}
129 };
130 
132  .class_name = "AC-3 Encoder",
133  .item_name = av_default_item_name,
134  .option = ff_ac3_enc_options,
135  .version = LIBAVUTIL_VERSION_INT,
136 };
137 
139  { "b", "0" },
140  { NULL }
141 };
142 
143 /**
144  * LUT for number of exponent groups.
145  * exponent_group_tab[coupling][exponent strategy-1][number of coefficients]
146  */
147 static uint8_t exponent_group_tab[2][3][256];
148 
149 
150 /**
151  * List of supported channel layouts.
152  */
163  {
164  .nb_channels = 2,
165  .order = AV_CHANNEL_ORDER_NATIVE,
167  },
168  {
169  .nb_channels = 3,
170  .order = AV_CHANNEL_ORDER_NATIVE,
172  },
173  {
174  .nb_channels = 4,
175  .order = AV_CHANNEL_ORDER_NATIVE,
177  },
178  {
179  .nb_channels = 4,
180  .order = AV_CHANNEL_ORDER_NATIVE,
182  },
183  {
184  .nb_channels = 5,
185  .order = AV_CHANNEL_ORDER_NATIVE,
187  },
190  { 0 },
191 };
192 
193 /**
194  * Table to remap channels from SMPTE order to AC-3 order.
195  * [channel_mode][lfe][ch]
196  */
197 static const uint8_t ac3_enc_channel_map[8][2][6] = {
199  { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
200  { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
201 };
202 
203 /**
204  * LUT to select the bandwidth code based on the bit rate, sample rate, and
205  * number of full-bandwidth channels.
206  * bandwidth_tab[fbw_channels-1][sample rate code][bit rate code]
207  */
208 static const uint8_t ac3_bandwidth_tab[5][3][19] = {
209 // 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
210 
211  { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
212  { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
213  { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
214 
215  { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
216  { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
217  { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
218 
219  { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
220  { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
221  { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
222 
223  { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
224  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
225  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
226 
227  { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
228  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
229  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
230 };
231 
232 
233 /**
234  * LUT to select the coupling start band based on the bit rate, sample rate, and
235  * number of full-bandwidth channels. -1 = coupling off
236  * ac3_coupling_start_tab[channel_mode-2][sample rate code][bit rate code]
237  *
238  * TODO: more testing for optimal parameters.
239  * multi-channel tests at 44.1kHz and 32kHz.
240  */
241 static const int8_t ac3_coupling_start_tab[6][3][19] = {
242 // 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
243 
244  // 2/0
245  { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
246  { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
247  { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
248 
249  // 3/0
250  { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
251  { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
252  { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
253 
254  // 2/1 - untested
255  { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
256  { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
257  { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
258 
259  // 3/1
260  { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
261  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
262  { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
263 
264  // 2/2 - untested
265  { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
266  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
267  { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
268 
269  // 3/2
270  { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
271  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
272  { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
273 };
274 
275 
276 /**
277  * Adjust the frame size to make the average bit rate match the target bit rate.
278  * This is only needed for 11025, 22050, and 44100 sample rates or any E-AC-3.
279  *
280  * @param s AC-3 encoder private context
281  */
283 {
284  while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
285  s->bits_written -= s->bit_rate;
286  s->samples_written -= s->sample_rate;
287  }
288  s->frame_size = s->frame_size_min +
289  2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
290  s->bits_written += s->frame_size * 8;
291  s->samples_written += AC3_BLOCK_SIZE * s->num_blocks;
292 }
293 
294 
295 /**
296  * Set the initial coupling strategy parameters prior to coupling analysis.
297  *
298  * @param s AC-3 encoder private context
299  */
301 {
302  int blk, ch;
303  int got_cpl_snr;
304  int num_cpl_blocks;
305 
306  /* set coupling use flags for each block/channel */
307  /* TODO: turn coupling on/off and adjust start band based on bit usage */
308  for (blk = 0; blk < s->num_blocks; blk++) {
309  AC3Block *block = &s->blocks[blk];
310  for (ch = 1; ch <= s->fbw_channels; ch++)
311  block->channel_in_cpl[ch] = s->cpl_on;
312  }
313 
314  /* enable coupling for each block if at least 2 channels have coupling
315  enabled for that block */
316  got_cpl_snr = 0;
317  num_cpl_blocks = 0;
318  for (blk = 0; blk < s->num_blocks; blk++) {
319  AC3Block *block = &s->blocks[blk];
320  block->num_cpl_channels = 0;
321  for (ch = 1; ch <= s->fbw_channels; ch++)
322  block->num_cpl_channels += block->channel_in_cpl[ch];
323  block->cpl_in_use = block->num_cpl_channels > 1;
324  num_cpl_blocks += block->cpl_in_use;
325  if (!block->cpl_in_use) {
326  block->num_cpl_channels = 0;
327  for (ch = 1; ch <= s->fbw_channels; ch++)
328  block->channel_in_cpl[ch] = 0;
329  }
330 
331  block->new_cpl_strategy = !blk;
332  if (blk) {
333  for (ch = 1; ch <= s->fbw_channels; ch++) {
334  if (block->channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]) {
335  block->new_cpl_strategy = 1;
336  break;
337  }
338  }
339  }
340  block->new_cpl_leak = block->new_cpl_strategy;
341 
342  if (!blk || (block->cpl_in_use && !got_cpl_snr)) {
343  block->new_snr_offsets = 1;
344  if (block->cpl_in_use)
345  got_cpl_snr = 1;
346  } else {
347  block->new_snr_offsets = 0;
348  }
349  }
350  if (!num_cpl_blocks)
351  s->cpl_on = 0;
352 
353  /* set bandwidth for each channel */
354  for (blk = 0; blk < s->num_blocks; blk++) {
355  AC3Block *block = &s->blocks[blk];
356  for (ch = 1; ch <= s->fbw_channels; ch++) {
357  if (block->channel_in_cpl[ch])
358  block->end_freq[ch] = s->start_freq[CPL_CH];
359  else
360  block->end_freq[ch] = s->bandwidth_code * 3 + 73;
361  }
362  }
363 }
364 
365 
366 /**
367  * Apply stereo rematrixing to coefficients based on rematrixing flags.
368  *
369  * @param s AC-3 encoder private context
370  */
372 {
373  int nb_coefs;
374  int blk, bnd, i;
375  int start, end;
376  uint8_t *flags = NULL;
377 
378  if (!s->rematrixing_enabled)
379  return;
380 
381  for (blk = 0; blk < s->num_blocks; blk++) {
382  AC3Block *block = &s->blocks[blk];
383  if (block->new_rematrixing_strategy)
384  flags = block->rematrixing_flags;
385  nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
386  for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
387  if (flags[bnd]) {
388  start = ff_ac3_rematrix_band_tab[bnd];
389  end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
390  for (i = start; i < end; i++) {
391  int32_t lt = block->fixed_coef[1][i];
392  int32_t rt = block->fixed_coef[2][i];
393  block->fixed_coef[1][i] = (lt + rt) >> 1;
394  block->fixed_coef[2][i] = (lt - rt) >> 1;
395  }
396  }
397  }
398  }
399 }
400 
401 
402 /*
403  * Initialize exponent tables.
404  */
405 static av_cold void exponent_init(void)
406 {
407  int expstr, i, grpsize;
408 
409  for (expstr = EXP_D15-1; expstr <= EXP_D45-1; expstr++) {
410  grpsize = 3 << expstr;
411  for (i = 12; i < 256; i++) {
412  exponent_group_tab[0][expstr][i] = (i + grpsize - 4) / grpsize;
413  exponent_group_tab[1][expstr][i] = (i ) / grpsize;
414  }
415  }
416  /* LFE */
417  exponent_group_tab[0][0][7] = 2;
418 }
419 
420 
421 /*
422  * Extract exponents from the MDCT coefficients.
423  */
425 {
426  int ch = !s->cpl_on;
427  int chan_size = AC3_MAX_COEFS * s->num_blocks * (s->channels - ch + 1);
428  AC3Block *block = &s->blocks[0];
429 
430  s->ac3dsp.extract_exponents(block->exp[ch], block->fixed_coef[ch], chan_size);
431 }
432 
433 
434 /**
435  * Exponent Difference Threshold.
436  * New exponents are sent if their SAD exceed this number.
437  */
438 #define EXP_DIFF_THRESHOLD 500
439 
440 /**
441  * Table used to select exponent strategy based on exponent reuse block interval.
442  */
443 static const uint8_t exp_strategy_reuse_tab[4][6] = {
448 };
449 
450 /*
451  * Calculate exponent strategies for all channels.
452  * Array arrangement is reversed to simplify the per-channel calculation.
453  */
455 {
456  int ch, blk, blk1;
457 
458  for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
459  uint8_t *exp_strategy = s->exp_strategy[ch];
460  uint8_t *exp = s->blocks[0].exp[ch];
461  int exp_diff;
462 
463  /* estimate if the exponent variation & decide if they should be
464  reused in the next frame */
465  exp_strategy[0] = EXP_NEW;
466  exp += AC3_MAX_COEFS;
467  for (blk = 1; blk < s->num_blocks; blk++, exp += AC3_MAX_COEFS) {
468  if (ch == CPL_CH) {
469  if (!s->blocks[blk-1].cpl_in_use) {
470  exp_strategy[blk] = EXP_NEW;
471  continue;
472  } else if (!s->blocks[blk].cpl_in_use) {
473  exp_strategy[blk] = EXP_REUSE;
474  continue;
475  }
476  } else if (s->blocks[blk].channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]) {
477  exp_strategy[blk] = EXP_NEW;
478  continue;
479  }
480  exp_diff = s->mecc.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
481  exp_strategy[blk] = EXP_REUSE;
482  if (ch == CPL_CH && exp_diff > (EXP_DIFF_THRESHOLD * (s->blocks[blk].end_freq[ch] - s->start_freq[ch]) / AC3_MAX_COEFS))
483  exp_strategy[blk] = EXP_NEW;
484  else if (ch > CPL_CH && exp_diff > EXP_DIFF_THRESHOLD)
485  exp_strategy[blk] = EXP_NEW;
486  }
487 
488  /* now select the encoding strategy type : if exponents are often
489  recoded, we use a coarse encoding */
490  blk = 0;
491  while (blk < s->num_blocks) {
492  blk1 = blk + 1;
493  while (blk1 < s->num_blocks && exp_strategy[blk1] == EXP_REUSE)
494  blk1++;
495  exp_strategy[blk] = exp_strategy_reuse_tab[s->num_blks_code][blk1-blk-1];
496  blk = blk1;
497  }
498  }
499  if (s->lfe_on) {
500  ch = s->lfe_channel;
501  s->exp_strategy[ch][0] = EXP_D15;
502  for (blk = 1; blk < s->num_blocks; blk++)
503  s->exp_strategy[ch][blk] = EXP_REUSE;
504  }
505 
506  /* for E-AC-3, determine frame exponent strategy */
507  if (CONFIG_EAC3_ENCODER && s->eac3)
509 }
510 
511 
512 /**
513  * Update the exponents so that they are the ones the decoder will decode.
514  *
515  * @param[in,out] exp array of exponents for 1 block in 1 channel
516  * @param nb_exps number of exponents in active bandwidth
517  * @param exp_strategy exponent strategy for the block
518  * @param cpl indicates if the block is in the coupling channel
519  */
520 static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy,
521  int cpl)
522 {
523  int nb_groups, i, k;
524 
525  nb_groups = exponent_group_tab[cpl][exp_strategy-1][nb_exps] * 3;
526 
527  /* for each group, compute the minimum exponent */
528  switch(exp_strategy) {
529  case EXP_D25:
530  for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
531  uint8_t exp_min = exp[k];
532  if (exp[k+1] < exp_min)
533  exp_min = exp[k+1];
534  exp[i-cpl] = exp_min;
535  k += 2;
536  }
537  break;
538  case EXP_D45:
539  for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
540  uint8_t exp_min = exp[k];
541  if (exp[k+1] < exp_min)
542  exp_min = exp[k+1];
543  if (exp[k+2] < exp_min)
544  exp_min = exp[k+2];
545  if (exp[k+3] < exp_min)
546  exp_min = exp[k+3];
547  exp[i-cpl] = exp_min;
548  k += 4;
549  }
550  break;
551  }
552 
553  /* constraint for DC exponent */
554  if (!cpl && exp[0] > 15)
555  exp[0] = 15;
556 
557  /* decrease the delta between each groups to within 2 so that they can be
558  differentially encoded */
559  for (i = 1; i <= nb_groups; i++)
560  exp[i] = FFMIN(exp[i], exp[i-1] + 2);
561  i--;
562  while (--i >= 0)
563  exp[i] = FFMIN(exp[i], exp[i+1] + 2);
564 
565  if (cpl)
566  exp[-1] = exp[0] & ~1;
567 
568  /* now we have the exponent values the decoder will see */
569  switch (exp_strategy) {
570  case EXP_D25:
571  for (i = nb_groups, k = (nb_groups * 2)-cpl; i > 0; i--) {
572  uint8_t exp1 = exp[i-cpl];
573  exp[k--] = exp1;
574  exp[k--] = exp1;
575  }
576  break;
577  case EXP_D45:
578  for (i = nb_groups, k = (nb_groups * 4)-cpl; i > 0; i--) {
579  exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i-cpl];
580  k -= 4;
581  }
582  break;
583  }
584 }
585 
586 
587 /*
588  * Encode exponents from original extracted form to what the decoder will see.
589  * This copies and groups exponents based on exponent strategy and reduces
590  * deltas between adjacent exponent groups so that they can be differentially
591  * encoded.
592  */
594 {
595  int blk, blk1, ch, cpl;
596  uint8_t *exp, *exp_strategy;
597  int nb_coefs, num_reuse_blocks;
598 
599  for (ch = !s->cpl_on; ch <= s->channels; ch++) {
600  exp = s->blocks[0].exp[ch] + s->start_freq[ch];
601  exp_strategy = s->exp_strategy[ch];
602 
603  cpl = (ch == CPL_CH);
604  blk = 0;
605  while (blk < s->num_blocks) {
606  AC3Block *block = &s->blocks[blk];
607  if (cpl && !block->cpl_in_use) {
608  exp += AC3_MAX_COEFS;
609  blk++;
610  continue;
611  }
612  nb_coefs = block->end_freq[ch] - s->start_freq[ch];
613  blk1 = blk + 1;
614 
615  /* count the number of EXP_REUSE blocks after the current block
616  and set exponent reference block numbers */
617  s->exp_ref_block[ch][blk] = blk;
618  while (blk1 < s->num_blocks && exp_strategy[blk1] == EXP_REUSE) {
619  s->exp_ref_block[ch][blk1] = blk;
620  blk1++;
621  }
622  num_reuse_blocks = blk1 - blk - 1;
623 
624  /* for the EXP_REUSE case we select the min of the exponents */
625  s->ac3dsp.ac3_exponent_min(exp-s->start_freq[ch], num_reuse_blocks,
626  AC3_MAX_COEFS);
627 
628  encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk], cpl);
629 
630  exp += AC3_MAX_COEFS * (num_reuse_blocks + 1);
631  blk = blk1;
632  }
633  }
634 
635  /* reference block numbers have been changed, so reset ref_bap_set */
636  s->ref_bap_set = 0;
637 }
638 
639 
640 /*
641  * Count exponent bits based on bandwidth, coupling, and exponent strategies.
642  */
644 {
645  int blk, ch;
646  int nb_groups, bit_count;
647 
648  bit_count = 0;
649  for (blk = 0; blk < s->num_blocks; blk++) {
650  AC3Block *block = &s->blocks[blk];
651  for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
652  int exp_strategy = s->exp_strategy[ch][blk];
653  int cpl = (ch == CPL_CH);
654  int nb_coefs = block->end_freq[ch] - s->start_freq[ch];
655 
656  if (exp_strategy == EXP_REUSE)
657  continue;
658 
659  nb_groups = exponent_group_tab[cpl][exp_strategy-1][nb_coefs];
660  bit_count += 4 + (nb_groups * 7);
661  }
662  }
663 
664  return bit_count;
665 }
666 
667 
668 /**
669  * Group exponents.
670  * 3 delta-encoded exponents are in each 7-bit group. The number of groups
671  * varies depending on exponent strategy and bandwidth.
672  *
673  * @param s AC-3 encoder private context
674  */
676 {
677  int blk, ch, i, cpl;
678  int group_size, nb_groups;
679  uint8_t *p;
680  int delta0, delta1, delta2;
681  int exp0, exp1;
682 
683  for (blk = 0; blk < s->num_blocks; blk++) {
684  AC3Block *block = &s->blocks[blk];
685  for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
686  int exp_strategy = s->exp_strategy[ch][blk];
687  if (exp_strategy == EXP_REUSE)
688  continue;
689  cpl = (ch == CPL_CH);
690  group_size = exp_strategy + (exp_strategy == EXP_D45);
691  nb_groups = exponent_group_tab[cpl][exp_strategy-1][block->end_freq[ch]-s->start_freq[ch]];
692  p = block->exp[ch] + s->start_freq[ch] - cpl;
693 
694  /* DC exponent */
695  exp1 = *p++;
696  block->grouped_exp[ch][0] = exp1;
697 
698  /* remaining exponents are delta encoded */
699  for (i = 1; i <= nb_groups; i++) {
700  /* merge three delta in one code */
701  exp0 = exp1;
702  exp1 = p[0];
703  p += group_size;
704  delta0 = exp1 - exp0 + 2;
705  av_assert2(delta0 >= 0 && delta0 <= 4);
706 
707  exp0 = exp1;
708  exp1 = p[0];
709  p += group_size;
710  delta1 = exp1 - exp0 + 2;
711  av_assert2(delta1 >= 0 && delta1 <= 4);
712 
713  exp0 = exp1;
714  exp1 = p[0];
715  p += group_size;
716  delta2 = exp1 - exp0 + 2;
717  av_assert2(delta2 >= 0 && delta2 <= 4);
718 
719  block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
720  }
721  }
722  }
723 }
724 
725 
726 /**
727  * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
728  * Extract exponents from MDCT coefficients, calculate exponent strategies,
729  * and encode final exponents.
730  *
731  * @param s AC-3 encoder private context
732  */
734 {
736 
738 
740 
741  emms_c();
742 }
743 
744 
745 /*
746  * Count frame bits that are based solely on fixed parameters.
747  * This only has to be run once when the encoder is initialized.
748  */
750 {
751  static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
752  int blk;
753  int frame_bits;
754 
755  /* assumptions:
756  * no dynamic range codes
757  * bit allocation parameters do not change between blocks
758  * no delta bit allocation
759  * no skipped data
760  * no auxiliary data
761  * no E-AC-3 metadata
762  */
763 
764  /* header */
765  frame_bits = 16; /* sync info */
766  if (s->eac3) {
767  /* bitstream info header */
768  frame_bits += 35;
769  frame_bits += 1 + 1;
770  if (s->num_blocks != 0x6)
771  frame_bits++;
772  frame_bits++;
773  /* audio frame header */
774  if (s->num_blocks == 6)
775  frame_bits += 2;
776  frame_bits += 10;
777  /* exponent strategy */
778  if (s->use_frame_exp_strategy)
779  frame_bits += 5 * s->fbw_channels;
780  else
781  frame_bits += s->num_blocks * 2 * s->fbw_channels;
782  if (s->lfe_on)
783  frame_bits += s->num_blocks;
784  /* converter exponent strategy */
785  if (s->num_blks_code != 0x3)
786  frame_bits++;
787  else
788  frame_bits += s->fbw_channels * 5;
789  /* snr offsets */
790  frame_bits += 10;
791  /* block start info */
792  if (s->num_blocks != 1)
793  frame_bits++;
794  } else {
795  frame_bits += 49;
796  frame_bits += frame_bits_inc[s->channel_mode];
797  }
798 
799  /* audio blocks */
800  for (blk = 0; blk < s->num_blocks; blk++) {
801  if (!s->eac3) {
802  /* block switch flags */
803  frame_bits += s->fbw_channels;
804 
805  /* dither flags */
806  frame_bits += s->fbw_channels;
807  }
808 
809  /* dynamic range */
810  frame_bits++;
811 
812  /* spectral extension */
813  if (s->eac3)
814  frame_bits++;
815 
816  /* coupling strategy exists: cplstre */
817  if (!s->eac3)
818  frame_bits++;
819 
820  if (!s->eac3) {
821  /* exponent strategy */
822  frame_bits += 2 * s->fbw_channels;
823  if (s->lfe_on)
824  frame_bits++;
825 
826  /* bit allocation params */
827  frame_bits++;
828  if (!blk)
829  frame_bits += 2 + 2 + 2 + 2 + 3;
830  }
831 
832  /* snroffste for AC-3, convsnroffste for E-AC-3 */
833  frame_bits++;
834 
835  if (!s->eac3) {
836  /* delta bit allocation */
837  frame_bits++;
838 
839  /* skipped data */
840  frame_bits++;
841  }
842  }
843 
844  /* auxiliary data */
845  frame_bits++;
846 
847  /* CRC */
848  frame_bits += 1 + 16;
849 
850  s->frame_bits_fixed = frame_bits;
851 }
852 
853 
854 /*
855  * Initialize bit allocation.
856  * Set default parameter codes and calculate parameter values.
857  */
859 {
860  int ch;
861 
862  /* init default parameters */
863  s->slow_decay_code = 2;
864  s->fast_decay_code = 1;
865  s->slow_gain_code = 1;
866  s->db_per_bit_code = s->eac3 ? 2 : 3;
867  s->floor_code = 7;
868  for (ch = 0; ch <= s->channels; ch++)
869  s->fast_gain_code[ch] = 4;
870 
871  /* initial snr offset */
872  s->coarse_snr_offset = 40;
873 
874  /* compute real values */
875  /* currently none of these values change during encoding, so we can just
876  set them once at initialization */
877  s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
878  s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
879  s->bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s->slow_gain_code];
880  s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
881  s->bit_alloc.floor = ff_ac3_floor_tab[s->floor_code];
882  s->bit_alloc.cpl_fast_leak = 0;
883  s->bit_alloc.cpl_slow_leak = 0;
884 
886 }
887 
888 
889 /*
890  * Count the bits used to encode the frame, minus exponents and mantissas.
891  * Bits based on fixed parameters have already been counted, so now we just
892  * have to add the bits based on parameters that change during encoding.
893  */
895 {
896  AC3EncOptions *opt = &s->options;
897  int blk, ch;
898  int frame_bits = 0;
899 
900  /* header */
901  if (s->eac3) {
902  if (opt->eac3_mixing_metadata) {
903  if (s->channel_mode > AC3_CHMODE_STEREO)
904  frame_bits += 2;
905  if (s->has_center)
906  frame_bits += 6;
907  if (s->has_surround)
908  frame_bits += 6;
909  frame_bits += s->lfe_on;
910  frame_bits += 1 + 1 + 2;
911  if (s->channel_mode < AC3_CHMODE_STEREO)
912  frame_bits++;
913  frame_bits++;
914  }
915  if (opt->eac3_info_metadata) {
916  frame_bits += 3 + 1 + 1;
917  if (s->channel_mode == AC3_CHMODE_STEREO)
918  frame_bits += 2 + 2;
919  if (s->channel_mode >= AC3_CHMODE_2F2R)
920  frame_bits += 2;
921  frame_bits++;
922  if (opt->audio_production_info)
923  frame_bits += 5 + 2 + 1;
924  frame_bits++;
925  }
926  /* coupling */
927  if (s->channel_mode > AC3_CHMODE_MONO) {
928  frame_bits++;
929  for (blk = 1; blk < s->num_blocks; blk++) {
930  AC3Block *block = &s->blocks[blk];
931  frame_bits++;
932  if (block->new_cpl_strategy)
933  frame_bits++;
934  }
935  }
936  /* coupling exponent strategy */
937  if (s->cpl_on) {
938  if (s->use_frame_exp_strategy) {
939  frame_bits += 5;
940  } else {
941  for (blk = 0; blk < s->num_blocks; blk++)
942  frame_bits += 2 * s->blocks[blk].cpl_in_use;
943  }
944  }
945  } else {
946  if (opt->audio_production_info)
947  frame_bits += 7;
948  if (s->bitstream_id == 6) {
949  if (opt->extended_bsi_1)
950  frame_bits += 14;
951  if (opt->extended_bsi_2)
952  frame_bits += 14;
953  }
954  }
955 
956  /* audio blocks */
957  for (blk = 0; blk < s->num_blocks; blk++) {
958  AC3Block *block = &s->blocks[blk];
959 
960  /* coupling strategy */
961  if (block->new_cpl_strategy) {
962  if (!s->eac3)
963  frame_bits++;
964  if (block->cpl_in_use) {
965  if (s->eac3)
966  frame_bits++;
967  if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO)
968  frame_bits += s->fbw_channels;
969  if (s->channel_mode == AC3_CHMODE_STEREO)
970  frame_bits++;
971  frame_bits += 4 + 4;
972  if (s->eac3)
973  frame_bits++;
974  else
975  frame_bits += s->num_cpl_subbands - 1;
976  }
977  }
978 
979  /* coupling coordinates */
980  if (block->cpl_in_use) {
981  for (ch = 1; ch <= s->fbw_channels; ch++) {
982  if (block->channel_in_cpl[ch]) {
983  if (!s->eac3 || block->new_cpl_coords[ch] != 2)
984  frame_bits++;
985  if (block->new_cpl_coords[ch]) {
986  frame_bits += 2;
987  frame_bits += (4 + 4) * s->num_cpl_bands;
988  }
989  }
990  }
991  }
992 
993  /* stereo rematrixing */
994  if (s->channel_mode == AC3_CHMODE_STEREO) {
995  if (!s->eac3 || blk > 0)
996  frame_bits++;
997  if (s->blocks[blk].new_rematrixing_strategy)
998  frame_bits += block->num_rematrixing_bands;
999  }
1000 
1001  /* bandwidth codes & gain range */
1002  for (ch = 1; ch <= s->fbw_channels; ch++) {
1003  if (s->exp_strategy[ch][blk] != EXP_REUSE) {
1004  if (!block->channel_in_cpl[ch])
1005  frame_bits += 6;
1006  frame_bits += 2;
1007  }
1008  }
1009 
1010  /* coupling exponent strategy */
1011  if (!s->eac3 && block->cpl_in_use)
1012  frame_bits += 2;
1013 
1014  /* snr offsets and fast gain codes */
1015  if (!s->eac3) {
1016  if (block->new_snr_offsets)
1017  frame_bits += 6 + (s->channels + block->cpl_in_use) * (4 + 3);
1018  }
1019 
1020  /* coupling leak info */
1021  if (block->cpl_in_use) {
1022  if (!s->eac3 || block->new_cpl_leak != 2)
1023  frame_bits++;
1024  if (block->new_cpl_leak)
1025  frame_bits += 3 + 3;
1026  }
1027  }
1028 
1029  s->frame_bits = s->frame_bits_fixed + frame_bits;
1030 }
1031 
1032 
1033 /*
1034  * Calculate masking curve based on the final exponents.
1035  * Also calculate the power spectral densities to use in future calculations.
1036  */
1038 {
1039  int blk, ch;
1040 
1041  for (blk = 0; blk < s->num_blocks; blk++) {
1042  AC3Block *block = &s->blocks[blk];
1043  for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
1044  /* We only need psd and mask for calculating bap.
1045  Since we currently do not calculate bap when exponent
1046  strategy is EXP_REUSE we do not need to calculate psd or mask. */
1047  if (s->exp_strategy[ch][blk] != EXP_REUSE) {
1048  ff_ac3_bit_alloc_calc_psd(block->exp[ch], s->start_freq[ch],
1049  block->end_freq[ch], block->psd[ch],
1050  block->band_psd[ch]);
1051  ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
1052  s->start_freq[ch], block->end_freq[ch],
1053  ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
1054  ch == s->lfe_channel,
1055  DBA_NONE, 0, NULL, NULL, NULL,
1056  block->mask[ch]);
1057  }
1058  }
1059  }
1060 }
1061 
1062 
1063 /*
1064  * Ensure that bap for each block and channel point to the current bap_buffer.
1065  * They may have been switched during the bit allocation search.
1066  */
1068 {
1069  int blk, ch;
1070  uint8_t *ref_bap;
1071 
1072  if (s->ref_bap[0][0] == s->bap_buffer && s->ref_bap_set)
1073  return;
1074 
1075  ref_bap = s->bap_buffer;
1076  for (ch = 0; ch <= s->channels; ch++) {
1077  for (blk = 0; blk < s->num_blocks; blk++)
1078  s->ref_bap[ch][blk] = ref_bap + AC3_MAX_COEFS * s->exp_ref_block[ch][blk];
1079  ref_bap += AC3_MAX_COEFS * s->num_blocks;
1080  }
1081  s->ref_bap_set = 1;
1082 }
1083 
1084 
1085 /**
1086  * Initialize mantissa counts.
1087  * These are set so that they are padded to the next whole group size when bits
1088  * are counted in compute_mantissa_size.
1089  *
1090  * @param[in,out] mant_cnt running counts for each bap value for each block
1091  */
1092 static void count_mantissa_bits_init(uint16_t mant_cnt[AC3_MAX_BLOCKS][16])
1093 {
1094  int blk;
1095 
1096  for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1097  memset(mant_cnt[blk], 0, sizeof(mant_cnt[blk]));
1098  mant_cnt[blk][1] = mant_cnt[blk][2] = 2;
1099  mant_cnt[blk][4] = 1;
1100  }
1101 }
1102 
1103 
1104 /**
1105  * Update mantissa bit counts for all blocks in 1 channel in a given bandwidth
1106  * range.
1107  *
1108  * @param s AC-3 encoder private context
1109  * @param ch channel index
1110  * @param[in,out] mant_cnt running counts for each bap value for each block
1111  * @param start starting coefficient bin
1112  * @param end ending coefficient bin
1113  */
1115  uint16_t mant_cnt[AC3_MAX_BLOCKS][16],
1116  int start, int end)
1117 {
1118  int blk;
1119 
1120  for (blk = 0; blk < s->num_blocks; blk++) {
1121  AC3Block *block = &s->blocks[blk];
1122  if (ch == CPL_CH && !block->cpl_in_use)
1123  continue;
1124  s->ac3dsp.update_bap_counts(mant_cnt[blk],
1125  s->ref_bap[ch][blk] + start,
1126  FFMIN(end, block->end_freq[ch]) - start);
1127  }
1128 }
1129 
1130 
1131 /*
1132  * Count the number of mantissa bits in the frame based on the bap values.
1133  */
1135 {
1136  int ch, max_end_freq;
1137  LOCAL_ALIGNED_16(uint16_t, mant_cnt, [AC3_MAX_BLOCKS], [16]);
1138 
1139  count_mantissa_bits_init(mant_cnt);
1140 
1141  max_end_freq = s->bandwidth_code * 3 + 73;
1142  for (ch = !s->cpl_enabled; ch <= s->channels; ch++)
1143  count_mantissa_bits_update_ch(s, ch, mant_cnt, s->start_freq[ch],
1144  max_end_freq);
1145 
1146  return s->ac3dsp.compute_mantissa_size(mant_cnt);
1147 }
1148 
1149 
1150 /**
1151  * Run the bit allocation with a given SNR offset.
1152  * This calculates the bit allocation pointers that will be used to determine
1153  * the quantization of each mantissa.
1154  *
1155  * @param s AC-3 encoder private context
1156  * @param snr_offset SNR offset, 0 to 1023
1157  * @return the number of bits needed for mantissas if the given SNR offset is
1158  * is used.
1159  */
1160 static int bit_alloc(AC3EncodeContext *s, int snr_offset)
1161 {
1162  int blk, ch;
1163 
1164  snr_offset = (snr_offset - 240) * 4;
1165 
1166  reset_block_bap(s);
1167  for (blk = 0; blk < s->num_blocks; blk++) {
1168  AC3Block *block = &s->blocks[blk];
1169 
1170  for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
1171  /* Currently the only bit allocation parameters which vary across
1172  blocks within a frame are the exponent values. We can take
1173  advantage of that by reusing the bit allocation pointers
1174  whenever we reuse exponents. */
1175  if (s->exp_strategy[ch][blk] != EXP_REUSE) {
1176  s->ac3dsp.bit_alloc_calc_bap(block->mask[ch], block->psd[ch],
1177  s->start_freq[ch], block->end_freq[ch],
1178  snr_offset, s->bit_alloc.floor,
1179  ff_ac3_bap_tab, s->ref_bap[ch][blk]);
1180  }
1181  }
1182  }
1183  return count_mantissa_bits(s);
1184 }
1185 
1186 
1187 /*
1188  * Constant bitrate bit allocation search.
1189  * Find the largest SNR offset that will allow data to fit in the frame.
1190  */
1192 {
1193  int ch;
1194  int bits_left;
1195  int snr_offset, snr_incr;
1196 
1197  bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
1198  if (bits_left < 0)
1199  return AVERROR(EINVAL);
1200 
1201  snr_offset = s->coarse_snr_offset << 4;
1202 
1203  /* if previous frame SNR offset was 1023, check if current frame can also
1204  use SNR offset of 1023. if so, skip the search. */
1205  if ((snr_offset | s->fine_snr_offset[1]) == 1023) {
1206  if (bit_alloc(s, 1023) <= bits_left)
1207  return 0;
1208  }
1209 
1210  while (snr_offset >= 0 &&
1211  bit_alloc(s, snr_offset) > bits_left) {
1212  snr_offset -= 64;
1213  }
1214  if (snr_offset < 0)
1215  return AVERROR(EINVAL);
1216 
1217  FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1218  for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1219  while (snr_offset + snr_incr <= 1023 &&
1220  bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
1221  snr_offset += snr_incr;
1222  FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1223  }
1224  }
1225  FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1226  reset_block_bap(s);
1227 
1228  s->coarse_snr_offset = snr_offset >> 4;
1229  for (ch = !s->cpl_on; ch <= s->channels; ch++)
1230  s->fine_snr_offset[ch] = snr_offset & 0xF;
1231 
1232  return 0;
1233 }
1234 
1235 
1236 /*
1237  * Perform bit allocation search.
1238  * Finds the SNR offset value that maximizes quality and fits in the specified
1239  * frame size. Output is the SNR offset and a set of bit allocation pointers
1240  * used to quantize the mantissas.
1241  */
1243 {
1245 
1246  s->exponent_bits = count_exponent_bits(s);
1247 
1249 
1250  return cbr_bit_allocation(s);
1251 }
1252 
1253 
1254 /**
1255  * Symmetric quantization on 'levels' levels.
1256  *
1257  * @param c unquantized coefficient
1258  * @param e exponent
1259  * @param levels number of quantization levels
1260  * @return quantized coefficient
1261  */
1262 static inline int sym_quant(int c, int e, int levels)
1263 {
1264  int v = (((levels * c) >> (24 - e)) + levels) >> 1;
1265  av_assert2(v >= 0 && v < levels);
1266  return v;
1267 }
1268 
1269 
1270 /**
1271  * Asymmetric quantization on 2^qbits levels.
1272  *
1273  * @param c unquantized coefficient
1274  * @param e exponent
1275  * @param qbits number of quantization bits
1276  * @return quantized coefficient
1277  */
1278 static inline int asym_quant(int c, int e, int qbits)
1279 {
1280  int m;
1281 
1282  c = (((c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
1283  m = (1 << (qbits-1));
1284  if (c >= m)
1285  c = m - 1;
1286  av_assert2(c >= -m);
1287  return c;
1288 }
1289 
1290 
1291 /**
1292  * Quantize a set of mantissas for a single channel in a single block.
1293  *
1294  * @param s Mantissa count context
1295  * @param fixed_coef unquantized fixed-point coefficients
1296  * @param exp exponents
1297  * @param bap bit allocation pointer indices
1298  * @param[out] qmant quantized coefficients
1299  * @param start_freq starting coefficient bin
1300  * @param end_freq ending coefficient bin
1301  */
1302 static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef,
1303  uint8_t *exp, uint8_t *bap,
1304  int16_t *qmant, int start_freq,
1305  int end_freq)
1306 {
1307  int i;
1308 
1309  for (i = start_freq; i < end_freq; i++) {
1310  int c = fixed_coef[i];
1311  int e = exp[i];
1312  int v = bap[i];
1313  switch (v) {
1314  case 0:
1315  break;
1316  case 1:
1317  v = sym_quant(c, e, 3);
1318  switch (s->mant1_cnt) {
1319  case 0:
1320  s->qmant1_ptr = &qmant[i];
1321  v = 9 * v;
1322  s->mant1_cnt = 1;
1323  break;
1324  case 1:
1325  *s->qmant1_ptr += 3 * v;
1326  s->mant1_cnt = 2;
1327  v = 128;
1328  break;
1329  default:
1330  *s->qmant1_ptr += v;
1331  s->mant1_cnt = 0;
1332  v = 128;
1333  break;
1334  }
1335  break;
1336  case 2:
1337  v = sym_quant(c, e, 5);
1338  switch (s->mant2_cnt) {
1339  case 0:
1340  s->qmant2_ptr = &qmant[i];
1341  v = 25 * v;
1342  s->mant2_cnt = 1;
1343  break;
1344  case 1:
1345  *s->qmant2_ptr += 5 * v;
1346  s->mant2_cnt = 2;
1347  v = 128;
1348  break;
1349  default:
1350  *s->qmant2_ptr += v;
1351  s->mant2_cnt = 0;
1352  v = 128;
1353  break;
1354  }
1355  break;
1356  case 3:
1357  v = sym_quant(c, e, 7);
1358  break;
1359  case 4:
1360  v = sym_quant(c, e, 11);
1361  switch (s->mant4_cnt) {
1362  case 0:
1363  s->qmant4_ptr = &qmant[i];
1364  v = 11 * v;
1365  s->mant4_cnt = 1;
1366  break;
1367  default:
1368  *s->qmant4_ptr += v;
1369  s->mant4_cnt = 0;
1370  v = 128;
1371  break;
1372  }
1373  break;
1374  case 5:
1375  v = sym_quant(c, e, 15);
1376  break;
1377  case 14:
1378  v = asym_quant(c, e, 14);
1379  break;
1380  case 15:
1381  v = asym_quant(c, e, 16);
1382  break;
1383  default:
1384  v = asym_quant(c, e, v - 1);
1385  break;
1386  }
1387  qmant[i] = v;
1388  }
1389 }
1390 
1391 
1392 /**
1393  * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
1394  *
1395  * @param s AC-3 encoder private context
1396  */
1398 {
1399  int blk, ch, ch0=0, got_cpl;
1400 
1401  for (blk = 0; blk < s->num_blocks; blk++) {
1402  AC3Block *block = &s->blocks[blk];
1403  AC3Mant m = { 0 };
1404 
1405  got_cpl = !block->cpl_in_use;
1406  for (ch = 1; ch <= s->channels; ch++) {
1407  if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
1408  ch0 = ch - 1;
1409  ch = CPL_CH;
1410  got_cpl = 1;
1411  }
1412  quantize_mantissas_blk_ch(&m, block->fixed_coef[ch],
1413  s->blocks[s->exp_ref_block[ch][blk]].exp[ch],
1414  s->ref_bap[ch][blk], block->qmant[ch],
1415  s->start_freq[ch], block->end_freq[ch]);
1416  if (ch == CPL_CH)
1417  ch = ch0;
1418  }
1419  }
1420 }
1421 
1422 
1423 /*
1424  * Write the AC-3 frame header to the output bitstream.
1425  */
1427 {
1428  AC3EncOptions *opt = &s->options;
1429 
1430  put_bits(&s->pb, 16, 0x0b77); /* frame header */
1431  put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
1432  put_bits(&s->pb, 2, s->bit_alloc.sr_code);
1433  put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
1434  put_bits(&s->pb, 5, s->bitstream_id);
1435  put_bits(&s->pb, 3, s->bitstream_mode);
1436  put_bits(&s->pb, 3, s->channel_mode);
1437  if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
1438  put_bits(&s->pb, 2, s->center_mix_level);
1439  if (s->channel_mode & 0x04)
1440  put_bits(&s->pb, 2, s->surround_mix_level);
1441  if (s->channel_mode == AC3_CHMODE_STEREO)
1442  put_bits(&s->pb, 2, opt->dolby_surround_mode);
1443  put_bits(&s->pb, 1, s->lfe_on); /* LFE */
1444  put_bits(&s->pb, 5, -opt->dialogue_level);
1445  put_bits(&s->pb, 1, 0); /* no compression control word */
1446  put_bits(&s->pb, 1, 0); /* no lang code */
1447  put_bits(&s->pb, 1, opt->audio_production_info);
1448  if (opt->audio_production_info) {
1449  put_bits(&s->pb, 5, opt->mixing_level - 80);
1450  put_bits(&s->pb, 2, opt->room_type);
1451  }
1452  put_bits(&s->pb, 1, opt->copyright);
1453  put_bits(&s->pb, 1, opt->original);
1454  if (s->bitstream_id == 6) {
1455  /* alternate bit stream syntax */
1456  put_bits(&s->pb, 1, opt->extended_bsi_1);
1457  if (opt->extended_bsi_1) {
1458  put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
1459  put_bits(&s->pb, 3, s->ltrt_center_mix_level);
1460  put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
1461  put_bits(&s->pb, 3, s->loro_center_mix_level);
1462  put_bits(&s->pb, 3, s->loro_surround_mix_level);
1463  }
1464  put_bits(&s->pb, 1, opt->extended_bsi_2);
1465  if (opt->extended_bsi_2) {
1466  put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
1467  put_bits(&s->pb, 2, opt->dolby_headphone_mode);
1468  put_bits(&s->pb, 1, opt->ad_converter_type);
1469  put_bits(&s->pb, 9, 0); /* xbsi2 and encinfo : reserved */
1470  }
1471  } else {
1472  put_bits(&s->pb, 1, 0); /* no time code 1 */
1473  put_bits(&s->pb, 1, 0); /* no time code 2 */
1474  }
1475  put_bits(&s->pb, 1, 0); /* no additional bit stream info */
1476 }
1477 
1478 
1479 /*
1480  * Write one audio block to the output bitstream.
1481  */
1483 {
1484  int ch, i, baie, bnd, got_cpl, av_uninit(ch0);
1485  AC3Block *block = &s->blocks[blk];
1486 
1487  /* block switching */
1488  if (!s->eac3) {
1489  for (ch = 0; ch < s->fbw_channels; ch++)
1490  put_bits(&s->pb, 1, 0);
1491  }
1492 
1493  /* dither flags */
1494  if (!s->eac3) {
1495  for (ch = 0; ch < s->fbw_channels; ch++)
1496  put_bits(&s->pb, 1, 1);
1497  }
1498 
1499  /* dynamic range codes */
1500  put_bits(&s->pb, 1, 0);
1501 
1502  /* spectral extension */
1503  if (s->eac3)
1504  put_bits(&s->pb, 1, 0);
1505 
1506  /* channel coupling */
1507  if (!s->eac3)
1508  put_bits(&s->pb, 1, block->new_cpl_strategy);
1509  if (block->new_cpl_strategy) {
1510  if (!s->eac3)
1511  put_bits(&s->pb, 1, block->cpl_in_use);
1512  if (block->cpl_in_use) {
1513  int start_sub, end_sub;
1514  if (s->eac3)
1515  put_bits(&s->pb, 1, 0); /* enhanced coupling */
1516  if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO) {
1517  for (ch = 1; ch <= s->fbw_channels; ch++)
1518  put_bits(&s->pb, 1, block->channel_in_cpl[ch]);
1519  }
1520  if (s->channel_mode == AC3_CHMODE_STEREO)
1521  put_bits(&s->pb, 1, 0); /* phase flags in use */
1522  start_sub = (s->start_freq[CPL_CH] - 37) / 12;
1523  end_sub = (s->cpl_end_freq - 37) / 12;
1524  put_bits(&s->pb, 4, start_sub);
1525  put_bits(&s->pb, 4, end_sub - 3);
1526  /* coupling band structure */
1527  if (s->eac3) {
1528  put_bits(&s->pb, 1, 0); /* use default */
1529  } else {
1530  for (bnd = start_sub+1; bnd < end_sub; bnd++)
1532  }
1533  }
1534  }
1535 
1536  /* coupling coordinates */
1537  if (block->cpl_in_use) {
1538  for (ch = 1; ch <= s->fbw_channels; ch++) {
1539  if (block->channel_in_cpl[ch]) {
1540  if (!s->eac3 || block->new_cpl_coords[ch] != 2)
1541  put_bits(&s->pb, 1, block->new_cpl_coords[ch]);
1542  if (block->new_cpl_coords[ch]) {
1543  put_bits(&s->pb, 2, block->cpl_master_exp[ch]);
1544  for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
1545  put_bits(&s->pb, 4, block->cpl_coord_exp [ch][bnd]);
1546  put_bits(&s->pb, 4, block->cpl_coord_mant[ch][bnd]);
1547  }
1548  }
1549  }
1550  }
1551  }
1552 
1553  /* stereo rematrixing */
1554  if (s->channel_mode == AC3_CHMODE_STEREO) {
1555  if (!s->eac3 || blk > 0)
1556  put_bits(&s->pb, 1, block->new_rematrixing_strategy);
1557  if (block->new_rematrixing_strategy) {
1558  /* rematrixing flags */
1559  for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++)
1560  put_bits(&s->pb, 1, block->rematrixing_flags[bnd]);
1561  }
1562  }
1563 
1564  /* exponent strategy */
1565  if (!s->eac3) {
1566  for (ch = !block->cpl_in_use; ch <= s->fbw_channels; ch++)
1567  put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
1568  if (s->lfe_on)
1569  put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
1570  }
1571 
1572  /* bandwidth */
1573  for (ch = 1; ch <= s->fbw_channels; ch++) {
1574  if (s->exp_strategy[ch][blk] != EXP_REUSE && !block->channel_in_cpl[ch])
1575  put_bits(&s->pb, 6, s->bandwidth_code);
1576  }
1577 
1578  /* exponents */
1579  for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
1580  int nb_groups;
1581  int cpl = (ch == CPL_CH);
1582 
1583  if (s->exp_strategy[ch][blk] == EXP_REUSE)
1584  continue;
1585 
1586  /* DC exponent */
1587  put_bits(&s->pb, 4, block->grouped_exp[ch][0] >> cpl);
1588 
1589  /* exponent groups */
1590  nb_groups = exponent_group_tab[cpl][s->exp_strategy[ch][blk]-1][block->end_freq[ch]-s->start_freq[ch]];
1591  for (i = 1; i <= nb_groups; i++)
1592  put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
1593 
1594  /* gain range info */
1595  if (ch != s->lfe_channel && !cpl)
1596  put_bits(&s->pb, 2, 0);
1597  }
1598 
1599  /* bit allocation info */
1600  if (!s->eac3) {
1601  baie = (blk == 0);
1602  put_bits(&s->pb, 1, baie);
1603  if (baie) {
1604  put_bits(&s->pb, 2, s->slow_decay_code);
1605  put_bits(&s->pb, 2, s->fast_decay_code);
1606  put_bits(&s->pb, 2, s->slow_gain_code);
1607  put_bits(&s->pb, 2, s->db_per_bit_code);
1608  put_bits(&s->pb, 3, s->floor_code);
1609  }
1610  }
1611 
1612  /* snr offset */
1613  if (!s->eac3) {
1614  put_bits(&s->pb, 1, block->new_snr_offsets);
1615  if (block->new_snr_offsets) {
1616  put_bits(&s->pb, 6, s->coarse_snr_offset);
1617  for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
1618  put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
1619  put_bits(&s->pb, 3, s->fast_gain_code[ch]);
1620  }
1621  }
1622  } else {
1623  put_bits(&s->pb, 1, 0); /* no converter snr offset */
1624  }
1625 
1626  /* coupling leak */
1627  if (block->cpl_in_use) {
1628  if (!s->eac3 || block->new_cpl_leak != 2)
1629  put_bits(&s->pb, 1, block->new_cpl_leak);
1630  if (block->new_cpl_leak) {
1631  put_bits(&s->pb, 3, s->bit_alloc.cpl_fast_leak);
1632  put_bits(&s->pb, 3, s->bit_alloc.cpl_slow_leak);
1633  }
1634  }
1635 
1636  if (!s->eac3) {
1637  put_bits(&s->pb, 1, 0); /* no delta bit allocation */
1638  put_bits(&s->pb, 1, 0); /* no data to skip */
1639  }
1640 
1641  /* mantissas */
1642  got_cpl = !block->cpl_in_use;
1643  for (ch = 1; ch <= s->channels; ch++) {
1644  int b, q;
1645 
1646  if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
1647  ch0 = ch - 1;
1648  ch = CPL_CH;
1649  got_cpl = 1;
1650  }
1651  for (i = s->start_freq[ch]; i < block->end_freq[ch]; i++) {
1652  q = block->qmant[ch][i];
1653  b = s->ref_bap[ch][blk][i];
1654  switch (b) {
1655  case 0: break;
1656  case 1: if (q != 128) put_bits (&s->pb, 5, q); break;
1657  case 2: if (q != 128) put_bits (&s->pb, 7, q); break;
1658  case 3: put_sbits(&s->pb, 3, q); break;
1659  case 4: if (q != 128) put_bits (&s->pb, 7, q); break;
1660  case 14: put_sbits(&s->pb, 14, q); break;
1661  case 15: put_sbits(&s->pb, 16, q); break;
1662  default: put_sbits(&s->pb, b-1, q); break;
1663  }
1664  }
1665  if (ch == CPL_CH)
1666  ch = ch0;
1667  }
1668 }
1669 
1670 
1671 /** CRC-16 Polynomial */
1672 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1673 
1674 
1675 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
1676 {
1677  unsigned int c;
1678 
1679  c = 0;
1680  while (a) {
1681  if (a & 1)
1682  c ^= b;
1683  a = a >> 1;
1684  b = b << 1;
1685  if (b & (1 << 16))
1686  b ^= poly;
1687  }
1688  return c;
1689 }
1690 
1691 
1692 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
1693 {
1694  unsigned int r;
1695  r = 1;
1696  while (n) {
1697  if (n & 1)
1698  r = mul_poly(r, a, poly);
1699  a = mul_poly(a, a, poly);
1700  n >>= 1;
1701  }
1702  return r;
1703 }
1704 
1705 
1706 /*
1707  * Fill the end of the frame with 0's and compute the two CRCs.
1708  */
1710 {
1711  const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
1712  int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1713  uint8_t *frame;
1714 
1715  frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
1716 
1717  /* pad the remainder of the frame with zeros */
1718  av_assert2(s->frame_size * 8 - put_bits_count(&s->pb) >= 18);
1719  flush_put_bits(&s->pb);
1720  frame = s->pb.buf;
1721  pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
1722  av_assert2(pad_bytes >= 0);
1723  if (pad_bytes > 0)
1724  memset(put_bits_ptr(&s->pb), 0, pad_bytes);
1725 
1726  if (s->eac3) {
1727  /* compute crc2 */
1728  crc2_partial = av_crc(crc_ctx, 0, frame + 2, s->frame_size - 5);
1729  } else {
1730  /* compute crc1 */
1731  /* this is not so easy because it is at the beginning of the data... */
1732  crc1 = av_bswap16(av_crc(crc_ctx, 0, frame + 4, frame_size_58 - 4));
1733  crc_inv = s->crc_inv[s->frame_size > s->frame_size_min];
1734  crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
1735  AV_WB16(frame + 2, crc1);
1736 
1737  /* compute crc2 */
1738  crc2_partial = av_crc(crc_ctx, 0, frame + frame_size_58,
1739  s->frame_size - frame_size_58 - 3);
1740  }
1741  crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
1742  /* ensure crc2 does not match sync word by flipping crcrsv bit if needed */
1743  if (crc2 == 0x770B) {
1744  frame[s->frame_size - 3] ^= 0x1;
1745  crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
1746  }
1747  crc2 = av_bswap16(crc2);
1748  AV_WB16(frame + s->frame_size - 2, crc2);
1749 }
1750 
1751 
1752 /**
1753  * Write the frame to the output bitstream.
1754  *
1755  * @param s AC-3 encoder private context
1756  * @param frame output data buffer
1757  */
1758 static void ac3_output_frame(AC3EncodeContext *s, unsigned char *frame)
1759 {
1760  int blk;
1761 
1762  init_put_bits(&s->pb, frame, s->frame_size);
1763 
1764  s->output_frame_header(s);
1765 
1766  for (blk = 0; blk < s->num_blocks; blk++)
1768 
1770 }
1771 
1773  const AVFrame *frame, int *got_packet_ptr)
1774 {
1775  AC3EncodeContext *const s = avctx->priv_data;
1776  int ret;
1777 
1779 
1781 
1783  if (ret) {
1784  av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
1785  return ret;
1786  }
1787 
1789 
1791 
1792  ret = ff_get_encode_buffer(avctx, avpkt, s->frame_size, 0);
1793  if (ret < 0)
1794  return ret;
1795  ac3_output_frame(s, avpkt->data);
1796 
1797  if (frame->pts != AV_NOPTS_VALUE)
1798  avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
1799 
1800  *got_packet_ptr = 1;
1801  return 0;
1802 }
1803 
1805 {
1806 #ifdef DEBUG
1807  AVCodecContext *avctx = s->avctx;
1808  AC3EncOptions *opt = &s->options;
1809  char strbuf[32];
1810 
1811  switch (s->bitstream_id) {
1812  case 6: av_strlcpy(strbuf, "AC-3 (alt syntax)", 32); break;
1813  case 8: av_strlcpy(strbuf, "AC-3 (standard)", 32); break;
1814  case 9: av_strlcpy(strbuf, "AC-3 (dnet half-rate)", 32); break;
1815  case 10: av_strlcpy(strbuf, "AC-3 (dnet quater-rate)", 32); break;
1816  case 16: av_strlcpy(strbuf, "E-AC-3 (enhanced)", 32); break;
1817  default: snprintf(strbuf, 32, "ERROR");
1818  }
1819  ff_dlog(avctx, "bitstream_id: %s (%d)\n", strbuf, s->bitstream_id);
1820  ff_dlog(avctx, "sample_fmt: %s\n", av_get_sample_fmt_name(avctx->sample_fmt));
1821  av_channel_layout_describe(&avctx->ch_layout, strbuf, sizeof(strbuf));
1822  ff_dlog(avctx, "channel_layout: %s\n", strbuf);
1823  ff_dlog(avctx, "sample_rate: %d\n", s->sample_rate);
1824  ff_dlog(avctx, "bit_rate: %d\n", s->bit_rate);
1825  ff_dlog(avctx, "blocks/frame: %d (code=%d)\n", s->num_blocks, s->num_blks_code);
1826  if (s->cutoff)
1827  ff_dlog(avctx, "cutoff: %d\n", s->cutoff);
1828 
1829  ff_dlog(avctx, "per_frame_metadata: %s\n",
1830  opt->allow_per_frame_metadata?"on":"off");
1831  if (s->has_center)
1832  ff_dlog(avctx, "center_mixlev: %0.3f (%d)\n", opt->center_mix_level,
1833  s->center_mix_level);
1834  else
1835  ff_dlog(avctx, "center_mixlev: {not written}\n");
1836  if (s->has_surround)
1837  ff_dlog(avctx, "surround_mixlev: %0.3f (%d)\n", opt->surround_mix_level,
1838  s->surround_mix_level);
1839  else
1840  ff_dlog(avctx, "surround_mixlev: {not written}\n");
1841  if (opt->audio_production_info) {
1842  ff_dlog(avctx, "mixing_level: %ddB\n", opt->mixing_level);
1843  switch (opt->room_type) {
1844  case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
1845  case AC3ENC_OPT_LARGE_ROOM: av_strlcpy(strbuf, "large", 32); break;
1846  case AC3ENC_OPT_SMALL_ROOM: av_strlcpy(strbuf, "small", 32); break;
1847  default: snprintf(strbuf, 32, "ERROR (%d)", opt->room_type);
1848  }
1849  ff_dlog(avctx, "room_type: %s\n", strbuf);
1850  } else {
1851  ff_dlog(avctx, "mixing_level: {not written}\n");
1852  ff_dlog(avctx, "room_type: {not written}\n");
1853  }
1854  ff_dlog(avctx, "copyright: %s\n", opt->copyright?"on":"off");
1855  ff_dlog(avctx, "dialnorm: %ddB\n", opt->dialogue_level);
1856  if (s->channel_mode == AC3_CHMODE_STEREO) {
1857  switch (opt->dolby_surround_mode) {
1858  case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
1859  case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
1860  case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
1861  default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_mode);
1862  }
1863  ff_dlog(avctx, "dsur_mode: %s\n", strbuf);
1864  } else {
1865  ff_dlog(avctx, "dsur_mode: {not written}\n");
1866  }
1867  ff_dlog(avctx, "original: %s\n", opt->original?"on":"off");
1868 
1869  if (s->bitstream_id == 6) {
1870  if (opt->extended_bsi_1) {
1871  switch (opt->preferred_stereo_downmix) {
1872  case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
1873  case AC3ENC_OPT_DOWNMIX_LTRT: av_strlcpy(strbuf, "ltrt", 32); break;
1874  case AC3ENC_OPT_DOWNMIX_LORO: av_strlcpy(strbuf, "loro", 32); break;
1875  default: snprintf(strbuf, 32, "ERROR (%d)", opt->preferred_stereo_downmix);
1876  }
1877  ff_dlog(avctx, "dmix_mode: %s\n", strbuf);
1878  ff_dlog(avctx, "ltrt_cmixlev: %0.3f (%d)\n",
1879  opt->ltrt_center_mix_level, s->ltrt_center_mix_level);
1880  ff_dlog(avctx, "ltrt_surmixlev: %0.3f (%d)\n",
1881  opt->ltrt_surround_mix_level, s->ltrt_surround_mix_level);
1882  ff_dlog(avctx, "loro_cmixlev: %0.3f (%d)\n",
1883  opt->loro_center_mix_level, s->loro_center_mix_level);
1884  ff_dlog(avctx, "loro_surmixlev: %0.3f (%d)\n",
1885  opt->loro_surround_mix_level, s->loro_surround_mix_level);
1886  } else {
1887  ff_dlog(avctx, "extended bitstream info 1: {not written}\n");
1888  }
1889  if (opt->extended_bsi_2) {
1890  switch (opt->dolby_surround_ex_mode) {
1891  case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
1892  case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
1893  case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
1894  default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_ex_mode);
1895  }
1896  ff_dlog(avctx, "dsurex_mode: %s\n", strbuf);
1897  switch (opt->dolby_headphone_mode) {
1898  case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
1899  case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
1900  case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
1901  default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_headphone_mode);
1902  }
1903  ff_dlog(avctx, "dheadphone_mode: %s\n", strbuf);
1904 
1905  switch (opt->ad_converter_type) {
1906  case AC3ENC_OPT_ADCONV_STANDARD: av_strlcpy(strbuf, "standard", 32); break;
1907  case AC3ENC_OPT_ADCONV_HDCD: av_strlcpy(strbuf, "hdcd", 32); break;
1908  default: snprintf(strbuf, 32, "ERROR (%d)", opt->ad_converter_type);
1909  }
1910  ff_dlog(avctx, "ad_conv_type: %s\n", strbuf);
1911  } else {
1912  ff_dlog(avctx, "extended bitstream info 2: {not written}\n");
1913  }
1914  }
1915 #endif
1916 }
1917 
1918 
1919 #define FLT_OPTION_THRESHOLD 0.01
1920 
1921 static int validate_float_option(float v, const float *v_list, int v_list_size)
1922 {
1923  int i;
1924 
1925  for (i = 0; i < v_list_size; i++) {
1926  if (v < (v_list[i] + FLT_OPTION_THRESHOLD) &&
1927  v > (v_list[i] - FLT_OPTION_THRESHOLD))
1928  break;
1929  }
1930  if (i == v_list_size)
1931  return AVERROR(EINVAL);
1932 
1933  return i;
1934 }
1935 
1936 
1937 static void validate_mix_level(void *log_ctx, const char *opt_name,
1938  float *opt_param, const float *list,
1939  int list_size, int default_value, int min_value,
1940  int *ctx_param)
1941 {
1942  int mixlev = validate_float_option(*opt_param, list, list_size);
1943  if (mixlev < min_value) {
1944  mixlev = default_value;
1945  if (*opt_param >= 0.0) {
1946  av_log(log_ctx, AV_LOG_WARNING, "requested %s is not valid. using "
1947  "default value: %0.3f\n", opt_name, list[mixlev]);
1948  }
1949  }
1950  *opt_param = list[mixlev];
1951  *ctx_param = mixlev;
1952 }
1953 
1954 
1955 /**
1956  * Validate metadata options as set by AVOption system.
1957  * These values can optionally be changed per-frame.
1958  *
1959  * @param s AC-3 encoder private context
1960  */
1962 {
1963  AVCodecContext *avctx = s->avctx;
1964  AC3EncOptions *opt = &s->options;
1965 
1966  opt->audio_production_info = 0;
1967  opt->extended_bsi_1 = 0;
1968  opt->extended_bsi_2 = 0;
1969  opt->eac3_mixing_metadata = 0;
1970  opt->eac3_info_metadata = 0;
1971 
1972  /* determine mixing metadata / xbsi1 use */
1973  if (s->channel_mode > AC3_CHMODE_STEREO && opt->preferred_stereo_downmix != AC3ENC_OPT_NONE) {
1974  opt->extended_bsi_1 = 1;
1975  opt->eac3_mixing_metadata = 1;
1976  }
1977  if (s->has_center &&
1978  (opt->ltrt_center_mix_level >= 0 || opt->loro_center_mix_level >= 0)) {
1979  opt->extended_bsi_1 = 1;
1980  opt->eac3_mixing_metadata = 1;
1981  }
1982  if (s->has_surround &&
1983  (opt->ltrt_surround_mix_level >= 0 || opt->loro_surround_mix_level >= 0)) {
1984  opt->extended_bsi_1 = 1;
1985  opt->eac3_mixing_metadata = 1;
1986  }
1987 
1988  if (s->eac3) {
1989  /* determine info metadata use */
1991  opt->eac3_info_metadata = 1;
1992  if (opt->copyright != AC3ENC_OPT_NONE || opt->original != AC3ENC_OPT_NONE)
1993  opt->eac3_info_metadata = 1;
1994  if (s->channel_mode == AC3_CHMODE_STEREO &&
1996  opt->eac3_info_metadata = 1;
1997  if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE)
1998  opt->eac3_info_metadata = 1;
1999  if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE ||
2001  opt->audio_production_info = 1;
2002  opt->eac3_info_metadata = 1;
2003  }
2004  } else {
2005  /* determine audio production info use */
2006  if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE)
2007  opt->audio_production_info = 1;
2008 
2009  /* determine xbsi2 use */
2010  if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE)
2011  opt->extended_bsi_2 = 1;
2012  if (s->channel_mode == AC3_CHMODE_STEREO && opt->dolby_headphone_mode != AC3ENC_OPT_NONE)
2013  opt->extended_bsi_2 = 1;
2014  if (opt->ad_converter_type != AC3ENC_OPT_NONE)
2015  opt->extended_bsi_2 = 1;
2016  }
2017 
2018  /* validate AC-3 mixing levels */
2019  if (!s->eac3) {
2020  if (s->has_center) {
2021  validate_mix_level(avctx, "center_mix_level", &opt->center_mix_level,
2023  &s->center_mix_level);
2024  }
2025  if (s->has_surround) {
2026  validate_mix_level(avctx, "surround_mix_level", &opt->surround_mix_level,
2028  &s->surround_mix_level);
2029  }
2030  }
2031 
2032  /* validate extended bsi 1 / mixing metadata */
2033  if (opt->extended_bsi_1 || opt->eac3_mixing_metadata) {
2034  /* default preferred stereo downmix */
2037  if (!s->eac3 || s->has_center) {
2038  /* validate Lt/Rt center mix level */
2039  validate_mix_level(avctx, "ltrt_center_mix_level",
2041  EXTMIXLEV_NUM_OPTIONS, 5, 0,
2042  &s->ltrt_center_mix_level);
2043  /* validate Lo/Ro center mix level */
2044  validate_mix_level(avctx, "loro_center_mix_level",
2046  EXTMIXLEV_NUM_OPTIONS, 5, 0,
2047  &s->loro_center_mix_level);
2048  }
2049  if (!s->eac3 || s->has_surround) {
2050  /* validate Lt/Rt surround mix level */
2051  validate_mix_level(avctx, "ltrt_surround_mix_level",
2053  EXTMIXLEV_NUM_OPTIONS, 6, 3,
2054  &s->ltrt_surround_mix_level);
2055  /* validate Lo/Ro surround mix level */
2056  validate_mix_level(avctx, "loro_surround_mix_level",
2058  EXTMIXLEV_NUM_OPTIONS, 6, 3,
2059  &s->loro_surround_mix_level);
2060  }
2061  }
2062 
2063  /* validate audio service type / channels combination */
2065  avctx->ch_layout.nb_channels == 1) ||
2069  && avctx->ch_layout.nb_channels > 1)) {
2070  av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the "
2071  "specified number of channels\n");
2072  return AVERROR(EINVAL);
2073  }
2074 
2075  /* validate extended bsi 2 / info metadata */
2076  if (opt->extended_bsi_2 || opt->eac3_info_metadata) {
2077  /* default dolby headphone mode */
2080  /* default dolby surround ex mode */
2083  /* default A/D converter type */
2084  if (opt->ad_converter_type == AC3ENC_OPT_NONE)
2086  }
2087 
2088  /* copyright & original defaults */
2089  if (!s->eac3 || opt->eac3_info_metadata) {
2090  /* default copyright */
2091  if (opt->copyright == AC3ENC_OPT_NONE)
2092  opt->copyright = AC3ENC_OPT_OFF;
2093  /* default original */
2094  if (opt->original == AC3ENC_OPT_NONE)
2095  opt->original = AC3ENC_OPT_ON;
2096  }
2097 
2098  /* dolby surround mode default */
2099  if (!s->eac3 || opt->eac3_info_metadata) {
2102  }
2103 
2104  /* validate audio production info */
2105  if (opt->audio_production_info) {
2106  if (opt->mixing_level == AC3ENC_OPT_NONE) {
2107  av_log(avctx, AV_LOG_ERROR, "mixing_level must be set if "
2108  "room_type is set\n");
2109  return AVERROR(EINVAL);
2110  }
2111  if (opt->mixing_level < 80) {
2112  av_log(avctx, AV_LOG_ERROR, "invalid mixing level. must be between "
2113  "80dB and 111dB\n");
2114  return AVERROR(EINVAL);
2115  }
2116  /* default room type */
2117  if (opt->room_type == AC3ENC_OPT_NONE)
2119  }
2120 
2121  /* set bitstream id for alternate bitstream syntax */
2122  if (!s->eac3 && (opt->extended_bsi_1 || opt->extended_bsi_2)) {
2123  if (s->bitstream_id > 8 && s->bitstream_id < 11) {
2124  if (!s->warned_alternate_bitstream) {
2125  av_log(avctx, AV_LOG_WARNING, "alternate bitstream syntax is "
2126  "not compatible with reduced samplerates. writing of "
2127  "extended bitstream information will be disabled.\n");
2128  s->warned_alternate_bitstream = 1;
2129  }
2130  } else {
2131  s->bitstream_id = 6;
2132  }
2133  }
2134 
2135  return 0;
2136 }
2137 
2138 
2139 /**
2140  * Finalize encoding and free any memory allocated by the encoder.
2141  *
2142  * @param avctx Codec context
2143  */
2145 {
2146  int blk, ch;
2147  AC3EncodeContext *s = avctx->priv_data;
2148 
2149  av_freep(&s->mdct_window);
2150  av_freep(&s->windowed_samples);
2151  if (s->planar_samples)
2152  for (ch = 0; ch < s->channels; ch++)
2153  av_freep(&s->planar_samples[ch]);
2154  av_freep(&s->planar_samples);
2155  av_freep(&s->bap_buffer);
2156  av_freep(&s->bap1_buffer);
2157  av_freep(&s->mdct_coef_buffer);
2158  av_freep(&s->fixed_coef_buffer);
2159  av_freep(&s->exp_buffer);
2160  av_freep(&s->grouped_exp_buffer);
2161  av_freep(&s->psd_buffer);
2162  av_freep(&s->band_psd_buffer);
2163  av_freep(&s->mask_buffer);
2164  av_freep(&s->qmant_buffer);
2165  av_freep(&s->cpl_coord_exp_buffer);
2166  av_freep(&s->cpl_coord_mant_buffer);
2167  av_freep(&s->fdsp);
2168  for (blk = 0; blk < s->num_blocks; blk++) {
2169  AC3Block *block = &s->blocks[blk];
2170  av_freep(&block->mdct_coef);
2171  av_freep(&block->fixed_coef);
2172  av_freep(&block->exp);
2173  av_freep(&block->grouped_exp);
2174  av_freep(&block->psd);
2175  av_freep(&block->band_psd);
2176  av_freep(&block->mask);
2177  av_freep(&block->qmant);
2178  av_freep(&block->cpl_coord_exp);
2179  av_freep(&block->cpl_coord_mant);
2180  }
2181 
2182  av_tx_uninit(&s->tx);
2183 
2184  return 0;
2185 }
2186 
2187 
2188 /*
2189  * Set channel information during initialization.
2190  */
2192 {
2193  AC3EncodeContext *s = avctx->priv_data;
2194  int channels = avctx->ch_layout.nb_channels;
2195  uint64_t mask = avctx->ch_layout.u.mask;
2196 
2198  return AVERROR(EINVAL);
2199  if (mask > 0x7FF)
2200  return AVERROR(EINVAL);
2201 
2202  if (!mask)
2204  mask = avctx->ch_layout.u.mask;
2205 
2206  s->lfe_on = !!(mask & AV_CH_LOW_FREQUENCY);
2207  s->channels = channels;
2208  s->fbw_channels = channels - s->lfe_on;
2209  s->lfe_channel = s->lfe_on ? s->fbw_channels + 1 : -1;
2210  if (s->lfe_on)
2212 
2213  switch (mask) {
2214  case AV_CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;
2215  case AV_CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;
2216  case AV_CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;
2217  case AV_CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;
2218  case AV_CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;
2219  case AV_CH_LAYOUT_QUAD:
2220  case AV_CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;
2221  case AV_CH_LAYOUT_5POINT0:
2222  case AV_CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;
2223  default:
2224  return AVERROR(EINVAL);
2225  }
2226  s->has_center = (s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO;
2227  s->has_surround = s->channel_mode & 0x04;
2228 
2229  s->channel_map = ac3_enc_channel_map[s->channel_mode][s->lfe_on];
2230  if (s->lfe_on)
2233 
2234  return 0;
2235 }
2236 
2237 
2239 {
2240  AVCodecContext *avctx = s->avctx;
2241  int i, ret, max_sr;
2242 
2243  /* validate channel layout */
2244  if (!avctx->ch_layout.nb_channels) {
2245  av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
2246  "encoder will guess the layout, but it "
2247  "might be incorrect.\n");
2248  }
2249  ret = set_channel_info(avctx);
2250  if (ret) {
2251  av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
2252  return ret;
2253  }
2254 
2255  /* validate sample rate */
2256  /* note: max_sr could be changed from 2 to 5 for E-AC-3 once we find a
2257  decoder that supports half sample rate so we can validate that
2258  the generated files are correct. */
2259  max_sr = s->eac3 ? 2 : 8;
2260  for (i = 0; i <= max_sr; i++) {
2261  if ((ff_ac3_sample_rate_tab[i % 3] >> (i / 3)) == avctx->sample_rate)
2262  break;
2263  }
2264  if (i > max_sr) {
2265  av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
2266  return AVERROR(EINVAL);
2267  }
2268  s->sample_rate = avctx->sample_rate;
2269  s->bit_alloc.sr_shift = i / 3;
2270  s->bit_alloc.sr_code = i % 3;
2271  s->bitstream_id = s->eac3 ? 16 : 8 + s->bit_alloc.sr_shift;
2272 
2273  /* select a default bit rate if not set by the user */
2274  if (!avctx->bit_rate) {
2275  switch (s->fbw_channels) {
2276  case 1: avctx->bit_rate = 96000; break;
2277  case 2: avctx->bit_rate = 192000; break;
2278  case 3: avctx->bit_rate = 320000; break;
2279  case 4: avctx->bit_rate = 384000; break;
2280  case 5: avctx->bit_rate = 448000; break;
2281  }
2282  }
2283 
2284  /* validate bit rate */
2285  if (s->eac3) {
2286  int max_br, min_br, wpf, min_br_code;
2287  int num_blks_code, num_blocks, frame_samples;
2288  long long min_br_dist;
2289 
2290  /* calculate min/max bitrate */
2291  /* TODO: More testing with 3 and 2 blocks. All E-AC-3 samples I've
2292  found use either 6 blocks or 1 block, even though 2 or 3 blocks
2293  would work as far as the bit rate is concerned. */
2294  for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2295  num_blocks = ((int[]){ 1, 2, 3, 6 })[num_blks_code];
2296  frame_samples = AC3_BLOCK_SIZE * num_blocks;
2297  max_br = 2048 * s->sample_rate / frame_samples * 16;
2298  min_br = ((s->sample_rate + (frame_samples-1)) / frame_samples) * 16;
2299  if (avctx->bit_rate <= max_br)
2300  break;
2301  }
2302  if (avctx->bit_rate < min_br || avctx->bit_rate > max_br) {
2303  av_log(avctx, AV_LOG_ERROR, "invalid bit rate. must be %d to %d "
2304  "for this sample rate\n", min_br, max_br);
2305  return AVERROR(EINVAL);
2306  }
2307  s->num_blks_code = num_blks_code;
2308  s->num_blocks = num_blocks;
2309 
2310  /* calculate words-per-frame for the selected bitrate */
2311  wpf = (avctx->bit_rate / 16) * frame_samples / s->sample_rate;
2312  av_assert1(wpf > 0 && wpf <= 2048);
2313 
2314  /* find the closest AC-3 bitrate code to the selected bitrate.
2315  this is needed for lookup tables for bandwidth and coupling
2316  parameter selection */
2317  min_br_code = -1;
2318  min_br_dist = INT64_MAX;
2319  for (i = 0; i < 19; i++) {
2320  long long br_dist = llabs(ff_ac3_bitrate_tab[i] * 1000 - avctx->bit_rate);
2321  if (br_dist < min_br_dist) {
2322  min_br_dist = br_dist;
2323  min_br_code = i;
2324  }
2325  }
2326 
2327  /* make sure the minimum frame size is below the average frame size */
2328  s->frame_size_code = min_br_code << 1;
2329  while (wpf > 1 && wpf * s->sample_rate / AC3_FRAME_SIZE * 16 > avctx->bit_rate)
2330  wpf--;
2331  s->frame_size_min = 2 * wpf;
2332  } else {
2333  int best_br = 0, best_code = 0;
2334  long long best_diff = INT64_MAX;
2335  for (i = 0; i < 19; i++) {
2336  int br = (ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift) * 1000;
2337  long long diff = llabs(br - avctx->bit_rate);
2338  if (diff < best_diff) {
2339  best_br = br;
2340  best_code = i;
2341  best_diff = diff;
2342  }
2343  if (!best_diff)
2344  break;
2345  }
2346  avctx->bit_rate = best_br;
2347  s->frame_size_code = best_code << 1;
2348  s->frame_size_min = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
2349  s->num_blks_code = 0x3;
2350  s->num_blocks = 6;
2351  }
2352  s->bit_rate = avctx->bit_rate;
2353  s->frame_size = s->frame_size_min;
2354 
2355  /* validate cutoff */
2356  if (avctx->cutoff < 0) {
2357  av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\n");
2358  return AVERROR(EINVAL);
2359  }
2360  s->cutoff = avctx->cutoff;
2361  if (s->cutoff > (s->sample_rate >> 1))
2362  s->cutoff = s->sample_rate >> 1;
2363 
2365  if (ret)
2366  return ret;
2367 
2368  s->rematrixing_enabled = s->options.stereo_rematrixing &&
2369  (s->channel_mode == AC3_CHMODE_STEREO);
2370 
2371  s->cpl_enabled = s->options.channel_coupling &&
2372  s->channel_mode >= AC3_CHMODE_STEREO;
2373 
2374  return 0;
2375 }
2376 
2377 
2378 /*
2379  * Set bandwidth for all channels.
2380  * The user can optionally supply a cutoff frequency. Otherwise an appropriate
2381  * default value will be used.
2382  */
2384 {
2385  int blk, ch, av_uninit(cpl_start);
2386 
2387  if (s->cutoff) {
2388  /* calculate bandwidth based on user-specified cutoff frequency */
2389  int fbw_coeffs;
2390  fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
2391  s->bandwidth_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
2392  } else {
2393  /* use default bandwidth setting */
2394  s->bandwidth_code = ac3_bandwidth_tab[s->fbw_channels-1][s->bit_alloc.sr_code][s->frame_size_code/2];
2395  }
2396 
2397  /* set number of coefficients for each channel */
2398  for (ch = 1; ch <= s->fbw_channels; ch++) {
2399  s->start_freq[ch] = 0;
2400  for (blk = 0; blk < s->num_blocks; blk++)
2401  s->blocks[blk].end_freq[ch] = s->bandwidth_code * 3 + 73;
2402  }
2403  /* LFE channel always has 7 coefs */
2404  if (s->lfe_on) {
2405  s->start_freq[s->lfe_channel] = 0;
2406  for (blk = 0; blk < s->num_blocks; blk++)
2407  s->blocks[blk].end_freq[ch] = 7;
2408  }
2409 
2410  /* initialize coupling strategy */
2411  if (s->cpl_enabled) {
2412  if (s->options.cpl_start != AC3ENC_OPT_AUTO) {
2413  cpl_start = s->options.cpl_start;
2414  } else {
2415  cpl_start = ac3_coupling_start_tab[s->channel_mode-2][s->bit_alloc.sr_code][s->frame_size_code/2];
2416  if (cpl_start < 0) {
2417  if (s->options.channel_coupling == AC3ENC_OPT_AUTO)
2418  s->cpl_enabled = 0;
2419  else
2420  cpl_start = 15;
2421  }
2422  }
2423  }
2424  if (s->cpl_enabled) {
2425  int i, cpl_start_band, cpl_end_band;
2426  uint8_t *cpl_band_sizes = s->cpl_band_sizes;
2427 
2428  cpl_end_band = s->bandwidth_code / 4 + 3;
2429  cpl_start_band = av_clip(cpl_start, 0, FFMIN(cpl_end_band-1, 15));
2430 
2431  s->num_cpl_subbands = cpl_end_band - cpl_start_band;
2432 
2433  s->num_cpl_bands = 1;
2434  *cpl_band_sizes = 12;
2435  for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
2437  *cpl_band_sizes += 12;
2438  } else {
2439  s->num_cpl_bands++;
2440  cpl_band_sizes++;
2441  *cpl_band_sizes = 12;
2442  }
2443  }
2444 
2445  s->start_freq[CPL_CH] = cpl_start_band * 12 + 37;
2446  s->cpl_end_freq = cpl_end_band * 12 + 37;
2447  for (blk = 0; blk < s->num_blocks; blk++)
2448  s->blocks[blk].end_freq[CPL_CH] = s->cpl_end_freq;
2449  }
2450 }
2451 
2452 
2454 {
2455  int blk, ch;
2456  int channels = s->channels + 1; /* includes coupling channel */
2457  int channel_blocks = channels * s->num_blocks;
2458  int total_coefs = AC3_MAX_COEFS * channel_blocks;
2459 
2460  if (s->allocate_sample_buffers(s))
2461  return AVERROR(ENOMEM);
2462 
2463  if (!FF_ALLOC_TYPED_ARRAY(s->bap_buffer, total_coefs) ||
2464  !FF_ALLOC_TYPED_ARRAY(s->bap1_buffer, total_coefs) ||
2465  !FF_ALLOCZ_TYPED_ARRAY(s->mdct_coef_buffer, total_coefs) ||
2466  !FF_ALLOC_TYPED_ARRAY(s->exp_buffer, total_coefs) ||
2467  !FF_ALLOC_TYPED_ARRAY(s->grouped_exp_buffer, channel_blocks * 128) ||
2468  !FF_ALLOC_TYPED_ARRAY(s->psd_buffer, total_coefs) ||
2469  !FF_ALLOC_TYPED_ARRAY(s->band_psd_buffer, channel_blocks * 64) ||
2470  !FF_ALLOC_TYPED_ARRAY(s->mask_buffer, channel_blocks * 64) ||
2471  !FF_ALLOC_TYPED_ARRAY(s->qmant_buffer, total_coefs))
2472  return AVERROR(ENOMEM);
2473 
2474  if (s->cpl_enabled) {
2475  if (!FF_ALLOC_TYPED_ARRAY(s->cpl_coord_exp_buffer, channel_blocks * 16) ||
2476  !FF_ALLOC_TYPED_ARRAY(s->cpl_coord_mant_buffer, channel_blocks * 16))
2477  return AVERROR(ENOMEM);
2478  }
2479  for (blk = 0; blk < s->num_blocks; blk++) {
2480  AC3Block *block = &s->blocks[blk];
2481 
2482  if (!FF_ALLOCZ_TYPED_ARRAY(block->mdct_coef, channels) ||
2484  !FF_ALLOCZ_TYPED_ARRAY(block->grouped_exp, channels) ||
2486  !FF_ALLOCZ_TYPED_ARRAY(block->band_psd, channels) ||
2489  return AVERROR(ENOMEM);
2490 
2491  if (s->cpl_enabled) {
2492  if (!FF_ALLOCZ_TYPED_ARRAY(block->cpl_coord_exp, channels) ||
2493  !FF_ALLOCZ_TYPED_ARRAY(block->cpl_coord_mant, channels))
2494  return AVERROR(ENOMEM);
2495  }
2496 
2497  for (ch = 0; ch < channels; ch++) {
2498  /* arrangement: block, channel, coeff */
2499  block->grouped_exp[ch] = &s->grouped_exp_buffer[128 * (blk * channels + ch)];
2500  block->psd[ch] = &s->psd_buffer [AC3_MAX_COEFS * (blk * channels + ch)];
2501  block->band_psd[ch] = &s->band_psd_buffer [64 * (blk * channels + ch)];
2502  block->mask[ch] = &s->mask_buffer [64 * (blk * channels + ch)];
2503  block->qmant[ch] = &s->qmant_buffer [AC3_MAX_COEFS * (blk * channels + ch)];
2504  if (s->cpl_enabled) {
2505  block->cpl_coord_exp[ch] = &s->cpl_coord_exp_buffer [16 * (blk * channels + ch)];
2506  block->cpl_coord_mant[ch] = &s->cpl_coord_mant_buffer[16 * (blk * channels + ch)];
2507  }
2508 
2509  /* arrangement: channel, block, coeff */
2510  block->exp[ch] = &s->exp_buffer [AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
2511  block->mdct_coef[ch] = &s->mdct_coef_buffer [AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
2512  }
2513  }
2514 
2515  if (!s->fixed_point) {
2516  if (!FF_ALLOCZ_TYPED_ARRAY(s->fixed_coef_buffer, total_coefs))
2517  return AVERROR(ENOMEM);
2518  for (blk = 0; blk < s->num_blocks; blk++) {
2519  AC3Block *block = &s->blocks[blk];
2520  if (!FF_ALLOCZ_TYPED_ARRAY(block->fixed_coef, channels))
2521  return AVERROR(ENOMEM);
2522  for (ch = 0; ch < channels; ch++)
2523  block->fixed_coef[ch] = &s->fixed_coef_buffer[AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
2524  }
2525  } else {
2526  for (blk = 0; blk < s->num_blocks; blk++) {
2527  AC3Block *block = &s->blocks[blk];
2528  if (!FF_ALLOCZ_TYPED_ARRAY(block->fixed_coef, channels))
2529  return AVERROR(ENOMEM);
2530  for (ch = 0; ch < channels; ch++)
2531  block->fixed_coef[ch] = (int32_t *)block->mdct_coef[ch];
2532  }
2533  }
2534 
2535  return 0;
2536 }
2537 
2538 
2540 {
2541  static AVOnce init_static_once = AV_ONCE_INIT;
2542  AC3EncodeContext *s = avctx->priv_data;
2543  int ret, frame_size_58;
2544 
2545  s->avctx = avctx;
2546 
2547  s->eac3 = avctx->codec_id == AV_CODEC_ID_EAC3;
2548 
2549  ret = validate_options(s);
2550  if (ret)
2551  return ret;
2552 
2553  avctx->frame_size = AC3_BLOCK_SIZE * s->num_blocks;
2555 
2556  s->bitstream_mode = avctx->audio_service_type;
2557  if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)
2558  s->bitstream_mode = 0x7;
2559 
2560  s->bits_written = 0;
2561  s->samples_written = 0;
2562 
2563  /* calculate crc_inv for both possible frame sizes */
2564  frame_size_58 = (( s->frame_size >> 2) + ( s->frame_size >> 4)) << 1;
2565  s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
2566  if (s->bit_alloc.sr_code == 1) {
2567  frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;
2568  s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
2569  }
2570 
2571  if (CONFIG_EAC3_ENCODER && s->eac3) {
2572  static AVOnce init_static_once_eac3 = AV_ONCE_INIT;
2573  ff_thread_once(&init_static_once_eac3, ff_eac3_exponent_init);
2574  s->output_frame_header = ff_eac3_output_frame_header;
2575  } else
2576  s->output_frame_header = ac3_output_frame_header;
2577 
2578  set_bandwidth(s);
2579 
2580  bit_alloc_init(s);
2581 
2582  ret = s->mdct_init(s);
2583  if (ret)
2584  return ret;
2585 
2586  ret = allocate_buffers(s);
2587  if (ret)
2588  return ret;
2589 
2590  ff_audiodsp_init(&s->adsp);
2591  ff_me_cmp_init(&s->mecc, avctx);
2592  ff_ac3dsp_init(&s->ac3dsp);
2593 
2594  dprint_options(s);
2595 
2596  ff_thread_once(&init_static_once, exponent_init);
2597 
2598  return 0;
2599 }
FF_ALLOCZ_TYPED_ARRAY
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
Definition: internal.h:88
frame_samples
static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
Definition: sync_queue.c:141
OFFSET
#define OFFSET(param)
Definition: ac3enc.c:79
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1077
AC3_CHMODE_3F
@ AC3_CHMODE_3F
Definition: ac3defs.h:58
nb_coefs
static int nb_coefs(int length, int level, uint64_t sn)
Definition: af_afwtdn.c:514
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
ff_ac3_fast_decay_tab
const uint8_t ff_ac3_fast_decay_tab[4]
Definition: ac3tab.c:130
EXP_D45
#define EXP_D45
Definition: ac3defs.h:43
AV_CH_LAYOUT_5POINT0_BACK
#define AV_CH_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:216
av_clip
#define av_clip
Definition: common.h:98
AC3EncOptions::mixing_level
int mixing_level
Definition: ac3enc.h:103
ac3_compute_bit_allocation
static int ac3_compute_bit_allocation(AC3EncodeContext *s)
Definition: ac3enc.c:1242
r
const char * r
Definition: vf_curves.c:126
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVChannelLayout::u
union AVChannelLayout::@349 u
Details about which channels are present in this layout.
cmixlev_options
static const float cmixlev_options[CMIXLEV_NUM_OPTIONS]
Definition: ac3enc.c:61
AVCodecContext::audio_service_type
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:1097
AC3EncOptions::dolby_headphone_mode
int dolby_headphone_mode
Definition: ac3enc.h:115
ac3_output_frame_header
static void ac3_output_frame_header(AC3EncodeContext *s)
Definition: ac3enc.c:1426
mem_internal.h
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:379
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
LEVEL_MINUS_6DB
#define LEVEL_MINUS_6DB
Definition: ac3.h:90
AVCRC
uint32_t AVCRC
Definition: crc.h:46
thread.h
ff_ac3_compute_coupling_strategy
void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
Set the initial coupling strategy parameters prior to coupling analysis.
Definition: ac3enc.c:300
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:204
exp_strategy_reuse_tab
static const uint8_t exp_strategy_reuse_tab[4][6]
Table used to select exponent strategy based on exponent reuse block interval.
Definition: ac3enc.c:443
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:281
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_2_2
Definition: channel_layout.h:386
COMMON_CHANNEL_MAP
#define COMMON_CHANNEL_MAP
Definition: ac3tab.h:48
AC3EncOptions::ltrt_surround_mix_level
float ltrt_surround_mix_level
Definition: ac3enc.h:110
AC3EncOptions::dialogue_level
int dialogue_level
Definition: ac3enc.h:97
surmixlev_options
static const float surmixlev_options[SURMIXLEV_NUM_OPTIONS]
Definition: ac3enc.c:66
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:222
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
AC3EncOptions::dolby_surround_mode
int dolby_surround_mode
Definition: ac3enc.h:101
AC3Mant::mant1_cnt
int mant1_cnt
Definition: ac3enc.c:57
count_mantissa_bits_init
static void count_mantissa_bits_init(uint16_t mant_cnt[AC3_MAX_BLOCKS][16])
Initialize mantissa counts.
Definition: ac3enc.c:1092
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:346
encode.h
b
#define b
Definition: input.c:41
AV_AUDIO_SERVICE_TYPE_VOICE_OVER
@ AV_AUDIO_SERVICE_TYPE_VOICE_OVER
Definition: defs.h:230
LEVEL_MINUS_4POINT5DB
#define LEVEL_MINUS_4POINT5DB
Definition: ac3.h:89
ff_audiodsp_init
av_cold void ff_audiodsp_init(AudioDSPContext *c)
Definition: audiodsp.c:106
AC3EncOptions::center_mix_level
float center_mix_level
Definition: ac3enc.h:99
ff_eac3_get_frame_exp_strategy
void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
Determine frame exponent strategy use and indices.
Definition: eac3enc.c:66
AVChannelLayout::mask
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
Definition: channel_layout.h:335
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
ff_ac3_validate_metadata
int ff_ac3_validate_metadata(AC3EncodeContext *s)
Validate metadata options as set by AVOption system.
Definition: ac3enc.c:1961
validate_mix_level
static void validate_mix_level(void *log_ctx, const char *opt_name, float *opt_param, const float *list, int list_size, int default_value, int min_value, int *ctx_param)
Definition: ac3enc.c:1937
exponent_group_tab
static uint8_t exponent_group_tab[2][3][256]
LUT for number of exponent groups.
Definition: ac3enc.c:147
AC3Mant::mant2_cnt
int mant2_cnt
Definition: ac3enc.c:57
ac3_process_exponents
static void ac3_process_exponents(AC3EncodeContext *s)
Calculate final exponents from the supplied MDCT coefficients and exponent shift.
Definition: ac3enc.c:733
ff_ac3dsp_init
av_cold void ff_ac3dsp_init(AC3DSPContext *c)
Definition: ac3dsp.c:377
count_mantissa_bits
static int count_mantissa_bits(AC3EncodeContext *s)
Definition: ac3enc.c:1134
ff_ac3_enc_options
const AVOption ff_ac3_enc_options[]
Definition: ac3enc.c:81
crc.h
FFCodecDefault
Definition: codec_internal.h:97
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
AC3Mant::qmant4_ptr
int16_t * qmant4_ptr
mantissa pointers for bap=1,2,4
Definition: ac3enc.c:56
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1122
AC3_CHMODE_3F1R
@ AC3_CHMODE_3F1R
Definition: ac3defs.h:60
ff_me_cmp_init
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
Definition: me_cmp.c:1008
FF_ALLOC_TYPED_ARRAY
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
Definition: internal.h:87
ff_ac3_bap_tab
const uint8_t ff_ac3_bap_tab[64]
Definition: ac3tab.c:116
LEVEL_PLUS_3DB
#define LEVEL_PLUS_3DB
Definition: ac3.h:85
EXP_REUSE
#define EXP_REUSE
Definition: ac3defs.h:38
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:205
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:382
ac3_quantize_mantissas
static void ac3_quantize_mantissas(AC3EncodeContext *s)
Quantize mantissas using coefficients, exponents, and bit allocation pointers.
Definition: ac3enc.c:1397
bit_alloc
static int bit_alloc(AC3EncodeContext *s, int snr_offset)
Run the bit allocation with a given SNR offset.
Definition: ac3enc.c:1160
AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_QUAD
Definition: channel_layout.h:213
set_bandwidth
static av_cold void set_bandwidth(AC3EncodeContext *s)
Definition: ac3enc.c:2383
ff_ac3_ch_layouts
const AVChannelLayout ff_ac3_ch_layouts[19]
List of supported channel layouts.
Definition: ac3enc.c:153
avassert.h
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AC3EncOptions::eac3_mixing_metadata
int eac3_mixing_metadata
Definition: ac3enc.h:117
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:171
mask
static const uint16_t mask[17]
Definition: lzw.c:38
av_channel_layout_describe
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
Definition: channel_layout.c:644
AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_4POINT0
Definition: channel_layout.h:384
emms_c
#define emms_c()
Definition: emms.h:63
EXP_NEW
#define EXP_NEW
Definition: ac3defs.h:39
ac3_apply_rematrixing
static void ac3_apply_rematrixing(AC3EncodeContext *s)
Apply stereo rematrixing to coefficients based on rematrixing flags.
Definition: ac3enc.c:371
s
#define s(width, name)
Definition: cbs_vp9.c:198
EXTMIXLEV_NUM_OPTIONS
#define EXTMIXLEV_NUM_OPTIONS
Definition: ac3enc.c:70
LEVEL_MINUS_3DB
#define LEVEL_MINUS_3DB
Definition: ac3.h:88
validate_float_option
static int validate_float_option(float v, const float *v_list, int v_list_size)
Definition: ac3enc.c:1921
AC3_MAX_COEFS
#define AC3_MAX_COEFS
Definition: ac3defs.h:29
av_channel_layout_from_mask
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:242
quantize_mantissas_blk_ch
static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef, uint8_t *exp, uint8_t *bap, int16_t *qmant, int start_freq, int end_freq)
Quantize a set of mantissas for a single channel in a single block.
Definition: ac3enc.c:1302
AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:390
ff_ac3_floor_tab
const int16_t ff_ac3_floor_tab[8]
Definition: ac3tab.c:142
LOCAL_ALIGNED_16
#define LOCAL_ALIGNED_16(t, v,...)
Definition: mem_internal.h:150
encode_exponents_blk_ch
static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy, int cpl)
Update the exponents so that they are the ones the decoder will decode.
Definition: ac3enc.c:520
ac3_group_exponents
static void ac3_group_exponents(AC3EncodeContext *s)
Group exponents.
Definition: ac3enc.c:675
extmixlev_options
static const float extmixlev_options[EXTMIXLEV_NUM_OPTIONS]
Definition: ac3enc.c:71
channels
channels
Definition: aptx.h:31
AC3EncOptions::audio_production_info
int audio_production_info
Definition: ac3enc.h:102
blk
#define blk(i)
Definition: sha.c:186
av_get_sample_fmt_name
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:51
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
AV_CH_LAYOUT_2_1
#define AV_CH_LAYOUT_2_1
Definition: channel_layout.h:207
if
if(ret)
Definition: filter_design.txt:179
AV_CRC_16_ANSI
@ AV_CRC_16_ANSI
Definition: crc.h:50
SURMIXLEV_NUM_OPTIONS
#define SURMIXLEV_NUM_OPTIONS
Definition: ac3enc.c:65
ac3defs.h
EXP_D15
#define EXP_D15
Definition: ac3defs.h:41
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
AC3EncOptions::ad_converter_type
int ad_converter_type
Definition: ac3enc.h:116
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
AC3ENC_OPT_AUTO
#define AC3ENC_OPT_AUTO
Definition: ac3enc.h:74
NULL
#define NULL
Definition: coverity.c:32
AC3_FRAME_SIZE
#define AC3_FRAME_SIZE
Definition: ac3defs.h:32
bits_left
#define bits_left
Definition: bitstream.h:114
ac3enc.h
extract_exponents
static void extract_exponents(AC3EncodeContext *s)
Definition: ac3enc.c:424
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:403
AC3ENC_OPT_DOWNMIX_DPLII
#define AC3ENC_OPT_DOWNMIX_DPLII
Definition: ac3enc.h:87
ff_samples_to_time_base
static av_always_inline int64_t ff_samples_to_time_base(const AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: encode.h:90
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:495
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
DBA_NONE
@ DBA_NONE
Definition: ac3defs.h:49
AC3ENC_OPT_NONE
#define AC3ENC_OPT_NONE
Definition: ac3enc.h:73
list
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining list
Definition: filter_design.txt:25
ac3dsp.h
set_channel_info
static av_cold int set_channel_info(AVCodecContext *avctx)
Definition: ac3enc.c:2191
count_frame_bits
static void count_frame_bits(AC3EncodeContext *s)
Definition: ac3enc.c:894
AV_AUDIO_SERVICE_TYPE_EMERGENCY
@ AV_AUDIO_SERVICE_TYPE_EMERGENCY
Definition: defs.h:229
AC3EncodeContext
AC-3 encoder private context.
Definition: ac3enc.h:158
AC3_MAX_CHANNELS
#define AC3_MAX_CHANNELS
maximum number of channels, including coupling channel
Definition: ac3defs.h:26
exp
int8_t exp
Definition: eval.c:74
AC3EncOptions::extended_bsi_1
int extended_bsi_1
Definition: ac3enc.h:107
AC3EncOptions::copyright
int copyright
Definition: ac3enc.h:105
AC3ENC_OPT_DOWNMIX_LORO
#define AC3ENC_OPT_DOWNMIX_LORO
Definition: ac3enc.h:86
AVOnce
#define AVOnce
Definition: thread.h:202
AC3Block
Data for a single audio block.
Definition: ac3enc.h:130
ff_ac3_adjust_frame_size
void ff_ac3_adjust_frame_size(AC3EncodeContext *s)
Adjust the frame size to make the average bit rate match the target bit rate.
Definition: ac3enc.c:282
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:480
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
ac3_bandwidth_tab
static const uint8_t ac3_bandwidth_tab[5][3][19]
LUT to select the bandwidth code based on the bit rate, sample rate, and number of full-bandwidth cha...
Definition: ac3enc.c:208
pow_poly
static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
Definition: ac3enc.c:1692
AC3_CHMODE_STEREO
@ AC3_CHMODE_STEREO
Definition: ac3defs.h:57
AC3EncOptions::eac3_info_metadata
int eac3_info_metadata
Definition: ac3enc.h:118
AC3EncOptions::dolby_surround_ex_mode
int dolby_surround_ex_mode
Definition: ac3enc.h:114
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
codec_internal.h
AC3_BLOCK_SIZE
#define AC3_BLOCK_SIZE
Definition: ac3defs.h:30
ff_eac3_output_frame_header
void ff_eac3_output_frame_header(AC3EncodeContext *s)
Write the E-AC-3 frame header to the output bitstream.
Definition: eac3enc.c:126
ff_ac3_db_per_bit_tab
const uint16_t ff_ac3_db_per_bit_tab[4]
Definition: ac3tab.c:138
AC3ENC_OPT_ADCONV_HDCD
#define AC3ENC_OPT_ADCONV_HDCD
Definition: ac3enc.h:89
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
bit_alloc_masking
static void bit_alloc_masking(AC3EncodeContext *s)
Definition: ac3enc.c:1037
LEVEL_MINUS_1POINT5DB
#define LEVEL_MINUS_1POINT5DB
Definition: ac3.h:87
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AC3ENC_OPT_ON
#define AC3ENC_OPT_ON
Definition: ac3enc.h:76
AC3EncOptions::room_type
int room_type
Definition: ac3enc.h:104
EXP_DIFF_THRESHOLD
#define EXP_DIFF_THRESHOLD
Exponent Difference Threshold.
Definition: ac3enc.c:438
AC3EncOptions
Encoding Options used by AVOption.
Definition: ac3enc.h:95
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:164
AC3ENC_OPT_SMALL_ROOM
#define AC3ENC_OPT_SMALL_ROOM
Definition: ac3enc.h:84
ac3_output_frame
static void ac3_output_frame(AC3EncodeContext *s, unsigned char *frame)
Write the frame to the output bitstream.
Definition: ac3enc.c:1758
AC3ENC_OPT_ADCONV_STANDARD
#define AC3ENC_OPT_ADCONV_STANDARD
Definition: ac3enc.h:88
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
LEVEL_ONE
#define LEVEL_ONE
Definition: ac3.h:93
CMIXLEV_NUM_OPTIONS
#define CMIXLEV_NUM_OPTIONS
Definition: ac3enc.c:60
sym_quant
static int sym_quant(int c, int e, int levels)
Symmetric quantization on 'levels' levels.
Definition: ac3enc.c:1262
attributes.h
AV_CHANNEL_ORDER_NATIVE
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
Definition: channel_layout.h:118
ac3_coupling_start_tab
static const int8_t ac3_coupling_start_tab[6][3][19]
LUT to select the coupling start band based on the bit rate, sample rate, and number of full-bandwidt...
Definition: ac3enc.c:241
bit_alloc_init
static av_cold void bit_alloc_init(AC3EncodeContext *s)
Definition: ac3enc.c:858
eac3enc.h
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:294
CRC16_POLY
#define CRC16_POLY
CRC-16 Polynomial.
Definition: ac3enc.c:1672
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:238
LEVEL_PLUS_1POINT5DB
#define LEVEL_PLUS_1POINT5DB
Definition: ac3.h:86
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:830
ff_ac3_rematrix_band_tab
const uint8_t ff_ac3_rematrix_band_tab[5]
Table of bin locations for rematrixing bands reference: Section 7.5.2 Rematrixing : Frequency Band De...
Definition: ac3tab.c:107
emms.h
AV_CH_LAYOUT_5POINT0
#define AV_CH_LAYOUT_5POINT0
Definition: channel_layout.h:214
CPL_CH
#define CPL_CH
coupling channel index
Definition: ac3defs.h:27
AC3Mant::qmant2_ptr
int16_t * qmant2_ptr
Definition: ac3enc.c:56
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
ff_ac3_sample_rate_tab
const int ff_ac3_sample_rate_tab[]
Definition: ac3tab.c:95
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
put_bits_count
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:80
AV_CHANNEL_LAYOUT_QUAD
#define AV_CHANNEL_LAYOUT_QUAD
Definition: channel_layout.h:387
internal.h
dprint_options
static void dprint_options(AC3EncodeContext *s)
Definition: ac3enc.c:1804
AC3ENC_OPT_OFF
#define AC3ENC_OPT_OFF
Definition: ac3enc.h:75
AVCodecContext::cutoff
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:1090
output_frame_end
static void output_frame_end(AC3EncodeContext *s)
Definition: ac3enc.c:1709
count_frame_bits_fixed
static void count_frame_bits_fixed(AC3EncodeContext *s)
Definition: ac3enc.c:749
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
mul_poly
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
Definition: ac3enc.c:1675
ff_ac3_slow_decay_tab
const uint8_t ff_ac3_slow_decay_tab[4]
Definition: ac3tab.c:126
AC3EncOptions::original
int original
Definition: ac3enc.h:106
AC3Mant::mant4_cnt
int mant4_cnt
mantissa counts for bap=1,2,4
Definition: ac3enc.c:57
compute_exp_strategy
static void compute_exp_strategy(AC3EncodeContext *s)
Definition: ac3enc.c:454
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AC3_MAX_BLOCKS
#define AC3_MAX_BLOCKS
Definition: ac3defs.h:31
AC3Mant
Definition: ac3enc.c:55
AC3ENC_PARAM
#define AC3ENC_PARAM
Definition: ac3enc.c:80
ff_ac3_frame_size_tab
const uint16_t ff_ac3_frame_size_tab[38][3]
Possible frame sizes.
Definition: ac3tab.c:35
AC3ENC_OPT_MODE_OFF
#define AC3ENC_OPT_MODE_OFF
Definition: ac3enc.h:79
AC3_CHMODE_MONO
@ AC3_CHMODE_MONO
Definition: ac3defs.h:56
avcodec.h
AC3_CHMODE_3F2R
@ AC3_CHMODE_3F2R
Definition: ac3defs.h:62
AC3_CHMODE_2F1R
@ AC3_CHMODE_2F1R
Definition: ac3defs.h:59
av_uninit
#define av_uninit(x)
Definition: attributes.h:154
ret
ret
Definition: filter_design.txt:187
LEVEL_ZERO
#define LEVEL_ZERO
Definition: ac3.h:92
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_SURROUND
Definition: channel_layout.h:208
AC3EncOptions::loro_surround_mix_level
float loro_surround_mix_level
Definition: ac3enc.h:112
AC3ENC_OPT_LARGE_ROOM
#define AC3ENC_OPT_LARGE_ROOM
Definition: ac3enc.h:83
AC3_CHMODE_2F2R
@ AC3_CHMODE_2F2R
Definition: ac3defs.h:61
ff_eac3_exponent_init
av_cold void ff_eac3_exponent_init(void)
Initialize E-AC-3 exponent tables.
Definition: eac3enc.c:50
AV_AUDIO_SERVICE_TYPE_KARAOKE
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition: defs.h:231
AC3EncOptions::ltrt_center_mix_level
float ltrt_center_mix_level
Definition: ac3enc.h:109
me_cmp.h
AC3EncOptions::preferred_stereo_downmix
int preferred_stereo_downmix
Definition: ac3enc.h:108
AV_CHANNEL_LAYOUT_2_1
#define AV_CHANNEL_LAYOUT_2_1
Definition: channel_layout.h:381
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AC3EncOptions::allow_per_frame_metadata
int allow_per_frame_metadata
Definition: ac3enc.h:121
put_bits_ptr
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:377
channel_layout.h
asym_quant
static int asym_quant(int c, int e, int qbits)
Asymmetric quantization on 2^qbits levels.
Definition: ac3enc.c:1278
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:105
av_crc
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:392
FLT_OPTION_THRESHOLD
#define FLT_OPTION_THRESHOLD
Definition: ac3enc.c:1919
output_audio_block
static void output_audio_block(AC3EncodeContext *s, int blk)
Definition: ac3enc.c:1482
ff_ac3_bitrate_tab
const uint16_t ff_ac3_bitrate_tab[19]
Definition: ac3tab.c:98
AV_AUDIO_SERVICE_TYPE_COMMENTARY
@ AV_AUDIO_SERVICE_TYPE_COMMENTARY
Definition: defs.h:228
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
allocate_buffers
static av_cold int allocate_buffers(AC3EncodeContext *s)
Definition: ac3enc.c:2453
ff_ac3enc_class
const AVClass ff_ac3enc_class
Definition: ac3enc.c:131
ff_ac3_enc_defaults
const FFCodecDefault ff_ac3_enc_defaults[]
Definition: ac3enc.c:138
ff_ac3_encode_init
av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
Definition: ac3enc.c:2539
ac3_enc_channel_map
static const uint8_t ac3_enc_channel_map[8][2][6]
Table to remap channels from SMPTE order to AC-3 order.
Definition: ac3enc.c:197
audiodsp.h
encode_exponents
static void encode_exponents(AC3EncodeContext *s)
Definition: ac3enc.c:593
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
ff_ac3_bit_alloc_calc_mask
int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd, int start, int end, int fast_gain, int is_lfe, int dba_mode, int dba_nsegs, uint8_t *dba_offsets, uint8_t *dba_lengths, uint8_t *dba_values, int16_t *mask)
Calculate the masking curve.
Definition: ac3.c:201
ff_ac3_slow_gain_tab
const uint16_t ff_ac3_slow_gain_tab[4]
Definition: ac3tab.c:134
AC3ENC_OPT_DSUREX_DPLIIZ
#define AC3ENC_OPT_DSUREX_DPLIIZ
Definition: ac3enc.h:80
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:499
ac3.h
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
count_exponent_bits
static int count_exponent_bits(AC3EncodeContext *s)
Definition: ac3enc.c:643
ff_ac3_fast_gain_tab
const uint16_t ff_ac3_fast_gain_tab[8]
Definition: ac3tab.c:146
reset_block_bap
static void reset_block_bap(AC3EncodeContext *s)
Definition: ac3enc.c:1067
AC3ENC_OPT_NOT_INDICATED
#define AC3ENC_OPT_NOT_INDICATED
Definition: ac3enc.h:77
AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_4POINT0
Definition: channel_layout.h:210
ff_ac3_encode_close
av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
Finalize encoding and free any memory allocated by the encoder.
Definition: ac3enc.c:2144
AC3ENC_OPT_DOWNMIX_LTRT
#define AC3ENC_OPT_DOWNMIX_LTRT
Definition: ac3enc.h:85
int32_t
int32_t
Definition: audioconvert.c:56
AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:391
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
EXP_D25
#define EXP_D25
Definition: ac3defs.h:42
cbr_bit_allocation
static int cbr_bit_allocation(AC3EncodeContext *s)
Definition: ac3enc.c:1191
av_strlcpy
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:85
ff_ac3_encode_frame_common_end
int ff_ac3_encode_frame_common_end(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: ac3enc.c:1772
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AC3EncOptions::surround_mix_level
float surround_mix_level
Definition: ac3enc.h:100
AC3ENC_OPT_MODE_ON
#define AC3ENC_OPT_MODE_ON
Definition: ac3enc.h:78
AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_5POINT0
Definition: channel_layout.h:388
ac3tab.h
avstring.h
AC3EncOptions::extended_bsi_2
int extended_bsi_2
Definition: ac3enc.h:113
AC3Mant::qmant1_ptr
int16_t * qmant1_ptr
Definition: ac3enc.c:56
av_bswap16
#define av_bswap16
Definition: bswap.h:27
AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_5POINT1
Definition: channel_layout.h:389
put_bits.h
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:244
snprintf
#define snprintf
Definition: snprintf.h:34
AV_AUDIO_SERVICE_TYPE_MAIN
@ AV_AUDIO_SERVICE_TYPE_MAIN
Definition: defs.h:223
AV_CH_LAYOUT_2_2
#define AV_CH_LAYOUT_2_2
Definition: channel_layout.h:212
exponent_init
static av_cold void exponent_init(void)
Definition: ac3enc.c:405
validate_options
static av_cold int validate_options(AC3EncodeContext *s)
Definition: ac3enc.c:2238
AC3EncOptions::loro_center_mix_level
float loro_center_mix_level
Definition: ac3enc.h:111
ff_ac3_bit_alloc_calc_psd
void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd, int16_t *band_psd)
Calculate the log power-spectral density of the input signal.
Definition: ac3.c:175
ff_eac3_default_cpl_band_struct
const uint8_t ff_eac3_default_cpl_band_struct[18]
Table E2.16 Default Coupling Banding Structure.
Definition: ac3tab.c:112
count_mantissa_bits_update_ch
static void count_mantissa_bits_update_ch(AC3EncodeContext *s, int ch, uint16_t mant_cnt[AC3_MAX_BLOCKS][16], int start, int end)
Update mantissa bit counts for all blocks in 1 channel in a given bandwidth range.
Definition: ac3enc.c:1114