FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ac3enc.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 1011 1206 83.8%
Branches: 624 867 72.0%

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