2 Copyright (C) 2012-2014 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 "image_proxy.h"
23 #include "raw_image_proxy.h"
24 #include "content_video.h"
31 using std::back_inserter;
32 using boost::shared_ptr;
33 using boost::optional;
35 VideoDecoder::VideoDecoder (shared_ptr<const VideoContent> c)
36 #ifdef DCPOMATIC_DEBUG
44 _black_image.reset (new Image (PIX_FMT_RGB24, _video_content->video_size(), true));
45 _black_image->make_black ();
49 VideoDecoder::decoded_video (VideoFrame frame)
51 list<ContentVideo> output;
53 for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
54 if (i->frame == frame) {
55 output.push_back (*i);
62 /** Get all frames which exist in the content at a given frame index.
63 * @param frame Frame index.
64 * @param accurate true to try hard to return frames at the precise time that was requested, otherwise frames nearby may be returned.
65 * @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
68 VideoDecoder::get_video (VideoFrame frame, bool accurate)
70 /* At this stage, if we have get_video()ed before, _decoded_video will contain the last frame that this
71 method returned (and possibly a few more). If the requested frame is not in _decoded_video and it is not the next
72 one after the end of _decoded_video we need to seek.
75 if (_decoded_video.empty() || frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1)) {
76 seek (ContentTime::from_frames (frame, _video_content->video_frame_rate()), accurate);
79 list<ContentVideo> dec;
81 /* Now enough pass() calls should either:
82 * (a) give us what we want, or
83 * (b) give us something after what we want, indicating that we will never get what we want, or
84 * (c) hit the end of the decoder.
87 /* We are being accurate, so we want the right frame.
88 * This could all be one statement but it's split up for clarity.
91 if (!decoded_video(frame).empty ()) {
92 /* We got what we want */
97 /* The decoder has nothing more for us */
101 if (!_decoded_video.empty() && _decoded_video.front().frame > frame) {
102 /* We're never going to get the frame we want. Perhaps the caller is asking
103 * for a video frame before the content's video starts (if its audio
104 * begins before its video, for example).
110 dec = decoded_video (frame);
112 /* Any frame will do: use the first one that comes out of pass() */
113 while (_decoded_video.empty() && !pass ()) {}
114 if (!_decoded_video.empty ()) {
115 dec.push_back (_decoded_video.front ());
119 /* Clean up _decoded_video; keep the frame we are returning (which may have two images
120 for 3D), but nothing before that */
121 while (!_decoded_video.empty() && _decoded_video.front().frame < dec.front().frame) {
122 _decoded_video.pop_front ();
128 /** Fill _decoded_video up to, but not including, the specified frame */
130 VideoDecoder::fill_up_to_2d (VideoFrame frame)
137 /* Fill with black... */
138 boost::shared_ptr<const ImageProxy> filler_image (new RawImageProxy (_black_image));
139 Part filler_part = PART_WHOLE;
141 /* ...unless there's some video we can fill with */
142 if (!_decoded_video.empty ()) {
143 filler_image = _decoded_video.back().image;
144 filler_part = _decoded_video.back().part;
147 VideoFrame filler_frame = _decoded_video.empty() ? 0 : (_decoded_video.back().frame + 1);
148 while (filler_frame < frame) {
150 #ifdef DCPOMATIC_DEBUG
154 _decoded_video.push_back (
155 ContentVideo (filler_image, EYES_BOTH, filler_part, filler_frame)
162 /** Fill _decoded_video up to, but not including, the specified frame and eye */
164 VideoDecoder::fill_up_to_3d (VideoFrame frame, Eyes eye)
166 if (frame == 0 && eye == EYES_LEFT) {
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;
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;
187 if (filler_left_image && filler_right_image) {
192 VideoFrame filler_frame = _decoded_video.empty() ? 0 : _decoded_video.back().frame;
193 Eyes filler_eye = _decoded_video.empty() ? EYES_LEFT : _decoded_video.back().eyes;
195 if (_decoded_video.empty ()) {
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;
206 while (filler_frame != frame || filler_eye != eye) {
208 #ifdef DCPOMATIC_DEBUG
212 _decoded_video.push_back (
214 filler_eye == EYES_LEFT ? filler_left_image : filler_right_image,
216 filler_eye == EYES_LEFT ? filler_left_part : filler_right_part,
221 if (filler_eye == EYES_LEFT) {
222 filler_eye = EYES_RIGHT;
224 filler_eye = EYES_LEFT;
230 /** Called by subclasses when they have a video frame ready */
232 VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
234 /* We may receive the same frame index twice for 3D, and we need to know
237 _same = (!_decoded_video.empty() && frame == _decoded_video.back().frame);
239 /* Work out what we are going to push into _decoded_video next */
240 list<ContentVideo> to_push;
241 switch (_video_content->video_frame_type ()) {
242 case VIDEO_FRAME_TYPE_2D:
243 to_push.push_back (ContentVideo (image, EYES_BOTH, PART_WHOLE, frame));
245 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
246 to_push.push_back (ContentVideo (image, _same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE, frame));
248 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
249 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_LEFT_HALF, frame));
250 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_RIGHT_HALF, frame));
252 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
253 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_TOP_HALF, frame));
254 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_BOTTOM_HALF, frame));
256 case VIDEO_FRAME_TYPE_3D_LEFT:
257 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_WHOLE, frame));
259 case VIDEO_FRAME_TYPE_3D_RIGHT:
260 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_WHOLE, frame));
266 /* Now VideoDecoder is required never to have gaps in the frames that it presents
267 via get_video(). Hence we need to fill in any gap between the last thing in _decoded_video
268 and the things we are about to push.
271 if (_video_content->video_frame_type() == VIDEO_FRAME_TYPE_2D) {
272 fill_up_to_2d (to_push.front().frame);
274 fill_up_to_3d (to_push.front().frame, to_push.front().eyes);
277 copy (to_push.begin(), to_push.end(), back_inserter (_decoded_video));
281 VideoDecoder::seek (ContentTime, bool)
283 _decoded_video.clear ();