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