It builds.
[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 shared_ptr<ContentVideo>
38 VideoDecoder::decoded_video (VideoFrame frame)
39 {
40         for (list<shared_ptr<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 shared_ptr<ContentVideo> ();
47 }
48
49 shared_ptr<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         shared_ptr<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         switch (_video_content->video_frame_type ()) {
89         case VIDEO_FRAME_TYPE_2D:
90                 _decoded_video.push_back (shared_ptr<ContentVideo> (new ContentVideo (image, EYES_BOTH, frame)));
91                 break;
92         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
93                 _decoded_video.push_back (shared_ptr<ContentVideo> (new ContentVideo (image, (frame % 2) ? EYES_RIGHT : EYES_LEFT, frame)));
94                 break;
95         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
96         {
97                 int const half = image->size().width / 2;
98                 _decoded_video.push_back (shared_ptr<ContentVideo> (new ContentVideo (image->crop (Crop (0, half, 0, 0), true), EYES_LEFT, frame)));
99                 _decoded_video.push_back (shared_ptr<ContentVideo> (new ContentVideo (image->crop (Crop (half, 0, 0, 0), true), EYES_RIGHT, frame)));
100                 break;
101         }
102         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
103         {
104                 int const half = image->size().height / 2;
105                 _decoded_video.push_back (shared_ptr<ContentVideo> (new ContentVideo (image->crop (Crop (0, 0, 0, half), true), EYES_LEFT, frame)));
106                 _decoded_video.push_back (shared_ptr<ContentVideo> (new ContentVideo (image->crop (Crop (0, 0, half, 0), true), EYES_RIGHT, frame)));
107                 break;
108         }
109         default:
110                 assert (false);
111         }
112 }
113
114 void
115 VideoDecoder::seek (ContentTime, bool)
116 {
117         _decoded_video.clear ();
118 }
119