[FFmpeg-devel] [PATCH 3/3] avcodec/av1_parser: use an AVBufferRef to avoid data copying
Andreas Rheinhardt
andreas.rheinhardt at outlook.com
Sat May 24 13:56:44 EEST 2025
James Almer:
> Signed-off-by: James Almer <jamrial at 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
More information about the ffmpeg-devel
mailing list