diff --git a/libavformat/isom.h b/libavformat/isom.h index d9956cf63a..426f732247 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -27,6 +27,7 @@ #include #include +#include "libavutil/mastering_display_metadata.h" #include "libavutil/spherical.h" #include "libavutil/stereo3d.h" @@ -194,6 +195,7 @@ typedef struct MOVStreamContext { AVStereo3D *stereo3d; AVSphericalMapping *spherical; size_t spherical_size; + AVMasteringDisplayMetadata *mastering; uint32_t format; diff --git a/libavformat/mov.c b/libavformat/mov.c index 90b0ab1de7..7535bb9925 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -4612,6 +4612,52 @@ static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } +static int mov_read_smdm(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + MOVStreamContext *sc; + const int chroma_den = 50000; + const int luma_den = 10000; + int i, j, version; + + if (c->fc->nb_streams < 1) + return AVERROR_INVALIDDATA; + + sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data; + + if (atom.size < 5) { + av_log(c->fc, AV_LOG_ERROR, "Empty Mastering Display Metadata box\n"); + return AVERROR_INVALIDDATA; + } + + version = avio_r8(pb); + if (version) { + av_log(c->fc, AV_LOG_WARNING, "Unsupported Mastering Display Metadata box version %d\n", version); + return 0; + } + avio_skip(pb, 3); /* flags */ + + sc->mastering = av_mastering_display_metadata_alloc(); + if (!sc->mastering) + return AVERROR(ENOMEM); + + for (i = 0; i < 3; i++) + for (j = 0; j < 2; j++) + sc->mastering->display_primaries[i][j] = + av_make_q(lrint(((double)avio_rb16(pb) / (1 << 16)) * chroma_den), chroma_den); + for (i = 0; i < 2; i++) + sc->mastering->white_point[i] = + av_make_q(lrint(((double)avio_rb16(pb) / (1 << 16)) * chroma_den), chroma_den); + sc->mastering->max_luminance = + av_make_q(lrint(((double)avio_rb32(pb) / (1 << 8)) * luma_den), luma_den); + sc->mastering->min_luminance = + av_make_q(lrint(((double)avio_rb32(pb) / (1 << 14)) * luma_den), luma_den); + + sc->mastering->has_primaries = 1; + sc->mastering->has_luminance = 1; + + return 0; +} + static int mov_read_st3d(MOVContext *c, AVIOContext *pb, MOVAtom atom) { AVStream *st; @@ -5398,6 +5444,7 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('s','t','3','d'), mov_read_st3d }, /* stereoscopic 3D video box */ { MKTAG('s','v','3','d'), mov_read_sv3d }, /* spherical video box */ { MKTAG('d','O','p','s'), mov_read_dops }, +{ MKTAG('S','m','D','m'), mov_read_smdm }, { 0, NULL } }; @@ -5822,6 +5869,7 @@ static int mov_read_close(AVFormatContext *s) av_freep(&sc->stereo3d); av_freep(&sc->spherical); + av_freep(&sc->mastering); } if (mov->dv_demux) { @@ -6172,6 +6220,15 @@ static int mov_read_header(AVFormatContext *s) sc->spherical = NULL; } + if (sc->mastering) { + err = av_stream_add_side_data(st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, + (uint8_t *)sc->mastering, + sizeof(*sc->mastering)); + if (err < 0) + return err; + + sc->mastering = NULL; + } break; } }