Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: allegro: write correct colorspace into SPS

Currently, the driver always writes PAL as video format into the SPS of
the encoded stream.

Set the video format to the default value 5 (unspecified) and use the
color description that is already configured on the channel as color
space. This fixes the color space definition in the coded data to
reflect the configured color space of the video data that is encoded.

Add lookup functions to convert the color primaries, transfer function
and matrix coefficients from the V4L2 control values to the values
specified in the h.264 standard.

Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

authored by

Michael Tretter and committed by
Mauro Carvalho Chehab
0317c05f e5c28f21

+131 -5
+9 -5
drivers/media/platform/allegro-dvt/allegro-core.c
··· 1620 1620 sps->vui_parameters_present_flag = 1; 1621 1621 sps->vui.aspect_ratio_info_present_flag = 0; 1622 1622 sps->vui.overscan_info_present_flag = 0; 1623 + 1623 1624 sps->vui.video_signal_type_present_flag = 1; 1624 - sps->vui.video_format = 1; 1625 - sps->vui.video_full_range_flag = 0; 1625 + sps->vui.video_format = 5; /* unspecified */ 1626 + sps->vui.video_full_range_flag = nal_h264_full_range(channel->quantization); 1626 1627 sps->vui.colour_description_present_flag = 1; 1627 - sps->vui.colour_primaries = 5; 1628 - sps->vui.transfer_characteristics = 5; 1629 - sps->vui.matrix_coefficients = 5; 1628 + sps->vui.colour_primaries = nal_h264_color_primaries(channel->colorspace); 1629 + sps->vui.transfer_characteristics = 1630 + nal_h264_transfer_characteristics(channel->colorspace, channel->xfer_func); 1631 + sps->vui.matrix_coefficients = 1632 + nal_h264_matrix_coeffs(channel->colorspace, channel->ycbcr_enc); 1633 + 1630 1634 sps->vui.chroma_loc_info_present_flag = 1; 1631 1635 sps->vui.chroma_sample_loc_type_top_field = 0; 1632 1636 sps->vui.chroma_sample_loc_type_bottom_field = 0;
+122
drivers/media/platform/allegro-dvt/nal-h264.h
··· 264 264 } 265 265 } 266 266 267 + /** 268 + * nal_h264_full_range() - Get video_full_range_flag for v4l2 quantization 269 + * @quantization: the quantization type as &enum v4l2_quantization 270 + * 271 + * Convert the &enum v4l2_quantization to video_full_range_flag as specified in 272 + * Rec. ITU-T H.264 (04/2017) E.2.1. 273 + * 274 + * Return: the video_full_range_flag value for the passed quantization 275 + */ 276 + static inline int nal_h264_full_range(enum v4l2_quantization quantization) 277 + { 278 + switch (quantization) { 279 + case V4L2_QUANTIZATION_FULL_RANGE: 280 + return 1; 281 + case V4L2_QUANTIZATION_LIM_RANGE: 282 + return 0; 283 + default: 284 + break; 285 + } 286 + 287 + return 0; 288 + } 289 + 290 + /** 291 + * nal_h264_color_primaries() - Get color_primaries for v4l2 colorspace 292 + * @colorspace: the color space as &enum v4l2_colorspace 293 + * 294 + * Convert the &enum v4l2_colorspace to color_primaries as specified in 295 + * Rec. ITU-T H.264 (04/2017) E.2.1. 296 + * 297 + * Return: the color_primaries value for the passed colorspace 298 + */ 299 + static inline int nal_h264_color_primaries(enum v4l2_colorspace colorspace) 300 + { 301 + switch (colorspace) { 302 + case V4L2_COLORSPACE_SMPTE170M: 303 + return 6; 304 + case V4L2_COLORSPACE_SMPTE240M: 305 + return 7; 306 + case V4L2_COLORSPACE_REC709: 307 + return 1; 308 + case V4L2_COLORSPACE_470_SYSTEM_M: 309 + return 4; 310 + case V4L2_COLORSPACE_JPEG: 311 + case V4L2_COLORSPACE_SRGB: 312 + case V4L2_COLORSPACE_470_SYSTEM_BG: 313 + return 5; 314 + case V4L2_COLORSPACE_BT2020: 315 + return 9; 316 + case V4L2_COLORSPACE_DEFAULT: 317 + case V4L2_COLORSPACE_OPRGB: 318 + case V4L2_COLORSPACE_RAW: 319 + case V4L2_COLORSPACE_DCI_P3: 320 + default: 321 + return 2; 322 + } 323 + } 324 + 325 + /** 326 + * nal_h264_transfer_characteristics() - Get transfer_characteristics for v4l2 xfer_func 327 + * @colorspace: the color space as &enum v4l2_colorspace 328 + * @xfer_func: the transfer function as &enum v4l2_xfer_func 329 + * 330 + * Convert the &enum v4l2_xfer_func to transfer_characteristics as specified in 331 + * Rec. ITU-T H.264 (04/2017) E.2.1. 332 + * 333 + * Return: the transfer_characteristics value for the passed transfer function 334 + */ 335 + static inline int nal_h264_transfer_characteristics(enum v4l2_colorspace colorspace, 336 + enum v4l2_xfer_func xfer_func) 337 + { 338 + if (xfer_func == V4L2_XFER_FUNC_DEFAULT) 339 + xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(colorspace); 340 + 341 + switch (xfer_func) { 342 + case V4L2_XFER_FUNC_709: 343 + return 6; 344 + case V4L2_XFER_FUNC_SMPTE2084: 345 + return 16; 346 + case V4L2_XFER_FUNC_SRGB: 347 + case V4L2_XFER_FUNC_OPRGB: 348 + case V4L2_XFER_FUNC_NONE: 349 + case V4L2_XFER_FUNC_DCI_P3: 350 + case V4L2_XFER_FUNC_SMPTE240M: 351 + default: 352 + return 2; 353 + } 354 + } 355 + 356 + /** 357 + * nal_h264_matrix_coeffs() - Get matrix_coefficients for v4l2 v4l2_ycbcr_encoding 358 + * @colorspace: the color space as &enum v4l2_colorspace 359 + * @ycbcr_encoding: the ycbcr encoding as &enum v4l2_ycbcr_encoding 360 + * 361 + * Convert the &enum v4l2_ycbcr_encoding to matrix_coefficients as specified in 362 + * Rec. ITU-T H.264 (04/2017) E.2.1. 363 + * 364 + * Return: the matrix_coefficients value for the passed encoding 365 + */ 366 + static inline int nal_h264_matrix_coeffs(enum v4l2_colorspace colorspace, 367 + enum v4l2_ycbcr_encoding ycbcr_encoding) 368 + { 369 + if (ycbcr_encoding == V4L2_YCBCR_ENC_DEFAULT) 370 + ycbcr_encoding = V4L2_MAP_YCBCR_ENC_DEFAULT(colorspace); 371 + 372 + switch (ycbcr_encoding) { 373 + case V4L2_YCBCR_ENC_601: 374 + case V4L2_YCBCR_ENC_XV601: 375 + return 5; 376 + case V4L2_YCBCR_ENC_709: 377 + case V4L2_YCBCR_ENC_XV709: 378 + return 1; 379 + case V4L2_YCBCR_ENC_BT2020: 380 + return 9; 381 + case V4L2_YCBCR_ENC_BT2020_CONST_LUM: 382 + return 10; 383 + case V4L2_YCBCR_ENC_SMPTE240M: 384 + default: 385 + return 2; 386 + } 387 + } 388 + 267 389 ssize_t nal_h264_write_sps(const struct device *dev, 268 390 void *dest, size_t n, struct nal_h264_sps *sps); 269 391 ssize_t nal_h264_read_sps(const struct device *dev,