From patchwork Mon Apr 20 17:57:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wheatley X-Patchwork-Id: 19110 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 CC325445146 for ; Mon, 20 Apr 2020 20:58:44 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B7B0B68B828; Mon, 20 Apr 2020 20:58:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7A85C68B491 for ; Mon, 20 Apr 2020 20:58:37 +0300 (EEST) Received: by mail-wm1-f50.google.com with SMTP id t63so539195wmt.3 for ; Mon, 20 Apr 2020 10:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=eNmYCjmeTeHx8hz07M8lXX5YGyUxONuAq1V/bYCtZfY=; b=IdUN7udX0HKwYyv9iI9h7SjOAG11CyH3ykGn3T31ISsoU6/7GP0VwqvTan/mPPhIKm KXx0PokxJa+D4J26MIrhkBIU0Wy+ukRAwA00dyImjCWJR0/8BrHvBShTjJJPAJbK+emK 7uuYPbQpeFtPYKSdomsWOafEVS5om3roblJUmY1HCgChebp7iX1n3Sz1zCsgQHzuEIwt RsXbsK1Beh+mBBJP82HkrRi+XU+6vdmmV8gl3EO4wpYPNFQqS8Ty/Bm0ovxI7C6f/wQ3 QccZJsXO0A/CZZ16ji8aPrkJKiBF06tq11OHU8NgJLHetZAxBHJIjMSX8IZl4NRuqESb 208Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=eNmYCjmeTeHx8hz07M8lXX5YGyUxONuAq1V/bYCtZfY=; b=Tb/92kGuDU7fUoi2x0Kb1VppAh34e5ev2ZXhRd6GhD2gOQ834ojAWHatxMtccPktaQ i3uHwwZnZtOZYIgQ/CG5EzXLaEuXV8xM3nf8G/Z2x9NFaQqartlr6HIyWtU001nrspOp r02pkfGhArHIZp6A7SAie3xgSep1ch9ZApf6vq4A3irEAc7QzNoVh8HjARHKlQuRJ4s3 9xJ7VoaQu++lN3GVW9bpkdJH8xLeBL9x3ftzl3TQwJE1NvDvEtuZsWxB3mhpYOE8wZRA 24nXQAzmU86VRcDZYwRW/gEr54RKTCtEe26Y3BkLad6+kBwzVtOOyv8Pkf/W5HYIbziV zs/g== X-Gm-Message-State: AGi0PubwGjF9mITEcLEes8wF/RV92G3Xi/YtF/wP6jIxDJQ9Z6+XjQug BWSDd9NG1HSyaPAtrSJdR8d/iOi/T3Q= X-Google-Smtp-Source: APiQypKbtYlGxalH+EsBRFbeNntTnGUYIMijeZpNOl9GSC1rGnHlF25sabhdmM3Ymlaap9da2SoQpw== X-Received: by 2002:a1c:492:: with SMTP id 140mr567202wme.9.1587405516341; Mon, 20 Apr 2020 10:58:36 -0700 (PDT) Received: from derwent.ldn.framestore.com (ldndefault.framestore.com. [185.101.14.7]) by smtp.gmail.com with ESMTPSA id l5sm287930wrm.66.2020.04.20.10.58.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Apr 2020 10:58:35 -0700 (PDT) From: Kevin Wheatley To: ffmpeg-devel@ffmpeg.org Date: Mon, 20 Apr 2020 18:57:39 +0100 Message-Id: <1587405459-29001-4-git-send-email-kevin.j.wheatley@gmail.com> X-Mailer: git-send-email 1.8.5.6 In-Reply-To: <1587405459-29001-1-git-send-email-kevin.j.wheatley@gmail.com> References: <1587405459-29001-1-git-send-email-kevin.j.wheatley@gmail.com> Subject: [FFmpeg-devel] [PATCH 3/3] avformat/movenc: Add an automatic timescale computation 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 Cc: Kevin Wheatley MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Activated when the -mov_timescale command line/MOVMuxContext parameter is set to 0 or less. If the user passes any value greater than 0, then it will be used as-is. The default value is kept unchanged at 1000. When active, it uses the base assumption from the QuickTime specification of 600 and combines the video stream time bases to compute an accurate answer if possible. In cases when the first stream result falls outside MOV_MAX_AUTO_TIMESCALE or if a non-integer video stream is encounted, then the first stream's time_base will be used as the base. Signed-off-by: Kevin Wheatley --- libavformat/movenc.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/movenc.h | 1 + 2 files changed, 72 insertions(+) diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 508fa73..8edb848 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -6208,6 +6208,70 @@ static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track, return 0; } +static int mov_determine_movie_timescale(AVFormatContext *s, MOVMuxContext *mov) +{ + // Assume typical integer frame rates: + // 600 is a common multiple of 24, 25, 30, 50, 60, etc. + // see p221, https://developer.apple.com/standards/qtff-2001.pdf + int timescale = 600; + AVRational temp; + int exact; + int first_video_track = 1; + + // If the user specified a timescale, just use it as-is + if (mov->mov_timescale > 0) { + return mov->mov_timescale; + } + + // Determine the timescale, based on the video stream time_base values + for (int i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + + // If the first video track is not an integer, use its denominator + // as the basis of the remaining calculations + if (first_video_track && st->time_base.num != 1) { + timescale = st->time_base.den; + av_log(s, AV_LOG_VERBOSE, "Using first video stream non-integer time_base: %d/%d\n", + st->time_base.den, st->time_base.num); + } + + // determine if we can make an even multiple of the current timescale and the video track + av_log(s, AV_LOG_VERBOSE, "Current estimated timescale: %d\n", timescale); + av_log(s, AV_LOG_TRACE, "Stream #%d time_base: %d/%d\n", i, + st->time_base.num, st->time_base.den); + + // Try keep the time_scale within a sensible bound + exact = av_reduce(&temp.num, &temp.den, + (int64_t)timescale * st->time_base.num, + st->time_base.den, + MOV_MAX_AUTO_TIMESCALE); + + // Use a scaled version of the timescale + if (exact) { + timescale *= temp.den; + av_log(s, AV_LOG_TRACE, "Adjusted timescale: %d/%d %d\n", + temp.den, temp.num, timescale); + } else { + // We overflowed try the denominator as is + timescale = temp.den; + av_log(s, AV_LOG_WARNING, "Timescale calculation out of bounds approximating %d/%d %d\n", + temp.den, temp.num, timescale); + } + + if (first_video_track && ((timescale > MOV_MAX_AUTO_TIMESCALE) || !exact)) { + timescale = st->time_base.den; + av_log(s, AV_LOG_WARNING, "Potentially large timescale, " + "using first video stream time_base: %d/%d\n", + st->time_base.den, st->time_base.num); + } + first_video_track = 0; + } + } + av_log(s, AV_LOG_VERBOSE, "Final timescale: %d\n", timescale); + return timescale; +} + static int mov_init(AVFormatContext *s) { MOVMuxContext *mov = s->priv_data; @@ -6266,6 +6330,13 @@ static int mov_init(AVFormatContext *s) mov->reserved_moov_size = -1; } + mov->mov_timescale = mov_determine_movie_timescale(s, mov); + if (mov->mov_timescale > MOV_MAX_AUTO_TIMESCALE) { + av_log(s, AV_LOG_WARNING, "Potentially large timescale %d, use " + "-mov_timescale if you have issues\n", + mov->mov_timescale); + } + if (mov->use_editlist < 0) { mov->use_editlist = 1; if (mov->flags & FF_MOV_FLAG_FRAGMENT && diff --git a/libavformat/movenc.h b/libavformat/movenc.h index 322968c..4d6b7b7 100644 --- a/libavformat/movenc.h +++ b/libavformat/movenc.h @@ -30,6 +30,7 @@ #define MOV_FRAG_INFO_ALLOC_INCREMENT 64 #define MOV_INDEX_CLUSTER_SIZE 1024 #define MOV_TIMESCALE 1000 +#define MOV_MAX_AUTO_TIMESCALE 10000 #define RTP_MAX_PACKET_SIZE 1450