21083504eace640ca826814c14133bc8e0776297
[dcpomatic.git] / src / lib / j2k_image_proxy.cc
1 /*
2     Copyright (C) 2014-2015 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 "j2k_image_proxy.h"
22 #include "dcpomatic_socket.h"
23 #include "image.h"
24 #include "dcpomatic_assert.h"
25 #include "warnings.h"
26 #include <dcp/raw_convert.h>
27 #include <dcp/openjpeg_image.h>
28 #include <dcp/mono_picture_frame.h>
29 #include <dcp/stereo_picture_frame.h>
30 #include <dcp/colour_conversion.h>
31 #include <dcp/rgb_xyz.h>
32 #include <dcp/j2k.h>
33 #include <libcxml/cxml.h>
34 DCPOMATIC_DISABLE_WARNINGS
35 #include <libxml++/libxml++.h>
36 DCPOMATIC_ENABLE_WARNINGS
37 #include <iostream>
38
39 #include "i18n.h"
40
41 using std::string;
42 using std::cout;
43 using std::max;
44 using std::pair;
45 using std::make_pair;
46 using boost::shared_ptr;
47 using boost::optional;
48 using boost::dynamic_pointer_cast;
49 using dcp::ArrayData;
50 using dcp::raw_convert;
51
52 /** Construct a J2KImageProxy from a JPEG2000 file */
53 J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size, AVPixelFormat pixel_format)
54         : _data (new dcp::ArrayData(path))
55         , _size (size)
56         , _pixel_format (pixel_format)
57         , _error (false)
58 {
59         /* ::image assumes 16bpp */
60         DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
61 }
62
63
64 J2KImageProxy::J2KImageProxy (
65         shared_ptr<const dcp::MonoPictureFrame> frame,
66         dcp::Size size,
67         AVPixelFormat pixel_format,
68         optional<int> forced_reduction
69         )
70         : _data (frame)
71         , _size (size)
72         , _pixel_format (pixel_format)
73         , _forced_reduction (forced_reduction)
74         , _error (false)
75 {
76         /* ::image assumes 16bpp */
77         DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
78 }
79
80
81 J2KImageProxy::J2KImageProxy (
82         shared_ptr<const dcp::StereoPictureFrame> frame,
83         dcp::Size size,
84         dcp::Eye eye,
85         AVPixelFormat pixel_format,
86         optional<int> forced_reduction
87         )
88         : _data (eye ? frame->left() : frame->right())
89         , _size (size)
90         , _eye (eye)
91         , _pixel_format (pixel_format)
92         , _forced_reduction (forced_reduction)
93         , _error (false)
94 {
95         /* ::image assumes 16bpp */
96         DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
97 }
98
99
100 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
101         : _error (false)
102 {
103         _size = dcp::Size (xml->number_child<int> ("Width"), xml->number_child<int> ("Height"));
104         if (xml->optional_number_child<int> ("Eye")) {
105                 _eye = static_cast<dcp::Eye> (xml->number_child<int> ("Eye"));
106         }
107         shared_ptr<ArrayData> data(new ArrayData(xml->number_child<int>("Size")));
108         /* This only matters when we are using J2KImageProxy for the preview, which
109            will never use this constructor (which is only used for passing data to
110            encode servers).  So we can put anything in here.  It's a bit of a hack.
111         */
112         _pixel_format = AV_PIX_FMT_XYZ12LE;
113         socket->read (data->data(), data->size());
114         _data = data;
115 }
116
117 int
118 J2KImageProxy::prepare (optional<dcp::Size> target_size) const
119 {
120         boost::mutex::scoped_lock lm (_mutex);
121
122         if (_image && target_size == _target_size) {
123                 DCPOMATIC_ASSERT (_reduce);
124                 return *_reduce;
125         }
126
127         int reduce = 0;
128
129         if (_forced_reduction) {
130                 reduce = *_forced_reduction;
131         } else {
132                 while (target_size && (_size.width / pow(2, reduce)) > target_size->width && (_size.height / pow(2, reduce)) > target_size->height) {
133                         ++reduce;
134                 }
135
136                 --reduce;
137                 reduce = max (0, reduce);
138         }
139
140         try {
141                 /* XXX: should check that potentially trashing _data here doesn't matter */
142                 shared_ptr<dcp::OpenJPEGImage> decompressed = dcp::decompress_j2k (const_cast<uint8_t*>(_data->data()), _data->size(), reduce);
143                 _image.reset (new Image (_pixel_format, decompressed->size(), true));
144
145                 int const shift = 16 - decompressed->precision (0);
146
147                 /* Copy data in whatever format (sRGB or XYZ) into our Image; I'm assuming
148                    the data is 12-bit either way.
149                    */
150
151                 int const width = decompressed->size().width;
152
153                 int p = 0;
154                 int* decomp_0 = decompressed->data (0);
155                 int* decomp_1 = decompressed->data (1);
156                 int* decomp_2 = decompressed->data (2);
157                 for (int y = 0; y < decompressed->size().height; ++y) {
158                         uint16_t* q = (uint16_t *) (_image->data()[0] + y * _image->stride()[0]);
159                         for (int x = 0; x < width; ++x) {
160                                 *q++ = decomp_0[p] << shift;
161                                 *q++ = decomp_1[p] << shift;
162                                 *q++ = decomp_2[p] << shift;
163                                 ++p;
164                         }
165                 }
166         } catch (dcp::J2KDecompressionError& e) {
167                 _image.reset (new Image (_pixel_format, _size, true));
168                 _image->make_black ();
169                 _error = true;
170         }
171
172         _target_size = target_size;
173         _reduce = reduce;
174
175         return reduce;
176 }
177
178
179 ImageProxy::Result
180 J2KImageProxy::image (optional<dcp::Size> target_size) const
181 {
182         int const r = prepare (target_size);
183
184         /* I think this is safe without a lock on mutex.  _image is guaranteed to be
185            set up when prepare() has happened.
186         */
187         return Result (_image, r, _error);
188 }
189
190
191 void
192 J2KImageProxy::add_metadata (xmlpp::Node* node) const
193 {
194         node->add_child("Type")->add_child_text (N_("J2K"));
195         node->add_child("Width")->add_child_text (raw_convert<string> (_size.width));
196         node->add_child("Height")->add_child_text (raw_convert<string> (_size.height));
197         if (_eye) {
198                 node->add_child("Eye")->add_child_text (raw_convert<string> (static_cast<int> (_eye.get ())));
199         }
200         node->add_child("Size")->add_child_text (raw_convert<string>(_data->size()));
201 }
202
203 void
204 J2KImageProxy::write_to_socket (shared_ptr<Socket> socket) const
205 {
206         socket->write (_data->data(), _data->size());
207 }
208
209 bool
210 J2KImageProxy::same (shared_ptr<const ImageProxy> other) const
211 {
212         shared_ptr<const J2KImageProxy> jp = dynamic_pointer_cast<const J2KImageProxy> (other);
213         if (!jp) {
214                 return false;
215         }
216
217         return *_data == *jp->_data;
218 }
219
220 J2KImageProxy::J2KImageProxy (ArrayData data, dcp::Size size, AVPixelFormat pixel_format)
221         : _data (new ArrayData(data))
222         , _size (size)
223         , _pixel_format (pixel_format)
224         , _error (false)
225 {
226         /* ::image assumes 16bpp */
227         DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
228 }
229
230 size_t
231 J2KImageProxy::memory_used () const
232 {
233         size_t m = _data->size();
234         if (_image) {
235                 /* 3 components, 16-bits per pixel */
236                 m += 3 * 2 * _image->size().width * _image->size().height;
237         }
238         return m;
239 }