From patchwork Wed Feb 15 09:24:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wm4 X-Patchwork-Id: 2553 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp1866611vsb; Wed, 15 Feb 2017 01:24:44 -0800 (PST) X-Received: by 10.223.129.163 with SMTP id 32mr27667388wra.140.1487150684784; Wed, 15 Feb 2017 01:24:44 -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 z40si4280948wrz.203.2017.02.15.01.24.44; Wed, 15 Feb 2017 01:24:44 -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 1A0CF689A3C; Wed, 15 Feb 2017 11:24:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f194.google.com (mail-wr0-f194.google.com [209.85.128.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B203E689A37 for ; Wed, 15 Feb 2017 11:24:14 +0200 (EET) Received: by mail-wr0-f194.google.com with SMTP id i10so30472589wrb.0 for ; Wed, 15 Feb 2017 01:24:20 -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=ZHmL5yqy0l+XI+cxDdub/x8Csk+sNFWtX4tVVGpCFgM=; b=IB0BJWyoo/ddhNwqL2qUoraomqEgntbVzG9ROCA2Ppwb79asdb12RlpLjRFxRHehry +mLjzLKSCoP8h7g8Yw+zClrDj1TW9LUiYw4b9Uae9wN5kKN258vuHvvyXUtoVet9OHlw nH8NffxmtXFeWwnHmLxjFFlCJFpco1nqkKGxw3v/VPu6xUzwx7x0CIKA6rcMAdg7doEj KcFtWelAEU0yDjne4Pv2TftjhJNiE8phzrkqlwXj9T+ulHA83eqvPQKtvPQ5OUJJ86Ws HLJOO4iScv2rmlV+IpwvfNtFypJ+7EHU3u8dibENW0aqhfL3wMJ0u7j9w2yAlXsLymGu 5Zug== 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=ZHmL5yqy0l+XI+cxDdub/x8Csk+sNFWtX4tVVGpCFgM=; b=TTVPmGweOTnjD6VuY/Xx4dkhZjjaTM/SFJRdL3L+6Dwd+6YIiSegV+r8bhKn494F5N MCcpE9UGyUHTgrGlyD2NpkZsuqG8ov2MnUBUw7d47Lag90rj8NZARIPSTFRXtNtrPjcO 7F8RrHF2JfNLj1n1ZwLIeJZDQTjwlb2HDHW8R6ui12QxG4rr4KuwAy8TXe1F3NbNDhTG vXKe49eZH9LFLKiFg8HAv9gqIy2tQCzXHT7e1882vJJwp3umhYH7zLHC6MQI0+1x2Xny Fy44/Eb5nDhhtlfYvjKbs/rbMznXLY9YaU8+G6IPFhi/KxvPm8hcIyma+6nWhS0yrd03 msFg== X-Gm-Message-State: AMke39lM3xH9gI9YIMb3bJKsh31fODLrs2MvNOIaHPdPv5yyNlHwYbSVMKRMfeS+vZn4/A== X-Received: by 10.223.170.3 with SMTP id p3mr32172910wrd.100.1487150659819; Wed, 15 Feb 2017 01:24:19 -0800 (PST) Received: from localhost.localdomain (p4FF0242F.dip0.t-ipconnect.de. [79.240.36.47]) by smtp.googlemail.com with ESMTPSA id u189sm6787730wmu.1.2017.02.15.01.24.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Feb 2017 01:24:19 -0800 (PST) From: wm4 To: ffmpeg-devel@ffmpeg.org Date: Wed, 15 Feb 2017 10:24:17 +0100 Message-Id: <20170215092423.5358-3-nfxjfg@googlemail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170215092423.5358-1-nfxjfg@googlemail.com> References: <20170210123541.32375-1-nfxjfg@googlemail.com> <20170215092423.5358-1-nfxjfg@googlemail.com> Subject: [FFmpeg-devel] [PATCH v2 2/8] 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, and avconv: Move rescale to stream timebase before monotonisation also 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..2876e43b32 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -713,10 +713,12 @@ 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); } } + av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base); + if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && @@ -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;