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