diff --git a/src/utils/video_frame_pool.cpp b/src/utils/video_frame_pool.cpp index 85b1b71a4..f9b6c43b1 100644 --- a/src/utils/video_frame_pool.cpp +++ b/src/utils/video_frame_pool.cpp @@ -38,18 +38,18 @@ #include "video_frame_pool.h" void *video_frame_pool_init(struct video_desc desc, int len) { - auto out = new video_frame_pool(len); + auto *out = new video_frame_pool(len, default_data_allocator()); out->reconfigure(desc); return (void *) out; } struct video_frame *video_frame_pool_get_disposable_frame(void *state) { - auto s = static_cast* >(state); + auto *s = static_cast(state); return s->get_disposable_frame(); } void video_frame_pool_destroy(void *state) { - auto s = static_cast* >(state); + auto *s = static_cast(state); delete s; } diff --git a/src/utils/video_frame_pool.h b/src/utils/video_frame_pool.h index 575ab003c..9098eb497 100644 --- a/src/utils/video_frame_pool.h +++ b/src/utils/video_frame_pool.h @@ -54,16 +54,26 @@ #include #include -struct default_data_allocator { - void *allocate(size_t size) { +struct video_frame_pool_allocator { + virtual void *allocate(size_t size) = 0; + virtual void deallocate(void *ptr) = 0; + virtual struct video_frame_pool_allocator *clone() const = 0; + virtual ~video_frame_pool_allocator() {} +}; + + +struct default_data_allocator : public video_frame_pool_allocator { + void *allocate(size_t size) override { return malloc(size); } - void deallocate(void *ptr) { + void deallocate(void *ptr) override { free(ptr); } + struct video_frame_pool_allocator *clone() const override { + return new default_data_allocator(*this); + } }; -template struct video_frame_pool { public: /** @@ -72,7 +82,7 @@ struct video_frame_pool { * is called and that number of frames * is unreturned, get_frames() will block. */ - video_frame_pool(unsigned int max_used_frames = 0) : m_generation(0), m_desc(), m_max_data_len(0), m_unreturned_frames(0), m_max_used_frames(max_used_frames) { + video_frame_pool(unsigned int max_used_frames = 0, video_frame_pool_allocator const &alloc = default_data_allocator()) : m_allocator(alloc.clone()), m_generation(0), m_desc(), m_max_data_len(0), m_unreturned_frames(0), m_max_used_frames(max_used_frames) { } virtual ~video_frame_pool() { @@ -116,7 +126,7 @@ struct video_frame_pool { ret = vf_alloc_desc(m_desc); for (unsigned int i = 0; i < m_desc.tile_count; ++i) { ret->tiles[i].data = (char *) - m_allocator.allocate(m_max_data_len); + m_allocator->allocate(m_max_data_len); if (ret->tiles[i].data == NULL) { throw std::runtime_error("Cannot allocate data"); } @@ -165,8 +175,8 @@ struct video_frame_pool { return out; } - allocator & get_allocator() { - return m_allocator; + video_frame_pool_allocator const & get_allocator() { + return *m_allocator; } private: @@ -182,11 +192,12 @@ struct video_frame_pool { if (frame == NULL) return; for (unsigned int i = 0; i < frame->tile_count; ++i) { - m_allocator.deallocate(frame->tiles[i].data); + m_allocator->deallocate(frame->tiles[i].data); } vf_free(frame); } + std::unique_ptr m_allocator; std::queue m_free_frames; std::mutex m_lock; std::condition_variable m_frame_returned; @@ -194,7 +205,6 @@ struct video_frame_pool { struct video_desc m_desc; size_t m_max_data_len; unsigned int m_unreturned_frames; - allocator m_allocator; unsigned int m_max_used_frames; }; #endif // __cplusplus diff --git a/src/video_capture/aja.cpp b/src/video_capture/aja.cpp index ac6bff5e0..e5570aa39 100644 --- a/src/video_capture/aja.cpp +++ b/src/video_capture/aja.cpp @@ -161,7 +161,7 @@ class vidcap_state_aja { uint32_t mVideoBufferSize{}; /// My video buffer size, in bytes uint32_t mAudioBufferSize{}; /// My audio buffer size, in bytes thread mProducerThread; /// My producer thread object -- does the frame capturing - video_frame_pool mPool; + video_frame_pool mPool; shared_ptr mOutputFrame; shared_ptr mOutputAudioFrame; size_t mOutputAudioFrameSize{}; diff --git a/src/video_compress/cmpto_j2k.cpp b/src/video_compress/cmpto_j2k.cpp index 974f40eb3..af29a8eb6 100644 --- a/src/video_compress/cmpto_j2k.cpp +++ b/src/video_compress/cmpto_j2k.cpp @@ -96,7 +96,7 @@ struct state_video_compress_j2k { struct cmpto_j2k_enc_cfg *enc_settings{}; long long int rate; ///< bitrate in bits per second int mct; // force use of mct - -1 means default - video_frame_pool pool; ///< pool for frames allocated by us but not yet consumed by encoder + video_frame_pool pool; ///< pool for frames allocated by us but not yet consumed by encoder unsigned int max_in_frames; ///< max number of frames between push and pop unsigned int in_frames{}; ///< number of currently encoding frames mutex lock; diff --git a/src/video_compress/cuda_dxt.cpp b/src/video_compress/cuda_dxt.cpp index b7384e20e..fb60fe02f 100644 --- a/src/video_compress/cuda_dxt.cpp +++ b/src/video_compress/cuda_dxt.cpp @@ -56,8 +56,8 @@ using namespace std; namespace { -struct cuda_buffer_data_allocator { - void *allocate(size_t size) { +struct cuda_buffer_data_allocator : public video_frame_pool_allocator { + void *allocate(size_t size) override { void *ptr; if (CUDA_WRAPPER_SUCCESS != cuda_wrapper_malloc_host(&ptr, size)) { @@ -65,9 +65,12 @@ struct cuda_buffer_data_allocator { } return ptr; } - void deallocate(void *ptr) { + void deallocate(void *ptr) override { cuda_wrapper_free(ptr); } + video_frame_pool_allocator *clone() const override { + return new cuda_buffer_data_allocator(*this); + } }; struct state_video_compress_cuda_dxt { @@ -81,7 +84,7 @@ struct state_video_compress_cuda_dxt { codec_t out_codec; decoder_t decoder; - video_frame_pool pool; + video_frame_pool pool{0, cuda_buffer_data_allocator()}; }; static void cuda_dxt_compress_done(struct module *mod); diff --git a/src/video_compress/dxt_glsl.cpp b/src/video_compress/dxt_glsl.cpp index 8cf192ddc..3a933dd5f 100644 --- a/src/video_compress/dxt_glsl.cpp +++ b/src/video_compress/dxt_glsl.cpp @@ -79,7 +79,7 @@ struct state_video_compress_rtdxt { struct gl_context gl_context; - video_frame_pool pool; + video_frame_pool pool; }; static int configure_with(struct state_video_compress_rtdxt *s, struct video_frame *frame); diff --git a/src/video_compress/gpujpeg.cpp b/src/video_compress/gpujpeg.cpp index 9798a067c..d733230c9 100644 --- a/src/video_compress/gpujpeg.cpp +++ b/src/video_compress/gpujpeg.cpp @@ -87,7 +87,7 @@ private: int m_device_id; struct gpujpeg_encoder *m_encoder; struct video_desc m_saved_desc; - video_frame_pool m_pool; + video_frame_pool m_pool; decoder_t m_decoder; codec_t m_enc_input_codec{}; unique_ptr m_decoded; diff --git a/src/video_compress/uyvy.cpp b/src/video_compress/uyvy.cpp index 9271bcc3c..aa1fade91 100644 --- a/src/video_compress/uyvy.cpp +++ b/src/video_compress/uyvy.cpp @@ -124,7 +124,7 @@ struct state_video_compress_uyvy { int gl_format; - video_frame_pool *pool; + video_frame_pool *pool; }; int uyvy_configure_with(struct state_video_compress_uyvy *s, struct video_frame *tx); @@ -156,7 +156,7 @@ struct module * uyvy_compress_init(struct module *parent, const char *) gl_context_make_current(NULL); - s->pool = new video_frame_pool(); + s->pool = new video_frame_pool(); module_init_default(&s->module_data); s->module_data.cls = MODULE_CLASS_DATA;