From patchwork Wed Sep 9 14:56:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Harry Mallon X-Patchwork-Id: 22231 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 789F044A031 for ; Wed, 9 Sep 2020 18:03:19 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4C85968B8BD; Wed, 9 Sep 2020 18:03:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E978168B7CB for ; Wed, 9 Sep 2020 18:03:12 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id m6so3363904wrn.0 for ; Wed, 09 Sep 2020 08:03:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=codex.online; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=+2SIYZVKZbvPSqdh8vb3d5eZR3m5+WY0jqF5rvluFwU=; b=G7EgcI+47Iidxn2hTtGqiDUQH4cGZ93mjPMC0RJTNGik92pl/HwPjBQf5dhyD5dBN3 uai/asVUdXBd51RF36bsgWqUAiyWrN82ARnyDOb2YRcvQ3o7fZGhuGFx2L+3qIP45QSj 0AB8nEhwafMrxzOMMUq8/xJHDOYrsmaHV2q3Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+2SIYZVKZbvPSqdh8vb3d5eZR3m5+WY0jqF5rvluFwU=; b=sBDkWg3gE/mzvzEWOPbBzXpXtDsw2Fj4Uz3pio2jU2Bo8JV0yoT8BBKCmA6Fnv6cWJ wRXRzDLnNsk9ov4fQ/yd3wENi7fuxnCdXlSU2db+vGbznxWKOCKNMwTIl48Bae2QEB9X iUhTuBwUmyOZKnq08QXWWvYgxwaXVg4WX8vJ/2R6LQvgR/heKfua8CDDiru9iVLMony6 weute90xuRUEl6wKgkIXMFOj+qLt20EFpmvAN3nI0EB62R2JQ5T5mtiqp07qxkA7K+OP b39/5p/e+rbyNIXujZUtzjyq2p7nYKOaDtDHlYE30C3+5T1g3n4z1KXrJZ7HWft+Kt+O LpZw== X-Gm-Message-State: AOAM532k3e+doW4gc4D/1pKhQHxoMVuHImR4IcxWb05vyXjv0uuDHUs3 d/H9FtVJgZhpYIkduSwyfNgb+Qi9FgG3oykP X-Google-Smtp-Source: ABdhPJwk47e73M9a22v1TKUIy/qRSkrb+TlMzygyaNykD6AVaTIVDtIQajjwXfmzaO3wrzKCSMeZbA== X-Received: by 2002:adf:f101:: with SMTP id r1mr4273733wro.314.1599663402633; Wed, 09 Sep 2020 07:56:42 -0700 (PDT) Received: from localhost ([2a01:4b00:8571:3800:9b7:7612:40c6:2653]) by smtp.gmail.com with ESMTPSA id 8sm5046786wrl.7.2020.09.09.07.56.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 09 Sep 2020 07:56:41 -0700 (PDT) From: Harry Mallon To: ffmpeg-devel@ffmpeg.org Date: Wed, 9 Sep 2020 15:56:37 +0100 Message-Id: <20200909145639.14922-1-harry.mallon@codex.online> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200831190756.80515-1-harry.mallon@codex.online> References: <20200831190756.80515-1-harry.mallon@codex.online> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/3] avformat/mxfdec: Read Mastering Display Colour Volume from MXF X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Described in Annex B SMPTE ST 2067-21:2020 Signed-off-by: Harry Mallon --- libavformat/mxf.c | 11 ++++++++++ libavformat/mxf.h | 11 ++++++++++ libavformat/mxfdec.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ libavformat/mxfenc.c | 5 ----- 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/libavformat/mxf.c b/libavformat/mxf.c index e51fc48a84..88f69ebcfb 100644 --- a/libavformat/mxf.c +++ b/libavformat/mxf.c @@ -22,6 +22,17 @@ #include "libavutil/common.h" #include "mxf.h" +const uint8_t ff_mxf_mastering_display_prefix[13] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x04,0x20,0x04,0x01,0x01 }; + +/* be careful to update references to this array if reordering */ +/* local tags are dynamic and must not clash with others in mxfenc.c */ +const MXFLocalTagPair ff_mxf_mastering_display_local_tags[4] = { + { 0x8301, { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x04,0x20,0x04,0x01,0x01,0x01,0x00,0x00 }}, /* Mastering Display Primaries */ + { 0x8302, { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x04,0x20,0x04,0x01,0x01,0x02,0x00,0x00 }}, /* Mastering Display White Point Chromaticity */ + { 0x8303, { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x04,0x20,0x04,0x01,0x01,0x03,0x00,0x00 }}, /* Mastering Display Maximum Luminance */ + { 0x8304, { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x04,0x20,0x04,0x01,0x01,0x04,0x00,0x00 }} /* Mastering Display Minimum Luminance */ +}; + /** * SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */ diff --git a/libavformat/mxf.h b/libavformat/mxf.h index fc587f19f0..7fa10bcca1 100644 --- a/libavformat/mxf.h +++ b/libavformat/mxf.h @@ -78,6 +78,17 @@ typedef enum { RawVWrap } MXFWrappingIndicatorType; +typedef struct MXFLocalTagPair { + int local_tag; + UID uid; +} MXFLocalTagPair; + +extern const uint8_t ff_mxf_mastering_display_prefix[13]; +extern const MXFLocalTagPair ff_mxf_mastering_display_local_tags[4]; + +#define FF_MXF_MASTERING_CHROMA_DEN 50000 +#define FF_MXF_MASTERING_LUMA_DEN 10000 + typedef struct MXFCodecUL { UID uid; unsigned matching_len; diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 6f6e8d586c..8d315620bc 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -28,6 +28,7 @@ * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container * SMPTE 382M Mapping AES3 and Broadcast Wave Audio into the MXF Generic Container * SMPTE 383M Mapping DV-DIF Data to the MXF Generic Container + * SMPTE 2067-21 Interoperable Master Format — Application #2E * * Principle * Search for Track numbers which will identify essence element KLV packets. @@ -47,6 +48,7 @@ #include "libavutil/aes.h" #include "libavutil/avassert.h" +#include "libavutil/mastering_display_metadata.h" #include "libavutil/mathematics.h" #include "libavcodec/bytestream.h" #include "libavutil/intreadwrite.h" @@ -213,6 +215,7 @@ typedef struct MXFDescriptor { UID color_primaries_ul; UID color_trc_ul; UID color_space_ul; + AVMasteringDisplayMetadata *mastering; } MXFDescriptor; typedef struct MXFIndexTableSegment { @@ -337,6 +340,7 @@ static void mxf_free_metadataset(MXFMetadataSet **ctx, int freectx) switch ((*ctx)->type) { case Descriptor: av_freep(&((MXFDescriptor *)*ctx)->extradata); + av_freep(&((MXFDescriptor *)*ctx)->mastering); break; case MultipleDescriptor: av_freep(&((MXFDescriptor *)*ctx)->sub_descriptors_refs); @@ -1272,6 +1276,42 @@ static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int rsiz == FF_PROFILE_JPEG2000_DCINEMA_4K) descriptor->pix_fmt = AV_PIX_FMT_XYZ12; } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_prefix)) { + if (!descriptor->mastering) { + descriptor->mastering = av_mastering_display_metadata_alloc(); + if (!descriptor->mastering) + return AVERROR(ENOMEM); + } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_local_tags[0].uid)) { + for (int i = 0; i < 3; i++) { + /* Order: large x, large y, other (i.e. RGB) */ + descriptor->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), FF_MXF_MASTERING_CHROMA_DEN); + descriptor->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), FF_MXF_MASTERING_CHROMA_DEN); + } + /* Check we have seen mxf_mastering_display_white_point_chromaticity */ + if (descriptor->mastering->white_point[0].den != 0) + descriptor->mastering->has_primaries = 1; + } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_local_tags[1].uid)) { + descriptor->mastering->white_point[0] = av_make_q(avio_rb16(pb), FF_MXF_MASTERING_CHROMA_DEN); + descriptor->mastering->white_point[1] = av_make_q(avio_rb16(pb), FF_MXF_MASTERING_CHROMA_DEN); + /* Check we have seen mxf_mastering_display_primaries */ + if (descriptor->mastering->display_primaries[0][0].den != 0) + descriptor->mastering->has_primaries = 1; + } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_local_tags[2].uid)) { + descriptor->mastering->max_luminance = av_make_q(avio_rb32(pb), FF_MXF_MASTERING_LUMA_DEN); + /* Check we have seen mxf_mastering_display_minimum_luminance */ + if (descriptor->mastering->min_luminance.den != 0) + descriptor->mastering->has_luminance = 1; + } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_local_tags[3].uid)) { + descriptor->mastering->min_luminance = av_make_q(avio_rb32(pb), FF_MXF_MASTERING_LUMA_DEN); + /* Check we have seen mxf_mastering_display_maximum_luminance */ + if (descriptor->mastering->max_luminance.den != 0) + descriptor->mastering->has_luminance = 1; + } + } break; } return 0; @@ -2532,6 +2572,14 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) st->codecpar->color_primaries = mxf_get_codec_ul(ff_mxf_color_primaries_uls, &descriptor->color_primaries_ul)->id; st->codecpar->color_trc = mxf_get_codec_ul(ff_mxf_color_trc_uls, &descriptor->color_trc_ul)->id; st->codecpar->color_space = mxf_get_codec_ul(ff_mxf_color_space_uls, &descriptor->color_space_ul)->id; + if (descriptor->mastering) { + ret = av_stream_add_side_data(st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, + (uint8_t *)descriptor->mastering, + sizeof(*descriptor->mastering)); + if (ret < 0) + goto fail_and_free; + descriptor->mastering = NULL; + } } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul); /* Only overwrite existing codec ID if it is unset or A-law, which is the default according to SMPTE RP 224. */ diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c index e495b5ba0e..733c747a9a 100644 --- a/libavformat/mxfenc.c +++ b/libavformat/mxfenc.c @@ -65,11 +65,6 @@ extern AVOutputFormat ff_mxf_opatom_muxer; #define EDIT_UNITS_PER_BODY 250 #define KAG_SIZE 512 -typedef struct MXFLocalTagPair { - int local_tag; - UID uid; -} MXFLocalTagPair; - typedef struct MXFIndexEntry { uint64_t offset; unsigned slice_offset; ///< offset of audio slice