From patchwork Mon Aug 22 10:47:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 37384 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:139a:b0:8f:1db5:eae2 with SMTP id w26csp1224620pzh; Mon, 22 Aug 2022 04:15:49 -0700 (PDT) X-Google-Smtp-Source: AA6agR7PwazHwiy1XysZAQtKmCSuH1Qj75XxgECUT7RCkf+ccNm7g1a/dHukywBng5BuatDd6CuF X-Received: by 2002:a05:6402:11cb:b0:43c:c7a3:ff86 with SMTP id j11-20020a05640211cb00b0043cc7a3ff86mr15927546edw.383.1661166949333; Mon, 22 Aug 2022 04:15:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661166949; cv=none; d=google.com; s=arc-20160816; b=atSWCXZjbCXUIJNaO0gSJUekBkXQHSLncF9JNA92FnkJ2ksKCsU37RgCqWXdp4h+UN L6eJYcgy8CFe2ZdTsbQNdeai5UqsmBMEfs/6VlOu00SqJzEmV3sSMUWlrJqgtk1o955T sWFHU+tFmGYpY68t4lJDIbYoYCFdWT60xkvVn7XSQu0fdVD0VtcfaxSM8WZGtEGMFWqR h1oRcAaSk8/c4BU8xO/BxDN0vVk3y37Y5y2/6IQDxsW+3/LQMhGr6ZyISvD1swSrxC8r rYviT38zH95Wak+qA225f+1l+MkXYw9wayv3q9Xl6Ofnow4tE23uzr2QNXfYztVaZd7b +Bgw== 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=ws6gE2GvFyiLkYLWQKOIYzpdwa7KTFURu/3f8zuzDCs=; b=PP8SiiRBsYH6DNOCOyrxioNMxDQa8tYASAUyhqgyT6eVZ7XLDmJJB9jozIVabJrjVK V+Kp3A9MrAnHYwK/6BFFCI88HDjvE5sX8+j/bVRpqOUBlKx10cWOzxTL/kye8KN4CYGG teDwxY9U139wfJbJpGpSvf+NIMZqdKfZy2z5b5fkuzP3MuzIun6HIUp6oyscsf4TNKeN 3O286IvwalCfilF2zZH1Cva+AEalvLHTBdHPvFTQMYlrlc6awYnITpArccDEJwAxSsFR kNylQghO08bTg0KsY4VpbTlzphJf/jmkStIEUlP3wCMejilH8an2naozVagwAc7h4fBd 5vOA== 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-20020a170906695000b00734b96e0c85si7534254ejs.299.2022.08.22.04.15.48; Mon, 22 Aug 2022 04:15:49 -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 02B8C68B9DD; Mon, 22 Aug 2022 14:15:45 +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 3A38168B98D for ; Mon, 22 Aug 2022 14:15:38 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B8F5E240D0E for ; Mon, 22 Aug 2022 13:15:37 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id hhNHHKFr3ToV for ; Mon, 22 Aug 2022 13:15:36 +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 88F5C240D03 for ; Mon, 22 Aug 2022 13:15:36 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 4C5B63A1E7C; Mon, 22 Aug 2022 13:15:26 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 22 Aug 2022 12:47:59 +0200 Message-Id: <20220822104759.20314-1-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] fftools/ffmpeg: stop accessing av_stream_get_parser() from the main 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: 6Wx35QGHJhS3 It races with the demuxing thread. Instead, send the information along with the demuxed packets. Ideally, the code should stop using the stream-internal parsing completely, but that requires considerably more effort. Fixes races, e.g. in: - fate-h264-brokensps-2580 - fate-h264-extradata-reload - fate-iv8-demux - fate-m4v-cfr - fate-m4v --- fftools/ffmpeg.c | 8 ++++++-- fftools/ffmpeg.h | 6 ++++++ fftools/ffmpeg_demux.c | 16 ++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index ef7177fc33..ff74534ad0 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2452,7 +2452,9 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo if (pkt && pkt->duration) { duration_dts = av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q); } else if(ist->dec_ctx->framerate.num != 0 && ist->dec_ctx->framerate.den != 0) { - int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict+1 : ist->dec_ctx->ticks_per_frame; + int ticks = ist->last_pkt_repeat_pict >= 0 ? + ist->last_pkt_repeat_pict + 1 : + ist->dec_ctx->ticks_per_frame; duration_dts = ((int64_t)AV_TIME_BASE * ist->dec_ctx->framerate.den * ticks) / ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame; @@ -2555,7 +2557,9 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo } else if (pkt->duration) { ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q); } else if(ist->dec_ctx->framerate.num != 0) { - int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame; + int ticks = ist->last_pkt_repeat_pict >= 0 ? + ist->last_pkt_repeat_pict + 1 : + ist->dec_ctx->ticks_per_frame; ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->framerate.den * ticks) / ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 921e579c32..27ef92654c 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -348,6 +348,12 @@ typedef struct InputStream { int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units) int wrap_correction_done; + // the value of AVCodecParserContext.repeat_pict from the AVStream parser + // for the last packet returned from ifile_get_packet() + // -1 if unknown + // FIXME: this is a hack, the avstream parser should not be used + int last_pkt_repeat_pict; + int64_t filter_in_rescale_delta_last; int64_t min_pts; /* pts with the smallest value in a current stream */ diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c index 6dfb5bb35b..6e89f5999a 100644 --- a/fftools/ffmpeg_demux.c +++ b/fftools/ffmpeg_demux.c @@ -32,6 +32,9 @@ typedef struct DemuxMsg { AVPacket *pkt; int looping; + + // repeat_pict from the demuxer-internal parser + int repeat_pict; } DemuxMsg; static void report_new_stream(InputFile *file, const AVPacket *pkt) @@ -114,7 +117,7 @@ static int seek_to_start(InputFile *ifile) return ret; } -static void ts_fixup(InputFile *ifile, AVPacket *pkt) +static void ts_fixup(InputFile *ifile, AVPacket *pkt, int *repeat_pict) { InputStream *ist = input_streams[ifile->ist_index + pkt->stream_index]; const int64_t start_time = ifile->ctx->start_time; @@ -167,6 +170,11 @@ static void ts_fixup(InputFile *ifile, AVPacket *pkt) if (pkt->dts != AV_NOPTS_VALUE) pkt->dts += duration; + + *repeat_pict = -1; + if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + av_stream_get_parser(ist->st)) + *repeat_pict = av_stream_get_parser(ist->st)->repeat_pict; } static void *input_thread(void *arg) @@ -231,7 +239,7 @@ static void *input_thread(void *arg) } } - ts_fixup(f, pkt); + ts_fixup(f, pkt, &msg.repeat_pict); msg.pkt = av_packet_alloc(); if (!msg.pkt) { @@ -352,6 +360,7 @@ int init_input_threads(void) int ifile_get_packet(InputFile *f, AVPacket **pkt) { + InputStream *ist; DemuxMsg msg; int ret; @@ -382,6 +391,9 @@ int ifile_get_packet(InputFile *f, AVPacket **pkt) if (msg.looping) return 1; + ist = input_streams[f->ist_index + msg.pkt->stream_index]; + ist->last_pkt_repeat_pict = msg.repeat_pict; + *pkt = msg.pkt; return 0; }