Hopefully much cleaner handling of PTS changes under resample.
[dcpomatic.git] / src / lib / player.cc
index e1173a36bf654b6a8591c9d1c77de8ff7adab84e..620efbd0dc30cb17bcde648f75dadfbf71fae5c1 100644 (file)
@@ -95,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));
@@ -191,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;
        }
@@ -213,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;
        }
@@ -224,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);
@@ -244,11 +257,11 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
 #endif
 
        Video (work_image, eyes, same, time);
-       time += TIME_HZ / _film->dcp_video_frame_rate();
+       time += TIME_HZ / _film->video_frame_rate();
 
        if (frc.repeat) {
                Video (work_image, eyes, true, time);
-               time += TIME_HZ / _film->dcp_video_frame_rate();
+               time += TIME_HZ / _film->video_frame_rate();
        }
 
        _video_position = piece->video_position = time;
@@ -267,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) {
@@ -283,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;
                }
@@ -304,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 */
@@ -314,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) {
@@ -381,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);
        }
 
@@ -481,7 +501,6 @@ Player::content_changed (weak_ptr<Content> w, int property, bool frequent)
                update_subtitle ();
                Changed (frequent);
        } else if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
-               cout << "vft change.\n";
                Changed (frequent);
        }
 }
@@ -502,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;
@@ -529,8 +552,8 @@ Player::emit_black ()
 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);