1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
8 #include <media/v4l2-event.h>
9 #include <media/v4l2-mem2mem.h>
10 #include <media/videobuf2-dma-contig.h>
12 #include "mtk_vcodec_drv.h"
13 #include "mtk_vcodec_dec.h"
14 #include "mtk_vcodec_intr.h"
15 #include "mtk_vcodec_util.h"
16 #include "vdec_drv_if.h"
17 #include "mtk_vcodec_dec_pm.h"
22 #define MTK_VDEC_MIN_W 64U
23 #define MTK_VDEC_MIN_H 64U
24 #define DFT_CFG_WIDTH MTK_VDEC_MIN_W
25 #define DFT_CFG_HEIGHT MTK_VDEC_MIN_H
27 static const struct mtk_video_fmt mtk_video_formats
[] = {
29 .fourcc
= V4L2_PIX_FMT_H264
,
32 .flags
= V4L2_FMT_FLAG_DYN_RESOLUTION
,
35 .fourcc
= V4L2_PIX_FMT_VP8
,
38 .flags
= V4L2_FMT_FLAG_DYN_RESOLUTION
,
41 .fourcc
= V4L2_PIX_FMT_VP9
,
44 .flags
= V4L2_FMT_FLAG_DYN_RESOLUTION
,
47 .fourcc
= V4L2_PIX_FMT_MT21C
,
48 .type
= MTK_FMT_FRAME
,
53 static const struct mtk_codec_framesizes mtk_vdec_framesizes
[] = {
55 .fourcc
= V4L2_PIX_FMT_H264
,
56 .stepwise
= { MTK_VDEC_MIN_W
, MTK_VDEC_MAX_W
, 16,
57 MTK_VDEC_MIN_H
, MTK_VDEC_MAX_H
, 16 },
60 .fourcc
= V4L2_PIX_FMT_VP8
,
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_VP9
,
66 .stepwise
= { MTK_VDEC_MIN_W
, MTK_VDEC_MAX_W
, 16,
67 MTK_VDEC_MIN_H
, MTK_VDEC_MAX_H
, 16 },
71 #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
72 #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
74 static const struct mtk_video_fmt
*mtk_vdec_find_format(struct v4l2_format
*f
)
76 const struct mtk_video_fmt
*fmt
;
79 for (k
= 0; k
< NUM_FORMATS
; k
++) {
80 fmt
= &mtk_video_formats
[k
];
81 if (fmt
->fourcc
== f
->fmt
.pix_mp
.pixelformat
)
88 static struct mtk_q_data
*mtk_vdec_get_q_data(struct mtk_vcodec_ctx
*ctx
,
89 enum v4l2_buf_type type
)
91 if (V4L2_TYPE_IS_OUTPUT(type
))
92 return &ctx
->q_data
[MTK_Q_DATA_SRC
];
94 return &ctx
->q_data
[MTK_Q_DATA_DST
];
98 * This function tries to clean all display buffers, the buffers will return
100 * Note the buffers returned from codec driver may still be in driver's
103 static struct vb2_buffer
*get_display_buffer(struct mtk_vcodec_ctx
*ctx
)
105 struct vdec_fb
*disp_frame_buffer
= NULL
;
106 struct mtk_video_dec_buf
*dstbuf
;
107 struct vb2_v4l2_buffer
*vb
;
109 mtk_v4l2_debug(3, "[%d]", ctx
->id
);
110 if (vdec_if_get_param(ctx
,
111 GET_PARAM_DISP_FRAME_BUFFER
,
112 &disp_frame_buffer
)) {
113 mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
118 if (disp_frame_buffer
== NULL
) {
119 mtk_v4l2_debug(3, "No display frame buffer");
123 dstbuf
= container_of(disp_frame_buffer
, struct mtk_video_dec_buf
,
125 vb
= &dstbuf
->m2m_buf
.vb
;
126 mutex_lock(&ctx
->lock
);
128 vb2_set_plane_payload(&vb
->vb2_buf
, 0,
129 ctx
->picinfo
.fb_sz
[0]);
130 if (ctx
->q_data
[MTK_Q_DATA_DST
].fmt
->num_planes
== 2)
131 vb2_set_plane_payload(&vb
->vb2_buf
, 1,
132 ctx
->picinfo
.fb_sz
[1]);
135 "[%d]status=%x queue id=%d to done_list %d",
136 ctx
->id
, disp_frame_buffer
->status
,
138 dstbuf
->queued_in_vb2
);
140 v4l2_m2m_buf_done(vb
, VB2_BUF_STATE_DONE
);
141 ctx
->decoded_frame_cnt
++;
143 mutex_unlock(&ctx
->lock
);
148 * This function tries to clean all capture buffers that are not used as
149 * reference buffers by codec driver any more
150 * In this case, we need re-queue buffer to vb2 buffer if user space
151 * already returns this buffer to v4l2 or this buffer is just the output of
152 * previous sps/pps/resolution change decode, or do nothing if user
153 * space still owns this buffer
155 static struct vb2_buffer
*get_free_buffer(struct mtk_vcodec_ctx
*ctx
)
157 struct mtk_video_dec_buf
*dstbuf
;
158 struct vdec_fb
*free_frame_buffer
= NULL
;
159 struct vb2_v4l2_buffer
*vb
;
161 if (vdec_if_get_param(ctx
,
162 GET_PARAM_FREE_FRAME_BUFFER
,
163 &free_frame_buffer
)) {
164 mtk_v4l2_err("[%d] Error!! Cannot get param", ctx
->id
);
167 if (free_frame_buffer
== NULL
) {
168 mtk_v4l2_debug(3, " No free frame buffer");
172 mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
173 ctx
->id
, free_frame_buffer
);
175 dstbuf
= container_of(free_frame_buffer
, struct mtk_video_dec_buf
,
177 vb
= &dstbuf
->m2m_buf
.vb
;
179 mutex_lock(&ctx
->lock
);
181 if ((dstbuf
->queued_in_vb2
) &&
182 (dstbuf
->queued_in_v4l2
) &&
183 (free_frame_buffer
->status
== FB_ST_FREE
)) {
185 * After decode sps/pps or non-display buffer, we don't
186 * need to return capture buffer to user space, but
187 * just re-queue this capture buffer to vb2 queue.
188 * This reduce overheads that dq/q unused capture
189 * buffer. In this case, queued_in_vb2 = true.
192 "[%d]status=%x queue id=%d to rdy_queue %d",
193 ctx
->id
, free_frame_buffer
->status
,
195 dstbuf
->queued_in_vb2
);
196 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, vb
);
197 } else if ((dstbuf
->queued_in_vb2
== false) &&
198 (dstbuf
->queued_in_v4l2
== true)) {
200 * If buffer in v4l2 driver but not in vb2 queue yet,
201 * and we get this buffer from free_list, it means
202 * that codec driver do not use this buffer as
203 * reference buffer anymore. We should q buffer to vb2
204 * queue, so later work thread could get this buffer
205 * for decode. In this case, queued_in_vb2 = false
206 * means this buffer is not from previous decode
210 "[%d]status=%x queue id=%d to rdy_queue",
211 ctx
->id
, free_frame_buffer
->status
,
213 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, vb
);
214 dstbuf
->queued_in_vb2
= true;
217 * Codec driver do not need to reference this capture
218 * buffer and this buffer is not in v4l2 driver.
219 * Then we don't need to do any thing, just add log when
220 * we need to debug buffer flow.
221 * When this buffer q from user space, it could
222 * directly q to vb2 buffer
224 mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
225 ctx
->id
, free_frame_buffer
->status
,
227 dstbuf
->queued_in_vb2
,
228 dstbuf
->queued_in_v4l2
);
230 dstbuf
->used
= false;
232 mutex_unlock(&ctx
->lock
);
236 static void clean_display_buffer(struct mtk_vcodec_ctx
*ctx
)
238 struct vb2_buffer
*framptr
;
241 framptr
= get_display_buffer(ctx
);
245 static void clean_free_buffer(struct mtk_vcodec_ctx
*ctx
)
247 struct vb2_buffer
*framptr
;
250 framptr
= get_free_buffer(ctx
);
254 static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx
*ctx
)
256 static const struct v4l2_event ev_src_ch
= {
257 .type
= V4L2_EVENT_SOURCE_CHANGE
,
258 .u
.src_change
.changes
=
259 V4L2_EVENT_SRC_CH_RESOLUTION
,
262 mtk_v4l2_debug(1, "[%d]", ctx
->id
);
263 v4l2_event_queue_fh(&ctx
->fh
, &ev_src_ch
);
266 static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx
*ctx
)
271 ret
= vdec_if_decode(ctx
, NULL
, NULL
, &res_chg
);
273 mtk_v4l2_err("DecodeFinal failed, ret=%d", ret
);
275 clean_display_buffer(ctx
);
276 clean_free_buffer(ctx
);
279 static void mtk_vdec_update_fmt(struct mtk_vcodec_ctx
*ctx
,
280 unsigned int pixelformat
)
282 const struct mtk_video_fmt
*fmt
;
283 struct mtk_q_data
*dst_q_data
;
286 dst_q_data
= &ctx
->q_data
[MTK_Q_DATA_DST
];
287 for (k
= 0; k
< NUM_FORMATS
; k
++) {
288 fmt
= &mtk_video_formats
[k
];
289 if (fmt
->fourcc
== pixelformat
) {
290 mtk_v4l2_debug(1, "Update cap fourcc(%d -> %d)",
291 dst_q_data
->fmt
->fourcc
, pixelformat
);
292 dst_q_data
->fmt
= fmt
;
297 mtk_v4l2_err("Cannot get fourcc(%d), using init value", pixelformat
);
300 static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx
*ctx
)
302 unsigned int dpbsize
= 0;
305 if (vdec_if_get_param(ctx
,
307 &ctx
->last_decoded_picinfo
)) {
308 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
313 if (ctx
->last_decoded_picinfo
.pic_w
== 0 ||
314 ctx
->last_decoded_picinfo
.pic_h
== 0 ||
315 ctx
->last_decoded_picinfo
.buf_w
== 0 ||
316 ctx
->last_decoded_picinfo
.buf_h
== 0) {
317 mtk_v4l2_err("Cannot get correct pic info");
321 if (ctx
->last_decoded_picinfo
.cap_fourcc
!= ctx
->picinfo
.cap_fourcc
&&
322 ctx
->picinfo
.cap_fourcc
!= 0)
323 mtk_vdec_update_fmt(ctx
, ctx
->picinfo
.cap_fourcc
);
325 if ((ctx
->last_decoded_picinfo
.pic_w
== ctx
->picinfo
.pic_w
) ||
326 (ctx
->last_decoded_picinfo
.pic_h
== ctx
->picinfo
.pic_h
))
330 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
331 ctx
->id
, ctx
->last_decoded_picinfo
.pic_w
,
332 ctx
->last_decoded_picinfo
.pic_h
,
333 ctx
->picinfo
.pic_w
, ctx
->picinfo
.pic_h
,
334 ctx
->last_decoded_picinfo
.buf_w
,
335 ctx
->last_decoded_picinfo
.buf_h
);
337 ret
= vdec_if_get_param(ctx
, GET_PARAM_DPB_SIZE
, &dpbsize
);
339 mtk_v4l2_err("Incorrect dpb size, ret=%d", ret
);
341 ctx
->dpb_size
= dpbsize
;
346 static void mtk_vdec_worker(struct work_struct
*work
)
348 struct mtk_vcodec_ctx
*ctx
= container_of(work
, struct mtk_vcodec_ctx
,
350 struct mtk_vcodec_dev
*dev
= ctx
->dev
;
351 struct vb2_v4l2_buffer
*src_buf
, *dst_buf
;
352 struct mtk_vcodec_mem buf
;
354 bool res_chg
= false;
356 struct mtk_video_dec_buf
*dst_buf_info
, *src_buf_info
;
358 src_buf
= v4l2_m2m_next_src_buf(ctx
->m2m_ctx
);
359 if (src_buf
== NULL
) {
360 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
361 mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx
->id
);
365 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
366 if (dst_buf
== NULL
) {
367 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
368 mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx
->id
);
372 src_buf_info
= container_of(src_buf
, struct mtk_video_dec_buf
,
374 dst_buf_info
= container_of(dst_buf
, struct mtk_video_dec_buf
,
377 pfb
= &dst_buf_info
->frame_buffer
;
378 pfb
->base_y
.va
= vb2_plane_vaddr(&dst_buf
->vb2_buf
, 0);
379 pfb
->base_y
.dma_addr
= vb2_dma_contig_plane_dma_addr(&dst_buf
->vb2_buf
, 0);
380 pfb
->base_y
.size
= ctx
->picinfo
.fb_sz
[0];
382 pfb
->base_c
.va
= vb2_plane_vaddr(&dst_buf
->vb2_buf
, 1);
383 pfb
->base_c
.dma_addr
= vb2_dma_contig_plane_dma_addr(&dst_buf
->vb2_buf
, 1);
384 pfb
->base_c
.size
= ctx
->picinfo
.fb_sz
[1];
386 mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx
->id
);
389 "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
390 dst_buf
->vb2_buf
.index
, pfb
,
391 pfb
->base_y
.va
, &pfb
->base_y
.dma_addr
,
392 &pfb
->base_c
.dma_addr
, pfb
->base_y
.size
);
394 if (src_buf_info
->lastframe
) {
395 mtk_v4l2_debug(1, "Got empty flush input buffer.");
396 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
398 /* update dst buf status */
399 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
);
400 mutex_lock(&ctx
->lock
);
401 dst_buf_info
->used
= false;
402 mutex_unlock(&ctx
->lock
);
404 vdec_if_decode(ctx
, NULL
, NULL
, &res_chg
);
405 clean_display_buffer(ctx
);
406 vb2_set_plane_payload(&dst_buf
->vb2_buf
, 0, 0);
407 if (ctx
->q_data
[MTK_Q_DATA_DST
].fmt
->num_planes
== 2)
408 vb2_set_plane_payload(&dst_buf
->vb2_buf
, 1, 0);
409 dst_buf
->flags
|= V4L2_BUF_FLAG_LAST
;
410 v4l2_m2m_buf_done(dst_buf
, VB2_BUF_STATE_DONE
);
411 clean_free_buffer(ctx
);
412 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
415 buf
.va
= vb2_plane_vaddr(&src_buf
->vb2_buf
, 0);
416 buf
.dma_addr
= vb2_dma_contig_plane_dma_addr(&src_buf
->vb2_buf
, 0);
417 buf
.size
= (size_t)src_buf
->vb2_buf
.planes
[0].bytesused
;
419 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
420 mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
421 ctx
->id
, src_buf
->vb2_buf
.index
);
424 mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
425 ctx
->id
, buf
.va
, &buf
.dma_addr
, buf
.size
, src_buf
);
426 dst_buf
->vb2_buf
.timestamp
= src_buf
->vb2_buf
.timestamp
;
427 dst_buf
->timecode
= src_buf
->timecode
;
428 mutex_lock(&ctx
->lock
);
429 dst_buf_info
->used
= true;
430 mutex_unlock(&ctx
->lock
);
431 src_buf_info
->used
= true;
433 ret
= vdec_if_decode(ctx
, &buf
, pfb
, &res_chg
);
437 " <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
439 src_buf
->vb2_buf
.index
,
441 src_buf
->vb2_buf
.timestamp
,
442 dst_buf
->vb2_buf
.index
,
444 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
446 mutex_lock(&ctx
->lock
);
447 src_buf_info
->error
= true;
448 mutex_unlock(&ctx
->lock
);
450 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_ERROR
);
451 } else if (res_chg
== false) {
453 * we only return src buffer with VB2_BUF_STATE_DONE
454 * when decode success without resolution change
456 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
457 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_DONE
);
460 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
);
461 clean_display_buffer(ctx
);
462 clean_free_buffer(ctx
);
464 if (!ret
&& res_chg
) {
465 mtk_vdec_pic_info_update(ctx
);
467 * On encountering a resolution change in the stream.
468 * The driver must first process and decode all
469 * remaining buffers from before the resolution change
470 * point, so call flush decode here
472 mtk_vdec_flush_decoder(ctx
);
474 * After all buffers containing decoded frames from
475 * before the resolution change point ready to be
476 * dequeued on the CAPTURE queue, the driver sends a
477 * V4L2_EVENT_SOURCE_CHANGE event for source change
478 * type V4L2_EVENT_SRC_CH_RESOLUTION
480 mtk_vdec_queue_res_chg_event(ctx
);
482 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
485 static int vidioc_try_decoder_cmd(struct file
*file
, void *priv
,
486 struct v4l2_decoder_cmd
*cmd
)
489 case V4L2_DEC_CMD_STOP
:
490 case V4L2_DEC_CMD_START
:
491 if (cmd
->flags
!= 0) {
492 mtk_v4l2_err("cmd->flags=%u", cmd
->flags
);
503 static int vidioc_decoder_cmd(struct file
*file
, void *priv
,
504 struct v4l2_decoder_cmd
*cmd
)
506 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
507 struct vb2_queue
*src_vq
, *dst_vq
;
510 ret
= vidioc_try_decoder_cmd(file
, priv
, cmd
);
514 mtk_v4l2_debug(1, "decoder cmd=%u", cmd
->cmd
);
515 dst_vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
,
516 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
);
518 case V4L2_DEC_CMD_STOP
:
519 src_vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
,
520 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
);
521 if (!vb2_is_streaming(src_vq
)) {
522 mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
525 if (!vb2_is_streaming(dst_vq
)) {
526 mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
529 v4l2_m2m_buf_queue(ctx
->m2m_ctx
,
530 &ctx
->empty_flush_buf
->m2m_buf
.vb
);
531 v4l2_m2m_try_schedule(ctx
->m2m_ctx
);
534 case V4L2_DEC_CMD_START
:
535 vb2_clear_last_buffer_dequeued(dst_vq
);
545 void mtk_vdec_unlock(struct mtk_vcodec_ctx
*ctx
)
547 mutex_unlock(&ctx
->dev
->dec_mutex
);
550 void mtk_vdec_lock(struct mtk_vcodec_ctx
*ctx
)
552 mutex_lock(&ctx
->dev
->dec_mutex
);
555 void mtk_vcodec_dec_release(struct mtk_vcodec_ctx
*ctx
)
558 ctx
->state
= MTK_STATE_FREE
;
561 void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx
*ctx
)
563 struct mtk_q_data
*q_data
;
565 ctx
->m2m_ctx
->q_lock
= &ctx
->dev
->dev_mutex
;
566 ctx
->fh
.m2m_ctx
= ctx
->m2m_ctx
;
567 ctx
->fh
.ctrl_handler
= &ctx
->ctrl_hdl
;
568 INIT_WORK(&ctx
->decode_work
, mtk_vdec_worker
);
569 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
570 ctx
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
571 ctx
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
572 ctx
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
574 q_data
= &ctx
->q_data
[MTK_Q_DATA_SRC
];
575 memset(q_data
, 0, sizeof(struct mtk_q_data
));
576 q_data
->visible_width
= DFT_CFG_WIDTH
;
577 q_data
->visible_height
= DFT_CFG_HEIGHT
;
578 q_data
->fmt
= &mtk_video_formats
[OUT_FMT_IDX
];
579 q_data
->field
= V4L2_FIELD_NONE
;
581 q_data
->sizeimage
[0] = DFT_CFG_WIDTH
* DFT_CFG_HEIGHT
;
582 q_data
->bytesperline
[0] = 0;
584 q_data
= &ctx
->q_data
[MTK_Q_DATA_DST
];
585 memset(q_data
, 0, sizeof(struct mtk_q_data
));
586 q_data
->visible_width
= DFT_CFG_WIDTH
;
587 q_data
->visible_height
= DFT_CFG_HEIGHT
;
588 q_data
->coded_width
= DFT_CFG_WIDTH
;
589 q_data
->coded_height
= DFT_CFG_HEIGHT
;
590 q_data
->fmt
= &mtk_video_formats
[CAP_FMT_IDX
];
591 q_data
->field
= V4L2_FIELD_NONE
;
593 v4l_bound_align_image(&q_data
->coded_width
,
596 &q_data
->coded_height
,
598 MTK_VDEC_MAX_H
, 5, 6);
600 q_data
->sizeimage
[0] = q_data
->coded_width
* q_data
->coded_height
;
601 q_data
->bytesperline
[0] = q_data
->coded_width
;
602 q_data
->sizeimage
[1] = q_data
->sizeimage
[0] / 2;
603 q_data
->bytesperline
[1] = q_data
->coded_width
;
606 static int vidioc_vdec_qbuf(struct file
*file
, void *priv
,
607 struct v4l2_buffer
*buf
)
609 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
611 if (ctx
->state
== MTK_STATE_ABORT
) {
612 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
617 return v4l2_m2m_qbuf(file
, ctx
->m2m_ctx
, buf
);
620 static int vidioc_vdec_dqbuf(struct file
*file
, void *priv
,
621 struct v4l2_buffer
*buf
)
623 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
625 if (ctx
->state
== MTK_STATE_ABORT
) {
626 mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
631 return v4l2_m2m_dqbuf(file
, ctx
->m2m_ctx
, buf
);
634 static int vidioc_vdec_querycap(struct file
*file
, void *priv
,
635 struct v4l2_capability
*cap
)
637 strscpy(cap
->driver
, MTK_VCODEC_DEC_NAME
, sizeof(cap
->driver
));
638 strscpy(cap
->bus_info
, MTK_PLATFORM_STR
, sizeof(cap
->bus_info
));
639 strscpy(cap
->card
, MTK_PLATFORM_STR
, sizeof(cap
->card
));
644 static int vidioc_vdec_subscribe_evt(struct v4l2_fh
*fh
,
645 const struct v4l2_event_subscription
*sub
)
649 return v4l2_event_subscribe(fh
, sub
, 2, NULL
);
650 case V4L2_EVENT_SOURCE_CHANGE
:
651 return v4l2_src_change_event_subscribe(fh
, sub
);
653 return v4l2_ctrl_subscribe_event(fh
, sub
);
657 static int vidioc_try_fmt(struct v4l2_format
*f
,
658 const struct mtk_video_fmt
*fmt
)
660 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
663 pix_fmt_mp
->field
= V4L2_FIELD_NONE
;
665 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
666 pix_fmt_mp
->num_planes
= 1;
667 pix_fmt_mp
->plane_fmt
[0].bytesperline
= 0;
668 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
671 pix_fmt_mp
->height
= clamp(pix_fmt_mp
->height
,
674 pix_fmt_mp
->width
= clamp(pix_fmt_mp
->width
,
679 * Find next closer width align 64, heign align 64, size align
681 * Note: This only get default value, the real HW needed value
682 * only available when ctx in MTK_STATE_HEADER state
684 tmp_w
= pix_fmt_mp
->width
;
685 tmp_h
= pix_fmt_mp
->height
;
686 v4l_bound_align_image(&pix_fmt_mp
->width
,
691 MTK_VDEC_MAX_H
, 6, 9);
693 if (pix_fmt_mp
->width
< tmp_w
&&
694 (pix_fmt_mp
->width
+ 64) <= MTK_VDEC_MAX_W
)
695 pix_fmt_mp
->width
+= 64;
696 if (pix_fmt_mp
->height
< tmp_h
&&
697 (pix_fmt_mp
->height
+ 64) <= MTK_VDEC_MAX_H
)
698 pix_fmt_mp
->height
+= 64;
701 "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
702 tmp_w
, tmp_h
, pix_fmt_mp
->width
,
704 pix_fmt_mp
->width
* pix_fmt_mp
->height
);
706 pix_fmt_mp
->num_planes
= fmt
->num_planes
;
707 pix_fmt_mp
->plane_fmt
[0].sizeimage
=
708 pix_fmt_mp
->width
* pix_fmt_mp
->height
;
709 pix_fmt_mp
->plane_fmt
[0].bytesperline
= pix_fmt_mp
->width
;
711 if (pix_fmt_mp
->num_planes
== 2) {
712 pix_fmt_mp
->plane_fmt
[1].sizeimage
=
713 (pix_fmt_mp
->width
* pix_fmt_mp
->height
) / 2;
714 pix_fmt_mp
->plane_fmt
[1].bytesperline
=
719 for (i
= 0; i
< pix_fmt_mp
->num_planes
; i
++)
720 memset(&(pix_fmt_mp
->plane_fmt
[i
].reserved
[0]), 0x0,
721 sizeof(pix_fmt_mp
->plane_fmt
[0].reserved
));
723 pix_fmt_mp
->flags
= 0;
724 memset(&pix_fmt_mp
->reserved
, 0x0, sizeof(pix_fmt_mp
->reserved
));
728 static int vidioc_try_fmt_vid_cap_mplane(struct file
*file
, void *priv
,
729 struct v4l2_format
*f
)
731 const struct mtk_video_fmt
*fmt
;
733 fmt
= mtk_vdec_find_format(f
);
735 f
->fmt
.pix
.pixelformat
= mtk_video_formats
[CAP_FMT_IDX
].fourcc
;
736 fmt
= mtk_vdec_find_format(f
);
739 return vidioc_try_fmt(f
, fmt
);
742 static int vidioc_try_fmt_vid_out_mplane(struct file
*file
, void *priv
,
743 struct v4l2_format
*f
)
745 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
746 const struct mtk_video_fmt
*fmt
;
748 fmt
= mtk_vdec_find_format(f
);
750 f
->fmt
.pix
.pixelformat
= mtk_video_formats
[OUT_FMT_IDX
].fourcc
;
751 fmt
= mtk_vdec_find_format(f
);
754 if (pix_fmt_mp
->plane_fmt
[0].sizeimage
== 0) {
755 mtk_v4l2_err("sizeimage of output format must be given");
759 return vidioc_try_fmt(f
, fmt
);
762 static int vidioc_vdec_g_selection(struct file
*file
, void *priv
,
763 struct v4l2_selection
*s
)
765 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
766 struct mtk_q_data
*q_data
;
768 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
771 q_data
= &ctx
->q_data
[MTK_Q_DATA_DST
];
774 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
777 s
->r
.width
= ctx
->picinfo
.pic_w
;
778 s
->r
.height
= ctx
->picinfo
.pic_h
;
780 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
783 s
->r
.width
= ctx
->picinfo
.buf_w
;
784 s
->r
.height
= ctx
->picinfo
.buf_h
;
786 case V4L2_SEL_TGT_COMPOSE
:
787 if (vdec_if_get_param(ctx
, GET_PARAM_CROP_INFO
, &(s
->r
))) {
788 /* set to default value if header info not ready yet*/
791 s
->r
.width
= q_data
->visible_width
;
792 s
->r
.height
= q_data
->visible_height
;
799 if (ctx
->state
< MTK_STATE_HEADER
) {
800 /* set to default value if header info not ready yet*/
803 s
->r
.width
= q_data
->visible_width
;
804 s
->r
.height
= q_data
->visible_height
;
811 static int vidioc_vdec_s_selection(struct file
*file
, void *priv
,
812 struct v4l2_selection
*s
)
814 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
816 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
820 case V4L2_SEL_TGT_COMPOSE
:
823 s
->r
.width
= ctx
->picinfo
.pic_w
;
824 s
->r
.height
= ctx
->picinfo
.pic_h
;
833 static int vidioc_vdec_s_fmt(struct file
*file
, void *priv
,
834 struct v4l2_format
*f
)
836 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
837 struct v4l2_pix_format_mplane
*pix_mp
;
838 struct mtk_q_data
*q_data
;
840 const struct mtk_video_fmt
*fmt
;
842 mtk_v4l2_debug(3, "[%d]", ctx
->id
);
844 q_data
= mtk_vdec_get_q_data(ctx
, f
->type
);
848 pix_mp
= &f
->fmt
.pix_mp
;
850 * Setting OUTPUT format after OUTPUT buffers are allocated is invalid
851 * if using the stateful API.
853 if ((f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) &&
854 vb2_is_busy(&ctx
->m2m_ctx
->out_q_ctx
.q
)) {
855 mtk_v4l2_err("out_q_ctx buffers already requested");
860 * Setting CAPTURE format after CAPTURE buffers are allocated is
863 if ((f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) &&
864 vb2_is_busy(&ctx
->m2m_ctx
->cap_q_ctx
.q
)) {
865 mtk_v4l2_err("cap_q_ctx buffers already requested");
869 fmt
= mtk_vdec_find_format(f
);
871 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
872 f
->fmt
.pix
.pixelformat
=
873 mtk_video_formats
[OUT_FMT_IDX
].fourcc
;
874 fmt
= mtk_vdec_find_format(f
);
875 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
876 f
->fmt
.pix
.pixelformat
=
877 mtk_video_formats
[CAP_FMT_IDX
].fourcc
;
878 fmt
= mtk_vdec_find_format(f
);
885 vidioc_try_fmt(f
, q_data
->fmt
);
886 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
887 q_data
->sizeimage
[0] = pix_mp
->plane_fmt
[0].sizeimage
;
888 q_data
->coded_width
= pix_mp
->width
;
889 q_data
->coded_height
= pix_mp
->height
;
891 ctx
->colorspace
= pix_mp
->colorspace
;
892 ctx
->ycbcr_enc
= pix_mp
->ycbcr_enc
;
893 ctx
->quantization
= pix_mp
->quantization
;
894 ctx
->xfer_func
= pix_mp
->xfer_func
;
896 if (ctx
->state
== MTK_STATE_FREE
) {
897 ret
= vdec_if_init(ctx
, q_data
->fmt
->fourcc
);
899 mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
903 ctx
->state
= MTK_STATE_INIT
;
910 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
911 struct v4l2_frmsizeenum
*fsize
)
914 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
916 if (fsize
->index
!= 0)
919 for (i
= 0; i
< NUM_SUPPORTED_FRAMESIZE
; ++i
) {
920 if (fsize
->pixel_format
!= mtk_vdec_framesizes
[i
].fourcc
)
923 fsize
->type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
924 fsize
->stepwise
= mtk_vdec_framesizes
[i
].stepwise
;
925 if (!(ctx
->dev
->dec_capability
&
926 VCODEC_CAPABILITY_4K_DISABLED
)) {
927 mtk_v4l2_debug(3, "4K is enabled");
928 fsize
->stepwise
.max_width
=
929 VCODEC_DEC_4K_CODED_WIDTH
;
930 fsize
->stepwise
.max_height
=
931 VCODEC_DEC_4K_CODED_HEIGHT
;
933 mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
934 ctx
->dev
->dec_capability
,
935 fsize
->stepwise
.min_width
,
936 fsize
->stepwise
.max_width
,
937 fsize
->stepwise
.step_width
,
938 fsize
->stepwise
.min_height
,
939 fsize
->stepwise
.max_height
,
940 fsize
->stepwise
.step_height
);
947 static int vidioc_enum_fmt(struct v4l2_fmtdesc
*f
, bool output_queue
)
949 const struct mtk_video_fmt
*fmt
;
952 for (i
= 0; i
< NUM_FORMATS
; i
++) {
953 if (output_queue
&& (mtk_video_formats
[i
].type
!= MTK_FMT_DEC
))
956 (mtk_video_formats
[i
].type
!= MTK_FMT_FRAME
))
964 if (i
== NUM_FORMATS
)
967 fmt
= &mtk_video_formats
[i
];
968 f
->pixelformat
= fmt
->fourcc
;
969 f
->flags
= fmt
->flags
;
974 static int vidioc_vdec_enum_fmt_vid_cap(struct file
*file
, void *priv
,
975 struct v4l2_fmtdesc
*f
)
977 return vidioc_enum_fmt(f
, false);
980 static int vidioc_vdec_enum_fmt_vid_out(struct file
*file
, void *priv
,
981 struct v4l2_fmtdesc
*f
)
983 return vidioc_enum_fmt(f
, true);
986 static int vidioc_vdec_g_fmt(struct file
*file
, void *priv
,
987 struct v4l2_format
*f
)
989 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
990 struct v4l2_pix_format_mplane
*pix_mp
= &f
->fmt
.pix_mp
;
991 struct vb2_queue
*vq
;
992 struct mtk_q_data
*q_data
;
994 vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, f
->type
);
996 mtk_v4l2_err("no vb2 queue for type=%d", f
->type
);
1000 q_data
= mtk_vdec_get_q_data(ctx
, f
->type
);
1002 pix_mp
->field
= V4L2_FIELD_NONE
;
1003 pix_mp
->colorspace
= ctx
->colorspace
;
1004 pix_mp
->ycbcr_enc
= ctx
->ycbcr_enc
;
1005 pix_mp
->quantization
= ctx
->quantization
;
1006 pix_mp
->xfer_func
= ctx
->xfer_func
;
1008 if ((f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) &&
1009 (ctx
->state
>= MTK_STATE_HEADER
)) {
1010 /* Until STREAMOFF is called on the CAPTURE queue
1011 * (acknowledging the event), the driver operates as if
1012 * the resolution hasn't changed yet.
1013 * So we just return picinfo yet, and update picinfo in
1014 * stop_streaming hook function
1016 q_data
->sizeimage
[0] = ctx
->picinfo
.fb_sz
[0];
1017 q_data
->sizeimage
[1] = ctx
->picinfo
.fb_sz
[1];
1018 q_data
->bytesperline
[0] = ctx
->last_decoded_picinfo
.buf_w
;
1019 q_data
->bytesperline
[1] = ctx
->last_decoded_picinfo
.buf_w
;
1020 q_data
->coded_width
= ctx
->picinfo
.buf_w
;
1021 q_data
->coded_height
= ctx
->picinfo
.buf_h
;
1022 ctx
->last_decoded_picinfo
.cap_fourcc
= q_data
->fmt
->fourcc
;
1025 * Width and height are set to the dimensions
1026 * of the movie, the buffer is bigger and
1027 * further processing stages should crop to this
1030 pix_mp
->width
= q_data
->coded_width
;
1031 pix_mp
->height
= q_data
->coded_height
;
1034 * Set pixelformat to the format in which mt vcodec
1035 * outputs the decoded frame
1037 pix_mp
->num_planes
= q_data
->fmt
->num_planes
;
1038 pix_mp
->pixelformat
= q_data
->fmt
->fourcc
;
1039 pix_mp
->plane_fmt
[0].bytesperline
= q_data
->bytesperline
[0];
1040 pix_mp
->plane_fmt
[0].sizeimage
= q_data
->sizeimage
[0];
1041 pix_mp
->plane_fmt
[1].bytesperline
= q_data
->bytesperline
[1];
1042 pix_mp
->plane_fmt
[1].sizeimage
= q_data
->sizeimage
[1];
1044 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1046 * This is run on OUTPUT
1047 * The buffer contains compressed image
1048 * so width and height have no meaning.
1049 * Assign value here to pass v4l2-compliance test
1051 pix_mp
->width
= q_data
->visible_width
;
1052 pix_mp
->height
= q_data
->visible_height
;
1053 pix_mp
->plane_fmt
[0].bytesperline
= q_data
->bytesperline
[0];
1054 pix_mp
->plane_fmt
[0].sizeimage
= q_data
->sizeimage
[0];
1055 pix_mp
->pixelformat
= q_data
->fmt
->fourcc
;
1056 pix_mp
->num_planes
= q_data
->fmt
->num_planes
;
1058 pix_mp
->width
= q_data
->coded_width
;
1059 pix_mp
->height
= q_data
->coded_height
;
1060 pix_mp
->num_planes
= q_data
->fmt
->num_planes
;
1061 pix_mp
->pixelformat
= q_data
->fmt
->fourcc
;
1062 pix_mp
->plane_fmt
[0].bytesperline
= q_data
->bytesperline
[0];
1063 pix_mp
->plane_fmt
[0].sizeimage
= q_data
->sizeimage
[0];
1064 pix_mp
->plane_fmt
[1].bytesperline
= q_data
->bytesperline
[1];
1065 pix_mp
->plane_fmt
[1].sizeimage
= q_data
->sizeimage
[1];
1067 mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
1068 ctx
->id
, f
->type
, ctx
->state
);
1074 static int vb2ops_vdec_queue_setup(struct vb2_queue
*vq
,
1075 unsigned int *nbuffers
,
1076 unsigned int *nplanes
,
1077 unsigned int sizes
[],
1078 struct device
*alloc_devs
[])
1080 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(vq
);
1081 struct mtk_q_data
*q_data
;
1084 q_data
= mtk_vdec_get_q_data(ctx
, vq
->type
);
1086 if (q_data
== NULL
) {
1087 mtk_v4l2_err("vq->type=%d err\n", vq
->type
);
1092 for (i
= 0; i
< *nplanes
; i
++) {
1093 if (sizes
[i
] < q_data
->sizeimage
[i
])
1097 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1102 for (i
= 0; i
< *nplanes
; i
++)
1103 sizes
[i
] = q_data
->sizeimage
[i
];
1107 "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
1108 ctx
->id
, vq
->type
, *nplanes
, *nbuffers
,
1109 sizes
[0], sizes
[1]);
1114 static int vb2ops_vdec_buf_prepare(struct vb2_buffer
*vb
)
1116 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1117 struct mtk_q_data
*q_data
;
1120 mtk_v4l2_debug(3, "[%d] (%d) id=%d",
1121 ctx
->id
, vb
->vb2_queue
->type
, vb
->index
);
1123 q_data
= mtk_vdec_get_q_data(ctx
, vb
->vb2_queue
->type
);
1125 for (i
= 0; i
< q_data
->fmt
->num_planes
; i
++) {
1126 if (vb2_plane_size(vb
, i
) < q_data
->sizeimage
[i
]) {
1127 mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
1128 i
, vb2_plane_size(vb
, i
),
1129 q_data
->sizeimage
[i
]);
1136 static void vb2ops_vdec_buf_queue(struct vb2_buffer
*vb
)
1138 struct vb2_v4l2_buffer
*src_buf
;
1139 struct mtk_vcodec_mem src_mem
;
1140 bool res_chg
= false;
1142 unsigned int dpbsize
= 1, i
= 0;
1143 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1144 struct vb2_v4l2_buffer
*vb2_v4l2
= NULL
;
1145 struct mtk_video_dec_buf
*buf
= NULL
;
1146 struct mtk_q_data
*dst_q_data
;
1148 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
1149 ctx
->id
, vb
->vb2_queue
->type
,
1152 * check if this buffer is ready to be used after decode
1154 if (vb
->vb2_queue
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1155 vb2_v4l2
= to_vb2_v4l2_buffer(vb
);
1156 buf
= container_of(vb2_v4l2
, struct mtk_video_dec_buf
,
1158 mutex_lock(&ctx
->lock
);
1159 if (buf
->used
== false) {
1160 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, vb2_v4l2
);
1161 buf
->queued_in_vb2
= true;
1162 buf
->queued_in_v4l2
= true;
1164 buf
->queued_in_vb2
= false;
1165 buf
->queued_in_v4l2
= true;
1167 mutex_unlock(&ctx
->lock
);
1171 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, to_vb2_v4l2_buffer(vb
));
1173 if (ctx
->state
!= MTK_STATE_INIT
) {
1174 mtk_v4l2_debug(3, "[%d] already init driver %d",
1175 ctx
->id
, ctx
->state
);
1179 src_buf
= v4l2_m2m_next_src_buf(ctx
->m2m_ctx
);
1181 mtk_v4l2_err("No src buffer");
1184 buf
= container_of(src_buf
, struct mtk_video_dec_buf
, m2m_buf
.vb
);
1185 if (buf
->lastframe
) {
1186 /* This shouldn't happen. Just in case. */
1187 mtk_v4l2_err("Invalid flush buffer.");
1188 v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
1192 src_mem
.va
= vb2_plane_vaddr(&src_buf
->vb2_buf
, 0);
1193 src_mem
.dma_addr
= vb2_dma_contig_plane_dma_addr(&src_buf
->vb2_buf
, 0);
1194 src_mem
.size
= (size_t)src_buf
->vb2_buf
.planes
[0].bytesused
;
1196 "[%d] buf id=%d va=%p dma=%pad size=%zx",
1197 ctx
->id
, src_buf
->vb2_buf
.index
,
1198 src_mem
.va
, &src_mem
.dma_addr
,
1201 ret
= vdec_if_decode(ctx
, &src_mem
, NULL
, &res_chg
);
1202 if (ret
|| !res_chg
) {
1204 * fb == NULL means to parse SPS/PPS header or
1205 * resolution info in src_mem. Decode can fail
1206 * if there is no SPS header or picture info
1210 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
1212 mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.",
1214 ctx
->state
= MTK_STATE_ABORT
;
1215 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_ERROR
);
1217 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_DONE
);
1219 mtk_v4l2_debug(ret
? 0 : 1,
1220 "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
1221 ctx
->id
, src_buf
->vb2_buf
.index
,
1222 src_mem
.size
, ret
, res_chg
);
1226 if (vdec_if_get_param(ctx
, GET_PARAM_PIC_INFO
, &ctx
->picinfo
)) {
1227 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
1232 ctx
->last_decoded_picinfo
= ctx
->picinfo
;
1233 dst_q_data
= &ctx
->q_data
[MTK_Q_DATA_DST
];
1234 for (i
= 0; i
< dst_q_data
->fmt
->num_planes
; i
++) {
1235 dst_q_data
->sizeimage
[i
] = ctx
->picinfo
.fb_sz
[i
];
1236 dst_q_data
->bytesperline
[i
] = ctx
->picinfo
.buf_w
;
1239 mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
1241 ctx
->picinfo
.buf_w
, ctx
->picinfo
.buf_h
,
1242 ctx
->picinfo
.pic_w
, ctx
->picinfo
.pic_h
,
1243 dst_q_data
->sizeimage
[0],
1244 dst_q_data
->sizeimage
[1]);
1246 ret
= vdec_if_get_param(ctx
, GET_PARAM_DPB_SIZE
, &dpbsize
);
1248 mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx
->id
, ret
);
1250 ctx
->dpb_size
= dpbsize
;
1251 ctx
->state
= MTK_STATE_HEADER
;
1252 mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx
->id
, ctx
->dpb_size
);
1254 mtk_vdec_queue_res_chg_event(ctx
);
1257 static void vb2ops_vdec_buf_finish(struct vb2_buffer
*vb
)
1259 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1260 struct vb2_v4l2_buffer
*vb2_v4l2
;
1261 struct mtk_video_dec_buf
*buf
;
1264 vb2_v4l2
= container_of(vb
, struct vb2_v4l2_buffer
, vb2_buf
);
1265 buf
= container_of(vb2_v4l2
, struct mtk_video_dec_buf
, m2m_buf
.vb
);
1266 mutex_lock(&ctx
->lock
);
1267 if (vb
->vb2_queue
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1268 buf
->queued_in_v4l2
= false;
1269 buf
->queued_in_vb2
= false;
1271 buf_error
= buf
->error
;
1272 mutex_unlock(&ctx
->lock
);
1275 mtk_v4l2_err("Unrecoverable error on buffer.");
1276 ctx
->state
= MTK_STATE_ABORT
;
1280 static int vb2ops_vdec_buf_init(struct vb2_buffer
*vb
)
1282 struct vb2_v4l2_buffer
*vb2_v4l2
= container_of(vb
,
1283 struct vb2_v4l2_buffer
, vb2_buf
);
1284 struct mtk_video_dec_buf
*buf
= container_of(vb2_v4l2
,
1285 struct mtk_video_dec_buf
, m2m_buf
.vb
);
1287 if (vb
->vb2_queue
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1289 buf
->queued_in_v4l2
= false;
1291 buf
->lastframe
= false;
1297 static int vb2ops_vdec_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1299 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(q
);
1301 if (ctx
->state
== MTK_STATE_FLUSH
)
1302 ctx
->state
= MTK_STATE_HEADER
;
1307 static void vb2ops_vdec_stop_streaming(struct vb2_queue
*q
)
1309 struct vb2_v4l2_buffer
*src_buf
= NULL
, *dst_buf
= NULL
;
1310 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(q
);
1312 mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
1313 ctx
->id
, q
->type
, ctx
->state
, ctx
->decoded_frame_cnt
);
1315 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1316 while ((src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
))) {
1317 struct mtk_video_dec_buf
*buf_info
= container_of(
1318 src_buf
, struct mtk_video_dec_buf
, m2m_buf
.vb
);
1319 if (!buf_info
->lastframe
)
1320 v4l2_m2m_buf_done(src_buf
,
1321 VB2_BUF_STATE_ERROR
);
1326 if (ctx
->state
>= MTK_STATE_HEADER
) {
1328 /* Until STREAMOFF is called on the CAPTURE queue
1329 * (acknowledging the event), the driver operates
1330 * as if the resolution hasn't changed yet, i.e.
1331 * VIDIOC_G_FMT< etc. return previous resolution.
1332 * So we update picinfo here
1334 ctx
->picinfo
= ctx
->last_decoded_picinfo
;
1337 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
1338 ctx
->id
, ctx
->last_decoded_picinfo
.pic_w
,
1339 ctx
->last_decoded_picinfo
.pic_h
,
1340 ctx
->picinfo
.pic_w
, ctx
->picinfo
.pic_h
,
1341 ctx
->last_decoded_picinfo
.buf_w
,
1342 ctx
->last_decoded_picinfo
.buf_h
);
1344 mtk_vdec_flush_decoder(ctx
);
1346 ctx
->state
= MTK_STATE_FLUSH
;
1348 while ((dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
))) {
1349 vb2_set_plane_payload(&dst_buf
->vb2_buf
, 0, 0);
1350 if (ctx
->q_data
[MTK_Q_DATA_DST
].fmt
->num_planes
== 2)
1351 vb2_set_plane_payload(&dst_buf
->vb2_buf
, 1, 0);
1352 v4l2_m2m_buf_done(dst_buf
, VB2_BUF_STATE_ERROR
);
1357 static void m2mops_vdec_device_run(void *priv
)
1359 struct mtk_vcodec_ctx
*ctx
= priv
;
1360 struct mtk_vcodec_dev
*dev
= ctx
->dev
;
1362 queue_work(dev
->decode_workqueue
, &ctx
->decode_work
);
1365 static int m2mops_vdec_job_ready(void *m2m_priv
)
1367 struct mtk_vcodec_ctx
*ctx
= m2m_priv
;
1369 mtk_v4l2_debug(3, "[%d]", ctx
->id
);
1371 if (ctx
->state
== MTK_STATE_ABORT
)
1374 if ((ctx
->last_decoded_picinfo
.pic_w
!= ctx
->picinfo
.pic_w
) ||
1375 (ctx
->last_decoded_picinfo
.pic_h
!= ctx
->picinfo
.pic_h
))
1378 if (ctx
->state
!= MTK_STATE_HEADER
)
1384 static void m2mops_vdec_job_abort(void *priv
)
1386 struct mtk_vcodec_ctx
*ctx
= priv
;
1388 ctx
->state
= MTK_STATE_ABORT
;
1391 static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl
*ctrl
)
1393 struct mtk_vcodec_ctx
*ctx
= ctrl_to_ctx(ctrl
);
1397 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
:
1398 if (ctx
->state
>= MTK_STATE_HEADER
) {
1399 ctrl
->val
= ctx
->dpb_size
;
1401 mtk_v4l2_debug(0, "Seqinfo not ready");
1411 static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops
= {
1412 .g_volatile_ctrl
= mtk_vdec_g_v_ctrl
,
1415 int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx
*ctx
)
1417 struct v4l2_ctrl
*ctrl
;
1419 v4l2_ctrl_handler_init(&ctx
->ctrl_hdl
, 1);
1421 ctrl
= v4l2_ctrl_new_std(&ctx
->ctrl_hdl
,
1422 &mtk_vcodec_dec_ctrl_ops
,
1423 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
,
1425 ctrl
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
1426 v4l2_ctrl_new_std_menu(&ctx
->ctrl_hdl
,
1427 &mtk_vcodec_dec_ctrl_ops
,
1428 V4L2_CID_MPEG_VIDEO_VP9_PROFILE
,
1429 V4L2_MPEG_VIDEO_VP9_PROFILE_0
,
1430 0, V4L2_MPEG_VIDEO_VP9_PROFILE_0
);
1432 if (ctx
->ctrl_hdl
.error
) {
1433 mtk_v4l2_err("Adding control failed %d",
1434 ctx
->ctrl_hdl
.error
);
1435 return ctx
->ctrl_hdl
.error
;
1438 v4l2_ctrl_handler_setup(&ctx
->ctrl_hdl
);
1442 const struct v4l2_m2m_ops mtk_vdec_m2m_ops
= {
1443 .device_run
= m2mops_vdec_device_run
,
1444 .job_ready
= m2mops_vdec_job_ready
,
1445 .job_abort
= m2mops_vdec_job_abort
,
1448 static const struct vb2_ops mtk_vdec_vb2_ops
= {
1449 .queue_setup
= vb2ops_vdec_queue_setup
,
1450 .buf_prepare
= vb2ops_vdec_buf_prepare
,
1451 .buf_queue
= vb2ops_vdec_buf_queue
,
1452 .wait_prepare
= vb2_ops_wait_prepare
,
1453 .wait_finish
= vb2_ops_wait_finish
,
1454 .buf_init
= vb2ops_vdec_buf_init
,
1455 .buf_finish
= vb2ops_vdec_buf_finish
,
1456 .start_streaming
= vb2ops_vdec_start_streaming
,
1457 .stop_streaming
= vb2ops_vdec_stop_streaming
,
1460 const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops
= {
1461 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
1462 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
1463 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
1464 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
1465 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
1467 .vidioc_qbuf
= vidioc_vdec_qbuf
,
1468 .vidioc_dqbuf
= vidioc_vdec_dqbuf
,
1470 .vidioc_try_fmt_vid_cap_mplane
= vidioc_try_fmt_vid_cap_mplane
,
1471 .vidioc_try_fmt_vid_out_mplane
= vidioc_try_fmt_vid_out_mplane
,
1473 .vidioc_s_fmt_vid_cap_mplane
= vidioc_vdec_s_fmt
,
1474 .vidioc_s_fmt_vid_out_mplane
= vidioc_vdec_s_fmt
,
1475 .vidioc_g_fmt_vid_cap_mplane
= vidioc_vdec_g_fmt
,
1476 .vidioc_g_fmt_vid_out_mplane
= vidioc_vdec_g_fmt
,
1478 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
1480 .vidioc_enum_fmt_vid_cap
= vidioc_vdec_enum_fmt_vid_cap
,
1481 .vidioc_enum_fmt_vid_out
= vidioc_vdec_enum_fmt_vid_out
,
1482 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
1484 .vidioc_querycap
= vidioc_vdec_querycap
,
1485 .vidioc_subscribe_event
= vidioc_vdec_subscribe_evt
,
1486 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1487 .vidioc_g_selection
= vidioc_vdec_g_selection
,
1488 .vidioc_s_selection
= vidioc_vdec_s_selection
,
1490 .vidioc_decoder_cmd
= vidioc_decoder_cmd
,
1491 .vidioc_try_decoder_cmd
= vidioc_try_decoder_cmd
,
1494 int mtk_vcodec_dec_queue_init(void *priv
, struct vb2_queue
*src_vq
,
1495 struct vb2_queue
*dst_vq
)
1497 struct mtk_vcodec_ctx
*ctx
= priv
;
1500 mtk_v4l2_debug(3, "[%d]", ctx
->id
);
1502 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
1503 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1504 src_vq
->drv_priv
= ctx
;
1505 src_vq
->buf_struct_size
= sizeof(struct mtk_video_dec_buf
);
1506 src_vq
->ops
= &mtk_vdec_vb2_ops
;
1507 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
1508 src_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1509 src_vq
->lock
= &ctx
->dev
->dev_mutex
;
1510 src_vq
->dev
= &ctx
->dev
->plat_dev
->dev
;
1512 ret
= vb2_queue_init(src_vq
);
1514 mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
1517 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1518 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1519 dst_vq
->drv_priv
= ctx
;
1520 dst_vq
->buf_struct_size
= sizeof(struct mtk_video_dec_buf
);
1521 dst_vq
->ops
= &mtk_vdec_vb2_ops
;
1522 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1523 dst_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1524 dst_vq
->lock
= &ctx
->dev
->dev_mutex
;
1525 dst_vq
->dev
= &ctx
->dev
->plat_dev
->dev
;
1527 ret
= vb2_queue_init(dst_vq
);
1529 vb2_queue_release(src_vq
);
1530 mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");