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 {
39
40 }
41
42 optional<ContentVideo>
43 VideoDecoder::decoded_video (VideoFrame frame)
44 {
45         for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
46                 if (i->frame == frame) {
47                         return *i;
48                 }
49         }
50
51         return optional<ContentVideo> ();
52 }
53
54 optional<ContentVideo>
55 VideoDecoder::get_video (VideoFrame frame, bool accurate)
56 {
57         if (_decoded_video.empty() || (frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1))) {
58                 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
59                 seek (ContentTime::from_frames (frame, _video_content->video_frame_rate()), accurate);
60         }
61
62         optional<ContentVideo> dec;
63
64         /* Now enough pass() calls should either:
65          *  (a) give us what we want, or
66          *  (b) hit the end of the decoder.
67          */
68         if (accurate) {
69                 /* We are being accurate, so we want the right frame.
70                  * This could all be one statement but it's split up for clarity.
71                  */
72                 while (true) {
73                         if (decoded_video (frame)) {
74                                 /* We got what we want */
75                                 break;
76                         }
77
78                         if (pass ()) {
79                                 /* The decoder has nothing more for us */
80                                 break;
81                         }
82
83                         if (!_decoded_video.empty() && _decoded_video.front().frame > frame) {
84                                 /* We're never going to get the frame we want.  Perhaps the caller is asking
85                                  * for a video frame before the content's video starts (if its audio
86                                  * begins before its video, for example).
87                                  */
88                                 break;
89                         }
90                 }
91
92                 dec = decoded_video (frame);
93         } else {
94                 /* Any frame will do: use the first one that comes out of pass() */
95                 while (_decoded_video.empty() && !pass ()) {}
96                 if (!_decoded_video.empty ()) {
97                         dec = _decoded_video.front ();
98                 }
99         }
100
101         /* Clean up decoded_video */
102         while (!_decoded_video.empty() && _decoded_video.front().frame < (frame - 1)) {
103                 _decoded_video.pop_front ();
104         }
105
106         return dec;
107 }
108
109
110 /** Called by subclasses when they have a video frame ready */
111 void
112 VideoDecoder::video (shared_ptr<const Image> image, VideoFrame frame)
113 {
114         /* We should not receive the same thing twice */
115         assert (_decoded_video.empty() || frame != _decoded_video.back().frame);
116
117         /* Fill in gaps */
118         /* XXX: 3D */
119
120         while (!_decoded_video.empty () && (_decoded_video.back().frame + 1) < frame) {
121 #ifdef DCPOMATIC_DEBUG
122                 test_gaps++;
123 #endif
124                 _decoded_video.push_back (
125                         ContentVideo (
126                                 _decoded_video.back().image,
127                                 _decoded_video.back().eyes,
128                                 _decoded_video.back().frame + 1
129                                 )
130                         );
131         }
132         
133         switch (_video_content->video_frame_type ()) {
134         case VIDEO_FRAME_TYPE_2D:
135                 _decoded_video.push_back (ContentVideo (image, EYES_BOTH, frame));
136                 break;
137         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
138                 _decoded_video.push_back (ContentVideo (image, (frame % 2) ? EYES_RIGHT : EYES_LEFT, frame));
139                 break;
140         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
141         {
142                 int const half = image->size().width / 2;
143                 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, half, 0, 0), true), EYES_LEFT, frame));
144                 _decoded_video.push_back (ContentVideo (image->crop (Crop (half, 0, 0, 0), true), EYES_RIGHT, frame));
145                 break;
146         }
147         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
148         {
149                 int const half = image->size().height / 2;
150                 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, 0, half), true), EYES_LEFT, frame));
151                 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, half, 0), true), EYES_RIGHT, frame));
152                 break;
153         }
154         case VIDEO_FRAME_TYPE_3D_LEFT:
155                 _decoded_video.push_back (ContentVideo (image, EYES_LEFT, frame));
156                 break;
157         case VIDEO_FRAME_TYPE_3D_RIGHT:
158                 _decoded_video.push_back (ContentVideo (image, EYES_RIGHT, frame));
159                 break;
160         default:
161                 assert (false);
162         }
163 }
164
165 void
166 VideoDecoder::seek (ContentTime, bool)
167 {
168         _decoded_video.clear ();
169 }
170