X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fplayer.cc;h=c8ac591a7b2919a36a6f77080dad538417c08982;hb=d2137ac5db409e686b4d9b3fa567935a5e416d41;hp=8cc1849e865f0909001a55374954bca5fbe48247;hpb=ec7c27b80a07bffcdb175512fa6d3811c277d959;p=dcpomatic.git diff --git a/src/lib/player.cc b/src/lib/player.cc index 8cc1849e8..c8ac591a7 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2014 Carl Hetherington 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 @@ -18,608 +18,559 @@ */ #include +#include #include "player.h" #include "film.h" #include "ffmpeg_decoder.h" +#include "audio_buffers.h" #include "ffmpeg_content.h" -#include "still_image_decoder.h" -#include "still_image_content.h" +#include "image_decoder.h" +#include "image_content.h" #include "sndfile_decoder.h" #include "sndfile_content.h" #include "subtitle_content.h" +#include "subrip_decoder.h" +#include "subrip_content.h" #include "playlist.h" #include "job.h" #include "image.h" +#include "raw_image_proxy.h" #include "ratio.h" -#include "resampler.h" #include "log.h" #include "scaler.h" +#include "render_subtitles.h" +#include "config.h" +#include "content_video.h" +#include "player_video.h" +#include "frame_rate_change.h" +#include "dcp_content.h" +#include "dcp_decoder.h" +#include "dcp_subtitle_content.h" +#include "dcp_subtitle_decoder.h" + +#define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL); using std::list; using std::cout; using std::min; using std::max; +using std::min; using std::vector; using std::pair; using std::map; +using std::make_pair; using boost::shared_ptr; using boost::weak_ptr; using boost::dynamic_pointer_cast; - -//#define DEBUG_PLAYER 1 - -class Piece -{ -public: - Piece (shared_ptr c) - : content (c) - , video_position (c->start ()) - , audio_position (c->start ()) - {} - - Piece (shared_ptr c, shared_ptr d) - : content (c) - , decoder (d) - , video_position (c->start ()) - , audio_position (c->start ()) - {} - - shared_ptr content; - shared_ptr decoder; - Time video_position; - Time audio_position; -}; - -#ifdef DEBUG_PLAYER -std::ostream& operator<<(std::ostream& s, Piece const & p) -{ - if (dynamic_pointer_cast (p.content)) { - s << "\tffmpeg "; - } else if (dynamic_pointer_cast (p.content)) { - s << "\tstill image"; - } else if (dynamic_pointer_cast (p.content)) { - s << "\tsndfile "; - } - - s << " at " << p.content->start() << " until " << p.content->end(); - - return s; -} -#endif +using boost::optional; Player::Player (shared_ptr f, shared_ptr p) : _film (f) , _playlist (p) - , _video (true) - , _audio (true) , _have_valid_pieces (false) - , _video_position (0) - , _audio_position (0) - , _audio_buffers (f->audio_channels(), 0) -{ - _playlist->Changed.connect (bind (&Player::playlist_changed, this)); - _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2, _3)); - _film->Changed.connect (bind (&Player::film_changed, this, _1)); - set_video_container_size (_film->container()->size (_film->full_frame ())); -} - -void -Player::disable_video () + , _approximate_size (false) { - _video = false; + _playlist_changed_connection = _playlist->Changed.connect (bind (&Player::playlist_changed, this)); + _playlist_content_changed_connection = _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2, _3)); + _film_changed_connection = _film->Changed.connect (bind (&Player::film_changed, this, _1)); + set_video_container_size (_film->frame_size ()); } void -Player::disable_audio () -{ - _audio = false; -} - -bool -Player::pass () +Player::setup_pieces () { - if (!_have_valid_pieces) { - setup_pieces (); - _have_valid_pieces = true; - } + list > old_pieces = _pieces; + _pieces.clear (); -#ifdef DEBUG_PLAYER - cout << "= PASS\n"; -#endif + ContentList content = _playlist->content (); - Time earliest_t = TIME_MAX; - shared_ptr earliest; - enum { - VIDEO, - AUDIO - } type = VIDEO; + for (ContentList::iterator i = content.begin(); i != content.end(); ++i) { - for (list >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) { - if ((*i)->decoder->done ()) { + if (!(*i)->paths_valid ()) { continue; } - - if (_video && dynamic_pointer_cast ((*i)->decoder)) { - if ((*i)->video_position < earliest_t) { - earliest_t = (*i)->video_position; - earliest = *i; - type = VIDEO; + + shared_ptr decoder; + optional 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 best_overlap; + for (ContentList::iterator j = content.begin(); j != content.end(); ++j) { + shared_ptr vc = dynamic_pointer_cast (*j); + if (!vc) { + continue; } - } - - if (_audio && dynamic_pointer_cast ((*i)->decoder)) { - if ((*i)->audio_position < earliest_t) { - earliest_t = (*i)->audio_position; - earliest = *i; - type = AUDIO; + + 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; } } - } - if (!earliest) { -#ifdef DEBUG_PLAYER - cout << "no earliest piece.\n"; -#endif - - flush (); - return true; - } - - switch (type) { - case VIDEO: - if (earliest_t > _video_position) { -#ifdef DEBUG_PLAYER - cout << "no video here; emitting black frame (earliest=" << earliest_t << ", video_position=" << _video_position << ").\n"; -#endif - emit_black (); - } else { -#ifdef DEBUG_PLAYER - cout << "Pass " << *earliest << "\n"; -#endif - earliest->decoder->pass (); - } - break; - - case AUDIO: - if (earliest_t > _audio_position) { -#ifdef DEBUG_PLAYER - cout << "no audio here; emitting silence.\n"; -#endif - emit_silence (_film->time_to_audio_frames (earliest_t - _audio_position)); + optional best_overlap_frc; + if (best_overlap) { + best_overlap_frc = FrameRateChange (best_overlap->video_frame_rate(), _film->video_frame_rate ()); } else { -#ifdef DEBUG_PLAYER - cout << "Pass " << *earliest << "\n"; -#endif - earliest->decoder->pass (); - - if (earliest->decoder->done()) { - shared_ptr ac = dynamic_pointer_cast (earliest->content); - assert (ac); - shared_ptr re = resampler (ac, false); - if (re) { - shared_ptr b = re->flush (); - if (b->frames ()) { - process_audio (earliest, b, ac->audio_length ()); - } - } - } + /* No video overlap; e.g. if the DCP is just audio */ + best_overlap_frc = FrameRateChange (_film->video_frame_rate(), _film->video_frame_rate ()); } - break; - } - -#ifdef DEBUG_PLAYER - cout << "\tpost pass " << _video_position << " " << _audio_position << "\n"; -#endif - - return false; -} -void -Player::process_video (weak_ptr weak_piece, shared_ptr image, Eyes eyes, bool same, VideoContent::Frame frame) -{ - shared_ptr piece = weak_piece.lock (); - if (!piece) { - return; - } - - shared_ptr content = dynamic_pointer_cast (piece->content); - assert (content); + /* FFmpeg */ + shared_ptr fc = dynamic_pointer_cast (*i); + if (fc) { + decoder.reset (new FFmpegDecoder (fc, _film->log())); + frc = FrameRateChange (fc->video_frame_rate(), _film->video_frame_rate()); + } - FrameRateConversion frc (content->video_frame_rate(), _film->video_frame_rate()); - if (frc.skip && (frame % 2) == 1) { - return; - } + shared_ptr dc = dynamic_pointer_cast (*i); + if (dc) { + decoder.reset (new DCPDecoder (dc, _film->log ())); + frc = FrameRateChange (dc->video_frame_rate(), _film->video_frame_rate()); + } - shared_ptr work_image = image->crop (content->crop(), true); + /* ImageContent */ + shared_ptr ic = dynamic_pointer_cast (*i); + if (ic) { + /* See if we can re-use an old ImageDecoder */ + for (list >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) { + shared_ptr imd = dynamic_pointer_cast ((*j)->decoder); + if (imd && imd->content() == ic) { + decoder = imd; + } + } - libdcp::Size const image_size = content->ratio()->size (_video_container_size); - - work_image = work_image->scale_and_convert_to_rgb (image_size, _film->scaler(), true); + if (!decoder) { + decoder.reset (new ImageDecoder (ic)); + } - Time time = content->start() + (frame * frc.factor() * TIME_HZ / _film->video_frame_rate()); - - if (_film->with_subtitles () && _out_subtitle.image && time >= _out_subtitle.from && time <= _out_subtitle.to) { - work_image->alpha_blend (_out_subtitle.image, _out_subtitle.position); - } + frc = FrameRateChange (ic->video_frame_rate(), _film->video_frame_rate()); + } - if (image_size != _video_container_size) { - assert (image_size.width <= _video_container_size.width); - assert (image_size.height <= _video_container_size.height); - shared_ptr im (new Image (PIX_FMT_RGB24, _video_container_size, true)); - im->make_black (); - im->copy (work_image, Position ((_video_container_size.width - image_size.width) / 2, (_video_container_size.height - image_size.height) / 2)); - work_image = im; - } + /* SndfileContent */ + shared_ptr sc = dynamic_pointer_cast (*i); + if (sc) { + decoder.reset (new SndfileDecoder (sc)); + frc = best_overlap_frc; + } -#ifdef DCPOMATIC_DEBUG - _last_video = piece->content; -#endif + /* SubRipContent */ + shared_ptr rc = dynamic_pointer_cast (*i); + if (rc) { + decoder.reset (new SubRipDecoder (rc)); + frc = best_overlap_frc; + } - Video (work_image, eyes, same, time); - time += TIME_HZ / _film->video_frame_rate(); + /* DCPSubtitleContent */ + shared_ptr dsc = dynamic_pointer_cast (*i); + if (dsc) { + decoder.reset (new DCPSubtitleDecoder (dsc)); + frc = best_overlap_frc; + } - if (frc.repeat) { - Video (work_image, eyes, true, time); - time += TIME_HZ / _film->video_frame_rate(); + _pieces.push_back (shared_ptr (new Piece (*i, decoder, frc.get ()))); } - _video_position = piece->video_position = time; + _have_valid_pieces = true; } void -Player::process_audio (weak_ptr weak_piece, shared_ptr audio, AudioContent::Frame frame) +Player::content_changed (weak_ptr w, int property, bool frequent) { - shared_ptr piece = weak_piece.lock (); - if (!piece) { + shared_ptr c = w.lock (); + if (!c) { return; } - shared_ptr content = dynamic_pointer_cast (piece->content); - assert (content); - - /* Resample */ - if (content->content_audio_frame_rate() != content->output_audio_frame_rate()) { - shared_ptr r = resampler (content, true); - audio = r->run (audio); - } + if ( + property == ContentProperty::POSITION || + property == ContentProperty::LENGTH || + property == ContentProperty::TRIM_START || + property == ContentProperty::TRIM_END || + property == ContentProperty::PATH || + property == VideoContentProperty::VIDEO_FRAME_TYPE + ) { + + _have_valid_pieces = false; + Changed (frequent); - /* Remap channels */ - shared_ptr dcp_mapped (new AudioBuffers (_film->audio_channels(), audio->frames())); - dcp_mapped->make_silent (); - list > map = content->audio_mapping().content_to_dcp (); - for (list >::iterator i = map.begin(); i != map.end(); ++i) { - if (i->first < audio->channels() && i->second < dcp_mapped->channels()) { - dcp_mapped->accumulate_channel (audio.get(), i->first, i->second); - } + } else if ( + property == SubtitleContentProperty::USE_SUBTITLES || + property == SubtitleContentProperty::SUBTITLE_X_OFFSET || + property == SubtitleContentProperty::SUBTITLE_Y_OFFSET || + property == SubtitleContentProperty::SUBTITLE_SCALE || + property == VideoContentProperty::VIDEO_CROP || + property == VideoContentProperty::VIDEO_SCALE || + property == VideoContentProperty::VIDEO_FRAME_RATE + ) { + + Changed (frequent); } +} - audio = dcp_mapped; +/** @param already_resampled true if this data has already been through the chain up to the resampler */ +void +Player::playlist_changed () +{ + _have_valid_pieces = false; + Changed (false); +} - /* Convert frame to time. After resampling, the frame time (in the DCP rate) will be T_D where - T_D = frame * DCP_rate / original_rate. Hence the time in seconds is T_D / DCP_rate. - */ - Time time = content->start() + (frame * TIME_HZ / _film->audio_frame_rate()) + (content->audio_delay() * TIME_HZ / 1000); +void +Player::set_video_container_size (dcp::Size s) +{ + _video_container_size = s; - /* We must cut off anything that comes before the start of all time */ - if (time < 0) { - int const frames = - time * _film->audio_frame_rate() / TIME_HZ; - if (frames >= audio->frames ()) { - return; - } + _black_image.reset (new Image (PIX_FMT_RGB24, _video_container_size, true)); + _black_image->make_black (); +} - shared_ptr trimmed (new AudioBuffers (audio->channels(), audio->frames() - frames)); - trimmed->copy_from (audio.get(), audio->frames() - frames, frames, 0); +void +Player::film_changed (Film::Property p) +{ + /* Here we should notice Film properties that affect our output, and + alert listeners that our output now would be different to how it was + last time we were run. + */ - audio = trimmed; - time = 0; + if (p == Film::SCALER || p == Film::CONTAINER || p == Film::VIDEO_FRAME_RATE) { + Changed (false); } +} - /* The time of this audio may indicate that some of our buffered audio is not going to - be added to any more, so it can be emitted. - */ - - if (time > _audio_position) { - /* We can emit some audio from our buffers */ - OutputAudioFrame const N = _film->time_to_audio_frames (time - _audio_position); - if (N > _audio_buffers.frames()) { - /* We need some extra silence before whatever is in the buffers */ - _audio_buffers.ensure_size (N); - _audio_buffers.move (0, N - _audio_buffers.frames(), _audio_buffers.frames ()); - _audio_buffers.make_silent (0, _audio_buffers.frames()); - _audio_buffers.set_frames (N); - } - assert (N <= _audio_buffers.frames()); - shared_ptr emit (new AudioBuffers (_audio_buffers.channels(), N)); - emit->copy_from (&_audio_buffers, N, 0, 0); - Audio (emit, _audio_position); - _audio_position = piece->audio_position = time + _film->audio_frames_to_time (N); - - /* And remove it from our buffers */ - if (_audio_buffers.frames() > N) { - _audio_buffers.move (N, 0, _audio_buffers.frames() - N); +list +Player::transform_image_subtitles (list subs) const +{ + list all; + + for (list::const_iterator i = subs.begin(); i != subs.end(); ++i) { + if (!i->image) { + continue; } - _audio_buffers.set_frames (_audio_buffers.frames() - N); + + /* 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_scale) / 2) and + * (height_before_subtitle_scale * (1 - subtitle_scale) / 2). + * + * Combining these two translations gives these expressions. + */ + + all.push_back ( + PositionImage ( + i->image->scale ( + scaled_size, + Scaler::from_id ("bicubic"), + i->image->pixel_format (), + true + ), + Position ( + rint (_video_container_size.width * i->rectangle.x), + rint (_video_container_size.height * i->rectangle.y) + ) + ) + ); } - /* Now accumulate the new audio into our buffers */ - _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames()); - _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ()); - _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames()); + return all; } void -Player::flush () +Player::set_approximate_size () { - if (_audio_buffers.frames() > 0) { - shared_ptr emit (new AudioBuffers (_audio_buffers.channels(), _audio_buffers.frames())); - emit->copy_from (&_audio_buffers, _audio_buffers.frames(), 0, 0); - Audio (emit, _audio_position); - _audio_position += _film->audio_frames_to_time (_audio_buffers.frames ()); - _audio_buffers.set_frames (0); - } - - while (_video_position < _audio_position) { - emit_black (); - } + _approximate_size = true; +} - while (_audio_position < _video_position) { - emit_silence (_film->time_to_audio_frames (_video_position - _audio_position)); - } - +shared_ptr +Player::black_player_video_frame (DCPTime time) const +{ + return shared_ptr ( + new PlayerVideo ( + shared_ptr (new RawImageProxy (_black_image, _film->log ())), + time, + Crop (), + _video_container_size, + _video_container_size, + Scaler::from_id ("bicubic"), + EYES_BOTH, + PART_WHOLE, + Config::instance()->colour_conversions().front().conversion + ) + ); } -/** Seek so that the next pass() will yield (approximately) the requested frame. - * Pass accurate = true to try harder to get close to the request. - * @return true on error - */ -void -Player::seek (Time t, bool accurate) +/** @return All PlayerVideos at the given time (there may be two frames for 3D) */ +list > +Player::get_video (DCPTime time, bool accurate) { if (!_have_valid_pieces) { setup_pieces (); - _have_valid_pieces = true; - } - - if (_pieces.empty ()) { - return; } - - for (list >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) { - shared_ptr vc = dynamic_pointer_cast ((*i)->content); - if (!vc) { - continue; + + list > ov = overlaps ( + time, + time + DCPTime::from_frames (1, _film->video_frame_rate ()) + ); + + list > pvf; + + if (ov.empty ()) { + /* No video content at this time */ + pvf.push_back (black_player_video_frame (time)); + } else { + /* Create a PlayerVideo from the content's video at this time */ + + shared_ptr piece = ov.back (); + shared_ptr decoder = dynamic_pointer_cast (piece->decoder); + assert (decoder); + shared_ptr content = dynamic_pointer_cast (piece->content); + assert (content); + + list content_video = decoder->get_video (dcp_to_content_video (piece, time), accurate); + if (content_video.empty ()) { + pvf.push_back (black_player_video_frame (time)); + return pvf; } - Time s = t - vc->start (); - s = max (static_cast