[FFmpeg-devel] [PATCH] add media100 bsf and use it

Anton Khirnov anton at khirnov.net
Tue Jan 31 14:48:24 EET 2023


Quoting Paul B Mahol (2023-01-31 11:18:52)
> Patch attached.
> 
> From c9ec824211cccb745b3a4ab014d6be726c8ef1b9 Mon Sep 17 00:00:00 2001
> From: Paul B Mahol <onemda at gmail.com>
> Date: Tue, 31 Jan 2023 10:18:17 +0100
> Subject: [PATCH] avcodec: add media100_to_mjpegb bitstream filter and use it
> 
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  libavcodec/Makefile                           |   3 +-
>  libavcodec/bitstream_filters.c                |   1 +
>  .../{media100.c => media100_to_mjpegb_bsf.c}  | 112 +++++++-----------
>  libavcodec/mjpegbdec.c                        |  15 +++
>  4 files changed, 61 insertions(+), 70 deletions(-)
>  rename libavcodec/{media100.c => media100_to_mjpegb_bsf.c} (65%)

Needs media100_decoder_select="media100_to_mjpegb_bsf" in configure and
a changelog entry.

> -static av_cold int media100_decode_init(AVCodecContext *avctx)
> +static av_cold int init(AVBSFContext *ctx)
>  {
> -    Media100Context *ctx = avctx->priv_data;
> -    const AVCodec *codec;
> -    int ret;
> -
> -    codec = avcodec_find_decoder(AV_CODEC_ID_MJPEGB);
> -    if (!codec)
> -        return AVERROR_BUG;
> -    ctx->avctx = avcodec_alloc_context3(codec);
> -    if (!ctx->avctx)
> -        return AVERROR(ENOMEM);
> -    ctx->avctx->thread_count = 1;
> -    ctx->avctx->flags  = avctx->flags;
> -    ctx->avctx->flags2 = avctx->flags2;
> -    ctx->avctx->width  = ctx->avctx->coded_width  = avctx->width;
> -    ctx->avctx->height = ctx->avctx->coded_height = avctx->height;
> -
> -    ret = avcodec_open2(ctx->avctx, codec, NULL);
> -    if (ret < 0)
> -        return ret;
> +    Media100Context *s = ctx->priv_data;
>  
> -    ctx->pkt = av_packet_alloc();
> -    if (!ctx->pkt)
> +    s->pkt = av_packet_alloc();
> +    if (!s->pkt)
>          return AVERROR(ENOMEM);

Should set ctx->par_out->codec_id to AV_CODEC_ID_MJPEGB, so people can
use this bsf standalone.

>  
>      return 0;
>  }
>  
> -static int media100_decode_frame(AVCodecContext *avctx,
> -                                 AVFrame *frame, int *got_frame,
> -                                 AVPacket *avpkt)
> +static int filter(AVBSFContext *ctx, AVPacket *avpkt)
>  {
> -    Media100Context *ctx = avctx->priv_data;
> +    Media100Context *s = ctx->priv_data;
>      unsigned second_field_offset = 0;
>      unsigned next_field = 0;
>      unsigned dht_offset[2];
> @@ -83,18 +62,20 @@ static int media100_decode_frame(AVCodecContext *avctx,
>      AVPacket *pkt;
>      int ret;
>  
> -    if (avpkt->size + 1024 > ctx->pkt->size) {
> -        ret = av_grow_packet(ctx->pkt, avpkt->size + 1024 - ctx->pkt->size);
> -        if (ret < 0)
> -            return ret;
> -    }
> +    ret = ff_bsf_get_packet_ref(ctx, avpkt);
> +    if (ret < 0)
> +        return ret;
>  
> -    ret = av_packet_make_writable(ctx->pkt);
> +    ret = av_new_packet(s->pkt, avpkt->size + 1024);
> +    if (ret < 0)
> +        return ret;
> +
> +    ret = av_packet_make_writable(s->pkt);
>      if (ret < 0)
>          return ret;
>  
>      bytestream2_init(&gb, avpkt->data, avpkt->size);
> -    bytestream2_init_writer(&pb, ctx->pkt->data, ctx->pkt->size);
> +    bytestream2_init_writer(&pb, s->pkt->data, s->pkt->size);
>  
>  second_field:
>      bytestream2_put_be32(&pb, 0);
> @@ -107,8 +88,8 @@ second_field:
>      sof_offset[field] = bytestream2_tell_p(&pb);
>      bytestream2_put_be16(&pb, 17);
>      bytestream2_put_byte(&pb, 8);
> -    bytestream2_put_be16(&pb, avctx->height / 2);
> -    bytestream2_put_be16(&pb, avctx->width);
> +    bytestream2_put_be16(&pb, ctx->par_in->height / 2);
> +    bytestream2_put_be16(&pb, ctx->par_in->width);
>      bytestream2_put_byte(&pb, 3);
>      bytestream2_put_byte(&pb, 1);
>      bytestream2_put_byte(&pb, 0x21);
> @@ -164,7 +145,7 @@ second_field:
>          goto second_field;
>      }
>  
> -    pkt = ctx->pkt;
> +    pkt = s->pkt;
>  
>      AV_WB32(pkt->data +  8, second_field_offset);
>      AV_WB32(pkt->data + 12, second_field_offset);
> @@ -186,40 +167,33 @@ second_field:
>  
>      pkt->size = bytestream2_tell_p(&pb);
>  
> -    ret = avcodec_send_packet(ctx->avctx, pkt);
> -    if (ret < 0) {
> -        av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
> -        return ret;
> -    }
> +    av_packet_copy_props(pkt, avpkt);

check the return value

A FATE test would be nice.

-- 
Anton Khirnov


More information about the ffmpeg-devel mailing list