Missed update to private test repo version.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "compose.hpp"
23 #include "film.h"
24 #include "frame_interval_checker.h"
25 #include "image.h"
26 #include "j2k_image_proxy.h"
27 #include "log.h"
28 #include "raw_image_proxy.h"
29 #include "video_decoder.h"
30 #include <iostream>
31
32 #include "i18n.h"
33
34
35 using std::cout;
36 using std::dynamic_pointer_cast;
37 using std::shared_ptr;
38 using namespace dcpomatic;
39
40
41 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c)
42         : DecoderPart (parent)
43         , _content (c)
44         , _frame_interval_checker (new FrameIntervalChecker())
45 {
46
47 }
48
49
50 /** Called by decoder classes when they have a video frame ready.
51  *  @param frame Frame index within the content; this does not take into account 3D
52  *  so for 3D_ALTERNATE this value goes:
53  *     0: frame 0 left
54  *     1: frame 0 right
55  *     2: frame 1 left
56  *     3: frame 1 right
57  *  and so on.
58  */
59 void
60 VideoDecoder::emit (shared_ptr<const Film> film, shared_ptr<const ImageProxy> image, Frame decoder_frame)
61 {
62         if (ignore ()) {
63                 return;
64         }
65
66         auto const afr = _content->active_video_frame_rate(film);
67         auto const vft = _content->video->frame_type();
68
69         auto frame_time = ContentTime::from_frames (decoder_frame, afr);
70
71         /* Do some heuristics to try and spot the case where the user sets content to 3D
72          * when it is not.  We try to tell this by looking at the differences in time between
73          * the first few frames.  Real 3D content should have two frames for each timestamp.
74          */
75         if (_frame_interval_checker) {
76                 _frame_interval_checker->feed (frame_time, afr);
77                 if (_frame_interval_checker->guess() == FrameIntervalChecker::PROBABLY_NOT_3D && vft == VideoFrameType::THREE_D) {
78                         boost::throw_exception (
79                                 DecodeError(
80                                         String::compose(
81                                                 _("The content file %1 is set as 3D but does not appear to contain 3D images.  Please set it to 2D.  "
82                                                   "You can still make a 3D DCP from this content by ticking the 3D option in the DCP video tab."),
83                                                 _content->path(0)
84                                                 )
85                                         )
86                                 );
87                 }
88
89                 if (_frame_interval_checker->guess() != FrameIntervalChecker::AGAIN) {
90                         _frame_interval_checker.reset ();
91                 }
92         }
93
94         Frame frame;
95         Eyes eyes = Eyes::BOTH;
96         if (!_position) {
97                 /* This is the first data we have received since initialisation or seek.  Set
98                    the position based on the frame that was given.  After this first time
99                    we just count frames, since (as with audio) it seems that ContentTimes
100                    are unreliable from FFmpegDecoder.  They are much better than audio times
101                    but still we get the occasional one which is duplicated.  In this case
102                    ffmpeg seems to carry on regardless, processing the video frame as normal.
103                    If we drop the frame with the duplicated timestamp we obviously lose sync.
104                 */
105
106                 if (vft == VideoFrameType::THREE_D_ALTERNATE) {
107                         frame = decoder_frame / 2;
108                         eyes = (decoder_frame % 2) ? Eyes::RIGHT : Eyes::LEFT;
109                 } else {
110                         frame = decoder_frame;
111                         if (vft == VideoFrameType::THREE_D) {
112                                 auto j2k = dynamic_pointer_cast<const J2KImageProxy>(image);
113                                 /* At the moment only DCP decoders producers VideoFrameType::THREE_D, so only the J2KImageProxy
114                                  * knows which eye it is.
115                                  */
116                                 if (j2k && j2k->eye()) {
117                                         eyes = j2k->eye().get() == dcp::Eye::LEFT ? Eyes::LEFT : Eyes::RIGHT;
118                                 }
119                         }
120                 }
121
122                 _position = ContentTime::from_frames (frame, afr);
123         } else {
124                 if (vft == VideoFrameType::THREE_D) {
125                         auto j2k = dynamic_pointer_cast<const J2KImageProxy>(image);
126                         if (j2k && j2k->eye()) {
127                                 if (j2k->eye() == dcp::Eye::LEFT) {
128                                         frame = _position->frames_round(afr) + 1;
129                                         eyes = Eyes::LEFT;
130                                 } else {
131                                         frame = _position->frames_round(afr);
132                                         eyes = Eyes::RIGHT;
133                                 }
134                         } else {
135                                 /* This should not happen; see above */
136                                 frame = _position->frames_round(afr) + 1;
137                         }
138                 } else if (vft == VideoFrameType::THREE_D_ALTERNATE) {
139                         DCPOMATIC_ASSERT (_last_emitted_eyes);
140                         if (_last_emitted_eyes.get() == Eyes::RIGHT) {
141                                 frame = _position->frames_round(afr) + 1;
142                                 eyes = Eyes::LEFT;
143                         } else {
144                                 frame = _position->frames_round(afr);
145                                 eyes = Eyes::RIGHT;
146                         }
147                 } else {
148                         frame = _position->frames_round(afr) + 1;
149                 }
150         }
151
152         switch (vft) {
153         case VideoFrameType::TWO_D:
154         case VideoFrameType::THREE_D:
155                 Data (ContentVideo (image, frame, eyes, Part::WHOLE));
156                 break;
157         case VideoFrameType::THREE_D_ALTERNATE:
158         {
159                 Data (ContentVideo (image, frame, eyes, Part::WHOLE));
160                 _last_emitted_eyes = eyes;
161                 break;
162         }
163         case VideoFrameType::THREE_D_LEFT_RIGHT:
164                 Data (ContentVideo (image, frame, Eyes::LEFT, Part::LEFT_HALF));
165                 Data (ContentVideo (image, frame, Eyes::RIGHT, Part::RIGHT_HALF));
166                 break;
167         case VideoFrameType::THREE_D_TOP_BOTTOM:
168                 Data (ContentVideo (image, frame, Eyes::LEFT, Part::TOP_HALF));
169                 Data (ContentVideo (image, frame, Eyes::RIGHT, Part::BOTTOM_HALF));
170                 break;
171         case VideoFrameType::THREE_D_LEFT:
172                 Data (ContentVideo (image, frame, Eyes::LEFT, Part::WHOLE));
173                 break;
174         case VideoFrameType::THREE_D_RIGHT:
175                 Data (ContentVideo (image, frame, Eyes::RIGHT, Part::WHOLE));
176                 break;
177         default:
178                 DCPOMATIC_ASSERT (false);
179         }
180
181         _position = ContentTime::from_frames (frame, afr);
182 }
183
184
185 void
186 VideoDecoder::seek ()
187 {
188         _position = boost::none;
189         _last_emitted_eyes.reset ();
190         _frame_interval_checker.reset (new FrameIntervalChecker());
191 }