mtk_vcodec_dec.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. /*
  2. * Copyright (c) 2016 MediaTek Inc.
  3. * Author: PC Chen <pc.chen@mediatek.com>
  4. * Tiffany Lin <tiffany.lin@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <media/v4l2-event.h>
  16. #include <media/v4l2-mem2mem.h>
  17. #include <media/videobuf2-dma-contig.h>
  18. #include "mtk_vcodec_drv.h"
  19. #include "mtk_vcodec_dec.h"
  20. #include "mtk_vcodec_intr.h"
  21. #include "mtk_vcodec_util.h"
  22. #include "vdec_drv_if.h"
  23. #include "mtk_vcodec_dec_pm.h"
  24. #define OUT_FMT_IDX 0
  25. #define CAP_FMT_IDX 3
  26. #define MTK_VDEC_MIN_W 64U
  27. #define MTK_VDEC_MIN_H 64U
  28. #define DFT_CFG_WIDTH MTK_VDEC_MIN_W
  29. #define DFT_CFG_HEIGHT MTK_VDEC_MIN_H
  30. static struct mtk_video_fmt mtk_video_formats[] = {
  31. {
  32. .fourcc = V4L2_PIX_FMT_H264,
  33. .type = MTK_FMT_DEC,
  34. .num_planes = 1,
  35. },
  36. {
  37. .fourcc = V4L2_PIX_FMT_VP8,
  38. .type = MTK_FMT_DEC,
  39. .num_planes = 1,
  40. },
  41. {
  42. .fourcc = V4L2_PIX_FMT_VP9,
  43. .type = MTK_FMT_DEC,
  44. .num_planes = 1,
  45. },
  46. {
  47. .fourcc = V4L2_PIX_FMT_MT21C,
  48. .type = MTK_FMT_FRAME,
  49. .num_planes = 2,
  50. },
  51. };
  52. static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
  53. {
  54. .fourcc = V4L2_PIX_FMT_H264,
  55. .stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
  56. MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
  57. },
  58. {
  59. .fourcc = V4L2_PIX_FMT_VP8,
  60. .stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
  61. MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
  62. },
  63. {
  64. .fourcc = V4L2_PIX_FMT_VP9,
  65. .stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
  66. MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
  67. },
  68. };
  69. #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
  70. #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
  71. static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
  72. {
  73. struct mtk_video_fmt *fmt;
  74. unsigned int k;
  75. for (k = 0; k < NUM_FORMATS; k++) {
  76. fmt = &mtk_video_formats[k];
  77. if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
  78. return fmt;
  79. }
  80. return NULL;
  81. }
  82. static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
  83. enum v4l2_buf_type type)
  84. {
  85. if (V4L2_TYPE_IS_OUTPUT(type))
  86. return &ctx->q_data[MTK_Q_DATA_SRC];
  87. return &ctx->q_data[MTK_Q_DATA_DST];
  88. }
  89. /*
  90. * This function tries to clean all display buffers, the buffers will return
  91. * in display order.
  92. * Note the buffers returned from codec driver may still be in driver's
  93. * reference list.
  94. */
  95. static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
  96. {
  97. struct vdec_fb *disp_frame_buffer = NULL;
  98. struct mtk_video_dec_buf *dstbuf;
  99. mtk_v4l2_debug(3, "[%d]", ctx->id);
  100. if (vdec_if_get_param(ctx,
  101. GET_PARAM_DISP_FRAME_BUFFER,
  102. &disp_frame_buffer)) {
  103. mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
  104. ctx->id);
  105. return NULL;
  106. }
  107. if (disp_frame_buffer == NULL) {
  108. mtk_v4l2_debug(3, "No display frame buffer");
  109. return NULL;
  110. }
  111. dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
  112. frame_buffer);
  113. mutex_lock(&ctx->lock);
  114. if (dstbuf->used) {
  115. vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
  116. ctx->picinfo.y_bs_sz);
  117. vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
  118. ctx->picinfo.c_bs_sz);
  119. dstbuf->ready_to_display = true;
  120. mtk_v4l2_debug(2,
  121. "[%d]status=%x queue id=%d to done_list %d",
  122. ctx->id, disp_frame_buffer->status,
  123. dstbuf->vb.vb2_buf.index,
  124. dstbuf->queued_in_vb2);
  125. v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
  126. ctx->decoded_frame_cnt++;
  127. }
  128. mutex_unlock(&ctx->lock);
  129. return &dstbuf->vb.vb2_buf;
  130. }
  131. /*
  132. * This function tries to clean all capture buffers that are not used as
  133. * reference buffers by codec driver any more
  134. * In this case, we need re-queue buffer to vb2 buffer if user space
  135. * already returns this buffer to v4l2 or this buffer is just the output of
  136. * previous sps/pps/resolution change decode, or do nothing if user
  137. * space still owns this buffer
  138. */
  139. static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
  140. {
  141. struct mtk_video_dec_buf *dstbuf;
  142. struct vdec_fb *free_frame_buffer = NULL;
  143. if (vdec_if_get_param(ctx,
  144. GET_PARAM_FREE_FRAME_BUFFER,
  145. &free_frame_buffer)) {
  146. mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->id);
  147. return NULL;
  148. }
  149. if (free_frame_buffer == NULL) {
  150. mtk_v4l2_debug(3, " No free frame buffer");
  151. return NULL;
  152. }
  153. mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
  154. ctx->id, free_frame_buffer);
  155. dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf,
  156. frame_buffer);
  157. mutex_lock(&ctx->lock);
  158. if (dstbuf->used) {
  159. if ((dstbuf->queued_in_vb2) &&
  160. (dstbuf->queued_in_v4l2) &&
  161. (free_frame_buffer->status == FB_ST_FREE)) {
  162. /*
  163. * After decode sps/pps or non-display buffer, we don't
  164. * need to return capture buffer to user space, but
  165. * just re-queue this capture buffer to vb2 queue.
  166. * This reduce overheads that dq/q unused capture
  167. * buffer. In this case, queued_in_vb2 = true.
  168. */
  169. mtk_v4l2_debug(2,
  170. "[%d]status=%x queue id=%d to rdy_queue %d",
  171. ctx->id, free_frame_buffer->status,
  172. dstbuf->vb.vb2_buf.index,
  173. dstbuf->queued_in_vb2);
  174. v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
  175. } else if ((dstbuf->queued_in_vb2 == false) &&
  176. (dstbuf->queued_in_v4l2 == true)) {
  177. /*
  178. * If buffer in v4l2 driver but not in vb2 queue yet,
  179. * and we get this buffer from free_list, it means
  180. * that codec driver do not use this buffer as
  181. * reference buffer anymore. We should q buffer to vb2
  182. * queue, so later work thread could get this buffer
  183. * for decode. In this case, queued_in_vb2 = false
  184. * means this buffer is not from previous decode
  185. * output.
  186. */
  187. mtk_v4l2_debug(2,
  188. "[%d]status=%x queue id=%d to rdy_queue",
  189. ctx->id, free_frame_buffer->status,
  190. dstbuf->vb.vb2_buf.index);
  191. v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
  192. dstbuf->queued_in_vb2 = true;
  193. } else {
  194. /*
  195. * Codec driver do not need to reference this capture
  196. * buffer and this buffer is not in v4l2 driver.
  197. * Then we don't need to do any thing, just add log when
  198. * we need to debug buffer flow.
  199. * When this buffer q from user space, it could
  200. * directly q to vb2 buffer
  201. */
  202. mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
  203. ctx->id, free_frame_buffer->status,
  204. dstbuf->vb.vb2_buf.index,
  205. dstbuf->queued_in_vb2,
  206. dstbuf->queued_in_v4l2);
  207. }
  208. dstbuf->used = false;
  209. }
  210. mutex_unlock(&ctx->lock);
  211. return &dstbuf->vb.vb2_buf;
  212. }
  213. static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
  214. {
  215. struct vb2_buffer *framptr;
  216. do {
  217. framptr = get_display_buffer(ctx);
  218. } while (framptr);
  219. }
  220. static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
  221. {
  222. struct vb2_buffer *framptr;
  223. do {
  224. framptr = get_free_buffer(ctx);
  225. } while (framptr);
  226. }
  227. static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
  228. {
  229. static const struct v4l2_event ev_src_ch = {
  230. .type = V4L2_EVENT_SOURCE_CHANGE,
  231. .u.src_change.changes =
  232. V4L2_EVENT_SRC_CH_RESOLUTION,
  233. };
  234. mtk_v4l2_debug(1, "[%d]", ctx->id);
  235. v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
  236. }
  237. static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
  238. {
  239. bool res_chg;
  240. int ret = 0;
  241. ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
  242. if (ret)
  243. mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
  244. clean_display_buffer(ctx);
  245. clean_free_buffer(ctx);
  246. }
  247. static void mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
  248. {
  249. unsigned int dpbsize = 0;
  250. int ret;
  251. if (vdec_if_get_param(ctx,
  252. GET_PARAM_PIC_INFO,
  253. &ctx->last_decoded_picinfo)) {
  254. mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
  255. ctx->id);
  256. return;
  257. }
  258. if (ctx->last_decoded_picinfo.pic_w == 0 ||
  259. ctx->last_decoded_picinfo.pic_h == 0 ||
  260. ctx->last_decoded_picinfo.buf_w == 0 ||
  261. ctx->last_decoded_picinfo.buf_h == 0) {
  262. mtk_v4l2_err("Cannot get correct pic info");
  263. return;
  264. }
  265. if ((ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w) ||
  266. (ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h))
  267. return;
  268. mtk_v4l2_debug(1,
  269. "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
  270. ctx->id, ctx->last_decoded_picinfo.pic_w,
  271. ctx->last_decoded_picinfo.pic_h,
  272. ctx->picinfo.pic_w, ctx->picinfo.pic_h,
  273. ctx->last_decoded_picinfo.buf_w,
  274. ctx->last_decoded_picinfo.buf_h);
  275. ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
  276. if (dpbsize == 0)
  277. mtk_v4l2_err("Incorrect dpb size, ret=%d", ret);
  278. ctx->dpb_size = dpbsize;
  279. }
  280. static void mtk_vdec_worker(struct work_struct *work)
  281. {
  282. struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
  283. decode_work);
  284. struct mtk_vcodec_dev *dev = ctx->dev;
  285. struct vb2_buffer *src_buf, *dst_buf;
  286. struct mtk_vcodec_mem buf;
  287. struct vdec_fb *pfb;
  288. bool res_chg = false;
  289. int ret;
  290. struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
  291. struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
  292. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  293. if (src_buf == NULL) {
  294. v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
  295. mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id);
  296. return;
  297. }
  298. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  299. if (dst_buf == NULL) {
  300. v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
  301. mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
  302. return;
  303. }
  304. src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
  305. src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_dec_buf, vb);
  306. dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
  307. dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_dec_buf, vb);
  308. buf.va = vb2_plane_vaddr(src_buf, 0);
  309. buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  310. buf.size = (size_t)src_buf->planes[0].bytesused;
  311. if (!buf.va) {
  312. v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
  313. mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
  314. ctx->id, src_buf->index);
  315. return;
  316. }
  317. pfb = &dst_buf_info->frame_buffer;
  318. pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
  319. pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  320. pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
  321. pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
  322. pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
  323. pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
  324. pfb->status = 0;
  325. mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
  326. mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
  327. ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
  328. mtk_v4l2_debug(3,
  329. "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
  330. dst_buf->index, pfb,
  331. pfb->base_y.va, &pfb->base_y.dma_addr,
  332. &pfb->base_c.dma_addr, pfb->base_y.size);
  333. if (src_buf_info->lastframe) {
  334. /* update src buf status */
  335. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  336. src_buf_info->lastframe = false;
  337. v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
  338. /* update dst buf status */
  339. dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  340. dst_buf_info->used = false;
  341. vdec_if_decode(ctx, NULL, NULL, &res_chg);
  342. clean_display_buffer(ctx);
  343. vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
  344. vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
  345. v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
  346. clean_free_buffer(ctx);
  347. v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
  348. return;
  349. }
  350. dst_buf_info->vb.vb2_buf.timestamp
  351. = src_buf_info->vb.vb2_buf.timestamp;
  352. dst_buf_info->vb.timecode
  353. = src_buf_info->vb.timecode;
  354. mutex_lock(&ctx->lock);
  355. dst_buf_info->used = true;
  356. mutex_unlock(&ctx->lock);
  357. src_buf_info->used = true;
  358. ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
  359. if (ret) {
  360. mtk_v4l2_err(
  361. " <===[%d], src_buf[%d]%d sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
  362. ctx->id,
  363. src_buf->index,
  364. src_buf_info->lastframe,
  365. buf.size,
  366. src_buf_info->vb.vb2_buf.timestamp,
  367. dst_buf->index,
  368. ret, res_chg);
  369. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  370. v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_ERROR);
  371. } else if (res_chg == false) {
  372. /*
  373. * we only return src buffer with VB2_BUF_STATE_DONE
  374. * when decode success without resolution change
  375. */
  376. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  377. v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
  378. }
  379. dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  380. clean_display_buffer(ctx);
  381. clean_free_buffer(ctx);
  382. if (!ret && res_chg) {
  383. mtk_vdec_pic_info_update(ctx);
  384. /*
  385. * On encountering a resolution change in the stream.
  386. * The driver must first process and decode all
  387. * remaining buffers from before the resolution change
  388. * point, so call flush decode here
  389. */
  390. mtk_vdec_flush_decoder(ctx);
  391. /*
  392. * After all buffers containing decoded frames from
  393. * before the resolution change point ready to be
  394. * dequeued on the CAPTURE queue, the driver sends a
  395. * V4L2_EVENT_SOURCE_CHANGE event for source change
  396. * type V4L2_EVENT_SRC_CH_RESOLUTION
  397. */
  398. mtk_vdec_queue_res_chg_event(ctx);
  399. }
  400. v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
  401. }
  402. void mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
  403. {
  404. mutex_unlock(&ctx->dev->dec_mutex);
  405. }
  406. void mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
  407. {
  408. mutex_lock(&ctx->dev->dec_mutex);
  409. }
  410. void mtk_vcodec_dec_release(struct mtk_vcodec_ctx *ctx)
  411. {
  412. vdec_if_deinit(ctx);
  413. ctx->state = MTK_STATE_FREE;
  414. }
  415. void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
  416. {
  417. struct mtk_q_data *q_data;
  418. ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
  419. ctx->fh.m2m_ctx = ctx->m2m_ctx;
  420. ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
  421. INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
  422. ctx->colorspace = V4L2_COLORSPACE_REC709;
  423. ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  424. ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
  425. ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  426. q_data = &ctx->q_data[MTK_Q_DATA_SRC];
  427. memset(q_data, 0, sizeof(struct mtk_q_data));
  428. q_data->visible_width = DFT_CFG_WIDTH;
  429. q_data->visible_height = DFT_CFG_HEIGHT;
  430. q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
  431. q_data->field = V4L2_FIELD_NONE;
  432. q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
  433. q_data->bytesperline[0] = 0;
  434. q_data = &ctx->q_data[MTK_Q_DATA_DST];
  435. memset(q_data, 0, sizeof(struct mtk_q_data));
  436. q_data->visible_width = DFT_CFG_WIDTH;
  437. q_data->visible_height = DFT_CFG_HEIGHT;
  438. q_data->coded_width = DFT_CFG_WIDTH;
  439. q_data->coded_height = DFT_CFG_HEIGHT;
  440. q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
  441. q_data->field = V4L2_FIELD_NONE;
  442. v4l_bound_align_image(&q_data->coded_width,
  443. MTK_VDEC_MIN_W,
  444. MTK_VDEC_MAX_W, 4,
  445. &q_data->coded_height,
  446. MTK_VDEC_MIN_H,
  447. MTK_VDEC_MAX_H, 5, 6);
  448. q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
  449. q_data->bytesperline[0] = q_data->coded_width;
  450. q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
  451. q_data->bytesperline[1] = q_data->coded_width;
  452. }
  453. static int vidioc_vdec_qbuf(struct file *file, void *priv,
  454. struct v4l2_buffer *buf)
  455. {
  456. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  457. struct vb2_queue *vq;
  458. struct vb2_buffer *vb;
  459. struct mtk_video_dec_buf *mtkbuf;
  460. struct vb2_v4l2_buffer *vb2_v4l2;
  461. if (ctx->state == MTK_STATE_ABORT) {
  462. mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
  463. ctx->id);
  464. return -EIO;
  465. }
  466. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
  467. if (buf->index >= vq->num_buffers) {
  468. mtk_v4l2_debug(1, "buffer index %d out of range", buf->index);
  469. return -EINVAL;
  470. }
  471. vb = vq->bufs[buf->index];
  472. vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
  473. mtkbuf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
  474. if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
  475. (buf->m.planes[0].bytesused == 0)) {
  476. mtkbuf->lastframe = true;
  477. mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
  478. ctx->id, buf->type, buf->index,
  479. mtkbuf->lastframe, buf->bytesused,
  480. buf->m.planes[0].bytesused, buf->length,
  481. vb);
  482. }
  483. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  484. }
  485. static int vidioc_vdec_dqbuf(struct file *file, void *priv,
  486. struct v4l2_buffer *buf)
  487. {
  488. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  489. if (ctx->state == MTK_STATE_ABORT) {
  490. mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
  491. ctx->id);
  492. return -EIO;
  493. }
  494. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  495. }
  496. static int vidioc_vdec_querycap(struct file *file, void *priv,
  497. struct v4l2_capability *cap)
  498. {
  499. strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
  500. strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
  501. strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
  502. return 0;
  503. }
  504. static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
  505. const struct v4l2_event_subscription *sub)
  506. {
  507. switch (sub->type) {
  508. case V4L2_EVENT_EOS:
  509. return v4l2_event_subscribe(fh, sub, 2, NULL);
  510. case V4L2_EVENT_SOURCE_CHANGE:
  511. return v4l2_src_change_event_subscribe(fh, sub);
  512. default:
  513. return v4l2_ctrl_subscribe_event(fh, sub);
  514. }
  515. }
  516. static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt)
  517. {
  518. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  519. int i;
  520. pix_fmt_mp->field = V4L2_FIELD_NONE;
  521. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  522. pix_fmt_mp->num_planes = 1;
  523. pix_fmt_mp->plane_fmt[0].bytesperline = 0;
  524. } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  525. int tmp_w, tmp_h;
  526. pix_fmt_mp->height = clamp(pix_fmt_mp->height,
  527. MTK_VDEC_MIN_H,
  528. MTK_VDEC_MAX_H);
  529. pix_fmt_mp->width = clamp(pix_fmt_mp->width,
  530. MTK_VDEC_MIN_W,
  531. MTK_VDEC_MAX_W);
  532. /*
  533. * Find next closer width align 64, heign align 64, size align
  534. * 64 rectangle
  535. * Note: This only get default value, the real HW needed value
  536. * only available when ctx in MTK_STATE_HEADER state
  537. */
  538. tmp_w = pix_fmt_mp->width;
  539. tmp_h = pix_fmt_mp->height;
  540. v4l_bound_align_image(&pix_fmt_mp->width,
  541. MTK_VDEC_MIN_W,
  542. MTK_VDEC_MAX_W, 6,
  543. &pix_fmt_mp->height,
  544. MTK_VDEC_MIN_H,
  545. MTK_VDEC_MAX_H, 6, 9);
  546. if (pix_fmt_mp->width < tmp_w &&
  547. (pix_fmt_mp->width + 64) <= MTK_VDEC_MAX_W)
  548. pix_fmt_mp->width += 64;
  549. if (pix_fmt_mp->height < tmp_h &&
  550. (pix_fmt_mp->height + 64) <= MTK_VDEC_MAX_H)
  551. pix_fmt_mp->height += 64;
  552. mtk_v4l2_debug(0,
  553. "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
  554. tmp_w, tmp_h, pix_fmt_mp->width,
  555. pix_fmt_mp->height,
  556. pix_fmt_mp->width * pix_fmt_mp->height);
  557. pix_fmt_mp->num_planes = fmt->num_planes;
  558. pix_fmt_mp->plane_fmt[0].sizeimage =
  559. pix_fmt_mp->width * pix_fmt_mp->height;
  560. pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
  561. if (pix_fmt_mp->num_planes == 2) {
  562. pix_fmt_mp->plane_fmt[1].sizeimage =
  563. (pix_fmt_mp->width * pix_fmt_mp->height) / 2;
  564. pix_fmt_mp->plane_fmt[1].bytesperline =
  565. pix_fmt_mp->width;
  566. }
  567. }
  568. for (i = 0; i < pix_fmt_mp->num_planes; i++)
  569. memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
  570. sizeof(pix_fmt_mp->plane_fmt[0].reserved));
  571. pix_fmt_mp->flags = 0;
  572. memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved));
  573. return 0;
  574. }
  575. static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
  576. struct v4l2_format *f)
  577. {
  578. struct mtk_video_fmt *fmt;
  579. fmt = mtk_vdec_find_format(f);
  580. if (!fmt) {
  581. f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
  582. fmt = mtk_vdec_find_format(f);
  583. }
  584. return vidioc_try_fmt(f, fmt);
  585. }
  586. static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
  587. struct v4l2_format *f)
  588. {
  589. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  590. struct mtk_video_fmt *fmt;
  591. fmt = mtk_vdec_find_format(f);
  592. if (!fmt) {
  593. f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
  594. fmt = mtk_vdec_find_format(f);
  595. }
  596. if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
  597. mtk_v4l2_err("sizeimage of output format must be given");
  598. return -EINVAL;
  599. }
  600. return vidioc_try_fmt(f, fmt);
  601. }
  602. static int vidioc_vdec_g_selection(struct file *file, void *priv,
  603. struct v4l2_selection *s)
  604. {
  605. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  606. struct mtk_q_data *q_data;
  607. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  608. return -EINVAL;
  609. q_data = &ctx->q_data[MTK_Q_DATA_DST];
  610. switch (s->target) {
  611. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  612. s->r.left = 0;
  613. s->r.top = 0;
  614. s->r.width = ctx->picinfo.pic_w;
  615. s->r.height = ctx->picinfo.pic_h;
  616. break;
  617. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  618. s->r.left = 0;
  619. s->r.top = 0;
  620. s->r.width = ctx->picinfo.buf_w;
  621. s->r.height = ctx->picinfo.buf_h;
  622. break;
  623. case V4L2_SEL_TGT_COMPOSE:
  624. if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
  625. /* set to default value if header info not ready yet*/
  626. s->r.left = 0;
  627. s->r.top = 0;
  628. s->r.width = q_data->visible_width;
  629. s->r.height = q_data->visible_height;
  630. }
  631. break;
  632. default:
  633. return -EINVAL;
  634. }
  635. if (ctx->state < MTK_STATE_HEADER) {
  636. /* set to default value if header info not ready yet*/
  637. s->r.left = 0;
  638. s->r.top = 0;
  639. s->r.width = q_data->visible_width;
  640. s->r.height = q_data->visible_height;
  641. return 0;
  642. }
  643. return 0;
  644. }
  645. static int vidioc_vdec_s_selection(struct file *file, void *priv,
  646. struct v4l2_selection *s)
  647. {
  648. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  649. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  650. return -EINVAL;
  651. switch (s->target) {
  652. case V4L2_SEL_TGT_COMPOSE:
  653. s->r.left = 0;
  654. s->r.top = 0;
  655. s->r.width = ctx->picinfo.pic_w;
  656. s->r.height = ctx->picinfo.pic_h;
  657. break;
  658. default:
  659. return -EINVAL;
  660. }
  661. return 0;
  662. }
  663. static int vidioc_vdec_s_fmt(struct file *file, void *priv,
  664. struct v4l2_format *f)
  665. {
  666. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  667. struct v4l2_pix_format_mplane *pix_mp;
  668. struct mtk_q_data *q_data;
  669. int ret = 0;
  670. struct mtk_video_fmt *fmt;
  671. mtk_v4l2_debug(3, "[%d]", ctx->id);
  672. q_data = mtk_vdec_get_q_data(ctx, f->type);
  673. if (!q_data)
  674. return -EINVAL;
  675. pix_mp = &f->fmt.pix_mp;
  676. if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
  677. vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
  678. mtk_v4l2_err("out_q_ctx buffers already requested");
  679. ret = -EBUSY;
  680. }
  681. if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
  682. vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
  683. mtk_v4l2_err("cap_q_ctx buffers already requested");
  684. ret = -EBUSY;
  685. }
  686. fmt = mtk_vdec_find_format(f);
  687. if (fmt == NULL) {
  688. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  689. f->fmt.pix.pixelformat =
  690. mtk_video_formats[OUT_FMT_IDX].fourcc;
  691. fmt = mtk_vdec_find_format(f);
  692. } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  693. f->fmt.pix.pixelformat =
  694. mtk_video_formats[CAP_FMT_IDX].fourcc;
  695. fmt = mtk_vdec_find_format(f);
  696. }
  697. }
  698. q_data->fmt = fmt;
  699. vidioc_try_fmt(f, q_data->fmt);
  700. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  701. q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
  702. q_data->coded_width = pix_mp->width;
  703. q_data->coded_height = pix_mp->height;
  704. ctx->colorspace = f->fmt.pix_mp.colorspace;
  705. ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
  706. ctx->quantization = f->fmt.pix_mp.quantization;
  707. ctx->xfer_func = f->fmt.pix_mp.xfer_func;
  708. if (ctx->state == MTK_STATE_FREE) {
  709. ret = vdec_if_init(ctx, q_data->fmt->fourcc);
  710. if (ret) {
  711. mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
  712. ctx->id, ret);
  713. return -EINVAL;
  714. }
  715. ctx->state = MTK_STATE_INIT;
  716. }
  717. }
  718. return 0;
  719. }
  720. static int vidioc_enum_framesizes(struct file *file, void *priv,
  721. struct v4l2_frmsizeenum *fsize)
  722. {
  723. int i = 0;
  724. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  725. if (fsize->index != 0)
  726. return -EINVAL;
  727. for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
  728. if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
  729. continue;
  730. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  731. fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
  732. if (!(ctx->dev->dec_capability &
  733. VCODEC_CAPABILITY_4K_DISABLED)) {
  734. mtk_v4l2_debug(3, "4K is enabled");
  735. fsize->stepwise.max_width =
  736. VCODEC_DEC_4K_CODED_WIDTH;
  737. fsize->stepwise.max_height =
  738. VCODEC_DEC_4K_CODED_HEIGHT;
  739. }
  740. mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
  741. ctx->dev->dec_capability,
  742. fsize->stepwise.min_width,
  743. fsize->stepwise.max_width,
  744. fsize->stepwise.step_width,
  745. fsize->stepwise.min_height,
  746. fsize->stepwise.max_height,
  747. fsize->stepwise.step_height);
  748. return 0;
  749. }
  750. return -EINVAL;
  751. }
  752. static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
  753. {
  754. struct mtk_video_fmt *fmt;
  755. int i, j = 0;
  756. for (i = 0; i < NUM_FORMATS; i++) {
  757. if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
  758. continue;
  759. if (!output_queue &&
  760. (mtk_video_formats[i].type != MTK_FMT_FRAME))
  761. continue;
  762. if (j == f->index)
  763. break;
  764. ++j;
  765. }
  766. if (i == NUM_FORMATS)
  767. return -EINVAL;
  768. fmt = &mtk_video_formats[i];
  769. f->pixelformat = fmt->fourcc;
  770. return 0;
  771. }
  772. static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
  773. struct v4l2_fmtdesc *f)
  774. {
  775. return vidioc_enum_fmt(f, false);
  776. }
  777. static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
  778. struct v4l2_fmtdesc *f)
  779. {
  780. return vidioc_enum_fmt(f, true);
  781. }
  782. static int vidioc_vdec_g_fmt(struct file *file, void *priv,
  783. struct v4l2_format *f)
  784. {
  785. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  786. struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
  787. struct vb2_queue *vq;
  788. struct mtk_q_data *q_data;
  789. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  790. if (!vq) {
  791. mtk_v4l2_err("no vb2 queue for type=%d", f->type);
  792. return -EINVAL;
  793. }
  794. q_data = mtk_vdec_get_q_data(ctx, f->type);
  795. pix_mp->field = V4L2_FIELD_NONE;
  796. pix_mp->colorspace = ctx->colorspace;
  797. pix_mp->ycbcr_enc = ctx->ycbcr_enc;
  798. pix_mp->quantization = ctx->quantization;
  799. pix_mp->xfer_func = ctx->xfer_func;
  800. if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
  801. (ctx->state >= MTK_STATE_HEADER)) {
  802. /* Until STREAMOFF is called on the CAPTURE queue
  803. * (acknowledging the event), the driver operates as if
  804. * the resolution hasn't changed yet.
  805. * So we just return picinfo yet, and update picinfo in
  806. * stop_streaming hook function
  807. */
  808. q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
  809. ctx->picinfo.y_len_sz;
  810. q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
  811. ctx->picinfo.c_len_sz;
  812. q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
  813. q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
  814. q_data->coded_width = ctx->picinfo.buf_w;
  815. q_data->coded_height = ctx->picinfo.buf_h;
  816. /*
  817. * Width and height are set to the dimensions
  818. * of the movie, the buffer is bigger and
  819. * further processing stages should crop to this
  820. * rectangle.
  821. */
  822. pix_mp->width = q_data->coded_width;
  823. pix_mp->height = q_data->coded_height;
  824. /*
  825. * Set pixelformat to the format in which mt vcodec
  826. * outputs the decoded frame
  827. */
  828. pix_mp->num_planes = q_data->fmt->num_planes;
  829. pix_mp->pixelformat = q_data->fmt->fourcc;
  830. pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
  831. pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
  832. pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
  833. pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
  834. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  835. /*
  836. * This is run on OUTPUT
  837. * The buffer contains compressed image
  838. * so width and height have no meaning.
  839. * Assign value here to pass v4l2-compliance test
  840. */
  841. pix_mp->width = q_data->visible_width;
  842. pix_mp->height = q_data->visible_height;
  843. pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
  844. pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
  845. pix_mp->pixelformat = q_data->fmt->fourcc;
  846. pix_mp->num_planes = q_data->fmt->num_planes;
  847. } else {
  848. pix_mp->width = q_data->coded_width;
  849. pix_mp->height = q_data->coded_height;
  850. pix_mp->num_planes = q_data->fmt->num_planes;
  851. pix_mp->pixelformat = q_data->fmt->fourcc;
  852. pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
  853. pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
  854. pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
  855. pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
  856. mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
  857. ctx->id, f->type, ctx->state);
  858. }
  859. return 0;
  860. }
  861. static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
  862. unsigned int *nbuffers,
  863. unsigned int *nplanes,
  864. unsigned int sizes[],
  865. struct device *alloc_devs[])
  866. {
  867. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
  868. struct mtk_q_data *q_data;
  869. unsigned int i;
  870. q_data = mtk_vdec_get_q_data(ctx, vq->type);
  871. if (q_data == NULL) {
  872. mtk_v4l2_err("vq->type=%d err\n", vq->type);
  873. return -EINVAL;
  874. }
  875. if (*nplanes) {
  876. for (i = 0; i < *nplanes; i++) {
  877. if (sizes[i] < q_data->sizeimage[i])
  878. return -EINVAL;
  879. }
  880. } else {
  881. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  882. *nplanes = 2;
  883. else
  884. *nplanes = 1;
  885. for (i = 0; i < *nplanes; i++)
  886. sizes[i] = q_data->sizeimage[i];
  887. }
  888. mtk_v4l2_debug(1,
  889. "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
  890. ctx->id, vq->type, *nplanes, *nbuffers,
  891. sizes[0], sizes[1]);
  892. return 0;
  893. }
  894. static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
  895. {
  896. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  897. struct mtk_q_data *q_data;
  898. int i;
  899. mtk_v4l2_debug(3, "[%d] (%d) id=%d",
  900. ctx->id, vb->vb2_queue->type, vb->index);
  901. q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
  902. for (i = 0; i < q_data->fmt->num_planes; i++) {
  903. if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
  904. mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
  905. i, vb2_plane_size(vb, i),
  906. q_data->sizeimage[i]);
  907. }
  908. }
  909. return 0;
  910. }
  911. static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
  912. {
  913. struct vb2_buffer *src_buf;
  914. struct mtk_vcodec_mem src_mem;
  915. bool res_chg = false;
  916. int ret = 0;
  917. unsigned int dpbsize = 1;
  918. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  919. struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
  920. struct vb2_v4l2_buffer, vb2_buf);
  921. struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
  922. struct mtk_video_dec_buf, vb);
  923. mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
  924. ctx->id, vb->vb2_queue->type,
  925. vb->index, vb);
  926. /*
  927. * check if this buffer is ready to be used after decode
  928. */
  929. if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  930. mutex_lock(&ctx->lock);
  931. if (buf->used == false) {
  932. v4l2_m2m_buf_queue(ctx->m2m_ctx,
  933. to_vb2_v4l2_buffer(vb));
  934. buf->queued_in_vb2 = true;
  935. buf->queued_in_v4l2 = true;
  936. buf->ready_to_display = false;
  937. } else {
  938. buf->queued_in_vb2 = false;
  939. buf->queued_in_v4l2 = true;
  940. buf->ready_to_display = false;
  941. }
  942. mutex_unlock(&ctx->lock);
  943. return;
  944. }
  945. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
  946. if (ctx->state != MTK_STATE_INIT) {
  947. mtk_v4l2_debug(3, "[%d] already init driver %d",
  948. ctx->id, ctx->state);
  949. return;
  950. }
  951. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  952. if (!src_buf) {
  953. mtk_v4l2_err("No src buffer");
  954. return;
  955. }
  956. src_mem.va = vb2_plane_vaddr(src_buf, 0);
  957. src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  958. src_mem.size = (size_t)src_buf->planes[0].bytesused;
  959. mtk_v4l2_debug(2,
  960. "[%d] buf id=%d va=%p dma=%pad size=%zx",
  961. ctx->id, src_buf->index,
  962. src_mem.va, &src_mem.dma_addr,
  963. src_mem.size);
  964. ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
  965. if (ret || !res_chg) {
  966. /*
  967. * fb == NULL menas to parse SPS/PPS header or
  968. * resolution info in src_mem. Decode can fail
  969. * if there is no SPS header or picture info
  970. * in bs
  971. */
  972. int log_level = ret ? 0 : 1;
  973. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  974. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
  975. VB2_BUF_STATE_DONE);
  976. mtk_v4l2_debug(log_level,
  977. "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
  978. ctx->id, src_buf->index,
  979. src_mem.size, ret, res_chg);
  980. return;
  981. }
  982. if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
  983. mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
  984. ctx->id);
  985. return;
  986. }
  987. ctx->last_decoded_picinfo = ctx->picinfo;
  988. ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
  989. ctx->picinfo.y_bs_sz +
  990. ctx->picinfo.y_len_sz;
  991. ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
  992. ctx->picinfo.buf_w;
  993. ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
  994. ctx->picinfo.c_bs_sz +
  995. ctx->picinfo.c_len_sz;
  996. ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] = ctx->picinfo.buf_w;
  997. mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
  998. ctx->id,
  999. ctx->picinfo.buf_w, ctx->picinfo.buf_h,
  1000. ctx->picinfo.pic_w, ctx->picinfo.pic_h,
  1001. ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
  1002. ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
  1003. ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
  1004. if (dpbsize == 0)
  1005. mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
  1006. ctx->dpb_size = dpbsize;
  1007. ctx->state = MTK_STATE_HEADER;
  1008. mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
  1009. }
  1010. static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
  1011. {
  1012. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1013. struct vb2_v4l2_buffer *vb2_v4l2;
  1014. struct mtk_video_dec_buf *buf;
  1015. if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1016. return;
  1017. vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
  1018. buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
  1019. mutex_lock(&ctx->lock);
  1020. buf->queued_in_v4l2 = false;
  1021. buf->queued_in_vb2 = false;
  1022. mutex_unlock(&ctx->lock);
  1023. }
  1024. static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
  1025. {
  1026. struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
  1027. struct vb2_v4l2_buffer, vb2_buf);
  1028. struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
  1029. struct mtk_video_dec_buf, vb);
  1030. if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1031. buf->used = false;
  1032. buf->ready_to_display = false;
  1033. buf->queued_in_v4l2 = false;
  1034. } else {
  1035. buf->lastframe = false;
  1036. }
  1037. return 0;
  1038. }
  1039. static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
  1040. {
  1041. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
  1042. if (ctx->state == MTK_STATE_FLUSH)
  1043. ctx->state = MTK_STATE_HEADER;
  1044. return 0;
  1045. }
  1046. static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
  1047. {
  1048. struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
  1049. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
  1050. mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
  1051. ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
  1052. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1053. while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx)))
  1054. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
  1055. VB2_BUF_STATE_ERROR);
  1056. return;
  1057. }
  1058. if (ctx->state >= MTK_STATE_HEADER) {
  1059. /* Until STREAMOFF is called on the CAPTURE queue
  1060. * (acknowledging the event), the driver operates
  1061. * as if the resolution hasn't changed yet, i.e.
  1062. * VIDIOC_G_FMT< etc. return previous resolution.
  1063. * So we update picinfo here
  1064. */
  1065. ctx->picinfo = ctx->last_decoded_picinfo;
  1066. mtk_v4l2_debug(2,
  1067. "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
  1068. ctx->id, ctx->last_decoded_picinfo.pic_w,
  1069. ctx->last_decoded_picinfo.pic_h,
  1070. ctx->picinfo.pic_w, ctx->picinfo.pic_h,
  1071. ctx->last_decoded_picinfo.buf_w,
  1072. ctx->last_decoded_picinfo.buf_h);
  1073. mtk_vdec_flush_decoder(ctx);
  1074. }
  1075. ctx->state = MTK_STATE_FLUSH;
  1076. while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
  1077. vb2_set_plane_payload(dst_buf, 0, 0);
  1078. vb2_set_plane_payload(dst_buf, 1, 0);
  1079. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
  1080. VB2_BUF_STATE_ERROR);
  1081. }
  1082. }
  1083. static void m2mops_vdec_device_run(void *priv)
  1084. {
  1085. struct mtk_vcodec_ctx *ctx = priv;
  1086. struct mtk_vcodec_dev *dev = ctx->dev;
  1087. queue_work(dev->decode_workqueue, &ctx->decode_work);
  1088. }
  1089. static int m2mops_vdec_job_ready(void *m2m_priv)
  1090. {
  1091. struct mtk_vcodec_ctx *ctx = m2m_priv;
  1092. mtk_v4l2_debug(3, "[%d]", ctx->id);
  1093. if (ctx->state == MTK_STATE_ABORT)
  1094. return 0;
  1095. if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
  1096. (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
  1097. return 0;
  1098. if (ctx->state != MTK_STATE_HEADER)
  1099. return 0;
  1100. return 1;
  1101. }
  1102. static void m2mops_vdec_job_abort(void *priv)
  1103. {
  1104. struct mtk_vcodec_ctx *ctx = priv;
  1105. ctx->state = MTK_STATE_ABORT;
  1106. }
  1107. static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
  1108. {
  1109. struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
  1110. int ret = 0;
  1111. switch (ctrl->id) {
  1112. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
  1113. if (ctx->state >= MTK_STATE_HEADER) {
  1114. ctrl->val = ctx->dpb_size;
  1115. } else {
  1116. mtk_v4l2_debug(0, "Seqinfo not ready");
  1117. ctrl->val = 0;
  1118. }
  1119. break;
  1120. default:
  1121. ret = -EINVAL;
  1122. }
  1123. return ret;
  1124. }
  1125. static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
  1126. .g_volatile_ctrl = mtk_vdec_g_v_ctrl,
  1127. };
  1128. int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
  1129. {
  1130. struct v4l2_ctrl *ctrl;
  1131. v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
  1132. ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
  1133. &mtk_vcodec_dec_ctrl_ops,
  1134. V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
  1135. 0, 32, 1, 1);
  1136. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1137. if (ctx->ctrl_hdl.error) {
  1138. mtk_v4l2_err("Adding control failed %d",
  1139. ctx->ctrl_hdl.error);
  1140. return ctx->ctrl_hdl.error;
  1141. }
  1142. v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
  1143. return 0;
  1144. }
  1145. static void m2mops_vdec_lock(void *m2m_priv)
  1146. {
  1147. struct mtk_vcodec_ctx *ctx = m2m_priv;
  1148. mtk_v4l2_debug(3, "[%d]", ctx->id);
  1149. mutex_lock(&ctx->dev->dev_mutex);
  1150. }
  1151. static void m2mops_vdec_unlock(void *m2m_priv)
  1152. {
  1153. struct mtk_vcodec_ctx *ctx = m2m_priv;
  1154. mtk_v4l2_debug(3, "[%d]", ctx->id);
  1155. mutex_unlock(&ctx->dev->dev_mutex);
  1156. }
  1157. const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
  1158. .device_run = m2mops_vdec_device_run,
  1159. .job_ready = m2mops_vdec_job_ready,
  1160. .job_abort = m2mops_vdec_job_abort,
  1161. .lock = m2mops_vdec_lock,
  1162. .unlock = m2mops_vdec_unlock,
  1163. };
  1164. static const struct vb2_ops mtk_vdec_vb2_ops = {
  1165. .queue_setup = vb2ops_vdec_queue_setup,
  1166. .buf_prepare = vb2ops_vdec_buf_prepare,
  1167. .buf_queue = vb2ops_vdec_buf_queue,
  1168. .wait_prepare = vb2_ops_wait_prepare,
  1169. .wait_finish = vb2_ops_wait_finish,
  1170. .buf_init = vb2ops_vdec_buf_init,
  1171. .buf_finish = vb2ops_vdec_buf_finish,
  1172. .start_streaming = vb2ops_vdec_start_streaming,
  1173. .stop_streaming = vb2ops_vdec_stop_streaming,
  1174. };
  1175. const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
  1176. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1177. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1178. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1179. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1180. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  1181. .vidioc_qbuf = vidioc_vdec_qbuf,
  1182. .vidioc_dqbuf = vidioc_vdec_dqbuf,
  1183. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
  1184. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
  1185. .vidioc_s_fmt_vid_cap_mplane = vidioc_vdec_s_fmt,
  1186. .vidioc_s_fmt_vid_out_mplane = vidioc_vdec_s_fmt,
  1187. .vidioc_g_fmt_vid_cap_mplane = vidioc_vdec_g_fmt,
  1188. .vidioc_g_fmt_vid_out_mplane = vidioc_vdec_g_fmt,
  1189. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  1190. .vidioc_enum_fmt_vid_cap_mplane = vidioc_vdec_enum_fmt_vid_cap_mplane,
  1191. .vidioc_enum_fmt_vid_out_mplane = vidioc_vdec_enum_fmt_vid_out_mplane,
  1192. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1193. .vidioc_querycap = vidioc_vdec_querycap,
  1194. .vidioc_subscribe_event = vidioc_vdec_subscribe_evt,
  1195. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1196. .vidioc_g_selection = vidioc_vdec_g_selection,
  1197. .vidioc_s_selection = vidioc_vdec_s_selection,
  1198. };
  1199. int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
  1200. struct vb2_queue *dst_vq)
  1201. {
  1202. struct mtk_vcodec_ctx *ctx = priv;
  1203. int ret = 0;
  1204. mtk_v4l2_debug(3, "[%d]", ctx->id);
  1205. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1206. src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1207. src_vq->drv_priv = ctx;
  1208. src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
  1209. src_vq->ops = &mtk_vdec_vb2_ops;
  1210. src_vq->mem_ops = &vb2_dma_contig_memops;
  1211. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1212. src_vq->lock = &ctx->dev->dev_mutex;
  1213. src_vq->dev = &ctx->dev->plat_dev->dev;
  1214. ret = vb2_queue_init(src_vq);
  1215. if (ret) {
  1216. mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
  1217. return ret;
  1218. }
  1219. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1220. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1221. dst_vq->drv_priv = ctx;
  1222. dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
  1223. dst_vq->ops = &mtk_vdec_vb2_ops;
  1224. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1225. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1226. dst_vq->lock = &ctx->dev->dev_mutex;
  1227. dst_vq->dev = &ctx->dev->plat_dev->dev;
  1228. ret = vb2_queue_init(dst_vq);
  1229. if (ret) {
  1230. vb2_queue_release(src_vq);
  1231. mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
  1232. }
  1233. return ret;
  1234. }