Fix incorrect reel numbers in subtitle XML/MXF.
[dcpomatic.git] / src / lib / reel_writer.cc
index 784eed1efb295d800dd84d6d092f3f1e9da80292..8334dc4772d3208e6b95af6fd46f8aeb19136ec7 100644 (file)
@@ -41,6 +41,7 @@
 #include <dcp/certificate_chain.h>
 #include <dcp/interop_subtitle_asset.h>
 #include <dcp/smpte_subtitle_asset.h>
+#include <dcp/raw_convert.h>
 #include <boost/foreach.hpp>
 
 #include "i18n.h"
@@ -57,6 +58,7 @@ using boost::shared_ptr;
 using boost::optional;
 using boost::dynamic_pointer_cast;
 using dcp::Data;
+using dcp::raw_convert;
 
 int const ReelWriter::_info_size = 48;
 
@@ -88,6 +90,7 @@ ReelWriter::ReelWriter (
 
        if (_film->encrypted ()) {
                _picture_asset->set_key (_film->key ());
+               _picture_asset->set_context_id (_film->context_id ());
        }
 
        _picture_asset->set_file (
@@ -275,6 +278,7 @@ ReelWriter::finish ()
 {
        if (!_picture_asset_writer->finalize ()) {
                /* Nothing was written to the picture asset */
+               LOG_GENERAL ("Nothing was written to reel %1 of %2", _reel_index, _reel_count);
                _picture_asset.reset ();
        }
 
@@ -343,16 +347,28 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                        reel_picture_asset.reset (new dcp::ReelStereoPictureAsset (stereo, 0));
                }
        } else {
+               LOG_GENERAL ("no picture asset of our own; look through %1", refs.size());
                /* We don't have a picture asset of our own; hopefully we have one to reference */
                BOOST_FOREACH (ReferencedReelAsset j, refs) {
                        shared_ptr<dcp::ReelPictureAsset> k = dynamic_pointer_cast<dcp::ReelPictureAsset> (j.asset);
+                       if (k) {
+                               LOG_GENERAL ("candidate picture asset period is %1-%2", j.period.from.get(), j.period.to.get());
+                       }
                        if (k && j.period == _period) {
                                reel_picture_asset = k;
                        }
                }
        }
 
+       LOG_GENERAL ("create_reel for %1-%2; %3 of %4", _period.from.get(), _period.to.get(), _reel_index, _reel_count);
+
        DCPOMATIC_ASSERT (reel_picture_asset);
+       if (reel_picture_asset->duration() != _period.duration().frames_round (_film->video_frame_rate ())) {
+               throw ProgrammingError (
+                       __FILE__, __LINE__,
+                       String::compose ("%1 vs %2", reel_picture_asset->duration(), _period.duration().frames_round (_film->video_frame_rate ()))
+                       );
+       }
        reel->add (reel_picture_asset);
 
        /* If we have a hash for this asset in the CPL, assume that it is correct */
@@ -360,15 +376,17 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                reel_picture_asset->asset_ref()->set_hash (reel_picture_asset->hash().get());
        }
 
+       shared_ptr<dcp::ReelSoundAsset> reel_sound_asset;
+
        if (_sound_asset) {
                /* We have made a sound asset of our own.  Put it into the reel */
-               reel->add (shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (_sound_asset, 0)));
+               reel_sound_asset.reset (new dcp::ReelSoundAsset (_sound_asset, 0));
        } else {
                /* We don't have a sound asset of our own; hopefully we have one to reference */
                BOOST_FOREACH (ReferencedReelAsset j, refs) {
                        shared_ptr<dcp::ReelSoundAsset> k = dynamic_pointer_cast<dcp::ReelSoundAsset> (j.asset);
                        if (k && j.period == _period) {
-                               reel->add (k);
+                               reel_sound_asset = k;
                                /* If we have a hash for this asset in the CPL, assume that it is correct */
                                if (k->hash()) {
                                        k->asset_ref()->set_hash (k->hash().get());
@@ -377,6 +395,19 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                }
        }
 
+       DCPOMATIC_ASSERT (reel_sound_asset);
+       if (reel_sound_asset->duration() != _period.duration().frames_round (_film->video_frame_rate ())) {
+               LOG_ERROR (
+                       "Reel sound asset has length %1 but reel period is %2",
+                       reel_sound_asset->duration(),
+                       _period.duration().frames_round(_film->video_frame_rate())
+                       );
+               DCPOMATIC_ASSERT (reel_sound_asset->duration() == _period.duration().frames_round (_film->video_frame_rate ()));
+       }
+       reel->add (reel_sound_asset);
+
+       shared_ptr<dcp::ReelSubtitleAsset> reel_subtitle_asset;
+
        if (_subtitle_asset) {
 
                boost::filesystem::path liberation_normal;
@@ -417,20 +448,20 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                                );
                }
 
-               reel->add (shared_ptr<dcp::ReelSubtitleAsset> (
-                                  new dcp::ReelSubtitleAsset (
-                                          _subtitle_asset,
-                                          dcp::Fraction (_film->video_frame_rate(), 1),
-                                          reel_picture_asset->intrinsic_duration (),
-                                          0
-                                          )
-                                  ));
+               reel_subtitle_asset.reset (
+                       new dcp::ReelSubtitleAsset (
+                               _subtitle_asset,
+                               dcp::Fraction (_film->video_frame_rate(), 1),
+                               reel_picture_asset->intrinsic_duration (),
+                               0
+                               )
+                       );
        } else {
                /* We don't have a subtitle asset of our own; hopefully we have one to reference */
                BOOST_FOREACH (ReferencedReelAsset j, refs) {
                        shared_ptr<dcp::ReelSubtitleAsset> k = dynamic_pointer_cast<dcp::ReelSubtitleAsset> (j.asset);
                        if (k && j.period == _period) {
-                               reel->add (k);
+                               reel_subtitle_asset = k;
                                /* If we have a hash for this asset in the CPL, assume that it is correct */
                                if (k->hash()) {
                                        k->asset_ref()->set_hash (k->hash().get());
@@ -439,6 +470,11 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                }
        }
 
+       if (reel_subtitle_asset) {
+               DCPOMATIC_ASSERT (reel_subtitle_asset->duration() == _period.duration().frames_round (_film->video_frame_rate ()));
+               reel->add (reel_subtitle_asset);
+       }
+
        return reel;
 }
 
@@ -472,7 +508,7 @@ ReelWriter::write (shared_ptr<const AudioBuffers> audio)
                _sound_asset_writer->write (audio->data(), audio->frames());
        }
 
-       ++_total_written_audio_frames;
+       _total_written_audio_frames += audio->frames ();
 }
 
 void
@@ -487,14 +523,14 @@ ReelWriter::write (PlayerSubtitles subs)
                        shared_ptr<dcp::InteropSubtitleAsset> s (new dcp::InteropSubtitleAsset ());
                        s->set_movie_title (_film->name ());
                        s->set_language (lang);
-                       s->set_reel_number ("1");
+                       s->set_reel_number (raw_convert<string> (_reel_index + 1));
                        _subtitle_asset = s;
                } else {
                        shared_ptr<dcp::SMPTESubtitleAsset> s (new dcp::SMPTESubtitleAsset ());
                        s->set_content_title_text (_film->name ());
                        s->set_language (lang);
                        s->set_edit_rate (dcp::Fraction (_film->video_frame_rate (), 1));
-                       s->set_reel_number (1);
+                       s->set_reel_number (_reel_index + 1);
                        s->set_time_code_rate (_film->video_frame_rate ());
                        s->set_start_time (dcp::Time ());
                        if (_film->encrypted ()) {