nal-h264.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2019 Pengutronix, Michael Tretter <[email protected]>
  4. *
  5. * Convert NAL units between raw byte sequence payloads (RBSP) and C structs.
  6. */
  7. #ifndef __NAL_H264_H__
  8. #define __NAL_H264_H__
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/v4l2-controls.h>
  13. #include <linux/videodev2.h>
  14. /*
  15. * struct nal_h264_hrd_parameters - HRD 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. * struct nal_h264_vui_parameters - VUI parameters
  36. *
  37. * C struct representation of the VUI parameters as defined by Rec. ITU-T
  38. * H.264 (04/2017) E.1.1 VUI parameters syntax.
  39. */
  40. struct nal_h264_vui_parameters {
  41. unsigned int aspect_ratio_info_present_flag;
  42. struct {
  43. unsigned int aspect_ratio_idc;
  44. unsigned int sar_width;
  45. unsigned int sar_height;
  46. };
  47. unsigned int overscan_info_present_flag;
  48. unsigned int overscan_appropriate_flag;
  49. unsigned int video_signal_type_present_flag;
  50. struct {
  51. unsigned int video_format;
  52. unsigned int video_full_range_flag;
  53. unsigned int colour_description_present_flag;
  54. struct {
  55. unsigned int colour_primaries;
  56. unsigned int transfer_characteristics;
  57. unsigned int matrix_coefficients;
  58. };
  59. };
  60. unsigned int chroma_loc_info_present_flag;
  61. struct {
  62. unsigned int chroma_sample_loc_type_top_field;
  63. unsigned int chroma_sample_loc_type_bottom_field;
  64. };
  65. unsigned int timing_info_present_flag;
  66. struct {
  67. unsigned int num_units_in_tick;
  68. unsigned int time_scale;
  69. unsigned int fixed_frame_rate_flag;
  70. };
  71. unsigned int nal_hrd_parameters_present_flag;
  72. struct nal_h264_hrd_parameters nal_hrd_parameters;
  73. unsigned int vcl_hrd_parameters_present_flag;
  74. struct nal_h264_hrd_parameters vcl_hrd_parameters;
  75. unsigned int low_delay_hrd_flag;
  76. unsigned int pic_struct_present_flag;
  77. unsigned int bitstream_restriction_flag;
  78. struct {
  79. unsigned int motion_vectors_over_pic_boundaries_flag;
  80. unsigned int max_bytes_per_pic_denom;
  81. unsigned int max_bits_per_mb_denom;
  82. unsigned int log2_max_mv_length_horizontal;
  83. unsigned int log21_max_mv_length_vertical;
  84. unsigned int max_num_reorder_frames;
  85. unsigned int max_dec_frame_buffering;
  86. };
  87. };
  88. /*
  89. * struct nal_h264_sps - Sequence parameter set
  90. *
  91. * C struct representation of the sequence parameter set NAL unit as defined by
  92. * Rec. ITU-T H.264 (04/2017) 7.3.2.1.1 Sequence parameter set data syntax.
  93. */
  94. struct nal_h264_sps {
  95. unsigned int profile_idc;
  96. unsigned int constraint_set0_flag;
  97. unsigned int constraint_set1_flag;
  98. unsigned int constraint_set2_flag;
  99. unsigned int constraint_set3_flag;
  100. unsigned int constraint_set4_flag;
  101. unsigned int constraint_set5_flag;
  102. unsigned int reserved_zero_2bits;
  103. unsigned int level_idc;
  104. unsigned int seq_parameter_set_id;
  105. struct {
  106. unsigned int chroma_format_idc;
  107. unsigned int separate_colour_plane_flag;
  108. unsigned int bit_depth_luma_minus8;
  109. unsigned int bit_depth_chroma_minus8;
  110. unsigned int qpprime_y_zero_transform_bypass_flag;
  111. unsigned int seq_scaling_matrix_present_flag;
  112. };
  113. unsigned int log2_max_frame_num_minus4;
  114. unsigned int pic_order_cnt_type;
  115. union {
  116. unsigned int log2_max_pic_order_cnt_lsb_minus4;
  117. struct {
  118. unsigned int delta_pic_order_always_zero_flag;
  119. int offset_for_non_ref_pic;
  120. int offset_for_top_to_bottom_field;
  121. unsigned int num_ref_frames_in_pic_order_cnt_cycle;
  122. int offset_for_ref_frame[255];
  123. };
  124. };
  125. unsigned int max_num_ref_frames;
  126. unsigned int gaps_in_frame_num_value_allowed_flag;
  127. unsigned int pic_width_in_mbs_minus1;
  128. unsigned int pic_height_in_map_units_minus1;
  129. unsigned int frame_mbs_only_flag;
  130. unsigned int mb_adaptive_frame_field_flag;
  131. unsigned int direct_8x8_inference_flag;
  132. unsigned int frame_cropping_flag;
  133. struct {
  134. unsigned int crop_left;
  135. unsigned int crop_right;
  136. unsigned int crop_top;
  137. unsigned int crop_bottom;
  138. };
  139. unsigned int vui_parameters_present_flag;
  140. struct nal_h264_vui_parameters vui;
  141. };
  142. /*
  143. * struct nal_h264_pps - Picture parameter set
  144. *
  145. * C struct representation of the picture parameter set NAL unit as defined by
  146. * Rec. ITU-T H.264 (04/2017) 7.3.2.2 Picture parameter set RBSP syntax.
  147. */
  148. struct nal_h264_pps {
  149. unsigned int pic_parameter_set_id;
  150. unsigned int seq_parameter_set_id;
  151. unsigned int entropy_coding_mode_flag;
  152. unsigned int bottom_field_pic_order_in_frame_present_flag;
  153. unsigned int num_slice_groups_minus1;
  154. unsigned int slice_group_map_type;
  155. union {
  156. unsigned int run_length_minus1[8];
  157. struct {
  158. unsigned int top_left[8];
  159. unsigned int bottom_right[8];
  160. };
  161. struct {
  162. unsigned int slice_group_change_direction_flag;
  163. unsigned int slice_group_change_rate_minus1;
  164. };
  165. struct {
  166. unsigned int pic_size_in_map_units_minus1;
  167. unsigned int slice_group_id[8];
  168. };
  169. };
  170. unsigned int num_ref_idx_l0_default_active_minus1;
  171. unsigned int num_ref_idx_l1_default_active_minus1;
  172. unsigned int weighted_pred_flag;
  173. unsigned int weighted_bipred_idc;
  174. int pic_init_qp_minus26;
  175. int pic_init_qs_minus26;
  176. int chroma_qp_index_offset;
  177. unsigned int deblocking_filter_control_present_flag;
  178. unsigned int constrained_intra_pred_flag;
  179. unsigned int redundant_pic_cnt_present_flag;
  180. struct {
  181. unsigned int transform_8x8_mode_flag;
  182. unsigned int pic_scaling_matrix_present_flag;
  183. int second_chroma_qp_index_offset;
  184. };
  185. };
  186. /**
  187. * nal_h264_profile() - Get profile_idc for v4l2 h264 profile
  188. * @profile: the profile as &enum v4l2_mpeg_video_h264_profile
  189. *
  190. * Convert the &enum v4l2_mpeg_video_h264_profile to profile_idc as specified
  191. * in Rec. ITU-T H.264 (04/2017) A.2.
  192. *
  193. * Return: the profile_idc for the passed level
  194. */
  195. static inline int nal_h264_profile(enum v4l2_mpeg_video_h264_profile profile)
  196. {
  197. switch (profile) {
  198. case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  199. return 66;
  200. case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  201. return 77;
  202. case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED:
  203. return 88;
  204. case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  205. return 100;
  206. default:
  207. return -EINVAL;
  208. }
  209. }
  210. /**
  211. * nal_h264_level() - Get level_idc for v4l2 h264 level
  212. * @level: the level as &enum v4l2_mpeg_video_h264_level
  213. *
  214. * Convert the &enum v4l2_mpeg_video_h264_level to level_idc as specified in
  215. * Rec. ITU-T H.264 (04/2017) A.3.2.
  216. *
  217. * Return: the level_idc for the passed level
  218. */
  219. static inline int nal_h264_level(enum v4l2_mpeg_video_h264_level level)
  220. {
  221. switch (level) {
  222. case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  223. return 10;
  224. case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  225. return 9;
  226. case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  227. return 11;
  228. case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  229. return 12;
  230. case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  231. return 13;
  232. case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  233. return 20;
  234. case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  235. return 21;
  236. case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  237. return 22;
  238. case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  239. return 30;
  240. case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  241. return 31;
  242. case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  243. return 32;
  244. case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  245. return 40;
  246. case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
  247. return 41;
  248. case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
  249. return 42;
  250. case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
  251. return 50;
  252. case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
  253. return 51;
  254. default:
  255. return -EINVAL;
  256. }
  257. }
  258. /**
  259. * nal_h264_full_range() - Get video_full_range_flag for v4l2 quantization
  260. * @quantization: the quantization type as &enum v4l2_quantization
  261. *
  262. * Convert the &enum v4l2_quantization to video_full_range_flag as specified in
  263. * Rec. ITU-T H.264 (04/2017) E.2.1.
  264. *
  265. * Return: the video_full_range_flag value for the passed quantization
  266. */
  267. static inline int nal_h264_full_range(enum v4l2_quantization quantization)
  268. {
  269. switch (quantization) {
  270. case V4L2_QUANTIZATION_FULL_RANGE:
  271. return 1;
  272. case V4L2_QUANTIZATION_LIM_RANGE:
  273. return 0;
  274. default:
  275. break;
  276. }
  277. return 0;
  278. }
  279. /**
  280. * nal_h264_color_primaries() - Get color_primaries for v4l2 colorspace
  281. * @colorspace: the color space as &enum v4l2_colorspace
  282. *
  283. * Convert the &enum v4l2_colorspace to color_primaries as specified in
  284. * Rec. ITU-T H.264 (04/2017) E.2.1.
  285. *
  286. * Return: the color_primaries value for the passed colorspace
  287. */
  288. static inline int nal_h264_color_primaries(enum v4l2_colorspace colorspace)
  289. {
  290. switch (colorspace) {
  291. case V4L2_COLORSPACE_SMPTE170M:
  292. return 6;
  293. case V4L2_COLORSPACE_SMPTE240M:
  294. return 7;
  295. case V4L2_COLORSPACE_REC709:
  296. return 1;
  297. case V4L2_COLORSPACE_470_SYSTEM_M:
  298. return 4;
  299. case V4L2_COLORSPACE_JPEG:
  300. case V4L2_COLORSPACE_SRGB:
  301. case V4L2_COLORSPACE_470_SYSTEM_BG:
  302. return 5;
  303. case V4L2_COLORSPACE_BT2020:
  304. return 9;
  305. case V4L2_COLORSPACE_DEFAULT:
  306. case V4L2_COLORSPACE_OPRGB:
  307. case V4L2_COLORSPACE_RAW:
  308. case V4L2_COLORSPACE_DCI_P3:
  309. default:
  310. return 2;
  311. }
  312. }
  313. /**
  314. * nal_h264_transfer_characteristics() - Get transfer_characteristics for v4l2 xfer_func
  315. * @colorspace: the color space as &enum v4l2_colorspace
  316. * @xfer_func: the transfer function as &enum v4l2_xfer_func
  317. *
  318. * Convert the &enum v4l2_xfer_func to transfer_characteristics as specified in
  319. * Rec. ITU-T H.264 (04/2017) E.2.1.
  320. *
  321. * Return: the transfer_characteristics value for the passed transfer function
  322. */
  323. static inline int nal_h264_transfer_characteristics(enum v4l2_colorspace colorspace,
  324. enum v4l2_xfer_func xfer_func)
  325. {
  326. if (xfer_func == V4L2_XFER_FUNC_DEFAULT)
  327. xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(colorspace);
  328. switch (xfer_func) {
  329. case V4L2_XFER_FUNC_709:
  330. return 6;
  331. case V4L2_XFER_FUNC_SMPTE2084:
  332. return 16;
  333. case V4L2_XFER_FUNC_SRGB:
  334. case V4L2_XFER_FUNC_OPRGB:
  335. case V4L2_XFER_FUNC_NONE:
  336. case V4L2_XFER_FUNC_DCI_P3:
  337. case V4L2_XFER_FUNC_SMPTE240M:
  338. default:
  339. return 2;
  340. }
  341. }
  342. /**
  343. * nal_h264_matrix_coeffs() - Get matrix_coefficients for v4l2 v4l2_ycbcr_encoding
  344. * @colorspace: the color space as &enum v4l2_colorspace
  345. * @ycbcr_encoding: the ycbcr encoding as &enum v4l2_ycbcr_encoding
  346. *
  347. * Convert the &enum v4l2_ycbcr_encoding to matrix_coefficients as specified in
  348. * Rec. ITU-T H.264 (04/2017) E.2.1.
  349. *
  350. * Return: the matrix_coefficients value for the passed encoding
  351. */
  352. static inline int nal_h264_matrix_coeffs(enum v4l2_colorspace colorspace,
  353. enum v4l2_ycbcr_encoding ycbcr_encoding)
  354. {
  355. if (ycbcr_encoding == V4L2_YCBCR_ENC_DEFAULT)
  356. ycbcr_encoding = V4L2_MAP_YCBCR_ENC_DEFAULT(colorspace);
  357. switch (ycbcr_encoding) {
  358. case V4L2_YCBCR_ENC_601:
  359. case V4L2_YCBCR_ENC_XV601:
  360. return 5;
  361. case V4L2_YCBCR_ENC_709:
  362. case V4L2_YCBCR_ENC_XV709:
  363. return 1;
  364. case V4L2_YCBCR_ENC_BT2020:
  365. return 9;
  366. case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
  367. return 10;
  368. case V4L2_YCBCR_ENC_SMPTE240M:
  369. default:
  370. return 2;
  371. }
  372. }
  373. ssize_t nal_h264_write_sps(const struct device *dev,
  374. void *dest, size_t n, struct nal_h264_sps *sps);
  375. ssize_t nal_h264_read_sps(const struct device *dev,
  376. struct nal_h264_sps *sps, void *src, size_t n);
  377. void nal_h264_print_sps(const struct device *dev, struct nal_h264_sps *sps);
  378. ssize_t nal_h264_write_pps(const struct device *dev,
  379. void *dest, size_t n, struct nal_h264_pps *pps);
  380. ssize_t nal_h264_read_pps(const struct device *dev,
  381. struct nal_h264_pps *pps, void *src, size_t n);
  382. void nal_h264_print_pps(const struct device *dev, struct nal_h264_pps *pps);
  383. ssize_t nal_h264_write_filler(const struct device *dev, void *dest, size_t n);
  384. ssize_t nal_h264_read_filler(const struct device *dev, void *src, size_t n);
  385. #endif /* __NAL_H264_H__ */