Various range fixes.
[dcpomatic.git] / src / lib / decoder.cc
index 358109a7e081dad3d180aaea8db72778295b6532..5860f339ef650a6090b51e03c23e9d57e9974791 100644 (file)
 #include "util.h"
 #include "log.h"
 #include "decoder.h"
-#include "filter.h"
 #include "delay_line.h"
-#include "ffmpeg_compatibility.h"
 #include "subtitle.h"
+#include "filter_graph.h"
 
 using std::string;
 using std::stringstream;
 using std::min;
+using std::pair;
 using std::list;
 using boost::shared_ptr;
 
@@ -49,22 +49,20 @@ using boost::shared_ptr;
  *  @param j Job that we are running within, or 0
  *  @param minimal true to do the bare minimum of work; just run through the content.  Useful for acquiring
  *  accurate frame counts as quickly as possible.  This generates no video or audio output.
- *  @param ignore_length Ignore the content's claimed length when computing progress.
  */
-Decoder::Decoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j, bool minimal, bool ignore_length)
+Decoder::Decoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j, bool minimal)
        : _film (f)
        , _opt (o)
        , _job (j)
        , _minimal (minimal)
-       , _ignore_length (ignore_length)
-       , _video_frame (0)
+       , _video_frames_in (0)
+       , _video_frames_out (0)
+       , _audio_frames_in (0)
+       , _audio_frames_out (0)
        , _delay_line (0)
-       , _delay_in_bytes (0)
-       , _audio_frames_processed (0)
+       , _delay_in_frames (0)
 {
-       if (_opt->decode_video_frequency != 0 && !_film->length()) {
-               throw DecodeError ("cannot do a partial decode if length is unknown");
-       }
+       
 }
 
 Decoder::~Decoder ()
@@ -72,61 +70,63 @@ Decoder::~Decoder ()
        delete _delay_line;
 }
 
-/** Start off a decode processing run */
+/** Start off a decode processing run.  This should only be called once on
+ *  a given Decoder object.
+ */
 void
 Decoder::process_begin ()
 {
-       _delay_in_bytes = _film->audio_delay() * audio_sample_rate() * audio_channels() * bytes_per_audio_sample() / 1000;
-       delete _delay_line;
-       _delay_line = new DelayLine (_delay_in_bytes);
-
-       _audio_frames_processed = 0;
+       _delay_in_frames = _film->audio_delay() * audio_sample_rate() / 1000;
+       _delay_line = new DelayLine (audio_channels(), _delay_in_frames);
 }
 
 /** Finish off a decode processing run */
 void
 Decoder::process_end ()
 {
-       if (_delay_in_bytes < 0) {
-               uint8_t remainder[-_delay_in_bytes];
-               _delay_line->get_remaining (remainder);
-               _audio_frames_processed += _delay_in_bytes / (audio_channels() * bytes_per_audio_sample());
-               emit_audio (remainder, -_delay_in_bytes);
+       if (_delay_in_frames < 0 && _opt->decode_audio && audio_channels()) {
+               shared_ptr<AudioBuffers> b (new AudioBuffers (audio_channels(), -_delay_in_frames));
+               b->make_silent ();
+               emit_audio (b);
        }
 
-       /* If we cut the decode off, the audio may be short; push some silence
-          in to get it to the right length.
-       */
+       if (_opt->decode_audio && audio_channels()) {
 
-       int64_t const video_length_in_audio_frames = ((int64_t) last_video_frame() * audio_sample_rate() / frames_per_second());
-       int64_t const audio_short_by_frames = video_length_in_audio_frames - _audio_frames_processed;
+               /* Ensure that our video and audio emissions are the same length */
 
-       _film->log()->log (
-               String::compose ("DCP length is %1 (%2 audio frames); %3 frames of audio processed.",
-                                last_video_frame(),
-                                video_length_in_audio_frames,
-                                _audio_frames_processed)
-               );
-       
-       if (audio_short_by_frames >= 0 && _opt->decode_audio) {
+               int64_t audio_short_by_frames = video_frames_to_audio_frames (_video_frames_out) - _audio_frames_out;
 
-               _film->log()->log (String::compose ("DCP length is %1; %2 frames of audio processed.", last_video_frame(), _audio_frames_processed));
-               _film->log()->log (String::compose ("Adding %1 frames of silence to the end.", audio_short_by_frames));
+               _film->log()->log (
+                       String::compose ("Decoder has emitted %1 video frames (which equals %2 audio frames) and %3 audio frames",
+                                        _video_frames_out,
+                                        video_frames_to_audio_frames (_video_frames_out),
+                                        _audio_frames_out)
+                       );
 
-               /* XXX: this is slightly questionable; does memset () give silence with all
-                  sample formats?
-               */
+               if (audio_short_by_frames < 0) {
 
-               int64_t bytes = audio_short_by_frames * _film->audio_channels() * bytes_per_audio_sample();
-               
-               int64_t const silence_size = 16 * 1024 * _film->audio_channels() * bytes_per_audio_sample();
-               uint8_t silence[silence_size];
-               memset (silence, 0, silence_size);
-               
-               while (bytes) {
-                       int64_t const t = min (bytes, silence_size);
-                       emit_audio (silence, t);
-                       bytes -= t;
+                       _film->log()->log (String::compose ("Emitted %1 too many audio frames", -audio_short_by_frames));
+                       
+                       /* We have emitted more audio than video.  Emit enough black video frames so that we reverse this */
+                       int const black_video_frames = ceil (-audio_short_by_frames * frames_per_second() / audio_sample_rate());
+
+                       _film->log()->log (String::compose ("Emitting %1 frames of black video", black_video_frames));
+
+                       shared_ptr<Image> black (new CompactImage (pixel_format(), native_size()));
+                       black->make_black ();
+                       for (int i = 0; i < black_video_frames; ++i) {
+                               emit_video (black, shared_ptr<Subtitle> ());
+                       }
+
+                       /* Now recompute our check value */
+                       audio_short_by_frames = video_frames_to_audio_frames (_video_frames_out) - _audio_frames_out;
+               }
+       
+               if (audio_short_by_frames > 0) {
+                       _film->log()->log (String::compose ("Emitted %1 too few audio frames", audio_short_by_frames));
+                       shared_ptr<AudioBuffers> b (new AudioBuffers (audio_channels(), audio_short_by_frames));
+                       b->make_silent ();
+                       emit_audio (b);
                }
        }
 }
@@ -143,27 +143,13 @@ Decoder::go ()
 
        while (pass () == false) {
                if (_job && _film->dcp_length()) {
-                       _job->set_progress (float (_video_frame) / _film->dcp_length().get());
+                       _job->set_progress (float (_video_frames_out) / _film->dcp_length().get());
                }
        }
 
        process_end ();
 }
 
-/** Run one pass.  This may or may not generate any actual video / audio data;
- *  some decoders may require several passes to generate a single frame.
- *  @return true if we have finished processing all data; otherwise false.
- */
-bool
-Decoder::pass ()
-{
-       if (!_ignore_length && _video_frame >= _film->dcp_length()) {
-               return true;
-       }
-
-       return do_pass ();
-}
-
 /** Called by subclasses to tell the world that some audio data is ready
  *  @param data Audio data, in Film::audio_sample_format.
  *  @param size Number of bytes of data.
@@ -171,15 +157,13 @@ Decoder::pass ()
 void
 Decoder::process_audio (uint8_t* data, int size)
 {
-       /* Push into the delay line */
-       size = _delay_line->feed (data, size);
-
-       emit_audio (data, size);
-}
-
-void
-Decoder::emit_audio (uint8_t* data, int size)
-{
+       if (size == 0) {
+               return;
+       }
+       
+       assert (_film->audio_channels());
+       assert (bytes_per_audio_sample());
+       
        /* Deinterleave and convert to float */
 
        assert ((size % (bytes_per_audio_sample() * audio_channels())) == 0);
@@ -246,10 +230,44 @@ Decoder::emit_audio (uint8_t* data, int size)
                }
        }
 
-       /* Update the number of audio frames we've pushed to the encoder */
-       _audio_frames_processed += audio->frames ();
+       _delay_line->feed (audio);
+
+       if (_opt->decode_range) {
+               /* Decode range in audio frames */
+               pair<int64_t, int64_t> required_range (
+                       video_frames_to_audio_frames (_opt->decode_range.get().first),
+                       video_frames_to_audio_frames (_opt->decode_range.get().second)
+                       );
+               
+               /* Range of this block of data */
+               pair<int64_t, int64_t> this_range (
+                       _audio_frames_in,
+                       _audio_frames_in + audio->frames()
+                       );
+               
+               /* Trim start */
+               if (required_range.first >= this_range.first && required_range.first < this_range.second) {
+                       int64_t const shift = this_range.first - required_range.first;
+                       audio->move (shift, 0, audio->frames() - shift);
+                       audio->set_frames (audio->frames() - shift);
+               }
+               
+               /* Trim end */
+               if (required_range.second >= this_range.first && required_range.second < this_range.second) {
+                       audio->set_frames (this_range.first - required_range.second);
+               }
+       }
+               
+       if (audio->frames()) {
+               emit_audio (audio);
+       }
+}
 
+void
+Decoder::emit_audio (shared_ptr<AudioBuffers> audio)
+{
        Audio (audio);
+       _audio_frames_out += audio->frames ();
 }
 
 /** Called by subclasses to tell the world that some video data is ready.
@@ -260,20 +278,21 @@ void
 Decoder::process_video (AVFrame* frame)
 {
        if (_minimal) {
-               ++_video_frame;
+               ++_video_frames_in;
                return;
        }
 
-       /* Use Film::length here as our one may be wrong */
-
-       int gap = 0;
-       if (_opt->decode_video_frequency != 0) {
-               gap = _film->length().get() / _opt->decode_video_frequency;
+       if (_opt->decode_video_skip != 0 && (_video_frames_in % _opt->decode_video_skip) != 0) {
+               ++_video_frames_in;
+               return;
        }
 
-       if (_opt->decode_video_frequency != 0 && gap != 0 && (_video_frame % gap) != 0) {
-               ++_video_frame;
-               return;
+       if (_opt->decode_range) {
+               pair<SourceFrame, SourceFrame> r = _opt->decode_range.get();
+               if (_video_frames_in < r.first || _video_frames_in >= r.second) {
+                       ++_video_frames_in;
+                       return;
+               }
        }
 
        shared_ptr<FilterGraph> graph;
@@ -286,7 +305,7 @@ Decoder::process_video (AVFrame* frame)
        if (i == _filter_graphs.end ()) {
                graph.reset (new FilterGraph (_film, this, _opt->apply_crop, Size (frame->width, frame->height), (AVPixelFormat) frame->format));
                _filter_graphs.push_back (graph);
-               std::cout << "NEW GRAPH for " << frame->width << "x" << frame->height << " " << frame->format << "\n";
+               _film->log()->log (String::compose ("New graph for %1x%2, pixel format %3", frame->width, frame->height, frame->format));
        } else {
                graph = *i;
        }
@@ -294,19 +313,34 @@ Decoder::process_video (AVFrame* frame)
        list<shared_ptr<Image> > images = graph->process (frame);
 
        for (list<shared_ptr<Image> >::iterator i = images.begin(); i != images.end(); ++i) {
-               if (_opt->black_after > 0 && _video_frame > _opt->black_after) {
-                       (*i)->make_black ();
-               }
-               
                shared_ptr<Subtitle> sub;
-               if (_timed_subtitle && _timed_subtitle->displayed_at (double (last_video_frame()) / _film->frames_per_second())) {
+               if (_timed_subtitle && _timed_subtitle->displayed_at (double (video_frames_in()) / _film->frames_per_second())) {
                        sub = _timed_subtitle->subtitle ();
                }
-               
-               TIMING ("Decoder emits %1", _video_frame);
-               Video ((*i), _video_frame, sub);
-               ++_video_frame;
+
+               emit_video (*i, sub);
+       }
+}
+
+void
+Decoder::repeat_last_video ()
+{
+       if (!_last_image) {
+               _last_image.reset (new CompactImage (pixel_format(), native_size()));
+               _last_image->make_black ();
        }
+
+       emit_video (_last_image, _last_subtitle);
+}
+
+void
+Decoder::emit_video (shared_ptr<Image> image, shared_ptr<Subtitle> sub)
+{
+       TIMING ("Decoder emits %1", _video_frames_out);
+       Video (image, _video_frames_out, sub);
+       ++_video_frames_out;
+       _last_image = image;
+       _last_subtitle = sub;
 }
 
 void
@@ -326,3 +360,9 @@ Decoder::bytes_per_audio_sample () const
 {
        return av_get_bytes_per_sample (audio_sample_format ());
 }
+
+int64_t
+Decoder::video_frames_to_audio_frames (SourceFrame v) const
+{
+       return ((int64_t) v * audio_sample_rate() / frames_per_second());
+}