2 Copyright (C) 2022 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
23 #include "cuda_j2k_frame_encoder.h"
24 #include "dcpomatic_log.h"
25 #include "dcp_video.h"
26 #include "exceptions.h"
27 #include "player_video.h"
28 #include <dcp/openjpeg_image.h>
35 using boost::optional;
38 CUDAJ2KFrameEncoder::CUDAJ2KFrameEncoder()
40 nvjpeg2kEncoderCreateSimple(&_encoder_handle);
41 nvjpeg2kEncodeStateCreate(_encoder_handle, &_encoder_state);
42 nvjpeg2kEncodeParamsCreate(&_encoder_params);
44 cudaStreamCreateWithFlags(&_stream, cudaStreamNonBlocking);
48 CUDAJ2KFrameEncoder::Input::Input(DCPVideo const& vf)
52 auto xyz = convert_to_xyz(vf.frame(), boost::bind(&Log::dcp_log, dcpomatic_log.get(), _1, _2));
54 void* pixel_data_h[] = {
60 auto const pitch = xyz->size().width * 2;
62 for (int i = 0; i < 3; ++i) {
63 _pitch_in_bytes[i] = pitch;
64 auto status = cudaMallocPitch(
65 reinterpret_cast<void**>(&_pixel_data_d[i]),
71 if (status != cudaSuccess) {
72 throw CUDAError("cudaMallocPitch", status);
75 status = cudaMemcpy2D(
82 cudaMemcpyHostToDevice
85 if (status != cudaSuccess) {
86 throw CUDAError("cudaMemcpy2D", status);
90 _device_image.num_components = 3;
91 _device_image.pixel_data = reinterpret_cast<void**>(_pixel_data_d);
92 _device_image.pixel_type = NVJPEG2K_UINT16;
93 _device_image.pitch_in_bytes = reinterpret_cast<size_t*>(_pitch_in_bytes);
97 CUDAJ2KFrameEncoder::Input::Input(Input&& other)
98 : _index(other._index)
101 for (int i = 0; i < 3; ++i) {
102 _pixel_data_d[i] = other._pixel_data_d[i];
103 other._pixel_data_d[i] = nullptr;
104 _pitch_in_bytes[i] = other._pitch_in_bytes[i];
107 _device_image.num_components = other._device_image.num_components;
108 _device_image.pixel_data = reinterpret_cast<void**>(_pixel_data_d);
109 _device_image.pixel_type = NVJPEG2K_UINT16;
110 _device_image.pitch_in_bytes = reinterpret_cast<size_t*>(_pitch_in_bytes);
114 CUDAJ2KFrameEncoder::Input::~Input()
116 cudaFree(_pixel_data_d[0]);
117 cudaFree(_pixel_data_d[1]);
118 cudaFree(_pixel_data_d[2]);
122 optional<dcp::ArrayData>
123 CUDAJ2KFrameEncoder::encode(DCPVideo const& vf)
125 auto input = Input(vf);
127 auto const size = vf.frame()->out_size();
128 DCPOMATIC_ASSERT(!_size || size == *_size);
131 DCPOMATIC_ASSERT(!_resolution || vf.resolution() == *_resolution);
132 _resolution = vf.resolution();
134 nvjpeg2kImageComponentInfo_t info[3];
135 for (int i = 0; i < 3; ++i) {
136 info[i].component_width = _size->width;
137 info[i].component_height = _size->height;
138 info[i].precision = 12;
142 nvjpeg2kEncodeConfig_t config;
143 memset(&config, 0, sizeof(config));
144 config.stream_type = NVJPEG2K_STREAM_J2K;
145 config.color_space = NVJPEG2K_COLORSPACE_SRGB;
146 config.image_width = _size->width;
147 config.image_height = _size->height;
148 config.num_components = 3;
149 config.image_comp_info = reinterpret_cast<nvjpeg2kImageComponentInfo_t*>(&info);
150 config.code_block_w = 32;
151 config.code_block_h = 32;
152 config.irreversible = 0;
154 config.prog_order = NVJPEG2K_CPRL;
155 config.num_resolutions = *_resolution == Resolution::FOUR_K ? 7 : 6;
157 auto status = nvjpeg2kEncodeParamsSetEncodeConfig(_encoder_params, &config);
158 if (status != NVJPEG2K_STATUS_SUCCESS) {
159 throw CUDAError("nvjpeg2kEncodeParamsSetEncodeConfig", status);
163 status = nvjpeg2kEncodeParamsSetQuality(_encoder_params, 30);
164 if (status != NVJPEG2K_STATUS_SUCCESS) {
165 throw CUDAError("nvjpeg2kEncodeParamsSetQuality", status);
168 status = nvjpeg2kEncode(_encoder_handle, _encoder_state, _encoder_params, input.device_image(), _stream);
169 if (status != NVJPEG2K_STATUS_SUCCESS) {
170 throw CUDAError("nvjpeg2kEncode", status);
173 size_t compressed_size;
174 status = nvjpeg2kEncodeRetrieveBitstream(_encoder_handle, _encoder_state, nullptr, &compressed_size, _stream);
176 dcp::ArrayData output(compressed_size);
177 status = nvjpeg2kEncodeRetrieveBitstream(_encoder_handle, _encoder_state, output.data(), &compressed_size, _stream);
178 if (status != NVJPEG2K_STATUS_SUCCESS) {
179 throw CUDAError("nvjpeg2kEncodeRetrieveBitstream", status);
187 CUDAJ2KFrameEncoder::log_thread_start ()
189 LOG_TIMING("start-encoder-thread thread=%1", thread_id());
194 CUDAJ2KFrameEncoder::flush()