Put Time types in dcpomatic namespace.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "video_decoder.h"
22 #include "image.h"
23 #include "raw_image_proxy.h"
24 #include "film.h"
25 #include "log.h"
26 #include "compose.hpp"
27 #include <boost/foreach.hpp>
28 #include <iostream>
29
30 #include "i18n.h"
31
32 using std::cout;
33 using std::list;
34 using std::max;
35 using std::back_inserter;
36 using boost::shared_ptr;
37 using boost::optional;
38 using namespace dcpomatic;
39
40 VideoDecoder::VideoDecoder (Decoder* parent, shared_ptr<const Content> c)
41         : DecoderPart (parent)
42         , _content (c)
43 {
44
45 }
46
47 /** Called by decoder classes when they have a video frame ready.
48  *  @param frame Frame index within the content; this does not take into account 3D
49  *  so for 3D_ALTERNATE this value goes:
50  *     0: frame 0 left
51  *     1: frame 0 right
52  *     2: frame 1 left
53  *     3: frame 1 right
54  *  and so on.
55  */
56 void
57 VideoDecoder::emit (shared_ptr<const Film> film, shared_ptr<const ImageProxy> image, Frame frame)
58 {
59         if (ignore ()) {
60                 return;
61         }
62
63         switch (_content->video->frame_type ()) {
64         case VIDEO_FRAME_TYPE_2D:
65                 Data (ContentVideo (image, frame, EYES_BOTH, PART_WHOLE));
66                 break;
67         case VIDEO_FRAME_TYPE_3D:
68         {
69                 /* We receive the same frame index twice for 3D; hence we know which
70                    frame this one is.
71                 */
72                 bool const same = (_last_emitted && _last_emitted.get() == frame);
73                 Data (ContentVideo (image, frame, same ? EYES_RIGHT : EYES_LEFT, PART_WHOLE));
74                 _last_emitted = frame;
75                 break;
76         }
77         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
78                 Data (ContentVideo (image, frame / 2, (frame % 2) ? EYES_RIGHT : EYES_LEFT, PART_WHOLE));
79                 frame /= 2;
80                 break;
81         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
82                 Data (ContentVideo (image, frame, EYES_LEFT, PART_LEFT_HALF));
83                 Data (ContentVideo (image, frame, EYES_RIGHT, PART_RIGHT_HALF));
84                 break;
85         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
86                 Data (ContentVideo (image, frame, EYES_LEFT, PART_TOP_HALF));
87                 Data (ContentVideo (image, frame, EYES_RIGHT, PART_BOTTOM_HALF));
88                 break;
89         case VIDEO_FRAME_TYPE_3D_LEFT:
90                 Data (ContentVideo (image, frame, EYES_LEFT, PART_WHOLE));
91                 break;
92         case VIDEO_FRAME_TYPE_3D_RIGHT:
93                 Data (ContentVideo (image, frame, EYES_RIGHT, PART_WHOLE));
94                 break;
95         default:
96                 DCPOMATIC_ASSERT (false);
97         }
98
99         _position = ContentTime::from_frames (frame, _content->active_video_frame_rate(film));
100 }
101
102 void
103 VideoDecoder::seek ()
104 {
105         _position = ContentTime();
106         _last_emitted.reset ();
107 }