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 == 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 || frame > (_decoded.back().frame + 1)) {
90 _parent->seek (ContentTime::from_frames (frame, _content->active_video_frame_rate()), accurate);
93 list<ContentVideo> dec;
95 /* Now enough pass() calls should either:
96 * (a) give us what we want, or
97 * (b) give us something after what we want, indicating that we will never get what we want, or
98 * (c) hit the end of the decoder.
101 /* We are being accurate, so we want the right frame.
102 * This could all be one statement but it's split up for clarity.
104 bool no_data = false;
107 if (!decoded(frame).empty ()) {
108 /* We got what we want */
112 if (_parent->pass (Decoder::PASS_REASON_VIDEO, accurate)) {
113 /* The decoder has nothing more for us */
118 if (!_decoded.empty() && _decoded.front().frame > frame) {
119 /* We're never going to get the frame we want. Perhaps the caller is asking
120 * for a video frame before the content's video starts (if its audio
121 * begins before its video, for example).
127 dec = decoded (frame);
129 if (no_data && dec.empty()) {
130 _no_data_frame = frame;
134 /* Any frame will do: use the first one that comes out of pass() */
135 while (_decoded.empty() && !_parent->pass (Decoder::PASS_REASON_VIDEO, accurate)) {}
136 if (!_decoded.empty ()) {
137 dec.push_back (_decoded.front ());
141 /* Clean up _decoded; keep the frame we are returning, if any (which may have two images
142 for 3D), but nothing before that */
143 while (!_decoded.empty() && !dec.empty() && _decoded.front().frame < dec.front().frame) {
144 _decoded.pop_front ();
150 /** Fill _decoded from `from' up to, but not including, `to' with
151 * a frame for one particular Eyes value (which could be EYES_BOTH,
152 * EYES_LEFT or EYES_RIGHT)
155 VideoDecoder::fill_one_eye (Frame from, Frame to, Eyes eye)
162 /* Fill with black... */
163 shared_ptr<const ImageProxy> filler_image (new RawImageProxy (_black_image));
164 Part filler_part = PART_WHOLE;
166 /* ...unless there's some video we can fill with */
167 if (!_decoded.empty ()) {
168 filler_image = _decoded.back().image;
169 filler_part = _decoded.back().part;
172 for (Frame i = from; i < to; ++i) {
173 #ifdef DCPOMATIC_DEBUG
177 ContentVideo (filler_image, eye, filler_part, i)
182 /** Fill _decoded from `from' up to, but not including, `to'
183 * adding both left and right eye frames.
186 VideoDecoder::fill_both_eyes (Frame from, Frame to, Eyes eye)
188 if (to == 0 && eye == EYES_LEFT) {
193 /* Fill with black... */
194 shared_ptr<const ImageProxy> filler_left_image (new RawImageProxy (_black_image));
195 shared_ptr<const ImageProxy> filler_right_image (new RawImageProxy (_black_image));
196 Part filler_left_part = PART_WHOLE;
197 Part filler_right_part = PART_WHOLE;
199 /* ...unless there's some video we can fill with */
200 for (list<ContentVideo>::const_reverse_iterator i = _decoded.rbegin(); i != _decoded.rend(); ++i) {
201 if (i->eyes == EYES_LEFT && !filler_left_image) {
202 filler_left_image = i->image;
203 filler_left_part = i->part;
204 } else if (i->eyes == EYES_RIGHT && !filler_right_image) {
205 filler_right_image = i->image;
206 filler_right_part = i->part;
209 if (filler_left_image && filler_right_image) {
214 Frame filler_frame = from;
215 Eyes filler_eye = _decoded.empty() ? EYES_LEFT : _decoded.back().eyes;
217 if (_decoded.empty ()) {
219 filler_eye = EYES_LEFT;
220 } else if (_decoded.back().eyes == EYES_LEFT) {
221 filler_frame = _decoded.back().frame;
222 filler_eye = EYES_RIGHT;
223 } else if (_decoded.back().eyes == EYES_RIGHT) {
224 filler_frame = _decoded.back().frame + 1;
225 filler_eye = EYES_LEFT;
228 while (filler_frame != to || filler_eye != eye) {
230 #ifdef DCPOMATIC_DEBUG
236 filler_eye == EYES_LEFT ? filler_left_image : filler_right_image,
238 filler_eye == EYES_LEFT ? filler_left_part : filler_right_part,
243 if (filler_eye == EYES_LEFT) {
244 filler_eye = EYES_RIGHT;
246 filler_eye = EYES_LEFT;
252 /** Called by decoder classes when they have a video frame ready */
254 VideoDecoder::give (shared_ptr<const ImageProxy> image, Frame frame)
260 _log->log (String::compose ("VD receives %1", frame), LogEntry::TYPE_DEBUG_DECODE);
262 /* Work out what we are going to push into _decoded next */
263 list<ContentVideo> to_push;
264 switch (_content->video->frame_type ()) {
265 case VIDEO_FRAME_TYPE_2D:
266 to_push.push_back (ContentVideo (image, EYES_BOTH, PART_WHOLE, frame));
268 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
270 /* We receive the same frame index twice for 3D-alternate; hence we know which
273 bool const same = (!_decoded.empty() && frame == _decoded.back().frame);
274 to_push.push_back (ContentVideo (image, same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE, frame));
277 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
278 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_LEFT_HALF, frame));
279 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_RIGHT_HALF, frame));
281 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
282 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_TOP_HALF, frame));
283 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_BOTTOM_HALF, frame));
285 case VIDEO_FRAME_TYPE_3D_LEFT:
286 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_WHOLE, frame));
288 case VIDEO_FRAME_TYPE_3D_RIGHT:
289 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_WHOLE, frame));
292 DCPOMATIC_ASSERT (false);
295 /* Now VideoDecoder is required never to have gaps in the frames that it presents
296 via get_video(). Hence we need to fill in any gap between the last thing in _decoded
297 and the things we are about to push.
300 optional<Frame> from;
303 if (_decoded.empty() && _last_seek_time && _last_seek_accurate) {
304 from = _last_seek_time->frames_round (_content->active_video_frame_rate ());
305 to = to_push.front().frame;
306 } else if (!_decoded.empty ()) {
307 from = _decoded.back().frame + 1;
308 to = to_push.front().frame;
311 /* If we've pre-rolled on a seek we may now receive out-of-order frames
312 (frames before the last seek time) which we can just ignore.
315 if (from && to && from.get() > to.get()) {
320 switch (_content->video->frame_type ()) {
321 case VIDEO_FRAME_TYPE_2D:
322 fill_one_eye (from.get(), to.get (), EYES_BOTH);
324 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
325 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
326 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
327 fill_both_eyes (from.get(), to.get(), to_push.front().eyes);
329 case VIDEO_FRAME_TYPE_3D_LEFT:
330 fill_one_eye (from.get(), to.get (), EYES_LEFT);
332 case VIDEO_FRAME_TYPE_3D_RIGHT:
333 fill_one_eye (from.get(), to.get (), EYES_RIGHT);
338 copy (to_push.begin(), to_push.end(), back_inserter (_decoded));
340 /* We can't let this build up too much or we will run out of memory. There is a
341 `best' value for the allowed size of _decoded which balances memory use
342 with decoding efficiency (lack of seeks). Throwing away video frames here
343 is not a problem for correctness, so do it.
345 while (_decoded.size() > 96) {
346 _decoded.pop_back ();
351 VideoDecoder::seek (ContentTime s, bool accurate)
355 _last_seek_accurate = accurate;
358 /** Set this decoder never to produce any data */
360 VideoDecoder::set_ignore ()