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"
26 #include "../dcpomatic_log.h"
27 #include "../writer.h"
28 #include "messenger.h"
32 using namespace dcpomatic;
34 static std::mutex launchMutex;
39 struct GrokLogger : public MessengerLogger {
40 explicit GrokLogger(const std::string &preamble) : MessengerLogger(preamble)
42 virtual ~GrokLogger() = default;
43 void info(const char* fmt, ...) override{
46 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_GENERAL);
49 void warn(const char* fmt, ...) override{
52 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_WARNING);
55 void error(const char* fmt, ...) override{
58 dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_ERROR);
63 struct GrokInitializer {
64 GrokInitializer(void) {
65 setMessengerLogger(new GrokLogger("[GROK] "));
67 ~GrokInitializer() = default;
71 FrameProxy(int index, Eyes eyes, DCPVideo dcpv) : index_(index), eyes_(eyes), vf(dcpv)
76 Eyes eyes(void) const {
84 struct DcpomaticContext {
85 DcpomaticContext(std::shared_ptr<const Film> film, Writer& writer,
86 EventHistory &history, const std::string &location) :
87 film_(film), writer_(writer),
88 history_(history), location_(location),
91 void setDimensions(uint32_t w, uint32_t h) {
95 std::shared_ptr<const Film> film_;
97 EventHistory &history_;
98 std::string location_;
105 explicit GrokContext(const DcpomaticContext &dcpomaticContext) :
106 dcpomaticContext_(dcpomaticContext),
110 struct CompressedData : public dcp::Data {
111 explicit CompressedData(int dataLen) : data_(new uint8_t[dataLen]), dataLen_(dataLen)
113 ~CompressedData(void){
116 uint8_t const * data () const override {
119 uint8_t * data () override {
122 int size () const override {
128 if (Config::instance()->enable_gpu ()) {
129 boost::filesystem::path folder(dcpomaticContext_.location_);
130 boost::filesystem::path binaryPath = folder / "grk_compress";
131 if (!boost::filesystem::exists(binaryPath)) {
132 getMessengerLogger()->error("Invalid binary location %s",
133 dcpomaticContext_.location_.c_str());
136 auto proc = [this](const std::string& str) {
139 auto tag = msg.next();
140 if(tag == GRK_MSGR_BATCH_SUBMIT_COMPRESSED)
142 auto clientFrameId = msg.nextUint();
143 auto compressedFrameId = msg.nextUint();
144 (void)compressedFrameId;
145 auto compressedFrameLength = msg.nextUint();
147 [this](FrameProxy srcFrame, uint8_t* compressed, uint32_t compressedFrameLength)
149 auto compressedData = std::make_shared<CompressedData>(compressedFrameLength);
150 memcpy(compressedData->data_,compressed,compressedFrameLength );
151 dcpomaticContext_.writer_.write(compressedData, srcFrame.index(), srcFrame.eyes());
154 int const minimum_size = 16384;
155 bool needsRecompression = compressedFrameLength < minimum_size;
156 messenger_->processCompressed(str, processor, needsRecompression);
157 if (needsRecompression) {
158 auto fp = messenger_->retrieve(clientFrameId);
163 auto encoded = std::make_shared<dcp::ArrayData>(fp->vf.encode_locally());
164 dcpomaticContext_.writer_.write(encoded, fp->vf.index(), fp->vf.eyes());
168 } catch (std::exception &ex){
169 getMessengerLogger()->error("%s",ex.what());
173 MessengerInit(clientToGrokMessageBuf, clientSentSynch, grokReceiveReadySynch,
174 grokToClientMessageBuf, grokSentSynch, clientReceiveReadySynch, proc,
175 std::thread::hardware_concurrency());
176 messenger_ = new ScheduledMessenger<FrameProxy>(clientInit);
182 bool launch(DCPVideo dcpv, int device){
187 std::unique_lock<std::mutex> lk_global(launchMutex);
192 if (MessengerInit::firstLaunch(true)) {
193 auto s = dcpv.get_size();
194 dcpomaticContext_.setDimensions(s.width, s.height);
195 auto config = Config::instance();
196 messenger_->launchGrok(dcpomaticContext_.location_,
197 dcpomaticContext_.width_,dcpomaticContext_.width_,
198 dcpomaticContext_.height_,
200 dcpomaticContext_.film_->resolution() == Resolution::FOUR_K,
201 dcpomaticContext_.film_->video_frame_rate(),
202 dcpomaticContext_.film_->j2k_bandwidth(),
203 config->gpu_license_server(),
204 config->gpu_license_port(),
205 config->gpu_license());
207 launched_ = messenger_->waitForClientInit();
211 bool scheduleCompress(DCPVideo const& vf){
215 auto fp = FrameProxy(vf.index(), vf.eyes(), vf);
216 auto cvt = [this, &fp](BufferSrc src){
218 fp.vf.convert_to_xyz((uint16_t*)src.framePtr_);
220 return messenger_->scheduleCompress(fp, cvt);
226 std::unique_lock<std::mutex> lk_global(launchMutex);
230 messenger_->shutdown();
232 messenger_ = nullptr;
235 dcpomaticContext_.history_.event ();
238 DcpomaticContext dcpomaticContext_;
239 ScheduledMessenger<FrameProxy> *messenger_;