From patchwork Thu Aug 18 13:46:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 37351 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:139a:b0:8f:1db5:eae2 with SMTP id w26csp323315pzh; Thu, 18 Aug 2022 06:46:56 -0700 (PDT) X-Google-Smtp-Source: AA6agR68riZhabPSF1xRteYOGA7e030g7s/SRkh/eUtsdATxVrKCkTmjocYorTGIgl1eW5vkaMsa X-Received: by 2002:a17:906:845c:b0:730:bbf1:196a with SMTP id e28-20020a170906845c00b00730bbf1196amr1984895ejy.13.1660830416023; Thu, 18 Aug 2022 06:46:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660830416; cv=none; d=google.com; s=arc-20160816; b=NM0Ir+zGJlv0W9b6WmHbYNfUOj8kMAV+19XLrT8dKmbH8hnWFYImzXz6sMPh/B+ihc hApgdGNgzSunqjsNTDDOVmgf1uUUfltfVrtcvPQm5tsO4DERSLbgwHEyeTlMIBfPBfGS 0Q+8/hP0ZNFFi025wQxvj723L/KpY6BNcogmp6FYxSSAYM4fBT1ezrv6J2BNHYog+zRZ njRSyqIVTw97FULesymOcvtVVdhxcxbLykTNxiSbF83TFWMnkirHuEqlIHrnSHlU8JCp uRTttT1oFT8vrag+OdS7XB9TrntnbWrLi5F2BWtoaQinxW4LQtuLBt8MbUN4PLXjCEJB Oaug== 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=v7eTXZ6SnhE/yzp7YU6UAEkujoXe4XpTLAt8JYZjCuw=; b=J1n6flk/70rQgh1OO1tm3Wobs8Yee6idLDsv2xcCJ7KHiaXmyHx6Iftp1by+VvBh+2 BQn3OudFQvqv4FTchawfNlh2Jh47WZfDUNLvXq0DcCS6gYQ2QQkvwosnmAmRqs08PKZE ikiyxg+z7RZio6Mj5smTHFLD5rcMuUYvhg3UTXb0sZTYwpjKIWfTl9U60rnQmM05FH9t hLr01BRokdZ1IdtIVoAgPNmaus8Cc7Xm3V6L7Y+cKhjDMUB91Gs3XD0Qh60HgJhRYqAR eKM+FS4jkZnLBryLLr5PplD5aLkq5IYNwCQMZ2nXeVGUlz/cD2Vm26PDqu884LjIZHnX z6eQ== 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 c14-20020a0564021f8e00b00445db771c57si1091410edc.133.2022.08.18.06.46.54; Thu, 18 Aug 2022 06:46:56 -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 9E59468B9AE; Thu, 18 Aug 2022 16:46:24 +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 A11BB68B972 for ; Thu, 18 Aug 2022 16:46:15 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 031FF240506 for ; Thu, 18 Aug 2022 15:46:15 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id bBhNZKv-OZCo for ; Thu, 18 Aug 2022 15:46:14 +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 6A41724050B for ; Thu, 18 Aug 2022 15:46:12 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 4A7E13A0520; Thu, 18 Aug 2022 15:46:06 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 18 Aug 2022 15:46:03 +0200 Message-Id: <20220818134605.12583-2-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220818134605.12583-1-anton@khirnov.net> References: <20220818134605.12583-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] fftools/ffmpeg: stop using av_stream_get_parser() 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: sqeBlDLjNlTM The parser is internal to the demuxer, so its state at any particular point is not well-defined for the caller. Additionally, it is being accessed from the main thread, while demuxing runs in a separate thread. Use a separate parser owned by ffmpeg.c to retrieve the same information. Fixes races, e.g. in: - fate-h264-brokensps-2580 - fate-h264-extradata-reload - fate-iv8-demux - fate-m4v-cfr - fate-m4v --- fftools/ffmpeg.c | 33 +++++++++++++++++++++++++++++++-- fftools/ffmpeg.h | 9 +++++++++ fftools/ffmpeg_opt.c | 9 +++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index ef7177fc33..580df0443a 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -610,6 +610,9 @@ static void ffmpeg_cleanup(int ret) avcodec_free_context(&ist->dec_ctx); avcodec_parameters_free(&ist->par); + avcodec_free_context(&ist->parser_dec); + av_parser_close(ist->parser); + av_freep(&input_streams[i]); } @@ -2421,6 +2424,15 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo ret = av_packet_ref(avpkt, pkt); if (ret < 0) return ret; + + if (ist->parser) { + // we do not use the parsed output, only the + // filled codec context fields + uint8_t *dummy; + int dummy_size; + av_parser_parse2(ist->parser, ist->parser_dec, &dummy, &dummy_size, + pkt->data, pkt->size, pkt->pts, pkt->dts, pkt->pos); + } } if (pkt && pkt->dts != AV_NOPTS_VALUE) { @@ -2452,7 +2464,8 @@ 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->parser ? ist->parser->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 +2568,8 @@ 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->parser ? ist->parser->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; @@ -2688,6 +2702,21 @@ static int init_input_stream(int ist_index, char *error, int error_len) assert_avoptions(ist->decoder_opts); } + if (ist->parser) { + AVCodecParameters *par_tmp; + + par_tmp = avcodec_parameters_alloc(); + if (!par_tmp) + return AVERROR(ENOMEM); + + ret = avcodec_parameters_from_context(par_tmp, ist->dec_ctx); + if (ret >= 0) + ret = avcodec_parameters_to_context(ist->parser_dec, par_tmp); + avcodec_parameters_free(&par_tmp); + if (ret < 0) + return ret; + } + ist->next_pts = AV_NOPTS_VALUE; ist->next_dts = AV_NOPTS_VALUE; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 44cc23fa84..f67a2f1d1d 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -334,6 +334,15 @@ typedef struct InputStream { AVFrame *decoded_frame; AVPacket *pkt; + /** + * Parser for timestamp processing. + */ + AVCodecParserContext *parser; + /** + * Codec context needed by parsing. + */ + AVCodecContext *parser_dec; + int64_t prev_pkt_pts; int64_t start; /* time when read started */ /* predicted dts of the next packet read for this stream or (when there are diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 30ca5cd609..a505c7b26f 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1065,6 +1065,15 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic) ist->top_field_first = -1; MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st); + ist->parser = av_parser_init(ist->dec->id); + if (ist->parser) { + ist->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; + + ist->parser_dec = avcodec_alloc_context3(NULL); + if (!ist->parser_dec) + exit_program(1); + } + break; case AVMEDIA_TYPE_AUDIO: ist->guess_layout_max = INT_MAX;