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