vdec_h264_if.c 13 KB

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