nal-hevc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019-2020 Pengutronix, Michael Tretter <[email protected]>
  4. *
  5. * Convert NAL units between raw byte sequence payloads (RBSP) and C structs.
  6. *
  7. * The conversion is defined in "ITU-T Rec. H.265 (02/2018) high efficiency
  8. * video coding". Decoder drivers may use the parser to parse RBSP from
  9. * encoded streams and configure the hardware, if the hardware is not able to
  10. * parse RBSP itself. Encoder drivers may use the generator to generate the
  11. * RBSP for VPS/SPS/PPS nal units and add them to the encoded stream if the
  12. * hardware does not generate the units.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/string.h>
  17. #include <linux/v4l2-controls.h>
  18. #include <linux/device.h>
  19. #include <linux/export.h>
  20. #include <linux/log2.h>
  21. #include "nal-hevc.h"
  22. #include "nal-rbsp.h"
  23. /*
  24. * See Rec. ITU-T H.265 (02/2018) Table 7-1 - NAL unit type codes and NAL unit
  25. * type classes
  26. */
  27. enum nal_unit_type {
  28. VPS_NUT = 32,
  29. SPS_NUT = 33,
  30. PPS_NUT = 34,
  31. FD_NUT = 38,
  32. };
  33. static void nal_hevc_write_start_code_prefix(struct rbsp *rbsp)
  34. {
  35. u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
  36. int i = 4;
  37. if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
  38. rbsp->error = -EINVAL;
  39. return;
  40. }
  41. p[0] = 0x00;
  42. p[1] = 0x00;
  43. p[2] = 0x00;
  44. p[3] = 0x01;
  45. rbsp->pos += i * 8;
  46. }
  47. static void nal_hevc_read_start_code_prefix(struct rbsp *rbsp)
  48. {
  49. u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
  50. int i = 4;
  51. if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
  52. rbsp->error = -EINVAL;
  53. return;
  54. }
  55. if (p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x00 || p[3] != 0x01) {
  56. rbsp->error = -EINVAL;
  57. return;
  58. }
  59. rbsp->pos += i * 8;
  60. }
  61. static void nal_hevc_write_filler_data(struct rbsp *rbsp)
  62. {
  63. u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
  64. int i;
  65. /* Keep 1 byte extra for terminating the NAL unit */
  66. i = rbsp->size - DIV_ROUND_UP(rbsp->pos, 8) - 1;
  67. memset(p, 0xff, i);
  68. rbsp->pos += i * 8;
  69. }
  70. static void nal_hevc_read_filler_data(struct rbsp *rbsp)
  71. {
  72. u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
  73. while (*p == 0xff) {
  74. if (DIV_ROUND_UP(rbsp->pos, 8) > rbsp->size) {
  75. rbsp->error = -EINVAL;
  76. return;
  77. }
  78. p++;
  79. rbsp->pos += 8;
  80. }
  81. }
  82. static void nal_hevc_rbsp_profile_tier_level(struct rbsp *rbsp,
  83. struct nal_hevc_profile_tier_level *ptl)
  84. {
  85. unsigned int i;
  86. unsigned int max_num_sub_layers_minus_1 = 0;
  87. rbsp_bits(rbsp, 2, &ptl->general_profile_space);
  88. rbsp_bit(rbsp, &ptl->general_tier_flag);
  89. rbsp_bits(rbsp, 5, &ptl->general_profile_idc);
  90. for (i = 0; i < 32; i++)
  91. rbsp_bit(rbsp, &ptl->general_profile_compatibility_flag[i]);
  92. rbsp_bit(rbsp, &ptl->general_progressive_source_flag);
  93. rbsp_bit(rbsp, &ptl->general_interlaced_source_flag);
  94. rbsp_bit(rbsp, &ptl->general_non_packed_constraint_flag);
  95. rbsp_bit(rbsp, &ptl->general_frame_only_constraint_flag);
  96. if (ptl->general_profile_idc == 4 ||
  97. ptl->general_profile_compatibility_flag[4] ||
  98. ptl->general_profile_idc == 5 ||
  99. ptl->general_profile_compatibility_flag[5] ||
  100. ptl->general_profile_idc == 6 ||
  101. ptl->general_profile_compatibility_flag[6] ||
  102. ptl->general_profile_idc == 7 ||
  103. ptl->general_profile_compatibility_flag[7] ||
  104. ptl->general_profile_idc == 8 ||
  105. ptl->general_profile_compatibility_flag[8] ||
  106. ptl->general_profile_idc == 9 ||
  107. ptl->general_profile_compatibility_flag[9] ||
  108. ptl->general_profile_idc == 10 ||
  109. ptl->general_profile_compatibility_flag[10]) {
  110. rbsp_bit(rbsp, &ptl->general_max_12bit_constraint_flag);
  111. rbsp_bit(rbsp, &ptl->general_max_10bit_constraint_flag);
  112. rbsp_bit(rbsp, &ptl->general_max_8bit_constraint_flag);
  113. rbsp_bit(rbsp, &ptl->general_max_422chroma_constraint_flag);
  114. rbsp_bit(rbsp, &ptl->general_max_420chroma_constraint_flag);
  115. rbsp_bit(rbsp, &ptl->general_max_monochrome_constraint_flag);
  116. rbsp_bit(rbsp, &ptl->general_intra_constraint_flag);
  117. rbsp_bit(rbsp, &ptl->general_one_picture_only_constraint_flag);
  118. rbsp_bit(rbsp, &ptl->general_lower_bit_rate_constraint_flag);
  119. if (ptl->general_profile_idc == 5 ||
  120. ptl->general_profile_compatibility_flag[5] ||
  121. ptl->general_profile_idc == 9 ||
  122. ptl->general_profile_compatibility_flag[9] ||
  123. ptl->general_profile_idc == 10 ||
  124. ptl->general_profile_compatibility_flag[10]) {
  125. rbsp_bit(rbsp, &ptl->general_max_14bit_constraint_flag);
  126. rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_33bits);
  127. rbsp_bits(rbsp, 33 - 32, &ptl->general_reserved_zero_33bits);
  128. } else {
  129. rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_34bits);
  130. rbsp_bits(rbsp, 34 - 2, &ptl->general_reserved_zero_34bits);
  131. }
  132. } else if (ptl->general_profile_idc == 2 ||
  133. ptl->general_profile_compatibility_flag[2]) {
  134. rbsp_bits(rbsp, 7, &ptl->general_reserved_zero_7bits);
  135. rbsp_bit(rbsp, &ptl->general_one_picture_only_constraint_flag);
  136. rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_35bits);
  137. rbsp_bits(rbsp, 35 - 32, &ptl->general_reserved_zero_35bits);
  138. } else {
  139. rbsp_bits(rbsp, 32, &ptl->general_reserved_zero_43bits);
  140. rbsp_bits(rbsp, 43 - 32, &ptl->general_reserved_zero_43bits);
  141. }
  142. if ((ptl->general_profile_idc >= 1 && ptl->general_profile_idc <= 5) ||
  143. ptl->general_profile_idc == 9 ||
  144. ptl->general_profile_compatibility_flag[1] ||
  145. ptl->general_profile_compatibility_flag[2] ||
  146. ptl->general_profile_compatibility_flag[3] ||
  147. ptl->general_profile_compatibility_flag[4] ||
  148. ptl->general_profile_compatibility_flag[5] ||
  149. ptl->general_profile_compatibility_flag[9])
  150. rbsp_bit(rbsp, &ptl->general_inbld_flag);
  151. else
  152. rbsp_bit(rbsp, &ptl->general_reserved_zero_bit);
  153. rbsp_bits(rbsp, 8, &ptl->general_level_idc);
  154. if (max_num_sub_layers_minus_1 > 0)
  155. rbsp_unsupported(rbsp);
  156. }
  157. static void nal_hevc_rbsp_vps(struct rbsp *rbsp, struct nal_hevc_vps *vps)
  158. {
  159. unsigned int i, j;
  160. unsigned int reserved_0xffff_16bits = 0xffff;
  161. rbsp_bits(rbsp, 4, &vps->video_parameter_set_id);
  162. rbsp_bit(rbsp, &vps->base_layer_internal_flag);
  163. rbsp_bit(rbsp, &vps->base_layer_available_flag);
  164. rbsp_bits(rbsp, 6, &vps->max_layers_minus1);
  165. rbsp_bits(rbsp, 3, &vps->max_sub_layers_minus1);
  166. rbsp_bits(rbsp, 1, &vps->temporal_id_nesting_flag);
  167. rbsp_bits(rbsp, 16, &reserved_0xffff_16bits);
  168. nal_hevc_rbsp_profile_tier_level(rbsp, &vps->profile_tier_level);
  169. rbsp_bit(rbsp, &vps->sub_layer_ordering_info_present_flag);
  170. for (i = vps->sub_layer_ordering_info_present_flag ? 0 : vps->max_sub_layers_minus1;
  171. i <= vps->max_sub_layers_minus1; i++) {
  172. rbsp_uev(rbsp, &vps->max_dec_pic_buffering_minus1[i]);
  173. rbsp_uev(rbsp, &vps->max_num_reorder_pics[i]);
  174. rbsp_uev(rbsp, &vps->max_latency_increase_plus1[i]);
  175. }
  176. rbsp_bits(rbsp, 6, &vps->max_layer_id);
  177. rbsp_uev(rbsp, &vps->num_layer_sets_minus1);
  178. for (i = 0; i <= vps->num_layer_sets_minus1; i++)
  179. for (j = 0; j <= vps->max_layer_id; j++)
  180. rbsp_bit(rbsp, &vps->layer_id_included_flag[i][j]);
  181. rbsp_bit(rbsp, &vps->timing_info_present_flag);
  182. if (vps->timing_info_present_flag)
  183. rbsp_unsupported(rbsp);
  184. rbsp_bit(rbsp, &vps->extension_flag);
  185. if (vps->extension_flag)
  186. rbsp_unsupported(rbsp);
  187. }
  188. static void nal_hevc_rbsp_sub_layer_hrd_parameters(struct rbsp *rbsp,
  189. struct nal_hevc_sub_layer_hrd_parameters *hrd)
  190. {
  191. unsigned int i;
  192. unsigned int cpb_cnt = 1;
  193. for (i = 0; i < cpb_cnt; i++) {
  194. rbsp_uev(rbsp, &hrd->bit_rate_value_minus1[i]);
  195. rbsp_uev(rbsp, &hrd->cpb_size_value_minus1[i]);
  196. rbsp_bit(rbsp, &hrd->cbr_flag[i]);
  197. }
  198. }
  199. static void nal_hevc_rbsp_hrd_parameters(struct rbsp *rbsp,
  200. struct nal_hevc_hrd_parameters *hrd)
  201. {
  202. unsigned int i;
  203. unsigned int max_num_sub_layers_minus_1 = 0;
  204. rbsp_bit(rbsp, &hrd->nal_hrd_parameters_present_flag);
  205. rbsp_bit(rbsp, &hrd->vcl_hrd_parameters_present_flag);
  206. if (hrd->nal_hrd_parameters_present_flag || hrd->vcl_hrd_parameters_present_flag) {
  207. rbsp_bit(rbsp, &hrd->sub_pic_hrd_params_present_flag);
  208. if (hrd->sub_pic_hrd_params_present_flag) {
  209. rbsp_bits(rbsp, 8, &hrd->tick_divisor_minus2);
  210. rbsp_bits(rbsp, 5, &hrd->du_cpb_removal_delay_increment_length_minus1);
  211. rbsp_bit(rbsp, &hrd->sub_pic_cpb_params_in_pic_timing_sei_flag);
  212. rbsp_bits(rbsp, 5, &hrd->dpb_output_delay_du_length_minus1);
  213. }
  214. rbsp_bits(rbsp, 4, &hrd->bit_rate_scale);
  215. rbsp_bits(rbsp, 4, &hrd->cpb_size_scale);
  216. if (hrd->sub_pic_hrd_params_present_flag)
  217. rbsp_bits(rbsp, 4, &hrd->cpb_size_du_scale);
  218. rbsp_bits(rbsp, 5, &hrd->initial_cpb_removal_delay_length_minus1);
  219. rbsp_bits(rbsp, 5, &hrd->au_cpb_removal_delay_length_minus1);
  220. rbsp_bits(rbsp, 5, &hrd->dpb_output_delay_length_minus1);
  221. }
  222. for (i = 0; i <= max_num_sub_layers_minus_1; i++) {
  223. rbsp_bit(rbsp, &hrd->fixed_pic_rate_general_flag[i]);
  224. if (!hrd->fixed_pic_rate_general_flag[i])
  225. rbsp_bit(rbsp, &hrd->fixed_pic_rate_within_cvs_flag[i]);
  226. if (hrd->fixed_pic_rate_within_cvs_flag[i])
  227. rbsp_uev(rbsp, &hrd->elemental_duration_in_tc_minus1[i]);
  228. else
  229. rbsp_bit(rbsp, &hrd->low_delay_hrd_flag[i]);
  230. if (!hrd->low_delay_hrd_flag[i])
  231. rbsp_uev(rbsp, &hrd->cpb_cnt_minus1[i]);
  232. if (hrd->nal_hrd_parameters_present_flag)
  233. nal_hevc_rbsp_sub_layer_hrd_parameters(rbsp, &hrd->vcl_hrd[i]);
  234. if (hrd->vcl_hrd_parameters_present_flag)
  235. nal_hevc_rbsp_sub_layer_hrd_parameters(rbsp, &hrd->vcl_hrd[i]);
  236. }
  237. }
  238. static void nal_hevc_rbsp_vui_parameters(struct rbsp *rbsp,
  239. struct nal_hevc_vui_parameters *vui)
  240. {
  241. if (!vui) {
  242. rbsp->error = -EINVAL;
  243. return;
  244. }
  245. rbsp_bit(rbsp, &vui->aspect_ratio_info_present_flag);
  246. if (vui->aspect_ratio_info_present_flag) {
  247. rbsp_bits(rbsp, 8, &vui->aspect_ratio_idc);
  248. if (vui->aspect_ratio_idc == 255) {
  249. rbsp_bits(rbsp, 16, &vui->sar_width);
  250. rbsp_bits(rbsp, 16, &vui->sar_height);
  251. }
  252. }
  253. rbsp_bit(rbsp, &vui->overscan_info_present_flag);
  254. if (vui->overscan_info_present_flag)
  255. rbsp_bit(rbsp, &vui->overscan_appropriate_flag);
  256. rbsp_bit(rbsp, &vui->video_signal_type_present_flag);
  257. if (vui->video_signal_type_present_flag) {
  258. rbsp_bits(rbsp, 3, &vui->video_format);
  259. rbsp_bit(rbsp, &vui->video_full_range_flag);
  260. rbsp_bit(rbsp, &vui->colour_description_present_flag);
  261. if (vui->colour_description_present_flag) {
  262. rbsp_bits(rbsp, 8, &vui->colour_primaries);
  263. rbsp_bits(rbsp, 8, &vui->transfer_characteristics);
  264. rbsp_bits(rbsp, 8, &vui->matrix_coeffs);
  265. }
  266. }
  267. rbsp_bit(rbsp, &vui->chroma_loc_info_present_flag);
  268. if (vui->chroma_loc_info_present_flag) {
  269. rbsp_uev(rbsp, &vui->chroma_sample_loc_type_top_field);
  270. rbsp_uev(rbsp, &vui->chroma_sample_loc_type_bottom_field);
  271. }
  272. rbsp_bit(rbsp, &vui->neutral_chroma_indication_flag);
  273. rbsp_bit(rbsp, &vui->field_seq_flag);
  274. rbsp_bit(rbsp, &vui->frame_field_info_present_flag);
  275. rbsp_bit(rbsp, &vui->default_display_window_flag);
  276. if (vui->default_display_window_flag) {
  277. rbsp_uev(rbsp, &vui->def_disp_win_left_offset);
  278. rbsp_uev(rbsp, &vui->def_disp_win_right_offset);
  279. rbsp_uev(rbsp, &vui->def_disp_win_top_offset);
  280. rbsp_uev(rbsp, &vui->def_disp_win_bottom_offset);
  281. }
  282. rbsp_bit(rbsp, &vui->vui_timing_info_present_flag);
  283. if (vui->vui_timing_info_present_flag) {
  284. rbsp_bits(rbsp, 32, &vui->vui_num_units_in_tick);
  285. rbsp_bits(rbsp, 32, &vui->vui_time_scale);
  286. rbsp_bit(rbsp, &vui->vui_poc_proportional_to_timing_flag);
  287. if (vui->vui_poc_proportional_to_timing_flag)
  288. rbsp_uev(rbsp, &vui->vui_num_ticks_poc_diff_one_minus1);
  289. rbsp_bit(rbsp, &vui->vui_hrd_parameters_present_flag);
  290. if (vui->vui_hrd_parameters_present_flag)
  291. nal_hevc_rbsp_hrd_parameters(rbsp, &vui->nal_hrd_parameters);
  292. }
  293. rbsp_bit(rbsp, &vui->bitstream_restriction_flag);
  294. if (vui->bitstream_restriction_flag) {
  295. rbsp_bit(rbsp, &vui->tiles_fixed_structure_flag);
  296. rbsp_bit(rbsp, &vui->motion_vectors_over_pic_boundaries_flag);
  297. rbsp_bit(rbsp, &vui->restricted_ref_pic_lists_flag);
  298. rbsp_uev(rbsp, &vui->min_spatial_segmentation_idc);
  299. rbsp_uev(rbsp, &vui->max_bytes_per_pic_denom);
  300. rbsp_uev(rbsp, &vui->max_bits_per_min_cu_denom);
  301. rbsp_uev(rbsp, &vui->log2_max_mv_length_horizontal);
  302. rbsp_uev(rbsp, &vui->log2_max_mv_length_vertical);
  303. }
  304. }
  305. static void nal_hevc_rbsp_sps(struct rbsp *rbsp, struct nal_hevc_sps *sps)
  306. {
  307. unsigned int i;
  308. rbsp_bits(rbsp, 4, &sps->video_parameter_set_id);
  309. rbsp_bits(rbsp, 3, &sps->max_sub_layers_minus1);
  310. rbsp_bit(rbsp, &sps->temporal_id_nesting_flag);
  311. nal_hevc_rbsp_profile_tier_level(rbsp, &sps->profile_tier_level);
  312. rbsp_uev(rbsp, &sps->seq_parameter_set_id);
  313. rbsp_uev(rbsp, &sps->chroma_format_idc);
  314. if (sps->chroma_format_idc == 3)
  315. rbsp_bit(rbsp, &sps->separate_colour_plane_flag);
  316. rbsp_uev(rbsp, &sps->pic_width_in_luma_samples);
  317. rbsp_uev(rbsp, &sps->pic_height_in_luma_samples);
  318. rbsp_bit(rbsp, &sps->conformance_window_flag);
  319. if (sps->conformance_window_flag) {
  320. rbsp_uev(rbsp, &sps->conf_win_left_offset);
  321. rbsp_uev(rbsp, &sps->conf_win_right_offset);
  322. rbsp_uev(rbsp, &sps->conf_win_top_offset);
  323. rbsp_uev(rbsp, &sps->conf_win_bottom_offset);
  324. }
  325. rbsp_uev(rbsp, &sps->bit_depth_luma_minus8);
  326. rbsp_uev(rbsp, &sps->bit_depth_chroma_minus8);
  327. rbsp_uev(rbsp, &sps->log2_max_pic_order_cnt_lsb_minus4);
  328. rbsp_bit(rbsp, &sps->sub_layer_ordering_info_present_flag);
  329. for (i = (sps->sub_layer_ordering_info_present_flag ? 0 : sps->max_sub_layers_minus1);
  330. i <= sps->max_sub_layers_minus1; i++) {
  331. rbsp_uev(rbsp, &sps->max_dec_pic_buffering_minus1[i]);
  332. rbsp_uev(rbsp, &sps->max_num_reorder_pics[i]);
  333. rbsp_uev(rbsp, &sps->max_latency_increase_plus1[i]);
  334. }
  335. rbsp_uev(rbsp, &sps->log2_min_luma_coding_block_size_minus3);
  336. rbsp_uev(rbsp, &sps->log2_diff_max_min_luma_coding_block_size);
  337. rbsp_uev(rbsp, &sps->log2_min_luma_transform_block_size_minus2);
  338. rbsp_uev(rbsp, &sps->log2_diff_max_min_luma_transform_block_size);
  339. rbsp_uev(rbsp, &sps->max_transform_hierarchy_depth_inter);
  340. rbsp_uev(rbsp, &sps->max_transform_hierarchy_depth_intra);
  341. rbsp_bit(rbsp, &sps->scaling_list_enabled_flag);
  342. if (sps->scaling_list_enabled_flag)
  343. rbsp_unsupported(rbsp);
  344. rbsp_bit(rbsp, &sps->amp_enabled_flag);
  345. rbsp_bit(rbsp, &sps->sample_adaptive_offset_enabled_flag);
  346. rbsp_bit(rbsp, &sps->pcm_enabled_flag);
  347. if (sps->pcm_enabled_flag) {
  348. rbsp_bits(rbsp, 4, &sps->pcm_sample_bit_depth_luma_minus1);
  349. rbsp_bits(rbsp, 4, &sps->pcm_sample_bit_depth_chroma_minus1);
  350. rbsp_uev(rbsp, &sps->log2_min_pcm_luma_coding_block_size_minus3);
  351. rbsp_uev(rbsp, &sps->log2_diff_max_min_pcm_luma_coding_block_size);
  352. rbsp_bit(rbsp, &sps->pcm_loop_filter_disabled_flag);
  353. }
  354. rbsp_uev(rbsp, &sps->num_short_term_ref_pic_sets);
  355. if (sps->num_short_term_ref_pic_sets > 0)
  356. rbsp_unsupported(rbsp);
  357. rbsp_bit(rbsp, &sps->long_term_ref_pics_present_flag);
  358. if (sps->long_term_ref_pics_present_flag)
  359. rbsp_unsupported(rbsp);
  360. rbsp_bit(rbsp, &sps->sps_temporal_mvp_enabled_flag);
  361. rbsp_bit(rbsp, &sps->strong_intra_smoothing_enabled_flag);
  362. rbsp_bit(rbsp, &sps->vui_parameters_present_flag);
  363. if (sps->vui_parameters_present_flag)
  364. nal_hevc_rbsp_vui_parameters(rbsp, &sps->vui);
  365. rbsp_bit(rbsp, &sps->extension_present_flag);
  366. if (sps->extension_present_flag) {
  367. rbsp_bit(rbsp, &sps->sps_range_extension_flag);
  368. rbsp_bit(rbsp, &sps->sps_multilayer_extension_flag);
  369. rbsp_bit(rbsp, &sps->sps_3d_extension_flag);
  370. rbsp_bit(rbsp, &sps->sps_scc_extension_flag);
  371. rbsp_bits(rbsp, 5, &sps->sps_extension_4bits);
  372. }
  373. if (sps->sps_range_extension_flag)
  374. rbsp_unsupported(rbsp);
  375. if (sps->sps_multilayer_extension_flag)
  376. rbsp_unsupported(rbsp);
  377. if (sps->sps_3d_extension_flag)
  378. rbsp_unsupported(rbsp);
  379. if (sps->sps_scc_extension_flag)
  380. rbsp_unsupported(rbsp);
  381. if (sps->sps_extension_4bits)
  382. rbsp_unsupported(rbsp);
  383. }
  384. static void nal_hevc_rbsp_pps(struct rbsp *rbsp, struct nal_hevc_pps *pps)
  385. {
  386. unsigned int i;
  387. rbsp_uev(rbsp, &pps->pps_pic_parameter_set_id);
  388. rbsp_uev(rbsp, &pps->pps_seq_parameter_set_id);
  389. rbsp_bit(rbsp, &pps->dependent_slice_segments_enabled_flag);
  390. rbsp_bit(rbsp, &pps->output_flag_present_flag);
  391. rbsp_bits(rbsp, 3, &pps->num_extra_slice_header_bits);
  392. rbsp_bit(rbsp, &pps->sign_data_hiding_enabled_flag);
  393. rbsp_bit(rbsp, &pps->cabac_init_present_flag);
  394. rbsp_uev(rbsp, &pps->num_ref_idx_l0_default_active_minus1);
  395. rbsp_uev(rbsp, &pps->num_ref_idx_l1_default_active_minus1);
  396. rbsp_sev(rbsp, &pps->init_qp_minus26);
  397. rbsp_bit(rbsp, &pps->constrained_intra_pred_flag);
  398. rbsp_bit(rbsp, &pps->transform_skip_enabled_flag);
  399. rbsp_bit(rbsp, &pps->cu_qp_delta_enabled_flag);
  400. if (pps->cu_qp_delta_enabled_flag)
  401. rbsp_uev(rbsp, &pps->diff_cu_qp_delta_depth);
  402. rbsp_sev(rbsp, &pps->pps_cb_qp_offset);
  403. rbsp_sev(rbsp, &pps->pps_cr_qp_offset);
  404. rbsp_bit(rbsp, &pps->pps_slice_chroma_qp_offsets_present_flag);
  405. rbsp_bit(rbsp, &pps->weighted_pred_flag);
  406. rbsp_bit(rbsp, &pps->weighted_bipred_flag);
  407. rbsp_bit(rbsp, &pps->transquant_bypass_enabled_flag);
  408. rbsp_bit(rbsp, &pps->tiles_enabled_flag);
  409. rbsp_bit(rbsp, &pps->entropy_coding_sync_enabled_flag);
  410. if (pps->tiles_enabled_flag) {
  411. rbsp_uev(rbsp, &pps->num_tile_columns_minus1);
  412. rbsp_uev(rbsp, &pps->num_tile_rows_minus1);
  413. rbsp_bit(rbsp, &pps->uniform_spacing_flag);
  414. if (!pps->uniform_spacing_flag) {
  415. for (i = 0; i < pps->num_tile_columns_minus1; i++)
  416. rbsp_uev(rbsp, &pps->column_width_minus1[i]);
  417. for (i = 0; i < pps->num_tile_rows_minus1; i++)
  418. rbsp_uev(rbsp, &pps->row_height_minus1[i]);
  419. }
  420. rbsp_bit(rbsp, &pps->loop_filter_across_tiles_enabled_flag);
  421. }
  422. rbsp_bit(rbsp, &pps->pps_loop_filter_across_slices_enabled_flag);
  423. rbsp_bit(rbsp, &pps->deblocking_filter_control_present_flag);
  424. if (pps->deblocking_filter_control_present_flag) {
  425. rbsp_bit(rbsp, &pps->deblocking_filter_override_enabled_flag);
  426. rbsp_bit(rbsp, &pps->pps_deblocking_filter_disabled_flag);
  427. if (!pps->pps_deblocking_filter_disabled_flag) {
  428. rbsp_sev(rbsp, &pps->pps_beta_offset_div2);
  429. rbsp_sev(rbsp, &pps->pps_tc_offset_div2);
  430. }
  431. }
  432. rbsp_bit(rbsp, &pps->pps_scaling_list_data_present_flag);
  433. if (pps->pps_scaling_list_data_present_flag)
  434. rbsp_unsupported(rbsp);
  435. rbsp_bit(rbsp, &pps->lists_modification_present_flag);
  436. rbsp_uev(rbsp, &pps->log2_parallel_merge_level_minus2);
  437. rbsp_bit(rbsp, &pps->slice_segment_header_extension_present_flag);
  438. rbsp_bit(rbsp, &pps->pps_extension_present_flag);
  439. if (pps->pps_extension_present_flag) {
  440. rbsp_bit(rbsp, &pps->pps_range_extension_flag);
  441. rbsp_bit(rbsp, &pps->pps_multilayer_extension_flag);
  442. rbsp_bit(rbsp, &pps->pps_3d_extension_flag);
  443. rbsp_bit(rbsp, &pps->pps_scc_extension_flag);
  444. rbsp_bits(rbsp, 4, &pps->pps_extension_4bits);
  445. }
  446. if (pps->pps_range_extension_flag)
  447. rbsp_unsupported(rbsp);
  448. if (pps->pps_multilayer_extension_flag)
  449. rbsp_unsupported(rbsp);
  450. if (pps->pps_3d_extension_flag)
  451. rbsp_unsupported(rbsp);
  452. if (pps->pps_scc_extension_flag)
  453. rbsp_unsupported(rbsp);
  454. if (pps->pps_extension_4bits)
  455. rbsp_unsupported(rbsp);
  456. }
  457. /**
  458. * nal_hevc_write_vps() - Write PPS NAL unit into RBSP format
  459. * @dev: device pointer
  460. * @dest: the buffer that is filled with RBSP data
  461. * @n: maximum size of @dest in bytes
  462. * @vps: &struct nal_hevc_vps to convert to RBSP
  463. *
  464. * Convert @vps to RBSP data and write it into @dest.
  465. *
  466. * The size of the VPS NAL unit is not known in advance and this function will
  467. * fail, if @dest does not hold sufficient space for the VPS NAL unit.
  468. *
  469. * Return: number of bytes written to @dest or negative error code
  470. */
  471. ssize_t nal_hevc_write_vps(const struct device *dev,
  472. void *dest, size_t n, struct nal_hevc_vps *vps)
  473. {
  474. struct rbsp rbsp;
  475. unsigned int forbidden_zero_bit = 0;
  476. unsigned int nal_unit_type = VPS_NUT;
  477. unsigned int nuh_layer_id = 0;
  478. unsigned int nuh_temporal_id_plus1 = 1;
  479. if (!dest)
  480. return -EINVAL;
  481. rbsp_init(&rbsp, dest, n, &write);
  482. nal_hevc_write_start_code_prefix(&rbsp);
  483. /* NAL unit header */
  484. rbsp_bit(&rbsp, &forbidden_zero_bit);
  485. rbsp_bits(&rbsp, 6, &nal_unit_type);
  486. rbsp_bits(&rbsp, 6, &nuh_layer_id);
  487. rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
  488. nal_hevc_rbsp_vps(&rbsp, vps);
  489. rbsp_trailing_bits(&rbsp);
  490. if (rbsp.error)
  491. return rbsp.error;
  492. return DIV_ROUND_UP(rbsp.pos, 8);
  493. }
  494. EXPORT_SYMBOL_GPL(nal_hevc_write_vps);
  495. /**
  496. * nal_hevc_read_vps() - Read VPS NAL unit from RBSP format
  497. * @dev: device pointer
  498. * @vps: the &struct nal_hevc_vps to fill from the RBSP data
  499. * @src: the buffer that contains the RBSP data
  500. * @n: size of @src in bytes
  501. *
  502. * Read RBSP data from @src and use it to fill @vps.
  503. *
  504. * Return: number of bytes read from @src or negative error code
  505. */
  506. ssize_t nal_hevc_read_vps(const struct device *dev,
  507. struct nal_hevc_vps *vps, void *src, size_t n)
  508. {
  509. struct rbsp rbsp;
  510. unsigned int forbidden_zero_bit;
  511. unsigned int nal_unit_type;
  512. unsigned int nuh_layer_id;
  513. unsigned int nuh_temporal_id_plus1;
  514. if (!src)
  515. return -EINVAL;
  516. rbsp_init(&rbsp, src, n, &read);
  517. nal_hevc_read_start_code_prefix(&rbsp);
  518. rbsp_bit(&rbsp, &forbidden_zero_bit);
  519. rbsp_bits(&rbsp, 6, &nal_unit_type);
  520. rbsp_bits(&rbsp, 6, &nuh_layer_id);
  521. rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
  522. if (rbsp.error ||
  523. forbidden_zero_bit != 0 ||
  524. nal_unit_type != VPS_NUT)
  525. return -EINVAL;
  526. nal_hevc_rbsp_vps(&rbsp, vps);
  527. rbsp_trailing_bits(&rbsp);
  528. if (rbsp.error)
  529. return rbsp.error;
  530. return DIV_ROUND_UP(rbsp.pos, 8);
  531. }
  532. EXPORT_SYMBOL_GPL(nal_hevc_read_vps);
  533. /**
  534. * nal_hevc_write_sps() - Write SPS NAL unit into RBSP format
  535. * @dev: device pointer
  536. * @dest: the buffer that is filled with RBSP data
  537. * @n: maximum size of @dest in bytes
  538. * @sps: &struct nal_hevc_sps to convert to RBSP
  539. *
  540. * Convert @sps to RBSP data and write it into @dest.
  541. *
  542. * The size of the SPS NAL unit is not known in advance and this function will
  543. * fail, if @dest does not hold sufficient space for the SPS NAL unit.
  544. *
  545. * Return: number of bytes written to @dest or negative error code
  546. */
  547. ssize_t nal_hevc_write_sps(const struct device *dev,
  548. void *dest, size_t n, struct nal_hevc_sps *sps)
  549. {
  550. struct rbsp rbsp;
  551. unsigned int forbidden_zero_bit = 0;
  552. unsigned int nal_unit_type = SPS_NUT;
  553. unsigned int nuh_layer_id = 0;
  554. unsigned int nuh_temporal_id_plus1 = 1;
  555. if (!dest)
  556. return -EINVAL;
  557. rbsp_init(&rbsp, dest, n, &write);
  558. nal_hevc_write_start_code_prefix(&rbsp);
  559. /* NAL unit header */
  560. rbsp_bit(&rbsp, &forbidden_zero_bit);
  561. rbsp_bits(&rbsp, 6, &nal_unit_type);
  562. rbsp_bits(&rbsp, 6, &nuh_layer_id);
  563. rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
  564. nal_hevc_rbsp_sps(&rbsp, sps);
  565. rbsp_trailing_bits(&rbsp);
  566. if (rbsp.error)
  567. return rbsp.error;
  568. return DIV_ROUND_UP(rbsp.pos, 8);
  569. }
  570. EXPORT_SYMBOL_GPL(nal_hevc_write_sps);
  571. /**
  572. * nal_hevc_read_sps() - Read SPS NAL unit from RBSP format
  573. * @dev: device pointer
  574. * @sps: the &struct nal_hevc_sps to fill from the RBSP data
  575. * @src: the buffer that contains the RBSP data
  576. * @n: size of @src in bytes
  577. *
  578. * Read RBSP data from @src and use it to fill @sps.
  579. *
  580. * Return: number of bytes read from @src or negative error code
  581. */
  582. ssize_t nal_hevc_read_sps(const struct device *dev,
  583. struct nal_hevc_sps *sps, void *src, size_t n)
  584. {
  585. struct rbsp rbsp;
  586. unsigned int forbidden_zero_bit;
  587. unsigned int nal_unit_type;
  588. unsigned int nuh_layer_id;
  589. unsigned int nuh_temporal_id_plus1;
  590. if (!src)
  591. return -EINVAL;
  592. rbsp_init(&rbsp, src, n, &read);
  593. nal_hevc_read_start_code_prefix(&rbsp);
  594. rbsp_bit(&rbsp, &forbidden_zero_bit);
  595. rbsp_bits(&rbsp, 6, &nal_unit_type);
  596. rbsp_bits(&rbsp, 6, &nuh_layer_id);
  597. rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
  598. if (rbsp.error ||
  599. forbidden_zero_bit != 0 ||
  600. nal_unit_type != SPS_NUT)
  601. return -EINVAL;
  602. nal_hevc_rbsp_sps(&rbsp, sps);
  603. rbsp_trailing_bits(&rbsp);
  604. if (rbsp.error)
  605. return rbsp.error;
  606. return DIV_ROUND_UP(rbsp.pos, 8);
  607. }
  608. EXPORT_SYMBOL_GPL(nal_hevc_read_sps);
  609. /**
  610. * nal_hevc_write_pps() - Write PPS NAL unit into RBSP format
  611. * @dev: device pointer
  612. * @dest: the buffer that is filled with RBSP data
  613. * @n: maximum size of @dest in bytes
  614. * @pps: &struct nal_hevc_pps to convert to RBSP
  615. *
  616. * Convert @pps to RBSP data and write it into @dest.
  617. *
  618. * The size of the PPS NAL unit is not known in advance and this function will
  619. * fail, if @dest does not hold sufficient space for the PPS NAL unit.
  620. *
  621. * Return: number of bytes written to @dest or negative error code
  622. */
  623. ssize_t nal_hevc_write_pps(const struct device *dev,
  624. void *dest, size_t n, struct nal_hevc_pps *pps)
  625. {
  626. struct rbsp rbsp;
  627. unsigned int forbidden_zero_bit = 0;
  628. unsigned int nal_unit_type = PPS_NUT;
  629. unsigned int nuh_layer_id = 0;
  630. unsigned int nuh_temporal_id_plus1 = 1;
  631. if (!dest)
  632. return -EINVAL;
  633. rbsp_init(&rbsp, dest, n, &write);
  634. nal_hevc_write_start_code_prefix(&rbsp);
  635. /* NAL unit header */
  636. rbsp_bit(&rbsp, &forbidden_zero_bit);
  637. rbsp_bits(&rbsp, 6, &nal_unit_type);
  638. rbsp_bits(&rbsp, 6, &nuh_layer_id);
  639. rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
  640. nal_hevc_rbsp_pps(&rbsp, pps);
  641. rbsp_trailing_bits(&rbsp);
  642. if (rbsp.error)
  643. return rbsp.error;
  644. return DIV_ROUND_UP(rbsp.pos, 8);
  645. }
  646. EXPORT_SYMBOL_GPL(nal_hevc_write_pps);
  647. /**
  648. * nal_hevc_read_pps() - Read PPS NAL unit from RBSP format
  649. * @dev: device pointer
  650. * @pps: the &struct nal_hevc_pps to fill from the RBSP data
  651. * @src: the buffer that contains the RBSP data
  652. * @n: size of @src in bytes
  653. *
  654. * Read RBSP data from @src and use it to fill @pps.
  655. *
  656. * Return: number of bytes read from @src or negative error code
  657. */
  658. ssize_t nal_hevc_read_pps(const struct device *dev,
  659. struct nal_hevc_pps *pps, void *src, size_t n)
  660. {
  661. struct rbsp rbsp;
  662. unsigned int forbidden_zero_bit;
  663. unsigned int nal_unit_type;
  664. unsigned int nuh_layer_id;
  665. unsigned int nuh_temporal_id_plus1;
  666. if (!src)
  667. return -EINVAL;
  668. rbsp_init(&rbsp, src, n, &read);
  669. nal_hevc_read_start_code_prefix(&rbsp);
  670. /* NAL unit header */
  671. rbsp_bit(&rbsp, &forbidden_zero_bit);
  672. rbsp_bits(&rbsp, 6, &nal_unit_type);
  673. rbsp_bits(&rbsp, 6, &nuh_layer_id);
  674. rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
  675. nal_hevc_rbsp_pps(&rbsp, pps);
  676. rbsp_trailing_bits(&rbsp);
  677. if (rbsp.error)
  678. return rbsp.error;
  679. return DIV_ROUND_UP(rbsp.pos, 8);
  680. }
  681. EXPORT_SYMBOL_GPL(nal_hevc_read_pps);
  682. /**
  683. * nal_hevc_write_filler() - Write filler data RBSP
  684. * @dev: device pointer
  685. * @dest: buffer to fill with filler data
  686. * @n: size of the buffer to fill with filler data
  687. *
  688. * Write a filler data RBSP to @dest with a size of @n bytes and return the
  689. * number of written filler data bytes.
  690. *
  691. * Use this function to generate dummy data in an RBSP data stream that can be
  692. * safely ignored by hevc decoders.
  693. *
  694. * The RBSP format of the filler data is specified in Rec. ITU-T H.265
  695. * (02/2018) 7.3.2.8 Filler data RBSP syntax.
  696. *
  697. * Return: number of filler data bytes (including marker) or negative error
  698. */
  699. ssize_t nal_hevc_write_filler(const struct device *dev, void *dest, size_t n)
  700. {
  701. struct rbsp rbsp;
  702. unsigned int forbidden_zero_bit = 0;
  703. unsigned int nal_unit_type = FD_NUT;
  704. unsigned int nuh_layer_id = 0;
  705. unsigned int nuh_temporal_id_plus1 = 1;
  706. if (!dest)
  707. return -EINVAL;
  708. rbsp_init(&rbsp, dest, n, &write);
  709. nal_hevc_write_start_code_prefix(&rbsp);
  710. rbsp_bit(&rbsp, &forbidden_zero_bit);
  711. rbsp_bits(&rbsp, 6, &nal_unit_type);
  712. rbsp_bits(&rbsp, 6, &nuh_layer_id);
  713. rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
  714. nal_hevc_write_filler_data(&rbsp);
  715. rbsp_trailing_bits(&rbsp);
  716. if (rbsp.error)
  717. return rbsp.error;
  718. return DIV_ROUND_UP(rbsp.pos, 8);
  719. }
  720. EXPORT_SYMBOL_GPL(nal_hevc_write_filler);
  721. /**
  722. * nal_hevc_read_filler() - Read filler data RBSP
  723. * @dev: device pointer
  724. * @src: buffer with RBSP data that is read
  725. * @n: maximum size of src that shall be read
  726. *
  727. * Read a filler data RBSP from @src up to a maximum size of @n bytes and
  728. * return the size of the filler data in bytes including the marker.
  729. *
  730. * This function is used to parse filler data and skip the respective bytes in
  731. * the RBSP data.
  732. *
  733. * The RBSP format of the filler data is specified in Rec. ITU-T H.265
  734. * (02/2018) 7.3.2.8 Filler data RBSP syntax.
  735. *
  736. * Return: number of filler data bytes (including marker) or negative error
  737. */
  738. ssize_t nal_hevc_read_filler(const struct device *dev, void *src, size_t n)
  739. {
  740. struct rbsp rbsp;
  741. unsigned int forbidden_zero_bit;
  742. unsigned int nal_unit_type;
  743. unsigned int nuh_layer_id;
  744. unsigned int nuh_temporal_id_plus1;
  745. if (!src)
  746. return -EINVAL;
  747. rbsp_init(&rbsp, src, n, &read);
  748. nal_hevc_read_start_code_prefix(&rbsp);
  749. rbsp_bit(&rbsp, &forbidden_zero_bit);
  750. rbsp_bits(&rbsp, 6, &nal_unit_type);
  751. rbsp_bits(&rbsp, 6, &nuh_layer_id);
  752. rbsp_bits(&rbsp, 3, &nuh_temporal_id_plus1);
  753. if (rbsp.error)
  754. return rbsp.error;
  755. if (forbidden_zero_bit != 0 ||
  756. nal_unit_type != FD_NUT)
  757. return -EINVAL;
  758. nal_hevc_read_filler_data(&rbsp);
  759. rbsp_trailing_bits(&rbsp);
  760. if (rbsp.error)
  761. return rbsp.error;
  762. return DIV_ROUND_UP(rbsp.pos, 8);
  763. }
  764. EXPORT_SYMBOL_GPL(nal_hevc_read_filler);