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"
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 {
77 DcpomaticContext(std::shared_ptr<const Film> film, Writer& writer,
78 EventHistory &history, const std::string &location) :
79 film_(film), writer_(writer),
80 history_(history), location_(location),
83 void setDimensions(uint32_t w, uint32_t h) {
87 std::shared_ptr<const Film> film_;
89 EventHistory &history_;
90 std::string location_;
97 explicit GrokContext(const DcpomaticContext &dcpomaticContext) :
98 dcpomaticContext_(dcpomaticContext),
102 struct CompressedData : public dcp::Data {
103 explicit CompressedData(int dataLen) : data_(new uint8_t[dataLen]), dataLen_(dataLen)
105 ~CompressedData(void){
108 uint8_t const * data () const override {
111 uint8_t * data () override {
114 int size () const override {
120 if (Config::instance()->enable_gpu ()) {
121 boost::filesystem::path folder(dcpomaticContext_.location_);
122 boost::filesystem::path binaryPath = folder / "grk_compress";
123 if (!boost::filesystem::exists(binaryPath)) {
124 getMessengerLogger()->error("Invalid binary location %s",
125 dcpomaticContext_.location_.c_str());
128 auto proc = [this](const std::string& str) {
131 auto tag = msg.next();
132 if(tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED)
134 auto clientFrameId = msg.nextUint();
135 auto compressedFrameId = msg.nextUint();
136 (void)compressedFrameId;
137 auto compressedFrameLength = msg.nextUint();
139 [this](FrameProxy srcFrame, uint8_t* compressed, uint32_t compressedFrameLength)
141 auto compressedData = std::make_shared<CompressedData>(compressedFrameLength);
142 memcpy(compressedData->data_,compressed,compressedFrameLength );
143 dcpomaticContext_.writer_.write(compressedData, srcFrame.index(), srcFrame.eyes());
146 int const minimum_size = 16384;
147 bool needsRecompression = compressedFrameLength < minimum_size;
148 messenger_->processCompressed(str, processor, needsRecompression);
149 if (needsRecompression) {
150 auto fp = messenger_->retrieve(clientFrameId);
155 auto encoded = std::make_shared<dcp::ArrayData>(fp->vf.encode_locally());
156 dcpomaticContext_.writer_.write(encoded, fp->vf.index(), fp->vf.eyes());
160 } catch (std::exception &ex){
161 getMessengerLogger()->error("%s",ex.what());
165 MessengerInit(clientToGrokMessageBuf, clientSentSynch, grokReceiveReadySynch,
166 grokToClientMessageBuf, grokSentSynch, clientReceiveReadySynch, proc,
167 std::thread::hardware_concurrency());
168 messenger_ = new ScheduledMessenger<FrameProxy>(clientInit);
174 bool launch(DCPVideo dcpv, int device){
179 std::unique_lock<std::mutex> lk_global(launchMutex);
184 if (MessengerInit::firstLaunch(true)) {
185 auto s = dcpv.get_size();
186 dcpomaticContext_.setDimensions(s.width, s.height);
187 auto config = Config::instance();
188 messenger_->launchGrok(dcpomaticContext_.location_,
189 dcpomaticContext_.width_,dcpomaticContext_.width_,
190 dcpomaticContext_.height_,
192 dcpomaticContext_.film_->resolution() == Resolution::FOUR_K,
193 dcpomaticContext_.film_->video_frame_rate(),
194 dcpomaticContext_.film_->j2k_bandwidth(),
195 config->gpu_license_server(),
196 config->gpu_license_port(),
197 config->gpu_license());
199 launched_ = messenger_->waitForClientInit();
203 bool scheduleCompress(DCPVideo const& vf){
207 auto fp = FrameProxy(vf.index(), vf.eyes(), vf);
208 auto cvt = [this, &fp](BufferSrc src){
210 fp.vf.convert_to_xyz((uint16_t*)src.framePtr_);
212 return messenger_->scheduleCompress(fp, cvt);
218 std::unique_lock<std::mutex> lk_global(launchMutex);
222 messenger_->shutdown();
224 messenger_ = nullptr;
227 dcpomaticContext_.history_.event ();
230 DcpomaticContext dcpomaticContext_;
231 ScheduledMessenger<FrameProxy> *messenger_;