* [FFmpeg-devel] [PATCH 1/4] lavf/mpegts: drop a cargo-culted check
@ 2024-02-01 8:29 Anton Khirnov
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 2/4] lavf/flacdec: stop accessing FFStream.avctx Anton Khirnov
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Anton Khirnov @ 2024-02-01 8:29 UTC (permalink / raw)
To: ffmpeg-devel
This check has survived the transition to AVCodecParameters, but is no
longer relevant after it, since the codec context is no longer updated
or accessed at all from the demuxer.
---
libavformat/mpegts.c | 5 -----
1 file changed, 5 deletions(-)
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index bef00c88e7..1cf390e98e 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -919,11 +919,6 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
int old_codec_id = st->codecpar->codec_id;
int old_codec_tag = st->codecpar->codec_tag;
- if (avcodec_is_open(sti->avctx)) {
- av_log(pes->stream, AV_LOG_DEBUG, "cannot set stream info, internal codec is open\n");
- return 0;
- }
-
avpriv_set_pts_info(st, 33, 1, 90000);
st->priv_data = pes;
st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
--
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".
^ permalink raw reply [flat|nested] 8+ messages in thread
* [FFmpeg-devel] [PATCH 2/4] lavf/flacdec: stop accessing FFStream.avctx
2024-02-01 8:29 [FFmpeg-devel] [PATCH 1/4] lavf/mpegts: drop a cargo-culted check Anton Khirnov
@ 2024-02-01 8:29 ` Anton Khirnov
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close() Anton Khirnov
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 4/4] lavc: deprecate avcodec_close() Anton Khirnov
2 siblings, 0 replies; 8+ messages in thread
From: Anton Khirnov @ 2024-02-01 8:29 UTC (permalink / raw)
To: ffmpeg-devel
The demuxer opens an internal parser instance in read_timestamp(), which
requires a codec context. There is no need for it to access the FFStream
one which is used for other purposes, it can allocate its own internal
one.
---
libavformat/flacdec.c | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c
index ab9ef052f9..bbb205078a 100644
--- a/libavformat/flacdec.c
+++ b/libavformat/flacdec.c
@@ -36,6 +36,8 @@
typedef struct FLACDecContext {
FFRawDemuxerContext rawctx;
int found_seektable;
+
+ AVCodecContext *parser_dec;
} FLACDecContext;
static void reset_index_position(int64_t metadata_head_size, AVStream *st)
@@ -269,6 +271,7 @@ static int flac_probe(const AVProbeData *p)
static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
int64_t *ppos, int64_t pos_limit)
{
+ FLACDecContext *flac = s->priv_data;
FFFormatContext *const si = ffformatcontext(s);
AVPacket *const pkt = si->parse_pkt;
AVStream *st = s->streams[stream_index];
@@ -285,6 +288,16 @@ static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_inde
}
parser->flags |= PARSER_FLAG_USE_CODEC_TS;
+ if (!flac->parser_dec) {
+ flac->parser_dec = avcodec_alloc_context3(NULL);
+ if (!flac->parser_dec)
+ return AV_NOPTS_VALUE;
+
+ ret = avcodec_parameters_to_context(flac->parser_dec, st->codecpar);
+ if (ret < 0)
+ return ret;
+ }
+
for (;;){
uint8_t *data;
int size;
@@ -298,7 +311,7 @@ static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_inde
av_assert1(!pkt->size);
}
}
- av_parser_parse2(parser, ffstream(st)->avctx,
+ av_parser_parse2(parser, flac->parser_dec,
&data, &size, pkt->data, pkt->size,
pkt->pts, pkt->dts, *ppos);
@@ -318,6 +331,15 @@ static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_inde
return pts;
}
+static int flac_close(AVFormatContext *s)
+{
+ FLACDecContext *flac = s->priv_data;
+
+ avcodec_free_context(&flac->parser_dec);
+
+ return 0;
+}
+
static int flac_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
AVStream *const st = s->streams[0];
FFStream *const sti = ffstream(st);
@@ -347,6 +369,7 @@ const AVInputFormat ff_flac_demuxer = {
.long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
.read_probe = flac_probe,
.read_header = flac_read_header,
+ .read_close = flac_close,
.read_packet = ff_raw_read_partial_packet,
.read_seek = flac_seek,
.read_timestamp = flac_read_timestamp,
--
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".
^ permalink raw reply [flat|nested] 8+ messages in thread
* [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close()
2024-02-01 8:29 [FFmpeg-devel] [PATCH 1/4] lavf/mpegts: drop a cargo-culted check Anton Khirnov
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 2/4] lavf/flacdec: stop accessing FFStream.avctx Anton Khirnov
@ 2024-02-01 8:29 ` Anton Khirnov
2024-02-03 2:35 ` Leo Izen
` (2 more replies)
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 4/4] lavc: deprecate avcodec_close() Anton Khirnov
2 siblings, 3 replies; 8+ messages in thread
From: Anton Khirnov @ 2024-02-01 8:29 UTC (permalink / raw)
To: ffmpeg-devel
Replace it with recreating the codec context.
This is the last remaining blocker for deprecating avcodec_close().
---
libavformat/demux.c | 53 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 48 insertions(+), 5 deletions(-)
diff --git a/libavformat/demux.c b/libavformat/demux.c
index 6f640b92b1..c1640c459c 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -1250,6 +1250,52 @@ 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 = 0;
+
+ 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;
+
+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,7 +1332,7 @@ 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);
+ codec_close(sti);
sti->info->found_decoder = 0;
}
@@ -3017,10 +3063,7 @@ find_stream_info_err:
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);
+ codec_close(sti);
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".
^ permalink raw reply [flat|nested] 8+ messages in thread
* [FFmpeg-devel] [PATCH 4/4] lavc: deprecate avcodec_close()
2024-02-01 8:29 [FFmpeg-devel] [PATCH 1/4] lavf/mpegts: drop a cargo-culted check Anton Khirnov
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 2/4] lavf/flacdec: stop accessing FFStream.avctx Anton Khirnov
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close() Anton Khirnov
@ 2024-02-01 8:29 ` Anton Khirnov
2 siblings, 0 replies; 8+ messages in thread
From: Anton Khirnov @ 2024-02-01 8:29 UTC (permalink / raw)
To: ffmpeg-devel
Its use has been discouraged since 2016, but now is no longer used in
avformat, so there is no reason to keep it public.
---
libavcodec/avcodec.c | 12 +++++++++---
libavcodec/avcodec.h | 5 ++++-
libavcodec/avcodec_internal.h | 2 ++
libavcodec/frame_thread_encoder.c | 5 +++--
libavcodec/options.c | 3 ++-
libavcodec/version_major.h | 1 +
6 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index a6c8629f6c..b6d27ada21 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -377,7 +377,7 @@ end:
return ret;
free_and_end:
- avcodec_close(avctx);
+ ff_codec_close(avctx);
goto end;
}
@@ -432,12 +432,12 @@ void avsubtitle_free(AVSubtitle *sub)
memset(sub, 0, sizeof(*sub));
}
-av_cold int avcodec_close(AVCodecContext *avctx)
+av_cold void ff_codec_close(AVCodecContext *avctx)
{
int i;
if (!avctx)
- return 0;
+ return;
if (avcodec_is_open(avctx)) {
AVCodecInternal *avci = avctx->internal;
@@ -497,9 +497,15 @@ av_cold int avcodec_close(AVCodecContext *avctx)
avctx->codec = NULL;
avctx->active_thread_type = 0;
+}
+#if FF_API_AVCODEC_CLOSE
+int avcodec_close(AVCodecContext *avctx)
+{
+ ff_codec_close(avctx);
return 0;
}
+#endif
static const char *unknown_if_null(const char *str)
{
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7fb44e28f4..0018ccbb0c 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2411,6 +2411,7 @@ int avcodec_parameters_to_context(AVCodecContext *codec,
*/
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
+#if FF_API_AVCODEC_CLOSE
/**
* Close a given AVCodecContext and free all the data associated with it
* (but not the AVCodecContext itself).
@@ -2419,12 +2420,14 @@ int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **op
* the codec-specific data allocated in avcodec_alloc_context3() with a non-NULL
* codec. Subsequent calls will do nothing.
*
- * @note Do not use this function. Use avcodec_free_context() to destroy a
+ * @deprecated Do not use this function. Use avcodec_free_context() to destroy a
* codec context (either open or closed). Opening and closing a codec context
* multiple times is not supported anymore -- use multiple codec contexts
* instead.
*/
+attribute_deprecated
int avcodec_close(AVCodecContext *avctx);
+#endif
/**
* Free all allocated data in the given subtitle struct.
diff --git a/libavcodec/avcodec_internal.h b/libavcodec/avcodec_internal.h
index 9b93ff3d81..4d1cb3a314 100644
--- a/libavcodec/avcodec_internal.h
+++ b/libavcodec/avcodec_internal.h
@@ -56,4 +56,6 @@ void ff_encode_flush_buffers(struct AVCodecContext *avctx);
struct AVCodecInternal *ff_decode_internal_alloc(void);
struct AVCodecInternal *ff_encode_internal_alloc(void);
+void ff_codec_close(struct AVCodecContext *avctx);
+
#endif // AVCODEC_AVCODEC_INTERNAL_H
diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
index 62d9580ad4..cda5158117 100644
--- a/libavcodec/frame_thread_encoder.c
+++ b/libavcodec/frame_thread_encoder.c
@@ -27,6 +27,7 @@
#include "libavutil/opt.h"
#include "libavutil/thread.h"
#include "avcodec.h"
+#include "avcodec_internal.h"
#include "encode.h"
#include "internal.h"
#include "pthread_internal.h"
@@ -110,7 +111,7 @@ static void * attribute_align_arg worker(void *v){
pthread_mutex_unlock(&c->finished_task_mutex);
}
end:
- avcodec_close(avctx);
+ ff_codec_close(avctx);
av_freep(&avctx);
return NULL;
}
@@ -230,7 +231,7 @@ av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
return 0;
fail:
- avcodec_close(thread_avctx);
+ ff_codec_close(thread_avctx);
av_freep(&thread_avctx);
avctx->thread_count = i;
av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
diff --git a/libavcodec/options.c b/libavcodec/options.c
index a9b35ee1c3..05a355fb45 100644
--- a/libavcodec/options.c
+++ b/libavcodec/options.c
@@ -27,6 +27,7 @@
#include "config_components.h"
#include "avcodec.h"
+#include "avcodec_internal.h"
#include "codec_internal.h"
#include "libavutil/avassert.h"
#include "libavutil/internal.h"
@@ -172,7 +173,7 @@ void avcodec_free_context(AVCodecContext **pavctx)
if (!avctx)
return;
- avcodec_close(avctx);
+ ff_codec_close(avctx);
av_freep(&avctx->extradata);
av_freep(&avctx->subtitle_header);
diff --git a/libavcodec/version_major.h b/libavcodec/version_major.h
index b9164fe5c6..45209c0a4f 100644
--- a/libavcodec/version_major.h
+++ b/libavcodec/version_major.h
@@ -52,6 +52,7 @@
#define FF_API_AVFFT (LIBAVCODEC_VERSION_MAJOR < 62)
#define FF_API_FF_PROFILE_LEVEL (LIBAVCODEC_VERSION_MAJOR < 62)
+#define FF_API_AVCODEC_CLOSE (LIBAVCODEC_VERSION_MAJOR < 62)
// reminder to remove CrystalHD decoders on next major bump
#define FF_CODEC_CRYSTAL_HD (LIBAVCODEC_VERSION_MAJOR < 61)
--
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".
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close()
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close() Anton Khirnov
@ 2024-02-03 2:35 ` Leo Izen
2024-02-05 16:59 ` Anton Khirnov
2024-02-03 3:13 ` James Almer
2024-02-05 17:03 ` [FFmpeg-devel] [PATCH v2 " Anton Khirnov
2 siblings, 1 reply; 8+ messages in thread
From: Leo Izen @ 2024-02-03 2:35 UTC (permalink / raw)
To: ffmpeg-devel
[-- Attachment #1.1.1: Type: text/plain, Size: 3184 bytes --]
On 2/1/24 03:29, Anton Khirnov wrote:
> Replace it with recreating the codec context.
>
> This is the last remaining blocker for deprecating avcodec_close().
> ---
> libavformat/demux.c | 53 ++++++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 48 insertions(+), 5 deletions(-)
>
> diff --git a/libavformat/demux.c b/libavformat/demux.c
> index 6f640b92b1..c1640c459c 100644
> --- a/libavformat/demux.c
> +++ b/libavformat/demux.c
> @@ -1250,6 +1250,52 @@ 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 = 0;
> +
I believe we can drop the initialization from avctx_new and from ret,
because avctx_new is assigned immediately, and ret is assigned before
each goto before it's assigned properly.
> + 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;
> +
> +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,7 +1332,7 @@ 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);
> + codec_close(sti);
> sti->info->found_decoder = 0;
> }
>
> @@ -3017,10 +3063,7 @@ find_stream_info_err:
> 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);
> + codec_close(sti);
> av_bsf_free(&sti->extract_extradata.bsf);
> }
> if (ic->pb) {
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
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".
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close()
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close() Anton Khirnov
2024-02-03 2:35 ` Leo Izen
@ 2024-02-03 3:13 ` James Almer
2024-02-05 17:03 ` [FFmpeg-devel] [PATCH v2 " Anton Khirnov
2 siblings, 0 replies; 8+ messages in thread
From: James Almer @ 2024-02-03 3:13 UTC (permalink / raw)
To: ffmpeg-devel
On 2/1/2024 5:29 AM, Anton Khirnov wrote:
> Replace it with recreating the codec context.
>
> This is the last remaining blocker for deprecating avcodec_close().
> ---
> libavformat/demux.c | 53 ++++++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 48 insertions(+), 5 deletions(-)
>
> diff --git a/libavformat/demux.c b/libavformat/demux.c
> index 6f640b92b1..c1640c459c 100644
> --- a/libavformat/demux.c
> +++ b/libavformat/demux.c
> @@ -1250,6 +1250,52 @@ 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)
This returns an error code but you're not checking it.
> +{
> + AVCodecContext *avctx_new = NULL;
> + AVCodecParameters *par_tmp = NULL;
> + int ret = 0;
> +
> + 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;
> +
> +fail:
> + avcodec_free_context(&avctx_new);
> + avcodec_parameters_free(&par_tmp);
> +
> + return ret;
For success scenarios, this will return whatever was last set on ret,
which in this case is the return value of
avcodec_parameters_to_context() that just happens to be 0. But something
else could be added later that also sets ret after it, so the proper
thing to do is to set ret to 0 immediately above the fail label.
> +}
> +
> static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
> {
> FFFormatContext *const si = ffformatcontext(s);
> @@ -1286,7 +1332,7 @@ 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);
> + codec_close(sti);
> sti->info->found_decoder = 0;
> }
>
> @@ -3017,10 +3063,7 @@ find_stream_info_err:
> 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);
> + codec_close(sti);
> av_bsf_free(&sti->extract_extradata.bsf);
> }
> if (ic->pb) {
_______________________________________________
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".
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close()
2024-02-03 2:35 ` Leo Izen
@ 2024-02-05 16:59 ` Anton Khirnov
0 siblings, 0 replies; 8+ messages in thread
From: Anton Khirnov @ 2024-02-05 16:59 UTC (permalink / raw)
To: FFmpeg development discussions and patches
Quoting Leo Izen (2024-02-03 03:35:45)
> On 2/1/24 03:29, Anton Khirnov wrote:
> > Replace it with recreating the codec context.
> >
> > This is the last remaining blocker for deprecating avcodec_close().
> > ---
> > libavformat/demux.c | 53 ++++++++++++++++++++++++++++++++++++++++-----
> > 1 file changed, 48 insertions(+), 5 deletions(-)
> >
> > diff --git a/libavformat/demux.c b/libavformat/demux.c
> > index 6f640b92b1..c1640c459c 100644
> > --- a/libavformat/demux.c
> > +++ b/libavformat/demux.c
> > @@ -1250,6 +1250,52 @@ 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 = 0;
> > +
>
> I believe we can drop the initialization from avctx_new and from ret,
> because avctx_new is assigned immediately, and ret is assigned before
> each goto before it's assigned properly.
It's safer wrt future modifications of the code, e.g. if someone would
insert something that could fail before allocating avctx.
--
Anton Khirnov
_______________________________________________
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".
^ permalink raw reply [flat|nested] 8+ messages in thread
* [FFmpeg-devel] [PATCH v2 3/4] lavf/demux: stop calling avcodec_close()
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close() Anton Khirnov
2024-02-03 2:35 ` Leo Izen
2024-02-03 3:13 ` James Almer
@ 2024-02-05 17:03 ` Anton Khirnov
2 siblings, 0 replies; 8+ messages in thread
From: Anton Khirnov @ 2024-02-05 17:03 UTC (permalink / raw)
To: ffmpeg-devel
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".
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2024-02-05 17:04 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-01 8:29 [FFmpeg-devel] [PATCH 1/4] lavf/mpegts: drop a cargo-culted check Anton Khirnov
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 2/4] lavf/flacdec: stop accessing FFStream.avctx Anton Khirnov
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 3/4] lavf/demux: stop calling avcodec_close() Anton Khirnov
2024-02-03 2:35 ` Leo Izen
2024-02-05 16:59 ` Anton Khirnov
2024-02-03 3:13 ` James Almer
2024-02-05 17:03 ` [FFmpeg-devel] [PATCH v2 " Anton Khirnov
2024-02-01 8:29 ` [FFmpeg-devel] [PATCH 4/4] lavc: deprecate avcodec_close() Anton Khirnov
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git