From c19853f03fbffc3f52533f2ad7960cbd7c785d96 Mon Sep 17 00:00:00 2001 From: loki Date: Wed, 5 May 2021 11:28:57 +0200 Subject: [PATCH] Add color for BT701 colorspace --- sunshine/platform/windows/display_vram.cpp | 33 ++++++++++++++-------- sunshine/video.cpp | 16 ++++++++++- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/sunshine/platform/windows/display_vram.cpp b/sunshine/platform/windows/display_vram.cpp index 5f5bef76..aa96d85b 100644 --- a/sunshine/platform/windows/display_vram.cpp +++ b/sunshine/platform/windows/display_vram.cpp @@ -32,17 +32,24 @@ struct __attribute__ ((__aligned__ (16))) color_t { DirectX::XMFLOAT4 color_vec_v; }; +color_t make_color_matrix(float Cr, float Cb, float U_max, float V_max, float add_Y, float add_UV) { + float Cg = 1.0f - Cr - Cb; + + float Cr_i = 1.0f - Cr; + float Cb_i = 1.0f - Cb; + + return { + { Cr, Cg, Cb, add_Y }, + { -(Cr * U_max / Cb_i), -(Cg * U_max / Cb_i), U_max, add_UV }, + { V_max, -(Cg * V_max / Cr_i), -(Cb * V_max / Cr_i), add_UV } + }; +} + color_t colors[] { - { - { 0.299f, 0.587f, 0.114f, 0.0625f }, // Color Luma (Y) - { -0.14713f, -0.28886f, 0.436f, 0.5f }, // Color Cb (U) - { 0.615f, -0.51499f, -0.10001f, 0.5f }, // Color Cr (V) - }, // BT602 -- MPEG - { - { 0.299f, 0.587f, 0.114f, 0.0f }, // Color Luma (Y) - { -0.168736f, -0.331264f, 0.5f, 0.5f }, // Color Cb (U) - { 0.5f, -0.418688f, -0.081312f, 0.5f }, // Color Cr (V) - } // BT601 -- JPEG + make_color_matrix(0.299f, 0.114f, 0.436f, 0.615f, 0.0625, 0.5f), // BT601 MPEG + make_color_matrix(0.299f, 0.114f, 0.5f, 0.5f, 0.0f, 0.5f), // BT601 JPEG + make_color_matrix(0.2126f, 0.0722f, 0.436f, 0.615f, 0.0625, 0.5f), //BT701 MPEG + make_color_matrix(0.2126f, 0.0722f, 0.5f, 0.5f, 0.0f, 0.5f), //BT701 JPEG }; template @@ -267,7 +274,6 @@ public: auto UV_rt_p = nv12_UV_rt.get(); _init_view_port(out_width, out_height); - device_ctx_p->OMSetRenderTargets(1, &Y_rt_p, nullptr); device_ctx_p->ClearRenderTargetView(Y_rt_p, aquamarine); device_ctx_p->VSSetShader(merge_Y_vs.get(), nullptr, 0); @@ -275,6 +281,8 @@ public: device_ctx_p->PSSetShaderResources(0, 1, &input_res_p); device_ctx_p->Draw(3, 0); + device_ctx_p->Flush(); + _init_view_port(out_width / 2, out_height / 2); device_ctx_p->OMSetRenderTargets(1, &UV_rt_p, nullptr); device_ctx_p->ClearRenderTargetView(UV_rt_p, aquamarine); @@ -292,6 +300,8 @@ public: color_p = &colors[0]; break; case 1: // SWS_CS_ITU709 + color_p = &colors[2]; + break; case 9: // SWS_CS_BT2020 default: BOOST_LOG(warning) << "Colorspace: ["sv << colorspace << "] not yet supported: switching to default"sv; @@ -581,6 +591,7 @@ public: device_ctx_t::pointer device_ctx_p; + // The destructor will remove itself from the list of hardware devices, this is done synchronously std::vector *hwdevices_p; }; diff --git a/sunshine/video.cpp b/sunshine/video.cpp index 986ef05f..e8e9b4b5 100644 --- a/sunshine/video.cpp +++ b/sunshine/video.cpp @@ -1304,6 +1304,15 @@ int hwframe_ctx(ctx_t &ctx, buffer_t &hwdevice, AVPixelFormat format) { void sw_img_to_frame(const platf::img_t &img, frame_t &frame) {} #ifdef _WIN32 +} + +// Ugly, but need to declare for wio +namespace platf::dxgi { +void lock(void *hwdevice); +void unlock(void *hwdevice); +} +void do_nothing(void*) {} +namespace video { void dxgi_img_to_frame(const platf::img_t &img, frame_t &frame) { if(img.data == frame->data[0]) { return; @@ -1334,13 +1343,18 @@ util::Either dxgi_make_hwdevice_ctx(platf::hwdevice_t *hwdevice_c std::fill_n((std::uint8_t*)ctx, sizeof(AVD3D11VADeviceContext), 0); auto device = (ID3D11Device*)hwdevice_ctx->data; + device->AddRef(); ctx->device = device; + ctx->lock_ctx = (void*)1; + ctx->lock = do_nothing; + ctx->unlock = do_nothing; + auto err = av_hwdevice_ctx_init(ctx_buf.get()); if(err) { char err_str[AV_ERROR_MAX_STRING_SIZE] {0}; - BOOST_LOG(error) << "Failed to create FFMpeg nvenc: "sv << av_make_error_string(err_str, AV_ERROR_MAX_STRING_SIZE, err); + BOOST_LOG(error) << "Failed to create FFMpeg hardware device context: "sv << av_make_error_string(err_str, AV_ERROR_MAX_STRING_SIZE, err); return err; }