summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2015-11-28 13:15:37 +0000
committerCarl Hetherington <cth@carlh.net>2015-11-28 13:15:37 +0000
commit8ccf9e982971a8eca1027ad5c04d837b6ad068dc (patch)
treef1f4785df95a266a7d1a511729e379913096f57a /src
parent257b936e9ed24043cfc1ed6e7e762858250bc388 (diff)
Add compress_j2k method and simple benchmark.
Diffstat (limited to 'src')
-rw-r--r--src/data.cc7
-rw-r--r--src/data.h7
-rw-r--r--src/util.cc113
-rw-r--r--src/util.h2
-rw-r--r--src/version.h2
5 files changed, 130 insertions, 1 deletions
diff --git a/src/data.cc b/src/data.cc
index a326ed3b..66970c91 100644
--- a/src/data.cc
+++ b/src/data.cc
@@ -47,3 +47,10 @@ Data::Data (boost::filesystem::path file)
throw FileError ("could not read file", file, -1);
}
}
+
+Data::Data (uint8_t const * data_, boost::uintmax_t size_)
+ : data (new uint8_t[size])
+ , size (size_)
+{
+ memcpy (data.get(), data_, size);
+}
diff --git a/src/data.h b/src/data.h
index 0b746052..12949fe4 100644
--- a/src/data.h
+++ b/src/data.h
@@ -17,6 +17,9 @@
*/
+#ifndef LIBDCP_DATA_H
+#define LIBDCP_DATA_H
+
/** @file src/data.h
* @brief Data class.
*/
@@ -38,6 +41,8 @@ public:
, size (size_)
{}
+ Data (uint8_t const * data, boost::uintmax_t size_);
+
Data (boost::filesystem::path file);
boost::shared_array<uint8_t> data;
@@ -45,3 +50,5 @@ public:
};
}
+
+#endif
diff --git a/src/util.cc b/src/util.cc
index 856bc9bd..0b8cf663 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -252,6 +252,119 @@ dcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
return shared_ptr<OpenJPEGImage> (new OpenJPEGImage (image));
}
+Data
+dcp::compress_j2k (shared_ptr<OpenJPEGImage> xyz, int bandwidth, int frames_per_second, bool threed, bool fourk)
+{
+ /* Set the max image and component sizes based on frame_rate */
+ int max_cs_len = ((float) bandwidth) / 8 / frames_per_second;
+ if (threed) {
+ /* In 3D we have only half the normal bandwidth per eye */
+ max_cs_len /= 2;
+ }
+ int const max_comp_size = max_cs_len / 1.25;
+
+ /* get a J2K compressor handle */
+ opj_cinfo_t* cinfo = opj_create_compress (CODEC_J2K);
+ if (cinfo == 0) {
+ throw MiscError ("could not create JPEG2000 encoder");
+ }
+
+ /* Set encoding parameters to default values */
+ opj_cparameters_t parameters;
+ opj_set_default_encoder_parameters (&parameters);
+
+ /* Set default cinema parameters */
+ parameters.tile_size_on = false;
+ parameters.cp_tdx = 1;
+ parameters.cp_tdy = 1;
+
+ /* Tile part */
+ parameters.tp_flag = 'C';
+ parameters.tp_on = 1;
+
+ /* Tile and Image shall be at (0,0) */
+ parameters.cp_tx0 = 0;
+ parameters.cp_ty0 = 0;
+ parameters.image_offset_x0 = 0;
+ parameters.image_offset_y0 = 0;
+
+ /* Codeblock size = 32x32 */
+ parameters.cblockw_init = 32;
+ parameters.cblockh_init = 32;
+ parameters.csty |= 0x01;
+
+ /* The progression order shall be CPRL */
+ parameters.prog_order = CPRL;
+
+ /* No ROI */
+ parameters.roi_compno = -1;
+
+ parameters.subsampling_dx = 1;
+ parameters.subsampling_dy = 1;
+
+ /* 9-7 transform */
+ parameters.irreversible = 1;
+
+ parameters.tcp_rates[0] = 0;
+ parameters.tcp_numlayers++;
+ parameters.cp_disto_alloc = 1;
+ parameters.cp_rsiz = fourk ? CINEMA4K : CINEMA2K;
+ if (fourk) {
+ parameters.numpocs = 2;
+ parameters.POC[0].tile = 1;
+ parameters.POC[0].resno0 = 0;
+ parameters.POC[0].compno0 = 0;
+ parameters.POC[0].layno1 = 1;
+ parameters.POC[0].resno1 = parameters.numresolution - 1;
+ parameters.POC[0].compno1 = 3;
+ parameters.POC[0].prg1 = CPRL;
+ parameters.POC[1].tile = 1;
+ parameters.POC[1].resno0 = parameters.numresolution - 1;
+ parameters.POC[1].compno0 = 0;
+ parameters.POC[1].layno1 = 1;
+ parameters.POC[1].resno1 = parameters.numresolution;
+ parameters.POC[1].compno1 = 3;
+ parameters.POC[1].prg1 = CPRL;
+ }
+
+ parameters.cp_comment = strdup ("libdcp");
+ parameters.cp_cinema = fourk ? CINEMA4K_24 : CINEMA2K_24;
+
+ /* 3 components, so use MCT */
+ parameters.tcp_mct = 1;
+
+ /* set max image */
+ parameters.max_comp_size = max_comp_size;
+ parameters.tcp_rates[0] = ((float) (3 * xyz->size().width * xyz->size().height * 12)) / (max_cs_len * 8);
+
+ /* Set event manager to null (openjpeg 1.3 bug) */
+ cinfo->event_mgr = 0;
+
+ /* Setup the encoder parameters using the current image and user parameters */
+ opj_setup_encoder (cinfo, &parameters, xyz->opj_image ());
+
+ opj_cio_t* cio = opj_cio_open ((opj_common_ptr) cinfo, 0, 0);
+ if (cio == 0) {
+ opj_destroy_compress (cinfo);
+ throw MiscError ("could not open JPEG2000 stream");
+ }
+
+ int const r = opj_encode (cinfo, cio, xyz->opj_image(), 0);
+ if (r == 0) {
+ opj_cio_close (cio);
+ opj_destroy_compress (cinfo);
+ throw MiscError ("JPEG2000 encoding failed");
+ }
+
+ Data enc (cio->buffer, cio_tell (cio));
+
+ opj_cio_close (cio);
+ free (parameters.cp_comment);
+ opj_destroy_compress (cinfo);
+
+ return enc;
+}
+
/** @param s A string.
* @return true if the string contains only space, newline or tab characters, or is empty.
*/
diff --git a/src/util.h b/src/util.h
index a56b1f4d..da1844cb 100644
--- a/src/util.h
+++ b/src/util.h
@@ -25,6 +25,7 @@
*/
#include "types.h"
+#include "data.h"
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/filesystem.hpp>
@@ -54,6 +55,7 @@ extern std::string content_kind_to_string (ContentKind kind);
extern ContentKind content_kind_from_string (std::string kind);
extern bool empty_or_white_space (std::string s);
extern boost::shared_ptr<OpenJPEGImage> decompress_j2k (uint8_t* data, int64_t size, int reduce);
+extern Data compress_j2k (boost::shared_ptr<OpenJPEGImage>, int bandwith, int frames_per_second, bool threed, bool fourk);
extern bool ids_equal (std::string a, std::string b);
extern void init ();
diff --git a/src/version.h b/src/version.h
index bd501382..407b966d 100644
--- a/src/version.h
+++ b/src/version.h
@@ -3,6 +3,6 @@ namespace dcp {
extern char const * version;
extern char const * git_commit;
-extern bool built_with_debug;
+extern bool const built_with_debug;
}