45 #include "compose.hpp"
54 using std::shared_ptr;
55 using boost::shared_array;
59 shared_ptr<dcp::OpenJPEGImage>
66 shared_ptr<dcp::OpenJPEGImage>
73 #ifdef LIBDCP_OPENJPEG2
78 ReadBuffer (uint8_t
const * data, int64_t size)
84 OPJ_SIZE_T read (
void* buffer, OPJ_SIZE_T nb_bytes)
86 int64_t N = min (nb_bytes, _size - _offset);
87 memcpy (buffer, _data + _offset, N);
93 uint8_t
const * _data;
100 read_function (
void* buffer, OPJ_SIZE_T nb_bytes,
void* data)
102 return reinterpret_cast<ReadBuffer*
>(data)->read (buffer, nb_bytes);
107 read_free_function (
void* data)
109 delete reinterpret_cast<ReadBuffer*
>(data);
114 decompress_error_callback (
char const * msg,
void *)
121 compress_error_callback (
char const * msg,
void *)
127 shared_ptr<dcp::OpenJPEGImage>
130 DCP_ASSERT (reduce >= 0);
132 uint8_t
const jp2_magic[] = {
143 auto format = OPJ_CODEC_J2K;
144 if (size >=
int (
sizeof (jp2_magic)) && memcmp (data, jp2_magic,
sizeof (jp2_magic)) == 0) {
145 format = OPJ_CODEC_JP2;
148 auto decoder = opj_create_decompress (format);
150 boost::throw_exception (
ReadError (
"could not create JPEG2000 decompresser"));
152 opj_dparameters_t parameters;
153 opj_set_default_decoder_parameters (¶meters);
154 parameters.cp_reduce = reduce;
155 opj_setup_decoder (decoder, ¶meters);
157 auto stream = opj_stream_default_create (OPJ_TRUE);
159 throw MiscError (
"could not create JPEG2000 stream");
162 opj_set_error_handler(decoder, decompress_error_callback, 00);
164 opj_stream_set_read_function (stream, read_function);
165 auto buffer =
new ReadBuffer (data, size);
166 opj_stream_set_user_data (stream, buffer, read_free_function);
167 opj_stream_set_user_data_length (stream, size);
169 opj_image_t* image = 0;
170 opj_read_header (stream, decoder, &image);
171 if (opj_decode (decoder, stream, image) == OPJ_FALSE) {
172 opj_destroy_codec (decoder);
173 opj_stream_destroy (stream);
174 if (format == OPJ_CODEC_J2K) {
175 boost::throw_exception (
ReadError (String::compose (
"could not decode JPEG2000 codestream of %1 bytes.", size)));
177 boost::throw_exception (
ReadError (String::compose (
"could not decode JP2 file of %1 bytes.", size)));
181 opj_destroy_codec (decoder);
182 opj_stream_destroy (stream);
184 image->x1 = rint (
float(image->x1) / pow (2.0f, reduce));
185 image->y1 = rint (
float(image->y1) / pow (2.0f, reduce));
186 return shared_ptr<OpenJPEGImage> (
new OpenJPEGImage (image));
192 #ifdef LIBDCP_OPENJPEG1
194 shared_ptr<dcp::OpenJPEGImage>
197 auto decoder = opj_create_decompress (CODEC_J2K);
198 opj_dparameters_t parameters;
199 opj_set_default_decoder_parameters (¶meters);
200 parameters.cp_reduce = reduce;
201 opj_setup_decoder (decoder, ¶meters);
202 auto cio = opj_cio_open ((opj_common_ptr) decoder, data, size);
203 auto image = opj_decode (decoder, cio);
205 opj_destroy_decompress (decoder);
207 boost::throw_exception (
ReadError (String::compose (
"could not decode JPEG2000 codestream of %1 bytes.", size)));
210 opj_destroy_decompress (decoder);
213 image->x1 = rint (
float(image->x1) / pow (2, reduce));
214 image->y1 = rint (
float(image->y1) / pow (2, reduce));
215 return shared_ptr<OpenJPEGImage> (
new OpenJPEGImage (image));
220 #ifdef LIBDCP_OPENJPEG2
226 #define MAX_J2K_SIZE (1024 * 1024 * 2)
228 : _data (shared_array<uint8_t>(new uint8_t[MAX_J2K_SIZE]), MAX_J2K_SIZE)
234 OPJ_SIZE_T write (
void* buffer, OPJ_SIZE_T nb_bytes)
236 DCP_ASSERT ((_offset + nb_bytes) < MAX_J2K_SIZE);
237 memcpy (_data.data() + _offset, buffer, nb_bytes);
239 if (_offset > OPJ_SIZE_T(_data.size())) {
240 _data.set_size (_offset);
245 OPJ_BOOL seek (OPJ_SIZE_T nb_bytes)
263 write_function (
void* buffer, OPJ_SIZE_T nb_bytes,
void* data)
265 return reinterpret_cast<WriteBuffer*
>(data)->write(buffer, nb_bytes);
270 write_free_function (
void* data)
272 delete reinterpret_cast<WriteBuffer*
>(data);
277 seek_function (OPJ_OFF_T nb_bytes,
void* data)
279 return reinterpret_cast<WriteBuffer*
>(data)->seek(nb_bytes);
285 dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz,
int bandwidth,
int frames_per_second,
bool threed,
bool fourk,
string comment)
288 auto encoder = opj_create_compress (OPJ_CODEC_J2K);
289 if (encoder ==
nullptr) {
290 throw MiscError (
"could not create JPEG2000 encoder");
293 if (comment.empty()) {
295 throw MiscError(
"compress_j2k comment can not be an empty string");
298 opj_set_error_handler (encoder, compress_error_callback, 0);
301 opj_cparameters_t parameters;
302 opj_set_default_encoder_parameters (¶meters);
304 parameters.numresolution = 7;
306 parameters.rsiz = fourk ? OPJ_PROFILE_CINEMA_4K : OPJ_PROFILE_CINEMA_2K;
307 parameters.cp_comment = strdup (comment.c_str());
310 parameters.max_cs_size = (bandwidth / 8) / frames_per_second;
313 parameters.max_cs_size /= 2;
315 parameters.max_comp_size = parameters.max_cs_size / 1.25;
316 parameters.tcp_numlayers = 1;
317 parameters.tcp_mct = 1;
318 parameters.numgbits = fourk ? 2 : 1;
321 opj_setup_encoder (encoder, ¶meters, xyz->opj_image());
323 auto stream = opj_stream_default_create (OPJ_FALSE);
325 opj_destroy_codec (encoder);
326 free (parameters.cp_comment);
327 throw MiscError (
"could not create JPEG2000 stream");
330 opj_stream_set_write_function (stream, write_function);
331 opj_stream_set_seek_function (stream, seek_function);
332 WriteBuffer* buffer =
new WriteBuffer ();
333 opj_stream_set_user_data (stream, buffer, write_free_function);
335 if (!opj_start_compress (encoder, xyz->opj_image(), stream)) {
336 opj_stream_destroy (stream);
337 opj_destroy_codec (encoder);
338 free (parameters.cp_comment);
339 if ((errno & 0x61500) == 0x61500) {
347 if (!opj_encode (encoder, stream)) {
348 opj_stream_destroy (stream);
349 opj_destroy_codec (encoder);
350 free (parameters.cp_comment);
351 throw MiscError (
"JPEG2000 encoding failed");
354 if (!opj_end_compress (encoder, stream)) {
355 opj_stream_destroy (stream);
356 opj_destroy_codec (encoder);
357 free (parameters.cp_comment);
358 throw MiscError (
"could not end JPEG2000 encoding");
363 opj_stream_destroy (stream);
364 opj_destroy_codec (encoder);
365 free (parameters.cp_comment);
373 #ifdef LIBDCP_OPENJPEG1
376 dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz,
int bandwidth,
int frames_per_second,
bool threed,
bool fourk)
379 int max_cs_len = ((float) bandwidth) / 8 / frames_per_second;
384 int const max_comp_size = max_cs_len / 1.25;
387 auto cinfo = opj_create_compress (CODEC_J2K);
388 if (cinfo ==
nullptr) {
389 throw MiscError (
"could not create JPEG2000 encoder");
393 opj_cparameters_t parameters;
394 opj_set_default_encoder_parameters (¶meters);
396 parameters.numresolution = 7;
400 parameters.tile_size_on =
false;
401 parameters.cp_tdx = 1;
402 parameters.cp_tdy = 1;
405 parameters.tp_flag =
'C';
406 parameters.tp_on = 1;
409 parameters.cp_tx0 = 0;
410 parameters.cp_ty0 = 0;
411 parameters.image_offset_x0 = 0;
412 parameters.image_offset_y0 = 0;
415 parameters.cblockw_init = 32;
416 parameters.cblockh_init = 32;
417 parameters.csty |= 0x01;
420 parameters.prog_order = CPRL;
423 parameters.roi_compno = -1;
425 parameters.subsampling_dx = 1;
426 parameters.subsampling_dy = 1;
429 parameters.irreversible = 1;
431 parameters.tcp_rates[0] = 0;
432 parameters.tcp_numlayers++;
433 parameters.cp_disto_alloc = 1;
434 parameters.cp_rsiz = fourk ? CINEMA4K : CINEMA2K;
436 parameters.numpocs = 2;
437 parameters.POC[0].tile = 1;
438 parameters.POC[0].resno0 = 0;
439 parameters.POC[0].compno0 = 0;
440 parameters.POC[0].layno1 = 1;
441 parameters.POC[0].resno1 = parameters.numresolution - 1;
442 parameters.POC[0].compno1 = 3;
443 parameters.POC[0].prg1 = CPRL;
444 parameters.POC[1].tile = 1;
445 parameters.POC[1].resno0 = parameters.numresolution - 1;
446 parameters.POC[1].compno0 = 0;
447 parameters.POC[1].layno1 = 1;
448 parameters.POC[1].resno1 = parameters.numresolution;
449 parameters.POC[1].compno1 = 3;
450 parameters.POC[1].prg1 = CPRL;
453 parameters.cp_comment = strdup (
"libdcp");
454 parameters.cp_cinema = fourk ? CINEMA4K_24 : CINEMA2K_24;
457 parameters.tcp_mct = 1;
460 parameters.max_comp_size = max_comp_size;
461 parameters.tcp_rates[0] = ((float) (3 * xyz->size().width * xyz->size().height * 12)) / (max_cs_len * 8);
464 cinfo->event_mgr = 0;
467 opj_setup_encoder (cinfo, ¶meters, xyz->opj_image());
469 auto cio = opj_cio_open ((opj_common_ptr) cinfo, 0, 0);
470 if (cio ==
nullptr) {
471 opj_destroy_compress (cinfo);
472 throw MiscError (
"could not open JPEG2000 stream");
475 int const r = opj_encode (cinfo, cio, xyz->opj_image(), 0);
478 opj_destroy_compress (cinfo);
479 throw MiscError (
"JPEG2000 encoding failed");
482 ArrayData enc (cio->buffer, cio_tell (cio));
485 free (parameters.cp_comment);
486 opj_destroy_compress (cinfo);
Class to hold an arbitrary block of data.
An error that occurs during decompression of JPEG2000 data.
A miscellaneous exception.
A wrapper of libopenjpeg's opj_image_t.
Any error that occurs when reading data from a DCP.
Exceptions thrown by libdcp.
Methods to encode and decode JPEG2000.
Namespace for everything in libdcp.
ArrayData compress_j2k(std::shared_ptr< const OpenJPEGImage >, int bandwith, int frames_per_second, bool threed, bool fourk, std::string comment="libdcp")
std::shared_ptr< OpenJPEGImage > decompress_j2k(uint8_t const *data, int64_t size, int reduce)