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