Hand-apply 80562fe5dce5fd625da583ca6f7c2833f9db8754 from master (remove default scale...
[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 "raw_image_proxy.h"
24 #include "content_video.h"
25
26 #include "i18n.h"
27
28 using std::cout;
29 using std::list;
30 using std::max;
31 using std::back_inserter;
32 using boost::shared_ptr;
33 using boost::optional;
34
35 VideoDecoder::VideoDecoder (shared_ptr<const VideoContent> c)
36 #ifdef DCPOMATIC_DEBUG
37         : test_gaps (0)
38         , _video_content (c)
39 #else
40         : _video_content (c)
41 #endif
42         , _same (false)
43 {
44         _black_image.reset (new Image (PIX_FMT_RGB24, _video_content->video_size(), true));
45         _black_image->make_black ();
46 }
47
48 list<ContentVideo>
49 VideoDecoder::decoded_video (VideoFrame frame)
50 {
51         list<ContentVideo> output;
52         
53         for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
54                 if (i->frame == frame) {
55                         output.push_back (*i);
56                 }
57         }
58
59         return output;
60 }
61
62 /** Get all frames which exist in the content at a given frame index.
63  *  @param frame Frame index.
64  *  @param accurate true to try hard to return frames at the precise time that was requested, otherwise frames nearby may be returned.
65  *  @return Frames; there may be none (if there is no video there), 1 for 2D or 2 for 3D.
66  */
67 list<ContentVideo>
68 VideoDecoder::get_video (VideoFrame frame, bool accurate)
69 {
70         /* At this stage, if we have get_video()ed before, _decoded_video will contain the last frame that this
71            method returned (and possibly a few more).  If the requested frame is not in _decoded_video and it is not the next
72            one after the end of _decoded_video we need to seek.
73         */
74            
75         if (_decoded_video.empty() || frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1)) {
76                 seek (ContentTime::from_frames (frame, _video_content->video_frame_rate()), accurate);
77         }
78
79         list<ContentVideo> dec;
80
81         /* Now enough pass() calls should either:
82          *  (a) give us what we want, or
83          *  (b) give us something after what we want, indicating that we will never get what we want, or
84          *  (c) hit the end of the decoder.
85          */
86         if (accurate) {
87                 /* We are being accurate, so we want the right frame.
88                  * This could all be one statement but it's split up for clarity.
89                  */
90                 while (true) {
91                         if (!decoded_video(frame).empty ()) {
92                                 /* We got what we want */
93                                 break;
94                         }
95
96                         if (pass ()) {
97                                 /* The decoder has nothing more for us */
98                                 break;
99                         }
100
101                         if (!_decoded_video.empty() && _decoded_video.front().frame > frame) {
102                                 /* We're never going to get the frame we want.  Perhaps the caller is asking
103                                  * for a video frame before the content's video starts (if its audio
104                                  * begins before its video, for example).
105                                  */
106                                 break;
107                         }
108                 }
109
110                 dec = decoded_video (frame);
111         } else {
112                 /* Any frame will do: use the first one that comes out of pass() */
113                 while (_decoded_video.empty() && !pass ()) {}
114                 if (!_decoded_video.empty ()) {
115                         dec.push_back (_decoded_video.front ());
116                 }
117         }
118
119         /* Clean up _decoded_video; keep the frame we are returning (which may have two images
120            for 3D), but nothing before that */
121         while (!_decoded_video.empty() && _decoded_video.front().frame < dec.front().frame) {
122                 _decoded_video.pop_front ();
123         }
124
125         return dec;
126 }
127
128 /** Fill _decoded_video up to, but not including, the specified frame */
129 void
130 VideoDecoder::fill_up_to_2d (VideoFrame frame)
131 {
132         if (frame == 0) {
133                 /* Already OK */
134                 return;
135         }
136
137         /* Fill with black... */
138         boost::shared_ptr<const ImageProxy> filler_image (new RawImageProxy (_black_image));
139         Part filler_part = PART_WHOLE;
140
141         /* ...unless there's some video we can fill with */
142         if (!_decoded_video.empty ()) {
143                 filler_image = _decoded_video.back().image;
144                 filler_part = _decoded_video.back().part;
145         }
146
147         VideoFrame filler_frame = _decoded_video.empty() ? 0 : (_decoded_video.back().frame + 1);
148         while (filler_frame < frame) {
149
150 #ifdef DCPOMATIC_DEBUG
151                 test_gaps++;
152 #endif
153
154                 _decoded_video.push_back (
155                         ContentVideo (filler_image, EYES_BOTH, filler_part, filler_frame)
156                         );
157                 
158                 ++filler_frame;
159         }
160 }
161
162 /** Fill _decoded_video up to, but not including, the specified frame and eye */
163 void
164 VideoDecoder::fill_up_to_3d (VideoFrame frame, Eyes eye)
165 {
166         if (frame == 0 && eye == EYES_LEFT) {
167                 /* Already OK */
168                 return;
169         }
170
171         /* Fill with black... */
172         boost::shared_ptr<const ImageProxy> filler_left_image (new RawImageProxy (_black_image));
173         boost::shared_ptr<const ImageProxy> filler_right_image (new RawImageProxy (_black_image));
174         Part filler_left_part = PART_WHOLE;
175         Part filler_right_part = PART_WHOLE;
176
177         /* ...unless there's some video we can fill with */
178         for (list<ContentVideo>::const_reverse_iterator i = _decoded_video.rbegin(); i != _decoded_video.rend(); ++i) {
179                 if (i->eyes == EYES_LEFT && !filler_left_image) {
180                         filler_left_image = i->image;
181                         filler_left_part = i->part;
182                 } else if (i->eyes == EYES_RIGHT && !filler_right_image) {
183                         filler_right_image = i->image;
184                         filler_right_part = i->part;
185                 }
186
187                 if (filler_left_image && filler_right_image) {
188                         break;
189                 }
190         }
191
192         VideoFrame filler_frame = _decoded_video.empty() ? 0 : _decoded_video.back().frame;
193         Eyes filler_eye = _decoded_video.empty() ? EYES_LEFT : _decoded_video.back().eyes;
194
195         if (_decoded_video.empty ()) {
196                 filler_frame = 0;
197                 filler_eye = EYES_LEFT;
198         } else if (_decoded_video.back().eyes == EYES_LEFT) {
199                 filler_frame = _decoded_video.back().frame;
200                 filler_eye = EYES_RIGHT;
201         } else if (_decoded_video.back().eyes == EYES_RIGHT) {
202                 filler_frame = _decoded_video.back().frame + 1;
203                 filler_eye = EYES_LEFT;
204         }
205
206         while (filler_frame != frame || filler_eye != eye) {
207
208 #ifdef DCPOMATIC_DEBUG
209                 test_gaps++;
210 #endif
211
212                 _decoded_video.push_back (
213                         ContentVideo (
214                                 filler_eye == EYES_LEFT ? filler_left_image : filler_right_image,
215                                 filler_eye,
216                                 filler_eye == EYES_LEFT ? filler_left_part : filler_right_part,
217                                 filler_frame
218                                 )
219                         );
220
221                 if (filler_eye == EYES_LEFT) {
222                         filler_eye = EYES_RIGHT;
223                 } else {
224                         filler_eye = EYES_LEFT;
225                         ++filler_frame;
226                 }
227         }
228 }
229         
230 /** Called by subclasses when they have a video frame ready */
231 void
232 VideoDecoder::video (shared_ptr<const ImageProxy> image, VideoFrame frame)
233 {
234         /* We may receive the same frame index twice for 3D, and we need to know
235            when that happens.
236         */
237         _same = (!_decoded_video.empty() && frame == _decoded_video.back().frame);
238
239         /* Work out what we are going to push into _decoded_video next */
240         list<ContentVideo> to_push;
241         switch (_video_content->video_frame_type ()) {
242         case VIDEO_FRAME_TYPE_2D:
243                 to_push.push_back (ContentVideo (image, EYES_BOTH, PART_WHOLE, frame));
244                 break;
245         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
246                 to_push.push_back (ContentVideo (image, _same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE, frame));
247                 break;
248         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
249                 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_LEFT_HALF, frame));
250                 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_RIGHT_HALF, frame));
251                 break;
252         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
253                 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_TOP_HALF, frame));
254                 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_BOTTOM_HALF, frame));
255                 break;
256         case VIDEO_FRAME_TYPE_3D_LEFT:
257                 to_push.push_back (ContentVideo (image, EYES_LEFT, PART_WHOLE, frame));
258                 break;
259         case VIDEO_FRAME_TYPE_3D_RIGHT:
260                 to_push.push_back (ContentVideo (image, EYES_RIGHT, PART_WHOLE, frame));
261                 break;
262         default:
263                 assert (false);
264         }
265
266         /* Now VideoDecoder is required never to have gaps in the frames that it presents
267            via get_video().  Hence we need to fill in any gap between the last thing in _decoded_video
268            and the things we are about to push.
269         */
270
271         if (_video_content->video_frame_type() == VIDEO_FRAME_TYPE_2D) {
272                 fill_up_to_2d (to_push.front().frame);
273         } else {
274                 fill_up_to_3d (to_push.front().frame, to_push.front().eyes);
275         }
276
277         copy (to_push.begin(), to_push.end(), back_inserter (_decoded_video));
278 }
279
280 void
281 VideoDecoder::seek (ContentTime, bool)
282 {
283         _decoded_video.clear ();
284 }
285