Allow + in DCP names for ISDCF RU rating strings.
[dcpomatic.git] / src / lib / player.cc
index a8a72a229a2b756e5e9e03d3fe3b6de228ef409b..6fcf57949d8340e1a225a4c2c7dd2f9a71546a88 100644 (file)
@@ -49,7 +49,6 @@
 #include "image_decoder.h"
 #include "compose.hpp"
 #include "shuffler.h"
-#include "delay.h"
 #include <dcp/reel.h>
 #include <dcp/reel_sound_asset.h>
 #include <dcp/reel_subtitle_asset.h>
@@ -79,6 +78,12 @@ using boost::dynamic_pointer_cast;
 using boost::optional;
 using boost::scoped_ptr;
 
+int const PlayerProperty::VIDEO_CONTAINER_SIZE = 700;
+int const PlayerProperty::PLAYLIST = 701;
+int const PlayerProperty::FILM_CONTAINER = 702;
+int const PlayerProperty::FILM_VIDEO_FRAME_RATE = 703;
+int const PlayerProperty::DCP_DECODE_REDUCTION = 704;
+
 Player::Player (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
        : _film (film)
        , _playlist (playlist)
@@ -90,7 +95,6 @@ Player::Player (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist
        , _play_referenced (false)
        , _audio_merger (_film->audio_frame_rate())
        , _shuffler (0)
-       , _delay (0)
 {
        _film_changed_connection = _film->Changed.connect (bind (&Player::film_changed, this, _1));
        _playlist_changed_connection = _playlist->Changed.connect (bind (&Player::playlist_changed, this));
@@ -105,7 +109,6 @@ Player::Player (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist
 Player::~Player ()
 {
        delete _shuffler;
-       delete _delay;
 }
 
 void
@@ -117,10 +120,6 @@ Player::setup_pieces ()
        _shuffler = new Shuffler();
        _shuffler->Video.connect(bind(&Player::video, this, _1, _2));
 
-       delete _delay;
-       _delay = new Delay();
-       _delay->Video.connect(bind(&Player::video, this, _1, _2));
-
        BOOST_FOREACH (shared_ptr<Content> i, _playlist->content ()) {
 
                if (!i->paths_valid ()) {
@@ -159,10 +158,7 @@ Player::setup_pieces ()
                                /* We need a Shuffler to cope with 3D L/R video data arriving out of sequence */
                                decoder->video->Data.connect (bind (&Shuffler::video, _shuffler, weak_ptr<Piece>(piece), _1));
                        } else {
-                               /* We need a Delay to give a little wiggle room to ensure that relevent subtitles arrive at the
-                                  player before the video that requires them.
-                               */
-                               decoder->video->Data.connect (bind (&Delay::video, _delay, weak_ptr<Piece>(piece), _1));
+                               decoder->video->Data.connect (bind (&Player::video, this, weak_ptr<Piece>(piece), _1));
                        }
                }
 
@@ -221,7 +217,7 @@ Player::playlist_content_changed (weak_ptr<Content> w, int property, bool freque
                ) {
 
                _have_valid_pieces = false;
-               Changed (frequent);
+               Changed (property, frequent);
 
        } else if (
                property == SubtitleContentProperty::LINE_SPACING ||
@@ -241,7 +237,7 @@ Player::playlist_content_changed (weak_ptr<Content> w, int property, bool freque
                property == VideoContentProperty::FADE_OUT
                ) {
 
-               Changed (frequent);
+               Changed (property, frequent);
        }
 }
 
@@ -257,14 +253,14 @@ Player::set_video_container_size (dcp::Size s)
        _black_image.reset (new Image (AV_PIX_FMT_RGB24, _video_container_size, true));
        _black_image->make_black ();
 
-       Changed (false);
+       Changed (PlayerProperty::VIDEO_CONTAINER_SIZE, false);
 }
 
 void
 Player::playlist_changed ()
 {
        _have_valid_pieces = false;
-       Changed (false);
+       Changed (PlayerProperty::PLAYLIST, false);
 }
 
 void
@@ -276,17 +272,19 @@ Player::film_changed (Film::Property p)
        */
 
        if (p == Film::CONTAINER) {
-               Changed (false);
+               Changed (PlayerProperty::FILM_CONTAINER, false);
        } else if (p == Film::VIDEO_FRAME_RATE) {
                /* Pieces contain a FrameRateChange which contains the DCP frame rate,
                   so we need new pieces here.
                */
                _have_valid_pieces = false;
-               Changed (false);
+               Changed (PlayerProperty::FILM_VIDEO_FRAME_RATE, false);
        } else if (p == Film::AUDIO_PROCESSOR) {
                if (_film->audio_processor ()) {
                        _audio_processor = _film->audio_processor()->clone (_film->audio_frame_rate ());
                }
+       } else if (p == Film::AUDIO_CHANNELS) {
+               _audio_merger.clear ();
        }
 }
 
@@ -303,18 +301,6 @@ Player::transform_image_subtitles (list<ImageSubtitle> subs) const
                /* We will scale the subtitle up to fit _video_container_size */
                dcp::Size scaled_size (i->rectangle.width * _video_container_size.width, i->rectangle.height * _video_container_size.height);
 
-               /* Then we need a corrective translation, consisting of two parts:
-                *
-                * 1.  that which is the result of the scaling of the subtitle by _video_container_size; this will be
-                *     rect.x * _video_container_size.width and rect.y * _video_container_size.height.
-                *
-                * 2.  that to shift the origin of the scale by subtitle_scale to the centre of the subtitle; this will be
-                *     (width_before_subtitle_scale * (1 - subtitle_x_scale) / 2) and
-                *     (height_before_subtitle_scale * (1 - subtitle_y_scale) / 2).
-                *
-                * Combining these two translations gives these expressions.
-                */
-
                all.push_back (
                        PositionImage (
                                i->image->scale (
@@ -347,7 +333,9 @@ Player::black_player_video_frame (Eyes eyes) const
                        _video_container_size,
                        eyes,
                        PART_WHOLE,
-                       PresetColourConversion::all().front().conversion
+                       PresetColourConversion::all().front().conversion,
+                       boost::weak_ptr<Content>(),
+                       boost::optional<Frame>()
                )
        );
 }
@@ -660,8 +648,11 @@ Player::pass ()
 
        if (done) {
                _shuffler->flush ();
-               _delay->flush ();
+               for (list<pair<shared_ptr<PlayerVideo>, DCPTime> >::const_iterator i = _delay.begin(); i != _delay.end(); ++i) {
+                       do_emit_video(i->first, i->second);
+               }
        }
+
        return done;
 }
 
@@ -765,7 +756,9 @@ Player::video (weak_ptr<Piece> wp, ContentVideo video)
                        _video_container_size,
                        video.eyes,
                        video.part,
-                       piece->content->video->colour_conversion ()
+                       piece->content->video->colour_conversion(),
+                       piece->content,
+                       video.frame
                        )
                );
 
@@ -857,14 +850,14 @@ Player::image_subtitle_start (weak_ptr<Piece> wp, ContentImageSubtitle subtitle)
        subtitle.sub.rectangle.x += piece->content->subtitle->x_offset ();
        subtitle.sub.rectangle.y += piece->content->subtitle->y_offset ();
 
+       /* Apply a corrective translation to keep the subtitle centred after the scale that is coming up */
+       subtitle.sub.rectangle.x -= subtitle.sub.rectangle.width * ((piece->content->subtitle->x_scale() - 1) / 2);
+       subtitle.sub.rectangle.y -= subtitle.sub.rectangle.height * ((piece->content->subtitle->y_scale() - 1) / 2);
+
        /* Apply content's subtitle scale */
        subtitle.sub.rectangle.width *= piece->content->subtitle->x_scale ();
        subtitle.sub.rectangle.height *= piece->content->subtitle->y_scale ();
 
-       /* Apply a corrective translation to keep the subtitle centred after that scale */
-       subtitle.sub.rectangle.x -= subtitle.sub.rectangle.width * (piece->content->subtitle->x_scale() - 1);
-       subtitle.sub.rectangle.y -= subtitle.sub.rectangle.height * (piece->content->subtitle->y_scale() - 1);
-
        PlayerSubtitles ps;
        ps.image.push_back (subtitle.sub);
        DCPTime from (content_time_to_dcp (piece, subtitle.from()));
@@ -883,6 +876,10 @@ Player::text_subtitle_start (weak_ptr<Piece> wp, ContentTextSubtitle subtitle)
        PlayerSubtitles ps;
        DCPTime const from (content_time_to_dcp (piece, subtitle.from()));
 
+       if (from > piece->content->end()) {
+               return;
+       }
+
        BOOST_FOREACH (dcp::SubtitleString s, subtitle.subs) {
                s.set_h_position (s.h_position() + piece->content->subtitle->x_offset ());
                s.set_v_position (s.v_position() + piece->content->subtitle->y_offset ());
@@ -925,6 +922,10 @@ Player::subtitle_stop (weak_ptr<Piece> wp, ContentTime to)
 
        DCPTime const dcp_to = content_time_to_dcp (piece, to);
 
+       if (dcp_to > piece->content->end()) {
+               return;
+       }
+
        pair<PlayerSubtitles, DCPTime> from = _active_subtitles.add_to (wp, dcp_to);
 
        if (piece->content->subtitle->use() && !_always_burn_subtitles && !piece->content->subtitle->burn()) {
@@ -943,9 +944,7 @@ Player::seek (DCPTime time, bool accurate)
                _shuffler->clear ();
        }
 
-       if (_delay) {
-               _delay->clear ();
-       }
+       _delay.clear ();
 
        if (_audio_processor) {
                _audio_processor->flush ();
@@ -956,8 +955,8 @@ Player::seek (DCPTime time, bool accurate)
 
        BOOST_FOREACH (shared_ptr<Piece> i, _pieces) {
                if (time < i->content->position()) {
-                       /* Before; seek to 0 */
-                       i->decoder->seek (ContentTime(), accurate);
+                       /* Before; seek to the start of the content */
+                       i->decoder->seek (dcp_to_content_time (i, i->content->position()), accurate);
                        i->done = false;
                } else if (i->content->position() <= time && time < i->content->end()) {
                        /* During; seek to position */
@@ -988,18 +987,38 @@ Player::seek (DCPTime time, bool accurate)
 void
 Player::emit_video (shared_ptr<PlayerVideo> pv, DCPTime time)
 {
-       optional<PositionImage> subtitles = subtitles_for_frame (time);
-       if (subtitles) {
-               pv->set_subtitle (subtitles.get ());
+       /* We need a delay to give a little wiggle room to ensure that relevent subtitles arrive at the
+          player before the video that requires them.
+       */
+       _delay.push_back (make_pair (pv, time));
+
+       if (pv->eyes() == EYES_BOTH || pv->eyes() == EYES_RIGHT) {
+               _last_video_time = time + one_video_frame();
        }
+       _last_video_eyes = increment_eyes (pv->eyes());
 
-       Video (pv, time);
+       if (_delay.size() < 3) {
+               return;
+       }
+
+       pair<shared_ptr<PlayerVideo>, DCPTime> to_do = _delay.front();
+       _delay.pop_front();
+       do_emit_video (to_do.first, to_do.second);
+}
 
+void
+Player::do_emit_video (shared_ptr<PlayerVideo> pv, DCPTime time)
+{
        if (pv->eyes() == EYES_BOTH || pv->eyes() == EYES_RIGHT) {
-               _last_video_time = time + one_video_frame();
                _active_subtitles.clear_before (time);
        }
-       _last_video_eyes = increment_eyes (pv->eyes());
+
+       optional<PositionImage> subtitles = subtitles_for_frame (time);
+       if (subtitles) {
+               pv->set_subtitle (subtitles.get ());
+       }
+
+       Video (pv, time);
 }
 
 void
@@ -1067,5 +1086,5 @@ Player::set_dcp_decode_reduction (optional<int> reduction)
 
        _dcp_decode_reduction = reduction;
        _have_valid_pieces = false;
-       Changed (false);
+       Changed (PlayerProperty::DCP_DECODE_REDUCTION, false);
 }