Remove _finalized check from picture writer too.
[libdcp.git] / src / picture_asset.cc
index 807564c5bad1e87049669ef87207c3fd308bb3d6..0019d5622f59a128451c3f6c0f26a541acfc6734 100644 (file)
@@ -79,7 +79,7 @@ PictureAsset::write_to_cpl (ostream& s) const
 }
 
 bool
-PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (string)> note) const
+PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
 {
        if (!MXFAsset::equals (other, opt, note)) {
                return false;
@@ -125,7 +125,7 @@ PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost:
 //             desc_A.QuantizationDefault != desc_B.QuantizationDefault
                ) {
                
-               note ("video MXF picture descriptors differ");
+               note (ERROR, "video MXF picture descriptors differ");
                return false;
        }
 
@@ -146,10 +146,12 @@ MonoPictureAsset::MonoPictureAsset (
        boost::signals2::signal<void (float)>* progress,
        int fps,
        int intrinsic_duration,
-       Size size)
+       Size size,
+       MXFMetadata const & metadata
+       )
        : PictureAsset (directory, mxf_name, progress, fps, intrinsic_duration, size)
 {
-       construct (get_path);
+       construct (get_path, metadata);
 }
 
 MonoPictureAsset::MonoPictureAsset (
@@ -159,10 +161,12 @@ MonoPictureAsset::MonoPictureAsset (
        boost::signals2::signal<void (float)>* progress,
        int fps,
        int intrinsic_duration,
-       Size size)
+       Size size,
+       MXFMetadata const & metadata
+       )
        : PictureAsset (directory, mxf_name, progress, fps, intrinsic_duration, size)
 {
-       construct (boost::bind (&MonoPictureAsset::path_from_list, this, _1, files));
+       construct (boost::bind (&MonoPictureAsset::path_from_list, this, _1, files), metadata);
 }
 
 MonoPictureAsset::MonoPictureAsset (string directory, string mxf_name, int fps, Size size)
@@ -192,7 +196,7 @@ MonoPictureAsset::MonoPictureAsset (string directory, string mxf_name)
 }
 
 void
-MonoPictureAsset::construct (boost::function<string (int)> get_path)
+MonoPictureAsset::construct (boost::function<string (int)> get_path, MXFMetadata const & metadata)
 {
        ASDCP::JP2K::CodestreamParser j2k_parser;
        ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
@@ -205,7 +209,7 @@ MonoPictureAsset::construct (boost::function<string (int)> get_path)
        picture_desc.EditRate = ASDCP::Rational (_edit_rate, 1);
        
        ASDCP::WriterInfo writer_info;
-       fill_writer_info (&writer_info, _uuid);
+       fill_writer_info (&writer_info, _uuid, metadata);
        
        ASDCP::JP2K::MXFWriter mxf_writer;
        if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc, 16384, false))) {
@@ -248,7 +252,7 @@ MonoPictureAsset::get_frame (int n) const
 
 
 bool
-MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (string)> note) const
+MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
 {
        if (!PictureAsset::equals (other, opt, note)) {
                return false;
@@ -258,6 +262,7 @@ MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, bo
        assert (other_picture);
 
        for (int i = 0; i < _intrinsic_duration; ++i) {
+               note (PROGRESS, "Comparing video frame " + lexical_cast<string> (i) + " of " + lexical_cast<string> (_intrinsic_duration));
                shared_ptr<const MonoPictureFrame> frame_A = get_frame (i);
                shared_ptr<const MonoPictureFrame> frame_B = other_picture->get_frame (i);
                
@@ -274,7 +279,7 @@ MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, bo
 }
 
 bool
-StereoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (string)> note) const
+StereoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
 {
        if (!PictureAsset::equals (other, opt, note)) {
                return false;
@@ -309,11 +314,12 @@ StereoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt,
 
 bool
 PictureAsset::frame_buffer_equals (
-       int frame, EqualityOptions opt, boost::function<void (string)> note,
+       int frame, EqualityOptions opt, boost::function<void (NoteType, string)> note,
        uint8_t const * data_A, unsigned int size_A, uint8_t const * data_B, unsigned int size_B
        ) const
 {
        if (size_A == size_B && memcmp (data_A, data_B, size_A) == 0) {
+               note (NOTE, "J2K identical");
                /* Easy result; the J2K data is identical */
                return true;
        }
@@ -325,7 +331,7 @@ PictureAsset::frame_buffer_equals (
        /* Compare them */
        
        if (image_A->numcomps != image_B->numcomps) {
-               note ("image component counts for frame " + lexical_cast<string>(frame) + " differ");
+               note (ERROR, "image component counts for frame " + lexical_cast<string>(frame) + " differ");
                return false;
        }
        
@@ -336,7 +342,7 @@ PictureAsset::frame_buffer_equals (
        for (int c = 0; c < image_A->numcomps; ++c) {
                
                if (image_A->comps[c].w != image_B->comps[c].w || image_A->comps[c].h != image_B->comps[c].h) {
-                       note ("image sizes for frame " + lexical_cast<string>(frame) + " differ");
+                       note (ERROR, "image sizes for frame " + lexical_cast<string>(frame) + " differ");
                        return false;
                }
                
@@ -362,11 +368,18 @@ PictureAsset::frame_buffer_equals (
        
        double const std_dev = sqrt (double (total_squared_deviation) / abs_diffs.size());
        
-       if (mean > opt.max_mean_pixel_error || std_dev > opt.max_std_dev_pixel_error) {
-               note ("mean or standard deviation out of range for " + lexical_cast<string>(frame));
+       note (NOTE, "mean difference " + lexical_cast<string> (mean) + ", deviation " + lexical_cast<string> (std_dev));
+       
+       if (mean > opt.max_mean_pixel_error) {
+               note (ERROR, "mean " + lexical_cast<string>(mean) + " out of range " + lexical_cast<string>(opt.max_mean_pixel_error) + " in frame " + lexical_cast<string>(frame));
                return false;
        }
-       
+
+       if (std_dev > opt.max_std_dev_pixel_error) {
+               note (ERROR, "standard deviation " + lexical_cast<string>(std_dev) + " out of range " + lexical_cast<string>(opt.max_std_dev_pixel_error) + " in frame " + lexical_cast<string>(frame));
+               return false;
+       }
+
        opj_image_destroy (image_A);
        opj_image_destroy (image_B);
 
@@ -398,10 +411,10 @@ StereoPictureAsset::get_frame (int n) const
 }
 
 shared_ptr<MonoPictureAssetWriter>
-MonoPictureAsset::start_write (bool overwrite)
+MonoPictureAsset::start_write (bool overwrite, MXFMetadata const & metadata)
 {
-       /* XXX: can't we use shared_ptr here? */
-       return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite));
+       /* XXX: can't we use shared_ptr here? */
+       return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite, metadata));
 }
 
 FrameInfo::FrameInfo (istream& s)
@@ -432,13 +445,14 @@ struct MonoPictureAssetWriter::ASDCPState
 /** @param a Asset to write to.  `a' must not be deleted while
  *  this writer class still exists, or bad things will happen.
  */
-MonoPictureAssetWriter::MonoPictureAssetWriter (MonoPictureAsset* a, bool overwrite)
+MonoPictureAssetWriter::MonoPictureAssetWriter (MonoPictureAsset* a, bool overwrite, MXFMetadata const & m)
        : _state (new MonoPictureAssetWriter::ASDCPState)
        , _asset (a)
        , _frames_written (0)
        , _started (false)
        , _finalized (false)
        , _overwrite (overwrite)
+       , _metadata (m)
 {
 
 }
@@ -454,7 +468,7 @@ MonoPictureAssetWriter::start (uint8_t* data, int size)
        _state->j2k_parser.FillPictureDescriptor (_state->picture_descriptor);
        _state->picture_descriptor.EditRate = ASDCP::Rational (_asset->edit_rate(), 1);
        
-       MXFAsset::fill_writer_info (&_state->writer_info, _asset->uuid());
+       MXFAsset::fill_writer_info (&_state->writer_info, _asset->uuid(), _metadata);
        
        if (ASDCP_FAILURE (_state->mxf_writer.OpenWrite (
                                   _asset->path().string().c_str(),
@@ -520,8 +534,3 @@ MonoPictureAssetWriter::finalize ()
        _asset->set_intrinsic_duration (_frames_written);
        _asset->set_duration (_frames_written);
 }
-
-MonoPictureAssetWriter::~MonoPictureAssetWriter ()
-{
-       assert (_finalized);
-}