Add support for hashing mono picture MXF writes on the way out.
authorCarl Hetherington <cth@carlh.net>
Mon, 28 Jan 2013 00:11:30 +0000 (00:11 +0000)
committerCarl Hetherington <cth@carlh.net>
Mon, 28 Jan 2013 00:11:30 +0000 (00:11 +0000)
asdcplib/src/AS_DCP.h
asdcplib/src/AS_DCP_JP2K.cpp
asdcplib/src/AS_DCP_internal.h
asdcplib/src/KM_fileio.cpp
asdcplib/src/KM_fileio.h
asdcplib/src/h__Writer.cpp
src/picture_asset.cc
src/picture_asset.h

index 3d679413f912c30945168ddb3c56c8ce912289d7..d5675586d39c3de50dcade3e4e43bc7d3303db6c 100755 (executable)
@@ -87,6 +87,7 @@ This project depends upon the following libraries:
 #define _AS_DCP_H_
 
 #include <KM_error.h>
+#include <KM_platform.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <math.h>
@@ -1172,12 +1173,16 @@ namespace ASDCP {
 
          // Writes a frame of essence to the MXF file. If the optional AESEncContext
          // argument is present, the essence is encrypted prior to writing.
+         // A MD5 hash of the data that we write is written to hash if it is not 0
          // Fails if the file is not open, is finalized, or an operating system
          // error occurs.
-         Result_t WriteFrame(const FrameBuffer&, AESEncContext* = 0, HMACContext* = 0);
+         Result_t WriteFrame(const FrameBuffer&, AESEncContext* = 0, HMACContext* = 0, std::string* hash = 0);
 
          // 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;
        };
 
       //
index ca6d52705a86f41ea422d9bcfaf362fb45abeebd..359cd910635a7e55fe57c5cd03dc79f7bf65adb8 100755 (executable)
@@ -815,7 +815,7 @@ public:
   Result_t OpenWrite(const char*, 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 JP2K::FrameBuffer&, bool add_index, AESEncContext*, HMACContext*);
+  Result_t WriteFrame(const JP2K::FrameBuffer&, bool add_index, AESEncContext*, HMACContext*, std::string* hash = 0);
   Result_t Finalize();
   Result_t JP2K_PDesc_to_MD(JP2K::PictureDescriptor& PDesc);
 };
@@ -978,7 +978,7 @@ lh__Writer::SetSourceStream(const PictureDescriptor& PDesc, const std::string& l
 //
 ASDCP::Result_t
 lh__Writer::WriteFrame(const JP2K::FrameBuffer& FrameBuf, bool add_index,
-                      AESEncContext* Ctx, HMACContext* HMAC)
+                      AESEncContext* Ctx, HMACContext* HMAC, std::string* hash)
 {
   Result_t result = RESULT_OK;
 
@@ -988,7 +988,7 @@ lh__Writer::WriteFrame(const JP2K::FrameBuffer& FrameBuf, bool add_index,
   ui64_t StreamOffset = m_StreamOffset;
 
   if ( ASDCP_SUCCESS(result) )
-    result = WriteEKLVPacket(FrameBuf, m_EssenceUL, Ctx, HMAC);
+    result = WriteEKLVPacket(FrameBuf, m_EssenceUL, Ctx, HMAC, hash);
 
   if ( ASDCP_SUCCESS(result) && add_index )
     {  
@@ -1099,12 +1099,12 @@ ASDCP::JP2K::MXFWriter::OpenWrite(const char* filename, const WriterInfo& Info,
 // Fails if the file is not open, is finalized, or an operating system
 // error occurs.
 ASDCP::Result_t
-ASDCP::JP2K::MXFWriter::WriteFrame(const FrameBuffer& FrameBuf, AESEncContext* Ctx, HMACContext* HMAC)
+ASDCP::JP2K::MXFWriter::WriteFrame(const FrameBuffer& FrameBuf, AESEncContext* Ctx, HMACContext* HMAC, std::string* hash)
 {
   if ( m_Writer.empty() )
     return RESULT_INIT;
 
-  return m_Writer->WriteFrame(FrameBuf, true, Ctx, HMAC);
+  return m_Writer->WriteFrame(FrameBuf, true, Ctx, HMAC, hash);
 }
 
 // Closes the MXF file, writing the index and other closing information.
@@ -1117,6 +1117,12 @@ ASDCP::JP2K::MXFWriter::Finalize()
   return m_Writer->Finalize();
 }
 
+ui64_t
+ASDCP::JP2K::MXFWriter::Tell() const
+{
+       return m_Writer->m_File.Tell();
+}
+
 
 //------------------------------------------------------------------------------------------
 //
index 4c1507c878171a0f72eb293149a7efb74acb53ef..15b5282260ba7425451ccc0016f27e361e7a95cb 100755 (executable)
@@ -237,7 +237,7 @@ namespace ASDCP
                              ui32_t TCFrameRate, ui32_t BytesPerEditUnit = 0);
 
       Result_t WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,
-                              const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext* HMAC);
+                              const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext* HMAC, std::string* hash = 0);
 
       Result_t WriteMXFFooter();
 
index 2914f9825686df9bd20b67d05483df7720512086..138bc3b4cfc1d8b813a26909787f37b01157ce81 100644 (file)
@@ -32,6 +32,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <KM_fileio.h>
 #include <KM_log.h>
 #include <fcntl.h>
+#include <sstream>
+#include <iomanip>
 
 #include <assert.h>
 
@@ -49,7 +51,6 @@ using namespace Kumu;
 typedef struct _stati64 fstat_t;
 #define S_IFLNK 0
 
-
 // win32 has WriteFileGather() and ReadFileScatter() but they
 // demand page alignment and page sizing, making them unsuitable
 // for use with arbitrary buffer sizes.
@@ -614,7 +615,10 @@ Kumu::FileReader::Size() const
 
 // these are declared here instead of in the header file
 // because we have a mem_ptr that is managing a hidden class
-Kumu::FileWriter::FileWriter() {}
+Kumu::FileWriter::FileWriter()
+       : m_Hashing (false)
+{}
+
 Kumu::FileWriter::~FileWriter() {}
 
 //
@@ -639,6 +643,37 @@ Kumu::FileWriter::Writev(const byte_t* buf, ui32_t buf_len)
   return RESULT_OK;
 }
 
+void
+Kumu::FileWriter::StartHashing()
+{
+       m_Hashing = true;
+       MD5_Init (&m_MD5Context);
+}
+
+void
+Kumu::FileWriter::MaybeHash(void const * data, int size)
+{
+       if (m_Hashing) {
+               MD5_Update (&m_MD5Context, data, size);
+       }
+}
+
+std::string
+Kumu::FileWriter::StopHashing()
+{
+       m_Hashing = false;
+       
+       unsigned char digest[MD5_DIGEST_LENGTH];
+       MD5_Final (digest, &m_MD5Context);
+
+       std::stringstream s;
+       for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
+               s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
+       }
+
+       return s.str ();
+}
+
 
 #ifdef KM_WIN32
 //------------------------------------------------------------------------------------------
@@ -830,6 +865,7 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
          break;
        }
 
+      MaybeHash (iov->m_iovec[i].iov_base, iov->m_iovec[i].iov_len);
       *bytes_written += tmp_count;
     }
 
@@ -860,6 +896,8 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written
   if ( result == 0 || *bytes_written != buf_len )
     return Kumu::RESULT_WRITEFAIL;
 
+  MaybeHash (buf, buf_len);
+  
   return Kumu::RESULT_OK;
 }
 
@@ -1006,6 +1044,10 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
   if ( write_size == -1L || write_size != total_size )
     return RESULT_WRITEFAIL;
 
+  for (int i = 0; i < iov->m_Count; ++i) {
+         MaybeHash (iov->m_iovec[i].iov_base, iov->m_iovec[i].iov_len);
+  }
+
   iov->m_Count = 0;
   *bytes_written = write_size;  
   return RESULT_OK;
@@ -1025,6 +1067,7 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written
     return RESULT_STATE;
 
   int write_size = write(m_Handle, buf, buf_len);
+  MaybeHash (buf, buf_len);
 
   if ( write_size == -1L || (ui32_t)write_size != buf_len )
     return RESULT_WRITEFAIL;
@@ -1034,7 +1077,7 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written
 }
 
 
-#endif // KM_WIN32
+#endif
 
 //------------------------------------------------------------------------------------------
 
index b078e32be01bec6140504eb31df42c7174757b24..cb00acc8d82ad52f50bc23b2b28977cadf265adf 100755 (executable)
@@ -35,6 +35,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <KM_util.h>
 #include <string>
 #include <boost/filesystem.hpp>
+#include <openssl/md5.h>
 
 #ifdef KM_WIN32
 # include <io.h>
@@ -298,6 +299,8 @@ namespace Kumu
       class h__iovec;
       mem_ptr<h__iovec>  m_IOVec;
       KM_NO_COPY_CONSTRUCT(FileWriter);
+      bool m_Hashing;
+      MD5_CTX m_MD5Context;
 
     public:
       FileWriter();
@@ -317,6 +320,10 @@ namespace Kumu
       // the iovec list will be written to disk before the given buffer,as though
       // you had called Writev() first.
       Result_t Write(const byte_t*, ui32_t, ui32_t* = 0);            // write buffer to disk
+
+      void StartHashing();
+      void MaybeHash(void const *, int);
+      std::string StopHashing();
    };
 
   Result_t CreateDirectoriesInPath(const std::string& Path);
index 5d3a1d5ab6a8a393ea0a1fbc55c14d55269fcafc..662e0f82eabd56979d198e798b4437c43a2e7969 100755 (executable)
@@ -499,11 +499,13 @@ ASDCP::h__Writer::WriteMXFHeader(const std::string& PackageLabel, const UL& Wrap
 // standard method of writing a plaintext or encrypted frame
 Result_t
 ASDCP::h__Writer::WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf, const byte_t* EssenceUL,
-                                 AESEncContext* Ctx, HMACContext* HMAC)
+                                 AESEncContext* Ctx, HMACContext* HMAC, std::string* hash)
 {
   Result_t result = RESULT_OK;
   IntegrityPack IntPack;
 
+  m_File.StartHashing();
+
   byte_t overhead[128];
   Kumu::MemIOWriter Overhead(overhead, 128);
   assert(m_Dict);
@@ -635,6 +637,10 @@ ASDCP::h__Writer::WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf, const byte
   if ( ASDCP_SUCCESS(result) )
     result = m_File.Writev();
 
+  if (hash) {
+         *hash = m_File.StopHashing();
+  }
+
   return result;
 }
 
index cd6dd4dc9b125ab14d260ab7029be02f63f84dda..d242777c2915f7a5bbda4e14d066b1a5ae82c0c1 100644 (file)
@@ -41,6 +41,9 @@ using std::ostream;
 using std::list;
 using std::vector;
 using std::max;
+using std::pair;
+using std::make_pair;
+using std::istream;
 using boost::shared_ptr;
 using boost::dynamic_pointer_cast;
 using boost::lexical_cast;
@@ -216,7 +219,6 @@ MonoPictureAsset::construct (boost::function<string (int)> get_path)
                        throw FileError ("could not open JPEG2000 file for reading", path);
                }
 
-               /* XXX: passing 0 to WriteFrame ok? */
                if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
                        throw MiscError ("error in writing video MXF");
                }
@@ -240,7 +242,7 @@ MonoPictureAsset::path_from_list (int f, vector<string> const & files) const
 shared_ptr<const MonoPictureFrame>
 MonoPictureAsset::get_frame (int n) const
 {
-       return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path().string(), n + _entry_point));
+       return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path().string(), n));
 }
 
 
@@ -390,7 +392,7 @@ StereoPictureAsset::StereoPictureAsset (string directory, string mxf_name, int f
 shared_ptr<const StereoPictureFrame>
 StereoPictureAsset::get_frame (int n) const
 {
-       return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n + _entry_point));
+       return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n));
 }
 
 shared_ptr<MonoPictureAssetWriter>
@@ -400,6 +402,17 @@ MonoPictureAsset::start_write ()
        return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this));
 }
 
+FrameInfo::FrameInfo (istream& s)
+{
+       s >> offset >> length >> hash;
+}
+
+void
+FrameInfo::write (ostream& s)
+{
+       s << offset << " " << length << " " << hash;
+}
+
 struct MonoPictureAssetWriter::ASDCPState
 {
        ASDCPState()
@@ -426,7 +439,7 @@ MonoPictureAssetWriter::MonoPictureAssetWriter (MonoPictureAsset* a)
 
 }
 
-void
+FrameInfo
 MonoPictureAssetWriter::write (uint8_t* data, int size)
 {
        assert (!_finalized);
@@ -448,11 +461,15 @@ MonoPictureAssetWriter::write (uint8_t* data, int size)
                }
        }
 
-       if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, 0, 0))) {
+       uint64_t const before_offset = _state->mxf_writer.Tell ();
+
+       string hash;
+       if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, 0, 0, &hash))) {
                throw MiscError ("error in writing video MXF");
        }
 
        _frames_written++;
+       return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
 }
 
 void
index 71900200ac52b15ccc6b8867ec1fad1438cfbd95..4e9e1dd7dccaf943acd8d85fa515fc90da0525d7 100644 (file)
@@ -17,6 +17,9 @@
 
 */
 
+#ifndef LIBDCP_PICTURE_ASSET_H
+#define LIBDCP_PICTURE_ASSET_H
+
 /** @file  src/picture_asset.h
  *  @brief An asset made up of JPEG2000 files
  */
@@ -79,6 +82,23 @@ protected:
 
 class MonoPictureAsset;
 
+struct FrameInfo
+{
+       FrameInfo (uint64_t o, uint64_t l, std::string h)
+               : offset (o)
+               , length (l)
+               , hash (h)
+       {}
+
+       FrameInfo (std::istream& s);
+
+       void write (std::ostream& s);
+       
+       uint64_t offset;
+       uint64_t length;
+       std::string hash;
+};
+
 /** A helper class for writing to MonoPictureAssets progressively (i.e. writing frame-by-frame,
  *  rather than giving libdcp all the frames in one go).
  *
@@ -94,7 +114,7 @@ class MonoPictureAssetWriter
 public:
        ~MonoPictureAssetWriter ();
 
-       void write (uint8_t* data, int size);
+       FrameInfo write (uint8_t* data, int size);
        void finalize ();
 
 private:
@@ -205,3 +225,5 @@ public:
        
 
 }
+
+#endif