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