a875c23d70406535a493439272599ea6f98661dc
[dcpomatic.git] / src / lib / gpu_j2k_encode_worker.cc
1 /*
2     Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "gpu_j2k_encode_worker.h"
22 #include "dcp_video.h"
23 #include "cross.h"
24 #include "dcpomatic_log.h"
25 #include <dcp/openjpeg_image.h>
26 extern "C" {
27 #include <poznanj2k/config/init_device.h>
28 #include <poznanj2k/types/image_types.h>
29 #include <poznanj2k/types/image.h>
30 #include <poznanj2k/preprocessing/mct.h>
31 #include <poznanj2k/dwt/dwt.h>
32 #include <poznanj2k/tier1/quantizer.h>
33 #include <poznanj2k/tier1/coeff_coder/gpu_coder.h>
34 #include <poznanj2k/tier2/codestream.h>
35 #include <poznanj2k/misc/memory_management.cuh>
36 #include <cuda_runtime_api.h>
37 }
38
39 #include "i18n.h"
40
41 using dcp::Data;
42 using boost::optional;
43 using boost::shared_ptr;
44
45 GPUJ2KEncodeWorker::GPUJ2KEncodeWorker ()
46 {
47         init_device (0);
48 }
49
50 optional<Data>
51 GPUJ2KEncodeWorker::encode (shared_ptr<DCPVideo> vf)
52 {
53         shared_ptr<dcp::OpenJPEGImage> image = DCPVideo::convert_to_xyz(vf->frame(), boost::bind(&Log::dcp_log, dcpomatic_log.get(), _1, _2));
54         int const width = image->size().width;
55         int const height = image->size().height;
56
57         type_image img;
58         img.mct_compression_method = 0;
59         img.width = width;
60         img.height = height;
61         img.num_components = 3;
62         img.depth = 36;
63         img.sign = UNSIGNED;
64         /* XXX: 6 for 4K? */
65         img.num_dlvls = 5;
66         img.wavelet_type = 1;
67         img.num_tiles = 1;
68         img.tile_w = width;
69         img.tile_h = height;
70         img.coding_style = CODING_STYLE_PRECINCTS_DEFINED;
71         img.prog_order = COMP_POS_RES_LY_PROG;
72         img.num_layers = 1;
73         img.num_range_bits = 12;
74         img.use_mct = 1;
75         img.use_part2_mct = 0;
76
77         set_coding_parameters (&img, (vf->j2k_bandwidth() / 8) / vf->frames_per_second());
78
79         init_tiles (&img, width, height, 5, 5);
80         type_tile* tile = &(img.tile[0]);
81
82         // XXX: it's a big shame about this int -> float conversion
83         for (int i = 0; i < 3; ++i) {
84                 type_tile_comp* c = &tile->tile_comp[i];
85                 c->tile_comp_no = i;
86                 int const pixels = c->width * c->height;
87                 for (int j = 0; j < pixels; ++j) {
88                         c->img_data[j] = float (image->data(i)[j]);
89                 }
90                 cuda_memcpy_htd (c->img_data, c->img_data_d, pixels * sizeof(type_data));
91         }
92
93         mct (&img, 10000, 0.000001, 1.0e-7);
94         fwt (tile);
95         quantize_tile (tile);
96         encode_tile (tile);
97
98         type_buffer buffer;
99         init_buffer (&buffer);
100         encode_codestream (&buffer, &img);
101         cudaThreadSynchronize ();
102
103         image_destroy(&img);
104
105         // XXX: remove this memcpy
106         dcp::Data encoded (buffer.bytes_count);
107         memcpy (encoded.data().get(), buffer.data, buffer.bytes_count);
108         free (buffer.data);
109         return encoded;
110 }
111
112 void
113 GPUJ2KEncodeWorker::log_thread_start ()
114 {
115         LOG_TIMING ("start-encoder-thread thread=%1 GPU", thread_id());
116 }