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>
7 * This file is part of FFmpeg.
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.
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.
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
26 * The simplest AC-3 encoder.
31 #include "libavutil/attributes.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/channel_layout.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"
42 #include "codec_internal.h"
43 #include "config_components.h"
55 #define SAMPLETYPE_SIZE(ctx) (sizeof(float) == sizeof(int32_t) ? sizeof(float) : \
56 (ctx)->fixed_point ? sizeof(int32_t) : sizeof(float))
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
63 #define CMIXLEV_NUM_OPTIONS 3
64 static const float cmixlev_options
[CMIXLEV_NUM_OPTIONS
] = {
65 LEVEL_MINUS_3DB
, LEVEL_MINUS_4POINT5DB
, LEVEL_MINUS_6DB
68 #define SURMIXLEV_NUM_OPTIONS 3
69 static const float surmixlev_options
[SURMIXLEV_NUM_OPTIONS
] = {
70 LEVEL_MINUS_3DB
, LEVEL_MINUS_6DB
, LEVEL_ZERO
73 #define EXTMIXLEV_NUM_OPTIONS 8
74 static const float extmixlev_options
[EXTMIXLEV_NUM_OPTIONS
] = {
75 LEVEL_PLUS_3DB
, LEVEL_PLUS_1POINT5DB
, LEVEL_ONE
, LEVEL_MINUS_1POINT5DB
,
76 LEVEL_MINUS_3DB
, LEVEL_MINUS_4POINT5DB
, LEVEL_MINUS_6DB
, LEVEL_ZERO
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)
84 const AVOption ff_ac3_enc_options
[] = {
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"},
134 const AVClass ff_ac3enc_class
= {
135 .class_name
= "AC-3 Encoder",
136 .item_name
= av_default_item_name
,
137 .option
= ff_ac3_enc_options
,
138 .version
= LIBAVUTIL_VERSION_INT
,
141 const FFCodecDefault ff_ac3_enc_defaults
[] = {
147 * LUT for number of exponent groups.
148 * exponent_group_tab[coupling][exponent strategy-1][number of coefficients]
150 static uint8_t exponent_group_tab
[2][3][256];
154 * List of supported channel layouts.
156 const AVChannelLayout ff_ac3_ch_layouts
[19] = {
157 AV_CHANNEL_LAYOUT_MONO
,
158 AV_CHANNEL_LAYOUT_STEREO
,
159 AV_CHANNEL_LAYOUT_2_1
,
160 AV_CHANNEL_LAYOUT_SURROUND
,
161 AV_CHANNEL_LAYOUT_2_2
,
162 AV_CHANNEL_LAYOUT_QUAD
,
163 AV_CHANNEL_LAYOUT_4POINT0
,
164 AV_CHANNEL_LAYOUT_5POINT0
,
165 AV_CHANNEL_LAYOUT_5POINT0_BACK
,
168 .order
= AV_CHANNEL_ORDER_NATIVE
,
169 .u
.mask
= AV_CH_LAYOUT_MONO
| AV_CH_LOW_FREQUENCY
,
173 .order
= AV_CHANNEL_ORDER_NATIVE
,
174 .u
.mask
= AV_CH_LAYOUT_STEREO
| AV_CH_LOW_FREQUENCY
,
178 .order
= AV_CHANNEL_ORDER_NATIVE
,
179 .u
.mask
= AV_CH_LAYOUT_2_1
| AV_CH_LOW_FREQUENCY
,
183 .order
= AV_CHANNEL_ORDER_NATIVE
,
184 .u
.mask
= AV_CH_LAYOUT_SURROUND
| AV_CH_LOW_FREQUENCY
,
188 .order
= AV_CHANNEL_ORDER_NATIVE
,
189 .u
.mask
= AV_CH_LAYOUT_4POINT0
| AV_CH_LOW_FREQUENCY
,
191 AV_CHANNEL_LAYOUT_5POINT1
,
192 AV_CHANNEL_LAYOUT_5POINT1_BACK
,
197 * Table to remap channels from SMPTE order to AC-3 order.
198 * [channel_mode][lfe][ch]
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 } },
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]
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
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 } },
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 } },
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 } },
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 } },
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 } }
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]
241 * TODO: more testing for optimal parameters.
242 * multi-channel tests at 44.1kHz and 32kHz.
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
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 } },
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 } },
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 } },
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 } },
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 } },
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 } },
279 #define FLT_OPTION_THRESHOLD 0.01
281 static int validate_float_option(float v
, const float *v_list
, int v_list_size
)
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
))
290 if (i
== v_list_size
)
291 return AVERROR(EINVAL
);
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
,
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
]);
310 *opt_param
= list
[mixlev
];
316 * Validate metadata options as set by AVOption system.
317 * These values can optionally be changed per-frame.
319 * @param s AC-3 encoder private context
321 static int ac3_validate_metadata(AC3EncodeContext
*s
)
323 AVCodecContext
*avctx
= s
->avctx
;
324 AC3EncOptions
*opt
= &s
->options
;
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;
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;
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;
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;
349 /* determine info metadata use */
350 if (avctx
->audio_service_type
!= AV_AUDIO_SERVICE_TYPE_MAIN
)
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
&&
355 (opt
->dolby_headphone_mode
!= AC3ENC_OPT_NONE
|| opt
->dolby_surround_mode
!= AC3ENC_OPT_NONE
))
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
||
360 opt
->ad_converter_type
!= AC3ENC_OPT_NONE
) {
361 opt
->audio_production_info
= 1;
362 opt
->eac3_info_metadata
= 1;
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;
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;
374 if (opt
->ad_converter_type
!= AC3ENC_OPT_NONE
)
375 opt
->extended_bsi_2
= 1;
378 /* validate AC-3 mixing levels */
381 validate_mix_level(avctx
, "center_mix_level", &opt
->center_mix_level
,
382 cmixlev_options
, CMIXLEV_NUM_OPTIONS
, 1, 0,
383 &s
->center_mix_level
);
385 if (s
->has_surround
) {
386 validate_mix_level(avctx
, "surround_mix_level", &opt
->surround_mix_level
,
387 surmixlev_options
, SURMIXLEV_NUM_OPTIONS
, 1, 0,
388 &s
->surround_mix_level
);
392 /* validate extended bsi 1 / mixing metadata */
393 if (opt
->extended_bsi_1
|| opt
->eac3_mixing_metadata
) {
394 /* default preferred stereo downmix */
395 if (opt
->preferred_stereo_downmix
== AC3ENC_OPT_NONE
)
396 opt
->preferred_stereo_downmix
= AC3ENC_OPT_NOT_INDICATED
;
397 if (!s
->eac3
|| s
->has_center
) {
398 /* validate Lt/Rt center mix level */
399 validate_mix_level(avctx
, "ltrt_center_mix_level",
400 &opt
->ltrt_center_mix_level
, extmixlev_options
,
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",
405 &opt
->loro_center_mix_level
, extmixlev_options
,
406 EXTMIXLEV_NUM_OPTIONS
, 5, 0,
407 &s
->loro_center_mix_level
);
409 if (!s
->eac3
|| s
->has_surround
) {
410 /* validate Lt/Rt surround mix level */
411 validate_mix_level(avctx
, "ltrt_surround_mix_level",
412 &opt
->ltrt_surround_mix_level
, extmixlev_options
,
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",
417 &opt
->loro_surround_mix_level
, extmixlev_options
,
418 EXTMIXLEV_NUM_OPTIONS
, 6, 3,
419 &s
->loro_surround_mix_level
);
423 /* validate audio service type / channels combination */
424 if ((avctx
->audio_service_type
== AV_AUDIO_SERVICE_TYPE_KARAOKE
&&
425 avctx
->ch_layout
.nb_channels
== 1) ||
426 ((avctx
->audio_service_type
== AV_AUDIO_SERVICE_TYPE_COMMENTARY
||
427 avctx
->audio_service_type
== AV_AUDIO_SERVICE_TYPE_EMERGENCY
||
428 avctx
->audio_service_type
== AV_AUDIO_SERVICE_TYPE_VOICE_OVER
)
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
);
435 /* validate extended bsi 2 / info metadata */
436 if (opt
->extended_bsi_2
|| opt
->eac3_info_metadata
) {
437 /* default dolby headphone mode */
438 if (opt
->dolby_headphone_mode
== AC3ENC_OPT_NONE
)
439 opt
->dolby_headphone_mode
= AC3ENC_OPT_NOT_INDICATED
;
440 /* default dolby surround ex mode */
441 if (opt
->dolby_surround_ex_mode
== AC3ENC_OPT_NONE
)
442 opt
->dolby_surround_ex_mode
= AC3ENC_OPT_NOT_INDICATED
;
443 /* default A/D converter type */
444 if (opt
->ad_converter_type
== AC3ENC_OPT_NONE
)
445 opt
->ad_converter_type
= AC3ENC_OPT_ADCONV_STANDARD
;
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
;
458 /* dolby surround mode default */
459 if (!s
->eac3
|| opt
->eac3_info_metadata
) {
460 if (opt
->dolby_surround_mode
== AC3ENC_OPT_NONE
)
461 opt
->dolby_surround_mode
= AC3ENC_OPT_NOT_INDICATED
;
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
);
471 if (opt
->mixing_level
< 80) {
472 av_log(avctx
, AV_LOG_ERROR
, "invalid mixing level. must be between "
474 return AVERROR(EINVAL
);
476 /* default room type */
477 if (opt
->room_type
== AC3ENC_OPT_NONE
)
478 opt
->room_type
= AC3ENC_OPT_NOT_INDICATED
;
481 /* set bitstream id for alternate bitstream syntax */
482 if (!s
->eac3
&& (opt
->extended_bsi_1
|| opt
->extended_bsi_2
))
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.
492 * @param s AC-3 encoder private context
494 static void ac3_adjust_frame_size(AC3EncodeContext
*s
)
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
;
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
;
507 * Set the initial coupling strategy parameters prior to coupling analysis.
509 * @param s AC-3 encoder private context
511 void ff_ac3_compute_coupling_strategy(AC3EncodeContext
*s
)
517 /* set coupling use flags for each block/channel */
518 /* TODO: turn coupling on/off and adjust start band based on bit usage */
519 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
520 AC3Block
*block
= &s
->blocks
[blk
];
521 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++)
522 block
->channel_in_cpl
[ch
] = s
->cpl_on
;
525 /* enable coupling for each block if at least 2 channels have coupling
526 enabled for that block */
529 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
530 AC3Block
*block
= &s
->blocks
[blk
];
531 block
->num_cpl_channels
= 0;
532 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++)
533 block
->num_cpl_channels
+= block
->channel_in_cpl
[ch
];
534 block
->cpl_in_use
= block
->num_cpl_channels
> 1;
535 num_cpl_blocks
+= block
->cpl_in_use
;
536 if (!block
->cpl_in_use
) {
537 block
->num_cpl_channels
= 0;
538 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++)
539 block
->channel_in_cpl
[ch
] = 0;
542 block
->new_cpl_strategy
= !blk
;
544 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
545 if (block
->channel_in_cpl
[ch
] != s
->blocks
[blk
-1].channel_in_cpl
[ch
]) {
546 block
->new_cpl_strategy
= 1;
551 block
->new_cpl_leak
= block
->new_cpl_strategy
;
553 if (!blk
|| (block
->cpl_in_use
&& !got_cpl_snr
)) {
554 block
->new_snr_offsets
= 1;
555 if (block
->cpl_in_use
)
558 block
->new_snr_offsets
= 0;
564 /* set bandwidth for each channel */
565 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
566 AC3Block
*block
= &s
->blocks
[blk
];
567 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
568 if (block
->channel_in_cpl
[ch
])
569 block
->end_freq
[ch
] = s
->start_freq
[CPL_CH
];
571 block
->end_freq
[ch
] = s
->bandwidth_code
* 3 + 73;
578 * Apply stereo rematrixing to coefficients based on rematrixing flags.
580 * @param s AC-3 encoder private context
582 static void ac3_apply_rematrixing(AC3EncodeContext
*s
)
587 uint8_t *flags
= NULL
;
589 if (!s
->rematrixing_enabled
)
592 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
593 AC3Block
*block
= &s
->blocks
[blk
];
594 if (block
->new_rematrixing_strategy
)
595 flags
= block
->rematrixing_flags
;
596 nb_coefs
= FFMIN(block
->end_freq
[1], block
->end_freq
[2]);
597 for (bnd
= 0; bnd
< block
->num_rematrixing_bands
; bnd
++) {
599 start
= ff_ac3_rematrix_band_tab
[bnd
];
600 end
= FFMIN(nb_coefs
, ff_ac3_rematrix_band_tab
[bnd
+1]);
601 for (i
= start
; i
< end
; i
++) {
602 int32_t lt
= block
->fixed_coef
[1][i
];
603 int32_t rt
= block
->fixed_coef
[2][i
];
604 block
->fixed_coef
[1][i
] = (lt
+ rt
) >> 1;
605 block
->fixed_coef
[2][i
] = (lt
- rt
) >> 1;
614 * Initialize exponent tables.
616 static av_cold
void exponent_init(void)
618 int expstr
, i
, grpsize
;
620 for (expstr
= EXP_D15
-1; expstr
<= EXP_D45
-1; expstr
++) {
621 grpsize
= 3 << expstr
;
622 for (i
= 12; i
< 256; i
++) {
623 exponent_group_tab
[0][expstr
][i
] = (i
+ grpsize
- 4) / grpsize
;
624 exponent_group_tab
[1][expstr
][i
] = (i
) / grpsize
;
628 exponent_group_tab
[0][0][7] = 2;
633 * Extract exponents from the MDCT coefficients.
635 static void extract_exponents(AC3EncodeContext
*s
)
638 int chan_size
= AC3_MAX_COEFS
* s
->num_blocks
* (s
->channels
- ch
+ 1);
639 AC3Block
*block
= &s
->blocks
[0];
641 s
->ac3dsp
.extract_exponents(block
->exp
[ch
], block
->fixed_coef
[ch
], chan_size
);
646 * Exponent Difference Threshold.
647 * New exponents are sent if their SAD exceed this number.
649 #define EXP_DIFF_THRESHOLD 500
652 * Table used to select exponent strategy based on exponent reuse block interval.
654 static const uint8_t exp_strategy_reuse_tab
[4][6] = {
655 { EXP_D15
, EXP_D15
, EXP_D15
, EXP_D15
, EXP_D15
, EXP_D15
},
656 { EXP_D15
, EXP_D15
, EXP_D15
, EXP_D15
, EXP_D15
, EXP_D15
},
657 { EXP_D25
, EXP_D25
, EXP_D15
, EXP_D15
, EXP_D15
, EXP_D15
},
658 { EXP_D45
, EXP_D25
, EXP_D25
, EXP_D15
, EXP_D15
, EXP_D15
}
662 * Calculate exponent strategies for all channels.
663 * Array arrangement is reversed to simplify the per-channel calculation.
665 static void compute_exp_strategy(AC3EncodeContext
*s
)
669 for (ch
= !s
->cpl_on
; ch
<= s
->fbw_channels
; ch
++) {
670 uint8_t *exp_strategy
= s
->exp_strategy
[ch
];
671 uint8_t *exp
= s
->blocks
[0].exp
[ch
];
674 /* estimate if the exponent variation & decide if they should be
675 reused in the next frame */
676 exp_strategy
[0] = EXP_NEW
;
677 exp
+= AC3_MAX_COEFS
;
678 for (blk
= 1; blk
< s
->num_blocks
; blk
++, exp
+= AC3_MAX_COEFS
) {
680 if (!s
->blocks
[blk
-1].cpl_in_use
) {
681 exp_strategy
[blk
] = EXP_NEW
;
683 } else if (!s
->blocks
[blk
].cpl_in_use
) {
684 exp_strategy
[blk
] = EXP_REUSE
;
687 } else if (s
->blocks
[blk
].channel_in_cpl
[ch
] != s
->blocks
[blk
-1].channel_in_cpl
[ch
]) {
688 exp_strategy
[blk
] = EXP_NEW
;
691 exp_diff
= s
->mecc
.sad
[0](NULL
, exp
, exp
- AC3_MAX_COEFS
, 16, 16);
692 exp_strategy
[blk
] = EXP_REUSE
;
693 if (ch
== CPL_CH
&& exp_diff
> (EXP_DIFF_THRESHOLD
* (s
->blocks
[blk
].end_freq
[ch
] - s
->start_freq
[ch
]) / AC3_MAX_COEFS
))
694 exp_strategy
[blk
] = EXP_NEW
;
695 else if (ch
> CPL_CH
&& exp_diff
> EXP_DIFF_THRESHOLD
)
696 exp_strategy
[blk
] = EXP_NEW
;
699 /* now select the encoding strategy type : if exponents are often
700 recoded, we use a coarse encoding */
702 while (blk
< s
->num_blocks
) {
704 while (blk1
< s
->num_blocks
&& exp_strategy
[blk1
] == EXP_REUSE
)
706 exp_strategy
[blk
] = exp_strategy_reuse_tab
[s
->num_blks_code
][blk1
-blk
-1];
712 s
->exp_strategy
[ch
][0] = EXP_D15
;
713 for (blk
= 1; blk
< s
->num_blocks
; blk
++)
714 s
->exp_strategy
[ch
][blk
] = EXP_REUSE
;
717 /* for E-AC-3, determine frame exponent strategy */
718 if (CONFIG_EAC3_ENCODER
&& s
->eac3
)
719 ff_eac3_get_frame_exp_strategy(s
);
724 * Update the exponents so that they are the ones the decoder will decode.
726 * @param[in,out] exp array of exponents for 1 block in 1 channel
727 * @param nb_exps number of exponents in active bandwidth
728 * @param exp_strategy exponent strategy for the block
729 * @param cpl indicates if the block is in the coupling channel
731 static void encode_exponents_blk_ch(uint8_t *exp
, int nb_exps
, int exp_strategy
,
736 nb_groups
= exponent_group_tab
[cpl
][exp_strategy
-1][nb_exps
] * 3;
738 /* for each group, compute the minimum exponent */
739 switch(exp_strategy
) {
741 for (i
= 1, k
= 1-cpl
; i
<= nb_groups
; i
++) {
742 uint8_t exp_min
= exp
[k
];
743 if (exp
[k
+1] < exp_min
)
745 exp
[i
-cpl
] = exp_min
;
750 for (i
= 1, k
= 1-cpl
; i
<= nb_groups
; i
++) {
751 uint8_t exp_min
= exp
[k
];
752 if (exp
[k
+1] < exp_min
)
754 if (exp
[k
+2] < exp_min
)
756 if (exp
[k
+3] < exp_min
)
758 exp
[i
-cpl
] = exp_min
;
764 /* constraint for DC exponent */
765 if (!cpl
&& exp
[0] > 15)
768 /* decrease the delta between each groups to within 2 so that they can be
769 differentially encoded */
770 for (i
= 1; i
<= nb_groups
; i
++)
771 exp
[i
] = FFMIN(exp
[i
], exp
[i
-1] + 2);
774 exp
[i
] = FFMIN(exp
[i
], exp
[i
+1] + 2);
777 exp
[-1] = exp
[0] & ~1;
779 /* now we have the exponent values the decoder will see */
780 switch (exp_strategy
) {
782 for (i
= nb_groups
, k
= (nb_groups
* 2)-cpl
; i
> 0; i
--) {
783 uint8_t exp1
= exp
[i
-cpl
];
789 for (i
= nb_groups
, k
= (nb_groups
* 4)-cpl
; i
> 0; i
--) {
790 exp
[k
] = exp
[k
-1] = exp
[k
-2] = exp
[k
-3] = exp
[i
-cpl
];
799 * Encode exponents from original extracted form to what the decoder will see.
800 * This copies and groups exponents based on exponent strategy and reduces
801 * deltas between adjacent exponent groups so that they can be differentially
804 static void encode_exponents(AC3EncodeContext
*s
)
806 int blk
, blk1
, ch
, cpl
;
807 uint8_t *exp
, *exp_strategy
;
808 int nb_coefs
, num_reuse_blocks
;
810 for (ch
= !s
->cpl_on
; ch
<= s
->channels
; ch
++) {
811 exp
= s
->blocks
[0].exp
[ch
] + s
->start_freq
[ch
];
812 exp_strategy
= s
->exp_strategy
[ch
];
814 cpl
= (ch
== CPL_CH
);
816 while (blk
< s
->num_blocks
) {
817 AC3Block
*block
= &s
->blocks
[blk
];
818 if (cpl
&& !block
->cpl_in_use
) {
819 exp
+= AC3_MAX_COEFS
;
823 nb_coefs
= block
->end_freq
[ch
] - s
->start_freq
[ch
];
826 /* count the number of EXP_REUSE blocks after the current block
827 and set exponent reference block numbers */
828 s
->exp_ref_block
[ch
][blk
] = blk
;
829 while (blk1
< s
->num_blocks
&& exp_strategy
[blk1
] == EXP_REUSE
) {
830 s
->exp_ref_block
[ch
][blk1
] = blk
;
833 num_reuse_blocks
= blk1
- blk
- 1;
835 /* for the EXP_REUSE case we select the min of the exponents */
836 s
->ac3dsp
.ac3_exponent_min(exp
-s
->start_freq
[ch
], num_reuse_blocks
,
839 encode_exponents_blk_ch(exp
, nb_coefs
, exp_strategy
[blk
], cpl
);
841 exp
+= AC3_MAX_COEFS
* (num_reuse_blocks
+ 1);
846 /* reference block numbers have been changed, so reset ref_bap_set */
852 * Count exponent bits based on bandwidth, coupling, and exponent strategies.
854 static int count_exponent_bits(AC3EncodeContext
*s
)
857 int nb_groups
, bit_count
;
860 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
861 AC3Block
*block
= &s
->blocks
[blk
];
862 for (ch
= !block
->cpl_in_use
; ch
<= s
->channels
; ch
++) {
863 int exp_strategy
= s
->exp_strategy
[ch
][blk
];
864 int cpl
= (ch
== CPL_CH
);
865 int nb_coefs
= block
->end_freq
[ch
] - s
->start_freq
[ch
];
867 if (exp_strategy
== EXP_REUSE
)
870 nb_groups
= exponent_group_tab
[cpl
][exp_strategy
-1][nb_coefs
];
871 bit_count
+= 4 + (nb_groups
* 7);
881 * 3 delta-encoded exponents are in each 7-bit group. The number of groups
882 * varies depending on exponent strategy and bandwidth.
884 * @param s AC-3 encoder private context
886 static void ac3_group_exponents(AC3EncodeContext
*s
)
889 int group_size
, nb_groups
;
891 int delta0
, delta1
, delta2
;
894 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
895 AC3Block
*block
= &s
->blocks
[blk
];
896 for (ch
= !block
->cpl_in_use
; ch
<= s
->channels
; ch
++) {
897 int exp_strategy
= s
->exp_strategy
[ch
][blk
];
898 if (exp_strategy
== EXP_REUSE
)
900 cpl
= (ch
== CPL_CH
);
901 group_size
= exp_strategy
+ (exp_strategy
== EXP_D45
);
902 nb_groups
= exponent_group_tab
[cpl
][exp_strategy
-1][block
->end_freq
[ch
]-s
->start_freq
[ch
]];
903 p
= block
->exp
[ch
] + s
->start_freq
[ch
] - cpl
;
907 block
->grouped_exp
[ch
][0] = exp1
;
909 /* remaining exponents are delta encoded */
910 for (i
= 1; i
<= nb_groups
; i
++) {
911 /* merge three delta in one code */
915 delta0
= exp1
- exp0
+ 2;
916 av_assert2(delta0
>= 0 && delta0
<= 4);
921 delta1
= exp1
- exp0
+ 2;
922 av_assert2(delta1
>= 0 && delta1
<= 4);
927 delta2
= exp1
- exp0
+ 2;
928 av_assert2(delta2
>= 0 && delta2
<= 4);
930 block
->grouped_exp
[ch
][i
] = ((delta0
* 5 + delta1
) * 5) + delta2
;
938 * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
939 * Extract exponents from MDCT coefficients, calculate exponent strategies,
940 * and encode final exponents.
942 * @param s AC-3 encoder private context
944 static void ac3_process_exponents(AC3EncodeContext
*s
)
946 extract_exponents(s
);
948 compute_exp_strategy(s
);
957 * Count frame bits that are based solely on fixed parameters.
958 * This only has to be run once when the encoder is initialized.
960 static void count_frame_bits_fixed(AC3EncodeContext
*s
)
962 static const uint8_t frame_bits_inc
[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
967 * no dynamic range codes
968 * bit allocation parameters do not change between blocks
969 * no delta bit allocation
976 frame_bits
= 16; /* sync info */
978 /* bitstream info header */
981 if (s
->num_blocks
!= 0x6)
984 /* audio frame header */
985 if (s
->num_blocks
== 6)
988 /* exponent strategy */
989 if (s
->use_frame_exp_strategy
)
990 frame_bits
+= 5 * s
->fbw_channels
;
992 frame_bits
+= s
->num_blocks
* 2 * s
->fbw_channels
;
994 frame_bits
+= s
->num_blocks
;
995 /* converter exponent strategy */
996 if (s
->num_blks_code
!= 0x3)
999 frame_bits
+= s
->fbw_channels
* 5;
1002 /* block start info */
1003 if (s
->num_blocks
!= 1)
1007 frame_bits
+= frame_bits_inc
[s
->channel_mode
];
1011 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
1013 /* block switch flags */
1014 frame_bits
+= s
->fbw_channels
;
1017 frame_bits
+= s
->fbw_channels
;
1023 /* spectral extension */
1027 /* coupling strategy exists: cplstre */
1032 /* exponent strategy */
1033 frame_bits
+= 2 * s
->fbw_channels
;
1037 /* bit allocation params */
1040 frame_bits
+= 2 + 2 + 2 + 2 + 3;
1043 /* snroffste for AC-3, convsnroffste for E-AC-3 */
1047 /* delta bit allocation */
1055 /* auxiliary data */
1059 frame_bits
+= 1 + 16;
1061 s
->frame_bits_fixed
= frame_bits
;
1066 * Initialize bit allocation.
1067 * Set default parameter codes and calculate parameter values.
1069 static av_cold
void bit_alloc_init(AC3EncodeContext
*s
)
1073 /* init default parameters */
1074 s
->slow_decay_code
= 2;
1075 s
->fast_decay_code
= 1;
1076 s
->slow_gain_code
= 1;
1077 s
->db_per_bit_code
= s
->eac3
? 2 : 3;
1079 for (ch
= 0; ch
<= s
->channels
; ch
++)
1080 s
->fast_gain_code
[ch
] = 4;
1082 /* initial snr offset */
1083 s
->coarse_snr_offset
= 40;
1085 /* compute real values */
1086 /* currently none of these values change during encoding, so we can just
1087 set them once at initialization */
1088 s
->bit_alloc
.slow_decay
= ff_ac3_slow_decay_tab
[s
->slow_decay_code
];
1089 s
->bit_alloc
.fast_decay
= ff_ac3_fast_decay_tab
[s
->fast_decay_code
];
1090 s
->bit_alloc
.slow_gain
= ff_ac3_slow_gain_tab
[s
->slow_gain_code
];
1091 s
->bit_alloc
.db_per_bit
= ff_ac3_db_per_bit_tab
[s
->db_per_bit_code
];
1092 s
->bit_alloc
.floor
= ff_ac3_floor_tab
[s
->floor_code
];
1093 s
->bit_alloc
.cpl_fast_leak
= 0;
1094 s
->bit_alloc
.cpl_slow_leak
= 0;
1096 count_frame_bits_fixed(s
);
1101 * Count the bits used to encode the frame, minus exponents and mantissas.
1102 * Bits based on fixed parameters have already been counted, so now we just
1103 * have to add the bits based on parameters that change during encoding.
1105 static void count_frame_bits(AC3EncodeContext
*s
)
1107 AC3EncOptions
*opt
= &s
->options
;
1113 if (opt
->eac3_mixing_metadata
) {
1114 if (s
->channel_mode
> AC3_CHMODE_STEREO
)
1118 if (s
->has_surround
)
1120 frame_bits
+= s
->lfe_on
;
1121 frame_bits
+= 1 + 1 + 2;
1122 if (s
->channel_mode
< AC3_CHMODE_STEREO
)
1126 if (opt
->eac3_info_metadata
) {
1127 frame_bits
+= 3 + 1 + 1;
1128 if (s
->channel_mode
== AC3_CHMODE_STEREO
)
1129 frame_bits
+= 2 + 2;
1130 if (s
->channel_mode
>= AC3_CHMODE_2F2R
)
1133 if (opt
->audio_production_info
)
1134 frame_bits
+= 5 + 2 + 1;
1138 if (s
->channel_mode
> AC3_CHMODE_MONO
) {
1140 for (blk
= 1; blk
< s
->num_blocks
; blk
++) {
1141 AC3Block
*block
= &s
->blocks
[blk
];
1143 if (block
->new_cpl_strategy
)
1147 /* coupling exponent strategy */
1149 if (s
->use_frame_exp_strategy
) {
1152 for (blk
= 0; blk
< s
->num_blocks
; blk
++)
1153 frame_bits
+= 2 * s
->blocks
[blk
].cpl_in_use
;
1157 if (opt
->audio_production_info
)
1159 if (s
->bitstream_id
== 6) {
1160 if (opt
->extended_bsi_1
)
1162 if (opt
->extended_bsi_2
)
1168 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
1169 AC3Block
*block
= &s
->blocks
[blk
];
1171 /* coupling strategy */
1172 if (block
->new_cpl_strategy
) {
1175 if (block
->cpl_in_use
) {
1178 if (!s
->eac3
|| s
->channel_mode
!= AC3_CHMODE_STEREO
)
1179 frame_bits
+= s
->fbw_channels
;
1180 if (s
->channel_mode
== AC3_CHMODE_STEREO
)
1182 frame_bits
+= 4 + 4;
1186 frame_bits
+= s
->num_cpl_subbands
- 1;
1190 /* coupling coordinates */
1191 if (block
->cpl_in_use
) {
1192 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
1193 if (block
->channel_in_cpl
[ch
]) {
1194 if (!s
->eac3
|| block
->new_cpl_coords
[ch
] != 2)
1196 if (block
->new_cpl_coords
[ch
]) {
1198 frame_bits
+= (4 + 4) * s
->num_cpl_bands
;
1204 /* stereo rematrixing */
1205 if (s
->channel_mode
== AC3_CHMODE_STEREO
) {
1206 if (!s
->eac3
|| blk
> 0)
1208 if (s
->blocks
[blk
].new_rematrixing_strategy
)
1209 frame_bits
+= block
->num_rematrixing_bands
;
1212 /* bandwidth codes & gain range */
1213 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
1214 if (s
->exp_strategy
[ch
][blk
] != EXP_REUSE
) {
1215 if (!block
->channel_in_cpl
[ch
])
1221 /* coupling exponent strategy */
1222 if (!s
->eac3
&& block
->cpl_in_use
)
1225 /* snr offsets and fast gain codes */
1227 if (block
->new_snr_offsets
)
1228 frame_bits
+= 6 + (s
->channels
+ block
->cpl_in_use
) * (4 + 3);
1231 /* coupling leak info */
1232 if (block
->cpl_in_use
) {
1233 if (!s
->eac3
|| block
->new_cpl_leak
!= 2)
1235 if (block
->new_cpl_leak
)
1236 frame_bits
+= 3 + 3;
1240 s
->frame_bits
= s
->frame_bits_fixed
+ frame_bits
;
1245 * Calculate masking curve based on the final exponents.
1246 * Also calculate the power spectral densities to use in future calculations.
1248 static void bit_alloc_masking(AC3EncodeContext
*s
)
1252 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
1253 AC3Block
*block
= &s
->blocks
[blk
];
1254 for (ch
= !block
->cpl_in_use
; ch
<= s
->channels
; ch
++) {
1255 /* We only need psd and mask for calculating bap.
1256 Since we currently do not calculate bap when exponent
1257 strategy is EXP_REUSE we do not need to calculate psd or mask. */
1258 if (s
->exp_strategy
[ch
][blk
] != EXP_REUSE
) {
1259 ff_ac3_bit_alloc_calc_psd(block
->exp
[ch
], s
->start_freq
[ch
],
1260 block
->end_freq
[ch
], block
->psd
[ch
],
1261 block
->band_psd
[ch
]);
1262 ff_ac3_bit_alloc_calc_mask(&s
->bit_alloc
, block
->band_psd
[ch
],
1263 s
->start_freq
[ch
], block
->end_freq
[ch
],
1264 ff_ac3_fast_gain_tab
[s
->fast_gain_code
[ch
]],
1265 ch
== s
->lfe_channel
,
1266 DBA_NONE
, 0, NULL
, NULL
, NULL
,
1275 * Ensure that bap for each block and channel point to the current bap_buffer.
1276 * They may have been switched during the bit allocation search.
1278 static void reset_block_bap(AC3EncodeContext
*s
)
1283 if (s
->ref_bap
[0][0] == s
->bap_buffer
&& s
->ref_bap_set
)
1286 ref_bap
= s
->bap_buffer
;
1287 for (ch
= 0; ch
<= s
->channels
; ch
++) {
1288 for (blk
= 0; blk
< s
->num_blocks
; blk
++)
1289 s
->ref_bap
[ch
][blk
] = ref_bap
+ AC3_MAX_COEFS
* s
->exp_ref_block
[ch
][blk
];
1290 ref_bap
+= AC3_MAX_COEFS
* s
->num_blocks
;
1297 * Initialize mantissa counts.
1298 * These are set so that they are padded to the next whole group size when bits
1299 * are counted in compute_mantissa_size.
1301 * @param[in,out] mant_cnt running counts for each bap value for each block
1303 static void count_mantissa_bits_init(uint16_t mant_cnt
[AC3_MAX_BLOCKS
][16])
1307 for (blk
= 0; blk
< AC3_MAX_BLOCKS
; blk
++) {
1308 memset(mant_cnt
[blk
], 0, sizeof(mant_cnt
[blk
]));
1309 mant_cnt
[blk
][1] = mant_cnt
[blk
][2] = 2;
1310 mant_cnt
[blk
][4] = 1;
1316 * Update mantissa bit counts for all blocks in 1 channel in a given bandwidth
1319 * @param s AC-3 encoder private context
1320 * @param ch channel index
1321 * @param[in,out] mant_cnt running counts for each bap value for each block
1322 * @param start starting coefficient bin
1323 * @param end ending coefficient bin
1325 static void count_mantissa_bits_update_ch(AC3EncodeContext
*s
, int ch
,
1326 uint16_t mant_cnt
[AC3_MAX_BLOCKS
][16],
1331 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
1332 AC3Block
*block
= &s
->blocks
[blk
];
1333 if (ch
== CPL_CH
&& !block
->cpl_in_use
)
1335 s
->ac3dsp
.update_bap_counts(mant_cnt
[blk
],
1336 s
->ref_bap
[ch
][blk
] + start
,
1337 FFMIN(end
, block
->end_freq
[ch
]) - start
);
1343 * Count the number of mantissa bits in the frame based on the bap values.
1345 static int count_mantissa_bits(AC3EncodeContext
*s
)
1347 int ch
, max_end_freq
;
1348 LOCAL_ALIGNED_16(uint16_t, mant_cnt
, [AC3_MAX_BLOCKS
], [16]);
1350 count_mantissa_bits_init(mant_cnt
);
1352 max_end_freq
= s
->bandwidth_code
* 3 + 73;
1353 for (ch
= !s
->cpl_enabled
; ch
<= s
->channels
; ch
++)
1354 count_mantissa_bits_update_ch(s
, ch
, mant_cnt
, s
->start_freq
[ch
],
1357 return s
->ac3dsp
.compute_mantissa_size(mant_cnt
);
1362 * Run the bit allocation with a given SNR offset.
1363 * This calculates the bit allocation pointers that will be used to determine
1364 * the quantization of each mantissa.
1366 * @param s AC-3 encoder private context
1367 * @param snr_offset SNR offset, 0 to 1023
1368 * @return the number of bits needed for mantissas if the given SNR offset is
1371 static int bit_alloc(AC3EncodeContext
*s
, int snr_offset
)
1375 snr_offset
= (snr_offset
- 240) * 4;
1378 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
1379 AC3Block
*block
= &s
->blocks
[blk
];
1381 for (ch
= !block
->cpl_in_use
; ch
<= s
->channels
; ch
++) {
1382 /* Currently the only bit allocation parameters which vary across
1383 blocks within a frame are the exponent values. We can take
1384 advantage of that by reusing the bit allocation pointers
1385 whenever we reuse exponents. */
1386 if (s
->exp_strategy
[ch
][blk
] != EXP_REUSE
) {
1387 s
->ac3dsp
.bit_alloc_calc_bap(block
->mask
[ch
], block
->psd
[ch
],
1388 s
->start_freq
[ch
], block
->end_freq
[ch
],
1389 snr_offset
, s
->bit_alloc
.floor
,
1390 ff_ac3_bap_tab
, s
->ref_bap
[ch
][blk
]);
1394 return count_mantissa_bits(s
);
1399 * Constant bitrate bit allocation search.
1400 * Find the largest SNR offset that will allow data to fit in the frame.
1402 static int cbr_bit_allocation(AC3EncodeContext
*s
)
1406 int snr_offset
, snr_incr
;
1408 bits_left
= 8 * s
->frame_size
- (s
->frame_bits
+ s
->exponent_bits
);
1410 return AVERROR(EINVAL
);
1412 snr_offset
= s
->coarse_snr_offset
<< 4;
1414 /* if previous frame SNR offset was 1023, check if current frame can also
1415 use SNR offset of 1023. if so, skip the search. */
1416 if ((snr_offset
| s
->fine_snr_offset
[1]) == 1023) {
1417 if (bit_alloc(s
, 1023) <= bits_left
)
1421 while (snr_offset
>= 0 &&
1422 bit_alloc(s
, snr_offset
) > bits_left
) {
1426 return AVERROR(EINVAL
);
1428 FFSWAP(uint8_t *, s
->bap_buffer
, s
->bap1_buffer
);
1429 for (snr_incr
= 64; snr_incr
> 0; snr_incr
>>= 2) {
1430 while (snr_offset
+ snr_incr
<= 1023 &&
1431 bit_alloc(s
, snr_offset
+ snr_incr
) <= bits_left
) {
1432 snr_offset
+= snr_incr
;
1433 FFSWAP(uint8_t *, s
->bap_buffer
, s
->bap1_buffer
);
1436 FFSWAP(uint8_t *, s
->bap_buffer
, s
->bap1_buffer
);
1439 s
->coarse_snr_offset
= snr_offset
>> 4;
1440 for (ch
= !s
->cpl_on
; ch
<= s
->channels
; ch
++)
1441 s
->fine_snr_offset
[ch
] = snr_offset
& 0xF;
1448 * Perform bit allocation search.
1449 * Finds the SNR offset value that maximizes quality and fits in the specified
1450 * frame size. Output is the SNR offset and a set of bit allocation pointers
1451 * used to quantize the mantissas.
1453 static int ac3_compute_bit_allocation(AC3EncodeContext
*s
)
1455 count_frame_bits(s
);
1457 s
->exponent_bits
= count_exponent_bits(s
);
1459 bit_alloc_masking(s
);
1461 return cbr_bit_allocation(s
);
1466 * Symmetric quantization on 'levels' levels.
1468 * @param c unquantized coefficient
1470 * @param levels number of quantization levels
1471 * @return quantized coefficient
1473 static inline int sym_quant(int c
, int e
, int levels
)
1475 int v
= (((levels
* c
) >> (24 - e
)) + levels
) >> 1;
1476 av_assert2(v
>= 0 && v
< levels
);
1482 * Asymmetric quantization on 2^qbits levels.
1484 * @param c unquantized coefficient
1486 * @param qbits number of quantization bits
1487 * @return quantized coefficient
1489 static inline int asym_quant(int c
, int e
, int qbits
)
1493 c
= (((c
* (1<<e
)) >> (24 - qbits
)) + 1) >> 1;
1494 m
= (1 << (qbits
-1));
1497 av_assert2(c
>= -m
);
1503 * Quantize a set of mantissas for a single channel in a single block.
1505 * @param s Mantissa count context
1506 * @param fixed_coef unquantized fixed-point coefficients
1507 * @param exp exponents
1508 * @param bap bit allocation pointer indices
1509 * @param[out] qmant quantized coefficients
1510 * @param start_freq starting coefficient bin
1511 * @param end_freq ending coefficient bin
1513 static void quantize_mantissas_blk_ch(AC3Mant
*s
, int32_t *fixed_coef
,
1514 uint8_t *exp
, uint8_t *bap
,
1515 int16_t *qmant
, int start_freq
,
1520 for (i
= start_freq
; i
< end_freq
; i
++) {
1521 int c
= fixed_coef
[i
];
1528 v
= sym_quant(c
, e
, 3);
1529 switch (s
->mant1_cnt
) {
1531 s
->qmant1_ptr
= &qmant
[i
];
1536 *s
->qmant1_ptr
+= 3 * v
;
1541 *s
->qmant1_ptr
+= v
;
1548 v
= sym_quant(c
, e
, 5);
1549 switch (s
->mant2_cnt
) {
1551 s
->qmant2_ptr
= &qmant
[i
];
1556 *s
->qmant2_ptr
+= 5 * v
;
1561 *s
->qmant2_ptr
+= v
;
1568 v
= sym_quant(c
, e
, 7);
1571 v
= sym_quant(c
, e
, 11);
1572 switch (s
->mant4_cnt
) {
1574 s
->qmant4_ptr
= &qmant
[i
];
1579 *s
->qmant4_ptr
+= v
;
1586 v
= sym_quant(c
, e
, 15);
1589 v
= asym_quant(c
, e
, 14);
1592 v
= asym_quant(c
, e
, 16);
1595 v
= asym_quant(c
, e
, v
- 1);
1604 * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
1606 * @param s AC-3 encoder private context
1608 static void ac3_quantize_mantissas(AC3EncodeContext
*s
)
1610 int blk
, ch
, ch0
=0, got_cpl
;
1612 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
1613 AC3Block
*block
= &s
->blocks
[blk
];
1616 got_cpl
= !block
->cpl_in_use
;
1617 for (ch
= 1; ch
<= s
->channels
; ch
++) {
1618 if (!got_cpl
&& ch
> 1 && block
->channel_in_cpl
[ch
-1]) {
1623 quantize_mantissas_blk_ch(&m
, block
->fixed_coef
[ch
],
1624 s
->blocks
[s
->exp_ref_block
[ch
][blk
]].exp
[ch
],
1625 s
->ref_bap
[ch
][blk
], block
->qmant
[ch
],
1626 s
->start_freq
[ch
], block
->end_freq
[ch
]);
1635 * Write the AC-3 frame header to the output bitstream.
1637 static void ac3_output_frame_header(AC3EncodeContext
*s
, PutBitContext
*pb
)
1639 AC3EncOptions
*opt
= &s
->options
;
1641 put_bits(pb
, 16, 0x0b77); /* frame header */
1642 put_bits(pb
, 16, 0); /* crc1: will be filled later */
1643 put_bits(pb
, 2, s
->bit_alloc
.sr_code
);
1644 put_bits(pb
, 6, s
->frame_size_code
+ (s
->frame_size
- s
->frame_size_min
) / 2);
1645 put_bits(pb
, 5, s
->bitstream_id
);
1646 put_bits(pb
, 3, s
->bitstream_mode
);
1647 put_bits(pb
, 3, s
->channel_mode
);
1648 if ((s
->channel_mode
& 0x01) && s
->channel_mode
!= AC3_CHMODE_MONO
)
1649 put_bits(pb
, 2, s
->center_mix_level
);
1650 if (s
->channel_mode
& 0x04)
1651 put_bits(pb
, 2, s
->surround_mix_level
);
1652 if (s
->channel_mode
== AC3_CHMODE_STEREO
)
1653 put_bits(pb
, 2, opt
->dolby_surround_mode
);
1654 put_bits(pb
, 1, s
->lfe_on
); /* LFE */
1655 put_bits(pb
, 5, -opt
->dialogue_level
);
1656 put_bits(pb
, 1, 0); /* no compression control word */
1657 put_bits(pb
, 1, 0); /* no lang code */
1658 put_bits(pb
, 1, opt
->audio_production_info
);
1659 if (opt
->audio_production_info
) {
1660 put_bits(pb
, 5, opt
->mixing_level
- 80);
1661 put_bits(pb
, 2, opt
->room_type
);
1663 put_bits(pb
, 1, opt
->copyright
);
1664 put_bits(pb
, 1, opt
->original
);
1665 if (s
->bitstream_id
== 6) {
1666 /* alternate bit stream syntax */
1667 put_bits(pb
, 1, opt
->extended_bsi_1
);
1668 if (opt
->extended_bsi_1
) {
1669 put_bits(pb
, 2, opt
->preferred_stereo_downmix
);
1670 put_bits(pb
, 3, s
->ltrt_center_mix_level
);
1671 put_bits(pb
, 3, s
->ltrt_surround_mix_level
);
1672 put_bits(pb
, 3, s
->loro_center_mix_level
);
1673 put_bits(pb
, 3, s
->loro_surround_mix_level
);
1675 put_bits(pb
, 1, opt
->extended_bsi_2
);
1676 if (opt
->extended_bsi_2
) {
1677 put_bits(pb
, 2, opt
->dolby_surround_ex_mode
);
1678 put_bits(pb
, 2, opt
->dolby_headphone_mode
);
1679 put_bits(pb
, 1, opt
->ad_converter_type
);
1680 put_bits(pb
, 9, 0); /* xbsi2 and encinfo : reserved */
1683 put_bits(pb
, 1, 0); /* no time code 1 */
1684 put_bits(pb
, 1, 0); /* no time code 2 */
1686 put_bits(pb
, 1, 0); /* no additional bit stream info */
1691 * Write one audio block to the output bitstream.
1693 static void output_audio_block(AC3EncodeContext
*s
, PutBitContext
*pb
, int blk
)
1695 int ch
, i
, baie
, bnd
, got_cpl
, av_uninit(ch0
);
1696 AC3Block
*block
= &s
->blocks
[blk
];
1698 /* block switching */
1700 for (ch
= 0; ch
< s
->fbw_channels
; ch
++)
1706 for (ch
= 0; ch
< s
->fbw_channels
; ch
++)
1710 /* dynamic range codes */
1713 /* spectral extension */
1717 /* channel coupling */
1719 put_bits(pb
, 1, block
->new_cpl_strategy
);
1720 if (block
->new_cpl_strategy
) {
1722 put_bits(pb
, 1, block
->cpl_in_use
);
1723 if (block
->cpl_in_use
) {
1724 int start_sub
, end_sub
;
1726 put_bits(pb
, 1, 0); /* enhanced coupling */
1727 if (!s
->eac3
|| s
->channel_mode
!= AC3_CHMODE_STEREO
) {
1728 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++)
1729 put_bits(pb
, 1, block
->channel_in_cpl
[ch
]);
1731 if (s
->channel_mode
== AC3_CHMODE_STEREO
)
1732 put_bits(pb
, 1, 0); /* phase flags in use */
1733 start_sub
= (s
->start_freq
[CPL_CH
] - 37) / 12;
1734 end_sub
= (s
->cpl_end_freq
- 37) / 12;
1735 put_bits(pb
, 4, start_sub
);
1736 put_bits(pb
, 4, end_sub
- 3);
1737 /* coupling band structure */
1739 put_bits(pb
, 1, 0); /* use default */
1741 for (bnd
= start_sub
+1; bnd
< end_sub
; bnd
++)
1742 put_bits(pb
, 1, ff_eac3_default_cpl_band_struct
[bnd
]);
1747 /* coupling coordinates */
1748 if (block
->cpl_in_use
) {
1749 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
1750 if (block
->channel_in_cpl
[ch
]) {
1751 if (!s
->eac3
|| block
->new_cpl_coords
[ch
] != 2)
1752 put_bits(pb
, 1, block
->new_cpl_coords
[ch
]);
1753 if (block
->new_cpl_coords
[ch
]) {
1754 put_bits(pb
, 2, block
->cpl_master_exp
[ch
]);
1755 for (bnd
= 0; bnd
< s
->num_cpl_bands
; bnd
++) {
1756 put_bits(pb
, 4, block
->cpl_coord_exp
[ch
][bnd
]);
1757 put_bits(pb
, 4, block
->cpl_coord_mant
[ch
][bnd
]);
1764 /* stereo rematrixing */
1765 if (s
->channel_mode
== AC3_CHMODE_STEREO
) {
1766 if (!s
->eac3
|| blk
> 0)
1767 put_bits(pb
, 1, block
->new_rematrixing_strategy
);
1768 if (block
->new_rematrixing_strategy
) {
1769 /* rematrixing flags */
1770 for (bnd
= 0; bnd
< block
->num_rematrixing_bands
; bnd
++)
1771 put_bits(pb
, 1, block
->rematrixing_flags
[bnd
]);
1775 /* exponent strategy */
1777 for (ch
= !block
->cpl_in_use
; ch
<= s
->fbw_channels
; ch
++)
1778 put_bits(pb
, 2, s
->exp_strategy
[ch
][blk
]);
1780 put_bits(pb
, 1, s
->exp_strategy
[s
->lfe_channel
][blk
]);
1784 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
1785 if (s
->exp_strategy
[ch
][blk
] != EXP_REUSE
&& !block
->channel_in_cpl
[ch
])
1786 put_bits(pb
, 6, s
->bandwidth_code
);
1790 for (ch
= !block
->cpl_in_use
; ch
<= s
->channels
; ch
++) {
1792 int cpl
= (ch
== CPL_CH
);
1794 if (s
->exp_strategy
[ch
][blk
] == EXP_REUSE
)
1798 put_bits(pb
, 4, block
->grouped_exp
[ch
][0] >> cpl
);
1800 /* exponent groups */
1801 nb_groups
= exponent_group_tab
[cpl
][s
->exp_strategy
[ch
][blk
]-1][block
->end_freq
[ch
]-s
->start_freq
[ch
]];
1802 for (i
= 1; i
<= nb_groups
; i
++)
1803 put_bits(pb
, 7, block
->grouped_exp
[ch
][i
]);
1805 /* gain range info */
1806 if (ch
!= s
->lfe_channel
&& !cpl
)
1810 /* bit allocation info */
1813 put_bits(pb
, 1, baie
);
1815 put_bits(pb
, 2, s
->slow_decay_code
);
1816 put_bits(pb
, 2, s
->fast_decay_code
);
1817 put_bits(pb
, 2, s
->slow_gain_code
);
1818 put_bits(pb
, 2, s
->db_per_bit_code
);
1819 put_bits(pb
, 3, s
->floor_code
);
1825 put_bits(pb
, 1, block
->new_snr_offsets
);
1826 if (block
->new_snr_offsets
) {
1827 put_bits(pb
, 6, s
->coarse_snr_offset
);
1828 for (ch
= !block
->cpl_in_use
; ch
<= s
->channels
; ch
++) {
1829 put_bits(pb
, 4, s
->fine_snr_offset
[ch
]);
1830 put_bits(pb
, 3, s
->fast_gain_code
[ch
]);
1834 put_bits(pb
, 1, 0); /* no converter snr offset */
1838 if (block
->cpl_in_use
) {
1839 if (!s
->eac3
|| block
->new_cpl_leak
!= 2)
1840 put_bits(pb
, 1, block
->new_cpl_leak
);
1841 if (block
->new_cpl_leak
) {
1842 put_bits(pb
, 3, s
->bit_alloc
.cpl_fast_leak
);
1843 put_bits(pb
, 3, s
->bit_alloc
.cpl_slow_leak
);
1848 put_bits(pb
, 1, 0); /* no delta bit allocation */
1849 put_bits(pb
, 1, 0); /* no data to skip */
1853 got_cpl
= !block
->cpl_in_use
;
1854 for (ch
= 1; ch
<= s
->channels
; ch
++) {
1857 if (!got_cpl
&& ch
> 1 && block
->channel_in_cpl
[ch
-1]) {
1862 for (i
= s
->start_freq
[ch
]; i
< block
->end_freq
[ch
]; i
++) {
1863 q
= block
->qmant
[ch
][i
];
1864 b
= s
->ref_bap
[ch
][blk
][i
];
1867 case 1: if (q
!= 128) put_bits (pb
, 5, q
); break;
1868 case 2: if (q
!= 128) put_bits (pb
, 7, q
); break;
1869 case 3: put_sbits(pb
, 3, q
); break;
1870 case 4: if (q
!= 128) put_bits (pb
, 7, q
); break;
1871 case 14: put_sbits(pb
, 14, q
); break;
1872 case 15: put_sbits(pb
, 16, q
); break;
1873 default: put_sbits(pb
, b
-1, q
); break;
1882 /** CRC-16 Polynomial */
1883 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1886 static unsigned int mul_poly(unsigned int a
, unsigned int b
, unsigned int poly
)
1903 static unsigned int pow_poly(unsigned int a
, unsigned int n
, unsigned int poly
)
1909 r
= mul_poly(r
, a
, poly
);
1910 a
= mul_poly(a
, a
, poly
);
1918 * Fill the end of the frame with 0's and compute the two CRCs.
1920 static void output_frame_end(AC3EncodeContext
*s
, PutBitContext
*pb
)
1922 const AVCRC
*crc_ctx
= av_crc_get_table(AV_CRC_16_ANSI
);
1923 int frame_size_58
, pad_bytes
, crc1
, crc2
, crc_inv
;
1926 frame_size_58
= ((s
->frame_size
>> 2) + (s
->frame_size
>> 4)) << 1;
1928 /* pad the remainder of the frame with zeros */
1929 av_assert2(s
->frame_size
* 8 - put_bits_count(pb
) >= 18);
1932 pad_bytes
= s
->frame_size
- (put_bits_ptr(pb
) - frame
) - 2;
1933 av_assert2(pad_bytes
>= 0);
1935 memset(put_bits_ptr(pb
), 0, pad_bytes
);
1939 crc2
= av_crc(crc_ctx
, 0, frame
+ 2, s
->frame_size
- 4);
1942 /* this is not so easy because it is at the beginning of the data... */
1943 crc1
= av_bswap16(av_crc(crc_ctx
, 0, frame
+ 4, frame_size_58
- 4));
1944 crc_inv
= s
->crc_inv
[s
->frame_size
> s
->frame_size_min
];
1945 crc1
= mul_poly(crc_inv
, crc1
, CRC16_POLY
);
1946 AV_WB16(frame
+ 2, crc1
);
1949 crc2
= av_crc(crc_ctx
, 0, frame
+ frame_size_58
,
1950 s
->frame_size
- frame_size_58
- 2);
1952 crc2
= av_bswap16(crc2
);
1953 /* ensure crc2 does not match sync word by flipping crcrsv bit if needed */
1954 if (crc2
== 0x0B77) {
1955 /* The CRC generator polynomial is x^16 + x^15 + x^2 + 1,
1956 * so xor'ing with 0x18005 does not affect the CRC. */
1957 frame
[s
->frame_size
- 3] ^= 0x1;
1960 AV_WB16(frame
+ s
->frame_size
- 2, crc2
);
1965 * Write the frame to the output bitstream.
1967 * @param s AC-3 encoder private context
1968 * @param frame output data buffer
1970 static void ac3_output_frame(AC3EncodeContext
*s
, unsigned char *frame
)
1975 init_put_bits(&pb
, frame
, s
->frame_size
);
1977 s
->output_frame_header(s
, &pb
);
1979 for (blk
= 0; blk
< s
->num_blocks
; blk
++)
1980 output_audio_block(s
, &pb
, blk
);
1982 output_frame_end(s
, &pb
);
1985 int ff_ac3_encode_frame(AVCodecContext
*avctx
, AVPacket
*avpkt
,
1986 const AVFrame
*frame
, int *got_packet_ptr
)
1988 AC3EncodeContext
*const s
= avctx
->priv_data
;
1991 if (s
->options
.allow_per_frame_metadata
) {
1992 ret
= ac3_validate_metadata(s
);
1997 if (s
->bit_alloc
.sr_code
== 1 || s
->eac3
)
1998 ac3_adjust_frame_size(s
);
2000 s
->encode_frame(s
, frame
->extended_data
);
2002 ac3_apply_rematrixing(s
);
2004 ac3_process_exponents(s
);
2006 ret
= ac3_compute_bit_allocation(s
);
2008 av_log(avctx
, AV_LOG_ERROR
, "Bit allocation failed. Try increasing the bitrate.\n");
2012 ac3_group_exponents(s
);
2014 ac3_quantize_mantissas(s
);
2016 ret
= ff_get_encode_buffer(avctx
, avpkt
, s
->frame_size
, 0);
2019 ac3_output_frame(s
, avpkt
->data
);
2021 if (frame
->pts
!= AV_NOPTS_VALUE
)
2022 avpkt
->pts
= frame
->pts
- ff_samples_to_time_base(avctx
, avctx
->initial_padding
);
2024 *got_packet_ptr
= 1;
2028 static void dprint_options(AC3EncodeContext
*s
)
2031 AVCodecContext
*avctx
= s
->avctx
;
2032 AC3EncOptions
*opt
= &s
->options
;
2036 switch (s
->bitstream_id
) {
2037 case 6: msg
= "AC-3 (alt syntax)"; break;
2038 case 8: msg
= "AC-3 (standard)"; break;
2039 case 16: msg
= "E-AC-3 (enhanced)"; break;
2040 default: msg
= "ERROR";
2042 ff_dlog(avctx
, "bitstream_id: %s (%d)\n", msg
, s
->bitstream_id
);
2043 ff_dlog(avctx
, "sample_fmt: %s\n", av_get_sample_fmt_name(avctx
->sample_fmt
));
2044 av_channel_layout_describe(&avctx
->ch_layout
, strbuf
, sizeof(strbuf
));
2045 ff_dlog(avctx
, "channel_layout: %s\n", strbuf
);
2046 ff_dlog(avctx
, "sample_rate: %d\n", s
->sample_rate
);
2047 ff_dlog(avctx
, "bit_rate: %d\n", s
->bit_rate
);
2048 ff_dlog(avctx
, "blocks/frame: %d (code=%d)\n", s
->num_blocks
, s
->num_blks_code
);
2050 ff_dlog(avctx
, "cutoff: %d\n", s
->cutoff
);
2052 ff_dlog(avctx
, "per_frame_metadata: %s\n",
2053 opt
->allow_per_frame_metadata
?"on":"off");
2055 ff_dlog(avctx
, "center_mixlev: %0.3f (%d)\n", opt
->center_mix_level
,
2056 s
->center_mix_level
);
2058 ff_dlog(avctx
, "center_mixlev: {not written}\n");
2059 if (s
->has_surround
)
2060 ff_dlog(avctx
, "surround_mixlev: %0.3f (%d)\n", opt
->surround_mix_level
,
2061 s
->surround_mix_level
);
2063 ff_dlog(avctx
, "surround_mixlev: {not written}\n");
2064 if (opt
->audio_production_info
) {
2065 ff_dlog(avctx
, "mixing_level: %ddB\n", opt
->mixing_level
);
2066 switch (opt
->room_type
) {
2067 case AC3ENC_OPT_NOT_INDICATED
: msg
= "notindicated"; break;
2068 case AC3ENC_OPT_LARGE_ROOM
: msg
= "large"; break;
2069 case AC3ENC_OPT_SMALL_ROOM
: msg
= "small"; break;
2071 snprintf(strbuf
, sizeof(strbuf
), "ERROR (%d)", opt
->room_type
);
2074 ff_dlog(avctx
, "room_type: %s\n", msg
);
2076 ff_dlog(avctx
, "mixing_level: {not written}\n");
2077 ff_dlog(avctx
, "room_type: {not written}\n");
2079 ff_dlog(avctx
, "copyright: %s\n", opt
->copyright
?"on":"off");
2080 ff_dlog(avctx
, "dialnorm: %ddB\n", opt
->dialogue_level
);
2081 if (s
->channel_mode
== AC3_CHMODE_STEREO
) {
2082 switch (opt
->dolby_surround_mode
) {
2083 case AC3ENC_OPT_NOT_INDICATED
: msg
= "notindicated"; break;
2084 case AC3ENC_OPT_MODE_ON
: msg
= "on"; break;
2085 case AC3ENC_OPT_MODE_OFF
: msg
= "off"; break;
2087 snprintf(strbuf
, sizeof(strbuf
), "ERROR (%d)", opt
->dolby_surround_mode
);
2090 ff_dlog(avctx
, "dsur_mode: %s\n", msg
);
2092 ff_dlog(avctx
, "dsur_mode: {not written}\n");
2094 ff_dlog(avctx
, "original: %s\n", opt
->original
?"on":"off");
2096 if (s
->bitstream_id
== 6) {
2097 if (opt
->extended_bsi_1
) {
2098 switch (opt
->preferred_stereo_downmix
) {
2099 case AC3ENC_OPT_NOT_INDICATED
: msg
= "notindicated"; break;
2100 case AC3ENC_OPT_DOWNMIX_LTRT
: msg
= "ltrt"; break;
2101 case AC3ENC_OPT_DOWNMIX_LORO
: msg
= "loro"; break;
2103 snprintf(strbuf
, sizeof(strbuf
), "ERROR (%d)", opt
->preferred_stereo_downmix
);
2106 ff_dlog(avctx
, "dmix_mode: %s\n", msg
);
2107 ff_dlog(avctx
, "ltrt_cmixlev: %0.3f (%d)\n",
2108 opt
->ltrt_center_mix_level
, s
->ltrt_center_mix_level
);
2109 ff_dlog(avctx
, "ltrt_surmixlev: %0.3f (%d)\n",
2110 opt
->ltrt_surround_mix_level
, s
->ltrt_surround_mix_level
);
2111 ff_dlog(avctx
, "loro_cmixlev: %0.3f (%d)\n",
2112 opt
->loro_center_mix_level
, s
->loro_center_mix_level
);
2113 ff_dlog(avctx
, "loro_surmixlev: %0.3f (%d)\n",
2114 opt
->loro_surround_mix_level
, s
->loro_surround_mix_level
);
2116 ff_dlog(avctx
, "extended bitstream info 1: {not written}\n");
2118 if (opt
->extended_bsi_2
) {
2119 switch (opt
->dolby_surround_ex_mode
) {
2120 case AC3ENC_OPT_NOT_INDICATED
: msg
= "notindicated"; break;
2121 case AC3ENC_OPT_MODE_ON
: msg
= "on"; break;
2122 case AC3ENC_OPT_MODE_OFF
: msg
= "off"; break;
2124 snprintf(strbuf
, sizeof(strbuf
), "ERROR (%d)", opt
->dolby_surround_ex_mode
);
2127 ff_dlog(avctx
, "dsurex_mode: %s\n", msg
);
2128 switch (opt
->dolby_headphone_mode
) {
2129 case AC3ENC_OPT_NOT_INDICATED
: msg
= "notindicated"; break;
2130 case AC3ENC_OPT_MODE_ON
: msg
= "on"; break;
2131 case AC3ENC_OPT_MODE_OFF
: msg
= "off"; break;
2133 snprintf(strbuf
, sizeof(strbuf
), "ERROR (%d)", opt
->dolby_headphone_mode
);
2136 ff_dlog(avctx
, "dheadphone_mode: %s\n", msg
);
2138 switch (opt
->ad_converter_type
) {
2139 case AC3ENC_OPT_ADCONV_STANDARD
: msg
= "standard"; break;
2140 case AC3ENC_OPT_ADCONV_HDCD
: msg
= "hdcd"; break;
2142 snprintf(strbuf
, sizeof(strbuf
), "ERROR (%d)", opt
->ad_converter_type
);
2145 ff_dlog(avctx
, "ad_conv_type: %s\n", msg
);
2147 ff_dlog(avctx
, "extended bitstream info 2: {not written}\n");
2154 * Finalize encoding and free any memory allocated by the encoder.
2156 * @param avctx Codec context
2158 av_cold
int ff_ac3_encode_close(AVCodecContext
*avctx
)
2160 AC3EncodeContext
*s
= avctx
->priv_data
;
2162 for (int ch
= 0; ch
< s
->channels
; ch
++)
2163 av_freep(&s
->planar_samples
[ch
]);
2164 av_freep(&s
->bap_buffer
);
2165 av_freep(&s
->bap1_buffer
);
2166 av_freep(&s
->mdct_coef_buffer
);
2167 av_freep(&s
->fixed_coef_buffer
);
2168 av_freep(&s
->exp_buffer
);
2169 av_freep(&s
->grouped_exp_buffer
);
2170 av_freep(&s
->psd_buffer
);
2171 av_freep(&s
->band_psd_buffer
);
2172 av_freep(&s
->mask_buffer
);
2173 av_freep(&s
->qmant_buffer
);
2174 av_freep(&s
->cpl_coord_buffer
);
2177 av_tx_uninit(&s
->tx
);
2184 * Set channel information during initialization.
2186 static av_cold
void set_channel_info(AVCodecContext
*avctx
)
2188 AC3EncodeContext
*s
= avctx
->priv_data
;
2189 uint64_t mask
= av_channel_layout_subset(&avctx
->ch_layout
, ~(uint64_t)0);
2190 int channels
= avctx
->ch_layout
.nb_channels
;
2192 s
->lfe_on
= !!(mask
& AV_CH_LOW_FREQUENCY
);
2193 s
->channels
= channels
;
2194 s
->fbw_channels
= channels
- s
->lfe_on
;
2195 s
->lfe_channel
= s
->lfe_on
? s
->fbw_channels
+ 1 : -1;
2197 switch (mask
& ~AV_CH_LOW_FREQUENCY
) {
2198 case AV_CH_LAYOUT_MONO
: s
->channel_mode
= AC3_CHMODE_MONO
; break;
2199 case AV_CH_LAYOUT_STEREO
: s
->channel_mode
= AC3_CHMODE_STEREO
; break;
2200 case AV_CH_LAYOUT_SURROUND
: s
->channel_mode
= AC3_CHMODE_3F
; break;
2201 case AV_CH_LAYOUT_2_1
: s
->channel_mode
= AC3_CHMODE_2F1R
; break;
2202 case AV_CH_LAYOUT_4POINT0
: s
->channel_mode
= AC3_CHMODE_3F1R
; break;
2203 case AV_CH_LAYOUT_QUAD
:
2204 case AV_CH_LAYOUT_2_2
: s
->channel_mode
= AC3_CHMODE_2F2R
; break;
2205 case AV_CH_LAYOUT_5POINT0
:
2206 case AV_CH_LAYOUT_5POINT0_BACK
: s
->channel_mode
= AC3_CHMODE_3F2R
; break;
2208 s
->has_center
= (s
->channel_mode
& 0x01) && s
->channel_mode
!= AC3_CHMODE_MONO
;
2209 s
->has_surround
= s
->channel_mode
& 0x04;
2211 s
->channel_map
= ac3_enc_channel_map
[s
->channel_mode
][s
->lfe_on
];
2215 static av_cold
int validate_options(AC3EncodeContext
*s
)
2217 AVCodecContext
*avctx
= s
->avctx
;
2220 set_channel_info(avctx
);
2222 for (int i
= 0;; i
++) {
2223 if (ff_ac3_sample_rate_tab
[i
] == avctx
->sample_rate
) {
2224 s
->bit_alloc
.sr_code
= i
;
2227 av_assert1(ff_ac3_sample_rate_tab
[i
] != 0);
2229 s
->sample_rate
= avctx
->sample_rate
;
2230 s
->bitstream_id
= s
->eac3
? 16 : 8;
2232 /* select a default bit rate if not set by the user */
2233 if (!avctx
->bit_rate
) {
2234 switch (s
->fbw_channels
) {
2235 case 1: avctx
->bit_rate
= 96000; break;
2236 case 2: avctx
->bit_rate
= 192000; break;
2237 case 3: avctx
->bit_rate
= 320000; break;
2238 case 4: avctx
->bit_rate
= 384000; break;
2239 case 5: avctx
->bit_rate
= 448000; break;
2243 /* validate bit rate */
2245 int max_br
, min_br
, wpf
, min_br_code
;
2246 int num_blks_code
, num_blocks
, frame_samples
;
2247 long long min_br_dist
;
2249 /* calculate min/max bitrate */
2250 /* TODO: More testing with 3 and 2 blocks. All E-AC-3 samples I've
2251 found use either 6 blocks or 1 block, even though 2 or 3 blocks
2252 would work as far as the bit rate is concerned. */
2253 for (num_blks_code
= 3; num_blks_code
>= 0; num_blks_code
--) {
2254 num_blocks
= ((int[]){ 1, 2, 3, 6 })[num_blks_code
];
2255 frame_samples
= AC3_BLOCK_SIZE
* num_blocks
;
2256 max_br
= 2048 * s
->sample_rate
/ frame_samples
* 16;
2257 min_br
= ((s
->sample_rate
+ (frame_samples
-1)) / frame_samples
) * 16;
2258 if (avctx
->bit_rate
<= max_br
)
2261 if (avctx
->bit_rate
< min_br
|| avctx
->bit_rate
> max_br
) {
2262 av_log(avctx
, AV_LOG_ERROR
, "invalid bit rate. must be %d to %d "
2263 "for this sample rate\n", min_br
, max_br
);
2264 return AVERROR(EINVAL
);
2266 s
->num_blks_code
= num_blks_code
;
2267 s
->num_blocks
= num_blocks
;
2269 /* calculate words-per-frame for the selected bitrate */
2270 wpf
= (avctx
->bit_rate
/ 16) * frame_samples
/ s
->sample_rate
;
2271 av_assert1(wpf
> 0 && wpf
<= 2048);
2273 /* find the closest AC-3 bitrate code to the selected bitrate.
2274 this is needed for lookup tables for bandwidth and coupling
2275 parameter selection */
2277 min_br_dist
= INT64_MAX
;
2278 for (int i
= 0; i
< 19; i
++) {
2279 long long br_dist
= llabs(ff_ac3_bitrate_tab
[i
] * 1000 - avctx
->bit_rate
);
2280 if (br_dist
< min_br_dist
) {
2281 min_br_dist
= br_dist
;
2286 /* make sure the minimum frame size is below the average frame size */
2287 s
->frame_size_code
= min_br_code
<< 1;
2288 while (wpf
> 1 && wpf
* s
->sample_rate
/ AC3_FRAME_SIZE
* 16 > avctx
->bit_rate
)
2290 s
->frame_size_min
= 2 * wpf
;
2292 int best_br
= 0, best_code
= 0;
2293 long long best_diff
= INT64_MAX
;
2294 for (int i
= 0; i
< 19; i
++) {
2295 int br
= ff_ac3_bitrate_tab
[i
] * 1000;
2296 long long diff
= llabs(br
- avctx
->bit_rate
);
2297 if (diff
< best_diff
) {
2305 avctx
->bit_rate
= best_br
;
2306 s
->frame_size_code
= best_code
<< 1;
2307 s
->frame_size_min
= 2 * ff_ac3_frame_size_tab
[s
->frame_size_code
][s
->bit_alloc
.sr_code
];
2308 s
->num_blks_code
= 0x3;
2311 s
->bit_rate
= avctx
->bit_rate
;
2312 s
->frame_size
= s
->frame_size_min
;
2314 /* validate cutoff */
2315 if (avctx
->cutoff
< 0) {
2316 av_log(avctx
, AV_LOG_ERROR
, "invalid cutoff frequency\n");
2317 return AVERROR(EINVAL
);
2319 s
->cutoff
= avctx
->cutoff
;
2320 if (s
->cutoff
> (s
->sample_rate
>> 1))
2321 s
->cutoff
= s
->sample_rate
>> 1;
2323 ret
= ac3_validate_metadata(s
);
2327 s
->rematrixing_enabled
= s
->options
.stereo_rematrixing
&&
2328 (s
->channel_mode
== AC3_CHMODE_STEREO
);
2330 s
->cpl_enabled
= s
->options
.channel_coupling
&&
2331 s
->channel_mode
>= AC3_CHMODE_STEREO
;
2338 * Set bandwidth for all channels.
2339 * The user can optionally supply a cutoff frequency. Otherwise an appropriate
2340 * default value will be used.
2342 static av_cold
void set_bandwidth(AC3EncodeContext
*s
)
2344 int blk
, ch
, av_uninit(cpl_start
);
2347 /* calculate bandwidth based on user-specified cutoff frequency */
2349 fbw_coeffs
= s
->cutoff
* 2 * AC3_MAX_COEFS
/ s
->sample_rate
;
2350 s
->bandwidth_code
= av_clip((fbw_coeffs
- 73) / 3, 0, 60);
2352 /* use default bandwidth setting */
2353 s
->bandwidth_code
= ac3_bandwidth_tab
[s
->fbw_channels
-1][s
->bit_alloc
.sr_code
][s
->frame_size_code
/2];
2356 /* set number of coefficients for each channel */
2357 for (ch
= 1; ch
<= s
->fbw_channels
; ch
++) {
2358 s
->start_freq
[ch
] = 0;
2359 for (blk
= 0; blk
< s
->num_blocks
; blk
++)
2360 s
->blocks
[blk
].end_freq
[ch
] = s
->bandwidth_code
* 3 + 73;
2362 /* LFE channel always has 7 coefs */
2364 s
->start_freq
[s
->lfe_channel
] = 0;
2365 for (blk
= 0; blk
< s
->num_blocks
; blk
++)
2366 s
->blocks
[blk
].end_freq
[ch
] = 7;
2369 /* initialize coupling strategy */
2370 if (s
->cpl_enabled
) {
2371 if (s
->options
.cpl_start
!= AC3ENC_OPT_AUTO
) {
2372 cpl_start
= s
->options
.cpl_start
;
2374 cpl_start
= ac3_coupling_start_tab
[s
->channel_mode
-2][s
->bit_alloc
.sr_code
][s
->frame_size_code
/2];
2375 if (cpl_start
< 0) {
2376 if (s
->options
.channel_coupling
== AC3ENC_OPT_AUTO
)
2383 if (s
->cpl_enabled
) {
2384 int i
, cpl_start_band
, cpl_end_band
;
2385 uint8_t *cpl_band_sizes
= s
->cpl_band_sizes
;
2387 cpl_end_band
= s
->bandwidth_code
/ 4 + 3;
2388 cpl_start_band
= av_clip(cpl_start
, 0, FFMIN(cpl_end_band
-1, 15));
2390 s
->num_cpl_subbands
= cpl_end_band
- cpl_start_band
;
2392 s
->num_cpl_bands
= 1;
2393 *cpl_band_sizes
= 12;
2394 for (i
= cpl_start_band
+ 1; i
< cpl_end_band
; i
++) {
2395 if (ff_eac3_default_cpl_band_struct
[i
]) {
2396 *cpl_band_sizes
+= 12;
2400 *cpl_band_sizes
= 12;
2404 s
->start_freq
[CPL_CH
] = cpl_start_band
* 12 + 37;
2405 s
->cpl_end_freq
= cpl_end_band
* 12 + 37;
2406 for (blk
= 0; blk
< s
->num_blocks
; blk
++)
2407 s
->blocks
[blk
].end_freq
[CPL_CH
] = s
->cpl_end_freq
;
2412 static av_cold
int allocate_buffers(AC3EncodeContext
*s
)
2415 int channels
= s
->channels
+ 1; /* includes coupling channel */
2416 int channel_blocks
= channels
* s
->num_blocks
;
2417 int total_coefs
= AC3_MAX_COEFS
* channel_blocks
;
2418 uint8_t *cpl_coord_mant_buffer
;
2419 const unsigned sampletype_size
= SAMPLETYPE_SIZE(s
);
2421 for (int ch
= 0; ch
< s
->channels
; ch
++) {
2422 s
->planar_samples
[ch
] = av_mallocz(AC3_BLOCK_SIZE
* sampletype_size
);
2423 if (!s
->planar_samples
[ch
])
2424 return AVERROR(ENOMEM
);
2427 if (!FF_ALLOC_TYPED_ARRAY(s
->bap_buffer
, total_coefs
) ||
2428 !FF_ALLOC_TYPED_ARRAY(s
->bap1_buffer
, total_coefs
) ||
2429 !FF_ALLOCZ_TYPED_ARRAY(s
->mdct_coef_buffer
, total_coefs
) ||
2430 !FF_ALLOC_TYPED_ARRAY(s
->exp_buffer
, total_coefs
) ||
2431 !FF_ALLOC_TYPED_ARRAY(s
->grouped_exp_buffer
, channel_blocks
* 128) ||
2432 !FF_ALLOC_TYPED_ARRAY(s
->psd_buffer
, total_coefs
) ||
2433 !FF_ALLOC_TYPED_ARRAY(s
->band_psd_buffer
, channel_blocks
* 64) ||
2434 !FF_ALLOC_TYPED_ARRAY(s
->mask_buffer
, channel_blocks
* 64) ||
2435 !FF_ALLOC_TYPED_ARRAY(s
->qmant_buffer
, total_coefs
))
2436 return AVERROR(ENOMEM
);
2438 if (!s
->fixed_point
) {
2439 if (!FF_ALLOCZ_TYPED_ARRAY(s
->fixed_coef_buffer
, total_coefs
))
2440 return AVERROR(ENOMEM
);
2442 if (s
->cpl_enabled
) {
2443 if (!FF_ALLOC_TYPED_ARRAY(s
->cpl_coord_buffer
, channel_blocks
* 32))
2444 return AVERROR(ENOMEM
);
2445 cpl_coord_mant_buffer
= s
->cpl_coord_buffer
+ 16 * channel_blocks
;
2447 for (blk
= 0; blk
< s
->num_blocks
; blk
++) {
2448 AC3Block
*block
= &s
->blocks
[blk
];
2450 for (ch
= 0; ch
< channels
; ch
++) {
2451 /* arrangement: block, channel, coeff */
2452 block
->grouped_exp
[ch
] = &s
->grouped_exp_buffer
[128 * (blk
* channels
+ ch
)];
2453 block
->psd
[ch
] = &s
->psd_buffer
[AC3_MAX_COEFS
* (blk
* channels
+ ch
)];
2454 block
->band_psd
[ch
] = &s
->band_psd_buffer
[64 * (blk
* channels
+ ch
)];
2455 block
->mask
[ch
] = &s
->mask_buffer
[64 * (blk
* channels
+ ch
)];
2456 block
->qmant
[ch
] = &s
->qmant_buffer
[AC3_MAX_COEFS
* (blk
* channels
+ ch
)];
2457 if (s
->cpl_enabled
) {
2458 block
->cpl_coord_exp
[ch
] = &s
->cpl_coord_buffer
[16 * (blk
* channels
+ ch
)];
2459 block
->cpl_coord_mant
[ch
] = &cpl_coord_mant_buffer
[16 * (blk
* channels
+ ch
)];
2462 /* arrangement: channel, block, coeff */
2463 block
->exp
[ch
] = &s
->exp_buffer
[AC3_MAX_COEFS
* (s
->num_blocks
* ch
+ blk
)];
2464 block
->mdct_coef
[ch
] = &s
->mdct_coef_buffer
[AC3_MAX_COEFS
* (s
->num_blocks
* ch
+ blk
)];
2466 block
->fixed_coef
[ch
] = (int32_t *)block
->mdct_coef
[ch
];
2468 block
->fixed_coef
[ch
] = &s
->fixed_coef_buffer
[AC3_MAX_COEFS
* (s
->num_blocks
* ch
+ blk
)];
2476 av_cold
int ff_ac3_encode_init(AVCodecContext
*avctx
)
2478 static AVOnce init_static_once
= AV_ONCE_INIT
;
2479 AC3EncodeContext
*s
= avctx
->priv_data
;
2480 int ret
, frame_size_58
;
2484 ret
= validate_options(s
);
2488 avctx
->frame_size
= AC3_BLOCK_SIZE
* s
->num_blocks
;
2489 avctx
->initial_padding
= AC3_BLOCK_SIZE
;
2491 s
->bitstream_mode
= avctx
->audio_service_type
;
2492 if (s
->bitstream_mode
== AV_AUDIO_SERVICE_TYPE_KARAOKE
)
2493 s
->bitstream_mode
= 0x7;
2495 s
->bits_written
= 0;
2496 s
->samples_written
= 0;
2498 /* calculate crc_inv for both possible frame sizes */
2499 frame_size_58
= (( s
->frame_size
>> 2) + ( s
->frame_size
>> 4)) << 1;
2500 s
->crc_inv
[0] = pow_poly((CRC16_POLY
>> 1), (8 * frame_size_58
) - 16, CRC16_POLY
);
2501 if (s
->bit_alloc
.sr_code
== 1) {
2502 frame_size_58
= (((s
->frame_size
+2) >> 2) + ((s
->frame_size
+2) >> 4)) << 1;
2503 s
->crc_inv
[1] = pow_poly((CRC16_POLY
>> 1), (8 * frame_size_58
) - 16, CRC16_POLY
);
2506 if (!s
->output_frame_header
)
2507 s
->output_frame_header
= ac3_output_frame_header
;
2513 ret
= allocate_buffers(s
);
2517 ff_audiodsp_init(&s
->adsp
);
2518 ff_me_cmp_init(&s
->mecc
, avctx
);
2519 ff_ac3dsp_init(&s
->ac3dsp
);
2523 ff_thread_once(&init_static_once
, exponent_init
);