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 "../config.h"
25 #include "../dcp_video.h"
28 #include "../dcpomatic_log.h"
29 #include "../writer.h"
30 #include "messenger.h"
31 #include <dcp/array_data.h>
32 #include <boost/filesystem.hpp>
35 static std::mutex launchMutex;
40 struct GrokLogger : public MessengerLogger {
41 explicit GrokLogger(const std::string &preamble) : MessengerLogger(preamble)
43 virtual ~GrokLogger() = default;
44 void info(const char* fmt, ...) override{
47 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_GENERAL);
50 void warn(const char* fmt, ...) override{
53 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_WARNING);
56 void error(const char* fmt, ...) override{
59 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_ERROR);
65 FrameProxy(int index, Eyes eyes, DCPVideo dcpv) : index_(index), eyes_(eyes), vf(dcpv)
70 Eyes eyes(void) const {
78 struct DcpomaticContext {
80 std::shared_ptr<const Film> film,
82 EventHistory& history,
83 boost::filesystem::path const& location
95 void setDimensions(uint32_t w, uint32_t h) {
99 std::shared_ptr<const Film> film_;
101 EventHistory &history_;
102 boost::filesystem::path _location;
111 explicit GrokContext(DcpomaticContext* dcpomatic_context)
112 : _dcpomatic_context(dcpomatic_context)
113 , messenger_(nullptr)
115 , launchFailed_(false)
117 if (Config::instance()->enable_gpu ()) {
118 boost::filesystem::path folder(_dcpomatic_context->_location);
119 boost::filesystem::path binaryPath = folder / "grk_compress";
120 if (!boost::filesystem::exists(binaryPath)) {
121 getMessengerLogger()->error(
122 "Invalid binary location %s", _dcpomatic_context->_location.c_str()
126 auto proc = [this](const std::string& str) {
129 auto tag = msg.next();
130 if(tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED)
132 auto clientFrameId = msg.nextUint();
133 auto compressedFrameId = msg.nextUint();
134 (void)compressedFrameId;
135 auto compressedFrameLength = msg.nextUint();
137 [this](FrameProxy srcFrame, uint8_t* compressed, uint32_t compressedFrameLength)
139 auto compressed_data = std::make_shared<dcp::ArrayData>(compressed, compressedFrameLength);
140 _dcpomatic_context->writer_.write(compressed_data, srcFrame.index(), srcFrame.eyes());
143 int const minimum_size = 16384;
144 bool needsRecompression = compressedFrameLength < minimum_size;
145 messenger_->processCompressed(str, processor, needsRecompression);
146 if (needsRecompression) {
147 auto fp = messenger_->retrieve(clientFrameId);
152 auto encoded = std::make_shared<dcp::ArrayData>(fp->vf.encode_locally());
153 _dcpomatic_context->writer_.write(encoded, fp->vf.index(), fp->vf.eyes());
157 } catch (std::exception &ex){
158 getMessengerLogger()->error("%s",ex.what());
162 MessengerInit(clientToGrokMessageBuf, clientSentSynch, grokReceiveReadySynch,
163 grokToClientMessageBuf, grokSentSynch, clientReceiveReadySynch, proc,
164 std::thread::hardware_concurrency());
165 messenger_ = new ScheduledMessenger<FrameProxy>(clientInit);
171 bool launch(DCPVideo dcpv, int device){
172 namespace fs = boost::filesystem;
180 std::unique_lock<std::mutex> lk_global(launchMutex);
187 if (MessengerInit::firstLaunch(true)) {
189 if (!fs::exists(_dcpomatic_context->_location) || !fs::is_directory(_dcpomatic_context->_location)) {
190 getMessengerLogger()->error("Invalid directory %s", _dcpomatic_context->_location.c_str());
193 auto s = dcpv.get_size();
194 _dcpomatic_context->setDimensions(s.width, s.height);
195 auto config = Config::instance();
196 if (!messenger_->launchGrok(
197 _dcpomatic_context->_location,
198 _dcpomatic_context->width_,_dcpomatic_context->width_,
199 _dcpomatic_context->height_,
201 _dcpomatic_context->film_->resolution() == Resolution::FOUR_K,
202 _dcpomatic_context->film_->video_frame_rate(),
203 _dcpomatic_context->film_->j2k_bandwidth(),
204 config->gpu_license_server(),
205 config->gpu_license_port(),
206 config->gpu_license())) {
207 launchFailed_ = true;
211 launched_ = messenger_->waitForClientInit();
212 launchFailed_ = launched_;
216 bool scheduleCompress(DCPVideo const& vf){
220 auto fp = FrameProxy(vf.index(), vf.eyes(), vf);
221 auto cvt = [this, &fp](BufferSrc src){
223 fp.vf.convert_to_xyz((uint16_t*)src.framePtr_);
225 return messenger_->scheduleCompress(fp, cvt);
231 std::unique_lock<std::mutex> lk_global(launchMutex);
235 messenger_->shutdown();
237 messenger_ = nullptr;
240 _dcpomatic_context->history_.event();
243 DcpomaticContext* _dcpomatic_context;
244 ScheduledMessenger<FrameProxy> *messenger_;