nal-h264.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. * The conversion is defined in "ITU-T Rec. H.264 (04/2017) Advanced video
  8. * coding for generic audiovisual services". Decoder drivers may use the
  9. * parser to parse RBSP from encoded streams and configure the hardware, if
  10. * the hardware is not able to parse RBSP itself. Encoder drivers may use the
  11. * generator to generate the RBSP for SPS/PPS nal units and add them to the
  12. * encoded stream if the 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-h264.h"
  22. #include "nal-rbsp.h"
  23. /*
  24. * See Rec. ITU-T H.264 (04/2017) Table 7-1 - NAL unit type codes, syntax
  25. * element categories, and NAL unit type classes
  26. */
  27. enum nal_unit_type {
  28. SEQUENCE_PARAMETER_SET = 7,
  29. PICTURE_PARAMETER_SET = 8,
  30. FILLER_DATA = 12,
  31. };
  32. static void nal_h264_write_start_code_prefix(struct rbsp *rbsp)
  33. {
  34. u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
  35. int i = 4;
  36. if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
  37. rbsp->error = -EINVAL;
  38. return;
  39. }
  40. p[0] = 0x00;
  41. p[1] = 0x00;
  42. p[2] = 0x00;
  43. p[3] = 0x01;
  44. rbsp->pos += i * 8;
  45. }
  46. static void nal_h264_read_start_code_prefix(struct rbsp *rbsp)
  47. {
  48. u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
  49. int i = 4;
  50. if (DIV_ROUND_UP(rbsp->pos, 8) + i > rbsp->size) {
  51. rbsp->error = -EINVAL;
  52. return;
  53. }
  54. if (p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x00 || p[3] != 0x01) {
  55. rbsp->error = -EINVAL;
  56. return;
  57. }
  58. rbsp->pos += i * 8;
  59. }
  60. static void nal_h264_write_filler_data(struct rbsp *rbsp)
  61. {
  62. u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
  63. int i;
  64. /* Keep 1 byte extra for terminating the NAL unit */
  65. i = rbsp->size - DIV_ROUND_UP(rbsp->pos, 8) - 1;
  66. memset(p, 0xff, i);
  67. rbsp->pos += i * 8;
  68. }
  69. static void nal_h264_read_filler_data(struct rbsp *rbsp)
  70. {
  71. u8 *p = rbsp->data + DIV_ROUND_UP(rbsp->pos, 8);
  72. while (*p == 0xff) {
  73. if (DIV_ROUND_UP(rbsp->pos, 8) > rbsp->size) {
  74. rbsp->error = -EINVAL;
  75. return;
  76. }
  77. p++;
  78. rbsp->pos += 8;
  79. }
  80. }
  81. static void nal_h264_rbsp_hrd_parameters(struct rbsp *rbsp,
  82. struct nal_h264_hrd_parameters *hrd)
  83. {
  84. unsigned int i;
  85. if (!hrd) {
  86. rbsp->error = -EINVAL;
  87. return;
  88. }
  89. rbsp_uev(rbsp, &hrd->cpb_cnt_minus1);
  90. rbsp_bits(rbsp, 4, &hrd->bit_rate_scale);
  91. rbsp_bits(rbsp, 4, &hrd->cpb_size_scale);
  92. for (i = 0; i <= hrd->cpb_cnt_minus1; i++) {
  93. rbsp_uev(rbsp, &hrd->bit_rate_value_minus1[i]);
  94. rbsp_uev(rbsp, &hrd->cpb_size_value_minus1[i]);
  95. rbsp_bit(rbsp, &hrd->cbr_flag[i]);
  96. }
  97. rbsp_bits(rbsp, 5, &hrd->initial_cpb_removal_delay_length_minus1);
  98. rbsp_bits(rbsp, 5, &hrd->cpb_removal_delay_length_minus1);
  99. rbsp_bits(rbsp, 5, &hrd->dpb_output_delay_length_minus1);
  100. rbsp_bits(rbsp, 5, &hrd->time_offset_length);
  101. }
  102. static void nal_h264_rbsp_vui_parameters(struct rbsp *rbsp,
  103. struct nal_h264_vui_parameters *vui)
  104. {
  105. if (!vui) {
  106. rbsp->error = -EINVAL;
  107. return;
  108. }
  109. rbsp_bit(rbsp, &vui->aspect_ratio_info_present_flag);
  110. if (vui->aspect_ratio_info_present_flag) {
  111. rbsp_bits(rbsp, 8, &vui->aspect_ratio_idc);
  112. if (vui->aspect_ratio_idc == 255) {
  113. rbsp_bits(rbsp, 16, &vui->sar_width);
  114. rbsp_bits(rbsp, 16, &vui->sar_height);
  115. }
  116. }
  117. rbsp_bit(rbsp, &vui->overscan_info_present_flag);
  118. if (vui->overscan_info_present_flag)
  119. rbsp_bit(rbsp, &vui->overscan_appropriate_flag);
  120. rbsp_bit(rbsp, &vui->video_signal_type_present_flag);
  121. if (vui->video_signal_type_present_flag) {
  122. rbsp_bits(rbsp, 3, &vui->video_format);
  123. rbsp_bit(rbsp, &vui->video_full_range_flag);
  124. rbsp_bit(rbsp, &vui->colour_description_present_flag);
  125. if (vui->colour_description_present_flag) {
  126. rbsp_bits(rbsp, 8, &vui->colour_primaries);
  127. rbsp_bits(rbsp, 8, &vui->transfer_characteristics);
  128. rbsp_bits(rbsp, 8, &vui->matrix_coefficients);
  129. }
  130. }
  131. rbsp_bit(rbsp, &vui->chroma_loc_info_present_flag);
  132. if (vui->chroma_loc_info_present_flag) {
  133. rbsp_uev(rbsp, &vui->chroma_sample_loc_type_top_field);
  134. rbsp_uev(rbsp, &vui->chroma_sample_loc_type_bottom_field);
  135. }
  136. rbsp_bit(rbsp, &vui->timing_info_present_flag);
  137. if (vui->timing_info_present_flag) {
  138. rbsp_bits(rbsp, 32, &vui->num_units_in_tick);
  139. rbsp_bits(rbsp, 32, &vui->time_scale);
  140. rbsp_bit(rbsp, &vui->fixed_frame_rate_flag);
  141. }
  142. rbsp_bit(rbsp, &vui->nal_hrd_parameters_present_flag);
  143. if (vui->nal_hrd_parameters_present_flag)
  144. nal_h264_rbsp_hrd_parameters(rbsp, &vui->nal_hrd_parameters);
  145. rbsp_bit(rbsp, &vui->vcl_hrd_parameters_present_flag);
  146. if (vui->vcl_hrd_parameters_present_flag)
  147. nal_h264_rbsp_hrd_parameters(rbsp, &vui->vcl_hrd_parameters);
  148. if (vui->nal_hrd_parameters_present_flag ||
  149. vui->vcl_hrd_parameters_present_flag)
  150. rbsp_bit(rbsp, &vui->low_delay_hrd_flag);
  151. rbsp_bit(rbsp, &vui->pic_struct_present_flag);
  152. rbsp_bit(rbsp, &vui->bitstream_restriction_flag);
  153. if (vui->bitstream_restriction_flag) {
  154. rbsp_bit(rbsp, &vui->motion_vectors_over_pic_boundaries_flag);
  155. rbsp_uev(rbsp, &vui->max_bytes_per_pic_denom);
  156. rbsp_uev(rbsp, &vui->max_bits_per_mb_denom);
  157. rbsp_uev(rbsp, &vui->log2_max_mv_length_horizontal);
  158. rbsp_uev(rbsp, &vui->log21_max_mv_length_vertical);
  159. rbsp_uev(rbsp, &vui->max_num_reorder_frames);
  160. rbsp_uev(rbsp, &vui->max_dec_frame_buffering);
  161. }
  162. }
  163. static void nal_h264_rbsp_sps(struct rbsp *rbsp, struct nal_h264_sps *sps)
  164. {
  165. unsigned int i;
  166. if (!sps) {
  167. rbsp->error = -EINVAL;
  168. return;
  169. }
  170. rbsp_bits(rbsp, 8, &sps->profile_idc);
  171. rbsp_bit(rbsp, &sps->constraint_set0_flag);
  172. rbsp_bit(rbsp, &sps->constraint_set1_flag);
  173. rbsp_bit(rbsp, &sps->constraint_set2_flag);
  174. rbsp_bit(rbsp, &sps->constraint_set3_flag);
  175. rbsp_bit(rbsp, &sps->constraint_set4_flag);
  176. rbsp_bit(rbsp, &sps->constraint_set5_flag);
  177. rbsp_bits(rbsp, 2, &sps->reserved_zero_2bits);
  178. rbsp_bits(rbsp, 8, &sps->level_idc);
  179. rbsp_uev(rbsp, &sps->seq_parameter_set_id);
  180. if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
  181. sps->profile_idc == 122 || sps->profile_idc == 244 ||
  182. sps->profile_idc == 44 || sps->profile_idc == 83 ||
  183. sps->profile_idc == 86 || sps->profile_idc == 118 ||
  184. sps->profile_idc == 128 || sps->profile_idc == 138 ||
  185. sps->profile_idc == 139 || sps->profile_idc == 134 ||
  186. sps->profile_idc == 135) {
  187. rbsp_uev(rbsp, &sps->chroma_format_idc);
  188. if (sps->chroma_format_idc == 3)
  189. rbsp_bit(rbsp, &sps->separate_colour_plane_flag);
  190. rbsp_uev(rbsp, &sps->bit_depth_luma_minus8);
  191. rbsp_uev(rbsp, &sps->bit_depth_chroma_minus8);
  192. rbsp_bit(rbsp, &sps->qpprime_y_zero_transform_bypass_flag);
  193. rbsp_bit(rbsp, &sps->seq_scaling_matrix_present_flag);
  194. if (sps->seq_scaling_matrix_present_flag)
  195. rbsp->error = -EINVAL;
  196. }
  197. rbsp_uev(rbsp, &sps->log2_max_frame_num_minus4);
  198. rbsp_uev(rbsp, &sps->pic_order_cnt_type);
  199. switch (sps->pic_order_cnt_type) {
  200. case 0:
  201. rbsp_uev(rbsp, &sps->log2_max_pic_order_cnt_lsb_minus4);
  202. break;
  203. case 1:
  204. rbsp_bit(rbsp, &sps->delta_pic_order_always_zero_flag);
  205. rbsp_sev(rbsp, &sps->offset_for_non_ref_pic);
  206. rbsp_sev(rbsp, &sps->offset_for_top_to_bottom_field);
  207. rbsp_uev(rbsp, &sps->num_ref_frames_in_pic_order_cnt_cycle);
  208. for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
  209. rbsp_sev(rbsp, &sps->offset_for_ref_frame[i]);
  210. break;
  211. default:
  212. rbsp->error = -EINVAL;
  213. break;
  214. }
  215. rbsp_uev(rbsp, &sps->max_num_ref_frames);
  216. rbsp_bit(rbsp, &sps->gaps_in_frame_num_value_allowed_flag);
  217. rbsp_uev(rbsp, &sps->pic_width_in_mbs_minus1);
  218. rbsp_uev(rbsp, &sps->pic_height_in_map_units_minus1);
  219. rbsp_bit(rbsp, &sps->frame_mbs_only_flag);
  220. if (!sps->frame_mbs_only_flag)
  221. rbsp_bit(rbsp, &sps->mb_adaptive_frame_field_flag);
  222. rbsp_bit(rbsp, &sps->direct_8x8_inference_flag);
  223. rbsp_bit(rbsp, &sps->frame_cropping_flag);
  224. if (sps->frame_cropping_flag) {
  225. rbsp_uev(rbsp, &sps->crop_left);
  226. rbsp_uev(rbsp, &sps->crop_right);
  227. rbsp_uev(rbsp, &sps->crop_top);
  228. rbsp_uev(rbsp, &sps->crop_bottom);
  229. }
  230. rbsp_bit(rbsp, &sps->vui_parameters_present_flag);
  231. if (sps->vui_parameters_present_flag)
  232. nal_h264_rbsp_vui_parameters(rbsp, &sps->vui);
  233. }
  234. static void nal_h264_rbsp_pps(struct rbsp *rbsp, struct nal_h264_pps *pps)
  235. {
  236. int i;
  237. rbsp_uev(rbsp, &pps->pic_parameter_set_id);
  238. rbsp_uev(rbsp, &pps->seq_parameter_set_id);
  239. rbsp_bit(rbsp, &pps->entropy_coding_mode_flag);
  240. rbsp_bit(rbsp, &pps->bottom_field_pic_order_in_frame_present_flag);
  241. rbsp_uev(rbsp, &pps->num_slice_groups_minus1);
  242. if (pps->num_slice_groups_minus1 > 0) {
  243. rbsp_uev(rbsp, &pps->slice_group_map_type);
  244. switch (pps->slice_group_map_type) {
  245. case 0:
  246. for (i = 0; i < pps->num_slice_groups_minus1; i++)
  247. rbsp_uev(rbsp, &pps->run_length_minus1[i]);
  248. break;
  249. case 2:
  250. for (i = 0; i < pps->num_slice_groups_minus1; i++) {
  251. rbsp_uev(rbsp, &pps->top_left[i]);
  252. rbsp_uev(rbsp, &pps->bottom_right[i]);
  253. }
  254. break;
  255. case 3: case 4: case 5:
  256. rbsp_bit(rbsp, &pps->slice_group_change_direction_flag);
  257. rbsp_uev(rbsp, &pps->slice_group_change_rate_minus1);
  258. break;
  259. case 6:
  260. rbsp_uev(rbsp, &pps->pic_size_in_map_units_minus1);
  261. for (i = 0; i < pps->pic_size_in_map_units_minus1; i++)
  262. rbsp_bits(rbsp,
  263. order_base_2(pps->num_slice_groups_minus1 + 1),
  264. &pps->slice_group_id[i]);
  265. break;
  266. default:
  267. break;
  268. }
  269. }
  270. rbsp_uev(rbsp, &pps->num_ref_idx_l0_default_active_minus1);
  271. rbsp_uev(rbsp, &pps->num_ref_idx_l1_default_active_minus1);
  272. rbsp_bit(rbsp, &pps->weighted_pred_flag);
  273. rbsp_bits(rbsp, 2, &pps->weighted_bipred_idc);
  274. rbsp_sev(rbsp, &pps->pic_init_qp_minus26);
  275. rbsp_sev(rbsp, &pps->pic_init_qs_minus26);
  276. rbsp_sev(rbsp, &pps->chroma_qp_index_offset);
  277. rbsp_bit(rbsp, &pps->deblocking_filter_control_present_flag);
  278. rbsp_bit(rbsp, &pps->constrained_intra_pred_flag);
  279. rbsp_bit(rbsp, &pps->redundant_pic_cnt_present_flag);
  280. if (/* more_rbsp_data() */ false) {
  281. rbsp_bit(rbsp, &pps->transform_8x8_mode_flag);
  282. rbsp_bit(rbsp, &pps->pic_scaling_matrix_present_flag);
  283. if (pps->pic_scaling_matrix_present_flag)
  284. rbsp->error = -EINVAL;
  285. rbsp_sev(rbsp, &pps->second_chroma_qp_index_offset);
  286. }
  287. }
  288. /**
  289. * nal_h264_write_sps() - Write SPS NAL unit into RBSP format
  290. * @dev: device pointer
  291. * @dest: the buffer that is filled with RBSP data
  292. * @n: maximum size of @dest in bytes
  293. * @sps: &struct nal_h264_sps to convert to RBSP
  294. *
  295. * Convert @sps to RBSP data and write it into @dest.
  296. *
  297. * The size of the SPS NAL unit is not known in advance and this function will
  298. * fail, if @dest does not hold sufficient space for the SPS NAL unit.
  299. *
  300. * Return: number of bytes written to @dest or negative error code
  301. */
  302. ssize_t nal_h264_write_sps(const struct device *dev,
  303. void *dest, size_t n, struct nal_h264_sps *sps)
  304. {
  305. struct rbsp rbsp;
  306. unsigned int forbidden_zero_bit = 0;
  307. unsigned int nal_ref_idc = 0;
  308. unsigned int nal_unit_type = SEQUENCE_PARAMETER_SET;
  309. if (!dest)
  310. return -EINVAL;
  311. rbsp_init(&rbsp, dest, n, &write);
  312. nal_h264_write_start_code_prefix(&rbsp);
  313. rbsp_bit(&rbsp, &forbidden_zero_bit);
  314. rbsp_bits(&rbsp, 2, &nal_ref_idc);
  315. rbsp_bits(&rbsp, 5, &nal_unit_type);
  316. nal_h264_rbsp_sps(&rbsp, sps);
  317. rbsp_trailing_bits(&rbsp);
  318. if (rbsp.error)
  319. return rbsp.error;
  320. return DIV_ROUND_UP(rbsp.pos, 8);
  321. }
  322. EXPORT_SYMBOL_GPL(nal_h264_write_sps);
  323. /**
  324. * nal_h264_read_sps() - Read SPS NAL unit from RBSP format
  325. * @dev: device pointer
  326. * @sps: the &struct nal_h264_sps to fill from the RBSP data
  327. * @src: the buffer that contains the RBSP data
  328. * @n: size of @src in bytes
  329. *
  330. * Read RBSP data from @src and use it to fill @sps.
  331. *
  332. * Return: number of bytes read from @src or negative error code
  333. */
  334. ssize_t nal_h264_read_sps(const struct device *dev,
  335. struct nal_h264_sps *sps, void *src, size_t n)
  336. {
  337. struct rbsp rbsp;
  338. unsigned int forbidden_zero_bit;
  339. unsigned int nal_ref_idc;
  340. unsigned int nal_unit_type;
  341. if (!src)
  342. return -EINVAL;
  343. rbsp_init(&rbsp, src, n, &read);
  344. nal_h264_read_start_code_prefix(&rbsp);
  345. rbsp_bit(&rbsp, &forbidden_zero_bit);
  346. rbsp_bits(&rbsp, 2, &nal_ref_idc);
  347. rbsp_bits(&rbsp, 5, &nal_unit_type);
  348. if (rbsp.error ||
  349. forbidden_zero_bit != 0 ||
  350. nal_ref_idc != 0 ||
  351. nal_unit_type != SEQUENCE_PARAMETER_SET)
  352. return -EINVAL;
  353. nal_h264_rbsp_sps(&rbsp, sps);
  354. rbsp_trailing_bits(&rbsp);
  355. if (rbsp.error)
  356. return rbsp.error;
  357. return DIV_ROUND_UP(rbsp.pos, 8);
  358. }
  359. EXPORT_SYMBOL_GPL(nal_h264_read_sps);
  360. /**
  361. * nal_h264_write_pps() - Write PPS NAL unit into RBSP format
  362. * @dev: device pointer
  363. * @dest: the buffer that is filled with RBSP data
  364. * @n: maximum size of @dest in bytes
  365. * @pps: &struct nal_h264_pps to convert to RBSP
  366. *
  367. * Convert @pps to RBSP data and write it into @dest.
  368. *
  369. * The size of the PPS NAL unit is not known in advance and this function will
  370. * fail, if @dest does not hold sufficient space for the PPS NAL unit.
  371. *
  372. * Return: number of bytes written to @dest or negative error code
  373. */
  374. ssize_t nal_h264_write_pps(const struct device *dev,
  375. void *dest, size_t n, struct nal_h264_pps *pps)
  376. {
  377. struct rbsp rbsp;
  378. unsigned int forbidden_zero_bit = 0;
  379. unsigned int nal_ref_idc = 0;
  380. unsigned int nal_unit_type = PICTURE_PARAMETER_SET;
  381. if (!dest)
  382. return -EINVAL;
  383. rbsp_init(&rbsp, dest, n, &write);
  384. nal_h264_write_start_code_prefix(&rbsp);
  385. /* NAL unit header */
  386. rbsp_bit(&rbsp, &forbidden_zero_bit);
  387. rbsp_bits(&rbsp, 2, &nal_ref_idc);
  388. rbsp_bits(&rbsp, 5, &nal_unit_type);
  389. nal_h264_rbsp_pps(&rbsp, pps);
  390. rbsp_trailing_bits(&rbsp);
  391. if (rbsp.error)
  392. return rbsp.error;
  393. return DIV_ROUND_UP(rbsp.pos, 8);
  394. }
  395. EXPORT_SYMBOL_GPL(nal_h264_write_pps);
  396. /**
  397. * nal_h264_read_pps() - Read PPS NAL unit from RBSP format
  398. * @dev: device pointer
  399. * @pps: the &struct nal_h264_pps to fill from the RBSP data
  400. * @src: the buffer that contains the RBSP data
  401. * @n: size of @src in bytes
  402. *
  403. * Read RBSP data from @src and use it to fill @pps.
  404. *
  405. * Return: number of bytes read from @src or negative error code
  406. */
  407. ssize_t nal_h264_read_pps(const struct device *dev,
  408. struct nal_h264_pps *pps, void *src, size_t n)
  409. {
  410. struct rbsp rbsp;
  411. if (!src)
  412. return -EINVAL;
  413. rbsp_init(&rbsp, src, n, &read);
  414. nal_h264_read_start_code_prefix(&rbsp);
  415. /* NAL unit header */
  416. rbsp.pos += 8;
  417. nal_h264_rbsp_pps(&rbsp, pps);
  418. rbsp_trailing_bits(&rbsp);
  419. if (rbsp.error)
  420. return rbsp.error;
  421. return DIV_ROUND_UP(rbsp.pos, 8);
  422. }
  423. EXPORT_SYMBOL_GPL(nal_h264_read_pps);
  424. /**
  425. * nal_h264_write_filler() - Write filler data RBSP
  426. * @dev: device pointer
  427. * @dest: buffer to fill with filler data
  428. * @n: size of the buffer to fill with filler data
  429. *
  430. * Write a filler data RBSP to @dest with a size of @n bytes and return the
  431. * number of written filler data bytes.
  432. *
  433. * Use this function to generate dummy data in an RBSP data stream that can be
  434. * safely ignored by h264 decoders.
  435. *
  436. * The RBSP format of the filler data is specified in Rec. ITU-T H.264
  437. * (04/2017) 7.3.2.7 Filler data RBSP syntax.
  438. *
  439. * Return: number of filler data bytes (including marker) or negative error
  440. */
  441. ssize_t nal_h264_write_filler(const struct device *dev, void *dest, size_t n)
  442. {
  443. struct rbsp rbsp;
  444. unsigned int forbidden_zero_bit = 0;
  445. unsigned int nal_ref_idc = 0;
  446. unsigned int nal_unit_type = FILLER_DATA;
  447. if (!dest)
  448. return -EINVAL;
  449. rbsp_init(&rbsp, dest, n, &write);
  450. nal_h264_write_start_code_prefix(&rbsp);
  451. rbsp_bit(&rbsp, &forbidden_zero_bit);
  452. rbsp_bits(&rbsp, 2, &nal_ref_idc);
  453. rbsp_bits(&rbsp, 5, &nal_unit_type);
  454. nal_h264_write_filler_data(&rbsp);
  455. rbsp_trailing_bits(&rbsp);
  456. return DIV_ROUND_UP(rbsp.pos, 8);
  457. }
  458. EXPORT_SYMBOL_GPL(nal_h264_write_filler);
  459. /**
  460. * nal_h264_read_filler() - Read filler data RBSP
  461. * @dev: device pointer
  462. * @src: buffer with RBSP data that is read
  463. * @n: maximum size of src that shall be read
  464. *
  465. * Read a filler data RBSP from @src up to a maximum size of @n bytes and
  466. * return the size of the filler data in bytes including the marker.
  467. *
  468. * This function is used to parse filler data and skip the respective bytes in
  469. * the RBSP data.
  470. *
  471. * The RBSP format of the filler data is specified in Rec. ITU-T H.264
  472. * (04/2017) 7.3.2.7 Filler data RBSP syntax.
  473. *
  474. * Return: number of filler data bytes (including marker) or negative error
  475. */
  476. ssize_t nal_h264_read_filler(const struct device *dev, void *src, size_t n)
  477. {
  478. struct rbsp rbsp;
  479. unsigned int forbidden_zero_bit;
  480. unsigned int nal_ref_idc;
  481. unsigned int nal_unit_type;
  482. if (!src)
  483. return -EINVAL;
  484. rbsp_init(&rbsp, src, n, &read);
  485. nal_h264_read_start_code_prefix(&rbsp);
  486. rbsp_bit(&rbsp, &forbidden_zero_bit);
  487. rbsp_bits(&rbsp, 2, &nal_ref_idc);
  488. rbsp_bits(&rbsp, 5, &nal_unit_type);
  489. if (rbsp.error)
  490. return rbsp.error;
  491. if (forbidden_zero_bit != 0 ||
  492. nal_ref_idc != 0 ||
  493. nal_unit_type != FILLER_DATA)
  494. return -EINVAL;
  495. nal_h264_read_filler_data(&rbsp);
  496. rbsp_trailing_bits(&rbsp);
  497. if (rbsp.error)
  498. return rbsp.error;
  499. return DIV_ROUND_UP(rbsp.pos, 8);
  500. }
  501. EXPORT_SYMBOL_GPL(nal_h264_read_filler);