1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019-2020 Pengutronix, Michael Tretter <kernel@pengutronix.de>
5 * Convert NAL units between raw byte sequence payloads (RBSP) and C structs.
7 * The conversion is defined in "ITU-T Rec. H.265 (02/2018) high efficiency
8 * video coding". Decoder drivers may use the parser to parse RBSP from
9 * encoded streams and configure the hardware, if the hardware is not able to
10 * parse RBSP itself. Encoder drivers may use the generator to generate the
11 * RBSP for VPS/SPS/PPS nal units and add them to the encoded stream if the
12 * hardware does not generate the units.
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/string.h>
18 #include <linux/v4l2-controls.h>
20 #include <linux/device.h>
21 #include <linux/export.h>
22 #include <linux/log2.h>
28 * See Rec. ITU-T H.265 (02/2018) Table 7-1 - NAL unit type codes and NAL unit
38 int nal_hevc_profile_from_v4l2(enum v4l2_mpeg_video_hevc_profile profile)
41 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
43 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
45 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
51 EXPORT_SYMBOL_GPL(nal_hevc_profile_from_v4l2);
53 int nal_hevc_tier_from_v4l2(enum v4l2_mpeg_video_hevc_tier tier)
56 case V4L2_MPEG_VIDEO_HEVC_TIER_MAIN:
58 case V4L2_MPEG_VIDEO_HEVC_TIER_HIGH:
64 EXPORT_SYMBOL_GPL(nal_hevc_tier_from_v4l2);
66 int nal_hevc_level_from_v4l2(enum v4l2_mpeg_video_hevc_level level)
69 * T-Rec-H.265 p. 280: general_level_idc and sub_layer_level_idc[ i ]
70 * shall be set equal to a value of 30 times the level number
71 * specified in Table A.6.
76 case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
78 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
80 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
82 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
84 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
86 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
88 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
90 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
92 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
94 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2:
96 case V4L2_MPEG_VIDEO_HEVC_LEVEL_6:
98 case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1:
100 case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2:
106 EXPORT_SYMBOL_GPL(nal_hevc_level_from_v4l2);
108 static void nal_hevc_write_start_code_prefix(struct rbsp *rbsp)
110 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
113 if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
114 rbsp->error = -EINVAL;
126 static void nal_hevc_read_start_code_prefix(struct rbsp *rbsp)
128 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
131 if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
132 rbsp->error = -EINVAL;
136 if (p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x00 || p[3] != 0x01) {
137 rbsp->error = -EINVAL;
144 static void nal_hevc_write_filler_data(struct rbsp *rbsp)
146 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
149 /* Keep 1 byte extra for terminating the NAL unit */
150 i = rbsp->size - DIV_ROUND_UP(rbsp->pos, 8) - 1;
155 static void nal_hevc_read_filler_data(struct rbsp *rbsp)
157 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
160 if (DIV_ROUND_UP(rbsp->pos, 8) > rbsp->size) {
161 rbsp->error = -EINVAL;
170 static void nal_hevc_rbsp_profile_tier_level(struct rbsp *rbsp,
171 struct nal_hevc_profile_tier_level *ptl)
174 unsigned int max_num_sub_layers_minus_1 = 0;
176 rbsp_bits(rbsp, 2, &ptl->general_profile_space);
177 rbsp_bit(rbsp, &ptl->general_tier_flag);
178 rbsp_bits(rbsp, 5, &ptl->general_profile_idc);
179 for (i = 0; i < 32; i++)
180 rbsp_bit(rbsp, &ptl->general_profile_compatibility_flag[i]);
181 rbsp_bit(rbsp, &ptl->general_progressive_source_flag);
182 rbsp_bit(rbsp, &ptl->general_interlaced_source_flag);
183 rbsp_bit(rbsp, &ptl->general_non_packed_constraint_flag);
184 rbsp_bit(rbsp, &ptl->general_frame_only_constraint_flag);
185 if (ptl->general_profile_idc == 4 ||
186 ptl->general_profile_compatibility_flag[4] ||
187 ptl->general_profile_idc == 5 ||
188 ptl->general_profile_compatibility_flag[5] ||
189 ptl->general_profile_idc == 6 ||
190 ptl->general_profile_compatibility_flag[6] ||
191 ptl->general_profile_idc == 7 ||
192 ptl->general_profile_compatibility_flag[7] ||
193 ptl->general_profile_idc == 8 ||
194 ptl->general_profile_compatibility_flag[8] ||
195 ptl->general_profile_idc == 9 ||
196 ptl->general_profile_compatibility_flag[9] ||
197 ptl->general_profile_idc == 10 ||
198 ptl->general_profile_compatibility_flag[10]) {
199 rbsp_bit(rbsp, &ptl->general_max_12bit_constraint_flag);
200 rbsp_bit(rbsp, &ptl->general_max_10bit_constraint_flag);
201 rbsp_bit(rbsp, &ptl->general_max_8bit_constraint_flag);
202 rbsp_bit(rbsp, &ptl->general_max_422chroma_constraint_flag);
203 rbsp_bit(rbsp, &ptl->general_max_420chroma_constraint_flag);
204 rbsp_bit(rbsp, &ptl->general_max_monochrome_constraint_flag);
205 rbsp_bit(rbsp, &ptl->general_intra_constraint_flag);
206 rbsp_bit(rbsp, &ptl->general_one_picture_only_constraint_flag);
207 rbsp_bit(rbsp, &ptl->general_lower_bit_rate_constraint_flag);
208 if (ptl->general_profile_idc == 5 ||
209 ptl->general_profile_compatibility_flag[5] ||
210 ptl->general_profile_idc == 9 ||
211 ptl->general_profile_compatibility_flag[9] ||
212 ptl->general_profile_idc == 10 ||
213 ptl->general_profile_compatibility_flag[10]) {
214 rbsp_bit(rbsp, &ptl->general_max_14bit_constraint_flag);
215 rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_33bits);
216 rbsp_bits(rbsp, 33 - 32, &ptl->general_reserved_zero_33bits);
218 rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_34bits);
219 rbsp_bits(rbsp, 34 - 2, &ptl->general_reserved_zero_34bits);
221 } else if (ptl->general_profile_idc == 2 ||
222 ptl->general_profile_compatibility_flag[2]) {
223 rbsp_bits(rbsp, 7, &ptl->general_reserved_zero_7bits);
224 rbsp_bit(rbsp, &ptl->general_one_picture_only_constraint_flag);
225 rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_35bits);
226 rbsp_bits(rbsp, 35 - 32, &ptl->general_reserved_zero_35bits);
228 rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_43bits);
229 rbsp_bits(rbsp, 43 - 32, &ptl->general_reserved_zero_43bits);
231 if ((ptl->general_profile_idc >= 1 && ptl->general_profile_idc <= 5) ||
232 ptl->general_profile_idc == 9 ||
233 ptl->general_profile_compatibility_flag[1] ||
234 ptl->general_profile_compatibility_flag[2] ||
235 ptl->general_profile_compatibility_flag[3] ||
236 ptl->general_profile_compatibility_flag[4] ||
237 ptl->general_profile_compatibility_flag[5] ||
238 ptl->general_profile_compatibility_flag[9])
239 rbsp_bit(rbsp, &ptl->general_inbld_flag);
241 rbsp_bit(rbsp, &ptl->general_reserved_zero_bit);
242 rbsp_bits(rbsp, 8, &ptl->general_level_idc);
243 if (max_num_sub_layers_minus_1 > 0)
244 rbsp_unsupported(rbsp);
247 static void nal_hevc_rbsp_vps(struct rbsp *rbsp, struct nal_hevc_vps *vps)
250 unsigned int reserved_0xffff_16bits = 0xffff;
252 rbsp_bits(rbsp, 4, &vps->video_parameter_set_id);
253 rbsp_bit(rbsp, &vps->base_layer_internal_flag);
254 rbsp_bit(rbsp, &vps->base_layer_available_flag);
255 rbsp_bits(rbsp, 6, &vps->max_layers_minus1);
256 rbsp_bits(rbsp, 3, &vps->max_sub_layers_minus1);
257 rbsp_bits(rbsp, 1, &vps->temporal_id_nesting_flag);
258 rbsp_bits(rbsp, 16, &reserved_0xffff_16bits);
259 nal_hevc_rbsp_profile_tier_level(rbsp, &vps->profile_tier_level);
260 rbsp_bit(rbsp, &vps->sub_layer_ordering_info_present_flag);
261 for (i = vps->sub_layer_ordering_info_present_flag ? 0 : vps->max_sub_layers_minus1;
262 i <= vps->max_sub_layers_minus1; i++) {
263 rbsp_uev(rbsp, &vps->max_dec_pic_buffering_minus1[i]);
264 rbsp_uev(rbsp, &vps->max_num_reorder_pics[i]);
265 rbsp_uev(rbsp, &vps->max_latency_increase_plus1[i]);
267 rbsp_bits(rbsp, 6, &vps->max_layer_id);
268 rbsp_uev(rbsp, &vps->num_layer_sets_minus1);
269 for (i = 0; i <= vps->num_layer_sets_minus1; i++)
270 for (j = 0; j <= vps->max_layer_id; j++)
271 rbsp_bit(rbsp, &vps->layer_id_included_flag[i][j]);
272 rbsp_bit(rbsp, &vps->timing_info_present_flag);
273 if (vps->timing_info_present_flag)
274 rbsp_unsupported(rbsp);
275 rbsp_bit(rbsp, &vps->extension_flag);
276 if (vps->extension_flag)
277 rbsp_unsupported(rbsp);
280 static void nal_hevc_rbsp_sps(struct rbsp *rbsp, struct nal_hevc_sps *sps)
284 rbsp_bits(rbsp, 4, &sps->video_parameter_set_id);
285 rbsp_bits(rbsp, 3, &sps->max_sub_layers_minus1);
286 rbsp_bit(rbsp, &sps->temporal_id_nesting_flag);
287 nal_hevc_rbsp_profile_tier_level(rbsp, &sps->profile_tier_level);
288 rbsp_uev(rbsp, &sps->seq_parameter_set_id);
290 rbsp_uev(rbsp, &sps->chroma_format_idc);
291 if (sps->chroma_format_idc == 3)
292 rbsp_bit(rbsp, &sps->separate_colour_plane_flag);
293 rbsp_uev(rbsp, &sps->pic_width_in_luma_samples);
294 rbsp_uev(rbsp, &sps->pic_height_in_luma_samples);
295 rbsp_bit(rbsp, &sps->conformance_window_flag);
296 if (sps->conformance_window_flag) {
297 rbsp_uev(rbsp, &sps->conf_win_left_offset);
298 rbsp_uev(rbsp, &sps->conf_win_right_offset);
299 rbsp_uev(rbsp, &sps->conf_win_top_offset);
300 rbsp_uev(rbsp, &sps->conf_win_bottom_offset);
302 rbsp_uev(rbsp, &sps->bit_depth_luma_minus8);
303 rbsp_uev(rbsp, &sps->bit_depth_chroma_minus8);
305 rbsp_uev(rbsp, &sps->log2_max_pic_order_cnt_lsb_minus4);
307 rbsp_bit(rbsp, &sps->sub_layer_ordering_info_present_flag);
308 for (i = (sps->sub_layer_ordering_info_present_flag ? 0 : sps->max_sub_layers_minus1);
309 i <= sps->max_sub_layers_minus1; i++) {
310 rbsp_uev(rbsp, &sps->max_dec_pic_buffering_minus1[i]);
311 rbsp_uev(rbsp, &sps->max_num_reorder_pics[i]);
312 rbsp_uev(rbsp, &sps->max_latency_increase_plus1[i]);
314 rbsp_uev(rbsp, &sps->log2_min_luma_coding_block_size_minus3);
315 rbsp_uev(rbsp, &sps->log2_diff_max_min_luma_coding_block_size);
316 rbsp_uev(rbsp, &sps->log2_min_luma_transform_block_size_minus2);
317 rbsp_uev(rbsp, &sps->log2_diff_max_min_luma_transform_block_size);
318 rbsp_uev(rbsp, &sps->max_transform_hierarchy_depth_inter);
319 rbsp_uev(rbsp, &sps->max_transform_hierarchy_depth_intra);
321 rbsp_bit(rbsp, &sps->scaling_list_enabled_flag);
322 if (sps->scaling_list_enabled_flag)
323 rbsp_unsupported(rbsp);
325 rbsp_bit(rbsp, &sps->amp_enabled_flag);
326 rbsp_bit(rbsp, &sps->sample_adaptive_offset_enabled_flag);
327 rbsp_bit(rbsp, &sps->pcm_enabled_flag);
328 if (sps->pcm_enabled_flag) {
329 rbsp_bits(rbsp, 4, &sps->pcm_sample_bit_depth_luma_minus1);
330 rbsp_bits(rbsp, 4, &sps->pcm_sample_bit_depth_chroma_minus1);
331 rbsp_uev(rbsp, &sps->log2_min_pcm_luma_coding_block_size_minus3);
332 rbsp_uev(rbsp, &sps->log2_diff_max_min_pcm_luma_coding_block_size);
333 rbsp_bit(rbsp, &sps->pcm_loop_filter_disabled_flag);
336 rbsp_uev(rbsp, &sps->num_short_term_ref_pic_sets);
337 if (sps->num_short_term_ref_pic_sets > 0)
338 rbsp_unsupported(rbsp);
340 rbsp_bit(rbsp, &sps->long_term_ref_pics_present_flag);
341 if (sps->long_term_ref_pics_present_flag)
342 rbsp_unsupported(rbsp);
344 rbsp_bit(rbsp, &sps->sps_temporal_mvp_enabled_flag);
345 rbsp_bit(rbsp, &sps->strong_intra_smoothing_enabled_flag);
346 rbsp_bit(rbsp, &sps->vui_parameters_present_flag);
347 if (sps->vui_parameters_present_flag)
348 rbsp_unsupported(rbsp);
350 rbsp_bit(rbsp, &sps->extension_present_flag);
351 if (sps->extension_present_flag) {
352 rbsp_bit(rbsp, &sps->sps_range_extension_flag);
353 rbsp_bit(rbsp, &sps->sps_multilayer_extension_flag);
354 rbsp_bit(rbsp, &sps->sps_3d_extension_flag);
355 rbsp_bit(rbsp, &sps->sps_scc_extension_flag);
356 rbsp_bits(rbsp, 5, &sps->sps_extension_4bits);
358 if (sps->sps_range_extension_flag)
359 rbsp_unsupported(rbsp);
360 if (sps->sps_multilayer_extension_flag)
361 rbsp_unsupported(rbsp);
362 if (sps->sps_3d_extension_flag)
363 rbsp_unsupported(rbsp);
364 if (sps->sps_scc_extension_flag)
365 rbsp_unsupported(rbsp);
366 if (sps->sps_extension_4bits)
367 rbsp_unsupported(rbsp);
370 static void nal_hevc_rbsp_pps(struct rbsp *rbsp, struct nal_hevc_pps *pps)
374 rbsp_uev(rbsp, &pps->pps_pic_parameter_set_id);
375 rbsp_uev(rbsp, &pps->pps_seq_parameter_set_id);
376 rbsp_bit(rbsp, &pps->dependent_slice_segments_enabled_flag);
377 rbsp_bit(rbsp, &pps->output_flag_present_flag);
378 rbsp_bits(rbsp, 3, &pps->num_extra_slice_header_bits);
379 rbsp_bit(rbsp, &pps->sign_data_hiding_enabled_flag);
380 rbsp_bit(rbsp, &pps->cabac_init_present_flag);
381 rbsp_uev(rbsp, &pps->num_ref_idx_l0_default_active_minus1);
382 rbsp_uev(rbsp, &pps->num_ref_idx_l1_default_active_minus1);
383 rbsp_sev(rbsp, &pps->init_qp_minus26);
384 rbsp_bit(rbsp, &pps->constrained_intra_pred_flag);
385 rbsp_bit(rbsp, &pps->transform_skip_enabled_flag);
386 rbsp_bit(rbsp, &pps->cu_qp_delta_enabled_flag);
387 if (pps->cu_qp_delta_enabled_flag)
388 rbsp_uev(rbsp, &pps->diff_cu_qp_delta_depth);
389 rbsp_sev(rbsp, &pps->pps_cb_qp_offset);
390 rbsp_sev(rbsp, &pps->pps_cr_qp_offset);
391 rbsp_bit(rbsp, &pps->pps_slice_chroma_qp_offsets_present_flag);
392 rbsp_bit(rbsp, &pps->weighted_pred_flag);
393 rbsp_bit(rbsp, &pps->weighted_bipred_flag);
394 rbsp_bit(rbsp, &pps->transquant_bypass_enabled_flag);
395 rbsp_bit(rbsp, &pps->tiles_enabled_flag);
396 rbsp_bit(rbsp, &pps->entropy_coding_sync_enabled_flag);
397 if (pps->tiles_enabled_flag) {
398 rbsp_uev(rbsp, &pps->num_tile_columns_minus1);
399 rbsp_uev(rbsp, &pps->num_tile_rows_minus1);
400 rbsp_bit(rbsp, &pps->uniform_spacing_flag);
401 if (!pps->uniform_spacing_flag) {
402 for (i = 0; i < pps->num_tile_columns_minus1; i++)
403 rbsp_uev(rbsp, &pps->column_width_minus1[i]);
404 for (i = 0; i < pps->num_tile_rows_minus1; i++)
405 rbsp_uev(rbsp, &pps->row_height_minus1[i]);
407 rbsp_bit(rbsp, &pps->loop_filter_across_tiles_enabled_flag);
409 rbsp_bit(rbsp, &pps->pps_loop_filter_across_slices_enabled_flag);
410 rbsp_bit(rbsp, &pps->deblocking_filter_control_present_flag);
411 if (pps->deblocking_filter_control_present_flag) {
412 rbsp_bit(rbsp, &pps->deblocking_filter_override_enabled_flag);
413 rbsp_bit(rbsp, &pps->pps_deblocking_filter_disabled_flag);
414 if (!pps->pps_deblocking_filter_disabled_flag) {
415 rbsp_sev(rbsp, &pps->pps_beta_offset_div2);
416 rbsp_sev(rbsp, &pps->pps_tc_offset_div2);
419 rbsp_bit(rbsp, &pps->pps_scaling_list_data_present_flag);
420 if (pps->pps_scaling_list_data_present_flag)
421 rbsp_unsupported(rbsp);
422 rbsp_bit(rbsp, &pps->lists_modification_present_flag);
423 rbsp_uev(rbsp, &pps->log2_parallel_merge_level_minus2);
424 rbsp_bit(rbsp, &pps->slice_segment_header_extension_present_flag);
425 rbsp_bit(rbsp, &pps->pps_extension_present_flag);
426 if (pps->pps_extension_present_flag) {
427 rbsp_bit(rbsp, &pps->pps_range_extension_flag);
428 rbsp_bit(rbsp, &pps->pps_multilayer_extension_flag);
429 rbsp_bit(rbsp, &pps->pps_3d_extension_flag);
430 rbsp_bit(rbsp, &pps->pps_scc_extension_flag);
431 rbsp_bits(rbsp, 4, &pps->pps_extension_4bits);
433 if (pps->pps_range_extension_flag)
434 rbsp_unsupported(rbsp);
435 if (pps->pps_multilayer_extension_flag)
436 rbsp_unsupported(rbsp);
437 if (pps->pps_3d_extension_flag)
438 rbsp_unsupported(rbsp);
439 if (pps->pps_scc_extension_flag)
440 rbsp_unsupported(rbsp);
441 if (pps->pps_extension_4bits)
442 rbsp_unsupported(rbsp);
446 * nal_hevc_write_vps() - Write PPS NAL unit into RBSP format
447 * @dev: device pointer
448 * @dest: the buffer that is filled with RBSP data
449 * @n: maximum size of @dest in bytes
450 * @vps: &struct nal_hevc_vps to convert to RBSP
452 * Convert @vps to RBSP data and write it into @dest.
454 * The size of the VPS NAL unit is not known in advance and this function will
455 * fail, if @dest does not hold sufficient space for the VPS NAL unit.
457 * Return: number of bytes written to @dest or negative error code
459 ssize_t nal_hevc_write_vps(const struct device *dev,
460 void *dest, size_t n, struct nal_hevc_vps *vps)
463 unsigned int forbidden_zero_bit = 0;
464 unsigned int nal_unit_type = VPS_NUT;
465 unsigned int nuh_layer_id = 0;
466 unsigned int nuh_temporal_id_plus1 = 1;
471 rbsp_init(&rbsp, dest, n, &write);
473 nal_hevc_write_start_code_prefix(&rbsp);
475 /* NAL unit header */
476 rbsp_bit(&rbsp, &forbidden_zero_bit);
477 rbsp_bits(&rbsp, 6, &nal_unit_type);
478 rbsp_bits(&rbsp, 6, &nuh_layer_id);
479 rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
481 nal_hevc_rbsp_vps(&rbsp, vps);
483 rbsp_trailing_bits(&rbsp);
488 return DIV_ROUND_UP(rbsp.pos, 8);
490 EXPORT_SYMBOL_GPL(nal_hevc_write_vps);
493 * nal_hevc_read_vps() - Read VPS NAL unit from RBSP format
494 * @dev: device pointer
495 * @vps: the &struct nal_hevc_vps to fill from the RBSP data
496 * @src: the buffer that contains the RBSP data
497 * @n: size of @src in bytes
499 * Read RBSP data from @src and use it to fill @vps.
501 * Return: number of bytes read from @src or negative error code
503 ssize_t nal_hevc_read_vps(const struct device *dev,
504 struct nal_hevc_vps *vps, void *src, size_t n)
507 unsigned int forbidden_zero_bit;
508 unsigned int nal_unit_type;
509 unsigned int nuh_layer_id;
510 unsigned int nuh_temporal_id_plus1;
515 rbsp_init(&rbsp, src, n, &read);
517 nal_hevc_read_start_code_prefix(&rbsp);
519 rbsp_bit(&rbsp, &forbidden_zero_bit);
520 rbsp_bits(&rbsp, 6, &nal_unit_type);
521 rbsp_bits(&rbsp, 6, &nuh_layer_id);
522 rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
525 forbidden_zero_bit != 0 ||
526 nal_unit_type != VPS_NUT)
529 nal_hevc_rbsp_vps(&rbsp, vps);
531 rbsp_trailing_bits(&rbsp);
536 return DIV_ROUND_UP(rbsp.pos, 8);
538 EXPORT_SYMBOL_GPL(nal_hevc_read_vps);
541 * nal_hevc_write_sps() - Write SPS NAL unit into RBSP format
542 * @dev: device pointer
543 * @dest: the buffer that is filled with RBSP data
544 * @n: maximum size of @dest in bytes
545 * @sps: &struct nal_hevc_sps to convert to RBSP
547 * Convert @sps to RBSP data and write it into @dest.
549 * The size of the SPS NAL unit is not known in advance and this function will
550 * fail, if @dest does not hold sufficient space for the SPS NAL unit.
552 * Return: number of bytes written to @dest or negative error code
554 ssize_t nal_hevc_write_sps(const struct device *dev,
555 void *dest, size_t n, struct nal_hevc_sps *sps)
558 unsigned int forbidden_zero_bit = 0;
559 unsigned int nal_unit_type = SPS_NUT;
560 unsigned int nuh_layer_id = 0;
561 unsigned int nuh_temporal_id_plus1 = 1;
566 rbsp_init(&rbsp, dest, n, &write);
568 nal_hevc_write_start_code_prefix(&rbsp);
570 /* NAL unit header */
571 rbsp_bit(&rbsp, &forbidden_zero_bit);
572 rbsp_bits(&rbsp, 6, &nal_unit_type);
573 rbsp_bits(&rbsp, 6, &nuh_layer_id);
574 rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
576 nal_hevc_rbsp_sps(&rbsp, sps);
578 rbsp_trailing_bits(&rbsp);
583 return DIV_ROUND_UP(rbsp.pos, 8);
585 EXPORT_SYMBOL_GPL(nal_hevc_write_sps);
588 * nal_hevc_read_sps() - Read SPS NAL unit from RBSP format
589 * @dev: device pointer
590 * @sps: the &struct nal_hevc_sps to fill from the RBSP data
591 * @src: the buffer that contains the RBSP data
592 * @n: size of @src in bytes
594 * Read RBSP data from @src and use it to fill @sps.
596 * Return: number of bytes read from @src or negative error code
598 ssize_t nal_hevc_read_sps(const struct device *dev,
599 struct nal_hevc_sps *sps, void *src, size_t n)
602 unsigned int forbidden_zero_bit;
603 unsigned int nal_unit_type;
604 unsigned int nuh_layer_id;
605 unsigned int nuh_temporal_id_plus1;
610 rbsp_init(&rbsp, src, n, &read);
612 nal_hevc_read_start_code_prefix(&rbsp);
614 rbsp_bit(&rbsp, &forbidden_zero_bit);
615 rbsp_bits(&rbsp, 6, &nal_unit_type);
616 rbsp_bits(&rbsp, 6, &nuh_layer_id);
617 rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
620 forbidden_zero_bit != 0 ||
621 nal_unit_type != SPS_NUT)
624 nal_hevc_rbsp_sps(&rbsp, sps);
626 rbsp_trailing_bits(&rbsp);
631 return DIV_ROUND_UP(rbsp.pos, 8);
633 EXPORT_SYMBOL_GPL(nal_hevc_read_sps);
636 * nal_hevc_write_pps() - Write PPS NAL unit into RBSP format
637 * @dev: device pointer
638 * @dest: the buffer that is filled with RBSP data
639 * @n: maximum size of @dest in bytes
640 * @pps: &struct nal_hevc_pps to convert to RBSP
642 * Convert @pps to RBSP data and write it into @dest.
644 * The size of the PPS NAL unit is not known in advance and this function will
645 * fail, if @dest does not hold sufficient space for the PPS NAL unit.
647 * Return: number of bytes written to @dest or negative error code
649 ssize_t nal_hevc_write_pps(const struct device *dev,
650 void *dest, size_t n, struct nal_hevc_pps *pps)
653 unsigned int forbidden_zero_bit = 0;
654 unsigned int nal_unit_type = PPS_NUT;
655 unsigned int nuh_layer_id = 0;
656 unsigned int nuh_temporal_id_plus1 = 1;
661 rbsp_init(&rbsp, dest, n, &write);
663 nal_hevc_write_start_code_prefix(&rbsp);
665 /* NAL unit header */
666 rbsp_bit(&rbsp, &forbidden_zero_bit);
667 rbsp_bits(&rbsp, 6, &nal_unit_type);
668 rbsp_bits(&rbsp, 6, &nuh_layer_id);
669 rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
671 nal_hevc_rbsp_pps(&rbsp, pps);
673 rbsp_trailing_bits(&rbsp);
678 return DIV_ROUND_UP(rbsp.pos, 8);
680 EXPORT_SYMBOL_GPL(nal_hevc_write_pps);
683 * nal_hevc_read_pps() - Read PPS NAL unit from RBSP format
684 * @dev: device pointer
685 * @pps: the &struct nal_hevc_pps to fill from the RBSP data
686 * @src: the buffer that contains the RBSP data
687 * @n: size of @src in bytes
689 * Read RBSP data from @src and use it to fill @pps.
691 * Return: number of bytes read from @src or negative error code
693 ssize_t nal_hevc_read_pps(const struct device *dev,
694 struct nal_hevc_pps *pps, void *src, size_t n)
697 unsigned int forbidden_zero_bit;
698 unsigned int nal_unit_type;
699 unsigned int nuh_layer_id;
700 unsigned int nuh_temporal_id_plus1;
705 rbsp_init(&rbsp, src, n, &read);
707 nal_hevc_read_start_code_prefix(&rbsp);
709 /* NAL unit header */
710 rbsp_bit(&rbsp, &forbidden_zero_bit);
711 rbsp_bits(&rbsp, 6, &nal_unit_type);
712 rbsp_bits(&rbsp, 6, &nuh_layer_id);
713 rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
715 nal_hevc_rbsp_pps(&rbsp, pps);
717 rbsp_trailing_bits(&rbsp);
722 return DIV_ROUND_UP(rbsp.pos, 8);
724 EXPORT_SYMBOL_GPL(nal_hevc_read_pps);
727 * nal_hevc_write_filler() - Write filler data RBSP
728 * @dev: device pointer
729 * @dest: buffer to fill with filler data
730 * @n: size of the buffer to fill with filler data
732 * Write a filler data RBSP to @dest with a size of @n bytes and return the
733 * number of written filler data bytes.
735 * Use this function to generate dummy data in an RBSP data stream that can be
736 * safely ignored by hevc decoders.
738 * The RBSP format of the filler data is specified in Rec. ITU-T H.265
739 * (02/2018) 7.3.2.8 Filler data RBSP syntax.
741 * Return: number of filler data bytes (including marker) or negative error
743 ssize_t nal_hevc_write_filler(const struct device *dev, void *dest, size_t n)
746 unsigned int forbidden_zero_bit = 0;
747 unsigned int nal_unit_type = FD_NUT;
748 unsigned int nuh_layer_id = 0;
749 unsigned int nuh_temporal_id_plus1 = 1;
754 rbsp_init(&rbsp, dest, n, &write);
756 nal_hevc_write_start_code_prefix(&rbsp);
758 rbsp_bit(&rbsp, &forbidden_zero_bit);
759 rbsp_bits(&rbsp, 6, &nal_unit_type);
760 rbsp_bits(&rbsp, 6, &nuh_layer_id);
761 rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
763 nal_hevc_write_filler_data(&rbsp);
764 rbsp_trailing_bits(&rbsp);
769 return DIV_ROUND_UP(rbsp.pos, 8);
771 EXPORT_SYMBOL_GPL(nal_hevc_write_filler);
774 * nal_hevc_read_filler() - Read filler data RBSP
775 * @dev: device pointer
776 * @src: buffer with RBSP data that is read
777 * @n: maximum size of src that shall be read
779 * Read a filler data RBSP from @src up to a maximum size of @n bytes and
780 * return the size of the filler data in bytes including the marker.
782 * This function is used to parse filler data and skip the respective bytes in
785 * The RBSP format of the filler data is specified in Rec. ITU-T H.265
786 * (02/2018) 7.3.2.8 Filler data RBSP syntax.
788 * Return: number of filler data bytes (including marker) or negative error
790 ssize_t nal_hevc_read_filler(const struct device *dev, void *src, size_t n)
793 unsigned int forbidden_zero_bit;
794 unsigned int nal_unit_type;
795 unsigned int nuh_layer_id;
796 unsigned int nuh_temporal_id_plus1;
801 rbsp_init(&rbsp, src, n, &read);
803 nal_hevc_read_start_code_prefix(&rbsp);
805 rbsp_bit(&rbsp, &forbidden_zero_bit);
806 rbsp_bits(&rbsp, 6, &nal_unit_type);
807 rbsp_bits(&rbsp, 6, &nuh_layer_id);
808 rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
812 if (forbidden_zero_bit != 0 ||
813 nal_unit_type != FD_NUT)
816 nal_hevc_read_filler_data(&rbsp);
817 rbsp_trailing_bits(&rbsp);
822 return DIV_ROUND_UP(rbsp.pos, 8);
824 EXPORT_SYMBOL_GPL(nal_hevc_read_filler);