MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ load_into_container()

bool MayaFlux::IO::VideoFileReader::load_into_container ( std::shared_ptr< Kakshya::SignalSourceContainer container)
overridevirtual

Load file data into an existing container.

Parameters
containerTarget container (must be compatible type).
Returns
true if successful.

Implements MayaFlux::IO::FileReader.

Definition at line 252 of file VideoFileReader.cpp.

254{
255 if (!container) {
256 set_error("Invalid container");
257 return false;
258 }
259
260 auto vc = std::dynamic_pointer_cast<Kakshya::VideoFileContainer>(container);
261 if (!vc) {
262 set_error("Container is not a VideoFileContainer");
263 return false;
264 }
265
266 std::shared_ptr<VideoStreamContext> video;
267 std::shared_ptr<AudioStreamContext> audio;
268 std::shared_ptr<FFmpegDemuxContext> demux;
269 {
270 std::shared_lock lock(m_context_mutex);
271 if (!m_demux || !m_video) {
272 set_error("File not open");
273 return false;
274 }
275 video = m_video;
276 audio = m_audio;
277 demux = m_demux;
278 }
279
280 const uint64_t total = video->total_frames;
281 if (total == 0) {
282 set_error("Video stream reports 0 frames");
283 return false;
284 }
285
286 const uint32_t ring_cap = std::min(
288 static_cast<uint32_t>(total));
289
290 const uint32_t threshold = (m_refill_threshold > 0)
292 : ring_cap / 4;
293
294 vc->setup_ring(total, ring_cap,
295 video->out_width, video->out_height,
296 video->out_bytes_per_pixel, video->frame_rate,
297 threshold, m_reader_id);
298
299 m_sws_buf.resize(
300 static_cast<size_t>(video->out_linesize) * video->out_height);
301
304 && demux->find_best_stream(AVMEDIA_TYPE_AUDIO) >= 0;
305
306 if (want_audio && audio && audio->is_valid()) {
307 {
308 std::unique_lock lock(m_context_mutex);
309 demux->seek(audio->stream_index, 0);
310 audio->flush_codec();
311 audio->drain_resampler_init();
312 }
313
314 SoundFileReader audio_reader;
315 audio_reader.set_audio_options(m_audio_options);
316 audio_reader.set_target_sample_rate(m_target_sample_rate);
317
318 if (audio_reader.open_from_demux(demux, audio, m_filepath, m_options)) {
319 auto sc = audio_reader.create_container();
320 if (audio_reader.load_into_container(sc)) {
321 m_audio_container = std::dynamic_pointer_cast<Kakshya::SoundFileContainer>(sc);
322 } else {
324 "VideoFileReader: audio load failed: {}",
325 audio_reader.get_last_error());
326 }
327 } else {
329 "VideoFileReader: open_from_demux failed: {}",
330 audio_reader.get_last_error());
331 }
332
333 {
334 std::unique_lock lock(m_context_mutex);
335 demux->seek(video->stream_index, 0);
336 video->flush_codec();
337 }
338 }
339
340 m_decode_head.store(0);
341 m_container_ref = vc;
342
343 const uint64_t preload = std::min(
344 static_cast<uint64_t>(ring_cap),
345 total);
346
347 uint64_t decoded = decode_batch(*vc, preload);
348
349 if (decoded == 0) {
350 set_error("Failed to decode any frames during preload");
351 return false;
352 }
353
355 "VideoFileReader: preloaded {}/{} frames ({}x{}, {:.1f} fps, ring={})",
356 decoded, total,
357 video->out_width, video->out_height,
358 video->frame_rate, ring_cap);
359
360 auto regions = get_regions();
361 auto region_groups = regions_to_groups(regions);
362 for (const auto& [name, group] : region_groups)
363 vc->add_region_group(group);
364
365 vc->create_default_processor();
366 vc->mark_ready_for_processing(true);
367
368 if (decoded < total)
370
371 return true;
372}
#define MF_INFO(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
static std::unordered_map< std::string, Kakshya::RegionGroup > regions_to_groups(const std::vector< FileRegion > &regions)
Convert file regions to region groups.
uint64_t decode_batch(Kakshya::VideoFileContainer &vc, uint64_t batch_size)
Decode up to batch_size frames starting at m_decode_head.
void set_error(const std::string &msg) const
std::vector< FileRegion > get_regions() const override
Get semantic regions from the file.
std::vector< uint8_t > m_sws_buf
One-frame sws scratch buffer (padded linesize, reused by decode thread).
std::weak_ptr< Kakshya::VideoFileContainer > m_container_ref
std::shared_ptr< FFmpegDemuxContext > m_demux
std::shared_ptr< VideoStreamContext > m_video
std::shared_ptr< AudioStreamContext > m_audio
std::shared_ptr< Kakshya::SoundFileContainer > m_audio_container
std::atomic< uint64_t > m_decode_head
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
void add_region_group(std::unordered_map< std::string, RegionGroup > &groups, const RegionGroup &group)
Add a RegionGroup to a group map.

References MayaFlux::IO::SoundFileReader::create_container(), decode_batch(), MayaFlux::IO::EXTRACT_AUDIO, MayaFlux::Journal::FileIO, MayaFlux::IO::SoundFileReader::get_last_error(), get_regions(), MayaFlux::Journal::IO, MayaFlux::IO::SoundFileReader::load_into_container(), m_audio, m_audio_container, m_audio_options, m_container_ref, m_context_mutex, m_decode_head, m_demux, m_filepath, m_options, m_reader_id, m_refill_threshold, m_ring_capacity, m_sws_buf, m_target_sample_rate, m_video, m_video_options, MF_INFO, MF_WARN, MayaFlux::IO::NONE, MayaFlux::IO::SoundFileReader::open_from_demux(), MayaFlux::IO::FileReader::regions_to_groups(), MayaFlux::IO::SoundFileReader::set_audio_options(), set_error(), MayaFlux::IO::SoundFileReader::set_target_sample_rate(), and start_decode_thread().

+ Here is the call graph for this function: