1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:28:12 +00:00

LibVideo: Implement CICP color space conversion

This adds a struct called CodingIndependentCodePoints and related enums
that are used by video codecs to define its color space that frames
must be converted from when displaying a video.

Pre-multiplied matrices and lookup tables are stored to avoid most of
the floating point division and exponentiation in the conversion.
This commit is contained in:
Zaggy1024 2022-10-10 05:03:57 -05:00 committed by Andreas Kling
parent ba79de0439
commit cd127b65c3
15 changed files with 935 additions and 18 deletions

View file

@ -12,6 +12,7 @@
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <LibMain/Main.h>
#include <LibVideo/Color/ColorConverter.h>
#include <LibVideo/MatroskaReader.h>
#include <LibVideo/VP9/Decoder.h>
@ -91,6 +92,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto uv_subsampling_y = vp9_decoder.get_uv_subsampling_y();
auto uv_subsampling_x = vp9_decoder.get_uv_subsampling_x();
Gfx::IntSize uv_size { y_size.width() >> uv_subsampling_x, y_size.height() >> uv_subsampling_y };
auto cicp = vp9_decoder.get_cicp_color_space();
cicp.default_code_points_if_unspecified(Video::ColorPrimaries::BT709, Video::TransferCharacteristics::BT709, Video::MatrixCoefficients::BT709);
auto color_converter_result = Video::ColorConverter::create(vp9_decoder.get_bit_depth(), cicp);
if (color_converter_result.is_error()) {
outln("Cannot convert video colors: {}", color_converter_result.release_error().string_literal());
return;
}
auto color_converter = color_converter_result.release_value();
for (auto y_row = 0u; y_row < video_track.pixel_height; y_row++) {
auto uv_row = y_row >> uv_subsampling_y;
@ -99,17 +109,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto uv_column = y_column >> uv_subsampling_x;
auto y = output_y[y_row * y_size.width() + y_column];
auto cb = output_u[uv_row * uv_size.width() + uv_column];
auto cr = output_v[uv_row * uv_size.width() + uv_column];
// Convert from Rec.709 YCbCr to RGB.
auto r_float = floorf(clamp(y + (cr - 128) * 219.0f / 224.0f * 1.5748f, 0, 255));
auto g_float = floorf(clamp(y + (cb - 128) * 219.0f / 224.0f * -0.0722f * 1.8556f / 0.7152f + (cr - 128) * 219.0f / 224.0f * -0.2126f * 1.5748f / 0.7152f, 0, 255));
auto b_float = floorf(clamp(y + (cb - 128) * 219.0f / 224.0f * 1.8556f, 0, 255));
auto r = static_cast<u8>(r_float);
auto g = static_cast<u8>(g_float);
auto b = static_cast<u8>(b_float);
auto u = output_u[uv_row * uv_size.width() + uv_column];
auto v = output_v[uv_row * uv_size.width() + uv_column];
image->set_pixel(y_column, y_row, Gfx::Color(r, g, b));
image->set_pixel(y_column, y_row, color_converter.convert_yuv_to_full_range_rgb(y, u, v));
}
}