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