Further fixes and tidying to 'better-seek'.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "video_decoder.h"
22 #include "image.h"
23 #include "raw_image_proxy.h"
24 #include "film.h"
25 #include "log.h"
26 #include "compose.hpp"
27 #include <boost/foreach.hpp>
28 #include <iostream>
29
30 #include "i18n.h"
31
32 using std::cout;
33 using std::list;
34 using std::max;
35 using std::back_inserter;
36 using boost::shared_ptr;
37 using boost::optional;
38
39 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c, shared_ptr<Log> log)
40         : DecoderPart (parent, log)
41 #ifdef DCPOMATIC_DEBUG
42         , test_gaps (0)
43 #endif
44         , _content (c)
45         , _last_seek_accurate (true)
46 {
47         _black_image.reset (new Image (AV_PIX_FMT_RGB24, _content->video->size(), true));
48         _black_image->make_black ();
49 }
50
51 list<ContentVideo>
52 VideoDecoder::decoded (Frame frame)
53 {
54         list<ContentVideo> output;
55
56         BOOST_FOREACH (ContentVideo const & i, _decoded) {
57                 if (i.frame.index() == frame) {
58                         output.push_back (i);
59                 }
60         }
61
62         return output;
63 }
64
65 /** Get all frames which exist in the content at a given frame index.
66  *  @param frame Frame index.
67  *  @param accurate true to try hard to return frames at the precise time that was requested, otherwise frames nearby may be returned.
68  *  @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
69  */
70 list<ContentVideo>
71 VideoDecoder::get (Frame frame, bool accurate)
72 {
73         if (_no_data_frame && frame >= _no_data_frame.get()) {
74                 return list<ContentVideo> ();
75         }
76
77         _log->log (String::compose ("VD has request for %1", frame), LogEntry::TYPE_DEBUG_DECODE);
78
79         /* See if we have frame, and suggest a seek if not */
80
81         list<ContentVideo>::const_iterator i = _decoded.begin ();
82         while (i != _decoded.end() && i->frame.index() != frame) {
83                 _log->log (String::compose ("VD has stored %1 which is no good", i->frame.index()), LogEntry::TYPE_DEBUG_DECODE);
84                 ++i;
85         }
86
87         if (i == _decoded.end()) {
88                 Frame seek_frame = frame;
89                 if (_content->video->frame_type() == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
90                         /* 3D alternate is a special case as the frame index in the content is not the same
91                            as the frame index we are talking about here.
92                         */
93                         seek_frame *= 2;
94                 }
95                 _log->log (String::compose ("VD suggests seek to %1", seek_frame), LogEntry::TYPE_DEBUG_DECODE);
96                 maybe_seek (ContentTime::from_frames (seek_frame, _content->active_video_frame_rate()), accurate);
97         }
98
99         /* Work out the number of frames that we should return; we
100            must return all frames in our content at the requested `time'
101            (i.e. frame)
102         */
103         unsigned int frames_wanted = 0;
104         switch (_content->video->frame_type()) {
105         case VIDEO_FRAME_TYPE_2D:
106         case VIDEO_FRAME_TYPE_3D_LEFT:
107         case VIDEO_FRAME_TYPE_3D_RIGHT:
108                 frames_wanted = 1;
109                 break;
110         case VIDEO_FRAME_TYPE_3D:
111         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
112         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
113         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
114                 frames_wanted = 2;
115                 break;
116         default:
117                 DCPOMATIC_ASSERT (false);
118         }
119
120         list<ContentVideo> dec;
121
122         /* Now enough pass() calls should either:
123          *  (a) give us what we want, or
124          *  (b) give us something after what we want, indicating that we will never get what we want, or
125          *  (c) hit the end of the decoder.
126          */
127         if (accurate) {
128                 /* We are being accurate, so we want the right frame.
129                  * This could all be one statement but it's split up for clarity.
130                  */
131                 bool no_data = false;
132
133                 while (true) {
134                         if (decoded(frame).size() == frames_wanted) {
135                                 /* We got what we want */
136                                 break;
137                         }
138
139                         if (_parent->pass (Decoder::PASS_REASON_VIDEO, accurate)) {
140                                 /* The decoder has nothing more for us */
141                                 no_data = true;
142                                 break;
143                         }
144
145                         if (!_decoded.empty() && _decoded.front().frame.index() > frame) {
146                                 /* We're never going to get the frame we want.  Perhaps the caller is asking
147                                  * for a video frame before the content's video starts (if its audio
148                                  * begins before its video, for example).
149                                  */
150                                 break;
151                         }
152                 }
153
154                 dec = decoded (frame);
155
156                 if (no_data && dec.empty()) {
157                         _no_data_frame = frame;
158                 }
159
160         } else {
161                 /* Any frame(s) will do: use the first one(s) that comes out of pass() */
162                 while (_decoded.size() < frames_wanted && !_parent->pass (Decoder::PASS_REASON_VIDEO, accurate)) {}
163                 list<ContentVideo>::const_iterator i = _decoded.begin();
164                 unsigned int j = 0;
165                 while (i != _decoded.end() && j < frames_wanted) {
166                         dec.push_back (*i);
167                         ++i;
168                         ++j;
169                 }
170         }
171
172         /* Clean up _decoded; keep the frame we are returning, if any (which may have two images
173            for 3D), but nothing before that
174         */
175         while (!_decoded.empty() && !dec.empty() && _decoded.front().frame.index() < dec.front().frame.index()) {
176                 _log->log (String::compose ("VD discards %1", _decoded.front().frame.index()), LogEntry::TYPE_DEBUG_DECODE);
177                 _decoded.pop_front ();
178         }
179
180         return dec;
181 }
182
183 /** Fill _decoded from `from' up to, but not including, `to' with
184  *  a frame for one particular Eyes value (which could be EYES_BOTH,
185  *  EYES_LEFT or EYES_RIGHT)
186  */
187 void
188 VideoDecoder::fill_one_eye (Frame from, Frame to, Eyes eye)
189 {
190         if (to == 0) {
191                 /* Already OK */
192                 return;
193         }
194
195         /* Fill with black... */
196         shared_ptr<const ImageProxy> filler_image (new RawImageProxy (_black_image));
197         Part filler_part = PART_WHOLE;
198
199         /* ...unless there's some video we can fill with */
200         if (!_decoded.empty ()) {
201                 filler_image = _decoded.back().image;
202                 filler_part = _decoded.back().part;
203         }
204
205         for (Frame i = from; i < to; ++i) {
206 #ifdef DCPOMATIC_DEBUG
207                 test_gaps++;
208 #endif
209                 _decoded.push_back (
210                         ContentVideo (filler_image, VideoFrame (i, eye), filler_part)
211                         );
212         }
213 }
214
215 /** Fill _decoded from `from' up to, but not including, `to'
216  *  adding both left and right eye frames.
217  */
218 void
219 VideoDecoder::fill_both_eyes (VideoFrame from, VideoFrame to)
220 {
221         /* Fill with black... */
222         shared_ptr<const ImageProxy> filler_left_image (new RawImageProxy (_black_image));
223         shared_ptr<const ImageProxy> filler_right_image (new RawImageProxy (_black_image));
224         Part filler_left_part = PART_WHOLE;
225         Part filler_right_part = PART_WHOLE;
226
227         /* ...unless there's some video we can fill with */
228         for (list<ContentVideo>::const_reverse_iterator i = _decoded.rbegin(); i != _decoded.rend(); ++i) {
229                 if (i->frame.eyes() == EYES_LEFT && !filler_left_image) {
230                         filler_left_image = i->image;
231                         filler_left_part = i->part;
232                 } else if (i->frame.eyes() == EYES_RIGHT && !filler_right_image) {
233                         filler_right_image = i->image;
234                         filler_right_part = i->part;
235                 }
236
237                 if (filler_left_image && filler_right_image) {
238                         break;
239                 }
240         }
241
242         while (from != to) {
243
244 #ifdef DCPOMATIC_DEBUG
245                 test_gaps++;
246 #endif
247
248                 _decoded.push_back (
249                         ContentVideo (
250                                 from.eyes() == EYES_LEFT ? filler_left_image : filler_right_image,
251                                 from,
252                                 from.eyes() == EYES_LEFT ? filler_left_part : filler_right_part
253                                 )
254                         );
255
256                 ++from;
257         }
258 }
259
260 /** Called by decoder classes when they have a video frame ready.
261  *  @param frame Frame index within the content; this does not take into account 3D
262  *  so for 3D_ALTERNATE this value goes:
263  *     0: frame 0 left
264  *     1: frame 0 right
265  *     2: frame 1 left
266  *     3: frame 1 right
267  *  and so on.
268  */
269 void
270 VideoDecoder::give (shared_ptr<const ImageProxy> image, Frame frame)
271 {
272         if (ignore ()) {
273                 return;
274         }
275
276         _log->log (String::compose ("VD receives %1", frame), LogEntry::TYPE_DEBUG_DECODE);
277         _position = ContentTime::from_frames (frame, _content->active_video_frame_rate());
278
279         /* Work out what we are going to push into _decoded next */
280         list<ContentVideo> to_push;
281         switch (_content->video->frame_type ()) {
282         case VIDEO_FRAME_TYPE_2D:
283                 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_BOTH), PART_WHOLE));
284                 break;
285         case VIDEO_FRAME_TYPE_3D:
286         {
287                 /* We receive the same frame index twice for 3D; hence we know which
288                    frame this one is.
289                 */
290                 bool const same = (!_decoded.empty() && frame == _decoded.back().frame.index());
291                 to_push.push_back (ContentVideo (image, VideoFrame (frame, same ? EYES_RIGHT : EYES_LEFT), PART_WHOLE));
292                 break;
293         }
294         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
295                 to_push.push_back (ContentVideo (image, VideoFrame (frame / 2, (frame % 2) ? EYES_RIGHT : EYES_LEFT), PART_WHOLE));
296                 break;
297         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
298                 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_LEFT), PART_LEFT_HALF));
299                 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_RIGHT), PART_RIGHT_HALF));
300                 break;
301         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
302                 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_LEFT), PART_TOP_HALF));
303                 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_RIGHT), PART_BOTTOM_HALF));
304                 break;
305         case VIDEO_FRAME_TYPE_3D_LEFT:
306                 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_LEFT), PART_WHOLE));
307                 break;
308         case VIDEO_FRAME_TYPE_3D_RIGHT:
309                 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_RIGHT), PART_WHOLE));
310                 break;
311         default:
312                 DCPOMATIC_ASSERT (false);
313         }
314
315         /* Now VideoDecoder is required never to have gaps in the frames that it presents
316            via get_video().  Hence we need to fill in any gap between the last thing in _decoded
317            and the things we are about to push.
318         */
319
320         optional<VideoFrame> from;
321
322         if (_decoded.empty() && _last_seek_time && _last_seek_accurate) {
323                 from = VideoFrame (
324                         _last_seek_time->frames_round (_content->active_video_frame_rate ()),
325                         _content->video->frame_type() == VIDEO_FRAME_TYPE_2D ? EYES_BOTH : EYES_LEFT
326                         );
327         } else if (!_decoded.empty ()) {
328                 /* Get the last frame we have */
329                 from = _decoded.back().frame;
330                 /* And move onto the first frame we need */
331                 ++(*from);
332                 if (_content->video->frame_type() == VIDEO_FRAME_TYPE_3D_LEFT || _content->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
333                         /* The previous ++ will increment a 3D-left-eye to the same index right-eye.  If we are dealing with
334                            a single-eye source we need an extra ++ to move back to the same eye.
335                         */
336                         ++(*from);
337                 }
338         }
339
340         /* If we've pre-rolled on a seek we may now receive out-of-order frames
341            (frames before the last seek time) which we can just ignore.
342         */
343         if (from && (*from) > to_push.front().frame) {
344                 return;
345         }
346
347         unsigned int const max_decoded_size = 96;
348
349         /* If _decoded is already `full' there is no point in adding anything more to it,
350            as the new stuff will just be removed again.
351         */
352         if (_decoded.size() < max_decoded_size) {
353                 if (from) {
354                         switch (_content->video->frame_type ()) {
355                         case VIDEO_FRAME_TYPE_2D:
356                                 fill_one_eye (from->index(), to_push.front().frame.index(), EYES_BOTH);
357                                 break;
358                         case VIDEO_FRAME_TYPE_3D:
359                         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
360                         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
361                         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
362                                 fill_both_eyes (from.get(), to_push.front().frame);
363                                 break;
364                         case VIDEO_FRAME_TYPE_3D_LEFT:
365                                 fill_one_eye (from->index(), to_push.front().frame.index(), EYES_LEFT);
366                                 break;
367                         case VIDEO_FRAME_TYPE_3D_RIGHT:
368                                 fill_one_eye (from->index(), to_push.front().frame.index(), EYES_RIGHT);
369                                 break;
370                         }
371                 }
372
373                 copy (to_push.begin(), to_push.end(), back_inserter (_decoded));
374         }
375
376         /* We can't let this build up too much or we will run out of memory.  There is a
377            `best' value for the allowed size of _decoded which balances memory use
378            with decoding efficiency (lack of seeks).  Throwing away video frames here
379            is not a problem for correctness, so do it.
380         */
381         while (_decoded.size() > max_decoded_size) {
382                 _decoded.pop_back ();
383         }
384 }
385
386 void
387 VideoDecoder::seek (ContentTime s, bool accurate)
388 {
389         _log->log (String::compose ("VD seek to %1", to_string(s)), LogEntry::TYPE_DEBUG_DECODE);
390         _decoded.clear ();
391         _last_seek_time = s;
392         _last_seek_accurate = accurate;
393         _position.reset ();
394 }