Change how video timing is done.
[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 "frame_interval_checker.h"
24 #include "image.h"
25 #include "j2k_image_proxy.h"
26 #include "log.h"
27 #include "raw_image_proxy.h"
28 #include "video_decoder.h"
29 #include <iostream>
30
31 #include "i18n.h"
32
33
34 using std::cout;
35 using std::dynamic_pointer_cast;
36 using std::shared_ptr;
37 using namespace dcpomatic;
38
39
40 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c)
41         : DecoderPart (parent)
42         , _content (c)
43         , _frame_interval_checker (new FrameIntervalChecker())
44 {
45
46 }
47
48
49 /** Called by decoder classes when they have a video frame ready */
50 void
51 VideoDecoder::emit(shared_ptr<const Film> film, shared_ptr<const ImageProxy> image, ContentTime time)
52 {
53         if (ignore ()) {
54                 return;
55         }
56
57         auto const afr = _content->active_video_frame_rate(film);
58         auto const vft = _content->video->frame_type();
59
60         /* Do some heuristics to try and spot the case where the user sets content to 3D
61          * when it is not.  We try to tell this by looking at the differences in time between
62          * the first few frames.  Real 3D content should have two frames for each timestamp.
63          */
64         if (_frame_interval_checker) {
65                 _frame_interval_checker->feed(time, afr);
66                 if (_frame_interval_checker->guess() == FrameIntervalChecker::PROBABLY_NOT_3D && vft == VideoFrameType::THREE_D) {
67                         boost::throw_exception (
68                                 DecodeError(
69                                         String::compose(
70                                                 _("The content file %1 is set as 3D but does not appear to contain 3D images.  Please set it to 2D.  "
71                                                   "You can still make a 3D DCP from this content by ticking the 3D option in the DCP video tab."),
72                                                 _content->path(0)
73                                                 )
74                                         )
75                                 );
76                 }
77
78                 if (_frame_interval_checker->guess() != FrameIntervalChecker::AGAIN) {
79                         _frame_interval_checker.reset ();
80                 }
81         }
82
83         switch (vft) {
84         case VideoFrameType::TWO_D:
85                 Data(ContentVideo(image, time, Eyes::BOTH, Part::WHOLE));
86                 break;
87         case VideoFrameType::THREE_D:
88         {
89                 auto eyes = Eyes::LEFT;
90                 auto j2k = dynamic_pointer_cast<const J2KImageProxy>(image);
91                 if (j2k && j2k->eye()) {
92                         eyes = *j2k->eye() == dcp::Eye::LEFT ? Eyes::LEFT : Eyes::RIGHT;
93                 }
94
95                 Data(ContentVideo(image, time, eyes, Part::WHOLE));
96                 break;
97         }
98         case VideoFrameType::THREE_D_ALTERNATE:
99         {
100                 Eyes eyes;
101                 if (_last_emitted_eyes) {
102                         eyes = _last_emitted_eyes.get() == Eyes::LEFT ? Eyes::RIGHT : Eyes::LEFT;
103                 } else {
104                         /* We don't know what eye this frame is, so just guess */
105                         auto frame = time.frames_round(_content->video_frame_rate().get_value_or(24));
106                         eyes = (frame % 2) ? Eyes::RIGHT : Eyes::LEFT;
107                 }
108                 Data(ContentVideo(image, time, eyes, Part::WHOLE));
109                 _last_emitted_eyes = eyes;
110                 break;
111         }
112         case VideoFrameType::THREE_D_LEFT_RIGHT:
113                 Data(ContentVideo(image, time, Eyes::LEFT, Part::LEFT_HALF));
114                 Data(ContentVideo(image, time, Eyes::RIGHT, Part::RIGHT_HALF));
115                 break;
116         case VideoFrameType::THREE_D_TOP_BOTTOM:
117                 Data(ContentVideo(image, time, Eyes::LEFT, Part::TOP_HALF));
118                 Data(ContentVideo(image, time, Eyes::RIGHT, Part::BOTTOM_HALF));
119                 break;
120         case VideoFrameType::THREE_D_LEFT:
121                 Data(ContentVideo(image, time, Eyes::LEFT, Part::WHOLE));
122                 break;
123         case VideoFrameType::THREE_D_RIGHT:
124                 Data(ContentVideo(image, time, Eyes::RIGHT, Part::WHOLE));
125                 break;
126         default:
127                 DCPOMATIC_ASSERT (false);
128         }
129
130         _position = time;
131 }
132
133
134 void
135 VideoDecoder::seek ()
136 {
137         _position = boost::none;
138         _last_emitted_eyes.reset ();
139         _frame_interval_checker.reset (new FrameIntervalChecker());
140 }