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