Allow overwrite (i.e. continue) when writing JPEG2000 MXFs.
authorCarl Hetherington <cth@carlh.net>
Thu, 21 Mar 2024 19:29:47 +0000 (20:29 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 21 Mar 2024 19:29:47 +0000 (20:29 +0100)
src/AS_DCP.h
src/AS_DCP_JP2K.cpp
src/AS_DCP_internal.h
src/KM_fileio.cpp
src/h__Writer.cpp

index 8a87bf29a5bccc3b9bbd434d32b71d426f010d88..dac5cd1e862ba14cccc622b01ce00fb93cd177fa 100755 (executable)
@@ -1262,11 +1262,11 @@ namespace ASDCP {
          virtual MXF::OPAtomIndexFooter& OPAtomIndexFooter();
          virtual MXF::RIP& RIP();
 
-         // Open the file for writing. The file must not exist. Returns error if
+         // Open the file for writing. The file must not exist unless overwrite is true. 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);
+                            const PictureDescriptor&, ui32_t HeaderSize = 16384, bool overwrite = false);
 
          // Writes a frame of essence to the MXF file. If the optional AESEncContext
          // argument is present, the essence is encrypted prior to writing.
@@ -1274,8 +1274,13 @@ namespace ASDCP {
          // error occurs.
          Result_t WriteFrame(const FrameBuffer&, AESEncContext* = 0, HMACContext* = 0);
 
+         Result_t FakeWriteFrame(int size);
+
          // Closes the MXF file, writing the index and revised header.
          Result_t Finalize();
+
+         // Return the current file offset in the MXF file that we are writing.
+         ui64_t Tell() const;
        };
 
       //
@@ -1367,11 +1372,11 @@ namespace ASDCP {
          virtual MXF::OPAtomIndexFooter& OPAtomIndexFooter();
          virtual MXF::RIP& RIP();
 
-         // Open the file for writing. The file must not exist. Returns error if
+         // Open the file for writing. The file must not exist unless overwrite is true. 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);
+                            const PictureDescriptor&, ui32_t HeaderSize = 16384, bool overwrite = false);
 
          // Writes a pair of frames of essence to the MXF file. If the optional AESEncContext
          // argument is present, the essence is encrypted prior to writing.
@@ -1388,9 +1393,14 @@ namespace ASDCP {
          Result_t WriteFrame(const FrameBuffer&, StereoscopicPhase_t phase,
                              AESEncContext* = 0, HMACContext* = 0);
 
+         Result_t FakeWriteFrame(int size, StereoscopicPhase_t phase);
+
          // Closes the MXF file, writing the index and revised header.  Returns
          // RESULT_SPHASE if WriteFrame was called an odd number of times.
          Result_t Finalize();
+
+         // Return the current file offset in the MXF file that we are writing
+         ui64_t Tell() const;
        };
 
       //
index 87961c34668ce098cd76c7509aea3b4a047e9d10..5e7d7a341acf7fbf6eb3ba8de7095a2b7fe449ae 100755 (executable)
@@ -1158,24 +1158,34 @@ public:
 
   virtual ~lh__Writer(){}
 
-  Result_t OpenWrite(const std::string&, EssenceType_t type, ui32_t HeaderSize);
+  Result_t OpenWrite(const std::string&, EssenceType_t type, ui32_t HeaderSize, bool);
   Result_t SetSourceStream(const PictureDescriptor&, const std::string& label,
                           ASDCP::Rational LocalEditRate = ASDCP::Rational(0,0));
   Result_t WriteFrame(const JP2K::FrameBuffer&, bool add_index, AESEncContext*, HMACContext*);
+  Result_t FakeWriteFrame(int size, bool add_index);
   Result_t Finalize();
 };
 } // namespace JP2K
 } // namespace asdcp
 
-// Open the file for writing. The file must not exist. Returns error if
+// Open the file for writing. The file must not exist unless overwrite is true. Returns error if
 // the operation cannot be completed.
 ASDCP::Result_t
-lh__Writer::OpenWrite(const std::string& filename, EssenceType_t type, ui32_t HeaderSize)
+lh__Writer::OpenWrite(const std::string& filename, EssenceType_t type, ui32_t HeaderSize, bool overwrite)
 {
   if ( ! m_State.Test_BEGIN() )
     return RESULT_STATE;
 
-  Result_t result = m_File.OpenWrite(filename);
+  Result_t result = RESULT_OK;
+  if (overwrite)
+    {
+      result = m_File.OpenModify(filename);
+      m_File.Seek(0);
+    }
+  else
+    {
+      result = m_File.OpenWrite(filename);
+    }
 
   if ( ASDCP_SUCCESS(result) )
     {
@@ -1283,6 +1293,31 @@ lh__Writer::WriteFrame(const JP2K::FrameBuffer& FrameBuf, bool add_index,
 }
 
 
+Result_t
+lh__Writer::FakeWriteFrame(int size, bool add_index)
+{
+  Result_t result = RESULT_OK;
+
+  if ( m_State.Test_READY() )
+    result = m_State.Goto_RUNNING();
+
+  ui64_t StreamOffset = m_StreamOffset;
+
+  if ( ASDCP_SUCCESS(result) )
+    result = FakeWriteEKLVPacket(size);
+
+  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
@@ -1365,11 +1400,11 @@ ASDCP::JP2K::MXFWriter::RIP()
   return m_Writer->m_RIP;
 }
 
-// Open the file for writing. The file must not exist. Returns error if
+// Open the file for writing. The file must not exist unless overwrite is true. Returns error if
 // the operation cannot be completed.
 ASDCP::Result_t
 ASDCP::JP2K::MXFWriter::OpenWrite(const std::string& filename, const WriterInfo& Info,
-                                 const PictureDescriptor& PDesc, ui32_t HeaderSize)
+                                 const PictureDescriptor& PDesc, ui32_t HeaderSize, bool overwrite)
 {
   if ( Info.LabelSetType == LS_MXF_SMPTE )
     m_Writer = new h__Writer(&DefaultSMPTEDict());
@@ -1378,7 +1413,7 @@ ASDCP::JP2K::MXFWriter::OpenWrite(const std::string& filename, const WriterInfo&
 
   m_Writer->m_Info = Info;
 
-  Result_t result = m_Writer->OpenWrite(filename, ASDCP::ESS_JPEG_2000, HeaderSize);
+  Result_t result = m_Writer->OpenWrite(filename, ASDCP::ESS_JPEG_2000, HeaderSize, overwrite);
 
   if ( ASDCP_SUCCESS(result) )
     result = m_Writer->SetSourceStream(PDesc, JP2K_PACKAGE_LABEL);
@@ -1403,6 +1438,15 @@ ASDCP::JP2K::MXFWriter::WriteFrame(const FrameBuffer& FrameBuf, AESEncContext* C
   return m_Writer->WriteFrame(FrameBuf, true, Ctx, HMAC);
 }
 
+ASDCP::Result_t
+ASDCP::JP2K::MXFWriter::FakeWriteFrame(int size)
+{
+  if ( m_Writer.empty() )
+    return RESULT_INIT;
+
+  return m_Writer->FakeWriteFrame(size, true);
+}
+
 // Closes the MXF file, writing the index and other closing information.
 ASDCP::Result_t
 ASDCP::JP2K::MXFWriter::Finalize()
@@ -1413,6 +1457,11 @@ ASDCP::JP2K::MXFWriter::Finalize()
   return m_Writer->Finalize();
 }
 
+ui64_t
+ASDCP::JP2K::MXFWriter::Tell() const
+{
+  return m_Writer->m_File.TellPosition();
+}
 
 //------------------------------------------------------------------------------------------
 //
@@ -1444,6 +1493,21 @@ public:
     return lh__Writer::WriteFrame(FrameBuf, false, Ctx, HMAC);
   }
 
+  Result_t FakeWriteFrame(int size, StereoscopicPhase_t phase)
+  {
+    if ( m_NextPhase != phase )
+      return RESULT_SPHASE;
+
+    if ( phase == SP_LEFT )
+      {
+       m_NextPhase = SP_RIGHT;
+       return lh__Writer::FakeWriteFrame(size, true);
+      }
+
+    m_NextPhase = SP_LEFT;
+    return lh__Writer::FakeWriteFrame(size, false);
+  }
+
   //
   Result_t Finalize()
   {
@@ -1515,7 +1579,7 @@ ASDCP::JP2K::MXFSWriter::RIP()
 // the operation cannot be completed.
 ASDCP::Result_t
 ASDCP::JP2K::MXFSWriter::OpenWrite(const std::string& filename, const WriterInfo& Info,
-                                  const PictureDescriptor& PDesc, ui32_t HeaderSize)
+                                  const PictureDescriptor& PDesc, ui32_t HeaderSize, bool overwrite)
 {
   if ( Info.LabelSetType == LS_MXF_SMPTE )
     m_Writer = new h__SWriter(&DefaultSMPTEDict());
@@ -1541,7 +1605,7 @@ ASDCP::JP2K::MXFSWriter::OpenWrite(const std::string& filename, const WriterInfo
 
   m_Writer->m_Info = Info;
 
-  Result_t result = m_Writer->OpenWrite(filename, ASDCP::ESS_JPEG_2000_S, HeaderSize);
+  Result_t result = m_Writer->OpenWrite(filename, ASDCP::ESS_JPEG_2000_S, HeaderSize, overwrite);
 
   if ( ASDCP_SUCCESS(result) )
     {
@@ -1611,6 +1675,15 @@ ASDCP::JP2K::MXFSWriter::WriteFrame(const FrameBuffer& FrameBuf, StereoscopicPha
   return m_Writer->WriteFrame(FrameBuf, phase, Ctx, HMAC);
 }
 
+ASDCP::Result_t
+ASDCP::JP2K::MXFSWriter::FakeWriteFrame(int size, StereoscopicPhase_t phase)
+{
+  if ( m_Writer.empty() )
+    return RESULT_INIT;
+
+  return m_Writer->FakeWriteFrame(size, phase);
+}
+
 // Closes the MXF file, writing the index and other closing information.
 ASDCP::Result_t
 ASDCP::JP2K::MXFSWriter::Finalize()
@@ -1621,6 +1694,12 @@ ASDCP::JP2K::MXFSWriter::Finalize()
   return m_Writer->Finalize();
 }
 
+ui64_t
+ASDCP::JP2K::MXFSWriter::Tell() const
+{
+  return m_Writer->m_File.TellPosition();
+}
+
 //
 // end AS_DCP_JP2K.cpp
 //
index 6d8688fbcee2cf39960b781691fa848b9b0ea285..31c141c9e70ab862afbecdcb8b4e17b77ab1d39c 100755 (executable)
@@ -966,6 +966,7 @@ extern MXF::RIP *g_RIP;
       Result_t WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,const byte_t* EssenceUL,
                               const ui32_t& MinEssenceElementBerLength,
                               AESEncContext* Ctx, HMACContext* HMAC);
+      Result_t FakeWriteEKLVPacket(int size);
       Result_t WriteASDCPFooter();
     };
 
index 2d3fd2781381ac79580c4a030f25fcd9070e2682..3ccb3f69a16d408b8007550a676bf55348860173 100644 (file)
@@ -1010,6 +1010,48 @@ Kumu::FileWriter::OpenWrite(const std::string& filename)
   return Kumu::RESULT_OK;
 }
 
+
+/** @param filename File name (UTF-8 encoded) */
+Kumu::Result_t
+Kumu::FileWriter::OpenModify(const std::string& filename)
+{
+  m_Filename = filename;
+
+  // suppress popup window on error
+  UINT prev = ::SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
+
+  int const wn = MultiByteToWideChar(CP_UTF8, 0, filename.c_str(), -1, 0, 0);
+  wchar_t* buffer = new wchar_t[wn];
+  if (MultiByteToWideChar(CP_UTF8, 0, filename.c_str(), -1, buffer, wn) == 0) {
+    delete[] buffer;
+    return Kumu::RESULT_FAIL;
+  }
+
+  m_Handle = ::CreateFileW(buffer,
+                         (GENERIC_WRITE|GENERIC_READ),  // open for reading
+                         FILE_SHARE_READ,               // share for reading
+                         NULL,                          // no security
+                         OPEN_ALWAYS,                   // don't truncate existing
+                         FILE_ATTRIBUTE_NORMAL,         // normal file
+                         NULL                           // no template file
+                         );
+
+  delete[] buffer;
+  HRESULT const last_error = GetLastError();
+
+  ::SetErrorMode(prev);
+
+  if (m_Handle == INVALID_HANDLE_VALUE)
+    {
+      DefaultLogSink().Error("CreateFileW failed: %lu\n", last_error);
+      return Kumu::RESULT_FILEOPEN;
+    }
+
+  m_IOVec = new h__iovec;
+  return Kumu::RESULT_OK;
+}
+
+
 //
 Kumu::Result_t
 Kumu::FileWriter::Writev(ui32_t* bytes_written)
index b36af6428ed646b306e309388a376552be673c9d..c7aa8ab792d48149a5339ce61d41c8617e1f30de 100755 (executable)
@@ -327,6 +327,15 @@ ASDCP::h__ASDCPWriter::WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,const
                           Ctx, HMAC);
 }
 
+Result_t
+ASDCP::h__ASDCPWriter::FakeWriteEKLVPacket(int size)
+{
+  m_StreamOffset += size;
+  m_File.Seek(size, Kumu::SP_POS);
+
+  return RESULT_OK;
+}
+
 // standard method of writing the header and footer of a completed MXF file
 //
 Result_t