2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: PC Chen <pc.chen@mediatek.com>
4 * Tiffany Lin <tiffany.lin@mediatek.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <media/v4l2-event.h>
17 #include <media/v4l2-mem2mem.h>
18 #include <media/videobuf2-dma-contig.h>
20 #include "mtk_vcodec_drv.h"
21 #include "mtk_vcodec_dec.h"
22 #include "mtk_vcodec_intr.h"
23 #include "mtk_vcodec_util.h"
24 #include "vdec_drv_if.h"
25 #include "mtk_vcodec_dec_pm.h"
30 #define MTK_VDEC_MIN_W 64U
31 #define MTK_VDEC_MIN_H 64U
32 #define DFT_CFG_WIDTH MTK_VDEC_MIN_W
33 #define DFT_CFG_HEIGHT MTK_VDEC_MIN_H
35 static struct mtk_video_fmt mtk_video_formats
[] = {
37 .fourcc
= V4L2_PIX_FMT_H264
,
42 .fourcc
= V4L2_PIX_FMT_VP8
,
47 .fourcc
= V4L2_PIX_FMT_VP9
,
52 .fourcc
= V4L2_PIX_FMT_MT21C
,
53 .type
= MTK_FMT_FRAME
,
58 static const struct mtk_codec_framesizes mtk_vdec_framesizes
[] = {
60 .fourcc
= V4L2_PIX_FMT_H264
,
61 .stepwise
= { MTK_VDEC_MIN_W
, MTK_VDEC_MAX_W
, 16,
62 MTK_VDEC_MIN_H
, MTK_VDEC_MAX_H
, 16 },
65 .fourcc
= V4L2_PIX_FMT_VP8
,
66 .stepwise
= { MTK_VDEC_MIN_W
, MTK_VDEC_MAX_W
, 16,
67 MTK_VDEC_MIN_H
, MTK_VDEC_MAX_H
, 16 },
70 .fourcc
= V4L2_PIX_FMT_VP9
,
71 .stepwise
= { MTK_VDEC_MIN_W
, MTK_VDEC_MAX_W
, 16,
72 MTK_VDEC_MIN_H
, MTK_VDEC_MAX_H
, 16 },
76 #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
77 #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
79 static struct mtk_video_fmt
*mtk_vdec_find_format(struct v4l2_format
*f
)
81 struct mtk_video_fmt
*fmt
;
84 for (k
= 0; k
< NUM_FORMATS
; k
++) {
85 fmt
= &mtk_video_formats
[k
];
86 if (fmt
->fourcc
== f
->fmt
.pix_mp
.pixelformat
)
93 static struct mtk_q_data
*mtk_vdec_get_q_data(struct mtk_vcodec_ctx
*ctx
,
94 enum v4l2_buf_type type
)
96 if (V4L2_TYPE_IS_OUTPUT(type
))
97 return &ctx
->q_data
[MTK_Q_DATA_SRC
];
99 return &ctx
->q_data
[MTK_Q_DATA_DST
];
103 * This function tries to clean all display buffers, the buffers will return
105 * Note the buffers returned from codec driver may still be in driver's
108 static struct vb2_buffer
*get_display_buffer(struct mtk_vcodec_ctx
*ctx
)
110 struct vdec_fb
*disp_frame_buffer
= NULL
;
111 struct mtk_video_dec_buf
*dstbuf
;
113 mtk_v4l2_debug(3, "[%d]", ctx
->id
);
114 if (vdec_if_get_param(ctx
,
115 GET_PARAM_DISP_FRAME_BUFFER
,
116 &disp_frame_buffer
)) {
117 mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
122 if (disp_frame_buffer
== NULL
) {
123 mtk_v4l2_debug(3, "No display frame buffer");
127 dstbuf
= container_of(disp_frame_buffer
, struct mtk_video_dec_buf
,
129 mutex_lock(&ctx
->lock
);
131 vb2_set_plane_payload(&dstbuf
->vb
.vb2_buf
, 0,
132 ctx
->picinfo
.y_bs_sz
);
133 vb2_set_plane_payload(&dstbuf
->vb
.vb2_buf
, 1,
134 ctx
->picinfo
.c_bs_sz
);
136 dstbuf
->ready_to_display
= true;
139 "[%d]status=%x queue id=%d to done_list %d",
140 ctx
->id
, disp_frame_buffer
->status
,
141 dstbuf
->vb
.vb2_buf
.index
,
142 dstbuf
->queued_in_vb2
);
144 v4l2_m2m_buf_done(&dstbuf
->vb
, VB2_BUF_STATE_DONE
);
145 ctx
->decoded_frame_cnt
++;
147 mutex_unlock(&ctx
->lock
);
148 return &dstbuf
->vb
.vb2_buf
;
152 * This function tries to clean all capture buffers that are not used as
153 * reference buffers by codec driver any more
154 * In this case, we need re-queue buffer to vb2 buffer if user space
155 * already returns this buffer to v4l2 or this buffer is just the output of
156 * previous sps/pps/resolution change decode, or do nothing if user
157 * space still owns this buffer
159 static struct vb2_buffer
*get_free_buffer(struct mtk_vcodec_ctx
*ctx
)
161 struct mtk_video_dec_buf
*dstbuf
;
162 struct vdec_fb
*free_frame_buffer
= NULL
;
164 if (vdec_if_get_param(ctx
,
165 GET_PARAM_FREE_FRAME_BUFFER
,
166 &free_frame_buffer
)) {
167 mtk_v4l2_err("[%d] Error!! Cannot get param", ctx
->id
);
170 if (free_frame_buffer
== NULL
) {
171 mtk_v4l2_debug(3, " No free frame buffer");
175 mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
176 ctx
->id
, free_frame_buffer
);
178 dstbuf
= container_of(free_frame_buffer
, struct mtk_video_dec_buf
,
181 mutex_lock(&ctx
->lock
);
183 if ((dstbuf
->queued_in_vb2
) &&
184 (dstbuf
->queued_in_v4l2
) &&
185 (free_frame_buffer
->status
== FB_ST_FREE
)) {
187 * After decode sps/pps or non-display buffer, we don't
188 * need to return capture buffer to user space, but
189 * just re-queue this capture buffer to vb2 queue.
190 * This reduce overheads that dq/q unused capture
191 * buffer. In this case, queued_in_vb2 = true.
194 "[%d]status=%x queue id=%d to rdy_queue %d",
195 ctx
->id
, free_frame_buffer
->status
,
196 dstbuf
->vb
.vb2_buf
.index
,
197 dstbuf
->queued_in_vb2
);
198 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, &dstbuf
->vb
);
199 } else if ((dstbuf
->queued_in_vb2
== false) &&
200 (dstbuf
->queued_in_v4l2
== true)) {
202 * If buffer in v4l2 driver but not in vb2 queue yet,
203 * and we get this buffer from free_list, it means
204 * that codec driver do not use this buffer as
205 * reference buffer anymore. We should q buffer to vb2
206 * queue, so later work thread could get this buffer
207 * for decode. In this case, queued_in_vb2 = false
208 * means this buffer is not from previous decode
212 "[%d]status=%x queue id=%d to rdy_queue",
213 ctx
->id
, free_frame_buffer
->status
,
214 dstbuf
->vb
.vb2_buf
.index
);
215 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, &dstbuf
->vb
);
216 dstbuf
->queued_in_vb2
= true;
219 * Codec driver do not need to reference this capture
220 * buffer and this buffer is not in v4l2 driver.
221 * Then we don't need to do any thing, just add log when
222 * we need to debug buffer flow.
223 * When this buffer q from user space, it could
224 * directly q to vb2 buffer
226 mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
227 ctx
->id
, free_frame_buffer
->status
,
228 dstbuf
->vb
.vb2_buf
.index
,
229 dstbuf
->queued_in_vb2
,
230 dstbuf
->queued_in_v4l2
);
232 dstbuf
->used
= false;
234 mutex_unlock(&ctx
->lock
);
235 return &dstbuf
->vb
.vb2_buf
;
238 static void clean_display_buffer(struct mtk_vcodec_ctx
*ctx
)
240 struct vb2_buffer
*framptr
;
243 framptr
= get_display_buffer(ctx
);
247 static void clean_free_buffer(struct mtk_vcodec_ctx
*ctx
)
249 struct vb2_buffer
*framptr
;
252 framptr
= get_free_buffer(ctx
);
256 static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx
*ctx
)
258 static const struct v4l2_event ev_src_ch
= {
259 .type
= V4L2_EVENT_SOURCE_CHANGE
,
260 .u
.src_change
.changes
=
261 V4L2_EVENT_SRC_CH_RESOLUTION
,
264 mtk_v4l2_debug(1, "[%d]", ctx
->id
);
265 v4l2_event_queue_fh(&ctx
->fh
, &ev_src_ch
);
268 static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx
*ctx
)
273 ret
= vdec_if_decode(ctx
, NULL
, NULL
, &res_chg
);
275 mtk_v4l2_err("DecodeFinal failed, ret=%d", ret
);
277 clean_display_buffer(ctx
);
278 clean_free_buffer(ctx
);
281 static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx
*ctx
)
283 unsigned int dpbsize
= 0;
286 if (vdec_if_get_param(ctx
,
288 &ctx
->last_decoded_picinfo
)) {
289 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
294 if (ctx
->last_decoded_picinfo
.pic_w
== 0 ||
295 ctx
->last_decoded_picinfo
.pic_h
== 0 ||
296 ctx
->last_decoded_picinfo
.buf_w
== 0 ||
297 ctx
->last_decoded_picinfo
.buf_h
== 0) {
298 mtk_v4l2_err("Cannot get correct pic info");
302 if ((ctx
->last_decoded_picinfo
.pic_w
== ctx
->picinfo
.pic_w
) ||
303 (ctx
->last_decoded_picinfo
.pic_h
== ctx
->picinfo
.pic_h
))
307 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
308 ctx
->id
, ctx
->last_decoded_picinfo
.pic_w
,
309 ctx
->last_decoded_picinfo
.pic_h
,
310 ctx
->picinfo
.pic_w
, ctx
->picinfo
.pic_h
,
311 ctx
->last_decoded_picinfo
.buf_w
,
312 ctx
->last_decoded_picinfo
.buf_h
);
314 ret
= vdec_if_get_param(ctx
, GET_PARAM_DPB_SIZE
, &dpbsize
);
316 mtk_v4l2_err("Incorrect dpb size, ret=%d", ret
);
318 ctx
->dpb_size
= dpbsize
;
323 static void mtk_vdec_worker(struct work_struct
*work
)
325 struct mtk_vcodec_ctx
*ctx
= container_of(work
, struct mtk_vcodec_ctx
,
327 struct mtk_vcodec_dev
*dev
= ctx
->dev
;
328 struct vb2_v4l2_buffer
*src_buf
, *dst_buf
;
329 struct mtk_vcodec_mem buf
;
331 bool res_chg
= false;
333 struct mtk_video_dec_buf
*dst_buf_info
, *src_buf_info
;
335 src_buf
= v4l2_m2m_next_src_buf(ctx
->m2m_ctx
);
336 if (src_buf
== NULL
) {
337 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
338 mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx
->id
);
342 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
343 if (dst_buf
== NULL
) {
344 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
345 mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx
->id
);
349 src_buf_info
= container_of(src_buf
, struct mtk_video_dec_buf
, vb
);
350 dst_buf_info
= container_of(dst_buf
, struct mtk_video_dec_buf
, vb
);
352 pfb
= &dst_buf_info
->frame_buffer
;
353 pfb
->base_y
.va
= vb2_plane_vaddr(&dst_buf
->vb2_buf
, 0);
354 pfb
->base_y
.dma_addr
= vb2_dma_contig_plane_dma_addr(&dst_buf
->vb2_buf
, 0);
355 pfb
->base_y
.size
= ctx
->picinfo
.y_bs_sz
+ ctx
->picinfo
.y_len_sz
;
357 pfb
->base_c
.va
= vb2_plane_vaddr(&dst_buf
->vb2_buf
, 1);
358 pfb
->base_c
.dma_addr
= vb2_dma_contig_plane_dma_addr(&dst_buf
->vb2_buf
, 1);
359 pfb
->base_c
.size
= ctx
->picinfo
.c_bs_sz
+ ctx
->picinfo
.c_len_sz
;
361 mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx
->id
);
364 "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
365 dst_buf
->vb2_buf
.index
, pfb
,
366 pfb
->base_y
.va
, &pfb
->base_y
.dma_addr
,
367 &pfb
->base_c
.dma_addr
, pfb
->base_y
.size
);
369 if (src_buf_info
->lastframe
) {
370 mtk_v4l2_debug(1, "Got empty flush input buffer.");
371 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
373 /* update dst buf status */
374 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
);
375 mutex_lock(&ctx
->lock
);
376 dst_buf_info
->used
= false;
377 mutex_unlock(&ctx
->lock
);
379 vdec_if_decode(ctx
, NULL
, NULL
, &res_chg
);
380 clean_display_buffer(ctx
);
381 vb2_set_plane_payload(&dst_buf_info
->vb
.vb2_buf
, 0, 0);
382 vb2_set_plane_payload(&dst_buf_info
->vb
.vb2_buf
, 1, 0);
383 dst_buf
->flags
|= V4L2_BUF_FLAG_LAST
;
384 v4l2_m2m_buf_done(&dst_buf_info
->vb
, VB2_BUF_STATE_DONE
);
385 clean_free_buffer(ctx
);
386 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
389 buf
.va
= vb2_plane_vaddr(&src_buf
->vb2_buf
, 0);
390 buf
.dma_addr
= vb2_dma_contig_plane_dma_addr(&src_buf
->vb2_buf
, 0);
391 buf
.size
= (size_t)src_buf
->planes
[0].bytesused
;
393 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
394 mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
395 ctx
->id
, src_buf
->vb2_buf
.index
);
398 mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
399 ctx
->id
, buf
.va
, &buf
.dma_addr
, buf
.size
, src_buf
);
400 dst_buf_info
->vb
.vb2_buf
.timestamp
401 = src_buf_info
->vb
.vb2_buf
.timestamp
;
402 dst_buf_info
->vb
.timecode
403 = src_buf_info
->vb
.timecode
;
404 mutex_lock(&ctx
->lock
);
405 dst_buf_info
->used
= true;
406 mutex_unlock(&ctx
->lock
);
407 src_buf_info
->used
= true;
409 ret
= vdec_if_decode(ctx
, &buf
, pfb
, &res_chg
);
413 " <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
415 src_buf
->vb2_buf
.index
,
417 src_buf_info
->vb
.vb2_buf
.timestamp
,
418 dst_buf
->vb2_buf
.index
,
420 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
422 mutex_lock(&ctx
->lock
);
423 src_buf_info
->error
= true;
424 mutex_unlock(&ctx
->lock
);
426 v4l2_m2m_buf_done(&src_buf_info
->vb
, VB2_BUF_STATE_ERROR
);
427 } else if (res_chg
== false) {
429 * we only return src buffer with VB2_BUF_STATE_DONE
430 * when decode success without resolution change
432 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
433 v4l2_m2m_buf_done(&src_buf_info
->vb
, VB2_BUF_STATE_DONE
);
436 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
);
437 clean_display_buffer(ctx
);
438 clean_free_buffer(ctx
);
440 if (!ret
&& res_chg
) {
441 mtk_vdec_pic_info_update(ctx
);
443 * On encountering a resolution change in the stream.
444 * The driver must first process and decode all
445 * remaining buffers from before the resolution change
446 * point, so call flush decode here
448 mtk_vdec_flush_decoder(ctx
);
450 * After all buffers containing decoded frames from
451 * before the resolution change point ready to be
452 * dequeued on the CAPTURE queue, the driver sends a
453 * V4L2_EVENT_SOURCE_CHANGE event for source change
454 * type V4L2_EVENT_SRC_CH_RESOLUTION
456 mtk_vdec_queue_res_chg_event(ctx
);
458 v4l2_m2m_job_finish(dev
->m2m_dev_dec
, ctx
->m2m_ctx
);
461 static int vidioc_try_decoder_cmd(struct file
*file
, void *priv
,
462 struct v4l2_decoder_cmd
*cmd
)
465 case V4L2_DEC_CMD_STOP
:
466 case V4L2_DEC_CMD_START
:
467 if (cmd
->flags
!= 0) {
468 mtk_v4l2_err("cmd->flags=%u", cmd
->flags
);
479 static int vidioc_decoder_cmd(struct file
*file
, void *priv
,
480 struct v4l2_decoder_cmd
*cmd
)
482 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
483 struct vb2_queue
*src_vq
, *dst_vq
;
486 ret
= vidioc_try_decoder_cmd(file
, priv
, cmd
);
490 mtk_v4l2_debug(1, "decoder cmd=%u", cmd
->cmd
);
491 dst_vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
,
492 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
);
494 case V4L2_DEC_CMD_STOP
:
495 src_vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
,
496 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
);
497 if (!vb2_is_streaming(src_vq
)) {
498 mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
501 if (!vb2_is_streaming(dst_vq
)) {
502 mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
505 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, &ctx
->empty_flush_buf
->vb
);
506 v4l2_m2m_try_schedule(ctx
->m2m_ctx
);
509 case V4L2_DEC_CMD_START
:
510 vb2_clear_last_buffer_dequeued(dst_vq
);
520 void mtk_vdec_unlock(struct mtk_vcodec_ctx
*ctx
)
522 mutex_unlock(&ctx
->dev
->dec_mutex
);
525 void mtk_vdec_lock(struct mtk_vcodec_ctx
*ctx
)
527 mutex_lock(&ctx
->dev
->dec_mutex
);
530 void mtk_vcodec_dec_release(struct mtk_vcodec_ctx
*ctx
)
533 ctx
->state
= MTK_STATE_FREE
;
536 void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx
*ctx
)
538 struct mtk_q_data
*q_data
;
540 ctx
->m2m_ctx
->q_lock
= &ctx
->dev
->dev_mutex
;
541 ctx
->fh
.m2m_ctx
= ctx
->m2m_ctx
;
542 ctx
->fh
.ctrl_handler
= &ctx
->ctrl_hdl
;
543 INIT_WORK(&ctx
->decode_work
, mtk_vdec_worker
);
544 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
545 ctx
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
546 ctx
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
547 ctx
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
549 q_data
= &ctx
->q_data
[MTK_Q_DATA_SRC
];
550 memset(q_data
, 0, sizeof(struct mtk_q_data
));
551 q_data
->visible_width
= DFT_CFG_WIDTH
;
552 q_data
->visible_height
= DFT_CFG_HEIGHT
;
553 q_data
->fmt
= &mtk_video_formats
[OUT_FMT_IDX
];
554 q_data
->field
= V4L2_FIELD_NONE
;
556 q_data
->sizeimage
[0] = DFT_CFG_WIDTH
* DFT_CFG_HEIGHT
;
557 q_data
->bytesperline
[0] = 0;
559 q_data
= &ctx
->q_data
[MTK_Q_DATA_DST
];
560 memset(q_data
, 0, sizeof(struct mtk_q_data
));
561 q_data
->visible_width
= DFT_CFG_WIDTH
;
562 q_data
->visible_height
= DFT_CFG_HEIGHT
;
563 q_data
->coded_width
= DFT_CFG_WIDTH
;
564 q_data
->coded_height
= DFT_CFG_HEIGHT
;
565 q_data
->fmt
= &mtk_video_formats
[CAP_FMT_IDX
];
566 q_data
->field
= V4L2_FIELD_NONE
;
568 v4l_bound_align_image(&q_data
->coded_width
,
571 &q_data
->coded_height
,
573 MTK_VDEC_MAX_H
, 5, 6);
575 q_data
->sizeimage
[0] = q_data
->coded_width
* q_data
->coded_height
;
576 q_data
->bytesperline
[0] = q_data
->coded_width
;
577 q_data
->sizeimage
[1] = q_data
->sizeimage
[0] / 2;
578 q_data
->bytesperline
[1] = q_data
->coded_width
;
581 static int vidioc_vdec_qbuf(struct file
*file
, void *priv
,
582 struct v4l2_buffer
*buf
)
584 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
586 if (ctx
->state
== MTK_STATE_ABORT
) {
587 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
592 return v4l2_m2m_qbuf(file
, ctx
->m2m_ctx
, buf
);
595 static int vidioc_vdec_dqbuf(struct file
*file
, void *priv
,
596 struct v4l2_buffer
*buf
)
598 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
600 if (ctx
->state
== MTK_STATE_ABORT
) {
601 mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
606 return v4l2_m2m_dqbuf(file
, ctx
->m2m_ctx
, buf
);
609 static int vidioc_vdec_querycap(struct file
*file
, void *priv
,
610 struct v4l2_capability
*cap
)
612 strscpy(cap
->driver
, MTK_VCODEC_DEC_NAME
, sizeof(cap
->driver
));
613 strscpy(cap
->bus_info
, MTK_PLATFORM_STR
, sizeof(cap
->bus_info
));
614 strscpy(cap
->card
, MTK_PLATFORM_STR
, sizeof(cap
->card
));
619 static int vidioc_vdec_subscribe_evt(struct v4l2_fh
*fh
,
620 const struct v4l2_event_subscription
*sub
)
624 return v4l2_event_subscribe(fh
, sub
, 2, NULL
);
625 case V4L2_EVENT_SOURCE_CHANGE
:
626 return v4l2_src_change_event_subscribe(fh
, sub
);
628 return v4l2_ctrl_subscribe_event(fh
, sub
);
632 static int vidioc_try_fmt(struct v4l2_format
*f
, struct mtk_video_fmt
*fmt
)
634 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
637 pix_fmt_mp
->field
= V4L2_FIELD_NONE
;
639 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
640 pix_fmt_mp
->num_planes
= 1;
641 pix_fmt_mp
->plane_fmt
[0].bytesperline
= 0;
642 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
645 pix_fmt_mp
->height
= clamp(pix_fmt_mp
->height
,
648 pix_fmt_mp
->width
= clamp(pix_fmt_mp
->width
,
653 * Find next closer width align 64, heign align 64, size align
655 * Note: This only get default value, the real HW needed value
656 * only available when ctx in MTK_STATE_HEADER state
658 tmp_w
= pix_fmt_mp
->width
;
659 tmp_h
= pix_fmt_mp
->height
;
660 v4l_bound_align_image(&pix_fmt_mp
->width
,
665 MTK_VDEC_MAX_H
, 6, 9);
667 if (pix_fmt_mp
->width
< tmp_w
&&
668 (pix_fmt_mp
->width
+ 64) <= MTK_VDEC_MAX_W
)
669 pix_fmt_mp
->width
+= 64;
670 if (pix_fmt_mp
->height
< tmp_h
&&
671 (pix_fmt_mp
->height
+ 64) <= MTK_VDEC_MAX_H
)
672 pix_fmt_mp
->height
+= 64;
675 "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
676 tmp_w
, tmp_h
, pix_fmt_mp
->width
,
678 pix_fmt_mp
->width
* pix_fmt_mp
->height
);
680 pix_fmt_mp
->num_planes
= fmt
->num_planes
;
681 pix_fmt_mp
->plane_fmt
[0].sizeimage
=
682 pix_fmt_mp
->width
* pix_fmt_mp
->height
;
683 pix_fmt_mp
->plane_fmt
[0].bytesperline
= pix_fmt_mp
->width
;
685 if (pix_fmt_mp
->num_planes
== 2) {
686 pix_fmt_mp
->plane_fmt
[1].sizeimage
=
687 (pix_fmt_mp
->width
* pix_fmt_mp
->height
) / 2;
688 pix_fmt_mp
->plane_fmt
[1].bytesperline
=
693 for (i
= 0; i
< pix_fmt_mp
->num_planes
; i
++)
694 memset(&(pix_fmt_mp
->plane_fmt
[i
].reserved
[0]), 0x0,
695 sizeof(pix_fmt_mp
->plane_fmt
[0].reserved
));
697 pix_fmt_mp
->flags
= 0;
698 memset(&pix_fmt_mp
->reserved
, 0x0, sizeof(pix_fmt_mp
->reserved
));
702 static int vidioc_try_fmt_vid_cap_mplane(struct file
*file
, void *priv
,
703 struct v4l2_format
*f
)
705 struct mtk_video_fmt
*fmt
;
707 fmt
= mtk_vdec_find_format(f
);
709 f
->fmt
.pix
.pixelformat
= mtk_video_formats
[CAP_FMT_IDX
].fourcc
;
710 fmt
= mtk_vdec_find_format(f
);
713 return vidioc_try_fmt(f
, fmt
);
716 static int vidioc_try_fmt_vid_out_mplane(struct file
*file
, void *priv
,
717 struct v4l2_format
*f
)
719 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
720 struct mtk_video_fmt
*fmt
;
722 fmt
= mtk_vdec_find_format(f
);
724 f
->fmt
.pix
.pixelformat
= mtk_video_formats
[OUT_FMT_IDX
].fourcc
;
725 fmt
= mtk_vdec_find_format(f
);
728 if (pix_fmt_mp
->plane_fmt
[0].sizeimage
== 0) {
729 mtk_v4l2_err("sizeimage of output format must be given");
733 return vidioc_try_fmt(f
, fmt
);
736 static int vidioc_vdec_g_selection(struct file
*file
, void *priv
,
737 struct v4l2_selection
*s
)
739 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
740 struct mtk_q_data
*q_data
;
742 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
745 q_data
= &ctx
->q_data
[MTK_Q_DATA_DST
];
748 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
751 s
->r
.width
= ctx
->picinfo
.pic_w
;
752 s
->r
.height
= ctx
->picinfo
.pic_h
;
754 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
757 s
->r
.width
= ctx
->picinfo
.buf_w
;
758 s
->r
.height
= ctx
->picinfo
.buf_h
;
760 case V4L2_SEL_TGT_COMPOSE
:
761 if (vdec_if_get_param(ctx
, GET_PARAM_CROP_INFO
, &(s
->r
))) {
762 /* set to default value if header info not ready yet*/
765 s
->r
.width
= q_data
->visible_width
;
766 s
->r
.height
= q_data
->visible_height
;
773 if (ctx
->state
< MTK_STATE_HEADER
) {
774 /* set to default value if header info not ready yet*/
777 s
->r
.width
= q_data
->visible_width
;
778 s
->r
.height
= q_data
->visible_height
;
785 static int vidioc_vdec_s_selection(struct file
*file
, void *priv
,
786 struct v4l2_selection
*s
)
788 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
790 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
794 case V4L2_SEL_TGT_COMPOSE
:
797 s
->r
.width
= ctx
->picinfo
.pic_w
;
798 s
->r
.height
= ctx
->picinfo
.pic_h
;
807 static int vidioc_vdec_s_fmt(struct file
*file
, void *priv
,
808 struct v4l2_format
*f
)
810 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
811 struct v4l2_pix_format_mplane
*pix_mp
;
812 struct mtk_q_data
*q_data
;
814 struct mtk_video_fmt
*fmt
;
816 mtk_v4l2_debug(3, "[%d]", ctx
->id
);
818 q_data
= mtk_vdec_get_q_data(ctx
, f
->type
);
822 pix_mp
= &f
->fmt
.pix_mp
;
823 if ((f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) &&
824 vb2_is_busy(&ctx
->m2m_ctx
->out_q_ctx
.q
)) {
825 mtk_v4l2_err("out_q_ctx buffers already requested");
829 if ((f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) &&
830 vb2_is_busy(&ctx
->m2m_ctx
->cap_q_ctx
.q
)) {
831 mtk_v4l2_err("cap_q_ctx buffers already requested");
835 fmt
= mtk_vdec_find_format(f
);
837 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
838 f
->fmt
.pix
.pixelformat
=
839 mtk_video_formats
[OUT_FMT_IDX
].fourcc
;
840 fmt
= mtk_vdec_find_format(f
);
841 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
842 f
->fmt
.pix
.pixelformat
=
843 mtk_video_formats
[CAP_FMT_IDX
].fourcc
;
844 fmt
= mtk_vdec_find_format(f
);
849 vidioc_try_fmt(f
, q_data
->fmt
);
850 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
851 q_data
->sizeimage
[0] = pix_mp
->plane_fmt
[0].sizeimage
;
852 q_data
->coded_width
= pix_mp
->width
;
853 q_data
->coded_height
= pix_mp
->height
;
855 ctx
->colorspace
= f
->fmt
.pix_mp
.colorspace
;
856 ctx
->ycbcr_enc
= f
->fmt
.pix_mp
.ycbcr_enc
;
857 ctx
->quantization
= f
->fmt
.pix_mp
.quantization
;
858 ctx
->xfer_func
= f
->fmt
.pix_mp
.xfer_func
;
860 if (ctx
->state
== MTK_STATE_FREE
) {
861 ret
= vdec_if_init(ctx
, q_data
->fmt
->fourcc
);
863 mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
867 ctx
->state
= MTK_STATE_INIT
;
874 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
875 struct v4l2_frmsizeenum
*fsize
)
878 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
880 if (fsize
->index
!= 0)
883 for (i
= 0; i
< NUM_SUPPORTED_FRAMESIZE
; ++i
) {
884 if (fsize
->pixel_format
!= mtk_vdec_framesizes
[i
].fourcc
)
887 fsize
->type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
888 fsize
->stepwise
= mtk_vdec_framesizes
[i
].stepwise
;
889 if (!(ctx
->dev
->dec_capability
&
890 VCODEC_CAPABILITY_4K_DISABLED
)) {
891 mtk_v4l2_debug(3, "4K is enabled");
892 fsize
->stepwise
.max_width
=
893 VCODEC_DEC_4K_CODED_WIDTH
;
894 fsize
->stepwise
.max_height
=
895 VCODEC_DEC_4K_CODED_HEIGHT
;
897 mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
898 ctx
->dev
->dec_capability
,
899 fsize
->stepwise
.min_width
,
900 fsize
->stepwise
.max_width
,
901 fsize
->stepwise
.step_width
,
902 fsize
->stepwise
.min_height
,
903 fsize
->stepwise
.max_height
,
904 fsize
->stepwise
.step_height
);
911 static int vidioc_enum_fmt(struct v4l2_fmtdesc
*f
, bool output_queue
)
913 struct mtk_video_fmt
*fmt
;
916 for (i
= 0; i
< NUM_FORMATS
; i
++) {
917 if (output_queue
&& (mtk_video_formats
[i
].type
!= MTK_FMT_DEC
))
920 (mtk_video_formats
[i
].type
!= MTK_FMT_FRAME
))
928 if (i
== NUM_FORMATS
)
931 fmt
= &mtk_video_formats
[i
];
932 f
->pixelformat
= fmt
->fourcc
;
937 static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file
*file
, void *pirv
,
938 struct v4l2_fmtdesc
*f
)
940 return vidioc_enum_fmt(f
, false);
943 static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file
*file
, void *priv
,
944 struct v4l2_fmtdesc
*f
)
946 return vidioc_enum_fmt(f
, true);
949 static int vidioc_vdec_g_fmt(struct file
*file
, void *priv
,
950 struct v4l2_format
*f
)
952 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(priv
);
953 struct v4l2_pix_format_mplane
*pix_mp
= &f
->fmt
.pix_mp
;
954 struct vb2_queue
*vq
;
955 struct mtk_q_data
*q_data
;
957 vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, f
->type
);
959 mtk_v4l2_err("no vb2 queue for type=%d", f
->type
);
963 q_data
= mtk_vdec_get_q_data(ctx
, f
->type
);
965 pix_mp
->field
= V4L2_FIELD_NONE
;
966 pix_mp
->colorspace
= ctx
->colorspace
;
967 pix_mp
->ycbcr_enc
= ctx
->ycbcr_enc
;
968 pix_mp
->quantization
= ctx
->quantization
;
969 pix_mp
->xfer_func
= ctx
->xfer_func
;
971 if ((f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) &&
972 (ctx
->state
>= MTK_STATE_HEADER
)) {
973 /* Until STREAMOFF is called on the CAPTURE queue
974 * (acknowledging the event), the driver operates as if
975 * the resolution hasn't changed yet.
976 * So we just return picinfo yet, and update picinfo in
977 * stop_streaming hook function
979 q_data
->sizeimage
[0] = ctx
->picinfo
.y_bs_sz
+
980 ctx
->picinfo
.y_len_sz
;
981 q_data
->sizeimage
[1] = ctx
->picinfo
.c_bs_sz
+
982 ctx
->picinfo
.c_len_sz
;
983 q_data
->bytesperline
[0] = ctx
->last_decoded_picinfo
.buf_w
;
984 q_data
->bytesperline
[1] = ctx
->last_decoded_picinfo
.buf_w
;
985 q_data
->coded_width
= ctx
->picinfo
.buf_w
;
986 q_data
->coded_height
= ctx
->picinfo
.buf_h
;
989 * Width and height are set to the dimensions
990 * of the movie, the buffer is bigger and
991 * further processing stages should crop to this
994 pix_mp
->width
= q_data
->coded_width
;
995 pix_mp
->height
= q_data
->coded_height
;
998 * Set pixelformat to the format in which mt vcodec
999 * outputs the decoded frame
1001 pix_mp
->num_planes
= q_data
->fmt
->num_planes
;
1002 pix_mp
->pixelformat
= q_data
->fmt
->fourcc
;
1003 pix_mp
->plane_fmt
[0].bytesperline
= q_data
->bytesperline
[0];
1004 pix_mp
->plane_fmt
[0].sizeimage
= q_data
->sizeimage
[0];
1005 pix_mp
->plane_fmt
[1].bytesperline
= q_data
->bytesperline
[1];
1006 pix_mp
->plane_fmt
[1].sizeimage
= q_data
->sizeimage
[1];
1008 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1010 * This is run on OUTPUT
1011 * The buffer contains compressed image
1012 * so width and height have no meaning.
1013 * Assign value here to pass v4l2-compliance test
1015 pix_mp
->width
= q_data
->visible_width
;
1016 pix_mp
->height
= q_data
->visible_height
;
1017 pix_mp
->plane_fmt
[0].bytesperline
= q_data
->bytesperline
[0];
1018 pix_mp
->plane_fmt
[0].sizeimage
= q_data
->sizeimage
[0];
1019 pix_mp
->pixelformat
= q_data
->fmt
->fourcc
;
1020 pix_mp
->num_planes
= q_data
->fmt
->num_planes
;
1022 pix_mp
->width
= q_data
->coded_width
;
1023 pix_mp
->height
= q_data
->coded_height
;
1024 pix_mp
->num_planes
= q_data
->fmt
->num_planes
;
1025 pix_mp
->pixelformat
= q_data
->fmt
->fourcc
;
1026 pix_mp
->plane_fmt
[0].bytesperline
= q_data
->bytesperline
[0];
1027 pix_mp
->plane_fmt
[0].sizeimage
= q_data
->sizeimage
[0];
1028 pix_mp
->plane_fmt
[1].bytesperline
= q_data
->bytesperline
[1];
1029 pix_mp
->plane_fmt
[1].sizeimage
= q_data
->sizeimage
[1];
1031 mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
1032 ctx
->id
, f
->type
, ctx
->state
);
1038 static int vb2ops_vdec_queue_setup(struct vb2_queue
*vq
,
1039 unsigned int *nbuffers
,
1040 unsigned int *nplanes
,
1041 unsigned int sizes
[],
1042 struct device
*alloc_devs
[])
1044 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(vq
);
1045 struct mtk_q_data
*q_data
;
1048 q_data
= mtk_vdec_get_q_data(ctx
, vq
->type
);
1050 if (q_data
== NULL
) {
1051 mtk_v4l2_err("vq->type=%d err\n", vq
->type
);
1056 for (i
= 0; i
< *nplanes
; i
++) {
1057 if (sizes
[i
] < q_data
->sizeimage
[i
])
1061 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1066 for (i
= 0; i
< *nplanes
; i
++)
1067 sizes
[i
] = q_data
->sizeimage
[i
];
1071 "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
1072 ctx
->id
, vq
->type
, *nplanes
, *nbuffers
,
1073 sizes
[0], sizes
[1]);
1078 static int vb2ops_vdec_buf_prepare(struct vb2_buffer
*vb
)
1080 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1081 struct mtk_q_data
*q_data
;
1084 mtk_v4l2_debug(3, "[%d] (%d) id=%d",
1085 ctx
->id
, vb
->vb2_queue
->type
, vb
->index
);
1087 q_data
= mtk_vdec_get_q_data(ctx
, vb
->vb2_queue
->type
);
1089 for (i
= 0; i
< q_data
->fmt
->num_planes
; i
++) {
1090 if (vb2_plane_size(vb
, i
) < q_data
->sizeimage
[i
]) {
1091 mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
1092 i
, vb2_plane_size(vb
, i
),
1093 q_data
->sizeimage
[i
]);
1100 static void vb2ops_vdec_buf_queue(struct vb2_buffer
*vb
)
1102 struct vb2_v4l2_buffer
*src_buf
;
1103 struct mtk_vcodec_mem src_mem
;
1104 bool res_chg
= false;
1106 unsigned int dpbsize
= 1;
1107 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1108 struct vb2_v4l2_buffer
*vb2_v4l2
= NULL
;
1109 struct mtk_video_dec_buf
*buf
= NULL
;
1111 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
1112 ctx
->id
, vb
->vb2_queue
->type
,
1115 * check if this buffer is ready to be used after decode
1117 if (vb
->vb2_queue
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1118 vb2_v4l2
= to_vb2_v4l2_buffer(vb
);
1119 buf
= container_of(vb2_v4l2
, struct mtk_video_dec_buf
, vb
);
1120 mutex_lock(&ctx
->lock
);
1121 if (buf
->used
== false) {
1122 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, vb2_v4l2
);
1123 buf
->queued_in_vb2
= true;
1124 buf
->queued_in_v4l2
= true;
1125 buf
->ready_to_display
= false;
1127 buf
->queued_in_vb2
= false;
1128 buf
->queued_in_v4l2
= true;
1129 buf
->ready_to_display
= false;
1131 mutex_unlock(&ctx
->lock
);
1135 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, to_vb2_v4l2_buffer(vb
));
1137 if (ctx
->state
!= MTK_STATE_INIT
) {
1138 mtk_v4l2_debug(3, "[%d] already init driver %d",
1139 ctx
->id
, ctx
->state
);
1143 src_buf
= v4l2_m2m_next_src_buf(ctx
->m2m_ctx
);
1145 mtk_v4l2_err("No src buffer");
1148 buf
= container_of(src_buf
, struct mtk_video_dec_buf
, vb
);
1149 if (buf
->lastframe
) {
1150 /* This shouldn't happen. Just in case. */
1151 mtk_v4l2_err("Invalid flush buffer.");
1152 v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
1156 src_mem
.va
= vb2_plane_vaddr(&src_buf
->vb2_buf
, 0);
1157 src_mem
.dma_addr
= vb2_dma_contig_plane_dma_addr(&src_buf
->vb2_buf
, 0);
1158 src_mem
.size
= (size_t)src_buf
->planes
[0].bytesused
;
1160 "[%d] buf id=%d va=%p dma=%pad size=%zx",
1161 ctx
->id
, src_buf
->index
,
1162 src_mem
.va
, &src_mem
.dma_addr
,
1165 ret
= vdec_if_decode(ctx
, &src_mem
, NULL
, &res_chg
);
1166 if (ret
|| !res_chg
) {
1168 * fb == NULL means to parse SPS/PPS header or
1169 * resolution info in src_mem. Decode can fail
1170 * if there is no SPS header or picture info
1174 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
1176 mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.",
1178 ctx
->state
= MTK_STATE_ABORT
;
1179 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_ERROR
);
1181 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_DONE
);
1183 mtk_v4l2_debug(ret
? 0 : 1,
1184 "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
1185 ctx
->id
, src_buf
->index
,
1186 src_mem
.size
, ret
, res_chg
);
1190 if (vdec_if_get_param(ctx
, GET_PARAM_PIC_INFO
, &ctx
->picinfo
)) {
1191 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
1196 ctx
->last_decoded_picinfo
= ctx
->picinfo
;
1197 ctx
->q_data
[MTK_Q_DATA_DST
].sizeimage
[0] =
1198 ctx
->picinfo
.y_bs_sz
+
1199 ctx
->picinfo
.y_len_sz
;
1200 ctx
->q_data
[MTK_Q_DATA_DST
].bytesperline
[0] =
1202 ctx
->q_data
[MTK_Q_DATA_DST
].sizeimage
[1] =
1203 ctx
->picinfo
.c_bs_sz
+
1204 ctx
->picinfo
.c_len_sz
;
1205 ctx
->q_data
[MTK_Q_DATA_DST
].bytesperline
[1] = ctx
->picinfo
.buf_w
;
1206 mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
1208 ctx
->picinfo
.buf_w
, ctx
->picinfo
.buf_h
,
1209 ctx
->picinfo
.pic_w
, ctx
->picinfo
.pic_h
,
1210 ctx
->q_data
[MTK_Q_DATA_DST
].sizeimage
[0],
1211 ctx
->q_data
[MTK_Q_DATA_DST
].sizeimage
[1]);
1213 ret
= vdec_if_get_param(ctx
, GET_PARAM_DPB_SIZE
, &dpbsize
);
1215 mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx
->id
, ret
);
1217 ctx
->dpb_size
= dpbsize
;
1218 ctx
->state
= MTK_STATE_HEADER
;
1219 mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx
->id
, ctx
->dpb_size
);
1221 mtk_vdec_queue_res_chg_event(ctx
);
1224 static void vb2ops_vdec_buf_finish(struct vb2_buffer
*vb
)
1226 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1227 struct vb2_v4l2_buffer
*vb2_v4l2
;
1228 struct mtk_video_dec_buf
*buf
;
1231 vb2_v4l2
= container_of(vb
, struct vb2_v4l2_buffer
, vb2_buf
);
1232 buf
= container_of(vb2_v4l2
, struct mtk_video_dec_buf
, vb
);
1233 mutex_lock(&ctx
->lock
);
1234 if (vb
->vb2_queue
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1235 buf
->queued_in_v4l2
= false;
1236 buf
->queued_in_vb2
= false;
1238 buf_error
= buf
->error
;
1239 mutex_unlock(&ctx
->lock
);
1242 mtk_v4l2_err("Unrecoverable error on buffer.");
1243 ctx
->state
= MTK_STATE_ABORT
;
1247 static int vb2ops_vdec_buf_init(struct vb2_buffer
*vb
)
1249 struct vb2_v4l2_buffer
*vb2_v4l2
= container_of(vb
,
1250 struct vb2_v4l2_buffer
, vb2_buf
);
1251 struct mtk_video_dec_buf
*buf
= container_of(vb2_v4l2
,
1252 struct mtk_video_dec_buf
, vb
);
1254 if (vb
->vb2_queue
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1256 buf
->ready_to_display
= false;
1257 buf
->queued_in_v4l2
= false;
1259 buf
->lastframe
= false;
1265 static int vb2ops_vdec_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1267 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(q
);
1269 if (ctx
->state
== MTK_STATE_FLUSH
)
1270 ctx
->state
= MTK_STATE_HEADER
;
1275 static void vb2ops_vdec_stop_streaming(struct vb2_queue
*q
)
1277 struct vb2_v4l2_buffer
*src_buf
= NULL
, *dst_buf
= NULL
;
1278 struct mtk_vcodec_ctx
*ctx
= vb2_get_drv_priv(q
);
1280 mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
1281 ctx
->id
, q
->type
, ctx
->state
, ctx
->decoded_frame_cnt
);
1283 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1284 while ((src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
))) {
1285 struct mtk_video_dec_buf
*buf_info
= container_of(
1286 src_buf
, struct mtk_video_dec_buf
, vb
);
1287 if (!buf_info
->lastframe
)
1288 v4l2_m2m_buf_done(src_buf
,
1289 VB2_BUF_STATE_ERROR
);
1294 if (ctx
->state
>= MTK_STATE_HEADER
) {
1296 /* Until STREAMOFF is called on the CAPTURE queue
1297 * (acknowledging the event), the driver operates
1298 * as if the resolution hasn't changed yet, i.e.
1299 * VIDIOC_G_FMT< etc. return previous resolution.
1300 * So we update picinfo here
1302 ctx
->picinfo
= ctx
->last_decoded_picinfo
;
1305 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
1306 ctx
->id
, ctx
->last_decoded_picinfo
.pic_w
,
1307 ctx
->last_decoded_picinfo
.pic_h
,
1308 ctx
->picinfo
.pic_w
, ctx
->picinfo
.pic_h
,
1309 ctx
->last_decoded_picinfo
.buf_w
,
1310 ctx
->last_decoded_picinfo
.buf_h
);
1312 mtk_vdec_flush_decoder(ctx
);
1314 ctx
->state
= MTK_STATE_FLUSH
;
1316 while ((dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
))) {
1317 vb2_set_plane_payload(&dst_buf
->vb2_buf
, 0, 0);
1318 vb2_set_plane_payload(&dst_buf
->vb2_buf
, 1, 0);
1319 v4l2_m2m_buf_done(dst_buf
, VB2_BUF_STATE_ERROR
);
1324 static void m2mops_vdec_device_run(void *priv
)
1326 struct mtk_vcodec_ctx
*ctx
= priv
;
1327 struct mtk_vcodec_dev
*dev
= ctx
->dev
;
1329 queue_work(dev
->decode_workqueue
, &ctx
->decode_work
);
1332 static int m2mops_vdec_job_ready(void *m2m_priv
)
1334 struct mtk_vcodec_ctx
*ctx
= m2m_priv
;
1336 mtk_v4l2_debug(3, "[%d]", ctx
->id
);
1338 if (ctx
->state
== MTK_STATE_ABORT
)
1341 if ((ctx
->last_decoded_picinfo
.pic_w
!= ctx
->picinfo
.pic_w
) ||
1342 (ctx
->last_decoded_picinfo
.pic_h
!= ctx
->picinfo
.pic_h
))
1345 if (ctx
->state
!= MTK_STATE_HEADER
)
1351 static void m2mops_vdec_job_abort(void *priv
)
1353 struct mtk_vcodec_ctx
*ctx
= priv
;
1355 ctx
->state
= MTK_STATE_ABORT
;
1358 static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl
*ctrl
)
1360 struct mtk_vcodec_ctx
*ctx
= ctrl_to_ctx(ctrl
);
1364 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
:
1365 if (ctx
->state
>= MTK_STATE_HEADER
) {
1366 ctrl
->val
= ctx
->dpb_size
;
1368 mtk_v4l2_debug(0, "Seqinfo not ready");
1378 static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops
= {
1379 .g_volatile_ctrl
= mtk_vdec_g_v_ctrl
,
1382 int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx
*ctx
)
1384 struct v4l2_ctrl
*ctrl
;
1386 v4l2_ctrl_handler_init(&ctx
->ctrl_hdl
, 1);
1388 ctrl
= v4l2_ctrl_new_std(&ctx
->ctrl_hdl
,
1389 &mtk_vcodec_dec_ctrl_ops
,
1390 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
,
1392 ctrl
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
1393 v4l2_ctrl_new_std_menu(&ctx
->ctrl_hdl
,
1394 &mtk_vcodec_dec_ctrl_ops
,
1395 V4L2_CID_MPEG_VIDEO_VP9_PROFILE
,
1396 V4L2_MPEG_VIDEO_VP9_PROFILE_0
,
1397 0, V4L2_MPEG_VIDEO_VP9_PROFILE_0
);
1399 if (ctx
->ctrl_hdl
.error
) {
1400 mtk_v4l2_err("Adding control failed %d",
1401 ctx
->ctrl_hdl
.error
);
1402 return ctx
->ctrl_hdl
.error
;
1405 v4l2_ctrl_handler_setup(&ctx
->ctrl_hdl
);
1409 const struct v4l2_m2m_ops mtk_vdec_m2m_ops
= {
1410 .device_run
= m2mops_vdec_device_run
,
1411 .job_ready
= m2mops_vdec_job_ready
,
1412 .job_abort
= m2mops_vdec_job_abort
,
1415 static const struct vb2_ops mtk_vdec_vb2_ops
= {
1416 .queue_setup
= vb2ops_vdec_queue_setup
,
1417 .buf_prepare
= vb2ops_vdec_buf_prepare
,
1418 .buf_queue
= vb2ops_vdec_buf_queue
,
1419 .wait_prepare
= vb2_ops_wait_prepare
,
1420 .wait_finish
= vb2_ops_wait_finish
,
1421 .buf_init
= vb2ops_vdec_buf_init
,
1422 .buf_finish
= vb2ops_vdec_buf_finish
,
1423 .start_streaming
= vb2ops_vdec_start_streaming
,
1424 .stop_streaming
= vb2ops_vdec_stop_streaming
,
1427 const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops
= {
1428 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
1429 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
1430 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
1431 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
1432 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
1434 .vidioc_qbuf
= vidioc_vdec_qbuf
,
1435 .vidioc_dqbuf
= vidioc_vdec_dqbuf
,
1437 .vidioc_try_fmt_vid_cap_mplane
= vidioc_try_fmt_vid_cap_mplane
,
1438 .vidioc_try_fmt_vid_out_mplane
= vidioc_try_fmt_vid_out_mplane
,
1440 .vidioc_s_fmt_vid_cap_mplane
= vidioc_vdec_s_fmt
,
1441 .vidioc_s_fmt_vid_out_mplane
= vidioc_vdec_s_fmt
,
1442 .vidioc_g_fmt_vid_cap_mplane
= vidioc_vdec_g_fmt
,
1443 .vidioc_g_fmt_vid_out_mplane
= vidioc_vdec_g_fmt
,
1445 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
1447 .vidioc_enum_fmt_vid_cap_mplane
= vidioc_vdec_enum_fmt_vid_cap_mplane
,
1448 .vidioc_enum_fmt_vid_out_mplane
= vidioc_vdec_enum_fmt_vid_out_mplane
,
1449 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
1451 .vidioc_querycap
= vidioc_vdec_querycap
,
1452 .vidioc_subscribe_event
= vidioc_vdec_subscribe_evt
,
1453 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1454 .vidioc_g_selection
= vidioc_vdec_g_selection
,
1455 .vidioc_s_selection
= vidioc_vdec_s_selection
,
1457 .vidioc_decoder_cmd
= vidioc_decoder_cmd
,
1458 .vidioc_try_decoder_cmd
= vidioc_try_decoder_cmd
,
1461 int mtk_vcodec_dec_queue_init(void *priv
, struct vb2_queue
*src_vq
,
1462 struct vb2_queue
*dst_vq
)
1464 struct mtk_vcodec_ctx
*ctx
= priv
;
1467 mtk_v4l2_debug(3, "[%d]", ctx
->id
);
1469 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
1470 src_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1471 src_vq
->drv_priv
= ctx
;
1472 src_vq
->buf_struct_size
= sizeof(struct mtk_video_dec_buf
);
1473 src_vq
->ops
= &mtk_vdec_vb2_ops
;
1474 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
1475 src_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1476 src_vq
->lock
= &ctx
->dev
->dev_mutex
;
1477 src_vq
->dev
= &ctx
->dev
->plat_dev
->dev
;
1479 ret
= vb2_queue_init(src_vq
);
1481 mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
1484 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1485 dst_vq
->io_modes
= VB2_DMABUF
| VB2_MMAP
;
1486 dst_vq
->drv_priv
= ctx
;
1487 dst_vq
->buf_struct_size
= sizeof(struct mtk_video_dec_buf
);
1488 dst_vq
->ops
= &mtk_vdec_vb2_ops
;
1489 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1490 dst_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1491 dst_vq
->lock
= &ctx
->dev
->dev_mutex
;
1492 dst_vq
->dev
= &ctx
->dev
->plat_dev
->dev
;
1494 ret
= vb2_queue_init(dst_vq
);
1496 vb2_queue_release(src_vq
);
1497 mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");