1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <media/v4l2-event.h>
4 #include <media/v4l2-mem2mem.h>
5 #include <media/videobuf2-dma-contig.h>
6 
7 #include "mtk_vcodec_drv.h"
8 #include "mtk_vcodec_dec.h"
9 #include "mtk_vcodec_intr.h"
10 #include "mtk_vcodec_util.h"
11 #include "mtk_vcodec_dec_pm.h"
12 #include "vdec_drv_if.h"
13 
14 static const struct mtk_video_fmt mtk_video_formats[] = {
15 	{
16 		.fourcc = V4L2_PIX_FMT_H264,
17 		.type = MTK_FMT_DEC,
18 		.num_planes = 1,
19 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
20 	},
21 	{
22 		.fourcc = V4L2_PIX_FMT_VP8,
23 		.type = MTK_FMT_DEC,
24 		.num_planes = 1,
25 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
26 	},
27 	{
28 		.fourcc = V4L2_PIX_FMT_VP9,
29 		.type = MTK_FMT_DEC,
30 		.num_planes = 1,
31 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
32 	},
33 	{
34 		.fourcc = V4L2_PIX_FMT_MT21C,
35 		.type = MTK_FMT_FRAME,
36 		.num_planes = 2,
37 	},
38 };
39 
40 #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
41 #define DEFAULT_OUT_FMT_IDX 0
42 #define DEFAULT_CAP_FMT_IDX 3
43 
44 static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
45 	{
46 		.fourcc = V4L2_PIX_FMT_H264,
47 		.stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
48 			      MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
49 	},
50 	{
51 		.fourcc = V4L2_PIX_FMT_VP8,
52 		.stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
53 			      MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
54 	},
55 	{
56 		.fourcc = V4L2_PIX_FMT_VP9,
57 		.stepwise = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
58 			      MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
59 	},
60 };
61 
62 #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
63 
64 /*
65  * This function tries to clean all display buffers, the buffers will return
66  * in display order.
67  * Note the buffers returned from codec driver may still be in driver's
68  * reference list.
69  */
get_display_buffer(struct mtk_vcodec_ctx * ctx)70 static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
71 {
72 	struct vdec_fb *disp_frame_buffer = NULL;
73 	struct mtk_video_dec_buf *dstbuf;
74 	struct vb2_v4l2_buffer *vb;
75 
76 	mtk_v4l2_debug(3, "[%d]", ctx->id);
77 	if (vdec_if_get_param(ctx, GET_PARAM_DISP_FRAME_BUFFER,
78 			      &disp_frame_buffer)) {
79 		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER", ctx->id);
80 		return NULL;
81 	}
82 
83 	if (!disp_frame_buffer) {
84 		mtk_v4l2_debug(3, "No display frame buffer");
85 		return NULL;
86 	}
87 
88 	dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
89 			      frame_buffer);
90 	vb = &dstbuf->m2m_buf.vb;
91 	mutex_lock(&ctx->lock);
92 	if (dstbuf->used) {
93 		vb2_set_plane_payload(&vb->vb2_buf, 0, ctx->picinfo.fb_sz[0]);
94 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
95 			vb2_set_plane_payload(&vb->vb2_buf, 1,
96 					      ctx->picinfo.fb_sz[1]);
97 
98 		mtk_v4l2_debug(2, "[%d]status=%x queue id=%d to done_list %d",
99 			       ctx->id, disp_frame_buffer->status,
100 			       vb->vb2_buf.index, dstbuf->queued_in_vb2);
101 
102 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_DONE);
103 		ctx->decoded_frame_cnt++;
104 	}
105 	mutex_unlock(&ctx->lock);
106 	return &vb->vb2_buf;
107 }
108 
109 /*
110  * This function tries to clean all capture buffers that are not used as
111  * reference buffers by codec driver any more
112  * In this case, we need re-queue buffer to vb2 buffer if user space
113  * already returns this buffer to v4l2 or this buffer is just the output of
114  * previous sps/pps/resolution change decode, or do nothing if user
115  * space still owns this buffer
116  */
get_free_buffer(struct mtk_vcodec_ctx * ctx)117 static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
118 {
119 	struct mtk_video_dec_buf *dstbuf;
120 	struct vdec_fb *free_frame_buffer = NULL;
121 	struct vb2_v4l2_buffer *vb;
122 
123 	if (vdec_if_get_param(ctx, GET_PARAM_FREE_FRAME_BUFFER,
124 			      &free_frame_buffer)) {
125 		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->id);
126 		return NULL;
127 	}
128 	if (!free_frame_buffer) {
129 		mtk_v4l2_debug(3, " No free frame buffer");
130 		return NULL;
131 	}
132 
133 	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p", ctx->id,
134 		       free_frame_buffer);
135 
136 	dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf,
137 			      frame_buffer);
138 	vb = &dstbuf->m2m_buf.vb;
139 
140 	mutex_lock(&ctx->lock);
141 	if (dstbuf->used) {
142 		if (dstbuf->queued_in_vb2 && dstbuf->queued_in_v4l2 &&
143 		    free_frame_buffer->status == FB_ST_FREE) {
144 			/*
145 			 * After decode sps/pps or non-display buffer, we don't
146 			 * need to return capture buffer to user space, but
147 			 * just re-queue this capture buffer to vb2 queue.
148 			 * This reduce overheads that dq/q unused capture
149 			 * buffer. In this case, queued_in_vb2 = true.
150 			 */
151 			mtk_v4l2_debug(2, "[%d]status=%x queue id=%d to rdy_queue %d",
152 				       ctx->id, free_frame_buffer->status,
153 				       vb->vb2_buf.index, dstbuf->queued_in_vb2);
154 			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
155 		} else if (!dstbuf->queued_in_vb2 && dstbuf->queued_in_v4l2) {
156 			/*
157 			 * If buffer in v4l2 driver but not in vb2 queue yet,
158 			 * and we get this buffer from free_list, it means
159 			 * that codec driver do not use this buffer as
160 			 * reference buffer anymore. We should q buffer to vb2
161 			 * queue, so later work thread could get this buffer
162 			 * for decode. In this case, queued_in_vb2 = false
163 			 * means this buffer is not from previous decode
164 			 * output.
165 			 */
166 			mtk_v4l2_debug(2,
167 				       "[%d]status=%x queue id=%d to rdy_queue",
168 				       ctx->id, free_frame_buffer->status,
169 				       vb->vb2_buf.index);
170 			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
171 			dstbuf->queued_in_vb2 = true;
172 		} else {
173 			/*
174 			 * Codec driver do not need to reference this capture
175 			 * buffer and this buffer is not in v4l2 driver.
176 			 * Then we don't need to do any thing, just add log when
177 			 * we need to debug buffer flow.
178 			 * When this buffer q from user space, it could
179 			 * directly q to vb2 buffer
180 			 */
181 			mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
182 				       ctx->id, free_frame_buffer->status,
183 				       vb->vb2_buf.index, dstbuf->queued_in_vb2,
184 				       dstbuf->queued_in_v4l2);
185 		}
186 		dstbuf->used = false;
187 	}
188 	mutex_unlock(&ctx->lock);
189 	return &vb->vb2_buf;
190 }
191 
clean_display_buffer(struct mtk_vcodec_ctx * ctx)192 static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
193 {
194 	while (get_display_buffer(ctx))
195 		;
196 }
197 
clean_free_buffer(struct mtk_vcodec_ctx * ctx)198 static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
199 {
200 	while (get_free_buffer(ctx))
201 		;
202 }
203 
mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx * ctx)204 static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
205 {
206 	static const struct v4l2_event ev_src_ch = {
207 		.type = V4L2_EVENT_SOURCE_CHANGE,
208 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
209 	};
210 
211 	mtk_v4l2_debug(1, "[%d]", ctx->id);
212 	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
213 }
214 
mtk_vdec_flush_decoder(struct mtk_vcodec_ctx * ctx)215 static int mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
216 {
217 	bool res_chg;
218 	int ret;
219 
220 	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
221 	if (ret)
222 		mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
223 
224 	clean_display_buffer(ctx);
225 	clean_free_buffer(ctx);
226 
227 	return 0;
228 }
229 
mtk_vdec_update_fmt(struct mtk_vcodec_ctx * ctx,unsigned int pixelformat)230 static void mtk_vdec_update_fmt(struct mtk_vcodec_ctx *ctx,
231 				unsigned int pixelformat)
232 {
233 	const struct mtk_video_fmt *fmt;
234 	struct mtk_q_data *dst_q_data;
235 	unsigned int k;
236 
237 	dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
238 	for (k = 0; k < NUM_FORMATS; k++) {
239 		fmt = &mtk_video_formats[k];
240 		if (fmt->fourcc == pixelformat) {
241 			mtk_v4l2_debug(1, "Update cap fourcc(%d -> %d)",
242 				       dst_q_data->fmt->fourcc, pixelformat);
243 			dst_q_data->fmt = fmt;
244 			return;
245 		}
246 	}
247 
248 	mtk_v4l2_err("Cannot get fourcc(%d), using init value", pixelformat);
249 }
250 
mtk_vdec_pic_info_update(struct mtk_vcodec_ctx * ctx)251 static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
252 {
253 	unsigned int dpbsize = 0;
254 	int ret;
255 
256 	if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO,
257 			      &ctx->last_decoded_picinfo)) {
258 		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR", ctx->id);
259 		return -EINVAL;
260 	}
261 
262 	if (ctx->last_decoded_picinfo.pic_w == 0 ||
263 	    ctx->last_decoded_picinfo.pic_h == 0 ||
264 	    ctx->last_decoded_picinfo.buf_w == 0 ||
265 	    ctx->last_decoded_picinfo.buf_h == 0) {
266 		mtk_v4l2_err("Cannot get correct pic info");
267 		return -EINVAL;
268 	}
269 
270 	if (ctx->last_decoded_picinfo.cap_fourcc != ctx->picinfo.cap_fourcc &&
271 	    ctx->picinfo.cap_fourcc != 0)
272 		mtk_vdec_update_fmt(ctx, ctx->picinfo.cap_fourcc);
273 
274 	if (ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w ||
275 	    ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h)
276 		return 0;
277 
278 	mtk_v4l2_debug(1, "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)", ctx->id,
279 		       ctx->last_decoded_picinfo.pic_w,
280 		       ctx->last_decoded_picinfo.pic_h, ctx->picinfo.pic_w,
281 		       ctx->picinfo.pic_h, ctx->last_decoded_picinfo.buf_w,
282 		       ctx->last_decoded_picinfo.buf_h);
283 
284 	ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
285 	if (dpbsize == 0)
286 		mtk_v4l2_err("Incorrect dpb size, ret=%d", ret);
287 
288 	ctx->dpb_size = dpbsize;
289 
290 	return ret;
291 }
292 
mtk_vdec_worker(struct work_struct * work)293 static void mtk_vdec_worker(struct work_struct *work)
294 {
295 	struct mtk_vcodec_ctx *ctx =
296 		container_of(work, struct mtk_vcodec_ctx, decode_work);
297 	struct mtk_vcodec_dev *dev = ctx->dev;
298 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
299 	struct mtk_vcodec_mem buf;
300 	struct vdec_fb *pfb;
301 	bool res_chg = false;
302 	int ret;
303 	struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
304 
305 	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
306 	if (!src_buf) {
307 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
308 		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id);
309 		return;
310 	}
311 
312 	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
313 	if (!dst_buf) {
314 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
315 		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
316 		return;
317 	}
318 
319 	dst_buf_info =
320 		container_of(dst_buf, struct mtk_video_dec_buf, m2m_buf.vb);
321 
322 	pfb = &dst_buf_info->frame_buffer;
323 	pfb->base_y.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
324 	pfb->base_y.dma_addr =
325 		vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
326 	pfb->base_y.size = ctx->picinfo.fb_sz[0];
327 
328 	pfb->base_c.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 1);
329 	pfb->base_c.dma_addr =
330 		vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 1);
331 	pfb->base_c.size = ctx->picinfo.fb_sz[1];
332 	pfb->status = 0;
333 	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
334 
335 	mtk_v4l2_debug(3,
336 		       "id=%d Framebuf  pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
337 		       dst_buf->vb2_buf.index, pfb, pfb->base_y.va,
338 		       &pfb->base_y.dma_addr, &pfb->base_c.dma_addr, pfb->base_y.size);
339 
340 	if (src_buf == &ctx->empty_flush_buf.vb) {
341 		mtk_v4l2_debug(1, "Got empty flush input buffer.");
342 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
343 
344 		/* update dst buf status */
345 		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
346 		mutex_lock(&ctx->lock);
347 		dst_buf_info->used = false;
348 		mutex_unlock(&ctx->lock);
349 
350 		vdec_if_decode(ctx, NULL, NULL, &res_chg);
351 		clean_display_buffer(ctx);
352 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
353 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
354 			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
355 		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
356 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
357 		clean_free_buffer(ctx);
358 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
359 		return;
360 	}
361 
362 	src_buf_info =
363 		container_of(src_buf, struct mtk_video_dec_buf, m2m_buf.vb);
364 
365 	buf.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
366 	buf.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
367 	buf.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
368 	if (!buf.va) {
369 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
370 		mtk_v4l2_err("[%d] id=%d src_addr is NULL!!", ctx->id,
371 			     src_buf->vb2_buf.index);
372 		return;
373 	}
374 	mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
375 		       ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
376 	dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
377 	dst_buf->timecode = src_buf->timecode;
378 	mutex_lock(&ctx->lock);
379 	dst_buf_info->used = true;
380 	mutex_unlock(&ctx->lock);
381 	src_buf_info->used = true;
382 
383 	ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
384 
385 	if (ret) {
386 		mtk_v4l2_err(" <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
387 			     ctx->id, src_buf->vb2_buf.index, buf.size,
388 			     src_buf->vb2_buf.timestamp, dst_buf->vb2_buf.index, ret, res_chg);
389 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
390 		if (ret == -EIO) {
391 			mutex_lock(&ctx->lock);
392 			src_buf_info->error = true;
393 			mutex_unlock(&ctx->lock);
394 		}
395 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
396 	} else if (!res_chg) {
397 		/*
398 		 * we only return src buffer with VB2_BUF_STATE_DONE
399 		 * when decode success without resolution change
400 		 */
401 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
402 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
403 	}
404 
405 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
406 	clean_display_buffer(ctx);
407 	clean_free_buffer(ctx);
408 
409 	if (!ret && res_chg) {
410 		mtk_vdec_pic_info_update(ctx);
411 		/*
412 		 * On encountering a resolution change in the stream.
413 		 * The driver must first process and decode all
414 		 * remaining buffers from before the resolution change
415 		 * point, so call flush decode here
416 		 */
417 		mtk_vdec_flush_decoder(ctx);
418 		/*
419 		 * After all buffers containing decoded frames from
420 		 * before the resolution change point ready to be
421 		 * dequeued on the CAPTURE queue, the driver sends a
422 		 * V4L2_EVENT_SOURCE_CHANGE event for source change
423 		 * type V4L2_EVENT_SRC_CH_RESOLUTION
424 		 */
425 		mtk_vdec_queue_res_chg_event(ctx);
426 	}
427 	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
428 }
429 
vb2ops_vdec_stateful_buf_queue(struct vb2_buffer * vb)430 static void vb2ops_vdec_stateful_buf_queue(struct vb2_buffer *vb)
431 {
432 	struct vb2_v4l2_buffer *src_buf;
433 	struct mtk_vcodec_mem src_mem;
434 	bool res_chg = false;
435 	int ret;
436 	unsigned int dpbsize = 1, i;
437 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
438 	struct vb2_v4l2_buffer *vb2_v4l2;
439 	struct mtk_q_data *dst_q_data;
440 
441 	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p", ctx->id,
442 		       vb->vb2_queue->type, vb->index, vb);
443 	/*
444 	 * check if this buffer is ready to be used after decode
445 	 */
446 	if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
447 		struct mtk_video_dec_buf *buf;
448 
449 		vb2_v4l2 = to_vb2_v4l2_buffer(vb);
450 		buf = container_of(vb2_v4l2, struct mtk_video_dec_buf,
451 				   m2m_buf.vb);
452 		mutex_lock(&ctx->lock);
453 		if (!buf->used) {
454 			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
455 			buf->queued_in_vb2 = true;
456 			buf->queued_in_v4l2 = true;
457 		} else {
458 			buf->queued_in_vb2 = false;
459 			buf->queued_in_v4l2 = true;
460 		}
461 		mutex_unlock(&ctx->lock);
462 		return;
463 	}
464 
465 	v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
466 
467 	if (ctx->state != MTK_STATE_INIT) {
468 		mtk_v4l2_debug(3, "[%d] already init driver %d", ctx->id,
469 			       ctx->state);
470 		return;
471 	}
472 
473 	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
474 	if (!src_buf) {
475 		mtk_v4l2_err("No src buffer");
476 		return;
477 	}
478 
479 	if (src_buf == &ctx->empty_flush_buf.vb) {
480 		/* This shouldn't happen. Just in case. */
481 		mtk_v4l2_err("Invalid flush buffer.");
482 		v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
483 		return;
484 	}
485 
486 	src_mem.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
487 	src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
488 	src_mem.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
489 	mtk_v4l2_debug(2, "[%d] buf id=%d va=%p dma=%pad size=%zx", ctx->id,
490 		       src_buf->vb2_buf.index, src_mem.va, &src_mem.dma_addr,
491 		       src_mem.size);
492 
493 	ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
494 	if (ret || !res_chg) {
495 		/*
496 		 * fb == NULL means to parse SPS/PPS header or
497 		 * resolution info in src_mem. Decode can fail
498 		 * if there is no SPS header or picture info
499 		 * in bs
500 		 */
501 
502 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
503 		if (ret == -EIO) {
504 			mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.", ctx->id);
505 			ctx->state = MTK_STATE_ABORT;
506 			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
507 		} else {
508 			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
509 		}
510 		mtk_v4l2_debug(ret ? 0 : 1,
511 			       "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
512 			       ctx->id, src_buf->vb2_buf.index, src_mem.size, ret, res_chg);
513 		return;
514 	}
515 
516 	if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
517 		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR", ctx->id);
518 		return;
519 	}
520 
521 	ctx->last_decoded_picinfo = ctx->picinfo;
522 	dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
523 	for (i = 0; i < dst_q_data->fmt->num_planes; i++) {
524 		dst_q_data->sizeimage[i] = ctx->picinfo.fb_sz[i];
525 		dst_q_data->bytesperline[i] = ctx->picinfo.buf_w;
526 	}
527 
528 	mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
529 		       ctx->id, ctx->picinfo.buf_w, ctx->picinfo.buf_h, ctx->picinfo.pic_w,
530 		       ctx->picinfo.pic_h, dst_q_data->sizeimage[0], dst_q_data->sizeimage[1]);
531 
532 	ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
533 	if (dpbsize == 0)
534 		mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
535 
536 	ctx->dpb_size = dpbsize;
537 	ctx->state = MTK_STATE_HEADER;
538 	mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
539 
540 	mtk_vdec_queue_res_chg_event(ctx);
541 }
542 
mtk_vdec_g_v_ctrl(struct v4l2_ctrl * ctrl)543 static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
544 {
545 	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
546 	int ret = 0;
547 
548 	switch (ctrl->id) {
549 	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
550 		if (ctx->state >= MTK_STATE_HEADER) {
551 			ctrl->val = ctx->dpb_size;
552 		} else {
553 			mtk_v4l2_debug(0, "Seqinfo not ready");
554 			ctrl->val = 0;
555 		}
556 		break;
557 	default:
558 		ret = -EINVAL;
559 	}
560 	return ret;
561 }
562 
563 static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
564 	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
565 };
566 
mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx * ctx)567 static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
568 {
569 	struct v4l2_ctrl *ctrl;
570 
571 	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
572 
573 	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops,
574 				 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 0, 32, 1, 1);
575 	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
576 	v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops,
577 			       V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
578 			       V4L2_MPEG_VIDEO_VP9_PROFILE_0, 0,
579 			       V4L2_MPEG_VIDEO_VP9_PROFILE_0);
580 	/*
581 	 * H264. Baseline / Extended decoding is not supported.
582 	 */
583 	v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops,
584 			       V4L2_CID_MPEG_VIDEO_H264_PROFILE, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
585 			       BIT(V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
586 			       BIT(V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED),
587 			       V4L2_MPEG_VIDEO_H264_PROFILE_MAIN);
588 
589 	if (ctx->ctrl_hdl.error) {
590 		mtk_v4l2_err("Adding control failed %d", ctx->ctrl_hdl.error);
591 		return ctx->ctrl_hdl.error;
592 	}
593 
594 	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
595 	return 0;
596 }
597 
mtk_init_vdec_params(struct mtk_vcodec_ctx * ctx)598 static void mtk_init_vdec_params(struct mtk_vcodec_ctx *ctx)
599 {
600 }
601 
602 static struct vb2_ops mtk_vdec_frame_vb2_ops = {
603 	.queue_setup = vb2ops_vdec_queue_setup,
604 	.buf_prepare = vb2ops_vdec_buf_prepare,
605 	.wait_prepare = vb2_ops_wait_prepare,
606 	.wait_finish = vb2_ops_wait_finish,
607 	.start_streaming = vb2ops_vdec_start_streaming,
608 
609 	.buf_queue = vb2ops_vdec_stateful_buf_queue,
610 	.buf_init = vb2ops_vdec_buf_init,
611 	.buf_finish = vb2ops_vdec_buf_finish,
612 	.stop_streaming = vb2ops_vdec_stop_streaming,
613 };
614 
615 const struct mtk_vcodec_dec_pdata mtk_vdec_8173_pdata = {
616 	.chip = MTK_MT8173,
617 	.init_vdec_params = mtk_init_vdec_params,
618 	.ctrls_setup = mtk_vcodec_dec_ctrls_setup,
619 	.vdec_vb2_ops = &mtk_vdec_frame_vb2_ops,
620 	.vdec_formats = mtk_video_formats,
621 	.num_formats = NUM_FORMATS,
622 	.default_out_fmt = &mtk_video_formats[DEFAULT_OUT_FMT_IDX],
623 	.default_cap_fmt = &mtk_video_formats[DEFAULT_CAP_FMT_IDX],
624 	.vdec_framesizes = mtk_vdec_framesizes,
625 	.num_framesizes = NUM_SUPPORTED_FRAMESIZE,
626 	.worker = mtk_vdec_worker,
627 	.flush_decoder = mtk_vdec_flush_decoder,
628 };
629