From patchwork Wed Apr 29 13:25:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: leozhang X-Patchwork-Id: 19371 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 0E0F644BA87 for ; Wed, 29 Apr 2020 16:25:50 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E64A368BFF0; Wed, 29 Apr 2020 16:25:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smg-sh-01.qiyi.com (unknown [101.227.12.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8D22868BB52 for ; Wed, 29 Apr 2020 16:25:42 +0300 (EEST) X-AuditID: 65e30cac-a39ff70000002643-b2-5ea98055d47f Received: from mail.iqiyi.com (Unknown_Domain [10.16.130.5]) by smg-sh-01.qiyi.com (Qiyi mail Gateway) with SMTP id 7E.30.09795.55089AE5; Wed, 29 Apr 2020 21:25:41 +0800 (HKT) From: leozhang To: Date: Wed, 29 Apr 2020 21:25:36 +0800 Message-ID: <1588166736-82427-1-git-send-email-leozhang@qiyi.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.49.22.14] X-ClientProxiedBy: BJ-CAS24.iqiyi.pps (10.15.221.33) To EXCH28B.iqiyi.pps (10.16.148.55) Subject: [FFmpeg-devel] [PATCH 2/3] avformat/fifo: add option to write packets in paced way 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: leozhang --- doc/muxers.texi | 3 +++ libavformat/fifo.c | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/doc/muxers.texi b/doc/muxers.texi index a74cbc4..5140c00 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -2274,6 +2274,9 @@ queue overflow or failure. This option is set to 0 (false) by default. @item output_delay Time to delay output, in microseconds. Default value is 0. +@item paced +If set to 1 (true), write packets in paced way. Default value is 0 (false). + @end table @subsection Examples diff --git a/libavformat/fifo.c b/libavformat/fifo.c index bdecf2d..81b7e9e 100644 --- a/libavformat/fifo.c +++ b/libavformat/fifo.c @@ -79,6 +79,12 @@ typedef struct FifoContext { /* Time to delay output, in microseconds */ uint64_t output_delay; + + /* If set to 1, write packets in paced way */ + int paced; + + /* Time to start */ + uint64_t start_time; } FifoContext; typedef struct FifoThreadContext { @@ -184,6 +190,14 @@ static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt) src_tb = avf->streams[s_idx]->time_base; dst_tb = avf2->streams[s_idx]->time_base; av_packet_rescale_ts(pkt, src_tb, dst_tb); + if (fifo->paced) { + uint64_t pts = av_rescale_q(pkt->dts, dst_tb, AV_TIME_BASE_Q); + uint64_t now = av_gettime_relative() - fifo->start_time; + av_assert0(now >= fifo->output_delay); + if (pts > now - fifo->output_delay) { + av_usleep(pts - (now - fifo->output_delay)); + } + } ret = av_write_frame(avf2, pkt); if (ret >= 0) @@ -515,6 +529,8 @@ static int fifo_init(AVFormatContext *avf) return AVERROR(ret); fifo->overflow_flag_lock_initialized = 1; + fifo->start_time = av_gettime_relative(); + return 0; } @@ -637,6 +653,9 @@ static const AVOption options[] = { {"output_delay", "Time to delay output, in microseconds", OFFSET(output_delay), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM}, + {"paced", "Write packets in paced way", OFFSET(paced), + AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, + {NULL}, };