Missed part of new video timing patch.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /*
2     Copyright (C) 2012-2018 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 "compose.hpp"
27 #include <boost/foreach.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 boost::shared_ptr;
37 using boost::optional;
38
39 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c)
40         : DecoderPart (parent)
41         , _content (c)
42 {
43
44 }
45
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:
49  *     0: frame 0 left
50  *     1: frame 0 right
51  *     2: frame 1 left
52  *     3: frame 1 right
53  *  and so on.
54  */
55 void
56 VideoDecoder::emit (shared_ptr<const Film> film, shared_ptr<const ImageProxy> image, Frame decoder_frame)
57 {
58         if (ignore ()) {
59                 return;
60         }
61
62         double const afr = _content->active_video_frame_rate(film);
63
64         Frame frame;
65         if (!_position) {
66                 /* This is the first data we have received since initialisation or seek.  Set
67                    the position based on the frame that was given.  After this first time
68                    we just cound frames, since (as with audio) it seems that ContentTimes
69                    are unreliable from FFmpegDecoder.  They are much better than audio times
70                    but still we get the occasional one which is duplicated.  In this case
71                    ffmpeg seems to carry on regardless, processing the video frame as normal.
72                    If we drop the frame with the duplicated timestamp we obviously lose sync.
73                 */
74                 _position = ContentTime::from_frames (decoder_frame, afr);
75                 if (_content->video->frame_type() == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
76                         frame = decoder_frame / 2;
77                         _last_emitted_eyes = EYES_RIGHT;
78                 } else {
79                         frame = decoder_frame;
80                 }
81         } else {
82                 if (_content->video->frame_type() == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
83                         DCPOMATIC_ASSERT (_last_emitted_eyes);
84                         if (_last_emitted_eyes.get() == EYES_RIGHT) {
85                                 frame = _position->frames_round(afr) + 1;
86                         } else {
87                                 frame = _position->frames_round(afr);
88                         }
89                 } else {
90                         frame = _position->frames_round(afr) + 1;
91                 }
92         }
93
94         switch (_content->video->frame_type ()) {
95         case VIDEO_FRAME_TYPE_2D:
96                 Data (ContentVideo (image, frame, EYES_BOTH, PART_WHOLE));
97                 break;
98         case VIDEO_FRAME_TYPE_3D:
99         {
100                 /* We should receive the same frame index twice for 3D; hence we know which
101                    frame this one is.
102                 */
103                 bool const same = (_last_emitted_frame && _last_emitted_frame.get() == frame);
104                 if (!same && _last_emitted_eyes && *_last_emitted_eyes == EYES_LEFT) {
105                         /* We just got a new frame index but the last frame was left-eye; it looks like
106                            this content is not really 3D.
107                         */
108                         boost::throw_exception (
109                                 DecodeError(
110                                         String::compose(
111                                                 _("The content file %1 is set as 3D but does not appear to contain 3D images.  Please set it to 2D.  "
112                                                   "You can still make a 3D DCP from this content by ticking the 3D option in the DCP video tab."),
113                                                 _content->path(0)
114                                                 )
115                                         )
116                                 );
117                 }
118                 Eyes const eyes = same ? EYES_RIGHT : EYES_LEFT;
119                 Data (ContentVideo (image, frame, eyes, PART_WHOLE));
120                 _last_emitted_frame = frame;
121                 _last_emitted_eyes = eyes;
122                 break;
123         }
124         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
125         {
126                 DCPOMATIC_ASSERT (_last_emitted_eyes);
127                 Eyes const eyes = _last_emitted_eyes.get() == EYES_LEFT ? EYES_RIGHT : EYES_LEFT;
128                 Data (ContentVideo (image, frame, eyes, PART_WHOLE));
129                 _last_emitted_eyes = eyes;
130                 break;
131         }
132         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
133                 Data (ContentVideo (image, frame, EYES_LEFT, PART_LEFT_HALF));
134                 Data (ContentVideo (image, frame, EYES_RIGHT, PART_RIGHT_HALF));
135                 break;
136         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
137                 Data (ContentVideo (image, frame, EYES_LEFT, PART_TOP_HALF));
138                 Data (ContentVideo (image, frame, EYES_RIGHT, PART_BOTTOM_HALF));
139                 break;
140         case VIDEO_FRAME_TYPE_3D_LEFT:
141                 Data (ContentVideo (image, frame, EYES_LEFT, PART_WHOLE));
142                 break;
143         case VIDEO_FRAME_TYPE_3D_RIGHT:
144                 Data (ContentVideo (image, frame, EYES_RIGHT, PART_WHOLE));
145                 break;
146         default:
147                 DCPOMATIC_ASSERT (false);
148         }
149
150         _position = ContentTime::from_frames (frame, afr);
151 }
152
153 void
154 VideoDecoder::seek ()
155 {
156         _position = boost::none;
157         _last_emitted_frame.reset ();
158         _last_emitted_eyes.reset ();
159 }