From patchwork Fri Feb 10 12:35:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wm4 X-Patchwork-Id: 2474 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp473772vsb; Fri, 10 Feb 2017 04:36:11 -0800 (PST) X-Received: by 10.223.142.208 with SMTP id q74mr7697269wrb.101.1486730171780; Fri, 10 Feb 2017 04:36:11 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z20si1027363wmz.148.2017.02.10.04.36.11; Fri, 10 Feb 2017 04:36:11 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@googlemail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B7953689DC6; Fri, 10 Feb 2017 14:35:50 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f175.google.com (mail-wr0-f175.google.com [209.85.128.175]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6E44968990D for ; Fri, 10 Feb 2017 14:35:44 +0200 (EET) Received: by mail-wr0-f175.google.com with SMTP id k90so106621756wrc.3 for ; Fri, 10 Feb 2017 04:35:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PQP0kfHrpxNyp3OfEhT6VUCY4p9BCztxOFH13wqsCV8=; b=aKr9s9T9tvXVamo8HdApUa/necHnHLoTv/d2B1Z6fcLU6EFW9arTEvlwGAPExwf/KS cmwJUphk0XtuezOzdCqwIO9IFASpPOOCXe7JdwM5egZSklRX1vjqn4kL6f3o5i2QU42X JXiYoS1VMNQfRa64W50/ebrKnITT2haPjwbfSwTvL0pnTSkzT+5SpojED6FBsaE863xV ZX2WKghfX/IapeRRzTGUrqT3ZkBLSJI/CbleZZp3V0JC60xUE6VwJwJoz3ab3onsRq2d qPwhdvsKjeefEFrvl0ieUhbLjJS7oNfiUWhFUQ9jZgvxGp0LiGNInk5n7T0gf63+cMCB E5Bg== 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=PQP0kfHrpxNyp3OfEhT6VUCY4p9BCztxOFH13wqsCV8=; b=tU71TgdwAAdCm/tKuwvw8DEWHhvct3xw8CprQipFjDKnqdLw4D9JoZcVMlWv1P+Jb4 tTO5XfL8b7uUcMcG+s+WpODkjrp2mSXzUZ5W3nqwrEiKTFamY3H6TSuaDfNNN3j7KHCx du0oviCVwNmsH1oLYWpRynhF6wFEZqOs7IN+Okx0N9uC67ia2+eI1HiXKQImwhGtha08 lhsK6JmhJwoNPh4NdCnl36hopV6NvwfCmM95LC8qEp8J9d5BxMiZoTkKRIxVIWKAS8Di EwfmPp59ynT4wyTOCK+ejKfL65TbYU6CMKepvquar4e9p1a3hj3TKd01rCY9esgmiXf4 Jv7A== X-Gm-Message-State: AMke39nlkeQ88of6rltWo4oFXq96QpTxr3IvWXsQWvJJX8Yf7TWO5rwHG20dssFM9tolAQ== X-Received: by 10.223.139.213 with SMTP id w21mr7775135wra.108.1486730147823; Fri, 10 Feb 2017 04:35:47 -0800 (PST) Received: from localhost.localdomain (p4FF029FA.dip0.t-ipconnect.de. [79.240.41.250]) by smtp.googlemail.com with ESMTPSA id s18sm1413656wmb.18.2017.02.10.04.35.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 04:35:47 -0800 (PST) From: wm4 To: ffmpeg-devel@ffmpeg.org Date: Fri, 10 Feb 2017 13:35:34 +0100 Message-Id: <20170210123541.32375-3-nfxjfg@googlemail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170210123541.32375-1-nfxjfg@googlemail.com> References: <20170210123541.32375-1-nfxjfg@googlemail.com> Subject: [FFmpeg-devel] [PATCH 2/9] ffmpeg: do packet ts rescaling in write_packet() 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: wm4 , Anton Khirnov MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Anton Khirnov This will be useful in the following commit, after which the muxer timebase is not always available when encoding. This merges Libav commit 3e265ca. It was previously skipped. There is a minor change with setting the mux_timebase field only after the muxer's write_header function has been called, because it can readjust the timebase. Includes a minor merge fix by Mark Thompson. Signed-off-by: wm4 --- ffmpeg.c | 39 ++++++++++++++++++++++----------------- ffmpeg.h | 2 ++ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/ffmpeg.c b/ffmpeg.c index 913c18d92b..8b829beb34 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -713,7 +713,7 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) if (pkt->duration > 0) av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n"); pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate), - ost->st->time_base); + ost->mux_timebase); } } @@ -759,6 +759,8 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) pkt->stream_index = ost->index; + av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base); + if (debug_ts) { av_log(NULL, AV_LOG_INFO, "muxer <- type:%s " "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n", @@ -907,13 +909,13 @@ static void do_audio_out(OutputFile *of, OutputStream *ost, update_benchmark("encode_audio %d.%d", ost->file_index, ost->index); - av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base); + av_packet_rescale_ts(&pkt, enc->time_base, ost->mux_timebase); if (debug_ts) { av_log(NULL, AV_LOG_INFO, "encoder -> type:audio " "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n", - av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base), - av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base)); + av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &enc->time_base), + av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &enc->time_base)); } output_packet(of, &pkt, ost); @@ -993,8 +995,8 @@ static void do_subtitle_out(OutputFile *of, av_init_packet(&pkt); pkt.data = subtitle_out; pkt.size = subtitle_out_size; - pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base); - pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->st->time_base); + pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase); + pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase); if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) { /* XXX: the pts correction is handled here. Maybe handling it in the codec would be better */ @@ -1187,7 +1189,7 @@ static void do_video_out(OutputFile *of, mux_par->field_order = AV_FIELD_PROGRESSIVE; pkt.data = (uint8_t *)in_picture; pkt.size = sizeof(AVPicture); - pkt.pts = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base); + pkt.pts = av_rescale_q(in_picture->pts, enc->time_base, ost->mux_timebase); pkt.flags |= AV_PKT_FLAG_KEY; output_packet(of, &pkt, ost); @@ -1283,13 +1285,13 @@ static void do_video_out(OutputFile *of, if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & AV_CODEC_CAP_DELAY)) pkt.pts = ost->sync_opts; - av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base); + av_packet_rescale_ts(&pkt, enc->time_base, ost->mux_timebase); if (debug_ts) { av_log(NULL, AV_LOG_INFO, "encoder -> type:video " "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n", - av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base), - av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base)); + av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->mux_timebase), + av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->mux_timebase)); } frame_size = pkt.size; @@ -1862,8 +1864,8 @@ static void flush_encoders(void) av_packet_unref(&pkt); continue; } - av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base); pkt_size = pkt.size; + av_packet_rescale_ts(&pkt, enc->time_base, ost->mux_timebase); output_packet(of, &pkt, ost); if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) { do_video_stats(ost, pkt_size); @@ -1897,7 +1899,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p OutputFile *of = output_files[ost->file_index]; InputFile *f = input_files [ist->file_index]; int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time; - int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->st->time_base); + int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase); AVPicture pict; AVPacket opkt; @@ -1938,14 +1940,14 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p ost->sync_opts++; if (pkt->pts != AV_NOPTS_VALUE) - opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time; + opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time; else opkt.pts = AV_NOPTS_VALUE; if (pkt->dts == AV_NOPTS_VALUE) - opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->st->time_base); + opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->mux_timebase); else - opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base); + opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase); opkt.dts -= ost_tb_start_time; if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && pkt->dts != AV_NOPTS_VALUE) { @@ -1954,10 +1956,11 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p duration = ist->dec_ctx->frame_size; opkt.dts = opkt.pts = av_rescale_delta(ist->st->time_base, pkt->dts, (AVRational){1, ist->dec_ctx->sample_rate}, duration, &ist->filter_in_rescale_delta_last, - ost->st->time_base) - ost_tb_start_time; + ost->mux_timebase) - ost_tb_start_time; } - opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base); + opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase); + opkt.flags = pkt->flags; // FIXME remove the following 2 lines they shall be replaced by the bitstream filters if ( ost->st->codecpar->codec_id != AV_CODEC_ID_H264 @@ -3410,6 +3413,8 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len) if (ret < 0) return ret; + ost->mux_timebase = ost->st->time_base; + return ret; } diff --git a/ffmpeg.h b/ffmpeg.h index 458bb8a3dc..ca35ccc260 100644 --- a/ffmpeg.h +++ b/ffmpeg.h @@ -446,6 +446,8 @@ typedef struct OutputStream { int64_t first_pts; /* dts of the last packet sent to the muxer */ int64_t last_mux_dts; + // the timebase of the packets sent to the muxer + AVRational mux_timebase; int nb_bitstream_filters; uint8_t *bsf_extradata_updated;