From 1dfee29db933f05601e632b5a61ddecaf66d21aa Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Fri, 21 May 2021 17:17:15 +0200 Subject: Added the JXS functionality again into the core library. --- src/AS_DCP_JXS.cpp | 1188 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1188 insertions(+) create mode 100644 src/AS_DCP_JXS.cpp (limited to 'src/AS_DCP_JXS.cpp') diff --git a/src/AS_DCP_JXS.cpp b/src/AS_DCP_JXS.cpp new file mode 100644 index 0000000..c69394a --- /dev/null +++ b/src/AS_DCP_JXS.cpp @@ -0,0 +1,1188 @@ +/* +Copyright (c) 2004-2016, John Hurst, +Copyright (c) 2020, Thomas Richter Fraunhofer IIS +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/*! \file AS_DCP_JXS.cpp + \version $Id$ + \brief AS-DCP library, JPEG XS essence reader and writer implementation +*/ + +#include "AS_DCP_internal.h" +#include +#include +#include + +using namespace ASDCP::JXS; +using Kumu::GenRandomValue; + +//------------------------------------------------------------------------------------------ + +static std::string JXS_PACKAGE_LABEL = "File Package: SMPTE 2124 frame wrapping of JPEG XS codestreams"; +//static std::string JP2K_S_PACKAGE_LABEL = "File Package: SMPTE 429-10 frame wrapping of stereoscopic JPEG XS codestreams"; +static std::string PICT_DEF_LABEL = "Picture Track"; + +static int s_exp_lookup[16] = { 0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,2048, 4096, 8192, 16384, 32768 }; + +// +std::ostream& +ASDCP::JXS::operator << (std::ostream& strm, const PictureDescriptor& PDesc) +{ + strm << " AspectRatio: " << PDesc.AspectRatio.Numerator << "/" << PDesc.AspectRatio.Denominator << std::endl; + strm << " EditRate: " << PDesc.EditRate.Numerator << "/" << PDesc.EditRate.Denominator << std::endl; + strm << " SampleRate: " << PDesc.SampleRate.Numerator << "/" << PDesc.SampleRate.Denominator << std::endl; + strm << " StoredWidth: " << (unsigned) PDesc.StoredWidth << std::endl; + strm << " StoredHeight: " << (unsigned) PDesc.StoredHeight << std::endl; + strm << " Wf: " << (unsigned) PDesc.Wf << std::endl; // width of the frame + strm << " Hf: " << (unsigned) PDesc.Hf << std::endl; // height of the frame + strm << " ContainerDuration: " << (unsigned) PDesc.ContainerDuration << std::endl; + + strm << "-- JPEG XS Metadata --" << std::endl; + strm << " ImageComponents:" << std::endl; + strm << " bits h-sep v-sep" << std::endl; + + ui32_t i; + for ( i = 0; i < PDesc.Nc && i < MaxComponents; ++i ) + { + strm << " " << std::setw(4) << PDesc.ImageComponents[i].Bc + << " " << std::setw(5) << PDesc.ImageComponents[i].Sx + << " " << std::setw(5) << PDesc.ImageComponents[i].Sy + << std::endl; + } + + strm << " Slice height: " << (short) PDesc.Hsl << std::endl; + strm << " Profile: " << (short) PDesc.Ppih << std::endl; + strm << " Level: " << (short) (PDesc.Plev >> 8) << std::endl; + strm << " Sublevel: " << (short) (PDesc.Plev & 0xff) << std::endl; + strm << " Column Width: " << (short) (PDesc.Cw) << std::endl; + strm << " Maximum Bit Rate: " << (PDesc.MaximumBitRate) << std::endl; + strm << " Primaries: " << (short) (PDesc.Primaries) << std::endl; + strm << " Transfer Curve: " << (short) (PDesc.TransferCurve) << std::endl; + strm << " Matrix: " << (short) (PDesc.Matrix) << std::endl; + strm << " full range: " << (PDesc.fullRange?("yes"):("no")) << std::endl; + /* + ** thor: at this point, do not print the CAP marker + */ + + return strm; +} + +// +void +ASDCP::JXS::PictureDescriptorDump(const PictureDescriptor& PDesc, FILE* stream) +{ + if ( stream == 0 ) + stream = stderr; + + fprintf(stream, "\ + AspectRatio: %d/%d\n\ + EditRate: %d/%d\n\ + SampleRate: %d/%d\n\ + StoredWidth: %u\n\ + StoredHeight: %u\n\ + Wf: %u\n\ + Hf: %u\n\ + Profile: %u\n\ + Level: %u\n\ + Sublevel: %u\n\ + Maximum BitRate: %u\n\ + ContainerDuration: %u\n\ + Primaries: %u\n\ + Transfer Curve: %u\n\ + Matrix: %u\n\ + full range: %s\n", + PDesc.AspectRatio.Numerator, PDesc.AspectRatio.Denominator, + PDesc.EditRate.Numerator, PDesc.EditRate.Denominator, + PDesc.SampleRate.Numerator, PDesc.SampleRate.Denominator, + PDesc.StoredWidth, + PDesc.StoredHeight, + PDesc.Wf, + PDesc.Hf, + PDesc.Ppih, + PDesc.Plev >> 4, + PDesc.Plev & 0x0f, + PDesc.MaximumBitRate, + PDesc.ContainerDuration, + PDesc.Primaries, + PDesc.TransferCurve, + PDesc.Matrix, + PDesc.fullRange?("yes"):("no") + ); + + fprintf(stream, "-- JPEG XS Metadata --\n"); + fprintf(stream, " ImageComponents:\n"); + fprintf(stream, " bits h-sep v-sep\n"); + + ui32_t i; + for ( i = 0; i < PDesc.Nc && i < MaxComponents; i++ ) + { + fprintf(stream, " %4d %5d %5d\n", + PDesc.ImageComponents[i].Bc, + PDesc.ImageComponents[i].Sx, + PDesc.ImageComponents[i].Sy + ); + } +} + +// +ASDCP::Result_t +ASDCP::JXS_PDesc_to_MD(const JXS::PictureDescriptor& PDesc, + const ASDCP::Dictionary&dict, + ASDCP::MXF::GenericPictureEssenceDescriptor& EssenceDescriptor, + ASDCP::MXF::JPEGXSPictureSubDescriptor& EssenceSubDescriptor) +{ + EssenceDescriptor.ContainerDuration = PDesc.ContainerDuration; + EssenceDescriptor.SampleRate = PDesc.EditRate; + EssenceDescriptor.FrameLayout = 0; + EssenceDescriptor.StoredWidth = PDesc.StoredWidth; + EssenceDescriptor.StoredHeight = PDesc.StoredHeight; + EssenceDescriptor.AspectRatio = PDesc.AspectRatio; + + EssenceSubDescriptor.JPEGXSPpih = PDesc.Ppih; + EssenceSubDescriptor.JPEGXSPlev = PDesc.Plev; + EssenceSubDescriptor.JPEGXSWf = PDesc.Wf; + EssenceSubDescriptor.JPEGXSHf = PDesc.Hf; + EssenceSubDescriptor.JPEGXSNc = PDesc.Nc; + + // Copy the value of the columns, but only if there are some + if (PDesc.Cw) { + EssenceSubDescriptor.JPEGXSCw = optional_property(PDesc.Cw); + } else { + EssenceSubDescriptor.JPEGXSCw.set_has_value(false); + } + + // Copy the slice height. Actually, this is optional + // and does not necessarily require copying all the time, + // but let's copy it nevertheless. + EssenceSubDescriptor.JPEGXSHsl = optional_property(PDesc.Hsl); + + if (PDesc.MaximumBitRate) { + EssenceSubDescriptor.JPEGXSMaximumBitRate = PDesc.MaximumBitRate; + } else { + EssenceSubDescriptor.JPEGXSMaximumBitRate.set_has_value(false); + } + + const ui32_t cdt_buffer_len = 8 * 2; // at most 8 components. + byte_t tmp_buffer[cdt_buffer_len]; + int i,comps = (PDesc.Nc > 8)?8:PDesc.Nc; + EssenceSubDescriptor.JPEGXSComponentTable.Length(4 + (comps << 1)); + // thor: unclear whether the marker size is part of this data. + tmp_buffer[0] = 0xff; + tmp_buffer[1] = 0x13; // the marker + tmp_buffer[2] = 0x00; + tmp_buffer[3] = comps * 2 + 2; // The size. + for(i = 0;i < comps;i++) { + tmp_buffer[4 + (i << 1)] = PDesc.ImageComponents[i].Bc; + tmp_buffer[5 + (i << 1)] = (PDesc.ImageComponents[i].Sx << 4) | (PDesc.ImageComponents[i].Sy); + } + + memcpy(EssenceSubDescriptor.JPEGXSComponentTable.Data(), tmp_buffer, 4 + (comps << 1)); + + // + switch(PDesc.Primaries) { + case 1: + EssenceDescriptor.ColorPrimaries = dict.ul(ASDCP::MDD_ColorPrimaries_ITU709); + break; + case 5: + EssenceDescriptor.ColorPrimaries = dict.ul(ASDCP::MDD_ColorPrimaries_ITU470_PAL); + break; + case 6: + EssenceDescriptor.ColorPrimaries = dict.ul(ASDCP::MDD_ColorPrimaries_SMPTE170M); + break; + case 9: + EssenceDescriptor.ColorPrimaries = dict.ul(ASDCP::MDD_ColorPrimaries_ITU2020); + break; + case 10: + EssenceDescriptor.ColorPrimaries = dict.ul(ASDCP::MDD_ColorPrimaries_SMPTE_DCDM); + break; + case 11: + EssenceDescriptor.ColorPrimaries = dict.ul(ASDCP::MDD_TheatricalViewingEnvironment); + break; + case 12: + EssenceDescriptor.ColorPrimaries = dict.ul(ASDCP::MDD_ColorPrimaries_P3D65); + break; + default: + return RESULT_PARAM; + break; + } + + switch(PDesc.TransferCurve) { + case 1: + case 6: + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_ITU709); + break; + case 5: // Display Gamma 2.8, BT.470-6 This does not seem to be supported + case 9: // Log(100:1) range This does not seem to be supported + case 10:// Log(100*Sqrt(10):1 range) + return Kumu::RESULT_NOTIMPL; + break; + case 8: + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_linear); + break; + case 11: + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_IEC6196624_xvYCC); + break; + case 13: + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_sRGB); + break; + case 14: + case 15: + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_ITU2020); + break; + case 16: + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_SMPTEST2084); + break; + case 17: + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_ST428); + break; + case 18: // HLG + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_HLG); + break; + case 12: // Rec. BT.1361 + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_BT1361); + break; + case 4: // Rec. BT.470 + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_BT470); + break; + case 7: // SMPTE 240M + EssenceDescriptor.TransferCharacteristic = dict.ul(ASDCP::MDD_TransferCharacteristic_ST240M); + break; + case 2: // Unspecified. This leaves the data intentionally undefined. + EssenceDescriptor.TransferCharacteristic.set_has_value(false); + break; + default: + return RESULT_PARAM; + break; + } + // + switch(PDesc.Matrix) { + case 0: // Identity matrix. Use the BGR coding equations. + EssenceDescriptor.CodingEquations = dict.ul(ASDCP::MDD_CodingEquations_BGR); + break; + case 4: // Title 47. + case 10: // ITU 2020 constant luminance? Does not seem to be supported + case 11: // SMPTE ST-2085 + return Kumu::RESULT_NOTIMPL; + case 1: + EssenceDescriptor.CodingEquations = dict.ul(ASDCP::MDD_CodingEquations_709); + break; + // Note: Matrix=2 does not set the optional parameter. This is intentional. + case 5: + case 6: + EssenceDescriptor.CodingEquations = dict.ul(ASDCP::MDD_CodingEquations_601); + break; + case 9: // ITU 2020 non-constant luminance? + EssenceDescriptor.CodingEquations = dict.ul(ASDCP::MDD_CodingEquations_Rec2020); + break; + case 2: // This is unspecified. The metadata item remains undefined on purpose. + EssenceDescriptor.CodingEquations.set_has_value(false); + break; + case 7: // ST 240M + EssenceDescriptor.CodingEquations = dict.ul(ASDCP::MDD_CodingEquations_ST240M); + break; + case 8: // YCgCo + EssenceDescriptor.CodingEquations = dict.ul(ASDCP::MDD_CodingEquations_YCGCO); + break; + default: + return RESULT_PARAM; + break; + } + // +#if 0 + if (rgba) { + byte_t layout[ASDCP::MXF::RGBAValueLength]; + if (m_bFullRange) { + rgba->ComponentMaxRef = (1UL << m_ucPrecision) - 1; + rgba->ComponentMinRef = 0; + } else { + rgba->ComponentMaxRef = 235 * (1UL << (m_ucPrecision - 8)); + rgba->ComponentMinRef = 16 * (1UL << (m_ucPrecision - 8)); + } + layout[0] = 'R'; + layout[1] = m_ucPrecision; + layout[2] = 'G'; + layout[3] = m_ucPrecision; + layout[4] = 'B'; + layout[5] = m_ucPrecision; + layout[6] = 0; +#endif + return RESULT_OK; +} + +// +ASDCP::Result_t +ASDCP::MD_to_JXS_PDesc(const ASDCP::MXF::GenericPictureEssenceDescriptor& EssenceDescriptor, + const ASDCP::MXF::JPEGXSPictureSubDescriptor& EssenceSubDescriptor, + const ASDCP::Rational& EditRate, const ASDCP::Rational& SampleRate, + ASDCP::JXS::PictureDescriptor& PDesc) +{ + const Dictionary *dict = &ASDCP::DefaultSMPTEDict(); + const ASDCP::MXF::RGBAEssenceDescriptor *rgba = dynamic_cast(&EssenceDescriptor); + const ASDCP::MXF::CDCIEssenceDescriptor *cdci = dynamic_cast(&EssenceDescriptor); + memset(&PDesc, 0, sizeof(PDesc)); + + PDesc.EditRate = EditRate; + PDesc.SampleRate = SampleRate; + assert(EssenceDescriptor.ContainerDuration.const_get() <= 0xFFFFFFFFL); + PDesc.ContainerDuration = static_cast(EssenceDescriptor.ContainerDuration.const_get()); + PDesc.StoredWidth = EssenceDescriptor.StoredWidth; + PDesc.StoredHeight = EssenceDescriptor.StoredHeight; + PDesc.AspectRatio = EssenceDescriptor.AspectRatio; + + PDesc.Ppih = EssenceSubDescriptor.JPEGXSPpih; + PDesc.Plev = EssenceSubDescriptor.JPEGXSPlev; + PDesc.Wf = EssenceSubDescriptor.JPEGXSWf; + PDesc.Hf = EssenceSubDescriptor.JPEGXSHf; + PDesc.Nc = EssenceSubDescriptor.JPEGXSNc; + + if (EssenceDescriptor.ColorPrimaries.empty()) { + PDesc.Primaries = 1; // If not set, let us assume 709 primaries. Yuck! + } else if (EssenceDescriptor.ColorPrimaries == dict->ul(ASDCP::MDD_ColorPrimaries_ITU709)) { + PDesc.Primaries = 1; + } else if (EssenceDescriptor.ColorPrimaries == dict->ul(ASDCP::MDD_ColorPrimaries_ITU470_PAL)) { + PDesc.Primaries = 5; + } else if (EssenceDescriptor.ColorPrimaries == dict->ul(ASDCP::MDD_ColorPrimaries_SMPTE170M)) { + PDesc.Primaries = 6; + } else if (EssenceDescriptor.ColorPrimaries == dict->ul(ASDCP::MDD_ColorPrimaries_ITU2020)) { + PDesc.Primaries = 9; + } else if (EssenceDescriptor.ColorPrimaries == dict->ul(ASDCP::MDD_ColorPrimaries_SMPTE_DCDM)) { + PDesc.Primaries = 10; + } else if (EssenceDescriptor.ColorPrimaries == dict->ul(ASDCP::MDD_TheatricalViewingEnvironment)) { + PDesc.Primaries = 11; + } else if (EssenceDescriptor.ColorPrimaries == dict->ul(ASDCP::MDD_ColorPrimaries_P3D65)) { + PDesc.Primaries = 12; + } else { + PDesc.Primaries = 0; + } + + if (EssenceDescriptor.TransferCharacteristic.empty()) { + PDesc.TransferCurve = 2; // Unspecified + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_ITU709)) { + PDesc.TransferCurve = 1; + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_linear)) { + PDesc.TransferCurve = 8; + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_IEC6196624_xvYCC)) { + PDesc.TransferCurve = 11; + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_sRGB)) { + PDesc.TransferCurve = 13; + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_SMPTEST2084)) { + PDesc.TransferCurve = 16; + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_ST428)) { + PDesc.TransferCurve = 17; + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_HLG)) { + PDesc.TransferCurve = 18; + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_BT1361)) { + PDesc.TransferCurve = 12; + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_BT470)) { + PDesc.TransferCurve = 4; + } else if (EssenceDescriptor.TransferCharacteristic == dict->ul(ASDCP::MDD_TransferCharacteristic_ST240M)) { + PDesc.TransferCurve = 7; + } else { + PDesc.TransferCurve = 0; + } + + if (EssenceDescriptor.CodingEquations.empty()) { + PDesc.Matrix = 2; + } else if (EssenceDescriptor.CodingEquations == dict->ul(ASDCP::MDD_CodingEquations_BGR)) { + PDesc.Matrix = 0; + } else if (EssenceDescriptor.CodingEquations == dict->ul(ASDCP::MDD_CodingEquations_709)) { + PDesc.Matrix = 1; + } else if (EssenceDescriptor.CodingEquations == dict->ul(ASDCP::MDD_CodingEquations_601)) { + PDesc.Matrix = 5; + } else if (EssenceDescriptor.CodingEquations == dict->ul(ASDCP::MDD_CodingEquations_Rec2020)) { + PDesc.Matrix = 9; + } else if (EssenceDescriptor.CodingEquations == dict->ul(ASDCP::MDD_CodingEquations_ST240M)) { + PDesc.Matrix = 7; + } else if (EssenceDescriptor.CodingEquations == dict->ul(ASDCP::MDD_CodingEquations_YCGCO)) { + PDesc.Matrix = 8; + } else { + PDesc.Matrix = 0; + } + + if (EssenceSubDescriptor.JPEGXSCw.const_get()==0 || EssenceSubDescriptor.JPEGXSCw.const_get() == 0) + PDesc.Cw = 0; + else + PDesc.Cw = static_cast(EssenceSubDescriptor.JPEGXSCw.const_get()); + + PDesc.Hsl = static_cast(EssenceSubDescriptor.JPEGXSHsl.const_get()); + + PDesc.MaximumBitRate = static_cast(EssenceSubDescriptor.JPEGXSMaximumBitRate.const_get()); + + // JPEGXSComponentTable + ui32_t tmp_size = EssenceSubDescriptor.JPEGXSComponentTable.Length(); + + if (tmp_size > 4 && (tmp_size & 1) == 0 && (PDesc.Nc << 1) + 4 == tmp_size) { + const byte_t *data = EssenceSubDescriptor.JPEGXSComponentTable.RoData() + 4; + for(int i = 0;i < PDesc.Nc;i++) { + PDesc.ImageComponents[i].Bc = data[0]; + PDesc.ImageComponents[i].Sy = data[1] >> 4; + PDesc.ImageComponents[i].Sx = data[1] & 0x0f; + data += 2; + } + } else { + return RESULT_FAIL; + } + + if (rgba) { + if (rgba->ComponentMinRef.empty()) { + if (rgba->ComponentMaxRef.empty()) { + PDesc.fullRange = false; + } else if (rgba->ComponentMaxRef == (1UL << PDesc.ImageComponents[0].Bc) - 1) { + PDesc.fullRange = true; + } else { + PDesc.fullRange = false; + } + } else if (rgba->ComponentMinRef == 0) { + PDesc.fullRange = true; + } else { + PDesc.fullRange = false; + } + } else if (cdci) { + if (cdci->BlackRefLevel.empty()) { + if (cdci->WhiteReflevel.empty()) { + PDesc.fullRange = false; + } else if (cdci->WhiteReflevel == (1UL << PDesc.ImageComponents[0].Bc) - 1) { + PDesc.fullRange = true; + } else { + PDesc.fullRange = false; + } + } else if (cdci->BlackRefLevel == 0) { + PDesc.fullRange = true; + } else { + PDesc.fullRange = false; + } + } else { + PDesc.fullRange = false; + } + + return RESULT_OK; +} + + +//------------------------------------------------------------------------------------------ +// +// hidden, internal implementation of JPEG XS reader + + +class ih__Reader : public ASDCP::h__ASDCPReader +{ + RGBAEssenceDescriptor* m_EssenceDescriptor; + JPEGXSPictureSubDescriptor* m_EssenceSubDescriptor; + ASDCP::Rational m_EditRate; + ASDCP::Rational m_SampleRate; + EssenceType_t m_Format; + + ASDCP_NO_COPY_CONSTRUCT(ih__Reader); + +public: + PictureDescriptor m_PDesc; // codestream parameter list + + ih__Reader(const Dictionary *d) : + ASDCP::h__ASDCPReader(d), m_EssenceDescriptor(0), m_EssenceSubDescriptor(0), m_Format(ESS_UNKNOWN) {} + + virtual ~ih__Reader() {} + + Result_t OpenRead(const std::string&, EssenceType_t); + Result_t ReadFrame(ui32_t, JXS::FrameBuffer&, AESDecContext*, HMACContext*); + Result_t CalcFrameBufferSize(ui64_t &size); +}; + + +// +// +ASDCP::Result_t +ih__Reader::OpenRead(const std::string& filename, EssenceType_t type) +{ + Result_t result = OpenMXFRead(filename); + + if( ASDCP_SUCCESS(result) ) + { + InterchangeObject* tmp_iobj = 0; + m_HeaderPart.GetMDObjectByType(OBJ_TYPE_ARGS(RGBAEssenceDescriptor), &tmp_iobj); + m_EssenceDescriptor = static_cast(tmp_iobj); + + if ( m_EssenceDescriptor == 0 ) + { + DefaultLogSink().Error("RGBAEssenceDescriptor object not found.\n"); + return RESULT_FORMAT; + } + + m_HeaderPart.GetMDObjectByType(OBJ_TYPE_ARGS(JPEGXSPictureSubDescriptor), &tmp_iobj); + m_EssenceSubDescriptor = static_cast(tmp_iobj); + + if ( m_EssenceSubDescriptor == 0 ) + { + m_EssenceDescriptor = 0; + DefaultLogSink().Error("JPEGXSPictureSubDescriptor object not found.\n"); + return RESULT_FORMAT; + } + + std::list ObjectList; + m_HeaderPart.GetMDObjectsByType(OBJ_TYPE_ARGS(Track), ObjectList); + + if ( ObjectList.empty() ) + { + DefaultLogSink().Error("MXF Metadata contains no Track Sets.\n"); + return RESULT_FORMAT; + } + + m_EditRate = ((Track*)ObjectList.front())->EditRate; + m_SampleRate = m_EssenceDescriptor->SampleRate; + + if ( type == ASDCP::ESS_JPEG_XS ) + { + if ( m_EditRate != m_SampleRate ) + { + DefaultLogSink().Warn("EditRate and SampleRate do not match (%.03f, %.03f).\n", + m_EditRate.Quotient(), m_SampleRate.Quotient()); + /* + ** thor: currently, I do not see support for stereoskopic content, + ** thus skip this at the time being. + if ( ( m_EditRate == EditRate_24 && m_SampleRate == EditRate_48 ) + || ( m_EditRate == EditRate_25 && m_SampleRate == EditRate_50 ) + || ( m_EditRate == EditRate_30 && m_SampleRate == EditRate_60 ) + || ( m_EditRate == EditRate_48 && m_SampleRate == EditRate_96 ) + || ( m_EditRate == EditRate_50 && m_SampleRate == EditRate_100 ) + || ( m_EditRate == EditRate_60 && m_SampleRate == EditRate_120 ) + || ( m_EditRate == EditRate_96 && m_SampleRate == EditRate_192 ) + || ( m_EditRate == EditRate_100 && m_SampleRate == EditRate_200 ) + || ( m_EditRate == EditRate_120 && m_SampleRate == EditRate_240 ) ) + { + DefaultLogSink().Debug("File may contain JPEG Interop stereoscopic images.\n"); + return RESULT_SFORMAT; + } + ** + */ + + return RESULT_FORMAT; + } + } + /* + ** thor: support for stereoskopic JPEG XS disabled as it may + ** not yet be supported by a standard. + else if ( type == ASDCP::ESS_JPEG_XS_S ) + { + if ( m_EditRate == EditRate_24 ) + { + if ( m_SampleRate != EditRate_48 ) + { + DefaultLogSink().Error("EditRate and SampleRate not correct for 24/48 stereoscopic essence.\n"); + return RESULT_FORMAT; + } + } + else if ( m_EditRate == EditRate_25 ) + { + if ( m_SampleRate != EditRate_50 ) + { + DefaultLogSink().Error("EditRate and SampleRate not correct for 25/50 stereoscopic essence.\n"); + return RESULT_FORMAT; + } + } + else if ( m_EditRate == EditRate_30 ) + { + if ( m_SampleRate != EditRate_60 ) + { + DefaultLogSink().Error("EditRate and SampleRate not correct for 30/60 stereoscopic essence.\n"); + return RESULT_FORMAT; + } + } + else if ( m_EditRate == EditRate_48 ) + { + if ( m_SampleRate != EditRate_96 ) + { + DefaultLogSink().Error("EditRate and SampleRate not correct for 48/96 stereoscopic essence.\n"); + return RESULT_FORMAT; + } + } + else if ( m_EditRate == EditRate_50 ) + { + if ( m_SampleRate != EditRate_100 ) + { + DefaultLogSink().Error("EditRate and SampleRate not correct for 50/100 stereoscopic essence.\n"); + return RESULT_FORMAT; + } + } + else if ( m_EditRate == EditRate_60 ) + { + if ( m_SampleRate != EditRate_120 ) + { + DefaultLogSink().Error("EditRate and SampleRate not correct for 60/120 stereoscopic essence.\n"); + return RESULT_FORMAT; + } + } + else if ( m_EditRate == EditRate_96 ) + { + if ( m_SampleRate != EditRate_192 ) + { + DefaultLogSink().Error("EditRate and SampleRate not correct for 96/192 stereoscopic essence.\n"); + return RESULT_FORMAT; + } + } + else if ( m_EditRate == EditRate_100 ) + { + if ( m_SampleRate != EditRate_200 ) + { + DefaultLogSink().Error("EditRate and SampleRate not correct for 100/200 stereoscopic essence.\n"); + return RESULT_FORMAT; + } + } + else if ( m_EditRate == EditRate_120 ) + { + if ( m_SampleRate != EditRate_240 ) + { + DefaultLogSink().Error("EditRate and SampleRate not correct for 120/240 stereoscopic essence.\n"); + return RESULT_FORMAT; + } + } + else + { + DefaultLogSink().Error("EditRate not correct for stereoscopic essence: %d/%d.\n", + m_EditRate.Numerator, m_EditRate.Denominator); + return RESULT_FORMAT; + } + } + */ + else + { + DefaultLogSink().Error("'type' argument unexpected: %x\n", type); + return RESULT_STATE; + } + + result = MD_to_JXS_PDesc(*m_EssenceDescriptor, *m_EssenceSubDescriptor, m_EditRate, m_SampleRate, m_PDesc); + } + + return result; +} + +// +// +ASDCP::Result_t +ih__Reader::ReadFrame(ui32_t FrameNum, JXS::FrameBuffer& FrameBuf, + AESDecContext* Ctx, HMACContext* HMAC) +{ + if ( ! m_File.IsOpen() ) + return RESULT_INIT; + + assert(m_Dict); + return ReadEKLVFrame(FrameNum, FrameBuf, m_Dict->ul(MDD_JPEGXSEssence), Ctx, HMAC); +} + +// +Result_t ih__Reader::CalcFrameBufferSize(ui64_t &size) +{ + if ( ! m_File.IsOpen() ) + return RESULT_INIT; + + assert(m_Dict); + return ASDCP::h__ASDCPReader::CalcFrameBufferSize(size); +} + +// +class ASDCP::JXS::MXFReader::h__Reader : public ih__Reader +{ + ASDCP_NO_COPY_CONSTRUCT(h__Reader); + h__Reader(); + +public: + h__Reader(const Dictionary *d) : ih__Reader(d) {} +}; + + + +//------------------------------------------------------------------------------------------ + + +// +void +ASDCP::JXS::FrameBuffer::Dump(FILE* stream, ui32_t dump_len) const +{ + if ( stream == 0 ) + stream = stderr; + + fprintf(stream, "Frame: %06u, %7u bytes", m_FrameNumber, m_Size); + + fputc('\n', stream); + + if ( dump_len > 0 ) + Kumu::hexdump(m_Data, dump_len, stream); +} + + +//------------------------------------------------------------------------------------------ + +ASDCP::JXS::MXFReader::MXFReader() +{ + m_Reader = new h__Reader(&DefaultCompositeDict()); +} + + +ASDCP::JXS::MXFReader::~MXFReader() +{ + if ( m_Reader && m_Reader->m_File.IsOpen() ) + m_Reader->Close(); +} + +// Warning: direct manipulation of MXF structures can interfere +// with the normal operation of the wrapper. Caveat emptor! +// +ASDCP::MXF::OP1aHeader& +ASDCP::JXS::MXFReader::OP1aHeader() +{ + if ( m_Reader.empty() ) + { + assert(g_OP1aHeader); + return *g_OP1aHeader; + } + + return m_Reader->m_HeaderPart; +} + +// Warning: direct manipulation of MXF structures can interfere +// with the normal operation of the wrapper. Caveat emptor! +// +ASDCP::MXF::OPAtomIndexFooter& +ASDCP::JXS::MXFReader::OPAtomIndexFooter() +{ + if ( m_Reader.empty() ) + { + assert(g_OPAtomIndexFooter); + return *g_OPAtomIndexFooter; + } + + return m_Reader->m_IndexAccess; +} + +// Warning: direct manipulation of MXF structures can interfere +// with the normal operation of the wrapper. Caveat emptor! +// +ASDCP::MXF::RIP& +ASDCP::JXS::MXFReader::RIP() +{ + if ( m_Reader.empty() ) + { + assert(g_RIP); + return *g_RIP; + } + + return m_Reader->m_RIP; +} + +// Open the file for reading. The file must exist. Returns error if the +// operation cannot be completed. +ASDCP::Result_t +ASDCP::JXS::MXFReader::OpenRead(const std::string& filename) const +{ + return m_Reader->OpenRead(filename, ASDCP::ESS_JPEG_XS); +} + +// +ASDCP::Result_t +ASDCP::JXS::MXFReader::ReadFrame(ui32_t FrameNum, FrameBuffer& FrameBuf, + AESDecContext* Ctx, HMACContext* HMAC) const +{ + if ( m_Reader && m_Reader->m_File.IsOpen() ) + return m_Reader->ReadFrame(FrameNum, FrameBuf, Ctx, HMAC); + + return RESULT_INIT; +} + +// +ASDCP::Result_t +ASDCP::JXS::MXFReader::CalcFrameBufferSize(ui64_t &size) const +{ + if ( m_Reader && m_Reader->m_File.IsOpen() ) + return m_Reader->CalcFrameBufferSize(size); + + return RESULT_INIT; +} + +ASDCP::Result_t +ASDCP::JXS::MXFReader::LocateFrame(ui32_t FrameNum, Kumu::fpos_t& streamOffset, i8_t& temporalOffset, i8_t& keyFrameOffset) const +{ + return m_Reader->LocateFrame(FrameNum, streamOffset, temporalOffset, keyFrameOffset); +} + + +// Fill the struct with the values from the file's header. +// Returns RESULT_INIT if the file is not open. +ASDCP::Result_t +ASDCP::JXS::MXFReader::FillPictureDescriptor(PictureDescriptor& PDesc) const +{ + if ( m_Reader && m_Reader->m_File.IsOpen() ) + { + PDesc = m_Reader->m_PDesc; + return RESULT_OK; + } + + return RESULT_INIT; +} + + +// Fill the struct with the values from the file's header. +// Returns RESULT_INIT if the file is not open. +ASDCP::Result_t +ASDCP::JXS::MXFReader::FillWriterInfo(WriterInfo& Info) const +{ + if ( m_Reader && m_Reader->m_File.IsOpen() ) + { + Info = m_Reader->m_Info; + return RESULT_OK; + } + + return RESULT_INIT; +} + +// +void +ASDCP::JXS::MXFReader::DumpHeaderMetadata(FILE* stream) const +{ + if ( m_Reader->m_File.IsOpen() ) + m_Reader->m_HeaderPart.Dump(stream); +} + + +// +void +ASDCP::JXS::MXFReader::DumpIndex(FILE* stream) const +{ + if ( m_Reader->m_File.IsOpen() ) + m_Reader->m_IndexAccess.Dump(stream); +} + +// +ASDCP::Result_t +ASDCP::JXS::MXFReader::Close() const +{ + if ( m_Reader && m_Reader->m_File.IsOpen() ) + { + m_Reader->Close(); + return RESULT_OK; + } + + return RESULT_INIT; +} + + +//------------------------------------------------------------------------------------------ + +// +class ih__Writer : public ASDCP::h__ASDCPWriter +{ + ASDCP_NO_COPY_CONSTRUCT(ih__Writer); + ih__Writer(); + + JPEGXSPictureSubDescriptor* m_EssenceSubDescriptor; + +public: + PictureDescriptor m_PDesc; + byte_t m_EssenceUL[SMPTE_UL_LENGTH]; + + ih__Writer(const Dictionary *d) : ASDCP::h__ASDCPWriter(d), m_EssenceSubDescriptor(0) { + memset(m_EssenceUL, 0, SMPTE_UL_LENGTH); + } + + virtual ~ih__Writer(){} + + Result_t OpenWrite(const std::string&, EssenceType_t type, ui32_t HeaderSize); + Result_t SetSourceStream(const PictureDescriptor&, const std::string& label, + ASDCP::Rational LocalEditRate = ASDCP::Rational(0,0)); + Result_t WriteFrame(const JXS::FrameBuffer&, bool add_index, AESEncContext*, HMACContext*); + Result_t Finalize(); +}; + +// Open the file for writing. The file must not exist. Returns error if +// the operation cannot be completed. +ASDCP::Result_t +ih__Writer::OpenWrite(const std::string& filename, EssenceType_t type, ui32_t HeaderSize) +{ + if ( ! m_State.Test_BEGIN() ) + return RESULT_STATE; + + Result_t result = m_File.OpenWrite(filename); + + if ( ASDCP_SUCCESS(result) ) + { + m_HeaderSize = HeaderSize; + RGBAEssenceDescriptor* tmp_rgba = new RGBAEssenceDescriptor(m_Dict); + tmp_rgba->ComponentMaxRef = 4095; + tmp_rgba->ComponentMinRef = 0; + + m_EssenceDescriptor = tmp_rgba; + m_EssenceSubDescriptor = new JPEGXSPictureSubDescriptor(m_Dict); + m_EssenceSubDescriptorList.push_back((InterchangeObject*)m_EssenceSubDescriptor); + + GenRandomValue(m_EssenceSubDescriptor->InstanceUID); + m_EssenceDescriptor->SubDescriptors.push_back(m_EssenceSubDescriptor->InstanceUID); + + /* + ** thor: stereoskopic images are currently disabled. + if ( type == ASDCP::ESS_JPEG_XS_S && m_Info.LabelSetType == LS_MXF_SMPTE ) + { + InterchangeObject* StereoSubDesc = new StereoscopicPictureSubDescriptor(m_Dict); + m_EssenceSubDescriptorList.push_back(StereoSubDesc); + GenRandomValue(StereoSubDesc->InstanceUID); + m_EssenceDescriptor->SubDescriptors.push_back(StereoSubDesc->InstanceUID); + } + ** + */ + + result = m_State.Goto_INIT(); + } + + return result; +} + +// Automatically sets the MXF file's metadata from the first jpeg codestream stream. +ASDCP::Result_t +ih__Writer::SetSourceStream(const PictureDescriptor& PDesc, const std::string& label, ASDCP::Rational LocalEditRate) +{ + assert(m_Dict); + if ( ! m_State.Test_INIT() ) + return RESULT_STATE; + + if ( LocalEditRate == ASDCP::Rational(0,0) ) + LocalEditRate = PDesc.EditRate; + + m_PDesc = PDesc; + assert(m_Dict); + assert(m_EssenceDescriptor); + assert(m_EssenceSubDescriptor); + Result_t result = JXS_PDesc_to_MD(m_PDesc, *m_Dict, + *static_cast(m_EssenceDescriptor), + *m_EssenceSubDescriptor); + + if ( ASDCP_SUCCESS(result) ) + { + ASDCP::MXF::GenericPictureEssenceDescriptor *gpe = static_cast(m_EssenceDescriptor); + switch(PDesc.Ppih) { + case 0: // Profile_Unrestricted + gpe->PictureEssenceCoding.Set(m_Dict->ul(MDD_JPEGXSUnrestrictedCodestream)); + break; + case 0x1500: // Profile_Light422 + gpe->PictureEssenceCoding.Set(m_Dict->ul(MDD_JPEGXSLight422_10Profile)); + break; + case 0x1a00: // Profile_Light444 + gpe->PictureEssenceCoding.Set(m_Dict->ul(MDD_JPEGXSLight444_12Profile)); + break; + case 0x2500: // Profile_LightSubline + gpe->PictureEssenceCoding.Set(m_Dict->ul(MDD_JPEGXSLightSubline422_10Profile)); + break; + case 0x3540: // Profile_Main422 + gpe->PictureEssenceCoding.Set(m_Dict->ul(MDD_JPEGXSMain422_10Profile)); + break; + case 0x3a40: // Profile_Main444 + gpe->PictureEssenceCoding.Set(m_Dict->ul(MDD_JPEGXSMain444_12Profile)); + break; + case 0x3e40: // Profile_Main4444 + gpe->PictureEssenceCoding.Set(m_Dict->ul(MDD_JPEGXSMain4444_12Profile)); + break; + case 0x4a40: // Profile_High444 + gpe->PictureEssenceCoding.Set(m_Dict->ul(MDD_JPEGXSHigh444_12Profile)); + break; + case 0x4e40: // Profile_High4444 + gpe->PictureEssenceCoding.Set(m_Dict->ul(MDD_JPEGXSHigh4444_12Profile)); + break; + default: + return RESULT_PARAM; + } + memcpy(m_EssenceUL, m_Dict->ul(MDD_JPEGXSEssence), SMPTE_UL_LENGTH); + m_EssenceUL[SMPTE_UL_LENGTH-1] = 1; // first (and only) essence container + result = m_State.Goto_READY(); + } + + if ( ASDCP_SUCCESS(result) ) + { + result = WriteASDCPHeader(label, UL(m_Dict->ul(MDD_MXFGCFUFrameWrappedPictureElement)), + PICT_DEF_LABEL, UL(m_EssenceUL), UL(m_Dict->ul(MDD_PictureDataDef)), + LocalEditRate, derive_timecode_rate_from_edit_rate(m_PDesc.EditRate)); + } + + return result; +} + +// Writes a frame of essence to the MXF file. If the optional AESEncContext +// argument is present, the essence is encrypted prior to writing. +// Fails if the file is not open, is finalized, or an operating system +// error occurs. +// +ASDCP::Result_t +ih__Writer::WriteFrame(const JXS::FrameBuffer& FrameBuf, bool add_index, + AESEncContext* Ctx, HMACContext* HMAC) +{ + Result_t result = RESULT_OK; + + if ( m_State.Test_READY() ) + result = m_State.Goto_RUNNING(); // first time through + + ui64_t StreamOffset = m_StreamOffset; + + if ( ASDCP_SUCCESS(result) ) + result = WriteEKLVPacket(FrameBuf, m_EssenceUL, MXF_BER_LENGTH, Ctx, HMAC); + + if ( ASDCP_SUCCESS(result) && add_index ) + { + IndexTableSegment::IndexEntry Entry; + Entry.StreamOffset = StreamOffset; + m_FooterPart.PushIndexEntry(Entry); + } + + m_FramesWritten++; + return result; +} + + +// Closes the MXF file, writing the index and other closing information. +// +ASDCP::Result_t +ih__Writer::Finalize() +{ + if ( ! m_State.Test_RUNNING() ) + return RESULT_STATE; + + m_State.Goto_FINAL(); + + return WriteASDCPFooter(); +} + + +// +class ASDCP::JXS::MXFWriter::h__Writer : public ih__Writer +{ + ASDCP_NO_COPY_CONSTRUCT(h__Writer); + h__Writer(); + +public: + h__Writer(const Dictionary *d) : ih__Writer(d) {} +}; + + +//------------------------------------------------------------------------------------------ + + + +ASDCP::JXS::MXFWriter::MXFWriter() +{ +} + +ASDCP::JXS::MXFWriter::~MXFWriter() +{ +} + +// Warning: direct manipulation of MXF structures can interfere +// with the normal operation of the wrapper. Caveat emptor! +// +ASDCP::MXF::OP1aHeader& +ASDCP::JXS::MXFWriter::OP1aHeader() +{ + if ( m_Writer.empty() ) + { + assert(g_OP1aHeader); + return *g_OP1aHeader; + } + + return m_Writer->m_HeaderPart; +} + +// Warning: direct manipulation of MXF structures can interfere +// with the normal operation of the wrapper. Caveat emptor! +// +ASDCP::MXF::OPAtomIndexFooter& +ASDCP::JXS::MXFWriter::OPAtomIndexFooter() +{ + if ( m_Writer.empty() ) + { + assert(g_OPAtomIndexFooter); + return *g_OPAtomIndexFooter; + } + + return m_Writer->m_FooterPart; +} + +// Warning: direct manipulation of MXF structures can interfere +// with the normal operation of the wrapper. Caveat emptor! +// +ASDCP::MXF::RIP& +ASDCP::JXS::MXFWriter::RIP() +{ + if ( m_Writer.empty() ) + { + assert(g_RIP); + return *g_RIP; + } + + return m_Writer->m_RIP; +} + +// Open the file for writing. The file must not exist. Returns error if +// the operation cannot be completed. +ASDCP::Result_t +ASDCP::JXS::MXFWriter::OpenWrite(const std::string& filename, const WriterInfo& Info, + const PictureDescriptor& PDesc, ui32_t HeaderSize) +{ + if ( Info.LabelSetType == LS_MXF_SMPTE ) + m_Writer = new h__Writer(&DefaultSMPTEDict()); + else + m_Writer = new h__Writer(&DefaultInteropDict()); + + m_Writer->m_Info = Info; + + Result_t result = m_Writer->OpenWrite(filename, ASDCP::ESS_JPEG_XS, HeaderSize); + + if ( ASDCP_SUCCESS(result) ) + result = m_Writer->SetSourceStream(PDesc, JXS_PACKAGE_LABEL); + + if ( ASDCP_FAILURE(result) ) + m_Writer.release(); + + return result; +} + + +// Writes a frame of essence to the MXF file. If the optional AESEncContext +// argument is present, the essence is encrypted prior to writing. +// Fails if the file is not open, is finalized, or an operating system +// error occurs. +ASDCP::Result_t +ASDCP::JXS::MXFWriter::WriteFrame(const FrameBuffer& FrameBuf, AESEncContext* Ctx, HMACContext* HMAC) +{ + if ( m_Writer.empty() ) + return RESULT_INIT; + + return m_Writer->WriteFrame(FrameBuf, true, Ctx, HMAC); +} + +// Closes the MXF file, writing the index and other closing information. +ASDCP::Result_t +ASDCP::JXS::MXFWriter::Finalize() +{ + if ( m_Writer.empty() ) + return RESULT_INIT; + + return m_Writer->Finalize(); +} + +// +// end AS_DCP_JXS.cpp +// -- cgit v1.2.3 From 483cc7f388df0353b970f80ac8774667815a8b16 Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Wed, 26 May 2021 12:51:06 +0200 Subject: Added JXS specific header files to separate JXS functionality out. Updated autoconf to switch JXS support for ADCP on or off. --- configure.ac | 8 + src/AS_02.h | 85 ----------- src/AS_02_JXS.cpp | 3 +- src/AS_02_JXS.h | 145 ++++++++++++++++++ src/AS_02_internal.h | 1 + src/AS_DCP.h | 223 ---------------------------- src/AS_DCP_JXS.cpp | 1 + src/AS_DCP_JXS.h | 333 ++++++++++++++++++++++++++++++++++++++++++ src/AS_DCP_internal.h | 9 -- src/CMakeLists.txt | 8 +- src/JXS_Codestream_Parser.cpp | 1 + src/JXS_Sequence_Parser.cpp | 3 +- src/Makefile.am | 14 +- 13 files changed, 507 insertions(+), 327 deletions(-) create mode 100644 src/AS_02_JXS.h create mode 100644 src/AS_DCP_JXS.h (limited to 'src/AS_DCP_JXS.cpp') diff --git a/configure.ac b/configure.ac index af3edf2..01c213c 100644 --- a/configure.ac +++ b/configure.ac @@ -102,6 +102,14 @@ AC_ARG_ENABLE([as-02], *) AC_MSG_ERROR([bad value ${enableval} for --enable-as-02]) ;; esac],[as_02_use=false]) AM_CONDITIONAL([USE_AS_02], [test x$as_02_use = xtrue]) +AC_ARG_ENABLE([asdcp-jxs], + [ --enable-asdcp-jxs enable support wrapping JPEG XS files], + [case "${enableval}" in + yes) asdcp_jxs_use=true ;; + no) asdcp_jxs_use=false ;; + *) AC_MSG_ERROR([bad value ${enableval} for --enable-asdcp-jxs]) ;; + esac],[asdcp_jxs_use=false]) + AM_CONDITIONAL([USE_ASDCP_JXS], [test x$asdcp_jxs_use = xtrue]) AC_ARG_ENABLE([phdr], [ --enable-phdr enable support for Prototype for High Dyamic Range in AS-02], [case "${enableval}" in diff --git a/src/AS_02.h b/src/AS_02.h index 55d7e8b..2fc7f5a 100644 --- a/src/AS_02.h +++ b/src/AS_02.h @@ -231,91 +231,6 @@ namespace AS_02 }; } //namespace JP2K - - namespace JXS - { - // - class MXFWriter - { - class h__Writer; - ASDCP::mem_ptr m_Writer; - ASDCP_NO_COPY_CONSTRUCT(MXFWriter); - - public: - MXFWriter(); - virtual ~MXFWriter(); - - // Warning: direct manipulation of MXF structures can interfere - // with the normal operation of the wrapper. Caveat emptor! - virtual ASDCP::MXF::OP1aHeader& OP1aHeader(); - virtual ASDCP::MXF::RIP& RIP(); - - // Open the file for writing. The file must not exist. Returns error if - // the operation cannot be completed or if nonsensical data is discovered - // in the essence descriptor. - Result_t OpenWrite(const std::string& filename, const ASDCP::WriterInfo&, - ASDCP::MXF::FileDescriptor* essence_descriptor, - ASDCP::MXF::InterchangeObject_list_t& essence_sub_descriptor_list, - const ASDCP::Rational& edit_rate, const ui32_t& header_size = 16384, - const IndexStrategy_t& strategy = IS_FOLLOW, const ui32_t& partition_space = 10); - - // Writes a frame of essence to the MXF file. If the optional AESEncContext - // argument is present, the essence is encrypted prior to writing. - // Fails if the file is not open, is finalized, or an operating system - // error occurs. - Result_t WriteFrame(const ASDCP::JXS::FrameBuffer&, ASDCP::AESEncContext* = 0, ASDCP::HMACContext* = 0); - - // Closes the MXF file, writing the index and revised header. - Result_t Finalize(); - }; - - // - class MXFReader - { - class h__Reader; - ASDCP::mem_ptr m_Reader; - ASDCP_NO_COPY_CONSTRUCT(MXFReader); - - public: - MXFReader(); - virtual ~MXFReader(); - - // Warning: direct manipulation of MXF structures can interfere - // with the normal operation of the wrapper. Caveat emptor! - virtual ASDCP::MXF::OP1aHeader& OP1aHeader(); - virtual AS_02::MXF::AS02IndexReader& AS02IndexReader(); - virtual ASDCP::MXF::RIP& RIP(); - - // Open the file for reading. The file must exist. Returns error if the - // operation cannot be completed. - Result_t OpenRead(const std::string& filename) const; - - // Returns RESULT_INIT if the file is not open. - Result_t Close() const; - - // Fill a WriterInfo struct with the values from the file's header. - // Returns RESULT_INIT if the file is not open. - Result_t FillWriterInfo(ASDCP::WriterInfo&) const; - - // Compute the necessary frame buffer size in bytes for CBR - // codestreamss. - Result_t CalcFrameBufferSize(ui64_t &size) const; - - // Reads a frame of essence from the MXF file. If the optional AESEncContext - // argument is present, the essence is decrypted after reading. If the MXF - // file is encrypted and the AESDecContext argument is NULL, the frame buffer - // will contain the ciphertext frame data. If the HMACContext argument is - // not NULL, the HMAC will be calculated (if the file supports it). - // Returns RESULT_INIT if the file is not open, failure if the frame number is - // out of range, or if optional decrypt or HAMC operations fail. - Result_t ReadFrame(ui32_t frame_number, ASDCP::JXS::FrameBuffer&, ASDCP::AESDecContext* = 0, ASDCP::HMACContext* = 0) const; - - // Print debugging information to stream - void DumpHeaderMetadata(FILE* = 0) const; - void DumpIndex(FILE* = 0) const; - }; - - } //namespace JXS //--------------------------------------------------------------------------------- // diff --git a/src/AS_02_JXS.cpp b/src/AS_02_JXS.cpp index b862f26..eb43b32 100644 --- a/src/AS_02_JXS.cpp +++ b/src/AS_02_JXS.cpp @@ -1,7 +1,7 @@ /* Copyright (c) 2011-2018, Robert Scheler, Heiko Sparenberg Fraunhofer IIS, +Copyright (c) 2020-2021, Thomas Richter, Fraunhofer IIS John Hurst -Copyright (c) 2020, Thomas Richter, Fraunhofer IIS All rights reserved. @@ -33,6 +33,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "AS_02_internal.h" +#include "AS_DCP_JXS.h" #include #include diff --git a/src/AS_02_JXS.h b/src/AS_02_JXS.h new file mode 100644 index 0000000..1fef4f1 --- /dev/null +++ b/src/AS_02_JXS.h @@ -0,0 +1,145 @@ +/* +Copyright (c) 2011-2018, Robert Scheler, Heiko Sparenberg Fraunhofer IIS, +Copyright (c) 2020-2021, Thomas Richter Fraunhofer IIS, +John Hurst + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/*! \file AS_02.h + \version $Id$ + \brief AS-02 library, public interface + +This module implements the JPEG XS specific wrapping of IMF + + o Write essence to a plaintext or ciphertext AS-02 file: + JPEG XS codestreams + + o Read essence from a plaintext or ciphertext AS-02 file: + JPEG XS codestreams + + o Read header metadata from an AS-02 file + +NOTE: ciphertext support for clip-wrapped PCM is not yet complete. +*/ + +#ifndef _AS_02_JXS_H_ +#define _AS_02_JXS_H_ + +#include "Metadata.h" +#include "AS_02.h" +#include "AS_DCP_JXS.h" + +namespace AS_02 +{ + using Kumu::Result_t; + + namespace JXS + { + // + class MXFWriter + { + class h__Writer; + ASDCP::mem_ptr m_Writer; + ASDCP_NO_COPY_CONSTRUCT(MXFWriter); + + public: + MXFWriter(); + virtual ~MXFWriter(); + + // Warning: direct manipulation of MXF structures can interfere + // with the normal operation of the wrapper. Caveat emptor! + virtual ASDCP::MXF::OP1aHeader& OP1aHeader(); + virtual ASDCP::MXF::RIP& RIP(); + + // Open the file for writing. The file must not exist. Returns error if + // the operation cannot be completed or if nonsensical data is discovered + // in the essence descriptor. + Result_t OpenWrite(const std::string& filename, const ASDCP::WriterInfo&, + ASDCP::MXF::FileDescriptor* essence_descriptor, + ASDCP::MXF::InterchangeObject_list_t& essence_sub_descriptor_list, + const ASDCP::Rational& edit_rate, const ui32_t& header_size = 16384, + const IndexStrategy_t& strategy = IS_FOLLOW, const ui32_t& partition_space = 10); + + // Writes a frame of essence to the MXF file. If the optional AESEncContext + // argument is present, the essence is encrypted prior to writing. + // Fails if the file is not open, is finalized, or an operating system + // error occurs. + Result_t WriteFrame(const ASDCP::JXS::FrameBuffer&, ASDCP::AESEncContext* = 0, ASDCP::HMACContext* = 0); + + // Closes the MXF file, writing the index and revised header. + Result_t Finalize(); + }; + + // + class MXFReader + { + class h__Reader; + ASDCP::mem_ptr m_Reader; + ASDCP_NO_COPY_CONSTRUCT(MXFReader); + + public: + MXFReader(); + virtual ~MXFReader(); + + // Warning: direct manipulation of MXF structures can interfere + // with the normal operation of the wrapper. Caveat emptor! + virtual ASDCP::MXF::OP1aHeader& OP1aHeader(); + virtual AS_02::MXF::AS02IndexReader& AS02IndexReader(); + virtual ASDCP::MXF::RIP& RIP(); + + // Open the file for reading. The file must exist. Returns error if the + // operation cannot be completed. + Result_t OpenRead(const std::string& filename) const; + + // Returns RESULT_INIT if the file is not open. + Result_t Close() const; + + // Fill a WriterInfo struct with the values from the file's header. + // Returns RESULT_INIT if the file is not open. + Result_t FillWriterInfo(ASDCP::WriterInfo&) const; + + // Compute the necessary frame buffer size in bytes for CBR + // codestreamss. + Result_t CalcFrameBufferSize(ui64_t &size) const; + + // Reads a frame of essence from the MXF file. If the optional AESEncContext + // argument is present, the essence is decrypted after reading. If the MXF + // file is encrypted and the AESDecContext argument is NULL, the frame buffer + // will contain the ciphertext frame data. If the HMACContext argument is + // not NULL, the HMAC will be calculated (if the file supports it). + // Returns RESULT_INIT if the file is not open, failure if the frame number is + // out of range, or if optional decrypt or HAMC operations fail. + Result_t ReadFrame(ui32_t frame_number, ASDCP::JXS::FrameBuffer&, ASDCP::AESDecContext* = 0, ASDCP::HMACContext* = 0) const; + + // Print debugging information to stream + void DumpHeaderMetadata(FILE* = 0) const; + void DumpIndex(FILE* = 0) const; + }; + + } //namespace JXS +} + +/// +#endif diff --git a/src/AS_02_internal.h b/src/AS_02_internal.h index 6236ee6..ba7c33d 100644 --- a/src/AS_02_internal.h +++ b/src/AS_02_internal.h @@ -37,6 +37,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "KM_log.h" #include "AS_DCP_internal.h" #include "AS_02.h" +#include "AS_02_JXS.h" using Kumu::DefaultLogSink; diff --git a/src/AS_DCP.h b/src/AS_DCP.h index c16ea57..9ca8679 100755 --- a/src/AS_DCP.h +++ b/src/AS_DCP.h @@ -1454,229 +1454,6 @@ namespace ASDCP { }; } // namespace JP2K - //--------------------------------------------------------------------------------- - // - namespace JXS - { - const ui32_t MaxComponents = 4; // ISO 21122-1 Annex A.2 up to 8 components - const ui32_t MaxHorizontalLevels = 15; - const ui32_t MaxVerticalLevels = 2; - -#pragma pack(1) - struct ImageComponent_t // Essentially, a lookalike of the CDT marker, just with less bit-packing - { - ui8_t Bc; // Bitdepth (literal, not -1) - ui8_t Sx; - ui8_t Sy; // Subsampling factors, horizontal and vertically. Bit-packed in the marker. - }; -#pragma pack() - - struct PictureDescriptor - { - Rational EditRate; - ui32_t ContainerDuration; - Rational SampleRate; - ui32_t StoredWidth; - ui32_t StoredHeight; - Rational AspectRatio; - ui16_t Ppih; // Profile, copy from the PIH marker - ui16_t Plev; // Level and sublevel, copy from the PIH marker - ui16_t Wf; // Frame width, copy from the PIH marker - ui16_t Hf; // Frame height, copy from the PIH marker - ui16_t Hsl; // slice height, copy from the PIH marker - ui16_t Cw; // column width, or 0 for no columns, copy from the PIH marker - ui8_t Nc; // number of components, copy from the PIH marker - ui32_t MaximumBitRate; // bit rate in MB/sec, or 0 if not known - ui8_t Primaries; // Color primaries as defined by CICP - ui8_t TransferCurve; // Transfer curve as defined by CICP - ui8_t Matrix; // Transform matrix, as defined by CICP - bool fullRange; // If true, no head and toe region - ImageComponent_t ImageComponents[MaxComponents]; // These are copies from the CDT (component table) - }; - // Print debugging information to std::ostream - std::ostream& operator << (std::ostream& strm, const PictureDescriptor& pdesc); - // Print debugging information to stream (stderr default) - void PictureDescriptorDump(const PictureDescriptor&, FILE* = 0); - - // - class FrameBuffer : public ASDCP::FrameBuffer - { - public: - FrameBuffer() {} - FrameBuffer(ui32_t size) { Capacity(size); } - virtual ~FrameBuffer() {} - - // Print debugging information to stream (stderr default) - void Dump(FILE* = 0, ui32_t dump_bytes = 0) const; - }; - - - // An object which opens and reads a JPEG XS codestream file. The file is expected - // to contain exactly one complete frame of picture essence as an unwrapped (raw) - // ISO/IEC 21122 codestream. - class CodestreamParser - { - class h__CodestreamParser; - mem_ptr m_Parser; - ASDCP_NO_COPY_CONSTRUCT(CodestreamParser); - - public: - CodestreamParser(); - virtual ~CodestreamParser(); - - // Opens a file for reading, parses enough data to provide a complete - // set of stream metadata for the MXFWriter below. - // The frame buffer's PlaintextOffset parameter will be set to the first - // byte of the data segment. Set this value to zero if you want - // encrypted headers. - Result_t OpenReadFrame(const std::string& filename, FrameBuffer&) const; - - // Fill a PictureDescriptor struct with the values from the file's codestream. - // Returns RESULT_INIT if the file is not open. - Result_t FillPictureDescriptor(PictureDescriptor&) const; - }; - - // Parses the data in the frame buffer to fill in the picture descriptor. Copies - // the offset of the image data into start_of_data. Returns error if the parser fails. - Result_t ParseMetadataIntoDesc(const FrameBuffer&, PictureDescriptor&, byte_t* start_of_data = 0); - - // An object which reads a sequence of files containing JPEG XS pictures. - class SequenceParser - { - class h__SequenceParser; - mem_ptr m_Parser; - ASDCP_NO_COPY_CONSTRUCT(SequenceParser); - - public: - SequenceParser(); - virtual ~SequenceParser(); - - // Opens a directory for reading. The directory is expected to contain one or - // more files, each containing the codestream for exactly one picture. The - // files must be named such that the frames are in temporal order when sorted - // alphabetically by filename. The parser will automatically parse enough data - // from the first file to provide a complete set of stream metadata for the - // MXFWriter below. If the "pedantic" parameter is given and is true, the - // parser will check the metadata for each codestream and fail if a - // mismatch is detected. - Result_t OpenRead(const std::string& filename) const; - - // Opens a file sequence for reading. The sequence is expected to contain one or - // more filenames, each naming a file containing the codestream for exactly one - // picture. The parser will automatically parse enough data - // from the first file to provide a complete set of stream metadata for the - // MXFWriter below. If the "pedantic" parameter is given and is true, the - // parser will check the metadata for each codestream and fail if a - // mismatch is detected. - Result_t OpenRead(const std::list& file_list) const; - - // Fill a PictureDescriptor struct with the values from the first file's codestream. - // Returns RESULT_INIT if the directory is not open. - Result_t FillPictureDescriptor(PictureDescriptor&) const; - - // Rewind the directory to the beginning. - Result_t Reset() const; - - // Reads the next sequential frame in the directory and places it in the - // frame buffer. Fails if the buffer is too small or the direcdtory - // contains no more files. - // The frame buffer's PlaintextOffset parameter will be set to the first - // byte of the data segment. Set this value to zero if you want - // encrypted headers. - Result_t ReadFrame(FrameBuffer&) const; - }; - - - // - class MXFWriter - { - class h__Writer; - mem_ptr m_Writer; - ASDCP_NO_COPY_CONSTRUCT(MXFWriter); - - public: - MXFWriter(); - virtual ~MXFWriter(); - - // Warning: direct manipulation of MXF structures can interfere - // with the normal operation of the wrapper. Caveat emptor! - virtual MXF::OP1aHeader& OP1aHeader(); - virtual MXF::OPAtomIndexFooter& OPAtomIndexFooter(); - virtual MXF::RIP& RIP(); - - // Open the file for writing. The file must not exist. Returns error if - // the operation cannot be completed or if nonsensical data is discovered - // in the essence descriptor. - Result_t OpenWrite(const std::string& filename, const WriterInfo&, - const PictureDescriptor&, ui32_t HeaderSize = 16384); - - // Writes a frame of essence to the MXF file. If the optional AESEncContext - // argument is present, the essence is encrypted prior to writing. - // Fails if the file is not open, is finalized, or an operating system - // error occurs. - Result_t WriteFrame(const FrameBuffer&, AESEncContext* = 0, HMACContext* = 0); - - // Closes the MXF file, writing the index and revised header. - Result_t Finalize(); - }; - - // - class MXFReader - { - class h__Reader; - mem_ptr m_Reader; - ASDCP_NO_COPY_CONSTRUCT(MXFReader); - - public: - MXFReader(); - virtual ~MXFReader(); - - // Warning: direct manipulation of MXF structures can interfere - // with the normal operation of the wrapper. Caveat emptor! - virtual MXF::OP1aHeader& OP1aHeader(); - virtual MXF::OPAtomIndexFooter& OPAtomIndexFooter(); - virtual MXF::RIP& RIP(); - - // Open the file for reading. The file must exist. Returns error if the - // operation cannot be completed. - Result_t OpenRead(const std::string& filename) const; - - // Returns RESULT_INIT if the file is not open. - Result_t Close() const; - - // Fill an AudioDescriptor struct with the values from the file's header. - // Returns RESULT_INIT if the file is not open. - Result_t FillPictureDescriptor(PictureDescriptor&) const; - - // Fill a WriterInfo struct with the values from the file's header. - // Returns RESULT_INIT if the file is not open. - Result_t FillWriterInfo(WriterInfo&) const; - - // Compute the necessary frame buffer size in bytes for CBR - // codestreamss. - Result_t CalcFrameBufferSize(ui64_t &size) const; - - // Reads a frame of essence from the MXF file. If the optional AESEncContext - // argument is present, the essence is decrypted after reading. If the MXF - // file is encrypted and the AESDecContext argument is NULL, the frame buffer - // will contain the ciphertext frame data. If the HMACContext argument is - // not NULL, the HMAC will be calculated (if the file supports it). - // Returns RESULT_INIT if the file is not open, failure if the frame number is - // out of range, or if optional decrypt or HAMC operations fail. - Result_t ReadFrame(ui32_t frame_number, FrameBuffer&, AESDecContext* = 0, HMACContext* = 0) const; - - // Using the index table read from the footer partition, lookup the frame number - // and return the offset into the file at which to read that frame of essence. - // Returns RESULT_INIT if the file is not open, and RESULT_FRAME if the frame number is - // out of range. - Result_t LocateFrame(ui32_t FrameNum, Kumu::fpos_t& streamOffset, i8_t& temporalOffset, i8_t& keyFrameOffset) const; - - // Print debugging information to stream - void DumpHeaderMetadata(FILE* = 0) const; - void DumpIndex(FILE* = 0) const; - }; - } - //--------------------------------------------------------------------------------- // namespace TimedText diff --git a/src/AS_DCP_JXS.cpp b/src/AS_DCP_JXS.cpp index c69394a..3dbbedc 100644 --- a/src/AS_DCP_JXS.cpp +++ b/src/AS_DCP_JXS.cpp @@ -31,6 +31,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "AS_DCP_internal.h" +#include "AS_DCP_JXS.h" #include #include #include diff --git a/src/AS_DCP_JXS.h b/src/AS_DCP_JXS.h new file mode 100644 index 0000000..dcd98e0 --- /dev/null +++ b/src/AS_DCP_JXS.h @@ -0,0 +1,333 @@ +/* +Copyright (c) 2003-2018, John Hurst, +Copyright (c) 2020-2021, Christian Minuth, Thomas Richter Fraunhofer IIS, +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/*! \file AS_DCP.h + \version $Id$ + \brief AS-DCP library, public interface + +The asdcplib library is a set of file access objects that offer simplified +access to files conforming to the standards published by the SMPTE +D-Cinema Technology Committee 21DC. The file format, labeled AS-DCP, +is described in a series of documents which includes but may not +be be limited to: + + o SMPTE ST 429-2:2011 DCP Operational Constraints + o SMPTE ST 429-3:2006 Sound and Picture Track File + o SMPTE ST 429-4:2006 MXF JPEG 2000 Application + o SMPTE ST 429-5:2009 Timed Text Track File + o SMPTE ST 429-6:2006 MXF Track File Essence Encryption + o SMPTE ST 429-10:2008 Stereoscopic Picture Track File + o SMPTE ST 429-14:2008 Aux Data Track File + o SMPTE ST 330:2004 - UMID + o SMPTE ST 336:2001 - KLV + o SMPTE ST 377:2004 - MXF (old version, required) + o SMPTE ST 377-1:2011 - MXF + o SMPTE ST 377-4:2012 - MXF Multichannel Audio Labeling Framework + o SMPTE ST 390:2011 - MXF OP-Atom + o SMPTE ST 379-1:2009 - MXF Generic Container (GC) + o SMPTE ST 381-1:2005 - MPEG2 picture in GC + o SMPTE ST 422:2006 - JPEG 2000 picture in GC + o SMPTE ST 382:2007 - WAV/PCM sound in GC + o IETF RFC 2104 - HMAC/SHA1 + o NIST FIPS 197 - AES (Rijndael) (via OpenSSL) + + o MXF Interop Track File Specification + o MXF Interop Track File Essence Encryption Specification + o MXF Interop Operational Constraints Specification + - Note: the MXF Interop documents are not formally published. + Contact the asdcplib support address to get copies. + +The following use cases are supported by the library: + + o Write essence to a plaintext or ciphertext AS-DCP file: + o Read essence from a plaintext or ciphertext AS-DCP file: + JPEG XS codestreams + + o Read header metadata from an AS-DCP file + +This project depends upon the following libraries: + - OpenSSL http://www.openssl.org/ + - Expat http://expat.sourceforge.net/ or + Xerces-C http://xerces.apache.org/xerces-c/ + An XML library is not needed if you don't need support for SMPTE ST 429-5:2009. +*/ + +#ifndef _AS_DCP_JXS_H_ +#define _AS_DCP_JXS_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "AS_DCP.h" +#include "Metadata.h" + +//-------------------------------------------------------------------------------- +// All library components are defined in the namespace ASDCP +// +namespace ASDCP { + // + //--------------------------------------------------------------------------------- + // + namespace JXS + { + const ui32_t MaxComponents = 4; // ISO 21122-1 Annex A.2 up to 8 components + const ui32_t MaxHorizontalLevels = 15; + const ui32_t MaxVerticalLevels = 2; + +#pragma pack(1) + struct ImageComponent_t // Essentially, a lookalike of the CDT marker, just with less bit-packing + { + ui8_t Bc; // Bitdepth (literal, not -1) + ui8_t Sx; + ui8_t Sy; // Subsampling factors, horizontal and vertically. Bit-packed in the marker. + }; +#pragma pack() + + struct PictureDescriptor + { + Rational EditRate; + ui32_t ContainerDuration; + Rational SampleRate; + ui32_t StoredWidth; + ui32_t StoredHeight; + Rational AspectRatio; + ui16_t Ppih; // Profile, copy from the PIH marker + ui16_t Plev; // Level and sublevel, copy from the PIH marker + ui16_t Wf; // Frame width, copy from the PIH marker + ui16_t Hf; // Frame height, copy from the PIH marker + ui16_t Hsl; // slice height, copy from the PIH marker + ui16_t Cw; // column width, or 0 for no columns, copy from the PIH marker + ui8_t Nc; // number of components, copy from the PIH marker + ui32_t MaximumBitRate; // bit rate in MB/sec, or 0 if not known + ui8_t Primaries; // Color primaries as defined by CICP + ui8_t TransferCurve; // Transfer curve as defined by CICP + ui8_t Matrix; // Transform matrix, as defined by CICP + bool fullRange; // If true, no head and toe region + ImageComponent_t ImageComponents[MaxComponents]; // These are copies from the CDT (component table) + }; + // Print debugging information to std::ostream + std::ostream& operator << (std::ostream& strm, const PictureDescriptor& pdesc); + // Print debugging information to stream (stderr default) + void PictureDescriptorDump(const PictureDescriptor&, FILE* = 0); + + // + class FrameBuffer : public ASDCP::FrameBuffer + { + public: + FrameBuffer() {} + FrameBuffer(ui32_t size) { Capacity(size); } + virtual ~FrameBuffer() {} + + // Print debugging information to stream (stderr default) + void Dump(FILE* = 0, ui32_t dump_bytes = 0) const; + }; + + + // An object which opens and reads a JPEG XS codestream file. The file is expected + // to contain exactly one complete frame of picture essence as an unwrapped (raw) + // ISO/IEC 21122 codestream. + class CodestreamParser + { + class h__CodestreamParser; + mem_ptr m_Parser; + ASDCP_NO_COPY_CONSTRUCT(CodestreamParser); + + public: + CodestreamParser(); + virtual ~CodestreamParser(); + + // Opens a file for reading, parses enough data to provide a complete + // set of stream metadata for the MXFWriter below. + // The frame buffer's PlaintextOffset parameter will be set to the first + // byte of the data segment. Set this value to zero if you want + // encrypted headers. + Result_t OpenReadFrame(const std::string& filename, FrameBuffer&) const; + + // Fill a PictureDescriptor struct with the values from the file's codestream. + // Returns RESULT_INIT if the file is not open. + Result_t FillPictureDescriptor(PictureDescriptor&) const; + }; + + // Parses the data in the frame buffer to fill in the picture descriptor. Copies + // the offset of the image data into start_of_data. Returns error if the parser fails. + Result_t ParseMetadataIntoDesc(const FrameBuffer&, PictureDescriptor&, byte_t* start_of_data = 0); + + // An object which reads a sequence of files containing JPEG XS pictures. + class SequenceParser + { + class h__SequenceParser; + mem_ptr m_Parser; + ASDCP_NO_COPY_CONSTRUCT(SequenceParser); + + public: + SequenceParser(); + virtual ~SequenceParser(); + + // Opens a directory for reading. The directory is expected to contain one or + // more files, each containing the codestream for exactly one picture. The + // files must be named such that the frames are in temporal order when sorted + // alphabetically by filename. The parser will automatically parse enough data + // from the first file to provide a complete set of stream metadata for the + // MXFWriter below. If the "pedantic" parameter is given and is true, the + // parser will check the metadata for each codestream and fail if a + // mismatch is detected. + Result_t OpenRead(const std::string& filename) const; + + // Opens a file sequence for reading. The sequence is expected to contain one or + // more filenames, each naming a file containing the codestream for exactly one + // picture. The parser will automatically parse enough data + // from the first file to provide a complete set of stream metadata for the + // MXFWriter below. If the "pedantic" parameter is given and is true, the + // parser will check the metadata for each codestream and fail if a + // mismatch is detected. + Result_t OpenRead(const std::list& file_list) const; + + // Fill a PictureDescriptor struct with the values from the first file's codestream. + // Returns RESULT_INIT if the directory is not open. + Result_t FillPictureDescriptor(PictureDescriptor&) const; + + // Rewind the directory to the beginning. + Result_t Reset() const; + + // Reads the next sequential frame in the directory and places it in the + // frame buffer. Fails if the buffer is too small or the direcdtory + // contains no more files. + // The frame buffer's PlaintextOffset parameter will be set to the first + // byte of the data segment. Set this value to zero if you want + // encrypted headers. + Result_t ReadFrame(FrameBuffer&) const; + }; + + + // + class MXFWriter + { + class h__Writer; + mem_ptr m_Writer; + ASDCP_NO_COPY_CONSTRUCT(MXFWriter); + + public: + MXFWriter(); + virtual ~MXFWriter(); + + // Warning: direct manipulation of MXF structures can interfere + // with the normal operation of the wrapper. Caveat emptor! + virtual MXF::OP1aHeader& OP1aHeader(); + virtual MXF::OPAtomIndexFooter& OPAtomIndexFooter(); + virtual MXF::RIP& RIP(); + + // Open the file for writing. The file must not exist. Returns error if + // the operation cannot be completed or if nonsensical data is discovered + // in the essence descriptor. + Result_t OpenWrite(const std::string& filename, const WriterInfo&, + const PictureDescriptor&, ui32_t HeaderSize = 16384); + + // Writes a frame of essence to the MXF file. If the optional AESEncContext + // argument is present, the essence is encrypted prior to writing. + // Fails if the file is not open, is finalized, or an operating system + // error occurs. + Result_t WriteFrame(const FrameBuffer&, AESEncContext* = 0, HMACContext* = 0); + + // Closes the MXF file, writing the index and revised header. + Result_t Finalize(); + }; + + // + class MXFReader + { + class h__Reader; + mem_ptr m_Reader; + ASDCP_NO_COPY_CONSTRUCT(MXFReader); + + public: + MXFReader(); + virtual ~MXFReader(); + + // Warning: direct manipulation of MXF structures can interfere + // with the normal operation of the wrapper. Caveat emptor! + virtual MXF::OP1aHeader& OP1aHeader(); + virtual MXF::OPAtomIndexFooter& OPAtomIndexFooter(); + virtual MXF::RIP& RIP(); + + // Open the file for reading. The file must exist. Returns error if the + // operation cannot be completed. + Result_t OpenRead(const std::string& filename) const; + + // Returns RESULT_INIT if the file is not open. + Result_t Close() const; + + // Fill an AudioDescriptor struct with the values from the file's header. + // Returns RESULT_INIT if the file is not open. + Result_t FillPictureDescriptor(PictureDescriptor&) const; + + // Fill a WriterInfo struct with the values from the file's header. + // Returns RESULT_INIT if the file is not open. + Result_t FillWriterInfo(WriterInfo&) const; + + // Compute the necessary frame buffer size in bytes for CBR + // codestreamss. + Result_t CalcFrameBufferSize(ui64_t &size) const; + + // Reads a frame of essence from the MXF file. If the optional AESEncContext + // argument is present, the essence is decrypted after reading. If the MXF + // file is encrypted and the AESDecContext argument is NULL, the frame buffer + // will contain the ciphertext frame data. If the HMACContext argument is + // not NULL, the HMAC will be calculated (if the file supports it). + // Returns RESULT_INIT if the file is not open, failure if the frame number is + // out of range, or if optional decrypt or HAMC operations fail. + Result_t ReadFrame(ui32_t frame_number, FrameBuffer&, AESDecContext* = 0, HMACContext* = 0) const; + + // Using the index table read from the footer partition, lookup the frame number + // and return the offset into the file at which to read that frame of essence. + // Returns RESULT_INIT if the file is not open, and RESULT_FRAME if the frame number is + // out of range. + Result_t LocateFrame(ui32_t FrameNum, Kumu::fpos_t& streamOffset, i8_t& temporalOffset, i8_t& keyFrameOffset) const; + + // Print debugging information to stream + void DumpHeaderMetadata(FILE* = 0) const; + void DumpIndex(FILE* = 0) const; + }; + } + Result_t MD_to_JXS_PDesc(const ASDCP::MXF::GenericPictureEssenceDescriptor& EssenceDescriptor, + const ASDCP::MXF::JPEGXSPictureSubDescriptor& EssenceSubDescriptor, + const ASDCP::Rational& EditRate, const ASDCP::Rational& SampleRate, + ASDCP::JXS::PictureDescriptor& PDesc); + + Result_t JXS_PDesc_to_MD(const JXS::PictureDescriptor& PDesc, + const ASDCP::Dictionary& dict, + ASDCP::MXF::GenericPictureEssenceDescriptor& EssenceDescriptor, + ASDCP::MXF::JPEGXSPictureSubDescriptor& EssenceSubDescriptor); +}; + +/// +#endif diff --git a/src/AS_DCP_internal.h b/src/AS_DCP_internal.h index d09fa6d..5626058 100755 --- a/src/AS_DCP_internal.h +++ b/src/AS_DCP_internal.h @@ -162,15 +162,6 @@ namespace ASDCP ASDCP::MXF::GenericPictureEssenceDescriptor& EssenceDescriptor, ASDCP::MXF::JPEG2000PictureSubDescriptor& EssenceSubDescriptor); - Result_t MD_to_JXS_PDesc(const ASDCP::MXF::GenericPictureEssenceDescriptor& EssenceDescriptor, - const ASDCP::MXF::JPEGXSPictureSubDescriptor& EssenceSubDescriptor, - const ASDCP::Rational& EditRate, const ASDCP::Rational& SampleRate, - ASDCP::JXS::PictureDescriptor& PDesc); - - Result_t JXS_PDesc_to_MD(const JXS::PictureDescriptor& PDesc, - const ASDCP::Dictionary& dict, - ASDCP::MXF::GenericPictureEssenceDescriptor& EssenceDescriptor, - ASDCP::MXF::JPEGXSPictureSubDescriptor& EssenceSubDescriptor); Result_t PCM_ADesc_to_MD(PCM::AudioDescriptor& ADesc, ASDCP::MXF::WaveAudioDescriptor* ADescObj); Result_t MD_to_PCM_ADesc(ASDCP::MXF::WaveAudioDescriptor* ADescObj, PCM::AudioDescriptor& ADesc); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 50d0b64..fe576a4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -63,11 +63,11 @@ if (HAVE_OPENSSL) endif() # header for deployment (install target) -set(asdcp_deploy_header AS_DCP.h PCMParserList.h AS_DCP_internal.h KM_error.h KM_fileio.h KM_util.h KM_memio.h KM_tai.h KM_platform.h KM_log.h KM_mutex.h dirent_win.h) +set(asdcp_deploy_header AS_DCP.h AS_DCP_JXS.h PCMParserList.h AS_DCP_internal.h KM_error.h KM_fileio.h KM_util.h KM_memio.h KM_tai.h KM_platform.h KM_log.h KM_mutex.h dirent_win.h) # header set(asdcp_src ${asdcp_src} Wav.h WavFileWriter.h MXF.h Metadata.h JP2K.h -JXS.h AS_DCP.h AS_DCP_internal.h KLV.h MPEG.h MXFTypes.h MDD.h +JXS.h AS_DCP.h AS_DCP_JXS.h AS_DCP_internal.h KLV.h MPEG.h MXFTypes.h MDD.h PCMParserList.h S12MTimecode.h AtmosSyncChannel_Generator.h AtmosSyncChannel_Mixer.h PCMDataProviders.h SyncEncoder.h SyncCommon.h CRC16.h UUIDInformation.h dirent_win.h ) @@ -78,10 +78,10 @@ set(as02_src h__02_Reader.cpp h__02_Writer.cpp AS_02_ISXD.cpp AS_02_JP2K.cpp AS_02_JXS.cpp AS_02_PCM.cpp ST2052_TextParser.cpp AS_02_TimedText.cpp AS_02_ACES.cpp ACES_Codestream_Parser.cpp ACES_Sequence_Parser.cpp ACES.cpp AS_02_IAB.cpp ST2052_TextParser.cpp) # header for deployment (install target) -set(as02_deploy_header AS_02.h Metadata.h MXF.h MXFTypes.h KLV.h MDD.h AS_02_ACES.h ACES.h AS_02_IAB.h AS_02_internal.h) +set(as02_deploy_header AS_02.h AS_02_JXS.h Metadata.h MXF.h MXFTypes.h KLV.h MDD.h AS_02_ACES.h ACES.h AS_02_IAB.h AS_02_internal.h) # header -set(as02_src ${as02_src} AS_02.h AS_02_internal.h AS_02_ACES.h ACES.h AS_02_IAB.h) +set(as02_src ${as02_src} AS_02.h AS_02_JXS.h AS_02_internal.h AS_02_ACES.h ACES.h AS_02_IAB.h) include_directories("${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/src/JXS_Codestream_Parser.cpp b/src/JXS_Codestream_Parser.cpp index c27dd45..bb7b729 100644 --- a/src/JXS_Codestream_Parser.cpp +++ b/src/JXS_Codestream_Parser.cpp @@ -33,6 +33,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include #include #include #include diff --git a/src/JXS_Sequence_Parser.cpp b/src/JXS_Sequence_Parser.cpp index f16e730..38b8de4 100644 --- a/src/JXS_Sequence_Parser.cpp +++ b/src/JXS_Sequence_Parser.cpp @@ -32,6 +32,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include +#include #include #include #include @@ -278,4 +279,4 @@ ASDCP::JXS::SequenceParser::FillPictureDescriptor(PictureDescriptor& PDesc) cons // // end JXS_Sequence_Parser.cpp -// \ No newline at end of file +// diff --git a/src/Makefile.am b/src/Makefile.am index 60ef423..179bf05 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -55,7 +55,8 @@ include_HEADERS = \ KM_util.h \ KM_tai.h \ KM_xml.h \ - AS_DCP.h + AS_DCP.h \ + AS_DCP_JXS.h if DEV_HEADERS include_HEADERS += \ @@ -84,6 +85,7 @@ endif if USE_AS_02 include_HEADERS += \ AS_02.h \ + AS_02_JXS.h \ AS_02_ACES.h endif @@ -120,13 +122,12 @@ nodist_libasdcp_la_SOURCES = Metadata_h.tt2 Metadata_cpp.tt2 \ # sources for asdcp library libasdcp_la_SOURCES = MPEG2_Parser.cpp MPEG.cpp JP2K_Codestream_Parser.cpp \ JP2K_Sequence_Parser.cpp JP2K.cpp \ - JXS_Codestream_Parser.cpp JXS_Sequence_Parser.cpp JXS.cpp \ PCM_Parser.cpp Wav.cpp TimedText_Parser.cpp KLV.cpp Dict.cpp MXFTypes.cpp MXF.cpp \ Index.cpp Metadata.cpp AS_DCP.cpp AS_DCP_MXF.cpp \ h__Reader.cpp h__Writer.cpp AS_DCP_MPEG2.cpp AS_DCP_JP2K.cpp \ - AS_DCP_JXS.cpp AS_DCP_PCM.cpp AS_DCP_TimedText.cpp PCMParserList.cpp \ + AS_DCP_PCM.cpp AS_DCP_TimedText.cpp PCMParserList.cpp \ Wav.h WavFileWriter.h MXF.h Metadata.h \ - JP2K.h JXS.h AS_DCP.h AS_DCP_internal.h KLV.h MPEG.h MXFTypes.h MDD.h \ + JP2K.h JXS.h AS_DCP.h AS_DCP_JXS.h AS_DCP_internal.h KLV.h MPEG.h MXFTypes.h MDD.h \ PCMParserList.h S12MTimecode.h MDD.cpp \ AS_DCP_ATMOS.cpp AS_DCP_DCData.cpp info.in \ DCData_ByteStream_Parser.cpp DCData_Sequence_Parser.cpp \ @@ -141,6 +142,10 @@ if HAVE_OPENSSL libasdcp_la_SOURCES += AS_DCP_AES.cpp endif +if USE_ASDCP_JXS +libasdcp_la_SOURCES += JXS_Codestream_Parser.cpp JXS_Sequence_Parser.cpp JXS.cpp AS_DCP_JXS.cpp +endif + libasdcp_la_LDFLAGS = -release @VERSION@ # additional libraries to link against for a library libasdcp_la_LIBADD = libkumu.la @@ -151,6 +156,7 @@ if USE_AS_02 libas02_la_SOURCES = \ AS_02.h \ AS_02_internal.h \ + AS_02_JXS.h \ ACES.h \ AS_02_ACES.h \ AS_02_IAB.h \ -- cgit v1.2.3