From patchwork Thu Jun 25 18:35:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas George X-Patchwork-Id: 20608 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 A46D9449F4F for ; Thu, 25 Jun 2020 21:35:27 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7EB5568805D; Thu, 25 Jun 2020 21:35:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from nef.ens.fr (nef2.ens.fr [129.199.96.40]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A9E8168011F for ; Thu, 25 Jun 2020 21:35:20 +0300 (EEST) X-ENS-nef-client: 129.199.129.80 Received: from phare.normalesup.org (phare.normalesup.org [129.199.129.80]) by nef.ens.fr (8.14.4/1.01.28121999) with ESMTP id 05PIZJ4X007010 for ; Thu, 25 Jun 2020 20:35:20 +0200 Received: by phare.normalesup.org (Postfix, from userid 1001) id E1795EB5C3; Thu, 25 Jun 2020 20:35:19 +0200 (CEST) From: Nicolas George To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Jun 2020 20:35:17 +0200 Message-Id: <20200625183518.1217406-2-george@nsup.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200625183518.1217406-1-george@nsup.org> References: <20200625183518.1217406-1-george@nsup.org> MIME-Version: 1.0 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (nef.ens.fr [129.199.96.32]); Thu, 25 Jun 2020 20:35:20 +0200 (CEST) Subject: [FFmpeg-devel] [PATCH 2/3] fftools/ffmpeg: move filter/encoder PTS computation to a separate 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" Signed-off-by: Nicolas George --- fftools/ffmpeg.c | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 2e9448ea2b..3919f2ab62 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1404,6 +1404,32 @@ static void finish_output_stream(OutputStream *ost) } } +static double compute_encoder_pts_from_filter_pts(AVFilterContext *filter, OutputFile *of, + AVCodecContext *enc, int64_t *filtered_frame_pts) +{ + double float_pts = AV_NOPTS_VALUE; + + 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); + } + return float_pts; +} + /** * Get and encode new output from any of the filtergraphs, without causing * activity. @@ -1460,24 +1486,7 @@ 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 = compute_encoder_pts_from_filter_pts(filter, of, enc, &filtered_frame->pts); switch (av_buffersink_get_type(filter)) { case AVMEDIA_TYPE_VIDEO: