vdec_h264_if.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * Copyright (c) 2016 MediaTek Inc.
  3. * Author: PC Chen <pc.chen@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include "../vdec_drv_if.h"
  17. #include "../mtk_vcodec_util.h"
  18. #include "../mtk_vcodec_dec.h"
  19. #include "../mtk_vcodec_intr.h"
  20. #include "../vdec_vpu_if.h"
  21. #include "../vdec_drv_base.h"
  22. #define NAL_NON_IDR_SLICE 0x01
  23. #define NAL_IDR_SLICE 0x05
  24. #define NAL_H264_PPS 0x08
  25. #define NAL_TYPE(value) ((value) & 0x1F)
  26. #define BUF_PREDICTION_SZ (32 * 1024)
  27. #define MB_UNIT_LEN 16
  28. /* motion vector size (bytes) for every macro block */
  29. #define HW_MB_STORE_SZ 64
  30. #define H264_MAX_FB_NUM 17
  31. #define HDR_PARSING_BUF_SZ 1024
  32. /**
  33. * struct h264_fb - h264 decode frame buffer information
  34. * @vdec_fb_va : virtual address of struct vdec_fb
  35. * @y_fb_dma : dma address of Y frame buffer (luma)
  36. * @c_fb_dma : dma address of C frame buffer (chroma)
  37. * @poc : picture order count of frame buffer
  38. * @reserved : for 8 bytes alignment
  39. */
  40. struct h264_fb {
  41. uint64_t vdec_fb_va;
  42. uint64_t y_fb_dma;
  43. uint64_t c_fb_dma;
  44. int32_t poc;
  45. uint32_t reserved;
  46. };
  47. /**
  48. * struct h264_ring_fb_list - ring frame buffer list
  49. * @fb_list : frame buffer arrary
  50. * @read_idx : read index
  51. * @write_idx : write index
  52. * @count : buffer count in list
  53. */
  54. struct h264_ring_fb_list {
  55. struct h264_fb fb_list[H264_MAX_FB_NUM];
  56. unsigned int read_idx;
  57. unsigned int write_idx;
  58. unsigned int count;
  59. unsigned int reserved;
  60. };
  61. /**
  62. * struct vdec_h264_dec_info - decode information
  63. * @dpb_sz : decoding picture buffer size
  64. * @resolution_changed : resoltion change happen
  65. * @realloc_mv_buf : flag to notify driver to re-allocate mv buffer
  66. * @reserved : for 8 bytes alignment
  67. * @bs_dma : Input bit-stream buffer dma address
  68. * @y_fb_dma : Y frame buffer dma address
  69. * @c_fb_dma : C frame buffer dma address
  70. * @vdec_fb_va : VDEC frame buffer struct virtual address
  71. */
  72. struct vdec_h264_dec_info {
  73. uint32_t dpb_sz;
  74. uint32_t resolution_changed;
  75. uint32_t realloc_mv_buf;
  76. uint32_t reserved;
  77. uint64_t bs_dma;
  78. uint64_t y_fb_dma;
  79. uint64_t c_fb_dma;
  80. uint64_t vdec_fb_va;
  81. };
  82. /**
  83. * struct vdec_h264_vsi - shared memory for decode information exchange
  84. * between VPU and Host.
  85. * The memory is allocated by VPU then mapping to Host
  86. * in vpu_dec_init() and freed in vpu_dec_deinit()
  87. * by VPU.
  88. * AP-W/R : AP is writer/reader on this item
  89. * VPU-W/R: VPU is write/reader on this item
  90. * @hdr_buf : Header parsing buffer (AP-W, VPU-R)
  91. * @pred_buf_dma : HW working predication buffer dma address (AP-W, VPU-R)
  92. * @mv_buf_dma : HW working motion vector buffer dma address (AP-W, VPU-R)
  93. * @list_free : free frame buffer ring list (AP-W/R, VPU-W)
  94. * @list_disp : display frame buffer ring list (AP-R, VPU-W)
  95. * @dec : decode information (AP-R, VPU-W)
  96. * @pic : picture information (AP-R, VPU-W)
  97. * @crop : crop information (AP-R, VPU-W)
  98. */
  99. struct vdec_h264_vsi {
  100. unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
  101. uint64_t pred_buf_dma;
  102. uint64_t mv_buf_dma[H264_MAX_FB_NUM];
  103. struct h264_ring_fb_list list_free;
  104. struct h264_ring_fb_list list_disp;
  105. struct vdec_h264_dec_info dec;
  106. struct vdec_pic_info pic;
  107. struct v4l2_rect crop;
  108. };
  109. /**
  110. * struct vdec_h264_inst - h264 decoder instance
  111. * @num_nalu : how many nalus be decoded
  112. * @ctx : point to mtk_vcodec_ctx
  113. * @pred_buf : HW working predication buffer
  114. * @mv_buf : HW working motion vector buffer
  115. * @vpu : VPU instance
  116. * @vsi : VPU shared information
  117. */
  118. struct vdec_h264_inst {
  119. unsigned int num_nalu;
  120. struct mtk_vcodec_ctx *ctx;
  121. struct mtk_vcodec_mem pred_buf;
  122. struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
  123. struct vdec_vpu_inst vpu;
  124. struct vdec_h264_vsi *vsi;
  125. };
  126. static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
  127. {
  128. return HW_MB_STORE_SZ * (width/MB_UNIT_LEN) * (height/MB_UNIT_LEN);
  129. }
  130. static int allocate_predication_buf(struct vdec_h264_inst *inst)
  131. {
  132. int err = 0;
  133. inst->pred_buf.size = BUF_PREDICTION_SZ;
  134. err = mtk_vcodec_mem_alloc(inst->ctx, &inst->pred_buf);
  135. if (err) {
  136. mtk_vcodec_err(inst, "failed to allocate ppl buf");
  137. return err;
  138. }
  139. inst->vsi->pred_buf_dma = inst->pred_buf.dma_addr;
  140. return 0;
  141. }
  142. static void free_predication_buf(struct vdec_h264_inst *inst)
  143. {
  144. struct mtk_vcodec_mem *mem = NULL;
  145. mtk_vcodec_debug_enter(inst);
  146. inst->vsi->pred_buf_dma = 0;
  147. mem = &inst->pred_buf;
  148. if (mem->va)
  149. mtk_vcodec_mem_free(inst->ctx, mem);
  150. }
  151. static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
  152. {
  153. int i;
  154. int err;
  155. struct mtk_vcodec_mem *mem = NULL;
  156. unsigned int buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
  157. for (i = 0; i < H264_MAX_FB_NUM; i++) {
  158. mem = &inst->mv_buf[i];
  159. if (mem->va)
  160. mtk_vcodec_mem_free(inst->ctx, mem);
  161. mem->size = buf_sz;
  162. err = mtk_vcodec_mem_alloc(inst->ctx, mem);
  163. if (err) {
  164. mtk_vcodec_err(inst, "failed to allocate mv buf");
  165. return err;
  166. }
  167. inst->vsi->mv_buf_dma[i] = mem->dma_addr;
  168. }
  169. return 0;
  170. }
  171. static void free_mv_buf(struct vdec_h264_inst *inst)
  172. {
  173. int i;
  174. struct mtk_vcodec_mem *mem = NULL;
  175. for (i = 0; i < H264_MAX_FB_NUM; i++) {
  176. inst->vsi->mv_buf_dma[i] = 0;
  177. mem = &inst->mv_buf[i];
  178. if (mem->va)
  179. mtk_vcodec_mem_free(inst->ctx, mem);
  180. }
  181. }
  182. static int check_list_validity(struct vdec_h264_inst *inst, bool disp_list)
  183. {
  184. struct h264_ring_fb_list *list;
  185. list = disp_list ? &inst->vsi->list_disp : &inst->vsi->list_free;
  186. if (list->count > H264_MAX_FB_NUM ||
  187. list->read_idx >= H264_MAX_FB_NUM ||
  188. list->write_idx >= H264_MAX_FB_NUM) {
  189. mtk_vcodec_err(inst, "%s list err: cnt=%d r_idx=%d w_idx=%d",
  190. disp_list ? "disp" : "free", list->count,
  191. list->read_idx, list->write_idx);
  192. return -EINVAL;
  193. }
  194. return 0;
  195. }
  196. static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
  197. {
  198. struct h264_ring_fb_list *list;
  199. if (fb) {
  200. if (check_list_validity(inst, false))
  201. return;
  202. list = &inst->vsi->list_free;
  203. if (list->count == H264_MAX_FB_NUM) {
  204. mtk_vcodec_err(inst, "[FB] put fb free_list full");
  205. return;
  206. }
  207. mtk_vcodec_debug(inst, "[FB] put fb into free_list @(%p, %llx)",
  208. fb->base_y.va, (u64)fb->base_y.dma_addr);
  209. list->fb_list[list->write_idx].vdec_fb_va = (u64)(uintptr_t)fb;
  210. list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
  211. 0 : list->write_idx + 1;
  212. list->count++;
  213. }
  214. }
  215. static void get_pic_info(struct vdec_h264_inst *inst,
  216. struct vdec_pic_info *pic)
  217. {
  218. *pic = inst->vsi->pic;
  219. mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
  220. pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
  221. mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
  222. pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
  223. }
  224. static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_rect *cr)
  225. {
  226. cr->left = inst->vsi->crop.left;
  227. cr->top = inst->vsi->crop.top;
  228. cr->width = inst->vsi->crop.width;
  229. cr->height = inst->vsi->crop.height;
  230. mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
  231. cr->left, cr->top, cr->width, cr->height);
  232. }
  233. static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
  234. {
  235. *dpb_sz = inst->vsi->dec.dpb_sz;
  236. mtk_vcodec_debug(inst, "sz=%d", *dpb_sz);
  237. }
  238. static int vdec_h264_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
  239. {
  240. struct vdec_h264_inst *inst = NULL;
  241. int err;
  242. inst = kzalloc(sizeof(*inst), GFP_KERNEL);
  243. if (!inst)
  244. return -ENOMEM;
  245. inst->ctx = ctx;
  246. inst->vpu.id = IPI_VDEC_H264;
  247. inst->vpu.dev = ctx->dev->vpu_plat_dev;
  248. inst->vpu.ctx = ctx;
  249. inst->vpu.handler = vpu_dec_ipi_handler;
  250. err = vpu_dec_init(&inst->vpu);
  251. if (err) {
  252. mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
  253. goto error_free_inst;
  254. }
  255. inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
  256. err = allocate_predication_buf(inst);
  257. if (err)
  258. goto error_deinit;
  259. mtk_vcodec_debug(inst, "H264 Instance >> %p", inst);
  260. *h_vdec = (unsigned long)inst;
  261. return 0;
  262. error_deinit:
  263. vpu_dec_deinit(&inst->vpu);
  264. error_free_inst:
  265. kfree(inst);
  266. return err;
  267. }
  268. static void vdec_h264_deinit(unsigned long h_vdec)
  269. {
  270. struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
  271. mtk_vcodec_debug_enter(inst);
  272. vpu_dec_deinit(&inst->vpu);
  273. free_predication_buf(inst);
  274. free_mv_buf(inst);
  275. kfree(inst);
  276. }
  277. static int find_start_code(unsigned char *data, unsigned int data_sz)
  278. {
  279. if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
  280. return 3;
  281. if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
  282. data[3] == 1)
  283. return 4;
  284. return -1;
  285. }
  286. static int vdec_h264_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
  287. struct vdec_fb *fb, bool *res_chg)
  288. {
  289. struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
  290. struct vdec_vpu_inst *vpu = &inst->vpu;
  291. int nal_start_idx = 0;
  292. int err = 0;
  293. unsigned int nal_start;
  294. unsigned int nal_type;
  295. unsigned char *buf;
  296. unsigned int buf_sz;
  297. unsigned int data[2];
  298. uint64_t vdec_fb_va = (u64)(uintptr_t)fb;
  299. uint64_t y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
  300. uint64_t c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
  301. mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
  302. ++inst->num_nalu, y_fb_dma, c_fb_dma, fb);
  303. /* bs NULL means flush decoder */
  304. if (bs == NULL)
  305. return vpu_dec_reset(vpu);
  306. buf = (unsigned char *)bs->va;
  307. buf_sz = bs->size;
  308. nal_start_idx = find_start_code(buf, buf_sz);
  309. if (nal_start_idx < 0)
  310. goto err_free_fb_out;
  311. nal_start = buf[nal_start_idx];
  312. nal_type = NAL_TYPE(buf[nal_start_idx]);
  313. mtk_vcodec_debug(inst, "\n + NALU[%d] type %d +\n", inst->num_nalu,
  314. nal_type);
  315. if (nal_type == NAL_H264_PPS) {
  316. buf_sz -= nal_start_idx;
  317. if (buf_sz > HDR_PARSING_BUF_SZ) {
  318. err = -EILSEQ;
  319. goto err_free_fb_out;
  320. }
  321. memcpy(inst->vsi->hdr_buf, buf + nal_start_idx, buf_sz);
  322. }
  323. inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
  324. inst->vsi->dec.y_fb_dma = y_fb_dma;
  325. inst->vsi->dec.c_fb_dma = c_fb_dma;
  326. inst->vsi->dec.vdec_fb_va = vdec_fb_va;
  327. data[0] = buf_sz;
  328. data[1] = nal_start;
  329. err = vpu_dec_start(vpu, data, 2);
  330. if (err)
  331. goto err_free_fb_out;
  332. *res_chg = inst->vsi->dec.resolution_changed;
  333. if (*res_chg) {
  334. struct vdec_pic_info pic;
  335. mtk_vcodec_debug(inst, "- resolution changed -");
  336. get_pic_info(inst, &pic);
  337. if (inst->vsi->dec.realloc_mv_buf) {
  338. err = alloc_mv_buf(inst, &pic);
  339. if (err)
  340. goto err_free_fb_out;
  341. }
  342. }
  343. if (nal_type == NAL_NON_IDR_SLICE || nal_type == NAL_IDR_SLICE) {
  344. /* wait decoder done interrupt */
  345. err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
  346. MTK_INST_IRQ_RECEIVED,
  347. WAIT_INTR_TIMEOUT_MS);
  348. if (err)
  349. goto err_free_fb_out;
  350. vpu_dec_end(vpu);
  351. }
  352. mtk_vcodec_debug(inst, "\n - NALU[%d] type=%d -\n", inst->num_nalu,
  353. nal_type);
  354. return 0;
  355. err_free_fb_out:
  356. put_fb_to_free(inst, fb);
  357. mtk_vcodec_err(inst, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
  358. return err;
  359. }
  360. static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
  361. struct h264_ring_fb_list *list,
  362. bool disp_list, struct vdec_fb **out_fb)
  363. {
  364. struct vdec_fb *fb;
  365. if (check_list_validity(inst, disp_list))
  366. return;
  367. if (list->count == 0) {
  368. mtk_vcodec_debug(inst, "[FB] there is no %s fb",
  369. disp_list ? "disp" : "free");
  370. *out_fb = NULL;
  371. return;
  372. }
  373. fb = (struct vdec_fb *)
  374. (uintptr_t)list->fb_list[list->read_idx].vdec_fb_va;
  375. fb->status |= (disp_list ? FB_ST_DISPLAY : FB_ST_FREE);
  376. *out_fb = fb;
  377. mtk_vcodec_debug(inst, "[FB] get %s fb st=%d poc=%d %llx",
  378. disp_list ? "disp" : "free",
  379. fb->status, list->fb_list[list->read_idx].poc,
  380. list->fb_list[list->read_idx].vdec_fb_va);
  381. list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
  382. 0 : list->read_idx + 1;
  383. list->count--;
  384. }
  385. static int vdec_h264_get_param(unsigned long h_vdec,
  386. enum vdec_get_param_type type, void *out)
  387. {
  388. struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
  389. switch (type) {
  390. case GET_PARAM_DISP_FRAME_BUFFER:
  391. vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
  392. break;
  393. case GET_PARAM_FREE_FRAME_BUFFER:
  394. vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
  395. break;
  396. case GET_PARAM_PIC_INFO:
  397. get_pic_info(inst, out);
  398. break;
  399. case GET_PARAM_DPB_SIZE:
  400. get_dpb_size(inst, out);
  401. break;
  402. case GET_PARAM_CROP_INFO:
  403. get_crop_info(inst, out);
  404. break;
  405. default:
  406. mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
  407. return -EINVAL;
  408. }
  409. return 0;
  410. }
  411. static struct vdec_common_if vdec_h264_if = {
  412. vdec_h264_init,
  413. vdec_h264_decode,
  414. vdec_h264_get_param,
  415. vdec_h264_deinit,
  416. };
  417. struct vdec_common_if *get_h264_dec_comm_if(void);
  418. struct vdec_common_if *get_h264_dec_comm_if(void)
  419. {
  420. return &vdec_h264_if;
  421. }