1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 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.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.
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.264 (04/2017) Table 7-1 - NAL unit type codes, syntax
29 * element categories, and NAL unit type classes
32 SEQUENCE_PARAMETER_SET = 7,
33 PICTURE_PARAMETER_SET = 8,
37 static void nal_h264_write_start_code_prefix(struct rbsp *rbsp)
39 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
42 if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
43 rbsp->error = -EINVAL;
55 static void nal_h264_read_start_code_prefix(struct rbsp *rbsp)
57 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
60 if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
61 rbsp->error = -EINVAL;
65 if (p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x00 || p[3] != 0x01) {
66 rbsp->error = -EINVAL;
73 static void nal_h264_write_filler_data(struct rbsp *rbsp)
75 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
78 /* Keep 1 byte extra for terminating the NAL unit */
79 i = rbsp->size - DIV_ROUND_UP(rbsp->pos, 8) - 1;
84 static void nal_h264_read_filler_data(struct rbsp *rbsp)
86 u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
89 if (DIV_ROUND_UP(rbsp->pos, 8) > rbsp->size) {
90 rbsp->error = -EINVAL;
99 static void nal_h264_rbsp_hrd_parameters(struct rbsp *rbsp,
100 struct nal_h264_hrd_parameters *hrd)
105 rbsp->error = -EINVAL;
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);
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]);
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);
125 static void nal_h264_rbsp_vui_parameters(struct rbsp *rbsp,
126 struct nal_h264_vui_parameters *vui)
129 rbsp->error = -EINVAL;
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);
142 rbsp_bit(rbsp, &vui->overscan_info_present_flag);
143 if (vui->overscan_info_present_flag)
144 rbsp_bit(rbsp, &vui->overscan_appropriate_flag);
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);
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);
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);
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);
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);
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);
180 if (vui->nal_hrd_parameters_present_flag ||
181 vui->vcl_hrd_parameters_present_flag)
182 rbsp_bit(rbsp, &vui->low_delay_hrd_flag);
184 rbsp_bit(rbsp, &vui->pic_struct_present_flag);
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);
198 static void nal_h264_rbsp_sps(struct rbsp *rbsp, struct nal_h264_sps *sps)
203 rbsp->error = -EINVAL;
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);
217 rbsp_uev(rbsp, &sps->seq_parameter_set_id);
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);
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;
238 rbsp_uev(rbsp, &sps->log2_max_frame_num_minus4);
240 rbsp_uev(rbsp, &sps->pic_order_cnt_type);
241 switch (sps->pic_order_cnt_type) {
243 rbsp_uev(rbsp, &sps->log2_max_pic_order_cnt_lsb_minus4);
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);
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]);
255 rbsp->error = -EINVAL;
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);
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);
268 rbsp_bit(rbsp, &sps->direct_8x8_inference_flag);
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);
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);
283 static void nal_h264_rbsp_pps(struct rbsp *rbsp, struct nal_h264_pps *pps)
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) {
296 for (i = 0; i < pps->num_slice_groups_minus1; i++)
297 rbsp_uev(rbsp, &pps->run_length_minus1[i]);
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]);
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);
310 rbsp_uev(rbsp, &pps->pic_size_in_map_units_minus1);
311 for (i = 0; i < pps->pic_size_in_map_units_minus1; i++)
313 order_base_2(pps->num_slice_groups_minus1 + 1),
314 &pps->slice_group_id[i]);
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);
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
346 * Convert @sps to RBSP data and write it into @dest.
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.
351 * Return: number of bytes written to @dest or negative error code
353 ssize_t nal_h264_write_sps(const struct device *dev,
354 void *dest, size_t n, struct nal_h264_sps *sps)
357 unsigned int forbidden_zero_bit = 0;
358 unsigned int nal_ref_idc = 0;
359 unsigned int nal_unit_type = SEQUENCE_PARAMETER_SET;
364 rbsp_init(&rbsp, dest, n, &write);
366 nal_h264_write_start_code_prefix(&rbsp);
368 rbsp_bit(&rbsp, &forbidden_zero_bit);
369 rbsp_bits(&rbsp, 2, &nal_ref_idc);
370 rbsp_bits(&rbsp, 5, &nal_unit_type);
372 nal_h264_rbsp_sps(&rbsp, sps);
374 rbsp_trailing_bits(&rbsp);
379 return DIV_ROUND_UP(rbsp.pos, 8);
381 EXPORT_SYMBOL_GPL(nal_h264_write_sps);
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
390 * Read RBSP data from @src and use it to fill @sps.
392 * Return: number of bytes read from @src or negative error code
394 ssize_t nal_h264_read_sps(const struct device *dev,
395 struct nal_h264_sps *sps, void *src, size_t n)
398 unsigned int forbidden_zero_bit;
399 unsigned int nal_ref_idc;
400 unsigned int nal_unit_type;
405 rbsp_init(&rbsp, src, n, &read);
407 nal_h264_read_start_code_prefix(&rbsp);
409 rbsp_bit(&rbsp, &forbidden_zero_bit);
410 rbsp_bits(&rbsp, 2, &nal_ref_idc);
411 rbsp_bits(&rbsp, 5, &nal_unit_type);
414 forbidden_zero_bit != 0 ||
416 nal_unit_type != SEQUENCE_PARAMETER_SET)
419 nal_h264_rbsp_sps(&rbsp, sps);
421 rbsp_trailing_bits(&rbsp);
426 return DIV_ROUND_UP(rbsp.pos, 8);
428 EXPORT_SYMBOL_GPL(nal_h264_read_sps);
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
437 * Convert @pps to RBSP data and write it into @dest.
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.
442 * Return: number of bytes written to @dest or negative error code
444 ssize_t nal_h264_write_pps(const struct device *dev,
445 void *dest, size_t n, struct nal_h264_pps *pps)
448 unsigned int forbidden_zero_bit = 0;
449 unsigned int nal_ref_idc = 0;
450 unsigned int nal_unit_type = PICTURE_PARAMETER_SET;
455 rbsp_init(&rbsp, dest, n, &write);
457 nal_h264_write_start_code_prefix(&rbsp);
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);
464 nal_h264_rbsp_pps(&rbsp, pps);
466 rbsp_trailing_bits(&rbsp);
471 return DIV_ROUND_UP(rbsp.pos, 8);
473 EXPORT_SYMBOL_GPL(nal_h264_write_pps);
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
482 * Read RBSP data from @src and use it to fill @pps.
484 * Return: number of bytes read from @src or negative error code
486 ssize_t nal_h264_read_pps(const struct device *dev,
487 struct nal_h264_pps *pps, void *src, size_t n)
494 rbsp_init(&rbsp, src, n, &read);
496 nal_h264_read_start_code_prefix(&rbsp);
498 /* NAL unit header */
501 nal_h264_rbsp_pps(&rbsp, pps);
503 rbsp_trailing_bits(&rbsp);
508 return DIV_ROUND_UP(rbsp.pos, 8);
510 EXPORT_SYMBOL_GPL(nal_h264_read_pps);
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
518 * Write a filler data RBSP to @dest with a size of @n bytes and return the
519 * number of written filler data bytes.
521 * Use this function to generate dummy data in an RBSP data stream that can be
522 * safely ignored by h264 decoders.
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.
527 * Return: number of filler data bytes (including marker) or negative error
529 ssize_t nal_h264_write_filler(const struct device *dev, void *dest, size_t n)
532 unsigned int forbidden_zero_bit = 0;
533 unsigned int nal_ref_idc = 0;
534 unsigned int nal_unit_type = FILLER_DATA;
539 rbsp_init(&rbsp, dest, n, &write);
541 nal_h264_write_start_code_prefix(&rbsp);
543 rbsp_bit(&rbsp, &forbidden_zero_bit);
544 rbsp_bits(&rbsp, 2, &nal_ref_idc);
545 rbsp_bits(&rbsp, 5, &nal_unit_type);
547 nal_h264_write_filler_data(&rbsp);
549 rbsp_trailing_bits(&rbsp);
551 return DIV_ROUND_UP(rbsp.pos, 8);
553 EXPORT_SYMBOL_GPL(nal_h264_write_filler);
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
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.
564 * This function is used to parse filler data and skip the respective bytes in
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.
570 * Return: number of filler data bytes (including marker) or negative error
572 ssize_t nal_h264_read_filler(const struct device *dev, void *src, size_t n)
575 unsigned int forbidden_zero_bit;
576 unsigned int nal_ref_idc;
577 unsigned int nal_unit_type;
582 rbsp_init(&rbsp, src, n, &read);
584 nal_h264_read_start_code_prefix(&rbsp);
586 rbsp_bit(&rbsp, &forbidden_zero_bit);
587 rbsp_bits(&rbsp, 2, &nal_ref_idc);
588 rbsp_bits(&rbsp, 5, &nal_unit_type);
592 if (forbidden_zero_bit != 0 ||
594 nal_unit_type != FILLER_DATA)
597 nal_h264_read_filler_data(&rbsp);
598 rbsp_trailing_bits(&rbsp);
603 return DIV_ROUND_UP(rbsp.pos, 8);
605 EXPORT_SYMBOL_GPL(nal_h264_read_filler);