PR #26091 opened by wangbin
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26091
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26091.patch

Parse the ISO 21496-1 metadata from the APP2 segment of gain map images
and attach it to their packets as AV_PKT_DATA_GAIN_MAP_PARAMS, so that
decoders other than the native MJPEG decoder (e.g. hardware or platform
decoders) can use the params without re-parsing the bitstream.

The params are attached per packet rather than to codecpar, since the
gain map image is not read during read_header().

Co-authored-by: Cursor <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

# Summary of changes

Briefly describe what this PR does and why.

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From a72ce2f6c3ae1572cb7ef0480c260b2bf5f9aed8 Mon Sep 17 00:00:00 2001
From: wangbin <[email protected]>
Date: Sat, 14 Nov 2026 21:37:01 +0800
Subject: [PATCH] avformat/jpegmpfdec: export gain map params as packet side
 data

Parse the ISO 21496-1 metadata from the APP2 segment of gain map images
and attach it to their packets as AV_PKT_DATA_GAIN_MAP_PARAMS, so that
decoders other than the native MJPEG decoder (e.g. hardware or platform
decoders) can use the params without re-parsing the bitstream.

The params are attached per packet rather than to codecpar, since the
gain map image is not read during read_header().

Co-authored-by: Cursor <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 libavformat/jpegmpfdec.c | 129 ++++++++++++++++++++++++++++++++-------
 1 file changed, 107 insertions(+), 22 deletions(-)

diff --git a/libavformat/jpegmpfdec.c b/libavformat/jpegmpfdec.c
index b5b72b58cbd..aa756350bc1 100644
--- a/libavformat/jpegmpfdec.c
+++ b/libavformat/jpegmpfdec.c
@@ -71,16 +71,13 @@ typedef struct MPFDemuxContext {
 
 #define CHECK_IDENT(strc) (len >= sizeof(strc) && !memcmp(gb->buffer, strc, 
sizeof(strc)))
 
-static int mpf_probe(const AVProbeData *p)
+static int walk_jpeg_app2(GetByteContext *gb,
+                          int (*on_app2)(void *opaque, GetByteContext *gb, int 
len),
+                          void *opaque)
 {
-    GetByteContext gbc, *gb = &gbc;
-    bytestream2_init(gb, p->buf, p->buf_size);
-    if (bytestream2_get_be16(gb) != (0xFF00 | SOI))
-        return 0;
-
     while (bytestream2_get_bytes_left(gb) >= 4) {
         if (bytestream2_get_byte(gb) != 0xFF)
-            return 0; /* not a JPEG marker */
+            return AVERROR_INVALIDDATA; /* not a JPEG marker */
 
         int marker = bytestream2_get_byte(gb);
         while (marker == 0xFF && bytestream2_get_bytes_left(gb) > 0) /* fill 
bytes */
@@ -88,31 +85,53 @@ static int mpf_probe(const AVProbeData *p)
 
         if (marker == SOS || marker == TEM || (marker >= RST0 && marker <= 
EOI))
             return 0; /* start of entropy coded data */
-        else if (marker == 0xFF || bytestream2_get_bytes_left(gb) < 2)
-            break; /* need more data */
+        if (marker == 0xFF || bytestream2_get_bytes_left(gb) < 2)
+            return AVERROR(EAGAIN); /* need more data */
 
         const int len = bytestream2_get_be16(gb) - 2;
         if (len < 0)
-            return 0;
+            return AVERROR_INVALIDDATA;
         if (len > bytestream2_get_bytes_left(gb))
-            break;
+            return AVERROR(EAGAIN);
 
-        switch (marker) {
-        case APP2:
-            /* Marker segments unique to MPF files; the base image of a gain
-             * map file only carries the version fields of the ISO 21496-1
-             * metadata (two 16-bit integers); we don't want to false positive
-             * on standalone JPEGs that happen to contain gain map metadata */
-            if (CHECK_IDENT("MPF") ||
-                (CHECK_IDENT(AV_ISO21496_IDENTIFIER) &&
-                 len == sizeof(AV_ISO21496_IDENTIFIER) + sizeof(uint16_t[2])))
-                return AVPROBE_SCORE_EXTENSION + 10;
-            break;
+        if (marker == APP2 && on_app2) {
+            int ret = on_app2(opaque, gb, len);
+            if (ret)
+                return ret;
         }
 
         bytestream2_skip(gb, len);
     }
 
+    return AVERROR(EAGAIN);
+}
+
+static int probe_app2(void *opaque, GetByteContext *gb, int len)
+{
+    /* Marker segments unique to MPF files; the base image of a gain
+     * map file only carries the version fields of the ISO 21496-1
+     * metadata (two 16-bit integers); we don't want to false positive
+     * on standalone JPEGs that happen to contain gain map metadata */
+    if (CHECK_IDENT("MPF") ||
+        (CHECK_IDENT(AV_ISO21496_IDENTIFIER) &&
+         len == sizeof(AV_ISO21496_IDENTIFIER) + sizeof(uint16_t[2])))
+        return AVPROBE_SCORE_EXTENSION + 10;
+    return 0;
+}
+
+static int mpf_probe(const AVProbeData *p)
+{
+    GetByteContext gbc, *gb = &gbc;
+    bytestream2_init(gb, p->buf, p->buf_size);
+    if (bytestream2_get_be16(gb) != (0xFF00 | SOI))
+        return 0;
+
+    int ret = walk_jpeg_app2(gb, probe_app2, NULL);
+    if (ret > 0)
+        return ret;
+    if (ret != AVERROR(EAGAIN))
+        return 0;
+
     /* If we ran out of probe bytes, we don't know yet whether this is an
      * MPF file or not, unless the extension is unique to MPF files */
     if (av_match_ext(p->filename, "mpo"))
@@ -224,6 +243,41 @@ fail:
     return ret;
 }
 
+typedef struct AttachGainMapCtx {
+    AVFormatContext *ctx;
+    AVGainMapParams *gain;
+    size_t gain_size;
+} AttachGainMapCtx;
+
+static int parse_iso21496_app2(void *opaque, GetByteContext *gb, int len)
+{
+    AttachGainMapCtx *c = opaque;
+    const uint8_t *data = gb->buffer;
+
+    if (len < sizeof(AV_ISO21496_IDENTIFIER) ||
+        memcmp(data, AV_ISO21496_IDENTIFIER, sizeof(AV_ISO21496_IDENTIFIER)))
+        return 0;
+
+    data += sizeof(AV_ISO21496_IDENTIFIER);
+    len  -= sizeof(AV_ISO21496_IDENTIFIER);
+
+    /* Discard gain map version stubs from base layer */
+    if (len <= sizeof(uint16_t[2]))
+        return 0;
+
+    c->gain = av_gain_map_params_alloc(&c->gain_size);
+    if (!c->gain)
+        return AVERROR(ENOMEM);
+
+    int ret = av_gain_map_params_from_iso21496(c->gain, data, len);
+    if (ret < 0) {
+        av_log(c->ctx, AV_LOG_VERBOSE, "Failed to parse ISO 21496 gain map 
header\n");
+        av_freep(&c->gain);
+        return 0;
+    }
+    return 1;
+}
+
 static int parse_app2(AVFormatContext *ctx, int offset, int len)
 {
     MPFDemuxContext *s = ctx->priv_data;
@@ -257,6 +311,30 @@ static int cmp_image_start(const void *a, const void *b)
     return FFDIFFSIGN(ia->index, ib->index);
 }
 
+static int attach_gain_map_params(AVFormatContext *ctx, AVPacket *pkt)
+{
+    AttachGainMapCtx c = { .ctx = ctx };
+    GetByteContext gb;
+
+    bytestream2_init(&gb, pkt->data, pkt->size);
+    if (bytestream2_get_be16(&gb) != (0xFF00 | SOI))
+        return 0;
+
+    /* Malformed/truncated headers are left for the decoder to deal with */
+    int ret = walk_jpeg_app2(&gb, parse_iso21496_app2, &c);
+    if (!c.gain)
+        return ret == AVERROR(ENOMEM) ? ret : 0;
+
+    /* The gain map image is not read in read_header(), and codecpar side
+     * data added afterwards would be overwritten by find_stream_info(),
+     * so attach the params to each gain map packet instead. */
+    ret = av_packet_add_side_data(pkt, AV_PKT_DATA_GAIN_MAP_PARAMS,
+                                  (uint8_t *)c.gain, c.gain_size);
+    if (ret < 0)
+        av_free(c.gain);
+    return ret;
+}
+
 static int add_gain_map_group(AVFormatContext *ctx, const MPFDemuxImage *base,
                               const MPFDemuxImage *gain)
 {
@@ -475,6 +553,13 @@ static int mpf_read_packet(AVFormatContext *ctx, AVPacket 
*pkt)
     pkt->pts = pkt->dts = s->pts;
     pkt->duration = 1;
     pkt->flags |= AV_PKT_FLAG_KEY;
+
+    if ((img->type & MPF_TYPE_INFO_MASK) == MPF_TYPE_GAIN_MAP) {
+        ret = attach_gain_map_params(ctx, pkt);
+        if (ret < 0)
+            return ret;
+    }
+
     return 0;
 }
 
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to