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