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 "content_video.h"
28 using boost::shared_ptr;
29 using boost::optional;
31 VideoDecoder::VideoDecoder (shared_ptr<const VideoContent> c)
32 #ifdef DCPOMATIC_DEBUG
43 VideoDecoder::decoded_video (VideoFrame frame)
45 list<ContentVideo> output;
47 for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
48 if (i->frame == frame) {
49 output.push_back (*i);
56 /** Get all frames which exist in the content at a given frame index.
57 * @param frame Frame index.
58 * @param accurate true to try hard to return frames at the precise time that was requested, otherwise frames nearby may be returned.
59 * @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
62 VideoDecoder::get_video (VideoFrame frame, bool accurate)
64 if (_decoded_video.empty() || (frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1))) {
65 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
66 seek (ContentTime::from_frames (frame, _video_content->video_frame_rate()), accurate);
69 list<ContentVideo> dec;
71 /* Now enough pass() calls should either:
72 * (a) give us what we want, or
73 * (b) hit the end of the decoder.
76 /* We are being accurate, so we want the right frame.
77 * This could all be one statement but it's split up for clarity.
80 if (!decoded_video(frame).empty ()) {
81 /* We got what we want */
86 /* The decoder has nothing more for us */
90 if (!_decoded_video.empty() && _decoded_video.front().frame > frame) {
91 /* We're never going to get the frame we want. Perhaps the caller is asking
92 * for a video frame before the content's video starts (if its audio
93 * begins before its video, for example).
99 dec = decoded_video (frame);
101 /* Any frame will do: use the first one that comes out of pass() */
102 while (_decoded_video.empty() && !pass ()) {}
103 if (!_decoded_video.empty ()) {
104 dec.push_back (_decoded_video.front ());
108 /* Clean up decoded_video */
109 while (!_decoded_video.empty() && _decoded_video.front().frame < (frame - 1)) {
110 _decoded_video.pop_front ();
117 /** Called by subclasses when they have a video frame ready */
119 VideoDecoder::video (shared_ptr<const Image> image, VideoFrame frame)
121 /* We should not receive the same thing twice */
122 assert (_decoded_video.empty() || frame != _decoded_video.back().frame);
127 while (!_decoded_video.empty () && (_decoded_video.back().frame + 1) < frame) {
128 #ifdef DCPOMATIC_DEBUG
131 _decoded_video.push_back (
133 _decoded_video.back().image,
134 _decoded_video.back().eyes,
135 _decoded_video.back().frame + 1
140 switch (_video_content->video_frame_type ()) {
141 case VIDEO_FRAME_TYPE_2D:
142 _decoded_video.push_back (ContentVideo (image, EYES_BOTH, frame));
144 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
145 _decoded_video.push_back (ContentVideo (image, (frame % 2) ? EYES_RIGHT : EYES_LEFT, frame));
147 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
149 int const half = image->size().width / 2;
150 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, half, 0, 0), true), EYES_LEFT, frame));
151 _decoded_video.push_back (ContentVideo (image->crop (Crop (half, 0, 0, 0), true), EYES_RIGHT, frame));
154 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
156 int const half = image->size().height / 2;
157 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, 0, half), true), EYES_LEFT, frame));
158 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, half, 0), true), EYES_RIGHT, frame));
161 case VIDEO_FRAME_TYPE_3D_LEFT:
162 _decoded_video.push_back (ContentVideo (image, EYES_LEFT, frame));
164 case VIDEO_FRAME_TYPE_3D_RIGHT:
165 _decoded_video.push_back (ContentVideo (image, EYES_RIGHT, frame));
173 VideoDecoder::seek (ContentTime, bool)
175 _decoded_video.clear ();