Reasonably straightforward stuff; main things are adding
[dcpomatic.git] / src / lib / player.cc
index c484ecc9a2de03243ebf2bbd3e091615ee4751a0..2b65fd54e393d5c252fecb67a564115d45bb3630 100644 (file)
@@ -27,8 +27,8 @@
 #include "sndfile_decoder.h"
 #include "sndfile_content.h"
 #include "subtitle_content.h"
-#include "subrip_decoder.h"
-#include "subrip_content.h"
+#include "text_subtitle_decoder.h"
+#include "text_subtitle_content.h"
 #include "dcp_content.h"
 #include "job.h"
 #include "image.h"
@@ -74,6 +74,7 @@ using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
 using boost::optional;
+using boost::scoped_ptr;
 
 Player::Player (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
        : _film (film)
@@ -112,13 +113,13 @@ Player::setup_pieces ()
                shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i);
                if (fc) {
                        decoder.reset (new FFmpegDecoder (fc, _film->log(), _fast));
-                       frc = FrameRateChange (fc->video_frame_rate(), _film->video_frame_rate());
+                       frc = FrameRateChange (fc->video->video_frame_rate(), _film->video_frame_rate());
                }
 
                shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (i);
                if (dc) {
-                       decoder.reset (new DCPDecoder (dc, _fast));
-                       frc = FrameRateChange (dc->video_frame_rate(), _film->video_frame_rate());
+                       decoder.reset (new DCPDecoder (dc, _film->log(), _fast));
+                       frc = FrameRateChange (dc->video->video_frame_rate(), _film->video_frame_rate());
                }
 
                /* ImageContent */
@@ -133,10 +134,10 @@ Player::setup_pieces ()
                        }
 
                        if (!decoder) {
-                               decoder.reset (new ImageDecoder (ic));
+                               decoder.reset (new ImageDecoder (ic, _film->log()));
                        }
 
-                       frc = FrameRateChange (ic->video_frame_rate(), _film->video_frame_rate());
+                       frc = FrameRateChange (ic->video->video_frame_rate(), _film->video_frame_rate());
                }
 
                /* SndfileContent */
@@ -146,22 +147,21 @@ Player::setup_pieces ()
 
                        /* Work out a FrameRateChange for the best overlap video for this content */
                        DCPTime best_overlap_t;
-                       shared_ptr<VideoContent> best_overlap;
+                       shared_ptr<Content> best_overlap;
                        BOOST_FOREACH (shared_ptr<Content> j, _playlist->content ()) {
-                               shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j);
-                               if (!vc) {
+                               if (!j->video) {
                                        continue;
                                }
 
-                               DCPTime const overlap = min (vc->end(), i->end()) - max (vc->position(), i->position());
+                               DCPTime const overlap = min (j->end(), i->end()) - max (j->position(), i->position());
                                if (overlap > best_overlap_t) {
-                                       best_overlap = vc;
+                                       best_overlap = j;
                                        best_overlap_t = overlap;
                                }
                        }
 
                        if (best_overlap) {
-                               frc = FrameRateChange (best_overlap->video_frame_rate(), _film->video_frame_rate ());
+                               frc = FrameRateChange (best_overlap->video->video_frame_rate(), _film->video_frame_rate ());
                        } else {
                                /* No video overlap; e.g. if the DCP is just audio */
                                frc = FrameRateChange (_film->video_frame_rate(), _film->video_frame_rate ());
@@ -173,10 +173,10 @@ Player::setup_pieces ()
                   as simultaneous video content (like we do with audio).
                */
 
-               /* SubRipContent */
-               shared_ptr<const SubRipContent> rc = dynamic_pointer_cast<const SubRipContent> (i);
+               /* TextSubtitleContent */
+               shared_ptr<const TextSubtitleContent> rc = dynamic_pointer_cast<const TextSubtitleContent> (i);
                if (rc) {
-                       decoder.reset (new SubRipDecoder (rc));
+                       decoder.reset (new TextSubtitleDecoder (rc));
                        frc = FrameRateChange (rc->subtitle_video_frame_rate(), _film->video_frame_rate());
                }
 
@@ -218,7 +218,11 @@ Player::playlist_content_changed (weak_ptr<Content> w, int property, bool freque
                property == ContentProperty::TRIM_END ||
                property == ContentProperty::PATH ||
                property == VideoContentProperty::VIDEO_FRAME_TYPE ||
-               property == DCPContentProperty::CAN_BE_PLAYED
+               property == DCPContentProperty::CAN_BE_PLAYED ||
+               property == TextSubtitleContentProperty::TEXT_SUBTITLE_COLOUR ||
+               property == TextSubtitleContentProperty::TEXT_SUBTITLE_OUTLINE ||
+               property == TextSubtitleContentProperty::TEXT_SUBTITLE_OUTLINE_COLOUR ||
+               property == FFmpegContentProperty::SUBTITLE_STREAM
                ) {
 
                _have_valid_pieces = false;
@@ -248,7 +252,7 @@ Player::set_video_container_size (dcp::Size s)
 {
        _video_container_size = s;
 
-       _black_image.reset (new Image (PIX_FMT_RGB24, _video_container_size, true));
+       _black_image.reset (new Image (AV_PIX_FMT_RGB24, _video_container_size, true));
        _black_image->make_black ();
 }
 
@@ -313,7 +317,8 @@ Player::transform_image_subtitles (list<ImageSubtitle> subs) const
                                        scaled_size,
                                        dcp::YUV_TO_RGB_REC601,
                                        i->image->pixel_format (),
-                                       true
+                                       true,
+                                       _fast
                                        ),
                                Position<int> (
                                        lrint (_video_container_size.width * i->rectangle.x),
@@ -357,7 +362,7 @@ Player::get_video (DCPTime time, bool accurate)
 
        /* Find subtitles for possible burn-in */
 
-       PlayerSubtitles ps = get_subtitles (time, DCPTime::from_frames (1, _film->video_frame_rate ()), false, true);
+       PlayerSubtitles ps = get_subtitles (time, DCPTime::from_frames (1, _film->video_frame_rate ()), false, true, accurate);
 
        list<PositionImage> sub_images;
 
@@ -380,7 +385,7 @@ Player::get_video (DCPTime time, bool accurate)
 
        list<shared_ptr<Piece> > ov = overlaps<VideoContent> (
                time,
-               time + DCPTime::from_frames (1, _film->video_frame_rate ()) - DCPTime::delta()
+               time + DCPTime::from_frames (1, _film->video_frame_rate ())
                );
 
        list<shared_ptr<PlayerVideo> > pvf;
@@ -391,17 +396,15 @@ Player::get_video (DCPTime time, bool accurate)
        } else {
                /* Some video content at this time */
                shared_ptr<Piece> last = *(ov.rbegin ());
-               VideoFrameType const last_type = dynamic_pointer_cast<VideoContent> (last->content)->video_frame_type ();
+               VideoFrameType const last_type = last->content->video->video_frame_type ();
 
                /* Get video from appropriate piece(s) */
                BOOST_FOREACH (shared_ptr<Piece> piece, ov) {
 
                        shared_ptr<VideoDecoder> decoder = dynamic_pointer_cast<VideoDecoder> (piece->decoder);
                        DCPOMATIC_ASSERT (decoder);
-                       shared_ptr<VideoContent> video_content = dynamic_pointer_cast<VideoContent> (piece->content);
-                       DCPOMATIC_ASSERT (video_content);
 
-                       shared_ptr<DCPContent> dcp_content = dynamic_pointer_cast<DCPContent> (video_content);
+                       shared_ptr<DCPContent> dcp_content = dynamic_pointer_cast<DCPContent> (piece->content);
                        if (dcp_content && dcp_content->reference_video () && !_play_referenced) {
                                continue;
                        }
@@ -410,8 +413,8 @@ Player::get_video (DCPTime time, bool accurate)
                                /* always use the last video */
                                piece == last ||
                                /* with a corresponding L/R eye if appropriate */
-                               (last_type == VIDEO_FRAME_TYPE_3D_LEFT && video_content->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) ||
-                               (last_type == VIDEO_FRAME_TYPE_3D_RIGHT && video_content->video_frame_type() == VIDEO_FRAME_TYPE_3D_LEFT);
+                               (last_type == VIDEO_FRAME_TYPE_3D_LEFT && piece->content->video->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) ||
+                               (last_type == VIDEO_FRAME_TYPE_3D_RIGHT && piece->content->video->video_frame_type() == VIDEO_FRAME_TYPE_3D_LEFT);
 
                        if (use) {
                                /* We want to use this piece */
@@ -419,7 +422,9 @@ Player::get_video (DCPTime time, bool accurate)
                                if (content_video.empty ()) {
                                        pvf.push_back (black_player_video_frame (time));
                                } else {
-                                       dcp::Size image_size = video_content->scale().size (video_content, _video_container_size, _film->frame_size ());
+                                       dcp::Size image_size = piece->content->video->scale().size (
+                                               piece->content->video, _video_container_size, _film->frame_size ()
+                                               );
 
                                        for (list<ContentVideo>::const_iterator i = content_video.begin(); i != content_video.end(); ++i) {
                                                pvf.push_back (
@@ -427,13 +432,13 @@ Player::get_video (DCPTime time, bool accurate)
                                                                new PlayerVideo (
                                                                        i->image,
                                                                        content_video_to_dcp (piece, i->frame),
-                                                                       video_content->crop (),
-                                                                       video_content->fade (i->frame),
+                                                                       piece->content->video->crop (),
+                                                                       piece->content->video->fade (i->frame),
                                                                        image_size,
                                                                        _video_container_size,
                                                                        i->eyes,
                                                                        i->part,
-                                                                       video_content->colour_conversion ()
+                                                                       piece->content->video->colour_conversion ()
                                                                        )
                                                                )
                                                        );
@@ -619,7 +624,7 @@ Player::content_subtitle_to_dcp (shared_ptr<const Piece> piece, ContentTime t) c
  *  _always_burn_subtitles is true; in this case, all subtitles will be returned.
  */
 PlayerSubtitles
-Player::get_subtitles (DCPTime time, DCPTime length, bool starting, bool burnt)
+Player::get_subtitles (DCPTime time, DCPTime length, bool starting, bool burnt, bool accurate)
 {
        list<shared_ptr<Piece> > subs = overlaps<SubtitleContent> (time, time + length);
 
@@ -641,7 +646,7 @@ Player::get_subtitles (DCPTime time, DCPTime length, bool starting, bool burnt)
                /* XXX: this video_frame_rate() should be the rate that the subtitle content has been prepared for */
                ContentTime const to = from + ContentTime::from_frames (1, _film->video_frame_rate ());
 
-               list<ContentImageSubtitle> image = subtitle_decoder->get_image_subtitles (ContentTimePeriod (from, to), starting);
+               list<ContentImageSubtitle> image = subtitle_decoder->get_image_subtitles (ContentTimePeriod (from, to), starting, accurate);
                for (list<ContentImageSubtitle>::iterator i = image.begin(); i != image.end(); ++i) {
 
                        /* Apply content's subtitle offsets */
@@ -659,20 +664,29 @@ Player::get_subtitles (DCPTime time, DCPTime length, bool starting, bool burnt)
                        ps.image.push_back (i->sub);
                }
 
-               list<ContentTextSubtitle> text = subtitle_decoder->get_text_subtitles (ContentTimePeriod (from, to), starting);
+               list<ContentTextSubtitle> text = subtitle_decoder->get_text_subtitles (ContentTimePeriod (from, to), starting, accurate);
                BOOST_FOREACH (ContentTextSubtitle& ts, text) {
                        BOOST_FOREACH (dcp::SubtitleString s, ts.subs) {
                                s.set_h_position (s.h_position() + subtitle_content->subtitle_x_offset ());
                                s.set_v_position (s.v_position() + subtitle_content->subtitle_y_offset ());
                                float const xs = subtitle_content->subtitle_x_scale();
                                float const ys = subtitle_content->subtitle_y_scale();
-                               float const average = s.size() * (xs + ys) / 2;
-                               s.set_size (average);
+                               float size = s.size();
+
+                               /* Adjust size to express the common part of the scaling;
+                                  e.g. if xs = ys = 0.5 we scale size by 2.
+                               */
+                               if (xs > 1e-5 && ys > 1e-5) {
+                                       size *= 1 / min (1 / xs, 1 / ys);
+                               }
+                               s.set_size (size);
+
+                               /* Then express aspect ratio changes */
                                if (fabs (1.0 - xs / ys) > dcp::ASPECT_ADJUST_EPSILON) {
                                        s.set_aspect_adjust (xs / ys);
                                }
-                               s.set_in (dcp::Time(content_subtitle_to_dcp (*j, ts.period().from).seconds()));
-                               s.set_out (dcp::Time(content_subtitle_to_dcp (*j, ts.period().to).seconds()));
+                               s.set_in (dcp::Time(content_subtitle_to_dcp (*j, ts.period().from).seconds(), 1000));
+                               s.set_out (dcp::Time(content_subtitle_to_dcp (*j, ts.period().to).seconds(), 1000));
                                ps.text.push_back (s);
                                ps.add_fonts (subtitle_content->fonts ());
                        }
@@ -752,9 +766,16 @@ Player::get_reel_assets ()
                if (!j) {
                        continue;
                }
-               DCPDecoder decoder (j, false);
+
+               scoped_ptr<DCPDecoder> decoder;
+               try {
+                       decoder.reset (new DCPDecoder (j, _film->log(), false));
+               } catch (...) {
+                       return a;
+               }
+
                int64_t offset = 0;
-               BOOST_FOREACH (shared_ptr<dcp::Reel> k, decoder.reels()) {
+               BOOST_FOREACH (shared_ptr<dcp::Reel> k, decoder->reels()) {
                        DCPTime const from = i->position() + DCPTime::from_frames (offset, _film->video_frame_rate());
                        if (j->reference_video ()) {
                                a.push_back (
@@ -775,6 +796,7 @@ Player::get_reel_assets ()
                        }
 
                        if (j->reference_subtitle ()) {
+                               DCPOMATIC_ASSERT (k->main_subtitle ());
                                a.push_back (
                                        ReferencedReelAsset (
                                                k->main_subtitle (),