version revv
[asdcplib.git] / src / AS_DCP_JP2K.cpp
index 39dceedd72b4e681f93bdfe39f8108911e0dc62d..076ba5c41a35b2002b5837dd90f928c6ee3825bb 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2004-2008, John Hurst
+Copyright (c) 2004-2012, 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>
 
 using namespace ASDCP::JP2K;
 using Kumu::GenRandomValue;
@@ -42,6 +44,71 @@ static std::string PICT_DEF_LABEL = "Picture Track";
 
 int s_exp_lookup[16] = { 0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,2048, 4096, 8192, 16384, 32768 };
 
+//
+std::ostream&
+ASDCP::JP2K::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 << "             Rsize: " << (unsigned) PDesc.Rsize << std::endl;
+  strm << "             Xsize: " << (unsigned) PDesc.Xsize << std::endl;
+  strm << "             Ysize: " << (unsigned) PDesc.Ysize << std::endl;
+  strm << "            XOsize: " << (unsigned) PDesc.XOsize << std::endl;
+  strm << "            YOsize: " << (unsigned) PDesc.YOsize << std::endl;
+  strm << "            XTsize: " << (unsigned) PDesc.XTsize << std::endl;
+  strm << "            YTsize: " << (unsigned) PDesc.YTsize << std::endl;
+  strm << "           XTOsize: " << (unsigned) PDesc.XTOsize << std::endl;
+  strm << "           YTOsize: " << (unsigned) PDesc.YTOsize << std::endl;
+  strm << " ContainerDuration: " << (unsigned) PDesc.ContainerDuration << std::endl;
+
+  strm << "-- JPEG 2000 Metadata --" << std::endl;
+  strm << "    ImageComponents:" << std::endl;
+  strm << "  bits  h-sep v-sep" << std::endl;
+
+  ui32_t i;
+  for ( i = 0; i < PDesc.Csize; i++ )
+    {
+      strm << "  " << std::setw(4) << PDesc.ImageComponents[i].Ssize + 1 /* See ISO 15444-1, Table A11, for the origin of '+1' */
+          << "  " << std::setw(5) << PDesc.ImageComponents[i].XRsize
+          << " " << std::setw(5) << PDesc.ImageComponents[i].YRsize
+          << std::endl;
+    }
+
+  strm << "               Scod: " << (short) PDesc.CodingStyleDefault.Scod << std::endl;
+  strm << "   ProgressionOrder: " << (short) PDesc.CodingStyleDefault.SGcod.ProgressionOrder << std::endl;
+  strm << "     NumberOfLayers: " << (short) KM_i16_BE(Kumu::cp2i<ui16_t>(PDesc.CodingStyleDefault.SGcod.NumberOfLayers)) << std::endl;
+  strm << " MultiCompTransform: " << (short) PDesc.CodingStyleDefault.SGcod.MultiCompTransform << std::endl;
+  strm << "DecompositionLevels: " << (short) PDesc.CodingStyleDefault.SPcod.DecompositionLevels << std::endl;
+  strm << "     CodeblockWidth: " << (short) PDesc.CodingStyleDefault.SPcod.CodeblockWidth << std::endl;
+  strm << "    CodeblockHeight: " << (short) PDesc.CodingStyleDefault.SPcod.CodeblockHeight << std::endl;
+  strm << "     CodeblockStyle: " << (short) PDesc.CodingStyleDefault.SPcod.CodeblockStyle << std::endl;
+  strm << "     Transformation: " << (short) PDesc.CodingStyleDefault.SPcod.Transformation << std::endl;
+
+
+  ui32_t precinct_set_size = 0;
+
+  for ( i = 0; PDesc.CodingStyleDefault.SPcod.PrecinctSize[i] != 0 && i < MaxPrecincts; i++ )
+    precinct_set_size++;
+
+  strm << "          Precincts: " << (short) precinct_set_size << std::endl;
+  strm << "precinct dimensions:" << std::endl;
+
+  for ( i = 0; i < precinct_set_size; i++ )
+    strm << "    " << i + 1 << ": " << s_exp_lookup[PDesc.CodingStyleDefault.SPcod.PrecinctSize[i]&0x0f] << " x "
+        << s_exp_lookup[(PDesc.CodingStyleDefault.SPcod.PrecinctSize[i]>>4)&0x0f] << std::endl;
+
+  strm << "               Sqcd: " << (short) PDesc.QuantizationDefault.Sqcd << std::endl;
+
+  char tmp_buf[MaxDefaults*2];
+  strm << "              SPqcd: " << Kumu::bin2hex(PDesc.QuantizationDefault.SPqcd, PDesc.QuantizationDefault.SPqcdLength, tmp_buf, MaxDefaults*2)
+       << std::endl;
+
+  return strm;
+}
+
 //
 void
 ASDCP::JP2K::PictureDescriptorDump(const PictureDescriptor& PDesc, FILE* stream)
@@ -52,6 +119,7 @@ ASDCP::JP2K::PictureDescriptorDump(const PictureDescriptor& PDesc, FILE* stream)
   fprintf(stream, "\
        AspectRatio: %d/%d\n\
           EditRate: %d/%d\n\
+        SampleRate: %d/%d\n\
        StoredWidth: %u\n\
       StoredHeight: %u\n\
              Rsize: %u\n\
@@ -66,6 +134,7 @@ ASDCP::JP2K::PictureDescriptorDump(const PictureDescriptor& PDesc, FILE* stream)
  ContainerDuration: %u\n",
          PDesc.AspectRatio.Numerator, PDesc.AspectRatio.Denominator,
          PDesc.EditRate.Numerator, PDesc.EditRate.Denominator,
+         PDesc.SampleRate.Numerator, PDesc.SampleRate.Denominator,
          PDesc.StoredWidth,
          PDesc.StoredHeight,
          PDesc.Rsize,
@@ -130,15 +199,18 @@ ASDCP::JP2K::PictureDescriptorDump(const PictureDescriptor& PDesc, FILE* stream)
          );
 }
 
+
 //------------------------------------------------------------------------------------------
 //
 // hidden, internal implementation of JPEG 2000 reader
 
+
 class lh__Reader : public ASDCP::h__Reader
 {
   RGBAEssenceDescriptor*        m_EssenceDescriptor;
   JPEG2000PictureSubDescriptor* m_EssenceSubDescriptor;
   ASDCP::Rational               m_EditRate;
+  ASDCP::Rational               m_SampleRate;
   EssenceType_t                 m_Format;
 
   ASDCP_NO_COPY_CONSTRUCT(lh__Reader);
@@ -146,7 +218,8 @@ class lh__Reader : public ASDCP::h__Reader
 public:
   PictureDescriptor m_PDesc;        // codestream parameter list
 
-  lh__Reader() : m_EssenceDescriptor(0), m_EssenceSubDescriptor(0), m_Format(ESS_UNKNOWN) {}
+  lh__Reader(const Dictionary& d) :
+    ASDCP::h__Reader(d), m_EssenceDescriptor(0), m_EssenceSubDescriptor(0), m_Format(ESS_UNKNOWN) {}
   Result_t    OpenRead(const char*, EssenceType_t);
   Result_t    ReadFrame(ui32_t, JP2K::FrameBuffer&, AESDecContext*, HMACContext*);
   Result_t    MD_to_JP2K_PDesc(JP2K::PictureDescriptor& PDesc);
@@ -160,6 +233,7 @@ lh__Reader::MD_to_JP2K_PDesc(JP2K::PictureDescriptor& PDesc)
   MXF::RGBAEssenceDescriptor* PDescObj = (MXF::RGBAEssenceDescriptor*)m_EssenceDescriptor;
 
   PDesc.EditRate           = m_EditRate;
+  PDesc.SampleRate         = m_SampleRate;
   assert(PDescObj->ContainerDuration <= 0xFFFFFFFFL);
   PDesc.ContainerDuration  = (ui32_t) PDescObj->ContainerDuration;
   PDesc.StoredWidth        = PDescObj->StoredWidth;
@@ -189,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;
@@ -232,17 +306,23 @@ lh__Reader::OpenRead(const char* filename, EssenceType_t type)
        }
 
       m_EditRate = ((Track*)ObjectList.front())->EditRate;
+      m_SampleRate = m_EssenceDescriptor->SampleRate;
 
       if ( type == ASDCP::ESS_JPEG_2000 )
        {
-         if ( m_EditRate != m_EssenceDescriptor->SampleRate )
+         if ( m_EditRate != m_SampleRate )
            {
-             DefaultLogSink().Error("EditRate and SampleRate do not match (%.03f, %.03f).\n",
-                                    m_EditRate.Quotient(), m_EssenceDescriptor->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_EssenceDescriptor->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().Error("File may contain JPEG Interop stereoscopic images.\n");
+                 DefaultLogSink().Debug("File may contain JPEG Interop stereoscopic images.\n");
                  return RESULT_SFORMAT;
                }
 
@@ -251,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_EssenceDescriptor->SampleRate == EditRate_48 ) )
+         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
            {
-             DefaultLogSink().Error("EditRate and SampleRate not correct for 24/48 stereoscopic essence.\n");
+             DefaultLogSink().Error("EditRate not correct for stereoscopic essence: %d/%d.\n",
+                                    m_EditRate.Numerator, m_EditRate.Denominator);
              return RESULT_FORMAT;
            }
        }
@@ -284,13 +413,19 @@ lh__Reader::ReadFrame(ui32_t FrameNum, JP2K::FrameBuffer& FrameBuf,
   if ( ! m_File.IsOpen() )
     return RESULT_INIT;
 
-  return ReadEKLVFrame(FrameNum, FrameBuf, Dict::ul(MDD_JPEG2000Essence), Ctx, HMAC);
+  assert(m_Dict);
+  return ReadEKLVFrame(FrameNum, FrameBuf, m_Dict->ul(MDD_JPEG2000Essence), Ctx, HMAC);
 }
 
 
 //
 class ASDCP::JP2K::MXFReader::h__Reader : public lh__Reader
 {
+  ASDCP_NO_COPY_CONSTRUCT(h__Reader);
+  h__Reader();
+
+public:
+  h__Reader(const Dictionary& d) : lh__Reader(d) {}
 };
 
 
@@ -318,7 +453,7 @@ ASDCP::JP2K::FrameBuffer::Dump(FILE* stream, ui32_t dump_len) const
 
 ASDCP::JP2K::MXFReader::MXFReader()
 {
-  m_Reader = new h__Reader;
+  m_Reader = new h__Reader(DefaultCompositeDict());
 }
 
 
@@ -326,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
@@ -392,15 +557,29 @@ 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;
+}
+
 
 //------------------------------------------------------------------------------------------
 
+
 class ASDCP::JP2K::MXFSReader::h__SReader : public lh__Reader
 {
   ui32_t m_StereoFrameReady;
 
 public:
-  h__SReader() : m_StereoFrameReady(0xffffffff) {}
+  h__SReader(const Dictionary& d) : lh__Reader(d), m_StereoFrameReady(0xffffffff) {}
 
   //
   Result_t ReadFrame(ui32_t FrameNum, StereoscopicPhase_t phase, FrameBuffer& FrameBuf,
@@ -466,7 +645,8 @@ public:
       {
        ui32_t SequenceNum = FrameNum * 2;
        SequenceNum += ( phase == SP_RIGHT ) ? 2 : 1;
-       result = ReadEKLVPacket(FrameNum, SequenceNum, FrameBuf, Dict::ul(MDD_JPEG2000Essence), Ctx, HMAC);
+       assert(m_Dict);
+       result = ReadEKLVPacket(FrameNum, SequenceNum, FrameBuf, m_Dict->ul(MDD_JPEG2000Essence), Ctx, HMAC);
       }
 
     return result;
@@ -477,7 +657,7 @@ public:
 
 ASDCP::JP2K::MXFSReader::MXFSReader()
 {
-  m_Reader = new h__SReader;
+  m_Reader = new h__SReader(DefaultCompositeDict());
 }
 
 
@@ -485,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
@@ -567,21 +777,36 @@ 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;
+}
+
+
 //------------------------------------------------------------------------------------------
 
 
 //
 class lh__Writer : public ASDCP::h__Writer
 {
+  ASDCP_NO_COPY_CONSTRUCT(lh__Writer);
+  lh__Writer();
+
   JPEG2000PictureSubDescriptor* m_EssenceSubDescriptor;
 
 public:
   PictureDescriptor m_PDesc;
   byte_t            m_EssenceUL[SMPTE_UL_LENGTH];
 
-  ASDCP_NO_COPY_CONSTRUCT(lh__Writer);
-
-  lh__Writer() : m_EssenceSubDescriptor(0) {
+  lh__Writer(const Dictionary& d) : ASDCP::h__Writer(d), m_EssenceSubDescriptor(0) {
     memset(m_EssenceUL, 0, SMPTE_UL_LENGTH);
   }
 
@@ -624,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(Dict::ul(MDD_JP2KEssenceCompression_2K));
+      PDescObj->PictureEssenceCoding.Set(m_Dict->ul(MDD_JP2KEssenceCompression_2K));
       m_EssenceSubDescriptor->Rsize = 3;
     }
   else
     {
-      PDescObj->PictureEssenceCoding.Set(Dict::ul(MDD_JP2KEssenceCompression_4K));
+      PDescObj->PictureEssenceCoding.Set(m_Dict->ul(MDD_JP2KEssenceCompression_4K));
       m_EssenceSubDescriptor->Rsize = 4;
     }
 
@@ -685,12 +911,12 @@ lh__Writer::OpenWrite(const char* filename, EssenceType_t type, ui32_t HeaderSiz
   if ( ASDCP_SUCCESS(result) )
     {
       m_HeaderSize = HeaderSize;
-      RGBAEssenceDescriptor* tmp_rgba = new RGBAEssenceDescriptor;
+      RGBAEssenceDescriptor* tmp_rgba = new RGBAEssenceDescriptor(m_Dict);
       tmp_rgba->ComponentMaxRef = 4095;
       tmp_rgba->ComponentMinRef = 0;
 
       m_EssenceDescriptor = tmp_rgba;
-      m_EssenceSubDescriptor = new JPEG2000PictureSubDescriptor;
+      m_EssenceSubDescriptor = new JPEG2000PictureSubDescriptor(m_Dict);
       m_EssenceSubDescriptorList.push_back((InterchangeObject*)m_EssenceSubDescriptor);
 
       GenRandomValue(m_EssenceSubDescriptor->InstanceUID);
@@ -698,7 +924,7 @@ lh__Writer::OpenWrite(const char* filename, EssenceType_t type, ui32_t HeaderSiz
 
       if ( type == ASDCP::ESS_JPEG_2000_S && m_Info.LabelSetType == LS_MXF_SMPTE )
        {
-         InterchangeObject* StereoSubDesc = new StereoscopicPictureSubDescriptor;
+         InterchangeObject* StereoSubDesc = new StereoscopicPictureSubDescriptor(m_Dict);
          m_EssenceSubDescriptorList.push_back(StereoSubDesc);
          GenRandomValue(StereoSubDesc->InstanceUID);
          m_EssenceDescriptor->SubDescriptors.push_back(StereoSubDesc->InstanceUID);
@@ -714,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;
 
@@ -723,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(Dict::ul(MDD_JPEG_2000Wrapping)),
-                             PICT_DEF_LABEL,     UL(Dict::ul(MDD_PictureDataDef)),
-                             LocalEditRate, 24 /* TCFrameRate */);
-
   if ( ASDCP_SUCCESS(result) )
     {
-      memcpy(m_EssenceUL, 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;
 }
 
@@ -786,6 +1017,11 @@ lh__Writer::Finalize()
 //
 class ASDCP::JP2K::MXFWriter::h__Writer : public lh__Writer
 {
+  ASDCP_NO_COPY_CONSTRUCT(h__Writer);
+  h__Writer();
+
+public:
+  h__Writer(const Dictionary& d) : lh__Writer(d) {}
 };
 
 
@@ -801,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.
@@ -808,7 +1073,11 @@ ASDCP::Result_t
 ASDCP::JP2K::MXFWriter::OpenWrite(const char* filename, const WriterInfo& Info,
                                  const PictureDescriptor& PDesc, 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, ASDCP::ESS_JPEG_2000, HeaderSize);
@@ -853,10 +1122,12 @@ ASDCP::JP2K::MXFWriter::Finalize()
 //
 class ASDCP::JP2K::MXFSWriter::h__SWriter : public lh__Writer
 {
+  ASDCP_NO_COPY_CONSTRUCT(h__SWriter);
+  h__SWriter();
   StereoscopicPhase_t m_NextPhase;
 
 public:
-  h__SWriter() : m_NextPhase(SP_LEFT) {}
+  h__SWriter(const Dictionary& d) : lh__Writer(d), m_NextPhase(SP_LEFT) {}
 
   //
   Result_t WriteFrame(const FrameBuffer& FrameBuf, StereoscopicPhase_t phase,
@@ -897,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.
@@ -904,11 +1204,19 @@ ASDCP::Result_t
 ASDCP::JP2K::MXFSWriter::OpenWrite(const char* filename, const WriterInfo& Info,
                                   const PictureDescriptor& PDesc, ui32_t HeaderSize)
 {
-  m_Writer = new h__SWriter;
-  
-  if ( PDesc.EditRate != ASDCP::EditRate_24 )
+  if ( Info.LabelSetType == LS_MXF_SMPTE )
+    m_Writer = new h__SWriter(DefaultSMPTEDict());
+  else
+    m_Writer = new h__SWriter(DefaultInteropDict());
+
+  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;
     }
 
@@ -922,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) )