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