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