Hopefully much cleaner handling of PTS changes under resample.
[dcpomatic.git] / src / lib / player.cc
index f5212f8d016f34e727fcc81d11c3354445884f47..620efbd0dc30cb17bcde648f75dadfbf71fae5c1 100644 (file)
@@ -22,8 +22,8 @@
 #include "film.h"
 #include "ffmpeg_decoder.h"
 #include "ffmpeg_content.h"
-#include "imagemagick_decoder.h"
-#include "imagemagick_content.h"
+#include "still_image_decoder.h"
+#include "still_image_content.h"
 #include "sndfile_decoder.h"
 #include "sndfile_content.h"
 #include "subtitle_content.h"
@@ -32,6 +32,7 @@
 #include "image.h"
 #include "ratio.h"
 #include "resampler.h"
+#include "log.h"
 #include "scaler.h"
 
 using std::list;
@@ -45,7 +46,7 @@ using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
 
-#define DEBUG_PLAYER 1
+//#define DEBUG_PLAYER 1
 
 class Piece
 {
@@ -74,8 +75,8 @@ std::ostream& operator<<(std::ostream& s, Piece const & p)
 {
        if (dynamic_pointer_cast<FFmpegContent> (p.content)) {
                s << "\tffmpeg     ";
-       } else if (dynamic_pointer_cast<ImageMagickContent> (p.content)) {
-               s << "\timagemagick";
+       } else if (dynamic_pointer_cast<StillImageContent> (p.content)) {
+               s << "\tstill image";
        } else if (dynamic_pointer_cast<SndfileContent> (p.content)) {
                s << "\tsndfile    ";
        }
@@ -94,7 +95,7 @@ Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
        , _have_valid_pieces (false)
        , _video_position (0)
        , _audio_position (0)
-       , _audio_buffers (f->dcp_audio_channels(), 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));
@@ -190,6 +191,19 @@ Player::pass ()
                        cout << "Pass " << *earliest << "\n";
 #endif                 
                        earliest->decoder->pass ();
+
+                       if (earliest->decoder->done()) {
+                               shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (earliest->content);
+                               assert (ac);
+                               shared_ptr<Resampler> re = resampler (ac, false);
+                               if (re) {
+                                       shared_ptr<const AudioBuffers> b = re->flush ();
+                                       if (b->frames ()) {
+                                               process_audio (earliest, b, ac->audio_length ());
+                                       }
+                               }
+                       }
+       
                }
                break;
        }
@@ -202,7 +216,7 @@ Player::pass ()
 }
 
 void
-Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image, bool same, VideoContent::Frame frame)
+Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image, Eyes eyes, bool same, VideoContent::Frame frame)
 {
        shared_ptr<Piece> piece = weak_piece.lock ();
        if (!piece) {
@@ -212,7 +226,7 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
        shared_ptr<VideoContent> content = dynamic_pointer_cast<VideoContent> (piece->content);
        assert (content);
 
-       FrameRateConversion frc (content->video_frame_rate(), _film->dcp_video_frame_rate());
+       FrameRateConversion frc (content->video_frame_rate(), _film->video_frame_rate());
        if (frc.skip && (frame % 2) == 1) {
                return;
        }
@@ -223,7 +237,7 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
        
        work_image = work_image->scale_and_convert_to_rgb (image_size, _film->scaler(), true);
 
-       Time time = content->start() + (frame * frc.factor() * TIME_HZ / _film->dcp_video_frame_rate());
+       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);
@@ -240,14 +254,14 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
 
 #ifdef DCPOMATIC_DEBUG
        _last_video = piece->content;
-#endif 
+#endif
 
-       Video (work_image, same, time);
-       time += TIME_HZ / _film->dcp_video_frame_rate();
+       Video (work_image, eyes, same, time);
+       time += TIME_HZ / _film->video_frame_rate();
 
        if (frc.repeat) {
-               Video (work_image, true, time);
-               time += TIME_HZ / _film->dcp_video_frame_rate();
+               Video (work_image, eyes, true, time);
+               time += TIME_HZ / _film->video_frame_rate();
        }
 
        _video_position = piece->video_position = time;
@@ -266,12 +280,14 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
 
        /* Resample */
        if (content->content_audio_frame_rate() != content->output_audio_frame_rate()) {
-               shared_ptr<Resampler> r = resampler (content);
-               audio = r->run (audio);
+               shared_ptr<Resampler> r = resampler (content, true);
+               pair<shared_ptr<const AudioBuffers>, AudioContent::Frame> ro = r->run (audio, frame);
+               audio = ro.first;
+               frame = ro.second;
        }
-
+       
        /* Remap channels */
-       shared_ptr<AudioBuffers> dcp_mapped (new AudioBuffers (_film->dcp_audio_channels(), audio->frames()));
+       shared_ptr<AudioBuffers> dcp_mapped (new AudioBuffers (_film->audio_channels(), audio->frames()));
        dcp_mapped->make_silent ();
        list<pair<int, libdcp::Channel> > map = content->audio_mapping().content_to_dcp ();
        for (list<pair<int, libdcp::Channel> >::iterator i = map.begin(); i != map.end(); ++i) {
@@ -282,11 +298,13 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
 
        audio = dcp_mapped;
 
-       Time time = content->start() + (frame * TIME_HZ / _film->dcp_audio_frame_rate()) + (content->audio_delay() * TIME_HZ / 1000);
+       Time time = content->start()
+               + _film->audio_frames_to_time (frame)
+               + (content->audio_delay() * TIME_HZ / 1000);
 
        /* We must cut off anything that comes before the start of all time */
        if (time < 0) {
-               int const frames = - time * _film->dcp_audio_frame_rate() / TIME_HZ;
+               int const frames = - time * _film->audio_frame_rate() / TIME_HZ;
                if (frames >= audio->frames ()) {
                        return;
                }
@@ -303,7 +321,7 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
        */
 
        if (time > _audio_position) {
-               /* We can emit some audio from our buffers */
+               /* We can emit some audio from our buffers; this is how many frames */
                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 */
@@ -313,10 +331,13 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
                        _audio_buffers.set_frames (N);
                }
                assert (N <= _audio_buffers.frames());
+
+               /* XXX: not convinced that a copy is necessary here */
                shared_ptr<AudioBuffers> 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);
+
+               _audio_position = piece->audio_position = _audio_position + _film->audio_frames_to_time (N);
 
                /* And remove it from our buffers */
                if (_audio_buffers.frames() > N) {
@@ -380,12 +401,12 @@ Player::seek (Time t, bool accurate)
 
                (*i)->video_position = (*i)->audio_position = vc->start() + s;
 
-               FrameRateConversion frc (vc->video_frame_rate(), _film->dcp_video_frame_rate());
+               FrameRateConversion frc (vc->video_frame_rate(), _film->video_frame_rate());
                /* Here we are converting from time (in the DCP) to a frame number in the content.
                   Hence we need to use the DCP's frame rate and the double/skip correction, not
                   the source's rate.
                */
-               VideoContent::Frame f = s * _film->dcp_video_frame_rate() / (frc.factor() * TIME_HZ);
+               VideoContent::Frame f = s * _film->video_frame_rate() / (frc.factor() * TIME_HZ);
                dynamic_pointer_cast<VideoDecoder>((*i)->decoder)->seek (f, accurate);
        }
 
@@ -401,10 +422,10 @@ Player::setup_pieces ()
 
        _pieces.clear ();
 
-       Playlist::ContentList content = _playlist->content_with_loop ();
+       ContentList content = _playlist->content ();
        sort (content.begin(), content.end(), ContentSorter ());
 
-       for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
+       for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
 
                shared_ptr<Piece> piece (new Piece (*i));
 
@@ -414,28 +435,28 @@ Player::setup_pieces ()
                if (fc) {
                        shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio));
                        
-                       fd->Video.connect (bind (&Player::process_video, this, piece, _1, _2, _3));
+                       fd->Video.connect (bind (&Player::process_video, this, piece, _1, _2, _3, _4));
                        fd->Audio.connect (bind (&Player::process_audio, this, piece, _1, _2));
                        fd->Subtitle.connect (bind (&Player::process_subtitle, this, piece, _1, _2, _3, _4));
 
                        piece->decoder = fd;
                }
                
-               shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
+               shared_ptr<const StillImageContent> ic = dynamic_pointer_cast<const StillImageContent> (*i);
                if (ic) {
-                       shared_ptr<ImageMagickDecoder> id;
+                       shared_ptr<StillImageDecoder> id;
                        
-                       /* See if we can re-use an old ImageMagickDecoder */
+                       /* See if we can re-use an old StillImageDecoder */
                        for (list<shared_ptr<Piece> >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) {
-                               shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> ((*j)->decoder);
+                               shared_ptr<StillImageDecoder> imd = dynamic_pointer_cast<StillImageDecoder> ((*j)->decoder);
                                if (imd && imd->content() == ic) {
                                        id = imd;
                                }
                        }
 
                        if (!id) {
-                               id.reset (new ImageMagickDecoder (_film, ic));
-                               id->Video.connect (bind (&Player::process_video, this, piece, _1, _2, _3));
+                               id.reset (new StillImageDecoder (_film, ic));
+                               id->Video.connect (bind (&Player::process_video, this, piece, _1, _2, _3, _4));
                        }
 
                        piece->decoder = id;
@@ -479,6 +500,8 @@ Player::content_changed (weak_ptr<Content> w, int property, bool frequent)
        } else if (property == SubtitleContentProperty::SUBTITLE_OFFSET || property == SubtitleContentProperty::SUBTITLE_SCALE) {
                update_subtitle ();
                Changed (frequent);
+       } else if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
+               Changed (frequent);
        }
 }
 
@@ -498,12 +521,16 @@ Player::set_video_container_size (libdcp::Size s)
 }
 
 shared_ptr<Resampler>
-Player::resampler (shared_ptr<AudioContent> c)
+Player::resampler (shared_ptr<AudioContent> c, bool create)
 {
        map<shared_ptr<AudioContent>, shared_ptr<Resampler> >::iterator i = _resamplers.find (c);
        if (i != _resamplers.end ()) {
                return i->second;
        }
+
+       if (!create) {
+               return shared_ptr<Resampler> ();
+       }
        
        shared_ptr<Resampler> r (new Resampler (c->content_audio_frame_rate(), c->output_audio_frame_rate(), c->audio_channels()));
        _resamplers[c] = r;
@@ -518,15 +545,15 @@ Player::emit_black ()
 #endif
        
        /* XXX: use same here */
-       Video (_black_frame, false, _video_position);
+       Video (_black_frame, EYES_BOTH, false, _video_position);
        _video_position += _film->video_frames_to_time (1);
 }
 
 void
 Player::emit_silence (OutputAudioFrame most)
 {
-       OutputAudioFrame N = min (most, _film->dcp_audio_frame_rate() / 2);
-       shared_ptr<AudioBuffers> silence (new AudioBuffers (_film->dcp_audio_channels(), N));
+       OutputAudioFrame N = min (most, _film->audio_frame_rate() / 2);
+       shared_ptr<AudioBuffers> silence (new AudioBuffers (_film->audio_channels(), N));
        silence->make_silent ();
        Audio (silence, _audio_position);
        _audio_position += _film->audio_frames_to_time (N);