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 using namespace dcpomatic;
33 static std::mutex launchMutex;
35 namespace grk_plugin {
37 struct GrokLogger : public MessengerLogger
39 explicit GrokLogger(const std::string& preamble)
40 : MessengerLogger(preamble)
44 virtual ~GrokLogger() = default;
46 void info(const char* fmt, ...) override
50 dcpomatic_log->log(preamble_ + log_message(fmt, arg), LogEntry::TYPE_GENERAL);
54 void warn(const char* fmt, ...) override
58 dcpomatic_log->log(preamble_ + log_message(fmt, arg), LogEntry::TYPE_WARNING);
62 void error(const char* fmt, ...) override
66 dcpomatic_log->log(preamble_ + log_message(fmt, arg), LogEntry::TYPE_ERROR);
71 struct GrokInitializer
75 setMessengerLogger(new GrokLogger("[GROK] "));
78 ~GrokInitializer() = default;
84 : FrameProxy(0, Eyes::LEFT, DCPVideo())
88 FrameProxy(int index, Eyes eyes, DCPVideo dcpv)
110 struct DcpomaticContext
112 DcpomaticContext(std::shared_ptr<const Film> film, Writer& writer,
113 EventHistory& history, const std::string& location)
117 , location_(location)
123 void set_dimensions(uint32_t w, uint32_t h)
129 std::shared_ptr<const Film> film_;
131 EventHistory& history_;
132 std::string location_;
140 explicit GrokContext(const DcpomaticContext& dcpomaticContext)
141 : _dcpomatic_context(dcpomaticContext)
142 , _messenger(nullptr)
145 struct CompressedData : public dcp::Data
147 explicit CompressedData(int dataLen)
148 : data_(new uint8_t[dataLen])
158 uint8_t const* data() const override
163 uint8_t* data() override
168 int size() const override
177 if (Config::instance()->enable_gpu()) {
178 boost::filesystem::path folder(_dcpomatic_context.location_);
179 boost::filesystem::path binaryPath = folder / "grk_compress";
180 if (!boost::filesystem::exists(binaryPath)) {
181 getMessengerLogger()->error("Invalid binary location %s",
182 _dcpomatic_context.location_.c_str());
185 auto proc = [this](const std::string& str) {
188 auto tag = msg.next();
189 if (tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED) {
190 auto clientFrameId = msg.next_uint();
191 auto compressedFrameId = msg.next_uint();
192 (void)compressedFrameId;
193 auto compressedFrameLength = msg.next_uint();
195 [this](FrameProxy srcFrame, uint8_t* compressed, uint32_t compressedFrameLength) {
196 auto compressedData = std::make_shared<CompressedData>(compressedFrameLength);
197 memcpy(compressedData->data_, compressed, compressedFrameLength);
198 _dcpomatic_context.writer_.write(compressedData, srcFrame.index(), srcFrame.eyes());
201 int const minimum_size = 16384;
202 bool needsRecompression = compressedFrameLength < minimum_size;
203 _messenger->process_compressed(str, processor, needsRecompression);
204 if (needsRecompression) {
205 bool success = false;
206 auto fp = _messenger->retrieve(clientFrameId, success);
211 auto encoded = std::make_shared<dcp::ArrayData>(fp.vf.encode_locally());
212 _dcpomatic_context.writer_.write(encoded, fp.vf.index(), fp.vf.eyes());
216 } catch (std::exception& ex) {
217 getMessengerLogger()->error("%s", ex.what());
221 MessengerInit(clientToGrokMessageBuf, clientSentSynch, grokReceiveReadySynch,
222 grokToClientMessageBuf, grokSentSynch, clientReceiveReadySynch, proc,
223 std::thread::hardware_concurrency());
224 _messenger = new ScheduledMessenger<FrameProxy>(clientInit);
233 bool launch(DCPVideo dcpv, int device)
241 std::unique_lock<std::mutex> lk_global(launchMutex);
248 if (MessengerInit::first_launch(true)) {
249 auto s = dcpv.get_size();
250 _dcpomatic_context.set_dimensions(s.width, s.height);
251 auto config = Config::instance();
252 _messenger->launch_grok(_dcpomatic_context.location_,
253 _dcpomatic_context.width_, _dcpomatic_context.width_,
254 _dcpomatic_context.height_,
256 _dcpomatic_context.film_->resolution() == Resolution::FOUR_K,
257 _dcpomatic_context.film_->video_frame_rate(),
258 _dcpomatic_context.film_->j2k_bandwidth(),
259 config->gpu_license_server(),
260 config->gpu_license_port(),
261 config->gpu_license());
263 _launched = _messenger->wait_for_client_init();
268 bool schedule_compress(const DCPVideo& vf)
274 auto fp = FrameProxy(vf.index(), vf.eyes(), vf);
275 auto cvt = [this, &fp](BufferSrc src) {
277 fp.vf.convert_to_xyz((uint16_t*)src.framePtr_);
279 return _messenger->schedule_compress(fp, cvt);
288 std::unique_lock<std::mutex> lk_global(launchMutex);
293 _messenger->shutdown();
296 _messenger = nullptr;
301 _dcpomatic_context.history_.event();
305 DcpomaticContext _dcpomatic_context;
306 ScheduledMessenger<FrameProxy>* _messenger;
310 } // namespace grk_plugin