vdec_vp9_if.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. * Copyright (c) 2016 MediaTek Inc.
  3. * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
  4. * Kai-Sean Yang <kai-sean.yang@mediatek.com>
  5. * Tiffany Lin <tiffany.lin@mediatek.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/fs.h>
  17. #include <linux/slab.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/delay.h>
  20. #include <linux/time.h>
  21. #include "../mtk_vcodec_intr.h"
  22. #include "../vdec_drv_base.h"
  23. #include "../vdec_vpu_if.h"
  24. #define VP9_SUPER_FRAME_BS_SZ 64
  25. #define MAX_VP9_DPB_SIZE 9
  26. #define REFS_PER_FRAME 3
  27. #define MAX_NUM_REF_FRAMES 8
  28. #define VP9_MAX_FRM_BUF_NUM 9
  29. #define VP9_MAX_FRM_BUF_NODE_NUM (VP9_MAX_FRM_BUF_NUM * 2)
  30. #define VP9_SEG_ID_SZ 0x12000
  31. /**
  32. * struct vp9_dram_buf - contains buffer info for vpu
  33. * @va : cpu address
  34. * @pa : iova address
  35. * @sz : buffer size
  36. * @padding : for 64 bytes alignment
  37. */
  38. struct vp9_dram_buf {
  39. unsigned long va;
  40. unsigned long pa;
  41. unsigned int sz;
  42. unsigned int padding;
  43. };
  44. /**
  45. * struct vp9_fb_info - contains frame buffer info
  46. * @fb : frmae buffer
  47. * @reserved : reserved field used by vpu
  48. */
  49. struct vp9_fb_info {
  50. struct vdec_fb *fb;
  51. unsigned int reserved[32];
  52. };
  53. /**
  54. * struct vp9_ref_cnt_buf - contains reference buffer information
  55. * @buf : referenced frame buffer
  56. * @ref_cnt : referenced frame buffer's reference count.
  57. * When reference count=0, remove it from reference list
  58. */
  59. struct vp9_ref_cnt_buf {
  60. struct vp9_fb_info buf;
  61. unsigned int ref_cnt;
  62. };
  63. /**
  64. * struct vp9_fb_info - contains current frame's reference buffer information
  65. * @buf : reference buffer
  66. * @idx : reference buffer index to frm_bufs
  67. * @reserved : reserved field used by vpu
  68. */
  69. struct vp9_ref_buf {
  70. struct vp9_fb_info *buf;
  71. unsigned int idx;
  72. unsigned int reserved[6];
  73. };
  74. /**
  75. * struct vp9_fb_info - contains frame buffer info
  76. * @fb : super frame reference frame buffer
  77. * @used : this reference frame info entry is used
  78. * @padding : for 64 bytes size align
  79. */
  80. struct vp9_sf_ref_fb {
  81. struct vdec_fb fb;
  82. int used;
  83. int padding;
  84. };
  85. /*
  86. * struct vdec_vp9_vsi - shared buffer between host and VPU firmware
  87. * AP-W/R : AP is writer/reader on this item
  88. * VPU-W/R: VPU is write/reader on this item
  89. * @sf_bs_buf : super frame backup buffer (AP-W, VPU-R)
  90. * @sf_ref_fb : record supoer frame reference buffer information
  91. * (AP-R/W, VPU-R/W)
  92. * @sf_next_ref_fb_idx : next available super frame (AP-W, VPU-R)
  93. * @sf_frm_cnt : super frame count, filled by vpu (AP-R, VPU-W)
  94. * @sf_frm_offset : super frame offset, filled by vpu (AP-R, VPU-W)
  95. * @sf_frm_sz : super frame size, filled by vpu (AP-R, VPU-W)
  96. * @sf_frm_idx : current super frame (AP-R, VPU-W)
  97. * @sf_init : inform super frame info already parsed by vpu (AP-R, VPU-W)
  98. * @fb : capture buffer (AP-W, VPU-R)
  99. * @bs : bs buffer (AP-W, VPU-R)
  100. * @cur_fb : current show capture buffer (AP-R/W, VPU-R/W)
  101. * @pic_w : picture width (AP-R, VPU-W)
  102. * @pic_h : picture height (AP-R, VPU-W)
  103. * @buf_w : codec width (AP-R, VPU-W)
  104. * @buf_h : coded height (AP-R, VPU-W)
  105. * @buf_sz_y_bs : ufo compressed y plane size (AP-R, VPU-W)
  106. * @buf_sz_c_bs : ufo compressed cbcr plane size (AP-R, VPU-W)
  107. * @buf_len_sz_y : size used to store y plane ufo info (AP-R, VPU-W)
  108. * @buf_len_sz_c : size used to store cbcr plane ufo info (AP-R, VPU-W)
  109. * @profile : profile sparsed from vpu (AP-R, VPU-W)
  110. * @show_frame : display this frame or not (AP-R, VPU-W)
  111. * @show_existing_frame : inform this frame is show existing frame
  112. * (AP-R, VPU-W)
  113. * @frm_to_show_idx : index to show frame (AP-R, VPU-W)
  114. * @refresh_frm_flags : indicate when frame need to refine reference count
  115. * (AP-R, VPU-W)
  116. * @resolution_changed : resolution change in this frame (AP-R, VPU-W)
  117. * @frm_bufs : maintain reference buffer info (AP-R/W, VPU-R/W)
  118. * @ref_frm_map : maintain reference buffer map info (AP-R/W, VPU-R/W)
  119. * @new_fb_idx : index to frm_bufs array (AP-R, VPU-W)
  120. * @frm_num : decoded frame number, include sub-frame count (AP-R, VPU-W)
  121. * @mv_buf : motion vector working buffer (AP-W, VPU-R)
  122. * @frm_refs : maintain three reference buffer info (AP-R/W, VPU-R/W)
  123. * @seg_id_buf : segmentation map working buffer (AP-W, VPU-R)
  124. */
  125. struct vdec_vp9_vsi {
  126. unsigned char sf_bs_buf[VP9_SUPER_FRAME_BS_SZ];
  127. struct vp9_sf_ref_fb sf_ref_fb[VP9_MAX_FRM_BUF_NUM-1];
  128. int sf_next_ref_fb_idx;
  129. unsigned int sf_frm_cnt;
  130. unsigned int sf_frm_offset[VP9_MAX_FRM_BUF_NUM-1];
  131. unsigned int sf_frm_sz[VP9_MAX_FRM_BUF_NUM-1];
  132. unsigned int sf_frm_idx;
  133. unsigned int sf_init;
  134. struct vdec_fb fb;
  135. struct mtk_vcodec_mem bs;
  136. struct vdec_fb cur_fb;
  137. unsigned int pic_w;
  138. unsigned int pic_h;
  139. unsigned int buf_w;
  140. unsigned int buf_h;
  141. unsigned int buf_sz_y_bs;
  142. unsigned int buf_sz_c_bs;
  143. unsigned int buf_len_sz_y;
  144. unsigned int buf_len_sz_c;
  145. unsigned int profile;
  146. unsigned int show_frame;
  147. unsigned int show_existing_frame;
  148. unsigned int frm_to_show_idx;
  149. unsigned int refresh_frm_flags;
  150. unsigned int resolution_changed;
  151. struct vp9_ref_cnt_buf frm_bufs[VP9_MAX_FRM_BUF_NUM];
  152. int ref_frm_map[MAX_NUM_REF_FRAMES];
  153. unsigned int new_fb_idx;
  154. unsigned int frm_num;
  155. struct vp9_dram_buf mv_buf;
  156. struct vp9_ref_buf frm_refs[REFS_PER_FRAME];
  157. struct vp9_dram_buf seg_id_buf;
  158. };
  159. /*
  160. * struct vdec_vp9_inst - vp9 decode instance
  161. * @mv_buf : working buffer for mv
  162. * @seg_id_buf : working buffer for segmentation map
  163. * @dec_fb : vdec_fb node to link fb to different fb_xxx_list
  164. * @available_fb_node_list : current available vdec_fb node
  165. * @fb_use_list : current used or referenced vdec_fb
  166. * @fb_free_list : current available to free vdec_fb
  167. * @fb_disp_list : current available to display vdec_fb
  168. * @cur_fb : current frame buffer
  169. * @ctx : current decode context
  170. * @vpu : vpu instance information
  171. * @vsi : shared buffer between host and VPU firmware
  172. * @total_frm_cnt : total frame count, it do not include sub-frames in super
  173. * frame
  174. * @mem : instance memory information
  175. */
  176. struct vdec_vp9_inst {
  177. struct mtk_vcodec_mem mv_buf;
  178. struct mtk_vcodec_mem seg_id_buf;
  179. struct vdec_fb_node dec_fb[VP9_MAX_FRM_BUF_NODE_NUM];
  180. struct list_head available_fb_node_list;
  181. struct list_head fb_use_list;
  182. struct list_head fb_free_list;
  183. struct list_head fb_disp_list;
  184. struct vdec_fb *cur_fb;
  185. struct mtk_vcodec_ctx *ctx;
  186. struct vdec_vpu_inst vpu;
  187. struct vdec_vp9_vsi *vsi;
  188. unsigned int total_frm_cnt;
  189. struct mtk_vcodec_mem mem;
  190. };
  191. static bool vp9_is_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
  192. {
  193. int i;
  194. struct vdec_vp9_vsi *vsi = inst->vsi;
  195. for (i = 0; i < ARRAY_SIZE(vsi->sf_ref_fb); i++) {
  196. if (fb == &vsi->sf_ref_fb[i].fb)
  197. return true;
  198. }
  199. return false;
  200. }
  201. static struct vdec_fb *vp9_rm_from_fb_use_list(struct vdec_vp9_inst
  202. *inst, void *addr)
  203. {
  204. struct vdec_fb *fb = NULL;
  205. struct vdec_fb_node *node;
  206. list_for_each_entry(node, &inst->fb_use_list, list) {
  207. fb = (struct vdec_fb *)node->fb;
  208. if (fb->base_y.va == addr) {
  209. list_move_tail(&node->list,
  210. &inst->available_fb_node_list);
  211. break;
  212. }
  213. }
  214. return fb;
  215. }
  216. static void vp9_add_to_fb_free_list(struct vdec_vp9_inst *inst,
  217. struct vdec_fb *fb)
  218. {
  219. struct vdec_fb_node *node;
  220. if (fb) {
  221. node = list_first_entry_or_null(&inst->available_fb_node_list,
  222. struct vdec_fb_node, list);
  223. if (node) {
  224. node->fb = fb;
  225. list_move_tail(&node->list, &inst->fb_free_list);
  226. }
  227. } else {
  228. mtk_vcodec_debug(inst, "No free fb node");
  229. }
  230. }
  231. static void vp9_free_sf_ref_fb(struct vdec_fb *fb)
  232. {
  233. struct vp9_sf_ref_fb *sf_ref_fb =
  234. container_of(fb, struct vp9_sf_ref_fb, fb);
  235. sf_ref_fb->used = 0;
  236. }
  237. static void vp9_ref_cnt_fb(struct vdec_vp9_inst *inst, int *idx,
  238. int new_idx)
  239. {
  240. struct vdec_vp9_vsi *vsi = inst->vsi;
  241. int ref_idx = *idx;
  242. if (ref_idx >= 0 && vsi->frm_bufs[ref_idx].ref_cnt > 0) {
  243. vsi->frm_bufs[ref_idx].ref_cnt--;
  244. if (vsi->frm_bufs[ref_idx].ref_cnt == 0) {
  245. if (!vp9_is_sf_ref_fb(inst,
  246. vsi->frm_bufs[ref_idx].buf.fb)) {
  247. struct vdec_fb *fb;
  248. fb = vp9_rm_from_fb_use_list(inst,
  249. vsi->frm_bufs[ref_idx].buf.fb->base_y.va);
  250. vp9_add_to_fb_free_list(inst, fb);
  251. } else
  252. vp9_free_sf_ref_fb(
  253. vsi->frm_bufs[ref_idx].buf.fb);
  254. }
  255. }
  256. *idx = new_idx;
  257. vsi->frm_bufs[new_idx].ref_cnt++;
  258. }
  259. static void vp9_free_all_sf_ref_fb(struct vdec_vp9_inst *inst)
  260. {
  261. int i;
  262. struct vdec_vp9_vsi *vsi = inst->vsi;
  263. for (i = 0; i < ARRAY_SIZE(vsi->sf_ref_fb); i++) {
  264. if (vsi->sf_ref_fb[i].fb.base_y.va) {
  265. mtk_vcodec_mem_free(inst->ctx,
  266. &vsi->sf_ref_fb[i].fb.base_y);
  267. mtk_vcodec_mem_free(inst->ctx,
  268. &vsi->sf_ref_fb[i].fb.base_c);
  269. vsi->sf_ref_fb[i].used = 0;
  270. }
  271. }
  272. }
  273. /* For each sub-frame except the last one, the driver will dynamically
  274. * allocate reference buffer by calling vp9_get_sf_ref_fb()
  275. * The last sub-frame will use the original fb provided by the
  276. * vp9_dec_decode() interface
  277. */
  278. static int vp9_get_sf_ref_fb(struct vdec_vp9_inst *inst)
  279. {
  280. int idx;
  281. struct mtk_vcodec_mem *mem_basy_y;
  282. struct mtk_vcodec_mem *mem_basy_c;
  283. struct vdec_vp9_vsi *vsi = inst->vsi;
  284. for (idx = 0;
  285. idx < ARRAY_SIZE(vsi->sf_ref_fb);
  286. idx++) {
  287. if (vsi->sf_ref_fb[idx].fb.base_y.va &&
  288. vsi->sf_ref_fb[idx].used == 0) {
  289. return idx;
  290. }
  291. }
  292. for (idx = 0;
  293. idx < ARRAY_SIZE(vsi->sf_ref_fb);
  294. idx++) {
  295. if (vsi->sf_ref_fb[idx].fb.base_y.va == NULL)
  296. break;
  297. }
  298. if (idx == ARRAY_SIZE(vsi->sf_ref_fb)) {
  299. mtk_vcodec_err(inst, "List Full");
  300. return -1;
  301. }
  302. mem_basy_y = &vsi->sf_ref_fb[idx].fb.base_y;
  303. mem_basy_y->size = vsi->buf_sz_y_bs +
  304. vsi->buf_len_sz_y;
  305. if (mtk_vcodec_mem_alloc(inst->ctx, mem_basy_y)) {
  306. mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf y_buf");
  307. return -1;
  308. }
  309. mem_basy_c = &vsi->sf_ref_fb[idx].fb.base_c;
  310. mem_basy_c->size = vsi->buf_sz_c_bs +
  311. vsi->buf_len_sz_c;
  312. if (mtk_vcodec_mem_alloc(inst->ctx, mem_basy_c)) {
  313. mtk_vcodec_err(inst, "Cannot allocate sf_ref_fb c_buf");
  314. return -1;
  315. }
  316. vsi->sf_ref_fb[idx].used = 0;
  317. return idx;
  318. }
  319. static bool vp9_alloc_work_buf(struct vdec_vp9_inst *inst)
  320. {
  321. struct vdec_vp9_vsi *vsi = inst->vsi;
  322. int result;
  323. struct mtk_vcodec_mem *mem;
  324. unsigned int max_pic_w;
  325. unsigned int max_pic_h;
  326. if (!(inst->ctx->dev->dec_capability &
  327. VCODEC_CAPABILITY_4K_DISABLED)) {
  328. max_pic_w = VCODEC_DEC_4K_CODED_WIDTH;
  329. max_pic_h = VCODEC_DEC_4K_CODED_HEIGHT;
  330. } else {
  331. max_pic_w = MTK_VDEC_MAX_W;
  332. max_pic_h = MTK_VDEC_MAX_H;
  333. }
  334. if ((vsi->pic_w > max_pic_w) ||
  335. (vsi->pic_h > max_pic_h)) {
  336. mtk_vcodec_err(inst, "Invalid w/h %d/%d",
  337. vsi->pic_w, vsi->pic_h);
  338. return false;
  339. }
  340. mtk_vcodec_debug(inst, "BUF CHG(%d): w/h/sb_w/sb_h=%d/%d/%d/%d",
  341. vsi->resolution_changed,
  342. vsi->pic_w,
  343. vsi->pic_h,
  344. vsi->buf_w,
  345. vsi->buf_h);
  346. mem = &inst->mv_buf;
  347. if (mem->va)
  348. mtk_vcodec_mem_free(inst->ctx, mem);
  349. mem->size = ((vsi->buf_w / 64) *
  350. (vsi->buf_h / 64) + 2) * 36 * 16;
  351. result = mtk_vcodec_mem_alloc(inst->ctx, mem);
  352. if (result) {
  353. mem->size = 0;
  354. mtk_vcodec_err(inst, "Cannot allocate mv_buf");
  355. return false;
  356. }
  357. /* Set the va again */
  358. vsi->mv_buf.va = (unsigned long)mem->va;
  359. vsi->mv_buf.pa = (unsigned long)mem->dma_addr;
  360. vsi->mv_buf.sz = (unsigned int)mem->size;
  361. mem = &inst->seg_id_buf;
  362. if (mem->va)
  363. mtk_vcodec_mem_free(inst->ctx, mem);
  364. mem->size = VP9_SEG_ID_SZ;
  365. result = mtk_vcodec_mem_alloc(inst->ctx, mem);
  366. if (result) {
  367. mem->size = 0;
  368. mtk_vcodec_err(inst, "Cannot allocate seg_id_buf");
  369. return false;
  370. }
  371. /* Set the va again */
  372. vsi->seg_id_buf.va = (unsigned long)mem->va;
  373. vsi->seg_id_buf.pa = (unsigned long)mem->dma_addr;
  374. vsi->seg_id_buf.sz = (unsigned int)mem->size;
  375. vp9_free_all_sf_ref_fb(inst);
  376. vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
  377. return true;
  378. }
  379. static bool vp9_add_to_fb_disp_list(struct vdec_vp9_inst *inst,
  380. struct vdec_fb *fb)
  381. {
  382. struct vdec_fb_node *node;
  383. if (!fb) {
  384. mtk_vcodec_err(inst, "fb == NULL");
  385. return false;
  386. }
  387. node = list_first_entry_or_null(&inst->available_fb_node_list,
  388. struct vdec_fb_node, list);
  389. if (node) {
  390. node->fb = fb;
  391. list_move_tail(&node->list, &inst->fb_disp_list);
  392. } else {
  393. mtk_vcodec_err(inst, "No available fb node");
  394. return false;
  395. }
  396. return true;
  397. }
  398. /* If any buffer updating is signaled it should be done here. */
  399. static void vp9_swap_frm_bufs(struct vdec_vp9_inst *inst)
  400. {
  401. struct vdec_vp9_vsi *vsi = inst->vsi;
  402. struct vp9_fb_info *frm_to_show;
  403. int ref_index = 0, mask;
  404. for (mask = vsi->refresh_frm_flags; mask; mask >>= 1) {
  405. if (mask & 1)
  406. vp9_ref_cnt_fb(inst, &vsi->ref_frm_map[ref_index],
  407. vsi->new_fb_idx);
  408. ++ref_index;
  409. }
  410. frm_to_show = &vsi->frm_bufs[vsi->new_fb_idx].buf;
  411. vsi->frm_bufs[vsi->new_fb_idx].ref_cnt--;
  412. if (frm_to_show->fb != inst->cur_fb) {
  413. /* This frame is show exist frame and no decode output
  414. * copy frame data from frm_to_show to current CAPTURE
  415. * buffer
  416. */
  417. if ((frm_to_show->fb != NULL) &&
  418. (inst->cur_fb->base_y.size >=
  419. frm_to_show->fb->base_y.size)) {
  420. memcpy((void *)inst->cur_fb->base_y.va,
  421. (void *)frm_to_show->fb->base_y.va,
  422. vsi->buf_w *
  423. vsi->buf_h);
  424. memcpy((void *)inst->cur_fb->base_c.va,
  425. (void *)frm_to_show->fb->base_c.va,
  426. vsi->buf_w *
  427. vsi->buf_h / 2);
  428. } else {
  429. /* After resolution change case, current CAPTURE buffer
  430. * may have less buffer size than frm_to_show buffer
  431. * size
  432. */
  433. if (frm_to_show->fb != NULL)
  434. mtk_vcodec_err(inst,
  435. "inst->cur_fb->base_y.size=%zu, frm_to_show->fb.base_y.size=%zu",
  436. inst->cur_fb->base_y.size,
  437. frm_to_show->fb->base_y.size);
  438. }
  439. if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
  440. if (vsi->show_frame)
  441. vp9_add_to_fb_disp_list(inst, inst->cur_fb);
  442. }
  443. } else {
  444. if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
  445. if (vsi->show_frame)
  446. vp9_add_to_fb_disp_list(inst, frm_to_show->fb);
  447. }
  448. }
  449. /* when ref_cnt ==0, move this fb to fb_free_list. v4l2 driver will
  450. * clean fb_free_list
  451. */
  452. if (vsi->frm_bufs[vsi->new_fb_idx].ref_cnt == 0) {
  453. if (!vp9_is_sf_ref_fb(
  454. inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb)) {
  455. struct vdec_fb *fb;
  456. fb = vp9_rm_from_fb_use_list(inst,
  457. vsi->frm_bufs[vsi->new_fb_idx].buf.fb->base_y.va);
  458. vp9_add_to_fb_free_list(inst, fb);
  459. } else {
  460. vp9_free_sf_ref_fb(
  461. vsi->frm_bufs[vsi->new_fb_idx].buf.fb);
  462. }
  463. }
  464. /* if this super frame and it is not last sub-frame, get next fb for
  465. * sub-frame decode
  466. */
  467. if (vsi->sf_frm_cnt > 0 && vsi->sf_frm_idx != vsi->sf_frm_cnt - 1)
  468. vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
  469. }
  470. static bool vp9_wait_dec_end(struct vdec_vp9_inst *inst)
  471. {
  472. struct mtk_vcodec_ctx *ctx = inst->ctx;
  473. mtk_vcodec_wait_for_done_ctx(inst->ctx,
  474. MTK_INST_IRQ_RECEIVED,
  475. WAIT_INTR_TIMEOUT_MS);
  476. if (ctx->irq_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
  477. return true;
  478. else
  479. return false;
  480. }
  481. static struct vdec_vp9_inst *vp9_alloc_inst(struct mtk_vcodec_ctx *ctx)
  482. {
  483. int result;
  484. struct mtk_vcodec_mem mem;
  485. struct vdec_vp9_inst *inst;
  486. memset(&mem, 0, sizeof(mem));
  487. mem.size = sizeof(struct vdec_vp9_inst);
  488. result = mtk_vcodec_mem_alloc(ctx, &mem);
  489. if (result)
  490. return NULL;
  491. inst = mem.va;
  492. inst->mem = mem;
  493. return inst;
  494. }
  495. static void vp9_free_inst(struct vdec_vp9_inst *inst)
  496. {
  497. struct mtk_vcodec_mem mem;
  498. mem = inst->mem;
  499. if (mem.va)
  500. mtk_vcodec_mem_free(inst->ctx, &mem);
  501. }
  502. static bool vp9_decode_end_proc(struct vdec_vp9_inst *inst)
  503. {
  504. struct vdec_vp9_vsi *vsi = inst->vsi;
  505. bool ret = false;
  506. if (!vsi->show_existing_frame) {
  507. ret = vp9_wait_dec_end(inst);
  508. if (!ret) {
  509. mtk_vcodec_err(inst, "Decode failed, Decode Timeout @[%d]",
  510. vsi->frm_num);
  511. return false;
  512. }
  513. if (vpu_dec_end(&inst->vpu)) {
  514. mtk_vcodec_err(inst, "vp9_dec_vpu_end failed");
  515. return false;
  516. }
  517. mtk_vcodec_debug(inst, "Decode Ok @%d (%d/%d)", vsi->frm_num,
  518. vsi->pic_w, vsi->pic_h);
  519. } else {
  520. mtk_vcodec_debug(inst, "Decode Ok @%d (show_existing_frame)",
  521. vsi->frm_num);
  522. }
  523. vp9_swap_frm_bufs(inst);
  524. vsi->frm_num++;
  525. return true;
  526. }
  527. static bool vp9_is_last_sub_frm(struct vdec_vp9_inst *inst)
  528. {
  529. struct vdec_vp9_vsi *vsi = inst->vsi;
  530. if (vsi->sf_frm_cnt <= 0 || vsi->sf_frm_idx == vsi->sf_frm_cnt)
  531. return true;
  532. return false;
  533. }
  534. static struct vdec_fb *vp9_rm_from_fb_disp_list(struct vdec_vp9_inst *inst)
  535. {
  536. struct vdec_fb_node *node;
  537. struct vdec_fb *fb = NULL;
  538. node = list_first_entry_or_null(&inst->fb_disp_list,
  539. struct vdec_fb_node, list);
  540. if (node) {
  541. fb = (struct vdec_fb *)node->fb;
  542. fb->status |= FB_ST_DISPLAY;
  543. list_move_tail(&node->list, &inst->available_fb_node_list);
  544. mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
  545. node->fb, fb->status);
  546. } else
  547. mtk_vcodec_debug(inst, "[FB] there is no disp fb");
  548. return fb;
  549. }
  550. static bool vp9_add_to_fb_use_list(struct vdec_vp9_inst *inst,
  551. struct vdec_fb *fb)
  552. {
  553. struct vdec_fb_node *node;
  554. if (!fb) {
  555. mtk_vcodec_debug(inst, "fb == NULL");
  556. return false;
  557. }
  558. node = list_first_entry_or_null(&inst->available_fb_node_list,
  559. struct vdec_fb_node, list);
  560. if (node) {
  561. node->fb = fb;
  562. list_move_tail(&node->list, &inst->fb_use_list);
  563. } else {
  564. mtk_vcodec_err(inst, "No free fb node");
  565. return false;
  566. }
  567. return true;
  568. }
  569. static void vp9_reset(struct vdec_vp9_inst *inst)
  570. {
  571. struct vdec_fb_node *node, *tmp;
  572. list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list)
  573. list_move_tail(&node->list, &inst->fb_free_list);
  574. vp9_free_all_sf_ref_fb(inst);
  575. inst->vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
  576. if (vpu_dec_reset(&inst->vpu))
  577. mtk_vcodec_err(inst, "vp9_dec_vpu_reset failed");
  578. /* Set the va again, since vpu_dec_reset will clear mv_buf in vpu */
  579. inst->vsi->mv_buf.va = (unsigned long)inst->mv_buf.va;
  580. inst->vsi->mv_buf.pa = (unsigned long)inst->mv_buf.dma_addr;
  581. inst->vsi->mv_buf.sz = (unsigned long)inst->mv_buf.size;
  582. /* Set the va again, since vpu_dec_reset will clear seg_id_buf in vpu */
  583. inst->vsi->seg_id_buf.va = (unsigned long)inst->seg_id_buf.va;
  584. inst->vsi->seg_id_buf.pa = (unsigned long)inst->seg_id_buf.dma_addr;
  585. inst->vsi->seg_id_buf.sz = (unsigned long)inst->seg_id_buf.size;
  586. }
  587. static void init_all_fb_lists(struct vdec_vp9_inst *inst)
  588. {
  589. int i;
  590. INIT_LIST_HEAD(&inst->available_fb_node_list);
  591. INIT_LIST_HEAD(&inst->fb_use_list);
  592. INIT_LIST_HEAD(&inst->fb_free_list);
  593. INIT_LIST_HEAD(&inst->fb_disp_list);
  594. for (i = 0; i < ARRAY_SIZE(inst->dec_fb); i++) {
  595. INIT_LIST_HEAD(&inst->dec_fb[i].list);
  596. inst->dec_fb[i].fb = NULL;
  597. list_add_tail(&inst->dec_fb[i].list,
  598. &inst->available_fb_node_list);
  599. }
  600. }
  601. static void get_pic_info(struct vdec_vp9_inst *inst, struct vdec_pic_info *pic)
  602. {
  603. pic->y_bs_sz = inst->vsi->buf_sz_y_bs;
  604. pic->c_bs_sz = inst->vsi->buf_sz_c_bs;
  605. pic->y_len_sz = inst->vsi->buf_len_sz_y;
  606. pic->c_len_sz = inst->vsi->buf_len_sz_c;
  607. pic->pic_w = inst->vsi->pic_w;
  608. pic->pic_h = inst->vsi->pic_h;
  609. pic->buf_w = inst->vsi->buf_w;
  610. pic->buf_h = inst->vsi->buf_h;
  611. mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
  612. pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
  613. mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
  614. pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
  615. }
  616. static void get_disp_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
  617. {
  618. *out_fb = vp9_rm_from_fb_disp_list(inst);
  619. if (*out_fb)
  620. (*out_fb)->status |= FB_ST_DISPLAY;
  621. }
  622. static void get_free_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
  623. {
  624. struct vdec_fb_node *node;
  625. struct vdec_fb *fb = NULL;
  626. node = list_first_entry_or_null(&inst->fb_free_list,
  627. struct vdec_fb_node, list);
  628. if (node) {
  629. list_move_tail(&node->list, &inst->available_fb_node_list);
  630. fb = (struct vdec_fb *)node->fb;
  631. fb->status |= FB_ST_FREE;
  632. mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
  633. node->fb, fb->status);
  634. } else {
  635. mtk_vcodec_debug(inst, "[FB] there is no free fb");
  636. }
  637. *out_fb = fb;
  638. }
  639. static int validate_vsi_array_indexes(struct vdec_vp9_inst *inst,
  640. struct vdec_vp9_vsi *vsi) {
  641. if (vsi->sf_frm_idx >= VP9_MAX_FRM_BUF_NUM - 1) {
  642. mtk_vcodec_err(inst, "Invalid vsi->sf_frm_idx=%u.",
  643. vsi->sf_frm_idx);
  644. return -EIO;
  645. }
  646. if (vsi->frm_to_show_idx >= VP9_MAX_FRM_BUF_NUM) {
  647. mtk_vcodec_err(inst, "Invalid vsi->frm_to_show_idx=%u.",
  648. vsi->frm_to_show_idx);
  649. return -EIO;
  650. }
  651. if (vsi->new_fb_idx >= VP9_MAX_FRM_BUF_NUM) {
  652. mtk_vcodec_err(inst, "Invalid vsi->new_fb_idx=%u.",
  653. vsi->new_fb_idx);
  654. return -EIO;
  655. }
  656. return 0;
  657. }
  658. static void vdec_vp9_deinit(unsigned long h_vdec)
  659. {
  660. struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
  661. struct mtk_vcodec_mem *mem;
  662. int ret = 0;
  663. ret = vpu_dec_deinit(&inst->vpu);
  664. if (ret)
  665. mtk_vcodec_err(inst, "vpu_dec_deinit failed");
  666. mem = &inst->mv_buf;
  667. if (mem->va)
  668. mtk_vcodec_mem_free(inst->ctx, mem);
  669. mem = &inst->seg_id_buf;
  670. if (mem->va)
  671. mtk_vcodec_mem_free(inst->ctx, mem);
  672. vp9_free_all_sf_ref_fb(inst);
  673. vp9_free_inst(inst);
  674. }
  675. static int vdec_vp9_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
  676. {
  677. struct vdec_vp9_inst *inst;
  678. inst = vp9_alloc_inst(ctx);
  679. if (!inst)
  680. return -ENOMEM;
  681. inst->total_frm_cnt = 0;
  682. inst->ctx = ctx;
  683. inst->vpu.id = IPI_VDEC_VP9;
  684. inst->vpu.dev = ctx->dev->vpu_plat_dev;
  685. inst->vpu.ctx = ctx;
  686. inst->vpu.handler = vpu_dec_ipi_handler;
  687. if (vpu_dec_init(&inst->vpu)) {
  688. mtk_vcodec_err(inst, "vp9_dec_vpu_init failed");
  689. goto err_deinit_inst;
  690. }
  691. inst->vsi = (struct vdec_vp9_vsi *)inst->vpu.vsi;
  692. init_all_fb_lists(inst);
  693. (*h_vdec) = (unsigned long)inst;
  694. return 0;
  695. err_deinit_inst:
  696. vp9_free_inst(inst);
  697. return -EINVAL;
  698. }
  699. static int vdec_vp9_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
  700. struct vdec_fb *fb, bool *res_chg)
  701. {
  702. int ret = 0;
  703. struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
  704. struct vdec_vp9_vsi *vsi = inst->vsi;
  705. u32 data[3];
  706. int i;
  707. *res_chg = false;
  708. if ((bs == NULL) && (fb == NULL)) {
  709. mtk_vcodec_debug(inst, "[EOS]");
  710. vp9_reset(inst);
  711. return ret;
  712. }
  713. if (bs == NULL) {
  714. mtk_vcodec_err(inst, "bs == NULL");
  715. return -EINVAL;
  716. }
  717. mtk_vcodec_debug(inst, "Input BS Size = %zu", bs->size);
  718. while (1) {
  719. struct vdec_fb *cur_fb = NULL;
  720. data[0] = *((unsigned int *)bs->va);
  721. data[1] = *((unsigned int *)(bs->va + 4));
  722. data[2] = *((unsigned int *)(bs->va + 8));
  723. vsi->bs = *bs;
  724. if (fb)
  725. vsi->fb = *fb;
  726. if (!vsi->sf_init) {
  727. unsigned int sf_bs_sz;
  728. unsigned int sf_bs_off;
  729. unsigned char *sf_bs_src;
  730. unsigned char *sf_bs_dst;
  731. sf_bs_sz = bs->size > VP9_SUPER_FRAME_BS_SZ ?
  732. VP9_SUPER_FRAME_BS_SZ : bs->size;
  733. sf_bs_off = VP9_SUPER_FRAME_BS_SZ - sf_bs_sz;
  734. sf_bs_src = bs->va + bs->size - sf_bs_sz;
  735. sf_bs_dst = vsi->sf_bs_buf + sf_bs_off;
  736. memcpy(sf_bs_dst, sf_bs_src, sf_bs_sz);
  737. } else {
  738. if ((vsi->sf_frm_cnt > 0) &&
  739. (vsi->sf_frm_idx < vsi->sf_frm_cnt)) {
  740. unsigned int idx = vsi->sf_frm_idx;
  741. memcpy((void *)bs->va,
  742. (void *)(bs->va +
  743. vsi->sf_frm_offset[idx]),
  744. vsi->sf_frm_sz[idx]);
  745. }
  746. }
  747. memset(inst->seg_id_buf.va, 0, inst->seg_id_buf.size);
  748. ret = vpu_dec_start(&inst->vpu, data, 3);
  749. if (ret) {
  750. mtk_vcodec_err(inst, "vpu_dec_start failed");
  751. goto DECODE_ERROR;
  752. }
  753. ret = validate_vsi_array_indexes(inst, vsi);
  754. if (ret) {
  755. mtk_vcodec_err(inst, "Invalid values from VPU.");
  756. goto DECODE_ERROR;
  757. }
  758. if (vsi->resolution_changed) {
  759. if (!vp9_alloc_work_buf(inst)) {
  760. ret = -EINVAL;
  761. goto DECODE_ERROR;
  762. }
  763. }
  764. if (vsi->sf_frm_cnt > 0) {
  765. cur_fb = &vsi->sf_ref_fb[vsi->sf_next_ref_fb_idx].fb;
  766. if (vsi->sf_frm_idx < vsi->sf_frm_cnt)
  767. inst->cur_fb = cur_fb;
  768. else
  769. inst->cur_fb = fb;
  770. } else {
  771. inst->cur_fb = fb;
  772. }
  773. vsi->frm_bufs[vsi->new_fb_idx].buf.fb = inst->cur_fb;
  774. if (!vp9_is_sf_ref_fb(inst, inst->cur_fb))
  775. vp9_add_to_fb_use_list(inst, inst->cur_fb);
  776. mtk_vcodec_debug(inst, "[#pic %d]", vsi->frm_num);
  777. if (vsi->show_existing_frame)
  778. mtk_vcodec_debug(inst,
  779. "drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
  780. vsi->new_fb_idx, vsi->frm_to_show_idx);
  781. if (vsi->show_existing_frame && (vsi->frm_to_show_idx <
  782. VP9_MAX_FRM_BUF_NUM)) {
  783. mtk_vcodec_err(inst,
  784. "Skip Decode drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
  785. vsi->new_fb_idx, vsi->frm_to_show_idx);
  786. vp9_ref_cnt_fb(inst, &vsi->new_fb_idx,
  787. vsi->frm_to_show_idx);
  788. ret = -EINVAL;
  789. goto DECODE_ERROR;
  790. }
  791. /* VPU assign the buffer pointer in its address space,
  792. * reassign here
  793. */
  794. for (i = 0; i < ARRAY_SIZE(vsi->frm_refs); i++) {
  795. unsigned int idx = vsi->frm_refs[i].idx;
  796. vsi->frm_refs[i].buf = &vsi->frm_bufs[idx].buf;
  797. }
  798. if (vsi->resolution_changed) {
  799. *res_chg = true;
  800. mtk_vcodec_debug(inst, "VDEC_ST_RESOLUTION_CHANGED");
  801. ret = 0;
  802. goto DECODE_ERROR;
  803. }
  804. if (vp9_decode_end_proc(inst) != true) {
  805. mtk_vcodec_err(inst, "vp9_decode_end_proc");
  806. ret = -EINVAL;
  807. goto DECODE_ERROR;
  808. }
  809. if (vp9_is_last_sub_frm(inst))
  810. break;
  811. }
  812. inst->total_frm_cnt++;
  813. DECODE_ERROR:
  814. if (ret < 0)
  815. vp9_add_to_fb_free_list(inst, fb);
  816. return ret;
  817. }
  818. static void get_crop_info(struct vdec_vp9_inst *inst, struct v4l2_rect *cr)
  819. {
  820. cr->left = 0;
  821. cr->top = 0;
  822. cr->width = inst->vsi->pic_w;
  823. cr->height = inst->vsi->pic_h;
  824. mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d\n",
  825. cr->left, cr->top, cr->width, cr->height);
  826. }
  827. static int vdec_vp9_get_param(unsigned long h_vdec,
  828. enum vdec_get_param_type type, void *out)
  829. {
  830. struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
  831. int ret = 0;
  832. switch (type) {
  833. case GET_PARAM_DISP_FRAME_BUFFER:
  834. get_disp_fb(inst, out);
  835. break;
  836. case GET_PARAM_FREE_FRAME_BUFFER:
  837. get_free_fb(inst, out);
  838. break;
  839. case GET_PARAM_PIC_INFO:
  840. get_pic_info(inst, out);
  841. break;
  842. case GET_PARAM_DPB_SIZE:
  843. *((unsigned int *)out) = MAX_VP9_DPB_SIZE;
  844. break;
  845. case GET_PARAM_CROP_INFO:
  846. get_crop_info(inst, out);
  847. break;
  848. default:
  849. mtk_vcodec_err(inst, "not supported param type %d", type);
  850. ret = -EINVAL;
  851. break;
  852. }
  853. return ret;
  854. }
  855. static struct vdec_common_if vdec_vp9_if = {
  856. .init = vdec_vp9_init,
  857. .decode = vdec_vp9_decode,
  858. .get_param = vdec_vp9_get_param,
  859. .deinit = vdec_vp9_deinit,
  860. };
  861. struct vdec_common_if *get_vp9_dec_comm_if(void);
  862. struct vdec_common_if *get_vp9_dec_comm_if(void)
  863. {
  864. return &vdec_vp9_if;
  865. }