Revision 991f3de1

View differences:

doc/encoders.texi
17 17
The option @code{-codecs} of the ff* tools will display the list of
18 18
enabled encoders.
19 19

  
20
A description of some of the currently available encoders follows.
21

  
22
@section Audio Encoders
23

  
24
@subsection ac3 and ac3_fixed
25

  
26
AC-3 audio encoders.
27

  
28
These encoders implement part of ATSC A/52:2010 and ETSI TS 102 366, as well as
29
the undocumented RealAudio 3 (a.k.a. dnet).
30

  
31
The @var{ac3} encoder uses floating-point math, while the @var{ac3_fixed}
32
encoder only uses fixed-point integer math. This does not mean that one is
33
always faster, just that one or the other may be better suited to a
34
particular system. The floating-point encoder will generally produce better
35
quality audio for a given bitrate. The @var{ac3_fixed} encoder is not the
36
default codec for any of the output formats, so it must be specified explicitly
37
using the option @code{-acodec ac3_fixed} in order to use it.
38

  
39
@subheading AC-3 Metadata
40

  
41
The AC-3 metadata options are used to set parameters that describe the audio,
42
but in most cases do not affect the audio encoding itself. Some of the options
43
do directly affect or influence the decoding and playback of the resulting
44
bitstream, while others are just for informational purposes. A few of the
45
options will add bits to the output stream that could otherwise be used for
46
audio data, and will thus affect the quality of the output. Those will be
47
indicated accordingly with a note in the option list below.
48

  
49
These parameters are described in detail in several publicly-available
50
documents.
51
@itemize
52
@item @uref{http://www.atsc.org/cms/standards/a_52-2010.pdf,A/52:2010 - Digital Audio Compression (AC-3) (E-AC-3) Standard}
53
@item @uref{http://www.atsc.org/cms/standards/a_54a_with_corr_1.pdf,A/54 - Guide to the Use of the ATSC Digital Television Standard}
54
@item @uref{http://www.dolby.com/uploadedFiles/zz-_Shared_Assets/English_PDFs/Professional/18_Metadata.Guide.pdf,Dolby Metadata Guide}
55
@item @uref{http://www.dolby.com/uploadedFiles/zz-_Shared_Assets/English_PDFs/Professional/46_DDEncodingGuidelines.pdf,Dolby Digital Professional Encoding Guidelines}
56
@end itemize
57

  
58
@subsubheading Metadata Control Options
59

  
60
@table @option
61

  
62
@item -per_frame_metadata @var{boolean}
63
Allow Per-Frame Metadata. Specifies if the encoder should check for changing
64
metadata for each frame.
65
@table @option
66
@item 0
67
The metadata values set at initialization will be used for every frame in the
68
stream. (default)
69
@item 1
70
Metadata values can be changed before encoding each frame.
71
@end table
72

  
73
@end table
74

  
75
@subsubheading Downmix Levels
76

  
77
@table @option
78

  
79
@item -center_mixlev @var{level}
80
Center Mix Level. The amount of gain the decoder should apply to the center
81
channel when downmixing to stereo. This field will only be written to the
82
bitstream if a center channel is present. The value is specified as a scale
83
factor. There are 3 valid values:
84
@table @option
85
@item 0.707
86
Apply -3dB gain
87
@item 0.595
88
Apply -4.5dB gain (default)
89
@item 0.500
90
Apply -6dB gain
91
@end table
92

  
93
@item -surround_mixlev @var{level}
94
Surround Mix Level. The amount of gain the decoder should apply to the surround
95
channel(s) when downmixing to stereo. This field will only be written to the
96
bitstream if one or more surround channels are present. The value is specified
97
as a scale factor.  There are 3 valid values:
98
@table @option
99
@item 0.707
100
Apply -3dB gain
101
@item 0.500
102
Apply -6dB gain (default)
103
@item 0.000
104
Silence Surround Channel(s)
105
@end table
106

  
107
@end table
108

  
109
@subsubheading Audio Production Information
110
Audio Production Information is optional information describing the mixing
111
environment.  Either none or both of the fields are written to the bitstream.
112

  
113
@table @option
114

  
115
@item -mixing_level @var{number}
116
Mixing Level. Specifies peak sound pressure level (SPL) in the production
117
environment when the mix was mastered. Valid values are 80 to 111, or -1 for
118
unknown or not indicated. The default value is -1, but that value cannot be
119
used if the Audio Production Information is written to the bitstream. Therefore,
120
if the @code{room_type} option is not the default value, the @code{mixing_level}
121
option must not be -1.
122

  
123
@item -room_type @var{type}
124
Room Type. Describes the equalization used during the final mixing session at
125
the studio or on the dubbing stage. A large room is a dubbing stage with the
126
industry standard X-curve equalization; a small room has flat equalization.
127
This field will not be written to the bitstream if both the @code{mixing_level}
128
option and the @code{room_type} option have the default values.
129
@table @option
130
@item 0
131
@itemx notindicated
132
Not Indicated (default)
133
@item 1
134
@itemx large
135
Large Room
136
@item 2
137
@itemx small
138
Small Room
139
@end table
140

  
141
@end table
142

  
143
@subsubheading Other Metadata Options
144

  
145
@table @option
146

  
147
@item -copyright @var{boolean}
148
Copyright Indicator. Specifies whether a copyright exists for this audio.
149
@table @option
150
@item 0
151
@itemx off
152
No Copyright Exists (default)
153
@item 1
154
@itemx on
155
Copyright Exists
156
@end table
157

  
158
@item -dialnorm @var{value}
159
Dialogue Normalization. Indicates how far the average dialogue level of the
160
program is below digital 100% full scale (0 dBFS). This parameter determines a
161
level shift during audio reproduction that sets the average volume of the
162
dialogue to a preset level. The goal is to match volume level between program
163
sources. A value of -31dB will result in no volume level change, relative to
164
the source volume, during audio reproduction. Valid values are whole numbers in
165
the range -31 to -1, with -31 being the default.
166

  
167
@item -dsur_mode @var{mode}
168
Dolby Surround Mode. Specifies whether the stereo signal uses Dolby Surround
169
(Pro Logic). This field will only be written to the bitstream if the audio
170
stream is stereo. Using this option does @b{NOT} mean the encoder will actually
171
apply Dolby Surround processing.
172
@table @option
173
@item 0
174
@itemx notindicated
175
Not Indicated (default)
176
@item 1
177
@itemx off
178
Not Dolby Surround Encoded
179
@item 2
180
@itemx on
181
Dolby Surround Encoded
182
@end table
183

  
184
@item -original @var{boolean}
185
Original Bit Stream Indicator. Specifies whether this audio is from the
186
original source and not a copy.
187
@table @option
188
@item 0
189
@itemx off
190
Not Original Source
191
@item 1
192
@itemx on
193
Original Source (default)
194
@end table
195

  
196
@end table
197

  
198
@subsubheading Extended Bitstream Information
199
The extended bitstream options are part of the Alternate Bit Stream Syntax as
200
specified in Annex D of the A/52:2010 standard. It is grouped into 2 parts.
201
If any one parameter in a group is specified, all values in that group will be
202
written to the bitstream.  Default values are used for those that are written
203
but have not been specified.  If the mixing levels are written, the decoder
204
will use these values instead of the ones specified in the @code{center_mixlev}
205
and @code{surround_mixlev} options if it supports the Alternate Bit Stream
206
Syntax.
207

  
208
@subsubheading Extended Bitstream Information - Part 1
209

  
210
@table @option
211

  
212
@item -dmix_mode @var{mode}
213
Preferred Stereo Downmix Mode. Allows the user to select either Lt/Rt
214
(Dolby Surround) or Lo/Ro (normal stereo) as the preferred stereo downmix mode.
215
@table @option
216
@item 0
217
@itemx notindicated
218
Not Indicated (default)
219
@item 1
220
@itemx ltrt
221
Lt/Rt Downmix Preferred
222
@item 2
223
@itemx loro
224
Lo/Ro Downmix Preferred
225
@end table
226

  
227
@item -ltrt_cmixlev @var{level}
228
Lt/Rt Center Mix Level. The amount of gain the decoder should apply to the
229
center channel when downmixing to stereo in Lt/Rt mode.
230
@table @option
231
@item 1.414
232
Apply +3dB gain
233
@item 1.189
234
Apply +1.5dB gain
235
@item 1.000
236
Apply 0dB gain
237
@item 0.841
238
Apply -1.5dB gain
239
@item 0.707
240
Apply -3.0dB gain
241
@item 0.595
242
Apply -4.5dB gain (default)
243
@item 0.500
244
Apply -6.0dB gain
245
@item 0.000
246
Silence Center Channel
247
@end table
248

  
249
@item -ltrt_surmixlev @var{level}
250
Lt/Rt Surround Mix Level. The amount of gain the decoder should apply to the
251
surround channel(s) when downmixing to stereo in Lt/Rt mode.
252
@table @option
253
@item 0.841
254
Apply -1.5dB gain
255
@item 0.707
256
Apply -3.0dB gain
257
@item 0.595
258
Apply -4.5dB gain
259
@item 0.500
260
Apply -6.0dB gain (default)
261
@item 0.000
262
Silence Surround Channel(s)
263
@end table
264

  
265
@item -loro_cmixlev @var{level}
266
Lo/Ro Center Mix Level. The amount of gain the decoder should apply to the
267
center channel when downmixing to stereo in Lo/Ro mode.
268
@table @option
269
@item 1.414
270
Apply +3dB gain
271
@item 1.189
272
Apply +1.5dB gain
273
@item 1.000
274
Apply 0dB gain
275
@item 0.841
276
Apply -1.5dB gain
277
@item 0.707
278
Apply -3.0dB gain
279
@item 0.595
280
Apply -4.5dB gain (default)
281
@item 0.500
282
Apply -6.0dB gain
283
@item 0.000
284
Silence Center Channel
285
@end table
286

  
287
@item -loro_surmixlev @var{level}
288
Lo/Ro Surround Mix Level. The amount of gain the decoder should apply to the
289
surround channel(s) when downmixing to stereo in Lo/Ro mode.
290
@table @option
291
@item 0.841
292
Apply -1.5dB gain
293
@item 0.707
294
Apply -3.0dB gain
295
@item 0.595
296
Apply -4.5dB gain
297
@item 0.500
298
Apply -6.0dB gain (default)
299
@item 0.000
300
Silence Surround Channel(s)
301
@end table
302

  
303
@end table
304

  
305
@subsubheading Extended Bitstream Information - Part 2
306

  
307
@table @option
308

  
309
@item -dsurex_mode @var{mode}
310
Dolby Surround EX Mode. Indicates whether the stream uses Dolby Surround EX
311
(7.1 matrixed to 5.1). Using this option does @b{NOT} mean the encoder will actually
312
apply Dolby Surround EX processing.
313
@table @option
314
@item 0
315
@itemx notindicated
316
Not Indicated (default)
317
@item 1
318
@itemx on
319
Dolby Surround EX On
320
@item 2
321
@itemx off
322
Dolby Surround EX Off
323
@end table
324

  
325
@item -dheadphone_mode @var{mode}
326
Dolby Headphone Mode. Indicates whether the stream uses Dolby Headphone
327
encoding (multi-channel matrixed to 2.0 for use with headphones). Using this
328
option does @b{NOT} mean the encoder will actually apply Dolby Headphone
329
processing.
330
@table @option
331
@item 0
332
@itemx notindicated
333
Not Indicated (default)
334
@item 1
335
@itemx on
336
Dolby Headphone On
337
@item 2
338
@itemx off
339
Dolby Headphone Off
340
@end table
341

  
342
@item -ad_conv_type @var{type}
343
A/D Converter Type. Indicates whether the audio has passed through HDCD A/D
344
conversion.
345
@table @option
346
@item 0
347
@itemx standard
348
Standard A/D Converter (default)
349
@item 1
350
@itemx hdcd
351
HDCD A/D Converter
352
@end table
353

  
354
@end table
355

  
20 356
@c man end ENCODERS
libavcodec/ac3.h
48 48
#define EXP_D25   2
49 49
#define EXP_D45   3
50 50

  
51
/* pre-defined gain values */
52
#define LEVEL_PLUS_3DB          1.4142135623730950
53
#define LEVEL_PLUS_1POINT5DB    1.1892071150027209
54
#define LEVEL_MINUS_1POINT5DB   0.8408964152537145
55
#define LEVEL_MINUS_3DB         0.7071067811865476
56
#define LEVEL_MINUS_4POINT5DB   0.5946035575013605
57
#define LEVEL_MINUS_6DB         0.5000000000000000
58
#define LEVEL_MINUS_9DB         0.3535533905932738
59
#define LEVEL_ZERO              0.0000000000000000
60
#define LEVEL_ONE               1.0000000000000000
61

  
51 62
/** Delta bit allocation strategy */
52 63
typedef enum {
53 64
    DBA_REUSE = 0,
libavcodec/ac3dec.c
67 67
static float dynamic_range_tab[256];
68 68

  
69 69
/** Adjustments in dB gain */
70
#define LEVEL_PLUS_3DB          1.4142135623730950
71
#define LEVEL_PLUS_1POINT5DB    1.1892071150027209
72
#define LEVEL_MINUS_1POINT5DB   0.8408964152537145
73
#define LEVEL_MINUS_3DB         0.7071067811865476
74
#define LEVEL_MINUS_4POINT5DB   0.5946035575013605
75
#define LEVEL_MINUS_6DB         0.5000000000000000
76
#define LEVEL_MINUS_9DB         0.3535533905932738
77
#define LEVEL_ZERO              0.0000000000000000
78
#define LEVEL_ONE               1.0000000000000000
79

  
80 70
static const float gain_levels[9] = {
81 71
    LEVEL_PLUS_3DB,
82 72
    LEVEL_PLUS_1POINT5DB,
libavcodec/ac3enc.c
32 32
#include "libavutil/audioconvert.h"
33 33
#include "libavutil/avassert.h"
34 34
#include "libavutil/crc.h"
35
#include "libavutil/opt.h"
35 36
#include "avcodec.h"
36 37
#include "put_bits.h"
37 38
#include "dsputil.h"
......
66 67

  
67 68

  
68 69
/**
70
 * Encoding Options used by AVOption.
71
 */
72
typedef struct AC3EncOptions {
73
    /* AC-3 metadata options*/
74
    int dialogue_level;
75
    int bitstream_mode;
76
    float center_mix_level;
77
    float surround_mix_level;
78
    int dolby_surround_mode;
79
    int audio_production_info;
80
    int mixing_level;
81
    int room_type;
82
    int copyright;
83
    int original;
84
    int extended_bsi_1;
85
    int preferred_stereo_downmix;
86
    float ltrt_center_mix_level;
87
    float ltrt_surround_mix_level;
88
    float loro_center_mix_level;
89
    float loro_surround_mix_level;
90
    int extended_bsi_2;
91
    int dolby_surround_ex_mode;
92
    int dolby_headphone_mode;
93
    int ad_converter_type;
94

  
95
    /* other encoding options */
96
    int allow_per_frame_metadata;
97
} AC3EncOptions;
98

  
99
/**
69 100
 * Data for a single audio block.
70 101
 */
71 102
typedef struct AC3Block {
......
87 118
 * AC-3 encoder private context.
88 119
 */
89 120
typedef struct AC3EncodeContext {
121
    AVClass *av_class;                      ///< AVClass used for AVOption
122
    AC3EncOptions options;                  ///< encoding options
90 123
    PutBitContext pb;                       ///< bitstream writer context
91 124
    DSPContext dsp;
92 125
    AC3DSPContext ac3dsp;                   ///< AC-3 optimized functions
......
111 144
    int channels;                           ///< total number of channels               (nchans)
112 145
    int lfe_on;                             ///< indicates if there is an LFE channel   (lfeon)
113 146
    int lfe_channel;                        ///< channel index of the LFE channel
147
    int has_center;                         ///< indicates if there is a center channel
148
    int has_surround;                       ///< indicates if there are one or more surround channels
114 149
    int channel_mode;                       ///< channel mode                           (acmod)
115 150
    const uint8_t *channel_map;             ///< channel map used to reorder channels
116 151

  
152
    int center_mix_level;                   ///< center mix level code
153
    int surround_mix_level;                 ///< surround mix level code
154
    int ltrt_center_mix_level;              ///< Lt/Rt center mix level code
155
    int ltrt_surround_mix_level;            ///< Lt/Rt surround mix level code
156
    int loro_center_mix_level;              ///< Lo/Ro center mix level code
157
    int loro_surround_mix_level;            ///< Lo/Ro surround mix level code
158

  
117 159
    int cutoff;                             ///< user-specified cutoff frequency, in Hz
118 160
    int bandwidth_code[AC3_MAX_CHANNELS];   ///< bandwidth code (0 to 60)               (chbwcod)
119 161
    int nb_coefs[AC3_MAX_CHANNELS];
......
157 199
} AC3EncodeContext;
158 200

  
159 201

  
202
#define CMIXLEV_NUM_OPTIONS 3
203
static const float cmixlev_options[CMIXLEV_NUM_OPTIONS] = {
204
    LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB
205
};
206

  
207
#define SURMIXLEV_NUM_OPTIONS 3
208
static const float surmixlev_options[SURMIXLEV_NUM_OPTIONS] = {
209
    LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO
210
};
211

  
212
#define EXTMIXLEV_NUM_OPTIONS 8
213
static const float extmixlev_options[EXTMIXLEV_NUM_OPTIONS] = {
214
    LEVEL_PLUS_3DB,  LEVEL_PLUS_1POINT5DB,  LEVEL_ONE,       LEVEL_MINUS_4POINT5DB,
215
    LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB, LEVEL_ZERO
216
};
217

  
218

  
219
#define OFFSET(param) offsetof(AC3EncodeContext, options.param)
220
#define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
221

  
222
static const AVOption options[] = {
223
/* Metadata Options */
224
{"per_frame_metadata", "Allow Changing Metadata Per-Frame", OFFSET(allow_per_frame_metadata), FF_OPT_TYPE_INT, 0, 0, 1, AC3ENC_PARAM},
225
/* downmix levels */
226
{"center_mixlev", "Center Mix Level", OFFSET(center_mix_level), FF_OPT_TYPE_FLOAT, LEVEL_MINUS_4POINT5DB, 0.0, 1.0, AC3ENC_PARAM},
227
{"surround_mixlev", "Surround Mix Level", OFFSET(surround_mix_level), FF_OPT_TYPE_FLOAT, LEVEL_MINUS_6DB, 0.0, 1.0, AC3ENC_PARAM},
228
/* audio production information */
229
{"mixing_level", "Mixing Level", OFFSET(mixing_level), FF_OPT_TYPE_INT, -1, -1, 111, AC3ENC_PARAM},
230
{"room_type", "Room Type", OFFSET(room_type), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "room_type"},
231
    {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
232
    {"large",        "Large Room",              0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
233
    {"small",        "Small Room",              0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
234
/* other metadata options */
235
{"copyright", "Copyright Bit", OFFSET(copyright), FF_OPT_TYPE_INT, 0, 0, 1, AC3ENC_PARAM},
236
{"dialnorm", "Dialogue Level (dB)", OFFSET(dialogue_level), FF_OPT_TYPE_INT, -31, -31, -1, AC3ENC_PARAM},
237
{"dsur_mode", "Dolby Surround Mode", OFFSET(dolby_surround_mode), FF_OPT_TYPE_INT, 0, 0, 2, AC3ENC_PARAM, "dsur_mode"},
238
    {"notindicated", "Not Indicated (default)",    0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
239
    {"on",           "Dolby Surround Encoded",     0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
240
    {"off",          "Not Dolby Surround Encoded", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
241
{"original", "Original Bit Stream", OFFSET(original), FF_OPT_TYPE_INT, 1, 0, 1, AC3ENC_PARAM},
242
/* extended bitstream information */
243
{"dmix_mode", "Preferred Stereo Downmix Mode", OFFSET(preferred_stereo_downmix), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "dmix_mode"},
244
    {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
245
    {"ltrt", "Lt/Rt Downmix Preferred",         0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
246
    {"loro", "Lo/Ro Downmix Preferred",         0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
247
{"ltrt_cmixlev", "Lt/Rt Center Mix Level", OFFSET(ltrt_center_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
248
{"ltrt_surmixlev", "Lt/Rt Surround Mix Level", OFFSET(ltrt_surround_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
249
{"loro_cmixlev", "Lo/Ro Center Mix Level", OFFSET(loro_center_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
250
{"loro_surmixlev", "Lo/Ro Surround Mix Level", OFFSET(loro_surround_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
251
{"dsurex_mode", "Dolby Surround EX Mode", OFFSET(dolby_surround_ex_mode), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "dsurex_mode"},
252
    {"notindicated", "Not Indicated (default)",       0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
253
    {"on",           "Dolby Surround EX Encoded",     0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
254
    {"off",          "Not Dolby Surround EX Encoded", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
255
{"dheadphone_mode", "Dolby Headphone Mode", OFFSET(dolby_headphone_mode), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "dheadphone_mode"},
256
    {"notindicated", "Not Indicated (default)",     0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
257
    {"on",           "Dolby Headphone Encoded",     0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
258
    {"off",          "Not Dolby Headphone Encoded", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
259
{"ad_conv_type", "A/D Converter Type", OFFSET(ad_converter_type), FF_OPT_TYPE_INT, -1, -1, 1, AC3ENC_PARAM, "ad_conv_type"},
260
    {"standard", "Standard (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
261
    {"hdcd",     "HDCD",               0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
262
{NULL}
263
};
264

  
265
#if CONFIG_AC3ENC_FLOAT
266
static AVClass ac3enc_class = { "AC-3 Encoder", av_default_item_name,
267
                                options, LIBAVUTIL_VERSION_INT };
268
#else
269
static AVClass ac3enc_class = { "Fixed-Point AC-3 Encoder", av_default_item_name,
270
                                options, LIBAVUTIL_VERSION_INT };
271
#endif
272

  
273

  
160 274
/* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
161 275

  
162 276
static av_cold void mdct_end(AC3MDCTContext *mdct);
......
786 900
 */
787 901
static void count_frame_bits(AC3EncodeContext *s)
788 902
{
903
    AC3EncOptions *opt = &s->options;
789 904
    int blk, ch;
790 905
    int frame_bits = 0;
791 906

  
907
    if (opt->audio_production_info)
908
        frame_bits += 7;
909
    if (s->bitstream_id == 6) {
910
        if (opt->extended_bsi_1)
911
            frame_bits += 14;
912
        if (opt->extended_bsi_2)
913
            frame_bits += 14;
914
    }
915

  
792 916
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
793 917
        /* stereo rematrixing */
794 918
        if (s->channel_mode == AC3_CHMODE_STEREO &&
......
1245 1369
 */
1246 1370
static void output_frame_header(AC3EncodeContext *s)
1247 1371
{
1372
    AC3EncOptions *opt = &s->options;
1373

  
1248 1374
    put_bits(&s->pb, 16, 0x0b77);   /* frame header */
1249 1375
    put_bits(&s->pb, 16, 0);        /* crc1: will be filled later */
1250 1376
    put_bits(&s->pb, 2,  s->bit_alloc.sr_code);
......
1253 1379
    put_bits(&s->pb, 3,  s->bitstream_mode);
1254 1380
    put_bits(&s->pb, 3,  s->channel_mode);
1255 1381
    if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
1256
        put_bits(&s->pb, 2, 1);     /* XXX -4.5 dB */
1382
        put_bits(&s->pb, 2, s->center_mix_level);
1257 1383
    if (s->channel_mode & 0x04)
1258
        put_bits(&s->pb, 2, 1);     /* XXX -6 dB */
1384
        put_bits(&s->pb, 2, s->surround_mix_level);
1259 1385
    if (s->channel_mode == AC3_CHMODE_STEREO)
1260
        put_bits(&s->pb, 2, 0);     /* surround not indicated */
1386
        put_bits(&s->pb, 2, opt->dolby_surround_mode);
1261 1387
    put_bits(&s->pb, 1, s->lfe_on); /* LFE */
1262
    put_bits(&s->pb, 5, 31);        /* dialog norm: -31 db */
1388
    put_bits(&s->pb, 5, -opt->dialogue_level);
1263 1389
    put_bits(&s->pb, 1, 0);         /* no compression control word */
1264 1390
    put_bits(&s->pb, 1, 0);         /* no lang code */
1265
    put_bits(&s->pb, 1, 0);         /* no audio production info */
1266
    put_bits(&s->pb, 1, 0);         /* no copyright */
1267
    put_bits(&s->pb, 1, 1);         /* original bitstream */
1391
    put_bits(&s->pb, 1, opt->audio_production_info);
1392
    if (opt->audio_production_info) {
1393
        put_bits(&s->pb, 5, opt->mixing_level - 80);
1394
        put_bits(&s->pb, 2, opt->room_type);
1395
    }
1396
    put_bits(&s->pb, 1, opt->copyright);
1397
    put_bits(&s->pb, 1, opt->original);
1398
    if (s->bitstream_id == 6) {
1399
        /* alternate bit stream syntax */
1400
        put_bits(&s->pb, 1, opt->extended_bsi_1);
1401
        if (opt->extended_bsi_1) {
1402
            put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
1403
            put_bits(&s->pb, 3, s->ltrt_center_mix_level);
1404
            put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
1405
            put_bits(&s->pb, 3, s->loro_center_mix_level);
1406
            put_bits(&s->pb, 3, s->loro_surround_mix_level);
1407
        }
1408
        put_bits(&s->pb, 1, opt->extended_bsi_2);
1409
        if (opt->extended_bsi_2) {
1410
            put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
1411
            put_bits(&s->pb, 2, opt->dolby_headphone_mode);
1412
            put_bits(&s->pb, 1, opt->ad_converter_type);
1413
            put_bits(&s->pb, 9, 0);     /* xbsi2 and encinfo : reserved */
1414
        }
1415
    } else {
1268 1416
    put_bits(&s->pb, 1, 0);         /* no time code 1 */
1269 1417
    put_bits(&s->pb, 1, 0);         /* no time code 2 */
1418
    }
1270 1419
    put_bits(&s->pb, 1, 0);         /* no additional bit stream info */
1271 1420
}
1272 1421

  
......
1479 1628
}
1480 1629

  
1481 1630

  
1631
static void dprint_options(AVCodecContext *avctx)
1632
{
1633
#ifdef DEBUG
1634
    AC3EncodeContext *s = avctx->priv_data;
1635
    AC3EncOptions *opt = &s->options;
1636
    char strbuf[32];
1637

  
1638
    switch (s->bitstream_id) {
1639
    case  6:  strncpy(strbuf, "AC-3 (alt syntax)", 32);      break;
1640
    case  8:  strncpy(strbuf, "AC-3 (standard)", 32);        break;
1641
    case  9:  strncpy(strbuf, "AC-3 (dnet half-rate)", 32);  break;
1642
    case 10:  strncpy(strbuf, "AC-3 (dnet quater-rate", 32); break;
1643
    default: snprintf(strbuf, 32, "ERROR");
1644
    }
1645
    av_dlog(avctx, "bitstream_id: %s (%d)\n", strbuf, s->bitstream_id);
1646
    av_dlog(avctx, "sample_fmt: %s\n", av_get_sample_fmt_name(avctx->sample_fmt));
1647
    av_get_channel_layout_string(strbuf, 32, s->channels, avctx->channel_layout);
1648
    av_dlog(avctx, "channel_layout: %s\n", strbuf);
1649
    av_dlog(avctx, "sample_rate: %d\n", s->sample_rate);
1650
    av_dlog(avctx, "bit_rate: %d\n", s->bit_rate);
1651
    if (s->cutoff)
1652
        av_dlog(avctx, "cutoff: %d\n", s->cutoff);
1653

  
1654
    av_dlog(avctx, "per_frame_metadata: %s\n",
1655
            opt->allow_per_frame_metadata?"on":"off");
1656
    if (s->has_center)
1657
        av_dlog(avctx, "center_mixlev: %0.3f (%d)\n", opt->center_mix_level,
1658
                s->center_mix_level);
1659
    else
1660
        av_dlog(avctx, "center_mixlev: {not written}\n");
1661
    if (s->has_surround)
1662
        av_dlog(avctx, "surround_mixlev: %0.3f (%d)\n", opt->surround_mix_level,
1663
                s->surround_mix_level);
1664
    else
1665
        av_dlog(avctx, "surround_mixlev: {not written}\n");
1666
    if (opt->audio_production_info) {
1667
        av_dlog(avctx, "mixing_level: %ddB\n", opt->mixing_level);
1668
        switch (opt->room_type) {
1669
        case 0:  strncpy(strbuf, "notindicated", 32); break;
1670
        case 1:  strncpy(strbuf, "large", 32);        break;
1671
        case 2:  strncpy(strbuf, "small", 32);        break;
1672
        default: snprintf(strbuf, 32, "ERROR (%d)", opt->room_type);
1673
        }
1674
        av_dlog(avctx, "room_type: %s\n", strbuf);
1675
    } else {
1676
        av_dlog(avctx, "mixing_level: {not written}\n");
1677
        av_dlog(avctx, "room_type: {not written}\n");
1678
    }
1679
    av_dlog(avctx, "copyright: %s\n", opt->copyright?"on":"off");
1680
    av_dlog(avctx, "dialnorm: %ddB\n", opt->dialogue_level);
1681
    if (s->channel_mode == AC3_CHMODE_STEREO) {
1682
        switch (opt->dolby_surround_mode) {
1683
        case 0:  strncpy(strbuf, "notindicated", 32); break;
1684
        case 1:  strncpy(strbuf, "on", 32);           break;
1685
        case 2:  strncpy(strbuf, "off", 32);          break;
1686
        default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_mode);
1687
        }
1688
        av_dlog(avctx, "dsur_mode: %s\n", strbuf);
1689
    } else {
1690
        av_dlog(avctx, "dsur_mode: {not written}\n");
1691
    }
1692
    av_dlog(avctx, "original: %s\n", opt->original?"on":"off");
1693

  
1694
    if (s->bitstream_id == 6) {
1695
        if (opt->extended_bsi_1) {
1696
            switch (opt->preferred_stereo_downmix) {
1697
            case 0:  strncpy(strbuf, "notindicated", 32); break;
1698
            case 1:  strncpy(strbuf, "ltrt", 32);         break;
1699
            case 2:  strncpy(strbuf, "loro", 32);         break;
1700
            default: snprintf(strbuf, 32, "ERROR (%d)", opt->preferred_stereo_downmix);
1701
            }
1702
            av_dlog(avctx, "dmix_mode: %s\n", strbuf);
1703
            av_dlog(avctx, "ltrt_cmixlev: %0.3f (%d)\n",
1704
                    opt->ltrt_center_mix_level, s->ltrt_center_mix_level);
1705
            av_dlog(avctx, "ltrt_surmixlev: %0.3f (%d)\n",
1706
                    opt->ltrt_surround_mix_level, s->ltrt_surround_mix_level);
1707
            av_dlog(avctx, "loro_cmixlev: %0.3f (%d)\n",
1708
                    opt->loro_center_mix_level, s->loro_center_mix_level);
1709
            av_dlog(avctx, "loro_surmixlev: %0.3f (%d)\n",
1710
                    opt->loro_surround_mix_level, s->loro_surround_mix_level);
1711
        } else {
1712
            av_dlog(avctx, "extended bitstream info 1: {not written}\n");
1713
        }
1714
        if (opt->extended_bsi_2) {
1715
            switch (opt->dolby_surround_ex_mode) {
1716
            case 0:  strncpy(strbuf, "notindicated", 32); break;
1717
            case 1:  strncpy(strbuf, "on", 32);           break;
1718
            case 2:  strncpy(strbuf, "off", 32);          break;
1719
            default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_ex_mode);
1720
            }
1721
            av_dlog(avctx, "dsurex_mode: %s\n", strbuf);
1722
            switch (opt->dolby_headphone_mode) {
1723
            case 0:  strncpy(strbuf, "notindicated", 32); break;
1724
            case 1:  strncpy(strbuf, "on", 32);           break;
1725
            case 2:  strncpy(strbuf, "off", 32);          break;
1726
            default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_headphone_mode);
1727
            }
1728
            av_dlog(avctx, "dheadphone_mode: %s\n", strbuf);
1729

  
1730
            switch (opt->ad_converter_type) {
1731
            case 0:  strncpy(strbuf, "standard", 32); break;
1732
            case 1:  strncpy(strbuf, "hdcd", 32);     break;
1733
            default: snprintf(strbuf, 32, "ERROR (%d)", opt->ad_converter_type);
1734
            }
1735
            av_dlog(avctx, "ad_conv_type: %s\n", strbuf);
1736
        } else {
1737
            av_dlog(avctx, "extended bitstream info 2: {not written}\n");
1738
        }
1739
    }
1740
#endif
1741
}
1742

  
1743

  
1744
#define FLT_OPTION_THRESHOLD 0.01
1745

  
1746
static int validate_float_option(float v, const float *v_list, int v_list_size)
1747
{
1748
    int i;
1749

  
1750
    for (i = 0; i < v_list_size; i++) {
1751
        if (v < (v_list[i] + FLT_OPTION_THRESHOLD) &&
1752
            v > (v_list[i] - FLT_OPTION_THRESHOLD))
1753
            break;
1754
    }
1755
    if (i == v_list_size)
1756
        return -1;
1757

  
1758
    return i;
1759
}
1760

  
1761

  
1762
static void validate_mix_level(void *log_ctx, const char *opt_name,
1763
                               float *opt_param, const float *list,
1764
                               int list_size, int default_value, int min_value,
1765
                               int *ctx_param)
1766
{
1767
    int mixlev = validate_float_option(*opt_param, list, list_size);
1768
    if (mixlev < min_value) {
1769
        mixlev = default_value;
1770
        if (*opt_param >= 0.0) {
1771
            av_log(log_ctx, AV_LOG_WARNING, "requested %s is not valid. using "
1772
                   "default value: %0.3f\n", opt_name, list[mixlev]);
1773
        }
1774
    }
1775
    *opt_param = list[mixlev];
1776
    *ctx_param = mixlev;
1777
}
1778

  
1779

  
1780
/**
1781
 * Validate metadata options as set by AVOption system.
1782
 * These values can optionally be changed per-frame.
1783
 */
1784
static int validate_metadata(AVCodecContext *avctx)
1785
{
1786
    AC3EncodeContext *s = avctx->priv_data;
1787
    AC3EncOptions *opt = &s->options;
1788

  
1789
    /* validate mixing levels */
1790
    if (s->has_center) {
1791
        validate_mix_level(avctx, "center_mix_level", &opt->center_mix_level,
1792
                           cmixlev_options, CMIXLEV_NUM_OPTIONS, 1, 0,
1793
                           &s->center_mix_level);
1794
    }
1795
    if (s->has_surround) {
1796
        validate_mix_level(avctx, "surround_mix_level", &opt->surround_mix_level,
1797
                           surmixlev_options, SURMIXLEV_NUM_OPTIONS, 1, 0,
1798
                           &s->surround_mix_level);
1799
    }
1800

  
1801
    /* set audio production info flag */
1802
    if (opt->mixing_level >= 0 || opt->room_type >= 0) {
1803
        if (opt->mixing_level < 0) {
1804
            av_log(avctx, AV_LOG_ERROR, "mixing_level must be set if "
1805
                   "room_type is set\n");
1806
            return AVERROR(EINVAL);
1807
        }
1808
        if (opt->mixing_level < 80) {
1809
            av_log(avctx, AV_LOG_ERROR, "invalid mixing level. must be between "
1810
                   "80dB and 111dB\n");
1811
            return AVERROR(EINVAL);
1812
        }
1813
        /* default room type */
1814
        if (opt->room_type < 0)
1815
            opt->room_type = 0;
1816
        opt->audio_production_info = 1;
1817
    } else {
1818
        opt->audio_production_info = 0;
1819
    }
1820

  
1821
    /* set extended bsi 1 flag */
1822
    if ((s->has_center || s->has_surround) &&
1823
        (opt->preferred_stereo_downmix >= 0 ||
1824
         opt->ltrt_center_mix_level   >= 0 ||
1825
         opt->ltrt_surround_mix_level >= 0 ||
1826
         opt->loro_center_mix_level   >= 0 ||
1827
         opt->loro_surround_mix_level >= 0)) {
1828
        /* default preferred stereo downmix */
1829
        if (opt->preferred_stereo_downmix < 0)
1830
            opt->preferred_stereo_downmix = 0;
1831
        /* validate Lt/Rt center mix level */
1832
        validate_mix_level(avctx, "ltrt_center_mix_level",
1833
                           &opt->ltrt_center_mix_level, extmixlev_options,
1834
                           EXTMIXLEV_NUM_OPTIONS, 5, 0,
1835
                           &s->ltrt_center_mix_level);
1836
        /* validate Lt/Rt surround mix level */
1837
        validate_mix_level(avctx, "ltrt_surround_mix_level",
1838
                           &opt->ltrt_surround_mix_level, extmixlev_options,
1839
                           EXTMIXLEV_NUM_OPTIONS, 6, 3,
1840
                           &s->ltrt_surround_mix_level);
1841
        /* validate Lo/Ro center mix level */
1842
        validate_mix_level(avctx, "loro_center_mix_level",
1843
                           &opt->loro_center_mix_level, extmixlev_options,
1844
                           EXTMIXLEV_NUM_OPTIONS, 5, 0,
1845
                           &s->loro_center_mix_level);
1846
        /* validate Lo/Ro surround mix level */
1847
        validate_mix_level(avctx, "loro_surround_mix_level",
1848
                           &opt->loro_surround_mix_level, extmixlev_options,
1849
                           EXTMIXLEV_NUM_OPTIONS, 6, 3,
1850
                           &s->loro_surround_mix_level);
1851
        opt->extended_bsi_1 = 1;
1852
    } else {
1853
        opt->extended_bsi_1 = 0;
1854
    }
1855

  
1856
    /* set extended bsi 2 flag */
1857
    if (opt->dolby_surround_ex_mode >= 0 ||
1858
        opt->dolby_headphone_mode   >= 0 ||
1859
        opt->ad_converter_type      >= 0) {
1860
        /* default dolby surround ex mode */
1861
        if (opt->dolby_surround_ex_mode < 0)
1862
            opt->dolby_surround_ex_mode = 0;
1863
        /* default dolby headphone mode */
1864
        if (opt->dolby_headphone_mode < 0)
1865
            opt->dolby_headphone_mode = 0;
1866
        /* default A/D converter type */
1867
        if (opt->ad_converter_type < 0)
1868
            opt->ad_converter_type = 0;
1869
        opt->extended_bsi_2 = 1;
1870
    } else {
1871
        opt->extended_bsi_2 = 0;
1872
    }
1873

  
1874
    /* set bitstream id for alternate bitstream syntax */
1875
    if (opt->extended_bsi_1 || opt->extended_bsi_2) {
1876
        if (s->bitstream_id > 8 && s->bitstream_id < 11) {
1877
            static int warn_once = 1;
1878
            if (warn_once) {
1879
                av_log(avctx, AV_LOG_WARNING, "alternate bitstream syntax is "
1880
                       "not compatible with reduced samplerates. writing of "
1881
                       "extended bitstream information will be disabled.\n");
1882
                warn_once = 0;
1883
            }
1884
        } else {
1885
            s->bitstream_id = 6;
1886
        }
1887
    }
1888

  
1889
    return 0;
1890
}
1891

  
1892

  
1482 1893
/**
1483 1894
 * Encode a single AC-3 frame.
1484 1895
 */
......
1489 1900
    const SampleType *samples = data;
1490 1901
    int ret;
1491 1902

  
1903
    if (s->options.allow_per_frame_metadata) {
1904
        ret = validate_metadata(avctx);
1905
        if (ret)
1906
            return ret;
1907
    }
1908

  
1492 1909
    if (s->bit_alloc.sr_code == 1)
1493 1910
        adjust_frame_size(s);
1494 1911

  
......
1597 2014
    default:
1598 2015
        return AVERROR(EINVAL);
1599 2016
    }
2017
    s->has_center   = (s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO;
2018
    s->has_surround =  s->channel_mode & 0x04;
1600 2019

  
1601 2020
    s->channel_map  = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
1602 2021
    *channel_layout = ch_layout;
......
1635 2054
    s->sample_rate        = avctx->sample_rate;
1636 2055
    s->bit_alloc.sr_shift = i % 3;
1637 2056
    s->bit_alloc.sr_code  = i / 3;
2057
    s->bitstream_id       = 8 + s->bit_alloc.sr_shift;
1638 2058

  
1639 2059
    /* validate bit rate */
1640 2060
    for (i = 0; i < 19; i++) {
......
1669 2089
        return AVERROR(EINVAL);
1670 2090
    }
1671 2091

  
2092
    ret = validate_metadata(avctx);
2093
    if (ret)
2094
        return ret;
2095

  
1672 2096
    return 0;
1673 2097
}
1674 2098

  
......
1810 2234
    if (ret)
1811 2235
        return ret;
1812 2236

  
1813
    s->bitstream_id   = 8 + s->bit_alloc.sr_shift;
1814 2237
    s->bitstream_mode = avctx->audio_service_type;
1815 2238
    if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)
1816 2239
        s->bitstream_mode = 0x7;
......
1849 2272
    dsputil_init(&s->dsp, avctx);
1850 2273
    ff_ac3dsp_init(&s->ac3dsp, avctx->flags & CODEC_FLAG_BITEXACT);
1851 2274

  
2275
    dprint_options(avctx);
2276

  
1852 2277
    return 0;
1853 2278
init_fail:
1854 2279
    ac3_encode_close(avctx);
libavcodec/ac3enc_fixed.c
410 410
    NULL,
411 411
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
412 412
    .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
413
    .priv_class = &ac3enc_class,
413 414
    .channel_layouts = ac3_channel_layouts,
414 415
};
libavcodec/ac3enc_float.c
120 120
    NULL,
121 121
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
122 122
    .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
123
    .priv_class = &ac3enc_class,
123 124
    .channel_layouts = ac3_channel_layouts,
124 125
};

Also available in: Unified diff