mtk_vcodec_enc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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 <soc/mediatek/smi.h>
  19. #include "mtk_vcodec_drv.h"
  20. #include "mtk_vcodec_enc.h"
  21. #include "mtk_vcodec_intr.h"
  22. #include "mtk_vcodec_util.h"
  23. #include "venc_drv_if.h"
  24. #define MTK_VENC_MIN_W 160U
  25. #define MTK_VENC_MIN_H 128U
  26. #define MTK_VENC_MAX_W 1920U
  27. #define MTK_VENC_MAX_H 1088U
  28. #define DFT_CFG_WIDTH MTK_VENC_MIN_W
  29. #define DFT_CFG_HEIGHT MTK_VENC_MIN_H
  30. #define MTK_MAX_CTRLS_HINT 20
  31. #define OUT_FMT_IDX 0
  32. #define CAP_FMT_IDX 4
  33. static void mtk_venc_worker(struct work_struct *work);
  34. static struct mtk_video_fmt mtk_video_formats[] = {
  35. {
  36. .fourcc = V4L2_PIX_FMT_NV12M,
  37. .type = MTK_FMT_FRAME,
  38. .num_planes = 2,
  39. },
  40. {
  41. .fourcc = V4L2_PIX_FMT_NV21M,
  42. .type = MTK_FMT_FRAME,
  43. .num_planes = 2,
  44. },
  45. {
  46. .fourcc = V4L2_PIX_FMT_YUV420M,
  47. .type = MTK_FMT_FRAME,
  48. .num_planes = 3,
  49. },
  50. {
  51. .fourcc = V4L2_PIX_FMT_YVU420M,
  52. .type = MTK_FMT_FRAME,
  53. .num_planes = 3,
  54. },
  55. {
  56. .fourcc = V4L2_PIX_FMT_H264,
  57. .type = MTK_FMT_ENC,
  58. .num_planes = 1,
  59. },
  60. {
  61. .fourcc = V4L2_PIX_FMT_VP8,
  62. .type = MTK_FMT_ENC,
  63. .num_planes = 1,
  64. },
  65. };
  66. #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
  67. static const struct mtk_codec_framesizes mtk_venc_framesizes[] = {
  68. {
  69. .fourcc = V4L2_PIX_FMT_H264,
  70. .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
  71. MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 },
  72. },
  73. {
  74. .fourcc = V4L2_PIX_FMT_VP8,
  75. .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
  76. MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 },
  77. },
  78. };
  79. #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_venc_framesizes)
  80. static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl)
  81. {
  82. struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
  83. struct mtk_enc_params *p = &ctx->enc_params;
  84. int ret = 0;
  85. switch (ctrl->id) {
  86. case V4L2_CID_MPEG_VIDEO_BITRATE:
  87. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_BITRATE val = %d",
  88. ctrl->val);
  89. p->bitrate = ctrl->val;
  90. ctx->param_change |= MTK_ENCODE_PARAM_BITRATE;
  91. break;
  92. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  93. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_B_FRAMES val = %d",
  94. ctrl->val);
  95. p->num_b_frame = ctrl->val;
  96. break;
  97. case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
  98. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE val = %d",
  99. ctrl->val);
  100. p->rc_frame = ctrl->val;
  101. break;
  102. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
  103. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_MAX_QP val = %d",
  104. ctrl->val);
  105. p->h264_max_qp = ctrl->val;
  106. break;
  107. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  108. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_HEADER_MODE val = %d",
  109. ctrl->val);
  110. p->seq_hdr_mode = ctrl->val;
  111. break;
  112. case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
  113. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE val = %d",
  114. ctrl->val);
  115. p->rc_mb = ctrl->val;
  116. break;
  117. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  118. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_PROFILE val = %d",
  119. ctrl->val);
  120. p->h264_profile = ctrl->val;
  121. break;
  122. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  123. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_LEVEL val = %d",
  124. ctrl->val);
  125. p->h264_level = ctrl->val;
  126. break;
  127. case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
  128. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_I_PERIOD val = %d",
  129. ctrl->val);
  130. p->intra_period = ctrl->val;
  131. ctx->param_change |= MTK_ENCODE_PARAM_INTRA_PERIOD;
  132. break;
  133. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  134. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_GOP_SIZE val = %d",
  135. ctrl->val);
  136. p->gop_size = ctrl->val;
  137. ctx->param_change |= MTK_ENCODE_PARAM_GOP_SIZE;
  138. break;
  139. case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
  140. mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME");
  141. p->force_intra = 1;
  142. ctx->param_change |= MTK_ENCODE_PARAM_FORCE_INTRA;
  143. break;
  144. default:
  145. ret = -EINVAL;
  146. break;
  147. }
  148. return ret;
  149. }
  150. static const struct v4l2_ctrl_ops mtk_vcodec_enc_ctrl_ops = {
  151. .s_ctrl = vidioc_venc_s_ctrl,
  152. };
  153. static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
  154. {
  155. struct mtk_video_fmt *fmt;
  156. int i, j = 0;
  157. for (i = 0; i < NUM_FORMATS; ++i) {
  158. if (output_queue && mtk_video_formats[i].type != MTK_FMT_FRAME)
  159. continue;
  160. if (!output_queue && mtk_video_formats[i].type != MTK_FMT_ENC)
  161. continue;
  162. if (j == f->index) {
  163. fmt = &mtk_video_formats[i];
  164. f->pixelformat = fmt->fourcc;
  165. memset(f->reserved, 0, sizeof(f->reserved));
  166. return 0;
  167. }
  168. ++j;
  169. }
  170. return -EINVAL;
  171. }
  172. static int vidioc_enum_framesizes(struct file *file, void *fh,
  173. struct v4l2_frmsizeenum *fsize)
  174. {
  175. int i = 0;
  176. if (fsize->index != 0)
  177. return -EINVAL;
  178. for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
  179. if (fsize->pixel_format != mtk_venc_framesizes[i].fourcc)
  180. continue;
  181. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  182. fsize->stepwise = mtk_venc_framesizes[i].stepwise;
  183. return 0;
  184. }
  185. return -EINVAL;
  186. }
  187. static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
  188. struct v4l2_fmtdesc *f)
  189. {
  190. return vidioc_enum_fmt(f, false);
  191. }
  192. static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
  193. struct v4l2_fmtdesc *f)
  194. {
  195. return vidioc_enum_fmt(f, true);
  196. }
  197. static int vidioc_venc_querycap(struct file *file, void *priv,
  198. struct v4l2_capability *cap)
  199. {
  200. strlcpy(cap->driver, MTK_VCODEC_ENC_NAME, sizeof(cap->driver));
  201. strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
  202. strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
  203. return 0;
  204. }
  205. static int vidioc_venc_s_parm(struct file *file, void *priv,
  206. struct v4l2_streamparm *a)
  207. {
  208. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  209. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  210. return -EINVAL;
  211. ctx->enc_params.framerate_num =
  212. a->parm.output.timeperframe.denominator;
  213. ctx->enc_params.framerate_denom =
  214. a->parm.output.timeperframe.numerator;
  215. ctx->param_change |= MTK_ENCODE_PARAM_FRAMERATE;
  216. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  217. return 0;
  218. }
  219. static int vidioc_venc_g_parm(struct file *file, void *priv,
  220. struct v4l2_streamparm *a)
  221. {
  222. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  223. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  224. return -EINVAL;
  225. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  226. a->parm.output.timeperframe.denominator =
  227. ctx->enc_params.framerate_num;
  228. a->parm.output.timeperframe.numerator =
  229. ctx->enc_params.framerate_denom;
  230. return 0;
  231. }
  232. static struct mtk_q_data *mtk_venc_get_q_data(struct mtk_vcodec_ctx *ctx,
  233. enum v4l2_buf_type type)
  234. {
  235. if (V4L2_TYPE_IS_OUTPUT(type))
  236. return &ctx->q_data[MTK_Q_DATA_SRC];
  237. return &ctx->q_data[MTK_Q_DATA_DST];
  238. }
  239. static struct mtk_video_fmt *mtk_venc_find_format(struct v4l2_format *f)
  240. {
  241. struct mtk_video_fmt *fmt;
  242. unsigned int k;
  243. for (k = 0; k < NUM_FORMATS; k++) {
  244. fmt = &mtk_video_formats[k];
  245. if (fmt->fourcc == f->fmt.pix.pixelformat)
  246. return fmt;
  247. }
  248. return NULL;
  249. }
  250. /* V4L2 specification suggests the driver corrects the format struct if any of
  251. * the dimensions is unsupported
  252. */
  253. static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt)
  254. {
  255. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  256. int i;
  257. pix_fmt_mp->field = V4L2_FIELD_NONE;
  258. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  259. pix_fmt_mp->num_planes = 1;
  260. pix_fmt_mp->plane_fmt[0].bytesperline = 0;
  261. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  262. int tmp_w, tmp_h;
  263. pix_fmt_mp->height = clamp(pix_fmt_mp->height,
  264. MTK_VENC_MIN_H,
  265. MTK_VENC_MAX_H);
  266. pix_fmt_mp->width = clamp(pix_fmt_mp->width,
  267. MTK_VENC_MIN_W,
  268. MTK_VENC_MAX_W);
  269. /* find next closer width align 16, heign align 32, size align
  270. * 64 rectangle
  271. */
  272. tmp_w = pix_fmt_mp->width;
  273. tmp_h = pix_fmt_mp->height;
  274. v4l_bound_align_image(&pix_fmt_mp->width,
  275. MTK_VENC_MIN_W,
  276. MTK_VENC_MAX_W, 4,
  277. &pix_fmt_mp->height,
  278. MTK_VENC_MIN_H,
  279. MTK_VENC_MAX_H, 5, 6);
  280. if (pix_fmt_mp->width < tmp_w &&
  281. (pix_fmt_mp->width + 16) <= MTK_VENC_MAX_W)
  282. pix_fmt_mp->width += 16;
  283. if (pix_fmt_mp->height < tmp_h &&
  284. (pix_fmt_mp->height + 32) <= MTK_VENC_MAX_H)
  285. pix_fmt_mp->height += 32;
  286. mtk_v4l2_debug(0,
  287. "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d %d",
  288. tmp_w, tmp_h, pix_fmt_mp->width,
  289. pix_fmt_mp->height,
  290. pix_fmt_mp->plane_fmt[0].sizeimage,
  291. pix_fmt_mp->plane_fmt[1].sizeimage);
  292. pix_fmt_mp->num_planes = fmt->num_planes;
  293. pix_fmt_mp->plane_fmt[0].sizeimage =
  294. pix_fmt_mp->width * pix_fmt_mp->height +
  295. ((ALIGN(pix_fmt_mp->width, 16) * 2) * 16);
  296. pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
  297. if (pix_fmt_mp->num_planes == 2) {
  298. pix_fmt_mp->plane_fmt[1].sizeimage =
  299. (pix_fmt_mp->width * pix_fmt_mp->height) / 2 +
  300. (ALIGN(pix_fmt_mp->width, 16) * 16);
  301. pix_fmt_mp->plane_fmt[2].sizeimage = 0;
  302. pix_fmt_mp->plane_fmt[1].bytesperline =
  303. pix_fmt_mp->width;
  304. pix_fmt_mp->plane_fmt[2].bytesperline = 0;
  305. } else if (pix_fmt_mp->num_planes == 3) {
  306. pix_fmt_mp->plane_fmt[1].sizeimage =
  307. pix_fmt_mp->plane_fmt[2].sizeimage =
  308. (pix_fmt_mp->width * pix_fmt_mp->height) / 4 +
  309. ((ALIGN(pix_fmt_mp->width, 16) / 2) * 16);
  310. pix_fmt_mp->plane_fmt[1].bytesperline =
  311. pix_fmt_mp->plane_fmt[2].bytesperline =
  312. pix_fmt_mp->width / 2;
  313. }
  314. }
  315. for (i = 0; i < pix_fmt_mp->num_planes; i++)
  316. memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
  317. sizeof(pix_fmt_mp->plane_fmt[0].reserved));
  318. pix_fmt_mp->flags = 0;
  319. memset(&pix_fmt_mp->reserved, 0x0,
  320. sizeof(pix_fmt_mp->reserved));
  321. return 0;
  322. }
  323. static void mtk_venc_set_param(struct mtk_vcodec_ctx *ctx,
  324. struct venc_enc_param *param)
  325. {
  326. struct mtk_q_data *q_data_src = &ctx->q_data[MTK_Q_DATA_SRC];
  327. struct mtk_enc_params *enc_params = &ctx->enc_params;
  328. switch (q_data_src->fmt->fourcc) {
  329. case V4L2_PIX_FMT_YUV420M:
  330. param->input_yuv_fmt = VENC_YUV_FORMAT_I420;
  331. break;
  332. case V4L2_PIX_FMT_YVU420M:
  333. param->input_yuv_fmt = VENC_YUV_FORMAT_YV12;
  334. break;
  335. case V4L2_PIX_FMT_NV12M:
  336. param->input_yuv_fmt = VENC_YUV_FORMAT_NV12;
  337. break;
  338. case V4L2_PIX_FMT_NV21M:
  339. param->input_yuv_fmt = VENC_YUV_FORMAT_NV21;
  340. break;
  341. default:
  342. mtk_v4l2_err("Unsupport fourcc =%d", q_data_src->fmt->fourcc);
  343. break;
  344. }
  345. param->h264_profile = enc_params->h264_profile;
  346. param->h264_level = enc_params->h264_level;
  347. /* Config visible resolution */
  348. param->width = q_data_src->visible_width;
  349. param->height = q_data_src->visible_height;
  350. /* Config coded resolution */
  351. param->buf_width = q_data_src->coded_width;
  352. param->buf_height = q_data_src->coded_height;
  353. param->frm_rate = enc_params->framerate_num /
  354. enc_params->framerate_denom;
  355. param->intra_period = enc_params->intra_period;
  356. param->gop_size = enc_params->gop_size;
  357. param->bitrate = enc_params->bitrate;
  358. mtk_v4l2_debug(0,
  359. "fmt 0x%x, P/L %d/%d, w/h %d/%d, buf %d/%d, fps/bps %d/%d, gop %d, i_period %d",
  360. param->input_yuv_fmt, param->h264_profile,
  361. param->h264_level, param->width, param->height,
  362. param->buf_width, param->buf_height,
  363. param->frm_rate, param->bitrate,
  364. param->gop_size, param->intra_period);
  365. }
  366. static int vidioc_venc_s_fmt_cap(struct file *file, void *priv,
  367. struct v4l2_format *f)
  368. {
  369. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  370. struct vb2_queue *vq;
  371. struct mtk_q_data *q_data;
  372. int i, ret;
  373. struct mtk_video_fmt *fmt;
  374. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  375. if (!vq) {
  376. mtk_v4l2_err("fail to get vq");
  377. return -EINVAL;
  378. }
  379. if (vb2_is_busy(vq)) {
  380. mtk_v4l2_err("queue busy");
  381. return -EBUSY;
  382. }
  383. q_data = mtk_venc_get_q_data(ctx, f->type);
  384. if (!q_data) {
  385. mtk_v4l2_err("fail to get q data");
  386. return -EINVAL;
  387. }
  388. fmt = mtk_venc_find_format(f);
  389. if (!fmt) {
  390. f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
  391. fmt = mtk_venc_find_format(f);
  392. }
  393. q_data->fmt = fmt;
  394. ret = vidioc_try_fmt(f, q_data->fmt);
  395. if (ret)
  396. return ret;
  397. q_data->coded_width = f->fmt.pix_mp.width;
  398. q_data->coded_height = f->fmt.pix_mp.height;
  399. q_data->field = f->fmt.pix_mp.field;
  400. for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
  401. struct v4l2_plane_pix_format *plane_fmt;
  402. plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
  403. q_data->bytesperline[i] = plane_fmt->bytesperline;
  404. q_data->sizeimage[i] = plane_fmt->sizeimage;
  405. }
  406. if (ctx->state == MTK_STATE_FREE) {
  407. ret = venc_if_init(ctx, q_data->fmt->fourcc);
  408. if (ret) {
  409. mtk_v4l2_err("venc_if_init failed=%d, codec type=%x",
  410. ret, q_data->fmt->fourcc);
  411. return -EBUSY;
  412. }
  413. ctx->state = MTK_STATE_INIT;
  414. }
  415. return 0;
  416. }
  417. static int vidioc_venc_s_fmt_out(struct file *file, void *priv,
  418. struct v4l2_format *f)
  419. {
  420. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  421. struct vb2_queue *vq;
  422. struct mtk_q_data *q_data;
  423. int ret, i;
  424. struct mtk_video_fmt *fmt;
  425. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  426. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  427. if (!vq) {
  428. mtk_v4l2_err("fail to get vq");
  429. return -EINVAL;
  430. }
  431. if (vb2_is_busy(vq)) {
  432. mtk_v4l2_err("queue busy");
  433. return -EBUSY;
  434. }
  435. q_data = mtk_venc_get_q_data(ctx, f->type);
  436. if (!q_data) {
  437. mtk_v4l2_err("fail to get q data");
  438. return -EINVAL;
  439. }
  440. fmt = mtk_venc_find_format(f);
  441. if (!fmt) {
  442. f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
  443. fmt = mtk_venc_find_format(f);
  444. }
  445. pix_fmt_mp->height = clamp(pix_fmt_mp->height,
  446. MTK_VENC_MIN_H,
  447. MTK_VENC_MAX_H);
  448. pix_fmt_mp->width = clamp(pix_fmt_mp->width,
  449. MTK_VENC_MIN_W,
  450. MTK_VENC_MAX_W);
  451. q_data->visible_width = f->fmt.pix_mp.width;
  452. q_data->visible_height = f->fmt.pix_mp.height;
  453. q_data->fmt = fmt;
  454. ret = vidioc_try_fmt(f, q_data->fmt);
  455. if (ret)
  456. return ret;
  457. q_data->coded_width = f->fmt.pix_mp.width;
  458. q_data->coded_height = f->fmt.pix_mp.height;
  459. q_data->field = f->fmt.pix_mp.field;
  460. ctx->colorspace = f->fmt.pix_mp.colorspace;
  461. ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
  462. ctx->quantization = f->fmt.pix_mp.quantization;
  463. ctx->xfer_func = f->fmt.pix_mp.xfer_func;
  464. for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
  465. struct v4l2_plane_pix_format *plane_fmt;
  466. plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
  467. q_data->bytesperline[i] = plane_fmt->bytesperline;
  468. q_data->sizeimage[i] = plane_fmt->sizeimage;
  469. }
  470. return 0;
  471. }
  472. static int vidioc_venc_g_fmt(struct file *file, void *priv,
  473. struct v4l2_format *f)
  474. {
  475. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  476. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  477. struct vb2_queue *vq;
  478. struct mtk_q_data *q_data;
  479. int i;
  480. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  481. if (!vq)
  482. return -EINVAL;
  483. q_data = mtk_venc_get_q_data(ctx, f->type);
  484. pix->width = q_data->coded_width;
  485. pix->height = q_data->coded_height;
  486. pix->pixelformat = q_data->fmt->fourcc;
  487. pix->field = q_data->field;
  488. pix->num_planes = q_data->fmt->num_planes;
  489. for (i = 0; i < pix->num_planes; i++) {
  490. pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
  491. pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
  492. memset(&(pix->plane_fmt[i].reserved[0]), 0x0,
  493. sizeof(pix->plane_fmt[i].reserved));
  494. }
  495. pix->flags = 0;
  496. pix->colorspace = ctx->colorspace;
  497. pix->ycbcr_enc = ctx->ycbcr_enc;
  498. pix->quantization = ctx->quantization;
  499. pix->xfer_func = ctx->xfer_func;
  500. return 0;
  501. }
  502. static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
  503. struct v4l2_format *f)
  504. {
  505. struct mtk_video_fmt *fmt;
  506. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  507. fmt = mtk_venc_find_format(f);
  508. if (!fmt) {
  509. f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
  510. fmt = mtk_venc_find_format(f);
  511. }
  512. f->fmt.pix_mp.colorspace = ctx->colorspace;
  513. f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc;
  514. f->fmt.pix_mp.quantization = ctx->quantization;
  515. f->fmt.pix_mp.xfer_func = ctx->xfer_func;
  516. return vidioc_try_fmt(f, fmt);
  517. }
  518. static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
  519. struct v4l2_format *f)
  520. {
  521. struct mtk_video_fmt *fmt;
  522. fmt = mtk_venc_find_format(f);
  523. if (!fmt) {
  524. f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
  525. fmt = mtk_venc_find_format(f);
  526. }
  527. if (!f->fmt.pix_mp.colorspace) {
  528. f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
  529. f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  530. f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
  531. f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  532. }
  533. return vidioc_try_fmt(f, fmt);
  534. }
  535. static int vidioc_venc_g_selection(struct file *file, void *priv,
  536. struct v4l2_selection *s)
  537. {
  538. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  539. struct mtk_q_data *q_data;
  540. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  541. return -EINVAL;
  542. q_data = mtk_venc_get_q_data(ctx, s->type);
  543. if (!q_data)
  544. return -EINVAL;
  545. switch (s->target) {
  546. case V4L2_SEL_TGT_CROP_DEFAULT:
  547. case V4L2_SEL_TGT_CROP_BOUNDS:
  548. s->r.top = 0;
  549. s->r.left = 0;
  550. s->r.width = q_data->coded_width;
  551. s->r.height = q_data->coded_height;
  552. break;
  553. case V4L2_SEL_TGT_CROP:
  554. s->r.top = 0;
  555. s->r.left = 0;
  556. s->r.width = q_data->visible_width;
  557. s->r.height = q_data->visible_height;
  558. break;
  559. default:
  560. return -EINVAL;
  561. }
  562. return 0;
  563. }
  564. static int vidioc_venc_s_selection(struct file *file, void *priv,
  565. struct v4l2_selection *s)
  566. {
  567. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  568. struct mtk_q_data *q_data;
  569. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  570. return -EINVAL;
  571. q_data = mtk_venc_get_q_data(ctx, s->type);
  572. if (!q_data)
  573. return -EINVAL;
  574. switch (s->target) {
  575. case V4L2_SEL_TGT_CROP:
  576. /* Only support crop from (0,0) */
  577. s->r.top = 0;
  578. s->r.left = 0;
  579. s->r.width = min(s->r.width, q_data->coded_width);
  580. s->r.height = min(s->r.height, q_data->coded_height);
  581. q_data->visible_width = s->r.width;
  582. q_data->visible_height = s->r.height;
  583. break;
  584. default:
  585. return -EINVAL;
  586. }
  587. return 0;
  588. }
  589. static int vidioc_venc_qbuf(struct file *file, void *priv,
  590. struct v4l2_buffer *buf)
  591. {
  592. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  593. if (ctx->state == MTK_STATE_ABORT) {
  594. mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
  595. ctx->id);
  596. return -EIO;
  597. }
  598. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  599. }
  600. static int vidioc_venc_dqbuf(struct file *file, void *priv,
  601. struct v4l2_buffer *buf)
  602. {
  603. struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
  604. if (ctx->state == MTK_STATE_ABORT) {
  605. mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
  606. ctx->id);
  607. return -EIO;
  608. }
  609. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  610. }
  611. const struct v4l2_ioctl_ops mtk_venc_ioctl_ops = {
  612. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  613. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  614. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  615. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  616. .vidioc_qbuf = vidioc_venc_qbuf,
  617. .vidioc_dqbuf = vidioc_venc_dqbuf,
  618. .vidioc_querycap = vidioc_venc_querycap,
  619. .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
  620. .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
  621. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  622. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
  623. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
  624. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  625. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  626. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  627. .vidioc_s_parm = vidioc_venc_s_parm,
  628. .vidioc_g_parm = vidioc_venc_g_parm,
  629. .vidioc_s_fmt_vid_cap_mplane = vidioc_venc_s_fmt_cap,
  630. .vidioc_s_fmt_vid_out_mplane = vidioc_venc_s_fmt_out,
  631. .vidioc_g_fmt_vid_cap_mplane = vidioc_venc_g_fmt,
  632. .vidioc_g_fmt_vid_out_mplane = vidioc_venc_g_fmt,
  633. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  634. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  635. .vidioc_g_selection = vidioc_venc_g_selection,
  636. .vidioc_s_selection = vidioc_venc_s_selection,
  637. };
  638. static int vb2ops_venc_queue_setup(struct vb2_queue *vq,
  639. unsigned int *nbuffers,
  640. unsigned int *nplanes,
  641. unsigned int sizes[],
  642. struct device *alloc_devs[])
  643. {
  644. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
  645. struct mtk_q_data *q_data;
  646. unsigned int i;
  647. q_data = mtk_venc_get_q_data(ctx, vq->type);
  648. if (q_data == NULL)
  649. return -EINVAL;
  650. if (*nplanes) {
  651. for (i = 0; i < *nplanes; i++)
  652. if (sizes[i] < q_data->sizeimage[i])
  653. return -EINVAL;
  654. } else {
  655. *nplanes = q_data->fmt->num_planes;
  656. for (i = 0; i < *nplanes; i++)
  657. sizes[i] = q_data->sizeimage[i];
  658. }
  659. return 0;
  660. }
  661. static int vb2ops_venc_buf_prepare(struct vb2_buffer *vb)
  662. {
  663. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  664. struct mtk_q_data *q_data;
  665. int i;
  666. q_data = mtk_venc_get_q_data(ctx, vb->vb2_queue->type);
  667. for (i = 0; i < q_data->fmt->num_planes; i++) {
  668. if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
  669. mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
  670. i, vb2_plane_size(vb, i),
  671. q_data->sizeimage[i]);
  672. return -EINVAL;
  673. }
  674. }
  675. return 0;
  676. }
  677. static void vb2ops_venc_buf_queue(struct vb2_buffer *vb)
  678. {
  679. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  680. struct vb2_v4l2_buffer *vb2_v4l2 =
  681. container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
  682. struct mtk_video_enc_buf *mtk_buf =
  683. container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
  684. if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
  685. (ctx->param_change != MTK_ENCODE_PARAM_NONE)) {
  686. mtk_v4l2_debug(1, "[%d] Before id=%d encode parameter change %x",
  687. ctx->id,
  688. mtk_buf->vb.vb2_buf.index,
  689. ctx->param_change);
  690. mtk_buf->param_change = ctx->param_change;
  691. mtk_buf->enc_params = ctx->enc_params;
  692. ctx->param_change = MTK_ENCODE_PARAM_NONE;
  693. }
  694. v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
  695. }
  696. static int vb2ops_venc_start_streaming(struct vb2_queue *q, unsigned int count)
  697. {
  698. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
  699. struct venc_enc_param param;
  700. int ret;
  701. int i;
  702. /* Once state turn into MTK_STATE_ABORT, we need stop_streaming
  703. * to clear it
  704. */
  705. if ((ctx->state == MTK_STATE_ABORT) || (ctx->state == MTK_STATE_FREE)) {
  706. ret = -EIO;
  707. goto err_set_param;
  708. }
  709. /* Do the initialization when both start_streaming have been called */
  710. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  711. if (!vb2_start_streaming_called(&ctx->m2m_ctx->cap_q_ctx.q))
  712. return 0;
  713. } else {
  714. if (!vb2_start_streaming_called(&ctx->m2m_ctx->out_q_ctx.q))
  715. return 0;
  716. }
  717. mtk_venc_set_param(ctx, &param);
  718. ret = venc_if_set_param(ctx, VENC_SET_PARAM_ENC, &param);
  719. if (ret) {
  720. mtk_v4l2_err("venc_if_set_param failed=%d", ret);
  721. ctx->state = MTK_STATE_ABORT;
  722. goto err_set_param;
  723. }
  724. ctx->param_change = MTK_ENCODE_PARAM_NONE;
  725. if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
  726. (ctx->enc_params.seq_hdr_mode !=
  727. V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)) {
  728. ret = venc_if_set_param(ctx,
  729. VENC_SET_PARAM_PREPEND_HEADER,
  730. NULL);
  731. if (ret) {
  732. mtk_v4l2_err("venc_if_set_param failed=%d", ret);
  733. ctx->state = MTK_STATE_ABORT;
  734. goto err_set_param;
  735. }
  736. ctx->state = MTK_STATE_HEADER;
  737. }
  738. return 0;
  739. err_set_param:
  740. for (i = 0; i < q->num_buffers; ++i) {
  741. if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
  742. mtk_v4l2_debug(0, "[%d] id=%d, type=%d, %d -> VB2_BUF_STATE_QUEUED",
  743. ctx->id, i, q->type,
  744. (int)q->bufs[i]->state);
  745. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(q->bufs[i]),
  746. VB2_BUF_STATE_QUEUED);
  747. }
  748. }
  749. return ret;
  750. }
  751. static void vb2ops_venc_stop_streaming(struct vb2_queue *q)
  752. {
  753. struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
  754. struct vb2_buffer *src_buf, *dst_buf;
  755. int ret;
  756. mtk_v4l2_debug(2, "[%d]-> type=%d", ctx->id, q->type);
  757. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  758. while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
  759. dst_buf->planes[0].bytesused = 0;
  760. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
  761. VB2_BUF_STATE_ERROR);
  762. }
  763. } else {
  764. while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx)))
  765. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
  766. VB2_BUF_STATE_ERROR);
  767. }
  768. if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  769. vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q)) ||
  770. (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
  771. vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q))) {
  772. mtk_v4l2_debug(1, "[%d]-> q type %d out=%d cap=%d",
  773. ctx->id, q->type,
  774. vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q),
  775. vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q));
  776. return;
  777. }
  778. /* Release the encoder if both streams are stopped. */
  779. ret = venc_if_deinit(ctx);
  780. if (ret)
  781. mtk_v4l2_err("venc_if_deinit failed=%d", ret);
  782. ctx->state = MTK_STATE_FREE;
  783. }
  784. static const struct vb2_ops mtk_venc_vb2_ops = {
  785. .queue_setup = vb2ops_venc_queue_setup,
  786. .buf_prepare = vb2ops_venc_buf_prepare,
  787. .buf_queue = vb2ops_venc_buf_queue,
  788. .wait_prepare = vb2_ops_wait_prepare,
  789. .wait_finish = vb2_ops_wait_finish,
  790. .start_streaming = vb2ops_venc_start_streaming,
  791. .stop_streaming = vb2ops_venc_stop_streaming,
  792. };
  793. static int mtk_venc_encode_header(void *priv)
  794. {
  795. struct mtk_vcodec_ctx *ctx = priv;
  796. int ret;
  797. struct vb2_buffer *src_buf, *dst_buf;
  798. struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
  799. struct mtk_vcodec_mem bs_buf;
  800. struct venc_done_result enc_result;
  801. dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  802. if (!dst_buf) {
  803. mtk_v4l2_debug(1, "No dst buffer");
  804. return -EINVAL;
  805. }
  806. bs_buf.va = vb2_plane_vaddr(dst_buf, 0);
  807. bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  808. bs_buf.size = (size_t)dst_buf->planes[0].length;
  809. mtk_v4l2_debug(1,
  810. "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu",
  811. ctx->id,
  812. dst_buf->index, bs_buf.va,
  813. (u64)bs_buf.dma_addr,
  814. bs_buf.size);
  815. ret = venc_if_encode(ctx,
  816. VENC_START_OPT_ENCODE_SEQUENCE_HEADER,
  817. NULL, &bs_buf, &enc_result);
  818. if (ret) {
  819. dst_buf->planes[0].bytesused = 0;
  820. ctx->state = MTK_STATE_ABORT;
  821. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
  822. VB2_BUF_STATE_ERROR);
  823. mtk_v4l2_err("venc_if_encode failed=%d", ret);
  824. return -EINVAL;
  825. }
  826. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  827. if (src_buf) {
  828. src_vb2_v4l2 = to_vb2_v4l2_buffer(src_buf);
  829. dst_vb2_v4l2 = to_vb2_v4l2_buffer(dst_buf);
  830. dst_buf->timestamp = src_buf->timestamp;
  831. dst_vb2_v4l2->timecode = src_vb2_v4l2->timecode;
  832. } else {
  833. mtk_v4l2_err("No timestamp for the header buffer.");
  834. }
  835. ctx->state = MTK_STATE_HEADER;
  836. dst_buf->planes[0].bytesused = enc_result.bs_size;
  837. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), VB2_BUF_STATE_DONE);
  838. return 0;
  839. }
  840. static int mtk_venc_param_change(struct mtk_vcodec_ctx *ctx)
  841. {
  842. struct venc_enc_param enc_prm;
  843. struct vb2_buffer *vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  844. struct vb2_v4l2_buffer *vb2_v4l2 =
  845. container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
  846. struct mtk_video_enc_buf *mtk_buf =
  847. container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
  848. int ret = 0;
  849. memset(&enc_prm, 0, sizeof(enc_prm));
  850. if (mtk_buf->param_change == MTK_ENCODE_PARAM_NONE)
  851. return 0;
  852. if (mtk_buf->param_change & MTK_ENCODE_PARAM_BITRATE) {
  853. enc_prm.bitrate = mtk_buf->enc_params.bitrate;
  854. mtk_v4l2_debug(1, "[%d] id=%d, change param br=%d",
  855. ctx->id,
  856. mtk_buf->vb.vb2_buf.index,
  857. enc_prm.bitrate);
  858. ret |= venc_if_set_param(ctx,
  859. VENC_SET_PARAM_ADJUST_BITRATE,
  860. &enc_prm);
  861. }
  862. if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FRAMERATE) {
  863. enc_prm.frm_rate = mtk_buf->enc_params.framerate_num /
  864. mtk_buf->enc_params.framerate_denom;
  865. mtk_v4l2_debug(1, "[%d] id=%d, change param fr=%d",
  866. ctx->id,
  867. mtk_buf->vb.vb2_buf.index,
  868. enc_prm.frm_rate);
  869. ret |= venc_if_set_param(ctx,
  870. VENC_SET_PARAM_ADJUST_FRAMERATE,
  871. &enc_prm);
  872. }
  873. if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_GOP_SIZE) {
  874. enc_prm.gop_size = mtk_buf->enc_params.gop_size;
  875. mtk_v4l2_debug(1, "change param intra period=%d",
  876. enc_prm.gop_size);
  877. ret |= venc_if_set_param(ctx,
  878. VENC_SET_PARAM_GOP_SIZE,
  879. &enc_prm);
  880. }
  881. if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) {
  882. mtk_v4l2_debug(1, "[%d] id=%d, change param force I=%d",
  883. ctx->id,
  884. mtk_buf->vb.vb2_buf.index,
  885. mtk_buf->enc_params.force_intra);
  886. if (mtk_buf->enc_params.force_intra)
  887. ret |= venc_if_set_param(ctx,
  888. VENC_SET_PARAM_FORCE_INTRA,
  889. NULL);
  890. }
  891. mtk_buf->param_change = MTK_ENCODE_PARAM_NONE;
  892. if (ret) {
  893. ctx->state = MTK_STATE_ABORT;
  894. mtk_v4l2_err("venc_if_set_param %d failed=%d",
  895. mtk_buf->param_change, ret);
  896. return -1;
  897. }
  898. return 0;
  899. }
  900. /*
  901. * v4l2_m2m_streamoff() holds dev_mutex and waits mtk_venc_worker()
  902. * to call v4l2_m2m_job_finish().
  903. * If mtk_venc_worker() tries to acquire dev_mutex, it will deadlock.
  904. * So this function must not try to acquire dev->dev_mutex.
  905. * This means v4l2 ioctls and mtk_venc_worker() can run at the same time.
  906. * mtk_venc_worker() should be carefully implemented to avoid bugs.
  907. */
  908. static void mtk_venc_worker(struct work_struct *work)
  909. {
  910. struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
  911. encode_work);
  912. struct vb2_buffer *src_buf, *dst_buf;
  913. struct venc_frm_buf frm_buf;
  914. struct mtk_vcodec_mem bs_buf;
  915. struct venc_done_result enc_result;
  916. int ret, i;
  917. struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
  918. /* check dst_buf, dst_buf may be removed in device_run
  919. * to stored encdoe header so we need check dst_buf and
  920. * call job_finish here to prevent recursion
  921. */
  922. dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  923. if (!dst_buf) {
  924. v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
  925. return;
  926. }
  927. src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  928. memset(&frm_buf, 0, sizeof(frm_buf));
  929. for (i = 0; i < src_buf->num_planes ; i++) {
  930. frm_buf.fb_addr[i].va = vb2_plane_vaddr(src_buf, i);
  931. frm_buf.fb_addr[i].dma_addr =
  932. vb2_dma_contig_plane_dma_addr(src_buf, i);
  933. frm_buf.fb_addr[i].size =
  934. (size_t)src_buf->planes[i].length;
  935. }
  936. bs_buf.va = vb2_plane_vaddr(dst_buf, 0);
  937. bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  938. bs_buf.size = (size_t)dst_buf->planes[0].length;
  939. mtk_v4l2_debug(2,
  940. "Framebuf VA=%p PA=%llx Size=0x%zx;VA=%p PA=0x%llx Size=0x%zx;VA=%p PA=0x%llx Size=%zu",
  941. frm_buf.fb_addr[0].va,
  942. (u64)frm_buf.fb_addr[0].dma_addr,
  943. frm_buf.fb_addr[0].size,
  944. frm_buf.fb_addr[1].va,
  945. (u64)frm_buf.fb_addr[1].dma_addr,
  946. frm_buf.fb_addr[1].size,
  947. frm_buf.fb_addr[2].va,
  948. (u64)frm_buf.fb_addr[2].dma_addr,
  949. frm_buf.fb_addr[2].size);
  950. ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME,
  951. &frm_buf, &bs_buf, &enc_result);
  952. src_vb2_v4l2 = to_vb2_v4l2_buffer(src_buf);
  953. dst_vb2_v4l2 = to_vb2_v4l2_buffer(dst_buf);
  954. dst_buf->timestamp = src_buf->timestamp;
  955. dst_vb2_v4l2->timecode = src_vb2_v4l2->timecode;
  956. if (enc_result.is_key_frm)
  957. dst_vb2_v4l2->flags |= V4L2_BUF_FLAG_KEYFRAME;
  958. if (ret) {
  959. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
  960. VB2_BUF_STATE_ERROR);
  961. dst_buf->planes[0].bytesused = 0;
  962. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
  963. VB2_BUF_STATE_ERROR);
  964. mtk_v4l2_err("venc_if_encode failed=%d", ret);
  965. } else {
  966. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
  967. VB2_BUF_STATE_DONE);
  968. dst_buf->planes[0].bytesused = enc_result.bs_size;
  969. v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
  970. VB2_BUF_STATE_DONE);
  971. mtk_v4l2_debug(2, "venc_if_encode bs size=%d",
  972. enc_result.bs_size);
  973. }
  974. v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
  975. mtk_v4l2_debug(1, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>",
  976. src_buf->index, dst_buf->index, ret,
  977. enc_result.bs_size);
  978. }
  979. static void m2mops_venc_device_run(void *priv)
  980. {
  981. struct mtk_vcodec_ctx *ctx = priv;
  982. if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
  983. (ctx->state != MTK_STATE_HEADER)) {
  984. /* encode h264 sps/pps header */
  985. mtk_venc_encode_header(ctx);
  986. queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
  987. return;
  988. }
  989. mtk_venc_param_change(ctx);
  990. queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
  991. }
  992. static int m2mops_venc_job_ready(void *m2m_priv)
  993. {
  994. struct mtk_vcodec_ctx *ctx = m2m_priv;
  995. if (ctx->state == MTK_STATE_ABORT || ctx->state == MTK_STATE_FREE) {
  996. mtk_v4l2_debug(3, "[%d]Not ready: state=0x%x.",
  997. ctx->id, ctx->state);
  998. return 0;
  999. }
  1000. return 1;
  1001. }
  1002. static void m2mops_venc_job_abort(void *priv)
  1003. {
  1004. struct mtk_vcodec_ctx *ctx = priv;
  1005. ctx->state = MTK_STATE_ABORT;
  1006. }
  1007. static void m2mops_venc_lock(void *m2m_priv)
  1008. {
  1009. struct mtk_vcodec_ctx *ctx = m2m_priv;
  1010. mutex_lock(&ctx->dev->dev_mutex);
  1011. }
  1012. static void m2mops_venc_unlock(void *m2m_priv)
  1013. {
  1014. struct mtk_vcodec_ctx *ctx = m2m_priv;
  1015. mutex_unlock(&ctx->dev->dev_mutex);
  1016. }
  1017. const struct v4l2_m2m_ops mtk_venc_m2m_ops = {
  1018. .device_run = m2mops_venc_device_run,
  1019. .job_ready = m2mops_venc_job_ready,
  1020. .job_abort = m2mops_venc_job_abort,
  1021. .lock = m2mops_venc_lock,
  1022. .unlock = m2mops_venc_unlock,
  1023. };
  1024. void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_ctx *ctx)
  1025. {
  1026. struct mtk_q_data *q_data;
  1027. ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
  1028. ctx->fh.m2m_ctx = ctx->m2m_ctx;
  1029. ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
  1030. INIT_WORK(&ctx->encode_work, mtk_venc_worker);
  1031. ctx->colorspace = V4L2_COLORSPACE_REC709;
  1032. ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  1033. ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
  1034. ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  1035. q_data = &ctx->q_data[MTK_Q_DATA_SRC];
  1036. memset(q_data, 0, sizeof(struct mtk_q_data));
  1037. q_data->visible_width = DFT_CFG_WIDTH;
  1038. q_data->visible_height = DFT_CFG_HEIGHT;
  1039. q_data->coded_width = DFT_CFG_WIDTH;
  1040. q_data->coded_height = DFT_CFG_HEIGHT;
  1041. q_data->field = V4L2_FIELD_NONE;
  1042. q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
  1043. v4l_bound_align_image(&q_data->coded_width,
  1044. MTK_VENC_MIN_W,
  1045. MTK_VENC_MAX_W, 4,
  1046. &q_data->coded_height,
  1047. MTK_VENC_MIN_H,
  1048. MTK_VENC_MAX_H, 5, 6);
  1049. if (q_data->coded_width < DFT_CFG_WIDTH &&
  1050. (q_data->coded_width + 16) <= MTK_VENC_MAX_W)
  1051. q_data->coded_width += 16;
  1052. if (q_data->coded_height < DFT_CFG_HEIGHT &&
  1053. (q_data->coded_height + 32) <= MTK_VENC_MAX_H)
  1054. q_data->coded_height += 32;
  1055. q_data->sizeimage[0] =
  1056. q_data->coded_width * q_data->coded_height+
  1057. ((ALIGN(q_data->coded_width, 16) * 2) * 16);
  1058. q_data->bytesperline[0] = q_data->coded_width;
  1059. q_data->sizeimage[1] =
  1060. (q_data->coded_width * q_data->coded_height) / 2 +
  1061. (ALIGN(q_data->coded_width, 16) * 16);
  1062. q_data->bytesperline[1] = q_data->coded_width;
  1063. q_data = &ctx->q_data[MTK_Q_DATA_DST];
  1064. memset(q_data, 0, sizeof(struct mtk_q_data));
  1065. q_data->coded_width = DFT_CFG_WIDTH;
  1066. q_data->coded_height = DFT_CFG_HEIGHT;
  1067. q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
  1068. q_data->field = V4L2_FIELD_NONE;
  1069. ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
  1070. DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
  1071. ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
  1072. }
  1073. int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx)
  1074. {
  1075. const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops;
  1076. struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
  1077. v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
  1078. v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE,
  1079. 1, 4000000, 1, 4000000);
  1080. v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES,
  1081. 0, 2, 1, 0);
  1082. v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
  1083. 0, 1, 1, 1);
  1084. v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
  1085. 0, 51, 1, 51);
  1086. v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
  1087. 0, 65535, 1, 0);
  1088. v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
  1089. 0, 65535, 1, 0);
  1090. v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
  1091. 0, 1, 1, 0);
  1092. v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
  1093. 0, 0, 0, 0);
  1094. v4l2_ctrl_new_std_menu(handler, ops,
  1095. V4L2_CID_MPEG_VIDEO_HEADER_MODE,
  1096. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
  1097. 0, V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
  1098. v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  1099. V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
  1100. 0, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
  1101. v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  1102. V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
  1103. 0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
  1104. if (handler->error) {
  1105. mtk_v4l2_err("Init control handler fail %d",
  1106. handler->error);
  1107. return handler->error;
  1108. }
  1109. v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
  1110. return 0;
  1111. }
  1112. int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq,
  1113. struct vb2_queue *dst_vq)
  1114. {
  1115. struct mtk_vcodec_ctx *ctx = priv;
  1116. int ret;
  1117. /* Note: VB2_USERPTR works with dma-contig because mt8173
  1118. * support iommu
  1119. * https://patchwork.kernel.org/patch/8335461/
  1120. * https://patchwork.kernel.org/patch/7596181/
  1121. */
  1122. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1123. src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
  1124. src_vq->drv_priv = ctx;
  1125. src_vq->buf_struct_size = sizeof(struct mtk_video_enc_buf);
  1126. src_vq->ops = &mtk_venc_vb2_ops;
  1127. src_vq->mem_ops = &vb2_dma_contig_memops;
  1128. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1129. src_vq->lock = &ctx->dev->dev_mutex;
  1130. src_vq->dev = &ctx->dev->plat_dev->dev;
  1131. ret = vb2_queue_init(src_vq);
  1132. if (ret)
  1133. return ret;
  1134. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1135. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
  1136. dst_vq->drv_priv = ctx;
  1137. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1138. dst_vq->ops = &mtk_venc_vb2_ops;
  1139. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1140. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1141. dst_vq->lock = &ctx->dev->dev_mutex;
  1142. dst_vq->dev = &ctx->dev->plat_dev->dev;
  1143. return vb2_queue_init(dst_vq);
  1144. }
  1145. int mtk_venc_unlock(struct mtk_vcodec_ctx *ctx)
  1146. {
  1147. struct mtk_vcodec_dev *dev = ctx->dev;
  1148. mutex_unlock(&dev->enc_mutex);
  1149. return 0;
  1150. }
  1151. int mtk_venc_lock(struct mtk_vcodec_ctx *ctx)
  1152. {
  1153. struct mtk_vcodec_dev *dev = ctx->dev;
  1154. mutex_lock(&dev->enc_mutex);
  1155. return 0;
  1156. }
  1157. void mtk_vcodec_enc_release(struct mtk_vcodec_ctx *ctx)
  1158. {
  1159. int ret = venc_if_deinit(ctx);
  1160. if (ret)
  1161. mtk_v4l2_err("venc_if_deinit failed=%d", ret);
  1162. ctx->state = MTK_STATE_FREE;
  1163. }