MCPcopy Create free account
hub / github.com/OpenShot/libopenshot / GetFrame

Method GetFrame

src/CacheDisk.cpp:157–224  ·  view source on GitHub ↗

Get a frame from the cache (or NULL shared_ptr if no frame is found)

Source from the content-addressed store, hash-verified

155
156// Get a frame from the cache (or NULL shared_ptr if no frame is found)
157std::shared_ptr<Frame> CacheDisk::GetFrame(int64_t frame_number)
158{
159 // Create a scoped lock, to protect the cache from multiple threads
160 const std::lock_guard<std::recursive_mutex> lock(*cacheMutex);
161
162 // Does frame exists in cache?
163 if (frames.count(frame_number)) {
164 // Does frame exist on disk
165 QString frame_path(path.path() + "/" + QString("%1.").arg(frame_number) + QString(image_format.c_str()).toLower());
166 if (path.exists(frame_path)) {
167
168 // Load image file
169 auto image = std::make_shared<QImage>();
170 image->load(frame_path);
171
172 // Set pixel formatimage->
173 image = std::make_shared<QImage>(image->convertToFormat(QImage::Format_RGBA8888_Premultiplied));
174
175 // Create frame object
176 auto frame = std::make_shared<Frame>();
177 frame->number = frame_number;
178 frame->AddImage(image);
179
180 // Get audio data (if found)
181 QString audio_path(path.path() + "/" + QString("%1").arg(frame_number) + ".audio");
182 QFile audio_file(audio_path);
183 if (audio_file.exists()) {
184 // Open audio file
185 QTextStream in(&audio_file);
186 if (audio_file.open(QIODevice::ReadOnly)) {
187 int sample_rate = in.readLine().toInt();
188 int channels = in.readLine().toInt();
189 int sample_count = in.readLine().toInt();
190 int channel_layout = in.readLine().toInt();
191
192 // Set basic audio properties
193 frame->ResizeAudio(channels, sample_count, sample_rate, (ChannelLayout) channel_layout);
194
195 // Loop through audio samples and add to frame
196 int current_channel = 0;
197 int current_sample = 0;
198 float *channel_samples = new float[sample_count];
199 while (!in.atEnd()) {
200 // Add sample to channel array
201 channel_samples[current_sample] = in.readLine().toFloat();
202 current_sample++;
203
204 if (current_sample == sample_count) {
205 // Add audio to frame
206 frame->AddAudio(true, current_channel, 0, channel_samples, sample_count, 1.0);
207
208 // Increment channel, and reset sample position
209 current_channel++;
210 current_sample = 0;
211 }
212
213 }
214 }

Callers

nothing calls this directly

Calls 4

AddImageMethod · 0.80
openMethod · 0.80
ResizeAudioMethod · 0.80
AddAudioMethod · 0.80

Tested by

no test coverage detected