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.
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/v4l2-controls.h>
15 #include <linux/videodev2.h>
18 * struct nal_h264_hrd_parameters - HRD parameters
20 * C struct representation of the sequence parameter set NAL unit as defined by
21 * Rec. ITU-T H.264 (04/2017) E.1.2 HRD parameters syntax.
23 struct nal_h264_hrd_parameters
{
24 unsigned int cpb_cnt_minus1
;
25 unsigned int bit_rate_scale
;
26 unsigned int cpb_size_scale
;
28 int bit_rate_value_minus1
[16];
29 int cpb_size_value_minus1
[16];
30 unsigned int cbr_flag
[16];
32 unsigned int initial_cpb_removal_delay_length_minus1
;
33 unsigned int cpb_removal_delay_length_minus1
;
34 unsigned int dpb_output_delay_length_minus1
;
35 unsigned int time_offset_length
;
39 * struct nal_h264_vui_parameters - VUI parameters
41 * C struct representation of the VUI parameters as defined by Rec. ITU-T
42 * H.264 (04/2017) E.1.1 VUI parameters syntax.
44 struct nal_h264_vui_parameters
{
45 unsigned int aspect_ratio_info_present_flag
;
47 unsigned int aspect_ratio_idc
;
48 unsigned int sar_width
;
49 unsigned int sar_height
;
51 unsigned int overscan_info_present_flag
;
52 unsigned int overscan_appropriate_flag
;
53 unsigned int video_signal_type_present_flag
;
55 unsigned int video_format
;
56 unsigned int video_full_range_flag
;
57 unsigned int colour_description_present_flag
;
59 unsigned int colour_primaries
;
60 unsigned int transfer_characteristics
;
61 unsigned int matrix_coefficients
;
64 unsigned int chroma_loc_info_present_flag
;
66 unsigned int chroma_sample_loc_type_top_field
;
67 unsigned int chroma_sample_loc_type_bottom_field
;
69 unsigned int timing_info_present_flag
;
71 unsigned int num_units_in_tick
;
72 unsigned int time_scale
;
73 unsigned int fixed_frame_rate_flag
;
75 unsigned int nal_hrd_parameters_present_flag
;
76 struct nal_h264_hrd_parameters nal_hrd_parameters
;
77 unsigned int vcl_hrd_parameters_present_flag
;
78 struct nal_h264_hrd_parameters vcl_hrd_parameters
;
79 unsigned int low_delay_hrd_flag
;
80 unsigned int pic_struct_present_flag
;
81 unsigned int bitstream_restriction_flag
;
83 unsigned int motion_vectors_over_pic_boundaries_flag
;
84 unsigned int max_bytes_per_pic_denom
;
85 unsigned int max_bits_per_mb_denom
;
86 unsigned int log2_max_mv_length_horizontal
;
87 unsigned int log21_max_mv_length_vertical
;
88 unsigned int max_num_reorder_frames
;
89 unsigned int max_dec_frame_buffering
;
94 * struct nal_h264_sps - Sequence parameter set
96 * C struct representation of the sequence parameter set NAL unit as defined by
97 * Rec. ITU-T H.264 (04/2017) 7.3.2.1.1 Sequence parameter set data syntax.
100 unsigned int profile_idc
;
101 unsigned int constraint_set0_flag
;
102 unsigned int constraint_set1_flag
;
103 unsigned int constraint_set2_flag
;
104 unsigned int constraint_set3_flag
;
105 unsigned int constraint_set4_flag
;
106 unsigned int constraint_set5_flag
;
107 unsigned int reserved_zero_2bits
;
108 unsigned int level_idc
;
109 unsigned int seq_parameter_set_id
;
111 unsigned int chroma_format_idc
;
112 unsigned int separate_colour_plane_flag
;
113 unsigned int bit_depth_luma_minus8
;
114 unsigned int bit_depth_chroma_minus8
;
115 unsigned int qpprime_y_zero_transform_bypass_flag
;
116 unsigned int seq_scaling_matrix_present_flag
;
118 unsigned int log2_max_frame_num_minus4
;
119 unsigned int pic_order_cnt_type
;
121 unsigned int log2_max_pic_order_cnt_lsb_minus4
;
123 unsigned int delta_pic_order_always_zero_flag
;
124 int offset_for_non_ref_pic
;
125 int offset_for_top_to_bottom_field
;
126 unsigned int num_ref_frames_in_pic_order_cnt_cycle
;
127 int offset_for_ref_frame
[255];
130 unsigned int max_num_ref_frames
;
131 unsigned int gaps_in_frame_num_value_allowed_flag
;
132 unsigned int pic_width_in_mbs_minus1
;
133 unsigned int pic_height_in_map_units_minus1
;
134 unsigned int frame_mbs_only_flag
;
135 unsigned int mb_adaptive_frame_field_flag
;
136 unsigned int direct_8x8_inference_flag
;
137 unsigned int frame_cropping_flag
;
139 unsigned int crop_left
;
140 unsigned int crop_right
;
141 unsigned int crop_top
;
142 unsigned int crop_bottom
;
144 unsigned int vui_parameters_present_flag
;
145 struct nal_h264_vui_parameters vui
;
149 * struct nal_h264_pps - Picture parameter set
151 * C struct representation of the picture parameter set NAL unit as defined by
152 * Rec. ITU-T H.264 (04/2017) 7.3.2.2 Picture parameter set RBSP syntax.
154 struct nal_h264_pps
{
155 unsigned int pic_parameter_set_id
;
156 unsigned int seq_parameter_set_id
;
157 unsigned int entropy_coding_mode_flag
;
158 unsigned int bottom_field_pic_order_in_frame_present_flag
;
159 unsigned int num_slice_groups_minus1
;
160 unsigned int slice_group_map_type
;
162 unsigned int run_length_minus1
[8];
164 unsigned int top_left
[8];
165 unsigned int bottom_right
[8];
168 unsigned int slice_group_change_direction_flag
;
169 unsigned int slice_group_change_rate_minus1
;
172 unsigned int pic_size_in_map_units_minus1
;
173 unsigned int slice_group_id
[8];
176 unsigned int num_ref_idx_l0_default_active_minus1
;
177 unsigned int num_ref_idx_l1_default_active_minus1
;
178 unsigned int weighted_pred_flag
;
179 unsigned int weighted_bipred_idc
;
180 int pic_init_qp_minus26
;
181 int pic_init_qs_minus26
;
182 int chroma_qp_index_offset
;
183 unsigned int deblocking_filter_control_present_flag
;
184 unsigned int constrained_intra_pred_flag
;
185 unsigned int redundant_pic_cnt_present_flag
;
187 unsigned int transform_8x8_mode_flag
;
188 unsigned int pic_scaling_matrix_present_flag
;
189 int second_chroma_qp_index_offset
;
194 * nal_h264_profile() - Get profile_idc for v4l2 h264 profile
195 * @profile: the profile as &enum v4l2_mpeg_video_h264_profile
197 * Convert the &enum v4l2_mpeg_video_h264_profile to profile_idc as specified
198 * in Rec. ITU-T H.264 (04/2017) A.2.
200 * Return: the profile_idc for the passed level
202 static inline int nal_h264_profile(enum v4l2_mpeg_video_h264_profile profile
)
205 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
:
207 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN
:
209 case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED
:
211 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
:
219 * nal_h264_level() - Get level_idc for v4l2 h264 level
220 * @level: the level as &enum v4l2_mpeg_video_h264_level
222 * Convert the &enum v4l2_mpeg_video_h264_level to level_idc as specified in
223 * Rec. ITU-T H.264 (04/2017) A.3.2.
225 * Return: the level_idc for the passed level
227 static inline int nal_h264_level(enum v4l2_mpeg_video_h264_level level
)
230 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0
:
232 case V4L2_MPEG_VIDEO_H264_LEVEL_1B
:
234 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1
:
236 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2
:
238 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3
:
240 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0
:
242 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1
:
244 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2
:
246 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0
:
248 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1
:
250 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2
:
252 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0
:
254 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1
:
256 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2
:
258 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0
:
260 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1
:
268 * nal_h264_full_range() - Get video_full_range_flag for v4l2 quantization
269 * @quantization: the quantization type as &enum v4l2_quantization
271 * Convert the &enum v4l2_quantization to video_full_range_flag as specified in
272 * Rec. ITU-T H.264 (04/2017) E.2.1.
274 * Return: the video_full_range_flag value for the passed quantization
276 static inline int nal_h264_full_range(enum v4l2_quantization quantization
)
278 switch (quantization
) {
279 case V4L2_QUANTIZATION_FULL_RANGE
:
281 case V4L2_QUANTIZATION_LIM_RANGE
:
291 * nal_h264_color_primaries() - Get color_primaries for v4l2 colorspace
292 * @colorspace: the color space as &enum v4l2_colorspace
294 * Convert the &enum v4l2_colorspace to color_primaries as specified in
295 * Rec. ITU-T H.264 (04/2017) E.2.1.
297 * Return: the color_primaries value for the passed colorspace
299 static inline int nal_h264_color_primaries(enum v4l2_colorspace colorspace
)
301 switch (colorspace
) {
302 case V4L2_COLORSPACE_SMPTE170M
:
304 case V4L2_COLORSPACE_SMPTE240M
:
306 case V4L2_COLORSPACE_REC709
:
308 case V4L2_COLORSPACE_470_SYSTEM_M
:
310 case V4L2_COLORSPACE_JPEG
:
311 case V4L2_COLORSPACE_SRGB
:
312 case V4L2_COLORSPACE_470_SYSTEM_BG
:
314 case V4L2_COLORSPACE_BT2020
:
316 case V4L2_COLORSPACE_DEFAULT
:
317 case V4L2_COLORSPACE_OPRGB
:
318 case V4L2_COLORSPACE_RAW
:
319 case V4L2_COLORSPACE_DCI_P3
:
326 * nal_h264_transfer_characteristics() - Get transfer_characteristics for v4l2 xfer_func
327 * @colorspace: the color space as &enum v4l2_colorspace
328 * @xfer_func: the transfer function as &enum v4l2_xfer_func
330 * Convert the &enum v4l2_xfer_func to transfer_characteristics as specified in
331 * Rec. ITU-T H.264 (04/2017) E.2.1.
333 * Return: the transfer_characteristics value for the passed transfer function
335 static inline int nal_h264_transfer_characteristics(enum v4l2_colorspace colorspace
,
336 enum v4l2_xfer_func xfer_func
)
338 if (xfer_func
== V4L2_XFER_FUNC_DEFAULT
)
339 xfer_func
= V4L2_MAP_XFER_FUNC_DEFAULT(colorspace
);
342 case V4L2_XFER_FUNC_709
:
344 case V4L2_XFER_FUNC_SMPTE2084
:
346 case V4L2_XFER_FUNC_SRGB
:
347 case V4L2_XFER_FUNC_OPRGB
:
348 case V4L2_XFER_FUNC_NONE
:
349 case V4L2_XFER_FUNC_DCI_P3
:
350 case V4L2_XFER_FUNC_SMPTE240M
:
357 * nal_h264_matrix_coeffs() - Get matrix_coefficients for v4l2 v4l2_ycbcr_encoding
358 * @colorspace: the color space as &enum v4l2_colorspace
359 * @ycbcr_encoding: the ycbcr encoding as &enum v4l2_ycbcr_encoding
361 * Convert the &enum v4l2_ycbcr_encoding to matrix_coefficients as specified in
362 * Rec. ITU-T H.264 (04/2017) E.2.1.
364 * Return: the matrix_coefficients value for the passed encoding
366 static inline int nal_h264_matrix_coeffs(enum v4l2_colorspace colorspace
,
367 enum v4l2_ycbcr_encoding ycbcr_encoding
)
369 if (ycbcr_encoding
== V4L2_YCBCR_ENC_DEFAULT
)
370 ycbcr_encoding
= V4L2_MAP_YCBCR_ENC_DEFAULT(colorspace
);
372 switch (ycbcr_encoding
) {
373 case V4L2_YCBCR_ENC_601
:
374 case V4L2_YCBCR_ENC_XV601
:
376 case V4L2_YCBCR_ENC_709
:
377 case V4L2_YCBCR_ENC_XV709
:
379 case V4L2_YCBCR_ENC_BT2020
:
381 case V4L2_YCBCR_ENC_BT2020_CONST_LUM
:
383 case V4L2_YCBCR_ENC_SMPTE240M
:
389 ssize_t
nal_h264_write_sps(const struct device
*dev
,
390 void *dest
, size_t n
, struct nal_h264_sps
*sps
);
391 ssize_t
nal_h264_read_sps(const struct device
*dev
,
392 struct nal_h264_sps
*sps
, void *src
, size_t n
);
393 void nal_h264_print_sps(const struct device
*dev
, struct nal_h264_sps
*sps
);
395 ssize_t
nal_h264_write_pps(const struct device
*dev
,
396 void *dest
, size_t n
, struct nal_h264_pps
*pps
);
397 ssize_t
nal_h264_read_pps(const struct device
*dev
,
398 struct nal_h264_pps
*pps
, void *src
, size_t n
);
399 void nal_h264_print_pps(const struct device
*dev
, struct nal_h264_pps
*pps
);
401 ssize_t
nal_h264_write_filler(const struct device
*dev
, void *dest
, size_t n
);
402 ssize_t
nal_h264_read_filler(const struct device
*dev
, void *src
, size_t n
);
404 #endif /* __NAL_H264_H__ */