xref: /linux/drivers/media/platform/amphion/vdec.c (revision 30c758a48a98c786ef77c655d8e290c591a5beef)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020-2021 NXP
4  */
5 
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/vmalloc.h>
13 #include <linux/videodev2.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-mem2mem.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/videobuf2-v4l2.h>
19 #include <media/videobuf2-dma-contig.h>
20 #include <media/videobuf2-vmalloc.h>
21 #include "vpu.h"
22 #include "vpu_defs.h"
23 #include "vpu_core.h"
24 #include "vpu_helpers.h"
25 #include "vpu_v4l2.h"
26 #include "vpu_cmds.h"
27 #include "vpu_rpc.h"
28 
29 #define VDEC_MIN_BUFFER_CAP		8
30 #define VDEC_MIN_BUFFER_OUT		8
31 
32 struct vdec_fs_info {
33 	char name[8];
34 	u32 type;
35 	u32 max_count;
36 	u32 req_count;
37 	u32 count;
38 	u32 index;
39 	u32 size;
40 	struct vpu_buffer buffer[32];
41 	u32 tag;
42 };
43 
44 struct vdec_t {
45 	u32 seq_hdr_found;
46 	struct vpu_buffer udata;
47 	struct vpu_decode_params params;
48 	struct vpu_dec_codec_info codec_info;
49 	enum vpu_codec_state state;
50 
51 	struct vpu_vb2_buffer *slots[VB2_MAX_FRAME];
52 	u32 req_frame_count;
53 	struct vdec_fs_info mbi;
54 	struct vdec_fs_info dcp;
55 	u32 seq_tag;
56 
57 	bool reset_codec;
58 	bool fixed_fmt;
59 	u32 decoded_frame_count;
60 	u32 display_frame_count;
61 	u32 sequence;
62 	u32 eos_received;
63 	bool is_source_changed;
64 	u32 source_change;
65 	u32 drain;
66 	bool aborting;
67 };
68 
69 static const struct vpu_format vdec_formats[] = {
70 	{
71 		.pixfmt = V4L2_PIX_FMT_NV12M_8L128,
72 		.mem_planes = 2,
73 		.comp_planes = 2,
74 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
75 		.sibling = V4L2_PIX_FMT_NV12_8L128,
76 	},
77 	{
78 		.pixfmt = V4L2_PIX_FMT_NV12_8L128,
79 		.mem_planes = 1,
80 		.comp_planes = 2,
81 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
82 		.sibling = V4L2_PIX_FMT_NV12M_8L128,
83 	},
84 	{
85 		.pixfmt = V4L2_PIX_FMT_NV12M_10BE_8L128,
86 		.mem_planes = 2,
87 		.comp_planes = 2,
88 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
89 		.sibling = V4L2_PIX_FMT_NV12_10BE_8L128,
90 	},
91 	{
92 		.pixfmt = V4L2_PIX_FMT_NV12_10BE_8L128,
93 		.mem_planes = 1,
94 		.comp_planes = 2,
95 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
96 		.sibling = V4L2_PIX_FMT_NV12M_10BE_8L128
97 	},
98 	{
99 		.pixfmt = V4L2_PIX_FMT_H264,
100 		.mem_planes = 1,
101 		.comp_planes = 1,
102 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
103 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
104 	},
105 	{
106 		.pixfmt = V4L2_PIX_FMT_H264_MVC,
107 		.mem_planes = 1,
108 		.comp_planes = 1,
109 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
110 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
111 	},
112 	{
113 		.pixfmt = V4L2_PIX_FMT_HEVC,
114 		.mem_planes = 1,
115 		.comp_planes = 1,
116 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
117 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
118 	},
119 	{
120 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
121 		.mem_planes = 1,
122 		.comp_planes = 1,
123 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
124 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
125 	},
126 	{
127 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
128 		.mem_planes = 1,
129 		.comp_planes = 1,
130 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
131 		.flags = V4L2_FMT_FLAG_COMPRESSED
132 	},
133 	{
134 		.pixfmt = V4L2_PIX_FMT_MPEG2,
135 		.mem_planes = 1,
136 		.comp_planes = 1,
137 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
138 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
139 	},
140 	{
141 		.pixfmt = V4L2_PIX_FMT_MPEG4,
142 		.mem_planes = 1,
143 		.comp_planes = 1,
144 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
145 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
146 	},
147 	{
148 		.pixfmt = V4L2_PIX_FMT_XVID,
149 		.mem_planes = 1,
150 		.comp_planes = 1,
151 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
152 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
153 	},
154 	{
155 		.pixfmt = V4L2_PIX_FMT_VP8,
156 		.mem_planes = 1,
157 		.comp_planes = 1,
158 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
159 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
160 	},
161 	{
162 		.pixfmt = V4L2_PIX_FMT_H263,
163 		.mem_planes = 1,
164 		.comp_planes = 1,
165 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
166 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
167 	},
168 	{0, 0, 0, 0},
169 };
170 
171 static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
172 {
173 	struct vpu_inst *inst = ctrl_to_inst(ctrl);
174 	struct vdec_t *vdec = inst->priv;
175 	int ret = 0;
176 
177 	vpu_inst_lock(inst);
178 	switch (ctrl->id) {
179 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
180 		vdec->params.display_delay_enable = ctrl->val;
181 		break;
182 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:
183 		vdec->params.display_delay = ctrl->val;
184 		break;
185 	default:
186 		ret = -EINVAL;
187 		break;
188 	}
189 	vpu_inst_unlock(inst);
190 
191 	return ret;
192 }
193 
194 static const struct v4l2_ctrl_ops vdec_ctrl_ops = {
195 	.s_ctrl = vdec_op_s_ctrl,
196 	.g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
197 };
198 
199 static int vdec_ctrl_init(struct vpu_inst *inst)
200 {
201 	struct v4l2_ctrl *ctrl;
202 	int ret;
203 
204 	ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
205 	if (ret)
206 		return ret;
207 
208 	v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
209 			  V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY,
210 			  0, 0, 1, 0);
211 
212 	v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
213 			  V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE,
214 			  0, 1, 1, 0);
215 
216 	ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
217 				 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
218 	if (ctrl)
219 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
220 
221 	ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
222 				 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
223 	if (ctrl)
224 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
225 
226 	if (inst->ctrl_handler.error) {
227 		ret = inst->ctrl_handler.error;
228 		v4l2_ctrl_handler_free(&inst->ctrl_handler);
229 		return ret;
230 	}
231 
232 	ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
233 	if (ret) {
234 		dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
235 		v4l2_ctrl_handler_free(&inst->ctrl_handler);
236 		return ret;
237 	}
238 
239 	return 0;
240 }
241 
242 static void vdec_handle_resolution_change(struct vpu_inst *inst)
243 {
244 	struct vdec_t *vdec = inst->priv;
245 	struct vb2_queue *q;
246 
247 	if (!inst->fh.m2m_ctx)
248 		return;
249 
250 	if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
251 		return;
252 	if (!vdec->source_change)
253 		return;
254 
255 	q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
256 	if (!list_empty(&q->done_list))
257 		return;
258 
259 	vdec->source_change--;
260 	vpu_notify_source_change(inst);
261 }
262 
263 static int vdec_update_state(struct vpu_inst *inst, enum vpu_codec_state state, u32 force)
264 {
265 	struct vdec_t *vdec = inst->priv;
266 	enum vpu_codec_state pre_state = inst->state;
267 
268 	if (state == VPU_CODEC_STATE_SEEK) {
269 		if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
270 			vdec->state = inst->state;
271 		else
272 			vdec->state = VPU_CODEC_STATE_ACTIVE;
273 	}
274 	if (inst->state != VPU_CODEC_STATE_SEEK || force)
275 		inst->state = state;
276 	else if (state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
277 		vdec->state = VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE;
278 
279 	if (inst->state != pre_state)
280 		vpu_trace(inst->dev, "[%d] %d -> %d\n", inst->id, pre_state, inst->state);
281 
282 	if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
283 		vdec_handle_resolution_change(inst);
284 
285 	return 0;
286 }
287 
288 static void vdec_set_last_buffer_dequeued(struct vpu_inst *inst)
289 {
290 	struct vdec_t *vdec = inst->priv;
291 
292 	if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
293 		return;
294 
295 	if (vdec->eos_received) {
296 		if (!vpu_set_last_buffer_dequeued(inst)) {
297 			vdec->eos_received--;
298 			vdec_update_state(inst, VPU_CODEC_STATE_DRAIN, 0);
299 		}
300 	}
301 }
302 
303 static int vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
304 {
305 	strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
306 	strscpy(cap->card, "amphion vpu decoder", sizeof(cap->card));
307 	strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
308 
309 	return 0;
310 }
311 
312 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
313 {
314 	struct vpu_inst *inst = to_inst(file);
315 	struct vdec_t *vdec = inst->priv;
316 	const struct vpu_format *fmt;
317 	int ret = -EINVAL;
318 
319 	vpu_inst_lock(inst);
320 	if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
321 		fmt = vpu_get_format(inst, f->type);
322 		if (f->index == 1)
323 			fmt = vpu_helper_find_sibling(inst, f->type, fmt->pixfmt);
324 		if (f->index > 1)
325 			fmt = NULL;
326 	} else {
327 		fmt = vpu_helper_enum_format(inst, f->type, f->index);
328 	}
329 	if (!fmt)
330 		goto exit;
331 
332 	memset(f->reserved, 0, sizeof(f->reserved));
333 	f->pixelformat = fmt->pixfmt;
334 	f->flags = fmt->flags;
335 	ret = 0;
336 exit:
337 	vpu_inst_unlock(inst);
338 	return ret;
339 }
340 
341 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
342 {
343 	struct vpu_inst *inst = to_inst(file);
344 	struct vdec_t *vdec = inst->priv;
345 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
346 	struct vpu_format *cur_fmt;
347 	int i;
348 
349 	vpu_inst_lock(inst);
350 	cur_fmt = vpu_get_format(inst, f->type);
351 
352 	pixmp->pixelformat = cur_fmt->pixfmt;
353 	pixmp->num_planes = cur_fmt->mem_planes;
354 	pixmp->width = cur_fmt->width;
355 	pixmp->height = cur_fmt->height;
356 	pixmp->field = cur_fmt->field;
357 	pixmp->flags = cur_fmt->flags;
358 	for (i = 0; i < pixmp->num_planes; i++) {
359 		pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
360 		pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
361 	}
362 
363 	f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
364 	f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
365 	f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
366 	f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
367 	vpu_inst_unlock(inst);
368 
369 	return 0;
370 }
371 
372 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
373 {
374 	struct vpu_inst *inst = to_inst(file);
375 	struct vdec_t *vdec = inst->priv;
376 	struct vpu_format fmt;
377 
378 	vpu_inst_lock(inst);
379 	if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
380 		struct vpu_format *cap_fmt = vpu_get_format(inst, f->type);
381 
382 		if (!vpu_helper_match_format(inst, cap_fmt->type, cap_fmt->pixfmt,
383 					     f->fmt.pix_mp.pixelformat))
384 			f->fmt.pix_mp.pixelformat = cap_fmt->pixfmt;
385 	}
386 
387 	vpu_try_fmt_common(inst, f, &fmt);
388 
389 	if (vdec->fixed_fmt) {
390 		f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
391 		f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
392 		f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
393 		f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
394 	} else {
395 		f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
396 		f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
397 		f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
398 		f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
399 	}
400 	vpu_inst_unlock(inst);
401 
402 	return 0;
403 }
404 
405 static int vdec_s_fmt_common(struct vpu_inst *inst, struct v4l2_format *f)
406 {
407 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
408 	struct vpu_format fmt;
409 	struct vpu_format *cur_fmt;
410 	struct vb2_queue *q;
411 	struct vdec_t *vdec = inst->priv;
412 	int i;
413 
414 	if (!inst->fh.m2m_ctx)
415 		return -EINVAL;
416 
417 	q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
418 	if (!q)
419 		return -EINVAL;
420 	if (vb2_is_busy(q))
421 		return -EBUSY;
422 
423 	if (vpu_try_fmt_common(inst, f, &fmt))
424 		return -EINVAL;
425 
426 	cur_fmt = vpu_get_format(inst, f->type);
427 	if (V4L2_TYPE_IS_OUTPUT(f->type) && inst->state != VPU_CODEC_STATE_DEINIT) {
428 		if (cur_fmt->pixfmt != fmt.pixfmt) {
429 			vdec->reset_codec = true;
430 			vdec->fixed_fmt = false;
431 		}
432 	}
433 	if (V4L2_TYPE_IS_OUTPUT(f->type) || !vdec->fixed_fmt) {
434 		memcpy(cur_fmt, &fmt, sizeof(*cur_fmt));
435 	} else {
436 		if (vpu_helper_match_format(inst, f->type, cur_fmt->pixfmt, pixmp->pixelformat)) {
437 			cur_fmt->pixfmt = fmt.pixfmt;
438 			cur_fmt->mem_planes = fmt.mem_planes;
439 		}
440 		pixmp->pixelformat = cur_fmt->pixfmt;
441 		pixmp->num_planes = cur_fmt->mem_planes;
442 		pixmp->width = cur_fmt->width;
443 		pixmp->height = cur_fmt->height;
444 		for (i = 0; i < pixmp->num_planes; i++) {
445 			pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
446 			pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
447 		}
448 		pixmp->field = cur_fmt->field;
449 	}
450 
451 	if (!vdec->fixed_fmt) {
452 		if (V4L2_TYPE_IS_OUTPUT(f->type)) {
453 			vdec->params.codec_format = cur_fmt->pixfmt;
454 			vdec->codec_info.color_primaries = f->fmt.pix_mp.colorspace;
455 			vdec->codec_info.transfer_chars = f->fmt.pix_mp.xfer_func;
456 			vdec->codec_info.matrix_coeffs = f->fmt.pix_mp.ycbcr_enc;
457 			vdec->codec_info.full_range = f->fmt.pix_mp.quantization;
458 		} else {
459 			vdec->params.output_format = cur_fmt->pixfmt;
460 			inst->crop.left = 0;
461 			inst->crop.top = 0;
462 			inst->crop.width = cur_fmt->width;
463 			inst->crop.height = cur_fmt->height;
464 		}
465 	}
466 
467 	vpu_trace(inst->dev, "[%d] %c%c%c%c %dx%d\n", inst->id,
468 		  f->fmt.pix_mp.pixelformat,
469 		  f->fmt.pix_mp.pixelformat >> 8,
470 		  f->fmt.pix_mp.pixelformat >> 16,
471 		  f->fmt.pix_mp.pixelformat >> 24,
472 		  f->fmt.pix_mp.width,
473 		  f->fmt.pix_mp.height);
474 
475 	return 0;
476 }
477 
478 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
479 {
480 	struct vpu_inst *inst = to_inst(file);
481 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
482 	struct vdec_t *vdec = inst->priv;
483 	int ret = 0;
484 
485 	vpu_inst_lock(inst);
486 	ret = vdec_s_fmt_common(inst, f);
487 	if (ret)
488 		goto exit;
489 
490 	if (V4L2_TYPE_IS_OUTPUT(f->type) && !vdec->fixed_fmt) {
491 		struct v4l2_format fc;
492 
493 		memset(&fc, 0, sizeof(fc));
494 		fc.type = inst->cap_format.type;
495 		fc.fmt.pix_mp.pixelformat = inst->cap_format.pixfmt;
496 		fc.fmt.pix_mp.width = pixmp->width;
497 		fc.fmt.pix_mp.height = pixmp->height;
498 		vdec_s_fmt_common(inst, &fc);
499 	}
500 
501 	f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
502 	f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
503 	f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
504 	f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
505 
506 exit:
507 	vpu_inst_unlock(inst);
508 	return ret;
509 }
510 
511 static int vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
512 {
513 	struct vpu_inst *inst = to_inst(file);
514 
515 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
516 		return -EINVAL;
517 
518 	switch (s->target) {
519 	case V4L2_SEL_TGT_COMPOSE:
520 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
521 	case V4L2_SEL_TGT_COMPOSE_PADDED:
522 		s->r = inst->crop;
523 		break;
524 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
525 		s->r.left = 0;
526 		s->r.top = 0;
527 		s->r.width = inst->cap_format.width;
528 		s->r.height = inst->cap_format.height;
529 		break;
530 	default:
531 		return -EINVAL;
532 	}
533 
534 	return 0;
535 }
536 
537 static int vdec_drain(struct vpu_inst *inst)
538 {
539 	struct vdec_t *vdec = inst->priv;
540 
541 	if (!inst->fh.m2m_ctx)
542 		return 0;
543 
544 	if (!vdec->drain)
545 		return 0;
546 
547 	if (!vpu_is_source_empty(inst))
548 		return 0;
549 
550 	if (!vdec->params.frame_count) {
551 		vpu_set_last_buffer_dequeued(inst);
552 		return 0;
553 	}
554 
555 	vpu_iface_add_scode(inst, SCODE_PADDING_EOS);
556 	vdec->params.end_flag = 1;
557 	vpu_iface_set_decode_params(inst, &vdec->params, 1);
558 	vdec->drain = 0;
559 	vpu_trace(inst->dev, "[%d] frame_count = %d\n", inst->id, vdec->params.frame_count);
560 
561 	return 0;
562 }
563 
564 static int vdec_cmd_start(struct vpu_inst *inst)
565 {
566 	struct vdec_t *vdec = inst->priv;
567 
568 	switch (inst->state) {
569 	case VPU_CODEC_STATE_STARTED:
570 	case VPU_CODEC_STATE_DRAIN:
571 	case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE:
572 		vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
573 		break;
574 	default:
575 		break;
576 	}
577 	vpu_process_capture_buffer(inst);
578 	if (vdec->eos_received)
579 		vdec_set_last_buffer_dequeued(inst);
580 	return 0;
581 }
582 
583 static int vdec_cmd_stop(struct vpu_inst *inst)
584 {
585 	struct vdec_t *vdec = inst->priv;
586 
587 	vpu_trace(inst->dev, "[%d]\n", inst->id);
588 
589 	if (inst->state == VPU_CODEC_STATE_DEINIT) {
590 		vpu_set_last_buffer_dequeued(inst);
591 	} else {
592 		vdec->drain = 1;
593 		vdec_drain(inst);
594 	}
595 
596 	return 0;
597 }
598 
599 static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
600 {
601 	struct vpu_inst *inst = to_inst(file);
602 	int ret;
603 
604 	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
605 	if (ret)
606 		return ret;
607 
608 	vpu_inst_lock(inst);
609 	switch (cmd->cmd) {
610 	case V4L2_DEC_CMD_START:
611 		vdec_cmd_start(inst);
612 		break;
613 	case V4L2_DEC_CMD_STOP:
614 		vdec_cmd_stop(inst);
615 		break;
616 	default:
617 		break;
618 	}
619 	vpu_inst_unlock(inst);
620 
621 	return 0;
622 }
623 
624 static int vdec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
625 {
626 	switch (sub->type) {
627 	case V4L2_EVENT_EOS:
628 		return v4l2_event_subscribe(fh, sub, 0, NULL);
629 	case V4L2_EVENT_SOURCE_CHANGE:
630 		return v4l2_src_change_event_subscribe(fh, sub);
631 	case V4L2_EVENT_CTRL:
632 		return v4l2_ctrl_subscribe_event(fh, sub);
633 	default:
634 		return -EINVAL;
635 	}
636 
637 	return 0;
638 }
639 
640 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
641 	.vidioc_querycap               = vdec_querycap,
642 	.vidioc_enum_fmt_vid_cap       = vdec_enum_fmt,
643 	.vidioc_enum_fmt_vid_out       = vdec_enum_fmt,
644 	.vidioc_g_fmt_vid_cap_mplane   = vdec_g_fmt,
645 	.vidioc_g_fmt_vid_out_mplane   = vdec_g_fmt,
646 	.vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
647 	.vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
648 	.vidioc_s_fmt_vid_cap_mplane   = vdec_s_fmt,
649 	.vidioc_s_fmt_vid_out_mplane   = vdec_s_fmt,
650 	.vidioc_g_selection            = vdec_g_selection,
651 	.vidioc_try_decoder_cmd        = v4l2_m2m_ioctl_try_decoder_cmd,
652 	.vidioc_decoder_cmd            = vdec_decoder_cmd,
653 	.vidioc_subscribe_event        = vdec_subscribe_event,
654 	.vidioc_unsubscribe_event      = v4l2_event_unsubscribe,
655 	.vidioc_reqbufs                = v4l2_m2m_ioctl_reqbufs,
656 	.vidioc_create_bufs	       = v4l2_m2m_ioctl_create_bufs,
657 	.vidioc_prepare_buf	       = v4l2_m2m_ioctl_prepare_buf,
658 	.vidioc_querybuf               = v4l2_m2m_ioctl_querybuf,
659 	.vidioc_qbuf                   = v4l2_m2m_ioctl_qbuf,
660 	.vidioc_expbuf                 = v4l2_m2m_ioctl_expbuf,
661 	.vidioc_dqbuf                  = v4l2_m2m_ioctl_dqbuf,
662 	.vidioc_streamon               = v4l2_m2m_ioctl_streamon,
663 	.vidioc_streamoff              = v4l2_m2m_ioctl_streamoff,
664 };
665 
666 static bool vdec_check_ready(struct vpu_inst *inst, unsigned int type)
667 {
668 	struct vdec_t *vdec = inst->priv;
669 
670 	if (V4L2_TYPE_IS_OUTPUT(type))
671 		return true;
672 
673 	if (vdec->req_frame_count)
674 		return true;
675 
676 	return false;
677 }
678 
679 static struct vb2_v4l2_buffer *vdec_get_src_buffer(struct vpu_inst *inst, u32 count)
680 {
681 	if (count > 1)
682 		vpu_skip_frame(inst, count - 1);
683 
684 	return vpu_next_src_buf(inst);
685 }
686 
687 static int vdec_frame_decoded(struct vpu_inst *inst, void *arg)
688 {
689 	struct vdec_t *vdec = inst->priv;
690 	struct vpu_dec_pic_info *info = arg;
691 	struct vpu_vb2_buffer *vpu_buf;
692 	struct vb2_v4l2_buffer *vbuf;
693 	struct vb2_v4l2_buffer *src_buf;
694 	int ret = 0;
695 
696 	if (!info || info->id >= ARRAY_SIZE(vdec->slots))
697 		return -EINVAL;
698 
699 	vpu_inst_lock(inst);
700 	vpu_buf = vdec->slots[info->id];
701 	if (!vpu_buf) {
702 		dev_err(inst->dev, "[%d] decoded invalid frame[%d]\n", inst->id, info->id);
703 		ret = -EINVAL;
704 		goto exit;
705 	}
706 	vbuf = &vpu_buf->m2m_buf.vb;
707 	src_buf = vdec_get_src_buffer(inst, info->consumed_count);
708 	if (src_buf) {
709 		v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
710 		if (info->consumed_count) {
711 			v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
712 			vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
713 			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
714 		} else {
715 			vpu_set_buffer_state(src_buf, VPU_BUF_STATE_DECODED);
716 		}
717 	}
718 	if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED)
719 		dev_info(inst->dev, "[%d] buf[%d] has been decoded\n", inst->id, info->id);
720 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_DECODED);
721 	vdec->decoded_frame_count++;
722 exit:
723 	vpu_inst_unlock(inst);
724 
725 	return ret;
726 }
727 
728 static struct vpu_vb2_buffer *vdec_find_buffer(struct vpu_inst *inst, u32 luma)
729 {
730 	struct vdec_t *vdec = inst->priv;
731 	int i;
732 
733 	for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
734 		if (!vdec->slots[i])
735 			continue;
736 		if (luma == vdec->slots[i]->luma)
737 			return vdec->slots[i];
738 	}
739 
740 	return NULL;
741 }
742 
743 static void vdec_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame)
744 {
745 	struct vdec_t *vdec = inst->priv;
746 	struct vpu_format *cur_fmt;
747 	struct vpu_vb2_buffer *vpu_buf;
748 	struct vb2_v4l2_buffer *vbuf;
749 	u32 sequence;
750 	int i;
751 
752 	if (!frame)
753 		return;
754 
755 	vpu_inst_lock(inst);
756 	sequence = vdec->sequence++;
757 	vpu_buf = vdec_find_buffer(inst, frame->luma);
758 	vpu_inst_unlock(inst);
759 	if (!vpu_buf) {
760 		dev_err(inst->dev, "[%d] can't find buffer, id = %d, addr = 0x%x\n",
761 			inst->id, frame->id, frame->luma);
762 		return;
763 	}
764 	if (frame->skipped) {
765 		dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
766 		return;
767 	}
768 
769 	cur_fmt = vpu_get_format(inst, inst->cap_format.type);
770 	vbuf = &vpu_buf->m2m_buf.vb;
771 	if (vbuf->vb2_buf.index != frame->id)
772 		dev_err(inst->dev, "[%d] buffer id(%d, %d) dismatch\n",
773 			inst->id, vbuf->vb2_buf.index, frame->id);
774 
775 	if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_DECODED)
776 		dev_err(inst->dev, "[%d] buffer(%d) ready without decoded\n", inst->id, frame->id);
777 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY);
778 	for (i = 0; i < vbuf->vb2_buf.num_planes; i++)
779 		vb2_set_plane_payload(&vbuf->vb2_buf, i, vpu_get_fmt_plane_size(cur_fmt, i));
780 	vbuf->field = cur_fmt->field;
781 	vbuf->sequence = sequence;
782 	dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
783 
784 	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
785 	vpu_inst_lock(inst);
786 	vdec->display_frame_count++;
787 	vpu_inst_unlock(inst);
788 	dev_dbg(inst->dev, "[%d] decoded : %d, display : %d, sequence : %d\n",
789 		inst->id, vdec->decoded_frame_count, vdec->display_frame_count, vdec->sequence);
790 }
791 
792 static void vdec_stop_done(struct vpu_inst *inst)
793 {
794 	struct vdec_t *vdec = inst->priv;
795 
796 	vpu_inst_lock(inst);
797 	vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 0);
798 	vdec->seq_hdr_found = 0;
799 	vdec->req_frame_count = 0;
800 	vdec->reset_codec = false;
801 	vdec->fixed_fmt = false;
802 	vdec->params.end_flag = 0;
803 	vdec->drain = 0;
804 	vdec->params.frame_count = 0;
805 	vdec->decoded_frame_count = 0;
806 	vdec->display_frame_count = 0;
807 	vdec->sequence = 0;
808 	vdec->eos_received = 0;
809 	vdec->is_source_changed = false;
810 	vdec->source_change = 0;
811 	inst->total_input_count = 0;
812 	vpu_inst_unlock(inst);
813 }
814 
815 static bool vdec_check_source_change(struct vpu_inst *inst)
816 {
817 	struct vdec_t *vdec = inst->priv;
818 	const struct vpu_format *sibling;
819 
820 	if (!inst->fh.m2m_ctx)
821 		return false;
822 
823 	if (vdec->reset_codec)
824 		return false;
825 
826 	sibling = vpu_helper_find_sibling(inst, inst->cap_format.type, inst->cap_format.pixfmt);
827 	if (sibling && vdec->codec_info.pixfmt == sibling->pixfmt)
828 		vdec->codec_info.pixfmt = inst->cap_format.pixfmt;
829 
830 	if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)))
831 		return true;
832 	if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
833 		return true;
834 	if (inst->cap_format.width != vdec->codec_info.decoded_width)
835 		return true;
836 	if (inst->cap_format.height != vdec->codec_info.decoded_height)
837 		return true;
838 	if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap)
839 		return true;
840 	if (inst->crop.left != vdec->codec_info.offset_x)
841 		return true;
842 	if (inst->crop.top != vdec->codec_info.offset_y)
843 		return true;
844 	if (inst->crop.width != vdec->codec_info.width)
845 		return true;
846 	if (inst->crop.height != vdec->codec_info.height)
847 		return true;
848 
849 	return false;
850 }
851 
852 static void vdec_init_fmt(struct vpu_inst *inst)
853 {
854 	struct vdec_t *vdec = inst->priv;
855 	struct v4l2_format f;
856 
857 	memset(&f, 0, sizeof(f));
858 	f.type = inst->cap_format.type;
859 	f.fmt.pix_mp.pixelformat = vdec->codec_info.pixfmt;
860 	f.fmt.pix_mp.width = vdec->codec_info.decoded_width;
861 	f.fmt.pix_mp.height = vdec->codec_info.decoded_height;
862 	if (vdec->codec_info.progressive)
863 		f.fmt.pix_mp.field = V4L2_FIELD_NONE;
864 	else
865 		f.fmt.pix_mp.field = V4L2_FIELD_SEQ_TB;
866 	vpu_try_fmt_common(inst, &f, &inst->cap_format);
867 
868 	inst->out_format.width = vdec->codec_info.width;
869 	inst->out_format.height = vdec->codec_info.height;
870 }
871 
872 static void vdec_init_crop(struct vpu_inst *inst)
873 {
874 	struct vdec_t *vdec = inst->priv;
875 
876 	inst->crop.left = vdec->codec_info.offset_x;
877 	inst->crop.top = vdec->codec_info.offset_y;
878 	inst->crop.width = vdec->codec_info.width;
879 	inst->crop.height = vdec->codec_info.height;
880 }
881 
882 static void vdec_init_mbi(struct vpu_inst *inst)
883 {
884 	struct vdec_t *vdec = inst->priv;
885 
886 	vdec->mbi.size = vdec->codec_info.mbi_size;
887 	vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer);
888 	scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi");
889 	vdec->mbi.type = MEM_RES_MBI;
890 	vdec->mbi.tag = vdec->seq_tag;
891 }
892 
893 static void vdec_init_dcp(struct vpu_inst *inst)
894 {
895 	struct vdec_t *vdec = inst->priv;
896 
897 	vdec->dcp.size = vdec->codec_info.dcp_size;
898 	vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer);
899 	scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp");
900 	vdec->dcp.type = MEM_RES_DCP;
901 	vdec->dcp.tag = vdec->seq_tag;
902 }
903 
904 static void vdec_request_one_fs(struct vdec_fs_info *fs)
905 {
906 	fs->req_count++;
907 	if (fs->req_count > fs->max_count)
908 		fs->req_count = fs->max_count;
909 }
910 
911 static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs)
912 {
913 	struct vpu_buffer *buffer;
914 
915 	if (!fs->size)
916 		return -EINVAL;
917 
918 	if (fs->count >= fs->req_count)
919 		return -EINVAL;
920 
921 	buffer = &fs->buffer[fs->count];
922 	if (buffer->virt && buffer->length >= fs->size)
923 		return 0;
924 
925 	vpu_free_dma(buffer);
926 	buffer->length = fs->size;
927 	return vpu_alloc_dma(inst->core, buffer);
928 }
929 
930 static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
931 {
932 	int ret;
933 
934 	while (fs->count < fs->req_count) {
935 		ret = vdec_alloc_fs_buffer(inst, fs);
936 		if (ret)
937 			break;
938 		fs->count++;
939 	}
940 }
941 
942 static void vdec_clear_fs(struct vdec_fs_info *fs)
943 {
944 	u32 i;
945 
946 	if (!fs)
947 		return;
948 
949 	for (i = 0; i < ARRAY_SIZE(fs->buffer); i++)
950 		vpu_free_dma(&fs->buffer[i]);
951 	memset(fs, 0, sizeof(*fs));
952 }
953 
954 static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
955 {
956 	struct vpu_fs_info info;
957 	int ret;
958 
959 	if (fs->index >= fs->count)
960 		return 0;
961 
962 	memset(&info, 0, sizeof(info));
963 	info.id = fs->index;
964 	info.type = fs->type;
965 	info.tag = fs->tag;
966 	info.luma_addr = fs->buffer[fs->index].phys;
967 	info.luma_size = fs->buffer[fs->index].length;
968 	ret = vpu_session_alloc_fs(inst, &info);
969 	if (ret)
970 		return ret;
971 
972 	fs->index++;
973 	return 0;
974 }
975 
976 static int vdec_response_frame_abnormal(struct vpu_inst *inst)
977 {
978 	struct vdec_t *vdec = inst->priv;
979 	struct vpu_fs_info info;
980 
981 	if (!vdec->req_frame_count)
982 		return 0;
983 
984 	memset(&info, 0, sizeof(info));
985 	info.type = MEM_RES_FRAME;
986 	info.tag = vdec->seq_tag + 0xf0;
987 	vpu_session_alloc_fs(inst, &info);
988 	vdec->req_frame_count--;
989 
990 	return 0;
991 }
992 
993 static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
994 {
995 	struct vdec_t *vdec = inst->priv;
996 	struct vpu_vb2_buffer *vpu_buf;
997 	struct vpu_fs_info info;
998 	int ret;
999 
1000 	if (inst->state != VPU_CODEC_STATE_ACTIVE)
1001 		return -EINVAL;
1002 
1003 	if (vdec->aborting)
1004 		return -EINVAL;
1005 
1006 	if (!vdec->req_frame_count)
1007 		return -EINVAL;
1008 
1009 	if (!vbuf)
1010 		return -EINVAL;
1011 
1012 	if (vdec->slots[vbuf->vb2_buf.index]) {
1013 		dev_err(inst->dev, "[%d] repeat alloc fs %d\n",
1014 			inst->id, vbuf->vb2_buf.index);
1015 		return -EINVAL;
1016 	}
1017 
1018 	dev_dbg(inst->dev, "[%d] state = %d, alloc fs %d, tag = 0x%x\n",
1019 		inst->id, inst->state, vbuf->vb2_buf.index, vdec->seq_tag);
1020 	vpu_buf = to_vpu_vb2_buffer(vbuf);
1021 
1022 	memset(&info, 0, sizeof(info));
1023 	info.id = vbuf->vb2_buf.index;
1024 	info.type = MEM_RES_FRAME;
1025 	info.tag = vdec->seq_tag;
1026 	info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0);
1027 	info.luma_size = inst->cap_format.sizeimage[0];
1028 	if (vbuf->vb2_buf.num_planes > 1)
1029 		info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1);
1030 	else
1031 		info.chroma_addr = info.luma_addr + info.luma_size;
1032 	info.chromau_size = inst->cap_format.sizeimage[1];
1033 	info.bytesperline = inst->cap_format.bytesperline[0];
1034 	ret = vpu_session_alloc_fs(inst, &info);
1035 	if (ret)
1036 		return ret;
1037 
1038 	vpu_buf->tag = info.tag;
1039 	vpu_buf->luma = info.luma_addr;
1040 	vpu_buf->chroma_u = info.chroma_addr;
1041 	vpu_buf->chroma_v = 0;
1042 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1043 	vdec->slots[info.id] = vpu_buf;
1044 	vdec->req_frame_count--;
1045 
1046 	return 0;
1047 }
1048 
1049 static void vdec_response_fs_request(struct vpu_inst *inst, bool force)
1050 {
1051 	struct vdec_t *vdec = inst->priv;
1052 	int i;
1053 	int ret;
1054 
1055 	if (force) {
1056 		for (i = vdec->req_frame_count; i > 0; i--)
1057 			vdec_response_frame_abnormal(inst);
1058 		return;
1059 	}
1060 
1061 	for (i = vdec->req_frame_count; i > 0; i--) {
1062 		ret = vpu_process_capture_buffer(inst);
1063 		if (ret)
1064 			break;
1065 		if (vdec->eos_received)
1066 			break;
1067 	}
1068 
1069 	for (i = vdec->mbi.index; i < vdec->mbi.count; i++) {
1070 		if (vdec_response_fs(inst, &vdec->mbi))
1071 			break;
1072 		if (vdec->eos_received)
1073 			break;
1074 	}
1075 	for (i = vdec->dcp.index; i < vdec->dcp.count; i++) {
1076 		if (vdec_response_fs(inst, &vdec->dcp))
1077 			break;
1078 		if (vdec->eos_received)
1079 			break;
1080 	}
1081 }
1082 
1083 static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag)
1084 {
1085 	struct vpu_fs_info info;
1086 
1087 	memset(&info, 0, sizeof(info));
1088 	info.id = id;
1089 	info.tag = tag;
1090 	vpu_session_release_fs(inst, &info);
1091 }
1092 
1093 static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1094 {
1095 	if (!inst->fh.m2m_ctx)
1096 		return;
1097 	if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE)
1098 		return;
1099 	if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index))
1100 		return;
1101 	v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
1102 }
1103 
1104 static void vdec_clear_slots(struct vpu_inst *inst)
1105 {
1106 	struct vdec_t *vdec = inst->priv;
1107 	struct vpu_vb2_buffer *vpu_buf;
1108 	struct vb2_v4l2_buffer *vbuf;
1109 	int i;
1110 
1111 	for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
1112 		if (!vdec->slots[i])
1113 			continue;
1114 
1115 		vpu_buf = vdec->slots[i];
1116 		vbuf = &vpu_buf->m2m_buf.vb;
1117 
1118 		vpu_trace(inst->dev, "clear slot %d\n", i);
1119 		vdec_response_fs_release(inst, i, vpu_buf->tag);
1120 		vdec_recycle_buffer(inst, vbuf);
1121 		vdec->slots[i]->state = VPU_BUF_STATE_IDLE;
1122 		vdec->slots[i] = NULL;
1123 	}
1124 }
1125 
1126 static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)
1127 {
1128 	struct vdec_t *vdec = inst->priv;
1129 
1130 	vpu_inst_lock(inst);
1131 	memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
1132 
1133 	vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
1134 		  inst->id,
1135 		  vdec->codec_info.decoded_width,
1136 		  vdec->codec_info.decoded_height,
1137 		  vdec->codec_info.offset_x,
1138 		  vdec->codec_info.offset_y,
1139 		  vdec->codec_info.width,
1140 		  vdec->codec_info.height,
1141 		  hdr->num_ref_frms,
1142 		  hdr->num_dpb_frms);
1143 	inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;
1144 	vdec->is_source_changed = vdec_check_source_change(inst);
1145 	vdec_init_fmt(inst);
1146 	vdec_init_crop(inst);
1147 	vdec_init_mbi(inst);
1148 	vdec_init_dcp(inst);
1149 	if (!vdec->seq_hdr_found) {
1150 		vdec->seq_tag = vdec->codec_info.tag;
1151 		if (vdec->is_source_changed) {
1152 			vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1153 			vdec->source_change++;
1154 			vdec_handle_resolution_change(inst);
1155 			vdec->is_source_changed = false;
1156 		}
1157 	}
1158 	if (vdec->seq_tag != vdec->codec_info.tag) {
1159 		vdec_response_fs_request(inst, true);
1160 		vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n",
1161 			  inst->id, vdec->seq_tag, vdec->codec_info.tag);
1162 	}
1163 	vdec->seq_hdr_found++;
1164 	vdec->fixed_fmt = true;
1165 	vpu_inst_unlock(inst);
1166 }
1167 
1168 static void vdec_event_resolution_change(struct vpu_inst *inst)
1169 {
1170 	struct vdec_t *vdec = inst->priv;
1171 
1172 	vpu_trace(inst->dev, "[%d]\n", inst->id);
1173 	vpu_inst_lock(inst);
1174 	vdec->seq_tag = vdec->codec_info.tag;
1175 	vdec_clear_fs(&vdec->mbi);
1176 	vdec_clear_fs(&vdec->dcp);
1177 	vdec_clear_slots(inst);
1178 	vdec_init_mbi(inst);
1179 	vdec_init_dcp(inst);
1180 	if (vdec->is_source_changed) {
1181 		vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1182 		vdec->source_change++;
1183 		vdec_handle_resolution_change(inst);
1184 		vdec->is_source_changed = false;
1185 	}
1186 	vpu_inst_unlock(inst);
1187 }
1188 
1189 static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1190 {
1191 	struct vdec_t *vdec = inst->priv;
1192 
1193 	if (!fs)
1194 		return;
1195 
1196 	vpu_inst_lock(inst);
1197 
1198 	switch (fs->type) {
1199 	case MEM_RES_FRAME:
1200 		vdec->req_frame_count++;
1201 		break;
1202 	case MEM_RES_MBI:
1203 		vdec_request_one_fs(&vdec->mbi);
1204 		break;
1205 	case MEM_RES_DCP:
1206 		vdec_request_one_fs(&vdec->dcp);
1207 		break;
1208 	default:
1209 		break;
1210 	}
1211 
1212 	vdec_alloc_fs(inst, &vdec->mbi);
1213 	vdec_alloc_fs(inst, &vdec->dcp);
1214 
1215 	vdec_response_fs_request(inst, false);
1216 
1217 	vpu_inst_unlock(inst);
1218 }
1219 
1220 static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1221 {
1222 	struct vdec_t *vdec = inst->priv;
1223 	struct vpu_vb2_buffer *vpu_buf;
1224 	struct vb2_v4l2_buffer *vbuf;
1225 
1226 	if (!fs || fs->id >= ARRAY_SIZE(vdec->slots))
1227 		return;
1228 	if (fs->type != MEM_RES_FRAME)
1229 		return;
1230 
1231 	if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) {
1232 		dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id);
1233 		return;
1234 	}
1235 
1236 	vpu_inst_lock(inst);
1237 	vpu_buf = vdec->slots[fs->id];
1238 	vdec->slots[fs->id] = NULL;
1239 
1240 	if (!vpu_buf) {
1241 		dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id);
1242 		goto exit;
1243 	}
1244 
1245 	vbuf = &vpu_buf->m2m_buf.vb;
1246 	if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) {
1247 		dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
1248 		vdec->sequence++;
1249 	}
1250 
1251 	vdec_response_fs_release(inst, fs->id, vpu_buf->tag);
1252 	if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY)
1253 		vdec_recycle_buffer(inst, vbuf);
1254 
1255 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
1256 	vpu_process_capture_buffer(inst);
1257 
1258 exit:
1259 	vpu_inst_unlock(inst);
1260 }
1261 
1262 static void vdec_event_eos(struct vpu_inst *inst)
1263 {
1264 	struct vdec_t *vdec = inst->priv;
1265 
1266 	vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1267 		  inst->id,
1268 		  vdec->params.frame_count,
1269 		  vdec->decoded_frame_count,
1270 		  vdec->display_frame_count,
1271 		  vdec->sequence);
1272 	vpu_inst_lock(inst);
1273 	vdec->eos_received++;
1274 	vdec->fixed_fmt = false;
1275 	inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1276 	vdec_set_last_buffer_dequeued(inst);
1277 	vpu_inst_unlock(inst);
1278 }
1279 
1280 static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data)
1281 {
1282 	switch (event) {
1283 	case VPU_MSG_ID_SEQ_HDR_FOUND:
1284 		vdec_event_seq_hdr(inst, data);
1285 		break;
1286 	case VPU_MSG_ID_RES_CHANGE:
1287 		vdec_event_resolution_change(inst);
1288 		break;
1289 	case VPU_MSG_ID_FRAME_REQ:
1290 		vdec_event_req_fs(inst, data);
1291 		break;
1292 	case VPU_MSG_ID_FRAME_RELEASE:
1293 		vdec_evnet_rel_fs(inst, data);
1294 		break;
1295 	case VPU_MSG_ID_PIC_EOS:
1296 		vdec_event_eos(inst);
1297 		break;
1298 	default:
1299 		break;
1300 	}
1301 }
1302 
1303 static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1304 {
1305 	struct vdec_t *vdec = inst->priv;
1306 	struct vb2_v4l2_buffer *vbuf;
1307 	struct vpu_rpc_buffer_desc desc;
1308 	u32 free_space;
1309 	int ret;
1310 
1311 	vbuf = to_vb2_v4l2_buffer(vb);
1312 	dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n",
1313 		inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0));
1314 
1315 	if (inst->state == VPU_CODEC_STATE_DEINIT)
1316 		return -EINVAL;
1317 	if (vdec->reset_codec)
1318 		return -EINVAL;
1319 
1320 	if (inst->state == VPU_CODEC_STATE_STARTED)
1321 		vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
1322 
1323 	ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1324 	if (ret)
1325 		return ret;
1326 
1327 	free_space = vpu_helper_get_free_space(inst);
1328 	if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000)
1329 		return -ENOMEM;
1330 
1331 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1332 	ret = vpu_iface_input_frame(inst, vb);
1333 	if (ret < 0)
1334 		return -ENOMEM;
1335 
1336 	dev_dbg(inst->dev, "[%d][INPUT  TS]%32lld\n", inst->id, vb->timestamp);
1337 	vdec->params.frame_count++;
1338 
1339 	if (vdec->drain)
1340 		vdec_drain(inst);
1341 
1342 	return 0;
1343 }
1344 
1345 static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1346 {
1347 	struct vdec_t *vdec = inst->priv;
1348 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1349 	int ret;
1350 
1351 	if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
1352 		return -EINVAL;
1353 	if (vdec->reset_codec)
1354 		return -EINVAL;
1355 
1356 	ret = vdec_response_frame(inst, vbuf);
1357 	if (ret)
1358 		return ret;
1359 	v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1360 	return 0;
1361 }
1362 
1363 static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type)
1364 {
1365 	struct vdec_t *vdec = inst->priv;
1366 
1367 	if (V4L2_TYPE_IS_OUTPUT(type))
1368 		return;
1369 
1370 	vdec_handle_resolution_change(inst);
1371 	if (vdec->eos_received)
1372 		vdec_set_last_buffer_dequeued(inst);
1373 }
1374 
1375 static void vdec_abort(struct vpu_inst *inst)
1376 {
1377 	struct vdec_t *vdec = inst->priv;
1378 	struct vpu_rpc_buffer_desc desc;
1379 	int ret;
1380 
1381 	vpu_trace(inst->dev, "[%d] state = %d\n", inst->id, inst->state);
1382 
1383 	vdec->aborting = true;
1384 	vpu_iface_add_scode(inst, SCODE_PADDING_ABORT);
1385 	vdec->params.end_flag = 1;
1386 	vpu_iface_set_decode_params(inst, &vdec->params, 1);
1387 
1388 	vpu_session_abort(inst);
1389 
1390 	ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1391 	if (!ret)
1392 		vpu_iface_update_stream_buffer(inst, desc.rptr, 1);
1393 
1394 	vpu_session_rst_buf(inst);
1395 	vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1396 		  inst->id,
1397 		  vdec->params.frame_count,
1398 		  vdec->decoded_frame_count,
1399 		  vdec->display_frame_count,
1400 		  vdec->sequence);
1401 	if (!vdec->seq_hdr_found)
1402 		vdec->reset_codec = true;
1403 	vdec->params.end_flag = 0;
1404 	vdec->drain = 0;
1405 	vdec->params.frame_count = 0;
1406 	vdec->decoded_frame_count = 0;
1407 	vdec->display_frame_count = 0;
1408 	vdec->sequence = 0;
1409 	vdec->aborting = false;
1410 	inst->extra_size = 0;
1411 }
1412 
1413 static void vdec_stop(struct vpu_inst *inst, bool free)
1414 {
1415 	struct vdec_t *vdec = inst->priv;
1416 
1417 	vdec_clear_slots(inst);
1418 	if (inst->state != VPU_CODEC_STATE_DEINIT)
1419 		vpu_session_stop(inst);
1420 	vdec_clear_fs(&vdec->mbi);
1421 	vdec_clear_fs(&vdec->dcp);
1422 	if (free) {
1423 		vpu_free_dma(&vdec->udata);
1424 		vpu_free_dma(&inst->stream_buffer);
1425 	}
1426 	vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1);
1427 	vdec->reset_codec = false;
1428 }
1429 
1430 static void vdec_release(struct vpu_inst *inst)
1431 {
1432 	if (inst->id != VPU_INST_NULL_ID)
1433 		vpu_trace(inst->dev, "[%d]\n", inst->id);
1434 	vpu_inst_lock(inst);
1435 	vdec_stop(inst, true);
1436 	vpu_inst_unlock(inst);
1437 }
1438 
1439 static void vdec_cleanup(struct vpu_inst *inst)
1440 {
1441 	struct vdec_t *vdec;
1442 
1443 	if (!inst)
1444 		return;
1445 
1446 	vdec = inst->priv;
1447 	vfree(vdec);
1448 	inst->priv = NULL;
1449 	vfree(inst);
1450 }
1451 
1452 static void vdec_init_params(struct vdec_t *vdec)
1453 {
1454 	vdec->params.frame_count = 0;
1455 	vdec->params.end_flag = 0;
1456 }
1457 
1458 static int vdec_start(struct vpu_inst *inst)
1459 {
1460 	struct vdec_t *vdec = inst->priv;
1461 	int stream_buffer_size;
1462 	int ret;
1463 
1464 	if (inst->state != VPU_CODEC_STATE_DEINIT)
1465 		return 0;
1466 
1467 	vpu_trace(inst->dev, "[%d]\n", inst->id);
1468 	if (!vdec->udata.virt) {
1469 		vdec->udata.length = 0x1000;
1470 		ret = vpu_alloc_dma(inst->core, &vdec->udata);
1471 		if (ret) {
1472 			dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id);
1473 			goto error;
1474 		}
1475 	}
1476 
1477 	if (!inst->stream_buffer.virt) {
1478 		stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
1479 		if (stream_buffer_size > 0) {
1480 			inst->stream_buffer.length = stream_buffer_size;
1481 			ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
1482 			if (ret) {
1483 				dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id);
1484 				goto error;
1485 			}
1486 			inst->use_stream_buffer = true;
1487 		}
1488 	}
1489 
1490 	if (inst->use_stream_buffer)
1491 		vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
1492 	vpu_iface_init_instance(inst);
1493 	vdec->params.udata.base = vdec->udata.phys;
1494 	vdec->params.udata.size = vdec->udata.length;
1495 	ret = vpu_iface_set_decode_params(inst, &vdec->params, 0);
1496 	if (ret) {
1497 		dev_err(inst->dev, "[%d] set decode params fail\n", inst->id);
1498 		goto error;
1499 	}
1500 
1501 	vdec_init_params(vdec);
1502 	ret = vpu_session_start(inst);
1503 	if (ret) {
1504 		dev_err(inst->dev, "[%d] start fail\n", inst->id);
1505 		goto error;
1506 	}
1507 
1508 	vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0);
1509 
1510 	return 0;
1511 error:
1512 	vpu_free_dma(&vdec->udata);
1513 	vpu_free_dma(&inst->stream_buffer);
1514 	return ret;
1515 }
1516 
1517 static int vdec_start_session(struct vpu_inst *inst, u32 type)
1518 {
1519 	struct vdec_t *vdec = inst->priv;
1520 	int ret = 0;
1521 
1522 	if (V4L2_TYPE_IS_OUTPUT(type)) {
1523 		if (vdec->reset_codec)
1524 			vdec_stop(inst, false);
1525 		if (inst->state == VPU_CODEC_STATE_DEINIT) {
1526 			ret = vdec_start(inst);
1527 			if (ret)
1528 				return ret;
1529 		}
1530 	}
1531 
1532 	if (V4L2_TYPE_IS_OUTPUT(type)) {
1533 		vdec_update_state(inst, vdec->state, 1);
1534 		vdec->eos_received = 0;
1535 		vpu_process_output_buffer(inst);
1536 	} else {
1537 		vdec_cmd_start(inst);
1538 	}
1539 	if (inst->state == VPU_CODEC_STATE_ACTIVE)
1540 		vdec_response_fs_request(inst, false);
1541 
1542 	return ret;
1543 }
1544 
1545 static int vdec_stop_session(struct vpu_inst *inst, u32 type)
1546 {
1547 	struct vdec_t *vdec = inst->priv;
1548 
1549 	if (inst->state == VPU_CODEC_STATE_DEINIT)
1550 		return 0;
1551 
1552 	if (V4L2_TYPE_IS_OUTPUT(type)) {
1553 		vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0);
1554 		vdec->drain = 0;
1555 	} else {
1556 		if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) {
1557 			vdec_abort(inst);
1558 			vdec->eos_received = 0;
1559 		}
1560 		vdec_clear_slots(inst);
1561 	}
1562 
1563 	return 0;
1564 }
1565 
1566 static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1567 {
1568 	struct vdec_t *vdec = inst->priv;
1569 	int num = -1;
1570 
1571 	switch (i) {
1572 	case 0:
1573 		num = scnprintf(str, size,
1574 				"req_frame_count = %d\ninterlaced = %d\n",
1575 				vdec->req_frame_count,
1576 				vdec->codec_info.progressive ? 0 : 1);
1577 		break;
1578 	case 1:
1579 		num = scnprintf(str, size,
1580 				"mbi: size = 0x%x request = %d, alloc = %d, response = %d\n",
1581 				vdec->mbi.size,
1582 				vdec->mbi.req_count,
1583 				vdec->mbi.count,
1584 				vdec->mbi.index);
1585 		break;
1586 	case 2:
1587 		num = scnprintf(str, size,
1588 				"dcp: size = 0x%x request = %d, alloc = %d, response = %d\n",
1589 				vdec->dcp.size,
1590 				vdec->dcp.req_count,
1591 				vdec->dcp.count,
1592 				vdec->dcp.index);
1593 		break;
1594 	case 3:
1595 		num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count);
1596 		break;
1597 	case 4:
1598 		num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count);
1599 		break;
1600 	case 5:
1601 		num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count);
1602 		break;
1603 	case 6:
1604 		num = scnprintf(str, size, "sequence = %d\n", vdec->sequence);
1605 		break;
1606 	case 7:
1607 		num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n",
1608 				vdec->drain, vdec->eos_received, vdec->source_change);
1609 		break;
1610 	case 8:
1611 		num = scnprintf(str, size, "fps = %d/%d\n",
1612 				vdec->codec_info.frame_rate.numerator,
1613 				vdec->codec_info.frame_rate.denominator);
1614 		break;
1615 	case 9:
1616 		num = scnprintf(str, size, "colorspace: %d, %d, %d, %d (%d)\n",
1617 				vdec->codec_info.color_primaries,
1618 				vdec->codec_info.transfer_chars,
1619 				vdec->codec_info.matrix_coeffs,
1620 				vdec->codec_info.full_range,
1621 				vdec->codec_info.vui_present);
1622 		break;
1623 	default:
1624 		break;
1625 	}
1626 
1627 	return num;
1628 }
1629 
1630 static struct vpu_inst_ops vdec_inst_ops = {
1631 	.ctrl_init = vdec_ctrl_init,
1632 	.check_ready = vdec_check_ready,
1633 	.buf_done = vdec_buf_done,
1634 	.get_one_frame = vdec_frame_decoded,
1635 	.stop_done = vdec_stop_done,
1636 	.event_notify = vdec_event_notify,
1637 	.release = vdec_release,
1638 	.cleanup = vdec_cleanup,
1639 	.start = vdec_start_session,
1640 	.stop = vdec_stop_session,
1641 	.process_output = vdec_process_output,
1642 	.process_capture = vdec_process_capture,
1643 	.on_queue_empty = vdec_on_queue_empty,
1644 	.get_debug_info = vdec_get_debug_info,
1645 	.wait_prepare = vpu_inst_unlock,
1646 	.wait_finish = vpu_inst_lock,
1647 };
1648 
1649 static void vdec_init(struct file *file)
1650 {
1651 	struct vpu_inst *inst = to_inst(file);
1652 	struct v4l2_format f;
1653 
1654 	memset(&f, 0, sizeof(f));
1655 	f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1656 	f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1657 	f.fmt.pix_mp.width = 1280;
1658 	f.fmt.pix_mp.height = 720;
1659 	f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1660 	vdec_s_fmt(file, &inst->fh, &f);
1661 
1662 	memset(&f, 0, sizeof(f));
1663 	f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1664 	f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M_8L128;
1665 	f.fmt.pix_mp.width = 1280;
1666 	f.fmt.pix_mp.height = 720;
1667 	f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1668 	vdec_s_fmt(file, &inst->fh, &f);
1669 }
1670 
1671 static int vdec_open(struct file *file)
1672 {
1673 	struct vpu_inst *inst;
1674 	struct vdec_t *vdec;
1675 	int ret;
1676 
1677 	inst = vzalloc(sizeof(*inst));
1678 	if (!inst)
1679 		return -ENOMEM;
1680 
1681 	vdec = vzalloc(sizeof(*vdec));
1682 	if (!vdec) {
1683 		vfree(inst);
1684 		return -ENOMEM;
1685 	}
1686 
1687 	inst->ops = &vdec_inst_ops;
1688 	inst->formats = vdec_formats;
1689 	inst->type = VPU_CORE_TYPE_DEC;
1690 	inst->priv = vdec;
1691 
1692 	ret = vpu_v4l2_open(file, inst);
1693 	if (ret)
1694 		return ret;
1695 
1696 	vdec->fixed_fmt = false;
1697 	vdec->state = VPU_CODEC_STATE_ACTIVE;
1698 	inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1699 	inst->min_buffer_out = VDEC_MIN_BUFFER_OUT;
1700 	vdec_init(file);
1701 
1702 	return 0;
1703 }
1704 
1705 static const struct v4l2_file_operations vdec_fops = {
1706 	.owner = THIS_MODULE,
1707 	.open = vdec_open,
1708 	.release = vpu_v4l2_close,
1709 	.unlocked_ioctl = video_ioctl2,
1710 	.poll = v4l2_m2m_fop_poll,
1711 	.mmap = v4l2_m2m_fop_mmap,
1712 };
1713 
1714 const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void)
1715 {
1716 	return &vdec_ioctl_ops;
1717 }
1718 
1719 const struct v4l2_file_operations *vdec_get_fops(void)
1720 {
1721 	return &vdec_fops;
1722 }
1723