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] add media100 bsf and use it
@ 2023-01-31 10:18 Paul B Mahol
  2023-01-31 11:20 ` Andreas Rheinhardt
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Paul B Mahol @ 2023-01-31 10:18 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 16 bytes --]

Patch attached.

[-- Attachment #2: 0001-avcodec-add-media100_to_mjpegb-bitstream-filter-and-.patch --]
[-- Type: text/x-patch, Size: 9035 bytes --]

From c9ec824211cccb745b3a4ab014d6be726c8ef1b9 Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@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@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%)

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index df76d37598..4971832ff4 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -485,7 +485,7 @@ OBJS-$(CONFIG_MACE6_DECODER)           += mace.o
 OBJS-$(CONFIG_MAGICYUV_DECODER)        += magicyuv.o
 OBJS-$(CONFIG_MAGICYUV_ENCODER)        += magicyuvenc.o
 OBJS-$(CONFIG_MDEC_DECODER)            += mdec.o mpeg12.o mpeg12data.o
-OBJS-$(CONFIG_MEDIA100_DECODER)        += media100.o
+OBJS-$(CONFIG_MEDIA100_DECODER)        += mjpegbdec.o
 OBJS-$(CONFIG_METASOUND_DECODER)       += metasound.o twinvq.o
 OBJS-$(CONFIG_MICRODVD_DECODER)        += microdvddec.o ass.o
 OBJS-$(CONFIG_MIMIC_DECODER)           += mimic.o
@@ -1221,6 +1221,7 @@ OBJS-$(CONFIG_HEVC_METADATA_BSF)          += h265_metadata_bsf.o h265_profile_le
                                              h2645data.o
 OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF)       += hevc_mp4toannexb_bsf.o
 OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF)        += imx_dump_header_bsf.o
+OBJS-$(CONFIG_MEDIA100_TO_MJPEGB_BSF)     += media100_to_mjpegb_bsf.o
 OBJS-$(CONFIG_MJPEG2JPEG_BSF)             += mjpeg2jpeg_bsf.o
 OBJS-$(CONFIG_MJPEGA_DUMP_HEADER_BSF)     += mjpega_dump_header_bsf.o
 OBJS-$(CONFIG_MPEG4_UNPACK_BFRAMES_BSF)   += mpeg4_unpack_bframes_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index a3bebefe5f..e8216819ca 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -43,6 +43,7 @@ extern const FFBitStreamFilter ff_hapqa_extract_bsf;
 extern const FFBitStreamFilter ff_hevc_metadata_bsf;
 extern const FFBitStreamFilter ff_hevc_mp4toannexb_bsf;
 extern const FFBitStreamFilter ff_imx_dump_header_bsf;
+extern const FFBitStreamFilter ff_media100_to_mjpegb_bsf;
 extern const FFBitStreamFilter ff_mjpeg2jpeg_bsf;
 extern const FFBitStreamFilter ff_mjpega_dump_header_bsf;
 extern const FFBitStreamFilter ff_mp3_header_decompress_bsf;
diff --git a/libavcodec/media100.c b/libavcodec/media100_to_mjpegb_bsf.c
similarity index 65%
rename from libavcodec/media100.c
rename to libavcodec/media100_to_mjpegb_bsf.c
index 37b98c9a57..dafb65a0ad 100644
--- a/libavcodec/media100.c
+++ b/libavcodec/media100_to_mjpegb_bsf.c
@@ -1,6 +1,6 @@
 /*
- * Media 100 decoder
- * Copyright (c) 2022 Paul B Mahol
+ * Media 100 to MJPEGB bitstream filter
+ * Copyright (c) 2023 Paul B Mahol
  *
  * This file is part of FFmpeg.
  *
@@ -21,55 +21,34 @@
 
 /**
  * @file
- * Media 100 decoder.
+ * Media 100 to MJPEGB bitstream filter.
  */
 
 #include <inttypes.h>
 
+#include "bsf.h"
+#include "bsf_internal.h"
 #include "libavutil/intreadwrite.h"
-#include "avcodec.h"
 #include "bytestream.h"
-#include "codec_internal.h"
 
 typedef struct Media100Context {
-    AVCodecContext *avctx;   // wrapper context for mjpegb
     AVPacket *pkt;
 } Media100Context;
 
-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);
 
     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);
+    av_packet_unref(avpkt);
+    av_packet_move_ref(avpkt, s->pkt);
 
-    ret = avcodec_receive_frame(ctx->avctx, frame);
-    if (ret < 0)
-        return ret;
+    return 0;
+}
 
-    avctx->pix_fmt = ctx->avctx->pix_fmt;
-    *got_frame = 1;
+static void flush(AVBSFContext *ctx)
+{
+    Media100Context *s = ctx->priv_data;
 
-    return avpkt->size;
+    av_packet_unref(s->pkt);
 }
 
-static av_cold int media100_decode_end(AVCodecContext *avctx)
+static av_cold void uninit(AVBSFContext *ctx)
 {
-    Media100Context *ctx = avctx->priv_data;
+    Media100Context *s = ctx->priv_data;
 
-    avcodec_free_context(&ctx->avctx);
-    av_packet_free(&ctx->pkt);
-
-    return 0;
+    av_packet_free(&s->pkt);
 }
 
-const FFCodec ff_media100_decoder = {
-    .p.name           = "media100",
-    CODEC_LONG_NAME("Media 100"),
-    .p.type           = AVMEDIA_TYPE_VIDEO,
-    .p.id             = AV_CODEC_ID_MEDIA100,
-    .priv_data_size   = sizeof(Media100Context),
-    .init             = media100_decode_init,
-    .close            = media100_decode_end,
-    FF_CODEC_DECODE_CB(media100_decode_frame),
-    .caps_internal    = FF_CODEC_CAP_INIT_CLEANUP,
+const FFBitStreamFilter ff_media100_to_mjpegb_bsf = {
+    .p.name         = "media100_to_mjpegb",
+    .p.codec_ids    = (const enum AVCodecID []){ AV_CODEC_ID_MEDIA100, AV_CODEC_ID_NONE },
+    .priv_data_size = sizeof(Media100Context),
+    .init           = init,
+    .flush          = flush,
+    .close          = uninit,
+    .filter         = filter,
 };
diff --git a/libavcodec/mjpegbdec.c b/libavcodec/mjpegbdec.c
index 6d422e7a29..4db1d9a89d 100644
--- a/libavcodec/mjpegbdec.c
+++ b/libavcodec/mjpegbdec.c
@@ -168,3 +168,18 @@ const FFCodec ff_mjpegb_decoder = {
     .p.max_lowres   = 3,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
 };
+
+const FFCodec ff_media100_decoder = {
+    .p.name         = "media100",
+    CODEC_LONG_NAME("Media 100"),
+    .p.type         = AVMEDIA_TYPE_VIDEO,
+    .p.id           = AV_CODEC_ID_MEDIA100,
+    .priv_data_size = sizeof(MJpegDecodeContext),
+    .init           = ff_mjpeg_decode_init,
+    .close          = ff_mjpeg_decode_end,
+    FF_CODEC_DECODE_CB(mjpegb_decode_frame),
+    .p.capabilities = AV_CODEC_CAP_DR1,
+    .p.max_lowres   = 3,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .bsfs           = "media100_to_mjpegb",
+};
-- 
2.39.1


[-- Attachment #3: 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] 4+ messages in thread

* Re: [FFmpeg-devel] [PATCH] add media100 bsf and use it
  2023-01-31 10:18 [FFmpeg-devel] [PATCH] add media100 bsf and use it Paul B Mahol
@ 2023-01-31 11:20 ` Andreas Rheinhardt
  2023-01-31 12:48 ` Anton Khirnov
  2023-01-31 15:33 ` Paul B Mahol
  2 siblings, 0 replies; 4+ messages in thread
From: Andreas Rheinhardt @ 2023-01-31 11:20 UTC (permalink / raw)
  To: ffmpeg-devel

Paul B Mahol:
> -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);

The avpkt here is destined for output; you should use s->pkt to get the
new input packet. This will allow to avoid the av_packet_move_ref() below.

> +    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);

av_new_packet() always returns writable packets.

>      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);
> +    av_packet_unref(avpkt);
> +    av_packet_move_ref(avpkt, s->pkt);
>  
> -    ret = avcodec_receive_frame(ctx->avctx, frame);
> -    if (ret < 0)
> -        return ret;
> +    return 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] 4+ messages in thread

* Re: [FFmpeg-devel] [PATCH] add media100 bsf and use it
  2023-01-31 10:18 [FFmpeg-devel] [PATCH] add media100 bsf and use it Paul B Mahol
  2023-01-31 11:20 ` Andreas Rheinhardt
@ 2023-01-31 12:48 ` Anton Khirnov
  2023-01-31 15:33 ` Paul B Mahol
  2 siblings, 0 replies; 4+ messages in thread
From: Anton Khirnov @ 2023-01-31 12:48 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

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

* Re: [FFmpeg-devel] [PATCH] add media100 bsf and use it
  2023-01-31 10:18 [FFmpeg-devel] [PATCH] add media100 bsf and use it Paul B Mahol
  2023-01-31 11:20 ` Andreas Rheinhardt
  2023-01-31 12:48 ` Anton Khirnov
@ 2023-01-31 15:33 ` Paul B Mahol
  2 siblings, 0 replies; 4+ messages in thread
From: Paul B Mahol @ 2023-01-31 15:33 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 26 bytes --]

Improved  patch attached.

[-- Attachment #2: 0001-avcodec-add-media100_to_mjpegb-bitstream-filter-and-.patch --]
[-- Type: text/x-patch, Size: 17333 bytes --]

From ef7436580ce401917abab5114ff2ff8a1fdd9d61 Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@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@gmail.com>
---
 configure                           |   1 +
 libavcodec/Makefile                 |   3 +-
 libavcodec/bitstream_filters.c      |   1 +
 libavcodec/media100.c               | 224 ----------------------------
 libavcodec/media100_to_mjpegb_bsf.c | 170 +++++++++++++++++++++
 libavcodec/mjpegbdec.c              |  15 ++
 6 files changed, 189 insertions(+), 225 deletions(-)
 delete mode 100644 libavcodec/media100.c
 create mode 100644 libavcodec/media100_to_mjpegb_bsf.c

diff --git a/configure b/configure
index 47790d10f5..174ae5c894 100755
--- a/configure
+++ b/configure
@@ -2850,6 +2850,7 @@ lscr_decoder_select="inflate_wrapper"
 magicyuv_decoder_select="llviddsp"
 magicyuv_encoder_select="llvidencdsp"
 mdec_decoder_select="blockdsp bswapdsp idctdsp"
+media100_decoder_select="media100_to_mjpegb_bsf"
 metasound_decoder_select="lsp sinewin"
 mimic_decoder_select="blockdsp bswapdsp hpeldsp idctdsp"
 mjpeg_decoder_select="blockdsp hpeldsp exif idctdsp jpegtables"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index df76d37598..4971832ff4 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -485,7 +485,7 @@ OBJS-$(CONFIG_MACE6_DECODER)           += mace.o
 OBJS-$(CONFIG_MAGICYUV_DECODER)        += magicyuv.o
 OBJS-$(CONFIG_MAGICYUV_ENCODER)        += magicyuvenc.o
 OBJS-$(CONFIG_MDEC_DECODER)            += mdec.o mpeg12.o mpeg12data.o
-OBJS-$(CONFIG_MEDIA100_DECODER)        += media100.o
+OBJS-$(CONFIG_MEDIA100_DECODER)        += mjpegbdec.o
 OBJS-$(CONFIG_METASOUND_DECODER)       += metasound.o twinvq.o
 OBJS-$(CONFIG_MICRODVD_DECODER)        += microdvddec.o ass.o
 OBJS-$(CONFIG_MIMIC_DECODER)           += mimic.o
@@ -1221,6 +1221,7 @@ OBJS-$(CONFIG_HEVC_METADATA_BSF)          += h265_metadata_bsf.o h265_profile_le
                                              h2645data.o
 OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF)       += hevc_mp4toannexb_bsf.o
 OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF)        += imx_dump_header_bsf.o
+OBJS-$(CONFIG_MEDIA100_TO_MJPEGB_BSF)     += media100_to_mjpegb_bsf.o
 OBJS-$(CONFIG_MJPEG2JPEG_BSF)             += mjpeg2jpeg_bsf.o
 OBJS-$(CONFIG_MJPEGA_DUMP_HEADER_BSF)     += mjpega_dump_header_bsf.o
 OBJS-$(CONFIG_MPEG4_UNPACK_BFRAMES_BSF)   += mpeg4_unpack_bframes_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index a3bebefe5f..e8216819ca 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -43,6 +43,7 @@ extern const FFBitStreamFilter ff_hapqa_extract_bsf;
 extern const FFBitStreamFilter ff_hevc_metadata_bsf;
 extern const FFBitStreamFilter ff_hevc_mp4toannexb_bsf;
 extern const FFBitStreamFilter ff_imx_dump_header_bsf;
+extern const FFBitStreamFilter ff_media100_to_mjpegb_bsf;
 extern const FFBitStreamFilter ff_mjpeg2jpeg_bsf;
 extern const FFBitStreamFilter ff_mjpega_dump_header_bsf;
 extern const FFBitStreamFilter ff_mp3_header_decompress_bsf;
diff --git a/libavcodec/media100.c b/libavcodec/media100.c
deleted file mode 100644
index fdfce2cac1..0000000000
--- a/libavcodec/media100.c
+++ /dev/null
@@ -1,224 +0,0 @@
-/*
- * Media 100 decoder
- * Copyright (c) 2022 Paul B Mahol
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file
- * Media 100 decoder.
- */
-
-#include <inttypes.h>
-
-#include "libavutil/intreadwrite.h"
-#include "avcodec.h"
-#include "bytestream.h"
-#include "codec_internal.h"
-
-typedef struct Media100Context {
-    AVCodecContext *avctx;   // wrapper context for mjpegb
-    AVPacket *pkt;
-} Media100Context;
-
-static av_cold int media100_decode_init(AVCodecContext *avctx)
-{
-    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;
-
-    ctx->pkt = av_packet_alloc();
-    if (!ctx->pkt)
-        return AVERROR(ENOMEM);
-
-    return 0;
-}
-
-static int media100_decode_frame(AVCodecContext *avctx,
-                                 AVFrame *frame, int *got_frame,
-                                 AVPacket *avpkt)
-{
-    Media100Context *ctx = avctx->priv_data;
-    unsigned second_field_offset = 0;
-    unsigned next_field = 0;
-    unsigned dht_offset[2];
-    unsigned dqt_offset[2];
-    unsigned sod_offset[2];
-    unsigned sof_offset[2];
-    unsigned sos_offset[2];
-    unsigned field = 0;
-    GetByteContext gb;
-    PutByteContext pb;
-    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 = av_packet_make_writable(ctx->pkt);
-    if (ret < 0)
-        return ret;
-
-    bytestream2_init(&gb, avpkt->data, avpkt->size);
-    bytestream2_init_writer(&pb, ctx->pkt->data, ctx->pkt->size);
-
-second_field:
-    bytestream2_put_be32(&pb, 0);
-    bytestream2_put_be32(&pb, AV_RB32("mjpg"));
-    bytestream2_put_be32(&pb, 0);
-    bytestream2_put_be32(&pb, 0);
-    for (int i = 0; i < 6; i++)
-        bytestream2_put_be32(&pb, 0);
-
-    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_byte(&pb, 3);
-    bytestream2_put_byte(&pb, 1);
-    bytestream2_put_byte(&pb, 0x21);
-    bytestream2_put_byte(&pb, 0);
-    bytestream2_put_byte(&pb, 2);
-    bytestream2_put_byte(&pb, 0x11);
-    bytestream2_put_byte(&pb, 1);
-    bytestream2_put_byte(&pb, 3);
-    bytestream2_put_byte(&pb, 0x11);
-    bytestream2_put_byte(&pb, 1);
-
-    sos_offset[field] = bytestream2_tell_p(&pb);
-    bytestream2_put_be16(&pb, 12);
-    bytestream2_put_byte(&pb, 3);
-    bytestream2_put_byte(&pb, 1);
-    bytestream2_put_byte(&pb, 0);
-    bytestream2_put_byte(&pb, 2);
-    bytestream2_put_byte(&pb, 0x11);
-    bytestream2_put_byte(&pb, 3);
-    bytestream2_put_byte(&pb, 0x11);
-    bytestream2_put_byte(&pb, 0);
-    bytestream2_put_byte(&pb, 0);
-    bytestream2_put_byte(&pb, 0);
-
-    dqt_offset[field] = bytestream2_tell_p(&pb);
-    bytestream2_put_be16(&pb, 132);
-    bytestream2_put_byte(&pb, 0);
-    bytestream2_skip(&gb, 4);
-    for (int i = 0; i < 64; i++)
-        bytestream2_put_byte(&pb, bytestream2_get_be32(&gb));
-    bytestream2_put_byte(&pb, 1);
-    for (int i = 0; i < 64; i++)
-        bytestream2_put_byte(&pb, bytestream2_get_be32(&gb));
-
-    dht_offset[field] = 0;
-    sod_offset[field] = bytestream2_tell_p(&pb);
-
-    for (int i = bytestream2_tell(&gb) + 8; next_field == 0 && i < avpkt->size - 4; i++) {
-        if (AV_RB32(avpkt->data + i) == 0x00000001) {
-            next_field = i;
-            break;
-        }
-    }
-
-    bytestream2_skip(&gb, 8);
-    bytestream2_copy_buffer(&pb, &gb, next_field - bytestream2_tell(&gb));
-    bytestream2_put_be64(&pb, 0);
-
-    if (field == 0) {
-        field = 1;
-        second_field_offset = bytestream2_tell_p(&pb);
-        next_field = avpkt->size;
-        goto second_field;
-    }
-
-    pkt = ctx->pkt;
-
-    AV_WB32(pkt->data +  8, second_field_offset);
-    AV_WB32(pkt->data + 12, second_field_offset);
-    AV_WB32(pkt->data + 16, second_field_offset);
-    AV_WB32(pkt->data + 20, dqt_offset[0]);
-    AV_WB32(pkt->data + 24, dht_offset[0]);
-    AV_WB32(pkt->data + 28, sof_offset[0]);
-    AV_WB32(pkt->data + 32, sos_offset[0]);
-    AV_WB32(pkt->data + 36, sod_offset[0]);
-
-    AV_WB32(pkt->data + second_field_offset +  8, bytestream2_tell_p(&pb) - second_field_offset);
-    AV_WB32(pkt->data + second_field_offset + 12, bytestream2_tell_p(&pb) - second_field_offset);
-    AV_WB32(pkt->data + second_field_offset + 16, 0);
-    AV_WB32(pkt->data + second_field_offset + 20, dqt_offset[1] - second_field_offset);
-    AV_WB32(pkt->data + second_field_offset + 24, dht_offset[1]);
-    AV_WB32(pkt->data + second_field_offset + 28, sof_offset[1] - second_field_offset);
-    AV_WB32(pkt->data + second_field_offset + 32, sos_offset[1] - second_field_offset);
-    AV_WB32(pkt->data + second_field_offset + 36, sod_offset[1] - second_field_offset);
-
-    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;
-    }
-
-    ret = avcodec_receive_frame(ctx->avctx, frame);
-    if (ret < 0)
-        return ret;
-
-    *got_frame = 1;
-
-    return avpkt->size;
-}
-
-static av_cold int media100_decode_end(AVCodecContext *avctx)
-{
-    Media100Context *ctx = avctx->priv_data;
-
-    avcodec_free_context(&ctx->avctx);
-    av_packet_free(&ctx->pkt);
-
-    return 0;
-}
-
-const FFCodec ff_media100_decoder = {
-    .p.name           = "media100",
-    CODEC_LONG_NAME("Media 100"),
-    .p.type           = AVMEDIA_TYPE_VIDEO,
-    .p.id             = AV_CODEC_ID_MEDIA100,
-    .priv_data_size   = sizeof(Media100Context),
-    .init             = media100_decode_init,
-    .close            = media100_decode_end,
-    FF_CODEC_DECODE_CB(media100_decode_frame),
-    .caps_internal    = FF_CODEC_CAP_INIT_CLEANUP,
-};
diff --git a/libavcodec/media100_to_mjpegb_bsf.c b/libavcodec/media100_to_mjpegb_bsf.c
new file mode 100644
index 0000000000..c262f4455f
--- /dev/null
+++ b/libavcodec/media100_to_mjpegb_bsf.c
@@ -0,0 +1,170 @@
+/*
+ * Media 100 to MJPEGB bitstream filter
+ * Copyright (c) 2023 Paul B Mahol
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Media 100 to MJPEGB bitstream filter.
+ */
+
+#include <inttypes.h>
+
+#include "bsf.h"
+#include "bsf_internal.h"
+#include "libavutil/intreadwrite.h"
+#include "bytestream.h"
+
+static av_cold int init(AVBSFContext *ctx)
+{
+    ctx->par_out->codec_id = AV_CODEC_ID_MJPEGB;
+    return 0;
+}
+
+static int filter(AVBSFContext *ctx, AVPacket *out)
+{
+    unsigned second_field_offset = 0;
+    unsigned next_field = 0;
+    unsigned dht_offset[2];
+    unsigned dqt_offset[2];
+    unsigned sod_offset[2];
+    unsigned sof_offset[2];
+    unsigned sos_offset[2];
+    unsigned field = 0;
+    GetByteContext gb;
+    PutByteContext pb;
+    AVPacket *in;
+    int ret;
+
+    ret = ff_bsf_get_packet(ctx, &in);
+    if (ret < 0)
+        return ret;
+
+    ret = av_new_packet(out, in->size + 1024);
+    if (ret < 0)
+        return ret;
+
+    bytestream2_init(&gb, in->data, in->size);
+    bytestream2_init_writer(&pb, out->data, out->size);
+
+second_field:
+    bytestream2_put_be32(&pb, 0);
+    bytestream2_put_be32(&pb, AV_RB32("mjpg"));
+    bytestream2_put_be32(&pb, 0);
+    bytestream2_put_be32(&pb, 0);
+    for (int i = 0; i < 6; i++)
+        bytestream2_put_be32(&pb, 0);
+
+    sof_offset[field] = bytestream2_tell_p(&pb);
+    bytestream2_put_be16(&pb, 17);
+    bytestream2_put_byte(&pb, 8);
+    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);
+    bytestream2_put_byte(&pb, 0);
+    bytestream2_put_byte(&pb, 2);
+    bytestream2_put_byte(&pb, 0x11);
+    bytestream2_put_byte(&pb, 1);
+    bytestream2_put_byte(&pb, 3);
+    bytestream2_put_byte(&pb, 0x11);
+    bytestream2_put_byte(&pb, 1);
+
+    sos_offset[field] = bytestream2_tell_p(&pb);
+    bytestream2_put_be16(&pb, 12);
+    bytestream2_put_byte(&pb, 3);
+    bytestream2_put_byte(&pb, 1);
+    bytestream2_put_byte(&pb, 0);
+    bytestream2_put_byte(&pb, 2);
+    bytestream2_put_byte(&pb, 0x11);
+    bytestream2_put_byte(&pb, 3);
+    bytestream2_put_byte(&pb, 0x11);
+    bytestream2_put_byte(&pb, 0);
+    bytestream2_put_byte(&pb, 0);
+    bytestream2_put_byte(&pb, 0);
+
+    dqt_offset[field] = bytestream2_tell_p(&pb);
+    bytestream2_put_be16(&pb, 132);
+    bytestream2_put_byte(&pb, 0);
+    bytestream2_skip(&gb, 4);
+    for (int i = 0; i < 64; i++)
+        bytestream2_put_byte(&pb, bytestream2_get_be32(&gb));
+    bytestream2_put_byte(&pb, 1);
+    for (int i = 0; i < 64; i++)
+        bytestream2_put_byte(&pb, bytestream2_get_be32(&gb));
+
+    dht_offset[field] = 0;
+    sod_offset[field] = bytestream2_tell_p(&pb);
+
+    for (int i = bytestream2_tell(&gb) + 8; next_field == 0 && i < in->size - 4; i++) {
+        if (AV_RB32(in->data + i) == 0x00000001) {
+            next_field = i;
+            break;
+        }
+    }
+
+    bytestream2_skip(&gb, 8);
+    bytestream2_copy_buffer(&pb, &gb, next_field - bytestream2_tell(&gb));
+    bytestream2_put_be64(&pb, 0);
+
+    if (field == 0) {
+        field = 1;
+        second_field_offset = bytestream2_tell_p(&pb);
+        next_field = in->size;
+        goto second_field;
+    }
+
+    AV_WB32(out->data +  8, second_field_offset);
+    AV_WB32(out->data + 12, second_field_offset);
+    AV_WB32(out->data + 16, second_field_offset);
+    AV_WB32(out->data + 20, dqt_offset[0]);
+    AV_WB32(out->data + 24, dht_offset[0]);
+    AV_WB32(out->data + 28, sof_offset[0]);
+    AV_WB32(out->data + 32, sos_offset[0]);
+    AV_WB32(out->data + 36, sod_offset[0]);
+
+    AV_WB32(out->data + second_field_offset +  8, bytestream2_tell_p(&pb) - second_field_offset);
+    AV_WB32(out->data + second_field_offset + 12, bytestream2_tell_p(&pb) - second_field_offset);
+    AV_WB32(out->data + second_field_offset + 16, 0);
+    AV_WB32(out->data + second_field_offset + 20, dqt_offset[1] - second_field_offset);
+    AV_WB32(out->data + second_field_offset + 24, dht_offset[1]);
+    AV_WB32(out->data + second_field_offset + 28, sof_offset[1] - second_field_offset);
+    AV_WB32(out->data + second_field_offset + 32, sos_offset[1] - second_field_offset);
+    AV_WB32(out->data + second_field_offset + 36, sod_offset[1] - second_field_offset);
+
+    out->size = bytestream2_tell_p(&pb);
+
+    ret = av_packet_copy_props(out, in);
+    if (ret < 0)
+        goto fail;
+
+fail:
+    if (ret < 0)
+        av_packet_unref(out);
+    av_packet_free(&in);
+    return ret;
+}
+
+const FFBitStreamFilter ff_media100_to_mjpegb_bsf = {
+    .p.name         = "media100_to_mjpegb",
+    .p.codec_ids    = (const enum AVCodecID []){ AV_CODEC_ID_MEDIA100, AV_CODEC_ID_NONE },
+    .init           = init,
+    .filter         = filter,
+};
diff --git a/libavcodec/mjpegbdec.c b/libavcodec/mjpegbdec.c
index 6d422e7a29..4db1d9a89d 100644
--- a/libavcodec/mjpegbdec.c
+++ b/libavcodec/mjpegbdec.c
@@ -168,3 +168,18 @@ const FFCodec ff_mjpegb_decoder = {
     .p.max_lowres   = 3,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
 };
+
+const FFCodec ff_media100_decoder = {
+    .p.name         = "media100",
+    CODEC_LONG_NAME("Media 100"),
+    .p.type         = AVMEDIA_TYPE_VIDEO,
+    .p.id           = AV_CODEC_ID_MEDIA100,
+    .priv_data_size = sizeof(MJpegDecodeContext),
+    .init           = ff_mjpeg_decode_init,
+    .close          = ff_mjpeg_decode_end,
+    FF_CODEC_DECODE_CB(mjpegb_decode_frame),
+    .p.capabilities = AV_CODEC_CAP_DR1,
+    .p.max_lowres   = 3,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .bsfs           = "media100_to_mjpegb",
+};
-- 
2.39.1


[-- Attachment #3: 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] 4+ messages in thread

end of thread, other threads:[~2023-01-31 15:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-31 10:18 [FFmpeg-devel] [PATCH] add media100 bsf and use it Paul B Mahol
2023-01-31 11:20 ` Andreas Rheinhardt
2023-01-31 12:48 ` Anton Khirnov
2023-01-31 15:33 ` Paul B Mahol

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