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

# Summary of changes

Adds support to demux uncompressed 8-bit monochrome as per [ISO/IEC 
23001-17:2024](https://www.iso.org/standard/82528.html)

Implements the `cmpd` and `uncC` (version 0) boxes. Note that `uncC` version 0 
has an overwhelming number of configurations and this PR only handles `gray8`.

### FATE Sample:
```fate-samples
mov/uncv-gray8.mp4
```

### The `uncC` box definition:

```C
aligned(8) class UncompressedFrameConfigBox extends FullBox('uncC', version, 0) 
{
    unsigned int(32) profile;
    if (version == 1) {
    
    } else if (version == 0) {
        unsigned int(32) component_count;
        {
            unsigned int(16) component_index;
            unsigned int(8) component_bit_depth_minus_one;
            unsigned int(8) component_format;
            unsigned int(8) component_align_size;
        } [component_count];
        unsigned int(8) sampling_type;
        unsigned int(8) interleave_type;
        unsigned int(8) block_size;
        bit(1) components_little_endian;
        bit(1) block_pad_lsb;
        bit(1) block_little_endian;
        bit(1) block_reversed;
        bit(1) pad_unknown;
        bit(3) reserved = 0;
        unsigned int(32) pixel_size;
        unsigned int(32) row_align_size;
        unsigned int(32) tile_align_size;
        unsigned int(32) num_tile_cols_minus_one;
        unsigned int(32) num_tile_rows_minus_one;
    }
}
```

### The `cmpd` box definition:

```C
aligned(8) class ComponentDefinitionBox extends Box('cmpd') {
    unsigned int(32) component_count;
    {
        unsigned int(16) component_type;
        if (component_type >= 0x8000) {
            utf8string component_type_uri;
        }
    } [component_count];
}
```

<!--
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 33f38ed523177051416297174ee3651dc9aae5c2 Mon Sep 17 00:00:00 2001
From: Devon Sookhoo <[email protected]>
Date: Sat, 21 Nov 2026 08:10:38 -0700
Subject: [PATCH] avformat/mov demux uncC-v0 gray8

---
 libavformat/isom.h            |  47 +++++
 libavformat/mov.c             | 371 ++++++++++++++++++++++++++++++++--
 tests/fate/mov.mak            |   4 +-
 tests/ref/fate/mov-uncv-gray8 |   8 +
 4 files changed, 409 insertions(+), 21 deletions(-)
 create mode 100644 tests/ref/fate/mov-uncv-gray8

diff --git a/libavformat/isom.h b/libavformat/isom.h
index 7ad42f52809..8a1c6f2733d 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -77,6 +77,51 @@ typedef struct MOVStsc {
     int id;
 } MOVStsc;
 
+typedef enum MOVComponentType {
+    /* ISO/IEC 23001-17 Table 1 */
+    MOV_CMPD_TYPE_MONOCHROME   = 0,
+    MOV_CMPD_TYPE_Y            = 1,  /* Luma */
+    MOV_CMPD_TYPE_U            = 2,  /* CB */
+    MOV_CMPD_TYPE_V            = 3,  /* CR */
+    MOV_CMPD_TYPE_RED          = 4,
+    MOV_CMPD_TYPE_GREEN        = 5,
+    MOV_CMPD_TYPE_BLUE         = 6,
+    MOV_CMPD_TYPE_ALPHA        = 7,
+    MOV_CMPD_TYPE_DEPTH        = 8,
+    MOV_CMPD_TYPE_DISPARITY    = 9,
+    MOV_CMPD_TYPE_PALETTE      = 10, /* Color LUT */
+    MOV_CMPD_TYPE_FILTER_ARRAY = 11, /* Bayer */
+    MOV_CMPD_TYPE_PADDED       = 12,
+    MOV_CMPD_TYPE_CYAN         = 13,
+    MOV_CMPD_TYPE_MAGENTA      = 14,
+    MOV_CMPD_TYPE_YELLOW       = 15,
+    MOV_CMPD_TYPE_KEY          = 16,
+} MOVComponentType;
+
+typedef struct MOVCmpd {
+    uint32_t component_count;
+    uint16_t *component_types;
+} MOVCmpd;
+
+typedef struct MOVUnccComponent {
+    uint16_t component_index;
+    uint8_t component_bit_depth;
+    uint8_t component_format;
+    uint8_t component_align_size;
+} MOVUnccComponent;
+
+typedef struct MOVUncc {
+    uint8_t version;
+    uint32_t profile;
+    uint32_t component_count;
+    MOVUnccComponent *components;
+    uint8_t sampling_type, interleave_type, block_size;
+    uint32_t pixel_size, row_align_size, tile_align_size;
+    uint32_t num_tile_cols_minus_one, num_tile_rows_minus_one;
+    uint8_t components_little_endian;
+    uint8_t block_pad_lsb, block_little_endian, block_reversed, pad_unknown;
+} MOVUncc;
+
 typedef struct MOVElst {
     int64_t duration;
     int64_t time;
@@ -203,6 +248,8 @@ typedef struct MOVStreamContext {
     int stsc_sample;
     unsigned int stps_count;
     unsigned *stps_data;  ///< partial sync sample for mpeg-2 open gop
+    MOVCmpd *cmpd;
+    MOVUncc *uncc;
     MOVElst *elst_data;
     unsigned int elst_count;
     int tts_index;
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 9ad780cde9c..6252cd13ff4 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -3318,6 +3318,212 @@ static int mov_parse_stsd_data(MOVContext *c, 
AVIOContext *pb,
     return 0;
 }
 
+static int mov_uncv_set_pix_fmt(AVFormatContext *fc, AVStream *st,
+                                MOVStreamContext *sc)
+{
+    const MOVUncc *uncc = sc->uncc;
+    const MOVCmpd *cmpd = sc->cmpd;
+    const MOVUnccComponent *comp;
+    MOVComponentType type;
+    uint32_t component_index;
+
+    if (!uncc) {
+        av_log(fc, AV_LOG_ERROR, "uncC box is missing\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (uncc->version == 1) {
+        switch (uncc->profile) {
+        case MKTAG('r','g','b','3'):
+            st->codecpar->format = AV_PIX_FMT_RGB24;
+            break;
+        case MKTAG('r','g','b','a'):
+            st->codecpar->format = AV_PIX_FMT_RGBA;
+            break;
+        default:
+            avpriv_request_sample(fc, "Unknown uncC profile %s",
+                av_fourcc2str(uncc->profile));
+            return AVERROR_PATCHWELCOME;
+        }
+    } else if (uncc->version == 0) {
+        if (!cmpd) {
+            av_log(fc, AV_LOG_ERROR, "cmpd box is missing\n");
+            return AVERROR_INVALIDDATA;
+        }
+
+        if (uncc->component_count != cmpd->component_count) {
+            av_log(fc, AV_LOG_ERROR,
+                "Component count mismatch between uncC and cmpd boxes\n");
+            return AVERROR_INVALIDDATA;
+        }
+
+        for (uint32_t i = 0; i < uncc->component_count; i++) {
+            comp = &uncc->components[i];
+
+            if (comp->component_index >= cmpd->component_count) {
+                av_log(fc, AV_LOG_ERROR, "Component index out of range\n");
+                return AVERROR_INVALIDDATA;
+            }
+
+            component_index = uncc->components[i].component_index;
+            type = cmpd->component_types[component_index];
+
+            switch (type) {
+            case MOV_CMPD_TYPE_FILTER_ARRAY:
+            case MOV_CMPD_TYPE_PALETTE:
+            case MOV_CMPD_TYPE_PADDED:
+                avpriv_request_sample(fc, "uncC: component type %u", type);
+                return AVERROR_PATCHWELCOME;
+            default:
+                break;
+            }
+
+            switch (comp->component_format) {
+            case 0:
+                /* unsigned_integer */
+                break;
+            case 1:
+                avpriv_request_sample(fc, "uncC: floating point component");
+                return AVERROR_PATCHWELCOME;
+            case 2:
+                avpriv_request_sample(fc,
+                    "uncC: complex floating point component");
+                return AVERROR_PATCHWELCOME;
+            case 3:
+                avpriv_request_sample(fc, "uncC: signed integer component");
+                return AVERROR_PATCHWELCOME;
+            default:
+                av_log(fc, AV_LOG_ERROR,
+                    "uncC: Unknown component format %"PRIu8"\n",
+                    comp->component_format);
+                return AVERROR_INVALIDDATA;
+            }
+
+            if (comp->component_align_size) {
+                /* If 0, the component value is coded on bit_depth bits 
exactly */
+                avpriv_request_sample(fc, "uncC: align_size %u",
+                    comp->component_align_size);
+                return AVERROR_PATCHWELCOME;
+            }
+        }
+
+        switch (uncc->sampling_type) {
+        case 0:
+            /* 444 (No subsampling) */
+            break;
+        case 1:
+            avpriv_request_sample(fc, "uncC: 422 sampling");
+            return AVERROR_PATCHWELCOME;
+        case 2:
+            avpriv_request_sample(fc, "uncC: 420 sampling");
+            return AVERROR_PATCHWELCOME;
+        case 3:
+            avpriv_request_sample(fc, "uncC: 411 sampling");
+            return AVERROR_PATCHWELCOME;
+        default:
+            av_log(fc, AV_LOG_ERROR, "uncC: Unknown sampling type %u\n",
+                uncc->sampling_type);
+            return AVERROR_INVALIDDATA;
+        }
+
+        switch (uncc->interleave_type) {
+        case 0:
+            /* Component Interleaving (planar) */
+            break;
+        case 1:
+            /* Pixel Interleaving (packed) */
+            break;
+        case 2:
+            /* Mixed Interleaving */
+            avpriv_request_sample(fc, "uncC: Mixed Interleaving");
+            return AVERROR_PATCHWELCOME;
+        case 3:
+            /* Row Interleaving */
+            avpriv_request_sample(fc, "uncC: Row Interleaving");
+            return AVERROR_PATCHWELCOME;
+        case 4:
+            /* Tile-component Interleaving */
+            avpriv_request_sample(fc, "uncC: Tile-component Interleaving");
+            return AVERROR_PATCHWELCOME;
+        case 5:
+            /* Multi-Y pixel Interleaving */
+            avpriv_request_sample(fc, "uncC: Multi-Y pixel Interleaving");
+            return AVERROR_PATCHWELCOME;
+        default:
+            av_log(fc, AV_LOG_ERROR, "uncC: Unknown interleave type %u\n",
+                uncc->interleave_type);
+            return AVERROR_INVALIDDATA;
+        }
+
+        if (uncc->block_size) {
+            /* If block_size is 0, then blocking is not used */
+            avpriv_request_sample(fc, "uncC: Block size handling");
+            return AVERROR_PATCHWELCOME;
+        }
+
+        if (uncc->block_pad_lsb) {
+            avpriv_request_sample(fc, "uncC: Block pad LSB");
+            return AVERROR_PATCHWELCOME;
+        }
+        if (uncc->block_little_endian) {
+            avpriv_request_sample(fc, "uncC: Block little endian");
+            return AVERROR_PATCHWELCOME;
+        }
+        if (uncc->block_reversed) {
+            avpriv_request_sample(fc, "uncC: Block reversed");
+            return AVERROR_PATCHWELCOME;
+        }
+        if (uncc->pad_unknown) {
+            avpriv_request_sample(fc, "uncC: Pad unknown");
+            return AVERROR_PATCHWELCOME;
+        }
+        if (uncc->pixel_size) {
+            /* If 0, then no additional padding is present after each pixel */
+            avpriv_request_sample(fc, "uncC: Pixel size handling");
+            return AVERROR_PATCHWELCOME;
+        }
+        if (uncc->row_align_size) {
+            /* Indicates the padding between rows */
+            avpriv_request_sample(fc, "uncC: Row alignment handling");
+            return AVERROR_PATCHWELCOME;
+        }
+        if (uncc->tile_align_size) {
+            /* Indicates the padding between tiles */
+            avpriv_request_sample(fc, "uncC: Tile alignment handling");
+            return AVERROR_PATCHWELCOME;
+        }
+        if (uncc->num_tile_cols_minus_one || uncc->num_tile_rows_minus_one) {
+            avpriv_request_sample(fc, "uncC: tiling");
+            return AVERROR_PATCHWELCOME;
+        }
+
+        switch (uncc->component_count) {
+        case 1:
+            comp = &uncc->components[0];
+            if (comp->component_bit_depth == 8) {
+                st->codecpar->format = AV_PIX_FMT_GRAY8;
+            } else {
+                avpriv_request_sample(fc,
+                    "uncC: Unsupported monochrome format");
+                return AVERROR_PATCHWELCOME;
+            }
+            break;
+        case 0:
+            av_log(fc, AV_LOG_ERROR, "uncC: component count is 0\n");
+            return AVERROR_INVALIDDATA;
+        default:
+            avpriv_request_sample(fc,
+                "uncC: component count: %"PRIu32, uncc->component_count);
+            return AVERROR_PATCHWELCOME;
+        }
+    } else {
+        av_log(fc, AV_LOG_ERROR, "uncC version: %"PRIu8"\n", uncc->version);
+        return AVERROR_INVALIDDATA;
+    }
+
+    return 0;
+}
+
 static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
                                    AVStream *st, MOVStreamContext *sc)
 {
@@ -3416,6 +3622,10 @@ static int mov_finalize_stsd_codec(MOVContext *c, 
AVIOContext *pb,
     case AV_CODEC_ID_H264:
         sti->need_parsing = AVSTREAM_PARSE_HEADERS;
         break;
+    case AV_CODEC_ID_RAWVIDEO:
+        if (st->codecpar->codec_tag == MKTAG('u','n','c','v'))
+            return mov_uncv_set_pix_fmt(c->fc, st, sc);
+        break;
     default:
         break;
     }
@@ -10435,43 +10645,154 @@ static int mov_read_mhac(MOVContext *c, AVIOContext 
*pb, MOVAtom atom)
 static int mov_read_uncc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 {
     AVStream *st;
-    int version;
-    uint32_t profile;
+    MOVStreamContext *sc;
+    MOVUncc *uncc;
+    MOVUnccComponent *comp;
+    int64_t expected_size;
+    uint8_t block_flags;
 
     if (c->fc->nb_streams < 1)
         return 0;
     st = c->fc->streams[c->fc->nb_streams - 1];
+    sc = st->priv_data;
+
+    if (sc->uncc) {
+        av_log(c->fc, AV_LOG_ERROR, "uncC: duplicate atom\n");
+        return AVERROR_INVALIDDATA;
+    }
 
     if (atom.size < 8)
         return AVERROR_INVALIDDATA;
 
-    version = avio_r8(pb);
-    avio_rb24(pb); /* flags */
-    profile = avio_rl32(pb);
+    sc->uncc = av_mallocz(sizeof(*sc->uncc));
+    if (!sc->uncc)
+        return AVERROR(ENOMEM);
 
-    if (version == 1) {
-        switch (profile) {
-            case MKTAG('r','g','b','3'):
-                st->codecpar->format = AV_PIX_FMT_RGB24;
-                break;
-            case MKTAG('r','g','b','a'):
-                st->codecpar->format = AV_PIX_FMT_RGBA;
-                break;
-            default:
-                avpriv_request_sample(c->fc, "Unknown uncC profile %s", 
av_fourcc2str(profile));
-                return AVERROR_INVALIDDATA;
+    uncc = sc->uncc;
+    uncc->version = avio_r8(pb);
+    avio_rb24(pb); /* flags */
+    uncc->profile = avio_rl32(pb);
+
+    if (uncc->version == 1) {
+        if (atom.size > 8) {
+            av_log(c->fc, AV_LOG_ERROR, "uncC: version 1 contains extra 
data\n");
+            return AVERROR_INVALIDDATA;
         }
-    } else if (version == 0) {
-        avpriv_request_sample(c->fc, "uncC version 0 not yet implemented");
-        return AVERROR_PATCHWELCOME;
+    } else if (uncc->version == 0) {
+        if (atom.size < 12)
+            return AVERROR_INVALIDDATA;
+
+        uncc->component_count = avio_rb32(pb);
+
+        if (uncc->component_count == 0) {
+            av_log(c->fc, AV_LOG_ERROR, "uncC: component_count is zero\n");
+            return AVERROR_INVALIDDATA;
+        }
+
+        expected_size = 36 + (int64_t)uncc->component_count * 5;
+        if (atom.size != expected_size) {
+            av_log(c->fc, AV_LOG_ERROR,
+                  "uncC: expected size: %"PRId64", actual size: %"PRId64"\n",
+                  expected_size, atom.size);
+            return AVERROR_INVALIDDATA;
+        }
+
+        uncc->components = av_calloc(uncc->component_count, 
sizeof(*uncc->components));
+        if (!uncc->components)
+            return AVERROR(ENOMEM);
+
+        for (uint32_t i = 0; i < uncc->component_count; i++) {
+            comp = &uncc->components[i];
+            comp->component_index = avio_rb16(pb);
+            comp->component_bit_depth = avio_r8(pb) + 1;
+            comp->component_format = avio_r8(pb);
+            comp->component_align_size = avio_r8(pb);
+        }
+
+        uncc->sampling_type = avio_r8(pb);
+        uncc->interleave_type = avio_r8(pb);
+        uncc->block_size = avio_r8(pb);
+
+        block_flags = avio_r8(pb);
+        uncc->components_little_endian = (block_flags >> 7) & 1;
+        uncc->block_pad_lsb = (block_flags >> 6) & 1;
+        uncc->block_little_endian = (block_flags >> 5) & 1;
+        uncc->block_reversed = (block_flags >> 4) & 1;
+        uncc->pad_unknown = (block_flags >> 3) & 1;
+
+        uncc->pixel_size = avio_rb32(pb);
+        uncc->row_align_size = avio_rb32(pb);
+        uncc->tile_align_size = avio_rb32(pb);
+        uncc->num_tile_cols_minus_one = avio_rb32(pb);
+        uncc->num_tile_rows_minus_one = avio_rb32(pb);
     } else {
-        av_log(c->fc, AV_LOG_WARNING, "Unknown uncC version %d\n", version);
+        av_log(c->fc, AV_LOG_ERROR, "uncC: unknown version: %"PRIu8"\n", 
uncc->version);
         return AVERROR_INVALIDDATA;
     }
 
     return 0;
 }
 
+static int mov_read_cmpd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
+{
+    AVStream *st;
+    MOVStreamContext *sc;
+    uint32_t comp_count;
+    MOVCmpd *cmpd;
+    uint16_t *types;
+    int64_t minimum_size;
+
+    if (c->fc->nb_streams < 1)
+        return 0;
+    st = c->fc->streams[c->fc->nb_streams - 1];
+    sc = st->priv_data;
+
+    if (sc->cmpd) {
+        av_log(c->fc, AV_LOG_ERROR, "cmpd: Multiple cmpd boxes found\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (atom.size < 4) {
+        av_log(c->fc, AV_LOG_ERROR, "cmpd: box too small\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    sc->cmpd = av_mallocz(sizeof(*sc->cmpd));
+    if (!sc->cmpd)
+        return AVERROR(ENOMEM);
+    cmpd = sc->cmpd;
+
+    cmpd->component_count = avio_rb32(pb);
+    comp_count = cmpd->component_count;
+
+    if (comp_count == 0) {
+        av_log(c->fc, AV_LOG_ERROR, "cmpd: component_count is zero\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    minimum_size = 4 + (int64_t)comp_count * sizeof(*types);
+
+    if (atom.size < minimum_size) {
+        av_log(c->fc, AV_LOG_ERROR, "cmpd: box too small\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    cmpd->component_types = av_calloc(comp_count, sizeof(*types));
+    if (!cmpd->component_types)
+        return AVERROR(ENOMEM);
+    types = cmpd->component_types;
+
+    for (uint32_t i = 0; i < comp_count; i++) {
+        types[i] = avio_rb16(pb);
+        if (types[i] >= 0x8000) {
+            avpriv_request_sample(c->fc, "cmpd component_type_uri");
+            return AVERROR_PATCHWELCOME;
+        }
+    }
+
+    return 0;
+}
+
 static const MOVParseTableEntry mov_default_parse_table[] = {
 { MKTAG('A','C','L','R'), mov_read_aclr },
 { MKTAG('A','P','R','G'), mov_read_avid },
@@ -10510,6 +10831,7 @@ static const MOVParseTableEntry 
mov_default_parse_table[] = {
 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
 { MKTAG('a','v','c','C'), mov_read_glbl },
 { MKTAG('u','n','c','C'), mov_read_uncc },
+{ MKTAG('c','m','p','d'), mov_read_cmpd },
 { MKTAG('p','a','s','p'), mov_read_pasp },
 { MKTAG('c','l','a','p'), mov_read_clap },
 { MKTAG('c','d','s','c'), mov_read_cdsc_rndr },
@@ -11085,6 +11407,15 @@ static void mov_free_stream_context(AVFormatContext 
*s, AVStream *st)
     av_freep(&sc->coll);
     av_freep(&sc->ambient);
 
+    if (sc->cmpd) {
+        av_freep(&sc->cmpd->component_types);
+        av_freep(&sc->cmpd);
+    }
+    if (sc->uncc) {
+        av_freep(&sc->uncc->components);
+        av_freep(&sc->uncc);
+    }
+
 #if CONFIG_IAMFDEC
     if (sc->iamf)
         ff_iamf_read_deinit(sc->iamf);
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index b62aca69885..39ba443f3f8 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -53,8 +53,10 @@ fate-mov-vpcc-reserved-color-tags: CMD = run 
ffprobe$(PROGSSUF)$(EXESUF) -v erro
     -show_entries stream=color_primaries,color_transfer,color_space -of 
default=nw=1 \
     $(TARGET_SAMPLES)/mov/vpcc-reserved-color-tags.mp4
 
-FATE_MOV-$(call DEMDEC, MOV, RAWVIDEO) += fate-mov-uncv-rgb24
+FATE_MOV-$(call DEMDEC, MOV, RAWVIDEO) += fate-mov-uncv-rgb24 \
+                                         fate-mov-uncv-gray8
 fate-mov-uncv-rgb24: CMD = framecrc -i $(TARGET_SAMPLES)/mov/uncv-rgb24.mp4
+fate-mov-uncv-gray8: CMD = framecrc -i $(TARGET_SAMPLES)/mov/uncv-gray8.mp4
 
 FATE_MOV_FFPROBE-$(CONFIG_MOV_DEMUXER) += fate-ffprobe-stream-entries-tmcd \
                                        fate-ffprobe-group-stream-entries-tmcd \
diff --git a/tests/ref/fate/mov-uncv-gray8 b/tests/ref/fate/mov-uncv-gray8
new file mode 100644
index 00000000000..cd47d766e1e
--- /dev/null
+++ b/tests/ref/fate/mov-uncv-gray8
@@ -0,0 +1,8 @@
+#tb 0: 1/1
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 128x64
+#sar 0: 0/1
+0,          0,          0,        1,     8192, 0x38b48078
+0,          1,          1,        1,     8192, 0x716800ff
+0,          2,          2,        1,     8192, 0xaa1c8177
-- 
2.54.1

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

Reply via email to