xref: /linux/drivers/media/platform/qcom/iris/iris_venc.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2022-2025 Qualcomm Innovation Center, Inc. All rights reserved.
4  */
5 
6 #include <media/v4l2-event.h>
7 #include <media/v4l2-mem2mem.h>
8 
9 #include "iris_buffer.h"
10 #include "iris_common.h"
11 #include "iris_ctrls.h"
12 #include "iris_instance.h"
13 #include "iris_power.h"
14 #include "iris_venc.h"
15 #include "iris_vpu_buffer.h"
16 
17 int iris_venc_inst_init(struct iris_inst *inst)
18 {
19 	struct iris_core *core = inst->core;
20 	struct v4l2_format *f;
21 
22 	inst->fmt_src = kzalloc_obj(*inst->fmt_src);
23 	inst->fmt_dst = kzalloc_obj(*inst->fmt_dst);
24 	if (!inst->fmt_src || !inst->fmt_dst) {
25 		kfree(inst->fmt_src);
26 		kfree(inst->fmt_dst);
27 		return -ENOMEM;
28 	}
29 
30 	f = inst->fmt_dst;
31 	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
32 	f->fmt.pix_mp.width = DEFAULT_WIDTH;
33 	f->fmt.pix_mp.height = DEFAULT_HEIGHT;
34 	f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
35 	inst->codec = f->fmt.pix_mp.pixelformat;
36 	f->fmt.pix_mp.num_planes = 1;
37 	f->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
38 	f->fmt.pix_mp.plane_fmt[0].sizeimage = iris_get_buffer_size(inst, BUF_OUTPUT);
39 	f->fmt.pix_mp.field = V4L2_FIELD_NONE;
40 	f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
41 	f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
42 	f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
43 	f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
44 	inst->buffers[BUF_OUTPUT].min_count = iris_vpu_buf_count(inst, BUF_OUTPUT);
45 	inst->buffers[BUF_OUTPUT].size = f->fmt.pix_mp.plane_fmt[0].sizeimage;
46 
47 	f = inst->fmt_src;
48 	f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
49 	f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12;
50 	f->fmt.pix_mp.width = ALIGN(DEFAULT_WIDTH, 128);
51 	f->fmt.pix_mp.height = ALIGN(DEFAULT_HEIGHT, 32);
52 	f->fmt.pix_mp.num_planes = 1;
53 	f->fmt.pix_mp.plane_fmt[0].bytesperline = ALIGN(DEFAULT_WIDTH, 128);
54 	f->fmt.pix_mp.plane_fmt[0].sizeimage = iris_get_buffer_size(inst, BUF_INPUT);
55 	f->fmt.pix_mp.field = V4L2_FIELD_NONE;
56 	f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
57 	f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
58 	f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
59 	f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
60 	inst->buffers[BUF_INPUT].min_count = iris_vpu_buf_count(inst, BUF_INPUT);
61 	inst->buffers[BUF_INPUT].size = f->fmt.pix_mp.plane_fmt[0].sizeimage;
62 
63 	inst->crop.left = 0;
64 	inst->crop.top = 0;
65 	inst->crop.width = DEFAULT_WIDTH;
66 	inst->crop.height = DEFAULT_HEIGHT;
67 
68 	inst->operating_rate = DEFAULT_FPS;
69 	inst->frame_rate = DEFAULT_FPS;
70 
71 	inst->enc_raw_width = DEFAULT_WIDTH;
72 	inst->enc_raw_height = DEFAULT_HEIGHT;
73 	inst->enc_scale_width = DEFAULT_WIDTH;
74 	inst->enc_scale_height = DEFAULT_HEIGHT;
75 
76 	memcpy(&inst->fw_caps[0], &core->inst_fw_caps_enc[0],
77 	       INST_FW_CAP_MAX * sizeof(struct platform_inst_fw_cap));
78 
79 	return iris_ctrls_init(inst);
80 }
81 
82 static const u32 iris_venc_formats_cap[] = {
83 	[IRIS_FMT_H264] = V4L2_PIX_FMT_H264,
84 	[IRIS_FMT_HEVC] = V4L2_PIX_FMT_HEVC,
85 };
86 
87 static const u32 iris_venc_formats_out[] = {
88 	[IRIS_FMT_NV12] = V4L2_PIX_FMT_NV12,
89 	[IRIS_FMT_QC08C] = V4L2_PIX_FMT_QC08C,
90 };
91 
92 static bool check_format(struct iris_inst *inst, u32 pixfmt, u32 type)
93 {
94 	unsigned int size, i;
95 	const u32 *fmt;
96 
97 	switch (type) {
98 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
99 		fmt = iris_venc_formats_out;
100 		size = ARRAY_SIZE(iris_venc_formats_out);
101 		break;
102 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
103 		fmt = iris_venc_formats_cap;
104 		size = ARRAY_SIZE(iris_venc_formats_cap);
105 		break;
106 	default:
107 		return false;
108 	}
109 
110 	for (i = 0; i < size; i++) {
111 		if (fmt[i] == pixfmt)
112 			return true;
113 	}
114 
115 	return false;
116 }
117 
118 static u32 find_format_by_index(struct iris_inst *inst, u32 index, u32 type)
119 {
120 	unsigned int size;
121 	const u32 *fmt;
122 
123 	switch (type) {
124 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
125 		fmt = iris_venc_formats_out;
126 		size = ARRAY_SIZE(iris_venc_formats_out);
127 		break;
128 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
129 		fmt = iris_venc_formats_cap;
130 		size = ARRAY_SIZE(iris_venc_formats_cap);
131 		break;
132 	default:
133 		return 0;
134 	}
135 
136 	if (index >= size)
137 		return 0;
138 
139 	return fmt[index];
140 }
141 
142 int iris_venc_enum_fmt(struct iris_inst *inst, struct v4l2_fmtdesc *f)
143 {
144 	u32 fmt;
145 
146 	switch (f->type) {
147 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
148 		fmt = find_format_by_index(inst, f->index, f->type);
149 		if (!fmt)
150 			return -EINVAL;
151 
152 		f->pixelformat = fmt;
153 		break;
154 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
155 		fmt = find_format_by_index(inst, f->index, f->type);
156 		if (!fmt)
157 			return -EINVAL;
158 
159 		f->pixelformat = fmt;
160 		f->flags = V4L2_FMT_FLAG_COMPRESSED | V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL;
161 		break;
162 	default:
163 		return -EINVAL;
164 	}
165 
166 	return 0;
167 }
168 
169 int iris_venc_try_fmt(struct iris_inst *inst, struct v4l2_format *f)
170 {
171 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
172 	struct v4l2_format *f_inst;
173 	bool supported;
174 
175 	memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
176 	supported = check_format(inst, pixmp->pixelformat, f->type);
177 	switch (f->type) {
178 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
179 		if (!supported) {
180 			f_inst = inst->fmt_src;
181 			f->fmt.pix_mp.width = f_inst->fmt.pix_mp.width;
182 			f->fmt.pix_mp.height = f_inst->fmt.pix_mp.height;
183 			f->fmt.pix_mp.pixelformat = f_inst->fmt.pix_mp.pixelformat;
184 		}
185 		break;
186 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
187 		if (!supported) {
188 			f_inst = inst->fmt_dst;
189 			f->fmt.pix_mp.width = f_inst->fmt.pix_mp.width;
190 			f->fmt.pix_mp.height = f_inst->fmt.pix_mp.height;
191 			f->fmt.pix_mp.pixelformat = f_inst->fmt.pix_mp.pixelformat;
192 		}
193 		break;
194 	default:
195 		return -EINVAL;
196 	}
197 
198 	if (pixmp->field == V4L2_FIELD_ANY)
199 		pixmp->field = V4L2_FIELD_NONE;
200 
201 	pixmp->num_planes = 1;
202 
203 	return 0;
204 }
205 
206 static int iris_venc_s_fmt_output(struct iris_inst *inst, struct v4l2_format *f)
207 {
208 	struct v4l2_format *fmt;
209 	u32 codec_align;
210 	bool supported;
211 
212 	iris_venc_try_fmt(inst, f);
213 
214 	supported = check_format(inst, f->fmt.pix_mp.pixelformat, f->type);
215 	if (!supported)
216 		return -EINVAL;
217 
218 	codec_align = (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC) ? 32 : 16;
219 
220 	fmt = inst->fmt_dst;
221 	fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
222 	/*
223 	 * If output format size != input format size,
224 	 * it is considered a scaling case,
225 	 * and the scaled size needs to be saved.
226 	 */
227 	if (f->fmt.pix_mp.width != inst->fmt_src->fmt.pix_mp.width ||
228 	    f->fmt.pix_mp.height != inst->fmt_src->fmt.pix_mp.height) {
229 		inst->enc_scale_width = f->fmt.pix_mp.width;
230 		inst->enc_scale_height = f->fmt.pix_mp.height;
231 		fmt->fmt.pix_mp.width = ALIGN(f->fmt.pix_mp.width, codec_align);
232 		fmt->fmt.pix_mp.height = ALIGN(f->fmt.pix_mp.height, codec_align);
233 	}
234 	fmt->fmt.pix_mp.num_planes = 1;
235 	fmt->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
236 	fmt->fmt.pix_mp.plane_fmt[0].sizeimage = iris_get_buffer_size(inst, BUF_OUTPUT);
237 
238 	if (f->fmt.pix_mp.colorspace != V4L2_COLORSPACE_DEFAULT &&
239 	    f->fmt.pix_mp.colorspace != V4L2_COLORSPACE_REC709)
240 		f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
241 	fmt->fmt.pix_mp.colorspace = f->fmt.pix_mp.colorspace;
242 	fmt->fmt.pix_mp.xfer_func = f->fmt.pix_mp.xfer_func;
243 	fmt->fmt.pix_mp.ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
244 	fmt->fmt.pix_mp.quantization = f->fmt.pix_mp.quantization;
245 
246 	inst->buffers[BUF_OUTPUT].min_count = iris_vpu_buf_count(inst, BUF_OUTPUT);
247 	inst->buffers[BUF_OUTPUT].size = fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
248 	fmt->fmt.pix_mp.pixelformat = f->fmt.pix_mp.pixelformat;
249 	inst->codec = f->fmt.pix_mp.pixelformat;
250 	memcpy(f, fmt, sizeof(struct v4l2_format));
251 
252 	return 0;
253 }
254 
255 static int iris_venc_s_fmt_input(struct iris_inst *inst, struct v4l2_format *f)
256 {
257 	struct v4l2_format *fmt, *output_fmt;
258 
259 	iris_venc_try_fmt(inst, f);
260 
261 	if (!check_format(inst, f->fmt.pix_mp.pixelformat, f->type))
262 		return -EINVAL;
263 
264 	fmt = inst->fmt_src;
265 	fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
266 	fmt->fmt.pix_mp.width = ALIGN(f->fmt.pix_mp.width, 128);
267 	fmt->fmt.pix_mp.height = ALIGN(f->fmt.pix_mp.height, 32);
268 	fmt->fmt.pix_mp.num_planes = 1;
269 	fmt->fmt.pix_mp.pixelformat = f->fmt.pix_mp.pixelformat;
270 	fmt->fmt.pix_mp.plane_fmt[0].bytesperline = ALIGN(f->fmt.pix_mp.width, 128);
271 	fmt->fmt.pix_mp.plane_fmt[0].sizeimage = iris_get_buffer_size(inst, BUF_INPUT);
272 
273 	fmt->fmt.pix_mp.colorspace = f->fmt.pix_mp.colorspace;
274 	fmt->fmt.pix_mp.xfer_func = f->fmt.pix_mp.xfer_func;
275 	fmt->fmt.pix_mp.ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
276 	fmt->fmt.pix_mp.quantization = f->fmt.pix_mp.quantization;
277 
278 	output_fmt = inst->fmt_dst;
279 	output_fmt->fmt.pix_mp.width = fmt->fmt.pix_mp.width;
280 	output_fmt->fmt.pix_mp.height = fmt->fmt.pix_mp.height;
281 	output_fmt->fmt.pix_mp.colorspace = fmt->fmt.pix_mp.colorspace;
282 	output_fmt->fmt.pix_mp.xfer_func = fmt->fmt.pix_mp.xfer_func;
283 	output_fmt->fmt.pix_mp.ycbcr_enc = fmt->fmt.pix_mp.ycbcr_enc;
284 	output_fmt->fmt.pix_mp.quantization = fmt->fmt.pix_mp.quantization;
285 
286 	inst->buffers[BUF_INPUT].min_count = iris_vpu_buf_count(inst, BUF_INPUT);
287 	inst->buffers[BUF_INPUT].size = fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
288 
289 	inst->enc_raw_width = f->fmt.pix_mp.width;
290 	inst->enc_raw_height = f->fmt.pix_mp.height;
291 	inst->enc_scale_width = f->fmt.pix_mp.width;
292 	inst->enc_scale_height = f->fmt.pix_mp.height;
293 
294 	if (f->fmt.pix_mp.width != inst->crop.width ||
295 	    f->fmt.pix_mp.height != inst->crop.height) {
296 		inst->crop.top = 0;
297 		inst->crop.left = 0;
298 		inst->crop.width = fmt->fmt.pix_mp.width;
299 		inst->crop.height = fmt->fmt.pix_mp.height;
300 
301 		iris_venc_s_fmt_output(inst, output_fmt);
302 	}
303 
304 	memcpy(f, fmt, sizeof(struct v4l2_format));
305 
306 	return 0;
307 }
308 
309 int iris_venc_s_fmt(struct iris_inst *inst, struct v4l2_format *f)
310 {
311 	struct vb2_queue *q;
312 
313 	q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type);
314 
315 	if (vb2_is_busy(q))
316 		return -EBUSY;
317 
318 	switch (f->type) {
319 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
320 		return iris_venc_s_fmt_input(inst, f);
321 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
322 		return iris_venc_s_fmt_output(inst, f);
323 	default:
324 		return -EINVAL;
325 	}
326 }
327 
328 int iris_venc_validate_format(struct iris_inst *inst, u32 pixelformat)
329 {
330 	bool supported;
331 
332 	supported = check_format(inst, pixelformat, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
333 	if (!supported)
334 		supported = check_format(inst, pixelformat, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
335 
336 	return supported ? 0 : -EINVAL;
337 }
338 
339 int iris_venc_subscribe_event(struct iris_inst *inst,
340 			      const struct v4l2_event_subscription *sub)
341 {
342 	switch (sub->type) {
343 	case V4L2_EVENT_EOS:
344 		return v4l2_event_subscribe(&inst->fh, sub, 0, NULL);
345 	case V4L2_EVENT_CTRL:
346 		return v4l2_ctrl_subscribe_event(&inst->fh, sub);
347 	default:
348 		return -EINVAL;
349 	}
350 }
351 
352 int iris_venc_s_selection(struct iris_inst *inst, struct v4l2_selection *s)
353 {
354 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
355 		return -EINVAL;
356 
357 	switch (s->target) {
358 	case V4L2_SEL_TGT_CROP:
359 		s->r.left = 0;
360 		s->r.top = 0;
361 
362 		if (s->r.width > inst->fmt_src->fmt.pix_mp.width ||
363 		    s->r.height > inst->fmt_src->fmt.pix_mp.height)
364 			return -EINVAL;
365 
366 		inst->crop.left = s->r.left;
367 		inst->crop.top = s->r.top;
368 		inst->crop.width = s->r.width;
369 		inst->crop.height = s->r.height;
370 		inst->fmt_dst->fmt.pix_mp.width = inst->crop.width;
371 		inst->fmt_dst->fmt.pix_mp.height = inst->crop.height;
372 		return iris_venc_s_fmt_output(inst, inst->fmt_dst);
373 	default:
374 		return -EINVAL;
375 	}
376 }
377 
378 int iris_venc_s_param(struct iris_inst *inst, struct v4l2_streamparm *s_parm)
379 {
380 	struct platform_inst_caps *caps = inst->core->iris_platform_data->inst_caps;
381 	struct vb2_queue *src_q = v4l2_m2m_get_src_vq(inst->m2m_ctx);
382 	struct vb2_queue *dst_q = v4l2_m2m_get_dst_vq(inst->m2m_ctx);
383 	struct v4l2_fract *timeperframe = NULL;
384 	u32 default_rate = DEFAULT_FPS;
385 	bool is_frame_rate = false;
386 	u32 fps, max_rate;
387 
388 	int ret = 0;
389 
390 	if (s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
391 		timeperframe = &s_parm->parm.output.timeperframe;
392 		max_rate = caps->max_operating_rate;
393 		s_parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
394 	} else {
395 		timeperframe = &s_parm->parm.capture.timeperframe;
396 		is_frame_rate = true;
397 		max_rate = caps->max_frame_rate;
398 		s_parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
399 	}
400 
401 	if (!timeperframe->denominator || !timeperframe->numerator) {
402 		if (!timeperframe->numerator)
403 			timeperframe->numerator = 1;
404 		if (!timeperframe->denominator)
405 			timeperframe->denominator = default_rate;
406 	}
407 
408 	fps = timeperframe->denominator / timeperframe->numerator;
409 	if (!fps)
410 		return -EINVAL;
411 
412 	if (fps > max_rate) {
413 		ret = -ENOMEM;
414 		goto reset_rate;
415 	}
416 
417 	if (is_frame_rate)
418 		inst->frame_rate = fps;
419 	else
420 		inst->operating_rate = fps;
421 
422 	if ((s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && vb2_is_streaming(src_q)) ||
423 	    (s_parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && vb2_is_streaming(dst_q))) {
424 		ret = iris_check_core_mbpf(inst);
425 		if (ret)
426 			goto reset_rate;
427 		ret = iris_check_core_mbps(inst);
428 		if (ret)
429 			goto reset_rate;
430 	}
431 
432 	return 0;
433 
434 reset_rate:
435 	if (ret) {
436 		if (is_frame_rate)
437 			inst->frame_rate = default_rate;
438 		else
439 			inst->operating_rate = default_rate;
440 	}
441 
442 	return ret;
443 }
444 
445 int iris_venc_g_param(struct iris_inst *inst, struct v4l2_streamparm *s_parm)
446 {
447 	struct v4l2_fract *timeperframe = NULL;
448 
449 	if (s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
450 		timeperframe = &s_parm->parm.output.timeperframe;
451 		timeperframe->numerator = 1;
452 		timeperframe->denominator = inst->operating_rate;
453 		s_parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
454 	} else {
455 		timeperframe = &s_parm->parm.capture.timeperframe;
456 		timeperframe->numerator = 1;
457 		timeperframe->denominator = inst->frame_rate;
458 		s_parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
459 	}
460 
461 	return 0;
462 }
463 
464 int iris_venc_streamon_input(struct iris_inst *inst)
465 {
466 	int ret;
467 
468 	ret = iris_set_properties(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
469 	if (ret)
470 		return ret;
471 
472 	ret = iris_alloc_and_queue_persist_bufs(inst, BUF_ARP);
473 	if (ret)
474 		return ret;
475 
476 	iris_get_internal_buffers(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
477 
478 	ret = iris_destroy_dequeued_internal_buffers(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
479 	if (ret)
480 		return ret;
481 
482 	ret = iris_create_internal_buffers(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
483 	if (ret)
484 		return ret;
485 
486 	ret = iris_queue_internal_buffers(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
487 	if (ret)
488 		return ret;
489 
490 	return iris_process_streamon_input(inst);
491 }
492 
493 int iris_venc_streamon_output(struct iris_inst *inst)
494 {
495 	int ret;
496 
497 	ret = iris_set_properties(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
498 	if (ret)
499 		goto error;
500 
501 	ret = iris_alloc_and_queue_persist_bufs(inst, BUF_ARP);
502 	if (ret)
503 		return ret;
504 
505 	iris_get_internal_buffers(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
506 
507 	ret = iris_destroy_dequeued_internal_buffers(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
508 	if (ret)
509 		goto error;
510 
511 	ret = iris_create_internal_buffers(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
512 	if (ret)
513 		goto error;
514 
515 	ret = iris_queue_internal_buffers(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
516 	if (ret)
517 		goto error;
518 
519 	ret = iris_process_streamon_output(inst);
520 	if (ret)
521 		goto error;
522 
523 	return ret;
524 
525 error:
526 	iris_session_streamoff(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
527 
528 	return ret;
529 }
530 
531 int iris_venc_qbuf(struct iris_inst *inst, struct vb2_v4l2_buffer *vbuf)
532 {
533 	struct iris_buffer *buf = to_iris_buffer(vbuf);
534 	struct vb2_buffer *vb2 = &vbuf->vb2_buf;
535 	struct vb2_queue *q;
536 	int ret;
537 
538 	ret = iris_vb2_buffer_to_driver(vb2, buf);
539 	if (ret)
540 		return ret;
541 
542 	if (buf->type == BUF_INPUT)
543 		iris_set_ts_metadata(inst, vbuf);
544 
545 	q = v4l2_m2m_get_vq(inst->m2m_ctx, vb2->type);
546 	if (!vb2_is_streaming(q)) {
547 		buf->attr |= BUF_ATTR_DEFERRED;
548 		return 0;
549 	}
550 
551 	iris_scale_power(inst);
552 
553 	return iris_queue_buffer(inst, buf);
554 }
555 
556 int iris_venc_start_cmd(struct iris_inst *inst)
557 {
558 	const struct iris_hfi_session_ops *hfi_ops = inst->hfi_session_ops;
559 	enum iris_inst_sub_state clear_sub_state = 0;
560 	struct vb2_queue *dst_vq;
561 	int ret;
562 
563 	dst_vq = v4l2_m2m_get_dst_vq(inst->m2m_ctx);
564 
565 	if (inst->sub_state & IRIS_INST_SUB_DRAIN &&
566 	    inst->sub_state & IRIS_INST_SUB_DRAIN_LAST) {
567 		vb2_clear_last_buffer_dequeued(dst_vq);
568 		clear_sub_state = IRIS_INST_SUB_DRAIN | IRIS_INST_SUB_DRAIN_LAST;
569 		if (inst->sub_state & IRIS_INST_SUB_INPUT_PAUSE) {
570 			if (hfi_ops->session_resume_drain) {
571 				ret = hfi_ops->session_resume_drain(inst,
572 					V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
573 				if (ret)
574 					return ret;
575 			}
576 			clear_sub_state |= IRIS_INST_SUB_INPUT_PAUSE;
577 		}
578 		if (inst->sub_state & IRIS_INST_SUB_OUTPUT_PAUSE) {
579 			if (hfi_ops->session_resume_drain) {
580 				ret = hfi_ops->session_resume_drain(inst,
581 					V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
582 				if (ret)
583 					return ret;
584 			}
585 			clear_sub_state |= IRIS_INST_SUB_OUTPUT_PAUSE;
586 		}
587 	} else {
588 		dev_err(inst->core->dev, "start called before receiving last_flag\n");
589 		iris_inst_change_state(inst, IRIS_INST_ERROR);
590 		return -EBUSY;
591 	}
592 
593 	inst->last_buffer_dequeued = false;
594 
595 	return iris_inst_change_sub_state(inst, clear_sub_state, 0);
596 }
597 
598 int iris_venc_stop_cmd(struct iris_inst *inst)
599 {
600 	const struct iris_hfi_session_ops *hfi_ops = inst->hfi_session_ops;
601 	int ret;
602 
603 	ret = hfi_ops->session_drain(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
604 	if (ret)
605 		return ret;
606 
607 	ret = iris_inst_change_sub_state(inst, 0, IRIS_INST_SUB_DRAIN);
608 
609 	iris_scale_power(inst);
610 
611 	return ret;
612 }
613