]> git.sesse.net Git - nageru/blobdiff - nageru/mjpeg_encoder.cpp
Unify VA-API initialization.
[nageru] / nageru / mjpeg_encoder.cpp
index 857346db7592d67a79305cef7a1c643b167912bf..2cd1607bac6e1f6055439e2f62a3407135341a10 100644 (file)
@@ -222,7 +222,13 @@ MJPEGEncoder::MJPEGEncoder(HTTPD *httpd, const string &va_display)
 
        // Initialize VA-API.
        string error;
-       va_dpy = try_open_va(va_display, &error, &config_id_422, &config_id_420);
+       va_dpy = try_open_va(va_display, { VAProfileJPEGBaseline }, VAEntrypointEncPicture,
+               {
+                       { "4:2:2", VA_RT_FORMAT_YUV422, VA_FOURCC_UYVY, &config_id_422, &uyvy_format },
+                       // We'd prefer VA_FOURCC_I420, but it's not supported by Intel's driver.
+                       { "4:2:0", VA_RT_FORMAT_YUV420, VA_FOURCC_NV12, &config_id_420, &nv12_format }
+               },
+               /*chosen_profile=*/nullptr, &error);
        if (va_dpy == nullptr) {
                fprintf(stderr, "Could not initialize VA-API for MJPEG encoding: %s. JPEGs will be encoded in software if needed.\n", error.c_str());
        }
@@ -271,87 +277,6 @@ void MJPEGEncoder::stop()
        }
 }
 
-unique_ptr<VADisplayWithCleanup> MJPEGEncoder::try_open_va(const string &va_display, string *error, VAConfigID *config_id_422, VAConfigID *config_id_420)
-{
-       unique_ptr<VADisplayWithCleanup> va_dpy = va_open_display(va_display);
-       if (va_dpy == nullptr) {
-               if (error) *error = "Opening VA display failed";
-               return nullptr;
-       }
-       int major_ver, minor_ver;
-       VAStatus va_status = vaInitialize(va_dpy->va_dpy, &major_ver, &minor_ver);
-       if (va_status != VA_STATUS_SUCCESS) {
-               char buf[256];
-               snprintf(buf, sizeof(buf), "vaInitialize() failed with status %d\n", va_status);
-               if (error != nullptr) *error = buf;
-               return nullptr;
-       }
-
-       {
-               VAConfigAttrib attr = { VAConfigAttribRTFormat, VA_RT_FORMAT_YUV422 };
-               va_status = vaCreateConfig(va_dpy->va_dpy, VAProfileJPEGBaseline, VAEntrypointEncPicture,
-                       &attr, 1, config_id_422);
-               if (va_status == VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT) {
-                       if (error != nullptr) *error = "No 4:2:2 hardware support";
-                       return nullptr;
-               } else if (va_status != VA_STATUS_SUCCESS) {
-                       char buf[256];
-                       snprintf(buf, sizeof(buf), "vaCreateConfig() for 4:2:2 failed with status %d\n", va_status);
-                       if (error != nullptr) *error = buf;
-                       return nullptr;
-               }
-       }
-       {
-               VAConfigAttrib attr = { VAConfigAttribRTFormat, VA_RT_FORMAT_YUV420 };
-               va_status = vaCreateConfig(va_dpy->va_dpy, VAProfileJPEGBaseline, VAEntrypointEncPicture,
-                       &attr, 1, config_id_420);
-               if (va_status == VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT) {
-                       if (error != nullptr) *error = "No 4:2:0 hardware support";
-                       return nullptr;
-               } else if (va_status != VA_STATUS_SUCCESS) {
-                       char buf[256];
-                       snprintf(buf, sizeof(buf), "vaCreateConfig() for 4:2:0 failed with status %d\n", va_status);
-                       if (error != nullptr) *error = buf;
-                       return nullptr;
-               }
-       }
-
-       // TODO: Unify with the code in Futatabi.
-       int num_formats = vaMaxNumImageFormats(va_dpy->va_dpy);
-       assert(num_formats > 0);
-
-       unique_ptr<VAImageFormat[]> formats(new VAImageFormat[num_formats]);
-       va_status = vaQueryImageFormats(va_dpy->va_dpy, formats.get(), &num_formats);
-       if (va_status != VA_STATUS_SUCCESS) {
-               char buf[256];
-               snprintf(buf, sizeof(buf), "vaQueryImageFormats() failed with status %d\n", va_status);
-               if (error != nullptr) *error = buf;
-               return nullptr;
-       }
-
-       bool uyvy_found = false, nv12_found = false;
-       for (int i = 0; i < num_formats; ++i) {
-               if (formats[i].fourcc == VA_FOURCC_UYVY) {
-                       memcpy(&uyvy_format, &formats[i], sizeof(VAImageFormat));
-                       uyvy_found = true;
-               }
-               if (formats[i].fourcc == VA_FOURCC_NV12) {
-                       memcpy(&nv12_format, &formats[i], sizeof(VAImageFormat));
-                       nv12_found = true;
-               }
-       }
-       if (!uyvy_found) {
-               if (error != nullptr) *error = "UYVY format not found";
-               return nullptr;
-       }
-       if (!nv12_found) {
-               if (error != nullptr) *error = "NV12 format not found";
-               return nullptr;
-       }
-
-       return va_dpy;
-}
-
 namespace {
 
 bool is_uyvy(RefCountedFrame frame)
@@ -874,7 +799,6 @@ void MJPEGEncoder::encode_jpeg_va(QueuedFrame &&qf)
                        resources = get_va_resources(width, height, VA_FOURCC_UYVY);
                } else {
                        assert(is_i420(qf.frame));
-                       // We'd prefer VA_FOURCC_I420, but it's not supported by Intel's driver.
                        resources = get_va_resources(width, height, VA_FOURCC_NV12);
                }
                release = ReleaseVAResources(this, resources);