1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
6 */
7
8 #include <media/v4l2-event.h>
9 #include <media/v4l2-mem2mem.h>
10 #include <media/videobuf2-dma-contig.h>
11
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"
18
19 #define OUT_FMT_IDX 0
20 #define CAP_FMT_IDX 3
21
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
26
27 static const struct mtk_video_fmt mtk_video_formats[] = {
28 {
29 .fourcc = V4L2_PIX_FMT_H264,
30 .type = MTK_FMT_DEC,
31 .num_planes = 1,
32 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
33 },
34 {
35 .fourcc = V4L2_PIX_FMT_VP8,
36 .type = MTK_FMT_DEC,
37 .num_planes = 1,
38 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
39 },
40 {
41 .fourcc = V4L2_PIX_FMT_VP9,
42 .type = MTK_FMT_DEC,
43 .num_planes = 1,
44 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
45 },
46 {
47 .fourcc = V4L2_PIX_FMT_MT21C,
48 .type = MTK_FMT_FRAME,
49 .num_planes = 2,
50 },
51 };
52
53 static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
54 {
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 },
58 },
59 {
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 },
63 },
64 {
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 },
68 },
69 };
70
71 #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
72 #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
73
mtk_vdec_find_format(struct v4l2_format * f)74 static const struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
75 {
76 const struct mtk_video_fmt *fmt;
77 unsigned int k;
78
79 for (k = 0; k < NUM_FORMATS; k++) {
80 fmt = &mtk_video_formats[k];
81 if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
82 return fmt;
83 }
84
85 return NULL;
86 }
87
mtk_vdec_get_q_data(struct mtk_vcodec_ctx * ctx,enum v4l2_buf_type type)88 static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
89 enum v4l2_buf_type type)
90 {
91 if (V4L2_TYPE_IS_OUTPUT(type))
92 return &ctx->q_data[MTK_Q_DATA_SRC];
93
94 return &ctx->q_data[MTK_Q_DATA_DST];
95 }
96
97 /*
98 * This function tries to clean all display buffers, the buffers will return
99 * in display order.
100 * Note the buffers returned from codec driver may still be in driver's
101 * reference list.
102 */
get_display_buffer(struct mtk_vcodec_ctx * ctx)103 static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
104 {
105 struct vdec_fb *disp_frame_buffer = NULL;
106 struct mtk_video_dec_buf *dstbuf;
107
108 mtk_v4l2_debug(3, "[%d]", ctx->id);
109 if (vdec_if_get_param(ctx,
110 GET_PARAM_DISP_FRAME_BUFFER,
111 &disp_frame_buffer)) {
112 mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
113 ctx->id);
114 return NULL;
115 }
116
117 if (disp_frame_buffer == NULL) {
118 mtk_v4l2_debug(3, "No display frame buffer");
119 return NULL;
120 }
121
122 dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
123 frame_buffer);
124 mutex_lock(&ctx->lock);
125 if (dstbuf->used) {
126 vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
127 ctx->picinfo.fb_sz[0]);
128 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
129 vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
130 ctx->picinfo.fb_sz[1]);
131
132 mtk_v4l2_debug(2,
133 "[%d]status=%x queue id=%d to done_list %d",
134 ctx->id, disp_frame_buffer->status,
135 dstbuf->vb.vb2_buf.index,
136 dstbuf->queued_in_vb2);
137
138 v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
139 ctx->decoded_frame_cnt++;
140 }
141 mutex_unlock(&ctx->lock);
142 return &dstbuf->vb.vb2_buf;
143 }
144
145 /*
146 * This function tries to clean all capture buffers that are not used as
147 * reference buffers by codec driver any more
148 * In this case, we need re-queue buffer to vb2 buffer if user space
149 * already returns this buffer to v4l2 or this buffer is just the output of
150 * previous sps/pps/resolution change decode, or do nothing if user
151 * space still owns this buffer
152 */
get_free_buffer(struct mtk_vcodec_ctx * ctx)153 static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
154 {
155 struct mtk_video_dec_buf *dstbuf;
156 struct vdec_fb *free_frame_buffer = NULL;
157
158 if (vdec_if_get_param(ctx,
159 GET_PARAM_FREE_FRAME_BUFFER,
160 &free_frame_buffer)) {
161 mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->id);
162 return NULL;
163 }
164 if (free_frame_buffer == NULL) {
165 mtk_v4l2_debug(3, " No free frame buffer");
166 return NULL;
167 }
168
169 mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
170 ctx->id, free_frame_buffer);
171
172 dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf,
173 frame_buffer);
174
175 mutex_lock(&ctx->lock);
176 if (dstbuf->used) {
177 if ((dstbuf->queued_in_vb2) &&
178 (dstbuf->queued_in_v4l2) &&
179 (free_frame_buffer->status == FB_ST_FREE)) {
180 /*
181 * After decode sps/pps or non-display buffer, we don't
182 * need to return capture buffer to user space, but
183 * just re-queue this capture buffer to vb2 queue.
184 * This reduce overheads that dq/q unused capture
185 * buffer. In this case, queued_in_vb2 = true.
186 */
187 mtk_v4l2_debug(2,
188 "[%d]status=%x queue id=%d to rdy_queue %d",
189 ctx->id, free_frame_buffer->status,
190 dstbuf->vb.vb2_buf.index,
191 dstbuf->queued_in_vb2);
192 v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
193 } else if ((dstbuf->queued_in_vb2 == false) &&
194 (dstbuf->queued_in_v4l2 == true)) {
195 /*
196 * If buffer in v4l2 driver but not in vb2 queue yet,
197 * and we get this buffer from free_list, it means
198 * that codec driver do not use this buffer as
199 * reference buffer anymore. We should q buffer to vb2
200 * queue, so later work thread could get this buffer
201 * for decode. In this case, queued_in_vb2 = false
202 * means this buffer is not from previous decode
203 * output.
204 */
205 mtk_v4l2_debug(2,
206 "[%d]status=%x queue id=%d to rdy_queue",
207 ctx->id, free_frame_buffer->status,
208 dstbuf->vb.vb2_buf.index);
209 v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
210 dstbuf->queued_in_vb2 = true;
211 } else {
212 /*
213 * Codec driver do not need to reference this capture
214 * buffer and this buffer is not in v4l2 driver.
215 * Then we don't need to do any thing, just add log when
216 * we need to debug buffer flow.
217 * When this buffer q from user space, it could
218 * directly q to vb2 buffer
219 */
220 mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
221 ctx->id, free_frame_buffer->status,
222 dstbuf->vb.vb2_buf.index,
223 dstbuf->queued_in_vb2,
224 dstbuf->queued_in_v4l2);
225 }
226 dstbuf->used = false;
227 }
228 mutex_unlock(&ctx->lock);
229 return &dstbuf->vb.vb2_buf;
230 }
231
clean_display_buffer(struct mtk_vcodec_ctx * ctx)232 static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
233 {
234 struct vb2_buffer *framptr;
235
236 do {
237 framptr = get_display_buffer(ctx);
238 } while (framptr);
239 }
240
clean_free_buffer(struct mtk_vcodec_ctx * ctx)241 static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
242 {
243 struct vb2_buffer *framptr;
244
245 do {
246 framptr = get_free_buffer(ctx);
247 } while (framptr);
248 }
249
mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx * ctx)250 static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
251 {
252 static const struct v4l2_event ev_src_ch = {
253 .type = V4L2_EVENT_SOURCE_CHANGE,
254 .u.src_change.changes =
255 V4L2_EVENT_SRC_CH_RESOLUTION,
256 };
257
258 mtk_v4l2_debug(1, "[%d]", ctx->id);
259 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
260 }
261
mtk_vdec_flush_decoder(struct mtk_vcodec_ctx * ctx)262 static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
263 {
264 bool res_chg;
265 int ret = 0;
266
267 ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
268 if (ret)
269 mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
270
271 clean_display_buffer(ctx);
272 clean_free_buffer(ctx);
273 }
274
mtk_vdec_update_fmt(struct mtk_vcodec_ctx * ctx,unsigned int pixelformat)275 static void mtk_vdec_update_fmt(struct mtk_vcodec_ctx *ctx,
276 unsigned int pixelformat)
277 {
278 const struct mtk_video_fmt *fmt;
279 struct mtk_q_data *dst_q_data;
280 unsigned int k;
281
282 dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
283 for (k = 0; k < NUM_FORMATS; k++) {
284 fmt = &mtk_video_formats[k];
285 if (fmt->fourcc == pixelformat) {
286 mtk_v4l2_debug(1, "Update cap fourcc(%d -> %d)",
287 dst_q_data->fmt.fourcc, pixelformat);
288 dst_q_data->fmt = fmt;
289 return;
290 }
291 }
292
293 mtk_v4l2_err("Cannot get fourcc(%d), using init value", pixelformat);
294 }
295
mtk_vdec_pic_info_update(struct mtk_vcodec_ctx * ctx)296 static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
297 {
298 unsigned int dpbsize = 0;
299 int ret;
300
301 if (vdec_if_get_param(ctx,
302 GET_PARAM_PIC_INFO,
303 &ctx->last_decoded_picinfo)) {
304 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
305 ctx->id);
306 return -EINVAL;
307 }
308
309 if (ctx->last_decoded_picinfo.pic_w == 0 ||
310 ctx->last_decoded_picinfo.pic_h == 0 ||
311 ctx->last_decoded_picinfo.buf_w == 0 ||
312 ctx->last_decoded_picinfo.buf_h == 0) {
313 mtk_v4l2_err("Cannot get correct pic info");
314 return -EINVAL;
315 }
316
317 if (ctx->last_decoded_picinfo.cap_fourcc != ctx->picinfo.cap_fourcc &&
318 ctx->picinfo.cap_fourcc != 0)
319 mtk_vdec_update_fmt(ctx, ctx->picinfo.cap_fourcc);
320
321 if ((ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w) ||
322 (ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h))
323 return 0;
324
325 mtk_v4l2_debug(1,
326 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
327 ctx->id, ctx->last_decoded_picinfo.pic_w,
328 ctx->last_decoded_picinfo.pic_h,
329 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
330 ctx->last_decoded_picinfo.buf_w,
331 ctx->last_decoded_picinfo.buf_h);
332
333 ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
334 if (dpbsize == 0)
335 mtk_v4l2_err("Incorrect dpb size, ret=%d", ret);
336
337 ctx->dpb_size = dpbsize;
338
339 return ret;
340 }
341
mtk_vdec_worker(struct work_struct * work)342 static void mtk_vdec_worker(struct work_struct *work)
343 {
344 struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
345 decode_work);
346 struct mtk_vcodec_dev *dev = ctx->dev;
347 struct vb2_v4l2_buffer *src_buf, *dst_buf;
348 struct mtk_vcodec_mem buf;
349 struct vdec_fb *pfb;
350 bool res_chg = false;
351 int ret;
352 struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
353
354 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
355 if (src_buf == NULL) {
356 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
357 mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id);
358 return;
359 }
360
361 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
362 if (dst_buf == NULL) {
363 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
364 mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
365 return;
366 }
367
368 src_buf_info = container_of(src_buf, struct mtk_video_dec_buf, vb);
369 dst_buf_info = container_of(dst_buf, struct mtk_video_dec_buf, vb);
370
371 pfb = &dst_buf_info->frame_buffer;
372 pfb->base_y.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
373 pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
374 pfb->base_y.size = ctx->picinfo.fb_sz[0];
375
376 pfb->base_c.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 1);
377 pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 1);
378 pfb->base_c.size = ctx->picinfo.fb_sz[1];
379 pfb->status = 0;
380 mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
381
382 mtk_v4l2_debug(3,
383 "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
384 dst_buf->vb2_buf.index, pfb,
385 pfb->base_y.va, &pfb->base_y.dma_addr,
386 &pfb->base_c.dma_addr, pfb->base_y.size);
387
388 if (src_buf_info->lastframe) {
389 mtk_v4l2_debug(1, "Got empty flush input buffer.");
390 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
391
392 /* update dst buf status */
393 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
394 mutex_lock(&ctx->lock);
395 dst_buf_info->used = false;
396 mutex_unlock(&ctx->lock);
397
398 vdec_if_decode(ctx, NULL, NULL, &res_chg);
399 clean_display_buffer(ctx);
400 vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
401 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
402 vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
403 dst_buf->flags |= V4L2_BUF_FLAG_LAST;
404 v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
405 clean_free_buffer(ctx);
406 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
407 return;
408 }
409 buf.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
410 buf.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
411 buf.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
412 if (!buf.va) {
413 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
414 mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
415 ctx->id, src_buf->vb2_buf.index);
416 return;
417 }
418 mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
419 ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
420 dst_buf_info->vb.vb2_buf.timestamp
421 = src_buf_info->vb.vb2_buf.timestamp;
422 dst_buf_info->vb.timecode
423 = src_buf_info->vb.timecode;
424 mutex_lock(&ctx->lock);
425 dst_buf_info->used = true;
426 mutex_unlock(&ctx->lock);
427 src_buf_info->used = true;
428
429 ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
430
431 if (ret) {
432 mtk_v4l2_err(
433 " <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
434 ctx->id,
435 src_buf->vb2_buf.index,
436 buf.size,
437 src_buf_info->vb.vb2_buf.timestamp,
438 dst_buf->vb2_buf.index,
439 ret, res_chg);
440 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
441 if (ret == -EIO) {
442 mutex_lock(&ctx->lock);
443 src_buf_info->error = true;
444 mutex_unlock(&ctx->lock);
445 }
446 v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_ERROR);
447 } else if (res_chg == false) {
448 /*
449 * we only return src buffer with VB2_BUF_STATE_DONE
450 * when decode success without resolution change
451 */
452 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
453 v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
454 }
455
456 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
457 clean_display_buffer(ctx);
458 clean_free_buffer(ctx);
459
460 if (!ret && res_chg) {
461 mtk_vdec_pic_info_update(ctx);
462 /*
463 * On encountering a resolution change in the stream.
464 * The driver must first process and decode all
465 * remaining buffers from before the resolution change
466 * point, so call flush decode here
467 */
468 mtk_vdec_flush_decoder(ctx);
469 /*
470 * After all buffers containing decoded frames from
471 * before the resolution change point ready to be
472 * dequeued on the CAPTURE queue, the driver sends a
473 * V4L2_EVENT_SOURCE_CHANGE event for source change
474 * type V4L2_EVENT_SRC_CH_RESOLUTION
475 */
476 mtk_vdec_queue_res_chg_event(ctx);
477 }
478 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
479 }
480
vidioc_try_decoder_cmd(struct file * file,void * priv,struct v4l2_decoder_cmd * cmd)481 static int vidioc_try_decoder_cmd(struct file *file, void *priv,
482 struct v4l2_decoder_cmd *cmd)
483 {
484 switch (cmd->cmd) {
485 case V4L2_DEC_CMD_STOP:
486 case V4L2_DEC_CMD_START:
487 if (cmd->flags != 0) {
488 mtk_v4l2_err("cmd->flags=%u", cmd->flags);
489 return -EINVAL;
490 }
491 break;
492 default:
493 return -EINVAL;
494 }
495 return 0;
496 }
497
498
vidioc_decoder_cmd(struct file * file,void * priv,struct v4l2_decoder_cmd * cmd)499 static int vidioc_decoder_cmd(struct file *file, void *priv,
500 struct v4l2_decoder_cmd *cmd)
501 {
502 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
503 struct vb2_queue *src_vq, *dst_vq;
504 int ret;
505
506 ret = vidioc_try_decoder_cmd(file, priv, cmd);
507 if (ret)
508 return ret;
509
510 mtk_v4l2_debug(1, "decoder cmd=%u", cmd->cmd);
511 dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
512 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
513 switch (cmd->cmd) {
514 case V4L2_DEC_CMD_STOP:
515 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
516 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
517 if (!vb2_is_streaming(src_vq)) {
518 mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
519 return 0;
520 }
521 if (!vb2_is_streaming(dst_vq)) {
522 mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
523 return 0;
524 }
525 v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf->vb);
526 v4l2_m2m_try_schedule(ctx->m2m_ctx);
527 break;
528
529 case V4L2_DEC_CMD_START:
530 vb2_clear_last_buffer_dequeued(dst_vq);
531 break;
532
533 default:
534 return -EINVAL;
535 }
536
537 return 0;
538 }
539
mtk_vdec_unlock(struct mtk_vcodec_ctx * ctx)540 void mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
541 {
542 mutex_unlock(&ctx->dev->dec_mutex);
543 }
544
mtk_vdec_lock(struct mtk_vcodec_ctx * ctx)545 void mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
546 {
547 mutex_lock(&ctx->dev->dec_mutex);
548 }
549
mtk_vcodec_dec_release(struct mtk_vcodec_ctx * ctx)550 void mtk_vcodec_dec_release(struct mtk_vcodec_ctx *ctx)
551 {
552 vdec_if_deinit(ctx);
553 ctx->state = MTK_STATE_FREE;
554 }
555
mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx * ctx)556 void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
557 {
558 struct mtk_q_data *q_data;
559
560 ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
561 ctx->fh.m2m_ctx = ctx->m2m_ctx;
562 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
563 INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
564 ctx->colorspace = V4L2_COLORSPACE_REC709;
565 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
566 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
567 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
568
569 q_data = &ctx->q_data[MTK_Q_DATA_SRC];
570 memset(q_data, 0, sizeof(struct mtk_q_data));
571 q_data->visible_width = DFT_CFG_WIDTH;
572 q_data->visible_height = DFT_CFG_HEIGHT;
573 q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
574 q_data->field = V4L2_FIELD_NONE;
575
576 q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
577 q_data->bytesperline[0] = 0;
578
579 q_data = &ctx->q_data[MTK_Q_DATA_DST];
580 memset(q_data, 0, sizeof(struct mtk_q_data));
581 q_data->visible_width = DFT_CFG_WIDTH;
582 q_data->visible_height = DFT_CFG_HEIGHT;
583 q_data->coded_width = DFT_CFG_WIDTH;
584 q_data->coded_height = DFT_CFG_HEIGHT;
585 q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
586 q_data->field = V4L2_FIELD_NONE;
587
588 v4l_bound_align_image(&q_data->coded_width,
589 MTK_VDEC_MIN_W,
590 MTK_VDEC_MAX_W, 4,
591 &q_data->coded_height,
592 MTK_VDEC_MIN_H,
593 MTK_VDEC_MAX_H, 5, 6);
594
595 q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
596 q_data->bytesperline[0] = q_data->coded_width;
597 q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
598 q_data->bytesperline[1] = q_data->coded_width;
599 }
600
vidioc_vdec_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)601 static int vidioc_vdec_qbuf(struct file *file, void *priv,
602 struct v4l2_buffer *buf)
603 {
604 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
605
606 if (ctx->state == MTK_STATE_ABORT) {
607 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
608 ctx->id);
609 return -EIO;
610 }
611
612 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
613 }
614
vidioc_vdec_dqbuf(struct file * file,void * priv,struct v4l2_buffer * buf)615 static int vidioc_vdec_dqbuf(struct file *file, void *priv,
616 struct v4l2_buffer *buf)
617 {
618 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
619
620 if (ctx->state == MTK_STATE_ABORT) {
621 mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
622 ctx->id);
623 return -EIO;
624 }
625
626 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
627 }
628
vidioc_vdec_querycap(struct file * file,void * priv,struct v4l2_capability * cap)629 static int vidioc_vdec_querycap(struct file *file, void *priv,
630 struct v4l2_capability *cap)
631 {
632 strscpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
633 strscpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
634 strscpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
635
636 return 0;
637 }
638
vidioc_vdec_subscribe_evt(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)639 static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
640 const struct v4l2_event_subscription *sub)
641 {
642 switch (sub->type) {
643 case V4L2_EVENT_EOS:
644 return v4l2_event_subscribe(fh, sub, 2, NULL);
645 case V4L2_EVENT_SOURCE_CHANGE:
646 return v4l2_src_change_event_subscribe(fh, sub);
647 default:
648 return v4l2_ctrl_subscribe_event(fh, sub);
649 }
650 }
651
vidioc_try_fmt(struct v4l2_format * f,const struct mtk_video_fmt * fmt)652 static int vidioc_try_fmt(struct v4l2_format *f,
653 const struct mtk_video_fmt *fmt)
654 {
655 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
656 int i;
657
658 pix_fmt_mp->field = V4L2_FIELD_NONE;
659
660 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
661 pix_fmt_mp->num_planes = 1;
662 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
663 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
664 int tmp_w, tmp_h;
665
666 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
667 MTK_VDEC_MIN_H,
668 MTK_VDEC_MAX_H);
669 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
670 MTK_VDEC_MIN_W,
671 MTK_VDEC_MAX_W);
672
673 /*
674 * Find next closer width align 64, heign align 64, size align
675 * 64 rectangle
676 * Note: This only get default value, the real HW needed value
677 * only available when ctx in MTK_STATE_HEADER state
678 */
679 tmp_w = pix_fmt_mp->width;
680 tmp_h = pix_fmt_mp->height;
681 v4l_bound_align_image(&pix_fmt_mp->width,
682 MTK_VDEC_MIN_W,
683 MTK_VDEC_MAX_W, 6,
684 &pix_fmt_mp->height,
685 MTK_VDEC_MIN_H,
686 MTK_VDEC_MAX_H, 6, 9);
687
688 if (pix_fmt_mp->width < tmp_w &&
689 (pix_fmt_mp->width + 64) <= MTK_VDEC_MAX_W)
690 pix_fmt_mp->width += 64;
691 if (pix_fmt_mp->height < tmp_h &&
692 (pix_fmt_mp->height + 64) <= MTK_VDEC_MAX_H)
693 pix_fmt_mp->height += 64;
694
695 mtk_v4l2_debug(0,
696 "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
697 tmp_w, tmp_h, pix_fmt_mp->width,
698 pix_fmt_mp->height,
699 pix_fmt_mp->width * pix_fmt_mp->height);
700
701 pix_fmt_mp->num_planes = fmt->num_planes;
702 pix_fmt_mp->plane_fmt[0].sizeimage =
703 pix_fmt_mp->width * pix_fmt_mp->height;
704 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
705
706 if (pix_fmt_mp->num_planes == 2) {
707 pix_fmt_mp->plane_fmt[1].sizeimage =
708 (pix_fmt_mp->width * pix_fmt_mp->height) / 2;
709 pix_fmt_mp->plane_fmt[1].bytesperline =
710 pix_fmt_mp->width;
711 }
712 }
713
714 for (i = 0; i < pix_fmt_mp->num_planes; i++)
715 memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
716 sizeof(pix_fmt_mp->plane_fmt[0].reserved));
717
718 pix_fmt_mp->flags = 0;
719 memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved));
720 return 0;
721 }
722
vidioc_try_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)723 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
724 struct v4l2_format *f)
725 {
726 const struct mtk_video_fmt *fmt;
727
728 fmt = mtk_vdec_find_format(f);
729 if (!fmt) {
730 f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
731 fmt = mtk_vdec_find_format(f);
732 }
733
734 return vidioc_try_fmt(f, fmt);
735 }
736
vidioc_try_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_format * f)737 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
738 struct v4l2_format *f)
739 {
740 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
741 const struct mtk_video_fmt *fmt;
742
743 fmt = mtk_vdec_find_format(f);
744 if (!fmt) {
745 f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
746 fmt = mtk_vdec_find_format(f);
747 }
748
749 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
750 mtk_v4l2_err("sizeimage of output format must be given");
751 return -EINVAL;
752 }
753
754 return vidioc_try_fmt(f, fmt);
755 }
756
vidioc_vdec_g_selection(struct file * file,void * priv,struct v4l2_selection * s)757 static int vidioc_vdec_g_selection(struct file *file, void *priv,
758 struct v4l2_selection *s)
759 {
760 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
761 struct mtk_q_data *q_data;
762
763 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
764 return -EINVAL;
765
766 q_data = &ctx->q_data[MTK_Q_DATA_DST];
767
768 switch (s->target) {
769 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
770 s->r.left = 0;
771 s->r.top = 0;
772 s->r.width = ctx->picinfo.pic_w;
773 s->r.height = ctx->picinfo.pic_h;
774 break;
775 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
776 s->r.left = 0;
777 s->r.top = 0;
778 s->r.width = ctx->picinfo.buf_w;
779 s->r.height = ctx->picinfo.buf_h;
780 break;
781 case V4L2_SEL_TGT_COMPOSE:
782 if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
783 /* set to default value if header info not ready yet*/
784 s->r.left = 0;
785 s->r.top = 0;
786 s->r.width = q_data->visible_width;
787 s->r.height = q_data->visible_height;
788 }
789 break;
790 default:
791 return -EINVAL;
792 }
793
794 if (ctx->state < MTK_STATE_HEADER) {
795 /* set to default value if header info not ready yet*/
796 s->r.left = 0;
797 s->r.top = 0;
798 s->r.width = q_data->visible_width;
799 s->r.height = q_data->visible_height;
800 return 0;
801 }
802
803 return 0;
804 }
805
vidioc_vdec_s_selection(struct file * file,void * priv,struct v4l2_selection * s)806 static int vidioc_vdec_s_selection(struct file *file, void *priv,
807 struct v4l2_selection *s)
808 {
809 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
810
811 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
812 return -EINVAL;
813
814 switch (s->target) {
815 case V4L2_SEL_TGT_COMPOSE:
816 s->r.left = 0;
817 s->r.top = 0;
818 s->r.width = ctx->picinfo.pic_w;
819 s->r.height = ctx->picinfo.pic_h;
820 break;
821 default:
822 return -EINVAL;
823 }
824
825 return 0;
826 }
827
vidioc_vdec_s_fmt(struct file * file,void * priv,struct v4l2_format * f)828 static int vidioc_vdec_s_fmt(struct file *file, void *priv,
829 struct v4l2_format *f)
830 {
831 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
832 struct v4l2_pix_format_mplane *pix_mp;
833 struct mtk_q_data *q_data;
834 int ret = 0;
835 const struct mtk_video_fmt *fmt;
836
837 mtk_v4l2_debug(3, "[%d]", ctx->id);
838
839 q_data = mtk_vdec_get_q_data(ctx, f->type);
840 if (!q_data)
841 return -EINVAL;
842
843 pix_mp = &f->fmt.pix_mp;
844 if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
845 vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
846 mtk_v4l2_err("out_q_ctx buffers already requested");
847 ret = -EBUSY;
848 }
849
850 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
851 vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
852 mtk_v4l2_err("cap_q_ctx buffers already requested");
853 ret = -EBUSY;
854 }
855
856 fmt = mtk_vdec_find_format(f);
857 if (fmt == NULL) {
858 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
859 f->fmt.pix.pixelformat =
860 mtk_video_formats[OUT_FMT_IDX].fourcc;
861 fmt = mtk_vdec_find_format(f);
862 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
863 f->fmt.pix.pixelformat =
864 mtk_video_formats[CAP_FMT_IDX].fourcc;
865 fmt = mtk_vdec_find_format(f);
866 }
867 }
868
869 q_data->fmt = fmt;
870 vidioc_try_fmt(f, q_data->fmt);
871 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
872 q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
873 q_data->coded_width = pix_mp->width;
874 q_data->coded_height = pix_mp->height;
875
876 ctx->colorspace = f->fmt.pix_mp.colorspace;
877 ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
878 ctx->quantization = f->fmt.pix_mp.quantization;
879 ctx->xfer_func = f->fmt.pix_mp.xfer_func;
880
881 if (ctx->state == MTK_STATE_FREE) {
882 ret = vdec_if_init(ctx, q_data->fmt->fourcc);
883 if (ret) {
884 mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
885 ctx->id, ret);
886 return -EINVAL;
887 }
888 ctx->state = MTK_STATE_INIT;
889 }
890 }
891
892 return 0;
893 }
894
vidioc_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)895 static int vidioc_enum_framesizes(struct file *file, void *priv,
896 struct v4l2_frmsizeenum *fsize)
897 {
898 int i = 0;
899 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
900
901 if (fsize->index != 0)
902 return -EINVAL;
903
904 for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
905 if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
906 continue;
907
908 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
909 fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
910 if (!(ctx->dev->dec_capability &
911 VCODEC_CAPABILITY_4K_DISABLED)) {
912 mtk_v4l2_debug(3, "4K is enabled");
913 fsize->stepwise.max_width =
914 VCODEC_DEC_4K_CODED_WIDTH;
915 fsize->stepwise.max_height =
916 VCODEC_DEC_4K_CODED_HEIGHT;
917 }
918 mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
919 ctx->dev->dec_capability,
920 fsize->stepwise.min_width,
921 fsize->stepwise.max_width,
922 fsize->stepwise.step_width,
923 fsize->stepwise.min_height,
924 fsize->stepwise.max_height,
925 fsize->stepwise.step_height);
926 return 0;
927 }
928
929 return -EINVAL;
930 }
931
vidioc_enum_fmt(struct v4l2_fmtdesc * f,bool output_queue)932 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
933 {
934 const struct mtk_video_fmt *fmt;
935 int i, j = 0;
936
937 for (i = 0; i < NUM_FORMATS; i++) {
938 if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
939 continue;
940 if (!output_queue &&
941 (mtk_video_formats[i].type != MTK_FMT_FRAME))
942 continue;
943
944 if (j == f->index)
945 break;
946 ++j;
947 }
948
949 if (i == NUM_FORMATS)
950 return -EINVAL;
951
952 fmt = &mtk_video_formats[i];
953 f->pixelformat = fmt->fourcc;
954 f->flags = fmt->flags;
955
956 return 0;
957 }
958
vidioc_vdec_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)959 static int vidioc_vdec_enum_fmt_vid_cap(struct file *file, void *priv,
960 struct v4l2_fmtdesc *f)
961 {
962 return vidioc_enum_fmt(f, false);
963 }
964
vidioc_vdec_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)965 static int vidioc_vdec_enum_fmt_vid_out(struct file *file, void *priv,
966 struct v4l2_fmtdesc *f)
967 {
968 return vidioc_enum_fmt(f, true);
969 }
970
vidioc_vdec_g_fmt(struct file * file,void * priv,struct v4l2_format * f)971 static int vidioc_vdec_g_fmt(struct file *file, void *priv,
972 struct v4l2_format *f)
973 {
974 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
975 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
976 struct vb2_queue *vq;
977 struct mtk_q_data *q_data;
978
979 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
980 if (!vq) {
981 mtk_v4l2_err("no vb2 queue for type=%d", f->type);
982 return -EINVAL;
983 }
984
985 q_data = mtk_vdec_get_q_data(ctx, f->type);
986
987 pix_mp->field = V4L2_FIELD_NONE;
988 pix_mp->colorspace = ctx->colorspace;
989 pix_mp->ycbcr_enc = ctx->ycbcr_enc;
990 pix_mp->quantization = ctx->quantization;
991 pix_mp->xfer_func = ctx->xfer_func;
992
993 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
994 (ctx->state >= MTK_STATE_HEADER)) {
995 /* Until STREAMOFF is called on the CAPTURE queue
996 * (acknowledging the event), the driver operates as if
997 * the resolution hasn't changed yet.
998 * So we just return picinfo yet, and update picinfo in
999 * stop_streaming hook function
1000 */
1001 q_data->sizeimage[0] = ctx->picinfo.fb_sz[0];
1002 q_data->sizeimage[1] = ctx->picinfo.fb_sz[1];
1003 q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
1004 q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
1005 q_data->coded_width = ctx->picinfo.buf_w;
1006 q_data->coded_height = ctx->picinfo.buf_h;
1007 ctx->last_decoded_picinfo.cap_fourcc = q_data->fmt->fourcc;
1008
1009 /*
1010 * Width and height are set to the dimensions
1011 * of the movie, the buffer is bigger and
1012 * further processing stages should crop to this
1013 * rectangle.
1014 */
1015 pix_mp->width = q_data->coded_width;
1016 pix_mp->height = q_data->coded_height;
1017
1018 /*
1019 * Set pixelformat to the format in which mt vcodec
1020 * outputs the decoded frame
1021 */
1022 pix_mp->num_planes = q_data->fmt->num_planes;
1023 pix_mp->pixelformat = q_data->fmt->fourcc;
1024 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1025 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1026 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1027 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1028
1029 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1030 /*
1031 * This is run on OUTPUT
1032 * The buffer contains compressed image
1033 * so width and height have no meaning.
1034 * Assign value here to pass v4l2-compliance test
1035 */
1036 pix_mp->width = q_data->visible_width;
1037 pix_mp->height = q_data->visible_height;
1038 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1039 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1040 pix_mp->pixelformat = q_data->fmt->fourcc;
1041 pix_mp->num_planes = q_data->fmt->num_planes;
1042 } else {
1043 pix_mp->width = q_data->coded_width;
1044 pix_mp->height = q_data->coded_height;
1045 pix_mp->num_planes = q_data->fmt->num_planes;
1046 pix_mp->pixelformat = q_data->fmt->fourcc;
1047 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1048 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1049 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1050 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1051
1052 mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
1053 ctx->id, f->type, ctx->state);
1054 }
1055
1056 return 0;
1057 }
1058
vb2ops_vdec_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])1059 static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
1060 unsigned int *nbuffers,
1061 unsigned int *nplanes,
1062 unsigned int sizes[],
1063 struct device *alloc_devs[])
1064 {
1065 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
1066 struct mtk_q_data *q_data;
1067 unsigned int i;
1068
1069 q_data = mtk_vdec_get_q_data(ctx, vq->type);
1070
1071 if (q_data == NULL) {
1072 mtk_v4l2_err("vq->type=%d err\n", vq->type);
1073 return -EINVAL;
1074 }
1075
1076 if (*nplanes) {
1077 for (i = 0; i < *nplanes; i++) {
1078 if (sizes[i] < q_data->sizeimage[i])
1079 return -EINVAL;
1080 }
1081 } else {
1082 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1083 *nplanes = 2;
1084 else
1085 *nplanes = 1;
1086
1087 for (i = 0; i < *nplanes; i++)
1088 sizes[i] = q_data->sizeimage[i];
1089 }
1090
1091 mtk_v4l2_debug(1,
1092 "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
1093 ctx->id, vq->type, *nplanes, *nbuffers,
1094 sizes[0], sizes[1]);
1095
1096 return 0;
1097 }
1098
vb2ops_vdec_buf_prepare(struct vb2_buffer * vb)1099 static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
1100 {
1101 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1102 struct mtk_q_data *q_data;
1103 int i;
1104
1105 mtk_v4l2_debug(3, "[%d] (%d) id=%d",
1106 ctx->id, vb->vb2_queue->type, vb->index);
1107
1108 q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
1109
1110 for (i = 0; i < q_data->fmt->num_planes; i++) {
1111 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
1112 mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
1113 i, vb2_plane_size(vb, i),
1114 q_data->sizeimage[i]);
1115 }
1116 }
1117
1118 return 0;
1119 }
1120
vb2ops_vdec_buf_queue(struct vb2_buffer * vb)1121 static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
1122 {
1123 struct vb2_v4l2_buffer *src_buf;
1124 struct mtk_vcodec_mem src_mem;
1125 bool res_chg = false;
1126 int ret = 0;
1127 unsigned int dpbsize = 1, i = 0;
1128 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1129 struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
1130 struct mtk_video_dec_buf *buf = NULL;
1131 struct mtk_q_data *dst_q_data;
1132
1133 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
1134 ctx->id, vb->vb2_queue->type,
1135 vb->index, vb);
1136 /*
1137 * check if this buffer is ready to be used after decode
1138 */
1139 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1140 vb2_v4l2 = to_vb2_v4l2_buffer(vb);
1141 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1142 mutex_lock(&ctx->lock);
1143 if (buf->used == false) {
1144 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
1145 buf->queued_in_vb2 = true;
1146 buf->queued_in_v4l2 = true;
1147 } else {
1148 buf->queued_in_vb2 = false;
1149 buf->queued_in_v4l2 = true;
1150 }
1151 mutex_unlock(&ctx->lock);
1152 return;
1153 }
1154
1155 v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
1156
1157 if (ctx->state != MTK_STATE_INIT) {
1158 mtk_v4l2_debug(3, "[%d] already init driver %d",
1159 ctx->id, ctx->state);
1160 return;
1161 }
1162
1163 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1164 if (!src_buf) {
1165 mtk_v4l2_err("No src buffer");
1166 return;
1167 }
1168 buf = container_of(src_buf, struct mtk_video_dec_buf, vb);
1169 if (buf->lastframe) {
1170 /* This shouldn't happen. Just in case. */
1171 mtk_v4l2_err("Invalid flush buffer.");
1172 v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1173 return;
1174 }
1175
1176 src_mem.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
1177 src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
1178 src_mem.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
1179 mtk_v4l2_debug(2,
1180 "[%d] buf id=%d va=%p dma=%pad size=%zx",
1181 ctx->id, src_buf->vb2_buf.index,
1182 src_mem.va, &src_mem.dma_addr,
1183 src_mem.size);
1184
1185 ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
1186 if (ret || !res_chg) {
1187 /*
1188 * fb == NULL means to parse SPS/PPS header or
1189 * resolution info in src_mem. Decode can fail
1190 * if there is no SPS header or picture info
1191 * in bs
1192 */
1193
1194 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1195 if (ret == -EIO) {
1196 mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.",
1197 ctx->id);
1198 ctx->state = MTK_STATE_ABORT;
1199 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1200 } else {
1201 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1202 }
1203 mtk_v4l2_debug(ret ? 0 : 1,
1204 "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
1205 ctx->id, src_buf->vb2_buf.index,
1206 src_mem.size, ret, res_chg);
1207 return;
1208 }
1209
1210 if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
1211 mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
1212 ctx->id);
1213 return;
1214 }
1215
1216 ctx->last_decoded_picinfo = ctx->picinfo;
1217 dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
1218 for (i = 0; i < dst_q_data->fmt->num_planes; i++) {
1219 dst_q_data->sizeimage[i] = ctx->picinfo.fb_sz[i];
1220 dst_q_data->bytesperline[i] = ctx->picinfo.buf_w;
1221 }
1222
1223 mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
1224 ctx->id,
1225 ctx->picinfo.buf_w, ctx->picinfo.buf_h,
1226 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1227 dst_q_data->sizeimage[0],
1228 dst_q_data->sizeimage[1]);
1229
1230 ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
1231 if (dpbsize == 0)
1232 mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
1233
1234 ctx->dpb_size = dpbsize;
1235 ctx->state = MTK_STATE_HEADER;
1236 mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
1237
1238 mtk_vdec_queue_res_chg_event(ctx);
1239 }
1240
vb2ops_vdec_buf_finish(struct vb2_buffer * vb)1241 static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
1242 {
1243 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1244 struct vb2_v4l2_buffer *vb2_v4l2;
1245 struct mtk_video_dec_buf *buf;
1246 bool buf_error;
1247
1248 vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
1249 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1250 mutex_lock(&ctx->lock);
1251 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1252 buf->queued_in_v4l2 = false;
1253 buf->queued_in_vb2 = false;
1254 }
1255 buf_error = buf->error;
1256 mutex_unlock(&ctx->lock);
1257
1258 if (buf_error) {
1259 mtk_v4l2_err("Unrecoverable error on buffer.");
1260 ctx->state = MTK_STATE_ABORT;
1261 }
1262 }
1263
vb2ops_vdec_buf_init(struct vb2_buffer * vb)1264 static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
1265 {
1266 struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
1267 struct vb2_v4l2_buffer, vb2_buf);
1268 struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
1269 struct mtk_video_dec_buf, vb);
1270
1271 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1272 buf->used = false;
1273 buf->queued_in_v4l2 = false;
1274 } else {
1275 buf->lastframe = false;
1276 }
1277
1278 return 0;
1279 }
1280
vb2ops_vdec_start_streaming(struct vb2_queue * q,unsigned int count)1281 static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1282 {
1283 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1284
1285 if (ctx->state == MTK_STATE_FLUSH)
1286 ctx->state = MTK_STATE_HEADER;
1287
1288 return 0;
1289 }
1290
vb2ops_vdec_stop_streaming(struct vb2_queue * q)1291 static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
1292 {
1293 struct vb2_v4l2_buffer *src_buf = NULL, *dst_buf = NULL;
1294 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1295
1296 mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
1297 ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
1298
1299 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1300 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
1301 struct mtk_video_dec_buf *buf_info = container_of(
1302 src_buf, struct mtk_video_dec_buf, vb);
1303 if (!buf_info->lastframe)
1304 v4l2_m2m_buf_done(src_buf,
1305 VB2_BUF_STATE_ERROR);
1306 }
1307 return;
1308 }
1309
1310 if (ctx->state >= MTK_STATE_HEADER) {
1311
1312 /* Until STREAMOFF is called on the CAPTURE queue
1313 * (acknowledging the event), the driver operates
1314 * as if the resolution hasn't changed yet, i.e.
1315 * VIDIOC_G_FMT< etc. return previous resolution.
1316 * So we update picinfo here
1317 */
1318 ctx->picinfo = ctx->last_decoded_picinfo;
1319
1320 mtk_v4l2_debug(2,
1321 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
1322 ctx->id, ctx->last_decoded_picinfo.pic_w,
1323 ctx->last_decoded_picinfo.pic_h,
1324 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1325 ctx->last_decoded_picinfo.buf_w,
1326 ctx->last_decoded_picinfo.buf_h);
1327
1328 mtk_vdec_flush_decoder(ctx);
1329 }
1330 ctx->state = MTK_STATE_FLUSH;
1331
1332 while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
1333 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1334 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
1335 vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
1336 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1337 }
1338
1339 }
1340
m2mops_vdec_device_run(void * priv)1341 static void m2mops_vdec_device_run(void *priv)
1342 {
1343 struct mtk_vcodec_ctx *ctx = priv;
1344 struct mtk_vcodec_dev *dev = ctx->dev;
1345
1346 queue_work(dev->decode_workqueue, &ctx->decode_work);
1347 }
1348
m2mops_vdec_job_ready(void * m2m_priv)1349 static int m2mops_vdec_job_ready(void *m2m_priv)
1350 {
1351 struct mtk_vcodec_ctx *ctx = m2m_priv;
1352
1353 mtk_v4l2_debug(3, "[%d]", ctx->id);
1354
1355 if (ctx->state == MTK_STATE_ABORT)
1356 return 0;
1357
1358 if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
1359 (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
1360 return 0;
1361
1362 if (ctx->state != MTK_STATE_HEADER)
1363 return 0;
1364
1365 return 1;
1366 }
1367
m2mops_vdec_job_abort(void * priv)1368 static void m2mops_vdec_job_abort(void *priv)
1369 {
1370 struct mtk_vcodec_ctx *ctx = priv;
1371
1372 ctx->state = MTK_STATE_ABORT;
1373 }
1374
mtk_vdec_g_v_ctrl(struct v4l2_ctrl * ctrl)1375 static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
1376 {
1377 struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
1378 int ret = 0;
1379
1380 switch (ctrl->id) {
1381 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
1382 if (ctx->state >= MTK_STATE_HEADER) {
1383 ctrl->val = ctx->dpb_size;
1384 } else {
1385 mtk_v4l2_debug(0, "Seqinfo not ready");
1386 ctrl->val = 0;
1387 }
1388 break;
1389 default:
1390 ret = -EINVAL;
1391 }
1392 return ret;
1393 }
1394
1395 static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
1396 .g_volatile_ctrl = mtk_vdec_g_v_ctrl,
1397 };
1398
mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx * ctx)1399 int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1400 {
1401 struct v4l2_ctrl *ctrl;
1402
1403 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
1404
1405 ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
1406 &mtk_vcodec_dec_ctrl_ops,
1407 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
1408 0, 32, 1, 1);
1409 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1410 v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl,
1411 &mtk_vcodec_dec_ctrl_ops,
1412 V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
1413 V4L2_MPEG_VIDEO_VP9_PROFILE_0,
1414 0, V4L2_MPEG_VIDEO_VP9_PROFILE_0);
1415
1416 if (ctx->ctrl_hdl.error) {
1417 mtk_v4l2_err("Adding control failed %d",
1418 ctx->ctrl_hdl.error);
1419 return ctx->ctrl_hdl.error;
1420 }
1421
1422 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1423 return 0;
1424 }
1425
1426 const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
1427 .device_run = m2mops_vdec_device_run,
1428 .job_ready = m2mops_vdec_job_ready,
1429 .job_abort = m2mops_vdec_job_abort,
1430 };
1431
1432 static const struct vb2_ops mtk_vdec_vb2_ops = {
1433 .queue_setup = vb2ops_vdec_queue_setup,
1434 .buf_prepare = vb2ops_vdec_buf_prepare,
1435 .buf_queue = vb2ops_vdec_buf_queue,
1436 .wait_prepare = vb2_ops_wait_prepare,
1437 .wait_finish = vb2_ops_wait_finish,
1438 .buf_init = vb2ops_vdec_buf_init,
1439 .buf_finish = vb2ops_vdec_buf_finish,
1440 .start_streaming = vb2ops_vdec_start_streaming,
1441 .stop_streaming = vb2ops_vdec_stop_streaming,
1442 };
1443
1444 const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
1445 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1446 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1447 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
1448 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1449 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1450
1451 .vidioc_qbuf = vidioc_vdec_qbuf,
1452 .vidioc_dqbuf = vidioc_vdec_dqbuf,
1453
1454 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
1455 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
1456
1457 .vidioc_s_fmt_vid_cap_mplane = vidioc_vdec_s_fmt,
1458 .vidioc_s_fmt_vid_out_mplane = vidioc_vdec_s_fmt,
1459 .vidioc_g_fmt_vid_cap_mplane = vidioc_vdec_g_fmt,
1460 .vidioc_g_fmt_vid_out_mplane = vidioc_vdec_g_fmt,
1461
1462 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1463
1464 .vidioc_enum_fmt_vid_cap = vidioc_vdec_enum_fmt_vid_cap,
1465 .vidioc_enum_fmt_vid_out = vidioc_vdec_enum_fmt_vid_out,
1466 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1467
1468 .vidioc_querycap = vidioc_vdec_querycap,
1469 .vidioc_subscribe_event = vidioc_vdec_subscribe_evt,
1470 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1471 .vidioc_g_selection = vidioc_vdec_g_selection,
1472 .vidioc_s_selection = vidioc_vdec_s_selection,
1473
1474 .vidioc_decoder_cmd = vidioc_decoder_cmd,
1475 .vidioc_try_decoder_cmd = vidioc_try_decoder_cmd,
1476 };
1477
mtk_vcodec_dec_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1478 int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
1479 struct vb2_queue *dst_vq)
1480 {
1481 struct mtk_vcodec_ctx *ctx = priv;
1482 int ret = 0;
1483
1484 mtk_v4l2_debug(3, "[%d]", ctx->id);
1485
1486 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1487 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1488 src_vq->drv_priv = ctx;
1489 src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1490 src_vq->ops = &mtk_vdec_vb2_ops;
1491 src_vq->mem_ops = &vb2_dma_contig_memops;
1492 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1493 src_vq->lock = &ctx->dev->dev_mutex;
1494 src_vq->dev = &ctx->dev->plat_dev->dev;
1495
1496 ret = vb2_queue_init(src_vq);
1497 if (ret) {
1498 mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
1499 return ret;
1500 }
1501 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1502 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1503 dst_vq->drv_priv = ctx;
1504 dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1505 dst_vq->ops = &mtk_vdec_vb2_ops;
1506 dst_vq->mem_ops = &vb2_dma_contig_memops;
1507 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1508 dst_vq->lock = &ctx->dev->dev_mutex;
1509 dst_vq->dev = &ctx->dev->plat_dev->dev;
1510
1511 ret = vb2_queue_init(dst_vq);
1512 if (ret) {
1513 vb2_queue_release(src_vq);
1514 mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
1515 }
1516
1517 return ret;
1518 }
1519