sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / platform / mtk-vcodec / mtk_vcodec_dec.c
blob0746592278645d0a8794bfd4e3286a2bd7396a33
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 void 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;
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;
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;
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;
321 static void mtk_vdec_worker(struct work_struct *work)
323 struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
324 decode_work);
325 struct mtk_vcodec_dev *dev = ctx->dev;
326 struct vb2_buffer *src_buf, *dst_buf;
327 struct mtk_vcodec_mem buf;
328 struct vdec_fb *pfb;
329 bool res_chg = false;
330 int ret;
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);
338 return;
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);
345 return;
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 buf.va = vb2_plane_vaddr(src_buf, 0);
355 buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
356 buf.size = (size_t)src_buf->planes[0].bytesused;
357 if (!buf.va) {
358 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
359 mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
360 ctx->id, src_buf->index);
361 return;
364 pfb = &dst_buf_info->frame_buffer;
365 pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
366 pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
367 pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz;
369 pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
370 pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1);
371 pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz;
372 pfb->status = 0;
373 mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
374 mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
375 ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
377 mtk_v4l2_debug(3,
378 "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
379 dst_buf->index, pfb,
380 pfb->base_y.va, &pfb->base_y.dma_addr,
381 &pfb->base_c.dma_addr, pfb->base_y.size);
383 if (src_buf_info->lastframe) {
384 /* update src buf status */
385 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
386 src_buf_info->lastframe = false;
387 v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
389 /* update dst buf status */
390 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
391 dst_buf_info->used = false;
393 vdec_if_decode(ctx, NULL, NULL, &res_chg);
394 clean_display_buffer(ctx);
395 vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
396 vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
397 v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
398 clean_free_buffer(ctx);
399 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
400 return;
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);
413 if (ret) {
414 mtk_v4l2_err(
415 " <===[%d], src_buf[%d]%d sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
416 ctx->id,
417 src_buf->index,
418 src_buf_info->lastframe,
419 buf.size,
420 src_buf_info->vb.vb2_buf.timestamp,
421 dst_buf->index,
422 ret, res_chg);
423 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
424 v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_ERROR);
425 } else if (res_chg == false) {
427 * we only return src buffer with VB2_BUF_STATE_DONE
428 * when decode success without resolution change
430 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
431 v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
434 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
435 clean_display_buffer(ctx);
436 clean_free_buffer(ctx);
438 if (!ret && res_chg) {
439 mtk_vdec_pic_info_update(ctx);
441 * On encountering a resolution change in the stream.
442 * The driver must first process and decode all
443 * remaining buffers from before the resolution change
444 * point, so call flush decode here
446 mtk_vdec_flush_decoder(ctx);
448 * After all buffers containing decoded frames from
449 * before the resolution change point ready to be
450 * dequeued on the CAPTURE queue, the driver sends a
451 * V4L2_EVENT_SOURCE_CHANGE event for source change
452 * type V4L2_EVENT_SRC_CH_RESOLUTION
454 mtk_vdec_queue_res_chg_event(ctx);
456 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
459 void mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
461 mutex_unlock(&ctx->dev->dec_mutex);
464 void mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
466 mutex_lock(&ctx->dev->dec_mutex);
469 void mtk_vcodec_dec_release(struct mtk_vcodec_ctx *ctx)
471 vdec_if_deinit(ctx);
472 ctx->state = MTK_STATE_FREE;
475 void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
477 struct mtk_q_data *q_data;
479 ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
480 ctx->fh.m2m_ctx = ctx->m2m_ctx;
481 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
482 INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
483 ctx->colorspace = V4L2_COLORSPACE_REC709;
484 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
485 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
486 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
488 q_data = &ctx->q_data[MTK_Q_DATA_SRC];
489 memset(q_data, 0, sizeof(struct mtk_q_data));
490 q_data->visible_width = DFT_CFG_WIDTH;
491 q_data->visible_height = DFT_CFG_HEIGHT;
492 q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
493 q_data->field = V4L2_FIELD_NONE;
495 q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
496 q_data->bytesperline[0] = 0;
498 q_data = &ctx->q_data[MTK_Q_DATA_DST];
499 memset(q_data, 0, sizeof(struct mtk_q_data));
500 q_data->visible_width = DFT_CFG_WIDTH;
501 q_data->visible_height = DFT_CFG_HEIGHT;
502 q_data->coded_width = DFT_CFG_WIDTH;
503 q_data->coded_height = DFT_CFG_HEIGHT;
504 q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
505 q_data->field = V4L2_FIELD_NONE;
507 v4l_bound_align_image(&q_data->coded_width,
508 MTK_VDEC_MIN_W,
509 MTK_VDEC_MAX_W, 4,
510 &q_data->coded_height,
511 MTK_VDEC_MIN_H,
512 MTK_VDEC_MAX_H, 5, 6);
514 q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
515 q_data->bytesperline[0] = q_data->coded_width;
516 q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
517 q_data->bytesperline[1] = q_data->coded_width;
520 static int vidioc_vdec_qbuf(struct file *file, void *priv,
521 struct v4l2_buffer *buf)
523 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
524 struct vb2_queue *vq;
525 struct vb2_buffer *vb;
526 struct mtk_video_dec_buf *mtkbuf;
527 struct vb2_v4l2_buffer *vb2_v4l2;
529 if (ctx->state == MTK_STATE_ABORT) {
530 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
531 ctx->id);
532 return -EIO;
535 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
536 if (buf->index >= vq->num_buffers) {
537 mtk_v4l2_debug(1, "buffer index %d out of range", buf->index);
538 return -EINVAL;
540 vb = vq->bufs[buf->index];
541 vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
542 mtkbuf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
544 if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
545 (buf->m.planes[0].bytesused == 0)) {
546 mtkbuf->lastframe = true;
547 mtk_v4l2_debug(1, "[%d] (%d) id=%d lastframe=%d (%d,%d, %d) vb=%p",
548 ctx->id, buf->type, buf->index,
549 mtkbuf->lastframe, buf->bytesused,
550 buf->m.planes[0].bytesused, buf->length,
551 vb);
554 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
557 static int vidioc_vdec_dqbuf(struct file *file, void *priv,
558 struct v4l2_buffer *buf)
560 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
562 if (ctx->state == MTK_STATE_ABORT) {
563 mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
564 ctx->id);
565 return -EIO;
568 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
571 static int vidioc_vdec_querycap(struct file *file, void *priv,
572 struct v4l2_capability *cap)
574 strlcpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
575 strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
576 strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
578 return 0;
581 static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
582 const struct v4l2_event_subscription *sub)
584 switch (sub->type) {
585 case V4L2_EVENT_EOS:
586 return v4l2_event_subscribe(fh, sub, 2, NULL);
587 case V4L2_EVENT_SOURCE_CHANGE:
588 return v4l2_src_change_event_subscribe(fh, sub);
589 default:
590 return v4l2_ctrl_subscribe_event(fh, sub);
594 static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt)
596 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
597 int i;
599 pix_fmt_mp->field = V4L2_FIELD_NONE;
601 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
602 pix_fmt_mp->num_planes = 1;
603 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
604 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
605 int tmp_w, tmp_h;
607 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
608 MTK_VDEC_MIN_H,
609 MTK_VDEC_MAX_H);
610 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
611 MTK_VDEC_MIN_W,
612 MTK_VDEC_MAX_W);
615 * Find next closer width align 64, heign align 64, size align
616 * 64 rectangle
617 * Note: This only get default value, the real HW needed value
618 * only available when ctx in MTK_STATE_HEADER state
620 tmp_w = pix_fmt_mp->width;
621 tmp_h = pix_fmt_mp->height;
622 v4l_bound_align_image(&pix_fmt_mp->width,
623 MTK_VDEC_MIN_W,
624 MTK_VDEC_MAX_W, 6,
625 &pix_fmt_mp->height,
626 MTK_VDEC_MIN_H,
627 MTK_VDEC_MAX_H, 6, 9);
629 if (pix_fmt_mp->width < tmp_w &&
630 (pix_fmt_mp->width + 64) <= MTK_VDEC_MAX_W)
631 pix_fmt_mp->width += 64;
632 if (pix_fmt_mp->height < tmp_h &&
633 (pix_fmt_mp->height + 64) <= MTK_VDEC_MAX_H)
634 pix_fmt_mp->height += 64;
636 mtk_v4l2_debug(0,
637 "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
638 tmp_w, tmp_h, pix_fmt_mp->width,
639 pix_fmt_mp->height,
640 pix_fmt_mp->width * pix_fmt_mp->height);
642 pix_fmt_mp->num_planes = fmt->num_planes;
643 pix_fmt_mp->plane_fmt[0].sizeimage =
644 pix_fmt_mp->width * pix_fmt_mp->height;
645 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
647 if (pix_fmt_mp->num_planes == 2) {
648 pix_fmt_mp->plane_fmt[1].sizeimage =
649 (pix_fmt_mp->width * pix_fmt_mp->height) / 2;
650 pix_fmt_mp->plane_fmt[1].bytesperline =
651 pix_fmt_mp->width;
655 for (i = 0; i < pix_fmt_mp->num_planes; i++)
656 memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
657 sizeof(pix_fmt_mp->plane_fmt[0].reserved));
659 pix_fmt_mp->flags = 0;
660 memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved));
661 return 0;
664 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
665 struct v4l2_format *f)
667 struct mtk_video_fmt *fmt;
669 fmt = mtk_vdec_find_format(f);
670 if (!fmt) {
671 f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
672 fmt = mtk_vdec_find_format(f);
675 return vidioc_try_fmt(f, fmt);
678 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
679 struct v4l2_format *f)
681 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
682 struct mtk_video_fmt *fmt;
684 fmt = mtk_vdec_find_format(f);
685 if (!fmt) {
686 f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
687 fmt = mtk_vdec_find_format(f);
690 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
691 mtk_v4l2_err("sizeimage of output format must be given");
692 return -EINVAL;
695 return vidioc_try_fmt(f, fmt);
698 static int vidioc_vdec_g_selection(struct file *file, void *priv,
699 struct v4l2_selection *s)
701 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
702 struct mtk_q_data *q_data;
704 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
705 return -EINVAL;
707 q_data = &ctx->q_data[MTK_Q_DATA_DST];
709 switch (s->target) {
710 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
711 s->r.left = 0;
712 s->r.top = 0;
713 s->r.width = ctx->picinfo.pic_w;
714 s->r.height = ctx->picinfo.pic_h;
715 break;
716 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
717 s->r.left = 0;
718 s->r.top = 0;
719 s->r.width = ctx->picinfo.buf_w;
720 s->r.height = ctx->picinfo.buf_h;
721 break;
722 case V4L2_SEL_TGT_COMPOSE:
723 if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
724 /* set to default value if header info not ready yet*/
725 s->r.left = 0;
726 s->r.top = 0;
727 s->r.width = q_data->visible_width;
728 s->r.height = q_data->visible_height;
730 break;
731 default:
732 return -EINVAL;
735 if (ctx->state < MTK_STATE_HEADER) {
736 /* set to default value if header info not ready yet*/
737 s->r.left = 0;
738 s->r.top = 0;
739 s->r.width = q_data->visible_width;
740 s->r.height = q_data->visible_height;
741 return 0;
744 return 0;
747 static int vidioc_vdec_s_selection(struct file *file, void *priv,
748 struct v4l2_selection *s)
750 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
752 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
753 return -EINVAL;
755 switch (s->target) {
756 case V4L2_SEL_TGT_COMPOSE:
757 s->r.left = 0;
758 s->r.top = 0;
759 s->r.width = ctx->picinfo.pic_w;
760 s->r.height = ctx->picinfo.pic_h;
761 break;
762 default:
763 return -EINVAL;
766 return 0;
769 static int vidioc_vdec_s_fmt(struct file *file, void *priv,
770 struct v4l2_format *f)
772 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
773 struct v4l2_pix_format_mplane *pix_mp;
774 struct mtk_q_data *q_data;
775 int ret = 0;
776 struct mtk_video_fmt *fmt;
778 mtk_v4l2_debug(3, "[%d]", ctx->id);
780 q_data = mtk_vdec_get_q_data(ctx, f->type);
781 if (!q_data)
782 return -EINVAL;
784 pix_mp = &f->fmt.pix_mp;
785 if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
786 vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
787 mtk_v4l2_err("out_q_ctx buffers already requested");
788 ret = -EBUSY;
791 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
792 vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
793 mtk_v4l2_err("cap_q_ctx buffers already requested");
794 ret = -EBUSY;
797 fmt = mtk_vdec_find_format(f);
798 if (fmt == NULL) {
799 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
800 f->fmt.pix.pixelformat =
801 mtk_video_formats[OUT_FMT_IDX].fourcc;
802 fmt = mtk_vdec_find_format(f);
803 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
804 f->fmt.pix.pixelformat =
805 mtk_video_formats[CAP_FMT_IDX].fourcc;
806 fmt = mtk_vdec_find_format(f);
810 q_data->fmt = fmt;
811 vidioc_try_fmt(f, q_data->fmt);
812 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
813 q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
814 q_data->coded_width = pix_mp->width;
815 q_data->coded_height = pix_mp->height;
817 ctx->colorspace = f->fmt.pix_mp.colorspace;
818 ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
819 ctx->quantization = f->fmt.pix_mp.quantization;
820 ctx->xfer_func = f->fmt.pix_mp.xfer_func;
822 if (ctx->state == MTK_STATE_FREE) {
823 ret = vdec_if_init(ctx, q_data->fmt->fourcc);
824 if (ret) {
825 mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
826 ctx->id, ret);
827 return -EINVAL;
829 ctx->state = MTK_STATE_INIT;
833 return 0;
836 static int vidioc_enum_framesizes(struct file *file, void *priv,
837 struct v4l2_frmsizeenum *fsize)
839 int i = 0;
840 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
842 if (fsize->index != 0)
843 return -EINVAL;
845 for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
846 if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
847 continue;
849 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
850 fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
851 if (!(ctx->dev->dec_capability &
852 VCODEC_CAPABILITY_4K_DISABLED)) {
853 mtk_v4l2_debug(3, "4K is enabled");
854 fsize->stepwise.max_width =
855 VCODEC_DEC_4K_CODED_WIDTH;
856 fsize->stepwise.max_height =
857 VCODEC_DEC_4K_CODED_HEIGHT;
859 mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
860 ctx->dev->dec_capability,
861 fsize->stepwise.min_width,
862 fsize->stepwise.max_width,
863 fsize->stepwise.step_width,
864 fsize->stepwise.min_height,
865 fsize->stepwise.max_height,
866 fsize->stepwise.step_height);
867 return 0;
870 return -EINVAL;
873 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
875 struct mtk_video_fmt *fmt;
876 int i, j = 0;
878 for (i = 0; i < NUM_FORMATS; i++) {
879 if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
880 continue;
881 if (!output_queue &&
882 (mtk_video_formats[i].type != MTK_FMT_FRAME))
883 continue;
885 if (j == f->index)
886 break;
887 ++j;
890 if (i == NUM_FORMATS)
891 return -EINVAL;
893 fmt = &mtk_video_formats[i];
894 f->pixelformat = fmt->fourcc;
896 return 0;
899 static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
900 struct v4l2_fmtdesc *f)
902 return vidioc_enum_fmt(f, false);
905 static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv,
906 struct v4l2_fmtdesc *f)
908 return vidioc_enum_fmt(f, true);
911 static int vidioc_vdec_g_fmt(struct file *file, void *priv,
912 struct v4l2_format *f)
914 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
915 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
916 struct vb2_queue *vq;
917 struct mtk_q_data *q_data;
919 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
920 if (!vq) {
921 mtk_v4l2_err("no vb2 queue for type=%d", f->type);
922 return -EINVAL;
925 q_data = mtk_vdec_get_q_data(ctx, f->type);
927 pix_mp->field = V4L2_FIELD_NONE;
928 pix_mp->colorspace = ctx->colorspace;
929 pix_mp->ycbcr_enc = ctx->ycbcr_enc;
930 pix_mp->quantization = ctx->quantization;
931 pix_mp->xfer_func = ctx->xfer_func;
933 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
934 (ctx->state >= MTK_STATE_HEADER)) {
935 /* Until STREAMOFF is called on the CAPTURE queue
936 * (acknowledging the event), the driver operates as if
937 * the resolution hasn't changed yet.
938 * So we just return picinfo yet, and update picinfo in
939 * stop_streaming hook function
941 q_data->sizeimage[0] = ctx->picinfo.y_bs_sz +
942 ctx->picinfo.y_len_sz;
943 q_data->sizeimage[1] = ctx->picinfo.c_bs_sz +
944 ctx->picinfo.c_len_sz;
945 q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
946 q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
947 q_data->coded_width = ctx->picinfo.buf_w;
948 q_data->coded_height = ctx->picinfo.buf_h;
951 * Width and height are set to the dimensions
952 * of the movie, the buffer is bigger and
953 * further processing stages should crop to this
954 * rectangle.
956 pix_mp->width = q_data->coded_width;
957 pix_mp->height = q_data->coded_height;
960 * Set pixelformat to the format in which mt vcodec
961 * outputs the decoded frame
963 pix_mp->num_planes = q_data->fmt->num_planes;
964 pix_mp->pixelformat = q_data->fmt->fourcc;
965 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
966 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
967 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
968 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
970 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
972 * This is run on OUTPUT
973 * The buffer contains compressed image
974 * so width and height have no meaning.
975 * Assign value here to pass v4l2-compliance test
977 pix_mp->width = q_data->visible_width;
978 pix_mp->height = q_data->visible_height;
979 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
980 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
981 pix_mp->pixelformat = q_data->fmt->fourcc;
982 pix_mp->num_planes = q_data->fmt->num_planes;
983 } else {
984 pix_mp->width = q_data->coded_width;
985 pix_mp->height = q_data->coded_height;
986 pix_mp->num_planes = q_data->fmt->num_planes;
987 pix_mp->pixelformat = q_data->fmt->fourcc;
988 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
989 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
990 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
991 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
993 mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
994 ctx->id, f->type, ctx->state);
997 return 0;
1000 static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
1001 unsigned int *nbuffers,
1002 unsigned int *nplanes,
1003 unsigned int sizes[],
1004 struct device *alloc_devs[])
1006 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
1007 struct mtk_q_data *q_data;
1008 unsigned int i;
1010 q_data = mtk_vdec_get_q_data(ctx, vq->type);
1012 if (q_data == NULL) {
1013 mtk_v4l2_err("vq->type=%d err\n", vq->type);
1014 return -EINVAL;
1017 if (*nplanes) {
1018 for (i = 0; i < *nplanes; i++) {
1019 if (sizes[i] < q_data->sizeimage[i])
1020 return -EINVAL;
1022 } else {
1023 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1024 *nplanes = 2;
1025 else
1026 *nplanes = 1;
1028 for (i = 0; i < *nplanes; i++)
1029 sizes[i] = q_data->sizeimage[i];
1032 mtk_v4l2_debug(1,
1033 "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
1034 ctx->id, vq->type, *nplanes, *nbuffers,
1035 sizes[0], sizes[1]);
1037 return 0;
1040 static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
1042 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1043 struct mtk_q_data *q_data;
1044 int i;
1046 mtk_v4l2_debug(3, "[%d] (%d) id=%d",
1047 ctx->id, vb->vb2_queue->type, vb->index);
1049 q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
1051 for (i = 0; i < q_data->fmt->num_planes; i++) {
1052 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
1053 mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
1054 i, vb2_plane_size(vb, i),
1055 q_data->sizeimage[i]);
1059 return 0;
1062 static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
1064 struct vb2_buffer *src_buf;
1065 struct mtk_vcodec_mem src_mem;
1066 bool res_chg = false;
1067 int ret = 0;
1068 unsigned int dpbsize = 1;
1069 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1070 struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
1071 struct vb2_v4l2_buffer, vb2_buf);
1072 struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
1073 struct mtk_video_dec_buf, vb);
1075 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
1076 ctx->id, vb->vb2_queue->type,
1077 vb->index, vb);
1079 * check if this buffer is ready to be used after decode
1081 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1082 mutex_lock(&ctx->lock);
1083 if (buf->used == false) {
1084 v4l2_m2m_buf_queue(ctx->m2m_ctx,
1085 to_vb2_v4l2_buffer(vb));
1086 buf->queued_in_vb2 = true;
1087 buf->queued_in_v4l2 = true;
1088 buf->ready_to_display = false;
1089 } else {
1090 buf->queued_in_vb2 = false;
1091 buf->queued_in_v4l2 = true;
1092 buf->ready_to_display = false;
1094 mutex_unlock(&ctx->lock);
1095 return;
1098 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
1100 if (ctx->state != MTK_STATE_INIT) {
1101 mtk_v4l2_debug(3, "[%d] already init driver %d",
1102 ctx->id, ctx->state);
1103 return;
1106 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1107 if (!src_buf) {
1108 mtk_v4l2_err("No src buffer");
1109 return;
1112 src_mem.va = vb2_plane_vaddr(src_buf, 0);
1113 src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
1114 src_mem.size = (size_t)src_buf->planes[0].bytesused;
1115 mtk_v4l2_debug(2,
1116 "[%d] buf id=%d va=%p dma=%pad size=%zx",
1117 ctx->id, src_buf->index,
1118 src_mem.va, &src_mem.dma_addr,
1119 src_mem.size);
1121 ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
1122 if (ret || !res_chg) {
1124 * fb == NULL menas to parse SPS/PPS header or
1125 * resolution info in src_mem. Decode can fail
1126 * if there is no SPS header or picture info
1127 * in bs
1129 int log_level = ret ? 0 : 1;
1131 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1132 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
1133 VB2_BUF_STATE_DONE);
1134 mtk_v4l2_debug(log_level,
1135 "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
1136 ctx->id, src_buf->index,
1137 src_mem.size, ret, res_chg);
1138 return;
1141 if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
1142 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
1143 ctx->id);
1144 return;
1147 ctx->last_decoded_picinfo = ctx->picinfo;
1148 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
1149 ctx->picinfo.y_bs_sz +
1150 ctx->picinfo.y_len_sz;
1151 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
1152 ctx->picinfo.buf_w;
1153 ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
1154 ctx->picinfo.c_bs_sz +
1155 ctx->picinfo.c_len_sz;
1156 ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] = ctx->picinfo.buf_w;
1157 mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
1158 ctx->id,
1159 ctx->picinfo.buf_w, ctx->picinfo.buf_h,
1160 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1161 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
1162 ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
1164 ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
1165 if (dpbsize == 0)
1166 mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
1168 ctx->dpb_size = dpbsize;
1169 ctx->state = MTK_STATE_HEADER;
1170 mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
1173 static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
1175 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1176 struct vb2_v4l2_buffer *vb2_v4l2;
1177 struct mtk_video_dec_buf *buf;
1179 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1180 return;
1182 vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
1183 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1184 mutex_lock(&ctx->lock);
1185 buf->queued_in_v4l2 = false;
1186 buf->queued_in_vb2 = false;
1187 mutex_unlock(&ctx->lock);
1190 static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
1192 struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
1193 struct vb2_v4l2_buffer, vb2_buf);
1194 struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
1195 struct mtk_video_dec_buf, vb);
1197 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1198 buf->used = false;
1199 buf->ready_to_display = false;
1200 buf->queued_in_v4l2 = false;
1201 } else {
1202 buf->lastframe = false;
1205 return 0;
1208 static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1210 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1212 if (ctx->state == MTK_STATE_FLUSH)
1213 ctx->state = MTK_STATE_HEADER;
1215 return 0;
1218 static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
1220 struct vb2_buffer *src_buf = NULL, *dst_buf = NULL;
1221 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1223 mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
1224 ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
1226 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1227 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx)))
1228 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
1229 VB2_BUF_STATE_ERROR);
1230 return;
1233 if (ctx->state >= MTK_STATE_HEADER) {
1235 /* Until STREAMOFF is called on the CAPTURE queue
1236 * (acknowledging the event), the driver operates
1237 * as if the resolution hasn't changed yet, i.e.
1238 * VIDIOC_G_FMT< etc. return previous resolution.
1239 * So we update picinfo here
1241 ctx->picinfo = ctx->last_decoded_picinfo;
1243 mtk_v4l2_debug(2,
1244 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
1245 ctx->id, ctx->last_decoded_picinfo.pic_w,
1246 ctx->last_decoded_picinfo.pic_h,
1247 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1248 ctx->last_decoded_picinfo.buf_w,
1249 ctx->last_decoded_picinfo.buf_h);
1251 mtk_vdec_flush_decoder(ctx);
1253 ctx->state = MTK_STATE_FLUSH;
1255 while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
1256 vb2_set_plane_payload(dst_buf, 0, 0);
1257 vb2_set_plane_payload(dst_buf, 1, 0);
1258 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
1259 VB2_BUF_STATE_ERROR);
1264 static void m2mops_vdec_device_run(void *priv)
1266 struct mtk_vcodec_ctx *ctx = priv;
1267 struct mtk_vcodec_dev *dev = ctx->dev;
1269 queue_work(dev->decode_workqueue, &ctx->decode_work);
1272 static int m2mops_vdec_job_ready(void *m2m_priv)
1274 struct mtk_vcodec_ctx *ctx = m2m_priv;
1276 mtk_v4l2_debug(3, "[%d]", ctx->id);
1278 if (ctx->state == MTK_STATE_ABORT)
1279 return 0;
1281 if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
1282 (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
1283 return 0;
1285 if (ctx->state != MTK_STATE_HEADER)
1286 return 0;
1288 return 1;
1291 static void m2mops_vdec_job_abort(void *priv)
1293 struct mtk_vcodec_ctx *ctx = priv;
1295 ctx->state = MTK_STATE_ABORT;
1298 static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
1300 struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
1301 int ret = 0;
1303 switch (ctrl->id) {
1304 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
1305 if (ctx->state >= MTK_STATE_HEADER) {
1306 ctrl->val = ctx->dpb_size;
1307 } else {
1308 mtk_v4l2_debug(0, "Seqinfo not ready");
1309 ctrl->val = 0;
1311 break;
1312 default:
1313 ret = -EINVAL;
1315 return ret;
1318 static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
1319 .g_volatile_ctrl = mtk_vdec_g_v_ctrl,
1322 int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1324 struct v4l2_ctrl *ctrl;
1326 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
1328 ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
1329 &mtk_vcodec_dec_ctrl_ops,
1330 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
1331 0, 32, 1, 1);
1332 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1334 if (ctx->ctrl_hdl.error) {
1335 mtk_v4l2_err("Adding control failed %d",
1336 ctx->ctrl_hdl.error);
1337 return ctx->ctrl_hdl.error;
1340 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1341 return 0;
1344 static void m2mops_vdec_lock(void *m2m_priv)
1346 struct mtk_vcodec_ctx *ctx = m2m_priv;
1348 mtk_v4l2_debug(3, "[%d]", ctx->id);
1349 mutex_lock(&ctx->dev->dev_mutex);
1352 static void m2mops_vdec_unlock(void *m2m_priv)
1354 struct mtk_vcodec_ctx *ctx = m2m_priv;
1356 mtk_v4l2_debug(3, "[%d]", ctx->id);
1357 mutex_unlock(&ctx->dev->dev_mutex);
1360 const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
1361 .device_run = m2mops_vdec_device_run,
1362 .job_ready = m2mops_vdec_job_ready,
1363 .job_abort = m2mops_vdec_job_abort,
1364 .lock = m2mops_vdec_lock,
1365 .unlock = m2mops_vdec_unlock,
1368 static const struct vb2_ops mtk_vdec_vb2_ops = {
1369 .queue_setup = vb2ops_vdec_queue_setup,
1370 .buf_prepare = vb2ops_vdec_buf_prepare,
1371 .buf_queue = vb2ops_vdec_buf_queue,
1372 .wait_prepare = vb2_ops_wait_prepare,
1373 .wait_finish = vb2_ops_wait_finish,
1374 .buf_init = vb2ops_vdec_buf_init,
1375 .buf_finish = vb2ops_vdec_buf_finish,
1376 .start_streaming = vb2ops_vdec_start_streaming,
1377 .stop_streaming = vb2ops_vdec_stop_streaming,
1380 const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
1381 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1382 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1383 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
1384 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1385 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1387 .vidioc_qbuf = vidioc_vdec_qbuf,
1388 .vidioc_dqbuf = vidioc_vdec_dqbuf,
1390 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
1391 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
1393 .vidioc_s_fmt_vid_cap_mplane = vidioc_vdec_s_fmt,
1394 .vidioc_s_fmt_vid_out_mplane = vidioc_vdec_s_fmt,
1395 .vidioc_g_fmt_vid_cap_mplane = vidioc_vdec_g_fmt,
1396 .vidioc_g_fmt_vid_out_mplane = vidioc_vdec_g_fmt,
1398 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1400 .vidioc_enum_fmt_vid_cap_mplane = vidioc_vdec_enum_fmt_vid_cap_mplane,
1401 .vidioc_enum_fmt_vid_out_mplane = vidioc_vdec_enum_fmt_vid_out_mplane,
1402 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1404 .vidioc_querycap = vidioc_vdec_querycap,
1405 .vidioc_subscribe_event = vidioc_vdec_subscribe_evt,
1406 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1407 .vidioc_g_selection = vidioc_vdec_g_selection,
1408 .vidioc_s_selection = vidioc_vdec_s_selection,
1411 int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
1412 struct vb2_queue *dst_vq)
1414 struct mtk_vcodec_ctx *ctx = priv;
1415 int ret = 0;
1417 mtk_v4l2_debug(3, "[%d]", ctx->id);
1419 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1420 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1421 src_vq->drv_priv = ctx;
1422 src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1423 src_vq->ops = &mtk_vdec_vb2_ops;
1424 src_vq->mem_ops = &vb2_dma_contig_memops;
1425 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1426 src_vq->lock = &ctx->dev->dev_mutex;
1427 src_vq->dev = &ctx->dev->plat_dev->dev;
1429 ret = vb2_queue_init(src_vq);
1430 if (ret) {
1431 mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
1432 return ret;
1434 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1435 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1436 dst_vq->drv_priv = ctx;
1437 dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1438 dst_vq->ops = &mtk_vdec_vb2_ops;
1439 dst_vq->mem_ops = &vb2_dma_contig_memops;
1440 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1441 dst_vq->lock = &ctx->dev->dev_mutex;
1442 dst_vq->dev = &ctx->dev->plat_dev->dev;
1444 ret = vb2_queue_init(dst_vq);
1445 if (ret) {
1446 vb2_queue_release(src_vq);
1447 mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
1450 return ret;