From patchwork Sun Apr 7 21:09:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 47899 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:24a8:b0:1a3:b6bb:3029 with SMTP id m40csp2147075pzd; Sun, 7 Apr 2024 14:10:24 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUieCZrZofnRYkfoyl8NiLNzozVwxi6+ioeqdVbEdgiiTKeDqJ2OlokJlr87ddU+mNrJxEFrL/ZznO+TRIWyLZq1JCZzsJKNxtkWg== X-Google-Smtp-Source: AGHT+IG3LgYTFkj6f27h3yn96boBtM2JVT8buCzZzQ+lD3bhZlgHE+D2KACS7vsb/aJofuxVwQd1 X-Received: by 2002:a50:bb65:0:b0:56e:34c5:c482 with SMTP id y92-20020a50bb65000000b0056e34c5c482mr4261332ede.27.1712524224120; Sun, 07 Apr 2024 14:10:24 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s26-20020a508d1a000000b0056e2374a397si2919421eds.108.2024.04.07.14.10.23; Sun, 07 Apr 2024 14:10:24 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=XlMEI2+s; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0F4D968D24D; Mon, 8 Apr 2024 00:09:53 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-db5eur01olkn2083.outbound.protection.outlook.com [40.92.64.83]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C205668D1D1 for ; Mon, 8 Apr 2024 00:09:49 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cRlVTDKO+Cn94fUObt5Q6s1WPEpjCoKL4QMYfzbiFP4bT4Z4f2FhQnDvPjDHJZlvAvKHN4+WLTwgWqLyb0DQTGzzIzbl9cNh/Nwk4Dz/IBiCmiJgcPPJWey5Ms4+wb3j8DPc+xHUozL1bOYkSoiBANsvwnKQUvOkOxJY9mu8wGsO0YYnMFECfua2j76ofK8AeulF8yFLuIkN1QmNwRHXYqjq0JgBrXLr2QsBimWusm+8GoK42TAW2U7awtfemDSbta3shBitWwwmwzezhtNoBekpzBx5aVwfrOXwn0FY6rGTTy/dRjway1i4zNQTcvPrMhppis0NCeBqPP+nTEyoHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4ut52leKLptUC+yDsmX46D8UWWkIMxiz6w/CLmClh4M=; b=FXLX5OOvY4MYhL7kCKvQCh5K4m5N61HA0fjhUk6GlgY74HScg8BZSL26JQpx+UzefcRZqczDeqMNeKLulx5lOONly39U278cNAFDbqvwThWKRwzVBne8eY07BbcaX2ePXoQ+mvekR2VKqKrS93MO4RWdN8GlHTCjl7hiojDYAjP+/Pbn1KhfWpYsa0PZ0SArrjF+HDu1eGSiXwVsP6MdUZI7dJtM72MggU3/9JgH5mIawT4f8TKyvghYfbJnoiqciwQ/TR9MZAFAaOJZ37h4EbGKt41qzCoPXJRydwd7s2fEFOXs6MtohM3BPC35lDt2l590vBdfCu265RBNo9Ap+Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4ut52leKLptUC+yDsmX46D8UWWkIMxiz6w/CLmClh4M=; b=XlMEI2+sh+hj8RsDGLXXbVhPG+z0x1uDpaTwur7yla7iafvlkJ+yUUasLIAfGZVPPBAIh/uzMh5TPPHt7BpPw0SFG9WJmxdIi7PNv7lR2jlVVaHTk22Mc5HqK3jq871bXfnPMbY3culXCZASQAa/0MaKTIhWxmUrQMuZWL+Lg+IAfnoJwQcgkC04gDhhmBkwfTvwBugNtjCvJFDSHiB6dCerFive2MzOpQzO/AuDVBWd6NuZ+NC3AEv1Hxh+gimB9gfTBNI9ffT8bTK33DX61lKTMfuQAF12ZgKDRuP+Y0DVIWs+b057rkvn/tf0X/yCzLoHJ/DP90QAMWliiasJWA== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by AS8P250MB0234.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:379::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.55; Sun, 7 Apr 2024 21:09:39 +0000 Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::4a3b:cf76:cddc:c68d]) by GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::4a3b:cf76:cddc:c68d%4]) with mapi id 15.20.7409.042; Sun, 7 Apr 2024 21:09:39 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sun, 7 Apr 2024 23:09:07 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [8cusl8JHIf3t8PBIxPN0Sz1Er6/HTovRSj8FKqku0EA=] X-ClientProxiedBy: ZR0P278CA0038.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:1d::7) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20240407210915.895619-8-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|AS8P250MB0234:EE_ X-MS-Office365-Filtering-Correlation-Id: 625f302d-e2cd-4530-874b-08dc5747092f X-MS-Exchange-SLBlob-MailProps: 9IecXKUgicDNEbE0encsoirT0ZUrgC9mflO0OxM9h9gG4aliL1uhn8QbDTKEusfEDDOIFLqXQdMbbhCWmnm5WsgBtW1VLpRZik+1ddxJj4m/BMkaOAm+SzFlsSxlxsMcRVRzn9HiFRus/w/xWcySt7DQ7lQ2Eb/7XZr8MFMvwaMSjFMq9N5G3u87pe6o5M3C0CmRkyfukl8qGgBV0/RWuntrBhSeMK68PGDfS2ynoKthzMnyv2Wajb+AqUZtHOaQZ6W6RzARu+P/NQ8FtZx1IHA3FP09RnWlNdVeJOwrJwE7TPB7+nIshRjkM3gRhMRMsbTA+qc0PrBNGI70elN7aw+0Zm4NPTFa3OA8qo8b5paX6gNOvI4ob/VNOe8ImfMipseEaTidbmZbv0Oa3tT1xqFtBZfy6C1YlJAeFZDylSIZtBLoNRnY1ofIZAGvygb+OWODEwSp4/7ewqry8VYkEMbNr5cplehey0l5mUTaLLKfVLUbxRDERZwmgpXu2HNxtuy8cCqz5K9DHJIhiILU6dnPYKBxxN5AH+ZP7yuGw19nd1XD8fzLiI+P4p3aO6v7atMQC5Muilt5NqfqfAqZVHPS6zuBZ+nagKugaoC3aXPOukNuCMUM7y/wTRt4PK8SSEzSJavA/35/p7XbSRxmIzt6T/6u3/wi6OxXsz0NMO5KFpic5J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874b-08dc5747092f X-MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Apr 2024 21:09:38.9967 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8P250MB0234 Subject: [FFmpeg-devel] [PATCH 09/17] avcodec/ac3enc: Move ff_ac3_validate_metadate() upwards X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: NJpLb8WL9COt Will avoid a forward declaration in the next commit. Signed-off-by: Andreas Rheinhardt --- libavcodec/ac3enc.c | 420 ++++++++++++++++++++++---------------------- 1 file changed, 209 insertions(+), 211 deletions(-) diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index c19837e88f..1f05436720 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -273,6 +273,215 @@ static const int8_t ac3_coupling_start_tab[6][3][19] = { }; +#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 AVERROR(EINVAL); + + 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. + * + * @param s AC-3 encoder private context + */ +int ff_ac3_validate_metadata(AC3EncodeContext *s) +{ + AVCodecContext *avctx = s->avctx; + AC3EncOptions *opt = &s->options; + + opt->audio_production_info = 0; + opt->extended_bsi_1 = 0; + opt->extended_bsi_2 = 0; + opt->eac3_mixing_metadata = 0; + opt->eac3_info_metadata = 0; + + /* determine mixing metadata / xbsi1 use */ + if (s->channel_mode > AC3_CHMODE_STEREO && opt->preferred_stereo_downmix != AC3ENC_OPT_NONE) { + opt->extended_bsi_1 = 1; + opt->eac3_mixing_metadata = 1; + } + if (s->has_center && + (opt->ltrt_center_mix_level >= 0 || opt->loro_center_mix_level >= 0)) { + opt->extended_bsi_1 = 1; + opt->eac3_mixing_metadata = 1; + } + if (s->has_surround && + (opt->ltrt_surround_mix_level >= 0 || opt->loro_surround_mix_level >= 0)) { + opt->extended_bsi_1 = 1; + opt->eac3_mixing_metadata = 1; + } + + if (s->eac3) { + /* determine info metadata use */ + if (avctx->audio_service_type != AV_AUDIO_SERVICE_TYPE_MAIN) + opt->eac3_info_metadata = 1; + if (opt->copyright != AC3ENC_OPT_NONE || opt->original != AC3ENC_OPT_NONE) + opt->eac3_info_metadata = 1; + if (s->channel_mode == AC3_CHMODE_STEREO && + (opt->dolby_headphone_mode != AC3ENC_OPT_NONE || opt->dolby_surround_mode != AC3ENC_OPT_NONE)) + opt->eac3_info_metadata = 1; + if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE) + opt->eac3_info_metadata = 1; + if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE || + opt->ad_converter_type != AC3ENC_OPT_NONE) { + opt->audio_production_info = 1; + opt->eac3_info_metadata = 1; + } + } else { + /* determine audio production info use */ + if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE) + opt->audio_production_info = 1; + + /* determine xbsi2 use */ + if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE) + opt->extended_bsi_2 = 1; + if (s->channel_mode == AC3_CHMODE_STEREO && opt->dolby_headphone_mode != AC3ENC_OPT_NONE) + opt->extended_bsi_2 = 1; + if (opt->ad_converter_type != AC3ENC_OPT_NONE) + opt->extended_bsi_2 = 1; + } + + /* validate AC-3 mixing levels */ + if (!s->eac3) { + 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); + } + } + + /* validate extended bsi 1 / mixing metadata */ + if (opt->extended_bsi_1 || opt->eac3_mixing_metadata) { + /* default preferred stereo downmix */ + if (opt->preferred_stereo_downmix == AC3ENC_OPT_NONE) + opt->preferred_stereo_downmix = AC3ENC_OPT_NOT_INDICATED; + if (!s->eac3 || s->has_center) { + /* 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 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); + } + if (!s->eac3 || s->has_surround) { + /* 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 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); + } + } + + /* validate audio service type / channels combination */ + if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE && + avctx->ch_layout.nb_channels == 1) || + ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY || + avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY || + avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER) + && avctx->ch_layout.nb_channels > 1)) { + av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the " + "specified number of channels\n"); + return AVERROR(EINVAL); + } + + /* validate extended bsi 2 / info metadata */ + if (opt->extended_bsi_2 || opt->eac3_info_metadata) { + /* default dolby headphone mode */ + if (opt->dolby_headphone_mode == AC3ENC_OPT_NONE) + opt->dolby_headphone_mode = AC3ENC_OPT_NOT_INDICATED; + /* default dolby surround ex mode */ + if (opt->dolby_surround_ex_mode == AC3ENC_OPT_NONE) + opt->dolby_surround_ex_mode = AC3ENC_OPT_NOT_INDICATED; + /* default A/D converter type */ + if (opt->ad_converter_type == AC3ENC_OPT_NONE) + opt->ad_converter_type = AC3ENC_OPT_ADCONV_STANDARD; + } + + /* copyright & original defaults */ + if (!s->eac3 || opt->eac3_info_metadata) { + /* default copyright */ + if (opt->copyright == AC3ENC_OPT_NONE) + opt->copyright = AC3ENC_OPT_OFF; + /* default original */ + if (opt->original == AC3ENC_OPT_NONE) + opt->original = AC3ENC_OPT_ON; + } + + /* dolby surround mode default */ + if (!s->eac3 || opt->eac3_info_metadata) { + if (opt->dolby_surround_mode == AC3ENC_OPT_NONE) + opt->dolby_surround_mode = AC3ENC_OPT_NOT_INDICATED; + } + + /* validate audio production info */ + if (opt->audio_production_info) { + if (opt->mixing_level == AC3ENC_OPT_NONE) { + 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 == AC3ENC_OPT_NONE) + opt->room_type = AC3ENC_OPT_NOT_INDICATED; + } + + /* set bitstream id for alternate bitstream syntax */ + if (!s->eac3 && (opt->extended_bsi_1 || opt->extended_bsi_2)) + s->bitstream_id = 6; + + return 0; +} + /** * Adjust the frame size to make the average bit rate match the target bit rate. * This is only needed for 11025, 22050, and 44100 sample rates or any E-AC-3. @@ -1930,217 +2139,6 @@ static void dprint_options(AC3EncodeContext *s) #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 AVERROR(EINVAL); - - 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. - * - * @param s AC-3 encoder private context - */ -int ff_ac3_validate_metadata(AC3EncodeContext *s) -{ - AVCodecContext *avctx = s->avctx; - AC3EncOptions *opt = &s->options; - - opt->audio_production_info = 0; - opt->extended_bsi_1 = 0; - opt->extended_bsi_2 = 0; - opt->eac3_mixing_metadata = 0; - opt->eac3_info_metadata = 0; - - /* determine mixing metadata / xbsi1 use */ - if (s->channel_mode > AC3_CHMODE_STEREO && opt->preferred_stereo_downmix != AC3ENC_OPT_NONE) { - opt->extended_bsi_1 = 1; - opt->eac3_mixing_metadata = 1; - } - if (s->has_center && - (opt->ltrt_center_mix_level >= 0 || opt->loro_center_mix_level >= 0)) { - opt->extended_bsi_1 = 1; - opt->eac3_mixing_metadata = 1; - } - if (s->has_surround && - (opt->ltrt_surround_mix_level >= 0 || opt->loro_surround_mix_level >= 0)) { - opt->extended_bsi_1 = 1; - opt->eac3_mixing_metadata = 1; - } - - if (s->eac3) { - /* determine info metadata use */ - if (avctx->audio_service_type != AV_AUDIO_SERVICE_TYPE_MAIN) - opt->eac3_info_metadata = 1; - if (opt->copyright != AC3ENC_OPT_NONE || opt->original != AC3ENC_OPT_NONE) - opt->eac3_info_metadata = 1; - if (s->channel_mode == AC3_CHMODE_STEREO && - (opt->dolby_headphone_mode != AC3ENC_OPT_NONE || opt->dolby_surround_mode != AC3ENC_OPT_NONE)) - opt->eac3_info_metadata = 1; - if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE) - opt->eac3_info_metadata = 1; - if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE || - opt->ad_converter_type != AC3ENC_OPT_NONE) { - opt->audio_production_info = 1; - opt->eac3_info_metadata = 1; - } - } else { - /* determine audio production info use */ - if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE) - opt->audio_production_info = 1; - - /* determine xbsi2 use */ - if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE) - opt->extended_bsi_2 = 1; - if (s->channel_mode == AC3_CHMODE_STEREO && opt->dolby_headphone_mode != AC3ENC_OPT_NONE) - opt->extended_bsi_2 = 1; - if (opt->ad_converter_type != AC3ENC_OPT_NONE) - opt->extended_bsi_2 = 1; - } - - /* validate AC-3 mixing levels */ - if (!s->eac3) { - 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); - } - } - - /* validate extended bsi 1 / mixing metadata */ - if (opt->extended_bsi_1 || opt->eac3_mixing_metadata) { - /* default preferred stereo downmix */ - if (opt->preferred_stereo_downmix == AC3ENC_OPT_NONE) - opt->preferred_stereo_downmix = AC3ENC_OPT_NOT_INDICATED; - if (!s->eac3 || s->has_center) { - /* 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 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); - } - if (!s->eac3 || s->has_surround) { - /* 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 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); - } - } - - /* validate audio service type / channels combination */ - if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE && - avctx->ch_layout.nb_channels == 1) || - ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY || - avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY || - avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER) - && avctx->ch_layout.nb_channels > 1)) { - av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the " - "specified number of channels\n"); - return AVERROR(EINVAL); - } - - /* validate extended bsi 2 / info metadata */ - if (opt->extended_bsi_2 || opt->eac3_info_metadata) { - /* default dolby headphone mode */ - if (opt->dolby_headphone_mode == AC3ENC_OPT_NONE) - opt->dolby_headphone_mode = AC3ENC_OPT_NOT_INDICATED; - /* default dolby surround ex mode */ - if (opt->dolby_surround_ex_mode == AC3ENC_OPT_NONE) - opt->dolby_surround_ex_mode = AC3ENC_OPT_NOT_INDICATED; - /* default A/D converter type */ - if (opt->ad_converter_type == AC3ENC_OPT_NONE) - opt->ad_converter_type = AC3ENC_OPT_ADCONV_STANDARD; - } - - /* copyright & original defaults */ - if (!s->eac3 || opt->eac3_info_metadata) { - /* default copyright */ - if (opt->copyright == AC3ENC_OPT_NONE) - opt->copyright = AC3ENC_OPT_OFF; - /* default original */ - if (opt->original == AC3ENC_OPT_NONE) - opt->original = AC3ENC_OPT_ON; - } - - /* dolby surround mode default */ - if (!s->eac3 || opt->eac3_info_metadata) { - if (opt->dolby_surround_mode == AC3ENC_OPT_NONE) - opt->dolby_surround_mode = AC3ENC_OPT_NOT_INDICATED; - } - - /* validate audio production info */ - if (opt->audio_production_info) { - if (opt->mixing_level == AC3ENC_OPT_NONE) { - 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 == AC3ENC_OPT_NONE) - opt->room_type = AC3ENC_OPT_NOT_INDICATED; - } - - /* set bitstream id for alternate bitstream syntax */ - if (!s->eac3 && (opt->extended_bsi_1 || opt->extended_bsi_2)) - s->bitstream_id = 6; - - return 0; -} - - /** * Finalize encoding and free any memory allocated by the encoder. *