From 2f576e282fb5d5a97ed7de3d791b90d649914617 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Sun, 5 Jan 2025 22:04:24 -0600 Subject: [PATCH] Experimental support to correctly convert 10-bit color into 8-bit colorspace. --- src/FFmpegReader.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index 1d5064c36..f0c6c1cf8 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -1505,6 +1505,26 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) { SwsContext *img_convert_ctx = sws_getContext(info.width, info.height, AV_GET_CODEC_PIXEL_FORMAT(pStream, pCodecCtx), width, height, PIX_FMT_RGBA, scale_mode, NULL, NULL, NULL); + // Determine the source and destination color ranges + int srcRange = (pFrame->color_range == AVCOL_RANGE_JPEG) ? 1 : 0; + int dstRange = 0; // Output as limited range (e.g., 8-bit broadcast range) + + // Get the source and destination color coefficients + const int *srcCoeff = sws_getCoefficients(pFrame->colorspace); + const int *dstCoeff = sws_getCoefficients(AVCOL_SPC_BT709); // Assume output is BT.709 + + // Set the color space details in the SwsContext + sws_setColorspaceDetails( + img_convert_ctx, + srcCoeff, + srcRange, + dstCoeff, + dstRange, + 0, // brightness adjustment (default: 0) + 1 << 16, // contrast adjustment (default: 1.0 in 16.16 fixed-point) + 1 << 16 // saturation adjustment (default: 1.0 in 16.16 fixed-point) + ); + // Resize / Convert to RGB sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, original_height, pFrameRGB->data, pFrameRGB->linesize);