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 
8 #ifndef __NAL_H264_H__
9 #define __NAL_H264_H__
10 
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 
14 /**
15  * struct nal_h264_hdr_parameters - HDR parameters
16  *
17  * C struct representation of the sequence parameter set NAL unit as defined by
18  * Rec. ITU-T H.264 (04/2017) E.1.2 HRD parameters syntax.
19  */
20 struct nal_h264_hrd_parameters {
21 	unsigned int cpb_cnt_minus1;
22 	unsigned int bit_rate_scale;
23 	unsigned int cpb_size_scale;
24 	struct {
25 		int bit_rate_value_minus1[16];
26 		int cpb_size_value_minus1[16];
27 		unsigned int cbr_flag[16];
28 	};
29 	unsigned int initial_cpb_removal_delay_length_minus1;
30 	unsigned int cpb_removal_delay_length_minus1;
31 	unsigned int dpb_output_delay_length_minus1;
32 	unsigned int time_offset_length;
33 };
34 
35 /**
36  * struct nal_h264_vui_parameters - VUI parameters
37  *
38  * C struct representation of the VUI parameters as defined by Rec. ITU-T
39  * H.264 (04/2017) E.1.1 VUI parameters syntax.
40  */
41 struct nal_h264_vui_parameters {
42 	unsigned int aspect_ratio_info_present_flag;
43 	struct {
44 		unsigned int aspect_ratio_idc;
45 		unsigned int sar_width;
46 		unsigned int sar_height;
47 	};
48 	unsigned int overscan_info_present_flag;
49 	unsigned int overscan_appropriate_flag;
50 	unsigned int video_signal_type_present_flag;
51 	struct {
52 		unsigned int video_format;
53 		unsigned int video_full_range_flag;
54 		unsigned int colour_description_present_flag;
55 		struct {
56 			unsigned int colour_primaries;
57 			unsigned int transfer_characteristics;
58 			unsigned int matrix_coefficients;
59 		};
60 	};
61 	unsigned int chroma_loc_info_present_flag;
62 	struct {
63 		unsigned int chroma_sample_loc_type_top_field;
64 		unsigned int chroma_sample_loc_type_bottom_field;
65 	};
66 	unsigned int timing_info_present_flag;
67 	struct {
68 		unsigned int num_units_in_tick;
69 		unsigned int time_scale;
70 		unsigned int fixed_frame_rate_flag;
71 	};
72 	unsigned int nal_hrd_parameters_present_flag;
73 	struct nal_h264_hrd_parameters nal_hrd_parameters;
74 	unsigned int vcl_hrd_parameters_present_flag;
75 	struct nal_h264_hrd_parameters vcl_hrd_parameters;
76 	unsigned int low_delay_hrd_flag;
77 	unsigned int pic_struct_present_flag;
78 	unsigned int bitstream_restriction_flag;
79 	struct {
80 		unsigned int motion_vectors_over_pic_boundaries_flag;
81 		unsigned int max_bytes_per_pic_denom;
82 		unsigned int max_bits_per_mb_denom;
83 		unsigned int log2_max_mv_length_horizontal;
84 		unsigned int log21_max_mv_length_vertical;
85 		unsigned int max_num_reorder_frames;
86 		unsigned int max_dec_frame_buffering;
87 	};
88 };
89 
90 /**
91  * struct nal_h264_sps - Sequence parameter set
92  *
93  * C struct representation of the sequence parameter set NAL unit as defined by
94  * Rec. ITU-T H.264 (04/2017) 7.3.2.1.1 Sequence parameter set data syntax.
95  */
96 struct nal_h264_sps {
97 	unsigned int profile_idc;
98 	unsigned int constraint_set0_flag;
99 	unsigned int constraint_set1_flag;
100 	unsigned int constraint_set2_flag;
101 	unsigned int constraint_set3_flag;
102 	unsigned int constraint_set4_flag;
103 	unsigned int constraint_set5_flag;
104 	unsigned int reserved_zero_2bits;
105 	unsigned int level_idc;
106 	unsigned int seq_parameter_set_id;
107 	struct {
108 		unsigned int chroma_format_idc;
109 		unsigned int separate_colour_plane_flag;
110 		unsigned int bit_depth_luma_minus8;
111 		unsigned int bit_depth_chroma_minus8;
112 		unsigned int qpprime_y_zero_transform_bypass_flag;
113 		unsigned int seq_scaling_matrix_present_flag;
114 	};
115 	unsigned int log2_max_frame_num_minus4;
116 	unsigned int pic_order_cnt_type;
117 	union {
118 		unsigned int log2_max_pic_order_cnt_lsb_minus4;
119 		struct {
120 			unsigned int delta_pic_order_always_zero_flag;
121 			int offset_for_non_ref_pic;
122 			int offset_for_top_to_bottom_field;
123 			unsigned int num_ref_frames_in_pic_order_cnt_cycle;
124 			int offset_for_ref_frame[255];
125 		};
126 	};
127 	unsigned int max_num_ref_frames;
128 	unsigned int gaps_in_frame_num_value_allowed_flag;
129 	unsigned int pic_width_in_mbs_minus1;
130 	unsigned int pic_height_in_map_units_minus1;
131 	unsigned int frame_mbs_only_flag;
132 	unsigned int mb_adaptive_frame_field_flag;
133 	unsigned int direct_8x8_inference_flag;
134 	unsigned int frame_cropping_flag;
135 	struct {
136 		unsigned int crop_left;
137 		unsigned int crop_right;
138 		unsigned int crop_top;
139 		unsigned int crop_bottom;
140 	};
141 	unsigned int vui_parameters_present_flag;
142 	struct nal_h264_vui_parameters vui;
143 };
144 
145 /**
146  * struct nal_h264_pps - Picture parameter set
147  *
148  * C struct representation of the picture parameter set NAL unit as defined by
149  * Rec. ITU-T H.264 (04/2017) 7.3.2.2 Picture parameter set RBSP syntax.
150  */
151 struct nal_h264_pps {
152 	unsigned int pic_parameter_set_id;
153 	unsigned int seq_parameter_set_id;
154 	unsigned int entropy_coding_mode_flag;
155 	unsigned int bottom_field_pic_order_in_frame_present_flag;
156 	unsigned int num_slice_groups_minus1;
157 	unsigned int slice_group_map_type;
158 	union {
159 		unsigned int run_length_minus1[8];
160 		struct {
161 			unsigned int top_left[8];
162 			unsigned int bottom_right[8];
163 		};
164 		struct {
165 			unsigned int slice_group_change_direction_flag;
166 			unsigned int slice_group_change_rate_minus1;
167 		};
168 		struct {
169 			unsigned int pic_size_in_map_units_minus1;
170 			unsigned int slice_group_id[8];
171 		};
172 	};
173 	unsigned int num_ref_idx_l0_default_active_minus1;
174 	unsigned int num_ref_idx_l1_default_active_minus1;
175 	unsigned int weighted_pred_flag;
176 	unsigned int weighted_bipred_idc;
177 	int pic_init_qp_minus26;
178 	int pic_init_qs_minus26;
179 	int chroma_qp_index_offset;
180 	unsigned int deblocking_filter_control_present_flag;
181 	unsigned int constrained_intra_pred_flag;
182 	unsigned int redundant_pic_cnt_present_flag;
183 	struct {
184 		unsigned int transform_8x8_mode_flag;
185 		unsigned int pic_scaling_matrix_present_flag;
186 		int second_chroma_qp_index_offset;
187 	};
188 };
189 
190 int nal_h264_profile_from_v4l2(enum v4l2_mpeg_video_h264_profile profile);
191 int nal_h264_level_from_v4l2(enum v4l2_mpeg_video_h264_level level);
192 
193 ssize_t nal_h264_write_sps(const struct device *dev,
194 			   void *dest, size_t n, struct nal_h264_sps *sps);
195 ssize_t nal_h264_read_sps(const struct device *dev,
196 			  struct nal_h264_sps *sps, void *src, size_t n);
197 void nal_h264_print_sps(const struct device *dev, struct nal_h264_sps *sps);
198 
199 ssize_t nal_h264_write_pps(const struct device *dev,
200 			   void *dest, size_t n, struct nal_h264_pps *pps);
201 ssize_t nal_h264_read_pps(const struct device *dev,
202 			  struct nal_h264_pps *pps, void *src, size_t n);
203 void nal_h264_print_pps(const struct device *dev, struct nal_h264_pps *pps);
204 
205 ssize_t nal_h264_write_filler(const struct device *dev, void *dest, size_t n);
206 ssize_t nal_h264_read_filler(const struct device *dev, void *src, size_t n);
207 
208 #endif /* __NAL_H264_H__ */
209