MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RegionProcessorBase.cpp
Go to the documentation of this file.
3
5
6namespace MayaFlux::Kakshya {
7
8void RegionProcessorBase::on_attach(const std::shared_ptr<SignalSourceContainer>& container)
9{
10 if (!container) {
11 error<std::invalid_argument>(Journal::Component::Kakshya, Journal::Context::ContainerProcessing, std::source_location::current(),
12 "Container cannot be null");
13 }
14
15 m_container_weak = container;
16 m_structure = container->get_structure();
17
18 m_cache_manager = std::make_unique<RegionCacheManager>(m_max_cache_size);
19 m_cache_manager->initialize();
20
22
23 organize_container_data(container);
24
25 container->mark_ready_for_processing(true);
26}
27
28void RegionProcessorBase::on_detach(const std::shared_ptr<SignalSourceContainer>& /*container*/)
29{
30 m_container_weak.reset();
31 m_cache_manager.reset();
32 m_organized_regions.clear();
33 m_current_position.clear();
34}
35
36void RegionProcessorBase::cache_region_if_needed(const RegionSegment& segment, const std::shared_ptr<SignalSourceContainer>& container)
37{
39 return;
40
41 if (m_cache_manager->get_cached_segment(segment)) {
42 return;
43 }
44
45 uint64_t segment_size = segment.get_total_elements();
46 if (segment_size <= m_max_cache_size / 10) { // Use max 10% of cache per segment
47 try {
48 RegionCache cache;
49 cache.data = container->get_region_data(segment.source_region);
50 cache.source_region = segment.source_region;
51 cache.load_time = std::chrono::steady_clock::now();
52
53 m_cache_manager->cache_region(cache);
54 } catch (const std::exception& e) {
56 "Failed to cache region segment: {}", e.what());
57 }
58 }
59}
60
61bool RegionProcessorBase::advance_position(std::vector<uint64_t>& position, uint64_t steps, const OrganizedRegion* region)
62{
63 if (position.empty())
64 return false;
65
66 for (auto& pos : position) {
67 pos += steps;
68 }
69
70 if ((region != nullptr) && region->looping_enabled && !region->loop_start.empty() && !region->loop_end.empty()) {
71 for (size_t dim = 0; dim < std::min(position.size(), region->loop_start.size()); ++dim) {
72 if (position[dim] >= region->loop_end[dim]) {
73 position[dim] = region->loop_start[dim] + (position[dim] - region->loop_end[dim]);
74 }
75 }
76 }
77
78 auto frame_count = m_structure.get_frame_count();
79 for (auto& pos : position) {
80 if (pos >= frame_count) {
81 pos = frame_count - 1;
82 return false;
83 }
84 }
85
86 return true;
87}
88
89void RegionProcessorBase::ensure_output_dimensioning(std::vector<DataVariant>& output_data, const std::vector<uint64_t>& required_shape)
90{
91 if (output_data.size() < required_shape[1]) {
92 output_data.resize(required_shape[1], DataVariant {});
93 }
94
95 for (auto& data : output_data) {
96 std::visit([required_shape](auto& dt) {
97 if (dt.size() < required_shape[0]) {
98 dt.resize(required_shape[0]);
99 }
100 },
101 data);
102 }
103}
104
105}
#define MF_WARN(comp, ctx,...)
virtual void organize_container_data(const std::shared_ptr< SignalSourceContainer > &container)=0
Organize container data into structured regions.
virtual void cache_region_if_needed(const RegionSegment &segment, const std::shared_ptr< SignalSourceContainer > &container)
Cache a region's data if beneficial and not already cached.
std::unique_ptr< RegionCacheManager > m_cache_manager
virtual bool advance_position(std::vector< uint64_t > &position, uint64_t steps=1, const OrganizedRegion *region=nullptr)
Advance position according to memory layout and looping.
virtual void ensure_output_dimensioning(std::vector< DataVariant > &output_data, const std::vector< uint64_t > &required_shape)
Ensure output data is properly dimensioned for region extraction.
std::vector< OrganizedRegion > m_organized_regions
void on_detach(const std::shared_ptr< SignalSourceContainer > &container) override
Detach this processor from its container.
void on_attach(const std::shared_ptr< SignalSourceContainer > &container) override
Attach this processor to a signal source container.
std::weak_ptr< SignalSourceContainer > m_container_weak
@ ContainerProcessing
Container operations (Kakshya - file/stream/region processing)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
Definition NDData.hpp:76
static size_t get_frame_size(const std::vector< DataDimension > &dimensions)
Extract the size of non time dimensions (channel, spatial, frequency)
static size_t get_frame_count(const std::vector< DataDimension > &dimensions)
Extract frame count from video dimensions.
std::vector< uint64_t > loop_start
Loop start coordinates.
std::vector< uint64_t > loop_end
Loop end coordinates.
A structured audio region with metadata and transition information.
Region source_region
Region this cache corresponds to.
std::vector< DataVariant > data
Cached data.
std::chrono::steady_clock::time_point load_time
When cache was loaded.
Stores cached data for a region, with metadata for cache management.
Region source_region
Associated region.
uint64_t get_total_elements() const
Get the total number of elements in the segment.
Represents a discrete segment of audio data with caching capabilities.