mtk_vcodec_dec.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. pfb = &dst_buf_info->frame_buffer;
  309. pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
  310. pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  311. pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
  312. pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
  313. pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
  314. pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
  315. pfb->status = 0;
  316. mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
  317. mtk_v4l2_debug(3,
  318. "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
  319. dst_buf->index, pfb,
  320. pfb->base_y.va, &pfb->base_y.dma_addr,
  321. &pfb->base_c.dma_addr, pfb->base_y.size);
  322. if (src_buf_info->lastframe) {
  323. mtk_v4l2_debug(1, "Got empty flush input buffer.");
  324. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  325. /* update dst buf status */
  326. dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  327. mutex_lock(&ctx->lock);
  328. dst_buf_info->used = false;
  329. mutex_unlock(&ctx->lock);
  330. vdec_if_decode(ctx, NULL, NULL, &res_chg);
  331. clean_display_buffer(ctx);
  332. vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
  333. vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
  334. dst_vb2_v4l2->flags |= V4L2_BUF_FLAG_LAST;
  335. v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
  336. clean_free_buffer(ctx);
  337. v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
  338. return;
  339. }
  340. buf.va = vb2_plane_vaddr(src_buf, 0);
  341. buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  342. buf.size = (size_t)src_buf->planes[0].bytesused;
  343. if (!buf.va) {
  344. v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
  345. mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
  346. ctx->id, src_buf->index);
  347. return;
  348. }
  349. mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
  350. ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
  351. dst_buf_info->vb.vb2_buf.timestamp
  352. = src_buf_info->vb.vb2_buf.timestamp;
  353. dst_buf_info->vb.timecode
  354. = src_buf_info->vb.timecode;
  355. mutex_lock(&ctx->lock);
  356. dst_buf_info->used = true;
  357. mutex_unlock(&ctx->lock);
  358. src_buf_info->used = true;
  359. ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
  360. if (ret) {
  361. mtk_v4l2_err(
  362. " <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
  363. ctx->id,
  364. src_buf->index,
  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. if (ret == -EIO) {
  371. mutex_lock(&ctx->lock);
  372. src_buf_info->error = true;
  373. mutex_unlock(&ctx->lock);
  374. }
  375. v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_ERROR);
  376. } else if (res_chg == false) {
  377. /*
  378. * we only return src buffer with VB2_BUF_STATE_DONE
  379. * when decode success without resolution change
  380. */
  381. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  382. v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
  383. }
  384. dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  385. clean_display_buffer(ctx);
  386. clean_free_buffer(ctx);
  387. if (!ret && res_chg) {
  388. mtk_vdec_pic_info_update(ctx);
  389. /*
  390. * On encountering a resolution change in the stream.
  391. * The driver must first process and decode all
  392. * remaining buffers from before the resolution change
  393. * point, so call flush decode here
  394. */
  395. mtk_vdec_flush_decoder(ctx);
  396. /*
  397. * After all buffers containing decoded frames from
  398. * before the resolution change point ready to be
  399. * dequeued on the CAPTURE queue, the driver sends a
  400. * V4L2_EVENT_SOURCE_CHANGE event for source change
  401. * type V4L2_EVENT_SRC_CH_RESOLUTION
  402. */
  403. mtk_vdec_queue_res_chg_event(ctx);
  404. }
  405. v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
  406. }
  407. static int vidioc_try_decoder_cmd(struct file *file, void *priv,
  408. struct v4l2_decoder_cmd *cmd)
  409. {
  410. switch (cmd->cmd) {
  411. case V4L2_DEC_CMD_STOP:
  412. case V4L2_DEC_CMD_START:
  413. if (cmd->flags != 0) {
  414. mtk_v4l2_err("cmd->flags=%u", cmd->flags);
  415. return -EINVAL;
  416. }
  417. break;
  418. default:
  419. return -EINVAL;
  420. }
  421. return 0;
  422. }
  423. static int vidioc_decoder_cmd(struct file *file, void *priv,
  424. struct v4l2_decoder_cmd *cmd)
  425. {
  426. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  427. struct vb2_queue *src_vq, *dst_vq;
  428. int ret;
  429. ret = vidioc_try_decoder_cmd(file, priv, cmd);
  430. if (ret)
  431. return ret;
  432. mtk_v4l2_debug(1, "decoder cmd=%u", cmd->cmd);
  433. dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
  434. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  435. switch (cmd->cmd) {
  436. case V4L2_DEC_CMD_STOP:
  437. src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
  438. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  439. if (!vb2_is_streaming(src_vq)) {
  440. mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
  441. return 0;
  442. }
  443. if (!vb2_is_streaming(dst_vq)) {
  444. mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
  445. return 0;
  446. }
  447. v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf->vb);
  448. v4l2_m2m_try_schedule(ctx->m2m_ctx);
  449. break;
  450. case V4L2_DEC_CMD_START:
  451. vb2_clear_last_buffer_dequeued(dst_vq);
  452. break;
  453. default:
  454. return -EINVAL;
  455. }
  456. return 0;
  457. }
  458. void mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
  459. {
  460. mutex_unlock(&ctx->dev->dec_mutex);
  461. }
  462. void mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
  463. {
  464. mutex_lock(&ctx->dev->dec_mutex);
  465. }
  466. void mtk_vcodec_dec_release(struct mtk_vcodec_ctx *ctx)
  467. {
  468. vdec_if_deinit(ctx);
  469. ctx->state = MTK_STATE_FREE;
  470. }
  471. void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
  472. {
  473. struct mtk_q_data *q_data;
  474. ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
  475. ctx->fh.m2m_ctx = ctx->m2m_ctx;
  476. ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
  477. INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
  478. ctx->colorspace = V4L2_COLORSPACE_REC709;
  479. ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  480. ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
  481. ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  482. q_data = &ctx->q_data[MTK_Q_DATA_SRC];
  483. memset(q_data, 0, sizeof(struct mtk_q_data));
  484. q_data->visible_width = DFT_CFG_WIDTH;
  485. q_data->visible_height = DFT_CFG_HEIGHT;
  486. q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
  487. q_data->field = V4L2_FIELD_NONE;
  488. q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
  489. q_data->bytesperline[0] = 0;
  490. q_data = &ctx->q_data[MTK_Q_DATA_DST];
  491. memset(q_data, 0, sizeof(struct mtk_q_data));
  492. q_data->visible_width = DFT_CFG_WIDTH;
  493. q_data->visible_height = DFT_CFG_HEIGHT;
  494. q_data->coded_width = DFT_CFG_WIDTH;
  495. q_data->coded_height = DFT_CFG_HEIGHT;
  496. q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
  497. q_data->field = V4L2_FIELD_NONE;
  498. v4l_bound_align_image(&q_data->coded_width,
  499. MTK_VDEC_MIN_W,
  500. MTK_VDEC_MAX_W, 4,
  501. &q_data->coded_height,
  502. MTK_VDEC_MIN_H,
  503. MTK_VDEC_MAX_H, 5, 6);
  504. q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
  505. q_data->bytesperline[0] = q_data->coded_width;
  506. q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
  507. q_data->bytesperline[1] = q_data->coded_width;
  508. }
  509. static int vidioc_vdec_qbuf(struct file *file, void *priv,
  510. struct v4l2_buffer *buf)
  511. {
  512. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  513. if (ctx->state == MTK_STATE_ABORT) {
  514. mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
  515. ctx->id);
  516. return -EIO;
  517. }
  518. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  519. }
  520. static int vidioc_vdec_dqbuf(struct file *file, void *priv,
  521. struct v4l2_buffer *buf)
  522. {
  523. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  524. if (ctx->state == MTK_STATE_ABORT) {
  525. mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
  526. ctx->id);
  527. return -EIO;
  528. }
  529. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  530. }
  531. static int vidioc_vdec_querycap(struct file *file, void *priv,
  532. struct v4l2_capability *cap)
  533. {
  534. strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
  535. strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
  536. strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
  537. return 0;
  538. }
  539. static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
  540. const struct v4l2_event_subscription *sub)
  541. {
  542. switch (sub->type) {
  543. case V4L2_EVENT_EOS:
  544. return v4l2_event_subscribe(fh, sub, 2, NULL);
  545. case V4L2_EVENT_SOURCE_CHANGE:
  546. return v4l2_src_change_event_subscribe(fh, sub);
  547. default:
  548. return v4l2_ctrl_subscribe_event(fh, sub);
  549. }
  550. }
  551. static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt)
  552. {
  553. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  554. int i;
  555. pix_fmt_mp->field = V4L2_FIELD_NONE;
  556. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  557. pix_fmt_mp->num_planes = 1;
  558. pix_fmt_mp->plane_fmt[0].bytesperline = 0;
  559. } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  560. int tmp_w, tmp_h;
  561. pix_fmt_mp->height = clamp(pix_fmt_mp->height,
  562. MTK_VDEC_MIN_H,
  563. MTK_VDEC_MAX_H);
  564. pix_fmt_mp->width = clamp(pix_fmt_mp->width,
  565. MTK_VDEC_MIN_W,
  566. MTK_VDEC_MAX_W);
  567. /*
  568. * Find next closer width align 64, heign align 64, size align
  569. * 64 rectangle
  570. * Note: This only get default value, the real HW needed value
  571. * only available when ctx in MTK_STATE_HEADER state
  572. */
  573. tmp_w = pix_fmt_mp->width;
  574. tmp_h = pix_fmt_mp->height;
  575. v4l_bound_align_image(&pix_fmt_mp->width,
  576. MTK_VDEC_MIN_W,
  577. MTK_VDEC_MAX_W, 6,
  578. &pix_fmt_mp->height,
  579. MTK_VDEC_MIN_H,
  580. MTK_VDEC_MAX_H, 6, 9);
  581. if (pix_fmt_mp->width < tmp_w &&
  582. (pix_fmt_mp->width + 64) <= MTK_VDEC_MAX_W)
  583. pix_fmt_mp->width += 64;
  584. if (pix_fmt_mp->height < tmp_h &&
  585. (pix_fmt_mp->height + 64) <= MTK_VDEC_MAX_H)
  586. pix_fmt_mp->height += 64;
  587. mtk_v4l2_debug(0,
  588. "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
  589. tmp_w, tmp_h, pix_fmt_mp->width,
  590. pix_fmt_mp->height,
  591. pix_fmt_mp->width * pix_fmt_mp->height);
  592. pix_fmt_mp->num_planes = fmt->num_planes;
  593. pix_fmt_mp->plane_fmt[0].sizeimage =
  594. pix_fmt_mp->width * pix_fmt_mp->height;
  595. pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
  596. if (pix_fmt_mp->num_planes == 2) {
  597. pix_fmt_mp->plane_fmt[1].sizeimage =
  598. (pix_fmt_mp->width * pix_fmt_mp->height) / 2;
  599. pix_fmt_mp->plane_fmt[1].bytesperline =
  600. pix_fmt_mp->width;
  601. }
  602. }
  603. for (i = 0; i < pix_fmt_mp->num_planes; i++)
  604. memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
  605. sizeof(pix_fmt_mp->plane_fmt[0].reserved));
  606. pix_fmt_mp->flags = 0;
  607. memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved));
  608. return 0;
  609. }
  610. static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
  611. struct v4l2_format *f)
  612. {
  613. struct mtk_video_fmt *fmt;
  614. fmt = mtk_vdec_find_format(f);
  615. if (!fmt) {
  616. f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
  617. fmt = mtk_vdec_find_format(f);
  618. }
  619. return vidioc_try_fmt(f, fmt);
  620. }
  621. static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
  622. struct v4l2_format *f)
  623. {
  624. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  625. struct mtk_video_fmt *fmt;
  626. fmt = mtk_vdec_find_format(f);
  627. if (!fmt) {
  628. f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
  629. fmt = mtk_vdec_find_format(f);
  630. }
  631. if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
  632. mtk_v4l2_err("sizeimage of output format must be given");
  633. return -EINVAL;
  634. }
  635. return vidioc_try_fmt(f, fmt);
  636. }
  637. static int vidioc_vdec_g_selection(struct file *file, void *priv,
  638. struct v4l2_selection *s)
  639. {
  640. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  641. struct mtk_q_data *q_data;
  642. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  643. return -EINVAL;
  644. q_data = &ctx->q_data[MTK_Q_DATA_DST];
  645. switch (s->target) {
  646. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  647. s->r.left = 0;
  648. s->r.top = 0;
  649. s->r.width = ctx->picinfo.pic_w;
  650. s->r.height = ctx->picinfo.pic_h;
  651. break;
  652. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  653. s->r.left = 0;
  654. s->r.top = 0;
  655. s->r.width = ctx->picinfo.buf_w;
  656. s->r.height = ctx->picinfo.buf_h;
  657. break;
  658. case V4L2_SEL_TGT_COMPOSE:
  659. if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
  660. /* set to default value if header info not ready yet*/
  661. s->r.left = 0;
  662. s->r.top = 0;
  663. s->r.width = q_data->visible_width;
  664. s->r.height = q_data->visible_height;
  665. }
  666. break;
  667. default:
  668. return -EINVAL;
  669. }
  670. if (ctx->state < MTK_STATE_HEADER) {
  671. /* set to default value if header info not ready yet*/
  672. s->r.left = 0;
  673. s->r.top = 0;
  674. s->r.width = q_data->visible_width;
  675. s->r.height = q_data->visible_height;
  676. return 0;
  677. }
  678. return 0;
  679. }
  680. static int vidioc_vdec_s_selection(struct file *file, void *priv,
  681. struct v4l2_selection *s)
  682. {
  683. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  684. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  685. return -EINVAL;
  686. switch (s->target) {
  687. case V4L2_SEL_TGT_COMPOSE:
  688. s->r.left = 0;
  689. s->r.top = 0;
  690. s->r.width = ctx->picinfo.pic_w;
  691. s->r.height = ctx->picinfo.pic_h;
  692. break;
  693. default:
  694. return -EINVAL;
  695. }
  696. return 0;
  697. }
  698. static int vidioc_vdec_s_fmt(struct file *file, void *priv,
  699. struct v4l2_format *f)
  700. {
  701. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  702. struct v4l2_pix_format_mplane *pix_mp;
  703. struct mtk_q_data *q_data;
  704. int ret = 0;
  705. struct mtk_video_fmt *fmt;
  706. mtk_v4l2_debug(3, "[%d]", ctx->id);
  707. q_data = mtk_vdec_get_q_data(ctx, f->type);
  708. if (!q_data)
  709. return -EINVAL;
  710. pix_mp = &f->fmt.pix_mp;
  711. if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
  712. vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
  713. mtk_v4l2_err("out_q_ctx buffers already requested");
  714. ret = -EBUSY;
  715. }
  716. if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
  717. vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
  718. mtk_v4l2_err("cap_q_ctx buffers already requested");
  719. ret = -EBUSY;
  720. }
  721. fmt = mtk_vdec_find_format(f);
  722. if (fmt == NULL) {
  723. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  724. f->fmt.pix.pixelformat =
  725. mtk_video_formats[OUT_FMT_IDX].fourcc;
  726. fmt = mtk_vdec_find_format(f);
  727. } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  728. f->fmt.pix.pixelformat =
  729. mtk_video_formats[CAP_FMT_IDX].fourcc;
  730. fmt = mtk_vdec_find_format(f);
  731. }
  732. }
  733. q_data->fmt = fmt;
  734. vidioc_try_fmt(f, q_data->fmt);
  735. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  736. q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
  737. q_data->coded_width = pix_mp->width;
  738. q_data->coded_height = pix_mp->height;
  739. ctx->colorspace = f->fmt.pix_mp.colorspace;
  740. ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
  741. ctx->quantization = f->fmt.pix_mp.quantization;
  742. ctx->xfer_func = f->fmt.pix_mp.xfer_func;
  743. if (ctx->state == MTK_STATE_FREE) {
  744. ret = vdec_if_init(ctx, q_data->fmt->fourcc);
  745. if (ret) {
  746. mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
  747. ctx->id, ret);
  748. return -EINVAL;
  749. }
  750. ctx->state = MTK_STATE_INIT;
  751. }
  752. }
  753. return 0;
  754. }
  755. static int vidioc_enum_framesizes(struct file *file, void *priv,
  756. struct v4l2_frmsizeenum *fsize)
  757. {
  758. int i = 0;
  759. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  760. if (fsize->index != 0)
  761. return -EINVAL;
  762. for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
  763. if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
  764. continue;
  765. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  766. fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
  767. if (!(ctx->dev->dec_capability &
  768. VCODEC_CAPABILITY_4K_DISABLED)) {
  769. mtk_v4l2_debug(3, "4K is enabled");
  770. fsize->stepwise.max_width =
  771. VCODEC_DEC_4K_CODED_WIDTH;
  772. fsize->stepwise.max_height =
  773. VCODEC_DEC_4K_CODED_HEIGHT;
  774. }
  775. mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
  776. ctx->dev->dec_capability,
  777. fsize->stepwise.min_width,
  778. fsize->stepwise.max_width,
  779. fsize->stepwise.step_width,
  780. fsize->stepwise.min_height,
  781. fsize->stepwise.max_height,
  782. fsize->stepwise.step_height);
  783. return 0;
  784. }
  785. return -EINVAL;
  786. }
  787. static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
  788. {
  789. struct mtk_video_fmt *fmt;
  790. int i, j = 0;
  791. for (i = 0; i < NUM_FORMATS; i++) {
  792. if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
  793. continue;
  794. if (!output_queue &&
  795. (mtk_video_formats[i].type != MTK_FMT_FRAME))
  796. continue;
  797. if (j == f->index)
  798. break;
  799. ++j;
  800. }
  801. if (i == NUM_FORMATS)
  802. return -EINVAL;
  803. fmt = &mtk_video_formats[i];
  804. f->pixelformat = fmt->fourcc;
  805. return 0;
  806. }
  807. static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
  808. struct v4l2_fmtdesc *f)
  809. {
  810. return vidioc_enum_fmt(f, false);
  811. }
  812. static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
  813. struct v4l2_fmtdesc *f)
  814. {
  815. return vidioc_enum_fmt(f, true);
  816. }
  817. static int vidioc_vdec_g_fmt(struct file *file, void *priv,
  818. struct v4l2_format *f)
  819. {
  820. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  821. struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
  822. struct vb2_queue *vq;
  823. struct mtk_q_data *q_data;
  824. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  825. if (!vq) {
  826. mtk_v4l2_err("no vb2 queue for type=%d", f->type);
  827. return -EINVAL;
  828. }
  829. q_data = mtk_vdec_get_q_data(ctx, f->type);
  830. pix_mp->field = V4L2_FIELD_NONE;
  831. pix_mp->colorspace = ctx->colorspace;
  832. pix_mp->ycbcr_enc = ctx->ycbcr_enc;
  833. pix_mp->quantization = ctx->quantization;
  834. pix_mp->xfer_func = ctx->xfer_func;
  835. if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
  836. (ctx->state >= MTK_STATE_HEADER)) {
  837. /* Until STREAMOFF is called on the CAPTURE queue
  838. * (acknowledging the event), the driver operates as if
  839. * the resolution hasn't changed yet.
  840. * So we just return picinfo yet, and update picinfo in
  841. * stop_streaming hook function
  842. */
  843. q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
  844. ctx->picinfo.y_len_sz;
  845. q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
  846. ctx->picinfo.c_len_sz;
  847. q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
  848. q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
  849. q_data->coded_width = ctx->picinfo.buf_w;
  850. q_data->coded_height = ctx->picinfo.buf_h;
  851. /*
  852. * Width and height are set to the dimensions
  853. * of the movie, the buffer is bigger and
  854. * further processing stages should crop to this
  855. * rectangle.
  856. */
  857. pix_mp->width = q_data->coded_width;
  858. pix_mp->height = q_data->coded_height;
  859. /*
  860. * Set pixelformat to the format in which mt vcodec
  861. * outputs the decoded frame
  862. */
  863. pix_mp->num_planes = q_data->fmt->num_planes;
  864. pix_mp->pixelformat = q_data->fmt->fourcc;
  865. pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
  866. pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
  867. pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
  868. pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
  869. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  870. /*
  871. * This is run on OUTPUT
  872. * The buffer contains compressed image
  873. * so width and height have no meaning.
  874. * Assign value here to pass v4l2-compliance test
  875. */
  876. pix_mp->width = q_data->visible_width;
  877. pix_mp->height = q_data->visible_height;
  878. pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
  879. pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
  880. pix_mp->pixelformat = q_data->fmt->fourcc;
  881. pix_mp->num_planes = q_data->fmt->num_planes;
  882. } else {
  883. pix_mp->width = q_data->coded_width;
  884. pix_mp->height = q_data->coded_height;
  885. pix_mp->num_planes = q_data->fmt->num_planes;
  886. pix_mp->pixelformat = q_data->fmt->fourcc;
  887. pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
  888. pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
  889. pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
  890. pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
  891. mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
  892. ctx->id, f->type, ctx->state);
  893. }
  894. return 0;
  895. }
  896. static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
  897. unsigned int *nbuffers,
  898. unsigned int *nplanes,
  899. unsigned int sizes[],
  900. struct device *alloc_devs[])
  901. {
  902. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
  903. struct mtk_q_data *q_data;
  904. unsigned int i;
  905. q_data = mtk_vdec_get_q_data(ctx, vq->type);
  906. if (q_data == NULL) {
  907. mtk_v4l2_err("vq->type=%d err\n", vq->type);
  908. return -EINVAL;
  909. }
  910. if (*nplanes) {
  911. for (i = 0; i < *nplanes; i++) {
  912. if (sizes[i] < q_data->sizeimage[i])
  913. return -EINVAL;
  914. }
  915. } else {
  916. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  917. *nplanes = 2;
  918. else
  919. *nplanes = 1;
  920. for (i = 0; i < *nplanes; i++)
  921. sizes[i] = q_data->sizeimage[i];
  922. }
  923. mtk_v4l2_debug(1,
  924. "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
  925. ctx->id, vq->type, *nplanes, *nbuffers,
  926. sizes[0], sizes[1]);
  927. return 0;
  928. }
  929. static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
  930. {
  931. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  932. struct mtk_q_data *q_data;
  933. int i;
  934. mtk_v4l2_debug(3, "[%d] (%d) id=%d",
  935. ctx->id, vb->vb2_queue->type, vb->index);
  936. q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
  937. for (i = 0; i < q_data->fmt->num_planes; i++) {
  938. if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
  939. mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
  940. i, vb2_plane_size(vb, i),
  941. q_data->sizeimage[i]);
  942. }
  943. }
  944. return 0;
  945. }
  946. static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
  947. {
  948. struct vb2_buffer *src_buf;
  949. struct mtk_vcodec_mem src_mem;
  950. bool res_chg = false;
  951. int ret = 0;
  952. unsigned int dpbsize = 1;
  953. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  954. struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
  955. struct mtk_video_dec_buf *buf = NULL;
  956. mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
  957. ctx->id, vb->vb2_queue->type,
  958. vb->index, vb);
  959. /*
  960. * check if this buffer is ready to be used after decode
  961. */
  962. if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  963. vb2_v4l2 = to_vb2_v4l2_buffer(vb);
  964. buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
  965. mutex_lock(&ctx->lock);
  966. if (buf->used == false) {
  967. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
  968. buf->queued_in_vb2 = true;
  969. buf->queued_in_v4l2 = true;
  970. buf->ready_to_display = false;
  971. } else {
  972. buf->queued_in_vb2 = false;
  973. buf->queued_in_v4l2 = true;
  974. buf->ready_to_display = false;
  975. }
  976. mutex_unlock(&ctx->lock);
  977. return;
  978. }
  979. v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
  980. if (ctx->state != MTK_STATE_INIT) {
  981. mtk_v4l2_debug(3, "[%d] already init driver %d",
  982. ctx->id, ctx->state);
  983. return;
  984. }
  985. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  986. if (!src_buf) {
  987. mtk_v4l2_err("No src buffer");
  988. return;
  989. }
  990. vb2_v4l2 = to_vb2_v4l2_buffer(src_buf);
  991. buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
  992. if (buf->lastframe) {
  993. /* This shouldn't happen. Just in case. */
  994. mtk_v4l2_err("Invalid flush buffer.");
  995. v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  996. return;
  997. }
  998. src_mem.va = vb2_plane_vaddr(src_buf, 0);
  999. src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  1000. src_mem.size = (size_t)src_buf->planes[0].bytesused;
  1001. mtk_v4l2_debug(2,
  1002. "[%d] buf id=%d va=%p dma=%pad size=%zx",
  1003. ctx->id, src_buf->index,
  1004. src_mem.va, &src_mem.dma_addr,
  1005. src_mem.size);
  1006. ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
  1007. if (ret || !res_chg) {
  1008. /*
  1009. * fb == NULL menas to parse SPS/PPS header or
  1010. * resolution info in src_mem. Decode can fail
  1011. * if there is no SPS header or picture info
  1012. * in bs
  1013. */
  1014. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  1015. if (ret == -EIO) {
  1016. mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.",
  1017. ctx->id);
  1018. ctx->state = MTK_STATE_ABORT;
  1019. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
  1020. VB2_BUF_STATE_ERROR);
  1021. } else {
  1022. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
  1023. VB2_BUF_STATE_DONE);
  1024. }
  1025. mtk_v4l2_debug(ret ? 0 : 1,
  1026. "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
  1027. ctx->id, src_buf->index,
  1028. src_mem.size, ret, res_chg);
  1029. return;
  1030. }
  1031. if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
  1032. mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
  1033. ctx->id);
  1034. return;
  1035. }
  1036. ctx->last_decoded_picinfo = ctx->picinfo;
  1037. ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
  1038. ctx->picinfo.y_bs_sz +
  1039. ctx->picinfo.y_len_sz;
  1040. ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
  1041. ctx->picinfo.buf_w;
  1042. ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
  1043. ctx->picinfo.c_bs_sz +
  1044. ctx->picinfo.c_len_sz;
  1045. ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] = ctx->picinfo.buf_w;
  1046. mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
  1047. ctx->id,
  1048. ctx->picinfo.buf_w, ctx->picinfo.buf_h,
  1049. ctx->picinfo.pic_w, ctx->picinfo.pic_h,
  1050. ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
  1051. ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
  1052. ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
  1053. if (dpbsize == 0)
  1054. mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
  1055. ctx->dpb_size = dpbsize;
  1056. ctx->state = MTK_STATE_HEADER;
  1057. mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
  1058. }
  1059. static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
  1060. {
  1061. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1062. struct vb2_v4l2_buffer *vb2_v4l2;
  1063. struct mtk_video_dec_buf *buf;
  1064. bool buf_error;
  1065. vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
  1066. buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
  1067. mutex_lock(&ctx->lock);
  1068. if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1069. buf->queued_in_v4l2 = false;
  1070. buf->queued_in_vb2 = false;
  1071. }
  1072. buf_error = buf->error;
  1073. mutex_unlock(&ctx->lock);
  1074. if (buf_error) {
  1075. mtk_v4l2_err("Unrecoverable error on buffer.");
  1076. ctx->state = MTK_STATE_ABORT;
  1077. }
  1078. }
  1079. static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
  1080. {
  1081. struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
  1082. struct vb2_v4l2_buffer, vb2_buf);
  1083. struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
  1084. struct mtk_video_dec_buf, vb);
  1085. if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1086. buf->used = false;
  1087. buf->ready_to_display = false;
  1088. buf->queued_in_v4l2 = false;
  1089. } else {
  1090. buf->lastframe = false;
  1091. }
  1092. return 0;
  1093. }
  1094. static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
  1095. {
  1096. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
  1097. if (ctx->state == MTK_STATE_FLUSH)
  1098. ctx->state = MTK_STATE_HEADER;
  1099. return 0;
  1100. }
  1101. static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
  1102. {
  1103. struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
  1104. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
  1105. mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
  1106. ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
  1107. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1108. while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
  1109. struct vb2_v4l2_buffer *vb2_v4l2 =
  1110. to_vb2_v4l2_buffer(src_buf);
  1111. struct mtk_video_dec_buf *buf_info = container_of(
  1112. vb2_v4l2, struct mtk_video_dec_buf, vb);
  1113. if (!buf_info->lastframe)
  1114. v4l2_m2m_buf_done(vb2_v4l2,
  1115. VB2_BUF_STATE_ERROR);
  1116. }
  1117. return;
  1118. }
  1119. if (ctx->state >= MTK_STATE_HEADER) {
  1120. /* Until STREAMOFF is called on the CAPTURE queue
  1121. * (acknowledging the event), the driver operates
  1122. * as if the resolution hasn't changed yet, i.e.
  1123. * VIDIOC_G_FMT< etc. return previous resolution.
  1124. * So we update picinfo here
  1125. */
  1126. ctx->picinfo = ctx->last_decoded_picinfo;
  1127. mtk_v4l2_debug(2,
  1128. "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
  1129. ctx->id, ctx->last_decoded_picinfo.pic_w,
  1130. ctx->last_decoded_picinfo.pic_h,
  1131. ctx->picinfo.pic_w, ctx->picinfo.pic_h,
  1132. ctx->last_decoded_picinfo.buf_w,
  1133. ctx->last_decoded_picinfo.buf_h);
  1134. mtk_vdec_flush_decoder(ctx);
  1135. }
  1136. ctx->state = MTK_STATE_FLUSH;
  1137. while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
  1138. vb2_set_plane_payload(dst_buf, 0, 0);
  1139. vb2_set_plane_payload(dst_buf, 1, 0);
  1140. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
  1141. VB2_BUF_STATE_ERROR);
  1142. }
  1143. }
  1144. static void m2mops_vdec_device_run(void *priv)
  1145. {
  1146. struct mtk_vcodec_ctx *ctx = priv;
  1147. struct mtk_vcodec_dev *dev = ctx->dev;
  1148. queue_work(dev->decode_workqueue, &ctx->decode_work);
  1149. }
  1150. static int m2mops_vdec_job_ready(void *m2m_priv)
  1151. {
  1152. struct mtk_vcodec_ctx *ctx = m2m_priv;
  1153. mtk_v4l2_debug(3, "[%d]", ctx->id);
  1154. if (ctx->state == MTK_STATE_ABORT)
  1155. return 0;
  1156. if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
  1157. (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
  1158. return 0;
  1159. if (ctx->state != MTK_STATE_HEADER)
  1160. return 0;
  1161. return 1;
  1162. }
  1163. static void m2mops_vdec_job_abort(void *priv)
  1164. {
  1165. struct mtk_vcodec_ctx *ctx = priv;
  1166. ctx->state = MTK_STATE_ABORT;
  1167. }
  1168. static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
  1169. {
  1170. struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
  1171. int ret = 0;
  1172. switch (ctrl->id) {
  1173. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
  1174. if (ctx->state >= MTK_STATE_HEADER) {
  1175. ctrl->val = ctx->dpb_size;
  1176. } else {
  1177. mtk_v4l2_debug(0, "Seqinfo not ready");
  1178. ctrl->val = 0;
  1179. }
  1180. break;
  1181. default:
  1182. ret = -EINVAL;
  1183. }
  1184. return ret;
  1185. }
  1186. static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
  1187. .g_volatile_ctrl = mtk_vdec_g_v_ctrl,
  1188. };
  1189. int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
  1190. {
  1191. struct v4l2_ctrl *ctrl;
  1192. v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
  1193. ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
  1194. &mtk_vcodec_dec_ctrl_ops,
  1195. V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
  1196. 0, 32, 1, 1);
  1197. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1198. if (ctx->ctrl_hdl.error) {
  1199. mtk_v4l2_err("Adding control failed %d",
  1200. ctx->ctrl_hdl.error);
  1201. return ctx->ctrl_hdl.error;
  1202. }
  1203. v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
  1204. return 0;
  1205. }
  1206. static void m2mops_vdec_lock(void *m2m_priv)
  1207. {
  1208. struct mtk_vcodec_ctx *ctx = m2m_priv;
  1209. mtk_v4l2_debug(3, "[%d]", ctx->id);
  1210. mutex_lock(&ctx->dev->dev_mutex);
  1211. }
  1212. static void m2mops_vdec_unlock(void *m2m_priv)
  1213. {
  1214. struct mtk_vcodec_ctx *ctx = m2m_priv;
  1215. mtk_v4l2_debug(3, "[%d]", ctx->id);
  1216. mutex_unlock(&ctx->dev->dev_mutex);
  1217. }
  1218. const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
  1219. .device_run = m2mops_vdec_device_run,
  1220. .job_ready = m2mops_vdec_job_ready,
  1221. .job_abort = m2mops_vdec_job_abort,
  1222. .lock = m2mops_vdec_lock,
  1223. .unlock = m2mops_vdec_unlock,
  1224. };
  1225. static const struct vb2_ops mtk_vdec_vb2_ops = {
  1226. .queue_setup = vb2ops_vdec_queue_setup,
  1227. .buf_prepare = vb2ops_vdec_buf_prepare,
  1228. .buf_queue = vb2ops_vdec_buf_queue,
  1229. .wait_prepare = vb2_ops_wait_prepare,
  1230. .wait_finish = vb2_ops_wait_finish,
  1231. .buf_init = vb2ops_vdec_buf_init,
  1232. .buf_finish = vb2ops_vdec_buf_finish,
  1233. .start_streaming = vb2ops_vdec_start_streaming,
  1234. .stop_streaming = vb2ops_vdec_stop_streaming,
  1235. };
  1236. const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
  1237. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1238. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1239. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1240. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1241. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  1242. .vidioc_qbuf = vidioc_vdec_qbuf,
  1243. .vidioc_dqbuf = vidioc_vdec_dqbuf,
  1244. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
  1245. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
  1246. .vidioc_s_fmt_vid_cap_mplane = vidioc_vdec_s_fmt,
  1247. .vidioc_s_fmt_vid_out_mplane = vidioc_vdec_s_fmt,
  1248. .vidioc_g_fmt_vid_cap_mplane = vidioc_vdec_g_fmt,
  1249. .vidioc_g_fmt_vid_out_mplane = vidioc_vdec_g_fmt,
  1250. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  1251. .vidioc_enum_fmt_vid_cap_mplane = vidioc_vdec_enum_fmt_vid_cap_mplane,
  1252. .vidioc_enum_fmt_vid_out_mplane = vidioc_vdec_enum_fmt_vid_out_mplane,
  1253. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1254. .vidioc_querycap = vidioc_vdec_querycap,
  1255. .vidioc_subscribe_event = vidioc_vdec_subscribe_evt,
  1256. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1257. .vidioc_g_selection = vidioc_vdec_g_selection,
  1258. .vidioc_s_selection = vidioc_vdec_s_selection,
  1259. .vidioc_decoder_cmd = vidioc_decoder_cmd,
  1260. .vidioc_try_decoder_cmd = vidioc_try_decoder_cmd,
  1261. };
  1262. int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
  1263. struct vb2_queue *dst_vq)
  1264. {
  1265. struct mtk_vcodec_ctx *ctx = priv;
  1266. int ret = 0;
  1267. mtk_v4l2_debug(3, "[%d]", ctx->id);
  1268. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1269. src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1270. src_vq->drv_priv = ctx;
  1271. src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
  1272. src_vq->ops = &mtk_vdec_vb2_ops;
  1273. src_vq->mem_ops = &vb2_dma_contig_memops;
  1274. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1275. src_vq->lock = &ctx->dev->dev_mutex;
  1276. src_vq->dev = &ctx->dev->plat_dev->dev;
  1277. ret = vb2_queue_init(src_vq);
  1278. if (ret) {
  1279. mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
  1280. return ret;
  1281. }
  1282. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1283. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1284. dst_vq->drv_priv = ctx;
  1285. dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
  1286. dst_vq->ops = &mtk_vdec_vb2_ops;
  1287. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1288. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1289. dst_vq->lock = &ctx->dev->dev_mutex;
  1290. dst_vq->dev = &ctx->dev->plat_dev->dev;
  1291. ret = vb2_queue_init(dst_vq);
  1292. if (ret) {
  1293. vb2_queue_release(src_vq);
  1294. mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
  1295. }
  1296. return ret;
  1297. }