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;
39 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c)
40 : DecoderPart (parent)
46 /** Called by decoder classes when they have a video frame ready.
47 * @param frame Frame index within the content; this does not take into account 3D
48 * so for 3D_ALTERNATE this value goes:
56 VideoDecoder::emit (shared_ptr<const Film> film, shared_ptr<const ImageProxy> image, Frame decoder_frame)
62 double const afr = _content->active_video_frame_rate(film);
63 VideoFrameType const vft = _content->video->frame_type();
67 /* This is the first data we have received since initialisation or seek. Set
68 the position based on the frame that was given. After this first time
69 we just cound frames, since (as with audio) it seems that ContentTimes
70 are unreliable from FFmpegDecoder. They are much better than audio times
71 but still we get the occasional one which is duplicated. In this case
72 ffmpeg seems to carry on regardless, processing the video frame as normal.
73 If we drop the frame with the duplicated timestamp we obviously lose sync.
75 _position = ContentTime::from_frames (decoder_frame, afr);
76 if (vft == VIDEO_FRAME_TYPE_3D) {
77 frame = decoder_frame;
78 _last_emitted_eyes = EYES_RIGHT;
79 } else if (vft == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
80 frame = decoder_frame / 2;
81 _last_emitted_eyes = EYES_RIGHT;
83 frame = decoder_frame;
86 if (vft == VIDEO_FRAME_TYPE_3D || vft == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
87 DCPOMATIC_ASSERT (_last_emitted_eyes);
88 if (_last_emitted_eyes.get() == EYES_RIGHT) {
89 frame = _position->frames_round(afr) + 1;
91 frame = _position->frames_round(afr);
94 frame = _position->frames_round(afr) + 1;
98 switch (_content->video->frame_type ()) {
99 case VIDEO_FRAME_TYPE_2D:
100 Data (ContentVideo (image, frame, EYES_BOTH, PART_WHOLE));
102 case VIDEO_FRAME_TYPE_3D:
104 /* We should receive the same frame index twice for 3D; hence we know which
107 bool const same = (_last_emitted_frame && _last_emitted_frame.get() == frame);
108 if (!same && _last_emitted_eyes && *_last_emitted_eyes == EYES_LEFT) {
109 /* We just got a new frame index but the last frame was left-eye; it looks like
110 this content is not really 3D.
112 boost::throw_exception (
115 _("The content file %1 is set as 3D but does not appear to contain 3D images. Please set it to 2D. "
116 "You can still make a 3D DCP from this content by ticking the 3D option in the DCP video tab."),
122 Eyes const eyes = same ? EYES_RIGHT : EYES_LEFT;
123 Data (ContentVideo (image, frame, eyes, PART_WHOLE));
124 _last_emitted_frame = frame;
125 _last_emitted_eyes = eyes;
128 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
130 DCPOMATIC_ASSERT (_last_emitted_eyes);
131 Eyes const eyes = _last_emitted_eyes.get() == EYES_LEFT ? EYES_RIGHT : EYES_LEFT;
132 Data (ContentVideo (image, frame, eyes, PART_WHOLE));
133 _last_emitted_eyes = eyes;
136 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
137 Data (ContentVideo (image, frame, EYES_LEFT, PART_LEFT_HALF));
138 Data (ContentVideo (image, frame, EYES_RIGHT, PART_RIGHT_HALF));
140 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
141 Data (ContentVideo (image, frame, EYES_LEFT, PART_TOP_HALF));
142 Data (ContentVideo (image, frame, EYES_RIGHT, PART_BOTTOM_HALF));
144 case VIDEO_FRAME_TYPE_3D_LEFT:
145 Data (ContentVideo (image, frame, EYES_LEFT, PART_WHOLE));
147 case VIDEO_FRAME_TYPE_3D_RIGHT:
148 Data (ContentVideo (image, frame, EYES_RIGHT, PART_WHOLE));
151 DCPOMATIC_ASSERT (false);
154 _position = ContentTime::from_frames (frame, afr);
158 VideoDecoder::seek ()
160 _position = boost::none;
161 _last_emitted_frame.reset ();
162 _last_emitted_eyes.reset ();