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 		.frmsize = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
21 			     MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
22 	},
23 	{
24 		.fourcc = V4L2_PIX_FMT_VP8,
25 		.type = MTK_FMT_DEC,
26 		.num_planes = 1,
27 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
28 		.frmsize = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
29 			     MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
30 	},
31 	{
32 		.fourcc = V4L2_PIX_FMT_VP9,
33 		.type = MTK_FMT_DEC,
34 		.num_planes = 1,
35 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
36 		.frmsize = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
37 			     MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
38 	},
39 	{
40 		.fourcc = V4L2_PIX_FMT_MT21C,
41 		.type = MTK_FMT_FRAME,
42 		.num_planes = 2,
43 	},
44 };
45 
46 static const unsigned int num_supported_formats =
47 	ARRAY_SIZE(mtk_video_formats);
48 
49 #define DEFAULT_OUT_FMT_IDX 0
50 #define DEFAULT_CAP_FMT_IDX 3
51 
52 /*
53  * This function tries to clean all display buffers, the buffers will return
54  * in display order.
55  * Note the buffers returned from codec driver may still be in driver's
56  * reference list.
57  */
get_display_buffer(struct mtk_vcodec_ctx * ctx)58 static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
59 {
60 	struct vdec_fb *disp_frame_buffer = NULL;
61 	struct mtk_video_dec_buf *dstbuf;
62 	struct vb2_v4l2_buffer *vb;
63 
64 	mtk_v4l2_debug(3, "[%d]", ctx->id);
65 	if (vdec_if_get_param(ctx, GET_PARAM_DISP_FRAME_BUFFER,
66 			      &disp_frame_buffer)) {
67 		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER", ctx->id);
68 		return NULL;
69 	}
70 
71 	if (!disp_frame_buffer) {
72 		mtk_v4l2_debug(3, "No display frame buffer");
73 		return NULL;
74 	}
75 
76 	dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
77 			      frame_buffer);
78 	vb = &dstbuf->m2m_buf.vb;
79 	mutex_lock(&ctx->lock);
80 	if (dstbuf->used) {
81 		mtk_v4l2_debug(2, "[%d]status=%x queue id=%d to done_list %d",
82 			       ctx->id, disp_frame_buffer->status,
83 			       vb->vb2_buf.index, dstbuf->queued_in_vb2);
84 
85 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_DONE);
86 		ctx->decoded_frame_cnt++;
87 	}
88 	mutex_unlock(&ctx->lock);
89 	return &vb->vb2_buf;
90 }
91 
92 /*
93  * This function tries to clean all capture buffers that are not used as
94  * reference buffers by codec driver any more
95  * In this case, we need re-queue buffer to vb2 buffer if user space
96  * already returns this buffer to v4l2 or this buffer is just the output of
97  * previous sps/pps/resolution change decode, or do nothing if user
98  * space still owns this buffer
99  */
get_free_buffer(struct mtk_vcodec_ctx * ctx)100 static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
101 {
102 	struct mtk_video_dec_buf *dstbuf;
103 	struct vdec_fb *free_frame_buffer = NULL;
104 	struct vb2_v4l2_buffer *vb;
105 
106 	if (vdec_if_get_param(ctx, GET_PARAM_FREE_FRAME_BUFFER,
107 			      &free_frame_buffer)) {
108 		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->id);
109 		return NULL;
110 	}
111 	if (!free_frame_buffer) {
112 		mtk_v4l2_debug(3, " No free frame buffer");
113 		return NULL;
114 	}
115 
116 	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p", ctx->id,
117 		       free_frame_buffer);
118 
119 	dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf,
120 			      frame_buffer);
121 	vb = &dstbuf->m2m_buf.vb;
122 
123 	mutex_lock(&ctx->lock);
124 	if (dstbuf->used) {
125 		if (dstbuf->queued_in_vb2 && dstbuf->queued_in_v4l2 &&
126 		    free_frame_buffer->status == FB_ST_FREE) {
127 			/*
128 			 * After decode sps/pps or non-display buffer, we don't
129 			 * need to return capture buffer to user space, but
130 			 * just re-queue this capture buffer to vb2 queue.
131 			 * This reduce overheads that dq/q unused capture
132 			 * buffer. In this case, queued_in_vb2 = true.
133 			 */
134 			mtk_v4l2_debug(2, "[%d]status=%x queue id=%d to rdy_queue %d",
135 				       ctx->id, free_frame_buffer->status,
136 				       vb->vb2_buf.index, dstbuf->queued_in_vb2);
137 			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
138 		} else if (!dstbuf->queued_in_vb2 && dstbuf->queued_in_v4l2) {
139 			/*
140 			 * If buffer in v4l2 driver but not in vb2 queue yet,
141 			 * and we get this buffer from free_list, it means
142 			 * that codec driver do not use this buffer as
143 			 * reference buffer anymore. We should q buffer to vb2
144 			 * queue, so later work thread could get this buffer
145 			 * for decode. In this case, queued_in_vb2 = false
146 			 * means this buffer is not from previous decode
147 			 * output.
148 			 */
149 			mtk_v4l2_debug(2,
150 				       "[%d]status=%x queue id=%d to rdy_queue",
151 				       ctx->id, free_frame_buffer->status,
152 				       vb->vb2_buf.index);
153 			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
154 			dstbuf->queued_in_vb2 = true;
155 		} else {
156 			/*
157 			 * Codec driver do not need to reference this capture
158 			 * buffer and this buffer is not in v4l2 driver.
159 			 * Then we don't need to do any thing, just add log when
160 			 * we need to debug buffer flow.
161 			 * When this buffer q from user space, it could
162 			 * directly q to vb2 buffer
163 			 */
164 			mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
165 				       ctx->id, free_frame_buffer->status,
166 				       vb->vb2_buf.index, dstbuf->queued_in_vb2,
167 				       dstbuf->queued_in_v4l2);
168 		}
169 		dstbuf->used = false;
170 	}
171 	mutex_unlock(&ctx->lock);
172 	return &vb->vb2_buf;
173 }
174 
clean_display_buffer(struct mtk_vcodec_ctx * ctx)175 static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
176 {
177 	while (get_display_buffer(ctx))
178 		;
179 }
180 
clean_free_buffer(struct mtk_vcodec_ctx * ctx)181 static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
182 {
183 	while (get_free_buffer(ctx))
184 		;
185 }
186 
mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx * ctx)187 static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
188 {
189 	static const struct v4l2_event ev_src_ch = {
190 		.type = V4L2_EVENT_SOURCE_CHANGE,
191 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
192 	};
193 
194 	mtk_v4l2_debug(1, "[%d]", ctx->id);
195 	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
196 }
197 
mtk_vdec_flush_decoder(struct mtk_vcodec_ctx * ctx)198 static int mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
199 {
200 	bool res_chg;
201 	int ret;
202 
203 	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
204 	if (ret)
205 		mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
206 
207 	clean_display_buffer(ctx);
208 	clean_free_buffer(ctx);
209 
210 	return 0;
211 }
212 
mtk_vdec_update_fmt(struct mtk_vcodec_ctx * ctx,unsigned int pixelformat)213 static void mtk_vdec_update_fmt(struct mtk_vcodec_ctx *ctx,
214 				unsigned int pixelformat)
215 {
216 	const struct mtk_video_fmt *fmt;
217 	struct mtk_q_data *dst_q_data;
218 	unsigned int k;
219 
220 	dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
221 	for (k = 0; k < num_supported_formats; k++) {
222 		fmt = &mtk_video_formats[k];
223 		if (fmt->fourcc == pixelformat) {
224 			mtk_v4l2_debug(1, "Update cap fourcc(%d -> %d)",
225 				       dst_q_data->fmt->fourcc, pixelformat);
226 			dst_q_data->fmt = fmt;
227 			return;
228 		}
229 	}
230 
231 	mtk_v4l2_err("Cannot get fourcc(%d), using init value", pixelformat);
232 }
233 
mtk_vdec_pic_info_update(struct mtk_vcodec_ctx * ctx)234 static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
235 {
236 	unsigned int dpbsize = 0;
237 	int ret;
238 
239 	if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO,
240 			      &ctx->last_decoded_picinfo)) {
241 		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR", ctx->id);
242 		return -EINVAL;
243 	}
244 
245 	if (ctx->last_decoded_picinfo.pic_w == 0 ||
246 	    ctx->last_decoded_picinfo.pic_h == 0 ||
247 	    ctx->last_decoded_picinfo.buf_w == 0 ||
248 	    ctx->last_decoded_picinfo.buf_h == 0) {
249 		mtk_v4l2_err("Cannot get correct pic info");
250 		return -EINVAL;
251 	}
252 
253 	if (ctx->last_decoded_picinfo.cap_fourcc != ctx->picinfo.cap_fourcc &&
254 	    ctx->picinfo.cap_fourcc != 0)
255 		mtk_vdec_update_fmt(ctx, ctx->picinfo.cap_fourcc);
256 
257 	if (ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w ||
258 	    ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h)
259 		return 0;
260 
261 	mtk_v4l2_debug(1, "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)", ctx->id,
262 		       ctx->last_decoded_picinfo.pic_w,
263 		       ctx->last_decoded_picinfo.pic_h, ctx->picinfo.pic_w,
264 		       ctx->picinfo.pic_h, ctx->last_decoded_picinfo.buf_w,
265 		       ctx->last_decoded_picinfo.buf_h);
266 
267 	ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
268 	if (dpbsize == 0)
269 		mtk_v4l2_err("Incorrect dpb size, ret=%d", ret);
270 
271 	ctx->dpb_size = dpbsize;
272 
273 	return ret;
274 }
275 
mtk_vdec_worker(struct work_struct * work)276 static void mtk_vdec_worker(struct work_struct *work)
277 {
278 	struct mtk_vcodec_ctx *ctx =
279 		container_of(work, struct mtk_vcodec_ctx, decode_work);
280 	struct mtk_vcodec_dev *dev = ctx->dev;
281 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
282 	struct mtk_vcodec_mem buf;
283 	struct vdec_fb *pfb;
284 	bool res_chg = false;
285 	int ret;
286 	struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
287 
288 	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
289 	if (!src_buf) {
290 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
291 		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id);
292 		return;
293 	}
294 
295 	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
296 	if (!dst_buf) {
297 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
298 		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
299 		return;
300 	}
301 
302 	dst_buf_info =
303 		container_of(dst_buf, struct mtk_video_dec_buf, m2m_buf.vb);
304 
305 	pfb = &dst_buf_info->frame_buffer;
306 	pfb->base_y.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
307 	pfb->base_y.dma_addr =
308 		vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
309 	pfb->base_y.size = ctx->picinfo.fb_sz[0];
310 
311 	pfb->base_c.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 1);
312 	pfb->base_c.dma_addr =
313 		vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 1);
314 	pfb->base_c.size = ctx->picinfo.fb_sz[1];
315 	pfb->status = 0;
316 	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
317 
318 	mtk_v4l2_debug(3,
319 		       "id=%d Framebuf  pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
320 		       dst_buf->vb2_buf.index, pfb, pfb->base_y.va,
321 		       &pfb->base_y.dma_addr, &pfb->base_c.dma_addr, pfb->base_y.size);
322 
323 	if (src_buf == &ctx->empty_flush_buf.vb) {
324 		mtk_v4l2_debug(1, "Got empty flush input buffer.");
325 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
326 
327 		/* update dst buf status */
328 		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
329 		mutex_lock(&ctx->lock);
330 		dst_buf_info->used = false;
331 		mutex_unlock(&ctx->lock);
332 
333 		vdec_if_decode(ctx, NULL, NULL, &res_chg);
334 		clean_display_buffer(ctx);
335 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
336 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
337 			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
338 		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
339 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
340 		clean_free_buffer(ctx);
341 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
342 		return;
343 	}
344 
345 	src_buf_info =
346 		container_of(src_buf, struct mtk_video_dec_buf, m2m_buf.vb);
347 
348 	buf.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
349 	buf.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
350 	buf.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
351 	if (!buf.va) {
352 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
353 		mtk_v4l2_err("[%d] id=%d src_addr is NULL!!", ctx->id,
354 			     src_buf->vb2_buf.index);
355 		return;
356 	}
357 	mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
358 		       ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
359 	dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
360 	dst_buf->timecode = src_buf->timecode;
361 	mutex_lock(&ctx->lock);
362 	dst_buf_info->used = true;
363 	mutex_unlock(&ctx->lock);
364 	src_buf_info->used = true;
365 
366 	ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
367 
368 	if (ret) {
369 		mtk_v4l2_err(" <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() 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_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_debug(3, "[%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_debug(3, "[%d] already init driver %d", ctx->id,
452 			       ctx->state);
453 		return;
454 	}
455 
456 	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
457 	if (!src_buf) {
458 		mtk_v4l2_err("No src buffer");
459 		return;
460 	}
461 
462 	if (src_buf == &ctx->empty_flush_buf.vb) {
463 		/* This shouldn't happen. Just in case. */
464 		mtk_v4l2_err("Invalid flush buffer.");
465 		v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
466 		return;
467 	}
468 
469 	src_mem.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
470 	src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
471 	src_mem.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
472 	mtk_v4l2_debug(2, "[%d] buf id=%d va=%p dma=%pad size=%zx", ctx->id,
473 		       src_buf->vb2_buf.index, src_mem.va, &src_mem.dma_addr,
474 		       src_mem.size);
475 
476 	ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
477 	if (ret || !res_chg) {
478 		/*
479 		 * fb == NULL means to parse SPS/PPS header or
480 		 * resolution info in src_mem. Decode can fail
481 		 * if there is no SPS header or picture info
482 		 * in bs
483 		 */
484 
485 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
486 		if (ret == -EIO) {
487 			mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.", ctx->id);
488 			ctx->state = MTK_STATE_ABORT;
489 			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
490 		} else {
491 			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
492 		}
493 		mtk_v4l2_debug(ret ? 0 : 1,
494 			       "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
495 			       ctx->id, src_buf->vb2_buf.index, src_mem.size, ret, res_chg);
496 		return;
497 	}
498 
499 	if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
500 		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR", 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_debug(2, "[%d] vdec_if_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_err("[%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_debug(1, "[%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_ctx *ctx = ctrl_to_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_debug(0, "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_ctx * ctx)550 static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_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_err("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_ctx * ctx)581 static void mtk_init_vdec_params(struct mtk_vcodec_ctx *ctx)
582 {
583 }
584 
585 static struct vb2_ops mtk_vdec_frame_vb2_ops = {
586 	.queue_setup = vb2ops_vdec_queue_setup,
587 	.buf_prepare = vb2ops_vdec_buf_prepare,
588 	.wait_prepare = vb2_ops_wait_prepare,
589 	.wait_finish = vb2_ops_wait_finish,
590 	.start_streaming = vb2ops_vdec_start_streaming,
591 
592 	.buf_queue = vb2ops_vdec_stateful_buf_queue,
593 	.buf_init = vb2ops_vdec_buf_init,
594 	.buf_finish = vb2ops_vdec_buf_finish,
595 	.stop_streaming = vb2ops_vdec_stop_streaming,
596 };
597 
598 const struct mtk_vcodec_dec_pdata mtk_vdec_8173_pdata = {
599 	.init_vdec_params = mtk_init_vdec_params,
600 	.ctrls_setup = mtk_vcodec_dec_ctrls_setup,
601 	.vdec_vb2_ops = &mtk_vdec_frame_vb2_ops,
602 	.vdec_formats = mtk_video_formats,
603 	.num_formats = &num_supported_formats,
604 	.default_out_fmt = &mtk_video_formats[DEFAULT_OUT_FMT_IDX],
605 	.default_cap_fmt = &mtk_video_formats[DEFAULT_CAP_FMT_IDX],
606 	.worker = mtk_vdec_worker,
607 	.flush_decoder = mtk_vdec_flush_decoder,
608 	.is_subdev_supported = false,
609 	.hw_arch = MTK_VDEC_PURE_SINGLE_CORE,
610 };
611