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         : _video_content (c)
33 {
34
35 }
36
37 optional<ContentVideo>
38 VideoDecoder::decoded_video (VideoFrame frame)
39 {
40         for (list<ContentVideo>::const_iterator i = _decoded_video.begin(); i != _decoded_video.end(); ++i) {
41                 if (i->frame == frame) {
42                         return *i;
43                 }
44         }
45
46         return optional<ContentVideo> ();
47 }
48
49 optional<ContentVideo>
50 VideoDecoder::get_video (VideoFrame frame, bool accurate)
51 {
52         if (_decoded_video.empty() || (frame < _decoded_video.front().frame || frame > (_decoded_video.back().frame + 1))) {
53                 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
54                 seek (ContentTime::from_frames (frame, _video_content->video_frame_rate()), accurate);
55         }
56
57         optional<ContentVideo> dec;
58
59         /* Now enough pass() calls will either:
60          *  (a) give us what we want, or
61          *  (b) hit the end of the decoder.
62          */
63         if (accurate) {
64                 /* We are being accurate, so we want the right frame */
65                 while (!decoded_video (frame) && !pass ()) {}
66                 dec = decoded_video (frame);
67         } else {
68                 /* Any frame will do: use the first one that comes out of pass() */
69                 while (_decoded_video.empty() && !pass ()) {}
70                 if (!_decoded_video.empty ()) {
71                         dec = _decoded_video.front ();
72                 }
73         }
74
75         /* Clean up decoded_video */
76         while (!_decoded_video.empty() && _decoded_video.front().frame < (frame - 1)) {
77                 _decoded_video.pop_front ();
78         }
79
80         return dec;
81 }
82
83
84 /** Called by subclasses when they have a video frame ready */
85 void
86 VideoDecoder::video (shared_ptr<const Image> image, VideoFrame frame)
87 {
88         /* Fill in gaps */
89         /* XXX: 3D */
90         while (!_decoded_video.empty () && (_decoded_video.back().frame + 1) < frame) {
91                 _decoded_video.push_back (
92                         ContentVideo (
93                                 _decoded_video.back().image,
94                                 _decoded_video.back().eyes,
95                                 _decoded_video.back().frame + 1
96                                 )
97                         );
98         }
99         
100         switch (_video_content->video_frame_type ()) {
101         case VIDEO_FRAME_TYPE_2D:
102                 _decoded_video.push_back (ContentVideo (image, EYES_BOTH, frame));
103                 break;
104         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
105                 _decoded_video.push_back (ContentVideo (image, (frame % 2) ? EYES_RIGHT : EYES_LEFT, frame));
106                 break;
107         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
108         {
109                 int const half = image->size().width / 2;
110                 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, half, 0, 0), true), EYES_LEFT, frame));
111                 _decoded_video.push_back (ContentVideo (image->crop (Crop (half, 0, 0, 0), true), EYES_RIGHT, frame));
112                 break;
113         }
114         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
115         {
116                 int const half = image->size().height / 2;
117                 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, 0, half), true), EYES_LEFT, frame));
118                 _decoded_video.push_back (ContentVideo (image->crop (Crop (0, 0, half, 0), true), EYES_RIGHT, frame));
119                 break;
120         }
121         case VIDEO_FRAME_TYPE_3D_LEFT:
122                 _decoded_video.push_back (ContentVideo (image, EYES_LEFT, frame));
123                 break;
124         case VIDEO_FRAME_TYPE_3D_RIGHT:
125                 _decoded_video.push_back (ContentVideo (image, EYES_RIGHT, frame));
126                 break;
127         default:
128                 assert (false);
129         }
130 }
131
132 void
133 VideoDecoder::seek (ContentTime, bool)
134 {
135         _decoded_video.clear ();
136 }
137