Cleanup: use a loop.
[libdcp.git] / src / stereo_picture_frame.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 /** @file  src/stereo_picture_frame.cc
36  *  @brief StereoPictureFrame class
37  */
38
39
40 #include "colour_conversion.h"
41 #include "compose.hpp"
42 #include "crypto_context.h"
43 #include "exceptions.h"
44 #include "j2k_transcode.h"
45 #include "rgb_xyz.h"
46 #include "stereo_picture_frame.h"
47 #include "util.h"
48 #include <asdcp/AS_DCP.h>
49 #include <asdcp/KM_fileio.h>
50
51
52 using std::string;
53 using std::shared_ptr;
54 using std::make_shared;
55 using namespace dcp;
56
57
58 StereoPictureFrame::Part::Part (shared_ptr<ASDCP::JP2K::SFrameBuffer> buffer, Eye eye)
59         : _buffer (buffer)
60         , _eye (eye)
61 {
62
63 }
64
65
66 ASDCP::JP2K::FrameBuffer &
67 StereoPictureFrame::Part::mono () const
68 {
69         return _eye == Eye::LEFT ? _buffer->Left : _buffer->Right;
70 }
71
72
73 uint8_t const *
74 StereoPictureFrame::Part::data () const
75 {
76         return mono().RoData();
77 }
78
79
80 uint8_t *
81 StereoPictureFrame::Part::data ()
82 {
83         return mono().Data();
84 }
85
86
87 int
88 StereoPictureFrame::Part::size () const
89 {
90         return mono().Size();
91 }
92
93
94 /** Make a picture frame from a 3D (stereoscopic) asset.
95  *  @param reader Reader for the MXF file.
96  *  @param n Frame within the asset, not taking EntryPoint into account.
97  *  @param check_hmac true to check the HMAC and give an error if it is not as expected.
98  */
99 StereoPictureFrame::StereoPictureFrame (ASDCP::JP2K::MXFSReader* reader, int n, shared_ptr<DecryptionContext> c, bool check_hmac)
100 {
101         /* XXX: unfortunate guesswork on this buffer size */
102         _buffer = make_shared<ASDCP::JP2K::SFrameBuffer>(4 * Kumu::Megabyte);
103
104         if (ASDCP_FAILURE (reader->ReadFrame (n, *_buffer, c->context(), check_hmac ? c->hmac() : nullptr))) {
105                 boost::throw_exception (ReadError (String::compose ("could not read video frame %1 of %2", n)));
106         }
107 }
108
109
110 StereoPictureFrame::StereoPictureFrame ()
111 {
112         _buffer = make_shared<ASDCP::JP2K::SFrameBuffer>(4 * Kumu::Megabyte);
113 }
114
115
116 /** @param eye Eye to return (EYE_LEFT or EYE_RIGHT).
117  *  @param reduce a factor by which to reduce the resolution
118  *  of the image, expressed as a power of two (pass 0 for no
119  *  reduction).
120  */
121 shared_ptr<OpenJPEGImage>
122 StereoPictureFrame::xyz_image (Eye eye, int reduce) const
123 {
124         switch (eye) {
125         case Eye::LEFT:
126                 return decompress_j2k (const_cast<uint8_t*>(_buffer->Left.RoData()), _buffer->Left.Size(), reduce);
127         case Eye::RIGHT:
128                 return decompress_j2k (const_cast<uint8_t*>(_buffer->Right.RoData()), _buffer->Right.Size(), reduce);
129         }
130
131         return {};
132 }
133
134
135 shared_ptr<StereoPictureFrame::Part>
136 StereoPictureFrame::right () const
137 {
138         return make_shared<Part>(_buffer, Eye::RIGHT);
139 }
140
141
142 shared_ptr<StereoPictureFrame::Part>
143 StereoPictureFrame::left () const
144 {
145         return make_shared<Part>(_buffer, Eye::LEFT);
146 }
147
148