Don't crash with no audio; get video MXF information from the MXF itself; get samplin... for-dcptovideo
authorCarl Hetherington <cth@carlh.net>
Sun, 12 Aug 2012 19:52:15 +0000 (20:52 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 12 Aug 2012 19:52:15 +0000 (20:52 +0100)
src/dcp.cc
src/picture_asset.cc
src/picture_asset.h
src/sound_asset.cc
src/sound_asset.h
src/sound_frame.cc [new file with mode: 0644]
src/sound_frame.h [new file with mode: 0644]
src/wscript

index fa94c44d3d823aa05dd5777d706669f0fbd038fb..f0269bdae8207c60e07b088e5c6ff8bc2c1f386b 100644 (file)
@@ -346,18 +346,17 @@ DCP::DCP (string directory)
                                           _directory,
                                           n,
                                           _fps,
-                                          _length,
-                                          cpl_assets->main_picture->screen_aspect_ratio.numerator,
-                                          cpl_assets->main_picture->screen_aspect_ratio.denominator
+                                          _length
                                           )
                                   ));
 
-       n = cpl_assets->main_sound->annotation_text;
-       if (n.empty ()) {
-               n = pkl->asset_from_id(cpl_assets->main_sound->id)->original_file_name;
-       }
-       
        if (cpl_assets->main_sound) {
+
+               n = cpl_assets->main_sound->annotation_text;
+               if (n.empty ()) {
+                       n = pkl->asset_from_id(cpl_assets->main_sound->id)->original_file_name;
+               }
+       
                _assets.push_back (shared_ptr<SoundAsset> (
                                           new SoundAsset (
                                                   _directory,
index 84c187df704fb76fc8394d9faeca9cf98921ac36..7b6dd29396a97db3bb936fd51792287b8eda3d87 100644 (file)
@@ -72,11 +72,21 @@ PictureAsset::PictureAsset (
        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)
+PictureAsset::PictureAsset (string directory, string mxf_name, int fps, int length)
        : Asset (directory, mxf_name, 0, fps, length)
-       , _width (width)
-       , _height (height)
 {
+       ASDCP::JP2K::MXFReader reader;
+       if (ASDCP_FAILURE (reader.OpenRead (mxf_path().string().c_str()))) {
+               throw FileError ("could not open MXF file for reading", mxf_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;
 
 }
 
index 18170f46e38efdf7dd81cb23c5f1bfe7086bd193..c0e5f6498200845b83c0f9f280297d1cba162ed1 100644 (file)
@@ -77,7 +77,7 @@ public:
                int height
                );
 
-       PictureAsset (std::string directory, std::string mxf_name, int fps, int length, int width, int height);
+       PictureAsset (std::string directory, std::string mxf_name, int fps, int length);
        
        /** Write details of this asset to a CPL stream.
         *  @param s Stream.
index a8b6372748c83069221fc95790bc5b8b36f6215e..189343bd8a7f38e86e2d00499666296772be8347 100644 (file)
@@ -30,6 +30,7 @@
 #include "sound_asset.h"
 #include "util.h"
 #include "exceptions.h"
+#include "sound_frame.h"
 
 using namespace std;
 using namespace boost;
@@ -40,6 +41,7 @@ SoundAsset::SoundAsset (
        )
        : Asset (directory, mxf_name, progress, fps, length)
        , _channels (files.size ())
+       , _sampling_rate (0)
 {
        construct (sigc::bind (sigc::mem_fun (*this, &SoundAsset::path_from_channel), files));
 }
@@ -49,6 +51,7 @@ SoundAsset::SoundAsset (
        )
        : Asset (directory, mxf_name, progress, fps, length)
        , _channels (channels)
+       , _sampling_rate (0)
 {
        construct (get_path);
 }
@@ -57,7 +60,19 @@ SoundAsset::SoundAsset (string directory, string mxf_name, int fps, int length)
        : Asset (directory, mxf_name, 0, fps, length)
        , _channels (0)
 {
+       ASDCP::PCM::MXFReader reader;
+       if (ASDCP_FAILURE (reader.OpenRead (mxf_path().string().c_str()))) {
+               throw FileError ("could not open MXF file for reading", mxf_path().string());
+       }
+
+       
+       ASDCP::PCM::AudioDescriptor desc;
+       if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
+               throw DCPReadError ("could not read audio MXF information");
+       }
 
+       _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
+       _channels = desc.ChannelCount;
 }
 
 string
@@ -246,21 +261,8 @@ SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt) const
        return notes;
 }
 
-
-int
-SoundAsset::sampling_rate () const
+shared_ptr<const SoundFrame>
+SoundAsset::get_frame (int n) const
 {
-       ASDCP::PCM::MXFReader reader;
-       if (ASDCP_FAILURE (reader.OpenRead (mxf_path().string().c_str()))) {
-               throw FileError ("could not open MXF file for reading", mxf_path().string());
-       }
-
-       
-       ASDCP::PCM::AudioDescriptor desc;
-       if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
-               throw DCPReadError ("could not read audio MXF information");
-       }
-
-       return desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
+       return shared_ptr<const SoundFrame> (new SoundFrame (mxf_path().string(), n));
 }
-
index f686e4bd6fbbd5c67c1402665eaa3401f8d83122..a0dd0a6e411b2de5771413164dcd31eb09aa8bc4 100644 (file)
@@ -30,6 +30,8 @@
 namespace libdcp
 {
 
+class SoundFrame;      
+
 /** @brief An asset made up of WAV files */
 class SoundAsset : public Asset
 {
@@ -86,11 +88,15 @@ public:
 
        std::list<std::string> equals (boost::shared_ptr<const Asset> other, EqualityOptions opt) const;
 
+       boost::shared_ptr<const SoundFrame> get_frame (int n) const;
+       
        int channels () const {
                return _channels;
        }
 
-       int sampling_rate () const;
+       int sampling_rate () const {
+               return _sampling_rate;
+       }
        
 private:
        void construct (sigc::slot<std::string, Channel> get_path);
@@ -98,6 +104,7 @@ private:
 
        /** Number of channels in the asset */
        int _channels;
+       int _sampling_rate;
 };
 
 }
diff --git a/src/sound_frame.cc b/src/sound_frame.cc
new file mode 100644 (file)
index 0000000..ed626f5
--- /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 "sound_frame.h"
+#include "exceptions.h"
+
+using namespace std;
+using namespace libdcp;
+
+SoundFrame::SoundFrame (string mxf_path, int n)
+{
+       ASDCP::PCM::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::PCM::FrameBuffer (1 * Kumu::Megabyte);
+
+       if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer))) {
+               throw DCPReadError ("could not read audio frame");
+       }
+}
+
+SoundFrame::~SoundFrame ()
+{
+       delete _buffer;
+}
+
+uint8_t const *
+SoundFrame::data () const
+{
+       return _buffer->RoData();
+}
+
+int
+SoundFrame::size () const
+{
+       return _buffer->Size ();
+}
diff --git a/src/sound_frame.h b/src/sound_frame.h
new file mode 100644 (file)
index 0000000..83846fc
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+    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 ASDCP {
+       namespace PCM {
+               class FrameBuffer;
+       }
+}
+
+namespace libdcp {
+
+class SoundFrame
+{
+public:
+       SoundFrame (std::string mxf_path, int n);
+       ~SoundFrame ();
+
+       uint8_t const * data () const;
+       int size () const;
+
+private:
+       ASDCP::PCM::FrameBuffer* _buffer;
+};
+
+}
index f96755c7a2c22561344ddf3c08575b056f057610..0a0c3e66fc68bc2fed5b80ae0a96565b411a4cdf 100644 (file)
@@ -20,6 +20,7 @@ def build(bld):
                  test_mode.cc
                  types.cc
                  xml.cc
+                 sound_frame.cc
                  """
 
     headers = """
@@ -33,6 +34,7 @@ def build(bld):
               sound_asset.h
               asset.h
               picture_frame.h
+              sound_frame.h
               """
 
     bld.install_files('${PREFIX}/include/libdcp', headers)