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);
62 struct GrokInitializer {
63 GrokInitializer(void) {
64 setMessengerLogger(new GrokLogger("[GROK] "));
66 ~GrokInitializer() = default;
70 FrameProxy(int index, Eyes eyes, DCPVideo dcpv) : index_(index), eyes_(eyes), vf(dcpv)
75 Eyes eyes(void) const {
83 struct DcpomaticContext {
84 DcpomaticContext(std::shared_ptr<const Film> film, Writer& writer,
85 EventHistory &history, const std::string &location) :
86 film_(film), writer_(writer),
87 history_(history), location_(location),
90 void setDimensions(uint32_t w, uint32_t h) {
94 std::shared_ptr<const Film> film_;
96 EventHistory &history_;
97 std::string location_;
104 explicit GrokContext(const DcpomaticContext &dcpomaticContext) :
105 dcpomaticContext_(dcpomaticContext),
109 struct CompressedData : public dcp::Data {
110 explicit CompressedData(int dataLen) : data_(new uint8_t[dataLen]), dataLen_(dataLen)
112 ~CompressedData(void){
115 uint8_t const * data () const override {
118 uint8_t * data () override {
121 int size () const override {
127 if (Config::instance()->enable_gpu ()) {
128 boost::filesystem::path folder(dcpomaticContext_.location_);
129 boost::filesystem::path binaryPath = folder / "grk_compress";
130 if (!boost::filesystem::exists(binaryPath)) {
131 getMessengerLogger()->error("Invalid binary location %s",
132 dcpomaticContext_.location_.c_str());
135 auto proc = [this](const std::string& str) {
138 auto tag = msg.next();
139 if(tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED)
141 auto clientFrameId = msg.nextUint();
142 auto compressedFrameId = msg.nextUint();
143 (void)compressedFrameId;
144 auto compressedFrameLength = msg.nextUint();
146 [this](FrameProxy srcFrame, uint8_t* compressed, uint32_t compressedFrameLength)
148 auto compressedData = std::make_shared<CompressedData>(compressedFrameLength);
149 memcpy(compressedData->data_,compressed,compressedFrameLength );
150 dcpomaticContext_.writer_.write(compressedData, srcFrame.index(), srcFrame.eyes());
153 int const minimum_size = 16384;
154 bool needsRecompression = compressedFrameLength < minimum_size;
155 messenger_->processCompressed(str, processor, needsRecompression);
156 if (needsRecompression) {
157 auto fp = messenger_->retrieve(clientFrameId);
162 auto encoded = std::make_shared<dcp::ArrayData>(fp->vf.encode_locally());
163 dcpomaticContext_.writer_.write(encoded, fp->vf.index(), fp->vf.eyes());
167 } catch (std::exception &ex){
168 getMessengerLogger()->error("%s",ex.what());
172 MessengerInit(clientToGrokMessageBuf, clientSentSynch, grokReceiveReadySynch,
173 grokToClientMessageBuf, grokSentSynch, clientReceiveReadySynch, proc,
174 std::thread::hardware_concurrency());
175 messenger_ = new ScheduledMessenger<FrameProxy>(clientInit);
181 bool launch(DCPVideo dcpv, int device){
186 std::unique_lock<std::mutex> lk_global(launchMutex);
191 if (MessengerInit::firstLaunch(true)) {
192 auto s = dcpv.get_size();
193 dcpomaticContext_.setDimensions(s.width, s.height);
194 auto config = Config::instance();
195 messenger_->launchGrok(dcpomaticContext_.location_,
196 dcpomaticContext_.width_,dcpomaticContext_.width_,
197 dcpomaticContext_.height_,
199 dcpomaticContext_.film_->resolution() == Resolution::FOUR_K,
200 dcpomaticContext_.film_->video_frame_rate(),
201 dcpomaticContext_.film_->j2k_bandwidth(),
202 config->gpu_license_server(),
203 config->gpu_license_port(),
204 config->gpu_license());
206 launched_ = messenger_->waitForClientInit();
210 bool scheduleCompress(DCPVideo const& vf){
214 auto fp = FrameProxy(vf.index(), vf.eyes(), vf);
215 auto cvt = [this, &fp](BufferSrc src){
217 fp.vf.convert_to_xyz((uint16_t*)src.framePtr_);
219 return messenger_->scheduleCompress(fp, cvt);
225 std::unique_lock<std::mutex> lk_global(launchMutex);
229 messenger_->shutdown();
231 messenger_ = nullptr;
234 dcpomaticContext_.history_.event ();
237 DcpomaticContext dcpomaticContext_;
238 ScheduledMessenger<FrameProxy> *messenger_;