+
+ if (mjpeg_encoder != nullptr &&
+ mjpeg_encoder->should_encode_mjpeg_for_card(card_index) &&
+ vf.userdata != nullptr) {
+ Userdata *ud = (Userdata *)vf.userdata;
+ vf.data_copy = ud->data_copy_malloc;
+ ud->data_copy_current_src = Userdata::FROM_MALLOC;
+ } else {
+ vf.data_copy = nullptr;
+ }
+
+ return vf;
+}
+
+bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride)
+{
+ Frame vf;
+
+ size_t desired_frame_bytes = width * stride;
+ if (stride > 8192 * 4 || height > 8192 || desired_frame_bytes > MAX_FRAME_SIZE) {
+ return vf;
+ }
+
+ {
+ lock_guard<mutex> lock(freelist_mutex);
+ if (freelist.empty()) {
+ printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
+ vf.len = 0;
+ vf.overflow = 0;
+ return vf;
+ } else {
+ vf = freelist.front();
+ freelist.pop();
+ }
+ }
+
+ Userdata *userdata = (Userdata *)vf.userdata;
+ assert(generation == userdata->generation);
+ if (vf.size < desired_frame_bytes || (vf.size > FRAME_SIZE && vf.size > desired_frame_bytes * 2)) {
+ // Frame is either too small or way too large, so reallocate it.
+ // Note that width and height now automatically becomes the right size
+ // (the one we just asked for, instead of the default for the allocator,
+ // which is generally the global resolution); it doesn't matter
+ // for correctness, since we'll recreate the texture on upload if needed,
+ // but it is nice to save that step.
+ destroy_frame(&vf);
+ init_frame(vf, userdata, this, pixel_format, std::max<size_t>(desired_frame_bytes, FRAME_SIZE), width, height, permissions, map_bits, buffer, generation);
+ };
+
+ vf.len = 0;
+ vf.overflow = 0;
+
+ if (mjpeg_encoder != nullptr &&
+ mjpeg_encoder->should_encode_mjpeg_for_card(card_index)) {
+ if (mjpeg_encoder->using_vaapi()) {
+ VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
+ VAResourcePool::VAResources resources = mjpeg_encoder->get_va_pool()->get_va_resources(width, height, VA_FOURCC_UYVY); // Only used by DeckLinkCapture, so always 4:2:2.
+ ReleaseVAResources release(mjpeg_encoder->get_va_pool(), resources);
+
+ if (resources.image.pitches[0] == stride) {
+ userdata->va_resources = move(resources);
+ userdata->va_resources_release = move(release);
+
+ VAStatus va_status = vaMapBuffer(va_dpy, resources.image.buf, (void **)&vf.data_copy);
+ CHECK_VASTATUS(va_status, "vaMapBuffer");
+ vf.data_copy += resources.image.offsets[0];
+ userdata->data_copy_current_src = Userdata::FROM_VA_API;
+ } else {
+ printf("WARNING: Could not copy directly into VA-API MJPEG buffer for %zu x %zu, since producer and consumer disagreed on stride (%zu != %d).\n", width, height, stride, resources.image.pitches[0]);
+ vf.data_copy = userdata->data_copy_malloc;
+ userdata->data_copy_current_src = Userdata::FROM_MALLOC;
+ }
+ } else {
+ vf.data_copy = userdata->data_copy_malloc;
+ userdata->data_copy_current_src = Userdata::FROM_MALLOC;
+ }
+ } else {
+ vf.data_copy = nullptr;
+ }
+