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