coda-h264.c 9.3 KB

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