mega datetime patch
[asdcplib.git] / src / AS_DCP_JP2K.cpp
index 20e3e8c3594deb1e655c13a139b051864c62943a..076ba5c41a35b2002b5837dd90f928c6ee3825bb 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2004-2009, John Hurst
+Copyright (c) 2004-2012, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -263,18 +263,18 @@ lh__Reader::MD_to_JP2K_PDesc(JP2K::PictureDescriptor& PDesc)
        DefaultLogSink().Error("Unexpected PictureComponentSizing size: %u, should be 17\n", tmp_size);
 
       // CodingStyleDefault
-      memset(&m_PDesc.CodingStyleDefault, 0, sizeof(CodingStyleDefault_t));
-      memcpy(&m_PDesc.CodingStyleDefault,
+      memset(&PDesc.CodingStyleDefault, 0, sizeof(CodingStyleDefault_t));
+      memcpy(&PDesc.CodingStyleDefault,
             m_EssenceSubDescriptor->CodingStyleDefault.RoData(),
             m_EssenceSubDescriptor->CodingStyleDefault.Length());
 
       // QuantizationDefault
-      memset(&m_PDesc.QuantizationDefault, 0, sizeof(QuantizationDefault_t));
-      memcpy(&m_PDesc.QuantizationDefault,
+      memset(&PDesc.QuantizationDefault, 0, sizeof(QuantizationDefault_t));
+      memcpy(&PDesc.QuantizationDefault,
             m_EssenceSubDescriptor->QuantizationDefault.RoData(),
             m_EssenceSubDescriptor->QuantizationDefault.Length());
 
-      m_PDesc.QuantizationDefault.SPqcdLength = m_EssenceSubDescriptor->QuantizationDefault.Length() - 1;
+      PDesc.QuantizationDefault.SPqcdLength = m_EssenceSubDescriptor->QuantizationDefault.Length() - 1;
     }
 
   return RESULT_OK;
@@ -312,10 +312,15 @@ lh__Reader::OpenRead(const char* filename, EssenceType_t type)
        {
          if ( m_EditRate != m_SampleRate )
            {
-             DefaultLogSink().Error("EditRate and SampleRate do not match (%.03f, %.03f).\n",
-                                    m_EditRate.Quotient(), m_SampleRate.Quotient());
+             DefaultLogSink().Warn("EditRate and SampleRate do not match (%.03f, %.03f).\n",
+                                   m_EditRate.Quotient(), m_SampleRate.Quotient());
              
-             if ( m_EditRate == EditRate_24 && m_SampleRate == EditRate_48 )
+             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 )
                {
                  DefaultLogSink().Debug("File may contain JPEG Interop stereoscopic images.\n");
                  return RESULT_SFORMAT;
@@ -326,9 +331,58 @@ lh__Reader::OpenRead(const char* filename, EssenceType_t type)
        }
       else if ( type == ASDCP::ESS_JPEG_2000_S )
        {
-         if ( ! ( m_EditRate == EditRate_24 && m_SampleRate == EditRate_48 ) )
+         if ( m_EditRate == EditRate_24 )
            {
-             DefaultLogSink().Error("EditRate and SampleRate not correct for 24/48 stereoscopic essence.\n");
+             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
+           {
+             DefaultLogSink().Error("EditRate not correct for stereoscopic essence: %d/%d.\n",
+                                    m_EditRate.Numerator, m_EditRate.Denominator);
              return RESULT_FORMAT;
            }
        }
@@ -359,7 +413,8 @@ lh__Reader::ReadFrame(ui32_t FrameNum, JP2K::FrameBuffer& FrameBuf,
   if ( ! m_File.IsOpen() )
     return RESULT_INIT;
 
-  return ReadEKLVFrame(FrameNum, FrameBuf, m_Dict.ul(MDD_JPEG2000Essence), Ctx, HMAC);
+  assert(m_Dict);
+  return ReadEKLVFrame(FrameNum, FrameBuf, m_Dict->ul(MDD_JPEG2000Essence), Ctx, HMAC);
 }
 
 
@@ -406,6 +461,36 @@ ASDCP::JP2K::MXFReader::~MXFReader()
 {
 }
 
+// Warning: direct manipulation of MXF structures can interfere
+// with the normal operation of the wrapper.  Caveat emptor!
+//
+ASDCP::MXF::OPAtomHeader&
+ASDCP::JP2K::MXFReader::OPAtomHeader()
+{
+  if ( m_Reader.empty() )
+    {
+      assert(g_OPAtomHeader);
+      return *g_OPAtomHeader;
+    }
+
+  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::JP2K::MXFReader::OPAtomIndexFooter()
+{
+  if ( m_Reader.empty() )
+    {
+      assert(g_OPAtomIndexFooter);
+      return *g_OPAtomIndexFooter;
+    }
+
+  return m_Reader->m_FooterPart;
+}
+
 // Open the file for reading. The file must exist. Returns error if the
 // operation cannot be completed.
 ASDCP::Result_t
@@ -472,6 +557,19 @@ ASDCP::JP2K::MXFReader::DumpIndex(FILE* stream) const
     m_Reader->m_FooterPart.Dump(stream);
 }
 
+//
+ASDCP::Result_t
+ASDCP::JP2K::MXFReader::Close() const
+{
+  if ( m_Reader && m_Reader->m_File.IsOpen() )
+    {
+      m_Reader->Close();
+      return RESULT_OK;
+    }
+
+  return RESULT_INIT;
+}
+
 
 //------------------------------------------------------------------------------------------
 
@@ -547,7 +645,8 @@ public:
       {
        ui32_t SequenceNum = FrameNum * 2;
        SequenceNum += ( phase == SP_RIGHT ) ? 2 : 1;
-       result = ReadEKLVPacket(FrameNum, SequenceNum, FrameBuf, m_Dict.ul(MDD_JPEG2000Essence), Ctx, HMAC);
+       assert(m_Dict);
+       result = ReadEKLVPacket(FrameNum, SequenceNum, FrameBuf, m_Dict->ul(MDD_JPEG2000Essence), Ctx, HMAC);
       }
 
     return result;
@@ -566,6 +665,36 @@ ASDCP::JP2K::MXFSReader::~MXFSReader()
 {
 }
 
+// Warning: direct manipulation of MXF structures can interfere
+// with the normal operation of the wrapper.  Caveat emptor!
+//
+ASDCP::MXF::OPAtomHeader&
+ASDCP::JP2K::MXFSReader::OPAtomHeader()
+{
+  if ( m_Reader.empty() )
+    {
+      assert(g_OPAtomHeader);
+      return *g_OPAtomHeader;
+    }
+
+  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::JP2K::MXFSReader::OPAtomIndexFooter()
+{
+  if ( m_Reader.empty() )
+    {
+      assert(g_OPAtomIndexFooter);
+      return *g_OPAtomIndexFooter;
+    }
+
+  return m_Reader->m_FooterPart;
+}
+
 // Open the file for reading. The file must exist. Returns error if the
 // operation cannot be completed.
 ASDCP::Result_t
@@ -648,6 +777,20 @@ ASDCP::JP2K::MXFSReader::DumpIndex(FILE* stream) const
     m_Reader->m_FooterPart.Dump(stream);
 }
 
+//
+ASDCP::Result_t
+ASDCP::JP2K::MXFSReader::Close() const
+{
+  if ( m_Reader && m_Reader->m_File.IsOpen() )
+    {
+      m_Reader->Close();
+      return RESULT_OK;
+    }
+
+  return RESULT_INIT;
+}
+
+
 //------------------------------------------------------------------------------------------
 
 
@@ -706,14 +849,15 @@ lh__Writer::JP2K_PDesc_to_MD(JP2K::PictureDescriptor& PDesc)
   // PixelLayout          byte_t[PixelLayoutSize] = s_PixelLayoutXYZ
   //    }
 
+  assert(m_Dict);
   if ( PDesc.StoredWidth < 2049 )
     {
-      PDescObj->PictureEssenceCoding.Set(m_Dict.ul(MDD_JP2KEssenceCompression_2K));
+      PDescObj->PictureEssenceCoding.Set(m_Dict->ul(MDD_JP2KEssenceCompression_2K));
       m_EssenceSubDescriptor->Rsize = 3;
     }
   else
     {
-      PDescObj->PictureEssenceCoding.Set(m_Dict.ul(MDD_JP2KEssenceCompression_4K));
+      PDescObj->PictureEssenceCoding.Set(m_Dict->ul(MDD_JP2KEssenceCompression_4K));
       m_EssenceSubDescriptor->Rsize = 4;
     }
 
@@ -796,6 +940,7 @@ lh__Writer::OpenWrite(const char* filename, EssenceType_t type, ui32_t HeaderSiz
 ASDCP::Result_t
 lh__Writer::SetSourceStream(const PictureDescriptor& PDesc, const std::string& label, ASDCP::Rational LocalEditRate)
 {
+  assert(m_Dict);
   if ( ! m_State.Test_INIT() )
     return RESULT_STATE;
 
@@ -805,18 +950,22 @@ lh__Writer::SetSourceStream(const PictureDescriptor& PDesc, const std::string& l
   m_PDesc = PDesc;
   Result_t result = JP2K_PDesc_to_MD(m_PDesc);
 
-  if ( ASDCP_SUCCESS(result) )
-      result = WriteMXFHeader(label, UL(m_Dict.ul(MDD_JPEG_2000Wrapping)),
-                             PICT_DEF_LABEL,     UL(m_Dict.ul(MDD_PictureDataDef)),
-                             LocalEditRate, 24 /* TCFrameRate */);
-
   if ( ASDCP_SUCCESS(result) )
     {
-      memcpy(m_EssenceUL, m_Dict.ul(MDD_JPEG2000Essence), SMPTE_UL_LENGTH);
+      memcpy(m_EssenceUL, m_Dict->ul(MDD_JPEG2000Essence), SMPTE_UL_LENGTH);
       m_EssenceUL[SMPTE_UL_LENGTH-1] = 1; // first (and only) essence container
       result = m_State.Goto_READY();
     }
 
+  if ( ASDCP_SUCCESS(result) )
+    {
+      ui32_t TCFrameRate = ( m_PDesc.EditRate == EditRate_23_98  ) ? 24 : m_PDesc.EditRate.Numerator;
+
+      result = WriteMXFHeader(label, UL(m_Dict->ul(MDD_JPEG_2000Wrapping)),
+                             PICT_DEF_LABEL, UL(m_EssenceUL), UL(m_Dict->ul(MDD_PictureDataDef)),
+                             LocalEditRate, TCFrameRate);
+    }
+
   return result;
 }
 
@@ -888,6 +1037,35 @@ ASDCP::JP2K::MXFWriter::~MXFWriter()
 {
 }
 
+// Warning: direct manipulation of MXF structures can interfere
+// with the normal operation of the wrapper.  Caveat emptor!
+//
+ASDCP::MXF::OPAtomHeader&
+ASDCP::JP2K::MXFWriter::OPAtomHeader()
+{
+  if ( m_Writer.empty() )
+    {
+      assert(g_OPAtomHeader);
+      return *g_OPAtomHeader;
+    }
+
+  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::JP2K::MXFWriter::OPAtomIndexFooter()
+{
+  if ( m_Writer.empty() )
+    {
+      assert(g_OPAtomIndexFooter);
+      return *g_OPAtomIndexFooter;
+    }
+
+  return m_Writer->m_FooterPart;
+}
 
 // Open the file for writing. The file must not exist. Returns error if
 // the operation cannot be completed.
@@ -990,6 +1168,35 @@ ASDCP::JP2K::MXFSWriter::~MXFSWriter()
 {
 }
 
+// Warning: direct manipulation of MXF structures can interfere
+// with the normal operation of the wrapper.  Caveat emptor!
+//
+ASDCP::MXF::OPAtomHeader&
+ASDCP::JP2K::MXFSWriter::OPAtomHeader()
+{
+  if ( m_Writer.empty() )
+    {
+      assert(g_OPAtomHeader);
+      return *g_OPAtomHeader;
+    }
+
+  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::JP2K::MXFSWriter::OPAtomIndexFooter()
+{
+  if ( m_Writer.empty() )
+    {
+      assert(g_OPAtomIndexFooter);
+      return *g_OPAtomIndexFooter;
+    }
+
+  return m_Writer->m_FooterPart;
+}
 
 // Open the file for writing. The file must not exist. Returns error if
 // the operation cannot be completed.
@@ -1002,9 +1209,14 @@ ASDCP::JP2K::MXFSWriter::OpenWrite(const char* filename, const WriterInfo& Info,
   else
     m_Writer = new h__SWriter(DefaultInteropDict());
 
-  if ( PDesc.EditRate != ASDCP::EditRate_24 )
+  if ( PDesc.EditRate != ASDCP::EditRate_24
+       && PDesc.EditRate != ASDCP::EditRate_25
+       && PDesc.EditRate != ASDCP::EditRate_30
+       && PDesc.EditRate != ASDCP::EditRate_48
+       && PDesc.EditRate != ASDCP::EditRate_50
+       && PDesc.EditRate != ASDCP::EditRate_60 )
     {
-      DefaultLogSink().Error("Stereoscopic wrapping requires 24 fps input streams.\n");
+      DefaultLogSink().Error("Stereoscopic wrapping requires 24, 25, 30, 48, 50 or 60 fps input streams.\n");
       return RESULT_FORMAT;
     }
 
@@ -1018,9 +1230,26 @@ ASDCP::JP2K::MXFSWriter::OpenWrite(const char* filename, const WriterInfo& Info,
   if ( ASDCP_SUCCESS(result) )
     {
       PictureDescriptor TmpPDesc = PDesc;
-      TmpPDesc.EditRate = ASDCP::EditRate_48;
 
-      result = m_Writer->SetSourceStream(TmpPDesc, JP2K_S_PACKAGE_LABEL, ASDCP::EditRate_24);
+      if ( PDesc.EditRate == ASDCP::EditRate_24 )
+       TmpPDesc.EditRate = ASDCP::EditRate_48;
+
+      else if ( PDesc.EditRate == ASDCP::EditRate_25 )
+       TmpPDesc.EditRate = ASDCP::EditRate_50;
+
+      else if ( PDesc.EditRate == ASDCP::EditRate_30 )
+       TmpPDesc.EditRate = ASDCP::EditRate_60;
+
+      else if ( PDesc.EditRate == ASDCP::EditRate_48 )
+       TmpPDesc.EditRate = ASDCP::EditRate_96;
+
+      else if ( PDesc.EditRate == ASDCP::EditRate_50 )
+       TmpPDesc.EditRate = ASDCP::EditRate_100;
+
+      else if ( PDesc.EditRate == ASDCP::EditRate_60 )
+       TmpPDesc.EditRate = ASDCP::EditRate_120;
+
+      result = m_Writer->SetSourceStream(TmpPDesc, JP2K_S_PACKAGE_LABEL, PDesc.EditRate);
     }
 
   if ( ASDCP_FAILURE(result) )