From patchwork Wed Aug 10 16:25:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 37219 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3d0d:b0:8d:a68e:8a0e with SMTP id y13csp1012863pzi; Wed, 10 Aug 2022 09:27:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR5ETIWb4uZd05xfPEzAOMhobFaEXNlJBdawuZULOPAwD2V5SMuIGbdWGrq74OkALa3+c0a4 X-Received: by 2002:a17:907:3f02:b0:6f3:5c42:321a with SMTP id hq2-20020a1709073f0200b006f35c42321amr20840139ejc.521.1660148857633; Wed, 10 Aug 2022 09:27:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660148857; cv=none; d=google.com; s=arc-20160816; b=l4YoyGWZlUtFgBIFjZ0XiYZtI6kIR2wXX/AN96ft3VJK0wE8N7bcAGlp34ZZN7N7+k GfFAzukF0W4S9N677BjwXICgTgKnm4ng5uvrLdnd30xpH8zTA4OrWCs9K0nCCrTVFlXA /139i/BiTOBj8v50jfDGiA6GmB9FzBhJNLZt2NJtK8cYh4WDSb++ChiPcX0rRRZ9m4vJ FGYJEtC5CkP/l3JA6wspHRQrFZHMw5JsQtD9TOaT0gJaMkb8S7lv62QHUNWx2qAqes6a /PZQvb1I6p8aDEEpFeALzPg5nilPcGdFhnWE2r+UPnbPAT3yAN89N4zUVswXIEd5cQ9A CaOQ== 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:references:in-reply-to:message-id :date:to:from:delivered-to; bh=ixBB7cgF5Aucz1Et0SVBHn2asGkd1786RLhrCZAc/0g=; b=RMT2eWViFR6X8Yy7qjwSdOrX46fiteLuJJSbgm0UOPQYHY9PKv0Id9leODjNT83syM u2qzCG45BHKnvpPLulXFVh0m59uwQ0fcjRRjswTVy+y0OQ9i21vmUL+/+z2RhKN/Tqj4 YHTapuyDRMxaNmZr/DFdyy6yvIaQY/ThJbKz94O2eDVMHbTZLpMhdZ/yddNYG8cx9XVp 31kSTGh0KU/JSFebm29s/xQrFCO/wafm4gCGWwsiwpZIE/e1YPLaR0B9Oq/7CITclsFp KQbMOSZ9rmL5mIVeaMvLk9+KIQPJLgTSKfW4A0NfMIAO7HD/Jpg+bA3knRm1/qZwgj3I 1W6A== 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 bx1-20020a0564020b4100b0043e2f1b306esi10503327edb.215.2022.08.10.09.27.37; Wed, 10 Aug 2022 09:27:37 -0700 (PDT) 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 8087868B8E9; Wed, 10 Aug 2022 19:26:14 +0300 (EEST) 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 0567268B8B1 for ; Wed, 10 Aug 2022 19:26:04 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 8109824050B for ; Wed, 10 Aug 2022 18:26:00 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 7B0mnfta-PRF for ; Wed, 10 Aug 2022 18:25:59 +0200 (CEST) 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 59FE5240511 for ; Wed, 10 Aug 2022 18:25:57 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 4F4873A0470; Wed, 10 Aug 2022 18:25:57 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 10 Aug 2022 18:25:45 +0200 Message-Id: <20220810162545.21382-9-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220810162545.21382-1-anton@khirnov.net> References: <20220810162545.21382-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 9/9] fftools/ffmpeg: move packet timestamp processing to demuxer thread 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: 8T/1+6yd/z9e Discontinuity detection/correction is left in the main thread, as it is entangled with InputStream.next_dts and related variables, which may be set by decoding code. Fixes races e.g. in fate-ffmpeg-streamloop after aae9de0cb2887e6e0bbfda6ffdf85ab77d3390f0. --- fftools/ffmpeg.c | 52 -------------------------------------- fftools/ffmpeg_demux.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 8d85af1c97..71a7e0b837 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3791,7 +3791,6 @@ static int process_input(int file_index) InputStream *ist; AVPacket *pkt; int ret, i, j; - int64_t duration; is = ifile->ctx; ret = ifile_get_packet(ifile, &pkt); @@ -3846,37 +3845,6 @@ static int process_input(int file_index) if (ist->discard) goto discard_packet; - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s " - "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n", - ifile->ist_index + pkt->stream_index, - av_get_media_type_string(ist->st->codecpar->codec_type), - av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q), - av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q), - av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base), - av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base), - av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base), - av_ts2str(input_files[ist->file_index]->ts_offset), - av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q)); - } - - if(!ist->wrap_correction_done && is->start_time != AV_NOPTS_VALUE && ist->st->pts_wrap_bits < 64){ - int64_t stime, stime2; - - stime = av_rescale_q(is->start_time, AV_TIME_BASE_Q, ist->st->time_base); - stime2= stime + (1ULL<st->pts_wrap_bits); - ist->wrap_correction_done = 1; - - if(stime2 > stime && pkt->dts != AV_NOPTS_VALUE && pkt->dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) { - pkt->dts -= 1ULL<st->pts_wrap_bits; - ist->wrap_correction_done = 0; - } - if(stime2 > stime && pkt->pts != AV_NOPTS_VALUE && pkt->pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) { - pkt->pts -= 1ULL<st->pts_wrap_bits; - ist->wrap_correction_done = 0; - } - } - /* add the stream-global side data to the first packet */ if (ist->nb_packets == 1) { for (i = 0; i < ist->st->nb_side_data; i++) { @@ -3897,26 +3865,6 @@ static int process_input(int file_index) } } - if (pkt->dts != AV_NOPTS_VALUE) - pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base); - if (pkt->pts != AV_NOPTS_VALUE) - pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base); - - if (pkt->pts != AV_NOPTS_VALUE) - pkt->pts *= ist->ts_scale; - if (pkt->dts != AV_NOPTS_VALUE) - pkt->dts *= ist->ts_scale; - - duration = av_rescale_q(ifile->duration, ifile->time_base, ist->st->time_base); - if (pkt->pts != AV_NOPTS_VALUE) { - pkt->pts += duration; - ist->max_pts = FFMAX(pkt->pts, ist->max_pts); - ist->min_pts = FFMIN(pkt->pts, ist->min_pts); - } - - if (pkt->dts != AV_NOPTS_VALUE) - pkt->dts += duration; - // detect and try to correct for timestamp discontinuities ts_discontinuity_process(ifile, ist, pkt); diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c index d15cee614d..6dfb5bb35b 100644 --- a/fftools/ffmpeg_demux.c +++ b/fftools/ffmpeg_demux.c @@ -114,6 +114,61 @@ static int seek_to_start(InputFile *ifile) return ret; } +static void ts_fixup(InputFile *ifile, AVPacket *pkt) +{ + InputStream *ist = input_streams[ifile->ist_index + pkt->stream_index]; + const int64_t start_time = ifile->ctx->start_time; + int64_t duration; + + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s " + "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n", + ifile->ist_index + pkt->stream_index, + av_get_media_type_string(ist->st->codecpar->codec_type), + av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base), + av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base), + av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base)); + } + + if (!ist->wrap_correction_done && start_time != AV_NOPTS_VALUE && + ist->st->pts_wrap_bits < 64) { + int64_t stime, stime2; + + stime = av_rescale_q(start_time, AV_TIME_BASE_Q, ist->st->time_base); + stime2= stime + (1ULL<st->pts_wrap_bits); + ist->wrap_correction_done = 1; + + if(stime2 > stime && pkt->dts != AV_NOPTS_VALUE && pkt->dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) { + pkt->dts -= 1ULL<st->pts_wrap_bits; + ist->wrap_correction_done = 0; + } + if(stime2 > stime && pkt->pts != AV_NOPTS_VALUE && pkt->pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) { + pkt->pts -= 1ULL<st->pts_wrap_bits; + ist->wrap_correction_done = 0; + } + } + + if (pkt->dts != AV_NOPTS_VALUE) + pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base); + if (pkt->pts != AV_NOPTS_VALUE) + pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base); + + if (pkt->pts != AV_NOPTS_VALUE) + pkt->pts *= ist->ts_scale; + if (pkt->dts != AV_NOPTS_VALUE) + pkt->dts *= ist->ts_scale; + + duration = av_rescale_q(ifile->duration, ifile->time_base, ist->st->time_base); + if (pkt->pts != AV_NOPTS_VALUE) { + pkt->pts += duration; + ist->max_pts = FFMAX(pkt->pts, ist->max_pts); + ist->min_pts = FFMIN(pkt->pts, ist->min_pts); + } + + if (pkt->dts != AV_NOPTS_VALUE) + pkt->dts += duration; +} + static void *input_thread(void *arg) { InputFile *f = arg; @@ -176,6 +231,8 @@ static void *input_thread(void *arg) } } + ts_fixup(f, pkt); + msg.pkt = av_packet_alloc(); if (!msg.pkt) { av_packet_unref(pkt);