Rename video/audio/subtitle part methods.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /*
2     Copyright (C) 2012-2016 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 #include <iostream>
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, shared_ptr<Log> log)
38 #ifdef DCPOMATIC_DEBUG
39         : test_gaps (0)
40         , _video_content (c)
41 #else
42         : _video_content (c)
43 #endif
44         , _log (log)
45         , _last_seek_accurate (true)
46         , _ignore_video (false)
47 {
48         _black_image.reset (new Image (AV_PIX_FMT_RGB24, _video_content->size(), true));
49         _black_image->make_black ();
50 }
51
52 list<ContentVideo>
53 VideoDecoder::decoded_video (Frame frame)
54 {
55         list<ContentVideo> output;
56
57         for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
58                 if (i->frame == frame) {
59                         output.push_back (*i);
60                 }
61         }
62
63         return output;
64 }
65
66 /** Get all frames which exist in the content at a given frame index.
67  *  @param frame Frame index.
68  *  @param accurate true to try hard to return frames at the precise time that was requested, otherwise frames nearby may be returned.
69  *  @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
70  */
71 list<ContentVideo>
72 VideoDecoder::get_video (Frame frame, bool accurate)
73 {
74         if (_no_data_frame && frame >= _no_data_frame.get()) {
75                 return list<ContentVideo> ();
76         }
77
78         /* At this stage, if we have get_video()ed before, _decoded_video will contain the last frame that this
79            method returned (and possibly a few more).  If the requested frame is not in _decoded_video and it is not the next
80            one after the end of _decoded_video we need to seek.
81         */
82
83         _log->log (String::compose ("VD has request for %1", frame), LogEntry::TYPE_DEBUG_DECODE);
84
85         if (_decoded_video.empty() || frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1)) {
86                 seek (ContentTime::from_frames (frame, _video_content->frame_rate()), accurate);
87         }
88
89         list<ContentVideo> dec;
90
91         /* Now enough pass() calls should either:
92          *  (a) give us what we want, or
93          *  (b) give us something after what we want, indicating that we will never get what we want, or
94          *  (c) hit the end of the decoder.
95          */
96         if (accurate) {
97                 /* We are being accurate, so we want the right frame.
98                  * This could all be one statement but it's split up for clarity.
99                  */
100                 bool no_data = false;
101
102                 while (true) {
103                         if (!decoded_video(frame).empty ()) {
104                                 /* We got what we want */
105                                 break;
106                         }
107
108                         if (pass (PASS_REASON_VIDEO, accurate)) {
109                                 /* The decoder has nothing more for us */
110                                 no_data = true;
111                                 break;
112                         }
113
114                         if (!_decoded_video.empty() && _decoded_video.front().frame > frame) {
115                                 /* We're never going to get the frame we want.  Perhaps the caller is asking
116                                  * for a video frame before the content's video starts (if its audio
117                                  * begins before its video, for example).
118                                  */
119                                 break;
120                         }
121                 }
122
123                 dec = decoded_video (frame);
124
125                 if (no_data && dec.empty()) {
126                         _no_data_frame = frame;
127                 }
128
129         } else {
130                 /* Any frame will do: use the first one that comes out of pass() */
131                 while (_decoded_video.empty() && !pass (PASS_REASON_VIDEO, accurate)) {}
132                 if (!_decoded_video.empty ()) {
133                         dec.push_back (_decoded_video.front ());
134                 }
135         }
136
137         /* Clean up _decoded_video; keep the frame we are returning, if any (which may have two images
138            for 3D), but nothing before that */
139         while (!_decoded_video.empty() && !dec.empty() && _decoded_video.front().frame < dec.front().frame) {
140                 _decoded_video.pop_front ();
141         }
142
143         return dec;
144 }
145
146 /** Fill _decoded_video from `from' up to, but not including, `to' with
147  *  a frame for one particular Eyes value (which could be EYES_BOTH,
148  *  EYES_LEFT or EYES_RIGHT)
149  */
150 void
151 VideoDecoder::fill_one_eye (Frame from, Frame to, Eyes eye)
152 {
153         if (to == 0) {
154                 /* Already OK */
155                 return;
156         }
157
158         /* Fill with black... */
159         shared_ptr<const ImageProxy> filler_image (new RawImageProxy (_black_image));
160         Part filler_part = PART_WHOLE;
161
162         /* ...unless there's some video we can fill with */
163         if (!_decoded_video.empty ()) {
164                 filler_image = _decoded_video.back().image;
165                 filler_part = _decoded_video.back().part;
166         }
167
168         for (Frame i = from; i < to; ++i) {
169 #ifdef DCPOMATIC_DEBUG
170                 test_gaps++;
171 #endif
172                 _decoded_video.push_back (
173                         ContentVideo (filler_image, eye, filler_part, i)
174                         );
175         }
176 }
177
178 /** Fill _decoded_video from `from' up to, but not including, `to'
179  *  adding both left and right eye frames.
180  */
181 void
182 VideoDecoder::fill_both_eyes (Frame from, Frame to, Eyes eye)
183 {
184         if (to == 0 && eye == EYES_LEFT) {
185                 /* Already OK */
186                 return;
187         }
188
189         /* Fill with black... */
190         shared_ptr<const ImageProxy> filler_left_image (new RawImageProxy (_black_image));
191         shared_ptr<const ImageProxy> filler_right_image (new RawImageProxy (_black_image));
192         Part filler_left_part = PART_WHOLE;
193         Part filler_right_part = PART_WHOLE;
194
195         /* ...unless there's some video we can fill with */
196         for (list<ContentVideo>::const_reverse_iterator i = _decoded_video.rbegin(); i != _decoded_video.rend(); ++i) {
197                 if (i->eyes == EYES_LEFT && !filler_left_image) {
198                         filler_left_image = i->image;
199                         filler_left_part = i->part;
200                 } else if (i->eyes == EYES_RIGHT && !filler_right_image) {
201                         filler_right_image = i->image;
202                         filler_right_part = i->part;
203                 }
204
205                 if (filler_left_image && filler_right_image) {
206                         break;
207                 }
208         }
209
210         Frame filler_frame = from;
211         Eyes filler_eye = _decoded_video.empty() ? EYES_LEFT : _decoded_video.back().eyes;
212
213         if (_decoded_video.empty ()) {
214                 filler_frame = 0;
215                 filler_eye = EYES_LEFT;
216         } else if (_decoded_video.back().eyes == EYES_LEFT) {
217                 filler_frame = _decoded_video.back().frame;
218                 filler_eye = EYES_RIGHT;
219         } else if (_decoded_video.back().eyes == EYES_RIGHT) {
220                 filler_frame = _decoded_video.back().frame + 1;
221                 filler_eye = EYES_LEFT;
222         }
223
224         while (filler_frame != to || filler_eye != eye) {
225
226 #ifdef DCPOMATIC_DEBUG
227                 test_gaps++;
228 #endif
229
230                 _decoded_video.push_back (
231                         ContentVideo (
232                                 filler_eye == EYES_LEFT ? filler_left_image : filler_right_image,
233                                 filler_eye,
234                                 filler_eye == EYES_LEFT ? filler_left_part : filler_right_part,
235                                 filler_frame
236                                 )
237                         );
238
239                 if (filler_eye == EYES_LEFT) {
240                         filler_eye = EYES_RIGHT;
241                 } else {
242                         filler_eye = EYES_LEFT;
243                         ++filler_frame;
244                 }
245         }
246 }
247
248 /** Called by subclasses when they have a video frame ready */
249 void
250 VideoDecoder::video (shared_ptr<const ImageProxy> image, Frame frame)
251 {
252         if (_ignore_video) {
253                 return;
254         }
255
256         _log->log (String::compose ("VD receives %1", frame), LogEntry::TYPE_DEBUG_DECODE);
257
258         /* Work out what we are going to push into _decoded_video next */
259         list<ContentVideo> to_push;
260         switch (_video_content->frame_type ()) {
261         case VIDEO_FRAME_TYPE_2D:
262                 to_push.push_back (ContentVideo (image, EYES_BOTH, PART_WHOLE, frame));
263                 break;
264         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
265         {
266                 /* We receive the same frame index twice for 3D-alternate; hence we know which
267                    frame this one is.
268                 */
269                 bool const same = (!_decoded_video.empty() && frame == _decoded_video.back().frame);
270                 to_push.push_back (ContentVideo (image, same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE, frame));
271                 break;
272         }
273         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
274                 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_LEFT_HALF, frame));
275                 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_RIGHT_HALF, frame));
276                 break;
277         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
278                 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_TOP_HALF, frame));
279                 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_BOTTOM_HALF, frame));
280                 break;
281         case VIDEO_FRAME_TYPE_3D_LEFT:
282                 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_WHOLE, frame));
283                 break;
284         case VIDEO_FRAME_TYPE_3D_RIGHT:
285                 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_WHOLE, frame));
286                 break;
287         default:
288                 DCPOMATIC_ASSERT (false);
289         }
290
291         /* Now VideoDecoder is required never to have gaps in the frames that it presents
292            via get_video().  Hence we need to fill in any gap between the last thing in _decoded_video
293            and the things we are about to push.
294         */
295
296         optional<Frame> from;
297         optional<Frame> to;
298
299         if (_decoded_video.empty() && _last_seek_time && _last_seek_accurate) {
300                 from = _last_seek_time->frames_round (_video_content->frame_rate ());
301                 to = to_push.front().frame;
302         } else if (!_decoded_video.empty ()) {
303                 from = _decoded_video.back().frame + 1;
304                 to = to_push.front().frame;
305         }
306
307         /* If we've pre-rolled on a seek we may now receive out-of-order frames
308            (frames before the last seek time) which we can just ignore.
309         */
310
311         if (from && to && from.get() > to.get()) {
312                 return;
313         }
314
315         if (from) {
316                 switch (_video_content->frame_type ()) {
317                 case VIDEO_FRAME_TYPE_2D:
318                         fill_one_eye (from.get(), to.get (), EYES_BOTH);
319                         break;
320                 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
321                 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
322                 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
323                         fill_both_eyes (from.get(), to.get(), to_push.front().eyes);
324                         break;
325                 case VIDEO_FRAME_TYPE_3D_LEFT:
326                         fill_one_eye (from.get(), to.get (), EYES_LEFT);
327                         break;
328                 case VIDEO_FRAME_TYPE_3D_RIGHT:
329                         fill_one_eye (from.get(), to.get (), EYES_RIGHT);
330                         break;
331                 }
332         }
333
334         copy (to_push.begin(), to_push.end(), back_inserter (_decoded_video));
335
336         /* We can't let this build up too much or we will run out of memory.  There is a
337            `best' value for the allowed size of _decoded_video which balances memory use
338            with decoding efficiency (lack of seeks).  Throwing away video frames here
339            is not a problem for correctness, so do it.
340         */
341         while (_decoded_video.size() > 96) {
342                 _decoded_video.pop_back ();
343         }
344 }
345
346 void
347 VideoDecoder::seek (ContentTime s, bool accurate)
348 {
349         _decoded_video.clear ();
350         _last_seek_time = s;
351         _last_seek_accurate = accurate;
352 }
353
354 /** Set this player never to produce any video data */
355 void
356 VideoDecoder::set_ignore_video ()
357 {
358         _ignore_video = true;
359 }