From patchwork Tue Feb 7 09:33:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40313 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:5494:b0:bf:7b3a:fd32 with SMTP id i20csp4130049pzk; Tue, 7 Feb 2023 01:33:33 -0800 (PST) X-Google-Smtp-Source: AK7set9JOnmpaJ4PZRUQESf/QQiPs3MJ9Pl0AO6ETjOVTv/sw/QKNkRtys4krMj/g3aPsXqugqV5 X-Received: by 2002:a50:999e:0:b0:4a3:f9db:c688 with SMTP id m30-20020a50999e000000b004a3f9dbc688mr2659979edb.42.1675762413414; Tue, 07 Feb 2023 01:33:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675762413; cv=none; d=google.com; s=arc-20160816; b=BG2KcVl4+bDNgWIHjqwvyrrolyVgoZGtIRs2F5mccG4y1q44OmeThX3EYkLIN8Xcw4 qU8KkbYgTBB+r2ccIqVu/dyqqfQLUqOOsp1QwvvBhjIF37gmjen8WFiC8Na9fGWyv9kP j/c9tSzltHrEo7GihocpJrOuy2e9pvWnYdILYTD5kRfNtIu54nqBoDnmzWsShYKdTHha 5QSk3pc10QRIdLqw2hDo9ewCFwEsQ4YIm3epFEMP4Av4aOOmeFE74GmTVQEVXFHp7Wl1 cH8bKxke2I1bxAl3c1LX5n2Okx6lkCPj02D5HznMJ/AL0h5UA0s9T972NUVoUmzNruNN 5vfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=+zWX6NXqNAwaee2KgTKkVeaK8Wxac0uSbwVf6FDY80k=; b=bHmoXoG4zi/cFxiiGzKlxr6uOAlVsxXdPntNbdP5j+N2tx7V886Mx2WyrYNNAv+EFy xtDp/GrwghcQLkCWTDHc7fNXBEdYYPLRN1V5CQvLlvcNPCMeN1saf5gxmK7WE2FZJYpd 8zpeL8it39UarxMfQ91ewqUnnkcH1ULmJB+hskG1dRon4CWrNZjZk8gRCzg0pjh5wAf6 798zF4iUyiK29TvU6mPgKAqoLfkrQP9wAWEVFOypE+49jiF9vf8tVcCb5gnOuvNn+Xda hwEvWspnSdloc4tKPw30NDmg5nySz/v6GaAzSzR2k0+sUV875cXc5dBFWTXZFM1kTc39 U8Ew== ARC-Authentication-Results: i=1; mx.google.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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id c16-20020aa7df10000000b004aa694bbc43si11636208edy.68.2023.02.07.01.33.32; Tue, 07 Feb 2023 01:33:33 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1F92668BEF5; Tue, 7 Feb 2023 11:33:29 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F1E2768BEC5 for ; Tue, 7 Feb 2023 11:33:21 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id E3CCC2404F5 for ; Tue, 7 Feb 2023 10:33:20 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id kHTIIY5nnQ7Y for ; Tue, 7 Feb 2023 10:33:20 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 033822404EC for ; Tue, 7 Feb 2023 10:33:20 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id B0DFF3A0101 for ; Tue, 7 Feb 2023 10:33:19 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 7 Feb 2023 10:33:05 +0100 Message-Id: <20230207093306.11959-1-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] fftools/ffmpeg: store output packet timebases in the packet X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: iWbgMeN8+cxi Useful to keep track of what timebase the packet's timestamps are in. --- fftools/ffmpeg.c | 33 ++++++++++++++++++++------------- fftools/ffmpeg_mux.c | 7 ++++--- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 743bc0c6b6..32e0c3febd 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -797,7 +797,7 @@ static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write fprintf(vstats_file,"f_size= %6d ", pkt->size); /* compute pts value */ - ti1 = pkt->dts * av_q2d(ost->mux_timebase); + ti1 = pkt->dts * av_q2d(pkt->time_base); if (ti1 < 0.01) ti1 = 0.01; @@ -812,7 +812,7 @@ static void enc_stats_write(OutputStream *ost, EncStats *es, const AVFrame *frame, const AVPacket *pkt) { AVIOContext *io = es->io; - AVRational tb = ost->enc_ctx->time_base; + AVRational tb = frame ? frame->time_base : pkt->time_base; int64_t pts = frame ? frame->pts : pkt->pts; AVRational tbi = (AVRational){ 0, 1}; @@ -912,6 +912,8 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) update_benchmark("%s_%s %d.%d", action, type_desc, ost->file_index, ost->index); + pkt->time_base = enc->time_base; + /* if two pass, output log on success and EOF */ if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out) fprintf(ost->logfile, "%s", enc->stats_out); @@ -942,7 +944,8 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); } - av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); + av_packet_rescale_ts(pkt, pkt->time_base, ost->mux_timebase); + pkt->time_base = ost->mux_timebase; if (debug_ts) { av_log(ost, AV_LOG_INFO, "encoder -> type:%s " @@ -1103,17 +1106,19 @@ static void do_subtitle_out(OutputFile *of, } av_shrink_packet(pkt, subtitle_out_size); - 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); + pkt->time_base = ost->mux_timebase; + pkt->pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, pkt->time_base); + pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base); 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 */ if (i == 0) - pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase); + pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base); else - pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase); + pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base); } pkt->dts = pkt->pts; + of_output_packet(of, pkt, ost, 0); } } @@ -1900,25 +1905,27 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p if (av_packet_ref(opkt, pkt) < 0) exit_program(1); + opkt->time_base = ost->mux_timebase; + if (pkt->pts != AV_NOPTS_VALUE) - opkt->pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time; + opkt->pts = av_rescale_q(pkt->pts, ist->st->time_base, opkt->time_base) - ost_tb_start_time; if (pkt->dts == AV_NOPTS_VALUE) { - opkt->dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->mux_timebase); + opkt->dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, opkt->time_base); } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { int duration = av_get_audio_frame_duration2(ist->par, pkt->size); if(!duration) duration = ist->par->frame_size; opkt->dts = av_rescale_delta(ist->st->time_base, pkt->dts, (AVRational){1, ist->par->sample_rate}, duration, - &ist->filter_in_rescale_delta_last, ost->mux_timebase); + &ist->filter_in_rescale_delta_last, opkt->time_base); /* dts will be set immediately afterwards to what pts is now */ opkt->pts = opkt->dts - ost_tb_start_time; } else - opkt->dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase); + opkt->dts = av_rescale_q(pkt->dts, ist->st->time_base, opkt->time_base); opkt->dts -= ost_tb_start_time; - opkt->duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase); + opkt->duration = av_rescale_q(pkt->duration, ist->st->time_base, opkt->time_base); { int ret = trigger_fix_sub_duration_heartbeat(ost, pkt); @@ -2475,7 +2482,7 @@ static int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts) static int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt) { OutputFile *of = output_files[ost->file_index]; - int64_t signal_pts = av_rescale_q(pkt->pts, ost->mux_timebase, + int64_t signal_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q); if (!ost->fix_sub_duration_heartbeat || !(pkt->flags & AV_PKT_FLAG_KEY)) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 5d427b44ea..30764e22d1 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -81,11 +81,12 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt) if (pkt->duration > 0) av_log(ost, 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->mux_timebase); + pkt->time_base); } } - av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base); + av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base); + pkt->time_base = ost->st->time_base; if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { if (pkt->dts != AV_NOPTS_VALUE && @@ -325,7 +326,7 @@ void of_output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) int ret = 0; if (!eof && pkt->dts != AV_NOPTS_VALUE) - ost->last_mux_dts = av_rescale_q(pkt->dts, ost->mux_timebase, AV_TIME_BASE_Q); + ost->last_mux_dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q); /* apply the output bitstream filters */ if (ms->bsf_ctx) {