+shared_ptr<VideoWidget::Frame> VideoWidget::alloc_frame(unsigned width, unsigned height, unsigned chroma_width, unsigned chroma_height)
+{
+ lock_guard lock(freelist_mu);
+ for (auto it = frame_freelist.begin(); it != frame_freelist.end(); ++it) {
+ if ((*it)->width == width &&
+ (*it)->height == height &&
+ (*it)->chroma_width == chroma_width &&
+ (*it)->chroma_height == chroma_height) {
+ Frame *frame = *it;
+ frame_freelist.erase(it);
+ return shared_ptr<Frame>{frame, free_frame};
+ }
+ }
+
+ Frame *frame = new Frame;
+ frame->owner = this;
+ frame->width = width;
+ frame->height = height;
+ frame->chroma_width = chroma_width;
+ frame->chroma_height = chroma_height;
+
+ size_t len = frame->width * frame->height + 2 * frame->chroma_width * frame->chroma_height;
+ frame->data.reset(new uint8_t[len]);
+
+ return shared_ptr<Frame>{frame, free_frame};
+}
+
+void VideoWidget::free_frame(VideoWidget::Frame *frame)
+{
+ VideoWidget *self = frame->owner;
+ lock_guard lock(self->freelist_mu);
+ if (self->frame_freelist.size() >= 16) {
+ delete self->frame_freelist.front();
+ self->frame_freelist.pop_front();
+ }
+ self->frame_freelist.push_back(frame);
+}
+
+shared_ptr<VideoWidget::Frame> VideoWidget::make_video_frame(const AVFrame *frame)