Implement missing support for DCP subs inside DCP content (#631).
authorCarl Hetherington <cth@carlh.net>
Sun, 5 Jul 2015 23:28:42 +0000 (00:28 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 5 Jul 2015 23:28:42 +0000 (00:28 +0100)
src/lib/dcp_decoder.cc
src/lib/dcp_decoder.h
test/dcp_subtitle_test.cc

index 0ec50e0cd24ff9a52523707a121ddb50acb22544..0c7e7589bc2d9772d185a3d1e906a82e4e1c0052 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2015 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
 #include <dcp/stereo_picture_asset.h>
 #include <dcp/reel_picture_asset.h>
 #include <dcp/reel_sound_asset.h>
+#include <dcp/reel_subtitle_asset.h>
 #include <dcp/mono_picture_frame.h>
 #include <dcp/stereo_picture_frame.h>
 #include <dcp/sound_frame.h>
+#include <boost/foreach.hpp>
 
 using std::list;
 using std::cout;
@@ -102,7 +104,25 @@ DCPDecoder::pass ()
                audio (_dcp_content->audio_stream(), data, _next);
        }
 
-       /* XXX: subtitle */
+       if ((*_reel)->main_subtitle ()) {
+               int64_t const entry_point = (*_reel)->main_subtitle()->entry_point ();
+               list<dcp::SubtitleString> subs = (*_reel)->main_subtitle()->subtitle_asset()->subtitles_during (
+                       dcp::Time (entry_point + frame, vfr, vfr),
+                       dcp::Time (entry_point + frame + 1, vfr, vfr),
+                       true
+                       );
+
+               if (!subs.empty ()) {
+                       /* XXX: assuming that all `subs' are at the same time; maybe this is ok */
+                       text_subtitle (
+                               ContentTimePeriod (
+                                       ContentTime::from_seconds (subs.front().in().as_seconds ()),
+                                       ContentTime::from_seconds (subs.front().out().as_seconds ())
+                                       ),
+                               subs
+                               );
+               }
+       }
 
        _next += ContentTime::from_frames (1, vfr);
 
@@ -139,8 +159,35 @@ DCPDecoder::image_subtitles_during (ContentTimePeriod, bool) const
 }
 
 list<ContentTimePeriod>
-DCPDecoder::text_subtitles_during (ContentTimePeriod, bool) const
+DCPDecoder::text_subtitles_during (ContentTimePeriod period, bool starting) const
 {
-       /* XXX */
-       return list<ContentTimePeriod> ();
+       /* XXX: inefficient */
+
+       list<ContentTimePeriod> ctp;
+       float const vfr = _dcp_content->video_frame_rate ();
+
+       BOOST_FOREACH (shared_ptr<dcp::Reel> r, _reels) {
+               if (!r->main_subtitle ()) {
+                       continue;
+               }
+
+               int64_t const entry_point = r->main_subtitle()->entry_point ();
+
+               list<dcp::SubtitleString> subs = r->main_subtitle()->subtitle_asset()->subtitles_during (
+                       dcp::Time (period.from.seconds ()) - dcp::Time (entry_point, vfr, vfr),
+                       dcp::Time (period.to.seconds ()) - dcp::Time (entry_point, vfr, vfr),
+                       starting
+                       );
+
+               BOOST_FOREACH (dcp::SubtitleString const & s, subs) {
+                       ctp.push_back (
+                               ContentTimePeriod (
+                                       ContentTime::from_seconds (s.in().as_seconds ()),
+                                       ContentTime::from_seconds (s.out().as_seconds ())
+                                       )
+                               );
+               }
+       }
+
+       return ctp;
 }
index 7d26139ba47ab6d62a403c066fc091aab30f1c89..d735ca6a5515b65f34ad713cb9cf58b401d336d4 100644 (file)
@@ -31,6 +31,7 @@ namespace dcp {
 
 class DCPContent;
 class Log;
+struct dcp_subtitle_within_dcp_test;
 
 class DCPDecoder : public VideoDecoder, public AudioDecoder, public SubtitleDecoder
 {
@@ -38,6 +39,8 @@ public:
        DCPDecoder (boost::shared_ptr<const DCPContent>);
 
 private:
+       friend struct dcp_subtitle_within_dcp_test;
+
        bool pass ();
        void seek (ContentTime t, bool accurate);
 
index e8a5a4c04cb25d0ae08b4c590fcee48e74faa48c..9007ad6114ac2ce418121019fbb7be17f119aa63 100644 (file)
 #include <boost/test/unit_test.hpp>
 #include "lib/film.h"
 #include "lib/dcp_subtitle_content.h"
+#include "lib/dcp_content.h"
 #include "lib/ratio.h"
+#include "lib/dcp_decoder.h"
 #include "lib/dcp_content_type.h"
 #include "test.h"
 
 using std::cout;
+using std::list;
 using boost::shared_ptr;
 
 /** Test pass-through of a very simple DCP subtitle file */
@@ -51,3 +54,44 @@ BOOST_AUTO_TEST_CASE (dcp_subtitle_test)
 
        check_dcp ("test/data/dcp_subtitle_test", film->dir (film->dcp_name ()));
 }
+
+/** Test parsing of a subtitle within an existing DCP */
+BOOST_AUTO_TEST_CASE (dcp_subtitle_within_dcp_test)
+{
+       shared_ptr<Film> film = new_test_film ("dcp_subtitle_within_dcp_test");
+       film->set_container (Ratio::from_id ("185"));
+       film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
+       film->set_name ("frobozz");
+       shared_ptr<DCPContent> content (new DCPContent (film, private_data / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV"));
+       film->examine_and_add_content (content);
+       wait_for_jobs ();
+
+       shared_ptr<DCPDecoder> decoder (new DCPDecoder (content));
+
+       list<ContentTimePeriod> ctp = decoder->text_subtitles_during (
+               ContentTimePeriod (
+                       ContentTime::from_seconds (25),
+                       ContentTime::from_seconds (26)
+                       ),
+               true
+               );
+
+       BOOST_REQUIRE_EQUAL (ctp.size(), 2);
+       BOOST_CHECK_EQUAL (ctp.front().from, ContentTime::from_seconds (25 + 12 * 0.04));
+       BOOST_CHECK_EQUAL (ctp.front().to, ContentTime::from_seconds (26 + 4 * 0.04));
+       BOOST_CHECK_EQUAL (ctp.back().from, ContentTime::from_seconds (25 + 12 * 0.04));
+       BOOST_CHECK_EQUAL (ctp.back().to, ContentTime::from_seconds (26 + 4 * 0.04));
+
+       list<ContentTextSubtitle> subs = decoder->get_text_subtitles (
+               ContentTimePeriod (
+                       ContentTime::from_seconds (25),
+                       ContentTime::from_seconds (26)
+                       ),
+               true
+               );
+
+       BOOST_REQUIRE_EQUAL (subs.size(), 1);
+       BOOST_REQUIRE_EQUAL (subs.front().subs.size(), 2);
+       BOOST_CHECK_EQUAL (subs.front().subs.front().text(), "Noch mal.");
+       BOOST_CHECK_EQUAL (subs.front().subs.back().text(), "Encore une fois.");
+}