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)
37 optional<ContentVideo>
38 VideoDecoder::decoded_video (VideoFrame frame)
40 for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
41 if (i->frame == frame) {
46 return optional<ContentVideo> ();
49 optional<ContentVideo>
50 VideoDecoder::get_video (VideoFrame frame, bool accurate)
52 if (_decoded_video.empty() || (frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1))) {
53 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
54 seek (ContentTime::from_frames (frame, _video_content->video_frame_rate()), accurate);
57 optional<ContentVideo> dec;
59 /* Now enough pass() calls will either:
60 * (a) give us what we want, or
61 * (b) hit the end of the decoder.
64 /* We are being accurate, so we want the right frame */
65 while (!decoded_video (frame) && !pass ()) {}
66 dec = decoded_video (frame);
68 /* Any frame will do: use the first one that comes out of pass() */
69 while (_decoded_video.empty() && !pass ()) {}
70 if (!_decoded_video.empty ()) {
71 dec = _decoded_video.front ();
75 /* Clean up decoded_video */
76 while (!_decoded_video.empty() && _decoded_video.front().frame < (frame - 1)) {
77 _decoded_video.pop_front ();
84 /** Called by subclasses when they have a video frame ready */
86 VideoDecoder::video (shared_ptr<const Image> image, VideoFrame frame)
90 while (!_decoded_video.empty () && (_decoded_video.back().frame + 1) < frame) {
91 _decoded_video.push_back (
93 _decoded_video.back().image,
94 _decoded_video.back().eyes,
95 _decoded_video.back().frame + 1
100 switch (_video_content->video_frame_type ()) {
101 case VIDEO_FRAME_TYPE_2D:
102 _decoded_video.push_back (ContentVideo (image, EYES_BOTH, frame));
104 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
105 _decoded_video.push_back (ContentVideo (image, (frame % 2) ? EYES_RIGHT : EYES_LEFT, frame));
107 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
109 int const half = image->size().width / 2;
110 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, half, 0, 0), true), EYES_LEFT, frame));
111 _decoded_video.push_back (ContentVideo (image->crop (Crop (half, 0, 0, 0), true), EYES_RIGHT, frame));
114 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
116 int const half = image->size().height / 2;
117 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, 0, half), true), EYES_LEFT, frame));
118 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, half, 0), true), EYES_RIGHT, frame));
121 case VIDEO_FRAME_TYPE_3D_LEFT:
122 _decoded_video.push_back (ContentVideo (image, EYES_LEFT, frame));
124 case VIDEO_FRAME_TYPE_3D_RIGHT:
125 _decoded_video.push_back (ContentVideo (image, EYES_RIGHT, frame));
133 VideoDecoder::seek (ContentTime, bool)
135 _decoded_video.clear ();