+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;
+
+ while (!video_window->isValid()) {
+ usleep(100000);
+ }
+
+ // Augh :-)
+ mutex mu;
+ condition_variable done_cv;
+ bool done = false;
+
+ post_to_main_thread([this, &frame, len, &done, &mu, &done_cv]{
+ video_window->makeCurrent();
+ glCreateBuffers(1, &frame->pbo);
+ glNamedBufferStorage(frame->pbo, len, nullptr, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT);
+ frame->data = (uint8_t *)glMapNamedBufferRange(frame->pbo, 0, len, GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT);
+ video_window->doneCurrent();
+
+ lock_guard lock(mu);
+ done = true;
+ done_cv.notify_all();
+ });
+ {
+ unique_lock lock(mu);
+ done_cv.wait(lock, [&done]{ return done; });
+ }
+
+ 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) {
+ GLuint pbo = frame->pbo;
+ post_to_main_thread([self, pbo]{
+ self->video_window->makeCurrent();
+ glUnmapNamedBuffer(pbo);
+ glDeleteBuffers(1, &pbo);
+ self->video_window->doneCurrent();
+ });
+ 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)