Allow specification of video bit rate separately for J2K and MPEG2.
[dcpomatic.git] / src / lib / film.cc
index a88e8a855573d62a270ccf9a2eb35a428b9df0aa..a3e78e8773e557cbb00b345d33c469b910537af0 100644 (file)
@@ -36,7 +36,7 @@
 #include "cross.h"
 #include "dcp_content.h"
 #include "dcp_content_type.h"
-#include "dcp_encoder.h"
+#include "dcp_film_encoder.h"
 #include "dcpomatic_log.h"
 #include "digester.h"
 #include "environment_info.h"
@@ -57,6 +57,7 @@
 #include "text_content.h"
 #include "transcode_job.h"
 #include "upload_job.h"
+#include "variant.h"
 #include "video_content.h"
 #include "version.h"
 #include <libcxml/cxml.h>
@@ -162,12 +163,12 @@ Film::Film (optional<boost::filesystem::path> dir)
        , _resolution (Resolution::TWO_K)
        , _encrypted (false)
        , _context_id (dcp::make_uuid ())
-       , _j2k_bandwidth (Config::instance()->default_j2k_bandwidth ())
        , _video_frame_rate (24)
        , _audio_channels (Config::instance()->default_dcp_audio_channels ())
        , _three_d (false)
        , _sequence (true)
        , _interop (Config::instance()->default_interop ())
+       , _video_encoding(VideoEncoding::JPEG2000)
        , _limit_to_smpte_bv20(false)
        , _audio_processor (0)
        , _reel_type (ReelType::SINGLE)
@@ -201,6 +202,10 @@ Film::Film (optional<boost::filesystem::path> dir)
                _studio = metadata["studio"];
        }
 
+       for (auto encoding: {VideoEncoding::JPEG2000, VideoEncoding::MPEG2}) {
+               _video_bit_rate[encoding] = Config::instance()->default_video_bit_rate(encoding);
+       }
+
        _playlist_change_connection = _playlist->Change.connect (bind (&Film::playlist_change, this, _1));
        _playlist_order_changed_connection = _playlist->OrderChange.connect (bind (&Film::playlist_order_changed, this));
        _playlist_content_change_connection = _playlist->ContentChange.connect (bind (&Film::playlist_content_change, this, _1, _2, _3, _4));
@@ -239,7 +244,7 @@ Film::video_identifier () const
                + "_" + resolution_to_string (_resolution)
                + "_" + _playlist->video_identifier()
                + "_" + raw_convert<string>(_video_frame_rate)
-               + "_" + raw_convert<string>(j2k_bandwidth());
+               + "_" + raw_convert<string>(video_bit_rate(video_encoding()));
 
        if (encrypted ()) {
                /* This is insecure but hey, the key is in plaintext in metadata.xml */
@@ -250,6 +255,9 @@ Film::video_identifier () const
 
        if (_interop) {
                s += "_I";
+               if (_video_encoding == VideoEncoding::MPEG2) {
+                       s += "_M";
+               }
        } else {
                s += "_S";
                if (_limit_to_smpte_bv20) {
@@ -396,7 +404,8 @@ Film::metadata (bool with_content_paths) const
        }
 
        cxml::add_text_child(root, "Resolution", resolution_to_string(_resolution));
-       cxml::add_text_child(root, "J2KBandwidth", raw_convert<string>(_j2k_bandwidth));
+       cxml::add_text_child(root, "J2KVideoBitRate", raw_convert<string>(_video_bit_rate[VideoEncoding::JPEG2000]));
+       cxml::add_text_child(root, "MPEG2VideoBitRate", raw_convert<string>(_video_bit_rate[VideoEncoding::MPEG2]));
        cxml::add_text_child(root, "VideoFrameRate", raw_convert<string>(_video_frame_rate));
        cxml::add_text_child(root, "AudioFrameRate", raw_convert<string>(_audio_frame_rate));
        cxml::add_text_child(root, "ISDCFDate", boost::gregorian::to_iso_string(_isdcf_date));
@@ -404,6 +413,7 @@ Film::metadata (bool with_content_paths) const
        cxml::add_text_child(root, "ThreeD", _three_d ? "1" : "0");
        cxml::add_text_child(root, "Sequence", _sequence ? "1" : "0");
        cxml::add_text_child(root, "Interop", _interop ? "1" : "0");
+       cxml::add_text_child(root, "VideoEncoding", video_encoding_to_string(_video_encoding));
        cxml::add_text_child(root, "LimitToSMPTEBv20", _limit_to_smpte_bv20 ? "1" : "0");
        cxml::add_text_child(root, "Encrypted", _encrypted ? "1" : "0");
        cxml::add_text_child(root, "Key", _key.hex ());
@@ -507,7 +517,13 @@ Film::read_metadata (optional<boost::filesystem::path> path)
 {
        if (!path) {
                if (dcp::filesystem::exists(file("metadata")) && !dcp::filesystem::exists(file(metadata_file))) {
-                       throw runtime_error (_("This film was created with an older version of DCP-o-matic, and unfortunately it cannot be loaded into this version.  You will need to create a new Film, re-add your content and set it up again.  Sorry!"));
+                       throw runtime_error(
+                               variant::insert_dcpomatic(
+                                       _("This film was created with an older version of %1, and unfortunately it cannot "
+                                         "be loaded into this version.  You will need to create a new Film, re-add your "
+                                         "content and set it up again.  Sorry!")
+                                       )
+                               );
                }
 
                path = file (metadata_file);
@@ -522,7 +538,7 @@ Film::read_metadata (optional<boost::filesystem::path> path)
 
        _state_version = f.number_child<int> ("Version");
        if (_state_version > current_state_version) {
-               throw runtime_error (_("This film was created with a newer version of DCP-o-matic, and it cannot be loaded into this version.  Sorry!"));
+               throw runtime_error(variant::insert_dcpomatic(_("This film was created with a newer version of %1, and it cannot be loaded into this version.  Sorry!")));
        } else if (_state_version < current_state_version) {
                /* This is an older version; save a copy (if we haven't already) */
                auto const older = path->parent_path() / String::compose("metadata.%1.xml", _state_version);
@@ -562,7 +578,12 @@ Film::read_metadata (optional<boost::filesystem::path> path)
        }
 
        _resolution = string_to_resolution (f.string_child ("Resolution"));
-       _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
+       if (auto j2k = f.optional_number_child<int>("J2KBandwidth")) {
+               _video_bit_rate[VideoEncoding::JPEG2000] = *j2k;
+       } else {
+               _video_bit_rate[VideoEncoding::JPEG2000] = f.number_child<int64_t>("J2KVideoBitRate");
+       }
+       _video_bit_rate[VideoEncoding::MPEG2] = f.optional_number_child<int64_t>("MPEG2VideoBitRate").get_value_or(Config::instance()->default_video_bit_rate(VideoEncoding::MPEG2));
        _video_frame_rate = f.number_child<int> ("VideoFrameRate");
        _audio_frame_rate = f.optional_number_child<int>("AudioFrameRate").get_value_or(48000);
        _encrypted = f.bool_child ("Encrypted");
@@ -584,6 +605,9 @@ Film::read_metadata (optional<boost::filesystem::path> path)
 
        _three_d = f.bool_child ("ThreeD");
        _interop = f.bool_child ("Interop");
+       if (auto encoding = f.optional_string_child("VideoEncoding")) {
+               _video_encoding = video_encoding_from_string(*encoding);
+       }
        _limit_to_smpte_bv20 = f.optional_bool_child("LimitToSMPTEBv20").get_value_or(false);
        _key = dcp::Key (f.string_child ("Key"));
        _context_id = f.optional_string_child("ContextID").get_value_or (dcp::make_uuid ());
@@ -1162,10 +1186,10 @@ Film::set_resolution (Resolution r, bool explicit_user)
 
 
 void
-Film::set_j2k_bandwidth (int b)
+Film::set_video_bit_rate(VideoEncoding encoding, int64_t bit_rate)
 {
-       FilmChangeSignaller ch(this, FilmProperty::J2K_BANDWIDTH);
-       _j2k_bandwidth = b;
+       FilmChangeSignaller ch(this, FilmProperty::VIDEO_BIT_RATE);
+       _video_bit_rate[encoding] = bit_rate;
 }
 
 /** @param f New frame rate.
@@ -1208,6 +1232,15 @@ Film::set_interop (bool i)
 }
 
 
+void
+Film::set_video_encoding(VideoEncoding encoding)
+{
+       FilmChangeSignaller ch(this, FilmProperty::VIDEO_ENCODING);
+       _video_encoding = encoding;
+       check_settings_consistency();
+}
+
+
 void
 Film::set_limit_to_smpte_bv20(bool limit)
 {
@@ -1582,7 +1615,7 @@ Film::check_settings_consistency ()
                        } else if (!atmos_rate && rate != video_frame_rate()) {
                                atmos_rate = rate;
                                set_video_frame_rate (rate, false);
-                               Message (_("DCP-o-matic had to change your settings so that the film's frame rate is the same as that of your Atmos content."));
+                               Message(variant::insert_dcpomatic(_("%1 had to change your settings so that the film's frame rate is the same as that of your Atmos content.")));
                        }
                }
        }
@@ -1614,7 +1647,7 @@ Film::check_settings_consistency ()
        }
 
        if (change_made) {
-               Message (_("DCP-o-matic had to change your settings for referring to DCPs as OV.  Please review those settings to make sure they are what you want."));
+               Message(variant::insert_dcpomatic(_("%1 had to change your settings for referring to DCPs as OV.  Please review those settings to make sure they are what you want.")));
        }
 
        if (reel_type() == ReelType::CUSTOM) {
@@ -1625,14 +1658,30 @@ Film::check_settings_consistency ()
 
                if (too_late != boundaries.end()) {
                        if (std::distance(too_late, boundaries.end()) > 1) {
-                               Message(_("DCP-o-matic had to remove some of your custom reel boundaries as they no longer lie within the film."));
+                               Message(variant::insert_dcpomatic(_("%1 had to remove some of your custom reel boundaries as they no longer lie within the film.")));
                        } else {
-                               Message(_("DCP-o-matic had to remove one of your custom reel boundaries as it no longer lies within the film."));
+                               Message(variant::insert_dcpomatic(_("%1 had to remove one of your custom reel boundaries as it no longer lies within the film.")));
                        }
                        boundaries.erase(too_late, boundaries.end());
                        set_custom_reel_boundaries(boundaries);
                }
        }
+
+       auto const hd = Ratio::from_id("178");
+
+       if (video_encoding() == VideoEncoding::MPEG2) {
+               if (container() != hd) {
+                       set_container(hd);
+                       Message(_("DCP-o-matic had to set your container to 1920x1080 as it's the only one that can be used with MPEG2 encoding."));
+               }
+               if (three_d()) {
+                       set_three_d(false);
+                       Message(_("DCP-o-matic had to set your film to 2D as 3D is not yet supported with MPEG2 encoding."));
+               }
+       } else if (container() == hd && !Config::instance()->allow_any_container()) {
+               set_container(Ratio::from_id("185"));
+               Message(_("DCP-o-matic set your container to DCI Flat as it was previously 1920x1080 and that is not a standard ratio with JPEG2000 encoding."));
+       }
 }
 
 void
@@ -1758,7 +1807,7 @@ Film::make_kdm(boost::filesystem::path cpl_file, dcp::LocalTime from, dcp::Local
 uint64_t
 Film::required_disk_space () const
 {
-       return _playlist->required_disk_space (shared_from_this(), j2k_bandwidth(), audio_channels(), audio_frame_rate());
+       return _playlist->required_disk_space (shared_from_this(), video_bit_rate(video_encoding()), audio_channels(), audio_frame_rate());
 }
 
 /** This method checks the disk that the Film is on and tries to decide whether or not
@@ -1890,7 +1939,7 @@ Film::reels () const
                /* Integer-divide reel length by the size of one frame to give the number of frames per reel,
                 * making sure we don't go less than 1s long.
                 */
-               Frame const reel_in_frames = max(_reel_length / ((j2k_bandwidth() / video_frame_rate()) / 8), static_cast<Frame>(video_frame_rate()));
+               Frame const reel_in_frames = max(_reel_length / ((video_bit_rate(video_encoding()) / video_frame_rate()) / 8), static_cast<Frame>(video_frame_rate()));
                while (current < len) {
                        DCPTime end = min (len, current + DCPTime::from_frames (reel_in_frames, video_frame_rate ()));
                        periods.emplace_back(current, end);
@@ -1934,7 +1983,9 @@ Film::use_template (string name)
        _dcp_content_type = _template_film->_dcp_content_type;
        _container = _template_film->_container;
        _resolution = _template_film->_resolution;
-       _j2k_bandwidth = _template_film->_j2k_bandwidth;
+       for (auto encoding: { VideoEncoding::JPEG2000, VideoEncoding::MPEG2 }) {
+               _video_bit_rate[encoding] = _template_film->_video_bit_rate[encoding];
+       }
        _video_frame_rate = _template_film->_video_frame_rate;
        _encrypted = _template_film->_encrypted;
        _audio_channels = _template_film->_audio_channels;