From patchwork Wed Sep 22 07:42:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xiang, Haihao" X-Patchwork-Id: 30453 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp263188iob; Wed, 22 Sep 2021 00:43:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbmA8CfD9HNNY9vk9PPWjElDhZpbybIltm1dJ1VfDKVw2iktwD5gyIgI8hmjh6OKQMSh9i X-Received: by 2002:a17:906:9401:: with SMTP id q1mr40168557ejx.313.1632296585931; Wed, 22 Sep 2021 00:43:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632296585; cv=none; d=google.com; s=arc-20160816; b=ta19JGfJeO1emEsyUKOqzuJSynTXycQlrL42LjD0Cs+ShSzVZfp37moHlsq/rgi1zK 2/0UN7liO4ZqPJo10mVIvTe0JjvXJwiak4co0sfIh5tjZBehJpvRUdRA3IJ1UtZXhJ/1 GN1+bm4H9V9LLs6+yOT0grE6ZVxba0pvPgA+IDKRNoNZThQVOwY9l3cMxX+CBoyzorau VjA5EUIhKuzGhWkIW21GyjWfKOzL/NLeiFvgRQmCY2klBpR2gnTPx93lOoo6YsL8c2TL W7JNn4NEn4QQY+zWGPcXuUBAMfmV6vK06zufm7/QOS9Roi8MFB7+IGi6m3MyVGi7M5Cg 5buA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:delivered-to; bh=Wqk77LW+6dOwiyRIYxjJa3jLGCwZ25PQ9Y3/FgDcwhk=; b=mzu/Lz9hFNuez0P6SrSpYmat3cS3HF2JyjU6uyhddW+RLjToHydLItSrYn/UFQrLP1 2x+/yWQotmEr5bgjUquw6MV3kGHhIAgCpajY6lmqabkoygT8QWFWLkz8CsGRRMNYSw7c djTx3Ao9STERb+nf9WUuEC76H5UMekOPS13xoZ16ZYcHfrlh0nMLTBt3G05gJvFZ94my 3gJcg0NnpRqk9NtmCsUhh/gvAFSisA5i79D8S8v43MJF7ilAt373LvzFkmBmpzZ7kP2J HUYySbbpxpyEh4dyCBzYZgR8tN7WuNGCgfcQrZqUDXs4/7nJQp/YdY/sOUmso+Gb7ASP uFrg== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id p2si1638365ejj.156.2021.09.22.00.42.42; Wed, 22 Sep 2021 00:43:05 -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; 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=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E4CDE68AEE0; Wed, 22 Sep 2021 10:42:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D157D68AE78 for ; Wed, 22 Sep 2021 10:42:31 +0300 (EEST) X-IronPort-AV: E=McAfee;i="6200,9189,10114"; a="221651664" X-IronPort-AV: E=Sophos;i="5.85,313,1624345200"; d="scan'208";a="221651664" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2021 00:42:28 -0700 X-IronPort-AV: E=Sophos;i="5.85,313,1624345200"; d="scan'208";a="474420201" Received: from xhh-dg164.sh.intel.com ([10.239.159.167]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2021 00:42:27 -0700 From: Haihao Xiang To: ffmpeg-devel@ffmpeg.org Date: Wed, 22 Sep 2021 15:42:06 +0800 Message-Id: <20210922074207.19451-1-haihao.xiang@intel.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/2] ffmpeg_opt: select a decoder after getting values for per-stream hwdec options 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: Haihao Xiang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 63tnREgsPfUQ After applying this patch, the desired HW acceleration method is known before selecting decoder, so we may take HW acceleration method into account when selecting decoder for input stream in the next commit There should be no functional changes in this patch --- fftools/ffmpeg_opt.c | 116 ++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 1d6d29cfc9..2d9a8a2ea0 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -853,63 +853,7 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic) st->codecpar->codec_tag = tag; } - ist->dec = choose_decoder(o, ic, st); - ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec); - - ist->reinit_filters = -1; - MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st); - - MATCH_PER_STREAM_OPT(discard, str, discard_str, ic, st); - ist->user_set_discard = AVDISCARD_NONE; - - if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) || - (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) || - (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) || - (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)) - ist->user_set_discard = AVDISCARD_ALL; - - if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard) < 0) { - av_log(NULL, AV_LOG_ERROR, "Error parsing discard %s.\n", - discard_str); - exit_program(1); - } - - ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE; - - ist->dec_ctx = avcodec_alloc_context3(ist->dec); - if (!ist->dec_ctx) { - av_log(NULL, AV_LOG_ERROR, "Error allocating the decoder context.\n"); - exit_program(1); - } - - ret = avcodec_parameters_to_context(ist->dec_ctx, par); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n"); - exit_program(1); - } - - if (o->bitexact) - ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT; - - switch (par->codec_type) { - case AVMEDIA_TYPE_VIDEO: - if(!ist->dec) - ist->dec = avcodec_find_decoder(par->codec_id); - - // avformat_find_stream_info() doesn't set this for us anymore. - ist->dec_ctx->framerate = st->avg_frame_rate; - - MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st); - if (framerate && av_parse_video_rate(&ist->framerate, - framerate) < 0) { - av_log(NULL, AV_LOG_ERROR, "Error parsing framerate %s.\n", - framerate); - exit_program(1); - } - - ist->top_field_first = -1; - MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st); - + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st); MATCH_PER_STREAM_OPT(hwaccel_output_formats, str, hwaccel_output_format, ic, st); @@ -986,6 +930,64 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic) } ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE; + } + + ist->dec = choose_decoder(o, ic, st); + ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec); + + ist->reinit_filters = -1; + MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st); + + MATCH_PER_STREAM_OPT(discard, str, discard_str, ic, st); + ist->user_set_discard = AVDISCARD_NONE; + + if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) || + (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) || + (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) || + (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)) + ist->user_set_discard = AVDISCARD_ALL; + + if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard) < 0) { + av_log(NULL, AV_LOG_ERROR, "Error parsing discard %s.\n", + discard_str); + exit_program(1); + } + + ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE; + + ist->dec_ctx = avcodec_alloc_context3(ist->dec); + if (!ist->dec_ctx) { + av_log(NULL, AV_LOG_ERROR, "Error allocating the decoder context.\n"); + exit_program(1); + } + + ret = avcodec_parameters_to_context(ist->dec_ctx, par); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n"); + exit_program(1); + } + + if (o->bitexact) + ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT; + + switch (par->codec_type) { + case AVMEDIA_TYPE_VIDEO: + if(!ist->dec) + ist->dec = avcodec_find_decoder(par->codec_id); + + // avformat_find_stream_info() doesn't set this for us anymore. + ist->dec_ctx->framerate = st->avg_frame_rate; + + MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st); + if (framerate && av_parse_video_rate(&ist->framerate, + framerate) < 0) { + av_log(NULL, AV_LOG_ERROR, "Error parsing framerate %s.\n", + framerate); + exit_program(1); + } + + ist->top_field_first = -1; + MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st); break; case AVMEDIA_TYPE_AUDIO: From patchwork Wed Sep 22 07:42:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xiang, Haihao" X-Patchwork-Id: 30454 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp263087iob; Wed, 22 Sep 2021 00:42:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNAc8qc5b+bVuVP0v4ubWfaSZGOXtiH6PTAjsZAoqPojmdZVQH36pKw/AHzrLaAFkx6+VV X-Received: by 2002:a05:6402:1e90:: with SMTP id f16mr39758667edf.136.1632296573173; Wed, 22 Sep 2021 00:42:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632296573; cv=none; d=google.com; s=arc-20160816; b=wcs+fFI0BMnO1NPlWTUoh7350Bw+jh1hHxexlf1PnLl/ziq4HdoaC8QYHQmXXUQ+yN FGKfGyVQz7GtPBk0DzKWB6G1yhNGivRi2N+J41p0S94K0P/6tUoZIMap1u5BC8eHIFna pKlXhkyZrh3ns5IIP2n5YQLYpwPy5ann/BoZl8teOWRYlaVIyn7MGUsbOKiqYTIGeNNE Wb+0U6q8/IefVwidlJjBAzHPBoCoCPtrTShXGaap5rnFXa3QvndMQCnZlh++MmFO3i1D wRy2Phb6bNYAsj8ND5a3/tMuQosRpBQ5AUuogDnWSMOtXevQW9clQQ7yNB2dMww18Jlp 8ygQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:delivered-to; bh=IfE09vv8urZWe/IX7JwLq9iMWGN7zB1y8oqG60VYNyI=; b=uh4bmNKvu5hvK5lqWoxZHFEvTSK/7n9Ca+N2cDjtANXy4XaWpj2VXLDB24rq10Nx7B H3IWOrep1cjhpQgfpyy3NDMAnLOxYOPpiIO1x/j+Z4DdRw+8zVWCxonXdR4W7cgNInJH kSs7sbyfBxSzofzdCNvd3CZmX7pbw32fYdYOhDscLfVT8tuGyG59M7B8KhnDYoqlT1ri GaRl3GmhzDRVH9OCgWEvyhv4bP1+sDmXN3pNg2AGGCXX2svT/HDxWKW5pZGN0y+7AAXp KfspoLk2yYi5YSSu/9yMVG+EaiN1xwL7eGwL5f4VLciw53lgv8+zISK7hfW6xW9iV03P nlsQ== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id i23si1778028edv.93.2021.09.22.00.42.52; Wed, 22 Sep 2021 00:42:53 -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; 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=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F14D868AF0C; Wed, 22 Sep 2021 10:42:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C040268A4C5 for ; Wed, 22 Sep 2021 10:42:37 +0300 (EEST) X-IronPort-AV: E=McAfee;i="6200,9189,10114"; a="221651665" X-IronPort-AV: E=Sophos;i="5.85,313,1624345200"; d="scan'208";a="221651665" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2021 00:42:29 -0700 X-IronPort-AV: E=Sophos;i="5.85,313,1624345200"; d="scan'208";a="474420205" Received: from xhh-dg164.sh.intel.com ([10.239.159.167]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2021 00:42:28 -0700 From: Haihao Xiang To: ffmpeg-devel@ffmpeg.org Date: Wed, 22 Sep 2021 15:42:07 +0800 Message-Id: <20210922074207.19451-2-haihao.xiang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210922074207.19451-1-haihao.xiang@intel.com> References: <20210922074207.19451-1-haihao.xiang@intel.com> Subject: [FFmpeg-devel] [PATCH 2/2] ffmpeg_opt: consider HW acceleration method when selecting decoder 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: Haihao Xiang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: KbXL721OiFCe Usually a HW decoder is expected when user specifies a HW acceleration method via -hwaccel option, however the current implementation doesn't take HW acceleration method into account, it is possible to select a SW decoder. For example: $> ffmpeg -hwaccel vaapi -i av1.ivf -f null - ... Stream #0:0 -> #0:0 (av1 (libdav1d) -> wrapped_avframe (native)) libdav1d is selected in this case even if vaapi is specified. After applying this patch, the native av1 decoder (with vaapi support) is selected. ... Stream #0:0 -> #0:0 (av1 (native) -> wrapped_avframe (native)) --- fftools/ffmpeg_opt.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 2d9a8a2ea0..914078add9 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -804,6 +804,48 @@ static const AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVSt return avcodec_find_decoder(st->codecpar->codec_id); } +static const AVCodec *choose_decoder2(InputStream *ist, OptionsContext *o, AVFormatContext *s, AVStream *st) +{ + char *codec_name = NULL; + + MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st); + if (codec_name) { + const AVCodec *codec = find_codec_or_die(codec_name, st->codecpar->codec_type, 0); + st->codecpar->codec_id = codec->id; + if (recast_media && st->codecpar->codec_type != codec->type) + st->codecpar->codec_type = codec->type; + return codec; + } else { + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + ist->hwaccel_id == HWACCEL_GENERIC && + ist->hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) { + const AVCodec *p; + void *i = 0; + + while ((p = av_codec_iterate(&i))) { + int j; + + if (p->id != st->codecpar->codec_id || + !av_codec_is_decoder(p) || + !avcodec_get_hw_config(p, 0)) + continue; + + for (j = 0; ;j++) { + const AVCodecHWConfig *config = avcodec_get_hw_config(p, j); + + if (!config) + break; + + if (config->device_type == ist->hwaccel_device_type) + return p; + } + } + } + + return avcodec_find_decoder(st->codecpar->codec_id); + } +} + /* Add all the streams from the given input file to the global * list of input streams. */ static void add_input_streams(OptionsContext *o, AVFormatContext *ic) @@ -932,7 +974,7 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic) ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE; } - ist->dec = choose_decoder(o, ic, st); + ist->dec = choose_decoder2(ist, o, ic, st); ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec); ist->reinit_filters = -1;