From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 89CA3491E1 for ; Mon, 5 Feb 2024 17:04:38 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 14D6268D16E; Mon, 5 Feb 2024 19:04:36 +0200 (EET) Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5B84868C69B for ; Mon, 5 Feb 2024 19:04:29 +0200 (EET) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=LBGRMb/p; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 7B03519B9 for ; Mon, 5 Feb 2024 18:04:28 +0100 (CET) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id 1RsK7jJ7SoOA for ; Mon, 5 Feb 2024 18:04:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1707152664; bh=JGhYduJyiubsbolEt0xKbaRUJeb10znuw6J+6A/1Vdw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=LBGRMb/puVSZzUEcyQN1vXNsdX+2ZsvAG5Vow9vY3Uf3udM7GxW/5lwsORJhKhYfa 3V8DGN5uZu19H3K+jEIBbw10y1vWlCAnzuYXduCmTOn7jfIA54rmEAUUCWLKW20bwX Z0qmD8lVOpm6Uqt+c88Iig4dOMsvB/J91NYPy7jX1F2eIr17XWsa1ATOyb/LBc4PFO dWNGxZM1WjWb7RTKpfoXhElc5QfpVgO4zxqGQA9QqeCedWlm2RaLPlTWwdcm9BquYX jlD3DLiEAq8OKZRyNe5NRoLbLPDbAQiLAIXpjn6e4Ee1hqduQdYIOx0W2VCdaGOo5H OEazB+7jWV9Dg== 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 mail1.khirnov.net (Postfix) with ESMTPS id 8683158B for ; Mon, 5 Feb 2024 18:04:24 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 2FEC73A0435 for ; Mon, 5 Feb 2024 18:04:16 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 5 Feb 2024 18:03:54 +0100 Message-ID: <20240205170415.20391-1-anton@khirnov.net> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240201082938.16687-3-anton@khirnov.net> References: <20240201082938.16687-3-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 3/4] lavf/demux: stop calling avcodec_close() 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Replace it with recreating the codec context. This is the last remaining blocker for deprecating avcodec_close(). --- Applied review comments. --- libavformat/demux.c | 61 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/libavformat/demux.c b/libavformat/demux.c index 6f640b92b1..b70979c520 100644 --- a/libavformat/demux.c +++ b/libavformat/demux.c @@ -1250,6 +1250,53 @@ static int64_t ts_to_samples(AVStream *st, int64_t ts) return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den); } +static int codec_close(FFStream *sti) +{ + AVCodecContext *avctx_new = NULL; + AVCodecParameters *par_tmp = NULL; + int ret; + + avctx_new = avcodec_alloc_context3(sti->avctx->codec); + if (!avctx_new) { + ret = AVERROR(ENOMEM); + goto fail; + } + + par_tmp = avcodec_parameters_alloc(); + if (!par_tmp) { + ret = AVERROR(ENOMEM); + goto fail; + } + + ret = avcodec_parameters_from_context(par_tmp, sti->avctx); + if (ret < 0) + goto fail; + + ret = avcodec_parameters_to_context(avctx_new, par_tmp); + if (ret < 0) + goto fail; + + avctx_new->pkt_timebase = sti->avctx->pkt_timebase; + +#if FF_API_TICKS_PER_FRAME +FF_DISABLE_DEPRECATION_WARNINGS + avctx_new->ticks_per_frame = sti->avctx->ticks_per_frame; +FF_ENABLE_DEPRECATION_WARNINGS +#endif + + avcodec_free_context(&sti->avctx); + sti->avctx = avctx_new; + + avctx_new = NULL; + ret = 0; + +fail: + avcodec_free_context(&avctx_new); + avcodec_parameters_free(&par_tmp); + + return ret; +} + static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) { FFFormatContext *const si = ffformatcontext(s); @@ -1286,8 +1333,10 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) if (sti->need_context_update) { if (avcodec_is_open(sti->avctx)) { av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n"); - avcodec_close(sti->avctx); + ret = codec_close(sti); sti->info->found_decoder = 0; + if (ret < 0) + return ret; } /* close parser, because it depends on the codec */ @@ -3013,14 +3062,16 @@ find_stream_info_err: for (unsigned i = 0; i < ic->nb_streams; i++) { AVStream *const st = ic->streams[i]; FFStream *const sti = ffstream(st); + int err; + if (sti->info) { av_freep(&sti->info->duration_error); av_freep(&sti->info); } - avcodec_close(sti->avctx); - // FIXME: avcodec_close() frees AVOption settable fields which includes ch_layout, - // so we need to restore it. - av_channel_layout_copy(&sti->avctx->ch_layout, &st->codecpar->ch_layout); + + err = codec_close(sti); + if (err < 0 && ret >= 0) + ret = err; av_bsf_free(&sti->extract_extradata.bsf); } if (ic->pb) { -- 2.42.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".