mtk_vcodec_dec.c 41 KB

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