Rename SafeStringStream -> locked_stringstream. Bump deps for removal of stringstream.
[dcpomatic.git] / src / lib / film.cc
index 49a0a9b13c0a41ab62045445291e53415ac46146..0138211cf0c49d823f3357fbdca62108a68dbe2c 100644 (file)
@@ -1,19 +1,20 @@
 /*
     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
 
-    This program is free software; you can redistribute it and/or modify
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic 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,
+    DCP-o-matic 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.
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 
 #include "dcp_content_type.h"
 #include "ratio.h"
 #include "cross.h"
-#include "safe_stringstream.h"
 #include "environment_info.h"
 #include "raw_convert.h"
 #include "audio_processor.h"
-#include "md5_digester.h"
+#include "digester.h"
 #include "compose.hpp"
 #include "screen.h"
 #include "audio_content.h"
@@ -51,6 +51,7 @@
 #include "dcp_content.h"
 #include "screen_kdm.h"
 #include "cinema.h"
+#include <locked_sstream.h>
 #include <libcxml/cxml.h>
 #include <dcp/cpl.h>
 #include <dcp/certificate_chain.h>
@@ -61,6 +62,7 @@
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
 #include <boost/foreach.hpp>
+#include <boost/regex.hpp>
 #include <unistd.h>
 #include <stdexcept>
 #include <iostream>
@@ -107,8 +109,15 @@ using boost::is_any_of;
  *
  * 32 -> 33
  * Changed <Period> to <Subtitle> in FFmpegSubtitleStream
+ * 33 -> 34
+ * Content only contains audio/subtitle-related tags if those things
+ * are present.
+ * 34 -> 35
+ * VideoFrameType in VideoContent is a string rather than an integer.
+ * 35 -> 36
+ * EffectColour rather than OutlineColour in Subtitle.
  */
-int const Film::current_state_version = 33;
+int const Film::current_state_version = 36;
 
 /** Construct a Film object in a given directory.
  *
@@ -126,7 +135,7 @@ Film::Film (boost::filesystem::path dir, bool log)
        , _j2k_bandwidth (Config::instance()->default_j2k_bandwidth ())
        , _isdcf_metadata (Config::instance()->default_isdcf_metadata ())
        , _video_frame_rate (24)
-       , _audio_channels (6)
+       , _audio_channels (Config::instance()->default_dcp_audio_channels ())
        , _three_d (false)
        , _sequence (true)
        , _interop (Config::instance()->default_interop ())
@@ -188,7 +197,7 @@ Film::video_identifier () const
 {
        DCPOMATIC_ASSERT (container ());
 
-       SafeStringStream s;
+       locked_stringstream s;
        s.imbue (std::locale::classic ());
 
        s << container()->id()
@@ -243,14 +252,14 @@ Film::audio_analysis_path (shared_ptr<const Playlist> playlist) const
 {
        boost::filesystem::path p = dir ("analysis");
 
-       MD5Digester digester;
+       Digester digester;
        BOOST_FOREACH (shared_ptr<Content> i, playlist->content ()) {
                if (!i->audio) {
                        continue;
                }
 
                digester.add (i->digest ());
-               digester.add (i->audio->audio_mapping().digest ());
+               digester.add (i->audio->mapping().digest ());
                if (playlist->content().size() != 1) {
                        /* Analyses should be considered equal regardless of gain
                           if they were made from just one piece of content.  This
@@ -258,7 +267,7 @@ Film::audio_analysis_path (shared_ptr<const Playlist> playlist) const
                           analysis at the plotting stage rather than having to
                           recompute it.
                        */
-                       digester.add (i->audio->audio_gain ());
+                       digester.add (i->audio->gain ());
                }
        }
 
@@ -521,7 +530,7 @@ Film::mapped_audio_channels () const
 string
 Film::isdcf_name (bool if_created_now) const
 {
-       SafeStringStream d;
+       locked_stringstream d;
 
        string raw_name = name ();
 
@@ -620,7 +629,7 @@ Film::isdcf_name (bool if_created_now) const
                                if (i->video->scale().ratio ()) {
                                        content_ratio = i->video->scale().ratio ();
                                } else {
-                                       content_ratio = Ratio::from_ratio (i->video->video_size().ratio ());
+                                       content_ratio = Ratio::from_ratio (i->video->size().ratio ());
                                }
                                break;
                        }
@@ -641,7 +650,7 @@ Film::isdcf_name (bool if_created_now) const
                                        continue;
                                }
 
-                               if (i->subtitle->use_subtitles() && !i->subtitle->burn_subtitles()) {
+                               if (i->subtitle->use() && !i->subtitle->burn()) {
                                        burnt_in = false;
                                }
                        }
@@ -887,7 +896,7 @@ Film::signal_changed (Property p)
 
        switch (p) {
        case Film::CONTENT:
-               set_video_frame_rate (_playlist->best_dcp_frame_rate ());
+               set_video_frame_rate (_playlist->best_video_frame_rate ());
                break;
        case Film::VIDEO_FRAME_RATE:
        case Film::SEQUENCE:
@@ -913,7 +922,7 @@ Film::j2c_path (int reel, Frame frame, Eyes eyes, bool tmp) const
        p /= "j2c";
        p /= video_identifier ();
 
-       SafeStringStream s;
+       locked_stringstream s;
        s.width (8);
        s << setfill('0') << reel << "_" << frame;
 
@@ -1082,7 +1091,7 @@ Film::length () const
 int
 Film::best_video_frame_rate () const
 {
-       return _playlist->best_dcp_frame_rate ();
+       return _playlist->best_video_frame_rate ();
 }
 
 FrameRateChange
@@ -1096,9 +1105,9 @@ Film::playlist_content_changed (weak_ptr<Content> c, int p, bool frequent)
 {
        _dirty = true;
 
-       if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
-               set_video_frame_rate (_playlist->best_dcp_frame_rate ());
-       } else if (p == AudioContentProperty::AUDIO_STREAMS) {
+       if (p == ContentProperty::VIDEO_FRAME_RATE) {
+               set_video_frame_rate (_playlist->best_video_frame_rate ());
+       } else if (p == AudioContentProperty::STREAMS) {
                signal_changed (NAME);
        }
 
@@ -1268,7 +1277,7 @@ Film::subtitle_language () const
        ContentList cl = content ();
        BOOST_FOREACH (shared_ptr<Content>& c, cl) {
                if (c->subtitle) {
-                       languages.insert (c->subtitle->subtitle_language ());
+                       languages.insert (c->subtitle->language ());
                }
        }
 
@@ -1286,18 +1295,44 @@ Film::subtitle_language () const
 
 /** Change the gains of the supplied AudioMapping to make it a default
  *  for this film.  The defaults are guessed based on what processor (if any)
- *  is in use and the number of input channels.
+ *  is in use, the number of input channels and any filename supplied.
  */
 void
-Film::make_audio_mapping_default (AudioMapping& mapping) const
+Film::make_audio_mapping_default (AudioMapping& mapping, optional<boost::filesystem::path> filename) const
 {
+       static string const regex[] = {
+               ".*[\\._-]L[\\._-].*",
+               ".*[\\._-]R[\\._-].*",
+               ".*[\\._-]C[\\._-].*",
+               ".*[\\._-]Lfe[\\._-].*",
+               ".*[\\._-]Ls[\\._-].*",
+               ".*[\\._-]Rs[\\._-].*"
+       };
+
+       static int const regexes = sizeof(regex) / sizeof(*regex);
+
        if (audio_processor ()) {
                audio_processor()->make_audio_mapping_default (mapping);
        } else {
                mapping.make_zero ();
                if (mapping.input_channels() == 1) {
-                       /* Mono -> Centre */
-                       mapping.set (0, static_cast<int> (dcp::CENTRE), 1);
+                       bool guessed = false;
+
+                       /* See if we can guess where this stream should go */
+                       if (filename) {
+                               for (int i = 0; i < regexes; ++i) {
+                                       boost::regex e (regex[i], boost::regex::icase);
+                                       if (boost::regex_match (filename->string(), e) && i < mapping.output_channels()) {
+                                               mapping.set (0, i, 1);
+                                               guessed = true;
+                                       }
+                               }
+                       }
+
+                       if (!guessed) {
+                               /* If we have no idea, just put it on centre */
+                               mapping.set (0, static_cast<int> (dcp::CENTRE), 1);
+                       }
                } else {
                        /* 1:1 mapping */
                        for (int i = 0; i < min (mapping.input_channels(), mapping.output_channels()); ++i) {
@@ -1372,7 +1407,6 @@ Film::reels () const
        {
                optional<DCPTime> last_split;
                shared_ptr<Content> last_video;
-               ContentList cl = content ();
                BOOST_FOREACH (shared_ptr<Content> c, content ()) {
                        if (c->video) {
                                BOOST_FOREACH (DCPTime t, c->reel_split_points()) {