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