Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion include/utils/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,26 @@ int safe_strcat(char *dest, const char *src, size_t size);
*/
void secure_zero_memory(void *ptr, size_t size);

#endif /* MEMORY_UTILS_H */
/**
* Track memory allocations for debugging and leak detection
*
* @param size Size of memory being allocated or freed
* @param is_allocation True if allocating, false if freeing
*/
void track_memory_allocation(size_t size, bool is_allocation);

/**
* Get the total amount of memory currently allocated
*
* @return Total memory allocated in bytes
*/
size_t get_total_memory_allocated(void);

/**
* Get the peak memory usage since program start
*
* @return Peak memory allocated in bytes
*/
size_t get_peak_memory_allocated(void);

#endif /* MEMORY_UTILS_H */
76 changes: 76 additions & 0 deletions include/video/detection_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef DETECTION_BUFFER_H
#define DETECTION_BUFFER_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>

// Buffer pool item structure
typedef struct {
uint8_t *buffer;
size_t size;
bool in_use;
time_t last_used; // Track when the buffer was last used
} buffer_pool_item_t;

/**
* Initialize the buffer pool
* This should be called at startup
*
* @return 0 on success, non-zero on failure
*/
int init_buffer_pool(void);

/**
* Cleanup the buffer pool
* This should be called at shutdown
*/
void cleanup_buffer_pool(void);

/**
* Get a buffer from the pool
*
* @param required_size The required buffer size
* @return Pointer to the buffer or NULL on failure
*/
uint8_t* get_buffer_from_pool(size_t required_size);

/**
* Return a buffer to the pool
*
* @param buffer Pointer to the buffer
* @return 0 on success, non-zero on failure
*/
int return_buffer_to_pool(uint8_t *buffer);

/**
* Get the number of active buffers
*
* @return Number of active buffers
*/
int get_active_buffer_count(void);

/**
* Get the maximum number of buffers in the pool
*
* @return Maximum number of buffers
*/
int get_max_buffer_count(void);

/**
* Emergency cleanup of buffer pool
* This frees all buffers that have been in use for too long
* Call this when buffer allocation fails to recover from leaks
*/
void emergency_buffer_pool_cleanup(void);

/**
* Track memory allocations
*
* @param size Size of the allocation
* @param is_allocation True if allocating, false if freeing
*/
void track_memory_allocation(size_t size, bool is_allocation);

#endif /* DETECTION_BUFFER_H */
68 changes: 68 additions & 0 deletions include/video/detection_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef DETECTION_CONFIG_H
#define DETECTION_CONFIG_H

#include <stdbool.h>
#include <stddef.h>

// Model types
#define MODEL_TYPE_SOD "sod"
#define MODEL_TYPE_SOD_REALNET "sod_realnet"
#define MODEL_TYPE_TFLITE "tflite"

// System configuration
typedef struct {
// Memory constraints
int buffer_pool_size; // Maximum number of buffers in the pool
int concurrent_detections; // Maximum number of concurrent detections
int buffer_allocation_retries; // Number of retries for buffer allocation

// Downscaling factors
int downscale_factor_default; // No downscaling by default
int downscale_factor_cnn; // Downscaling for CNN models
int downscale_factor_realnet; // Downscaling for RealNet models

// Thresholds
float threshold_cnn; // Detection threshold for CNN models
float threshold_cnn_embedded; // Detection threshold for CNN models on embedded devices
float threshold_realnet; // Detection threshold for RealNet models

// Debug options
bool save_frames_for_debug; // Enable/disable frame saving
} detection_config_t;

// Default configurations
extern detection_config_t default_config;
extern detection_config_t embedded_config;

/**
* Initialize detection configuration
* This should be called at startup
*
* @return 0 on success, non-zero on failure
*/
int init_detection_config(void);

/**
* Get the current detection configuration
*
* @return Pointer to the current configuration
*/
detection_config_t* get_detection_config(void);

/**
* Set custom detection configuration
*
* @param config The configuration to set
* @return 0 on success, non-zero on failure
*/
int set_detection_config(const detection_config_t* config);

/**
* Get current memory usage statistics for detection
*
* @param total_memory Pointer to store total allocated memory
* @param peak_memory Pointer to store peak allocated memory
*/
void get_detection_memory_usage(size_t *total_memory, size_t *peak_memory);

#endif /* DETECTION_CONFIG_H */
59 changes: 59 additions & 0 deletions include/video/detection_embedded.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef DETECTION_EMBEDDED_H
#define DETECTION_EMBEDDED_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

/**
* Check if we're running on an embedded device
*
* @return true if running on an embedded device, false otherwise
*/
bool is_embedded_device(void);

/**
* Get the appropriate downscale factor based on model type and device
*
* @param model_type The model type (MODEL_TYPE_SOD, MODEL_TYPE_SOD_REALNET, etc.)
* @return The downscale factor
*/
int get_downscale_factor(const char *model_type);

/**
* Get the appropriate detection threshold based on model type and device
*
* @param model_type The model type (MODEL_TYPE_SOD, MODEL_TYPE_SOD_REALNET, etc.)
* @param configured_threshold The threshold configured by the user (0.0 for default)
* @return The detection threshold
*/
float get_detection_threshold(const char *model_type, float configured_threshold);

/**
* Downscale a frame for detection
*
* @param src_data Source frame data
* @param src_width Source frame width
* @param src_height Source frame height
* @param src_channels Source frame channels
* @param dst_data Destination frame data (must be pre-allocated)
* @param dst_width Destination frame width
* @param dst_height Destination frame height
* @param dst_channels Destination frame channels
* @return 0 on success, non-zero on failure
*/
int downscale_frame(const uint8_t *src_data, int src_width, int src_height, int src_channels,
uint8_t *dst_data, int dst_width, int dst_height, int dst_channels);

/**
* Calculate the memory requirements for detection
*
* @param width Frame width
* @param height Frame height
* @param channels Frame channels
* @param model_type The model type (MODEL_TYPE_SOD, MODEL_TYPE_SOD_REALNET, etc.)
* @return The memory requirements in bytes
*/
size_t calculate_detection_memory_requirements(int width, int height, int channels, const char *model_type);

#endif /* DETECTION_EMBEDDED_H */
35 changes: 29 additions & 6 deletions include/video/detection_integration.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#include <pthread.h>
#include <stdbool.h>

// Maximum number of concurrent detections
#define MAX_CONCURRENT_DETECTIONS 3

// Expose detection state variables for memory-constrained optimization
extern pthread_mutex_t active_detections_mutex;
extern int active_detections;
/**
* Initialize the detection integration system
* This should be called at startup
*
* @return 0 on success, non-zero on failure
*/
int init_detection_integration(void);

/**
* Process a decoded frame for detection
Expand All @@ -30,4 +31,26 @@ int process_decoded_frame_for_detection(const char *stream_name, AVFrame *frame,
*/
void cleanup_detection_resources(void);

/**
* Get the number of active detections
*
* @return Number of active detections
*/
int get_active_detection_count(void);

/**
* Get the maximum number of concurrent detections
*
* @return Maximum number of concurrent detections
*/
int get_max_detection_count(void);

/**
* Check if a detection is already in progress for a specific stream
*
* @param stream_name The name of the stream to check
* @return true if detection is in progress, false otherwise
*/
bool is_detection_in_progress(const char *stream_name);

#endif /* DETECTION_INTEGRATION_H */
37 changes: 36 additions & 1 deletion src/utils/memory.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#include "utils/memory.h"
#include "core/logger.h"

// Memory tracking variables
static size_t total_memory_allocated = 0;
static size_t peak_memory_allocated = 0;

// Safe memory allocation
void *safe_malloc(size_t size) {
if (size == 0) {
Expand Down Expand Up @@ -91,4 +97,33 @@ void secure_zero_memory(void *ptr, size_t size) {
while (size--) {
*p++ = 0;
}
}
}

// Track memory allocations
void track_memory_allocation(size_t size, bool is_allocation) {
if (is_allocation) {
total_memory_allocated += size;
if (total_memory_allocated > peak_memory_allocated) {
peak_memory_allocated = total_memory_allocated;
}
} else {
if (size <= total_memory_allocated) {
total_memory_allocated -= size;
} else {
// This should not happen, but handle it gracefully
log_warn("Memory tracking inconsistency: trying to free %zu bytes when only %zu are tracked",
size, total_memory_allocated);
total_memory_allocated = 0;
}
}
}

// Get total memory allocated
size_t get_total_memory_allocated(void) {
return total_memory_allocated;
}

// Get peak memory allocated
size_t get_peak_memory_allocated(void) {
return peak_memory_allocated;
}
Loading