Reasonably straightforward stuff; main things are adding
[dcpomatic.git] / src / lib / player.cc
index 29523b1eec108e7b1dadd6118735bdc6307ddc65..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"
@@ -46,6 +46,7 @@
 #include "dcp_subtitle_decoder.h"
 #include "audio_processor.h"
 #include "playlist.h"
+#include "referenced_reel_asset.h"
 #include <dcp/reel.h>
 #include <dcp/reel_sound_asset.h>
 #include <dcp/reel_subtitle_asset.h>
@@ -57,7 +58,7 @@
 
 #include "i18n.h"
 
-#define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
+#define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
 
 using std::list;
 using std::cout;
@@ -73,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)
@@ -107,41 +109,17 @@ Player::setup_pieces ()
                shared_ptr<Decoder> decoder;
                optional<FrameRateChange> frc;
 
-               /* Work out a FrameRateChange for the best overlap video for this content, in case we need it below */
-               DCPTime best_overlap_t;
-               shared_ptr<VideoContent> best_overlap;
-               BOOST_FOREACH (shared_ptr<Content> j, _playlist->content ()) {
-                       shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j);
-                       if (!vc) {
-                               continue;
-                       }
-
-                       DCPTime const overlap = max (vc->position(), i->position()) - min (vc->end(), i->end());
-                       if (overlap > best_overlap_t) {
-                               best_overlap = vc;
-                               best_overlap_t = overlap;
-                       }
-               }
-
-               optional<FrameRateChange> best_overlap_frc;
-               if (best_overlap) {
-                       best_overlap_frc = FrameRateChange (best_overlap->video_frame_rate(), _film->video_frame_rate ());
-               } else {
-                       /* No video overlap; e.g. if the DCP is just audio */
-                       best_overlap_frc = FrameRateChange (_film->video_frame_rate(), _film->video_frame_rate ());
-               }
-
                /* FFmpeg */
                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 */
@@ -156,31 +134,57 @@ 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 */
                shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (i);
                if (sc) {
                        decoder.reset (new SndfileDecoder (sc, _fast));
-                       frc = best_overlap_frc;
+
+                       /* Work out a FrameRateChange for the best overlap video for this content */
+                       DCPTime best_overlap_t;
+                       shared_ptr<Content> best_overlap;
+                       BOOST_FOREACH (shared_ptr<Content> j, _playlist->content ()) {
+                               if (!j->video) {
+                                       continue;
+                               }
+
+                               DCPTime const overlap = min (j->end(), i->end()) - max (j->position(), i->position());
+                               if (overlap > best_overlap_t) {
+                                       best_overlap = j;
+                                       best_overlap_t = overlap;
+                               }
+                       }
+
+                       if (best_overlap) {
+                               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 ());
+                       }
                }
 
-               /* SubRipContent */
-               shared_ptr<const SubRipContent> rc = dynamic_pointer_cast<const SubRipContent> (i);
+               /* It's questionable whether subtitle content should have a video frame rate; perhaps
+                  it should be assumed that any subtitle content has been prepared at the same rate
+                  as simultaneous video content (like we do with audio).
+               */
+
+               /* TextSubtitleContent */
+               shared_ptr<const TextSubtitleContent> rc = dynamic_pointer_cast<const TextSubtitleContent> (i);
                if (rc) {
-                       decoder.reset (new SubRipDecoder (rc));
-                       frc = best_overlap_frc;
+                       decoder.reset (new TextSubtitleDecoder (rc));
+                       frc = FrameRateChange (rc->subtitle_video_frame_rate(), _film->video_frame_rate());
                }
 
                /* DCPSubtitleContent */
                shared_ptr<const DCPSubtitleContent> dsc = dynamic_pointer_cast<const DCPSubtitleContent> (i);
                if (dsc) {
                        decoder.reset (new DCPSubtitleDecoder (dsc));
-                       frc = best_overlap_frc;
+                       frc = FrameRateChange (dsc->subtitle_video_frame_rate(), _film->video_frame_rate());
                }
 
                shared_ptr<VideoDecoder> vd = dynamic_pointer_cast<VideoDecoder> (decoder);
@@ -214,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;
@@ -244,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 ();
 }
 
@@ -309,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),
@@ -353,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;
 
@@ -376,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;
@@ -387,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;
                        }
@@ -406,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 */
@@ -415,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 (
@@ -423,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 ()
                                                                        )
                                                                )
                                                        );
@@ -615,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);
 
@@ -637,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 */
@@ -655,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 ());
                        }
@@ -738,26 +756,57 @@ Player::set_play_referenced ()
        _have_valid_pieces = false;
 }
 
-list<shared_ptr<dcp::ReelAsset> >
+list<ReferencedReelAsset>
 Player::get_reel_assets ()
 {
-       list<shared_ptr<dcp::ReelAsset> > a;
+       list<ReferencedReelAsset> a;
 
        BOOST_FOREACH (shared_ptr<Content> i, _playlist->content ()) {
                shared_ptr<DCPContent> j = dynamic_pointer_cast<DCPContent> (i);
                if (!j) {
                        continue;
                }
-               /* XXX: hack hack hack */
-               DCPDecoder decoder (j, false);
-               if (j->reference_video ()) {
-                       a.push_back (decoder.reels().front()->main_picture ());
-               }
-               if (j->reference_audio ()) {
-                       a.push_back (decoder.reels().front()->main_sound ());
+
+               scoped_ptr<DCPDecoder> decoder;
+               try {
+                       decoder.reset (new DCPDecoder (j, _film->log(), false));
+               } catch (...) {
+                       return a;
                }
-               if (j->reference_subtitle ()) {
-                       a.push_back (decoder.reels().front()->main_subtitle ());
+
+               int64_t offset = 0;
+               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 (
+                                       ReferencedReelAsset (
+                                               k->main_picture (),
+                                               DCPTimePeriod (from, from + DCPTime::from_frames (k->main_picture()->duration(), _film->video_frame_rate()))
+                                               )
+                                       );
+                       }
+
+                       if (j->reference_audio ()) {
+                               a.push_back (
+                                       ReferencedReelAsset (
+                                               k->main_sound (),
+                                               DCPTimePeriod (from, from + DCPTime::from_frames (k->main_sound()->duration(), _film->video_frame_rate()))
+                                               )
+                                       );
+                       }
+
+                       if (j->reference_subtitle ()) {
+                               DCPOMATIC_ASSERT (k->main_subtitle ());
+                               a.push_back (
+                                       ReferencedReelAsset (
+                                               k->main_subtitle (),
+                                               DCPTimePeriod (from, from + DCPTime::from_frames (k->main_subtitle()->duration(), _film->video_frame_rate()))
+                                               )
+                                       );
+                       }
+
+                       /* Assume that main picture duration is the length of the reel */
+                       offset += k->main_picture()->duration ();
                }
        }