1 // SPDX-License-Identifier: GPL-2.0
3 #include <media/videobuf2-v4l2.h>
4 #include <media/videobuf2-dma-contig.h>
5 #include <media/v4l2-event.h>
6 #include <media/v4l2-mem2mem.h>
7 #include <linux/module.h>
9 #include "mtk_vcodec_drv.h"
10 #include "mtk_vcodec_dec.h"
11 #include "mtk_vcodec_intr.h"
12 #include "mtk_vcodec_util.h"
13 #include "mtk_vcodec_dec_pm.h"
14 #include "vdec_drv_if.h"
17 * struct mtk_stateless_control - CID control type
18 * @cfg: control configuration
19 * @codec_type: codec type (V4L2 pixel format) for CID control type
21 struct mtk_stateless_control {
22 struct v4l2_ctrl_config cfg;
26 static const struct mtk_stateless_control mtk_stateless_controls[] = {
29 .id = V4L2_CID_STATELESS_H264_SPS,
31 .codec_type = V4L2_PIX_FMT_H264_SLICE,
35 .id = V4L2_CID_STATELESS_H264_PPS,
37 .codec_type = V4L2_PIX_FMT_H264_SLICE,
41 .id = V4L2_CID_STATELESS_H264_SCALING_MATRIX,
43 .codec_type = V4L2_PIX_FMT_H264_SLICE,
47 .id = V4L2_CID_STATELESS_H264_DECODE_PARAMS,
49 .codec_type = V4L2_PIX_FMT_H264_SLICE,
53 .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
54 .def = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
55 .max = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
57 BIT(V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
58 BIT(V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED),
60 .codec_type = V4L2_PIX_FMT_H264_SLICE,
64 .id = V4L2_CID_STATELESS_H264_DECODE_MODE,
65 .min = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
66 .def = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
67 .max = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
69 .codec_type = V4L2_PIX_FMT_H264_SLICE,
73 .id = V4L2_CID_STATELESS_H264_START_CODE,
74 .min = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
75 .def = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
76 .max = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
78 .codec_type = V4L2_PIX_FMT_H264_SLICE,
82 .id = V4L2_CID_STATELESS_VP8_FRAME,
84 .codec_type = V4L2_PIX_FMT_VP8_FRAME,
88 .id = V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
89 .min = V4L2_MPEG_VIDEO_VP8_PROFILE_0,
90 .def = V4L2_MPEG_VIDEO_VP8_PROFILE_0,
91 .max = V4L2_MPEG_VIDEO_VP8_PROFILE_3,
93 .codec_type = V4L2_PIX_FMT_VP8_FRAME,
97 .id = V4L2_CID_STATELESS_VP9_FRAME,
99 .codec_type = V4L2_PIX_FMT_VP9_FRAME,
103 .id = V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
104 .min = V4L2_MPEG_VIDEO_VP9_PROFILE_0,
105 .def = V4L2_MPEG_VIDEO_VP9_PROFILE_0,
106 .max = V4L2_MPEG_VIDEO_VP9_PROFILE_3,
108 .codec_type = V4L2_PIX_FMT_VP9_FRAME,
112 #define NUM_CTRLS ARRAY_SIZE(mtk_stateless_controls)
114 static struct mtk_video_fmt mtk_video_formats[5];
115 static struct mtk_codec_framesizes mtk_vdec_framesizes[3];
117 static struct mtk_video_fmt default_out_format;
118 static struct mtk_video_fmt default_cap_format;
119 static unsigned int num_formats;
120 static unsigned int num_framesizes;
122 static struct v4l2_frmsize_stepwise stepwise_fhd = {
123 .min_width = MTK_VDEC_MIN_W,
124 .max_width = MTK_VDEC_MAX_W,
126 .min_height = MTK_VDEC_MIN_H,
127 .max_height = MTK_VDEC_MAX_H,
131 static void mtk_vdec_stateless_cap_to_disp(struct mtk_vcodec_ctx *ctx, int error,
132 struct media_request *src_buf_req)
134 struct vb2_v4l2_buffer *vb2_dst;
135 enum vb2_buffer_state state;
138 state = VB2_BUF_STATE_ERROR;
140 state = VB2_BUF_STATE_DONE;
142 vb2_dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
143 v4l2_m2m_buf_done(vb2_dst, state);
145 mtk_v4l2_debug(2, "free frame buffer id:%d to done list",
146 vb2_dst->vb2_buf.index);
149 v4l2_ctrl_request_complete(src_buf_req, &ctx->ctrl_hdl);
152 static struct vdec_fb *vdec_get_cap_buffer(struct mtk_vcodec_ctx *ctx)
154 struct mtk_video_dec_buf *framebuf;
155 struct vb2_v4l2_buffer *vb2_v4l2;
156 struct vb2_buffer *dst_buf;
159 vb2_v4l2 = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
161 mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
165 dst_buf = &vb2_v4l2->vb2_buf;
166 framebuf = container_of(vb2_v4l2, struct mtk_video_dec_buf, m2m_buf.vb);
168 pfb = &framebuf->frame_buffer;
169 pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
170 pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
171 pfb->base_y.size = ctx->q_data[MTK_Q_DATA_DST].sizeimage[0];
173 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2) {
174 pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
175 pfb->base_c.dma_addr =
176 vb2_dma_contig_plane_dma_addr(dst_buf, 1);
177 pfb->base_c.size = ctx->q_data[MTK_Q_DATA_DST].sizeimage[1];
179 mtk_v4l2_debug(1, "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx frame_count = %d",
180 dst_buf->index, pfb, pfb->base_y.va, &pfb->base_y.dma_addr,
181 &pfb->base_c.dma_addr, pfb->base_y.size, ctx->decoded_frame_cnt);
186 static void vb2ops_vdec_buf_request_complete(struct vb2_buffer *vb)
188 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
190 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_hdl);
193 static void mtk_vdec_worker(struct work_struct *work)
195 struct mtk_vcodec_ctx *ctx =
196 container_of(work, struct mtk_vcodec_ctx, decode_work);
197 struct mtk_vcodec_dev *dev = ctx->dev;
198 struct vb2_v4l2_buffer *vb2_v4l2_src;
199 struct vb2_buffer *vb2_src;
200 struct mtk_vcodec_mem *bs_src;
201 struct mtk_video_dec_buf *dec_buf_src;
202 struct media_request *src_buf_req;
203 enum vb2_buffer_state state;
204 bool res_chg = false;
207 vb2_v4l2_src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
209 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
210 mtk_v4l2_debug(1, "[%d] no available source buffer", ctx->id);
214 vb2_src = &vb2_v4l2_src->vb2_buf;
215 dec_buf_src = container_of(vb2_v4l2_src, struct mtk_video_dec_buf,
217 bs_src = &dec_buf_src->bs_buffer;
219 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p", ctx->id,
220 vb2_src->vb2_queue->type, vb2_src->index, vb2_src);
222 bs_src->va = vb2_plane_vaddr(vb2_src, 0);
223 bs_src->dma_addr = vb2_dma_contig_plane_dma_addr(vb2_src, 0);
224 bs_src->size = (size_t)vb2_src->planes[0].bytesused;
226 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
227 mtk_v4l2_err("[%d] id=%d source buffer is NULL", ctx->id,
232 mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
233 ctx->id, bs_src->va, &bs_src->dma_addr, bs_src->size, vb2_src);
234 /* Apply request controls. */
235 src_buf_req = vb2_src->req_obj.req;
237 v4l2_ctrl_request_setup(src_buf_req, &ctx->ctrl_hdl);
239 mtk_v4l2_err("vb2 buffer media request is NULL");
241 ret = vdec_if_decode(ctx, bs_src, NULL, &res_chg);
243 mtk_v4l2_err(" <===[%d], src_buf[%d] sz=0x%zx pts=%llu vdec_if_decode() ret=%d res_chg=%d===>",
244 ctx->id, vb2_src->index, bs_src->size,
245 vb2_src->timestamp, ret, res_chg);
247 mutex_lock(&ctx->lock);
248 dec_buf_src->error = true;
249 mutex_unlock(&ctx->lock);
253 state = ret ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE;
254 if (!IS_VDEC_LAT_ARCH(dev->vdec_pdata->hw_arch) ||
255 ctx->current_codec == V4L2_PIX_FMT_VP8_FRAME || ret) {
256 v4l2_m2m_buf_done_and_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx, state);
258 v4l2_ctrl_request_complete(src_buf_req, &ctx->ctrl_hdl);
260 v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
261 v4l2_m2m_buf_done(vb2_v4l2_src, state);
262 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
266 static void vb2ops_vdec_stateless_buf_queue(struct vb2_buffer *vb)
268 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
269 struct vb2_v4l2_buffer *vb2_v4l2 = to_vb2_v4l2_buffer(vb);
271 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p", ctx->id, vb->vb2_queue->type, vb->index, vb);
273 mutex_lock(&ctx->lock);
274 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
275 mutex_unlock(&ctx->lock);
276 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
279 /* If an OUTPUT buffer, we may need to update the state */
280 if (ctx->state == MTK_STATE_INIT) {
281 ctx->state = MTK_STATE_HEADER;
282 mtk_v4l2_debug(1, "Init driver from init to header.");
284 mtk_v4l2_debug(3, "[%d] already init driver %d", ctx->id, ctx->state);
288 static int mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
292 return vdec_if_decode(ctx, NULL, NULL, &res_chg);
295 static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
299 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS);
300 if (ctx->ctrl_hdl.error) {
301 mtk_v4l2_err("v4l2_ctrl_handler_init failed\n");
302 return ctx->ctrl_hdl.error;
305 for (i = 0; i < NUM_CTRLS; i++) {
306 struct v4l2_ctrl_config cfg = mtk_stateless_controls[i].cfg;
308 v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &cfg, NULL);
309 if (ctx->ctrl_hdl.error) {
310 mtk_v4l2_err("Adding control %d failed %d", i, ctx->ctrl_hdl.error);
311 return ctx->ctrl_hdl.error;
315 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
320 static int fops_media_request_validate(struct media_request *mreq)
322 const unsigned int buffer_cnt = vb2_request_buffer_cnt(mreq);
324 switch (buffer_cnt) {
326 /* We expect exactly one buffer with the request */
329 mtk_v4l2_debug(1, "No buffer provided with the request");
332 mtk_v4l2_debug(1, "Too many buffers (%d) provided with the request",
337 return vb2_request_validate(mreq);
340 const struct media_device_ops mtk_vcodec_media_ops = {
341 .req_validate = fops_media_request_validate,
342 .req_queue = v4l2_m2m_request_queue,
345 static void mtk_vcodec_add_formats(unsigned int fourcc,
346 struct mtk_vcodec_ctx *ctx)
348 struct mtk_vcodec_dev *dev = ctx->dev;
349 const struct mtk_vcodec_dec_pdata *pdata = dev->vdec_pdata;
350 int count_formats = *pdata->num_formats;
351 int count_framesizes = *pdata->num_framesizes;
354 case V4L2_PIX_FMT_H264_SLICE:
355 case V4L2_PIX_FMT_VP8_FRAME:
356 case V4L2_PIX_FMT_VP9_FRAME:
357 mtk_video_formats[count_formats].fourcc = fourcc;
358 mtk_video_formats[count_formats].type = MTK_FMT_DEC;
359 mtk_video_formats[count_formats].num_planes = 1;
361 mtk_vdec_framesizes[count_framesizes].fourcc = fourcc;
362 mtk_vdec_framesizes[count_framesizes].stepwise = stepwise_fhd;
365 case V4L2_PIX_FMT_MM21:
366 case V4L2_PIX_FMT_MT21C:
367 mtk_video_formats[count_formats].fourcc = fourcc;
368 mtk_video_formats[count_formats].type = MTK_FMT_FRAME;
369 mtk_video_formats[count_formats].num_planes = 2;
372 mtk_v4l2_err("Can not add unsupported format type");
377 mtk_v4l2_debug(3, "num_formats: %d num_frames:%d dec_capability: 0x%x",
378 count_formats, count_framesizes, ctx->dev->dec_capability);
381 static void mtk_vcodec_get_supported_formats(struct mtk_vcodec_ctx *ctx)
383 int cap_format_count = 0, out_format_count = 0;
385 if (num_formats && num_framesizes)
388 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_MM21) {
389 mtk_vcodec_add_formats(V4L2_PIX_FMT_MM21, ctx);
392 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_MT21C) {
393 mtk_vcodec_add_formats(V4L2_PIX_FMT_MT21C, ctx);
396 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_H264_SLICE) {
397 mtk_vcodec_add_formats(V4L2_PIX_FMT_H264_SLICE, ctx);
400 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_VP8_FRAME) {
401 mtk_vcodec_add_formats(V4L2_PIX_FMT_VP8_FRAME, ctx);
404 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_VP9_FRAME) {
405 mtk_vcodec_add_formats(V4L2_PIX_FMT_VP9_FRAME, ctx);
409 if (cap_format_count)
410 default_cap_format = mtk_video_formats[cap_format_count - 1];
411 if (out_format_count)
413 mtk_video_formats[cap_format_count + out_format_count - 1];
416 static void mtk_init_vdec_params(struct mtk_vcodec_ctx *ctx)
418 struct vb2_queue *src_vq;
420 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
421 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
423 if (!ctx->dev->vdec_pdata->is_subdev_supported)
424 ctx->dev->dec_capability |=
425 MTK_VDEC_FORMAT_H264_SLICE | MTK_VDEC_FORMAT_MM21;
426 mtk_vcodec_get_supported_formats(ctx);
428 /* Support request api for output plane */
429 src_vq->supports_requests = true;
430 src_vq->requires_requests = true;
433 static int vb2ops_vdec_out_buf_validate(struct vb2_buffer *vb)
435 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
437 vbuf->field = V4L2_FIELD_NONE;
441 static struct vb2_ops mtk_vdec_request_vb2_ops = {
442 .queue_setup = vb2ops_vdec_queue_setup,
443 .wait_prepare = vb2_ops_wait_prepare,
444 .wait_finish = vb2_ops_wait_finish,
445 .start_streaming = vb2ops_vdec_start_streaming,
446 .stop_streaming = vb2ops_vdec_stop_streaming,
448 .buf_queue = vb2ops_vdec_stateless_buf_queue,
449 .buf_out_validate = vb2ops_vdec_out_buf_validate,
450 .buf_init = vb2ops_vdec_buf_init,
451 .buf_prepare = vb2ops_vdec_buf_prepare,
452 .buf_finish = vb2ops_vdec_buf_finish,
453 .buf_request_complete = vb2ops_vdec_buf_request_complete,
456 const struct mtk_vcodec_dec_pdata mtk_vdec_8183_pdata = {
457 .init_vdec_params = mtk_init_vdec_params,
458 .ctrls_setup = mtk_vcodec_dec_ctrls_setup,
459 .vdec_vb2_ops = &mtk_vdec_request_vb2_ops,
460 .vdec_formats = mtk_video_formats,
461 .num_formats = &num_formats,
462 .default_out_fmt = &default_out_format,
463 .default_cap_fmt = &default_cap_format,
464 .vdec_framesizes = mtk_vdec_framesizes,
465 .num_framesizes = &num_framesizes,
466 .uses_stateless_api = true,
467 .worker = mtk_vdec_worker,
468 .flush_decoder = mtk_vdec_flush_decoder,
469 .cap_to_disp = mtk_vdec_stateless_cap_to_disp,
470 .get_cap_buffer = vdec_get_cap_buffer,
471 .is_subdev_supported = false,
472 .hw_arch = MTK_VDEC_PURE_SINGLE_CORE,
475 /* This platform data is used for one lat and one core architecture. */
476 const struct mtk_vcodec_dec_pdata mtk_lat_sig_core_pdata = {
477 .init_vdec_params = mtk_init_vdec_params,
478 .ctrls_setup = mtk_vcodec_dec_ctrls_setup,
479 .vdec_vb2_ops = &mtk_vdec_request_vb2_ops,
480 .vdec_formats = mtk_video_formats,
481 .num_formats = &num_formats,
482 .default_out_fmt = &default_out_format,
483 .default_cap_fmt = &default_cap_format,
484 .vdec_framesizes = mtk_vdec_framesizes,
485 .num_framesizes = &num_framesizes,
486 .uses_stateless_api = true,
487 .worker = mtk_vdec_worker,
488 .flush_decoder = mtk_vdec_flush_decoder,
489 .cap_to_disp = mtk_vdec_stateless_cap_to_disp,
490 .get_cap_buffer = vdec_get_cap_buffer,
491 .is_subdev_supported = true,
492 .hw_arch = MTK_VDEC_LAT_SINGLE_CORE,
495 const struct mtk_vcodec_dec_pdata mtk_vdec_single_core_pdata = {
496 .init_vdec_params = mtk_init_vdec_params,
497 .ctrls_setup = mtk_vcodec_dec_ctrls_setup,
498 .vdec_vb2_ops = &mtk_vdec_request_vb2_ops,
499 .vdec_formats = mtk_video_formats,
500 .num_formats = &num_formats,
501 .default_out_fmt = &default_out_format,
502 .default_cap_fmt = &default_cap_format,
503 .vdec_framesizes = mtk_vdec_framesizes,
504 .num_framesizes = &num_framesizes,
505 .uses_stateless_api = true,
506 .worker = mtk_vdec_worker,
507 .flush_decoder = mtk_vdec_flush_decoder,
508 .cap_to_disp = mtk_vdec_stateless_cap_to_disp,
509 .get_cap_buffer = vdec_get_cap_buffer,
510 .is_subdev_supported = true,
511 .hw_arch = MTK_VDEC_PURE_SINGLE_CORE,