2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "video_decoder.h"
22 #include "raw_image_proxy.h"
25 #include "compose.hpp"
26 #include <boost/foreach.hpp>
34 using std::back_inserter;
35 using boost::shared_ptr;
36 using boost::optional;
38 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c, shared_ptr<Log> log)
39 #ifdef DCPOMATIC_DEBUG
48 , _last_seek_accurate (true)
51 _black_image.reset (new Image (AV_PIX_FMT_RGB24, _content->video->size(), true));
52 _black_image->make_black ();
56 VideoDecoder::decoded (Frame frame)
58 list<ContentVideo> output;
60 BOOST_FOREACH (ContentVideo const & i, _decoded) {
61 if (i.frame == frame) {
69 /** Get all frames which exist in the content at a given frame index.
70 * @param frame Frame index.
71 * @param accurate true to try hard to return frames at the precise time that was requested, otherwise frames nearby may be returned.
72 * @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
75 VideoDecoder::get (Frame frame, bool accurate)
77 if (_no_data_frame && frame >= _no_data_frame.get()) {
78 return list<ContentVideo> ();
81 /* At this stage, if we have get_video()ed before, _decoded will contain the last frame that this
82 method returned (and possibly a few more). If the requested frame is not in _decoded and it is not the next
83 one after the end of _decoded we need to seek.
86 _log->log (String::compose ("VD has request for %1", frame), LogEntry::TYPE_DEBUG_DECODE);
88 if (_decoded.empty() || frame < _decoded.front().frame || frame > (_decoded.back().frame + 1)) {
89 _parent->seek (ContentTime::from_frames (frame, _content->active_video_frame_rate()), accurate);
92 list<ContentVideo> dec;
94 /* Now enough pass() calls should either:
95 * (a) give us what we want, or
96 * (b) give us something after what we want, indicating that we will never get what we want, or
97 * (c) hit the end of the decoder.
100 /* We are being accurate, so we want the right frame.
101 * This could all be one statement but it's split up for clarity.
103 bool no_data = false;
106 if (!decoded(frame).empty ()) {
107 /* We got what we want */
111 if (_parent->pass (Decoder::PASS_REASON_VIDEO, accurate)) {
112 /* The decoder has nothing more for us */
117 if (!_decoded.empty() && _decoded.front().frame > frame) {
118 /* We're never going to get the frame we want. Perhaps the caller is asking
119 * for a video frame before the content's video starts (if its audio
120 * begins before its video, for example).
126 dec = decoded (frame);
128 if (no_data && dec.empty()) {
129 _no_data_frame = frame;
133 /* Any frame will do: use the first one that comes out of pass() */
134 while (_decoded.empty() && !_parent->pass (Decoder::PASS_REASON_VIDEO, accurate)) {}
135 if (!_decoded.empty ()) {
136 dec.push_back (_decoded.front ());
140 /* Clean up _decoded; keep the frame we are returning, if any (which may have two images
141 for 3D), but nothing before that */
142 while (!_decoded.empty() && !dec.empty() && _decoded.front().frame < dec.front().frame) {
143 _decoded.pop_front ();
149 /** Fill _decoded from `from' up to, but not including, `to' with
150 * a frame for one particular Eyes value (which could be EYES_BOTH,
151 * EYES_LEFT or EYES_RIGHT)
154 VideoDecoder::fill_one_eye (Frame from, Frame to, Eyes eye)
161 /* Fill with black... */
162 shared_ptr<const ImageProxy> filler_image (new RawImageProxy (_black_image));
163 Part filler_part = PART_WHOLE;
165 /* ...unless there's some video we can fill with */
166 if (!_decoded.empty ()) {
167 filler_image = _decoded.back().image;
168 filler_part = _decoded.back().part;
171 for (Frame i = from; i < to; ++i) {
172 #ifdef DCPOMATIC_DEBUG
176 ContentVideo (filler_image, eye, filler_part, i)
181 /** Fill _decoded from `from' up to, but not including, `to'
182 * adding both left and right eye frames.
185 VideoDecoder::fill_both_eyes (Frame from, Frame to, Eyes eye)
187 if (to == 0 && eye == EYES_LEFT) {
192 /* Fill with black... */
193 shared_ptr<const ImageProxy> filler_left_image (new RawImageProxy (_black_image));
194 shared_ptr<const ImageProxy> filler_right_image (new RawImageProxy (_black_image));
195 Part filler_left_part = PART_WHOLE;
196 Part filler_right_part = PART_WHOLE;
198 /* ...unless there's some video we can fill with */
199 for (list<ContentVideo>::const_reverse_iterator i = _decoded.rbegin(); i != _decoded.rend(); ++i) {
200 if (i->eyes == EYES_LEFT && !filler_left_image) {
201 filler_left_image = i->image;
202 filler_left_part = i->part;
203 } else if (i->eyes == EYES_RIGHT && !filler_right_image) {
204 filler_right_image = i->image;
205 filler_right_part = i->part;
208 if (filler_left_image && filler_right_image) {
213 Frame filler_frame = from;
214 Eyes filler_eye = _decoded.empty() ? EYES_LEFT : _decoded.back().eyes;
216 if (_decoded.empty ()) {
218 filler_eye = EYES_LEFT;
219 } else if (_decoded.back().eyes == EYES_LEFT) {
220 filler_frame = _decoded.back().frame;
221 filler_eye = EYES_RIGHT;
222 } else if (_decoded.back().eyes == EYES_RIGHT) {
223 filler_frame = _decoded.back().frame + 1;
224 filler_eye = EYES_LEFT;
227 while (filler_frame != to || filler_eye != eye) {
229 #ifdef DCPOMATIC_DEBUG
235 filler_eye == EYES_LEFT ? filler_left_image : filler_right_image,
237 filler_eye == EYES_LEFT ? filler_left_part : filler_right_part,
242 if (filler_eye == EYES_LEFT) {
243 filler_eye = EYES_RIGHT;
245 filler_eye = EYES_LEFT;
251 /** Called by decoder classes when they have a video frame ready */
253 VideoDecoder::give (shared_ptr<const ImageProxy> image, Frame frame)
259 _log->log (String::compose ("VD receives %1", frame), LogEntry::TYPE_DEBUG_DECODE);
261 /* Work out what we are going to push into _decoded next */
262 list<ContentVideo> to_push;
263 switch (_content->video->frame_type ()) {
264 case VIDEO_FRAME_TYPE_2D:
265 to_push.push_back (ContentVideo (image, EYES_BOTH, PART_WHOLE, frame));
267 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
269 /* We receive the same frame index twice for 3D-alternate; hence we know which
272 bool const same = (!_decoded.empty() && frame == _decoded.back().frame);
273 to_push.push_back (ContentVideo (image, same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE, frame));
276 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
277 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_LEFT_HALF, frame));
278 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_RIGHT_HALF, frame));
280 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
281 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_TOP_HALF, frame));
282 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_BOTTOM_HALF, frame));
284 case VIDEO_FRAME_TYPE_3D_LEFT:
285 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_WHOLE, frame));
287 case VIDEO_FRAME_TYPE_3D_RIGHT:
288 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_WHOLE, frame));
291 DCPOMATIC_ASSERT (false);
294 /* Now VideoDecoder is required never to have gaps in the frames that it presents
295 via get_video(). Hence we need to fill in any gap between the last thing in _decoded
296 and the things we are about to push.
299 optional<Frame> from;
302 if (_decoded.empty() && _last_seek_time && _last_seek_accurate) {
303 from = _last_seek_time->frames_round (_content->active_video_frame_rate ());
304 to = to_push.front().frame;
305 } else if (!_decoded.empty ()) {
306 from = _decoded.back().frame + 1;
307 to = to_push.front().frame;
310 /* If we've pre-rolled on a seek we may now receive out-of-order frames
311 (frames before the last seek time) which we can just ignore.
314 if (from && to && from.get() > to.get()) {
319 switch (_content->video->frame_type ()) {
320 case VIDEO_FRAME_TYPE_2D:
321 fill_one_eye (from.get(), to.get (), EYES_BOTH);
323 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
324 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
325 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
326 fill_both_eyes (from.get(), to.get(), to_push.front().eyes);
328 case VIDEO_FRAME_TYPE_3D_LEFT:
329 fill_one_eye (from.get(), to.get (), EYES_LEFT);
331 case VIDEO_FRAME_TYPE_3D_RIGHT:
332 fill_one_eye (from.get(), to.get (), EYES_RIGHT);
337 copy (to_push.begin(), to_push.end(), back_inserter (_decoded));
339 /* We can't let this build up too much or we will run out of memory. There is a
340 `best' value for the allowed size of _decoded which balances memory use
341 with decoding efficiency (lack of seeks). Throwing away video frames here
342 is not a problem for correctness, so do it.
344 while (_decoded.size() > 96) {
345 _decoded.pop_back ();
350 VideoDecoder::seek (ContentTime s, bool accurate)
354 _last_seek_accurate = accurate;
357 /** Set this decoder never to produce any data */
359 VideoDecoder::set_ignore ()