From patchwork Fri Oct 16 13:16:45 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: 23015 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 A43CB44B20D for ; Fri, 16 Oct 2020 16:24:09 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 844AE68BAAC; Fri, 16 Oct 2020 16:24:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f65.google.com (mail-lf1-f65.google.com [209.85.167.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5D17D68B903 for ; Fri, 16 Oct 2020 16:24:02 +0300 (EEST) Received: by mail-lf1-f65.google.com with SMTP id l28so2843480lfp.10 for ; Fri, 16 Oct 2020 06:24:02 -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=Np0qDXgaZex9jai3MfKaTNk9A1UTLQqCFBFqKrQv3zo=; b=M46xt/E1a3Med4/YwDk1KDHEZZ/o1RTOPeNUh5VGKl5U55g72P4KjS3iNw1YqsSYlP UdF122ob+VRsroKQhlkpH6Mv9YbRmKPWIqXRxmIa2qIrrWxvcV1pcYn/G+etkF7XPqyl IkVfVgOZDJF/MU/e0mcRfj52SvZvIxfGHDVusD1waxSKi0+13lNeoopEK2pQmo311wyb kv9vQ3xljahsrgEjnq7wveFi5BVhVK8Yff5iVucAZ5sVUid4RBIUssZh1VAaw3HpEZsE qsGBBmvcK9eMvRruOWrEKavM8GAa7NyPs9KS/qLIZwkb9wz/mN9I+f9JJpj89lpQSUOf Y2gA== 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=Np0qDXgaZex9jai3MfKaTNk9A1UTLQqCFBFqKrQv3zo=; b=oJ0UhYaGoZbWdWGw6Zbd+/9nAEqq7OD8e86GV0u1dDhg5kQO1g/xdHm8tiVEiiZByp ZuNmf4zV6oOLg9X802T1xsT46VMOtu8chEYRuF7RJwCptXkJHKy83pmp5FmkNnYiO1JK 5DkbJ8ZrAQQr0x7+TkdzvjUODnFoI/NP7njoKpxuLzCbgJBakbtoZ3YxgX74vYXsimxw MfWPnT0+IR126qzOpCFg7BX7ldF5cpWnbe4ZTLWk5gt8qt57ay92Ikj2vmMLV+hkUAWI E31Rw3Pm0X+QOAD49oltqy8892LRYty8qIUXTWjK+Tqym9CBKDMM5qLRz4FrM4a8s4Vh aKgg== X-Gm-Message-State: AOAM532BTqGgFyhdUw1nPsHGKnR9ZDxxNOYpp3GKzw5PsrlRX79Z4S+z IEU214hyvxUMD4VhQ/P7VGxGNNY6+1w= X-Google-Smtp-Source: ABdhPJyLfHCdCZKFbIm/onE2Hem6TgCn3dJNj47wHMqzhpTfBpmnpYA7iaMHWfjusNA3OVSYoGrTFg== X-Received: by 2002:a19:8606:: with SMTP id i6mr1256296lfd.263.1602854217411; Fri, 16 Oct 2020 06:16:57 -0700 (PDT) Received: from localhost.localdomain (n89ridqjqdjpsztph-2.v6.elisa-mobile.fi. [2001:999:11:8336:994:7f72:f89e:9125]) by smtp.gmail.com with ESMTPSA id x13sm280594lfe.101.2020.10.16.06.16.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Oct 2020 06:16:56 -0700 (PDT) From: =?utf-8?q?Jan_Ekstr=C3=B6m?= To: ffmpeg-devel@ffmpeg.org Date: Fri, 16 Oct 2020 16:16:45 +0300 Message-Id: <20201016131649.4361-3-jeebjp@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201016131649.4361-1-jeebjp@gmail.com> References: <20201016131649.4361-1-jeebjp@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 2/6] 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 | 72 +++++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index cb7644de6a..0d8ed26912 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -897,6 +897,50 @@ static int check_recording_time(OutputStream *ost) return 1; } +static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost, + AVFrame *frame) +{ + double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision + AVCodecContext *enc = ost->enc_ctx; + if (!frame || frame->pts == AV_NOPTS_VALUE || + !enc || !ost->filter || !ost->filter->graph->graph) + goto early_exit; + + { + AVFilterContext *filter = ost->filter->filter; + + 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(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); + } + +early_exit: + + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n", + frame ? av_ts2str(frame->pts) : "NULL", + frame ? av_ts2timestr(frame->pts, &enc->time_base) : "NULL", + float_pts, + enc ? enc->time_base.num : -1, + enc ? enc->time_base.den : -1); + } + + return float_pts; +} + static void do_audio_out(OutputFile *of, OutputStream *ost, AVFrame *frame) { @@ -1473,37 +1517,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: