1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
Copyright (C) 2023 Grok Image Compression Inc.
This file is part of DCP-o-matic.
DCP-o-matic 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.
DCP-o-matic 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
*/
#include "context.h"
using namespace grk_plugin;
static std::mutex launchMutex;
GrokContext::GrokContext(DcpomaticContext* dcpomatic_context)
: _dcpomatic_context(dcpomatic_context)
{
auto grok = Config::instance()->grok();
if (!grok.enable) {
return;
}
boost::filesystem::path folder(_dcpomatic_context->location);
boost::filesystem::path binary_path = folder / "grk_compress";
if (!boost::filesystem::exists(binary_path)) {
getGrokLogger()->error(
"Invalid binary location %s", _dcpomatic_context->location.c_str()
);
return;
}
auto proc = [this](const std::string& str) {
try {
Message msg(str);
auto tag = msg.next();
if (tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED) {
auto clientFrameId = msg.nextUint();
msg.nextUint(); // compressed frame ID
auto compressedFrameLength = msg.nextUint();
auto processor = [this](DCPVideo srcFrame, uint8_t* compressed, uint32_t compressedFrameLength) {
auto compressed_data = std::make_shared<dcp::ArrayData>(compressed, compressedFrameLength);
_dcpomatic_context->writer.write(compressed_data, srcFrame.index(), srcFrame.eyes());
frame_done ();
};
int const minimum_size = 16384;
/* Write the compressed data out and tidy up */
bool needsRecompression = compressedFrameLength < minimum_size;
_messenger->process_compressed(str, processor, needsRecompression);
if (needsRecompression) {
/* The JPEG2000 frame data was too small, so handle it with the CPU encoder */
auto vf = _messenger->retrieve(clientFrameId);
if (!vf) {
return;
}
auto encoded = std::make_shared<dcp::ArrayData>(vf->encode_locally());
_dcpomatic_context->writer.write(encoded, vf->index(), vf->eyes());
frame_done ();
}
}
} catch (std::exception& ex) {
getGrokLogger()->error("%s",ex.what());
}
};
_messenger = new Messenger(proc, std::thread::hardware_concurrency());
}
GrokContext::~GrokContext()
{
if (!_messenger) {
return;
}
std::unique_lock<std::mutex> lk_global(launchMutex);
if (!_messenger) {
return;
}
if (_launched) {
_messenger->shutdown();
}
delete _messenger;
}
bool
GrokContext::launch(DCPVideo dcpv, int device)
{
namespace fs = boost::filesystem;
if (!_messenger) {
return false;
}
if (_launched) {
return true;
}
if (_launch_failed) {
return false;
}
std::unique_lock<std::mutex> lk_global(launchMutex);
if (!_messenger) {
return false;
}
if (_launched) {
return true;
}
if (_launch_failed) {
return false;
}
if (!fs::exists(_dcpomatic_context->location) || !fs::is_directory(_dcpomatic_context->location)) {
getGrokLogger()->error("Invalid directory %s", _dcpomatic_context->location.c_str());
return false;
}
auto s = dcpv.get_size();
_dcpomatic_context->set_dimensions(s.width, s.height);
auto grok = Config::instance()->grok();
if (!_messenger->launch_grok(
_dcpomatic_context->location,
_dcpomatic_context->width,
_dcpomatic_context->width,
_dcpomatic_context->height,
3,
12,
device,
_dcpomatic_context->film->resolution() == Resolution::FOUR_K,
_dcpomatic_context->film->video_frame_rate(),
_dcpomatic_context->film->video_bit_rate(VideoEncoding::JPEG2000),
grok.licence_server,
grok.licence)) {
_launch_failed = true;
return false;
}
_launched = _messenger->waitForClientInit();
_launch_failed = _launched;
return _launched;
}
bool
GrokContext::schedule_compress(DCPVideo const& vf)
{
if (!_messenger) {
return false;
}
auto cvt = [&vf](BufferSrc src) {
vf.convert_to_xyz((uint16_t*) src.frame_ptr);
};
return _messenger->schedule_compress(vf, cvt);
}
void
GrokContext::frame_done()
{
_dcpomatic_context->history.event();
}
GrokLogger* grk_plugin::sLogger = nullptr;
void
grk_plugin::setGrokLogger(grk_plugin::GrokLogger* logger)
{
delete sLogger;
sLogger = logger;
}
grk_plugin::GrokLogger*
grk_plugin::getGrokLogger()
{
return sLogger;
}
|