diff mbox series

[FFmpeg-devel] (No Subject)

Message ID XYGaEXafvuUF7uY7jZO4uphoZkgf-_pg72mMX45C-MQzykY67cD6HYwfSngk8LmTWZVZoQK30AJK4Nq3oyEaMy-I7bmAz3J9RqusVnFcvOc=@proton.me
State New
Headers show
Series [FFmpeg-devel] (No Subject) | expand

Commit Message

Cynthia Oct. 17, 2024, 6:38 a.m. UTC
From d7863bab8e1028b6cfb3ce848e216e86ff00eca0 Mon Sep 17 00:00:00 2001
From: cynthia2006 <cynthia2048@proton.me>
Date: Tue, 28 May 2024 22:03:50 +0530
Subject: [PATCH] lavc/mjpegdec: add option for ignorning malformed APPx
 segments
X-Unsent: 1
To: ffmpeg-devel@ffmpeg.org

A few cameras, namely Logitech C270 or similar produce MJPEG frames containing
malformed APP0 chunks, and as a result of which, FFmpeg spams stdout with

"[mjpeg @ 0xcoffeebabe] unable to decode APP fields: Invalid data found when processing input"

The issue is explained in full-detail here: https://stackoverflow.com/q/55439184.
TL;DR The second APP0 chunk is the culprit here.

Issue is mitigated if those segments are ignored. This patch adds a MJPEG decoder private
option `-allow_malformed_app` which if enabled, ignores those malformed APP0 segments.

Signed-off-by: cynthia2006 <cynthia2048@proton.me>
---
 libavcodec/mjpegdec.c | 5 +++++
 libavcodec/mjpegdec.h | 1 +
 2 files changed, 6 insertions(+)

Comments

Ramiro Polla Oct. 17, 2024, 10:31 a.m. UTC | #1
Hi,

On Thu, Oct 17, 2024 at 8:38 AM Cynthia via ffmpeg-devel
<ffmpeg-devel@ffmpeg.org> wrote:
> From d7863bab8e1028b6cfb3ce848e216e86ff00eca0 Mon Sep 17 00:00:00 2001
> From: cynthia2006 <cynthia2048@proton.me>
> Date: Tue, 28 May 2024 22:03:50 +0530
> Subject: [PATCH] lavc/mjpegdec: add option for ignorning malformed APPx
>  segments
> X-Unsent: 1
> To: ffmpeg-devel@ffmpeg.org
>
> A few cameras, namely Logitech C270 or similar produce MJPEG frames containing
> malformed APP0 chunks, and as a result of which, FFmpeg spams stdout with
>
> "[mjpeg @ 0xcoffeebabe] unable to decode APP fields: Invalid data found when processing input"
>
> The issue is explained in full-detail here: https://stackoverflow.com/q/55439184.
> TL;DR The second APP0 chunk is the culprit here.
>
> Issue is mitigated if those segments are ignored. This patch adds a MJPEG decoder private
> option `-allow_malformed_app` which if enabled, ignores those malformed APP0 segments.

I took a different approach where, instead of adding a new option, I
just silently ignore APPx stubs. I'll send my patches in a while.

> Signed-off-by: cynthia2006 <cynthia2048@proton.me>
> ---
>  libavcodec/mjpegdec.c | 5 +++++
>  libavcodec/mjpegdec.h | 1 +
>  2 files changed, 6 insertions(+)
>
> diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
> index 1481a7f285..b1e26044c0 100644
> --- a/libavcodec/mjpegdec.c
> +++ b/libavcodec/mjpegdec.c
> @@ -1863,6 +1863,9 @@ static int mjpeg_decode_app(MJpegDecodeContext *s)
>              av_log(s->avctx, AV_LOG_WARNING, "skipping APPx (len=%"PRId32") for bayer-encoded image\n", len);
>              skip_bits(&s->gb, len);
>              return 0;
> +        } else if (s->allow_malformed_app) {
> +            skip_bits(&s->gb, len);

len is in bytes, so this call to skip_bits() is not correct. I know
it's the same as in the existing code above, but that should be fixed
as well.

> +            return 0;
>          } else
>              return AVERROR_INVALIDDATA;
>      }
diff mbox series

Patch

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 1481a7f285..b1e26044c0 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -1863,6 +1863,9 @@  static int mjpeg_decode_app(MJpegDecodeContext *s)
             av_log(s->avctx, AV_LOG_WARNING, "skipping APPx (len=%"PRId32") for bayer-encoded image\n", len);
             skip_bits(&s->gb, len);
             return 0;
+        } else if (s->allow_malformed_app) {
+            skip_bits(&s->gb, len);
+            return 0;
         } else
             return AVERROR_INVALIDDATA;
     }
@@ -2988,6 +2991,8 @@  static void decode_flush(AVCodecContext *avctx)
 static const AVOption options[] = {
     { "extern_huff", "Use external huffman table.",
       OFFSET(extern_huff), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
+    { "allow_malformed_app", "Allow (ignore) malformed APPx chunks.",
+      OFFSET(allow_malformed_app), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
     { NULL },
 };
 
diff --git a/libavcodec/mjpegdec.h b/libavcodec/mjpegdec.h
index 13c524d597..d7e4f88682 100644
--- a/libavcodec/mjpegdec.h
+++ b/libavcodec/mjpegdec.h
@@ -138,6 +138,7 @@  typedef struct MJpegDecodeContext {
     unsigned int ljpeg_buffer_size;
 
     int extern_huff;
+    int allow_malformed_app;
     AVDictionary *exif_metadata;
 
     AVStereo3D *stereo3d; ///!< stereoscopic information (cached, since it is read before frame allocation)