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