From d73b6503f54f59a1c2366f04b2f9131aa63fbdfc Mon Sep 17 00:00:00 2001 From: Philip de Nier Date: Mon, 22 Apr 2024 13:33:00 +0100 Subject: [PATCH] Use aspect ratio from mpeg2 video bitstream if known --- include/bmx/mxf_helper/MPEG2LGMXFDescriptorHelper.h | 2 ++ include/bmx/writer_helper/MPEG2LGWriterHelper.h | 3 +++ src/as02/AS02MPEG2LGTrack.cpp | 1 + src/avid_mxf/AvidMPEG2LGTrack.cpp | 1 + src/mxf_helper/MPEG2LGMXFDescriptorHelper.cpp | 11 +++++++++++ src/mxf_op1a/OP1AMPEG2LGTrack.cpp | 1 + src/rdd9_mxf/RDD9MPEG2LGTrack.cpp | 1 + src/writer_helper/MPEG2LGWriterHelper.cpp | 9 +++++++++ test/avid_mxf/test_mpeg2lg.cmake | 2 +- 9 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/bmx/mxf_helper/MPEG2LGMXFDescriptorHelper.h b/include/bmx/mxf_helper/MPEG2LGMXFDescriptorHelper.h index da9125b9..b9897649 100644 --- a/include/bmx/mxf_helper/MPEG2LGMXFDescriptorHelper.h +++ b/include/bmx/mxf_helper/MPEG2LGMXFDescriptorHelper.h @@ -34,6 +34,7 @@ #include +#include @@ -66,6 +67,7 @@ class MPEG2LGMXFDescriptorHelper : public PictureMXFDescriptorHelper virtual mxfpp::FileDescriptor* CreateFileDescriptor(mxfpp::HeaderMetadata *header_metadata); virtual void UpdateFileDescriptor(); + void UpdateFileDescriptor(MPEG2EssenceParser *essence_parser); public: MXFFrameLayout GetFrameLayout() const; diff --git a/include/bmx/writer_helper/MPEG2LGWriterHelper.h b/include/bmx/writer_helper/MPEG2LGWriterHelper.h index 794574ec..42ab85e0 100644 --- a/include/bmx/writer_helper/MPEG2LGWriterHelper.h +++ b/include/bmx/writer_helper/MPEG2LGWriterHelper.h @@ -35,6 +35,7 @@ #include #include +#include @@ -55,6 +56,7 @@ class MPEG2LGWriterHelper MPEG2LGWriterHelper(); ~MPEG2LGWriterHelper(); + void SetDescriptorHelper(MPEG2LGMXFDescriptorHelper *descriptor_helper); void SetFlavour(Flavour flavour); public: @@ -85,6 +87,7 @@ class MPEG2LGWriterHelper uint32_t GetBitRate() const { return mBitRate; } private: + MPEG2LGMXFDescriptorHelper *mDescriptorHelper; Flavour mFlavour; MPEG2EssenceParser mEssenceParser; diff --git a/src/as02/AS02MPEG2LGTrack.cpp b/src/as02/AS02MPEG2LGTrack.cpp index e282deeb..85c81e5b 100644 --- a/src/as02/AS02MPEG2LGTrack.cpp +++ b/src/as02/AS02MPEG2LGTrack.cpp @@ -63,6 +63,7 @@ AS02MPEG2LGTrack::AS02MPEG2LGTrack(AS02Clip *clip, uint32_t track_index, Essence { mTrackNumber = MXF_MPEG_PICT_TRACK_NUM(0x01, MXF_MPEG_PICT_FRAME_WRAPPED_EE_TYPE, 0x00); mEssenceElementKey = VIDEO_ELEMENT_KEY; + mWriterHelper.SetDescriptorHelper(dynamic_cast(mDescriptorHelper)); mIndexStartPosition = 0; } diff --git a/src/avid_mxf/AvidMPEG2LGTrack.cpp b/src/avid_mxf/AvidMPEG2LGTrack.cpp index 644ba37c..6201df9b 100644 --- a/src/avid_mxf/AvidMPEG2LGTrack.cpp +++ b/src/avid_mxf/AvidMPEG2LGTrack.cpp @@ -53,6 +53,7 @@ using namespace mxfpp; AvidMPEG2LGTrack::AvidMPEG2LGTrack(AvidClip *clip, uint32_t track_index, EssenceType essence_type, File *file) : AvidPictureTrack(clip, track_index, essence_type, file) { + mWriterHelper.SetDescriptorHelper(dynamic_cast(mDescriptorHelper)); mWriterHelper.SetFlavour(MPEG2LGWriterHelper::AVID_FLAVOUR); mTrackNumber = MXF_AVID_MPEG_PICT_TRACK_NUM; diff --git a/src/mxf_helper/MPEG2LGMXFDescriptorHelper.cpp b/src/mxf_helper/MPEG2LGMXFDescriptorHelper.cpp index b8187ba0..e5c29644 100644 --- a/src/mxf_helper/MPEG2LGMXFDescriptorHelper.cpp +++ b/src/mxf_helper/MPEG2LGMXFDescriptorHelper.cpp @@ -441,6 +441,17 @@ void MPEG2LGMXFDescriptorHelper::UpdateFileDescriptor() } } +void MPEG2LGMXFDescriptorHelper::UpdateFileDescriptor(MPEG2EssenceParser *essence_parser) +{ + UpdateFileDescriptor(); + + GenericPictureEssenceDescriptor *picture_descriptor = dynamic_cast(mFileDescriptor); + BMX_ASSERT(picture_descriptor); + + if (essence_parser->HaveKnownAspectRatio()) + picture_descriptor->setAspectRatio(essence_parser->GetAspectRatio()); +} + MXFFrameLayout MPEG2LGMXFDescriptorHelper::GetFrameLayout() const { return SUPPORTED_ESSENCE[mEssenceIndex].frame_layout; diff --git a/src/mxf_op1a/OP1AMPEG2LGTrack.cpp b/src/mxf_op1a/OP1AMPEG2LGTrack.cpp index 1ba42178..cf37b8fd 100644 --- a/src/mxf_op1a/OP1AMPEG2LGTrack.cpp +++ b/src/mxf_op1a/OP1AMPEG2LGTrack.cpp @@ -53,6 +53,7 @@ OP1AMPEG2LGTrack::OP1AMPEG2LGTrack(OP1AFile *file, uint32_t track_index, uint32_ { mTrackNumber = MXF_MPEG_PICT_TRACK_NUM(0x01, MXF_MPEG_PICT_FRAME_WRAPPED_EE_TYPE, 0x00); mEssenceElementKey = VIDEO_ELEMENT_KEY; + mWriterHelper.SetDescriptorHelper(dynamic_cast(mDescriptorHelper)); } OP1AMPEG2LGTrack::~OP1AMPEG2LGTrack() diff --git a/src/rdd9_mxf/RDD9MPEG2LGTrack.cpp b/src/rdd9_mxf/RDD9MPEG2LGTrack.cpp index d4b79bea..a6760f8d 100644 --- a/src/rdd9_mxf/RDD9MPEG2LGTrack.cpp +++ b/src/rdd9_mxf/RDD9MPEG2LGTrack.cpp @@ -53,6 +53,7 @@ RDD9MPEG2LGTrack::RDD9MPEG2LGTrack(RDD9File *file, uint32_t track_index, uint32_ { mPictureDescriptorHelper = dynamic_cast(mDescriptorHelper); BMX_ASSERT(mPictureDescriptorHelper); + mWriterHelper.SetDescriptorHelper(dynamic_cast(mDescriptorHelper)); mValidator = 0; mPictureDescriptorHelper->SetAspectRatio(ASPECT_RATIO_16_9); diff --git a/src/writer_helper/MPEG2LGWriterHelper.cpp b/src/writer_helper/MPEG2LGWriterHelper.cpp index 66706eb8..6f5e62f5 100644 --- a/src/writer_helper/MPEG2LGWriterHelper.cpp +++ b/src/writer_helper/MPEG2LGWriterHelper.cpp @@ -51,6 +51,7 @@ using namespace bmx; MPEG2LGWriterHelper::MPEG2LGWriterHelper() { + mDescriptorHelper = 0; mFlavour = DEFAULT_FLAVOUR; mPosition = 0; mPrevKeyFramePosition = -1; @@ -84,6 +85,11 @@ MPEG2LGWriterHelper::~MPEG2LGWriterHelper() { } +void MPEG2LGWriterHelper::SetDescriptorHelper(MPEG2LGMXFDescriptorHelper *descriptor_helper) +{ + mDescriptorHelper = descriptor_helper; +} + void MPEG2LGWriterHelper::SetFlavour(Flavour flavour) { mFlavour = flavour; @@ -222,6 +228,9 @@ void MPEG2LGWriterHelper::ProcessFrame(const unsigned char *data, uint32_t size) mKeyFrameTemporalReference = mTemporalReference; } + if (mPosition == 0 && mDescriptorHelper) + mDescriptorHelper->UpdateFileDescriptor(&mEssenceParser); + mPosition++; } diff --git a/test/avid_mxf/test_mpeg2lg.cmake b/test/avid_mxf/test_mpeg2lg.cmake index 66bcf549..00980fb5 100644 --- a/test/avid_mxf/test_mpeg2lg.cmake +++ b/test/avid_mxf/test_mpeg2lg.cmake @@ -1,4 +1,4 @@ -# Test creating an Avid MXF file containing AVC video and 2 PCM tracks +# Test creating an Avid MXF file containing MPEG2 video and 2 PCM tracks include("${TEST_SOURCE_DIR}/test_common.cmake")