2 Copyright (C) 2012-2018 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;
38 using namespace dcpomatic;
40 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c)
41 : DecoderPart (parent)
47 /** Called by decoder classes when they have a video frame ready.
48 * @param frame Frame index within the content; this does not take into account 3D
49 * so for 3D_ALTERNATE this value goes:
57 VideoDecoder::emit (shared_ptr<const Film> film, shared_ptr<const ImageProxy> image, Frame decoder_frame)
63 /* Before we `re-write' the frame indexes of these incoming data we need to check for
64 the case where the user has some 2D content which they have marked as 3D. With 3D
65 we should get two frames for each frame index, but in this `bad' case we only get
66 one. We need to throw an exception if this happens.
69 if (_content->video->frame_type() == VIDEO_FRAME_TYPE_3D) {
70 if (_last_threed_frames.size() > 4) {
71 _last_threed_frames.erase (_last_threed_frames.begin());
73 _last_threed_frames.push_back (decoder_frame);
74 if (_last_threed_frames.size() == 4) {
75 if (_last_threed_frames[0] != _last_threed_frames[1] || _last_threed_frames[2] != _last_threed_frames[3]) {
76 boost::throw_exception (
79 _("The content file %1 is set as 3D but does not appear to contain 3D images. Please set it to 2D. "
80 "You can still make a 3D DCP from this content by ticking the 3D option in the DCP video tab."),
89 double const afr = _content->active_video_frame_rate(film);
92 Eyes eyes = EYES_BOTH;
94 /* This is the first data we have received since initialisation or seek. Set
95 the position based on the frame that was given. After this first time
96 we just cound frames, since (as with audio) it seems that ContentTimes
97 are unreliable from FFmpegDecoder. They are much better than audio times
98 but still we get the occasional one which is duplicated. In this case
99 ffmpeg seems to carry on regardless, processing the video frame as normal.
100 If we drop the frame with the duplicated timestamp we obviously lose sync.
102 _position = ContentTime::from_frames (decoder_frame, afr);
103 if (_content->video->frame_type() == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
104 frame = decoder_frame / 2;
105 _last_emitted_eyes = EYES_RIGHT;
107 frame = decoder_frame;
110 VideoFrameType const ft = _content->video->frame_type ();
111 if (ft == VIDEO_FRAME_TYPE_3D_ALTERNATE || ft == VIDEO_FRAME_TYPE_3D) {
112 DCPOMATIC_ASSERT (_last_emitted_eyes);
113 if (_last_emitted_eyes.get() == EYES_RIGHT) {
114 frame = _position->frames_round(afr) + 1;
117 frame = _position->frames_round(afr);
121 frame = _position->frames_round(afr) + 1;
125 switch (_content->video->frame_type ()) {
126 case VIDEO_FRAME_TYPE_2D:
127 Data (ContentVideo (image, frame, EYES_BOTH, PART_WHOLE));
129 case VIDEO_FRAME_TYPE_3D:
131 Data (ContentVideo (image, frame, eyes, PART_WHOLE));
132 _last_emitted_frame = frame;
133 _last_emitted_eyes = eyes;
136 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
138 Data (ContentVideo (image, frame, eyes, PART_WHOLE));
139 _last_emitted_eyes = eyes;
142 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
143 Data (ContentVideo (image, frame, EYES_LEFT, PART_LEFT_HALF));
144 Data (ContentVideo (image, frame, EYES_RIGHT, PART_RIGHT_HALF));
146 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
147 Data (ContentVideo (image, frame, EYES_LEFT, PART_TOP_HALF));
148 Data (ContentVideo (image, frame, EYES_RIGHT, PART_BOTTOM_HALF));
150 case VIDEO_FRAME_TYPE_3D_LEFT:
151 Data (ContentVideo (image, frame, EYES_LEFT, PART_WHOLE));
153 case VIDEO_FRAME_TYPE_3D_RIGHT:
154 Data (ContentVideo (image, frame, EYES_RIGHT, PART_WHOLE));
157 DCPOMATIC_ASSERT (false);
160 _position = ContentTime::from_frames (frame, afr);
164 VideoDecoder::seek ()
166 _position = boost::optional<ContentTime>();
167 _last_emitted_frame.reset ();
168 _last_emitted_eyes.reset ();