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