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