FFmpegContent does not need audio_length().
[dcpomatic.git] / src / lib / video_decoder.cc
index 1bb460da357c2b28c21b3eac7f21c8d93450702b..078e78c6f2c44d321cce2b67e56ff876ca9aec16 100644 (file)
@@ -21,7 +21,9 @@
 #include "image.h"
 #include "image_proxy.h"
 #include "raw_image_proxy.h"
-#include "content_video.h"
+#include "raw_image_proxy.h"
+#include "film.h"
+#include "log.h"
 
 #include "i18n.h"
 
@@ -39,15 +41,15 @@ VideoDecoder::VideoDecoder (shared_ptr<const VideoContent> c)
 #else
        : _video_content (c)
 #endif
-       , _same (false)
        , _last_seek_accurate (true)
+       , _ignore_video (false)
 {
        _black_image.reset (new Image (PIX_FMT_RGB24, _video_content->video_size(), true));
        _black_image->make_black ();
 }
 
 list<ContentVideo>
-VideoDecoder::decoded_video (VideoFrame frame)
+VideoDecoder::decoded_video (Frame frame)
 {
        list<ContentVideo> output;
        
@@ -66,7 +68,7 @@ VideoDecoder::decoded_video (VideoFrame frame)
  *  @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
  */
 list<ContentVideo>
-VideoDecoder::get_video (VideoFrame frame, bool accurate)
+VideoDecoder::get_video (Frame frame, bool accurate)
 {
        /* At this stage, if we have get_video()ed before, _decoded_video will contain the last frame that this
           method returned (and possibly a few more).  If the requested frame is not in _decoded_video and it is not the next
@@ -94,7 +96,7 @@ VideoDecoder::get_video (VideoFrame frame, bool accurate)
                                break;
                        }
 
-                       if (pass ()) {
+                       if (pass (PASS_REASON_VIDEO)) {
                                /* The decoder has nothing more for us */
                                break;
                        }
@@ -111,7 +113,7 @@ VideoDecoder::get_video (VideoFrame frame, bool accurate)
                dec = decoded_video (frame);
        } else {
                /* Any frame will do: use the first one that comes out of pass() */
-               while (_decoded_video.empty() && !pass ()) {}
+               while (_decoded_video.empty() && !pass (PASS_REASON_VIDEO)) {}
                if (!_decoded_video.empty ()) {
                        dec.push_back (_decoded_video.front ());
                }
@@ -128,7 +130,7 @@ VideoDecoder::get_video (VideoFrame frame, bool accurate)
 
 /** Fill _decoded_video from `from' up to, but not including, `to' */
 void
-VideoDecoder::fill_2d (VideoFrame from, VideoFrame to)
+VideoDecoder::fill_2d (Frame from, Frame to)
 {
        if (to == 0) {
                /* Already OK */
@@ -145,7 +147,7 @@ VideoDecoder::fill_2d (VideoFrame from, VideoFrame to)
                filler_part = _decoded_video.back().part;
        }
 
-       VideoFrame filler_frame = from;
+       Frame filler_frame = from;
        
        while (filler_frame < to) {
 
@@ -162,7 +164,7 @@ VideoDecoder::fill_2d (VideoFrame from, VideoFrame to)
 
 /** Fill _decoded_video from `from' up to, but not including, `to' */
 void
-VideoDecoder::fill_3d (VideoFrame from, VideoFrame to, Eyes eye)
+VideoDecoder::fill_3d (Frame from, Frame to, Eyes eye)
 {
        if (to == 0 && eye == EYES_LEFT) {
                /* Already OK */
@@ -190,7 +192,7 @@ VideoDecoder::fill_3d (VideoFrame from, VideoFrame to, Eyes eye)
                }
        }
 
-       VideoFrame filler_frame = from;
+       Frame filler_frame = from;
        Eyes filler_eye = _decoded_video.empty() ? EYES_LEFT : _decoded_video.back().eyes;
 
        if (_decoded_video.empty ()) {
@@ -230,12 +232,16 @@ VideoDecoder::fill_3d (VideoFrame from, VideoFrame to, Eyes eye)
        
 /** Called by subclasses when they have a video frame ready */
 void
-VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
+VideoDecoder::video (shared_ptr<const ImageProxy> image, Frame frame)
 {
+       if (_ignore_video) {
+               return;
+       }
+
        /* We may receive the same frame index twice for 3D, and we need to know
           when that happens.
        */
-       _same = (!_decoded_video.empty() && frame == _decoded_video.back().frame);
+       bool const same = (!_decoded_video.empty() && frame == _decoded_video.back().frame);
 
        /* Work out what we are going to push into _decoded_video next */
        list<ContentVideo> to_push;
@@ -244,7 +250,7 @@ VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
                to_push.push_back (ContentVideo (image, EYES_BOTH, PART_WHOLE, frame));
                break;
        case VIDEO_FRAME_TYPE_3D_ALTERNATE:
-               to_push.push_back (ContentVideo (image, _same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE, frame));
+               to_push.push_back (ContentVideo (image, same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE, frame));
                break;
        case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
                to_push.push_back (ContentVideo (image, EYES_LEFT, PART_LEFT_HALF, frame));
@@ -269,8 +275,8 @@ VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
           and the things we are about to push.
        */
 
-       boost::optional<VideoFrame> from;
-       boost::optional<VideoFrame> to;
+       boost::optional<Frame> from;
+       boost::optional<Frame> to;
        
        if (_decoded_video.empty() && _last_seek_time && _last_seek_accurate) {
                from = _last_seek_time->frames (_video_content->video_frame_rate ());
@@ -280,6 +286,17 @@ VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
                to = to_push.front().frame;
        }
 
+       /* It has been known that this method receives frames out of order; at this
+          point I'm not sure why, but we'll just ignore them.
+       */
+
+       if (from && to && from.get() > to.get()) {
+               _video_content->film()->log()->log (
+                       String::compose ("Ignoring out-of-order decoded frame %1 after %2", to.get(), from.get()), Log::TYPE_WARNING
+                       );
+               return;
+       }
+
        if (from) {
                if (_video_content->video_frame_type() == VIDEO_FRAME_TYPE_2D) {
                        fill_2d (from.get(), to.get ());
@@ -303,3 +320,10 @@ VideoDecoder::seek (ContentTime s, bool accurate)
        _last_seek_time = s;
        _last_seek_accurate = accurate;
 }
+
+/** Set this player never to produce any video data */
+void
+VideoDecoder::set_ignore_video ()
+{
+       _ignore_video = true;
+}