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