Missing audio asset decryption.
[libdcp.git] / src / picture_asset.cc
index eb17813e9214df5b7440875878e3274375f24c21..2aec187ac42657b9cd6b3862b83578162ae3c6e3 100644 (file)
@@ -36,6 +36,8 @@
 #include "util.h"
 #include "exceptions.h"
 #include "picture_frame.h"
+#include "xyz_frame.h"
+#include "picture_asset_writer.h"
 
 using std::string;
 using std::ostream;
@@ -66,11 +68,29 @@ PictureAsset::PictureAsset (string directory, string mxf_name)
 }
 
 string
-PictureAsset::cpl_node_name () const
+MonoPictureAsset::cpl_node_name () const
 {
        return "MainPicture";
 }
 
+int
+MonoPictureAsset::edit_rate_factor () const
+{
+       return 1;
+}
+
+string
+StereoPictureAsset::cpl_node_name () const
+{
+       return "MainStereoscopicPicture";
+}
+
+int
+StereoPictureAsset::edit_rate_factor () const
+{
+       return 2;
+}
+
 void
 PictureAsset::write_to_cpl (xmlpp::Node* node) const
 {
@@ -84,7 +104,7 @@ PictureAsset::write_to_cpl (xmlpp::Node* node) const
 
        assert (i != c.end ());
 
-       (*i)->add_child ("FrameRate")->add_child_text (lexical_cast<string> (_edit_rate) + " 1");
+       (*i)->add_child ("FrameRate")->add_child_text (lexical_cast<string> (_edit_rate * edit_rate_factor ()) + " 1");
        (*i)->add_child ("ScreenAspectRatio")->add_child_text (lexical_cast<string> (_size.width) + " " + lexical_cast<string> (_size.height));
 }
 
@@ -116,7 +136,6 @@ PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost:
        
        if (
                desc_A.EditRate != desc_B.EditRate ||
-               desc_A.ContainerDuration != desc_B.ContainerDuration ||
                desc_A.SampleRate != desc_B.SampleRate ||
                desc_A.StoredWidth != desc_B.StoredWidth ||
                desc_A.StoredHeight != desc_B.StoredHeight ||
@@ -139,6 +158,10 @@ PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost:
                return false;
        }
 
+       if (desc_A.ContainerDuration != desc_B.ContainerDuration) {
+               note (ERROR, "video container durations differ");
+       }
+       
 //             for (unsigned int j = 0; j < ASDCP::JP2K::MaxComponents; ++j) {
 //                     if (desc_A.ImageComponents[j] != desc_B.ImageComponents[j]) {
 //                             notes.pack_start ("video MXF picture descriptors differ");
@@ -274,6 +297,10 @@ MonoPictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, bo
        assert (other_picture);
 
        for (int i = 0; i < _intrinsic_duration; ++i) {
+               if (i >= other_picture->intrinsic_duration()) {
+                       return false;
+               }
+               
                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);
@@ -337,30 +364,25 @@ PictureAsset::frame_buffer_equals (
        }
                
        /* Decompress the images to bitmaps */
-       opj_image_t* image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
-       opj_image_t* image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
+       shared_ptr<XYZFrame> image_A = decompress_j2k (const_cast<uint8_t*> (data_A), size_A, 0);
+       shared_ptr<XYZFrame> image_B = decompress_j2k (const_cast<uint8_t*> (data_B), size_B, 0);
        
        /* Compare them */
        
-       if (image_A->numcomps != image_B->numcomps) {
-               note (ERROR, "image component counts for frame " + lexical_cast<string>(frame) + " differ");
-               return false;
-       }
-       
-       vector<int> abs_diffs (image_A->comps[0].w * image_A->comps[0].h * image_A->numcomps);
+       vector<int> abs_diffs (image_A->size().width * image_A->size().height * 3);
        int d = 0;
        int max_diff = 0;
        
-       for (int c = 0; c < image_A->numcomps; ++c) {
+       for (int c = 0; c < 3; ++c) {
                
-               if (image_A->comps[c].w != image_B->comps[c].w || image_A->comps[c].h != image_B->comps[c].h) {
+               if (image_A->size() != image_B->size()) {
                        note (ERROR, "image sizes for frame " + lexical_cast<string>(frame) + " differ");
                        return false;
                }
                
-               int const pixels = image_A->comps[c].w * image_A->comps[c].h;
+               int const pixels = image_A->size().width * image_A->size().height;
                for (int j = 0; j < pixels; ++j) {
-                       int const t = abs (image_A->comps[c].data[j] - image_B->comps[c].data[j]);
+                       int const t = abs (image_A->data(c)[j] - image_B->data(c)[j]);
                        abs_diffs[d++] = t;
                        max_diff = max (max_diff, t);
                }
@@ -392,9 +414,6 @@ PictureAsset::frame_buffer_equals (
                return false;
        }
 
-       opj_image_destroy (image_A);
-       opj_image_destroy (image_B);
-
        return true;
 }
 
@@ -422,133 +441,29 @@ StereoPictureAsset::get_frame (int n) const
        return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n));
 }
 
-shared_ptr<MonoPictureAssetWriter>
+shared_ptr<PictureAssetWriter>
 MonoPictureAsset::start_write (bool overwrite, MXFMetadata const & metadata)
 {
        /* XXX: can't we use shared_ptr here? */
        return shared_ptr<MonoPictureAssetWriter> (new MonoPictureAssetWriter (this, overwrite, metadata));
 }
 
-FrameInfo::FrameInfo (istream& s)
-{
-       s >> offset >> size >> hash;
-}
-
-void
-FrameInfo::write (ostream& s)
-{
-       s << offset << " " << size << " " << hash;
-}
-
-struct MonoPictureAssetWriter::ASDCPState
-{
-       ASDCPState()
-               : frame_buffer (4 * Kumu::Megabyte)
-       {}
-       
-       ASDCP::JP2K::CodestreamParser j2k_parser;
-       ASDCP::JP2K::FrameBuffer frame_buffer;
-       ASDCP::JP2K::MXFWriter mxf_writer;
-       ASDCP::WriterInfo writer_info;
-       ASDCP::JP2K::PictureDescriptor picture_descriptor;
-};
-
-
-/** @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, MXFMetadata const & m)
-       : _state (new MonoPictureAssetWriter::ASDCPState)
-       , _asset (a)
-       , _frames_written (0)
-       , _started (false)
-       , _finalized (false)
-       , _overwrite (overwrite)
-       , _metadata (m)
-{
-
-}
-
-
-void
-MonoPictureAssetWriter::start (uint8_t* data, int size)
-{
-       if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
-               boost::throw_exception (MiscError ("could not parse J2K frame"));
-       }
-
-       _state->j2k_parser.FillPictureDescriptor (_state->picture_descriptor);
-       _state->picture_descriptor.EditRate = ASDCP::Rational (_asset->edit_rate(), 1);
-       
-       _asset->fill_writer_info (&_state->writer_info, _asset->uuid(), _metadata);
-       
-       if (ASDCP_FAILURE (_state->mxf_writer.OpenWrite (
-                                  _asset->path().string().c_str(),
-                                  _state->writer_info,
-                                  _state->picture_descriptor,
-                                  16384,
-                                  _overwrite)
-                   )) {
-               
-               boost::throw_exception (MXFFileError ("could not open MXF file for writing", _asset->path().string()));
-       }
-
-       _started = true;
-}
-
-FrameInfo
-MonoPictureAssetWriter::write (uint8_t* data, int size)
+string
+PictureAsset::key_type () const
 {
-       assert (!_finalized);
-
-       if (!_started) {
-               start (data, size);
-       }
-
-       if (ASDCP_FAILURE (_state->j2k_parser.OpenReadFrame (data, size, _state->frame_buffer))) {
-               boost::throw_exception (MiscError ("could not parse J2K frame"));
-       }
-
-       uint64_t const before_offset = _state->mxf_writer.Tell ();
-
-       string hash;
-       if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, 0, 0, &hash))) {
-               boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
-       }
-
-       ++_frames_written;
-       return FrameInfo (before_offset, _state->mxf_writer.Tell() - before_offset, hash);
+       return "MDIK";
 }
 
-void
-MonoPictureAssetWriter::fake_write (int size)
+StereoPictureAsset::StereoPictureAsset (string directory, string mxf_name, int fps, Size size)
+       : PictureAsset (directory, mxf_name, 0, fps, 0, false, size)
 {
-       assert (_started);
-       assert (!_finalized);
-
-       if (ASDCP_FAILURE (_state->mxf_writer.FakeWriteFrame (size))) {
-               boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
-       }
 
-       ++_frames_written;
 }
 
-void
-MonoPictureAssetWriter::finalize ()
+shared_ptr<PictureAssetWriter>
+StereoPictureAsset::start_write (bool overwrite, MXFMetadata const & metadata)
 {
-       assert (!_finalized);
-       
-       if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
-               boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string()));
-       }
-
-       _finalized = true;
-       _asset->set_intrinsic_duration (_frames_written);
-       _asset->set_duration (_frames_written);
+       /* XXX: can't we use shared_ptr here? */
+       return shared_ptr<StereoPictureAssetWriter> (new StereoPictureAssetWriter (this, overwrite, metadata));
 }
 
-string
-PictureAsset::key_type () const
-{
-       return "MDIK";
-}