Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 04/31] fftools/ffmpeg: make decoding AVCodecContext private to the decoder
Date: Wed, 24 Jan 2024 09:16:34 +0100
Message-ID: <20240124081702.4759-4-anton@khirnov.net> (raw)
In-Reply-To: <20240124081702.4759-1-anton@khirnov.net>

---
 fftools/ffmpeg.h       |  3 +-
 fftools/ffmpeg_dec.c   | 90 ++++++++++++++++++++++++++----------------
 fftools/ffmpeg_demux.c | 16 +-------
 3 files changed, 60 insertions(+), 49 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 4c9424d02f..f0f57d48bc 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -306,7 +306,6 @@ typedef struct InputStream {
      */
     AVCodecParameters *par;
     Decoder *decoder;
-    AVCodecContext *dec_ctx;
     const AVCodec *dec;
 
     AVRational framerate_guessed;
@@ -733,6 +732,8 @@ int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input);
 int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx);
 void dec_free(Decoder **pdec);
 
+int dec_add_filter(Decoder *dec, InputFilter *ifilter);
+
 int enc_alloc(Encoder **penc, const AVCodec *codec,
               Scheduler *sch, unsigned sch_idx);
 void enc_free(Encoder **penc);
diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c
index cb967d5930..230da4fa7f 100644
--- a/fftools/ffmpeg_dec.c
+++ b/fftools/ffmpeg_dec.c
@@ -37,6 +37,8 @@
 typedef struct DecoderPriv {
     Decoder          dec;
 
+    AVCodecContext  *dec_ctx;
+
     AVFrame         *frame;
     AVPacket        *pkt;
 
@@ -79,6 +81,8 @@ void dec_free(Decoder **pdec)
         return;
     dp = dp_from_dec(dec);
 
+    avcodec_free_context(&dp->dec_ctx);
+
     av_frame_free(&dp->frame);
     av_packet_free(&dp->pkt);
 
@@ -216,9 +220,9 @@ static int64_t video_duration_estimate(const InputStream *ist, const AVFrame *fr
     if (frame->duration > 0 && (!ifile->format_nots || ist->framerate.num))
         return frame->duration;
 
-    if (ist->dec_ctx->framerate.den && ist->dec_ctx->framerate.num) {
+    if (dp->dec_ctx->framerate.den && dp->dec_ctx->framerate.num) {
         int fields = frame->repeat_pict + 2;
-        AVRational field_rate = av_mul_q(ist->dec_ctx->framerate,
+        AVRational field_rate = av_mul_q(dp->dec_ctx->framerate,
                                          (AVRational){ 2, 1 });
         codec_duration = av_rescale_q(fields, av_inv_q(field_rate),
                                       frame->time_base);
@@ -258,29 +262,29 @@ static int video_frame_process(InputStream *ist, AVFrame *frame)
 
     // The following line may be required in some cases where there is no parser
     // or the parser does not has_b_frames correctly
-    if (ist->par->video_delay < ist->dec_ctx->has_b_frames) {
-        if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) {
-            ist->par->video_delay = ist->dec_ctx->has_b_frames;
+    if (ist->par->video_delay < dp->dec_ctx->has_b_frames) {
+        if (dp->dec_ctx->codec_id == AV_CODEC_ID_H264) {
+            ist->par->video_delay = dp->dec_ctx->has_b_frames;
         } else
-            av_log(ist->dec_ctx, AV_LOG_WARNING,
+            av_log(dp->dec_ctx, AV_LOG_WARNING,
                    "video_delay is larger in decoder than demuxer %d > %d.\n"
                    "If you want to help, upload a sample "
                    "of this file to https://streams.videolan.org/upload/ "
                    "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
-                   ist->dec_ctx->has_b_frames,
+                   dp->dec_ctx->has_b_frames,
                    ist->par->video_delay);
     }
 
-    if (ist->dec_ctx->width  != frame->width ||
-        ist->dec_ctx->height != frame->height ||
-        ist->dec_ctx->pix_fmt != frame->format) {
+    if (dp->dec_ctx->width  != frame->width ||
+        dp->dec_ctx->height != frame->height ||
+        dp->dec_ctx->pix_fmt != frame->format) {
         av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
             frame->width,
             frame->height,
             frame->format,
-            ist->dec_ctx->width,
-            ist->dec_ctx->height,
-            ist->dec_ctx->pix_fmt);
+            dp->dec_ctx->width,
+            dp->dec_ctx->height,
+            dp->dec_ctx->pix_fmt);
     }
 
 #if FFMPEG_OPT_TOP
@@ -291,7 +295,7 @@ static int video_frame_process(InputStream *ist, AVFrame *frame)
 #endif
 
     if (frame->format == dp->hwaccel_pix_fmt) {
-        int err = hwaccel_retrieve_data(ist->dec_ctx, frame);
+        int err = hwaccel_retrieve_data(dp->dec_ctx, frame);
         if (err < 0)
             return err;
     }
@@ -431,7 +435,7 @@ static int transcode_subtitles(InputStream *ist, const AVPacket *pkt,
             return AVERROR(ENOMEM);
     }
 
-    ret = avcodec_decode_subtitle2(ist->dec_ctx, &subtitle, &got_output,
+    ret = avcodec_decode_subtitle2(dp->dec_ctx, &subtitle, &got_output,
                                    pkt ? pkt : flush_pkt);
     av_packet_free(&flush_pkt);
 
@@ -457,8 +461,8 @@ static int transcode_subtitles(InputStream *ist, const AVPacket *pkt,
         return ret;
     }
 
-    frame->width  = ist->dec_ctx->width;
-    frame->height = ist->dec_ctx->height;
+    frame->width  = dp->dec_ctx->width;
+    frame->height = dp->dec_ctx->height;
 
     return process_subtitle(ist, frame);
 }
@@ -467,7 +471,7 @@ static int packet_decode(InputStream *ist, AVPacket *pkt, AVFrame *frame)
 {
     const InputFile *ifile = ist->file;
     DecoderPriv *dp = dp_from_dec(ist->decoder);
-    AVCodecContext *dec = ist->dec_ctx;
+    AVCodecContext *dec = dp->dec_ctx;
     const char *type_desc = av_get_media_type_string(dec->codec_type);
     int ret;
 
@@ -582,11 +586,11 @@ static int packet_decode(InputStream *ist, AVPacket *pkt, AVFrame *frame)
     }
 }
 
-static void dec_thread_set_name(const InputStream *ist)
+static void dec_thread_set_name(const InputStream *ist, const DecoderPriv *dp)
 {
     char name[16];
     snprintf(name, sizeof(name), "dec%d:%d:%s", ist->file->index, ist->index,
-             ist->dec_ctx->codec->name);
+             dp->dec_ctx->codec->name);
     ff_thread_setname(name);
 }
 
@@ -628,7 +632,7 @@ void *decoder_thread(void *arg)
     if (ret < 0)
         goto finish;
 
-    dec_thread_set_name(ist);
+    dec_thread_set_name(ist, dp);
 
     while (!input_status) {
         int flush_buffers, have_data;
@@ -669,7 +673,7 @@ void *decoder_thread(void *arg)
                 dt.pkt->time_base = dp->last_frame_tb;
             }
 
-            avcodec_flush_buffers(ist->dec_ctx);
+            avcodec_flush_buffers(dp->dec_ctx);
         } else if (ret < 0) {
             av_log(ist, AV_LOG_ERROR, "Error processing packet in decoder: %s\n",
                    av_err2str(ret));
@@ -769,7 +773,7 @@ static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
     }
 }
 
-static int hw_device_setup_for_decode(InputStream *ist)
+static int hw_device_setup_for_decode(InputStream *ist, DecoderPriv *dp)
 {
     const AVCodecHWConfig *config;
     enum AVHWDeviceType type;
@@ -890,8 +894,8 @@ static int hw_device_setup_for_decode(InputStream *ist)
         return err;
     }
 
-    ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
-    if (!ist->dec_ctx->hw_device_ctx)
+    dp->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
+    if (!dp->dec_ctx->hw_device_ctx)
         return AVERROR(ENOMEM);
 
     return 0;
@@ -906,7 +910,7 @@ int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx)
     if (!codec) {
         av_log(ist, AV_LOG_ERROR,
                "Decoding requested, but no decoder found for: %s\n",
-                avcodec_get_name(ist->dec_ctx->codec_id));
+                avcodec_get_name(ist->par->codec_id));
         return AVERROR(EINVAL);
     }
 
@@ -929,10 +933,20 @@ int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx)
             return AVERROR(ENOMEM);
     }
 
-    ist->dec_ctx->opaque                = ist;
-    ist->dec_ctx->get_format            = get_format;
+    dp->dec_ctx = avcodec_alloc_context3(codec);
+    if (!dp->dec_ctx)
+        return AVERROR(ENOMEM);
 
-    if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
+    ret = avcodec_parameters_to_context(dp->dec_ctx, ist->par);
+    if (ret < 0) {
+        av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
+        return ret;
+    }
+
+    dp->dec_ctx->opaque                = ist;
+    dp->dec_ctx->get_format            = get_format;
+
+    if (dp->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
        (ist->decoding_needed & DECODING_FOR_OST)) {
         av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
         if (ist->decoding_needed & DECODING_FOR_FILTER)
@@ -941,7 +955,7 @@ int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx)
 
     /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
      * audio, and video decoders such as cuvid or mediacodec */
-    ist->dec_ctx->pkt_timebase = ist->st->time_base;
+    dp->dec_ctx->pkt_timebase = ist->st->time_base;
 
     if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
         av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
@@ -951,7 +965,7 @@ int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx)
 
     av_dict_set(&ist->decoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
 
-    ret = hw_device_setup_for_decode(ist);
+    ret = hw_device_setup_for_decode(ist, dp);
     if (ret < 0) {
         av_log(ist, AV_LOG_ERROR,
                "Hardware device setup failed for decoder: %s\n",
@@ -959,7 +973,7 @@ int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx)
         return ret;
     }
 
-    if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
+    if ((ret = avcodec_open2(dp->dec_ctx, codec, &ist->decoder_opts)) < 0) {
         av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n",
                av_err2str(ret));
         return ret;
@@ -969,8 +983,16 @@ int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx)
     if (ret < 0)
         return ret;
 
-    dp->dec.subtitle_header      = ist->dec_ctx->subtitle_header;
-    dp->dec.subtitle_header_size = ist->dec_ctx->subtitle_header_size;
+    dp->dec.subtitle_header      = dp->dec_ctx->subtitle_header;
+    dp->dec.subtitle_header_size = dp->dec_ctx->subtitle_header_size;
 
     return 0;
 }
+
+int dec_add_filter(Decoder *dec, InputFilter *ifilter)
+{
+    DecoderPriv *dp = dp_from_dec(dec);
+
+    // initialize fallback parameters for filtering
+    return ifilter_parameters_from_dec(ifilter, dp->dec_ctx);
+}
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 16d4f67e59..8e904d2150 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -825,7 +825,6 @@ static void ist_free(InputStream **pist)
     av_freep(&ist->outputs);
     av_freep(&ist->hwaccel_device);
 
-    avcodec_free_context(&ist->dec_ctx);
     avcodec_parameters_free(&ist->par);
 
     av_bsf_free(&ds->bsf);
@@ -886,16 +885,6 @@ static int ist_use(InputStream *ist, int decoding_needed)
     if (decoding_needed && ds->sch_idx_dec < 0) {
         int is_audio = ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
 
-        ist->dec_ctx = avcodec_alloc_context3(ist->dec);
-        if (!ist->dec_ctx)
-            return AVERROR(ENOMEM);
-
-        ret = avcodec_parameters_to_context(ist->dec_ctx, ist->par);
-        if (ret < 0) {
-            av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
-            return ret;
-        }
-
         ret = sch_add_dec(d->sch, decoder_thread, ist, d->loop && is_audio);
         if (ret < 0)
             return ret;
@@ -950,12 +939,11 @@ int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
 
     ist->filters[ist->nb_filters - 1] = ifilter;
 
-    // initialize fallback parameters for filtering
-    ret = ifilter_parameters_from_dec(ifilter, ist->dec_ctx);
+    ret = dec_add_filter(ist->decoder, ifilter);
     if (ret < 0)
         return ret;
 
-    if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
+    if (ist->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
         if (!d->pkt_heartbeat) {
             d->pkt_heartbeat = av_packet_alloc();
             if (!d->pkt_heartbeat)
-- 
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".

  parent reply	other threads:[~2024-01-24  8:18 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-24  8:16 [FFmpeg-devel] [PATCH 01/31] fftools/ffmpeg_dec: split Decoder into a private and public part Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 02/31] fftools/ffmpeg_dec: export subtitle_header in Decoder Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 03/31] fftools/ffmpeg_filter: consolidate decoder/filter type checks Anton Khirnov
2024-01-24  8:16 ` Anton Khirnov [this message]
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 05/31] fftools/ffmpeg_dec: add an AVClass to Decoder Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 06/31] fftools/ffmpeg_dec: pass decoder options as an argument to dec_open() Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 07/31] fftools/ffmpeg_dec: move decoding counters from InputStream to Decoder Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 08/31] fftools/ffmpeg_dec: drop useless and racy code Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 09/31] fftools/ffmpeg_dec: drop a useless log message Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 10/31] fftools/ffmpeg: move decoder existence check to a more appropriate place Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 11/31] fftools/ffmpeg_dec: override video SAR with AVCodecParameters value Anton Khirnov
2024-03-24 22:09   ` Michael Niedermayer
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 12/31] fftools/ffmpeg_dec: stop accesing InputStream.fix_sub_duration Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 13/31] fftools/ffmpeg: refactor disabling decoder threading for attached pictures Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 14/31] fftools/ffmpeg_dec: replace InputFile.format_nots with a decoder flag Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 15/31] fftools/ffmpeg: move hwaccel_retrieve_data() from ffmpeg_hw to ffmpeg_dec Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 16/31] fftools/ffmpeg_dec: pass hwaccel options to the decoder in a separate struct Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 17/31] fftools/ffmpeg_dec: move flags to DecoderOpts Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 18/31] fftools/ffmpeg_dec: pass forced/estimated framerate though DecoderOpts Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 19/31] fftools/ffmpeg_dec: move setting compute_edt to demuxer Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 20/31] fftools/ffmpeg_dec: pass input timebase through DecoderOpts Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 21/31] fftools/ffmpeg_dec: pass top_field_first " Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 22/31] fftools/ffmpeg_dec: pass decoder name " Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 23/31] fftools/ffmpeg_dec: eliminate InputStream use in hw_device_setup_for_decode() Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 24/31] fftools/ffmpeg_dec: pass AVCodec through DecoderOpts Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 25/31] fftools/ffmpeg_dec: pass AVCodecParameters " Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 26/31] fftools/ffmpeg_dec: remove unnecessary InputStream arguments Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 27/31] fftools/ffmpeg_dec: stop passing InputStream to dec_open() Anton Khirnov
2024-01-25  1:19   ` Michael Niedermayer
2024-01-28  9:40     ` [FFmpeg-devel] [PATCH v2 " Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 28/31] fftools/ffmpeg_dec: eliminate all remaining InputStream uses Anton Khirnov
2024-01-24  8:16 ` [FFmpeg-devel] [PATCH 29/31] fftools/ffmpeg: make InputStream.decoding_needed private to demuxer Anton Khirnov
2024-01-24  8:17 ` [FFmpeg-devel] [PATCH 30/31] fftools/ffmpeg: make InputStream.decoder_opts " Anton Khirnov
2024-01-24  8:17 ` [FFmpeg-devel] [PATCH 31/31] fftools/ffmpeg: cosmetics, vertically align Input{File, Stream} Anton Khirnov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240124081702.4759-4-anton@khirnov.net \
    --to=anton@khirnov.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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