Fix 3D support.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "video_decoder.h"
21 #include "image.h"
22 #include "content_video.h"
23
24 #include "i18n.h"
25
26 using std::cout;
27 using std::list;
28 using boost::shared_ptr;
29 using boost::optional;
30
31 VideoDecoder::VideoDecoder (shared_ptr<const VideoContent> c)
32 #ifdef DCPOMATIC_DEBUG
33         : test_gaps (0)
34         , _video_content (c)
35 #else
36         : _video_content (c)
37 #endif
38 {
39
40 }
41
42 list<ContentVideo>
43 VideoDecoder::decoded_video (VideoFrame frame)
44 {
45         list<ContentVideo> output;
46         
47         for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
48                 if (i->frame == frame) {
49                         output.push_back (*i);
50                 }
51         }
52
53         return output;
54 }
55
56 list<ContentVideo>
57 VideoDecoder::get_video (VideoFrame frame, bool accurate)
58 {
59         if (_decoded_video.empty() || (frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1))) {
60                 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
61                 seek (ContentTime::from_frames (frame, _video_content->video_frame_rate()), accurate);
62         }
63
64         list<ContentVideo> dec;
65
66         /* Now enough pass() calls should either:
67          *  (a) give us what we want, or
68          *  (b) hit the end of the decoder.
69          */
70         if (accurate) {
71                 /* We are being accurate, so we want the right frame.
72                  * This could all be one statement but it's split up for clarity.
73                  */
74                 while (true) {
75                         if (!decoded_video(frame).empty ()) {
76                                 /* We got what we want */
77                                 break;
78                         }
79
80                         if (pass ()) {
81                                 /* The decoder has nothing more for us */
82                                 break;
83                         }
84
85                         if (!_decoded_video.empty() && _decoded_video.front().frame > frame) {
86                                 /* We're never going to get the frame we want.  Perhaps the caller is asking
87                                  * for a video frame before the content's video starts (if its audio
88                                  * begins before its video, for example).
89                                  */
90                                 break;
91                         }
92                 }
93
94                 dec = decoded_video (frame);
95         } else {
96                 /* Any frame will do: use the first one that comes out of pass() */
97                 while (_decoded_video.empty() && !pass ()) {}
98                 if (!_decoded_video.empty ()) {
99                         dec.push_back (_decoded_video.front ());
100                 }
101         }
102
103         /* Clean up decoded_video */
104         while (!_decoded_video.empty() && _decoded_video.front().frame < (frame - 1)) {
105                 _decoded_video.pop_front ();
106         }
107
108         return dec;
109 }
110
111
112 /** Called by subclasses when they have a video frame ready */
113 void
114 VideoDecoder::video (shared_ptr<const Image> image, VideoFrame frame)
115 {
116         /* We should not receive the same thing twice */
117         assert (_decoded_video.empty() || frame != _decoded_video.back().frame);
118
119         /* Fill in gaps */
120         /* XXX: 3D */
121
122         while (!_decoded_video.empty () && (_decoded_video.back().frame + 1) < frame) {
123 #ifdef DCPOMATIC_DEBUG
124                 test_gaps++;
125 #endif
126                 _decoded_video.push_back (
127                         ContentVideo (
128                                 _decoded_video.back().image,
129                                 _decoded_video.back().eyes,
130                                 _decoded_video.back().frame + 1
131                                 )
132                         );
133         }
134         
135         switch (_video_content->video_frame_type ()) {
136         case VIDEO_FRAME_TYPE_2D:
137                 _decoded_video.push_back (ContentVideo (image, EYES_BOTH, frame));
138                 break;
139         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
140                 _decoded_video.push_back (ContentVideo (image, (frame % 2) ? EYES_RIGHT : EYES_LEFT, frame));
141                 break;
142         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
143         {
144                 int const half = image->size().width / 2;
145                 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, half, 0, 0), true), EYES_LEFT, frame));
146                 _decoded_video.push_back (ContentVideo (image->crop (Crop (half, 0, 0, 0), true), EYES_RIGHT, frame));
147                 break;
148         }
149         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
150         {
151                 int const half = image->size().height / 2;
152                 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, 0, half), true), EYES_LEFT, frame));
153                 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, half, 0), true), EYES_RIGHT, frame));
154                 break;
155         }
156         case VIDEO_FRAME_TYPE_3D_LEFT:
157                 _decoded_video.push_back (ContentVideo (image, EYES_LEFT, frame));
158                 break;
159         case VIDEO_FRAME_TYPE_3D_RIGHT:
160                 _decoded_video.push_back (ContentVideo (image, EYES_RIGHT, frame));
161                 break;
162         default:
163                 assert (false);
164         }
165 }
166
167 void
168 VideoDecoder::seek (ContentTime, bool)
169 {
170         _decoded_video.clear ();
171 }
172