Increase stored-frames limit; fix skip_frame_test reference.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /*
2     Copyright (C) 2012-2014 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 "image_proxy.h"
23 #include "raw_image_proxy.h"
24 #include "content_video.h"
25
26 #include "i18n.h"
27
28 using std::cout;
29 using std::list;
30 using std::max;
31 using std::back_inserter;
32 using boost::shared_ptr;
33 using boost::optional;
34
35 VideoDecoder::VideoDecoder (shared_ptr<const VideoContent> c)
36 #ifdef DCPOMATIC_DEBUG
37         : test_gaps (0)
38         , _video_content (c)
39 #else
40         : _video_content (c)
41 #endif
42         , _same (false)
43         , _last_seek_accurate (true)
44 {
45         _black_image.reset (new Image (PIX_FMT_RGB24, _video_content->video_size(), true));
46         _black_image->make_black ();
47 }
48
49 list<ContentVideo>
50 VideoDecoder::decoded_video (VideoFrame frame)
51 {
52         list<ContentVideo> output;
53         
54         for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
55                 if (i->frame == frame) {
56                         output.push_back (*i);
57                 }
58         }
59
60         return output;
61 }
62
63 /** Get all frames which exist in the content at a given frame index.
64  *  @param frame Frame index.
65  *  @param accurate true to try hard to return frames at the precise time that was requested, otherwise frames nearby may be returned.
66  *  @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
67  */
68 list<ContentVideo>
69 VideoDecoder::get_video (VideoFrame frame, bool accurate)
70 {
71         /* At this stage, if we have get_video()ed before, _decoded_video will contain the last frame that this
72            method returned (and possibly a few more).  If the requested frame is not in _decoded_video and it is not the next
73            one after the end of _decoded_video we need to seek.
74         */
75
76         if (_decoded_video.empty() || frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1)) {
77                 seek (ContentTime::from_frames (frame, _video_content->video_frame_rate()), accurate);
78         }
79
80         list<ContentVideo> dec;
81
82         /* Now enough pass() calls should either:
83          *  (a) give us what we want, or
84          *  (b) give us something after what we want, indicating that we will never get what we want, or
85          *  (c) hit the end of the decoder.
86          */
87         if (accurate) {
88                 /* We are being accurate, so we want the right frame.
89                  * This could all be one statement but it's split up for clarity.
90                  */
91                 while (true) {
92                         if (!decoded_video(frame).empty ()) {
93                                 /* We got what we want */
94                                 break;
95                         }
96
97                         if (pass ()) {
98                                 /* The decoder has nothing more for us */
99                                 break;
100                         }
101
102                         if (!_decoded_video.empty() && _decoded_video.front().frame > frame) {
103                                 /* We're never going to get the frame we want.  Perhaps the caller is asking
104                                  * for a video frame before the content's video starts (if its audio
105                                  * begins before its video, for example).
106                                  */
107                                 break;
108                         }
109                 }
110
111                 dec = decoded_video (frame);
112         } else {
113                 /* Any frame will do: use the first one that comes out of pass() */
114                 while (_decoded_video.empty() && !pass ()) {}
115                 if (!_decoded_video.empty ()) {
116                         dec.push_back (_decoded_video.front ());
117                 }
118         }
119
120         /* Clean up _decoded_video; keep the frame we are returning (which may have two images
121            for 3D), but nothing before that */
122         while (!_decoded_video.empty() && _decoded_video.front().frame < dec.front().frame) {
123                 _decoded_video.pop_front ();
124         }
125
126         return dec;
127 }
128
129 /** Fill _decoded_video from `from' up to, but not including, `to' */
130 void
131 VideoDecoder::fill_2d (VideoFrame from, VideoFrame to)
132 {
133         if (to == 0) {
134                 /* Already OK */
135                 return;
136         }
137
138         /* Fill with black... */
139         boost::shared_ptr<const ImageProxy> filler_image (new RawImageProxy (_black_image));
140         Part filler_part = PART_WHOLE;
141
142         /* ...unless there's some video we can fill with */
143         if (!_decoded_video.empty ()) {
144                 filler_image = _decoded_video.back().image;
145                 filler_part = _decoded_video.back().part;
146         }
147
148         VideoFrame filler_frame = from;
149         
150         while (filler_frame < to) {
151
152 #ifdef DCPOMATIC_DEBUG
153                 test_gaps++;
154 #endif
155                 _decoded_video.push_back (
156                         ContentVideo (filler_image, EYES_BOTH, filler_part, filler_frame)
157                         );
158                 
159                 ++filler_frame;
160         }
161 }
162
163 /** Fill _decoded_video from `from' up to, but not including, `to' */
164 void
165 VideoDecoder::fill_3d (VideoFrame from, VideoFrame to, Eyes eye)
166 {
167         if (to == 0 && eye == EYES_LEFT) {
168                 /* Already OK */
169                 return;
170         }
171
172         /* Fill with black... */
173         boost::shared_ptr<const ImageProxy> filler_left_image (new RawImageProxy (_black_image));
174         boost::shared_ptr<const ImageProxy> filler_right_image (new RawImageProxy (_black_image));
175         Part filler_left_part = PART_WHOLE;
176         Part filler_right_part = PART_WHOLE;
177
178         /* ...unless there's some video we can fill with */
179         for (list<ContentVideo>::const_reverse_iterator i = _decoded_video.rbegin(); i != _decoded_video.rend(); ++i) {
180                 if (i->eyes == EYES_LEFT && !filler_left_image) {
181                         filler_left_image = i->image;
182                         filler_left_part = i->part;
183                 } else if (i->eyes == EYES_RIGHT && !filler_right_image) {
184                         filler_right_image = i->image;
185                         filler_right_part = i->part;
186                 }
187
188                 if (filler_left_image && filler_right_image) {
189                         break;
190                 }
191         }
192
193         VideoFrame filler_frame = from;
194         Eyes filler_eye = _decoded_video.empty() ? EYES_LEFT : _decoded_video.back().eyes;
195
196         if (_decoded_video.empty ()) {
197                 filler_frame = 0;
198                 filler_eye = EYES_LEFT;
199         } else if (_decoded_video.back().eyes == EYES_LEFT) {
200                 filler_frame = _decoded_video.back().frame;
201                 filler_eye = EYES_RIGHT;
202         } else if (_decoded_video.back().eyes == EYES_RIGHT) {
203                 filler_frame = _decoded_video.back().frame + 1;
204                 filler_eye = EYES_LEFT;
205         }
206
207         while (filler_frame != to || filler_eye != eye) {
208
209 #ifdef DCPOMATIC_DEBUG
210                 test_gaps++;
211 #endif
212
213                 _decoded_video.push_back (
214                         ContentVideo (
215                                 filler_eye == EYES_LEFT ? filler_left_image : filler_right_image,
216                                 filler_eye,
217                                 filler_eye == EYES_LEFT ? filler_left_part : filler_right_part,
218                                 filler_frame
219                                 )
220                         );
221
222                 if (filler_eye == EYES_LEFT) {
223                         filler_eye = EYES_RIGHT;
224                 } else {
225                         filler_eye = EYES_LEFT;
226                         ++filler_frame;
227                 }
228         }
229 }
230         
231 /** Called by subclasses when they have a video frame ready */
232 void
233 VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
234 {
235         /* We may receive the same frame index twice for 3D, and we need to know
236            when that happens.
237         */
238         _same = (!_decoded_video.empty() && frame == _decoded_video.back().frame);
239
240         /* Work out what we are going to push into _decoded_video next */
241         list<ContentVideo> to_push;
242         switch (_video_content->video_frame_type ()) {
243         case VIDEO_FRAME_TYPE_2D:
244                 to_push.push_back (ContentVideo (image, EYES_BOTH, PART_WHOLE, frame));
245                 break;
246         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
247                 to_push.push_back (ContentVideo (image, _same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE, frame));
248                 break;
249         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
250                 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_LEFT_HALF, frame));
251                 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_RIGHT_HALF, frame));
252                 break;
253         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
254                 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_TOP_HALF, frame));
255                 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_BOTTOM_HALF, frame));
256                 break;
257         case VIDEO_FRAME_TYPE_3D_LEFT:
258                 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_WHOLE, frame));
259                 break;
260         case VIDEO_FRAME_TYPE_3D_RIGHT:
261                 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_WHOLE, frame));
262                 break;
263         default:
264                 DCPOMATIC_ASSERT (false);
265         }
266
267         /* Now VideoDecoder is required never to have gaps in the frames that it presents
268            via get_video().  Hence we need to fill in any gap between the last thing in _decoded_video
269            and the things we are about to push.
270         */
271
272         boost::optional<VideoFrame> from;
273         boost::optional<VideoFrame> to;
274         
275         if (_decoded_video.empty() && _last_seek_time && _last_seek_accurate) {
276                 from = _last_seek_time->frames (_video_content->video_frame_rate ());
277                 to = to_push.front().frame;
278         } else if (!_decoded_video.empty ()) {
279                 from = _decoded_video.back().frame + 1;
280                 to = to_push.front().frame;
281         }
282
283         if (from) {
284                 if (_video_content->video_frame_type() == VIDEO_FRAME_TYPE_2D) {
285                         fill_2d (from.get(), to.get ());
286                 } else {
287                         fill_3d (from.get(), to.get(), to_push.front().eyes);
288                 }
289         }
290
291         copy (to_push.begin(), to_push.end(), back_inserter (_decoded_video));
292
293         /* We can't let this build up too much or we will run out of memory.  We need to allow
294            the most frames that can exist between blocks of sound in a multiplexed file.
295         */
296         DCPOMATIC_ASSERT (_decoded_video.size() <= 96);
297 }
298
299 void
300 VideoDecoder::seek (ContentTime s, bool accurate)
301 {
302         _decoded_video.clear ();
303         _last_seek_time = s;
304         _last_seek_accurate = accurate;
305 }