2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
22 #include "exceptions.h"
23 #include "openjpeg_image.h"
25 #include "dcp_assert.h"
26 #include "compose.hpp"
33 using boost::shared_ptr;
34 using boost::shared_array;
37 shared_ptr<dcp::OpenJPEGImage>
38 dcp::decompress_j2k (Data data, int reduce)
40 return dcp::decompress_j2k (data.data().get(), data.size(), reduce);
46 ReadBuffer (uint8_t* data, int64_t size)
52 OPJ_SIZE_T read (void* buffer, OPJ_SIZE_T nb_bytes)
54 int64_t N = min (nb_bytes, _size - _offset);
55 memcpy (buffer, _data + _offset, N);
67 read_function (void* buffer, OPJ_SIZE_T nb_bytes, void* data)
69 return reinterpret_cast<ReadBuffer*>(data)->read (buffer, nb_bytes);
73 read_free_function (void* data)
75 delete reinterpret_cast<ReadBuffer*>(data);
78 /** Decompress a JPEG2000 image to a bitmap.
79 * @param data JPEG2000 data.
80 * @param size Size of data in bytes.
81 * @param reduce A power of 2 by which to reduce the size of the decoded image;
82 * e.g. 0 reduces by (2^0 == 1), ie keeping the same size.
83 * 1 reduces by (2^1 == 2), ie halving the size of the image.
84 * This is useful for scaling 4K DCP images down to 2K.
85 * @return OpenJPEGImage.
87 shared_ptr<dcp::OpenJPEGImage>
88 dcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
90 uint8_t const jp2_magic[] = {
101 OPJ_CODEC_FORMAT format = OPJ_CODEC_J2K;
102 if (size >= int (sizeof (jp2_magic)) && memcmp (data, jp2_magic, sizeof (jp2_magic)) == 0) {
103 format = OPJ_CODEC_JP2;
106 opj_codec_t* decoder = opj_create_decompress (format);
108 boost::throw_exception (DCPReadError ("could not create JPEG2000 decompresser"));
110 opj_dparameters_t parameters;
111 opj_set_default_decoder_parameters (¶meters);
112 parameters.cp_reduce = reduce;
113 opj_setup_decoder (decoder, ¶meters);
115 opj_stream_t* stream = opj_stream_default_create (OPJ_TRUE);
117 throw MiscError ("could not create JPEG2000 stream");
120 opj_stream_set_read_function (stream, read_function);
121 ReadBuffer* buffer = new ReadBuffer (data, size);
122 opj_stream_set_user_data (stream, buffer, read_free_function);
123 opj_stream_set_user_data_length (stream, size);
125 opj_image_t* image = 0;
126 opj_read_header (stream, decoder, &image);
127 if (opj_decode (decoder, stream, image) == OPJ_FALSE) {
128 opj_destroy_codec (decoder);
129 opj_stream_destroy (stream);
130 if (format == OPJ_CODEC_J2K) {
131 boost::throw_exception (DCPReadError (String::compose ("could not decode JPEG2000 codestream of %1 bytes.", size)));
133 boost::throw_exception (DCPReadError (String::compose ("could not decode JP2 file of %1 bytes.", size)));
137 opj_destroy_codec (decoder);
138 opj_stream_destroy (stream);
140 image->x1 = rint (float(image->x1) / pow (2.0f, reduce));
141 image->y1 = rint (float(image->y1) / pow (2.0f, reduce));
142 return shared_ptr<OpenJPEGImage> (new OpenJPEGImage (image));
148 /* XXX: is there a better strategy for this? */
149 #define MAX_J2K_SIZE (1024 * 1024 * 2)
151 : _data (shared_array<uint8_t> (new uint8_t[MAX_J2K_SIZE]), MAX_J2K_SIZE)
157 OPJ_SIZE_T write (void* buffer, OPJ_SIZE_T nb_bytes)
159 DCP_ASSERT ((_offset + nb_bytes) < MAX_J2K_SIZE);
160 memcpy (_data.data().get() + _offset, buffer, nb_bytes);
162 if (_offset > OPJ_SIZE_T (_data.size())) {
163 _data.set_size (_offset);
168 OPJ_BOOL seek (OPJ_SIZE_T nb_bytes)
185 write_function (void* buffer, OPJ_SIZE_T nb_bytes, void* data)
187 return reinterpret_cast<WriteBuffer*>(data)->write (buffer, nb_bytes);
191 write_free_function (void* data)
193 delete reinterpret_cast<WriteBuffer*>(data);
197 seek_function (OPJ_OFF_T nb_bytes, void* data)
199 return reinterpret_cast<WriteBuffer*>(data)->seek (nb_bytes);
203 error_callback (char const * msg, void *)
205 throw MiscError (msg);
209 dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frames_per_second, bool threed, bool fourk)
211 /* get a J2K compressor handle */
212 opj_codec_t* encoder = opj_create_compress (OPJ_CODEC_J2K);
214 throw MiscError ("could not create JPEG2000 encoder");
217 opj_set_error_handler (encoder, error_callback, 0);
219 /* Set encoding parameters to default values */
220 opj_cparameters_t parameters;
221 opj_set_default_encoder_parameters (¶meters);
222 parameters.rsiz = fourk ? OPJ_PROFILE_CINEMA_4K : OPJ_PROFILE_CINEMA_2K;
223 parameters.cp_comment = strdup ("libdcp");
226 parameters.max_cs_size = (bandwidth / 8) / frames_per_second;
228 /* In 3D we have only half the normal bandwidth per eye */
229 parameters.max_cs_size /= 2;
231 parameters.max_comp_size = parameters.max_cs_size / 1.25;
232 parameters.tcp_numlayers = 1;
233 parameters.tcp_mct = 1;
235 /* Setup the encoder parameters using the current image and user parameters */
236 opj_setup_encoder (encoder, ¶meters, xyz->opj_image());
238 opj_stream_t* stream = opj_stream_default_create (OPJ_FALSE);
240 throw MiscError ("could not create JPEG2000 stream");
243 opj_stream_set_write_function (stream, write_function);
244 opj_stream_set_seek_function (stream, seek_function);
245 WriteBuffer* buffer = new WriteBuffer ();
246 opj_stream_set_user_data (stream, buffer, write_free_function);
248 if (!opj_start_compress (encoder, xyz->opj_image(), stream)) {
249 throw MiscError ("could not start JPEG2000 encoding");
252 if (!opj_encode (encoder, stream)) {
253 opj_destroy_codec (encoder);
254 opj_stream_destroy (stream);
255 throw MiscError ("JPEG2000 encoding failed");
258 if (!opj_end_compress (encoder, stream)) {
259 opj_destroy_codec (encoder);
260 opj_stream_destroy (stream);
261 throw MiscError ("could not end JPEG2000 encoding");
264 Data enc (buffer->data ());
266 free (parameters.cp_comment);
267 opj_destroy_codec (encoder);
268 opj_stream_destroy (stream);