4834a25893001fea09cb93bb2058e6a3ad6dd8bb
[dcpomatic.git] / src / lib / poznan_encoder.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "poznan_encoder.h"
21 #include "exceptions.h"
22 #include <dlfcn.h>
23
24 #include "i18n.h"
25
26 using std::string;
27 using boost::shared_ptr;
28
29 PoznanEncoder::PoznanEncoder ()
30 {
31         void* config = open_library ("config");
32         void* preprocessing = open_library ("preprocessing");
33         void* dwt = open_library ("dwt");
34         void* tier1 = open_library ("tier1");
35         void* gpu_coeff_coder = open_library ("gpu_coeff_coder");
36         void* tier2 = open_library ("tier2");
37         
38         _init_device = (void (*)(type_parameters *)) dlsym (config, "init_device");
39         _mct = (void (*)(type_image *, type_parameters *)) dlsym (preprocessing, "mct");
40         _fwt = (void (*)(type_tile *)) dlsym (dwt, "fwt");
41         _quantize_tile = (void (*)(type_tile *)) dlsym (tier1, "quantize_tile");
42         _encode_tile = (void (*)(type_tile *)) dlsym (gpu_coeff_coder, "encode_tile");
43         _write_codestream = (void (*)(type_image *)) dlsym (tier2, "write_codestream");
44         
45         if (!_init_device || !_mct || !_fwt || !_quantize_tile || !_encode_tile || !_write_codestream) {
46                 throw JPEG2000EncoderUnavailableException (name(), "missing symbol");
47         }
48 }
49
50 void *
51 PoznanEncoder::open_library (string library_name)
52 {
53         /* XXX: need cross-platform implementation of dlopen etc. */
54         
55         library_name = "libdcpomatic-" + library_name + ".so";
56         void* lib = dlopen (library_name.c_str(), RTLD_LAZY | RTLD_GLOBAL);
57         if (!lib) {
58                 throw JPEG2000EncoderUnavailableException (name(), "could not find " + library_name);
59         }
60         return lib;
61 }
62
63 void
64 PoznanEncoder::parameters_changed ()
65 {
66         /* One tile which covers entire image */
67         _param.param_tile_w = -1;
68         _param.param_tile_h = -1;
69
70         /* Wavelet decomposition levels */
71         _param.param_tile_comp_dlvls = _resolution.get() == RESOLUTION_2K ? 5 : 6;
72
73         /* Power of 2 for maximum codeblock size */
74         _param.param_cblk_exp_w = 5;
75         _param.param_cblk_exp_h = 5;
76
77         /* DWT 9/7 transform */
78         _param.param_wavelet_type = 1;
79
80         /* Use MCT */
81         _param.param_use_mct = 1;
82
83         /* Device to run on */
84         _param.param_device = 0;
85
86         /* Target file size */
87         _param.param_target_size = (_bandwidth.get() / _frame_rate.get()) / 8;
88         if (_threed.get ()) {
89                 _param.param_target_size /= 2;
90         }
91
92         /* Bits per pixel per component */
93         _param.param_bp = 12;
94
95         /* Don't know about these: use the defaults */
96         _param.param_use_part2_mct = 0;
97         _param.param_mct_compression_method = 0;
98         _param.param_mct_klt_iterations = 10000;
99         _param.param_mct_klt_border_eigenvalue = 0.000001;
100         _param.param_mct_klt_err = 1.0e-7;
101         
102         _init_device (&_param);
103 }
104
105 string
106 PoznanEncoder::name () const
107 {
108         return _("CUDA (GPU) encoder (Poznan Supercomputing and Networking Center)");
109 }
110
111 shared_ptr<EncodedData>
112 PoznanEncoder::do_encode (shared_ptr<const dcp::XYZImage> input, dcp::NoteHandler note_handler)
113 {
114         type_image img;
115         _mct (&img, &_param);
116         for (size_t i = 0; i < img.num_tiles; ++i) {
117                 type_tile* tile = &(img.tile[i]);
118                 _fwt (tile);
119                 _quantize_tile (tile);
120                 _encode_tile (tile);
121         }
122         _write_codestream (&img);
123         return shared_ptr<EncodedData> ();
124 }