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