summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2013-01-28 00:11:30 +0000
committerCarl Hetherington <cth@carlh.net>2013-01-28 00:11:30 +0000
commita246eb45b34ebc6bf277694b295f693706be8c6a (patch)
treebb5d78210f036eaddd8ad66af1325bc3d6636fcb
parent103c20d48c22f0c604e402de41bce7336ef9b386 (diff)
Add support for hashing mono picture MXF writes on the way out.
-rwxr-xr-xasdcplib/src/AS_DCP.h7
-rwxr-xr-xasdcplib/src/AS_DCP_JP2K.cpp16
-rwxr-xr-xasdcplib/src/AS_DCP_internal.h2
-rw-r--r--asdcplib/src/KM_fileio.cpp49
-rwxr-xr-xasdcplib/src/KM_fileio.h7
-rwxr-xr-xasdcplib/src/h__Writer.cpp8
-rw-r--r--src/picture_asset.cc27
-rw-r--r--src/picture_asset.h24
8 files changed, 123 insertions, 17 deletions
diff --git a/asdcplib/src/AS_DCP.h b/asdcplib/src/AS_DCP.h
index 3d679413..d5675586 100755
--- a/asdcplib/src/AS_DCP.h
+++ b/asdcplib/src/AS_DCP.h
@@ -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;
};
//
diff --git a/asdcplib/src/AS_DCP_JP2K.cpp b/asdcplib/src/AS_DCP_JP2K.cpp
index ca6d5270..359cd910 100755
--- a/asdcplib/src/AS_DCP_JP2K.cpp
+++ b/asdcplib/src/AS_DCP_JP2K.cpp
@@ -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();
+}
+
//------------------------------------------------------------------------------------------
//
diff --git a/asdcplib/src/AS_DCP_internal.h b/asdcplib/src/AS_DCP_internal.h
index 4c1507c8..15b52822 100755
--- a/asdcplib/src/AS_DCP_internal.h
+++ b/asdcplib/src/AS_DCP_internal.h
@@ -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();
diff --git a/asdcplib/src/KM_fileio.cpp b/asdcplib/src/KM_fileio.cpp
index 2914f982..138bc3b4 100644
--- a/asdcplib/src/KM_fileio.cpp
+++ b/asdcplib/src/KM_fileio.cpp
@@ -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
//------------------------------------------------------------------------------------------
diff --git a/asdcplib/src/KM_fileio.h b/asdcplib/src/KM_fileio.h
index b078e32b..cb00acc8 100755
--- a/asdcplib/src/KM_fileio.h
+++ b/asdcplib/src/KM_fileio.h
@@ -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);
diff --git a/asdcplib/src/h__Writer.cpp b/asdcplib/src/h__Writer.cpp
index 5d3a1d5a..662e0f82 100755
--- a/asdcplib/src/h__Writer.cpp
+++ b/asdcplib/src/h__Writer.cpp
@@ -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;
}
diff --git a/src/picture_asset.cc b/src/picture_asset.cc
index cd6dd4dc..d242777c 100644
--- a/src/picture_asset.cc
+++ b/src/picture_asset.cc
@@ -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
diff --git a/src/picture_asset.h b/src/picture_asset.h
index 71900200..4e9e1dd7 100644
--- a/src/picture_asset.h
+++ b/src/picture_asset.h
@@ -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