From patchwork Wed Oct 14 08:53:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 22938 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id A92DA44ACC7 for ; Wed, 14 Oct 2020 11:53:27 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8992B68BB27; Wed, 14 Oct 2020 11:53:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 576F068B92B for ; Wed, 14 Oct 2020 11:53:18 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id EF95F296288 for ; Wed, 14 Oct 2020 10:53:17 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id d6si8onEZaHV for ; Wed, 14 Oct 2020 10:53:17 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.daenerys.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id BC056296313 for ; Wed, 14 Oct 2020 10:53:14 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id D53E720E00BD; Wed, 14 Oct 2020 10:53:13 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 14 Oct 2020 10:53:09 +0200 Message-Id: <20201014085311.14728-1-anton@khirnov.net> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] ffmpeg.c: rename 'area' to 'score' X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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" Other factors besides area are used to pick the best video stream, so the name 'area' is misleading. --- fftools/ffmpeg_opt.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 19f719e3ff..a0d1b06f2d 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2210,22 +2210,22 @@ static int open_output_file(OptionsContext *o, const char *filename) /* video: highest resolution */ if (!o->video_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_VIDEO) != AV_CODEC_ID_NONE) { - int area = 0, idx = -1; + 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 new_area; + int score; ist = input_streams[i]; - new_area = ist->st->codecpar->width * ist->st->codecpar->height + 100000000*!!ist->st->codec_info_nb_frames + score = ist->st->codecpar->width * ist->st->codecpar->height + 100000000*!!ist->st->codec_info_nb_frames + 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)) - new_area = 1; + score = 1; if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && - new_area > area) { + score > best_score) { if((qcr==MKTAG('A', 'P', 'I', 'C')) && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) continue; - area = new_area; + best_score = score; idx = i; } } From patchwork Wed Oct 14 08:53:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 22937 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 19D8A44ACC7 for ; Wed, 14 Oct 2020 11:53:26 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EE9BE68BB7A; Wed, 14 Oct 2020 11:53:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4DD4B68B8F8 for ; Wed, 14 Oct 2020 11:53:18 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 87CC4296314 for ; Wed, 14 Oct 2020 10:53:17 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id nOUH2zZhHXIy for ; Wed, 14 Oct 2020 10:53:15 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.daenerys.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id BA099296288 for ; Wed, 14 Oct 2020 10:53:14 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 16F5B20E0039; Wed, 14 Oct 2020 10:53:13 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 14 Oct 2020 10:53:10 +0200 Message-Id: <20201014085311.14728-2-anton@khirnov.net> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201014085311.14728-1-anton@khirnov.net> References: <20201014085311.14728-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] ffmpeg.c: refine picking default video stream X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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" Use a floating-point score value to take into account bitrate, when multiple streams with the same resolution are present. Stop accessing private AVStream.codec_info_nb_frames field, as the sample in question http://samples.ffmpeg.org/ffmpeg-bugs/trac/ticket2639/Thailand-Wave.wmv is now handled by the above change. --- fftools/ffmpeg_opt.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index a0d1b06f2d..afef23919c 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -19,6 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include "ffmpeg.h" @@ -2208,15 +2209,27 @@ static int open_output_file(OptionsContext *o, const char *filename) char *subtitle_codec_name = NULL; /* pick the "best" stream of each type */ - /* video: highest resolution */ + /* video */ 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; + double best_score = 0.0; + int idx = -1; int qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0); for (i = 0; i < nb_input_streams; i++) { - int score; + double score; ist = input_streams[i]; - score = ist->st->codecpar->width * ist->st->codecpar->height + 100000000*!!ist->st->codec_info_nb_frames - + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT); + + /* base score is just the area in pixels */ + score = (double)ist->st->codecpar->width * ist->st->codecpar->height; + /* add a fractional part favoring higher bitrate among same-area streams */ + if (ist->st->codecpar->bit_rate) { + const double bitrate_max = 100e6; // cap at 100Mb/s + const double bitrate = FFMIN(ist->st->codecpar->bit_rate, bitrate_max - 1.0); + score += bitrate / bitrate_max; + } + /* default streams get max score */ + if (ist->st->disposition & AV_DISPOSITION_DEFAULT) + score = DBL_MAX; + if (ist->user_set_discard == AVDISCARD_ALL) continue; if((qcr!=MKTAG('A', 'P', 'I', 'C')) && (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)) From patchwork Wed Oct 14 08:53:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 22936 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id AB58E44ACC7 for ; Wed, 14 Oct 2020 11:53:24 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7A08168BAB2; Wed, 14 Oct 2020 11:53:24 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 457C868B8D9 for ; Wed, 14 Oct 2020 11:53:18 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 8961A296315 for ; Wed, 14 Oct 2020 10:53:17 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id pz2G25xbsaPv for ; Wed, 14 Oct 2020 10:53:15 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.daenerys.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id BB055296312 for ; Wed, 14 Oct 2020 10:53:14 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 2D06F20E00B1; Wed, 14 Oct 2020 10:53:13 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 14 Oct 2020 10:53:11 +0200 Message-Id: <20201014085311.14728-3-anton@khirnov.net> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201014085311.14728-1-anton@khirnov.net> References: <20201014085311.14728-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] ffmpeg.c: refactor picking default video stream X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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" Do not bother computing the score for streams that are going to be skipped. This is easier to follow. --- fftools/ffmpeg_opt.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index afef23919c..eebb678e5e 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2218,6 +2218,11 @@ static int open_output_file(OptionsContext *o, const char *filename) double score; ist = input_streams[i]; + if (ist->st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO || + ist->user_set_discard == AVDISCARD_ALL || + (qcr == MKTAG('A', 'P', 'I', 'C') && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))) + continue; + /* base score is just the area in pixels */ score = (double)ist->st->codecpar->width * ist->st->codecpar->height; /* add a fractional part favoring higher bitrate among same-area streams */ @@ -2230,14 +2235,10 @@ static int open_output_file(OptionsContext *o, const char *filename) if (ist->st->disposition & AV_DISPOSITION_DEFAULT) score = DBL_MAX; - 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)) - continue; + + if (score > best_score) { best_score = score; idx = i; }