Add picture frame extraction.
authorCarl Hetherington <cth@carlh.net>
Thu, 9 Aug 2012 22:27:15 +0000 (23:27 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 9 Aug 2012 22:27:15 +0000 (23:27 +0100)
src/asset.cc
src/asset.h
src/dcp.cc
src/dcp.h
src/picture_asset.cc
src/picture_asset.h
src/picture_frame.cc [new file with mode: 0644]
src/picture_frame.h [new file with mode: 0644]
src/wscript

index 78686a17bf16ffe6ec59ef62c464f64a26042f0b..d8cf25dae5922f0d973991a2cef918ab1dd3ece8 100644 (file)
@@ -159,6 +159,10 @@ Asset::digest () const
        return _digest;
 }
 
-               
-               
-       
+int
+Asset::length () const
+{
+       return _length;
+}
+
+       
index 6c0a9803adb9e47ead40255e0aefc380a0f20c3f..b7b2065b78447625861fd1867d07b94ab02fdc59 100644 (file)
@@ -25,6 +25,7 @@
 #define LIBDCP_ASSET_H
 
 #include <string>
+#include <boost/filesystem.hpp>
 #include <sigc++/sigc++.h>
 #include "types.h"
 
@@ -68,6 +69,8 @@ public:
 
        virtual std::list<std::string> equals (boost::shared_ptr<const Asset> other, EqualityOptions opt) const;
 
+       int length () const;
+
 protected:
        friend class PictureAsset;
        friend class SoundAsset;
index 53d6b55dcc01ff3162a394318d0af322b5e66771..b6014941b823c03b3f37eb257e452a6473791342 100644 (file)
@@ -369,3 +369,16 @@ DCP::equals (DCP const & other, EqualityOptions opt) const
 
        return notes;
 }
+
+shared_ptr<const PictureAsset>
+DCP::picture_asset () const
+{
+       for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
+               shared_ptr<PictureAsset> p = dynamic_pointer_cast<PictureAsset> (*i);
+               if (p) {
+                       return p;
+               }
+       }
+
+       return shared_ptr<const PictureAsset> ();
+}
index 5a429a171379899295b121563e0ebc236673a81b..924a9f4f5ea7444536487749ecb6138ed9906a47 100644 (file)
--- a/src/dcp.h
+++ b/src/dcp.h
@@ -39,6 +39,7 @@ namespace libdcp
 {
 
 class Asset;   
+class PictureAsset;
 
 /** @class DCP dcp.h libdcp/dcp.h
  *  @brief A class to create or read a DCP.
@@ -106,6 +107,8 @@ public:
                return _length;
        }
 
+       boost::shared_ptr<const PictureAsset> picture_asset () const;
+
        std::list<std::string> equals (DCP const & other, EqualityOptions options) const;
 
        /** Emitted with a parameter between 0 and 1 to indicate progress
index c59664d9cb263833c3c8b0179a3e3460f12468c2..d7036a190fcebf68033ed923bdb035fbcbd83adc 100644 (file)
@@ -34,6 +34,7 @@
 #include "picture_asset.h"
 #include "util.h"
 #include "exceptions.h"
+#include "picture_frame.h"
 
 using namespace std;
 using namespace boost;
@@ -303,3 +304,9 @@ PictureAsset::decompress_j2k (uint8_t* data, int64_t size) const
        opj_cio_close (cio);
        return image;
 }
+
+shared_ptr<const PictureFrame>
+PictureAsset::get_frame (int n) const
+{
+       return shared_ptr<const PictureFrame> (new PictureFrame (mxf_path().string(), n));
+}
index 41b09b58c4b532529b37515e44d6fd429b9f9cdf..4e1801f9d2de22029e44972685cc5cea395ed501 100644 (file)
@@ -27,6 +27,8 @@
 namespace libdcp
 {
 
+class PictureFrame;    
+
 /** @brief An asset made up of JPEG2000 files */
 class PictureAsset : public Asset
 {
@@ -83,6 +85,8 @@ public:
        void write_to_cpl (std::ostream& s) const;
 
        std::list<std::string> equals (boost::shared_ptr<const Asset> other, EqualityOptions opt) const;
+
+       boost::shared_ptr<const PictureFrame> get_frame (int n) const;
        
 private:
        std::string path_from_list (int f, std::vector<std::string> const & files) const;
diff --git a/src/picture_frame.cc b/src/picture_frame.cc
new file mode 100644 (file)
index 0000000..d2ef698
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "AS_DCP.h"
+#include "KM_fileio.h"
+#include "picture_frame.h"
+#include "exceptions.h"
+
+using namespace std;
+using namespace libdcp;
+
+PictureFrame::PictureFrame (string mxf_path, int n)
+{
+       ASDCP::JP2K::MXFReader reader;
+       if (ASDCP_FAILURE (reader.OpenRead (mxf_path.c_str()))) {
+               throw FileError ("could not open MXF file for reading", mxf_path);
+       }
+
+       /* XXX: unfortunate guesswork on this buffer size */
+       _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
+
+       if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer))) {
+               throw DCPReadError ("could not read video frame");
+       }
+}
+
+PictureFrame::~PictureFrame ()
+{
+       delete _buffer;
+}
+
+uint8_t const *
+PictureFrame::data () const
+{
+       return _buffer->RoData();
+}
+
+int
+PictureFrame::size () const
+{
+       return _buffer->Size ();
+}
diff --git a/src/picture_frame.h b/src/picture_frame.h
new file mode 100644 (file)
index 0000000..4d35933
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include <string>
+#include <stdint.h>
+
+namespace libdcp {
+
+class PictureFrame
+{
+public:
+       PictureFrame (std::string mxf_path, int n);
+       ~PictureFrame ();
+
+       uint8_t const * data () const;
+       int size () const;
+
+private:
+       ASDCP::JP2K::FrameBuffer* _buffer;
+};
+
+}
index 8caae703ae22d7a90815592de0d69f251d9ebe64..2956029c4b351073a26b7fbef3d8339cbcca6f2c 100644 (file)
@@ -12,6 +12,7 @@ def build(bld):
                  dcp.cc        
                  sound_asset.cc
                  picture_asset.cc
+                 picture_frame.cc
                  pkl.cc
                  util.cc
                  metadata.cc
@@ -28,6 +29,8 @@ def build(bld):
               exceptions.h
               test_mode.h
               version.h
+              picture_asset.h
+              asset.h
               """
 
     bld.install_files('${PREFIX}/include/libdcp', headers)