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"
32 static std::mutex launchMutex;
37 struct GrokLogger : public MessengerLogger {
38 explicit GrokLogger(const std::string &preamble) : MessengerLogger(preamble)
40 virtual ~GrokLogger() = default;
41 void info(const char* fmt, ...) override{
44 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_GENERAL);
47 void warn(const char* fmt, ...) override{
50 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_WARNING);
53 void error(const char* fmt, ...) override{
56 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_ERROR);
62 FrameProxy(int index, Eyes eyes, DCPVideo dcpv) : index_(index), eyes_(eyes), vf(dcpv)
67 Eyes eyes(void) const {
75 struct DcpomaticContext {
76 DcpomaticContext(std::shared_ptr<const Film> film, Writer& writer,
77 EventHistory &history, const std::string &location) :
78 film_(film), writer_(writer),
79 history_(history), location_(location),
82 void setDimensions(uint32_t w, uint32_t h) {
86 std::shared_ptr<const Film> film_;
88 EventHistory &history_;
89 std::string location_;
96 explicit GrokContext(const DcpomaticContext &dcpomaticContext) :
97 dcpomaticContext_(dcpomaticContext),
101 struct CompressedData : public dcp::Data {
102 explicit CompressedData(int dataLen) : data_(new uint8_t[dataLen]), dataLen_(dataLen)
104 ~CompressedData(void){
107 uint8_t const * data () const override {
110 uint8_t * data () override {
113 int size () const override {
119 if (Config::instance()->enable_gpu ()) {
120 boost::filesystem::path folder(dcpomaticContext_.location_);
121 boost::filesystem::path binaryPath = folder / "grk_compress";
122 if (!boost::filesystem::exists(binaryPath)) {
123 getMessengerLogger()->error("Invalid binary location %s",
124 dcpomaticContext_.location_.c_str());
127 auto proc = [this](const std::string& str) {
130 auto tag = msg.next();
131 if(tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED)
133 auto clientFrameId = msg.nextUint();
134 auto compressedFrameId = msg.nextUint();
135 (void)compressedFrameId;
136 auto compressedFrameLength = msg.nextUint();
138 [this](FrameProxy srcFrame, uint8_t* compressed, uint32_t compressedFrameLength)
140 auto compressedData = std::make_shared<CompressedData>(compressedFrameLength);
141 memcpy(compressedData->data_,compressed,compressedFrameLength );
142 dcpomaticContext_.writer_.write(compressedData, srcFrame.index(), srcFrame.eyes());
145 int const minimum_size = 16384;
146 bool needsRecompression = compressedFrameLength < minimum_size;
147 messenger_->processCompressed(str, processor, needsRecompression);
148 if (needsRecompression) {
149 auto fp = messenger_->retrieve(clientFrameId);
154 auto encoded = std::make_shared<dcp::ArrayData>(fp->vf.encode_locally());
155 dcpomaticContext_.writer_.write(encoded, fp->vf.index(), fp->vf.eyes());
159 } catch (std::exception &ex){
160 getMessengerLogger()->error("%s",ex.what());
164 MessengerInit(clientToGrokMessageBuf, clientSentSynch, grokReceiveReadySynch,
165 grokToClientMessageBuf, grokSentSynch, clientReceiveReadySynch, proc,
166 std::thread::hardware_concurrency());
167 messenger_ = new ScheduledMessenger<FrameProxy>(clientInit);
173 bool launch(DCPVideo dcpv, int device){
178 std::unique_lock<std::mutex> lk_global(launchMutex);
183 if (MessengerInit::firstLaunch(true)) {
184 auto s = dcpv.get_size();
185 dcpomaticContext_.setDimensions(s.width, s.height);
186 auto config = Config::instance();
187 messenger_->launchGrok(dcpomaticContext_.location_,
188 dcpomaticContext_.width_,dcpomaticContext_.width_,
189 dcpomaticContext_.height_,
191 dcpomaticContext_.film_->resolution() == Resolution::FOUR_K,
192 dcpomaticContext_.film_->video_frame_rate(),
193 dcpomaticContext_.film_->j2k_bandwidth(),
194 config->gpu_license_server(),
195 config->gpu_license_port(),
196 config->gpu_license());
198 launched_ = messenger_->waitForClientInit();
202 bool scheduleCompress(DCPVideo const& vf){
206 auto fp = FrameProxy(vf.index(), vf.eyes(), vf);
207 auto cvt = [this, &fp](BufferSrc src){
209 fp.vf.convert_to_xyz((uint16_t*)src.framePtr_);
211 return messenger_->scheduleCompress(fp, cvt);
217 std::unique_lock<std::mutex> lk_global(launchMutex);
221 messenger_->shutdown();
223 messenger_ = nullptr;
226 dcpomaticContext_.history_.event ();
229 DcpomaticContext dcpomaticContext_;
230 ScheduledMessenger<FrameProxy> *messenger_;