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