+static void dprint_options(AVCodecContext *avctx)
+{
+#ifdef DEBUG
+ AC3EncodeContext *s = avctx->priv_data;
+ AC3EncOptions *opt = &s->options;
+ char strbuf[32];
+
+ switch (s->bitstream_id) {
+ case 6: strncpy(strbuf, "AC-3 (alt syntax)", 32); break;
+ case 8: strncpy(strbuf, "AC-3 (standard)", 32); break;
+ case 9: strncpy(strbuf, "AC-3 (dnet half-rate)", 32); break;
+ case 10: strncpy(strbuf, "AC-3 (dnet quater-rate", 32); break;
+ default: snprintf(strbuf, 32, "ERROR");
+ }
+ av_dlog(avctx, "bitstream_id: %s (%d)\n", strbuf, s->bitstream_id);
+ av_dlog(avctx, "sample_fmt: %s\n", av_get_sample_fmt_name(avctx->sample_fmt));
+ av_get_channel_layout_string(strbuf, 32, s->channels, avctx->channel_layout);
+ av_dlog(avctx, "channel_layout: %s\n", strbuf);
+ av_dlog(avctx, "sample_rate: %d\n", s->sample_rate);
+ av_dlog(avctx, "bit_rate: %d\n", s->bit_rate);
+ if (s->cutoff)
+ av_dlog(avctx, "cutoff: %d\n", s->cutoff);
+
+ av_dlog(avctx, "per_frame_metadata: %s\n",
+ opt->allow_per_frame_metadata?"on":"off");
+ if (s->has_center)
+ av_dlog(avctx, "center_mixlev: %0.3f (%d)\n", opt->center_mix_level,
+ s->center_mix_level);
+ else
+ av_dlog(avctx, "center_mixlev: {not written}\n");
+ if (s->has_surround)
+ av_dlog(avctx, "surround_mixlev: %0.3f (%d)\n", opt->surround_mix_level,
+ s->surround_mix_level);
+ else
+ av_dlog(avctx, "surround_mixlev: {not written}\n");
+ if (opt->audio_production_info) {
+ av_dlog(avctx, "mixing_level: %ddB\n", opt->mixing_level);
+ switch (opt->room_type) {
+ case 0: strncpy(strbuf, "notindicated", 32); break;
+ case 1: strncpy(strbuf, "large", 32); break;
+ case 2: strncpy(strbuf, "small", 32); break;
+ default: snprintf(strbuf, 32, "ERROR (%d)", opt->room_type);
+ }
+ av_dlog(avctx, "room_type: %s\n", strbuf);
+ } else {
+ av_dlog(avctx, "mixing_level: {not written}\n");
+ av_dlog(avctx, "room_type: {not written}\n");
+ }
+ av_dlog(avctx, "copyright: %s\n", opt->copyright?"on":"off");
+ av_dlog(avctx, "dialnorm: %ddB\n", opt->dialogue_level);
+ if (s->channel_mode == AC3_CHMODE_STEREO) {
+ switch (opt->dolby_surround_mode) {
+ case 0: strncpy(strbuf, "notindicated", 32); break;
+ case 1: strncpy(strbuf, "on", 32); break;
+ case 2: strncpy(strbuf, "off", 32); break;
+ default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_mode);
+ }
+ av_dlog(avctx, "dsur_mode: %s\n", strbuf);
+ } else {
+ av_dlog(avctx, "dsur_mode: {not written}\n");
+ }
+ av_dlog(avctx, "original: %s\n", opt->original?"on":"off");
+
+ if (s->bitstream_id == 6) {
+ if (opt->extended_bsi_1) {
+ switch (opt->preferred_stereo_downmix) {
+ case 0: strncpy(strbuf, "notindicated", 32); break;
+ case 1: strncpy(strbuf, "ltrt", 32); break;
+ case 2: strncpy(strbuf, "loro", 32); break;
+ default: snprintf(strbuf, 32, "ERROR (%d)", opt->preferred_stereo_downmix);
+ }
+ av_dlog(avctx, "dmix_mode: %s\n", strbuf);
+ av_dlog(avctx, "ltrt_cmixlev: %0.3f (%d)\n",
+ opt->ltrt_center_mix_level, s->ltrt_center_mix_level);
+ av_dlog(avctx, "ltrt_surmixlev: %0.3f (%d)\n",
+ opt->ltrt_surround_mix_level, s->ltrt_surround_mix_level);
+ av_dlog(avctx, "loro_cmixlev: %0.3f (%d)\n",
+ opt->loro_center_mix_level, s->loro_center_mix_level);
+ av_dlog(avctx, "loro_surmixlev: %0.3f (%d)\n",
+ opt->loro_surround_mix_level, s->loro_surround_mix_level);
+ } else {
+ av_dlog(avctx, "extended bitstream info 1: {not written}\n");
+ }
+ if (opt->extended_bsi_2) {
+ switch (opt->dolby_surround_ex_mode) {
+ case 0: strncpy(strbuf, "notindicated", 32); break;
+ case 1: strncpy(strbuf, "on", 32); break;
+ case 2: strncpy(strbuf, "off", 32); break;
+ default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_ex_mode);
+ }
+ av_dlog(avctx, "dsurex_mode: %s\n", strbuf);
+ switch (opt->dolby_headphone_mode) {
+ case 0: strncpy(strbuf, "notindicated", 32); break;
+ case 1: strncpy(strbuf, "on", 32); break;
+ case 2: strncpy(strbuf, "off", 32); break;
+ default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_headphone_mode);
+ }
+ av_dlog(avctx, "dheadphone_mode: %s\n", strbuf);
+
+ switch (opt->ad_converter_type) {
+ case 0: strncpy(strbuf, "standard", 32); break;
+ case 1: strncpy(strbuf, "hdcd", 32); break;
+ default: snprintf(strbuf, 32, "ERROR (%d)", opt->ad_converter_type);
+ }
+ av_dlog(avctx, "ad_conv_type: %s\n", strbuf);
+ } else {
+ av_dlog(avctx, "extended bitstream info 2: {not written}\n");
+ }
+ }
+#endif
+}
+
+
+#define FLT_OPTION_THRESHOLD 0.01
+
+static int validate_float_option(float v, const float *v_list, int v_list_size)
+{
+ int i;
+
+ for (i = 0; i < v_list_size; i++) {
+ if (v < (v_list[i] + FLT_OPTION_THRESHOLD) &&
+ v > (v_list[i] - FLT_OPTION_THRESHOLD))
+ break;
+ }
+ if (i == v_list_size)
+ return -1;
+
+ return i;
+}
+
+
+static void validate_mix_level(void *log_ctx, const char *opt_name,
+ float *opt_param, const float *list,
+ int list_size, int default_value, int min_value,
+ int *ctx_param)
+{
+ int mixlev = validate_float_option(*opt_param, list, list_size);
+ if (mixlev < min_value) {
+ mixlev = default_value;
+ if (*opt_param >= 0.0) {
+ av_log(log_ctx, AV_LOG_WARNING, "requested %s is not valid. using "
+ "default value: %0.3f\n", opt_name, list[mixlev]);
+ }
+ }
+ *opt_param = list[mixlev];
+ *ctx_param = mixlev;
+}
+
+
+/**
+ * Validate metadata options as set by AVOption system.
+ * These values can optionally be changed per-frame.
+ */
+static int validate_metadata(AVCodecContext *avctx)
+{
+ AC3EncodeContext *s = avctx->priv_data;
+ AC3EncOptions *opt = &s->options;
+
+ /* validate mixing levels */
+ if (s->has_center) {
+ validate_mix_level(avctx, "center_mix_level", &opt->center_mix_level,
+ cmixlev_options, CMIXLEV_NUM_OPTIONS, 1, 0,
+ &s->center_mix_level);
+ }
+ if (s->has_surround) {
+ validate_mix_level(avctx, "surround_mix_level", &opt->surround_mix_level,
+ surmixlev_options, SURMIXLEV_NUM_OPTIONS, 1, 0,
+ &s->surround_mix_level);
+ }
+
+ /* set audio production info flag */
+ if (opt->mixing_level >= 0 || opt->room_type >= 0) {
+ if (opt->mixing_level < 0) {
+ av_log(avctx, AV_LOG_ERROR, "mixing_level must be set if "
+ "room_type is set\n");
+ return AVERROR(EINVAL);
+ }
+ if (opt->mixing_level < 80) {
+ av_log(avctx, AV_LOG_ERROR, "invalid mixing level. must be between "
+ "80dB and 111dB\n");
+ return AVERROR(EINVAL);
+ }
+ /* default room type */
+ if (opt->room_type < 0)
+ opt->room_type = 0;
+ opt->audio_production_info = 1;
+ } else {
+ opt->audio_production_info = 0;
+ }
+
+ /* set extended bsi 1 flag */
+ if ((s->has_center || s->has_surround) &&
+ (opt->preferred_stereo_downmix >= 0 ||
+ opt->ltrt_center_mix_level >= 0 ||
+ opt->ltrt_surround_mix_level >= 0 ||
+ opt->loro_center_mix_level >= 0 ||
+ opt->loro_surround_mix_level >= 0)) {
+ /* default preferred stereo downmix */
+ if (opt->preferred_stereo_downmix < 0)
+ opt->preferred_stereo_downmix = 0;
+ /* validate Lt/Rt center mix level */
+ validate_mix_level(avctx, "ltrt_center_mix_level",
+ &opt->ltrt_center_mix_level, extmixlev_options,
+ EXTMIXLEV_NUM_OPTIONS, 5, 0,
+ &s->ltrt_center_mix_level);
+ /* validate Lt/Rt surround mix level */
+ validate_mix_level(avctx, "ltrt_surround_mix_level",
+ &opt->ltrt_surround_mix_level, extmixlev_options,
+ EXTMIXLEV_NUM_OPTIONS, 6, 3,
+ &s->ltrt_surround_mix_level);
+ /* validate Lo/Ro center mix level */
+ validate_mix_level(avctx, "loro_center_mix_level",
+ &opt->loro_center_mix_level, extmixlev_options,
+ EXTMIXLEV_NUM_OPTIONS, 5, 0,
+ &s->loro_center_mix_level);
+ /* validate Lo/Ro surround mix level */
+ validate_mix_level(avctx, "loro_surround_mix_level",
+ &opt->loro_surround_mix_level, extmixlev_options,
+ EXTMIXLEV_NUM_OPTIONS, 6, 3,
+ &s->loro_surround_mix_level);
+ opt->extended_bsi_1 = 1;
+ } else {
+ opt->extended_bsi_1 = 0;
+ }
+
+ /* set extended bsi 2 flag */
+ if (opt->dolby_surround_ex_mode >= 0 ||
+ opt->dolby_headphone_mode >= 0 ||
+ opt->ad_converter_type >= 0) {
+ /* default dolby surround ex mode */
+ if (opt->dolby_surround_ex_mode < 0)
+ opt->dolby_surround_ex_mode = 0;
+ /* default dolby headphone mode */
+ if (opt->dolby_headphone_mode < 0)
+ opt->dolby_headphone_mode = 0;
+ /* default A/D converter type */
+ if (opt->ad_converter_type < 0)
+ opt->ad_converter_type = 0;
+ opt->extended_bsi_2 = 1;
+ } else {
+ opt->extended_bsi_2 = 0;
+ }
+
+ /* set bitstream id for alternate bitstream syntax */
+ if (opt->extended_bsi_1 || opt->extended_bsi_2) {
+ if (s->bitstream_id > 8 && s->bitstream_id < 11) {
+ static int warn_once = 1;
+ if (warn_once) {
+ av_log(avctx, AV_LOG_WARNING, "alternate bitstream syntax is "
+ "not compatible with reduced samplerates. writing of "
+ "extended bitstream information will be disabled.\n");
+ warn_once = 0;
+ }
+ } else {
+ s->bitstream_id = 6;
+ }
+ }
+
+ return 0;
+}
+
+