Fix loading of some old metadata versions.
[dcpomatic.git] / src / lib / dcp_content.cc
index b46b3dab7abdd216425865bba6bebcf1264ce907..d866a6b86c69f03a9593d04b94d8ec1b2a5bca1c 100644 (file)
@@ -30,6 +30,7 @@
 #include "dcp_decoder.h"
 #include "subtitle_content.h"
 #include <dcp/dcp.h>
+#include <dcp/raw_convert.h>
 #include <dcp/exceptions.h>
 #include <dcp/reel_picture_asset.h>
 #include <dcp/reel.h>
@@ -50,6 +51,7 @@ using boost::shared_ptr;
 using boost::scoped_ptr;
 using boost::optional;
 using boost::function;
+using dcp::raw_convert;
 
 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
@@ -63,6 +65,7 @@ DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
        , _reference_video (false)
        , _reference_audio (false)
        , _reference_subtitle (false)
+       , _three_d (false)
 {
        video.reset (new VideoContent (this));
        audio.reset (new AudioContent (this));
@@ -82,7 +85,10 @@ DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, in
                AudioStreamPtr (
                        new AudioStream (
                                node->number_child<int> ("AudioFrameRate"),
-                               node->number_child<Frame> ("AudioLength"),
+                               /* AudioLength was not present in some old metadata versions */
+                               node->optional_number_child<Frame>("AudioLength").get_value_or (
+                                       video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
+                                       ),
                                AudioMapping (node->node_child ("AudioMapping"), version)
                                )
                        )
@@ -108,6 +114,7 @@ DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, in
                        DCPOMATIC_ASSERT (false);
                }
        }
+       _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
 }
 
 void
@@ -155,11 +162,14 @@ DCPContent::examine (shared_ptr<Job> job)
                _encrypted = examiner->encrypted ();
                _kdm_valid = examiner->kdm_valid ();
                _standard = examiner->standard ();
+               _three_d = examiner->three_d ();
        }
 
        if (could_be_played != can_be_played ()) {
                signal_changed (DCPContentProperty::CAN_BE_PLAYED);
        }
+
+       video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
 }
 
 string
@@ -221,6 +231,7 @@ DCPContent::as_xml (xmlpp::Node* node) const
                        DCPOMATIC_ASSERT (false);
                }
        }
+       node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
 }
 
 DCPTime
@@ -233,14 +244,13 @@ DCPContent::full_length () const
 string
 DCPContent::identifier () const
 {
-       SafeStringStream s;
-       s << Content::identifier() << "_" << video->identifier() << "_";
+       string s = Content::identifier() + "_" + video->identifier() + "_";
        if (subtitle) {
-               s << subtitle->identifier () << " ";
+               s += subtitle->identifier () + " ";
        }
-       s << (_reference_video ? "1" : "0")
-         << (_reference_subtitle ? "1" : "0");
-       return s.str ();
+
+       s += string (_reference_video ? "1" : "0") + string (_reference_subtitle ? "1" : "0");
+       return s;
 }
 
 void
@@ -326,7 +336,7 @@ DCPContent::reels () const
        list<DCPTimePeriod> p;
        scoped_ptr<DCPDecoder> decoder;
        try {
-               decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
+               decoder.reset (new DCPDecoder (shared_from_this(), film()->log()));
        } catch (...) {
                /* Could not load the DCP; guess reels */
                list<DCPTimePeriod> p;
@@ -397,7 +407,7 @@ DCPContent::can_reference_video (list<string>& why_not) const
 bool
 DCPContent::can_reference_audio (list<string>& why_not) const
 {
-        DCPDecoder decoder (shared_from_this(), film()->log(), false);
+        DCPDecoder decoder (shared_from_this(), film()->log());
         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
                 if (!i->main_sound()) {
                         why_not.push_back (_("The DCP does not have sound in all reels."));
@@ -411,7 +421,7 @@ DCPContent::can_reference_audio (list<string>& why_not) const
 bool
 DCPContent::can_reference_subtitle (list<string>& why_not) const
 {
-        DCPDecoder decoder (shared_from_this(), film()->log(), false);
+        DCPDecoder decoder (shared_from_this(), film()->log());
         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder.reels()) {
                 if (!i->main_subtitle()) {
                         why_not.push_back (_("The DCP does not have subtitles in all reels."));