X-Git-Url: https://git.sesse.net/?p=nageru;a=blobdiff_plain;f=nageru%2Fmjpeg_encoder.cpp;fp=nageru%2Fmjpeg_encoder.cpp;h=26eb9a868f70ddeb6bbf23349318f7435d73565e;hp=2cd1607bac6e1f6055439e2f62a3407135341a10;hb=8850f9468d6a1727e7be302d2a9b4360f69210cc;hpb=58c351c4a2afa8377047c89ca55c367156fe36f9 diff --git a/nageru/mjpeg_encoder.cpp b/nageru/mjpeg_encoder.cpp index 2cd1607..26eb9a8 100644 --- a/nageru/mjpeg_encoder.cpp +++ b/nageru/mjpeg_encoder.cpp @@ -221,6 +221,7 @@ MJPEGEncoder::MJPEGEncoder(HTTPD *httpd, const string &va_display) add_stream(HTTPD::StreamID{ HTTPD::MULTICAM_STREAM, 0 }); // Initialize VA-API. + VAConfigID config_id_422, config_id_420; string error; va_dpy = try_open_va(va_display, { VAProfileJPEGBaseline }, VAEntrypointEncPicture, { @@ -233,6 +234,8 @@ MJPEGEncoder::MJPEGEncoder(HTTPD *httpd, const string &va_display) fprintf(stderr, "Could not initialize VA-API for MJPEG encoding: %s. JPEGs will be encoded in software if needed.\n", error.c_str()); } + va_pool.reset(new VAResourcePool(va_dpy->va_dpy, uyvy_format, nv12_format, config_id_422, config_id_420, /*with_data_buffer=*/true)); + encoder_thread = thread(&MJPEGEncoder::encoder_thread_func, this); if (va_dpy != nullptr) { va_receiver_thread = thread(&MJPEGEncoder::va_receiver_thread_func, this); @@ -449,85 +452,6 @@ private: VABufferID buf; }; -MJPEGEncoder::VAResources MJPEGEncoder::get_va_resources(unsigned width, unsigned height, uint32_t fourcc) -{ - { - lock_guard lock(va_resources_mutex); - for (auto it = va_resources_freelist.begin(); it != va_resources_freelist.end(); ++it) { - if (it->width == width && it->height == height && it->fourcc == fourcc) { - VAResources ret = *it; - va_resources_freelist.erase(it); - return ret; - } - } - } - - VAResources ret; - - ret.width = width; - ret.height = height; - ret.fourcc = fourcc; - - VASurfaceAttrib attrib; - attrib.flags = VA_SURFACE_ATTRIB_SETTABLE; - attrib.type = VASurfaceAttribPixelFormat; - attrib.value.type = VAGenericValueTypeInteger; - attrib.value.value.i = fourcc; - - VAStatus va_status; - VAConfigID config_id; - if (fourcc == VA_FOURCC_UYVY) { - va_status = vaCreateSurfaces(va_dpy->va_dpy, VA_RT_FORMAT_YUV422, width, height, &ret.surface, 1, &attrib, 1); - config_id = config_id_422; - } else { - assert(fourcc == VA_FOURCC_NV12); - va_status = vaCreateSurfaces(va_dpy->va_dpy, VA_RT_FORMAT_YUV420, width, height, &ret.surface, 1, &attrib, 1); - config_id = config_id_420; - } - - va_status = vaCreateContext(va_dpy->va_dpy, config_id, width, height, 0, &ret.surface, 1, &ret.context); - CHECK_VASTATUS(va_status, "vaCreateContext"); - - va_status = vaCreateBuffer(va_dpy->va_dpy, ret.context, VAEncCodedBufferType, width * height * 3 + 8192, 1, nullptr, &ret.data_buffer); - CHECK_VASTATUS(va_status, "vaCreateBuffer"); - - if (fourcc == VA_FOURCC_UYVY) { - va_status = vaCreateImage(va_dpy->va_dpy, &uyvy_format, width, height, &ret.image); - CHECK_VASTATUS(va_status, "vaCreateImage"); - } else { - assert(fourcc == VA_FOURCC_NV12); - va_status = vaCreateImage(va_dpy->va_dpy, &nv12_format, width, height, &ret.image); - CHECK_VASTATUS(va_status, "vaCreateImage"); - } - - return ret; -} - -void MJPEGEncoder::release_va_resources(MJPEGEncoder::VAResources resources) -{ - lock_guard lock(va_resources_mutex); - if (va_resources_freelist.size() > 50) { - auto it = va_resources_freelist.end(); - --it; - - VAStatus va_status = vaDestroyBuffer(va_dpy->va_dpy, it->data_buffer); - CHECK_VASTATUS(va_status, "vaDestroyBuffer"); - - va_status = vaDestroyContext(va_dpy->va_dpy, it->context); - CHECK_VASTATUS(va_status, "vaDestroyContext"); - - va_status = vaDestroySurfaces(va_dpy->va_dpy, &it->surface, 1); - CHECK_VASTATUS(va_status, "vaDestroySurfaces"); - - va_status = vaDestroyImage(va_dpy->va_dpy, it->image.image_id); - CHECK_VASTATUS(va_status, "vaDestroyImage"); - - va_resources_freelist.erase(it); - } - - va_resources_freelist.push_front(resources); -} - namespace { void push16(uint16_t val, string *str) @@ -787,7 +711,7 @@ void MJPEGEncoder::encode_jpeg_va(QueuedFrame &&qf) unsigned width = qf.video_format.width; unsigned height = qf.video_format.height; - VAResources resources; + VAResourcePool::VAResources resources; ReleaseVAResources release; if (userdata->data_copy_current_src == PBOFrameAllocator::Userdata::FROM_VA_API) { assert(is_uyvy(qf.frame)); @@ -796,12 +720,12 @@ void MJPEGEncoder::encode_jpeg_va(QueuedFrame &&qf) } else { assert(userdata->data_copy_current_src == PBOFrameAllocator::Userdata::FROM_MALLOC); if (is_uyvy(qf.frame)) { - resources = get_va_resources(width, height, VA_FOURCC_UYVY); + resources = va_pool->get_va_resources(width, height, VA_FOURCC_UYVY); } else { assert(is_i420(qf.frame)); - resources = get_va_resources(width, height, VA_FOURCC_NV12); + resources = va_pool->get_va_resources(width, height, VA_FOURCC_NV12); } - release = ReleaseVAResources(this, resources); + release = ReleaseVAResources(va_pool.get(), resources); } int y_h_samp_factor, y_v_samp_factor;