2 Copyright (C) 2023 Grok Image Compression Inc.
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/>.
24 #include "dcpomatic_log.h"
25 #include "grok_messenger.h"
31 static std::mutex launchMutex;
33 namespace grk_plugin {
35 struct GrokLogger : public MessengerLogger
37 explicit GrokLogger(const std::string& preamble)
38 : MessengerLogger(preamble)
42 virtual ~GrokLogger() = default;
44 void info(const char* fmt, ...) override
48 dcpomatic_log->log(preamble_ + log_message(fmt, arg), LogEntry::TYPE_GENERAL);
52 void warn(const char* fmt, ...) override
56 dcpomatic_log->log(preamble_ + log_message(fmt, arg), LogEntry::TYPE_WARNING);
60 void error(const char* fmt, ...) override
64 dcpomatic_log->log(preamble_ + log_message(fmt, arg), LogEntry::TYPE_ERROR);
69 struct GrokInitializer
73 setMessengerLogger(new GrokLogger("[GROK] "));
76 ~GrokInitializer() = default;
82 : FrameProxy(0, Eyes::LEFT, DCPVideo())
86 FrameProxy(int index, Eyes eyes, DCPVideo dcpv)
108 struct DcpomaticContext
111 std::shared_ptr<const Film> film_,
113 EventHistory& history_,
114 const std::string& location_
119 , location(location_)
125 void set_dimensions(uint32_t w, uint32_t h)
131 std::shared_ptr<const Film> film;
133 EventHistory& history;
134 std::string location;
142 explicit GrokContext(const DcpomaticContext& dcpomaticContext)
143 : _dcpomatic_context(dcpomaticContext)
144 , _messenger(nullptr)
147 struct CompressedData : public dcp::Data
149 explicit CompressedData(int dataLen)
150 : data_(new uint8_t[dataLen])
160 uint8_t const* data() const override
165 uint8_t* data() override
170 int size() const override
179 if (Config::instance()->enable_gpu()) {
180 boost::filesystem::path folder(_dcpomatic_context.location);
181 boost::filesystem::path binaryPath = folder / "grk_compress";
182 if (!boost::filesystem::exists(binaryPath)) {
183 getMessengerLogger()->error("Invalid binary location %s",
184 _dcpomatic_context.location.c_str());
187 auto proc = [this](const std::string& str) {
190 auto tag = msg.next();
191 if (tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED) {
192 auto clientFrameId = msg.next_uint();
193 auto compressedFrameId = msg.next_uint();
194 (void)compressedFrameId;
195 auto compressedFrameLength = msg.next_uint();
197 [this](FrameProxy srcFrame, uint8_t* compressed, uint32_t compressedFrameLength) {
198 auto compressedData = std::make_shared<CompressedData>(compressedFrameLength);
199 memcpy(compressedData->data_, compressed, compressedFrameLength);
200 _dcpomatic_context.writer.write(compressedData, srcFrame.index(), srcFrame.eyes());
203 int const minimum_size = 16384;
204 bool needsRecompression = compressedFrameLength < minimum_size;
205 _messenger->process_compressed(str, processor, needsRecompression);
206 if (needsRecompression) {
207 bool success = false;
208 auto fp = _messenger->retrieve(clientFrameId, success);
213 auto encoded = std::make_shared<dcp::ArrayData>(fp.vf.encode_locally());
214 _dcpomatic_context.writer.write(encoded, fp.vf.index(), fp.vf.eyes());
218 } catch (std::exception& ex) {
219 getMessengerLogger()->error("%s", ex.what());
223 MessengerInit(clientToGrokMessageBuf, clientSentSynch, grokReceiveReadySynch,
224 grokToClientMessageBuf, grokSentSynch, clientReceiveReadySynch, proc,
225 std::thread::hardware_concurrency());
226 _messenger = new ScheduledMessenger<FrameProxy>(clientInit);
235 bool launch(DCPVideo dcpv, int device)
243 std::unique_lock<std::mutex> lk_global(launchMutex);
250 if (MessengerInit::first_launch(true)) {
251 auto s = dcpv.get_size();
252 _dcpomatic_context.set_dimensions(s.width, s.height);
253 auto config = Config::instance();
254 _messenger->launch_grok(_dcpomatic_context.location,
255 _dcpomatic_context.width, _dcpomatic_context.width,
256 _dcpomatic_context.height,
258 _dcpomatic_context.film->resolution() == Resolution::FOUR_K,
259 _dcpomatic_context.film->video_frame_rate(),
260 _dcpomatic_context.film->j2k_bandwidth(),
261 config->gpu_license_server(),
262 config->gpu_license_port(),
263 config->gpu_license());
265 _launched = _messenger->wait_for_client_init();
270 bool schedule_compress(const DCPVideo& vf)
276 auto fp = FrameProxy(vf.index(), vf.eyes(), vf);
277 auto cvt = [this, &fp](BufferSrc src) {
279 fp.vf.convert_to_xyz((uint16_t*)src.framePtr_);
281 return _messenger->schedule_compress(fp, cvt);
290 std::unique_lock<std::mutex> lk_global(launchMutex);
295 _messenger->shutdown();
298 _messenger = nullptr;
303 _dcpomatic_context.history.event();
307 DcpomaticContext _dcpomatic_context;
308 ScheduledMessenger<FrameProxy>* _messenger;
312 } // namespace grk_plugin