Merge tag 'selinux-pr-20210629' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / media / platform / allegro-dvt / nal-hevc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019-2020 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.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.
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-hevc.h"
25 #include "nal-rbsp.h"
26
27 /*
28  * See Rec. ITU-T H.265 (02/2018) Table 7-1 - NAL unit type codes and NAL unit
29  * type classes
30  */
31 enum nal_unit_type {
32         VPS_NUT = 32,
33         SPS_NUT = 33,
34         PPS_NUT = 34,
35         FD_NUT = 38,
36 };
37
38 int nal_hevc_profile_from_v4l2(enum v4l2_mpeg_video_hevc_profile profile)
39 {
40         switch (profile) {
41         case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
42                 return 1;
43         case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
44                 return 2;
45         case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
46                 return 3;
47         default:
48                 return -EINVAL;
49         }
50 }
51 EXPORT_SYMBOL_GPL(nal_hevc_profile_from_v4l2);
52
53 int nal_hevc_tier_from_v4l2(enum v4l2_mpeg_video_hevc_tier tier)
54 {
55         switch (tier) {
56         case V4L2_MPEG_VIDEO_HEVC_TIER_MAIN:
57                 return 0;
58         case V4L2_MPEG_VIDEO_HEVC_TIER_HIGH:
59                 return 1;
60         default:
61                 return -EINVAL;
62         }
63 }
64 EXPORT_SYMBOL_GPL(nal_hevc_tier_from_v4l2);
65
66 int nal_hevc_level_from_v4l2(enum v4l2_mpeg_video_hevc_level level)
67 {
68         /*
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.
72          */
73         int factor = 30 / 10;
74
75         switch (level) {
76         case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
77                 return factor * 10;
78         case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
79                 return factor * 20;
80         case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
81                 return factor * 21;
82         case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
83                 return factor * 30;
84         case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
85                 return factor * 31;
86         case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
87                 return factor * 40;
88         case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
89                 return factor * 41;
90         case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
91                 return factor * 50;
92         case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
93                 return factor * 51;
94         case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2:
95                 return factor * 52;
96         case V4L2_MPEG_VIDEO_HEVC_LEVEL_6:
97                 return factor * 60;
98         case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1:
99                 return factor * 61;
100         case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2:
101                 return factor * 62;
102         default:
103                 return -EINVAL;
104         }
105 }
106 EXPORT_SYMBOL_GPL(nal_hevc_level_from_v4l2);
107
108 static void nal_hevc_write_start_code_prefix(struct rbsp *rbsp)
109 {
110         u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
111         int i = 4;
112
113         if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
114                 rbsp->error = -EINVAL;
115                 return;
116         }
117
118         p[0] = 0x00;
119         p[1] = 0x00;
120         p[2] = 0x00;
121         p[3] = 0x01;
122
123         rbsp->pos += i * 8;
124 }
125
126 static void nal_hevc_read_start_code_prefix(struct rbsp *rbsp)
127 {
128         u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
129         int i = 4;
130
131         if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
132                 rbsp->error = -EINVAL;
133                 return;
134         }
135
136         if (p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x00 || p[3] != 0x01) {
137                 rbsp->error = -EINVAL;
138                 return;
139         }
140
141         rbsp->pos += i * 8;
142 }
143
144 static void nal_hevc_write_filler_data(struct rbsp *rbsp)
145 {
146         u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
147         int i;
148
149         /* Keep 1 byte extra for terminating the NAL unit */
150         i = rbsp->size - DIV_ROUND_UP(rbsp->pos, 8) - 1;
151         memset(p, 0xff, i);
152         rbsp->pos += i * 8;
153 }
154
155 static void nal_hevc_read_filler_data(struct rbsp *rbsp)
156 {
157         u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
158
159         while (*p == 0xff) {
160                 if (DIV_ROUND_UP(rbsp->pos, 8) > rbsp->size) {
161                         rbsp->error = -EINVAL;
162                         return;
163                 }
164
165                 p++;
166                 rbsp->pos += 8;
167         }
168 }
169
170 static void nal_hevc_rbsp_profile_tier_level(struct rbsp *rbsp,
171                                              struct nal_hevc_profile_tier_level *ptl)
172 {
173         unsigned int i;
174         unsigned int max_num_sub_layers_minus_1 = 0;
175
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);
217                 } else {
218                         rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_34bits);
219                         rbsp_bits(rbsp, 34 - 2, &ptl->general_reserved_zero_34bits);
220                 }
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);
227         } else {
228                 rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_43bits);
229                 rbsp_bits(rbsp, 43 - 32, &ptl->general_reserved_zero_43bits);
230         }
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);
240         else
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);
245 }
246
247 static void nal_hevc_rbsp_vps(struct rbsp *rbsp, struct nal_hevc_vps *vps)
248 {
249         unsigned int i, j;
250         unsigned int reserved_0xffff_16bits = 0xffff;
251
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]);
266         }
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);
278 }
279
280 static void nal_hevc_rbsp_sps(struct rbsp *rbsp, struct nal_hevc_sps *sps)
281 {
282         unsigned int i;
283
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);
289
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);
301         }
302         rbsp_uev(rbsp, &sps->bit_depth_luma_minus8);
303         rbsp_uev(rbsp, &sps->bit_depth_chroma_minus8);
304
305         rbsp_uev(rbsp, &sps->log2_max_pic_order_cnt_lsb_minus4);
306
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]);
313         }
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);
320
321         rbsp_bit(rbsp, &sps->scaling_list_enabled_flag);
322         if (sps->scaling_list_enabled_flag)
323                 rbsp_unsupported(rbsp);
324
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);
334         }
335
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);
339
340         rbsp_bit(rbsp, &sps->long_term_ref_pics_present_flag);
341         if (sps->long_term_ref_pics_present_flag)
342                 rbsp_unsupported(rbsp);
343
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);
349
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);
357         }
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);
368 }
369
370 static void nal_hevc_rbsp_pps(struct rbsp *rbsp, struct nal_hevc_pps *pps)
371 {
372         unsigned int i;
373
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]);
406                 }
407                 rbsp_bit(rbsp, &pps->loop_filter_across_tiles_enabled_flag);
408         }
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);
417                 }
418         }
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);
432         }
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);
443 }
444
445 /**
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
451  *
452  * Convert @vps to RBSP data and write it into @dest.
453  *
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.
456  *
457  * Return: number of bytes written to @dest or negative error code
458  */
459 ssize_t nal_hevc_write_vps(const struct device *dev,
460                            void *dest, size_t n, struct nal_hevc_vps *vps)
461 {
462         struct rbsp rbsp;
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;
467
468         if (!dest)
469                 return -EINVAL;
470
471         rbsp_init(&rbsp, dest, n, &write);
472
473         nal_hevc_write_start_code_prefix(&rbsp);
474
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);
480
481         nal_hevc_rbsp_vps(&rbsp, vps);
482
483         rbsp_trailing_bits(&rbsp);
484
485         if (rbsp.error)
486                 return rbsp.error;
487
488         return DIV_ROUND_UP(rbsp.pos, 8);
489 }
490 EXPORT_SYMBOL_GPL(nal_hevc_write_vps);
491
492 /**
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
498  *
499  * Read RBSP data from @src and use it to fill @vps.
500  *
501  * Return: number of bytes read from @src or negative error code
502  */
503 ssize_t nal_hevc_read_vps(const struct device *dev,
504                           struct nal_hevc_vps *vps, void *src, size_t n)
505 {
506         struct rbsp rbsp;
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;
511
512         if (!src)
513                 return -EINVAL;
514
515         rbsp_init(&rbsp, src, n, &read);
516
517         nal_hevc_read_start_code_prefix(&rbsp);
518
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);
523
524         if (rbsp.error ||
525             forbidden_zero_bit != 0 ||
526             nal_unit_type != VPS_NUT)
527                 return -EINVAL;
528
529         nal_hevc_rbsp_vps(&rbsp, vps);
530
531         rbsp_trailing_bits(&rbsp);
532
533         if (rbsp.error)
534                 return rbsp.error;
535
536         return DIV_ROUND_UP(rbsp.pos, 8);
537 }
538 EXPORT_SYMBOL_GPL(nal_hevc_read_vps);
539
540 /**
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
546  *
547  * Convert @sps to RBSP data and write it into @dest.
548  *
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.
551  *
552  * Return: number of bytes written to @dest or negative error code
553  */
554 ssize_t nal_hevc_write_sps(const struct device *dev,
555                            void *dest, size_t n, struct nal_hevc_sps *sps)
556 {
557         struct rbsp rbsp;
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;
562
563         if (!dest)
564                 return -EINVAL;
565
566         rbsp_init(&rbsp, dest, n, &write);
567
568         nal_hevc_write_start_code_prefix(&rbsp);
569
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);
575
576         nal_hevc_rbsp_sps(&rbsp, sps);
577
578         rbsp_trailing_bits(&rbsp);
579
580         if (rbsp.error)
581                 return rbsp.error;
582
583         return DIV_ROUND_UP(rbsp.pos, 8);
584 }
585 EXPORT_SYMBOL_GPL(nal_hevc_write_sps);
586
587 /**
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
593  *
594  * Read RBSP data from @src and use it to fill @sps.
595  *
596  * Return: number of bytes read from @src or negative error code
597  */
598 ssize_t nal_hevc_read_sps(const struct device *dev,
599                           struct nal_hevc_sps *sps, void *src, size_t n)
600 {
601         struct rbsp rbsp;
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;
606
607         if (!src)
608                 return -EINVAL;
609
610         rbsp_init(&rbsp, src, n, &read);
611
612         nal_hevc_read_start_code_prefix(&rbsp);
613
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);
618
619         if (rbsp.error ||
620             forbidden_zero_bit != 0 ||
621             nal_unit_type != SPS_NUT)
622                 return -EINVAL;
623
624         nal_hevc_rbsp_sps(&rbsp, sps);
625
626         rbsp_trailing_bits(&rbsp);
627
628         if (rbsp.error)
629                 return rbsp.error;
630
631         return DIV_ROUND_UP(rbsp.pos, 8);
632 }
633 EXPORT_SYMBOL_GPL(nal_hevc_read_sps);
634
635 /**
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
641  *
642  * Convert @pps to RBSP data and write it into @dest.
643  *
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.
646  *
647  * Return: number of bytes written to @dest or negative error code
648  */
649 ssize_t nal_hevc_write_pps(const struct device *dev,
650                            void *dest, size_t n, struct nal_hevc_pps *pps)
651 {
652         struct rbsp rbsp;
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;
657
658         if (!dest)
659                 return -EINVAL;
660
661         rbsp_init(&rbsp, dest, n, &write);
662
663         nal_hevc_write_start_code_prefix(&rbsp);
664
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);
670
671         nal_hevc_rbsp_pps(&rbsp, pps);
672
673         rbsp_trailing_bits(&rbsp);
674
675         if (rbsp.error)
676                 return rbsp.error;
677
678         return DIV_ROUND_UP(rbsp.pos, 8);
679 }
680 EXPORT_SYMBOL_GPL(nal_hevc_write_pps);
681
682 /**
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
688  *
689  * Read RBSP data from @src and use it to fill @pps.
690  *
691  * Return: number of bytes read from @src or negative error code
692  */
693 ssize_t nal_hevc_read_pps(const struct device *dev,
694                           struct nal_hevc_pps *pps, void *src, size_t n)
695 {
696         struct rbsp rbsp;
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;
701
702         if (!src)
703                 return -EINVAL;
704
705         rbsp_init(&rbsp, src, n, &read);
706
707         nal_hevc_read_start_code_prefix(&rbsp);
708
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);
714
715         nal_hevc_rbsp_pps(&rbsp, pps);
716
717         rbsp_trailing_bits(&rbsp);
718
719         if (rbsp.error)
720                 return rbsp.error;
721
722         return DIV_ROUND_UP(rbsp.pos, 8);
723 }
724 EXPORT_SYMBOL_GPL(nal_hevc_read_pps);
725
726 /**
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
731  *
732  * Write a filler data RBSP to @dest with a size of @n bytes and return the
733  * number of written filler data bytes.
734  *
735  * Use this function to generate dummy data in an RBSP data stream that can be
736  * safely ignored by hevc decoders.
737  *
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.
740  *
741  * Return: number of filler data bytes (including marker) or negative error
742  */
743 ssize_t nal_hevc_write_filler(const struct device *dev, void *dest, size_t n)
744 {
745         struct rbsp rbsp;
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;
750
751         if (!dest)
752                 return -EINVAL;
753
754         rbsp_init(&rbsp, dest, n, &write);
755
756         nal_hevc_write_start_code_prefix(&rbsp);
757
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);
762
763         nal_hevc_write_filler_data(&rbsp);
764         rbsp_trailing_bits(&rbsp);
765
766         if (rbsp.error)
767                 return rbsp.error;
768
769         return DIV_ROUND_UP(rbsp.pos, 8);
770 }
771 EXPORT_SYMBOL_GPL(nal_hevc_write_filler);
772
773 /**
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
778  *
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.
781  *
782  * This function is used to parse filler data and skip the respective bytes in
783  * the RBSP data.
784  *
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.
787  *
788  * Return: number of filler data bytes (including marker) or negative error
789  */
790 ssize_t nal_hevc_read_filler(const struct device *dev, void *src, size_t n)
791 {
792         struct rbsp rbsp;
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;
797
798         if (!src)
799                 return -EINVAL;
800
801         rbsp_init(&rbsp, src, n, &read);
802
803         nal_hevc_read_start_code_prefix(&rbsp);
804
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);
809
810         if (rbsp.error)
811                 return rbsp.error;
812         if (forbidden_zero_bit != 0 ||
813             nal_unit_type != FD_NUT)
814                 return -EINVAL;
815
816         nal_hevc_read_filler_data(&rbsp);
817         rbsp_trailing_bits(&rbsp);
818
819         if (rbsp.error)
820                 return rbsp.error;
821
822         return DIV_ROUND_UP(rbsp.pos, 8);
823 }
824 EXPORT_SYMBOL_GPL(nal_hevc_read_filler);