X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Fmjpeg_encoder.cpp;h=71af1693778aa51684c8c98ba65c99582f779cde;hb=f81ae3be1aae619fe4ad022f55d95a4a83ace076;hp=2cd1607bac6e1f6055439e2f62a3407135341a10;hpb=0c7201c2d136870ea8c5fe205bee21207369312c;p=nageru diff --git a/nageru/mjpeg_encoder.cpp b/nageru/mjpeg_encoder.cpp index 2cd1607..71af169 100644 --- a/nageru/mjpeg_encoder.cpp +++ b/nageru/mjpeg_encoder.cpp @@ -1,32 +1,59 @@ #include "mjpeg_encoder.h" +#include +#include #include +#include #include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #if __SSE2__ #include #endif -#include extern "C" { +#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include } -#include "defs.h" -#include "shared/ffmpeg_raii.h" #include "flags.h" +#include "pbo_frame_allocator.h" +#include "ref_counted_frame.h" +#include "shared/ffmpeg_raii.h" #include "shared/httpd.h" #include "shared/memcpy_interleaved.h" #include "shared/metrics.h" -#include "pbo_frame_allocator.h" +#include "shared/shared_defs.h" #include "shared/timebase.h" #include "shared/va_display.h" - -#include - -#include -#include -#include +#include "shared/va_resource_pool.h" using namespace Eigen; using namespace bmusb; @@ -190,8 +217,9 @@ void add_audio_stream(AVFormatContext *avctx) stream->time_base = AVRational{ 1, OUTPUT_FREQUENCY }; stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; stream->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE; - stream->codecpar->channel_layout = AV_CH_LAYOUT_STEREO; - stream->codecpar->channels = 2; + stream->codecpar->ch_layout.order = AV_CHANNEL_ORDER_NATIVE; + stream->codecpar->ch_layout.nb_channels = 2; + stream->codecpar->ch_layout.u.mask = AV_CH_LAYOUT_STEREO; stream->codecpar->sample_rate = OUTPUT_FREQUENCY; } @@ -221,6 +249,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, { @@ -235,6 +264,7 @@ MJPEGEncoder::MJPEGEncoder(HTTPD *httpd, const string &va_display) encoder_thread = thread(&MJPEGEncoder::encoder_thread_func, this); if (va_dpy != nullptr) { + va_pool.reset(new VAResourcePool(va_dpy->va_dpy, uyvy_format, nv12_format, config_id_422, config_id_420, /*with_data_buffer=*/true)); va_receiver_thread = thread(&MJPEGEncoder::va_receiver_thread_func, this); } @@ -449,85 +479,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 +738,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 +747,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;