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/errno.h>
12#include <linux/kernel.h>
13#include <linux/types.h>
14#include <linux/v4l2-controls.h>
15#include <linux/videodev2.h>
16
17/*
18 * struct nal_h264_hrd_parameters - HRD parameters
19 *
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.
22 */
23struct nal_h264_hrd_parameters {
24 unsigned int cpb_cnt_minus1;
25 unsigned int bit_rate_scale;
26 unsigned int cpb_size_scale;
27 struct {
28 int bit_rate_value_minus1[16];
29 int cpb_size_value_minus1[16];
30 unsigned int cbr_flag[16];
31 };
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;
36};
37
38/*
39 * struct nal_h264_vui_parameters - VUI parameters
40 *
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.
43 */
44struct nal_h264_vui_parameters {
45 unsigned int aspect_ratio_info_present_flag;
46 struct {
47 unsigned int aspect_ratio_idc;
48 unsigned int sar_width;
49 unsigned int sar_height;
50 };
51 unsigned int overscan_info_present_flag;
52 unsigned int overscan_appropriate_flag;
53 unsigned int video_signal_type_present_flag;
54 struct {
55 unsigned int video_format;
56 unsigned int video_full_range_flag;
57 unsigned int colour_description_present_flag;
58 struct {
59 unsigned int colour_primaries;
60 unsigned int transfer_characteristics;
61 unsigned int matrix_coefficients;
62 };
63 };
64 unsigned int chroma_loc_info_present_flag;
65 struct {
66 unsigned int chroma_sample_loc_type_top_field;
67 unsigned int chroma_sample_loc_type_bottom_field;
68 };
69 unsigned int timing_info_present_flag;
70 struct {
71 unsigned int num_units_in_tick;
72 unsigned int time_scale;
73 unsigned int fixed_frame_rate_flag;
74 };
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;
82 struct {
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;
90 };
91};
92
93/*
94 * struct nal_h264_sps - Sequence parameter set
95 *
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.
98 */
99struct nal_h264_sps {
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;
110 struct {
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;
117 };
118 unsigned int log2_max_frame_num_minus4;
119 unsigned int pic_order_cnt_type;
120 union {
121 unsigned int log2_max_pic_order_cnt_lsb_minus4;
122 struct {
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];
128 };
129 };
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;
138 struct {
139 unsigned int crop_left;
140 unsigned int crop_right;
141 unsigned int crop_top;
142 unsigned int crop_bottom;
143 };
144 unsigned int vui_parameters_present_flag;
145 struct nal_h264_vui_parameters vui;
146};
147
148/*
149 * struct nal_h264_pps - Picture parameter set
150 *
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.
153 */
154struct 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;
161 union {
162 unsigned int run_length_minus1[8];
163 struct {
164 unsigned int top_left[8];
165 unsigned int bottom_right[8];
166 };
167 struct {
168 unsigned int slice_group_change_direction_flag;
169 unsigned int slice_group_change_rate_minus1;
170 };
171 struct {
172 unsigned int pic_size_in_map_units_minus1;
173 unsigned int slice_group_id[8];
174 };
175 };
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;
186 struct {
187 unsigned int transform_8x8_mode_flag;
188 unsigned int pic_scaling_matrix_present_flag;
189 int second_chroma_qp_index_offset;
190 };
191};
192
193/**
194 * nal_h264_profile() - Get profile_idc for v4l2 h264 profile
195 * @profile: the profile as &enum v4l2_mpeg_video_h264_profile
196 *
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.
199 *
200 * Return: the profile_idc for the passed level
201 */
202static inline int nal_h264_profile(enum v4l2_mpeg_video_h264_profile profile)
203{
204 switch (profile) {
205 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
206 return 66;
207 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
208 return 77;
209 case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED:
210 return 88;
211 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
212 return 100;
213 default:
214 return -EINVAL;
215 }
216}
217
218/**
219 * nal_h264_level() - Get level_idc for v4l2 h264 level
220 * @level: the level as &enum v4l2_mpeg_video_h264_level
221 *
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.
224 *
225 * Return: the level_idc for the passed level
226 */
227static inline int nal_h264_level(enum v4l2_mpeg_video_h264_level level)
228{
229 switch (level) {
230 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
231 return 10;
232 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
233 return 9;
234 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
235 return 11;
236 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
237 return 12;
238 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
239 return 13;
240 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
241 return 20;
242 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
243 return 21;
244 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
245 return 22;
246 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
247 return 30;
248 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
249 return 31;
250 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
251 return 32;
252 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
253 return 40;
254 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
255 return 41;
256 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
257 return 42;
258 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
259 return 50;
260 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
261 return 51;
262 default:
263 return -EINVAL;
264 }
265}
266
267/**
268 * nal_h264_full_range() - Get video_full_range_flag for v4l2 quantization
269 * @quantization: the quantization type as &enum v4l2_quantization
270 *
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.
273 *
274 * Return: the video_full_range_flag value for the passed quantization
275 */
276static inline int nal_h264_full_range(enum v4l2_quantization quantization)
277{
278 switch (quantization) {
279 case V4L2_QUANTIZATION_FULL_RANGE:
280 return 1;
281 case V4L2_QUANTIZATION_LIM_RANGE:
282 return 0;
283 default:
284 break;
285 }
286
287 return 0;
288}
289
290/**
291 * nal_h264_color_primaries() - Get color_primaries for v4l2 colorspace
292 * @colorspace: the color space as &enum v4l2_colorspace
293 *
294 * Convert the &enum v4l2_colorspace to color_primaries as specified in
295 * Rec. ITU-T H.264 (04/2017) E.2.1.
296 *
297 * Return: the color_primaries value for the passed colorspace
298 */
299static inline int nal_h264_color_primaries(enum v4l2_colorspace colorspace)
300{
301 switch (colorspace) {
302 case V4L2_COLORSPACE_SMPTE170M:
303 return 6;
304 case V4L2_COLORSPACE_SMPTE240M:
305 return 7;
306 case V4L2_COLORSPACE_REC709:
307 return 1;
308 case V4L2_COLORSPACE_470_SYSTEM_M:
309 return 4;
310 case V4L2_COLORSPACE_JPEG:
311 case V4L2_COLORSPACE_SRGB:
312 case V4L2_COLORSPACE_470_SYSTEM_BG:
313 return 5;
314 case V4L2_COLORSPACE_BT2020:
315 return 9;
316 case V4L2_COLORSPACE_DEFAULT:
317 case V4L2_COLORSPACE_OPRGB:
318 case V4L2_COLORSPACE_RAW:
319 case V4L2_COLORSPACE_DCI_P3:
320 default:
321 return 2;
322 }
323}
324
325/**
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
329 *
330 * Convert the &enum v4l2_xfer_func to transfer_characteristics as specified in
331 * Rec. ITU-T H.264 (04/2017) E.2.1.
332 *
333 * Return: the transfer_characteristics value for the passed transfer function
334 */
335static inline int nal_h264_transfer_characteristics(enum v4l2_colorspace colorspace,
336 enum v4l2_xfer_func xfer_func)
337{
338 if (xfer_func == V4L2_XFER_FUNC_DEFAULT)
339 xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(colorspace);
340
341 switch (xfer_func) {
342 case V4L2_XFER_FUNC_709:
343 return 6;
344 case V4L2_XFER_FUNC_SMPTE2084:
345 return 16;
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:
351 default:
352 return 2;
353 }
354}
355
356/**
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
360 *
361 * Convert the &enum v4l2_ycbcr_encoding to matrix_coefficients as specified in
362 * Rec. ITU-T H.264 (04/2017) E.2.1.
363 *
364 * Return: the matrix_coefficients value for the passed encoding
365 */
366static inline int nal_h264_matrix_coeffs(enum v4l2_colorspace colorspace,
367 enum v4l2_ycbcr_encoding ycbcr_encoding)
368{
369 if (ycbcr_encoding == V4L2_YCBCR_ENC_DEFAULT)
370 ycbcr_encoding = V4L2_MAP_YCBCR_ENC_DEFAULT(colorspace);
371
372 switch (ycbcr_encoding) {
373 case V4L2_YCBCR_ENC_601:
374 case V4L2_YCBCR_ENC_XV601:
375 return 5;
376 case V4L2_YCBCR_ENC_709:
377 case V4L2_YCBCR_ENC_XV709:
378 return 1;
379 case V4L2_YCBCR_ENC_BT2020:
380 return 9;
381 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
382 return 10;
383 case V4L2_YCBCR_ENC_SMPTE240M:
384 default:
385 return 2;
386 }
387}
388
389ssize_t nal_h264_write_sps(const struct device *dev,
390 void *dest, size_t n, struct nal_h264_sps *sps);
391ssize_t nal_h264_read_sps(const struct device *dev,
392 struct nal_h264_sps *sps, void *src, size_t n);
393void nal_h264_print_sps(const struct device *dev, struct nal_h264_sps *sps);
394
395ssize_t nal_h264_write_pps(const struct device *dev,
396 void *dest, size_t n, struct nal_h264_pps *pps);
397ssize_t nal_h264_read_pps(const struct device *dev,
398 struct nal_h264_pps *pps, void *src, size_t n);
399void nal_h264_print_pps(const struct device *dev, struct nal_h264_pps *pps);
400
401ssize_t nal_h264_write_filler(const struct device *dev, void *dest, size_t n);
402ssize_t nal_h264_read_filler(const struct device *dev, void *src, size_t n);
403
404#endif /* __NAL_H264_H__ */
405

source code of linux/drivers/media/platform/allegro-dvt/nal-h264.h