From patchwork Wed Jul 28 08:39:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 29082 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a11:4023:0:0:0:0 with SMTP id ky35csp5592049pxb; Wed, 28 Jul 2021 01:39:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8ICHHNJNb0JcEXKrPgFqmHV3Veo7701pyX0IPY15TndZrm+DJIcVINY920JV11QBzjVPm X-Received: by 2002:a05:6402:152:: with SMTP id s18mr32184811edu.221.1627461571312; Wed, 28 Jul 2021 01:39:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627461571; cv=none; d=google.com; s=arc-20160816; b=S9z7+g+EdeY5+Q6vMih2IeMvQdWZ9AbsowjTBAWBWP59SzERUbEVmQAKcyuLEI98tf fby4SmrL8gx4NjGIz0g2q2AwufYuFTqPylbUZDCRY9pUR8xUHsCRLEy433johPtgSn+A 3qqa1eovWLkkJbxRpIuJVTAGEl7PWYvWbgmxP3Tuhe85sNR3t/9YB53RMcQEMeq7GHKV paZQa9bxoWSKL/kbFx5zYb3IK0IIEdiuedhUrdqEU8SZhO9wmc27Vm4Uht50r68lDfri pj+9CcAJ7fVQKXQI+J1CvaGLlzkFmnlGQxxX8U58P4L5HroRSwoaT41PTz8TEExS2jT6 dXtw== 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:message-id:date:to:from :delivered-to; bh=nYD3pvFtB9yvxDyfITrAjNvKkzxh4Jh5JR4foybsdKE=; b=cqvMZrZIdSZku508go/Gx+yNslEIWM45vURfOe4twgXw8AuTD7yjFTKFCH/suvdvH6 qwg8MUfIS2/CfDz/TtYtSnieowB+afp1C7V2JJwHLb9/31bHmjHVAjS/xgLg37wl4CW9 mR+mqhH9TjzjgtnIdMWFWonF9UEScudxw9AJY7liblgJBtCiVk16iIwuDCqt19ZaP6e9 DydSgtVxpFhzcMhT9ay9Nlxib3DIfoKsEQvwyReWmU0FabH0kHfatbJrdAIF7QG1rNc3 5B+r9nFwu+DVfuerDKjHqMFeiILAGS9JEIYMOToGuk1jDqj1DjWjLrs0txCN2vtwvWUz uLOw== 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 j25si6250935edr.48.2021.07.28.01.39.30; Wed, 28 Jul 2021 01:39:31 -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 E78CA68A0D5; Wed, 28 Jul 2021 11:39:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 21FD06881B5 for ; Wed, 28 Jul 2021 11:39:19 +0300 (EEST) Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (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-101.mailbox.org (Postfix) with ESMTPS id 4GZRsf5XkszQk2M for ; Wed, 28 Jul 2021 10:39:18 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id 8ky-ZBhuidvi for ; Wed, 28 Jul 2021 10:39:14 +0200 (CEST) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Wed, 28 Jul 2021 14:09:00 +0530 Message-Id: <20210728083900.1634-1-ffmpeg@gyani.pro> MIME-Version: 1.0 X-Rspamd-Queue-Id: CCEA518C1 X-Rspamd-UID: 26825a Subject: [FFmpeg-devel] [PATCH v2] 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: ntFwarFFQZMo 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. --- Corrected out-of-bounds input stream access. fftools/ffmpeg_opt.c | 78 +++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 1b43bab9fc..72eddf3678 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2260,23 +2260,34 @@ 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) { + 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 +2296,32 @@ 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); +av_log(NULL, AV_LOG_WARNING, "within file %d istindex %d i %d score %d file_best_score %d file_best_idx %d best_score %d idx %d\n", + j, ifile->ist_index, i, score, file_best_score, file_best_idx, best_score, idx); + 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);