Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function
@ 2025-05-23  3:10 James Almer
  2025-05-23  3:10 ` [FFmpeg-devel] [PATCH 2/3] avcodec/cbs_av1: don't store a reference to the unit data if it's not needed James Almer
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: James Almer @ 2025-05-23  3:10 UTC (permalink / raw)
  To: ffmpeg-devel

And rename the existing one to ff_cbs_alloc().
This will allow for more versatility when setting options in a module, allowing
them to be taken into account when calling module specific init functions.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/apv_decode.c          |  6 +++++-
 libavcodec/apv_parser.c          |  6 +++++-
 libavcodec/av1_parser.c          |  6 +++++-
 libavcodec/av1dec.c              | 14 +++++++++++---
 libavcodec/bsf/av1_frame_merge.c | 16 ++++++++++++++--
 libavcodec/bsf/av1_frame_split.c |  6 +++++-
 libavcodec/bsf/dts2pts.c         |  6 +++++-
 libavcodec/bsf/filter_units.c    |  6 +++++-
 libavcodec/bsf/trace_headers.c   |  2 +-
 libavcodec/cbs.c                 | 28 +++++++++++++++++++++++++++-
 libavcodec/cbs.h                 | 10 ++++++++--
 libavcodec/cbs_bsf.c             | 12 ++++++++++--
 libavcodec/cbs_internal.h        |  3 +++
 libavcodec/d3d12va_encode_hevc.c |  6 +++++-
 libavcodec/vaapi_encode_av1.c    |  6 +++++-
 libavcodec/vaapi_encode_h264.c   |  6 +++++-
 libavcodec/vaapi_encode_h265.c   |  6 +++++-
 libavcodec/vaapi_encode_mjpeg.c  |  6 +++++-
 libavcodec/vaapi_encode_mpeg2.c  |  6 +++++-
 libavcodec/vulkan_encode_h264.c  | 12 ++++++++++--
 libavcodec/vulkan_encode_h265.c  | 12 ++++++++++--
 libavcodec/vvc/dec.c             |  6 +++++-
 libavcodec/vvc_parser.c          |  5 ++++-
 libavformat/movenccenc.c         |  6 +++++-
 24 files changed, 168 insertions(+), 30 deletions(-)

diff --git a/libavcodec/apv_decode.c b/libavcodec/apv_decode.c
index eb47298e2e..3667933df6 100644
--- a/libavcodec/apv_decode.c
+++ b/libavcodec/apv_decode.c
@@ -119,7 +119,11 @@ static av_cold int apv_decode_init(AVCodecContext *avctx)
 
     ff_thread_once(&apv_entropy_once, apv_entropy_build_decode_lut);
 
-    err = ff_cbs_init(&apv->cbc, AV_CODEC_ID_APV, avctx);
+    err = ff_cbs_alloc(&apv->cbc, AV_CODEC_ID_APV, avctx);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(apv->cbc, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/apv_parser.c b/libavcodec/apv_parser.c
index fdd575339b..e0aa152ca8 100644
--- a/libavcodec/apv_parser.c
+++ b/libavcodec/apv_parser.c
@@ -122,7 +122,11 @@ static av_cold int init(AVCodecParserContext *s)
     APVParseContext *p = s->priv_data;
     int ret;
 
-    ret = ff_cbs_init(&p->cbc, AV_CODEC_ID_APV, NULL);
+    ret = ff_cbs_alloc(&p->cbc, AV_CODEC_ID_APV, NULL);
+    if (ret < 0)
+        return ret;
+
+    ret = ff_cbs_init(p->cbc, NULL);
     if (ret < 0)
         return ret;
 
diff --git a/libavcodec/av1_parser.c b/libavcodec/av1_parser.c
index 1792e813f4..77906d0c91 100644
--- a/libavcodec/av1_parser.c
+++ b/libavcodec/av1_parser.c
@@ -190,13 +190,17 @@ static av_cold int av1_parser_init(AVCodecParserContext *ctx)
     AV1ParseContext *s = ctx->priv_data;
     int ret;
 
-    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
+    ret = ff_cbs_alloc(&s->cbc, AV_CODEC_ID_AV1, NULL);
     if (ret < 0)
         return ret;
 
     s->cbc->decompose_unit_types    = decompose_unit_types;
     s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
 
+    ret = ff_cbs_init(s->cbc, NULL);
+    if (ret < 0)
+        return ret;
+
     return 0;
 }
 
diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index 8ff1bf394c..3130364534 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -858,6 +858,7 @@ static av_cold int av1_decode_init(AVCodecContext *avctx)
 {
     AV1DecContext *s = avctx->priv_data;
     AV1RawSequenceHeader *seq;
+    AVDictionary *options = NULL;
     const AVPacketSideData *sd;
     int ret;
 
@@ -865,20 +866,27 @@ static av_cold int av1_decode_init(AVCodecContext *avctx)
     s->pkt = avctx->internal->in_pkt;
     s->pix_fmt = AV_PIX_FMT_NONE;
 
-    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
+    ret = ff_cbs_alloc(&s->cbc, AV_CODEC_ID_AV1, avctx);
     if (ret < 0)
         return ret;
 
     s->cbc->decompose_unit_types    = decompose_unit_types;
     s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
 
+    ret = av_dict_set_int(&options, "operating_point", s->operating_point, 0);
+    if (ret < 0)
+        return ret;
+
+    ret = ff_cbs_init(s->cbc, &options);
+    av_dict_free(&options);
+    if (ret < 0)
+        return ret;
+
     s->itut_t35_fifo = av_fifo_alloc2(1, sizeof(AV1RawMetadataITUTT35),
                                       AV_FIFO_FLAG_AUTO_GROW);
     if (!s->itut_t35_fifo)
         return AVERROR(ENOMEM);
 
-    av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
-
     if (avctx->extradata && avctx->extradata_size) {
         ret = ff_cbs_read_extradata_from_codec(s->cbc,
                                                &s->current_obu,
diff --git a/libavcodec/bsf/av1_frame_merge.c b/libavcodec/bsf/av1_frame_merge.c
index 4c54f2167e..530688be49 100644
--- a/libavcodec/bsf/av1_frame_merge.c
+++ b/libavcodec/bsf/av1_frame_merge.c
@@ -133,11 +133,23 @@ static int av1_frame_merge_init(AVBSFContext *bsf)
     if (!ctx->in || !ctx->pkt)
         return AVERROR(ENOMEM);
 
-    err =  ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
+    err =  ff_cbs_alloc(&ctx->input, AV_CODEC_ID_AV1, bsf);
     if (err < 0)
         return err;
 
-    return ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
+    err =  ff_cbs_alloc(&ctx->output, AV_CODEC_ID_AV1, bsf);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(ctx->input, NULL);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(ctx->output, NULL);
+    if (err < 0)
+        return err;
+
+    return 0;
 }
 
 static void av1_frame_merge_close(AVBSFContext *bsf)
diff --git a/libavcodec/bsf/av1_frame_split.c b/libavcodec/bsf/av1_frame_split.c
index 5f6a40316c..a64e9178e7 100644
--- a/libavcodec/bsf/av1_frame_split.c
+++ b/libavcodec/bsf/av1_frame_split.c
@@ -210,7 +210,11 @@ static int av1_frame_split_init(AVBSFContext *ctx)
     if (!s->buffer_pkt)
         return AVERROR(ENOMEM);
 
-    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, ctx);
+    ret = ff_cbs_alloc(&s->cbc, AV_CODEC_ID_AV1, ctx);
+    if (ret < 0)
+        return ret;
+
+    ret = ff_cbs_init(s->cbc, NULL);
     if (ret < 0)
         return ret;
 
diff --git a/libavcodec/bsf/dts2pts.c b/libavcodec/bsf/dts2pts.c
index 9d31d7dc08..189a9f9678 100644
--- a/libavcodec/bsf/dts2pts.c
+++ b/libavcodec/bsf/dts2pts.c
@@ -402,7 +402,11 @@ static int dts2pts_init(AVBSFContext *ctx)
     if (!s->node_pool)
         return AVERROR(ENOMEM);
 
-    ret = ff_cbs_init(&s->cbc, ctx->par_in->codec_id, ctx);
+    ret = ff_cbs_alloc(&s->cbc, ctx->par_in->codec_id, ctx);
+    if (ret < 0)
+        return ret;
+
+    ret = ff_cbs_init(s->cbc, NULL);
     if (ret < 0)
         return ret;
 
diff --git a/libavcodec/bsf/filter_units.c b/libavcodec/bsf/filter_units.c
index 336331733f..0edf2fa472 100644
--- a/libavcodec/bsf/filter_units.c
+++ b/libavcodec/bsf/filter_units.c
@@ -187,7 +187,11 @@ static int filter_units_init(AVBSFContext *bsf)
         return 0;
     }
 
-    err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
+    err = ff_cbs_alloc(&ctx->cbc, bsf->par_in->codec_id, bsf);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(ctx->cbc, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/bsf/trace_headers.c b/libavcodec/bsf/trace_headers.c
index 8781f5f100..e885f7a73f 100644
--- a/libavcodec/bsf/trace_headers.c
+++ b/libavcodec/bsf/trace_headers.c
@@ -38,7 +38,7 @@ static int trace_headers_init(AVBSFContext *bsf)
     TraceHeadersContext *ctx = bsf->priv_data;
     int err;
 
-    err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
+    err = ff_cbs_alloc(&ctx->cbc, bsf->par_in->codec_id, bsf);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index 6b2ebe597d..8c0659f7af 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -21,6 +21,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/buffer.h"
 #include "libavutil/common.h"
+#include "libavutil/dict.h"
 #include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
@@ -91,7 +92,7 @@ const enum AVCodecID CBS_FUNC(all_codec_ids)[] = {
     AV_CODEC_ID_NONE
 };
 
-av_cold int CBS_FUNC(init)(CodedBitstreamContext **ctx_ptr,
+av_cold int CBS_FUNC(alloc)(CodedBitstreamContext **ctx_ptr,
                         enum AVCodecID codec_id, void *log_ctx)
 {
     CodedBitstreamContext *ctx;
@@ -137,6 +138,31 @@ av_cold int CBS_FUNC(init)(CodedBitstreamContext **ctx_ptr,
     return 0;
 }
 
+av_cold int CBS_FUNC(init)(CodedBitstreamContext *ctx, AVDictionary **options)
+{
+    int err;
+
+    if (!ctx->codec)
+        return AVERROR(EINVAL);
+
+    if (ctx->codec->priv_data_size) {
+        if (!ctx->priv_data)
+            return AVERROR(EINVAL);
+
+        err = av_opt_set_dict2(ctx->priv_data, options, 0);
+        if (err < 0)
+            return err;
+    }
+
+    if (ctx->codec->init) {
+        err = ctx->codec->init(ctx);
+        if (err < 0)
+            return err;
+    }
+
+    return 0;
+}
+
 av_cold void CBS_FUNC(flush)(CodedBitstreamContext *ctx)
 {
     if (ctx->codec->flush)
diff --git a/libavcodec/cbs.h b/libavcodec/cbs.h
index 67f2ec9e50..5b0e1f5c6b 100644
--- a/libavcodec/cbs.h
+++ b/libavcodec/cbs.h
@@ -176,6 +176,7 @@ typedef struct CodedBitstreamFragment {
 } CodedBitstreamFragment;
 
 
+struct AVDictionary;
 struct CodedBitstreamContext;
 struct GetBitContext;
 struct PutBitContext;
@@ -305,11 +306,16 @@ extern const enum AVCodecID CBS_FUNC(all_codec_ids)[];
 
 
 /**
- * Create and initialise a new context for the given codec.
+ * Create a new context for the given codec.
  */
-int CBS_FUNC(init)(CodedBitstreamContext **ctx,
+int CBS_FUNC(alloc)(CodedBitstreamContext **ctx,
                 enum AVCodecID codec_id, void *log_ctx);
 
+/**
+ * Initialise a context.
+ */
+int CBS_FUNC(init)(CodedBitstreamContext *ctx, struct AVDictionary **options);
+
 /**
  * Reset all internal state in a context.
  */
diff --git a/libavcodec/cbs_bsf.c b/libavcodec/cbs_bsf.c
index b25285483b..25b9e730d8 100644
--- a/libavcodec/cbs_bsf.c
+++ b/libavcodec/cbs_bsf.c
@@ -115,11 +115,19 @@ int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
 
     ctx->type = type;
 
-    err = ff_cbs_init(&ctx->input, type->codec_id, bsf);
+    err = ff_cbs_alloc(&ctx->input, type->codec_id, bsf);
     if (err < 0)
         return err;
 
-    err = ff_cbs_init(&ctx->output, type->codec_id, bsf);
+    err = ff_cbs_alloc(&ctx->output, type->codec_id, bsf);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(ctx->input, NULL);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(ctx->output, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h
index c3265924ba..734269d368 100644
--- a/libavcodec/cbs_internal.h
+++ b/libavcodec/cbs_internal.h
@@ -184,6 +184,9 @@ typedef struct CodedBitstreamType {
     int (*assemble_fragment)(CodedBitstreamContext *ctx,
                              CodedBitstreamFragment *frag);
 
+    // Initialize the codec internal state.
+    int (*init)(CodedBitstreamContext *ctx);
+
     // Reset the codec internal state.
     void (*flush)(CodedBitstreamContext *ctx);
 
diff --git a/libavcodec/d3d12va_encode_hevc.c b/libavcodec/d3d12va_encode_hevc.c
index 938ba01f54..5317e2f850 100644
--- a/libavcodec/d3d12va_encode_hevc.c
+++ b/libavcodec/d3d12va_encode_hevc.c
@@ -442,7 +442,11 @@ static int d3d12va_encode_hevc_configure(AVCodecContext *avctx)
     int fixed_qp_idr, fixed_qp_p, fixed_qp_b;
     int err;
 
-    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
+    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(priv->cbc, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/vaapi_encode_av1.c b/libavcodec/vaapi_encode_av1.c
index f3df5baddc..f80efd006e 100644
--- a/libavcodec/vaapi_encode_av1.c
+++ b/libavcodec/vaapi_encode_av1.c
@@ -128,7 +128,7 @@ static av_cold int vaapi_encode_av1_configure(AVCodecContext *avctx)
     VAAPIEncodeAV1Context *priv = avctx->priv_data;
     int ret;
 
-    ret = ff_cbs_init(&priv->cbc, AV_CODEC_ID_AV1, avctx);
+    ret = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_AV1, avctx);
     if (ret < 0)
         return ret;
     priv->cbc->trace_enable  = 1;
@@ -136,6 +136,10 @@ static av_cold int vaapi_encode_av1_configure(AVCodecContext *avctx)
     priv->cbc->trace_context = ctx;
     priv->cbc->trace_write_callback = vaapi_encode_av1_trace_write_log;
 
+    ret = ff_cbs_init(priv->cbc, NULL);
+    if (ret < 0)
+        return ret;
+
     if (ctx->rc_mode->quality) {
         priv->q_idx_p = av_clip(ctx->rc_quality, 0, AV1_MAX_QUANT);
         if (fabs(avctx->i_quant_factor) > 0.0)
diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c
index 0cd5b0ac50..7c98833944 100644
--- a/libavcodec/vaapi_encode_h264.c
+++ b/libavcodec/vaapi_encode_h264.c
@@ -874,7 +874,11 @@ static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
     VAAPIEncodeH264Context *priv = avctx->priv_data;
     int err;
 
-    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
+    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_H264, avctx);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(priv->cbc, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c
index 2acde2296b..0cfe67735b 100644
--- a/libavcodec/vaapi_encode_h265.c
+++ b/libavcodec/vaapi_encode_h265.c
@@ -967,7 +967,11 @@ static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
     VAAPIEncodeH265Context *priv = avctx->priv_data;
     int err;
 
-    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
+    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(priv->cbc, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/vaapi_encode_mjpeg.c b/libavcodec/vaapi_encode_mjpeg.c
index 2eef5bf090..5a7915cec4 100644
--- a/libavcodec/vaapi_encode_mjpeg.c
+++ b/libavcodec/vaapi_encode_mjpeg.c
@@ -474,7 +474,11 @@ static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
         ctx->va_packed_headers |=  VA_ENC_PACKED_HEADER_SLICE;
     }
 
-    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MJPEG, avctx);
+    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_MJPEG, avctx);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(priv->cbc, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/vaapi_encode_mpeg2.c b/libavcodec/vaapi_encode_mpeg2.c
index 94cb3d4fb6..3e0b7f9ed9 100644
--- a/libavcodec/vaapi_encode_mpeg2.c
+++ b/libavcodec/vaapi_encode_mpeg2.c
@@ -519,7 +519,11 @@ static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
     VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
     int err;
 
-    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
+    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(priv->cbc, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/vulkan_encode_h264.c b/libavcodec/vulkan_encode_h264.c
index f31b6d4069..be3edfe0d7 100644
--- a/libavcodec/vulkan_encode_h264.c
+++ b/libavcodec/vulkan_encode_h264.c
@@ -1057,10 +1057,14 @@ static int parse_feedback_units(AVCodecContext *avctx,
     CodedBitstreamContext *cbs;
     CodedBitstreamFragment au = { 0 };
 
-    err = ff_cbs_init(&cbs, AV_CODEC_ID_H264, avctx);
+    err = ff_cbs_alloc(&cbs, AV_CODEC_ID_H264, avctx);
     if (err < 0)
         return err;
 
+    err = ff_cbs_init(cbs, NULL);
+    if (err < 0)
+        goto fail;
+
     err = ff_cbs_read(cbs, &au, NULL, data, size);
     if (err < 0) {
         av_log(avctx, AV_LOG_ERROR, "Unable to parse feedback units, bad drivers: %s\n",
@@ -1540,7 +1544,11 @@ static av_cold int vulkan_encode_h264_init(AVCodecContext *avctx)
     }
 
     /* Init CBS */
-    err = ff_cbs_init(&enc->cbs, AV_CODEC_ID_H264, avctx);
+    err = ff_cbs_alloc(&enc->cbs, AV_CODEC_ID_H264, avctx);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(enc->cbs, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/vulkan_encode_h265.c b/libavcodec/vulkan_encode_h265.c
index d81d2de95a..af13edeec4 100644
--- a/libavcodec/vulkan_encode_h265.c
+++ b/libavcodec/vulkan_encode_h265.c
@@ -1210,10 +1210,14 @@ static int parse_feedback_units(AVCodecContext *avctx,
     CodedBitstreamContext *cbs;
     CodedBitstreamFragment au = { 0 };
 
-    err = ff_cbs_init(&cbs, AV_CODEC_ID_HEVC, avctx);
+    err = ff_cbs_alloc(&cbs, AV_CODEC_ID_HEVC, avctx);
     if (err < 0)
         return err;
 
+    err = ff_cbs_init(cbs, NULL);
+    if (err < 0)
+        goto fail;
+
     err = ff_cbs_read(cbs, &au, NULL, data, size);
     if (err < 0) {
         av_log(avctx, AV_LOG_ERROR, "Unable to parse feedback units, bad drivers: %s\n",
@@ -1677,7 +1681,11 @@ static av_cold int vulkan_encode_h265_init(AVCodecContext *avctx)
     base_ctx->decode_delay = base_ctx->max_b_depth;
 
     /* Init CBS */
-    err = ff_cbs_init(&enc->cbs, AV_CODEC_ID_HEVC, avctx);
+    err = ff_cbs_alloc(&enc->cbs, AV_CODEC_ID_HEVC, avctx);
+    if (err < 0)
+        return err;
+
+    err = ff_cbs_init(enc->cbs, NULL);
     if (err < 0)
         return err;
 
diff --git a/libavcodec/vvc/dec.c b/libavcodec/vvc/dec.c
index 49ba47d5d4..662e5edc60 100644
--- a/libavcodec/vvc/dec.c
+++ b/libavcodec/vvc/dec.c
@@ -1120,10 +1120,14 @@ static av_cold int vvc_decode_init(AVCodecContext *avctx)
 
     s->avctx = avctx;
 
-    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_VVC, avctx);
+    ret = ff_cbs_alloc(&s->cbc, AV_CODEC_ID_VVC, avctx);
     if (ret)
         return ret;
 
+    ret = ff_cbs_init(s->cbc, NULL);
+    if (ret < 0)
+        return ret;
+
     if (avctx->extradata_size > 0 && avctx->extradata) {
         ret = ff_cbs_read_extradata_from_codec(s->cbc, &s->current_frame, avctx);
         if (ret < 0)
diff --git a/libavcodec/vvc_parser.c b/libavcodec/vvc_parser.c
index c9c3a3949f..8b3c794dbe 100644
--- a/libavcodec/vvc_parser.c
+++ b/libavcodec/vvc_parser.c
@@ -482,7 +482,10 @@ static av_cold int vvc_parser_init(AVCodecParserContext *s)
     VVCParserContext *ctx = s->priv_data;
     int ret;
 
-    ret = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_VVC, NULL);
+    ret = ff_cbs_alloc(&ctx->cbc, AV_CODEC_ID_VVC, NULL);
+    if (ret < 0)
+        return ret;
+    ret = ff_cbs_init(ctx->cbc, NULL);
     if (ret < 0)
         return ret;
     au_detector_init(&ctx->au_detector);
diff --git a/libavformat/movenccenc.c b/libavformat/movenccenc.c
index 32094ebd7b..7966a978a8 100644
--- a/libavformat/movenccenc.c
+++ b/libavformat/movenccenc.c
@@ -618,7 +618,11 @@ int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key,
     ctx->use_subsamples = use_subsamples;
 
     if (codec_id == AV_CODEC_ID_AV1) {
-        ret = ff_lavf_cbs_init(&ctx->cbc, codec_id, NULL);
+        ret = ff_lavf_cbs_alloc(&ctx->cbc, codec_id, NULL);
+        if (ret < 0)
+            return ret;
+
+        ret = ff_lavf_cbs_init(ctx->cbc, NULL);
         if (ret < 0)
             return ret;
 
-- 
2.49.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] 6+ messages in thread

* [FFmpeg-devel] [PATCH 2/3] avcodec/cbs_av1: don't store a reference to the unit data if it's not needed
  2025-05-23  3:10 [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function James Almer
@ 2025-05-23  3:10 ` James Almer
  2025-05-23  3:10 ` [FFmpeg-devel] [PATCH 3/3] avcodec/av1_parser: use an AVBufferRef to avoid data copying James Almer
  2025-05-24 11:08 ` [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function Andreas Rheinhardt
  2 siblings, 0 replies; 6+ messages in thread
From: James Almer @ 2025-05-23  3:10 UTC (permalink / raw)
  To: ffmpeg-devel

If a module using CBS-AV1 doesn't bother to decompose redundant frame header
OBUs, then there's no need to keep a reference to the previous frame header
around.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/cbs_av1.c                 | 23 +++++++++++++++++++++++
 libavcodec/cbs_av1.h                 |  3 +++
 libavcodec/cbs_av1_syntax_template.c |  2 ++
 3 files changed, 28 insertions(+)

diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c
index 4edb6ecd50..1938c5acff 100644
--- a/libavcodec/cbs_av1.c
+++ b/libavcodec/cbs_av1.c
@@ -1104,6 +1104,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
         {
             err = cbs_av1_write_frame_header_obu(ctx, pbc,
                                                  &obu->obu.frame_header,
+                                                 priv->redundant &&
                                                  obu->header.obu_type ==
                                                  AV1_OBU_REDUNDANT_FRAME_HEADER,
                                                  NULL);
@@ -1265,6 +1266,27 @@ static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx,
 #endif
 }
 
+static int cbs_av1_init(CodedBitstreamContext *ctx)
+{
+    CodedBitstreamAV1Context *priv = ctx->priv_data;
+
+    if (ctx->decompose_unit_types) {
+        for (int i = 0; i < ctx->nb_decompose_unit_types; i++) {
+            switch (ctx->decompose_unit_types[i]) {
+            case AV1_OBU_REDUNDANT_FRAME_HEADER:
+                priv->redundant = 1;
+                break;
+            default:
+                break;
+            }
+        }
+    } else {
+        priv->redundant = 1;
+    }
+
+    return 0;
+}
+
 static void cbs_av1_flush(CodedBitstreamContext *ctx)
 {
     CodedBitstreamAV1Context *priv = ctx->priv_data;
@@ -1385,6 +1407,7 @@ const CodedBitstreamType CBS_FUNC(type_av1) = {
     .write_unit        = &cbs_av1_write_obu,
     .assemble_fragment = &cbs_av1_assemble_fragment,
 
+    .init              = &cbs_av1_init,
     .flush             = &cbs_av1_flush,
     .close             = &cbs_av1_close,
 };
diff --git a/libavcodec/cbs_av1.h b/libavcodec/cbs_av1.h
index 874f64561f..e79e90c6eb 100644
--- a/libavcodec/cbs_av1.h
+++ b/libavcodec/cbs_av1.h
@@ -490,6 +490,9 @@ typedef struct CodedBitstreamAV1Context {
 
     AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES];
 
+    // Init values
+    int redundant;
+
     // AVOptions
     int operating_point;
     // When writing, fix the length in bytes of the obu_size field.
diff --git a/libavcodec/cbs_av1_syntax_template.c b/libavcodec/cbs_av1_syntax_template.c
index 5518544a4d..701f474ce4 100644
--- a/libavcodec/cbs_av1_syntax_template.c
+++ b/libavcodec/cbs_av1_syntax_template.c
@@ -1795,6 +1795,7 @@ static int FUNC(frame_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
 #endif
             fh_bytes = (fh_bits + 7) / 8;
 
+            if (priv->redundant) {
             priv->frame_header_size = fh_bits;
 
             if (rw_buffer_ref) {
@@ -1810,6 +1811,7 @@ static int FUNC(frame_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
                 priv->frame_header = priv->frame_header_ref->data;
                 memcpy(priv->frame_header, fh_start, fh_bytes);
             }
+            }
         }
     }
 
-- 
2.49.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] 6+ messages in thread

* [FFmpeg-devel] [PATCH 3/3] avcodec/av1_parser: use an AVBufferRef to avoid data copying
  2025-05-23  3:10 [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function James Almer
  2025-05-23  3:10 ` [FFmpeg-devel] [PATCH 2/3] avcodec/cbs_av1: don't store a reference to the unit data if it's not needed James Almer
@ 2025-05-23  3:10 ` James Almer
  2025-05-24 10:56   ` Andreas Rheinhardt
  2025-05-24 11:08 ` [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function Andreas Rheinhardt
  2 siblings, 1 reply; 6+ messages in thread
From: James Almer @ 2025-05-23  3:10 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/av1_parser.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/libavcodec/av1_parser.c b/libavcodec/av1_parser.c
index 77906d0c91..b9a96ad59a 100644
--- a/libavcodec/av1_parser.c
+++ b/libavcodec/av1_parser.c
@@ -50,6 +50,11 @@ static const enum AVPixelFormat pix_fmts_rgb[3] = {
     AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
 };
 
+static void dummy_free(void *opaque, uint8_t *data)
+{
+    av_assert0(opaque == data);
+}
+
 static int av1_parser_parse(AVCodecParserContext *ctx,
                             AVCodecContext *avctx,
                             const uint8_t **out_data, int *out_size,
@@ -60,6 +65,7 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
     const CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
     const AV1RawSequenceHeader *seq;
     const AV1RawColorConfig *color;
+    AVBufferRef *ref = NULL;
     int ret;
 
     *out_data = data;
@@ -69,6 +75,11 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
     ctx->pict_type         = AV_PICTURE_TYPE_NONE;
     ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
 
+    ref = av_buffer_create((uint8_t *)data, size, dummy_free,
+                           (void *)data, AV_BUFFER_FLAG_READONLY);
+    if (!ref)
+        return size;
+
     s->cbc->log_ctx = avctx;
 
     if (avctx->extradata_size && !s->parsed_extradata) {
@@ -171,6 +182,8 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
 
 end:
     ff_cbs_fragment_reset(td);
+    av_assert1(av_buffer_get_ref_count(ref) == 1);
+    av_buffer_unref(&ref);
 
     s->cbc->log_ctx = NULL;
 
-- 
2.49.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] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 3/3] avcodec/av1_parser: use an AVBufferRef to avoid data copying
  2025-05-23  3:10 ` [FFmpeg-devel] [PATCH 3/3] avcodec/av1_parser: use an AVBufferRef to avoid data copying James Almer
@ 2025-05-24 10:56   ` Andreas Rheinhardt
  0 siblings, 0 replies; 6+ messages in thread
From: Andreas Rheinhardt @ 2025-05-24 10:56 UTC (permalink / raw)
  To: ffmpeg-devel

James Almer:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavcodec/av1_parser.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/libavcodec/av1_parser.c b/libavcodec/av1_parser.c
> index 77906d0c91..b9a96ad59a 100644
> --- a/libavcodec/av1_parser.c
> +++ b/libavcodec/av1_parser.c
> @@ -50,6 +50,11 @@ static const enum AVPixelFormat pix_fmts_rgb[3] = {
>      AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
>  };
>  
> +static void dummy_free(void *opaque, uint8_t *data)
> +{
> +    av_assert0(opaque == data);
> +}
> +
>  static int av1_parser_parse(AVCodecParserContext *ctx,
>                              AVCodecContext *avctx,
>                              const uint8_t **out_data, int *out_size,
> @@ -60,6 +65,7 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
>      const CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
>      const AV1RawSequenceHeader *seq;
>      const AV1RawColorConfig *color;
> +    AVBufferRef *ref = NULL;
>      int ret;
>  
>      *out_data = data;
> @@ -69,6 +75,11 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
>      ctx->pict_type         = AV_PICTURE_TYPE_NONE;
>      ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
>  
> +    ref = av_buffer_create((uint8_t *)data, size, dummy_free,
> +                           (void *)data, AV_BUFFER_FLAG_READONLY);
> +    if (!ref)
> +        return size;
> +
>      s->cbc->log_ctx = avctx;
>  
>      if (avctx->extradata_size && !s->parsed_extradata) {
> @@ -171,6 +182,8 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
>  
>  end:
>      ff_cbs_fragment_reset(td);
> +    av_assert1(av_buffer_get_ref_count(ref) == 1);
> +    av_buffer_unref(&ref);
>  
>      s->cbc->log_ctx = NULL;
>  

Lying to CBS (and the AVBuffer API) is an ugly hack which just happens
to work and relies on knowledge of the internals of CBS AV1. I very much
prefer my approach in
https://ffmpeg.org/pipermail/ffmpeg-devel/2025-May/343195.html (where
CBS is explicitly told of the desire not to create additional references).

- Andreas

_______________________________________________
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] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function
  2025-05-23  3:10 [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function James Almer
  2025-05-23  3:10 ` [FFmpeg-devel] [PATCH 2/3] avcodec/cbs_av1: don't store a reference to the unit data if it's not needed James Almer
  2025-05-23  3:10 ` [FFmpeg-devel] [PATCH 3/3] avcodec/av1_parser: use an AVBufferRef to avoid data copying James Almer
@ 2025-05-24 11:08 ` Andreas Rheinhardt
  2025-05-25  2:41   ` James Almer
  2 siblings, 1 reply; 6+ messages in thread
From: Andreas Rheinhardt @ 2025-05-24 11:08 UTC (permalink / raw)
  To: ffmpeg-devel

James Almer:
> And rename the existing one to ff_cbs_alloc().
> This will allow for more versatility when setting options in a module, allowing
> them to be taken into account when calling module specific init functions.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavcodec/apv_decode.c          |  6 +++++-
>  libavcodec/apv_parser.c          |  6 +++++-
>  libavcodec/av1_parser.c          |  6 +++++-
>  libavcodec/av1dec.c              | 14 +++++++++++---
>  libavcodec/bsf/av1_frame_merge.c | 16 ++++++++++++++--
>  libavcodec/bsf/av1_frame_split.c |  6 +++++-
>  libavcodec/bsf/dts2pts.c         |  6 +++++-
>  libavcodec/bsf/filter_units.c    |  6 +++++-
>  libavcodec/bsf/trace_headers.c   |  2 +-
>  libavcodec/cbs.c                 | 28 +++++++++++++++++++++++++++-
>  libavcodec/cbs.h                 | 10 ++++++++--
>  libavcodec/cbs_bsf.c             | 12 ++++++++++--
>  libavcodec/cbs_internal.h        |  3 +++
>  libavcodec/d3d12va_encode_hevc.c |  6 +++++-
>  libavcodec/vaapi_encode_av1.c    |  6 +++++-
>  libavcodec/vaapi_encode_h264.c   |  6 +++++-
>  libavcodec/vaapi_encode_h265.c   |  6 +++++-
>  libavcodec/vaapi_encode_mjpeg.c  |  6 +++++-
>  libavcodec/vaapi_encode_mpeg2.c  |  6 +++++-
>  libavcodec/vulkan_encode_h264.c  | 12 ++++++++++--
>  libavcodec/vulkan_encode_h265.c  | 12 ++++++++++--
>  libavcodec/vvc/dec.c             |  6 +++++-
>  libavcodec/vvc_parser.c          |  5 ++++-
>  libavformat/movenccenc.c         |  6 +++++-
>  24 files changed, 168 insertions(+), 30 deletions(-)
> 
> diff --git a/libavcodec/apv_decode.c b/libavcodec/apv_decode.c
> index eb47298e2e..3667933df6 100644
> --- a/libavcodec/apv_decode.c
> +++ b/libavcodec/apv_decode.c
> @@ -119,7 +119,11 @@ static av_cold int apv_decode_init(AVCodecContext *avctx)
>  
>      ff_thread_once(&apv_entropy_once, apv_entropy_build_decode_lut);
>  
> -    err = ff_cbs_init(&apv->cbc, AV_CODEC_ID_APV, avctx);
> +    err = ff_cbs_alloc(&apv->cbc, AV_CODEC_ID_APV, avctx);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(apv->cbc, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/apv_parser.c b/libavcodec/apv_parser.c
> index fdd575339b..e0aa152ca8 100644
> --- a/libavcodec/apv_parser.c
> +++ b/libavcodec/apv_parser.c
> @@ -122,7 +122,11 @@ static av_cold int init(AVCodecParserContext *s)
>      APVParseContext *p = s->priv_data;
>      int ret;
>  
> -    ret = ff_cbs_init(&p->cbc, AV_CODEC_ID_APV, NULL);
> +    ret = ff_cbs_alloc(&p->cbc, AV_CODEC_ID_APV, NULL);
> +    if (ret < 0)
> +        return ret;
> +
> +    ret = ff_cbs_init(p->cbc, NULL);
>      if (ret < 0)
>          return ret;
>  
> diff --git a/libavcodec/av1_parser.c b/libavcodec/av1_parser.c
> index 1792e813f4..77906d0c91 100644
> --- a/libavcodec/av1_parser.c
> +++ b/libavcodec/av1_parser.c
> @@ -190,13 +190,17 @@ static av_cold int av1_parser_init(AVCodecParserContext *ctx)
>      AV1ParseContext *s = ctx->priv_data;
>      int ret;
>  
> -    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
> +    ret = ff_cbs_alloc(&s->cbc, AV_CODEC_ID_AV1, NULL);
>      if (ret < 0)
>          return ret;
>  
>      s->cbc->decompose_unit_types    = decompose_unit_types;
>      s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
>  
> +    ret = ff_cbs_init(s->cbc, NULL);
> +    if (ret < 0)
> +        return ret;
> +
>      return 0;
>  }
>  
> diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
> index 8ff1bf394c..3130364534 100644
> --- a/libavcodec/av1dec.c
> +++ b/libavcodec/av1dec.c
> @@ -858,6 +858,7 @@ static av_cold int av1_decode_init(AVCodecContext *avctx)
>  {
>      AV1DecContext *s = avctx->priv_data;
>      AV1RawSequenceHeader *seq;
> +    AVDictionary *options = NULL;
>      const AVPacketSideData *sd;
>      int ret;
>  
> @@ -865,20 +866,27 @@ static av_cold int av1_decode_init(AVCodecContext *avctx)
>      s->pkt = avctx->internal->in_pkt;
>      s->pix_fmt = AV_PIX_FMT_NONE;
>  
> -    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
> +    ret = ff_cbs_alloc(&s->cbc, AV_CODEC_ID_AV1, avctx);
>      if (ret < 0)
>          return ret;
>  
>      s->cbc->decompose_unit_types    = decompose_unit_types;
>      s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
>  
> +    ret = av_dict_set_int(&options, "operating_point", s->operating_point, 0);
> +    if (ret < 0)
> +        return ret;
> +
> +    ret = ff_cbs_init(s->cbc, &options);
> +    av_dict_free(&options);
> +    if (ret < 0)
> +        return ret;
> +
>      s->itut_t35_fifo = av_fifo_alloc2(1, sizeof(AV1RawMetadataITUTT35),
>                                        AV_FIFO_FLAG_AUTO_GROW);
>      if (!s->itut_t35_fifo)
>          return AVERROR(ENOMEM);
>  
> -    av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
> -
>      if (avctx->extradata && avctx->extradata_size) {
>          ret = ff_cbs_read_extradata_from_codec(s->cbc,
>                                                 &s->current_obu,
> diff --git a/libavcodec/bsf/av1_frame_merge.c b/libavcodec/bsf/av1_frame_merge.c
> index 4c54f2167e..530688be49 100644
> --- a/libavcodec/bsf/av1_frame_merge.c
> +++ b/libavcodec/bsf/av1_frame_merge.c
> @@ -133,11 +133,23 @@ static int av1_frame_merge_init(AVBSFContext *bsf)
>      if (!ctx->in || !ctx->pkt)
>          return AVERROR(ENOMEM);
>  
> -    err =  ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
> +    err =  ff_cbs_alloc(&ctx->input, AV_CODEC_ID_AV1, bsf);
>      if (err < 0)
>          return err;
>  
> -    return ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
> +    err =  ff_cbs_alloc(&ctx->output, AV_CODEC_ID_AV1, bsf);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(ctx->input, NULL);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(ctx->output, NULL);
> +    if (err < 0)
> +        return err;
> +
> +    return 0;
>  }
>  
>  static void av1_frame_merge_close(AVBSFContext *bsf)
> diff --git a/libavcodec/bsf/av1_frame_split.c b/libavcodec/bsf/av1_frame_split.c
> index 5f6a40316c..a64e9178e7 100644
> --- a/libavcodec/bsf/av1_frame_split.c
> +++ b/libavcodec/bsf/av1_frame_split.c
> @@ -210,7 +210,11 @@ static int av1_frame_split_init(AVBSFContext *ctx)
>      if (!s->buffer_pkt)
>          return AVERROR(ENOMEM);
>  
> -    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, ctx);
> +    ret = ff_cbs_alloc(&s->cbc, AV_CODEC_ID_AV1, ctx);
> +    if (ret < 0)
> +        return ret;
> +
> +    ret = ff_cbs_init(s->cbc, NULL);
>      if (ret < 0)
>          return ret;
>  
> diff --git a/libavcodec/bsf/dts2pts.c b/libavcodec/bsf/dts2pts.c
> index 9d31d7dc08..189a9f9678 100644
> --- a/libavcodec/bsf/dts2pts.c
> +++ b/libavcodec/bsf/dts2pts.c
> @@ -402,7 +402,11 @@ static int dts2pts_init(AVBSFContext *ctx)
>      if (!s->node_pool)
>          return AVERROR(ENOMEM);
>  
> -    ret = ff_cbs_init(&s->cbc, ctx->par_in->codec_id, ctx);
> +    ret = ff_cbs_alloc(&s->cbc, ctx->par_in->codec_id, ctx);
> +    if (ret < 0)
> +        return ret;
> +
> +    ret = ff_cbs_init(s->cbc, NULL);
>      if (ret < 0)
>          return ret;
>  
> diff --git a/libavcodec/bsf/filter_units.c b/libavcodec/bsf/filter_units.c
> index 336331733f..0edf2fa472 100644
> --- a/libavcodec/bsf/filter_units.c
> +++ b/libavcodec/bsf/filter_units.c
> @@ -187,7 +187,11 @@ static int filter_units_init(AVBSFContext *bsf)
>          return 0;
>      }
>  
> -    err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
> +    err = ff_cbs_alloc(&ctx->cbc, bsf->par_in->codec_id, bsf);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(ctx->cbc, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/bsf/trace_headers.c b/libavcodec/bsf/trace_headers.c
> index 8781f5f100..e885f7a73f 100644
> --- a/libavcodec/bsf/trace_headers.c
> +++ b/libavcodec/bsf/trace_headers.c
> @@ -38,7 +38,7 @@ static int trace_headers_init(AVBSFContext *bsf)
>      TraceHeadersContext *ctx = bsf->priv_data;
>      int err;
>  
> -    err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
> +    err = ff_cbs_alloc(&ctx->cbc, bsf->par_in->codec_id, bsf);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
> index 6b2ebe597d..8c0659f7af 100644
> --- a/libavcodec/cbs.c
> +++ b/libavcodec/cbs.c
> @@ -21,6 +21,7 @@
>  #include "libavutil/avassert.h"
>  #include "libavutil/buffer.h"
>  #include "libavutil/common.h"
> +#include "libavutil/dict.h"
>  #include "libavutil/mem.h"
>  #include "libavutil/opt.h"
>  
> @@ -91,7 +92,7 @@ const enum AVCodecID CBS_FUNC(all_codec_ids)[] = {
>      AV_CODEC_ID_NONE
>  };
>  
> -av_cold int CBS_FUNC(init)(CodedBitstreamContext **ctx_ptr,
> +av_cold int CBS_FUNC(alloc)(CodedBitstreamContext **ctx_ptr,
>                          enum AVCodecID codec_id, void *log_ctx)
>  {
>      CodedBitstreamContext *ctx;
> @@ -137,6 +138,31 @@ av_cold int CBS_FUNC(init)(CodedBitstreamContext **ctx_ptr,
>      return 0;
>  }
>  
> +av_cold int CBS_FUNC(init)(CodedBitstreamContext *ctx, AVDictionary **options)
> +{
> +    int err;
> +
> +    if (!ctx->codec)
> +        return AVERROR(EINVAL);
> +
> +    if (ctx->codec->priv_data_size) {
> +        if (!ctx->priv_data)
> +            return AVERROR(EINVAL);
> +
> +        err = av_opt_set_dict2(ctx->priv_data, options, 0);
> +        if (err < 0)
> +            return err;
> +    }
> +
> +    if (ctx->codec->init) {
> +        err = ctx->codec->init(ctx);
> +        if (err < 0)
> +            return err;
> +    }
> +
> +    return 0;
> +}
> +
>  av_cold void CBS_FUNC(flush)(CodedBitstreamContext *ctx)
>  {
>      if (ctx->codec->flush)
> diff --git a/libavcodec/cbs.h b/libavcodec/cbs.h
> index 67f2ec9e50..5b0e1f5c6b 100644
> --- a/libavcodec/cbs.h
> +++ b/libavcodec/cbs.h
> @@ -176,6 +176,7 @@ typedef struct CodedBitstreamFragment {
>  } CodedBitstreamFragment;
>  
>  
> +struct AVDictionary;
>  struct CodedBitstreamContext;
>  struct GetBitContext;
>  struct PutBitContext;
> @@ -305,11 +306,16 @@ extern const enum AVCodecID CBS_FUNC(all_codec_ids)[];
>  
>  
>  /**
> - * Create and initialise a new context for the given codec.
> + * Create a new context for the given codec.
>   */
> -int CBS_FUNC(init)(CodedBitstreamContext **ctx,
> +int CBS_FUNC(alloc)(CodedBitstreamContext **ctx,
>                  enum AVCodecID codec_id, void *log_ctx);
>  
> +/**
> + * Initialise a context.
> + */
> +int CBS_FUNC(init)(CodedBitstreamContext *ctx, struct AVDictionary **options);
> +
>  /**
>   * Reset all internal state in a context.
>   */
> diff --git a/libavcodec/cbs_bsf.c b/libavcodec/cbs_bsf.c
> index b25285483b..25b9e730d8 100644
> --- a/libavcodec/cbs_bsf.c
> +++ b/libavcodec/cbs_bsf.c
> @@ -115,11 +115,19 @@ int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
>  
>      ctx->type = type;
>  
> -    err = ff_cbs_init(&ctx->input, type->codec_id, bsf);
> +    err = ff_cbs_alloc(&ctx->input, type->codec_id, bsf);
>      if (err < 0)
>          return err;
>  
> -    err = ff_cbs_init(&ctx->output, type->codec_id, bsf);
> +    err = ff_cbs_alloc(&ctx->output, type->codec_id, bsf);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(ctx->input, NULL);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(ctx->output, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h
> index c3265924ba..734269d368 100644
> --- a/libavcodec/cbs_internal.h
> +++ b/libavcodec/cbs_internal.h
> @@ -184,6 +184,9 @@ typedef struct CodedBitstreamType {
>      int (*assemble_fragment)(CodedBitstreamContext *ctx,
>                               CodedBitstreamFragment *frag);
>  
> +    // Initialize the codec internal state.
> +    int (*init)(CodedBitstreamContext *ctx);
> +
>      // Reset the codec internal state.
>      void (*flush)(CodedBitstreamContext *ctx);
>  
> diff --git a/libavcodec/d3d12va_encode_hevc.c b/libavcodec/d3d12va_encode_hevc.c
> index 938ba01f54..5317e2f850 100644
> --- a/libavcodec/d3d12va_encode_hevc.c
> +++ b/libavcodec/d3d12va_encode_hevc.c
> @@ -442,7 +442,11 @@ static int d3d12va_encode_hevc_configure(AVCodecContext *avctx)
>      int fixed_qp_idr, fixed_qp_p, fixed_qp_b;
>      int err;
>  
> -    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
> +    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(priv->cbc, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/vaapi_encode_av1.c b/libavcodec/vaapi_encode_av1.c
> index f3df5baddc..f80efd006e 100644
> --- a/libavcodec/vaapi_encode_av1.c
> +++ b/libavcodec/vaapi_encode_av1.c
> @@ -128,7 +128,7 @@ static av_cold int vaapi_encode_av1_configure(AVCodecContext *avctx)
>      VAAPIEncodeAV1Context *priv = avctx->priv_data;
>      int ret;
>  
> -    ret = ff_cbs_init(&priv->cbc, AV_CODEC_ID_AV1, avctx);
> +    ret = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_AV1, avctx);
>      if (ret < 0)
>          return ret;
>      priv->cbc->trace_enable  = 1;
> @@ -136,6 +136,10 @@ static av_cold int vaapi_encode_av1_configure(AVCodecContext *avctx)
>      priv->cbc->trace_context = ctx;
>      priv->cbc->trace_write_callback = vaapi_encode_av1_trace_write_log;
>  
> +    ret = ff_cbs_init(priv->cbc, NULL);
> +    if (ret < 0)
> +        return ret;
> +
>      if (ctx->rc_mode->quality) {
>          priv->q_idx_p = av_clip(ctx->rc_quality, 0, AV1_MAX_QUANT);
>          if (fabs(avctx->i_quant_factor) > 0.0)
> diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c
> index 0cd5b0ac50..7c98833944 100644
> --- a/libavcodec/vaapi_encode_h264.c
> +++ b/libavcodec/vaapi_encode_h264.c
> @@ -874,7 +874,11 @@ static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
>      VAAPIEncodeH264Context *priv = avctx->priv_data;
>      int err;
>  
> -    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
> +    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_H264, avctx);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(priv->cbc, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c
> index 2acde2296b..0cfe67735b 100644
> --- a/libavcodec/vaapi_encode_h265.c
> +++ b/libavcodec/vaapi_encode_h265.c
> @@ -967,7 +967,11 @@ static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
>      VAAPIEncodeH265Context *priv = avctx->priv_data;
>      int err;
>  
> -    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
> +    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(priv->cbc, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/vaapi_encode_mjpeg.c b/libavcodec/vaapi_encode_mjpeg.c
> index 2eef5bf090..5a7915cec4 100644
> --- a/libavcodec/vaapi_encode_mjpeg.c
> +++ b/libavcodec/vaapi_encode_mjpeg.c
> @@ -474,7 +474,11 @@ static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
>          ctx->va_packed_headers |=  VA_ENC_PACKED_HEADER_SLICE;
>      }
>  
> -    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MJPEG, avctx);
> +    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_MJPEG, avctx);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(priv->cbc, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/vaapi_encode_mpeg2.c b/libavcodec/vaapi_encode_mpeg2.c
> index 94cb3d4fb6..3e0b7f9ed9 100644
> --- a/libavcodec/vaapi_encode_mpeg2.c
> +++ b/libavcodec/vaapi_encode_mpeg2.c
> @@ -519,7 +519,11 @@ static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
>      VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
>      int err;
>  
> -    err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
> +    err = ff_cbs_alloc(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(priv->cbc, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/vulkan_encode_h264.c b/libavcodec/vulkan_encode_h264.c
> index f31b6d4069..be3edfe0d7 100644
> --- a/libavcodec/vulkan_encode_h264.c
> +++ b/libavcodec/vulkan_encode_h264.c
> @@ -1057,10 +1057,14 @@ static int parse_feedback_units(AVCodecContext *avctx,
>      CodedBitstreamContext *cbs;
>      CodedBitstreamFragment au = { 0 };
>  
> -    err = ff_cbs_init(&cbs, AV_CODEC_ID_H264, avctx);
> +    err = ff_cbs_alloc(&cbs, AV_CODEC_ID_H264, avctx);
>      if (err < 0)
>          return err;
>  
> +    err = ff_cbs_init(cbs, NULL);
> +    if (err < 0)
> +        goto fail;
> +
>      err = ff_cbs_read(cbs, &au, NULL, data, size);
>      if (err < 0) {
>          av_log(avctx, AV_LOG_ERROR, "Unable to parse feedback units, bad drivers: %s\n",
> @@ -1540,7 +1544,11 @@ static av_cold int vulkan_encode_h264_init(AVCodecContext *avctx)
>      }
>  
>      /* Init CBS */
> -    err = ff_cbs_init(&enc->cbs, AV_CODEC_ID_H264, avctx);
> +    err = ff_cbs_alloc(&enc->cbs, AV_CODEC_ID_H264, avctx);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(enc->cbs, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/vulkan_encode_h265.c b/libavcodec/vulkan_encode_h265.c
> index d81d2de95a..af13edeec4 100644
> --- a/libavcodec/vulkan_encode_h265.c
> +++ b/libavcodec/vulkan_encode_h265.c
> @@ -1210,10 +1210,14 @@ static int parse_feedback_units(AVCodecContext *avctx,
>      CodedBitstreamContext *cbs;
>      CodedBitstreamFragment au = { 0 };
>  
> -    err = ff_cbs_init(&cbs, AV_CODEC_ID_HEVC, avctx);
> +    err = ff_cbs_alloc(&cbs, AV_CODEC_ID_HEVC, avctx);
>      if (err < 0)
>          return err;
>  
> +    err = ff_cbs_init(cbs, NULL);
> +    if (err < 0)
> +        goto fail;
> +
>      err = ff_cbs_read(cbs, &au, NULL, data, size);
>      if (err < 0) {
>          av_log(avctx, AV_LOG_ERROR, "Unable to parse feedback units, bad drivers: %s\n",
> @@ -1677,7 +1681,11 @@ static av_cold int vulkan_encode_h265_init(AVCodecContext *avctx)
>      base_ctx->decode_delay = base_ctx->max_b_depth;
>  
>      /* Init CBS */
> -    err = ff_cbs_init(&enc->cbs, AV_CODEC_ID_HEVC, avctx);
> +    err = ff_cbs_alloc(&enc->cbs, AV_CODEC_ID_HEVC, avctx);
> +    if (err < 0)
> +        return err;
> +
> +    err = ff_cbs_init(enc->cbs, NULL);
>      if (err < 0)
>          return err;
>  
> diff --git a/libavcodec/vvc/dec.c b/libavcodec/vvc/dec.c
> index 49ba47d5d4..662e5edc60 100644
> --- a/libavcodec/vvc/dec.c
> +++ b/libavcodec/vvc/dec.c
> @@ -1120,10 +1120,14 @@ static av_cold int vvc_decode_init(AVCodecContext *avctx)
>  
>      s->avctx = avctx;
>  
> -    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_VVC, avctx);
> +    ret = ff_cbs_alloc(&s->cbc, AV_CODEC_ID_VVC, avctx);
>      if (ret)
>          return ret;
>  
> +    ret = ff_cbs_init(s->cbc, NULL);
> +    if (ret < 0)
> +        return ret;
> +
>      if (avctx->extradata_size > 0 && avctx->extradata) {
>          ret = ff_cbs_read_extradata_from_codec(s->cbc, &s->current_frame, avctx);
>          if (ret < 0)
> diff --git a/libavcodec/vvc_parser.c b/libavcodec/vvc_parser.c
> index c9c3a3949f..8b3c794dbe 100644
> --- a/libavcodec/vvc_parser.c
> +++ b/libavcodec/vvc_parser.c
> @@ -482,7 +482,10 @@ static av_cold int vvc_parser_init(AVCodecParserContext *s)
>      VVCParserContext *ctx = s->priv_data;
>      int ret;
>  
> -    ret = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_VVC, NULL);
> +    ret = ff_cbs_alloc(&ctx->cbc, AV_CODEC_ID_VVC, NULL);
> +    if (ret < 0)
> +        return ret;
> +    ret = ff_cbs_init(ctx->cbc, NULL);
>      if (ret < 0)
>          return ret;
>      au_detector_init(&ctx->au_detector);
> diff --git a/libavformat/movenccenc.c b/libavformat/movenccenc.c
> index 32094ebd7b..7966a978a8 100644
> --- a/libavformat/movenccenc.c
> +++ b/libavformat/movenccenc.c
> @@ -618,7 +618,11 @@ int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key,
>      ctx->use_subsamples = use_subsamples;
>  
>      if (codec_id == AV_CODEC_ID_AV1) {
> -        ret = ff_lavf_cbs_init(&ctx->cbc, codec_id, NULL);
> +        ret = ff_lavf_cbs_alloc(&ctx->cbc, codec_id, NULL);
> +        if (ret < 0)
> +            return ret;
> +
> +        ret = ff_lavf_cbs_init(ctx->cbc, NULL);
>          if (ret < 0)
>              return ret;
>  

If something like this were ever needed, it should not be implemented
like this; instead ff_cbs_init() should be like avformat_open_input()
and allocate a context if none was provided.

- Andreas

_______________________________________________
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] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function
  2025-05-24 11:08 ` [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function Andreas Rheinhardt
@ 2025-05-25  2:41   ` James Almer
  0 siblings, 0 replies; 6+ messages in thread
From: James Almer @ 2025-05-25  2:41 UTC (permalink / raw)
  To: ffmpeg-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 388 bytes --]

On 5/24/2025 8:08 AM, Andreas Rheinhardt wrote:
> If something like this were ever needed, it should not be implemented
> like this; instead ff_cbs_init() should be like avformat_open_input()
> and allocate a context if none was provided.
> 
> - Andreas

I didn't do that because it would require ff_cbs_init() to have the 
codec type and logctx as arguments in its signature.


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 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] 6+ messages in thread

end of thread, other threads:[~2025-05-25  2:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-23  3:10 [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function James Almer
2025-05-23  3:10 ` [FFmpeg-devel] [PATCH 2/3] avcodec/cbs_av1: don't store a reference to the unit data if it's not needed James Almer
2025-05-23  3:10 ` [FFmpeg-devel] [PATCH 3/3] avcodec/av1_parser: use an AVBufferRef to avoid data copying James Almer
2025-05-24 10:56   ` Andreas Rheinhardt
2025-05-24 11:08 ` [FFmpeg-devel] [PATCH 1/3] avcodec/cbs: add a new init function Andreas Rheinhardt
2025-05-25  2:41   ` James Almer

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