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/>.
25 #include "dcpomatic_log.h"
27 #include "grok_messenger.h"
31 using namespace dcpomatic;
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);
62 struct GrokInitializer {
63 GrokInitializer(void) {
64 setMessengerLogger(new GrokLogger("[GROK] "));
66 ~GrokInitializer() = default;
70 FrameProxy(void) : FrameProxy(0,Eyes::LEFT,DCPVideo())
72 FrameProxy(int index, Eyes eyes, DCPVideo dcpv) : index_(index), eyes_(eyes), vf(dcpv)
77 Eyes eyes(void) const {
85 struct DcpomaticContext {
86 DcpomaticContext(std::shared_ptr<const Film> film, Writer& writer,
87 EventHistory &history, const std::string &location) :
88 film_(film), writer_(writer),
89 history_(history), location_(location),
92 void setDimensions(uint32_t w, uint32_t h) {
96 std::shared_ptr<const Film> film_;
98 EventHistory &history_;
99 std::string location_;
106 explicit GrokContext(const DcpomaticContext &dcpomaticContext) :
107 dcpomaticContext_(dcpomaticContext),
111 struct CompressedData : public dcp::Data {
112 explicit CompressedData(int dataLen) : data_(new uint8_t[dataLen]), dataLen_(dataLen)
114 ~CompressedData(void){
117 uint8_t const * data () const override {
120 uint8_t * data () override {
123 int size () const override {
129 if (Config::instance()->enable_gpu ()) {
130 boost::filesystem::path folder(dcpomaticContext_.location_);
131 boost::filesystem::path binaryPath = folder / "grk_compress";
132 if (!boost::filesystem::exists(binaryPath)) {
133 getMessengerLogger()->error("Invalid binary location %s",
134 dcpomaticContext_.location_.c_str());
137 auto proc = [this](const std::string& str) {
140 auto tag = msg.next();
141 if(tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED)
143 auto clientFrameId = msg.nextUint();
144 auto compressedFrameId = msg.nextUint();
145 (void)compressedFrameId;
146 auto compressedFrameLength = msg.nextUint();
148 [this](FrameProxy srcFrame, uint8_t* compressed, uint32_t compressedFrameLength)
150 auto compressedData = std::make_shared<CompressedData>(compressedFrameLength);
151 memcpy(compressedData->data_,compressed,compressedFrameLength );
152 dcpomaticContext_.writer_.write(compressedData, srcFrame.index(), srcFrame.eyes());
155 int const minimum_size = 16384;
156 bool needsRecompression = compressedFrameLength < minimum_size;
157 messenger_->processCompressed(str, processor, needsRecompression);
158 if (needsRecompression) {
159 bool success = false;
160 auto fp = messenger_->retrieve(clientFrameId, success);
164 auto encoded = std::make_shared<dcp::ArrayData>(fp.vf.encode_locally());
165 dcpomaticContext_.writer_.write(encoded, fp.vf.index(), fp.vf.eyes());
169 } catch (std::exception &ex){
170 getMessengerLogger()->error("%s",ex.what());
174 MessengerInit(clientToGrokMessageBuf, clientSentSynch, grokReceiveReadySynch,
175 grokToClientMessageBuf, grokSentSynch, clientReceiveReadySynch, proc,
176 std::thread::hardware_concurrency());
177 messenger_ = new ScheduledMessenger<FrameProxy>(clientInit);
183 bool launch(DCPVideo dcpv, int device){
188 std::unique_lock<std::mutex> lk_global(launchMutex);
193 if (MessengerInit::firstLaunch(true)) {
194 auto s = dcpv.get_size();
195 dcpomaticContext_.setDimensions(s.width, s.height);
196 auto config = Config::instance();
197 messenger_->launchGrok(dcpomaticContext_.location_,
198 dcpomaticContext_.width_,dcpomaticContext_.width_,
199 dcpomaticContext_.height_,
201 dcpomaticContext_.film_->resolution() == Resolution::FOUR_K,
202 dcpomaticContext_.film_->video_frame_rate(),
203 dcpomaticContext_.film_->j2k_bandwidth(),
204 config->gpu_license_server(),
205 config->gpu_license_port(),
206 config->gpu_license());
208 launched_ = messenger_->waitForClientInit();
212 bool scheduleCompress(const DCPVideo &vf){
216 auto fp = FrameProxy(vf.index(),vf.eyes(),vf);
217 auto cvt = [this, &fp](BufferSrc src){
219 fp.vf.convert_to_xyz((uint16_t*)src.framePtr_);
221 return messenger_->scheduleCompress(fp, cvt);
227 std::unique_lock<std::mutex> lk_global(launchMutex);
231 messenger_->shutdown();
233 messenger_ = nullptr;
236 dcpomaticContext_.history_.event ();
239 DcpomaticContext dcpomaticContext_;
240 ScheduledMessenger<FrameProxy> *messenger_;