Very slightly less verbose subs checking.
[libdcp.git] / src / picture_asset.cc
index c59664d9cb263833c3c8b0179a3e3460f12468c2..3fbc09478d48480c54f974b55b2e3dbd3aebfc75 100644 (file)
 #include "picture_asset.h"
 #include "util.h"
 #include "exceptions.h"
+#include "picture_frame.h"
 
 using namespace std;
 using namespace boost;
 using namespace libdcp;
 
-PictureAsset::PictureAsset (
-       sigc::slot<string, int> get_path,
-       string directory,
-       string mxf_name,
-       sigc::signal1<void, float>* progress,
-       int fps,
-       int length,
-       int width,
-       int height)
-       : Asset (directory, mxf_name, progress, fps, length)
-       , _width (width)
-       , _height (height)
-{
-       construct (get_path);
-}
-
-PictureAsset::PictureAsset (
-       vector<string> const & files,
-       string directory,
-       string mxf_name,
-       sigc::signal1<void, float>* progress,
-       int fps,
-       int length,
-       int width,
-       int height)
-       : Asset (directory, mxf_name, progress, fps, length)
-       , _width (width)
-       , _height (height)
+PictureAsset::PictureAsset (string directory, string mxf_name, sigc::signal1<void, float>* progress, int fps, int entry_point, int length)
+       : MXFAsset (directory, mxf_name, progress, fps, entry_point, length)
+       , _width (0)
+       , _height (0)
 {
-       construct (sigc::bind (sigc::mem_fun (*this, &PictureAsset::path_from_list), files));
-}
-
-PictureAsset::PictureAsset (string directory, string mxf_name, int fps, int length, int width, int height)
-       : Asset (directory, mxf_name, 0, fps, length)
-       , _width (width)
-       , _height (height)
-{
-
-}
 
-string
-PictureAsset::path_from_list (int f, vector<string> const & files) const
-{
-       return files[f];
-}
-
-void
-PictureAsset::construct (sigc::slot<string, int> get_path)
-{
-       ASDCP::JP2K::CodestreamParser j2k_parser;
-       ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
-       if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
-               throw FileError ("could not open JPEG2000 file for reading", get_path (0));
-       }
-       
-       ASDCP::JP2K::PictureDescriptor picture_desc;
-       j2k_parser.FillPictureDescriptor (picture_desc);
-       picture_desc.EditRate = ASDCP::Rational (_fps, 1);
-       
-       ASDCP::WriterInfo writer_info;
-       fill_writer_info (&writer_info);
-       
-       ASDCP::JP2K::MXFWriter mxf_writer;
-       if (ASDCP_FAILURE (mxf_writer.OpenWrite (mxf_path().string().c_str(), writer_info, picture_desc))) {
-               throw FileError ("could not open MXF file for writing", mxf_path().string());
-       }
-
-       for (int i = 0; i < _length; ++i) {
-
-               string const path = get_path (i);
-               
-               if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
-                       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");
-               }
-               
-               (*_progress) (0.5 * float (i) / _length);
-       }
-       
-       if (ASDCP_FAILURE (mxf_writer.Finalize())) {
-               throw MiscError ("error in finalising video MXF");
-       }
 }
 
 void
@@ -132,7 +53,7 @@ PictureAsset::write_to_cpl (ostream& s) const
 {
        s << "        <MainPicture>\n"
          << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
-         << "          <AnnotationText>" << _mxf_name << "</AnnotationText>\n"
+         << "          <AnnotationText>" << _file_name << "</AnnotationText>\n"
          << "          <EditRate>" << _fps << " 1</EditRate>\n"
          << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
          << "          <EntryPoint>0</EntryPoint>\n"
@@ -142,20 +63,21 @@ PictureAsset::write_to_cpl (ostream& s) const
          << "        </MainPicture>\n";
 }
 
+/* XXX: could use get_frame()? */
 list<string>
 PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt) const
 {
-       list<string> notes = Asset::equals (other, opt);
+       list<string> notes = MXFAsset::equals (other, opt);
                     
        if (opt.flags & MXF_INSPECT) {
                ASDCP::JP2K::MXFReader reader_A;
-               if (ASDCP_FAILURE (reader_A.OpenRead (mxf_path().string().c_str()))) {
-                       throw FileError ("could not open MXF file for reading", mxf_path().string());
+               if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
+                       throw FileError ("could not open MXF file for reading", path().string());
                }
 
                ASDCP::JP2K::MXFReader reader_B;
-               if (ASDCP_FAILURE (reader_B.OpenRead (other->mxf_path().string().c_str()))) {
-                       throw FileError ("could not open MXF file for reading", mxf_path().string());
+               if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
+                       throw FileError ("could not open MXF file for reading", path().string());
                }
 
                ASDCP::JP2K::PictureDescriptor desc_A;
@@ -285,21 +207,128 @@ PictureAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt) const
        return notes;
 }
 
-opj_image_t *
-PictureAsset::decompress_j2k (uint8_t* data, int64_t size) const
+MonoPictureAsset::MonoPictureAsset (
+       sigc::slot<string, int> get_path,
+       string directory,
+       string mxf_name,
+       sigc::signal1<void, float>* progress,
+       int fps,
+       int length,
+       int width,
+       int height)
+       : PictureAsset (directory, mxf_name, progress, fps, 0, length)
 {
-       opj_dinfo_t* decoder = opj_create_decompress (CODEC_J2K);
-       opj_dparameters_t parameters;
-       opj_set_default_decoder_parameters (&parameters);
-       opj_setup_decoder (decoder, &parameters);
-       opj_cio_t* cio = opj_cio_open ((opj_common_ptr) decoder, data, size);
-       opj_image_t* image = opj_decode (decoder, cio);
-       if (!image) {
-               opj_destroy_decompress (decoder);
-               opj_cio_close (cio);
-               throw DCPReadError ("could not decode JPEG2000 codestream");
+       _width = width;
+       _height = height;
+       construct (get_path);
+}
+
+MonoPictureAsset::MonoPictureAsset (
+       vector<string> const & files,
+       string directory,
+       string mxf_name,
+       sigc::signal1<void, float>* progress,
+       int fps,
+       int length,
+       int width,
+       int height)
+       : PictureAsset (directory, mxf_name, progress, fps, 0, length)
+{
+       _width = width;
+       _height = height;
+       construct (sigc::bind (sigc::mem_fun (*this, &MonoPictureAsset::path_from_list), files));
+}
+
+MonoPictureAsset::MonoPictureAsset (string directory, string mxf_name, int fps, int entry_point, int length)
+       : PictureAsset (directory, mxf_name, 0, fps, entry_point, length)
+{
+       ASDCP::JP2K::MXFReader reader;
+       if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
+               throw FileError ("could not open MXF file for reading", path().string());
+       }
+       
+       ASDCP::JP2K::PictureDescriptor desc;
+       if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
+               throw DCPReadError ("could not read video MXF information");
+       }
+
+       _width = desc.StoredWidth;
+       _height = desc.StoredHeight;
+}
+
+void
+MonoPictureAsset::construct (sigc::slot<string, int> get_path)
+{
+       ASDCP::JP2K::CodestreamParser j2k_parser;
+       ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
+       if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).c_str(), frame_buffer))) {
+               throw FileError ("could not open JPEG2000 file for reading", get_path (0));
+       }
+       
+       ASDCP::JP2K::PictureDescriptor picture_desc;
+       j2k_parser.FillPictureDescriptor (picture_desc);
+       picture_desc.EditRate = ASDCP::Rational (_fps, 1);
+       
+       ASDCP::WriterInfo writer_info;
+       fill_writer_info (&writer_info);
+       
+       ASDCP::JP2K::MXFWriter mxf_writer;
+       if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc))) {
+               throw FileError ("could not open MXF file for writing", path().string());
        }
 
-       opj_cio_close (cio);
-       return image;
+       for (int i = 0; i < _length; ++i) {
+
+               string const path = get_path (i);
+               
+               if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.c_str(), frame_buffer))) {
+                       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");
+               }
+               
+               (*_progress) (0.5 * float (i) / _length);
+       }
+       
+       if (ASDCP_FAILURE (mxf_writer.Finalize())) {
+               throw MiscError ("error in finalising video MXF");
+       }
+}
+
+string
+MonoPictureAsset::path_from_list (int f, vector<string> const & files) const
+{
+       return files[f];
+}
+
+shared_ptr<const MonoPictureFrame>
+MonoPictureAsset::get_frame (int n) const
+{
+       return shared_ptr<const MonoPictureFrame> (new MonoPictureFrame (path().string(), n + _entry_point));
+}
+
+StereoPictureAsset::StereoPictureAsset (string directory, string mxf_name, int fps, int entry_point, int length)
+       : PictureAsset (directory, mxf_name, 0, fps, entry_point, length)
+{
+       ASDCP::JP2K::MXFSReader reader;
+       if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
+               throw FileError ("could not open MXF file for reading", path().string());
+       }
+       
+       ASDCP::JP2K::PictureDescriptor desc;
+       if (ASDCP_FAILURE (reader.FillPictureDescriptor (desc))) {
+               throw DCPReadError ("could not read video MXF information");
+       }
+
+       _width = desc.StoredWidth;
+       _height = desc.StoredHeight;
+}
+
+shared_ptr<const StereoPictureFrame>
+StereoPictureAsset::get_frame (int n) const
+{
+       return shared_ptr<const StereoPictureFrame> (new StereoPictureFrame (path().string(), n + _entry_point));
 }