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