2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "j2k_image_proxy.h"
22 #include "dcpomatic_socket.h"
24 #include "dcpomatic_assert.h"
25 #include <dcp/raw_convert.h>
26 #include <dcp/openjpeg_image.h>
27 #include <dcp/mono_picture_frame.h>
28 #include <dcp/stereo_picture_frame.h>
29 #include <dcp/colour_conversion.h>
30 #include <dcp/rgb_xyz.h>
32 #include <libcxml/cxml.h>
33 #include <libxml++/libxml++.h>
43 using boost::shared_ptr;
44 using boost::optional;
45 using boost::dynamic_pointer_cast;
47 using dcp::raw_convert;
49 /** Construct a J2KImageProxy from a JPEG2000 file */
50 J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size, AVPixelFormat pixel_format)
53 , _pixel_format (pixel_format)
55 /* ::image assumes 16bpp */
56 DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
59 J2KImageProxy::J2KImageProxy (
60 shared_ptr<const dcp::MonoPictureFrame> frame,
62 AVPixelFormat pixel_format,
63 optional<int> forced_reduction
65 : _data (frame->j2k_size ())
67 , _pixel_format (pixel_format)
68 , _forced_reduction (forced_reduction)
70 /* ::image assumes 16bpp */
71 DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
72 memcpy (_data.data().get(), frame->j2k_data(), _data.size ());
75 J2KImageProxy::J2KImageProxy (
76 shared_ptr<const dcp::StereoPictureFrame> frame,
79 AVPixelFormat pixel_format,
80 optional<int> forced_reduction
84 , _pixel_format (pixel_format)
85 , _forced_reduction (forced_reduction)
87 /* ::image assumes 16bpp */
88 DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
91 _data = Data (frame->left_j2k_size ());
92 memcpy (_data.data().get(), frame->left_j2k_data(), _data.size ());
95 _data = Data (frame->right_j2k_size ());
96 memcpy (_data.data().get(), frame->right_j2k_data(), _data.size ());
101 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
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"));
107 _data = Data (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.
112 _pixel_format = AV_PIX_FMT_XYZ12LE;
113 socket->read (_data.data().get (), _data.size ());
117 J2KImageProxy::prepare (optional<dcp::Size> target_size) const
119 boost::mutex::scoped_lock lm (_mutex);
121 if (_image && target_size == _target_size) {
122 DCPOMATIC_ASSERT (_reduce);
128 if (_forced_reduction) {
129 reduce = *_forced_reduction;
131 while (target_size && (_size.width / pow(2, reduce)) > target_size->width && (_size.height / pow(2, reduce)) > target_size->height) {
136 reduce = max (0, reduce);
139 shared_ptr<dcp::OpenJPEGImage> decompressed = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), reduce);
141 /* When scaling JPEG2000 images (using AV_PIX_FMT_XYZ12LE) ffmpeg will call xyz12ToRgb48 which reads data
142 from the whole of the image stride. If we are cropping, Image::crop_scale_window munges the
143 start addresses of each image row (to do the crop) but keeps the stride the same. This means
144 that under crop we will read over the end of the image by the amount of the crop. To allow this
145 to happen without invalid memory access we need to overallocate by one whole stride's worth of pixels.
147 _image.reset (new Image (_pixel_format, decompressed->size(), true, decompressed->size().width));
149 int const shift = 16 - decompressed->precision (0);
151 /* Copy data in whatever format (sRGB or XYZ) into our Image; I'm assuming
152 the data is 12-bit either way.
155 int const width = decompressed->size().width;
158 int* decomp_0 = decompressed->data (0);
159 int* decomp_1 = decompressed->data (1);
160 int* decomp_2 = decompressed->data (2);
161 for (int y = 0; y < decompressed->size().height; ++y) {
162 uint16_t* q = (uint16_t *) (_image->data()[0] + y * _image->stride()[0]);
163 for (int x = 0; x < width; ++x) {
164 *q++ = decomp_0[p] << shift;
165 *q++ = decomp_1[p] << shift;
166 *q++ = decomp_2[p] << shift;
171 _target_size = target_size;
177 pair<shared_ptr<Image>, int>
178 J2KImageProxy::image (optional<dcp::Size> target_size) const
180 int const r = prepare (target_size);
181 /* I think this is safe without a lock on mutex. _image is guaranteed to be
182 set up when prepare() has happened.
184 return make_pair (_image, r);
188 J2KImageProxy::add_metadata (xmlpp::Node* node) const
190 node->add_child("Type")->add_child_text (N_("J2K"));
191 node->add_child("Width")->add_child_text (raw_convert<string> (_size.width));
192 node->add_child("Height")->add_child_text (raw_convert<string> (_size.height));
194 node->add_child("Eye")->add_child_text (raw_convert<string> (static_cast<int> (_eye.get ())));
196 node->add_child("Size")->add_child_text (raw_convert<string> (_data.size ()));
200 J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
202 socket->write (_data.data().get(), _data.size());
206 J2KImageProxy::same (shared_ptr<const ImageProxy> other) const
208 shared_ptr<const J2KImageProxy> jp = dynamic_pointer_cast<const J2KImageProxy> (other);
213 if (_data.size() != jp->_data.size()) {
217 return memcmp (_data.data().get(), jp->_data.data().get(), _data.size()) == 0;
220 J2KImageProxy::J2KImageProxy (Data data, dcp::Size size, AVPixelFormat pixel_format)
223 , _pixel_format (pixel_format)
225 /* ::image assumes 16bpp */
226 DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
230 J2KImageProxy::memory_used () const
232 size_t m = _data.size();
234 /* 3 components, 16-bits per pixel */
235 m += 3 * 2 * _image->size().width * _image->size().height;