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