2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "video_decoder.h"
23 #include "raw_image_proxy.h"
26 #include "compose.hpp"
27 #include <boost/foreach.hpp>
35 using std::back_inserter;
36 using boost::shared_ptr;
37 using boost::optional;
39 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c, shared_ptr<Log> log)
40 #ifdef DCPOMATIC_DEBUG
49 , _last_seek_accurate (true)
52 _black_image.reset (new Image (AV_PIX_FMT_RGB24, _content->video->size(), true));
53 _black_image->make_black ();
57 VideoDecoder::decoded (Frame frame)
59 list<ContentVideo> output;
61 BOOST_FOREACH (ContentVideo const & i, _decoded) {
62 if (i.frame.index() == frame) {
70 /** Get all frames which exist in the content at a given frame index.
71 * @param frame Frame index.
72 * @param accurate true to try hard to return frames at the precise time that was requested, otherwise frames nearby may be returned.
73 * @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
76 VideoDecoder::get (Frame frame, bool accurate)
78 if (_no_data_frame && frame >= _no_data_frame.get()) {
79 return list<ContentVideo> ();
82 /* At this stage, if we have get_video()ed before, _decoded will contain the last frame that this
83 method returned (and possibly a few more). If the requested frame is not in _decoded and it is not the next
84 one after the end of _decoded we need to seek.
87 _log->log (String::compose ("VD has request for %1", frame), LogEntry::TYPE_DEBUG_DECODE);
89 if (_decoded.empty() || frame < _decoded.front().frame.index() || frame > (_decoded.back().frame.index() + 1)) {
90 Frame seek_frame = frame;
91 if (_content->video->frame_type() == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
92 /* 3D alternate is a special case as the frame index in the content is not the same
93 as the frame index we are talking about here.
97 _log->log (String::compose ("VD seeks to %1", seek_frame), LogEntry::TYPE_DEBUG_DECODE);
98 _parent->seek (ContentTime::from_frames (seek_frame, _content->active_video_frame_rate()), accurate);
101 /* Work out the number of frames that we should return; we
102 must return all frames in our content at the requested `time'
105 unsigned int frames_wanted = 0;
106 switch (_content->video->frame_type()) {
107 case VIDEO_FRAME_TYPE_2D:
108 case VIDEO_FRAME_TYPE_3D_LEFT:
109 case VIDEO_FRAME_TYPE_3D_RIGHT:
112 case VIDEO_FRAME_TYPE_3D:
113 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
114 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
115 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
119 DCPOMATIC_ASSERT (false);
122 list<ContentVideo> dec;
124 /* Now enough pass() calls should either:
125 * (a) give us what we want, or
126 * (b) give us something after what we want, indicating that we will never get what we want, or
127 * (c) hit the end of the decoder.
130 /* We are being accurate, so we want the right frame.
131 * This could all be one statement but it's split up for clarity.
133 bool no_data = false;
136 if (decoded(frame).size() == frames_wanted) {
137 /* We got what we want */
141 if (_parent->pass (Decoder::PASS_REASON_VIDEO, accurate)) {
142 /* The decoder has nothing more for us */
147 if (!_decoded.empty() && _decoded.front().frame.index() > frame) {
148 /* We're never going to get the frame we want. Perhaps the caller is asking
149 * for a video frame before the content's video starts (if its audio
150 * begins before its video, for example).
156 dec = decoded (frame);
158 if (no_data && dec.empty()) {
159 _no_data_frame = frame;
163 /* Any frame(s) will do: use the first one(s) that comes out of pass() */
164 while (_decoded.size() < frames_wanted && !_parent->pass (Decoder::PASS_REASON_VIDEO, accurate)) {}
165 list<ContentVideo>::const_iterator i = _decoded.begin();
167 while (i != _decoded.end() && j < frames_wanted) {
174 /* Clean up _decoded; keep the frame we are returning, if any (which may have two images
175 for 3D), but nothing before that
177 while (!_decoded.empty() && !dec.empty() && _decoded.front().frame.index() < dec.front().frame.index()) {
178 _decoded.pop_front ();
184 /** Fill _decoded from `from' up to, but not including, `to' with
185 * a frame for one particular Eyes value (which could be EYES_BOTH,
186 * EYES_LEFT or EYES_RIGHT)
189 VideoDecoder::fill_one_eye (Frame from, Frame to, Eyes eye)
196 /* Fill with black... */
197 shared_ptr<const ImageProxy> filler_image (new RawImageProxy (_black_image));
198 Part filler_part = PART_WHOLE;
200 /* ...unless there's some video we can fill with */
201 if (!_decoded.empty ()) {
202 filler_image = _decoded.back().image;
203 filler_part = _decoded.back().part;
206 for (Frame i = from; i < to; ++i) {
207 #ifdef DCPOMATIC_DEBUG
211 ContentVideo (filler_image, VideoFrame (i, eye), filler_part)
216 /** Fill _decoded from `from' up to, but not including, `to'
217 * adding both left and right eye frames.
220 VideoDecoder::fill_both_eyes (VideoFrame from, VideoFrame to)
222 /* Fill with black... */
223 shared_ptr<const ImageProxy> filler_left_image (new RawImageProxy (_black_image));
224 shared_ptr<const ImageProxy> filler_right_image (new RawImageProxy (_black_image));
225 Part filler_left_part = PART_WHOLE;
226 Part filler_right_part = PART_WHOLE;
228 /* ...unless there's some video we can fill with */
229 for (list<ContentVideo>::const_reverse_iterator i = _decoded.rbegin(); i != _decoded.rend(); ++i) {
230 if (i->frame.eyes() == EYES_LEFT && !filler_left_image) {
231 filler_left_image = i->image;
232 filler_left_part = i->part;
233 } else if (i->frame.eyes() == EYES_RIGHT && !filler_right_image) {
234 filler_right_image = i->image;
235 filler_right_part = i->part;
238 if (filler_left_image && filler_right_image) {
245 #ifdef DCPOMATIC_DEBUG
251 from.eyes() == EYES_LEFT ? filler_left_image : filler_right_image,
253 from.eyes() == EYES_LEFT ? filler_left_part : filler_right_part
261 /** Called by decoder classes when they have a video frame ready.
262 * @param frame Frame index within the content; this does not take into account 3D
263 * so for 3D_ALTERNATE this value goes:
271 VideoDecoder::give (shared_ptr<const ImageProxy> image, Frame frame)
277 _log->log (String::compose ("VD receives %1", frame), LogEntry::TYPE_DEBUG_DECODE);
279 /* Work out what we are going to push into _decoded next */
280 list<ContentVideo> to_push;
281 switch (_content->video->frame_type ()) {
282 case VIDEO_FRAME_TYPE_2D:
283 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_BOTH), PART_WHOLE));
285 case VIDEO_FRAME_TYPE_3D:
287 /* We receive the same frame index twice for 3D; hence we know which
290 bool const same = (!_decoded.empty() && frame == _decoded.back().frame.index());
291 to_push.push_back (ContentVideo (image, VideoFrame (frame, same ? EYES_RIGHT : EYES_LEFT), PART_WHOLE));
294 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
295 to_push.push_back (ContentVideo (image, VideoFrame (frame / 2, (frame % 2) ? EYES_RIGHT : EYES_LEFT), PART_WHOLE));
297 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
298 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_LEFT), PART_LEFT_HALF));
299 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_RIGHT), PART_RIGHT_HALF));
301 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
302 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_LEFT), PART_TOP_HALF));
303 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_RIGHT), PART_BOTTOM_HALF));
305 case VIDEO_FRAME_TYPE_3D_LEFT:
306 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_LEFT), PART_WHOLE));
308 case VIDEO_FRAME_TYPE_3D_RIGHT:
309 to_push.push_back (ContentVideo (image, VideoFrame (frame, EYES_RIGHT), PART_WHOLE));
312 DCPOMATIC_ASSERT (false);
315 /* Now VideoDecoder is required never to have gaps in the frames that it presents
316 via get_video(). Hence we need to fill in any gap between the last thing in _decoded
317 and the things we are about to push.
320 optional<VideoFrame> from;
322 if (_decoded.empty() && _last_seek_time && _last_seek_accurate) {
324 _last_seek_time->frames_round (_content->active_video_frame_rate ()),
325 _content->video->frame_type() == VIDEO_FRAME_TYPE_2D ? EYES_BOTH : EYES_LEFT
327 } else if (!_decoded.empty ()) {
328 /* Get the last frame we have */
329 from = _decoded.back().frame;
330 /* And move onto the first frame we need */
332 if (_content->video->frame_type() == VIDEO_FRAME_TYPE_3D_LEFT || _content->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
333 /* The previous ++ will increment a 3D-left-eye to the same index right-eye. If we are dealing with
334 a single-eye source we need an extra ++ to move back to the same eye.
340 /* If we've pre-rolled on a seek we may now receive out-of-order frames
341 (frames before the last seek time) which we can just ignore.
343 if (from && (*from) > to_push.front().frame) {
347 int const max_decoded_size = 96;
349 /* If _decoded is already `full' there is no point in adding anything more to it,
350 as the new stuff will just be removed again.
352 if (_decoded.size() < max_decoded_size) {
354 switch (_content->video->frame_type ()) {
355 case VIDEO_FRAME_TYPE_2D:
356 fill_one_eye (from->index(), to_push.front().frame.index(), EYES_BOTH);
358 case VIDEO_FRAME_TYPE_3D:
359 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
360 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
361 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
362 fill_both_eyes (from.get(), to_push.front().frame);
364 case VIDEO_FRAME_TYPE_3D_LEFT:
365 fill_one_eye (from->index(), to_push.front().frame.index(), EYES_LEFT);
367 case VIDEO_FRAME_TYPE_3D_RIGHT:
368 fill_one_eye (from->index(), to_push.front().frame.index(), EYES_RIGHT);
373 copy (to_push.begin(), to_push.end(), back_inserter (_decoded));
376 /* We can't let this build up too much or we will run out of memory. There is a
377 `best' value for the allowed size of _decoded which balances memory use
378 with decoding efficiency (lack of seeks). Throwing away video frames here
379 is not a problem for correctness, so do it.
381 while (_decoded.size() > max_decoded_size) {
382 _decoded.pop_back ();
387 VideoDecoder::seek (ContentTime s, bool accurate)
391 _last_seek_accurate = accurate;
394 /** Set this decoder never to produce any data */
396 VideoDecoder::set_ignore ()