2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: PC Chen <pc.chen@mediatek.com>
4 * Tiffany Lin <tiffany.lin@mediatek.com>
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.
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.
16 #include <media/v4l2-event.h>
17 #include <media/v4l2-mem2mem.h>
18 #include <media/videobuf2-dma-contig.h>
20 #include "mtk_vcodec_drv.h"
21 #include "mtk_vcodec_dec.h"
22 #include "mtk_vcodec_intr.h"
23 #include "mtk_vcodec_util.h"
24 #include "vdec_drv_if.h"
25 #include "mtk_vcodec_dec_pm.h"
30 #define MTK_VDEC_MIN_W 64U
31 #define MTK_VDEC_MIN_H 64U
32 #define DFT_CFG_WIDTH MTK_VDEC_MIN_W
33 #define DFT_CFG_HEIGHT MTK_VDEC_MIN_H
35 static struct mtk_video_fmt mtk_video_formats[] = {
37 .fourcc = V4L2_PIX_FMT_H264,
42 .fourcc = V4L2_PIX_FMT_VP8,
47 .fourcc = V4L2_PIX_FMT_VP9,
52 .fourcc = V4L2_PIX_FMT_MT21C,
53 .type = MTK_FMT_FRAME,
58 static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
60 .fourcc = V4L2_PIX_FMT_H264,
61 .stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
62 MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
65 .fourcc = V4L2_PIX_FMT_VP8,
66 .stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
67 MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
70 .fourcc = V4L2_PIX_FMT_VP9,
71 .stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
72 MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
76 #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
77 #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
79 static struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
81 struct mtk_video_fmt *fmt;
84 for (k = 0; k < NUM_FORMATS; k++) {
85 fmt = &mtk_video_formats[k];
86 if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
93 static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
94 enum v4l2_buf_type type)
96 if (V4L2_TYPE_IS_OUTPUT(type))
97 return &ctx->q_data[MTK_Q_DATA_SRC];
99 return &ctx->q_data[MTK_Q_DATA_DST];
103 * This function tries to clean all display buffers, the buffers will return
105 * Note the buffers returned from codec driver may still be in driver's
108 static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
110 struct vdec_fb *disp_frame_buffer = NULL;
111 struct mtk_video_dec_buf *dstbuf;
113 mtk_v4l2_debug(3, "[%d]", ctx->id);
114 if (vdec_if_get_param(ctx,
115 GET_PARAM_DISP_FRAME_BUFFER,
116 &disp_frame_buffer)) {
117 mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
122 if (disp_frame_buffer == NULL) {
123 mtk_v4l2_debug(3, "No display frame buffer");
127 dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
129 mutex_lock(&ctx->lock);
131 vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
132 ctx->picinfo.y_bs_sz);
133 vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
134 ctx->picinfo.c_bs_sz);
136 dstbuf->ready_to_display = true;
139 "[%d]status=%x queue id=%d to done_list %d",
140 ctx->id, disp_frame_buffer->status,
141 dstbuf->vb.vb2_buf.index,
142 dstbuf->queued_in_vb2);
144 v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
145 ctx->decoded_frame_cnt++;
147 mutex_unlock(&ctx->lock);
148 return &dstbuf->vb.vb2_buf;
152 * This function tries to clean all capture buffers that are not used as
153 * reference buffers by codec driver any more
154 * In this case, we need re-queue buffer to vb2 buffer if user space
155 * already returns this buffer to v4l2 or this buffer is just the output of
156 * previous sps/pps/resolution change decode, or do nothing if user
157 * space still owns this buffer
159 static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
161 struct mtk_video_dec_buf *dstbuf;
162 struct vdec_fb *free_frame_buffer = NULL;
164 if (vdec_if_get_param(ctx,
165 GET_PARAM_FREE_FRAME_BUFFER,
166 &free_frame_buffer)) {
167 mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->id);
170 if (free_frame_buffer == NULL) {
171 mtk_v4l2_debug(3, " No free frame buffer");
175 mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
176 ctx->id, free_frame_buffer);
178 dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf,
181 mutex_lock(&ctx->lock);
183 if ((dstbuf->queued_in_vb2) &&
184 (dstbuf->queued_in_v4l2) &&
185 (free_frame_buffer->status == FB_ST_FREE)) {
187 * After decode sps/pps or non-display buffer, we don't
188 * need to return capture buffer to user space, but
189 * just re-queue this capture buffer to vb2 queue.
190 * This reduce overheads that dq/q unused capture
191 * buffer. In this case, queued_in_vb2 = true.
194 "[%d]status=%x queue id=%d to rdy_queue %d",
195 ctx->id, free_frame_buffer->status,
196 dstbuf->vb.vb2_buf.index,
197 dstbuf->queued_in_vb2);
198 v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
199 } else if ((dstbuf->queued_in_vb2 == false) &&
200 (dstbuf->queued_in_v4l2 == true)) {
202 * If buffer in v4l2 driver but not in vb2 queue yet,
203 * and we get this buffer from free_list, it means
204 * that codec driver do not use this buffer as
205 * reference buffer anymore. We should q buffer to vb2
206 * queue, so later work thread could get this buffer
207 * for decode. In this case, queued_in_vb2 = false
208 * means this buffer is not from previous decode
212 "[%d]status=%x queue id=%d to rdy_queue",
213 ctx->id, free_frame_buffer->status,
214 dstbuf->vb.vb2_buf.index);
215 v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
216 dstbuf->queued_in_vb2 = true;
219 * Codec driver do not need to reference this capture
220 * buffer and this buffer is not in v4l2 driver.
221 * Then we don't need to do any thing, just add log when
222 * we need to debug buffer flow.
223 * When this buffer q from user space, it could
224 * directly q to vb2 buffer
226 mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
227 ctx->id, free_frame_buffer->status,
228 dstbuf->vb.vb2_buf.index,
229 dstbuf->queued_in_vb2,
230 dstbuf->queued_in_v4l2);
232 dstbuf->used = false;
234 mutex_unlock(&ctx->lock);
235 return &dstbuf->vb.vb2_buf;
238 static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
240 struct vb2_buffer *framptr;
243 framptr = get_display_buffer(ctx);
247 static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
249 struct vb2_buffer *framptr;
252 framptr = get_free_buffer(ctx);
256 static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
258 static const struct v4l2_event ev_src_ch = {
259 .type = V4L2_EVENT_SOURCE_CHANGE,
260 .u.src_change.changes =
261 V4L2_EVENT_SRC_CH_RESOLUTION,
264 mtk_v4l2_debug(1, "[%d]", ctx->id);
265 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
268 static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
273 ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
275 mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
277 clean_display_buffer(ctx);
278 clean_free_buffer(ctx);
281 static void mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
283 unsigned int dpbsize = 0;
286 if (vdec_if_get_param(ctx,
288 &ctx->last_decoded_picinfo)) {
289 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
294 if (ctx->last_decoded_picinfo.pic_w == 0 ||
295 ctx->last_decoded_picinfo.pic_h == 0 ||
296 ctx->last_decoded_picinfo.buf_w == 0 ||
297 ctx->last_decoded_picinfo.buf_h == 0) {
298 mtk_v4l2_err("Cannot get correct pic info");
302 if ((ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w) ||
303 (ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h))
307 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
308 ctx->id, ctx->last_decoded_picinfo.pic_w,
309 ctx->last_decoded_picinfo.pic_h,
310 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
311 ctx->last_decoded_picinfo.buf_w,
312 ctx->last_decoded_picinfo.buf_h);
314 ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
316 mtk_v4l2_err("Incorrect dpb size, ret=%d", ret);
318 ctx->dpb_size = dpbsize;
321 static void mtk_vdec_worker(struct work_struct *work)
323 struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
325 struct mtk_vcodec_dev *dev = ctx->dev;
326 struct vb2_buffer *src_buf, *dst_buf;
327 struct mtk_vcodec_mem buf;
329 bool res_chg = false;
331 struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
332 struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
334 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
335 if (src_buf == NULL) {
336 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
337 mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id);
341 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
342 if (dst_buf == NULL) {
343 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
344 mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
348 src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
349 src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_dec_buf, vb);
351 dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
352 dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_dec_buf, vb);
354 pfb = &dst_buf_info->frame_buffer;
355 pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
356 pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
357 pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
359 pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
360 pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
361 pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
363 mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
366 "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
368 pfb->base_y.va, &pfb->base_y.dma_addr,
369 &pfb->base_c.dma_addr, pfb->base_y.size);
371 if (src_buf_info->lastframe) {
372 mtk_v4l2_debug(1, "Got empty flush input buffer.");
373 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
375 /* update dst buf status */
376 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
377 mutex_lock(&ctx->lock);
378 dst_buf_info->used = false;
379 mutex_unlock(&ctx->lock);
381 vdec_if_decode(ctx, NULL, NULL, &res_chg);
382 clean_display_buffer(ctx);
383 vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
384 vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
385 dst_vb2_v4l2->flags |= V4L2_BUF_FLAG_LAST;
386 v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
387 clean_free_buffer(ctx);
388 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
391 buf.va = vb2_plane_vaddr(src_buf, 0);
392 buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
393 buf.size = (size_t)src_buf->planes[0].bytesused;
395 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
396 mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
397 ctx->id, src_buf->index);
400 mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
401 ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
402 dst_buf_info->vb.vb2_buf.timestamp
403 = src_buf_info->vb.vb2_buf.timestamp;
404 dst_buf_info->vb.timecode
405 = src_buf_info->vb.timecode;
406 mutex_lock(&ctx->lock);
407 dst_buf_info->used = true;
408 mutex_unlock(&ctx->lock);
409 src_buf_info->used = true;
411 ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
415 " <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
419 src_buf_info->vb.vb2_buf.timestamp,
422 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
423 v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_ERROR);
424 } else if (res_chg == false) {
426 * we only return src buffer with VB2_BUF_STATE_DONE
427 * when decode success without resolution change
429 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
430 v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
433 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
434 clean_display_buffer(ctx);
435 clean_free_buffer(ctx);
437 if (!ret && res_chg) {
438 mtk_vdec_pic_info_update(ctx);
440 * On encountering a resolution change in the stream.
441 * The driver must first process and decode all
442 * remaining buffers from before the resolution change
443 * point, so call flush decode here
445 mtk_vdec_flush_decoder(ctx);
447 * After all buffers containing decoded frames from
448 * before the resolution change point ready to be
449 * dequeued on the CAPTURE queue, the driver sends a
450 * V4L2_EVENT_SOURCE_CHANGE event for source change
451 * type V4L2_EVENT_SRC_CH_RESOLUTION
453 mtk_vdec_queue_res_chg_event(ctx);
455 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
458 static int vidioc_try_decoder_cmd(struct file *file, void *priv,
459 struct v4l2_decoder_cmd *cmd)
462 case V4L2_DEC_CMD_STOP:
463 case V4L2_DEC_CMD_START:
464 if (cmd->flags != 0) {
465 mtk_v4l2_err("cmd->flags=%u", cmd->flags);
476 static int vidioc_decoder_cmd(struct file *file, void *priv,
477 struct v4l2_decoder_cmd *cmd)
479 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
480 struct vb2_queue *src_vq, *dst_vq;
483 ret = vidioc_try_decoder_cmd(file, priv, cmd);
487 mtk_v4l2_debug(1, "decoder cmd=%u", cmd->cmd);
488 dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
489 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
491 case V4L2_DEC_CMD_STOP:
492 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
493 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
494 if (!vb2_is_streaming(src_vq)) {
495 mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
498 if (!vb2_is_streaming(dst_vq)) {
499 mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
502 v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf->vb);
503 v4l2_m2m_try_schedule(ctx->m2m_ctx);
506 case V4L2_DEC_CMD_START:
507 vb2_clear_last_buffer_dequeued(dst_vq);
517 void mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
519 mutex_unlock(&ctx->dev->dec_mutex);
522 void mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
524 mutex_lock(&ctx->dev->dec_mutex);
527 void mtk_vcodec_dec_release(struct mtk_vcodec_ctx *ctx)
530 ctx->state = MTK_STATE_FREE;
533 void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
535 struct mtk_q_data *q_data;
537 ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
538 ctx->fh.m2m_ctx = ctx->m2m_ctx;
539 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
540 INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
541 ctx->colorspace = V4L2_COLORSPACE_REC709;
542 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
543 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
544 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
546 q_data = &ctx->q_data[MTK_Q_DATA_SRC];
547 memset(q_data, 0, sizeof(struct mtk_q_data));
548 q_data->visible_width = DFT_CFG_WIDTH;
549 q_data->visible_height = DFT_CFG_HEIGHT;
550 q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
551 q_data->field = V4L2_FIELD_NONE;
553 q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
554 q_data->bytesperline[0] = 0;
556 q_data = &ctx->q_data[MTK_Q_DATA_DST];
557 memset(q_data, 0, sizeof(struct mtk_q_data));
558 q_data->visible_width = DFT_CFG_WIDTH;
559 q_data->visible_height = DFT_CFG_HEIGHT;
560 q_data->coded_width = DFT_CFG_WIDTH;
561 q_data->coded_height = DFT_CFG_HEIGHT;
562 q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
563 q_data->field = V4L2_FIELD_NONE;
565 v4l_bound_align_image(&q_data->coded_width,
568 &q_data->coded_height,
570 MTK_VDEC_MAX_H, 5, 6);
572 q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
573 q_data->bytesperline[0] = q_data->coded_width;
574 q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
575 q_data->bytesperline[1] = q_data->coded_width;
578 static int vidioc_vdec_qbuf(struct file *file, void *priv,
579 struct v4l2_buffer *buf)
581 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
583 if (ctx->state == MTK_STATE_ABORT) {
584 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
589 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
592 static int vidioc_vdec_dqbuf(struct file *file, void *priv,
593 struct v4l2_buffer *buf)
595 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
597 if (ctx->state == MTK_STATE_ABORT) {
598 mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
603 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
606 static int vidioc_vdec_querycap(struct file *file, void *priv,
607 struct v4l2_capability *cap)
609 strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
610 strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
611 strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
616 static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
617 const struct v4l2_event_subscription *sub)
621 return v4l2_event_subscribe(fh, sub, 2, NULL);
622 case V4L2_EVENT_SOURCE_CHANGE:
623 return v4l2_src_change_event_subscribe(fh, sub);
625 return v4l2_ctrl_subscribe_event(fh, sub);
629 static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt)
631 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
634 pix_fmt_mp->field = V4L2_FIELD_NONE;
636 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
637 pix_fmt_mp->num_planes = 1;
638 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
639 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
642 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
645 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
650 * Find next closer width align 64, heign align 64, size align
652 * Note: This only get default value, the real HW needed value
653 * only available when ctx in MTK_STATE_HEADER state
655 tmp_w = pix_fmt_mp->width;
656 tmp_h = pix_fmt_mp->height;
657 v4l_bound_align_image(&pix_fmt_mp->width,
662 MTK_VDEC_MAX_H, 6, 9);
664 if (pix_fmt_mp->width < tmp_w &&
665 (pix_fmt_mp->width + 64) <= MTK_VDEC_MAX_W)
666 pix_fmt_mp->width += 64;
667 if (pix_fmt_mp->height < tmp_h &&
668 (pix_fmt_mp->height + 64) <= MTK_VDEC_MAX_H)
669 pix_fmt_mp->height += 64;
672 "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
673 tmp_w, tmp_h, pix_fmt_mp->width,
675 pix_fmt_mp->width * pix_fmt_mp->height);
677 pix_fmt_mp->num_planes = fmt->num_planes;
678 pix_fmt_mp->plane_fmt[0].sizeimage =
679 pix_fmt_mp->width * pix_fmt_mp->height;
680 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
682 if (pix_fmt_mp->num_planes == 2) {
683 pix_fmt_mp->plane_fmt[1].sizeimage =
684 (pix_fmt_mp->width * pix_fmt_mp->height) / 2;
685 pix_fmt_mp->plane_fmt[1].bytesperline =
690 for (i = 0; i < pix_fmt_mp->num_planes; i++)
691 memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
692 sizeof(pix_fmt_mp->plane_fmt[0].reserved));
694 pix_fmt_mp->flags = 0;
695 memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved));
699 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
700 struct v4l2_format *f)
702 struct mtk_video_fmt *fmt;
704 fmt = mtk_vdec_find_format(f);
706 f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
707 fmt = mtk_vdec_find_format(f);
710 return vidioc_try_fmt(f, fmt);
713 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
714 struct v4l2_format *f)
716 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
717 struct mtk_video_fmt *fmt;
719 fmt = mtk_vdec_find_format(f);
721 f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
722 fmt = mtk_vdec_find_format(f);
725 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
726 mtk_v4l2_err("sizeimage of output format must be given");
730 return vidioc_try_fmt(f, fmt);
733 static int vidioc_vdec_g_selection(struct file *file, void *priv,
734 struct v4l2_selection *s)
736 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
737 struct mtk_q_data *q_data;
739 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
742 q_data = &ctx->q_data[MTK_Q_DATA_DST];
745 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
748 s->r.width = ctx->picinfo.pic_w;
749 s->r.height = ctx->picinfo.pic_h;
751 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
754 s->r.width = ctx->picinfo.buf_w;
755 s->r.height = ctx->picinfo.buf_h;
757 case V4L2_SEL_TGT_COMPOSE:
758 if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
759 /* set to default value if header info not ready yet*/
762 s->r.width = q_data->visible_width;
763 s->r.height = q_data->visible_height;
770 if (ctx->state < MTK_STATE_HEADER) {
771 /* set to default value if header info not ready yet*/
774 s->r.width = q_data->visible_width;
775 s->r.height = q_data->visible_height;
782 static int vidioc_vdec_s_selection(struct file *file, void *priv,
783 struct v4l2_selection *s)
785 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
787 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
791 case V4L2_SEL_TGT_COMPOSE:
794 s->r.width = ctx->picinfo.pic_w;
795 s->r.height = ctx->picinfo.pic_h;
804 static int vidioc_vdec_s_fmt(struct file *file, void *priv,
805 struct v4l2_format *f)
807 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
808 struct v4l2_pix_format_mplane *pix_mp;
809 struct mtk_q_data *q_data;
811 struct mtk_video_fmt *fmt;
813 mtk_v4l2_debug(3, "[%d]", ctx->id);
815 q_data = mtk_vdec_get_q_data(ctx, f->type);
819 pix_mp = &f->fmt.pix_mp;
820 if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
821 vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
822 mtk_v4l2_err("out_q_ctx buffers already requested");
826 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
827 vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
828 mtk_v4l2_err("cap_q_ctx buffers already requested");
832 fmt = mtk_vdec_find_format(f);
834 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
835 f->fmt.pix.pixelformat =
836 mtk_video_formats[OUT_FMT_IDX].fourcc;
837 fmt = mtk_vdec_find_format(f);
838 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
839 f->fmt.pix.pixelformat =
840 mtk_video_formats[CAP_FMT_IDX].fourcc;
841 fmt = mtk_vdec_find_format(f);
846 vidioc_try_fmt(f, q_data->fmt);
847 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
848 q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
849 q_data->coded_width = pix_mp->width;
850 q_data->coded_height = pix_mp->height;
852 ctx->colorspace = f->fmt.pix_mp.colorspace;
853 ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
854 ctx->quantization = f->fmt.pix_mp.quantization;
855 ctx->xfer_func = f->fmt.pix_mp.xfer_func;
857 if (ctx->state == MTK_STATE_FREE) {
858 ret = vdec_if_init(ctx, q_data->fmt->fourcc);
860 mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
864 ctx->state = MTK_STATE_INIT;
871 static int vidioc_enum_framesizes(struct file *file, void *priv,
872 struct v4l2_frmsizeenum *fsize)
875 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
877 if (fsize->index != 0)
880 for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
881 if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
884 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
885 fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
886 if (!(ctx->dev->dec_capability &
887 VCODEC_CAPABILITY_4K_DISABLED)) {
888 mtk_v4l2_debug(3, "4K is enabled");
889 fsize->stepwise.max_width =
890 VCODEC_DEC_4K_CODED_WIDTH;
891 fsize->stepwise.max_height =
892 VCODEC_DEC_4K_CODED_HEIGHT;
894 mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
895 ctx->dev->dec_capability,
896 fsize->stepwise.min_width,
897 fsize->stepwise.max_width,
898 fsize->stepwise.step_width,
899 fsize->stepwise.min_height,
900 fsize->stepwise.max_height,
901 fsize->stepwise.step_height);
908 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
910 struct mtk_video_fmt *fmt;
913 for (i = 0; i < NUM_FORMATS; i++) {
914 if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
917 (mtk_video_formats[i].type != MTK_FMT_FRAME))
925 if (i == NUM_FORMATS)
928 fmt = &mtk_video_formats[i];
929 f->pixelformat = fmt->fourcc;
934 static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
935 struct v4l2_fmtdesc *f)
937 return vidioc_enum_fmt(f, false);
940 static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
941 struct v4l2_fmtdesc *f)
943 return vidioc_enum_fmt(f, true);
946 static int vidioc_vdec_g_fmt(struct file *file, void *priv,
947 struct v4l2_format *f)
949 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
950 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
951 struct vb2_queue *vq;
952 struct mtk_q_data *q_data;
954 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
956 mtk_v4l2_err("no vb2 queue for type=%d", f->type);
960 q_data = mtk_vdec_get_q_data(ctx, f->type);
962 pix_mp->field = V4L2_FIELD_NONE;
963 pix_mp->colorspace = ctx->colorspace;
964 pix_mp->ycbcr_enc = ctx->ycbcr_enc;
965 pix_mp->quantization = ctx->quantization;
966 pix_mp->xfer_func = ctx->xfer_func;
968 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
969 (ctx->state >= MTK_STATE_HEADER)) {
970 /* Until STREAMOFF is called on the CAPTURE queue
971 * (acknowledging the event), the driver operates as if
972 * the resolution hasn't changed yet.
973 * So we just return picinfo yet, and update picinfo in
974 * stop_streaming hook function
976 q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
977 ctx->picinfo.y_len_sz;
978 q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
979 ctx->picinfo.c_len_sz;
980 q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
981 q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
982 q_data->coded_width = ctx->picinfo.buf_w;
983 q_data->coded_height = ctx->picinfo.buf_h;
986 * Width and height are set to the dimensions
987 * of the movie, the buffer is bigger and
988 * further processing stages should crop to this
991 pix_mp->width = q_data->coded_width;
992 pix_mp->height = q_data->coded_height;
995 * Set pixelformat to the format in which mt vcodec
996 * outputs the decoded frame
998 pix_mp->num_planes = q_data->fmt->num_planes;
999 pix_mp->pixelformat = q_data->fmt->fourcc;
1000 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1001 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1002 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1003 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1005 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1007 * This is run on OUTPUT
1008 * The buffer contains compressed image
1009 * so width and height have no meaning.
1010 * Assign value here to pass v4l2-compliance test
1012 pix_mp->width = q_data->visible_width;
1013 pix_mp->height = q_data->visible_height;
1014 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1015 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1016 pix_mp->pixelformat = q_data->fmt->fourcc;
1017 pix_mp->num_planes = q_data->fmt->num_planes;
1019 pix_mp->width = q_data->coded_width;
1020 pix_mp->height = q_data->coded_height;
1021 pix_mp->num_planes = q_data->fmt->num_planes;
1022 pix_mp->pixelformat = q_data->fmt->fourcc;
1023 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1024 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1025 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1026 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1028 mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
1029 ctx->id, f->type, ctx->state);
1035 static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
1036 unsigned int *nbuffers,
1037 unsigned int *nplanes,
1038 unsigned int sizes[],
1039 struct device *alloc_devs[])
1041 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
1042 struct mtk_q_data *q_data;
1045 q_data = mtk_vdec_get_q_data(ctx, vq->type);
1047 if (q_data == NULL) {
1048 mtk_v4l2_err("vq->type=%d err\n", vq->type);
1053 for (i = 0; i < *nplanes; i++) {
1054 if (sizes[i] < q_data->sizeimage[i])
1058 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1063 for (i = 0; i < *nplanes; i++)
1064 sizes[i] = q_data->sizeimage[i];
1068 "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
1069 ctx->id, vq->type, *nplanes, *nbuffers,
1070 sizes[0], sizes[1]);
1075 static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
1077 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1078 struct mtk_q_data *q_data;
1081 mtk_v4l2_debug(3, "[%d] (%d) id=%d",
1082 ctx->id, vb->vb2_queue->type, vb->index);
1084 q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
1086 for (i = 0; i < q_data->fmt->num_planes; i++) {
1087 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
1088 mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
1089 i, vb2_plane_size(vb, i),
1090 q_data->sizeimage[i]);
1097 static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
1099 struct vb2_buffer *src_buf;
1100 struct mtk_vcodec_mem src_mem;
1101 bool res_chg = false;
1103 unsigned int dpbsize = 1;
1104 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1105 struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
1106 struct mtk_video_dec_buf *buf = NULL;
1108 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
1109 ctx->id, vb->vb2_queue->type,
1112 * check if this buffer is ready to be used after decode
1114 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1115 vb2_v4l2 = to_vb2_v4l2_buffer(vb);
1116 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1117 mutex_lock(&ctx->lock);
1118 if (buf->used == false) {
1119 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
1120 buf->queued_in_vb2 = true;
1121 buf->queued_in_v4l2 = true;
1122 buf->ready_to_display = false;
1124 buf->queued_in_vb2 = false;
1125 buf->queued_in_v4l2 = true;
1126 buf->ready_to_display = false;
1128 mutex_unlock(&ctx->lock);
1132 v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
1134 if (ctx->state != MTK_STATE_INIT) {
1135 mtk_v4l2_debug(3, "[%d] already init driver %d",
1136 ctx->id, ctx->state);
1140 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1142 mtk_v4l2_err("No src buffer");
1145 vb2_v4l2 = to_vb2_v4l2_buffer(src_buf);
1146 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1147 if (buf->lastframe) {
1148 /* This shouldn't happen. Just in case. */
1149 mtk_v4l2_err("Invalid flush buffer.");
1150 v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1154 src_mem.va = vb2_plane_vaddr(src_buf, 0);
1155 src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
1156 src_mem.size = (size_t)src_buf->planes[0].bytesused;
1158 "[%d] buf id=%d va=%p dma=%pad size=%zx",
1159 ctx->id, src_buf->index,
1160 src_mem.va, &src_mem.dma_addr,
1163 ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
1164 if (ret || !res_chg) {
1166 * fb == NULL menas to parse SPS/PPS header or
1167 * resolution info in src_mem. Decode can fail
1168 * if there is no SPS header or picture info
1172 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1173 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
1174 VB2_BUF_STATE_DONE);
1175 mtk_v4l2_debug(ret ? 0 : 1,
1176 "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
1177 ctx->id, src_buf->index,
1178 src_mem.size, ret, res_chg);
1182 if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
1183 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
1188 ctx->last_decoded_picinfo = ctx->picinfo;
1189 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
1190 ctx->picinfo.y_bs_sz +
1191 ctx->picinfo.y_len_sz;
1192 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
1194 ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
1195 ctx->picinfo.c_bs_sz +
1196 ctx->picinfo.c_len_sz;
1197 ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] = ctx->picinfo.buf_w;
1198 mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
1200 ctx->picinfo.buf_w, ctx->picinfo.buf_h,
1201 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1202 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
1203 ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
1205 ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
1207 mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
1209 ctx->dpb_size = dpbsize;
1210 ctx->state = MTK_STATE_HEADER;
1211 mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
1214 static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
1216 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1217 struct vb2_v4l2_buffer *vb2_v4l2;
1218 struct mtk_video_dec_buf *buf;
1220 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1223 vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
1224 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1225 mutex_lock(&ctx->lock);
1226 buf->queued_in_v4l2 = false;
1227 buf->queued_in_vb2 = false;
1228 mutex_unlock(&ctx->lock);
1231 static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
1233 struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
1234 struct vb2_v4l2_buffer, vb2_buf);
1235 struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
1236 struct mtk_video_dec_buf, vb);
1238 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1240 buf->ready_to_display = false;
1241 buf->queued_in_v4l2 = false;
1243 buf->lastframe = false;
1249 static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1251 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1253 if (ctx->state == MTK_STATE_FLUSH)
1254 ctx->state = MTK_STATE_HEADER;
1259 static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
1261 struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
1262 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1264 mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
1265 ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
1267 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1268 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
1269 struct vb2_v4l2_buffer *vb2_v4l2 =
1270 to_vb2_v4l2_buffer(src_buf);
1271 struct mtk_video_dec_buf *buf_info = container_of(
1272 vb2_v4l2, struct mtk_video_dec_buf, vb);
1273 if (!buf_info->lastframe)
1274 v4l2_m2m_buf_done(vb2_v4l2,
1275 VB2_BUF_STATE_ERROR);
1280 if (ctx->state >= MTK_STATE_HEADER) {
1282 /* Until STREAMOFF is called on the CAPTURE queue
1283 * (acknowledging the event), the driver operates
1284 * as if the resolution hasn't changed yet, i.e.
1285 * VIDIOC_G_FMT< etc. return previous resolution.
1286 * So we update picinfo here
1288 ctx->picinfo = ctx->last_decoded_picinfo;
1291 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
1292 ctx->id, ctx->last_decoded_picinfo.pic_w,
1293 ctx->last_decoded_picinfo.pic_h,
1294 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1295 ctx->last_decoded_picinfo.buf_w,
1296 ctx->last_decoded_picinfo.buf_h);
1298 mtk_vdec_flush_decoder(ctx);
1300 ctx->state = MTK_STATE_FLUSH;
1302 while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
1303 vb2_set_plane_payload(dst_buf, 0, 0);
1304 vb2_set_plane_payload(dst_buf, 1, 0);
1305 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
1306 VB2_BUF_STATE_ERROR);
1311 static void m2mops_vdec_device_run(void *priv)
1313 struct mtk_vcodec_ctx *ctx = priv;
1314 struct mtk_vcodec_dev *dev = ctx->dev;
1316 queue_work(dev->decode_workqueue, &ctx->decode_work);
1319 static int m2mops_vdec_job_ready(void *m2m_priv)
1321 struct mtk_vcodec_ctx *ctx = m2m_priv;
1323 mtk_v4l2_debug(3, "[%d]", ctx->id);
1325 if (ctx->state == MTK_STATE_ABORT)
1328 if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
1329 (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
1332 if (ctx->state != MTK_STATE_HEADER)
1338 static void m2mops_vdec_job_abort(void *priv)
1340 struct mtk_vcodec_ctx *ctx = priv;
1342 ctx->state = MTK_STATE_ABORT;
1345 static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
1347 struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
1351 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
1352 if (ctx->state >= MTK_STATE_HEADER) {
1353 ctrl->val = ctx->dpb_size;
1355 mtk_v4l2_debug(0, "Seqinfo not ready");
1365 static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
1366 .g_volatile_ctrl = mtk_vdec_g_v_ctrl,
1369 int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1371 struct v4l2_ctrl *ctrl;
1373 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
1375 ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
1376 &mtk_vcodec_dec_ctrl_ops,
1377 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
1379 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1381 if (ctx->ctrl_hdl.error) {
1382 mtk_v4l2_err("Adding control failed %d",
1383 ctx->ctrl_hdl.error);
1384 return ctx->ctrl_hdl.error;
1387 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1391 static void m2mops_vdec_lock(void *m2m_priv)
1393 struct mtk_vcodec_ctx *ctx = m2m_priv;
1395 mtk_v4l2_debug(3, "[%d]", ctx->id);
1396 mutex_lock(&ctx->dev->dev_mutex);
1399 static void m2mops_vdec_unlock(void *m2m_priv)
1401 struct mtk_vcodec_ctx *ctx = m2m_priv;
1403 mtk_v4l2_debug(3, "[%d]", ctx->id);
1404 mutex_unlock(&ctx->dev->dev_mutex);
1407 const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
1408 .device_run = m2mops_vdec_device_run,
1409 .job_ready = m2mops_vdec_job_ready,
1410 .job_abort = m2mops_vdec_job_abort,
1411 .lock = m2mops_vdec_lock,
1412 .unlock = m2mops_vdec_unlock,
1415 static const struct vb2_ops mtk_vdec_vb2_ops = {
1416 .queue_setup = vb2ops_vdec_queue_setup,
1417 .buf_prepare = vb2ops_vdec_buf_prepare,
1418 .buf_queue = vb2ops_vdec_buf_queue,
1419 .wait_prepare = vb2_ops_wait_prepare,
1420 .wait_finish = vb2_ops_wait_finish,
1421 .buf_init = vb2ops_vdec_buf_init,
1422 .buf_finish = vb2ops_vdec_buf_finish,
1423 .start_streaming = vb2ops_vdec_start_streaming,
1424 .stop_streaming = vb2ops_vdec_stop_streaming,
1427 const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
1428 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1429 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1430 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
1431 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1432 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1434 .vidioc_qbuf = vidioc_vdec_qbuf,
1435 .vidioc_dqbuf = vidioc_vdec_dqbuf,
1437 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
1438 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
1440 .vidioc_s_fmt_vid_cap_mplane = vidioc_vdec_s_fmt,
1441 .vidioc_s_fmt_vid_out_mplane = vidioc_vdec_s_fmt,
1442 .vidioc_g_fmt_vid_cap_mplane = vidioc_vdec_g_fmt,
1443 .vidioc_g_fmt_vid_out_mplane = vidioc_vdec_g_fmt,
1445 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1447 .vidioc_enum_fmt_vid_cap_mplane = vidioc_vdec_enum_fmt_vid_cap_mplane,
1448 .vidioc_enum_fmt_vid_out_mplane = vidioc_vdec_enum_fmt_vid_out_mplane,
1449 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1451 .vidioc_querycap = vidioc_vdec_querycap,
1452 .vidioc_subscribe_event = vidioc_vdec_subscribe_evt,
1453 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1454 .vidioc_g_selection = vidioc_vdec_g_selection,
1455 .vidioc_s_selection = vidioc_vdec_s_selection,
1457 .vidioc_decoder_cmd = vidioc_decoder_cmd,
1458 .vidioc_try_decoder_cmd = vidioc_try_decoder_cmd,
1461 int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
1462 struct vb2_queue *dst_vq)
1464 struct mtk_vcodec_ctx *ctx = priv;
1467 mtk_v4l2_debug(3, "[%d]", ctx->id);
1469 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1470 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1471 src_vq->drv_priv = ctx;
1472 src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1473 src_vq->ops = &mtk_vdec_vb2_ops;
1474 src_vq->mem_ops = &vb2_dma_contig_memops;
1475 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1476 src_vq->lock = &ctx->dev->dev_mutex;
1477 src_vq->dev = &ctx->dev->plat_dev->dev;
1479 ret = vb2_queue_init(src_vq);
1481 mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
1484 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1485 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1486 dst_vq->drv_priv = ctx;
1487 dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1488 dst_vq->ops = &mtk_vdec_vb2_ops;
1489 dst_vq->mem_ops = &vb2_dma_contig_memops;
1490 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1491 dst_vq->lock = &ctx->dev->dev_mutex;
1492 dst_vq->dev = &ctx->dev->plat_dev->dev;
1494 ret = vb2_queue_init(dst_vq);
1496 vb2_queue_release(src_vq);
1497 mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");