---
 libavcodec/allcodecs.c         |  1 -
 libavcodec/bitstream_filters.c |  4 +++
 libavcodec/mjpeg2jpeg_bsf.c    | 69 +++++++++++++++++++++++++-----------------
 3 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index c8875f7..032b163 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -538,7 +538,6 @@ void avcodec_register_all(void)
     REGISTER_PARSER(VP8,                vp8);
 
     /* bitstream filters */
-    REGISTER_BSF(MJPEG2JPEG,            mjpeg2jpeg);
     REGISTER_BSF(MJPEGA_DUMP_HEADER,    mjpega_dump_header);
     REGISTER_BSF(MOV2TEXTSUB,           mov2textsub);
     REGISTER_BSF(NOISE,                 noise);
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index e2522f7..9b26747 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -30,6 +30,7 @@ extern const AVBitStreamFilter ff_dump_extradata_bsf;
 extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_imx_dump_header_bsf;
+extern const AVBitStreamFilter ff_mjpeg2jpeg_bsf;
 
 static const AVBitStreamFilter *bitstream_filters[] = {
 #if CONFIG_AAC_ADTSTOASC_BSF
@@ -50,6 +51,9 @@ static const AVBitStreamFilter *bitstream_filters[] = {
 #if CONFIG_IMX_DUMP_HEADER_BSF
     &ff_imx_dump_header_bsf,
 #endif
+#if CONFIG_MJPEG2JPEG_BSF
+    &ff_mjpeg2jpeg_bsf,
+#endif
     NULL,
 };
 
diff --git a/libavcodec/mjpeg2jpeg_bsf.c b/libavcodec/mjpeg2jpeg_bsf.c
index d277934..eec3469 100644
--- a/libavcodec/mjpeg2jpeg_bsf.c
+++ b/libavcodec/mjpeg2jpeg_bsf.c
@@ -30,6 +30,7 @@
 #include "libavutil/mem.h"
 
 #include "avcodec.h"
+#include "bsf.h"
 #include "jpegtables.h"
 
 static const uint8_t jpeg_header[] = {
@@ -75,42 +76,56 @@ static uint8_t *append_dht_segment(uint8_t *buf)
     return buf;
 }
 
-static int mjpeg2jpeg_filter(AVBitStreamFilterContext *bsfc,
-                             AVCodecContext *avctx, const char *args,
-                             uint8_t **poutbuf, int *poutbuf_size,
-                             const uint8_t *buf, int buf_size,
-                             int keyframe)
+static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *out)
 {
+    AVPacket *in;
+    int ret = 0;
     int input_skip, output_size;
-    uint8_t *output, *out;
+    uint8_t *output;
 
-    if (buf_size < 12) {
-        av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
-        return AVERROR_INVALIDDATA;
+    ret = ff_bsf_get_packet(ctx, &in);
+
+    if (in->size < 12) {
+        av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
+        ret = AVERROR_INVALIDDATA;
+        goto fail;
     }
-    if (memcmp("AVI1", buf + 6, 4)) {
-        av_log(avctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
-        return AVERROR_INVALIDDATA;
+    if (memcmp("AVI1", in->data + 6, 4)) {
+        av_log(ctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
+        ret = AVERROR_INVALIDDATA;
+        goto fail;
     }
-    input_skip = (buf[4] << 8) + buf[5] + 4;
-    if (buf_size < input_skip) {
-        av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
-        return AVERROR_INVALIDDATA;
+
+    input_skip = (in->data[4] << 8) + in->data[5] + 4;
+    if (in->size < input_skip) {
+        av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
+        ret = AVERROR_INVALIDDATA;
+        goto fail;
     }
-    output_size = buf_size - input_skip +
+    output_size = in->size - input_skip +
                   sizeof(jpeg_header) + dht_segment_size;
-    output = out = av_malloc(output_size);
-    if (!output)
-        return AVERROR(ENOMEM);
-    out = append(out, jpeg_header, sizeof(jpeg_header));
-    out = append_dht_segment(out);
-    out = append(out, buf + input_skip, buf_size - input_skip);
-    *poutbuf = output;
-    *poutbuf_size = output_size;
-    return 1;
+    ret = av_new_packet(out, output_size);
+    if (ret < 0)
+        goto fail;
+
+    output = out->data;
+
+    output = append(output, jpeg_header, sizeof(jpeg_header));
+    output = append_dht_segment(output);
+    output = append(output, in->data + input_skip, in->size - input_skip);
+
+    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;
 }
 
-AVBitStreamFilter ff_mjpeg2jpeg_bsf = {
+const AVBitStreamFilter ff_mjpeg2jpeg_bsf = {
     .name           = "mjpeg2jpeg",
     .filter         = mjpeg2jpeg_filter,
 };
-- 
2.0.0

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to