Basic video fade 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 "image_proxy.h"
23 #include "content_video.h"
24
25 #include "i18n.h"
26
27 using std::cout;
28 using std::list;
29 using std::max;
30 using boost::shared_ptr;
31 using boost::optional;
32
33 VideoDecoder::VideoDecoder (shared_ptr<const VideoContent> c)
34 #ifdef DCPOMATIC_DEBUG
35         : test_gaps (0)
36         , _video_content (c)
37 #else
38         : _video_content (c)
39 #endif
40         , _same (false)
41 {
42
43 }
44
45 list<ContentVideo>
46 VideoDecoder::decoded_video (VideoFrame frame)
47 {
48         list<ContentVideo> output;
49         
50         for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
51                 if (i->frame == frame) {
52                         output.push_back (*i);
53                 }
54         }
55
56         return output;
57 }
58
59 /** Get all frames which exist in the content at a given frame index.
60  *  @param frame Frame index.
61  *  @param accurate true to try hard to return frames at the precise time that was requested, otherwise frames nearby may be returned.
62  *  @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
63  */
64 list<ContentVideo>
65 VideoDecoder::get_video (VideoFrame frame, bool accurate)
66 {
67         /* At this stage, if we have get_video()ed before, _decoded_video will contain the last frame that this
68            method returned (and possibly a few more).  If the requested frame is not in _decoded_video and it is not the next
69            one after the end of _decoded_video we need to seek.
70         */
71            
72         if (_decoded_video.empty() || frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1)) {
73                 seek (ContentTime::from_frames (frame, _video_content->video_frame_rate()), accurate);
74         }
75
76         list<ContentVideo> dec;
77
78         /* Now enough pass() calls should either:
79          *  (a) give us what we want, or
80          *  (b) give us something after what we want, indicating that we will never get what we want, or
81          *  (c) hit the end of the decoder.
82          */
83         if (accurate) {
84                 /* We are being accurate, so we want the right frame.
85                  * This could all be one statement but it's split up for clarity.
86                  */
87                 while (true) {
88                         if (!decoded_video(frame).empty ()) {
89                                 /* We got what we want */
90                                 break;
91                         }
92
93                         if (pass ()) {
94                                 /* The decoder has nothing more for us */
95                                 break;
96                         }
97
98                         if (!_decoded_video.empty() && _decoded_video.front().frame > frame) {
99                                 /* We're never going to get the frame we want.  Perhaps the caller is asking
100                                  * for a video frame before the content's video starts (if its audio
101                                  * begins before its video, for example).
102                                  */
103                                 break;
104                         }
105                 }
106
107                 dec = decoded_video (frame);
108         } else {
109                 /* Any frame will do: use the first one that comes out of pass() */
110                 while (_decoded_video.empty() && !pass ()) {}
111                 if (!_decoded_video.empty ()) {
112                         dec.push_back (_decoded_video.front ());
113                 }
114         }
115
116         /* Clean up _decoded_video; keep the frame we are returning, but nothing before that */
117         while (!_decoded_video.empty() && _decoded_video.front().frame < dec.front().frame) {
118                 _decoded_video.pop_front ();
119         }
120
121         return dec;
122 }
123
124
125 /** Called by subclasses when they have a video frame ready */
126 void
127 VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
128 {
129         /* We may receive the same frame index twice for 3D, and we need to know
130            when that happens.
131         */
132         _same = (!_decoded_video.empty() && frame == _decoded_video.back().frame);
133
134         /* Fill in gaps */
135         /* XXX: 3D */
136
137         while (!_decoded_video.empty () && (_decoded_video.back().frame + 1) < frame) {
138 #ifdef DCPOMATIC_DEBUG
139                 test_gaps++;
140 #endif
141                 _decoded_video.push_back (
142                         ContentVideo (
143                                 _decoded_video.back().image,
144                                 _decoded_video.back().eyes,
145                                 _decoded_video.back().part,
146                                 _decoded_video.back().frame + 1
147                                 )
148                         );
149         }
150         
151         switch (_video_content->video_frame_type ()) {
152         case VIDEO_FRAME_TYPE_2D:
153                 _decoded_video.push_back (ContentVideo (image, EYES_BOTH, PART_WHOLE, frame));
154                 break;
155         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
156                 _decoded_video.push_back (ContentVideo (image, _same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE, frame));
157                 break;
158         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
159                 _decoded_video.push_back (ContentVideo (image, EYES_LEFT, PART_LEFT_HALF, frame));
160                 _decoded_video.push_back (ContentVideo (image, EYES_RIGHT, PART_RIGHT_HALF, frame));
161                 break;
162         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
163                 _decoded_video.push_back (ContentVideo (image, EYES_LEFT, PART_TOP_HALF, frame));
164                 _decoded_video.push_back (ContentVideo (image, EYES_RIGHT, PART_BOTTOM_HALF, frame));
165                 break;
166         case VIDEO_FRAME_TYPE_3D_LEFT:
167                 _decoded_video.push_back (ContentVideo (image, EYES_LEFT, PART_WHOLE, frame));
168                 break;
169         case VIDEO_FRAME_TYPE_3D_RIGHT:
170                 _decoded_video.push_back (ContentVideo (image, EYES_RIGHT, PART_WHOLE, frame));
171                 break;
172         default:
173                 assert (false);
174         }
175 }
176
177 void
178 VideoDecoder::seek (ContentTime, bool)
179 {
180         _decoded_video.clear ();
181 }
182