27 , m_channels(channels)
28 , m_frame_rate(frame_rate)
83 uint32_t ring_capacity,
88 uint32_t refill_threshold,
111 auto&
pixels =
m_data[0].emplace<std::vector<uint8_t>>();
112 pixels.resize(frame_bytes * ring_capacity, 0);
114 m_slot_frame = std::vector<std::atomic<uint64_t>>(ring_capacity);
116 sf.store(UINT64_MAX, std::memory_order_relaxed);
127 flag.store(
true, std::memory_order_relaxed);
142 auto*
pixels = std::get_if<std::vector<uint8_t>>(&
m_data[0]);
163 sf.store(UINT64_MAX, std::memory_order_relaxed);
166 std::atomic_thread_fence(std::memory_order_release);
193 std::span<const uint8_t> result;
197 const auto*
pixels = std::get_if<std::vector<uint8_t>>(&
m_data[0]);
200 const size_t offset = frame_index * frame_bytes;
208 const uint32_t slot =
slot_for(frame_index);
209 if (
m_slot_frame[slot].load(std::memory_order_acquire) == frame_index) {
210 const auto*
pixels = std::get_if<std::vector<uint8_t>>(&
m_data[0]);
213 return {
pixels->data() + slot * frame_bytes, frame_bytes };
235 std::optional<std::vector<DataVariant>> result;
240 const auto*
pixels = std::get_if<std::vector<uint8_t>>(&
m_data[0]);
244 const std::span<const uint8_t> src {
pixels->data(),
pixels->size() };
247 }
catch (
const std::exception& e) {
249 "VideoStreamContainer::get_region_data extraction failed — {}", e.what());
253 return result.value_or(std::vector<DataVariant> {});
259 "VideoStreamContainer::set_region_data — write path not yet implemented");
264 std::optional<std::vector<DataVariant>> result;
268 return result.value_or(std::vector<DataVariant> {});
273 std::optional<std::vector<DataVariant>> result;
277 return result.value_or(std::vector<DataVariant> {});
289 std::optional<RegionGroup> result;
294 return result.value_or(empty);
299 std::optional<std::unordered_map<std::string, RegionGroup>> result;
303 return result.value_or(std::unordered_map<std::string, RegionGroup> {});
322 if (!position.empty())
333 const uint64_t head =
m_cache_head.load(std::memory_order_acquire);
334 const uint64_t buffered = (head > frame) ? (head - frame) : 0;
336 if (buffered < m_refill_threshold && m_io_service->request_decode)
342 thread_local std::vector<uint64_t> pos(1);
403 std::ranges::fill(
output, 0.0);
412 std::ranges::fill(
output, 0.0);
424 std::ranges::for_each(
m_data, [](
auto& v) {
425 std::visit([](
auto& vec) { vec.clear(); }, v);
441 const auto* v = std::get_if<std::vector<uint8_t>>(&
m_data[0]);
442 return (v && !v->empty()) ? v->data() :
nullptr;
454 result = std::visit([](
const auto& vec) {
return !vec.empty(); },
m_data[0]);
466 if (old != new_state)
479 std::function<
void(
const std::shared_ptr<SignalSourceContainer>&,
ProcessingState)> callback)
494 uint64_t start_frame,
496 const std::type_info& type)
const
498 if (type !=
typeid(uint8_t)) {
502 auto* out =
static_cast<uint8_t*
>(
output);
509 const size_t required =
static_cast<size_t>(num_frames) * frame_size;
511 if (
output.size() < required) {
512 error<std::runtime_error>(
515 std::source_location::current(),
516 "VideoStreamContainer::get_frames_typed: output buffer too small ({} < {})",
520 for (uint64_t i = 0; i < num_frames; ++i) {
522 std::ranges::copy(frame,
output.begin() +
static_cast<size_t>(i) * frame_size);
543 auto processor = std::make_shared<FrameAccessProcessor>();
561 old->on_detach(shared_from_this());
563 processor->on_attach(shared_from_this());
617 "VideoStreamContainer::channel_data — not meaningful for interleaved pixel data; returning full surface");
620 static DataVariant empty_variant = std::vector<uint8_t>();
635 const std::vector<uint64_t>& coords,
637 const std::type_info& type)
const
639 if (type !=
typeid(uint8_t) || coords.size() < 4)
642 const uint64_t frame = coords[0];
643 const uint64_t y = coords[1];
644 const uint64_t x = coords[2];
645 const uint64_t c = coords[3];
658 *
static_cast<uint8_t*
>(out) =
pixels[idx];
662 const std::vector<uint64_t>& coords,
664 const std::type_info& type)
666 if (type !=
typeid(uint8_t) || coords.size() < 4 ||
m_data.empty())
669 auto*
pixels = std::get_if<std::vector<uint8_t>>(&
m_data[0]);
673 const uint64_t frame = coords[0];
674 const uint64_t y = coords[1];
675 const uint64_t x = coords[2];
676 const uint64_t c = coords[3];
686 if (idx >=
pixels->size())
689 (*pixels)[idx] = *
static_cast<const uint8_t*
>(in);
699 const size_t new_size = frame_index + 1;
700 auto new_dirty = std::vector<std::atomic<bool>>(new_size);
702 for (
size_t i = 0; i < old_size; ++i) {
704 std::memory_order_relaxed);
707 for (
size_t i = old_size; i < new_size; ++i)
708 new_dirty[i].
store(
true, std::memory_order_relaxed);
737 flag.store(
true, std::memory_order_release);
#define MF_WARN(comp, ctx,...)
const std::vector< float > * pixels
std::shared_ptr< Core::VKImage > output
Type-erased accessor for NDData with semantic view construction.
std::vector< uint64_t > get_remaining_frames() const override
Get the number of remaining frames from the current position, per channel.
std::shared_ptr< DataProcessor > m_default_processor
std::vector< DataVariant > m_processed_data
void mark_dimension_consumed(uint32_t dimension_index, uint32_t reader_id) override
Mark a dimension as consumed for the current processing cycle.
uint32_t m_refill_threshold
Trigger refill when (m_cache_head - read_position) drops below this.
std::shared_ptr< DataProcessingChain > get_processing_chain() override
Get the current processing chain for this container.
std::shared_ptr< DataProcessingChain > m_processing_chain
std::function< void(const std::shared_ptr< SignalSourceContainer > &, ProcessingState)> m_state_callback
void set_region_data(const Region ®ion, const std::vector< DataVariant > &data) override
Set data for a specific region.
uint64_t peek_sequential(std::span< double > output, uint64_t count, uint64_t offset) const override
Peek at data without advancing the read position.
const std::vector< uint64_t > & get_read_position() const override
Get the current read position.
std::atomic< uint32_t > m_consumed_readers
uint8_t * mutable_slot_ptr(uint64_t frame_index)
Mutable pointer into m_data[0] for the decode thread to write into.
void get_value_impl(const std::vector< uint64_t > &coords, void *out, const std::type_info &type) const override
Type-erased single-element read.
bool is_at_end() const override
Check if read position has reached the end of the stream.
void unregister_dimension_reader(uint32_t dimension_index) override
Unregister a reader for a specific dimension.
std::vector< DataVariant > get_segments_data(const std::vector< RegionSegment > &segment) const override
Get data for multiple region segments efficiently.
std::vector< std::atomic< uint64_t > > m_slot_frame
bool is_ready() const override
Check if the stream is ready for reading.
DataAccess channel_data(size_t channel) override
Get channel data with semantic interpretation.
void get_frames_typed(std::span< uint8_t > output, uint64_t start_frame, uint64_t num_frames) const
void get_frames_impl(void *output, size_t count, uint64_t start_frame, uint64_t num_frames, const std::type_info &type) const override
Implementation-specific method to retrieve multiple frames.
std::vector< uint64_t > linear_index_to_coordinates(uint64_t linear_index) const override
Convert linear index to coordinates based on current memory layout.
std::vector< DataDimension > get_dimensions() const override
Get the dimensions describing the structure of the data.
RegionGroup get_region_group(const std::string &name) const override
Get a region group by name.
ProcessingState get_processing_state() const override
Get the current processing state of the container.
uint64_t read_sequential(std::span< double > output, uint64_t count) override
Read data sequentially from the current position.
uint64_t get_num_frames() const override
Get the number of frames in the primary (temporal) dimension.
void update_read_position_for_channel(size_t channel, uint64_t frame) override
Update the read position for a specific channel.
void create_default_processor() override
Create and configure a default processor for this container.
uint32_t register_dimension_reader(uint32_t dimension_index) override
Register a reader for a specific dimension.
Memory::LockFreeQueue< uint64_t, READY_QUEUE_CAPACITY > m_ready_queue
std::vector< DataAccess > all_channel_data() override
Get all channel data as accessors.
bool is_frame_available(uint64_t frame_index) const
Check if a frame is currently valid in the ring.
void unregister_state_change_callback() override
Unregister the state change callback, if any.
void add_region_group(const RegionGroup &group) override
Add a named group of regions to the container.
std::atomic< uint32_t > m_registered_readers
std::vector< std::atomic< bool > > m_float_frame_dirty
void set_value_impl(const std::vector< uint64_t > &coords, const void *in, const std::type_info &type) override
Type-erased single-element write.
std::vector< DataVariant > get_region_group_data(const RegionGroup &group) const override
Get data for multiple regions efficiently.
std::vector< std::vector< float > > m_float_frame_cache
void clear() override
Clear all data in the container.
std::vector< DataVariant > get_region_data(const Region ®ion) const override
Get data for a specific region.
uint64_t get_total_elements() const override
Get the total number of elements in the container.
bool has_data() const override
Check if the container currently holds any data.
VideoStreamContainer(uint32_t width=0, uint32_t height=0, uint32_t channels=4, double frame_rate=0.0)
Construct a VideoStreamContainer with specified parameters.
uint64_t get_temporal_rate() const override
Get the temporal rate (e.g., sample rate, frame rate) of the stream.
uint64_t time_to_position(double time) const override
Convert from time (seconds) to position units (e.g., frame/sample index).
const void * get_raw_data() const override
Get a raw pointer to the underlying data storage.
std::span< const float > processed_frame_as_float(uint64_t frame_index=0) const
Processed frame at frame_index as a normalised float span.
void load_region(const Region ®ion) override
Load a region into memory.
std::shared_ptr< DataProcessor > get_default_processor() const override
Get the current default data processor.
void remove_region_group(const std::string &name) override
Remove a region group by name.
void reset_float_frame_cache()
void reset_read_position() override
Reset read position to the beginning of the stream.
std::span< const uint8_t > get_frame_pixels(uint64_t frame_index) const
Get raw pixel data for a single frame as uint8_t span.
std::unordered_map< std::string, RegionGroup > m_region_groups
void advance_cache_head(uint64_t frame_index)
Advance the container's view of how many frames have been decoded.
Memory::Seqlock m_cb_lock
void setup_ring(uint64_t total_frames, uint32_t ring_capacity, uint32_t width, uint32_t height, uint32_t channels, double frame_rate, uint32_t refill_threshold, uint64_t reader_id=0)
Allocate m_data[0] as a ring of ring_capacity frames.
void invalidate_float_frame_cache(uint32_t slot_index=0)
void set_looping(bool enable) override
Enable or disable looping behavior for the stream.
Memory::Seqlock m_data_lock
double position_to_time(uint64_t position) const override
Convert from position units (e.g., frame/sample index) to time (seconds).
void notify_state_change(ProcessingState new_state)
uint64_t get_frame_size() const override
Get the number of elements that constitute one "frame".
void set_loop_region(const Region ®ion) override
Set the loop region using a Region.
void unload_region(const Region ®ion) override
Unload a region from memory.
std::atomic< uint64_t > m_cache_head
Highest frame index committed by the decode thread.
std::vector< DataVariant > m_data
uint32_t slot_for(uint64_t frame_index) const
void set_default_processor(const std::shared_ptr< DataProcessor > &processor) override
Set the default data processor for this container.
void mark_ready_for_processing(bool ready) override
Mark the container as ready or not ready for processing.
size_t get_frame_byte_size() const
Get the total byte size of one frame (width * height * channels).
void register_state_change_callback(std::function< void(const std::shared_ptr< SignalSourceContainer > &, ProcessingState)> callback) override
Register a callback to be invoked on processing state changes.
void process_default() override
Process the container's data using the default processor.
void invalidate_ring()
Invalidate all ring slots.
ContainerDataStructure m_structure
void advance_read_position(const std::vector< uint64_t > &frames) override
Advance the read position by a specified amount.
uint64_t coordinates_to_linear_index(const std::vector< uint64_t > &coordinates) const override
Convert coordinates to linear index based on current memory layout.
uint64_t m_total_source_frames
void set_read_position(const std::vector< uint64_t > &position) override
Set the current read position in the primary temporal dimension per channel.
bool is_ready_for_processing() const override
Check if the container is ready for processing.
std::atomic< uint64_t > m_read_position
bool has_active_readers() const override
Check if any dimensions currently have active readers.
bool is_region_loaded(const Region ®ion) const override
Check if a region is loaded in memory.
void commit_frame(uint64_t frame_index)
Publish a decoded frame.
std::unordered_map< std::string, RegionGroup > get_all_region_groups() const override
Get all region groups in the container.
void update_processing_state(ProcessingState new_state) override
Update the processing state of the container.
Region get_loop_region() const override
Get the current loop region.
bool all_dimensions_consumed() const override
Check if all active dimensions have been consumed in this cycle.
std::atomic< ProcessingState > m_processing_state
void set_memory_layout(MemoryLayout layout) override
Set the memory layout for this container.
Memory::Seqlock m_region_lock
Registry::Service::IOService * m_io_service
bool is_looping() const override
Check if looping is enabled for the stream.
std::span< const uint8_t > get_frame_typed(uint64_t frame_index) const
RAII guard that brackets a Seqlock write region.
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
@ ContainerProcessing
Container operations (Kakshya - file/stream/region processing)
@ Runtime
General runtime operations (default fallback)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
ProcessingState
Represents the current processing lifecycle state of a container.
@ READY
Container has data loaded and is ready for processing.
@ IDLE
Container is inactive with no data or not ready for processing.
@ PROCESSING
Container is actively being processed.
@ PROCESSED
Container has completed processing and results are available.
uint64_t coordinates_to_linear(const std::vector< uint64_t > &coords, const std::vector< DataDimension > &dimensions)
Convert N-dimensional coordinates to a linear index for interleaved data.
std::span< const float > as_normalised_float(const DataVariant &variant, std::vector< float > &storage)
Extract a DataVariant holding pixel data as a normalised float span.
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.
@ VIDEO_COLOR
4D video (time + 2D + color)
std::vector< uint64_t > linear_to_coordinates(uint64_t index, const std::vector< DataDimension > &dimensions)
Convert a linear index to N-dimensional coordinates for interleaved data.
MemoryLayout
Memory layout for multi-dimensional data.
@ ROW_MAJOR
C/C++ style (last dimension varies fastest)
std::shared_ptr< T > store(std::shared_ptr< T > obj)
Transfer ownership of an existing object to the persistent store for process lifetime.
std::vector< DataDimension > dimensions
static ContainerDataStructure image_interleaved()
Create structure for interleaved image data.
MemoryLayout memory_layout
static std::vector< DataDimension > create_dimensions(DataModality modality, const std::vector< uint64_t > &shape, MemoryLayout layout=MemoryLayout::ROW_MAJOR)
Create dimension descriptors for a data modality.
std::string name
Descriptive name of the group.
Organizes related signal regions into a categorized collection.
Represents a point or span in N-dimensional space.
std::function< void(uint64_t reader_id)> request_decode
Request the identified reader to decode the next batch of frames.
Backend IO streaming service interface.