FFmpeg coverage


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