1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2019 Pengutronix, Michael Tretter <kernel@pengutronix.de> 4 * 5 * Convert NAL units between raw byte sequence payloads (RBSP) and C structs 6 * 7 * The conversion is defined in "ITU-T Rec. H.264 (04/2017) Advanced video 8 * coding for generic audiovisual services". Decoder drivers may use the 9 * parser to parse RBSP from encoded streams and configure the hardware, if 10 * the hardware is not able to parse RBSP itself. Encoder drivers may use the 11 * generator to generate the RBSP for SPS/PPS nal units and add them to the 12 * encoded stream if the hardware does not generate the units. 13 */ 14 15 #include <linux/kernel.h> 16 #include <linux/types.h> 17 #include <linux/string.h> 18 #include <linux/v4l2-controls.h> 19 20 #include <linux/device.h> 21 #include <linux/export.h> 22 #include <linux/log2.h> 23 24 #include "nal-h264.h" 25 #include "nal-rbsp.h" 26 27 /* 28 * See Rec. ITU-T H.264 (04/2017) Table 7-1 – NAL unit type codes, syntax 29 * element categories, and NAL unit type classes 30 */ 31 enum nal_unit_type { 32 SEQUENCE_PARAMETER_SET = 7, 33 PICTURE_PARAMETER_SET = 8, 34 FILLER_DATA = 12, 35 }; 36 37 /** 38 * nal_h264_profile_from_v4l2() - Get profile_idc for v4l2 h264 profile 39 * @profile: the profile as &enum v4l2_mpeg_video_h264_profile 40 * 41 * Convert the &enum v4l2_mpeg_video_h264_profile to profile_idc as specified 42 * in Rec. ITU-T H.264 (04/2017) A.2. 43 * 44 * Return: the profile_idc for the passed level 45 */ 46 int nal_h264_profile_from_v4l2(enum v4l2_mpeg_video_h264_profile profile) 47 { 48 switch (profile) { 49 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE: 50 return 66; 51 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN: 52 return 77; 53 case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED: 54 return 88; 55 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH: 56 return 100; 57 default: 58 return -EINVAL; 59 } 60 } 61 62 /** 63 * nal_h264_level_from_v4l2() - Get level_idc for v4l2 h264 level 64 * @level: the level as &enum v4l2_mpeg_video_h264_level 65 * 66 * Convert the &enum v4l2_mpeg_video_h264_level to level_idc as specified in 67 * Rec. ITU-T H.264 (04/2017) A.3.2. 68 * 69 * Return: the level_idc for the passed level 70 */ 71 int nal_h264_level_from_v4l2(enum v4l2_mpeg_video_h264_level level) 72 { 73 switch (level) { 74 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0: 75 return 10; 76 case V4L2_MPEG_VIDEO_H264_LEVEL_1B: 77 return 9; 78 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1: 79 return 11; 80 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2: 81 return 12; 82 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3: 83 return 13; 84 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0: 85 return 20; 86 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1: 87 return 21; 88 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2: 89 return 22; 90 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0: 91 return 30; 92 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1: 93 return 31; 94 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2: 95 return 32; 96 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0: 97 return 40; 98 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1: 99 return 41; 100 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2: 101 return 42; 102 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0: 103 return 50; 104 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1: 105 return 51; 106 default: 107 return -EINVAL; 108 } 109 } 110 111 static void nal_h264_write_start_code_prefix(struct rbsp *rbsp) 112 { 113 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8); 114 int i = 4; 115 116 if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) { 117 rbsp->error = -EINVAL; 118 return; 119 } 120 121 p[0] = 0x00; 122 p[1] = 0x00; 123 p[2] = 0x00; 124 p[3] = 0x01; 125 126 rbsp->pos += i * 8; 127 } 128 129 static void nal_h264_read_start_code_prefix(struct rbsp *rbsp) 130 { 131 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8); 132 int i = 4; 133 134 if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) { 135 rbsp->error = -EINVAL; 136 return; 137 } 138 139 if (p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x00 || p[3] != 0x01) { 140 rbsp->error = -EINVAL; 141 return; 142 } 143 144 rbsp->pos += i * 8; 145 } 146 147 static void nal_h264_write_filler_data(struct rbsp *rbsp) 148 { 149 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8); 150 int i; 151 152 /* Keep 1 byte extra for terminating the NAL unit */ 153 i = rbsp->size - DIV_ROUND_UP(rbsp->pos, 8) - 1; 154 memset(p, 0xff, i); 155 rbsp->pos += i * 8; 156 } 157 158 static void nal_h264_read_filler_data(struct rbsp *rbsp) 159 { 160 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8); 161 162 while (*p == 0xff) { 163 if (DIV_ROUND_UP(rbsp->pos, 8) > rbsp->size) { 164 rbsp->error = -EINVAL; 165 return; 166 } 167 168 p++; 169 rbsp->pos += 8; 170 } 171 } 172 173 static void nal_h264_rbsp_hrd_parameters(struct rbsp *rbsp, 174 struct nal_h264_hrd_parameters *hrd) 175 { 176 unsigned int i; 177 178 if (!hrd) { 179 rbsp->error = -EINVAL; 180 return; 181 } 182 183 rbsp_uev(rbsp, &hrd->cpb_cnt_minus1); 184 rbsp_bits(rbsp, 4, &hrd->bit_rate_scale); 185 rbsp_bits(rbsp, 4, &hrd->cpb_size_scale); 186 187 for (i = 0; i <= hrd->cpb_cnt_minus1; i++) { 188 rbsp_uev(rbsp, &hrd->bit_rate_value_minus1[i]); 189 rbsp_uev(rbsp, &hrd->cpb_size_value_minus1[i]); 190 rbsp_bit(rbsp, &hrd->cbr_flag[i]); 191 } 192 193 rbsp_bits(rbsp, 5, &hrd->initial_cpb_removal_delay_length_minus1); 194 rbsp_bits(rbsp, 5, &hrd->cpb_removal_delay_length_minus1); 195 rbsp_bits(rbsp, 5, &hrd->dpb_output_delay_length_minus1); 196 rbsp_bits(rbsp, 5, &hrd->time_offset_length); 197 } 198 199 static void nal_h264_rbsp_vui_parameters(struct rbsp *rbsp, 200 struct nal_h264_vui_parameters *vui) 201 { 202 if (!vui) { 203 rbsp->error = -EINVAL; 204 return; 205 } 206 207 rbsp_bit(rbsp, &vui->aspect_ratio_info_present_flag); 208 if (vui->aspect_ratio_info_present_flag) { 209 rbsp_bits(rbsp, 8, &vui->aspect_ratio_idc); 210 if (vui->aspect_ratio_idc == 255) { 211 rbsp_bits(rbsp, 16, &vui->sar_width); 212 rbsp_bits(rbsp, 16, &vui->sar_height); 213 } 214 } 215 216 rbsp_bit(rbsp, &vui->overscan_info_present_flag); 217 if (vui->overscan_info_present_flag) 218 rbsp_bit(rbsp, &vui->overscan_appropriate_flag); 219 220 rbsp_bit(rbsp, &vui->video_signal_type_present_flag); 221 if (vui->video_signal_type_present_flag) { 222 rbsp_bits(rbsp, 3, &vui->video_format); 223 rbsp_bit(rbsp, &vui->video_full_range_flag); 224 225 rbsp_bit(rbsp, &vui->colour_description_present_flag); 226 if (vui->colour_description_present_flag) { 227 rbsp_bits(rbsp, 8, &vui->colour_primaries); 228 rbsp_bits(rbsp, 8, &vui->transfer_characteristics); 229 rbsp_bits(rbsp, 8, &vui->matrix_coefficients); 230 } 231 } 232 233 rbsp_bit(rbsp, &vui->chroma_loc_info_present_flag); 234 if (vui->chroma_loc_info_present_flag) { 235 rbsp_uev(rbsp, &vui->chroma_sample_loc_type_top_field); 236 rbsp_uev(rbsp, &vui->chroma_sample_loc_type_bottom_field); 237 } 238 239 rbsp_bit(rbsp, &vui->timing_info_present_flag); 240 if (vui->timing_info_present_flag) { 241 rbsp_bits(rbsp, 32, &vui->num_units_in_tick); 242 rbsp_bits(rbsp, 32, &vui->time_scale); 243 rbsp_bit(rbsp, &vui->fixed_frame_rate_flag); 244 } 245 246 rbsp_bit(rbsp, &vui->nal_hrd_parameters_present_flag); 247 if (vui->nal_hrd_parameters_present_flag) 248 nal_h264_rbsp_hrd_parameters(rbsp, &vui->nal_hrd_parameters); 249 250 rbsp_bit(rbsp, &vui->vcl_hrd_parameters_present_flag); 251 if (vui->vcl_hrd_parameters_present_flag) 252 nal_h264_rbsp_hrd_parameters(rbsp, &vui->vcl_hrd_parameters); 253 254 if (vui->nal_hrd_parameters_present_flag || 255 vui->vcl_hrd_parameters_present_flag) 256 rbsp_bit(rbsp, &vui->low_delay_hrd_flag); 257 258 rbsp_bit(rbsp, &vui->pic_struct_present_flag); 259 260 rbsp_bit(rbsp, &vui->bitstream_restriction_flag); 261 if (vui->bitstream_restriction_flag) { 262 rbsp_bit(rbsp, &vui->motion_vectors_over_pic_boundaries_flag); 263 rbsp_uev(rbsp, &vui->max_bytes_per_pic_denom); 264 rbsp_uev(rbsp, &vui->max_bits_per_mb_denom); 265 rbsp_uev(rbsp, &vui->log2_max_mv_length_horizontal); 266 rbsp_uev(rbsp, &vui->log21_max_mv_length_vertical); 267 rbsp_uev(rbsp, &vui->max_num_reorder_frames); 268 rbsp_uev(rbsp, &vui->max_dec_frame_buffering); 269 } 270 } 271 272 static void nal_h264_rbsp_sps(struct rbsp *rbsp, struct nal_h264_sps *sps) 273 { 274 unsigned int i; 275 276 if (!sps) { 277 rbsp->error = -EINVAL; 278 return; 279 } 280 281 rbsp_bits(rbsp, 8, &sps->profile_idc); 282 rbsp_bit(rbsp, &sps->constraint_set0_flag); 283 rbsp_bit(rbsp, &sps->constraint_set1_flag); 284 rbsp_bit(rbsp, &sps->constraint_set2_flag); 285 rbsp_bit(rbsp, &sps->constraint_set3_flag); 286 rbsp_bit(rbsp, &sps->constraint_set4_flag); 287 rbsp_bit(rbsp, &sps->constraint_set5_flag); 288 rbsp_bits(rbsp, 2, &sps->reserved_zero_2bits); 289 rbsp_bits(rbsp, 8, &sps->level_idc); 290 291 rbsp_uev(rbsp, &sps->seq_parameter_set_id); 292 293 if (sps->profile_idc == 100 || sps->profile_idc == 110 || 294 sps->profile_idc == 122 || sps->profile_idc == 244 || 295 sps->profile_idc == 44 || sps->profile_idc == 83 || 296 sps->profile_idc == 86 || sps->profile_idc == 118 || 297 sps->profile_idc == 128 || sps->profile_idc == 138 || 298 sps->profile_idc == 139 || sps->profile_idc == 134 || 299 sps->profile_idc == 135) { 300 rbsp_uev(rbsp, &sps->chroma_format_idc); 301 302 if (sps->chroma_format_idc == 3) 303 rbsp_bit(rbsp, &sps->separate_colour_plane_flag); 304 rbsp_uev(rbsp, &sps->bit_depth_luma_minus8); 305 rbsp_uev(rbsp, &sps->bit_depth_chroma_minus8); 306 rbsp_bit(rbsp, &sps->qpprime_y_zero_transform_bypass_flag); 307 rbsp_bit(rbsp, &sps->seq_scaling_matrix_present_flag); 308 if (sps->seq_scaling_matrix_present_flag) 309 rbsp->error = -EINVAL; 310 } 311 312 rbsp_uev(rbsp, &sps->log2_max_frame_num_minus4); 313 314 rbsp_uev(rbsp, &sps->pic_order_cnt_type); 315 switch (sps->pic_order_cnt_type) { 316 case 0: 317 rbsp_uev(rbsp, &sps->log2_max_pic_order_cnt_lsb_minus4); 318 break; 319 case 1: 320 rbsp_bit(rbsp, &sps->delta_pic_order_always_zero_flag); 321 rbsp_sev(rbsp, &sps->offset_for_non_ref_pic); 322 rbsp_sev(rbsp, &sps->offset_for_top_to_bottom_field); 323 324 rbsp_uev(rbsp, &sps->num_ref_frames_in_pic_order_cnt_cycle); 325 for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++) 326 rbsp_sev(rbsp, &sps->offset_for_ref_frame[i]); 327 break; 328 default: 329 rbsp->error = -EINVAL; 330 break; 331 } 332 333 rbsp_uev(rbsp, &sps->max_num_ref_frames); 334 rbsp_bit(rbsp, &sps->gaps_in_frame_num_value_allowed_flag); 335 rbsp_uev(rbsp, &sps->pic_width_in_mbs_minus1); 336 rbsp_uev(rbsp, &sps->pic_height_in_map_units_minus1); 337 338 rbsp_bit(rbsp, &sps->frame_mbs_only_flag); 339 if (!sps->frame_mbs_only_flag) 340 rbsp_bit(rbsp, &sps->mb_adaptive_frame_field_flag); 341 342 rbsp_bit(rbsp, &sps->direct_8x8_inference_flag); 343 344 rbsp_bit(rbsp, &sps->frame_cropping_flag); 345 if (sps->frame_cropping_flag) { 346 rbsp_uev(rbsp, &sps->crop_left); 347 rbsp_uev(rbsp, &sps->crop_right); 348 rbsp_uev(rbsp, &sps->crop_top); 349 rbsp_uev(rbsp, &sps->crop_bottom); 350 } 351 352 rbsp_bit(rbsp, &sps->vui_parameters_present_flag); 353 if (sps->vui_parameters_present_flag) 354 nal_h264_rbsp_vui_parameters(rbsp, &sps->vui); 355 } 356 357 static void nal_h264_rbsp_pps(struct rbsp *rbsp, struct nal_h264_pps *pps) 358 { 359 int i; 360 361 rbsp_uev(rbsp, &pps->pic_parameter_set_id); 362 rbsp_uev(rbsp, &pps->seq_parameter_set_id); 363 rbsp_bit(rbsp, &pps->entropy_coding_mode_flag); 364 rbsp_bit(rbsp, &pps->bottom_field_pic_order_in_frame_present_flag); 365 rbsp_uev(rbsp, &pps->num_slice_groups_minus1); 366 if (pps->num_slice_groups_minus1 > 0) { 367 rbsp_uev(rbsp, &pps->slice_group_map_type); 368 switch (pps->slice_group_map_type) { 369 case 0: 370 for (i = 0; i < pps->num_slice_groups_minus1; i++) 371 rbsp_uev(rbsp, &pps->run_length_minus1[i]); 372 break; 373 case 2: 374 for (i = 0; i < pps->num_slice_groups_minus1; i++) { 375 rbsp_uev(rbsp, &pps->top_left[i]); 376 rbsp_uev(rbsp, &pps->bottom_right[i]); 377 } 378 break; 379 case 3: case 4: case 5: 380 rbsp_bit(rbsp, &pps->slice_group_change_direction_flag); 381 rbsp_uev(rbsp, &pps->slice_group_change_rate_minus1); 382 break; 383 case 6: 384 rbsp_uev(rbsp, &pps->pic_size_in_map_units_minus1); 385 for (i = 0; i < pps->pic_size_in_map_units_minus1; i++) 386 rbsp_bits(rbsp, 387 order_base_2(pps->num_slice_groups_minus1 + 1), 388 &pps->slice_group_id[i]); 389 break; 390 default: 391 break; 392 } 393 } 394 rbsp_uev(rbsp, &pps->num_ref_idx_l0_default_active_minus1); 395 rbsp_uev(rbsp, &pps->num_ref_idx_l1_default_active_minus1); 396 rbsp_bit(rbsp, &pps->weighted_pred_flag); 397 rbsp_bits(rbsp, 2, &pps->weighted_bipred_idc); 398 rbsp_sev(rbsp, &pps->pic_init_qp_minus26); 399 rbsp_sev(rbsp, &pps->pic_init_qs_minus26); 400 rbsp_sev(rbsp, &pps->chroma_qp_index_offset); 401 rbsp_bit(rbsp, &pps->deblocking_filter_control_present_flag); 402 rbsp_bit(rbsp, &pps->constrained_intra_pred_flag); 403 rbsp_bit(rbsp, &pps->redundant_pic_cnt_present_flag); 404 if (/* more_rbsp_data() */ false) { 405 rbsp_bit(rbsp, &pps->transform_8x8_mode_flag); 406 rbsp_bit(rbsp, &pps->pic_scaling_matrix_present_flag); 407 if (pps->pic_scaling_matrix_present_flag) 408 rbsp->error = -EINVAL; 409 rbsp_sev(rbsp, &pps->second_chroma_qp_index_offset); 410 } 411 } 412 413 /** 414 * nal_h264_write_sps() - Write SPS NAL unit into RBSP format 415 * @dev: device pointer 416 * @dest: the buffer that is filled with RBSP data 417 * @n: maximum size of @dest in bytes 418 * @sps: &struct nal_h264_sps to convert to RBSP 419 * 420 * Convert @sps to RBSP data and write it into @dest. 421 * 422 * The size of the SPS NAL unit is not known in advance and this function will 423 * fail, if @dest does not hold sufficient space for the SPS NAL unit. 424 * 425 * Return: number of bytes written to @dest or negative error code 426 */ 427 ssize_t nal_h264_write_sps(const struct device *dev, 428 void *dest, size_t n, struct nal_h264_sps *sps) 429 { 430 struct rbsp rbsp; 431 unsigned int forbidden_zero_bit = 0; 432 unsigned int nal_ref_idc = 0; 433 unsigned int nal_unit_type = SEQUENCE_PARAMETER_SET; 434 435 if (!dest) 436 return -EINVAL; 437 438 rbsp_init(&rbsp, dest, n, &write); 439 440 nal_h264_write_start_code_prefix(&rbsp); 441 442 rbsp_bit(&rbsp, &forbidden_zero_bit); 443 rbsp_bits(&rbsp, 2, &nal_ref_idc); 444 rbsp_bits(&rbsp, 5, &nal_unit_type); 445 446 nal_h264_rbsp_sps(&rbsp, sps); 447 448 rbsp_trailing_bits(&rbsp); 449 450 if (rbsp.error) 451 return rbsp.error; 452 453 return DIV_ROUND_UP(rbsp.pos, 8); 454 } 455 EXPORT_SYMBOL_GPL(nal_h264_write_sps); 456 457 /** 458 * nal_h264_read_sps() - Read SPS NAL unit from RBSP format 459 * @dev: device pointer 460 * @sps: the &struct nal_h264_sps to fill from the RBSP data 461 * @src: the buffer that contains the RBSP data 462 * @n: size of @src in bytes 463 * 464 * Read RBSP data from @src and use it to fill @sps. 465 * 466 * Return: number of bytes read from @src or negative error code 467 */ 468 ssize_t nal_h264_read_sps(const struct device *dev, 469 struct nal_h264_sps *sps, void *src, size_t n) 470 { 471 struct rbsp rbsp; 472 unsigned int forbidden_zero_bit; 473 unsigned int nal_ref_idc; 474 unsigned int nal_unit_type; 475 476 if (!src) 477 return -EINVAL; 478 479 rbsp_init(&rbsp, src, n, &read); 480 481 nal_h264_read_start_code_prefix(&rbsp); 482 483 rbsp_bit(&rbsp, &forbidden_zero_bit); 484 rbsp_bits(&rbsp, 2, &nal_ref_idc); 485 rbsp_bits(&rbsp, 5, &nal_unit_type); 486 487 if (rbsp.error || 488 forbidden_zero_bit != 0 || 489 nal_ref_idc != 0 || 490 nal_unit_type != SEQUENCE_PARAMETER_SET) 491 return -EINVAL; 492 493 nal_h264_rbsp_sps(&rbsp, sps); 494 495 rbsp_trailing_bits(&rbsp); 496 497 if (rbsp.error) 498 return rbsp.error; 499 500 return DIV_ROUND_UP(rbsp.pos, 8); 501 } 502 EXPORT_SYMBOL_GPL(nal_h264_read_sps); 503 504 /** 505 * nal_h264_write_pps() - Write PPS NAL unit into RBSP format 506 * @dev: device pointer 507 * @dest: the buffer that is filled with RBSP data 508 * @n: maximum size of @dest in bytes 509 * @pps: &struct nal_h264_pps to convert to RBSP 510 * 511 * Convert @pps to RBSP data and write it into @dest. 512 * 513 * The size of the PPS NAL unit is not known in advance and this function will 514 * fail, if @dest does not hold sufficient space for the PPS NAL unit. 515 * 516 * Return: number of bytes written to @dest or negative error code 517 */ 518 ssize_t nal_h264_write_pps(const struct device *dev, 519 void *dest, size_t n, struct nal_h264_pps *pps) 520 { 521 struct rbsp rbsp; 522 unsigned int forbidden_zero_bit = 0; 523 unsigned int nal_ref_idc = 0; 524 unsigned int nal_unit_type = PICTURE_PARAMETER_SET; 525 526 if (!dest) 527 return -EINVAL; 528 529 rbsp_init(&rbsp, dest, n, &write); 530 531 nal_h264_write_start_code_prefix(&rbsp); 532 533 /* NAL unit header */ 534 rbsp_bit(&rbsp, &forbidden_zero_bit); 535 rbsp_bits(&rbsp, 2, &nal_ref_idc); 536 rbsp_bits(&rbsp, 5, &nal_unit_type); 537 538 nal_h264_rbsp_pps(&rbsp, pps); 539 540 rbsp_trailing_bits(&rbsp); 541 542 if (rbsp.error) 543 return rbsp.error; 544 545 return DIV_ROUND_UP(rbsp.pos, 8); 546 } 547 EXPORT_SYMBOL_GPL(nal_h264_write_pps); 548 549 /** 550 * nal_h264_read_pps() - Read PPS NAL unit from RBSP format 551 * @dev: device pointer 552 * @pps: the &struct nal_h264_pps to fill from the RBSP data 553 * @src: the buffer that contains the RBSP data 554 * @n: size of @src in bytes 555 * 556 * Read RBSP data from @src and use it to fill @pps. 557 * 558 * Return: number of bytes read from @src or negative error code 559 */ 560 ssize_t nal_h264_read_pps(const struct device *dev, 561 struct nal_h264_pps *pps, void *src, size_t n) 562 { 563 struct rbsp rbsp; 564 565 if (!src) 566 return -EINVAL; 567 568 rbsp_init(&rbsp, src, n, &read); 569 570 nal_h264_read_start_code_prefix(&rbsp); 571 572 /* NAL unit header */ 573 rbsp.pos += 8; 574 575 nal_h264_rbsp_pps(&rbsp, pps); 576 577 rbsp_trailing_bits(&rbsp); 578 579 if (rbsp.error) 580 return rbsp.error; 581 582 return DIV_ROUND_UP(rbsp.pos, 8); 583 } 584 EXPORT_SYMBOL_GPL(nal_h264_read_pps); 585 586 /** 587 * nal_h264_write_filler() - Write filler data RBSP 588 * @dev: device pointer 589 * @dest: buffer to fill with filler data 590 * @n: size of the buffer to fill with filler data 591 * 592 * Write a filler data RBSP to @dest with a size of @n bytes and return the 593 * number of written filler data bytes. 594 * 595 * Use this function to generate dummy data in an RBSP data stream that can be 596 * safely ignored by h264 decoders. 597 * 598 * The RBSP format of the filler data is specified in Rec. ITU-T H.264 599 * (04/2017) 7.3.2.7 Filler data RBSP syntax. 600 * 601 * Return: number of filler data bytes (including marker) or negative error 602 */ 603 ssize_t nal_h264_write_filler(const struct device *dev, void *dest, size_t n) 604 { 605 struct rbsp rbsp; 606 unsigned int forbidden_zero_bit = 0; 607 unsigned int nal_ref_idc = 0; 608 unsigned int nal_unit_type = FILLER_DATA; 609 610 if (!dest) 611 return -EINVAL; 612 613 rbsp_init(&rbsp, dest, n, &write); 614 615 nal_h264_write_start_code_prefix(&rbsp); 616 617 rbsp_bit(&rbsp, &forbidden_zero_bit); 618 rbsp_bits(&rbsp, 2, &nal_ref_idc); 619 rbsp_bits(&rbsp, 5, &nal_unit_type); 620 621 nal_h264_write_filler_data(&rbsp); 622 623 rbsp_trailing_bits(&rbsp); 624 625 return DIV_ROUND_UP(rbsp.pos, 8); 626 } 627 EXPORT_SYMBOL_GPL(nal_h264_write_filler); 628 629 /** 630 * nal_h264_read_filler() - Read filler data RBSP 631 * @dev: device pointer 632 * @src: buffer with RBSP data that is read 633 * @n: maximum size of src that shall be read 634 * 635 * Read a filler data RBSP from @src up to a maximum size of @n bytes and 636 * return the size of the filler data in bytes including the marker. 637 * 638 * This function is used to parse filler data and skip the respective bytes in 639 * the RBSP data. 640 * 641 * The RBSP format of the filler data is specified in Rec. ITU-T H.264 642 * (04/2017) 7.3.2.7 Filler data RBSP syntax. 643 * 644 * Return: number of filler data bytes (including marker) or negative error 645 */ 646 ssize_t nal_h264_read_filler(const struct device *dev, void *src, size_t n) 647 { 648 struct rbsp rbsp; 649 unsigned int forbidden_zero_bit; 650 unsigned int nal_ref_idc; 651 unsigned int nal_unit_type; 652 653 if (!src) 654 return -EINVAL; 655 656 rbsp_init(&rbsp, src, n, &read); 657 658 nal_h264_read_start_code_prefix(&rbsp); 659 660 rbsp_bit(&rbsp, &forbidden_zero_bit); 661 rbsp_bits(&rbsp, 2, &nal_ref_idc); 662 rbsp_bits(&rbsp, 5, &nal_unit_type); 663 664 if (rbsp.error) 665 return rbsp.error; 666 if (forbidden_zero_bit != 0 || 667 nal_ref_idc != 0 || 668 nal_unit_type != FILLER_DATA) 669 return -EINVAL; 670 671 nal_h264_read_filler_data(&rbsp); 672 rbsp_trailing_bits(&rbsp); 673 674 if (rbsp.error) 675 return rbsp.error; 676 677 return DIV_ROUND_UP(rbsp.pos, 8); 678 } 679 EXPORT_SYMBOL_GPL(nal_h264_read_filler); 680