BOOST_FOREACH.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /*
2     Copyright (C) 2012-2020 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 #include "video_decoder.h"
22 #include "image.h"
23 #include "raw_image_proxy.h"
24 #include "film.h"
25 #include "log.h"
26 #include "frame_interval_checker.h"
27 #include "compose.hpp"
28 #include <iostream>
29
30 #include "i18n.h"
31
32 using std::cout;
33 using std::list;
34 using std::max;
35 using std::back_inserter;
36 using std::shared_ptr;
37 using boost::optional;
38 using namespace dcpomatic;
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 /** Called by decoder classes when they have a video frame ready.
49  *  @param frame Frame index within the content; this does not take into account 3D
50  *  so for 3D_ALTERNATE this value goes:
51  *     0: frame 0 left
52  *     1: frame 0 right
53  *     2: frame 1 left
54  *     3: frame 1 right
55  *  and so on.
56  */
57 void
58 VideoDecoder::emit (shared_ptr<const Film> film, shared_ptr<const ImageProxy> image, Frame decoder_frame)
59 {
60         if (ignore ()) {
61                 return;
62         }
63
64         double const afr = _content->active_video_frame_rate(film);
65         VideoFrameType const vft = _content->video->frame_type();
66
67         ContentTime frame_time = ContentTime::from_frames (decoder_frame, afr);
68
69         /* Do some heuristics to try and spot the case where the user sets content to 3D
70          * when it is not.  We try to tell this by looking at the differences in time between
71          * the first few frames.  Real 3D content should have two frames for each timestamp.
72          */
73         if (_frame_interval_checker) {
74                 _frame_interval_checker->feed (frame_time, afr);
75                 if (_frame_interval_checker->guess() == FrameIntervalChecker::PROBABLY_NOT_3D && vft == VIDEO_FRAME_TYPE_3D) {
76                         boost::throw_exception (
77                                 DecodeError(
78                                         String::compose(
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."),
81                                                 _content->path(0)
82                                                 )
83                                         )
84                                 );
85                 }
86
87                 if (_frame_interval_checker->guess() != FrameIntervalChecker::AGAIN) {
88                         _frame_interval_checker.reset ();
89                 }
90         }
91
92         Frame frame;
93         Eyes eyes = EYES_BOTH;
94         if (!_position) {
95                 /* This is the first data we have received since initialisation or seek.  Set
96                    the position based on the frame that was given.  After this first time
97                    we just cound frames, since (as with audio) it seems that ContentTimes
98                    are unreliable from FFmpegDecoder.  They are much better than audio times
99                    but still we get the occasional one which is duplicated.  In this case
100                    ffmpeg seems to carry on regardless, processing the video frame as normal.
101                    If we drop the frame with the duplicated timestamp we obviously lose sync.
102                 */
103                 _position = ContentTime::from_frames (decoder_frame, afr);
104                 if (vft == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
105                         frame = decoder_frame / 2;
106                         _last_emitted_eyes = EYES_RIGHT;
107                 } else {
108                         frame = decoder_frame;
109                 }
110         } else {
111                 if (vft == VIDEO_FRAME_TYPE_3D || vft == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
112                         DCPOMATIC_ASSERT (_last_emitted_eyes);
113                         if (_last_emitted_eyes.get() == EYES_RIGHT) {
114                                 frame = _position->frames_round(afr) + 1;
115                                 eyes = EYES_LEFT;
116                         } else {
117                                 frame = _position->frames_round(afr);
118                                 eyes = EYES_RIGHT;
119                         }
120                 } else {
121                         frame = _position->frames_round(afr) + 1;
122                 }
123         }
124
125         switch (vft) {
126         case VIDEO_FRAME_TYPE_2D:
127                 Data (ContentVideo (image, frame, EYES_BOTH, PART_WHOLE));
128                 break;
129         case VIDEO_FRAME_TYPE_3D:
130         {
131                 Data (ContentVideo (image, frame, eyes, PART_WHOLE));
132                 _last_emitted_frame = frame;
133                 _last_emitted_eyes = eyes;
134                 break;
135         }
136         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
137         {
138                 Data (ContentVideo (image, frame, eyes, PART_WHOLE));
139                 _last_emitted_eyes = eyes;
140                 break;
141         }
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));
145                 break;
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));
149                 break;
150         case VIDEO_FRAME_TYPE_3D_LEFT:
151                 Data (ContentVideo (image, frame, EYES_LEFT, PART_WHOLE));
152                 break;
153         case VIDEO_FRAME_TYPE_3D_RIGHT:
154                 Data (ContentVideo (image, frame, EYES_RIGHT, PART_WHOLE));
155                 break;
156         default:
157                 DCPOMATIC_ASSERT (false);
158         }
159
160         _position = ContentTime::from_frames (frame, afr);
161 }
162
163 void
164 VideoDecoder::seek ()
165 {
166         _position = boost::none;
167         _last_emitted_frame.reset ();
168         _last_emitted_eyes.reset ();
169         _frame_interval_checker.reset (new FrameIntervalChecker());
170 }