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