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