From patchwork Thu Jul 29 04:44:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 29091 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a11:4023:0:0:0:0 with SMTP id ky35csp6421892pxb; Wed, 28 Jul 2021 21:44:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOPGcwPov3wdHlX1vxPJ5Z1q7RBhrBAxNS90E1ypgZWJzSuxIMKzlHV46D/z1IFKyU9U2H X-Received: by 2002:a17:906:1416:: with SMTP id p22mr2765892ejc.364.1627533893033; Wed, 28 Jul 2021 21:44:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627533893; cv=none; d=google.com; s=arc-20160816; b=urfADsVjWNveDAYmFlia035bAfZxJnMsgxbYdHZPLnxg1tw2qMv2/ZdHdjvUF/tDQK 4VFqAfrgjri5VwgdlhmcKWM3NdGnZyROcJLvE7PM+DkkCIb+diFFP7rjcB2EALG+f8M5 yZDNDpIxMIOQjdjRgC1HqH+dEmc2dD2UbP52Pb2c9v6qI9lweMqdY0bGnFoEWCzTUDGz SbUxd3P0s+weCHpCEBUIzYxXiuKPQFAZPO3Iqw+dasuLMPbtNxj2omdQmzWX1MpX0XJ+ O7jy2GcXlkma8mgVgcEukgX6ysH/xrim4x9P9ijpz5z7W9fNxaJxY2LLkZ9XnVOl2mnS ddUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=O45ITsn3Wxwjwp1Cjs32Xz7jEoaccXKiqVPgbKwWC00=; b=YDkmVPo5SrJf+5BkHBbmVb4cVSVgbivuYk4gXyxDbPkCl4aGhozXjl1aOktkybWG7H JUxYZInrkXExWeKUgQ3BxlpZltpdaEmObO7tW03sSWYW+eqv3VJG1XO1VqNwcrMVr+Xg Euzp528V0m1p6IX1tYQzOEQKzFz7vFBhAZnABhOTxoo2bRKXq1xXFUOKaN4SbxSh7Ar1 H0uvJT+vaytZF7XDjAVkVGtLnB0BtvFpxOtxRw5SQ5aDvcqT9pHs2/PA7RSvel4YOC0p MB9Tz/l/+lvWNenbmxR518iZ5A/2hPm+o1GJjvACN2M/EGAEJ9wjvUi3BRmFjiE6x1yF xCXQ== 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id l1si1565319edw.438.2021.07.28.21.44.51; Wed, 28 Jul 2021 21:44: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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 98B6368060F; Thu, 29 Jul 2021 07:44:46 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1BC9068060F for ; Thu, 29 Jul 2021 07:44:39 +0300 (EEST) Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4GZycR0ml9zQk37 for ; Thu, 29 Jul 2021 06:44:39 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by gerste.heinlein-support.de (gerste.heinlein-support.de [91.198.250.173]) (amavisd-new, port 10030) with ESMTP id pKZ12oDRg_s3 for ; Thu, 29 Jul 2021 06:44:35 +0200 (CEST) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Thu, 29 Jul 2021 10:14:20 +0530 Message-Id: <20210729044420.2282-1-ffmpeg@gyani.pro> In-Reply-To: <20210728202303.GC4777@pb2> References: <20210728202303.GC4777@pb2> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2D239183A X-Rspamd-UID: fdc28d Subject: [FFmpeg-devel] [PATCH v3] ffmpeg_opt: restore documented stream selection behaviour 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 4Cf0xbMTB+Zc 11d3b03fcb added consideration of default stream disposition for audio and video when choosing the 'best' stream among all the inputs. This can lead to video streams with lower resolution or audio streams with fewer channels being selected. Stream disposition, however, only sets a priority for a stream among all other streams in the *same input*. It cannot set a priority for a stream across all inputs. This patch sets a middle-way and selects the best stream from each file with default disposition considered. Then it discards disposition weight and selects best stream as per the original criteria of highest resolution for video and most channels for audio. --- fftools/ffmpeg_opt.c | 77 ++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 27 deletions(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 1b43bab9fc..34cc6c4fd3 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2260,23 +2260,35 @@ static int open_output_file(OptionsContext *o, const char *filename) if (!o->video_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_VIDEO) != AV_CODEC_ID_NONE) { int best_score = 0, idx = -1; int qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0); - for (i = 0; i < nb_input_streams; i++) { - int score; - ist = input_streams[i]; - score = ist->st->codecpar->width * ist->st->codecpar->height - + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS) - + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT); - if (ist->user_set_discard == AVDISCARD_ALL) - continue; - if((qcr!=MKTAG('A', 'P', 'I', 'C')) && (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) - score = 1; - if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && - score > best_score) { - if((qcr==MKTAG('A', 'P', 'I', 'C')) && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) + for (j = 0; j < nb_input_files; j++) { + InputFile *ifile = input_files[j]; + int file_best_score = 0, file_best_idx = -1; + for (i = 0; i < ifile->nb_streams; i++) { + int score; + ist = input_streams[ifile->ist_index + i]; + score = ist->st->codecpar->width * ist->st->codecpar->height + + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS) + + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT); + if (ist->user_set_discard == AVDISCARD_ALL) continue; - best_score = score; - idx = i; + if((qcr!=MKTAG('A', 'P', 'I', 'C')) && (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) + score = 1; + if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + score > file_best_score) { + if((qcr==MKTAG('A', 'P', 'I', 'C')) && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) + continue; + file_best_score = score; + file_best_idx = ifile->ist_index + i; + } } + if (file_best_idx >= 0) { + if((qcr == MKTAG('A', 'P', 'I', 'C')) || !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) + file_best_score -= 5000000*!!(input_streams[file_best_idx]->st->disposition & AV_DISPOSITION_DEFAULT); + if (file_best_score > best_score) { + best_score = file_best_score; + idx = file_best_idx; + } + } } if (idx >= 0) new_video_stream(o, oc, idx); @@ -2285,19 +2297,30 @@ static int open_output_file(OptionsContext *o, const char *filename) /* audio: most channels */ if (!o->audio_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_AUDIO) != AV_CODEC_ID_NONE) { int best_score = 0, idx = -1; - for (i = 0; i < nb_input_streams; i++) { - int score; - ist = input_streams[i]; - score = ist->st->codecpar->channels - + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS) - + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT); - if (ist->user_set_discard == AVDISCARD_ALL) - continue; - if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && - score > best_score) { - best_score = score; - idx = i; + for (j = 0; j < nb_input_files; j++) { + InputFile *ifile = input_files[j]; + int file_best_score = 0, file_best_idx = -1; + for (i = 0; i < ifile->nb_streams; i++) { + int score; + ist = input_streams[ifile->ist_index + i]; + score = ist->st->codecpar->channels + + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS) + + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT); + if (ist->user_set_discard == AVDISCARD_ALL) + continue; + if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && + score > file_best_score) { + file_best_score = score; + file_best_idx = ifile->ist_index + i; + } } + if (file_best_idx >= 0) { + file_best_score -= 5000000*!!(input_streams[file_best_idx]->st->disposition & AV_DISPOSITION_DEFAULT); + if (file_best_score > best_score) { + best_score = file_best_score; + idx = file_best_idx; + } + } } if (idx >= 0) new_audio_stream(o, oc, idx);