From patchwork Sun Sep 13 10:26:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Jan_Ekstr=C3=B6m?= X-Patchwork-Id: 22333 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 9B98E44B525 for ; Sun, 13 Sep 2020 13:34:31 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7A71268BBDE; Sun, 13 Sep 2020 13:34:31 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3CB6268BBB6 for ; Sun, 13 Sep 2020 13:34:24 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id w5so15557860wrp.8 for ; Sun, 13 Sep 2020 03:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=xFYAcJhkV718VcfZUdwhm+3AZWsZPZMIv+8l7ql/1vc=; b=C53XyPDMAFuB3SJ5A3pcQ2piG107wTfUlb+ZAG7QoUZaoV3TCNf/EKloxoEiC9miX4 n0cfxRJc+ZskYp0Z9NsOKhv9q01gwY5lCcY9L96UChBvdx9hyn2V5W/VN6z3LEBr/zJM oPCAmF7k6yPeGZNyNVGsdqLhYTftAt9ljNc4xzc8drL0yyrPw52RdN6u8T8WuImEcJ+g dwVjJ8ZL+lP/BCKxRt4eHrd2haEFrvw77eA9Ic3eXDgKf8XPG7ILoMO3JYTybqCtPJ2s VH8W8mJhGfoX6ZM7KChMRAI+2hVWGNiXoPugihRYVMuOL4Tk4VVGBcugujxQfVzt0xeN nNlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xFYAcJhkV718VcfZUdwhm+3AZWsZPZMIv+8l7ql/1vc=; b=nCMyFABvX41NmxmEkZ7j6h3pHQsYKQl4Iu0AENKHjQudeFiVDk8Qp+XRpUmArljqD/ tf0OUQ2hICq4KkEs0dxo9n/NKv4Lm8XG/imZ1pdjYQGTN7BpHLlkstk9dGHcG0zWBDh3 seA+/Ipun/A0jmmA++jv/oVbri33Li4z0yV8Eml2AoevBdbsOKFx5Y5IpcIINljsrgmN AbzSAHlWlijXVRWiaFr2V4r6P6GLCcCL1hZH5lIJYGOQBOC4XkACN2ogZT/CAcEWYlBh 3NDR88+sfCW29DIp0qrz1QxPoHhy7QPQFicBihHVHFQpZK/8vOJKRMC2vKVJYGbNQqoP Czeg== X-Gm-Message-State: AOAM5312WC+hGjvJB1dCzcw4GX407eS4OgLnB1Pd5pr+98uYc4n0m4wl tLY8O/EeIqlA18etN6e2Lzr0sByXJZE= X-Google-Smtp-Source: ABdhPJwfbeectil4VmEIHOcb51DYYWb2+D8w1lSc2eqlc026hb2hbumPWh8dszQpHW7bzKrpc9Zjfg== X-Received: by 2002:a19:7e92:: with SMTP id z140mr3133922lfc.299.1599992786660; Sun, 13 Sep 2020 03:26:26 -0700 (PDT) Received: from localhost.localdomain (91-159-194-103.elisa-laajakaista.fi. [91.159.194.103]) by smtp.gmail.com with ESMTPSA id n3sm2674790ljj.59.2020.09.13.03.26.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Sep 2020 03:26:26 -0700 (PDT) From: =?utf-8?q?Jan_Ekstr=C3=B6m?= To: ffmpeg-devel@ffmpeg.org Date: Sun, 13 Sep 2020 13:26:20 +0300 Message-Id: <20200913102622.168011-3-jeebjp@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200913102622.168011-1-jeebjp@gmail.com> References: <20200913102622.168011-1-jeebjp@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] ffmpeg: move AVFrame time base adjustment into a function 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" This will have to be called later for video down the line. --- fftools/ffmpeg.c | 77 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 25 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index cb7644de6a..7e6c0a962b 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -897,6 +897,55 @@ static int check_recording_time(OutputStream *ost) return 1; } +static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost, + AVFrame *frame) +{ + if (!frame || frame->pts == AV_NOPTS_VALUE || + !ost->filter || !ost->filter->graph->graph) + return AV_NOPTS_VALUE; + + double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision + AVFilterContext *filter = ost->filter->filter; + AVCodecContext *enc = ost->enc_ctx; + + int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time; + AVRational filter_tb = av_buffersink_get_time_base(filter); + AVRational tb = enc->time_base; + int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16); + + tb.den <<= extra_bits; + av_log(NULL, AV_LOG_VERBOSE, + "%s: frame pts: %"PRId64" start_time: %"PRId64", " + "filter_tb: %d/%d, tb: %d/%d\n", + __FUNCTION__, frame->pts, start_time, + filter_tb.num, filter_tb.den, + tb.num, tb.den); + + float_pts = + av_rescale_q(frame->pts, filter_tb, tb) - + av_rescale_q(start_time, AV_TIME_BASE_Q, tb); + float_pts /= 1 << extra_bits; + // avoid exact midoints to reduce the chance of rounding differences, this can be removed in case the fps code is changed to work with integers + float_pts += FFSIGN(float_pts) * 1.0 / (1<<17); + + frame->pts = + av_rescale_q(frame->pts, filter_tb, enc->time_base) - + av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base); + + av_log(NULL, AV_LOG_VERBOSE, + "%s: post-adjustment PTS: %"PRId64"\n", + __FUNCTION__, frame->pts); + + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n", + av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base), + float_pts, + enc->time_base.num, enc->time_base.den); + } + + return float_pts; +} + static void do_audio_out(OutputFile *of, OutputStream *ost, AVFrame *frame) { @@ -1473,37 +1522,15 @@ static int reap_filters(int flush) av_frame_unref(filtered_frame); continue; } - if (filtered_frame->pts != AV_NOPTS_VALUE) { - int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time; - AVRational filter_tb = av_buffersink_get_time_base(filter); - AVRational tb = enc->time_base; - int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16); - - tb.den <<= extra_bits; - float_pts = - av_rescale_q(filtered_frame->pts, filter_tb, tb) - - av_rescale_q(start_time, AV_TIME_BASE_Q, tb); - float_pts /= 1 << extra_bits; - // avoid exact midoints to reduce the chance of rounding differences, this can be removed in case the fps code is changed to work with integers - float_pts += FFSIGN(float_pts) * 1.0 / (1<<17); - - filtered_frame->pts = - av_rescale_q(filtered_frame->pts, filter_tb, enc->time_base) - - av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base); - } + + float_pts = adjust_frame_pts_to_encoder_tb(of, ost, + filtered_frame); switch (av_buffersink_get_type(filter)) { case AVMEDIA_TYPE_VIDEO: if (!ost->frame_aspect_ratio.num) enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio; - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n", - av_ts2str(filtered_frame->pts), av_ts2timestr(filtered_frame->pts, &enc->time_base), - float_pts, - enc->time_base.num, enc->time_base.den); - } - do_video_out(of, ost, filtered_frame, float_pts); break; case AVMEDIA_TYPE_AUDIO: