Revision 991f3de1 libavcodec/ac3enc.c

View differences:

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);

Also available in: Unified diff