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/>.
23 #include "../config.h"
24 #include "../dcp_video.h"
27 #include "../dcpomatic_log.h"
28 #include "../writer.h"
29 #include "messenger.h"
30 #include <dcp/array_data.h>
33 static std::mutex launchMutex;
38 struct GrokLogger : public MessengerLogger {
39 explicit GrokLogger(const std::string &preamble) : MessengerLogger(preamble)
41 virtual ~GrokLogger() = default;
42 void info(const char* fmt, ...) override{
45 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_GENERAL);
48 void warn(const char* fmt, ...) override{
51 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_WARNING);
54 void error(const char* fmt, ...) override{
57 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_ERROR);
63 FrameProxy(int index, Eyes eyes, DCPVideo dcpv) : index_(index), eyes_(eyes), vf(dcpv)
68 Eyes eyes(void) const {
76 struct DcpomaticContext {
78 std::shared_ptr<const Film> film,
80 EventHistory& history,
81 boost::filesystem::path const& location
93 void setDimensions(uint32_t w, uint32_t h) {
97 std::shared_ptr<const Film> film_;
99 EventHistory &history_;
100 boost::filesystem::path _location;
107 explicit GrokContext(const DcpomaticContext &dcpomaticContext) :
108 dcpomaticContext_(dcpomaticContext),
112 if (Config::instance()->enable_gpu ()) {
113 boost::filesystem::path folder(dcpomaticContext_._location);
114 boost::filesystem::path binaryPath = folder / "grk_compress";
115 if (!boost::filesystem::exists(binaryPath)) {
116 getMessengerLogger()->error(
117 "Invalid binary location %s", dcpomaticContext_._location.c_str()
121 auto proc = [this](const std::string& str) {
124 auto tag = msg.next();
125 if(tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED)
127 auto clientFrameId = msg.nextUint();
128 auto compressedFrameId = msg.nextUint();
129 (void)compressedFrameId;
130 auto compressedFrameLength = msg.nextUint();
132 [this](FrameProxy srcFrame, uint8_t* compressed, uint32_t compressedFrameLength)
134 auto compressed_data = std::make_shared<dcp::ArrayData>(compressed, compressedFrameLength);
135 dcpomaticContext_.writer_.write(compressed_data, srcFrame.index(), srcFrame.eyes());
138 int const minimum_size = 16384;
139 bool needsRecompression = compressedFrameLength < minimum_size;
140 messenger_->processCompressed(str, processor, needsRecompression);
141 if (needsRecompression) {
142 auto fp = messenger_->retrieve(clientFrameId);
147 auto encoded = std::make_shared<dcp::ArrayData>(fp->vf.encode_locally());
148 dcpomaticContext_.writer_.write(encoded, fp->vf.index(), fp->vf.eyes());
152 } catch (std::exception &ex){
153 getMessengerLogger()->error("%s",ex.what());
157 MessengerInit(clientToGrokMessageBuf, clientSentSynch, grokReceiveReadySynch,
158 grokToClientMessageBuf, grokSentSynch, clientReceiveReadySynch, proc,
159 std::thread::hardware_concurrency());
160 messenger_ = new ScheduledMessenger<FrameProxy>(clientInit);
166 bool launch(DCPVideo dcpv, int device){
171 std::unique_lock<std::mutex> lk_global(launchMutex);
176 if (MessengerInit::firstLaunch(true)) {
177 auto s = dcpv.get_size();
178 dcpomaticContext_.setDimensions(s.width, s.height);
179 auto config = Config::instance();
180 messenger_->launchGrok(
181 dcpomaticContext_._location,
182 dcpomaticContext_.width_,dcpomaticContext_.width_,
183 dcpomaticContext_.height_,
185 dcpomaticContext_.film_->resolution() == Resolution::FOUR_K,
186 dcpomaticContext_.film_->video_frame_rate(),
187 dcpomaticContext_.film_->j2k_bandwidth(),
188 config->gpu_license_server(),
189 config->gpu_license_port(),
190 config->gpu_license()
193 launched_ = messenger_->waitForClientInit();
197 bool scheduleCompress(DCPVideo const& vf){
201 auto fp = FrameProxy(vf.index(), vf.eyes(), vf);
202 auto cvt = [this, &fp](BufferSrc src){
204 fp.vf.convert_to_xyz((uint16_t*)src.framePtr_);
206 return messenger_->scheduleCompress(fp, cvt);
212 std::unique_lock<std::mutex> lk_global(launchMutex);
216 messenger_->shutdown();
218 messenger_ = nullptr;
221 dcpomaticContext_.history_.event ();
224 DcpomaticContext dcpomaticContext_;
225 ScheduledMessenger<FrameProxy> *messenger_;