Still more decode logging.
[dcpomatic.git] / src / lib / video_decoder.cc
index 8c1b27653199716288b69e506c8ce84a08204c67..383af102d9965e230fa2595070e8086e47fc9061 100644 (file)
@@ -37,17 +37,12 @@ using boost::shared_ptr;
 using boost::optional;
 
 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c, shared_ptr<Log> log)
+       : DecoderPart (parent, log)
 #ifdef DCPOMATIC_DEBUG
-       : test_gaps (0)
-       , _parent (parent),
-         _content (c)
-#else
-        : _parent (parent)
-       , _content (c)
+       , test_gaps (0)
 #endif
-       , _log (log)
+       , _content (c)
        , _last_seek_accurate (true)
-       , _ignore (false)
 {
        _black_image.reset (new Image (AV_PIX_FMT_RGB24, _content->video->size(), true));
        _black_image->make_black ();
@@ -79,15 +74,26 @@ VideoDecoder::get (Frame frame, bool accurate)
                return list<ContentVideo> ();
        }
 
-       /* At this stage, if we have get_video()ed before, _decoded will contain the last frame that this
-          method returned (and possibly a few more).  If the requested frame is not in _decoded and it is not the next
-          one after the end of _decoded we need to seek.
-       */
-
        _log->log (String::compose ("VD has request for %1", frame), LogEntry::TYPE_DEBUG_DECODE);
 
-       if (_decoded.empty() || frame < _decoded.front().frame.index() || frame > (_decoded.back().frame.index() + 1)) {
-               _parent->seek (ContentTime::from_frames (frame, _content->active_video_frame_rate()), accurate);
+       /* See if we have frame, and suggest a seek if not */
+
+       list<ContentVideo>::const_iterator i = _decoded.begin ();
+       while (i != _decoded.end() && i->frame.index() != frame) {
+               _log->log (String::compose ("VD has stored %1 which is no good", i->frame.index()), LogEntry::TYPE_DEBUG_DECODE);
+               ++i;
+       }
+
+       if (i == _decoded.end()) {
+               Frame seek_frame = frame;
+               if (_content->video->frame_type() == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
+                       /* 3D alternate is a special case as the frame index in the content is not the same
+                          as the frame index we are talking about here.
+                       */
+                       seek_frame *= 2;
+               }
+               _log->log (String::compose ("VD suggests seek to %1", seek_frame), LogEntry::TYPE_DEBUG_DECODE);
+               maybe_seek (ContentTime::from_frames (seek_frame, _content->active_video_frame_rate()), accurate);
        }
 
        /* Work out the number of frames that we should return; we
@@ -167,6 +173,7 @@ VideoDecoder::get (Frame frame, bool accurate)
           for 3D), but nothing before that
        */
        while (!_decoded.empty() && !dec.empty() && _decoded.front().frame.index() < dec.front().frame.index()) {
+               _log->log (String::compose ("VD discards %1", _decoded.front().frame.index()), LogEntry::TYPE_DEBUG_DECODE);
                _decoded.pop_front ();
        }
 
@@ -250,11 +257,19 @@ VideoDecoder::fill_both_eyes (VideoFrame from, VideoFrame to)
        }
 }
 
-/** Called by decoder classes when they have a video frame ready */
+/** Called by decoder classes when they have a video frame ready.
+ *  @param frame Frame index within the content; this does not take into account 3D
+ *  so for 3D_ALTERNATE this value goes:
+ *     0: frame 0 left
+ *     1: frame 0 right
+ *     2: frame 1 left
+ *     3: frame 1 right
+ *  and so on.
+ */
 void
 VideoDecoder::give (shared_ptr<const ImageProxy> image, Frame frame)
 {
-       if (_ignore) {
+       if (ignore ()) {
                return;
        }
 
@@ -370,14 +385,8 @@ VideoDecoder::give (shared_ptr<const ImageProxy> image, Frame frame)
 void
 VideoDecoder::seek (ContentTime s, bool accurate)
 {
+       _log->log (String::compose ("VD seek to %1", to_string(s)), LogEntry::TYPE_DEBUG_DECODE);
        _decoded.clear ();
        _last_seek_time = s;
        _last_seek_accurate = accurate;
 }
-
-/** Set this decoder never to produce any data */
-void
-VideoDecoder::set_ignore ()
-{
-       _ignore = true;
-}