working multi-dict
[asdcplib.git] / src / AS_DCP_MPEG2.cpp
index 04010be32af86aae2589a718cc9f726e41448e24..23703193a4bf2db814f25d2ffdfc0ff664767586 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2004-2006, John Hurst
+Copyright (c) 2004-2009, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -30,6 +30,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 #include "AS_DCP_internal.h"
+#include <iostream>
+#include <iomanip>
 
 
 //------------------------------------------------------------------------------------------
@@ -46,7 +48,8 @@ MD_to_MPEG2_VDesc(MXF::MPEG2VideoDescriptor* VDescObj, MPEG2::VideoDescriptor& V
   VDesc.SampleRate             = VDescObj->SampleRate;
   VDesc.EditRate               = VDescObj->SampleRate;
   VDesc.FrameRate              = VDescObj->SampleRate.Numerator;
-  VDesc.ContainerDuration      = VDescObj->ContainerDuration;
+  assert(VDescObj->ContainerDuration <= 0xFFFFFFFFL);
+  VDesc.ContainerDuration      = (ui32_t) VDescObj->ContainerDuration;
 
   VDesc.FrameLayout            = VDescObj->FrameLayout;
   VDesc.StoredWidth            = VDescObj->StoredWidth;
@@ -73,7 +76,6 @@ MPEG2_VDesc_to_MD(MPEG2::VideoDescriptor& VDesc, MXF::MPEG2VideoDescriptor* VDes
   ASDCP_TEST_NULL(VDescObj);
 
   VDescObj->SampleRate = VDesc.SampleRate;
-  VDescObj->SampleRate.Numerator = VDesc.FrameRate;
   VDescObj->ContainerDuration = VDesc.ContainerDuration;
 
   VDescObj->FrameLayout = VDesc.FrameLayout;
@@ -93,6 +95,27 @@ MPEG2_VDesc_to_MD(MPEG2::VideoDescriptor& VDesc, MXF::MPEG2VideoDescriptor* VDes
   return RESULT_OK;
 }
 
+//
+std::ostream&
+ASDCP::MPEG2::operator << (std::ostream& strm, const VideoDescriptor& VDesc)
+{
+  strm << "        SampleRate: " << VDesc.SampleRate.Numerator << "/" << VDesc.SampleRate.Denominator << std::endl;
+  strm << "       FrameLayout: " << (unsigned) VDesc.FrameLayout << std::endl;
+  strm << "       StoredWidth: " << (unsigned) VDesc.StoredWidth << std::endl;
+  strm << "      StoredHeight: " << (unsigned) VDesc.StoredHeight << std::endl;
+  strm << "       AspectRatio: " << VDesc.AspectRatio.Numerator << "/" << VDesc.AspectRatio.Denominator << std::endl;
+  strm << "    ComponentDepth: " << (unsigned) VDesc.ComponentDepth << std::endl;
+  strm << " HorizontalSubsmpl: " << (unsigned) VDesc.HorizontalSubsampling << std::endl;
+  strm << "   VerticalSubsmpl: " << (unsigned) VDesc.VerticalSubsampling << std::endl;
+  strm << "       ColorSiting: " << (unsigned) VDesc.ColorSiting << std::endl;
+  strm << "  CodedContentType: " << (unsigned) VDesc.CodedContentType << std::endl;
+  strm << "          LowDelay: " << (unsigned) VDesc.LowDelay << std::endl;
+  strm << "           BitRate: " << (unsigned) VDesc.BitRate << std::endl;
+  strm << "   ProfileAndLevel: " << (unsigned) VDesc.ProfileAndLevel << std::endl;
+  strm << " ContainerDuration: " << (unsigned) VDesc.ContainerDuration << std::endl;
+
+  return strm;
+}
 
 //
 void
@@ -140,11 +163,12 @@ ASDCP::MPEG2::VideoDescriptorDump(const VideoDescriptor& VDesc, FILE* stream)
 class ASDCP::MPEG2::MXFReader::h__Reader : public ASDCP::h__Reader
 {
   ASDCP_NO_COPY_CONSTRUCT(h__Reader);
+  h__Reader();
 
 public:
   VideoDescriptor m_VDesc;        // video parameter list
 
-  h__Reader() {}
+  h__Reader(const Dictionary& d) : ASDCP::h__Reader(d) {}
   ~h__Reader() {}
   Result_t    OpenRead(const char*);
   Result_t    ReadFrame(ui32_t, FrameBuffer&, AESDecContext*, HMACContext*);
@@ -231,7 +255,7 @@ ASDCP::MPEG2::MXFReader::h__Reader::ReadFrame(ui32_t FrameNum, FrameBuffer& Fram
   if ( ! m_File.IsOpen() )
     return RESULT_INIT;
 
-  Result_t result = ReadEKLVPacket(FrameNum, FrameBuf, Dict::ul(MDD_MPEG2Essence), Ctx, HMAC);
+  Result_t result = ReadEKLVFrame(FrameNum, FrameBuf, m_Dict->ul(MDD_MPEG2Essence), Ctx, HMAC);
 
   if ( ASDCP_FAILURE(result) )
     return result;
@@ -264,7 +288,7 @@ ASDCP::MPEG2::FrameBuffer::Dump(FILE* stream, ui32_t dump_len) const
   if ( stream == 0 )
     stream = stderr;
 
-  fprintf(stream, "Frame: %06lu, %c%-2hu, %7lu bytes",
+  fprintf(stream, "Frame: %06u, %c%-2hu, %7u bytes",
          m_FrameNumber, FrameTypeChar(m_FrameType), m_TemporalOffset, m_Size);
 
   if ( m_GOPStart )
@@ -281,7 +305,7 @@ ASDCP::MPEG2::FrameBuffer::Dump(FILE* stream, ui32_t dump_len) const
 
 ASDCP::MPEG2::MXFReader::MXFReader()
 {
-  m_Reader = new h__Reader;
+  m_Reader = new h__Reader(DefaultCompositeDict());
 }
 
 
@@ -384,14 +408,15 @@ ASDCP::MPEG2::MXFReader::DumpIndex(FILE* stream) const
 //
 class ASDCP::MPEG2::MXFWriter::h__Writer : public ASDCP::h__Writer
 {
+  ASDCP_NO_COPY_CONSTRUCT(h__Writer);
+  h__Writer();
+
 public:
   VideoDescriptor m_VDesc;
   ui32_t          m_GOPOffset;
   byte_t          m_EssenceUL[SMPTE_UL_LENGTH];
 
-  ASDCP_NO_COPY_CONSTRUCT(h__Writer);
-
-  h__Writer() : m_GOPOffset(0) {
+  h__Writer(const Dictionary& d) : ASDCP::h__Writer(d), m_GOPOffset(0) {
     memset(m_EssenceUL, 0, SMPTE_UL_LENGTH);
   }
 
@@ -417,7 +442,7 @@ ASDCP::MPEG2::MXFWriter::h__Writer::OpenWrite(const char* filename, ui32_t Heade
   if ( ASDCP_SUCCESS(result) )
     {
       m_HeaderSize = HeaderSize;
-      m_EssenceDescriptor = new MPEG2VideoDescriptor;
+      m_EssenceDescriptor = new MPEG2VideoDescriptor(m_Dict);
       result = m_State.Goto_INIT();
     }
 
@@ -435,13 +460,13 @@ ASDCP::MPEG2::MXFWriter::h__Writer::SetSourceStream(const VideoDescriptor& VDesc
   Result_t result = MPEG2_VDesc_to_MD(m_VDesc, (MPEG2VideoDescriptor*)m_EssenceDescriptor);
 
   if ( ASDCP_SUCCESS(result) )
-      result = WriteMXFHeader(MPEG_PACKAGE_LABEL, UL(Dict::ul(MDD_MPEG2_VESWrapping)), 
-                             PICT_DEF_LABEL,     UL(Dict::ul(MDD_PictureDataDef)),
+      result = WriteMXFHeader(MPEG_PACKAGE_LABEL, UL(m_Dict->ul(MDD_MPEG2_VESWrapping)), 
+                             PICT_DEF_LABEL,     UL(m_Dict->ul(MDD_PictureDataDef)),
                              m_VDesc.EditRate, 24 /* TCFrameRate */);
 
   if ( ASDCP_SUCCESS(result) )
     {
-      memcpy(m_EssenceUL, Dict::ul(MDD_MPEG2Essence), SMPTE_UL_LENGTH);
+      memcpy(m_EssenceUL, m_Dict->ul(MDD_MPEG2Essence), SMPTE_UL_LENGTH);
       m_EssenceUL[SMPTE_UL_LENGTH-1] = 1; // first (and only) essence container
       result = m_State.Goto_READY();
     }
@@ -493,8 +518,15 @@ ASDCP::MPEG2::MXFWriter::h__Writer::WriteFrame(const FrameBuffer& FrameBuf, AESE
 
   // update the index manager
   Entry.TemporalOffset = - FrameBuf.TemporalOffset();
-  Entry.KeyFrameOffset = m_GOPOffset;
+  Entry.KeyFrameOffset = 0 - m_GOPOffset;
   Entry.Flags = Flags;
+  /*
+  fprintf(stderr, "to: %4hd   ko: %4hd   c1: %4hd   c2: %4hd   fl: 0x%02x\n",
+         Entry.TemporalOffset, Entry.KeyFrameOffset,
+         m_GOPOffset + Entry.TemporalOffset,
+         Entry.KeyFrameOffset - Entry.TemporalOffset,
+         Entry.Flags);
+  */
   m_FooterPart.PushIndexEntry(Entry);
   m_FramesWritten++;
   m_GOPOffset++;
@@ -536,15 +568,17 @@ ASDCP::Result_t
 ASDCP::MPEG2::MXFWriter::OpenWrite(const char* filename, const WriterInfo& Info,
                                   const VideoDescriptor& VDesc, ui32_t HeaderSize)
 {
-  m_Writer = new h__Writer;
+  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, HeaderSize);
 
   if ( ASDCP_SUCCESS(result) )
-    {
-      m_Writer->m_Info = Info;
-      result = m_Writer->SetSourceStream(VDesc);
-    }
+    result = m_Writer->SetSourceStream(VDesc);
 
   if ( ASDCP_FAILURE(result) )
     m_Writer.release();