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