All audio content should resample if the output frame rate and content
[dcpomatic.git] / src / lib / player.cc
index eb4a3d3a56711a7567e774894aae4b1c9fc47033..68df8ea709afd905955be97dd410366092f5a612 100644 (file)
 #include "playlist.h"
 #include "job.h"
 #include "image.h"
+#include "image_proxy.h"
 #include "ratio.h"
 #include "resampler.h"
 #include "log.h"
 #include "scaler.h"
 #include "player_video_frame.h"
 
+#define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
+
 using std::list;
 using std::cout;
 using std::min;
@@ -146,7 +149,7 @@ Player::pass ()
                                if (re) {
                                        shared_ptr<const AudioBuffers> b = re->flush ();
                                        if (b->frames ()) {
-                                               process_audio (earliest, b, ac->audio_length ());
+                                               process_audio (earliest, b, ac->audio_length (), true);
                                        }
                                }
                        }
@@ -179,12 +182,13 @@ Player::pass ()
 
 /** @param extra Amount of extra time to add to the content frame's time (for repeat) */
 void
-Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image, Eyes eyes, bool same, VideoContent::Frame frame, Time extra)
+Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const ImageProxy> image, Eyes eyes, Part part, bool same, VideoContent::Frame frame, Time extra)
 {
        /* Keep a note of what came in so that we can repeat it if required */
        _last_incoming_video.weak_piece = weak_piece;
        _last_incoming_video.image = image;
        _last_incoming_video.eyes = eyes;
+       _last_incoming_video.part = part;
        _last_incoming_video.same = same;
        _last_incoming_video.frame = frame;
        _last_incoming_video.extra = extra;
@@ -216,7 +220,10 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
                        content->crop(),
                        image_size,
                        _video_container_size,
-                       _film->scaler()
+                       _film->scaler(),
+                       eyes,
+                       part,
+                       content->colour_conversion()
                        )
                );
        
@@ -252,7 +259,7 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
        _last_video = piece->content;
 #endif
 
-       Video (pi, eyes, content->colour_conversion(), same, time);
+       Video (pi, same, time);
 
        _last_emit_was_black = false;
        _video_position = piece->video_position = (time + TIME_HZ / _film->video_frame_rate());
@@ -262,8 +269,9 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
        }
 }
 
+/** @param already_resampled true if this data has already been through the chain up to the resampler */
 void
-Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers> audio, AudioContent::Frame frame)
+Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers> audio, AudioContent::Frame frame, bool already_resampled)
 {
        shared_ptr<Piece> piece = weak_piece.lock ();
        if (!piece) {
@@ -273,19 +281,21 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
        shared_ptr<AudioContent> content = dynamic_pointer_cast<AudioContent> (piece->content);
        assert (content);
 
-       /* Gain */
-       if (content->audio_gain() != 0) {
-               shared_ptr<AudioBuffers> gain (new AudioBuffers (audio));
-               gain->apply_gain (content->audio_gain ());
-               audio = gain;
-       }
-
-       /* Resample */
-       if (content->content_audio_frame_rate() != content->output_audio_frame_rate()) {
-               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;
+       if (!already_resampled) {
+               /* Gain */
+               if (content->audio_gain() != 0) {
+                       shared_ptr<AudioBuffers> gain (new AudioBuffers (audio));
+                       gain->apply_gain (content->audio_gain ());
+                       audio = gain;
+               }
+               
+               /* Resample */
+               if (content->content_audio_frame_rate() != content->output_audio_frame_rate()) {
+                       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;
+               }
        }
        
        Time const relative_time = _film->audio_frames_to_time (frame);
@@ -419,8 +429,8 @@ Player::setup_pieces ()
                if (fc) {
                        shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio));
                        
-                       fd->Video.connect (bind (&Player::process_video, this, weak_ptr<Piece> (piece), _1, _2, _3, _4, 0));
-                       fd->Audio.connect (bind (&Player::process_audio, this, weak_ptr<Piece> (piece), _1, _2));
+                       fd->Video.connect (bind (&Player::process_video, this, weak_ptr<Piece> (piece), _1, _2, _3, _4, _5, 0));
+                       fd->Audio.connect (bind (&Player::process_audio, this, weak_ptr<Piece> (piece), _1, _2, false));
                        fd->Subtitle.connect (bind (&Player::process_subtitle, this, weak_ptr<Piece> (piece), _1, _2, _3, _4));
 
                        fd->seek (fc->time_to_content_video_frames (fc->trim_start ()), true);
@@ -442,7 +452,7 @@ Player::setup_pieces ()
 
                        if (!reusing) {
                                shared_ptr<ImageDecoder> id (new ImageDecoder (_film, ic));
-                               id->Video.connect (bind (&Player::process_video, this, weak_ptr<Piece> (piece), _1, _2, _3, _4, 0));
+                               id->Video.connect (bind (&Player::process_video, this, weak_ptr<Piece> (piece), _1, _2, _3, _4, _5, 0));
                                piece->decoder = id;
                        }
                }
@@ -450,7 +460,7 @@ Player::setup_pieces ()
                shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
                if (sc) {
                        shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
-                       sd->Audio.connect (bind (&Player::process_audio, this, weak_ptr<Piece> (piece), _1, _2));
+                       sd->Audio.connect (bind (&Player::process_audio, this, weak_ptr<Piece> (piece), _1, _2, false));
 
                        piece->decoder = sd;
                }
@@ -521,11 +531,14 @@ Player::set_video_container_size (libdcp::Size s)
        
        _black_frame.reset (
                new PlayerVideoFrame (
-                       im,
+                       shared_ptr<ImageProxy> (new RawImageProxy (im, _film->log ())),
                        Crop(),
                        _video_container_size,
                        _video_container_size,
-                       Scaler::from_id ("bicubic")
+                       Scaler::from_id ("bicubic"),
+                       EYES_BOTH,
+                       PART_WHOLE,
+                       ColourConversion ()
                        )
                );
 }
@@ -542,12 +555,10 @@ Player::resampler (shared_ptr<AudioContent> c, bool create)
                return shared_ptr<Resampler> ();
        }
 
-       _film->log()->log (
-               String::compose (
-                       "Creating new resampler for %1 to %2 with %3 channels", c->content_audio_frame_rate(), c->output_audio_frame_rate(), c->audio_channels()
-                       )
+       LOG_GENERAL (
+               "Creating new resampler for %1 to %2 with %3 channels", c->content_audio_frame_rate(), c->output_audio_frame_rate(), c->audio_channels()
                );
-       
+
        shared_ptr<Resampler> r (new Resampler (c->content_audio_frame_rate(), c->output_audio_frame_rate(), c->audio_channels()));
        _resamplers[c] = r;
        return r;
@@ -560,7 +571,7 @@ Player::emit_black ()
        _last_video.reset ();
 #endif
 
-       Video (_black_frame, EYES_BOTH, ColourConversion(), _last_emit_was_black, _video_position);
+       Video (_black_frame, _last_emit_was_black, _video_position);
        _video_position += _film->video_frames_to_time (1);
        _last_emit_was_black = true;
 }
@@ -619,6 +630,7 @@ Player::repeat_last_video ()
                _last_incoming_video.weak_piece,
                _last_incoming_video.image,
                _last_incoming_video.eyes,
+               _last_incoming_video.part,
                _last_incoming_video.same,
                _last_incoming_video.frame,
                _last_incoming_video.extra