Commit 118b15dde6 for ffmpeg
commit 118b15dde6e4ffd131986e9dcde6092533d9a79b
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Tue Nov 17 10:53:32 2026 -0800
avformat/mov: read the gama atom
Use it for the transfer when the colr atom of the same sample description leaves
it unspecified, as QuickTime does, and every sample description has that gamma.
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/libavformat/isom.h b/libavformat/isom.h
index 6230218e37..7ad42f5280 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -262,6 +262,8 @@ typedef struct MOVStreamContext {
int *extradata_size;
int last_stsd_index;
int stsd_count;
+ int stsd_gama_trc; ///< transfer from the gama atom of the sample description being read
+ int stsd_colr_trc; ///< the colr atom of the sample description being read sets the transfer
int stsd_version;
int32_t *display_matrix;
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 85936bf825..41c2f1f409 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -2335,6 +2335,8 @@ static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
ret = ffio_read_size(pb, icc_profile, atom.size - 4);
if (ret < 0)
return ret;
+ if (st)
+ ((MOVStreamContext *)st->priv_data)->stsd_colr_trc = 1;
} else if (st) {
color_primaries = avio_rb16(pb);
color_trc = avio_rb16(pb);
@@ -2368,12 +2370,33 @@ static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
st->codecpar->color_primaries = color_primaries;
st->codecpar->color_trc = color_trc;
+ ((MOVStreamContext *)st->priv_data)->stsd_colr_trc = color_trc != AVCOL_TRC_UNSPECIFIED;
st->codecpar->color_space = color_matrix;
av_log(c->fc, AV_LOG_TRACE, "\n");
}
return 0;
}
+static int mov_read_gama(MOVContext *c, AVIOContext *pb, MOVAtom atom)
+{
+ AVStream *st = get_curr_st(c);
+ MOVStreamContext *sc;
+ double gamma;
+
+ if (!st || atom.size < 4)
+ return 0;
+ sc = st->priv_data;
+
+ gamma = avio_rb32(pb) / 65536.0;
+ av_log(c->fc, AV_LOG_TRACE, "gama %f\n", gamma);
+
+ if (fabs(gamma - 2.2) < 0.01)
+ sc->stsd_gama_trc = AVCOL_TRC_GAMMA22;
+ else if (fabs(gamma - 2.8) < 0.01)
+ sc->stsd_gama_trc = AVCOL_TRC_GAMMA28;
+ return 0;
+}
+
static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
@@ -3450,6 +3473,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
AVStream *st;
MOVStreamContext *sc;
int pseudo_stream_id;
+ int gama_trc = -1;
av_assert0 (c->fc->nb_streams >= 1);
st = c->fc->streams[c->fc->nb_streams-1];
@@ -3485,6 +3509,8 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
sc->dref_id= dref_id;
sc->format = format;
+ sc->stsd_gama_trc = AVCOL_TRC_UNSPECIFIED;
+ sc->stsd_colr_trc = 0;
id = mov_codec_id(st, format);
@@ -3524,6 +3550,14 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
} else if (a.size > 0)
avio_skip(pb, a.size);
+ // Use gama only when colr does not specify the transfer, like QuickTime does
+ if (sc->stsd_colr_trc)
+ sc->stsd_gama_trc = AVCOL_TRC_UNSPECIFIED;
+ if (gama_trc < 0)
+ gama_trc = sc->stsd_gama_trc;
+ else if (gama_trc != sc->stsd_gama_trc)
+ gama_trc = AVCOL_TRC_UNSPECIFIED;
+
ret = mov_finalize_stsd_entry(c, st);
if (ret < 0)
return ret;
@@ -3540,6 +3574,12 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
sc->stsd_count++;
}
+ // Only when every sample description has the same gamma,
+ // and nothing else (e.g. vpcC) set the transfer
+ if ((gama_trc == AVCOL_TRC_GAMMA22 || gama_trc == AVCOL_TRC_GAMMA28) &&
+ st->codecpar->color_trc == AVCOL_TRC_UNSPECIFIED)
+ st->codecpar->color_trc = gama_trc;
+
if (pb->eof_reached) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSD atom\n");
return AVERROR_EOF;
@@ -10439,6 +10479,7 @@ static const MOVParseTableEntry mov_default_parse_table[] = {
{ MKTAG('c','h','p','l'), mov_read_chpl },
{ MKTAG('c','o','6','4'), mov_read_stco },
{ MKTAG('c','o','l','r'), mov_read_colr },
+{ MKTAG('g','a','m','a'), mov_read_gama },
{ MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
{ MKTAG('d','i','n','f'), mov_read_default },
{ MKTAG('D','p','x','E'), mov_read_dpxe },