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