44#include <condition_variable>
60 unsigned int max_used_frames = 0,
64 size_t new_size = SIZE_MAX);
99 m_impl->reconfigure(new_desc, new_size);
101std::shared_ptr<video_frame>
104 return m_impl->get_frame();
109 return m_impl->get_disposable_frame();
114 return m_impl->get_pod_frame();
119 return m_impl->get_allocator();
142 : m_allocator(alloc.
clone()), m_max_used_frames(max_used_frames)
147 std::unique_lock<std::mutex> lk(m_lock);
148 remove_free_frames();
150 m_frame_returned.wait(lk, [
this] {
return m_unreturned_frames == 0;});
154 std::unique_lock<std::mutex> lk(m_lock);
157 remove_free_frames();
163 std::unique_lock<std::mutex> lk(m_lock);
164 assert(m_generation != 0);
165 if (!m_free_frames.empty()) {
166 ret = m_free_frames.front();
168 }
else if (m_max_used_frames > 0 && m_max_used_frames == m_unreturned_frames) {
169 m_frame_returned.wait(lk, [
this] {
return m_unreturned_frames < m_max_used_frames;});
170 assert(!m_free_frames.empty());
171 ret = m_free_frames.front();
176 for (
unsigned int i = 0; i <
m_desc.tile_count; ++i) {
178 (
char *) m_allocator->allocate(
181 throw std::runtime_error(
"Cannot allocate data");
185 }
catch (std::exception &e) {
186 std::cerr << e.what() << std::endl;
187 deallocate_frame(ret);
191 m_unreturned_frames += 1;
192 return std::shared_ptr<video_frame>(ret, std::bind([
this](
struct video_frame *
frame,
int generation) {
193 std::unique_lock<std::mutex> lk(m_lock);
195 assert(m_unreturned_frames > 0);
196 m_unreturned_frames -= 1;
197 m_frame_returned.notify_one();
199 if (this->m_generation != generation) {
200 this->deallocate_frame(
frame);
202 m_free_frames.push(
frame);
204 }, std::placeholders::_1, m_generation));
211 new std::shared_ptr<video_frame>(std::move(
frame));
212 static auto dispose = [](
video_frame *f) {
delete static_cast<std::shared_ptr<video_frame> *
>(f->callbacks.dispose_udata); };
224 new std::shared_ptr<video_frame>(std::move(
frame));
225 static auto deleter = [](
video_frame *f) {
delete static_cast<std::shared_ptr<video_frame> *
>(f->callbacks.dispose_udata); };
235 while (!m_free_frames.empty()) {
238 deallocate_frame(
frame);
257 out->reconfigure(
desc);
struct video_frame * frame
int void(** deleter)(void *))
void vf_free(struct video_frame *buf)
Frees video_frame structure.
struct video_desc video_desc_from_frame(const struct video_frame *frame)
Returns struct video_desc from video frame.
int vc_get_linesize(unsigned int width, codec_t codec)
struct video_frame * vf_alloc_desc(struct video_desc desc)
Allocates video frame accordig given desc.
struct video_frame_pool_allocator * clone() const override
void * allocate(size_t size) override
void deallocate(void *ptr) override
char * data
Raw tile data. Pointer must be at least 4B aligned.
unsigned int data_len
length of the data
unsigned int width
width of every tile (!)
unsigned int height
height of every tile (!)
void(* data_deleter)(struct video_frame *)
void(* dispose)(struct video_frame *)
void reconfigure(struct video_desc new_desc, size_t new_size=SIZE_MAX)
std::shared_ptr< video_frame > get_frame()
std::unique_ptr< video_frame_pool_allocator > m_allocator
video_frame_pool_allocator const & get_allocator()
impl(unsigned int max_used_frames=0, video_frame_pool_allocator const &alloc=default_data_allocator())
std::queue< struct video_frame * > m_free_frames
void remove_free_frames()
void deallocate_frame(struct video_frame *frame)
std::condition_variable m_frame_returned
unsigned int m_unreturned_frames
struct video_frame * get_pod_frame()
struct video_frame * get_disposable_frame()
unsigned int m_max_used_frames
virtual struct video_frame_pool_allocator * clone() const =0
struct video_frame * get_pod_frame()
std::unique_ptr< impl > m_impl
video_frame_pool(unsigned int max_used_frames=0, video_frame_pool_allocator const &alloc=default_data_allocator())
video_frame_pool_allocator const & get_allocator()
struct video_frame * get_disposable_frame()
std::shared_ptr< video_frame > get_frame()
video_frame_pool & operator=(video_frame_pool &&)
void reconfigure(struct video_desc new_desc, size_t new_size=SIZE_MAX)
Struct video_frame represents a video frame and contains video description.
struct tile tiles[FLEXIBLE_ARRAY_MEMBER]
struct video_frame_callbacks callbacks
EXTERN_C struct video_frame * video_frame_pool_get_disposable_frame(void *)
EXTERN_C void video_frame_pool_destroy(void *)
EXTERN_C void * video_frame_pool_init(struct video_desc desc, int len)