Add compress_j2k method and simple benchmark.
authorCarl Hetherington <cth@carlh.net>
Sat, 28 Nov 2015 13:15:37 +0000 (13:15 +0000)
committerCarl Hetherington <cth@carlh.net>
Sat, 28 Nov 2015 13:15:37 +0000 (13:15 +0000)
run/bench [new file with mode: 0755]
src/data.cc
src/data.h
src/util.cc
src/util.h
src/version.h
test/bench.cc [new file with mode: 0644]
test/wscript
wscript

diff --git a/run/bench b/run/bench
new file mode 100755 (executable)
index 0000000..b8ed060
--- /dev/null
+++ b/run/bench
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+# Private test data; this is stuff that is non-distributable
+private=../libdcp1-test-private
+
+export LD_LIBRARY_PATH=build/src:build/asdcplib/src:$LD_LIBRARY_PATH
+build/test/bench $private
index a326ed3b7e3b3fcb2dc958425ed55b368679b075..66970c91b4f1b7f72e208041ba2b42ab08bdeb3d 100644 (file)
@@ -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);
+}
index 0b746052a6e23cb45b21fa27bb10650c510cf36a..12949fe44fa346a0c6ffeda76c590eeb3bdca762 100644 (file)
@@ -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
index 856bc9bd0001d0d958cbae4334df0824b6cc0a25..0b8cf6638977e10144746ade170671032f34afb4 100644 (file)
@@ -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.
  */
index a56b1f4d10b43df7778c3bdbddda2ba2fbf84403..da1844cb539784df46fa3f0cac0d34d4b648de5b 100644 (file)
@@ -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 ();
index bd5013821cdf742f09de56d51b8e7dbe68adbdc3..407b966d22cb7ca3b710263d78fc9e2a4c3552d1 100644 (file)
@@ -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;
 
 }
diff --git a/test/bench.cc b/test/bench.cc
new file mode 100644 (file)
index 0000000..c3f7dd1
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+    Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "data.h"
+#include "test.h"
+#include "util.h"
+#include "version.h"
+#include <sys/time.h>
+#include <iostream>
+
+using std::cout;
+using std::cerr;
+using boost::shared_ptr;
+
+/** Run some basic benchmarks of JPEG2000 encoding / decoding */
+int
+main (int argc, char* argv[])
+{
+       if (argc < 2) {
+               cerr << "Syntax: " << argv[0] << " private-test-path\n";
+               exit (EXIT_FAILURE);
+       }
+
+       int const decompress_count = 100;
+       int const compress_count = 100;
+       int const j2k_bandwidth = 100000000;
+
+       struct timeval start;
+       dcp::Data j2k (boost::filesystem::path (argv[1]) / "thx.j2c");
+
+       gettimeofday (&start, 0);
+
+       shared_ptr<dcp::OpenJPEGImage> xyz;
+       for (int i = 0; i < decompress_count; ++i) {
+               xyz = dcp::decompress_j2k (j2k.data.get(), j2k.size, 0);
+       }
+
+       struct timeval stop;
+       gettimeofday (&stop, 0);
+
+       double start_seconds = start.tv_sec + double(start.tv_usec) / 1000000;
+       double stop_seconds = stop.tv_sec + double(start.tv_usec) / 1000000;
+       if (dcp::built_with_debug) {
+               cout << "Decompress (debug build): ";
+       } else {
+               cout << "Decompress: ";
+       }
+       cout << decompress_count / (stop_seconds - start_seconds) << " fps.\n";
+
+       gettimeofday (&start, 0);
+
+       for (int i = 0; i < compress_count; ++i) {
+               dcp::compress_j2k (xyz, j2k_bandwidth, 24, false, false);
+       }
+
+       gettimeofday (&stop, 0);
+
+       start_seconds = start.tv_sec + double(start.tv_usec) / 1000000;
+       stop_seconds = stop.tv_sec + double(start.tv_usec) / 1000000;
+       if (dcp::built_with_debug) {
+               cout << "Compress (debug build) ";
+       } else {
+               cout << "Compress ";
+       }
+
+       cout << (j2k_bandwidth / 1000000) << "Mbps: "
+            << compress_count / (stop_seconds - start_seconds) << " fps.";
+}
index 707b776fc78b8499fc103b28369f37b55ba39753..84c21a93127bb2bfd411820ee61aad367a0cd6de 100644 (file)
@@ -90,3 +90,11 @@ def build(bld):
     obj.source = 'rewrite_subs.cc'
     obj.target = 'rewrite_subs'
     obj.install_path = ''
+
+    obj = bld(features='cxx cxxprogram')
+    obj.name   = 'bench'
+    obj.uselib = 'BOOST_FILESYSTEM OPENJPEG CXML'
+    obj.use = 'libdcp%s' % bld.env.API_VERSION
+    obj.source = 'bench.cc'
+    obj.target = 'bench'
+    obj.install_path = ''
diff --git a/wscript b/wscript
index 3c475848edc906c92d3b3835ce257d997008040e..96eeffcbfde32c44901017f5fe20e57d7dcff40b 100644 (file)
--- a/wscript
+++ b/wscript
@@ -171,7 +171,7 @@ def create_version_cc(bld, version):
             debug_string = 'true'
         else:
             debug_string = 'false'
-        text += 'bool const built_with_debug = %s;\n' % debug_string
+        text += 'bool const dcp::built_with_debug = %s;\n' % debug_string
         print('Writing version information to src/version.cc')
         o = open('src/version.cc', 'w')
         o.write(text)