perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / platform / mtk-vcodec / mtk_vcodec_dec.c
blob0c8a8b4c4e1cf1683cfeb81e732655a9961a3cdf
1 /*
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"
27 #define OUT_FMT_IDX 0
28 #define CAP_FMT_IDX 3
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,
38 .type = MTK_FMT_DEC,
39 .num_planes = 1,
42 .fourcc = V4L2_PIX_FMT_VP8,
43 .type = MTK_FMT_DEC,
44 .num_planes = 1,
47 .fourcc = V4L2_PIX_FMT_VP9,
48 .type = MTK_FMT_DEC,
49 .num_planes = 1,
52 .fourcc = V4L2_PIX_FMT_MT21C,
53 .type = MTK_FMT_FRAME,
54 .num_planes = 2,
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;
82 unsigned int k;
84 for (k = 0; k < NUM_FORMATS; k++) {
85 fmt = &mtk_video_formats[k];
86 if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
87 return fmt;
90 return NULL;
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
104 * in display order.
105 * Note the buffers returned from codec driver may still be in driver's
106 * reference list.
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",
118 ctx->id);
119 return NULL;
122 if (disp_frame_buffer == NULL) {
123 mtk_v4l2_debug(3, "No display frame buffer");
124 return NULL;
127 dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
128 frame_buffer);
129 mutex_lock(&ctx->lock);
130 if (dstbuf->used) {
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;
138 mtk_v4l2_debug(2,
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);
168 return NULL;
170 if (free_frame_buffer == NULL) {
171 mtk_v4l2_debug(3, " No free frame buffer");
172 return NULL;
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,
179 frame_buffer);
181 mutex_lock(&ctx->lock);
182 if (dstbuf->used) {
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.
193 mtk_v4l2_debug(2,
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
209 * output.
211 mtk_v4l2_debug(2,
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;
217 } else {
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;
242 do {
243 framptr = get_display_buffer(ctx);
244 } while (framptr);
247 static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
249 struct vb2_buffer *framptr;
251 do {
252 framptr = get_free_buffer(ctx);
253 } while (framptr);
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)
270 bool res_chg;
271 int ret = 0;
273 ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
274 if (ret)
275 mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
277 clean_display_buffer(ctx);
278 clean_free_buffer(ctx);
281 static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
283 unsigned int dpbsize = 0;
284 int ret;
286 if (vdec_if_get_param(ctx,
287 GET_PARAM_PIC_INFO,
288 &ctx->last_decoded_picinfo)) {
289 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
290 ctx->id);
291 return -EINVAL;
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");
299 return -EINVAL;
302 if ((ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w) ||
303 (ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h))
304 return 0;
306 mtk_v4l2_debug(1,
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);
315 if (dpbsize == 0)
316 mtk_v4l2_err("Incorrect dpb size, ret=%d", ret);
318 ctx->dpb_size = dpbsize;
320 return ret;
323 static void mtk_vdec_worker(struct work_struct *work)
325 struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
326 decode_work);
327 struct mtk_vcodec_dev *dev = ctx->dev;
328 struct vb2_buffer *src_buf, *dst_buf;
329 struct mtk_vcodec_mem buf;
330 struct vdec_fb *pfb;
331 bool res_chg = false;
332 int ret;
333 struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
334 struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
336 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
337 if (src_buf == NULL) {
338 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
339 mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id);
340 return;
343 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
344 if (dst_buf == NULL) {
345 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
346 mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
347 return;
350 src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf);
351 src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_dec_buf, vb);
353 dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
354 dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_dec_buf, vb);
356 pfb = &dst_buf_info->frame_buffer;
357 pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
358 pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
359 pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
361 pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
362 pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
363 pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
364 pfb->status = 0;
365 mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
367 mtk_v4l2_debug(3,
368 "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
369 dst_buf->index, pfb,
370 pfb->base_y.va, &pfb->base_y.dma_addr,
371 &pfb->base_c.dma_addr, pfb->base_y.size);
373 if (src_buf_info->lastframe) {
374 mtk_v4l2_debug(1, "Got empty flush input buffer.");
375 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
377 /* update dst buf status */
378 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
379 mutex_lock(&ctx->lock);
380 dst_buf_info->used = false;
381 mutex_unlock(&ctx->lock);
383 vdec_if_decode(ctx, NULL, NULL, &res_chg);
384 clean_display_buffer(ctx);
385 vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
386 vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
387 dst_vb2_v4l2->flags |= V4L2_BUF_FLAG_LAST;
388 v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
389 clean_free_buffer(ctx);
390 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
391 return;
393 buf.va = vb2_plane_vaddr(src_buf, 0);
394 buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
395 buf.size = (size_t)src_buf->planes[0].bytesused;
396 if (!buf.va) {
397 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
398 mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
399 ctx->id, src_buf->index);
400 return;
402 mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
403 ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
404 dst_buf_info->vb.vb2_buf.timestamp
405 = src_buf_info->vb.vb2_buf.timestamp;
406 dst_buf_info->vb.timecode
407 = src_buf_info->vb.timecode;
408 mutex_lock(&ctx->lock);
409 dst_buf_info->used = true;
410 mutex_unlock(&ctx->lock);
411 src_buf_info->used = true;
413 ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
415 if (ret) {
416 mtk_v4l2_err(
417 " <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
418 ctx->id,
419 src_buf->index,
420 buf.size,
421 src_buf_info->vb.vb2_buf.timestamp,
422 dst_buf->index,
423 ret, res_chg);
424 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
425 if (ret == -EIO) {
426 mutex_lock(&ctx->lock);
427 src_buf_info->error = true;
428 mutex_unlock(&ctx->lock);
430 v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_ERROR);
431 } else if (res_chg == false) {
433 * we only return src buffer with VB2_BUF_STATE_DONE
434 * when decode success without resolution change
436 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
437 v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
440 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
441 clean_display_buffer(ctx);
442 clean_free_buffer(ctx);
444 if (!ret && res_chg) {
445 mtk_vdec_pic_info_update(ctx);
447 * On encountering a resolution change in the stream.
448 * The driver must first process and decode all
449 * remaining buffers from before the resolution change
450 * point, so call flush decode here
452 mtk_vdec_flush_decoder(ctx);
454 * After all buffers containing decoded frames from
455 * before the resolution change point ready to be
456 * dequeued on the CAPTURE queue, the driver sends a
457 * V4L2_EVENT_SOURCE_CHANGE event for source change
458 * type V4L2_EVENT_SRC_CH_RESOLUTION
460 mtk_vdec_queue_res_chg_event(ctx);
462 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
465 static int vidioc_try_decoder_cmd(struct file *file, void *priv,
466 struct v4l2_decoder_cmd *cmd)
468 switch (cmd->cmd) {
469 case V4L2_DEC_CMD_STOP:
470 case V4L2_DEC_CMD_START:
471 if (cmd->flags != 0) {
472 mtk_v4l2_err("cmd->flags=%u", cmd->flags);
473 return -EINVAL;
475 break;
476 default:
477 return -EINVAL;
479 return 0;
483 static int vidioc_decoder_cmd(struct file *file, void *priv,
484 struct v4l2_decoder_cmd *cmd)
486 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
487 struct vb2_queue *src_vq, *dst_vq;
488 int ret;
490 ret = vidioc_try_decoder_cmd(file, priv, cmd);
491 if (ret)
492 return ret;
494 mtk_v4l2_debug(1, "decoder cmd=%u", cmd->cmd);
495 dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
496 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
497 switch (cmd->cmd) {
498 case V4L2_DEC_CMD_STOP:
499 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
500 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
501 if (!vb2_is_streaming(src_vq)) {
502 mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
503 return 0;
505 if (!vb2_is_streaming(dst_vq)) {
506 mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
507 return 0;
509 v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf->vb);
510 v4l2_m2m_try_schedule(ctx->m2m_ctx);
511 break;
513 case V4L2_DEC_CMD_START:
514 vb2_clear_last_buffer_dequeued(dst_vq);
515 break;
517 default:
518 return -EINVAL;
521 return 0;
524 void mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
526 mutex_unlock(&ctx->dev->dec_mutex);
529 void mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
531 mutex_lock(&ctx->dev->dec_mutex);
534 void mtk_vcodec_dec_release(struct mtk_vcodec_ctx *ctx)
536 vdec_if_deinit(ctx);
537 ctx->state = MTK_STATE_FREE;
540 void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
542 struct mtk_q_data *q_data;
544 ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
545 ctx->fh.m2m_ctx = ctx->m2m_ctx;
546 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
547 INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
548 ctx->colorspace = V4L2_COLORSPACE_REC709;
549 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
550 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
551 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
553 q_data = &ctx->q_data[MTK_Q_DATA_SRC];
554 memset(q_data, 0, sizeof(struct mtk_q_data));
555 q_data->visible_width = DFT_CFG_WIDTH;
556 q_data->visible_height = DFT_CFG_HEIGHT;
557 q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
558 q_data->field = V4L2_FIELD_NONE;
560 q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
561 q_data->bytesperline[0] = 0;
563 q_data = &ctx->q_data[MTK_Q_DATA_DST];
564 memset(q_data, 0, sizeof(struct mtk_q_data));
565 q_data->visible_width = DFT_CFG_WIDTH;
566 q_data->visible_height = DFT_CFG_HEIGHT;
567 q_data->coded_width = DFT_CFG_WIDTH;
568 q_data->coded_height = DFT_CFG_HEIGHT;
569 q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
570 q_data->field = V4L2_FIELD_NONE;
572 v4l_bound_align_image(&q_data->coded_width,
573 MTK_VDEC_MIN_W,
574 MTK_VDEC_MAX_W, 4,
575 &q_data->coded_height,
576 MTK_VDEC_MIN_H,
577 MTK_VDEC_MAX_H, 5, 6);
579 q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
580 q_data->bytesperline[0] = q_data->coded_width;
581 q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
582 q_data->bytesperline[1] = q_data->coded_width;
585 static int vidioc_vdec_qbuf(struct file *file, void *priv,
586 struct v4l2_buffer *buf)
588 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
590 if (ctx->state == MTK_STATE_ABORT) {
591 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
592 ctx->id);
593 return -EIO;
596 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
599 static int vidioc_vdec_dqbuf(struct file *file, void *priv,
600 struct v4l2_buffer *buf)
602 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
604 if (ctx->state == MTK_STATE_ABORT) {
605 mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
606 ctx->id);
607 return -EIO;
610 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
613 static int vidioc_vdec_querycap(struct file *file, void *priv,
614 struct v4l2_capability *cap)
616 strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
617 strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
618 strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
620 return 0;
623 static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
624 const struct v4l2_event_subscription *sub)
626 switch (sub->type) {
627 case V4L2_EVENT_EOS:
628 return v4l2_event_subscribe(fh, sub, 2, NULL);
629 case V4L2_EVENT_SOURCE_CHANGE:
630 return v4l2_src_change_event_subscribe(fh, sub);
631 default:
632 return v4l2_ctrl_subscribe_event(fh, sub);
636 static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt)
638 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
639 int i;
641 pix_fmt_mp->field = V4L2_FIELD_NONE;
643 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
644 pix_fmt_mp->num_planes = 1;
645 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
646 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
647 int tmp_w, tmp_h;
649 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
650 MTK_VDEC_MIN_H,
651 MTK_VDEC_MAX_H);
652 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
653 MTK_VDEC_MIN_W,
654 MTK_VDEC_MAX_W);
657 * Find next closer width align 64, heign align 64, size align
658 * 64 rectangle
659 * Note: This only get default value, the real HW needed value
660 * only available when ctx in MTK_STATE_HEADER state
662 tmp_w = pix_fmt_mp->width;
663 tmp_h = pix_fmt_mp->height;
664 v4l_bound_align_image(&pix_fmt_mp->width,
665 MTK_VDEC_MIN_W,
666 MTK_VDEC_MAX_W, 6,
667 &pix_fmt_mp->height,
668 MTK_VDEC_MIN_H,
669 MTK_VDEC_MAX_H, 6, 9);
671 if (pix_fmt_mp->width < tmp_w &&
672 (pix_fmt_mp->width + 64) <= MTK_VDEC_MAX_W)
673 pix_fmt_mp->width += 64;
674 if (pix_fmt_mp->height < tmp_h &&
675 (pix_fmt_mp->height + 64) <= MTK_VDEC_MAX_H)
676 pix_fmt_mp->height += 64;
678 mtk_v4l2_debug(0,
679 "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
680 tmp_w, tmp_h, pix_fmt_mp->width,
681 pix_fmt_mp->height,
682 pix_fmt_mp->width * pix_fmt_mp->height);
684 pix_fmt_mp->num_planes = fmt->num_planes;
685 pix_fmt_mp->plane_fmt[0].sizeimage =
686 pix_fmt_mp->width * pix_fmt_mp->height;
687 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
689 if (pix_fmt_mp->num_planes == 2) {
690 pix_fmt_mp->plane_fmt[1].sizeimage =
691 (pix_fmt_mp->width * pix_fmt_mp->height) / 2;
692 pix_fmt_mp->plane_fmt[1].bytesperline =
693 pix_fmt_mp->width;
697 for (i = 0; i < pix_fmt_mp->num_planes; i++)
698 memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
699 sizeof(pix_fmt_mp->plane_fmt[0].reserved));
701 pix_fmt_mp->flags = 0;
702 memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved));
703 return 0;
706 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
707 struct v4l2_format *f)
709 struct mtk_video_fmt *fmt;
711 fmt = mtk_vdec_find_format(f);
712 if (!fmt) {
713 f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
714 fmt = mtk_vdec_find_format(f);
717 return vidioc_try_fmt(f, fmt);
720 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
721 struct v4l2_format *f)
723 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
724 struct mtk_video_fmt *fmt;
726 fmt = mtk_vdec_find_format(f);
727 if (!fmt) {
728 f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
729 fmt = mtk_vdec_find_format(f);
732 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
733 mtk_v4l2_err("sizeimage of output format must be given");
734 return -EINVAL;
737 return vidioc_try_fmt(f, fmt);
740 static int vidioc_vdec_g_selection(struct file *file, void *priv,
741 struct v4l2_selection *s)
743 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
744 struct mtk_q_data *q_data;
746 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
747 return -EINVAL;
749 q_data = &ctx->q_data[MTK_Q_DATA_DST];
751 switch (s->target) {
752 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
753 s->r.left = 0;
754 s->r.top = 0;
755 s->r.width = ctx->picinfo.pic_w;
756 s->r.height = ctx->picinfo.pic_h;
757 break;
758 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
759 s->r.left = 0;
760 s->r.top = 0;
761 s->r.width = ctx->picinfo.buf_w;
762 s->r.height = ctx->picinfo.buf_h;
763 break;
764 case V4L2_SEL_TGT_COMPOSE:
765 if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
766 /* set to default value if header info not ready yet*/
767 s->r.left = 0;
768 s->r.top = 0;
769 s->r.width = q_data->visible_width;
770 s->r.height = q_data->visible_height;
772 break;
773 default:
774 return -EINVAL;
777 if (ctx->state < MTK_STATE_HEADER) {
778 /* set to default value if header info not ready yet*/
779 s->r.left = 0;
780 s->r.top = 0;
781 s->r.width = q_data->visible_width;
782 s->r.height = q_data->visible_height;
783 return 0;
786 return 0;
789 static int vidioc_vdec_s_selection(struct file *file, void *priv,
790 struct v4l2_selection *s)
792 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
794 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
795 return -EINVAL;
797 switch (s->target) {
798 case V4L2_SEL_TGT_COMPOSE:
799 s->r.left = 0;
800 s->r.top = 0;
801 s->r.width = ctx->picinfo.pic_w;
802 s->r.height = ctx->picinfo.pic_h;
803 break;
804 default:
805 return -EINVAL;
808 return 0;
811 static int vidioc_vdec_s_fmt(struct file *file, void *priv,
812 struct v4l2_format *f)
814 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
815 struct v4l2_pix_format_mplane *pix_mp;
816 struct mtk_q_data *q_data;
817 int ret = 0;
818 struct mtk_video_fmt *fmt;
820 mtk_v4l2_debug(3, "[%d]", ctx->id);
822 q_data = mtk_vdec_get_q_data(ctx, f->type);
823 if (!q_data)
824 return -EINVAL;
826 pix_mp = &f->fmt.pix_mp;
827 if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
828 vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
829 mtk_v4l2_err("out_q_ctx buffers already requested");
830 ret = -EBUSY;
833 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
834 vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
835 mtk_v4l2_err("cap_q_ctx buffers already requested");
836 ret = -EBUSY;
839 fmt = mtk_vdec_find_format(f);
840 if (fmt == NULL) {
841 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
842 f->fmt.pix.pixelformat =
843 mtk_video_formats[OUT_FMT_IDX].fourcc;
844 fmt = mtk_vdec_find_format(f);
845 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
846 f->fmt.pix.pixelformat =
847 mtk_video_formats[CAP_FMT_IDX].fourcc;
848 fmt = mtk_vdec_find_format(f);
852 q_data->fmt = fmt;
853 vidioc_try_fmt(f, q_data->fmt);
854 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
855 q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
856 q_data->coded_width = pix_mp->width;
857 q_data->coded_height = pix_mp->height;
859 ctx->colorspace = f->fmt.pix_mp.colorspace;
860 ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
861 ctx->quantization = f->fmt.pix_mp.quantization;
862 ctx->xfer_func = f->fmt.pix_mp.xfer_func;
864 if (ctx->state == MTK_STATE_FREE) {
865 ret = vdec_if_init(ctx, q_data->fmt->fourcc);
866 if (ret) {
867 mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
868 ctx->id, ret);
869 return -EINVAL;
871 ctx->state = MTK_STATE_INIT;
875 return 0;
878 static int vidioc_enum_framesizes(struct file *file, void *priv,
879 struct v4l2_frmsizeenum *fsize)
881 int i = 0;
882 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
884 if (fsize->index != 0)
885 return -EINVAL;
887 for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
888 if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
889 continue;
891 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
892 fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
893 if (!(ctx->dev->dec_capability &
894 VCODEC_CAPABILITY_4K_DISABLED)) {
895 mtk_v4l2_debug(3, "4K is enabled");
896 fsize->stepwise.max_width =
897 VCODEC_DEC_4K_CODED_WIDTH;
898 fsize->stepwise.max_height =
899 VCODEC_DEC_4K_CODED_HEIGHT;
901 mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
902 ctx->dev->dec_capability,
903 fsize->stepwise.min_width,
904 fsize->stepwise.max_width,
905 fsize->stepwise.step_width,
906 fsize->stepwise.min_height,
907 fsize->stepwise.max_height,
908 fsize->stepwise.step_height);
909 return 0;
912 return -EINVAL;
915 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
917 struct mtk_video_fmt *fmt;
918 int i, j = 0;
920 for (i = 0; i < NUM_FORMATS; i++) {
921 if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
922 continue;
923 if (!output_queue &&
924 (mtk_video_formats[i].type != MTK_FMT_FRAME))
925 continue;
927 if (j == f->index)
928 break;
929 ++j;
932 if (i == NUM_FORMATS)
933 return -EINVAL;
935 fmt = &mtk_video_formats[i];
936 f->pixelformat = fmt->fourcc;
938 return 0;
941 static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
942 struct v4l2_fmtdesc *f)
944 return vidioc_enum_fmt(f, false);
947 static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
948 struct v4l2_fmtdesc *f)
950 return vidioc_enum_fmt(f, true);
953 static int vidioc_vdec_g_fmt(struct file *file, void *priv,
954 struct v4l2_format *f)
956 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
957 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
958 struct vb2_queue *vq;
959 struct mtk_q_data *q_data;
961 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
962 if (!vq) {
963 mtk_v4l2_err("no vb2 queue for type=%d", f->type);
964 return -EINVAL;
967 q_data = mtk_vdec_get_q_data(ctx, f->type);
969 pix_mp->field = V4L2_FIELD_NONE;
970 pix_mp->colorspace = ctx->colorspace;
971 pix_mp->ycbcr_enc = ctx->ycbcr_enc;
972 pix_mp->quantization = ctx->quantization;
973 pix_mp->xfer_func = ctx->xfer_func;
975 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
976 (ctx->state >= MTK_STATE_HEADER)) {
977 /* Until STREAMOFF is called on the CAPTURE queue
978 * (acknowledging the event), the driver operates as if
979 * the resolution hasn't changed yet.
980 * So we just return picinfo yet, and update picinfo in
981 * stop_streaming hook function
983 q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
984 ctx->picinfo.y_len_sz;
985 q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
986 ctx->picinfo.c_len_sz;
987 q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
988 q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
989 q_data->coded_width = ctx->picinfo.buf_w;
990 q_data->coded_height = ctx->picinfo.buf_h;
993 * Width and height are set to the dimensions
994 * of the movie, the buffer is bigger and
995 * further processing stages should crop to this
996 * rectangle.
998 pix_mp->width = q_data->coded_width;
999 pix_mp->height = q_data->coded_height;
1002 * Set pixelformat to the format in which mt vcodec
1003 * outputs the decoded frame
1005 pix_mp->num_planes = q_data->fmt->num_planes;
1006 pix_mp->pixelformat = q_data->fmt->fourcc;
1007 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1008 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1009 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1010 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1012 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1014 * This is run on OUTPUT
1015 * The buffer contains compressed image
1016 * so width and height have no meaning.
1017 * Assign value here to pass v4l2-compliance test
1019 pix_mp->width = q_data->visible_width;
1020 pix_mp->height = q_data->visible_height;
1021 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1022 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1023 pix_mp->pixelformat = q_data->fmt->fourcc;
1024 pix_mp->num_planes = q_data->fmt->num_planes;
1025 } else {
1026 pix_mp->width = q_data->coded_width;
1027 pix_mp->height = q_data->coded_height;
1028 pix_mp->num_planes = q_data->fmt->num_planes;
1029 pix_mp->pixelformat = q_data->fmt->fourcc;
1030 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1031 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1032 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1033 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1035 mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
1036 ctx->id, f->type, ctx->state);
1039 return 0;
1042 static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
1043 unsigned int *nbuffers,
1044 unsigned int *nplanes,
1045 unsigned int sizes[],
1046 struct device *alloc_devs[])
1048 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
1049 struct mtk_q_data *q_data;
1050 unsigned int i;
1052 q_data = mtk_vdec_get_q_data(ctx, vq->type);
1054 if (q_data == NULL) {
1055 mtk_v4l2_err("vq->type=%d err\n", vq->type);
1056 return -EINVAL;
1059 if (*nplanes) {
1060 for (i = 0; i < *nplanes; i++) {
1061 if (sizes[i] < q_data->sizeimage[i])
1062 return -EINVAL;
1064 } else {
1065 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1066 *nplanes = 2;
1067 else
1068 *nplanes = 1;
1070 for (i = 0; i < *nplanes; i++)
1071 sizes[i] = q_data->sizeimage[i];
1074 mtk_v4l2_debug(1,
1075 "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
1076 ctx->id, vq->type, *nplanes, *nbuffers,
1077 sizes[0], sizes[1]);
1079 return 0;
1082 static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
1084 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1085 struct mtk_q_data *q_data;
1086 int i;
1088 mtk_v4l2_debug(3, "[%d] (%d) id=%d",
1089 ctx->id, vb->vb2_queue->type, vb->index);
1091 q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
1093 for (i = 0; i < q_data->fmt->num_planes; i++) {
1094 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
1095 mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
1096 i, vb2_plane_size(vb, i),
1097 q_data->sizeimage[i]);
1101 return 0;
1104 static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
1106 struct vb2_buffer *src_buf;
1107 struct mtk_vcodec_mem src_mem;
1108 bool res_chg = false;
1109 int ret = 0;
1110 unsigned int dpbsize = 1;
1111 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1112 struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
1113 struct mtk_video_dec_buf *buf = NULL;
1115 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
1116 ctx->id, vb->vb2_queue->type,
1117 vb->index, vb);
1119 * check if this buffer is ready to be used after decode
1121 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1122 vb2_v4l2 = to_vb2_v4l2_buffer(vb);
1123 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1124 mutex_lock(&ctx->lock);
1125 if (buf->used == false) {
1126 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
1127 buf->queued_in_vb2 = true;
1128 buf->queued_in_v4l2 = true;
1129 buf->ready_to_display = false;
1130 } else {
1131 buf->queued_in_vb2 = false;
1132 buf->queued_in_v4l2 = true;
1133 buf->ready_to_display = false;
1135 mutex_unlock(&ctx->lock);
1136 return;
1139 v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
1141 if (ctx->state != MTK_STATE_INIT) {
1142 mtk_v4l2_debug(3, "[%d] already init driver %d",
1143 ctx->id, ctx->state);
1144 return;
1147 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1148 if (!src_buf) {
1149 mtk_v4l2_err("No src buffer");
1150 return;
1152 vb2_v4l2 = to_vb2_v4l2_buffer(src_buf);
1153 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1154 if (buf->lastframe) {
1155 /* This shouldn't happen. Just in case. */
1156 mtk_v4l2_err("Invalid flush buffer.");
1157 v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1158 return;
1161 src_mem.va = vb2_plane_vaddr(src_buf, 0);
1162 src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
1163 src_mem.size = (size_t)src_buf->planes[0].bytesused;
1164 mtk_v4l2_debug(2,
1165 "[%d] buf id=%d va=%p dma=%pad size=%zx",
1166 ctx->id, src_buf->index,
1167 src_mem.va, &src_mem.dma_addr,
1168 src_mem.size);
1170 ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
1171 if (ret || !res_chg) {
1173 * fb == NULL menas to parse SPS/PPS header or
1174 * resolution info in src_mem. Decode can fail
1175 * if there is no SPS header or picture info
1176 * in bs
1179 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1180 if (ret == -EIO) {
1181 mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.",
1182 ctx->id);
1183 ctx->state = MTK_STATE_ABORT;
1184 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
1185 VB2_BUF_STATE_ERROR);
1186 } else {
1187 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
1188 VB2_BUF_STATE_DONE);
1190 mtk_v4l2_debug(ret ? 0 : 1,
1191 "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
1192 ctx->id, src_buf->index,
1193 src_mem.size, ret, res_chg);
1194 return;
1197 if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
1198 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
1199 ctx->id);
1200 return;
1203 ctx->last_decoded_picinfo = ctx->picinfo;
1204 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
1205 ctx->picinfo.y_bs_sz +
1206 ctx->picinfo.y_len_sz;
1207 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
1208 ctx->picinfo.buf_w;
1209 ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
1210 ctx->picinfo.c_bs_sz +
1211 ctx->picinfo.c_len_sz;
1212 ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] = ctx->picinfo.buf_w;
1213 mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
1214 ctx->id,
1215 ctx->picinfo.buf_w, ctx->picinfo.buf_h,
1216 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1217 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
1218 ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
1220 ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
1221 if (dpbsize == 0)
1222 mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
1224 ctx->dpb_size = dpbsize;
1225 ctx->state = MTK_STATE_HEADER;
1226 mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
1228 mtk_vdec_queue_res_chg_event(ctx);
1231 static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
1233 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1234 struct vb2_v4l2_buffer *vb2_v4l2;
1235 struct mtk_video_dec_buf *buf;
1236 bool buf_error;
1238 vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
1239 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1240 mutex_lock(&ctx->lock);
1241 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1242 buf->queued_in_v4l2 = false;
1243 buf->queued_in_vb2 = false;
1245 buf_error = buf->error;
1246 mutex_unlock(&ctx->lock);
1248 if (buf_error) {
1249 mtk_v4l2_err("Unrecoverable error on buffer.");
1250 ctx->state = MTK_STATE_ABORT;
1254 static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
1256 struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
1257 struct vb2_v4l2_buffer, vb2_buf);
1258 struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
1259 struct mtk_video_dec_buf, vb);
1261 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1262 buf->used = false;
1263 buf->ready_to_display = false;
1264 buf->queued_in_v4l2 = false;
1265 } else {
1266 buf->lastframe = false;
1269 return 0;
1272 static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1274 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1276 if (ctx->state == MTK_STATE_FLUSH)
1277 ctx->state = MTK_STATE_HEADER;
1279 return 0;
1282 static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
1284 struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
1285 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1287 mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
1288 ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
1290 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1291 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
1292 struct vb2_v4l2_buffer *vb2_v4l2 =
1293 to_vb2_v4l2_buffer(src_buf);
1294 struct mtk_video_dec_buf *buf_info = container_of(
1295 vb2_v4l2, struct mtk_video_dec_buf, vb);
1296 if (!buf_info->lastframe)
1297 v4l2_m2m_buf_done(vb2_v4l2,
1298 VB2_BUF_STATE_ERROR);
1300 return;
1303 if (ctx->state >= MTK_STATE_HEADER) {
1305 /* Until STREAMOFF is called on the CAPTURE queue
1306 * (acknowledging the event), the driver operates
1307 * as if the resolution hasn't changed yet, i.e.
1308 * VIDIOC_G_FMT< etc. return previous resolution.
1309 * So we update picinfo here
1311 ctx->picinfo = ctx->last_decoded_picinfo;
1313 mtk_v4l2_debug(2,
1314 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
1315 ctx->id, ctx->last_decoded_picinfo.pic_w,
1316 ctx->last_decoded_picinfo.pic_h,
1317 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1318 ctx->last_decoded_picinfo.buf_w,
1319 ctx->last_decoded_picinfo.buf_h);
1321 mtk_vdec_flush_decoder(ctx);
1323 ctx->state = MTK_STATE_FLUSH;
1325 while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
1326 vb2_set_plane_payload(dst_buf, 0, 0);
1327 vb2_set_plane_payload(dst_buf, 1, 0);
1328 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
1329 VB2_BUF_STATE_ERROR);
1334 static void m2mops_vdec_device_run(void *priv)
1336 struct mtk_vcodec_ctx *ctx = priv;
1337 struct mtk_vcodec_dev *dev = ctx->dev;
1339 queue_work(dev->decode_workqueue, &ctx->decode_work);
1342 static int m2mops_vdec_job_ready(void *m2m_priv)
1344 struct mtk_vcodec_ctx *ctx = m2m_priv;
1346 mtk_v4l2_debug(3, "[%d]", ctx->id);
1348 if (ctx->state == MTK_STATE_ABORT)
1349 return 0;
1351 if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
1352 (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
1353 return 0;
1355 if (ctx->state != MTK_STATE_HEADER)
1356 return 0;
1358 return 1;
1361 static void m2mops_vdec_job_abort(void *priv)
1363 struct mtk_vcodec_ctx *ctx = priv;
1365 ctx->state = MTK_STATE_ABORT;
1368 static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
1370 struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
1371 int ret = 0;
1373 switch (ctrl->id) {
1374 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
1375 if (ctx->state >= MTK_STATE_HEADER) {
1376 ctrl->val = ctx->dpb_size;
1377 } else {
1378 mtk_v4l2_debug(0, "Seqinfo not ready");
1379 ctrl->val = 0;
1381 break;
1382 default:
1383 ret = -EINVAL;
1385 return ret;
1388 static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
1389 .g_volatile_ctrl = mtk_vdec_g_v_ctrl,
1392 int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1394 struct v4l2_ctrl *ctrl;
1396 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
1398 ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
1399 &mtk_vcodec_dec_ctrl_ops,
1400 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
1401 0, 32, 1, 1);
1402 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1403 v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl,
1404 &mtk_vcodec_dec_ctrl_ops,
1405 V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
1406 V4L2_MPEG_VIDEO_VP9_PROFILE_0,
1407 0, V4L2_MPEG_VIDEO_VP9_PROFILE_0);
1409 if (ctx->ctrl_hdl.error) {
1410 mtk_v4l2_err("Adding control failed %d",
1411 ctx->ctrl_hdl.error);
1412 return ctx->ctrl_hdl.error;
1415 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1416 return 0;
1419 const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
1420 .device_run = m2mops_vdec_device_run,
1421 .job_ready = m2mops_vdec_job_ready,
1422 .job_abort = m2mops_vdec_job_abort,
1425 static const struct vb2_ops mtk_vdec_vb2_ops = {
1426 .queue_setup = vb2ops_vdec_queue_setup,
1427 .buf_prepare = vb2ops_vdec_buf_prepare,
1428 .buf_queue = vb2ops_vdec_buf_queue,
1429 .wait_prepare = vb2_ops_wait_prepare,
1430 .wait_finish = vb2_ops_wait_finish,
1431 .buf_init = vb2ops_vdec_buf_init,
1432 .buf_finish = vb2ops_vdec_buf_finish,
1433 .start_streaming = vb2ops_vdec_start_streaming,
1434 .stop_streaming = vb2ops_vdec_stop_streaming,
1437 const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
1438 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1439 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1440 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
1441 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1442 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1444 .vidioc_qbuf = vidioc_vdec_qbuf,
1445 .vidioc_dqbuf = vidioc_vdec_dqbuf,
1447 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
1448 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
1450 .vidioc_s_fmt_vid_cap_mplane = vidioc_vdec_s_fmt,
1451 .vidioc_s_fmt_vid_out_mplane = vidioc_vdec_s_fmt,
1452 .vidioc_g_fmt_vid_cap_mplane = vidioc_vdec_g_fmt,
1453 .vidioc_g_fmt_vid_out_mplane = vidioc_vdec_g_fmt,
1455 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1457 .vidioc_enum_fmt_vid_cap_mplane = vidioc_vdec_enum_fmt_vid_cap_mplane,
1458 .vidioc_enum_fmt_vid_out_mplane = vidioc_vdec_enum_fmt_vid_out_mplane,
1459 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1461 .vidioc_querycap = vidioc_vdec_querycap,
1462 .vidioc_subscribe_event = vidioc_vdec_subscribe_evt,
1463 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1464 .vidioc_g_selection = vidioc_vdec_g_selection,
1465 .vidioc_s_selection = vidioc_vdec_s_selection,
1467 .vidioc_decoder_cmd = vidioc_decoder_cmd,
1468 .vidioc_try_decoder_cmd = vidioc_try_decoder_cmd,
1471 int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
1472 struct vb2_queue *dst_vq)
1474 struct mtk_vcodec_ctx *ctx = priv;
1475 int ret = 0;
1477 mtk_v4l2_debug(3, "[%d]", ctx->id);
1479 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1480 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1481 src_vq->drv_priv = ctx;
1482 src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1483 src_vq->ops = &mtk_vdec_vb2_ops;
1484 src_vq->mem_ops = &vb2_dma_contig_memops;
1485 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1486 src_vq->lock = &ctx->dev->dev_mutex;
1487 src_vq->dev = &ctx->dev->plat_dev->dev;
1489 ret = vb2_queue_init(src_vq);
1490 if (ret) {
1491 mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
1492 return ret;
1494 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1495 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1496 dst_vq->drv_priv = ctx;
1497 dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1498 dst_vq->ops = &mtk_vdec_vb2_ops;
1499 dst_vq->mem_ops = &vb2_dma_contig_memops;
1500 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1501 dst_vq->lock = &ctx->dev->dev_mutex;
1502 dst_vq->dev = &ctx->dev->plat_dev->dev;
1504 ret = vb2_queue_init(dst_vq);
1505 if (ret) {
1506 vb2_queue_release(src_vq);
1507 mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
1510 return ret;