diff options
| author | Carl Hetherington <cth@carlh.net> | 2015-11-28 13:15:37 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2015-11-28 13:15:37 +0000 |
| commit | 8ccf9e982971a8eca1027ad5c04d837b6ad068dc (patch) | |
| tree | f1f4785df95a266a7d1a511729e379913096f57a | |
| parent | 257b936e9ed24043cfc1ed6e7e762858250bc388 (diff) | |
Add compress_j2k method and simple benchmark.
| -rwxr-xr-x | run/bench | 7 | ||||
| -rw-r--r-- | src/data.cc | 7 | ||||
| -rw-r--r-- | src/data.h | 7 | ||||
| -rw-r--r-- | src/util.cc | 113 | ||||
| -rw-r--r-- | src/util.h | 2 | ||||
| -rw-r--r-- | src/version.h | 2 | ||||
| -rw-r--r-- | test/bench.cc | 84 | ||||
| -rw-r--r-- | test/wscript | 8 | ||||
| -rw-r--r-- | wscript | 2 |
9 files changed, 230 insertions, 2 deletions
diff --git a/run/bench b/run/bench new file mode 100755 index 00000000..b8ed0605 --- /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 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); +} @@ -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 (¶meters); + + /* 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, ¶meters, 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. */ @@ -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; } diff --git a/test/bench.cc b/test/bench.cc new file mode 100644 index 00000000..c3f7dd1e --- /dev/null +++ b/test/bench.cc @@ -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."; +} diff --git a/test/wscript b/test/wscript index 707b776f..84c21a93 100644 --- a/test/wscript +++ b/test/wscript @@ -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 = '' @@ -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) |
