coda-h264.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Coda multi-standard codec IP - H.264 helper functions
  4. *
  5. * Copyright (C) 2012 Vista Silicon S.L.
  6. * Javier Martin, <[email protected]>
  7. * Xavier Duret
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/videodev2.h>
  12. #include "coda.h"
  13. static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
  14. static const u8 *coda_find_nal_header(const u8 *buf, const u8 *end)
  15. {
  16. u32 val = 0xffffffff;
  17. do {
  18. val = val << 8 | *buf++;
  19. if (buf >= end)
  20. return NULL;
  21. } while (val != 0x00000001);
  22. return buf;
  23. }
  24. int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb)
  25. {
  26. const u8 *buf = vb2_plane_vaddr(vb, 0);
  27. const u8 *end = buf + vb2_get_plane_payload(vb, 0);
  28. /* Find SPS header */
  29. do {
  30. buf = coda_find_nal_header(buf, end);
  31. if (!buf)
  32. return -EINVAL;
  33. } while ((*buf++ & 0x1f) != 0x7);
  34. ctx->params.h264_profile_idc = buf[0];
  35. ctx->params.h264_level_idc = buf[2];
  36. return 0;
  37. }
  38. int coda_h264_filler_nal(int size, char *p)
  39. {
  40. if (size < 6)
  41. return -EINVAL;
  42. p[0] = 0x00;
  43. p[1] = 0x00;
  44. p[2] = 0x00;
  45. p[3] = 0x01;
  46. p[4] = 0x0c;
  47. memset(p + 5, 0xff, size - 6);
  48. /* Add rbsp stop bit and trailing at the end */
  49. p[size - 1] = 0x80;
  50. return 0;
  51. }
  52. int coda_h264_padding(int size, char *p)
  53. {
  54. int nal_size;
  55. int diff;
  56. diff = size - (size & ~0x7);
  57. if (diff == 0)
  58. return 0;
  59. nal_size = coda_filler_size[diff];
  60. coda_h264_filler_nal(nal_size, p);
  61. return nal_size;
  62. }
  63. int coda_h264_profile(int profile_idc)
  64. {
  65. switch (profile_idc) {
  66. case 66: return V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
  67. case 77: return V4L2_MPEG_VIDEO_H264_PROFILE_MAIN;
  68. case 88: return V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED;
  69. case 100: return V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
  70. default: return -EINVAL;
  71. }
  72. }
  73. int coda_h264_level(int level_idc)
  74. {
  75. switch (level_idc) {
  76. case 10: return V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
  77. case 9: return V4L2_MPEG_VIDEO_H264_LEVEL_1B;
  78. case 11: return V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
  79. case 12: return V4L2_MPEG_VIDEO_H264_LEVEL_1_2;
  80. case 13: return V4L2_MPEG_VIDEO_H264_LEVEL_1_3;
  81. case 20: return V4L2_MPEG_VIDEO_H264_LEVEL_2_0;
  82. case 21: return V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
  83. case 22: return V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
  84. case 30: return V4L2_MPEG_VIDEO_H264_LEVEL_3_0;
  85. case 31: return V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
  86. case 32: return V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
  87. case 40: return V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  88. case 41: return V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
  89. case 42: return V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
  90. case 50: return V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
  91. case 51: return V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
  92. default: return -EINVAL;
  93. }
  94. }
  95. struct rbsp {
  96. char *buf;
  97. int size;
  98. int pos;
  99. };
  100. static inline int rbsp_read_bit(struct rbsp *rbsp)
  101. {
  102. int shift = 7 - (rbsp->pos % 8);
  103. int ofs = rbsp->pos++ / 8;
  104. if (ofs >= rbsp->size)
  105. return -EINVAL;
  106. return (rbsp->buf[ofs] >> shift) & 1;
  107. }
  108. static inline int rbsp_write_bit(struct rbsp *rbsp, int bit)
  109. {
  110. int shift = 7 - (rbsp->pos % 8);
  111. int ofs = rbsp->pos++ / 8;
  112. if (ofs >= rbsp->size)
  113. return -EINVAL;
  114. rbsp->buf[ofs] &= ~(1 << shift);
  115. rbsp->buf[ofs] |= bit << shift;
  116. return 0;
  117. }
  118. static inline int rbsp_read_bits(struct rbsp *rbsp, int num, int *val)
  119. {
  120. int i, ret;
  121. int tmp = 0;
  122. if (num > 32)
  123. return -EINVAL;
  124. for (i = 0; i < num; i++) {
  125. ret = rbsp_read_bit(rbsp);
  126. if (ret < 0)
  127. return ret;
  128. tmp |= ret << (num - i - 1);
  129. }
  130. if (val)
  131. *val = tmp;
  132. return 0;
  133. }
  134. static int rbsp_write_bits(struct rbsp *rbsp, int num, int value)
  135. {
  136. int ret;
  137. while (num--) {
  138. ret = rbsp_write_bit(rbsp, (value >> num) & 1);
  139. if (ret)
  140. return ret;
  141. }
  142. return 0;
  143. }
  144. static int rbsp_read_uev(struct rbsp *rbsp, unsigned int *val)
  145. {
  146. int leading_zero_bits = 0;
  147. unsigned int tmp = 0;
  148. int ret;
  149. while ((ret = rbsp_read_bit(rbsp)) == 0)
  150. leading_zero_bits++;
  151. if (ret < 0)
  152. return ret;
  153. if (leading_zero_bits > 0) {
  154. ret = rbsp_read_bits(rbsp, leading_zero_bits, &tmp);
  155. if (ret)
  156. return ret;
  157. }
  158. if (val)
  159. *val = (1 << leading_zero_bits) - 1 + tmp;
  160. return 0;
  161. }
  162. static int rbsp_write_uev(struct rbsp *rbsp, unsigned int value)
  163. {
  164. int i;
  165. int ret;
  166. int tmp = value + 1;
  167. int leading_zero_bits = fls(tmp) - 1;
  168. for (i = 0; i < leading_zero_bits; i++) {
  169. ret = rbsp_write_bit(rbsp, 0);
  170. if (ret)
  171. return ret;
  172. }
  173. return rbsp_write_bits(rbsp, leading_zero_bits + 1, tmp);
  174. }
  175. static int rbsp_read_sev(struct rbsp *rbsp, int *val)
  176. {
  177. unsigned int tmp;
  178. int ret;
  179. ret = rbsp_read_uev(rbsp, &tmp);
  180. if (ret)
  181. return ret;
  182. if (val) {
  183. if (tmp & 1)
  184. *val = (tmp + 1) / 2;
  185. else
  186. *val = -(tmp / 2);
  187. }
  188. return 0;
  189. }
  190. /**
  191. * coda_h264_sps_fixup - fixes frame cropping values in h.264 SPS
  192. * @ctx: encoder context
  193. * @width: visible width
  194. * @height: visible height
  195. * @buf: buffer containing h.264 SPS RBSP, starting with NAL header
  196. * @size: modified RBSP size return value
  197. * @max_size: available size in buf
  198. *
  199. * Rewrites the frame cropping values in an h.264 SPS RBSP correctly for the
  200. * given visible width and height.
  201. */
  202. int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
  203. int *size, int max_size)
  204. {
  205. int profile_idc;
  206. unsigned int pic_order_cnt_type;
  207. int pic_width_in_mbs_minus1, pic_height_in_map_units_minus1;
  208. int frame_mbs_only_flag, frame_cropping_flag;
  209. int vui_parameters_present_flag;
  210. unsigned int crop_right, crop_bottom;
  211. struct rbsp sps;
  212. int pos;
  213. int ret;
  214. if (*size < 8 || *size >= max_size)
  215. return -EINVAL;
  216. sps.buf = buf + 5; /* Skip NAL header */
  217. sps.size = *size - 5;
  218. profile_idc = sps.buf[0];
  219. /* Skip constraint_set[0-5]_flag, reserved_zero_2bits */
  220. /* Skip level_idc */
  221. sps.pos = 24;
  222. /* seq_parameter_set_id */
  223. ret = rbsp_read_uev(&sps, NULL);
  224. if (ret)
  225. return ret;
  226. if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
  227. profile_idc == 244 || profile_idc == 44 || profile_idc == 83 ||
  228. profile_idc == 86 || profile_idc == 118 || profile_idc == 128 ||
  229. profile_idc == 138 || profile_idc == 139 || profile_idc == 134 ||
  230. profile_idc == 135) {
  231. dev_err(ctx->fh.vdev->dev_parent,
  232. "%s: Handling profile_idc %d not implemented\n",
  233. __func__, profile_idc);
  234. return -EINVAL;
  235. }
  236. /* log2_max_frame_num_minus4 */
  237. ret = rbsp_read_uev(&sps, NULL);
  238. if (ret)
  239. return ret;
  240. ret = rbsp_read_uev(&sps, &pic_order_cnt_type);
  241. if (ret)
  242. return ret;
  243. if (pic_order_cnt_type == 0) {
  244. /* log2_max_pic_order_cnt_lsb_minus4 */
  245. ret = rbsp_read_uev(&sps, NULL);
  246. if (ret)
  247. return ret;
  248. } else if (pic_order_cnt_type == 1) {
  249. unsigned int i, num_ref_frames_in_pic_order_cnt_cycle;
  250. /* delta_pic_order_always_zero_flag */
  251. ret = rbsp_read_bit(&sps);
  252. if (ret < 0)
  253. return ret;
  254. /* offset_for_non_ref_pic */
  255. ret = rbsp_read_sev(&sps, NULL);
  256. if (ret)
  257. return ret;
  258. /* offset_for_top_to_bottom_field */
  259. ret = rbsp_read_sev(&sps, NULL);
  260. if (ret)
  261. return ret;
  262. ret = rbsp_read_uev(&sps,
  263. &num_ref_frames_in_pic_order_cnt_cycle);
  264. if (ret)
  265. return ret;
  266. for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
  267. /* offset_for_ref_frame */
  268. ret = rbsp_read_sev(&sps, NULL);
  269. if (ret)
  270. return ret;
  271. }
  272. }
  273. /* max_num_ref_frames */
  274. ret = rbsp_read_uev(&sps, NULL);
  275. if (ret)
  276. return ret;
  277. /* gaps_in_frame_num_value_allowed_flag */
  278. ret = rbsp_read_bit(&sps);
  279. if (ret < 0)
  280. return ret;
  281. ret = rbsp_read_uev(&sps, &pic_width_in_mbs_minus1);
  282. if (ret)
  283. return ret;
  284. ret = rbsp_read_uev(&sps, &pic_height_in_map_units_minus1);
  285. if (ret)
  286. return ret;
  287. frame_mbs_only_flag = ret = rbsp_read_bit(&sps);
  288. if (ret < 0)
  289. return ret;
  290. if (!frame_mbs_only_flag) {
  291. /* mb_adaptive_frame_field_flag */
  292. ret = rbsp_read_bit(&sps);
  293. if (ret < 0)
  294. return ret;
  295. }
  296. /* direct_8x8_inference_flag */
  297. ret = rbsp_read_bit(&sps);
  298. if (ret < 0)
  299. return ret;
  300. /* Mark position of the frame cropping flag */
  301. pos = sps.pos;
  302. frame_cropping_flag = ret = rbsp_read_bit(&sps);
  303. if (ret < 0)
  304. return ret;
  305. if (frame_cropping_flag) {
  306. unsigned int crop_left, crop_top;
  307. ret = rbsp_read_uev(&sps, &crop_left);
  308. if (ret)
  309. return ret;
  310. ret = rbsp_read_uev(&sps, &crop_right);
  311. if (ret)
  312. return ret;
  313. ret = rbsp_read_uev(&sps, &crop_top);
  314. if (ret)
  315. return ret;
  316. ret = rbsp_read_uev(&sps, &crop_bottom);
  317. if (ret)
  318. return ret;
  319. }
  320. vui_parameters_present_flag = ret = rbsp_read_bit(&sps);
  321. if (ret < 0)
  322. return ret;
  323. if (vui_parameters_present_flag) {
  324. dev_err(ctx->fh.vdev->dev_parent,
  325. "%s: Handling vui_parameters not implemented\n",
  326. __func__);
  327. return -EINVAL;
  328. }
  329. crop_right = round_up(width, 16) - width;
  330. crop_bottom = round_up(height, 16) - height;
  331. crop_right /= 2;
  332. if (frame_mbs_only_flag)
  333. crop_bottom /= 2;
  334. else
  335. crop_bottom /= 4;
  336. sps.size = max_size - 5;
  337. sps.pos = pos;
  338. frame_cropping_flag = 1;
  339. ret = rbsp_write_bit(&sps, frame_cropping_flag);
  340. if (ret)
  341. return ret;
  342. ret = rbsp_write_uev(&sps, 0); /* crop_left */
  343. if (ret)
  344. return ret;
  345. ret = rbsp_write_uev(&sps, crop_right);
  346. if (ret)
  347. return ret;
  348. ret = rbsp_write_uev(&sps, 0); /* crop_top */
  349. if (ret)
  350. return ret;
  351. ret = rbsp_write_uev(&sps, crop_bottom);
  352. if (ret)
  353. return ret;
  354. ret = rbsp_write_bit(&sps, 0); /* vui_parameters_present_flag */
  355. if (ret)
  356. return ret;
  357. ret = rbsp_write_bit(&sps, 1);
  358. if (ret)
  359. return ret;
  360. *size = 5 + DIV_ROUND_UP(sps.pos, 8);
  361. return 0;
  362. }