Fix audio analysis; make sure we don't decode video and let it pile up unwanted.
[dcpomatic.git] / src / lib / video_decoder.cc
index 711f047d14ca8c33a711f83ec682418f5d963841..b7cf1641b57d6b999c7c62b3c05f405e1f7b7075 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,8 +41,8 @@ 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 ();
@@ -147,7 +149,7 @@ VideoDecoder::fill_2d (VideoFrame from, VideoFrame to)
 
        VideoFrame filler_frame = from;
        
-       while (filler_frame < (to - 1)) {
+       while (filler_frame < to) {
 
 #ifdef DCPOMATIC_DEBUG
                test_gaps++;
@@ -232,10 +234,14 @@ VideoDecoder::fill_3d (VideoFrame from, VideoFrame to, Eyes eye)
 void
 VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame 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));
@@ -261,7 +267,7 @@ VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
                to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_WHOLE, frame));
                break;
        default:
-               assert (false);
+               DCPOMATIC_ASSERT (false);
        }
 
        /* Now VideoDecoder is required never to have gaps in the frames that it presents
@@ -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 ());
@@ -289,6 +306,11 @@ VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
        }
 
        copy (to_push.begin(), to_push.end(), back_inserter (_decoded_video));
+
+       /* We can't let this build up too much or we will run out of memory.  We need to allow
+          the most frames that can exist between blocks of sound in a multiplexed file.
+       */
+       DCPOMATIC_ASSERT (_decoded_video.size() <= 96);
 }
 
 void
@@ -298,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;
+}