xref: /linux/drivers/media/test-drivers/visl/visl-dec.c (revision 24b10e5f8e0d2bee1a10fc67011ea5d936c1a389)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Contains the virtual decoder logic. The functions here control the
4  * tracing/TPG on a per-frame basis
5  */
6 
7 #include "visl.h"
8 #include "visl-debugfs.h"
9 #include "visl-dec.h"
10 #include "visl-trace-fwht.h"
11 #include "visl-trace-mpeg2.h"
12 #include "visl-trace-vp8.h"
13 #include "visl-trace-vp9.h"
14 #include "visl-trace-h264.h"
15 #include "visl-trace-hevc.h"
16 #include "visl-trace-av1.h"
17 
18 #include <linux/delay.h>
19 #include <linux/workqueue.h>
20 #include <media/v4l2-mem2mem.h>
21 #include <media/tpg/v4l2-tpg.h>
22 
23 #define LAST_BUF_IDX (V4L2_AV1_REF_LAST_FRAME - V4L2_AV1_REF_LAST_FRAME)
24 #define LAST2_BUF_IDX (V4L2_AV1_REF_LAST2_FRAME - V4L2_AV1_REF_LAST_FRAME)
25 #define LAST3_BUF_IDX (V4L2_AV1_REF_LAST3_FRAME - V4L2_AV1_REF_LAST_FRAME)
26 #define GOLDEN_BUF_IDX (V4L2_AV1_REF_GOLDEN_FRAME - V4L2_AV1_REF_LAST_FRAME)
27 #define BWD_BUF_IDX (V4L2_AV1_REF_BWDREF_FRAME - V4L2_AV1_REF_LAST_FRAME)
28 #define ALT2_BUF_IDX (V4L2_AV1_REF_ALTREF2_FRAME - V4L2_AV1_REF_LAST_FRAME)
29 #define ALT_BUF_IDX (V4L2_AV1_REF_ALTREF_FRAME - V4L2_AV1_REF_LAST_FRAME)
30 
31 static void *plane_vaddr(struct tpg_data *tpg, struct vb2_buffer *buf,
32 			 u32 p, u32 bpl[TPG_MAX_PLANES], u32 h)
33 {
34 	u32 i;
35 	void *vbuf;
36 
37 	if (p == 0 || tpg_g_buffers(tpg) > 1)
38 		return vb2_plane_vaddr(buf, p);
39 	vbuf = vb2_plane_vaddr(buf, 0);
40 	for (i = 0; i < p; i++)
41 		vbuf += bpl[i] * h / tpg->vdownsampling[i];
42 	return vbuf;
43 }
44 
45 static void visl_get_ref_frames(struct visl_ctx *ctx, u8 *buf,
46 				__kernel_size_t buflen, struct visl_run *run)
47 {
48 	struct vb2_queue *cap_q = &ctx->fh.m2m_ctx->cap_q_ctx.q;
49 	char header[] = "Reference frames:\n";
50 	u32 i;
51 	u32 len;
52 
53 	len = scnprintf(buf, buflen, header);
54 	buf += len;
55 	buflen -= len;
56 
57 	switch (ctx->current_codec) {
58 	case VISL_CODEC_NONE:
59 		break;
60 
61 	case VISL_CODEC_FWHT: {
62 		struct vb2_buffer *vb2_buf;
63 
64 		vb2_buf = vb2_find_buffer(cap_q, run->fwht.params->backward_ref_ts);
65 
66 		scnprintf(buf, buflen, "backwards_ref_ts: %lld, vb2_idx: %d",
67 			  run->fwht.params->backward_ref_ts,
68 			  vb2_buf ? vb2_buf->index : -1);
69 		break;
70 	}
71 
72 	case VISL_CODEC_MPEG2: {
73 		struct vb2_buffer *b_ref;
74 		struct vb2_buffer *f_ref;
75 
76 		b_ref = vb2_find_buffer(cap_q, run->mpeg2.pic->backward_ref_ts);
77 		f_ref = vb2_find_buffer(cap_q, run->mpeg2.pic->forward_ref_ts);
78 
79 		scnprintf(buf, buflen,
80 			  "backward_ref_ts: %llu, vb2_idx: %d\n"
81 			  "forward_ref_ts: %llu, vb2_idx: %d\n",
82 			  run->mpeg2.pic->backward_ref_ts,
83 			  b_ref ? b_ref->index : -1,
84 			  run->mpeg2.pic->forward_ref_ts,
85 			  f_ref ? f_ref->index : -1);
86 		break;
87 	}
88 
89 	case VISL_CODEC_VP8: {
90 		struct vb2_buffer *last;
91 		struct vb2_buffer *golden;
92 		struct vb2_buffer *alt;
93 
94 		last = vb2_find_buffer(cap_q, run->vp8.frame->last_frame_ts);
95 		golden = vb2_find_buffer(cap_q, run->vp8.frame->golden_frame_ts);
96 		alt = vb2_find_buffer(cap_q, run->vp8.frame->alt_frame_ts);
97 
98 		scnprintf(buf, buflen,
99 			  "last_ref_ts: %llu, vb2_idx: %d\n"
100 			  "golden_ref_ts: %llu, vb2_idx: %d\n"
101 			  "alt_ref_ts: %llu, vb2_idx: %d\n",
102 			  run->vp8.frame->last_frame_ts,
103 			  last ? last->index : -1,
104 			  run->vp8.frame->golden_frame_ts,
105 			  golden ? golden->index : -1,
106 			  run->vp8.frame->alt_frame_ts,
107 			  alt ? alt->index : -1);
108 		break;
109 	}
110 
111 	case VISL_CODEC_VP9: {
112 		struct vb2_buffer *last;
113 		struct vb2_buffer *golden;
114 		struct vb2_buffer *alt;
115 
116 		last = vb2_find_buffer(cap_q, run->vp9.frame->last_frame_ts);
117 		golden = vb2_find_buffer(cap_q, run->vp9.frame->golden_frame_ts);
118 		alt = vb2_find_buffer(cap_q, run->vp9.frame->alt_frame_ts);
119 
120 		scnprintf(buf, buflen,
121 			  "last_ref_ts: %llu, vb2_idx: %d\n"
122 			  "golden_ref_ts: %llu, vb2_idx: %d\n"
123 			  "alt_ref_ts: %llu, vb2_idx: %d\n",
124 			  run->vp9.frame->last_frame_ts,
125 			  last ? last->index : -1,
126 			  run->vp9.frame->golden_frame_ts,
127 			  golden ? golden->index : -1,
128 			  run->vp9.frame->alt_frame_ts,
129 			  alt ? alt->index : -1);
130 		break;
131 	}
132 
133 	case VISL_CODEC_H264: {
134 		char entry[] = "dpb[%d]:%u, vb2_index: %d\n";
135 		struct vb2_buffer *vb2_buf;
136 
137 		for (i = 0; i < ARRAY_SIZE(run->h264.dpram->dpb); i++) {
138 			vb2_buf = vb2_find_buffer(cap_q, run->h264.dpram->dpb[i].reference_ts);
139 			len = scnprintf(buf, buflen, entry, i,
140 					run->h264.dpram->dpb[i].reference_ts,
141 					vb2_buf ? vb2_buf->index : -1);
142 			buf += len;
143 			buflen -= len;
144 		}
145 
146 		break;
147 	}
148 
149 	case VISL_CODEC_HEVC: {
150 		char entry[] = "dpb[%d]:%u, vb2_index: %d\n";
151 		struct vb2_buffer *vb2_buf;
152 
153 		for (i = 0; i < ARRAY_SIZE(run->hevc.dpram->dpb); i++) {
154 			vb2_buf = vb2_find_buffer(cap_q, run->hevc.dpram->dpb[i].timestamp);
155 			len = scnprintf(buf, buflen, entry, i,
156 					run->hevc.dpram->dpb[i].timestamp,
157 					vb2_buf ? vb2_buf->index : -1);
158 			buf += len;
159 			buflen -= len;
160 		}
161 
162 		break;
163 	}
164 
165 	case VISL_CODEC_AV1: {
166 		int idx_last = run->av1.frame->ref_frame_idx[LAST_BUF_IDX];
167 		int idx_last2 = run->av1.frame->ref_frame_idx[LAST2_BUF_IDX];
168 		int idx_last3 = run->av1.frame->ref_frame_idx[LAST3_BUF_IDX];
169 		int idx_golden = run->av1.frame->ref_frame_idx[GOLDEN_BUF_IDX];
170 		int idx_bwd = run->av1.frame->ref_frame_idx[BWD_BUF_IDX];
171 		int idx_alt2 = run->av1.frame->ref_frame_idx[ALT2_BUF_IDX];
172 		int idx_alt = run->av1.frame->ref_frame_idx[ALT_BUF_IDX];
173 
174 		struct vb2_buffer *ref_last =
175 			vb2_find_buffer(cap_q, run->av1.frame->reference_frame_ts[idx_last]);
176 		struct vb2_buffer *ref_last2 =
177 			vb2_find_buffer(cap_q, run->av1.frame->reference_frame_ts[idx_last2]);
178 		struct vb2_buffer *ref_last3 =
179 			vb2_find_buffer(cap_q, run->av1.frame->reference_frame_ts[idx_last3]);
180 		struct vb2_buffer *ref_golden =
181 			vb2_find_buffer(cap_q, run->av1.frame->reference_frame_ts[idx_golden]);
182 		struct vb2_buffer *ref_bwd =
183 			vb2_find_buffer(cap_q, run->av1.frame->reference_frame_ts[idx_bwd]);
184 		struct vb2_buffer *ref_alt2 =
185 			vb2_find_buffer(cap_q, run->av1.frame->reference_frame_ts[idx_alt2]);
186 		struct vb2_buffer *ref_alt =
187 			vb2_find_buffer(cap_q, run->av1.frame->reference_frame_ts[idx_alt]);
188 
189 		scnprintf(buf, buflen,
190 			  "ref_last_ts: %llu, vb2_idx: %d\n"
191 			  "ref_last2_ts: %llu, vb2_idx: %d\n"
192 			  "ref_last3_ts: %llu, vb2_idx: %d\n"
193 			  "ref_golden_ts: %llu, vb2_idx: %d\n"
194 			  "ref_bwd_ts: %llu, vb2_idx: %d\n"
195 			  "ref_alt2_ts: %llu, vb2_idx: %d\n"
196 			  "ref_alt_ts: %llu, vb2_idx: %d\n",
197 			  run->av1.frame->reference_frame_ts[idx_last],
198 			  ref_last ? ref_last->index : -1,
199 			  run->av1.frame->reference_frame_ts[idx_last2],
200 			  ref_last2 ? ref_last2->index : -1,
201 			  run->av1.frame->reference_frame_ts[idx_last3],
202 			  ref_last3 ? ref_last3->index : -1,
203 			  run->av1.frame->reference_frame_ts[idx_golden],
204 			  ref_golden ? ref_golden->index : -1,
205 			  run->av1.frame->reference_frame_ts[idx_bwd],
206 			  ref_bwd ? ref_bwd->index : -1,
207 			  run->av1.frame->reference_frame_ts[idx_alt2],
208 			  ref_alt2 ? ref_alt2->index : -1,
209 			  run->av1.frame->reference_frame_ts[idx_alt],
210 			  ref_alt ? ref_alt->index : -1);
211 		break;
212 	}
213 	}
214 }
215 
216 static char *visl_get_vb2_state(enum vb2_buffer_state state)
217 {
218 	switch (state) {
219 	case VB2_BUF_STATE_DEQUEUED:
220 		return "Dequeued";
221 	case VB2_BUF_STATE_IN_REQUEST:
222 		return "In request";
223 	case VB2_BUF_STATE_PREPARING:
224 		return "Preparing";
225 	case VB2_BUF_STATE_QUEUED:
226 		return "Queued";
227 	case VB2_BUF_STATE_ACTIVE:
228 		return "Active";
229 	case VB2_BUF_STATE_DONE:
230 		return "Done";
231 	case VB2_BUF_STATE_ERROR:
232 		return "Error";
233 	default:
234 		return "";
235 	}
236 }
237 
238 static int visl_fill_bytesused(struct vb2_v4l2_buffer *v4l2_vb2_buf, char *buf, size_t bufsz)
239 {
240 	int len = 0;
241 	u32 i;
242 
243 	for (i = 0; i < v4l2_vb2_buf->vb2_buf.num_planes; i++)
244 		len += scnprintf(buf, bufsz,
245 				"bytesused[%u]: %u length[%u]: %u data_offset[%u]: %u",
246 				i, v4l2_vb2_buf->planes[i].bytesused,
247 				i, v4l2_vb2_buf->planes[i].length,
248 				i, v4l2_vb2_buf->planes[i].data_offset);
249 
250 	return len;
251 }
252 
253 static void visl_tpg_fill_sequence(struct visl_ctx *ctx,
254 				   struct visl_run *run, char buf[], size_t bufsz)
255 {
256 	u32 stream_ms;
257 
258 	stream_ms = jiffies_to_msecs(get_jiffies_64() - ctx->capture_streamon_jiffies);
259 
260 	scnprintf(buf, bufsz,
261 		  "stream time: %02d:%02d:%02d:%03d sequence:%u timestamp:%lld field:%s",
262 		  (stream_ms / (60 * 60 * 1000)) % 24,
263 		  (stream_ms / (60 * 1000)) % 60,
264 		  (stream_ms / 1000) % 60,
265 		  stream_ms % 1000,
266 		  run->dst->sequence,
267 		  run->dst->vb2_buf.timestamp,
268 		  (run->dst->field == V4L2_FIELD_ALTERNATE) ?
269 		  (run->dst->field == V4L2_FIELD_TOP ?
270 		  " top" : " bottom") : "none");
271 }
272 
273 static void visl_tpg_fill(struct visl_ctx *ctx, struct visl_run *run)
274 {
275 	u8 *basep[TPG_MAX_PLANES][2];
276 	char *buf = ctx->tpg_str_buf;
277 	char *tmp = buf;
278 	char *line_str;
279 	u32 line = 1;
280 	const u32 line_height = 16;
281 	u32 len;
282 	struct vb2_queue *out_q = &ctx->fh.m2m_ctx->out_q_ctx.q;
283 	struct vb2_queue *cap_q = &ctx->fh.m2m_ctx->cap_q_ctx.q;
284 	struct v4l2_pix_format_mplane *coded_fmt = &ctx->coded_fmt.fmt.pix_mp;
285 	struct v4l2_pix_format_mplane *decoded_fmt = &ctx->decoded_fmt.fmt.pix_mp;
286 	u32 p;
287 	u32 i;
288 
289 	for (p = 0; p < tpg_g_planes(&ctx->tpg); p++) {
290 		void *vbuf = plane_vaddr(&ctx->tpg,
291 					 &run->dst->vb2_buf, p,
292 					 ctx->tpg.bytesperline,
293 					 ctx->tpg.buf_height);
294 
295 		tpg_calc_text_basep(&ctx->tpg, basep, p, vbuf);
296 		tpg_fill_plane_buffer(&ctx->tpg, 0, p, vbuf);
297 	}
298 
299 	visl_tpg_fill_sequence(ctx, run, buf, TPG_STR_BUF_SZ);
300 	tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, buf);
301 	frame_dprintk(ctx->dev, run->dst->sequence, "%s\n", buf);
302 	frame_dprintk(ctx->dev, run->dst->sequence, "");
303 	line++;
304 
305 	visl_get_ref_frames(ctx, buf, TPG_STR_BUF_SZ, run);
306 
307 	while ((line_str = strsep(&tmp, "\n")) && strlen(line_str)) {
308 		tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, line_str);
309 		frame_dprintk(ctx->dev, run->dst->sequence, "%s\n", line_str);
310 	}
311 
312 	frame_dprintk(ctx->dev, run->dst->sequence, "");
313 	line++;
314 
315 	scnprintf(buf,
316 		  TPG_STR_BUF_SZ,
317 		  "OUTPUT pixelformat: %c%c%c%c, resolution: %dx%d, num_planes: %d",
318 		  coded_fmt->pixelformat,
319 		  (coded_fmt->pixelformat >> 8) & 0xff,
320 		  (coded_fmt->pixelformat >> 16) & 0xff,
321 		  (coded_fmt->pixelformat >> 24) & 0xff,
322 		  coded_fmt->width,
323 		  coded_fmt->height,
324 		  coded_fmt->num_planes);
325 
326 	tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, buf);
327 	frame_dprintk(ctx->dev, run->dst->sequence, "%s\n", buf);
328 
329 	for (i = 0; i < coded_fmt->num_planes; i++) {
330 		scnprintf(buf,
331 			  TPG_STR_BUF_SZ,
332 			  "plane[%d]: bytesperline: %d, sizeimage: %d",
333 			  i,
334 			  coded_fmt->plane_fmt[i].bytesperline,
335 			  coded_fmt->plane_fmt[i].sizeimage);
336 
337 		tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, buf);
338 		frame_dprintk(ctx->dev, run->dst->sequence, "%s\n", buf);
339 	}
340 
341 	line++;
342 	frame_dprintk(ctx->dev, run->dst->sequence, "");
343 	scnprintf(buf, TPG_STR_BUF_SZ, "Output queue status:");
344 	tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, buf);
345 	frame_dprintk(ctx->dev, run->dst->sequence, "%s\n", buf);
346 
347 	len = 0;
348 	for (i = 0; i < vb2_get_num_buffers(out_q); i++) {
349 		char entry[] = "index: %u, state: %s, request_fd: %d, ";
350 		u32 old_len = len;
351 		struct vb2_buffer *vb2;
352 		char *q_status;
353 
354 		vb2 = vb2_get_buffer(out_q, i);
355 		if (!vb2)
356 			continue;
357 
358 		q_status = visl_get_vb2_state(vb2->state);
359 
360 		len += scnprintf(&buf[len], TPG_STR_BUF_SZ - len,
361 				 entry, i, q_status,
362 				 to_vb2_v4l2_buffer(vb2)->request_fd);
363 
364 		len += visl_fill_bytesused(to_vb2_v4l2_buffer(vb2),
365 					   &buf[len],
366 					   TPG_STR_BUF_SZ - len);
367 
368 		tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, &buf[old_len]);
369 		frame_dprintk(ctx->dev, run->dst->sequence, "%s", &buf[old_len]);
370 	}
371 
372 	line++;
373 	frame_dprintk(ctx->dev, run->dst->sequence, "");
374 
375 	scnprintf(buf,
376 		  TPG_STR_BUF_SZ,
377 		  "CAPTURE pixelformat: %c%c%c%c, resolution: %dx%d, num_planes: %d",
378 		  decoded_fmt->pixelformat,
379 		  (decoded_fmt->pixelformat >> 8) & 0xff,
380 		  (decoded_fmt->pixelformat >> 16) & 0xff,
381 		  (decoded_fmt->pixelformat >> 24) & 0xff,
382 		  decoded_fmt->width,
383 		  decoded_fmt->height,
384 		  decoded_fmt->num_planes);
385 
386 	tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, buf);
387 	frame_dprintk(ctx->dev, run->dst->sequence, "%s\n", buf);
388 
389 	for (i = 0; i < decoded_fmt->num_planes; i++) {
390 		scnprintf(buf,
391 			  TPG_STR_BUF_SZ,
392 			  "plane[%d]: bytesperline: %d, sizeimage: %d",
393 			  i,
394 			  decoded_fmt->plane_fmt[i].bytesperline,
395 			  decoded_fmt->plane_fmt[i].sizeimage);
396 
397 		tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, buf);
398 		frame_dprintk(ctx->dev, run->dst->sequence, "%s\n", buf);
399 	}
400 
401 	line++;
402 	frame_dprintk(ctx->dev, run->dst->sequence, "");
403 	scnprintf(buf, TPG_STR_BUF_SZ, "Capture queue status:");
404 	tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, buf);
405 	frame_dprintk(ctx->dev, run->dst->sequence, "%s\n", buf);
406 
407 	len = 0;
408 	for (i = 0; i < vb2_get_num_buffers(cap_q); i++) {
409 		u32 old_len = len;
410 		struct vb2_buffer *vb2;
411 		char *q_status;
412 
413 		vb2 = vb2_get_buffer(cap_q, i);
414 		if (!vb2)
415 			continue;
416 
417 		q_status = visl_get_vb2_state(vb2->state);
418 
419 		len += scnprintf(&buf[len], TPG_STR_BUF_SZ - len,
420 				 "index: %u, status: %s, timestamp: %llu, is_held: %d",
421 				 vb2->index, q_status,
422 				 vb2->timestamp,
423 				 to_vb2_v4l2_buffer(vb2)->is_held);
424 
425 		tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, &buf[old_len]);
426 		frame_dprintk(ctx->dev, run->dst->sequence, "%s", &buf[old_len]);
427 	}
428 }
429 
430 static void visl_trace_ctrls(struct visl_ctx *ctx, struct visl_run *run)
431 {
432 	int i;
433 
434 	switch (ctx->current_codec) {
435 	default:
436 	case VISL_CODEC_NONE:
437 		break;
438 	case VISL_CODEC_FWHT:
439 		trace_v4l2_ctrl_fwht_params(run->fwht.params);
440 		break;
441 	case VISL_CODEC_MPEG2:
442 		trace_v4l2_ctrl_mpeg2_sequence(run->mpeg2.seq);
443 		trace_v4l2_ctrl_mpeg2_picture(run->mpeg2.pic);
444 		trace_v4l2_ctrl_mpeg2_quantisation(run->mpeg2.quant);
445 		break;
446 	case VISL_CODEC_VP8:
447 		trace_v4l2_ctrl_vp8_frame(run->vp8.frame);
448 		trace_v4l2_ctrl_vp8_entropy(run->vp8.frame);
449 		break;
450 	case VISL_CODEC_VP9:
451 		trace_v4l2_ctrl_vp9_frame(run->vp9.frame);
452 		trace_v4l2_ctrl_vp9_compressed_hdr(run->vp9.probs);
453 		trace_v4l2_ctrl_vp9_compressed_coeff(run->vp9.probs);
454 		trace_v4l2_vp9_mv_probs(&run->vp9.probs->mv);
455 		break;
456 	case VISL_CODEC_H264:
457 		trace_v4l2_ctrl_h264_sps(run->h264.sps);
458 		trace_v4l2_ctrl_h264_pps(run->h264.pps);
459 		trace_v4l2_ctrl_h264_scaling_matrix(run->h264.sm);
460 		trace_v4l2_ctrl_h264_slice_params(run->h264.spram);
461 
462 		for (i = 0; i < ARRAY_SIZE(run->h264.spram->ref_pic_list0); i++)
463 			trace_v4l2_h264_ref_pic_list0(&run->h264.spram->ref_pic_list0[i], i);
464 		for (i = 0; i < ARRAY_SIZE(run->h264.spram->ref_pic_list0); i++)
465 			trace_v4l2_h264_ref_pic_list1(&run->h264.spram->ref_pic_list1[i], i);
466 
467 		trace_v4l2_ctrl_h264_decode_params(run->h264.dpram);
468 
469 		for (i = 0; i < ARRAY_SIZE(run->h264.dpram->dpb); i++)
470 			trace_v4l2_h264_dpb_entry(&run->h264.dpram->dpb[i], i);
471 
472 		trace_v4l2_ctrl_h264_pred_weights(run->h264.pwht);
473 		break;
474 	case VISL_CODEC_HEVC:
475 		trace_v4l2_ctrl_hevc_sps(run->hevc.sps);
476 		trace_v4l2_ctrl_hevc_pps(run->hevc.pps);
477 		trace_v4l2_ctrl_hevc_slice_params(run->hevc.spram);
478 		trace_v4l2_ctrl_hevc_scaling_matrix(run->hevc.sm);
479 		trace_v4l2_ctrl_hevc_decode_params(run->hevc.dpram);
480 
481 		for (i = 0; i < ARRAY_SIZE(run->hevc.dpram->dpb); i++)
482 			trace_v4l2_hevc_dpb_entry(&run->hevc.dpram->dpb[i]);
483 
484 		trace_v4l2_hevc_pred_weight_table(&run->hevc.spram->pred_weight_table);
485 		break;
486 	case VISL_CODEC_AV1:
487 		trace_v4l2_ctrl_av1_sequence(run->av1.seq);
488 		trace_v4l2_ctrl_av1_frame(run->av1.frame);
489 		trace_v4l2_ctrl_av1_film_grain(run->av1.grain);
490 		trace_v4l2_ctrl_av1_tile_group_entry(run->av1.tge);
491 		break;
492 	}
493 }
494 
495 void visl_device_run(void *priv)
496 {
497 	struct visl_ctx *ctx = priv;
498 	struct visl_run run = {};
499 	struct media_request *src_req;
500 
501 	run.src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
502 	run.dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
503 
504 	/* Apply request(s) controls if needed. */
505 	src_req = run.src->vb2_buf.req_obj.req;
506 
507 	if (src_req)
508 		v4l2_ctrl_request_setup(src_req, &ctx->hdl);
509 
510 	v4l2_m2m_buf_copy_metadata(run.src, run.dst, true);
511 	run.dst->sequence = ctx->q_data[V4L2_M2M_DST].sequence++;
512 	run.src->sequence = ctx->q_data[V4L2_M2M_SRC].sequence++;
513 	run.dst->field = ctx->decoded_fmt.fmt.pix.field;
514 
515 	switch (ctx->current_codec) {
516 	default:
517 	case VISL_CODEC_NONE:
518 		break;
519 	case VISL_CODEC_FWHT:
520 		run.fwht.params = visl_find_control_data(ctx, V4L2_CID_STATELESS_FWHT_PARAMS);
521 		break;
522 	case VISL_CODEC_MPEG2:
523 		run.mpeg2.seq = visl_find_control_data(ctx, V4L2_CID_STATELESS_MPEG2_SEQUENCE);
524 		run.mpeg2.pic = visl_find_control_data(ctx, V4L2_CID_STATELESS_MPEG2_PICTURE);
525 		run.mpeg2.quant = visl_find_control_data(ctx,
526 							 V4L2_CID_STATELESS_MPEG2_QUANTISATION);
527 		break;
528 	case VISL_CODEC_VP8:
529 		run.vp8.frame = visl_find_control_data(ctx, V4L2_CID_STATELESS_VP8_FRAME);
530 		break;
531 	case VISL_CODEC_VP9:
532 		run.vp9.frame = visl_find_control_data(ctx, V4L2_CID_STATELESS_VP9_FRAME);
533 		run.vp9.probs = visl_find_control_data(ctx, V4L2_CID_STATELESS_VP9_COMPRESSED_HDR);
534 		break;
535 	case VISL_CODEC_H264:
536 		run.h264.sps = visl_find_control_data(ctx, V4L2_CID_STATELESS_H264_SPS);
537 		run.h264.pps = visl_find_control_data(ctx, V4L2_CID_STATELESS_H264_PPS);
538 		run.h264.sm = visl_find_control_data(ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
539 		run.h264.spram = visl_find_control_data(ctx, V4L2_CID_STATELESS_H264_SLICE_PARAMS);
540 		run.h264.dpram = visl_find_control_data(ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
541 		run.h264.pwht = visl_find_control_data(ctx, V4L2_CID_STATELESS_H264_PRED_WEIGHTS);
542 		break;
543 	case VISL_CODEC_HEVC:
544 		run.hevc.sps = visl_find_control_data(ctx, V4L2_CID_STATELESS_HEVC_SPS);
545 		run.hevc.pps = visl_find_control_data(ctx, V4L2_CID_STATELESS_HEVC_PPS);
546 		run.hevc.spram = visl_find_control_data(ctx, V4L2_CID_STATELESS_HEVC_SLICE_PARAMS);
547 		run.hevc.sm = visl_find_control_data(ctx, V4L2_CID_STATELESS_HEVC_SCALING_MATRIX);
548 		run.hevc.dpram = visl_find_control_data(ctx, V4L2_CID_STATELESS_HEVC_DECODE_PARAMS);
549 		break;
550 	case VISL_CODEC_AV1:
551 		run.av1.seq = visl_find_control_data(ctx, V4L2_CID_STATELESS_AV1_SEQUENCE);
552 		run.av1.frame = visl_find_control_data(ctx, V4L2_CID_STATELESS_AV1_FRAME);
553 		run.av1.tge = visl_find_control_data(ctx, V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY);
554 		run.av1.grain = visl_find_control_data(ctx, V4L2_CID_STATELESS_AV1_FILM_GRAIN);
555 		break;
556 	}
557 
558 	frame_dprintk(ctx->dev, run.dst->sequence,
559 		      "Got OUTPUT buffer sequence %d, timestamp %llu\n",
560 		      run.src->sequence, run.src->vb2_buf.timestamp);
561 
562 	frame_dprintk(ctx->dev, run.dst->sequence,
563 		      "Got CAPTURE buffer sequence %d, timestamp %llu\n",
564 		      run.dst->sequence, run.dst->vb2_buf.timestamp);
565 
566 	visl_tpg_fill(ctx, &run);
567 	visl_trace_ctrls(ctx, &run);
568 
569 	if (bitstream_trace_frame_start > -1 &&
570 	    run.dst->sequence >= bitstream_trace_frame_start &&
571 	    run.dst->sequence < bitstream_trace_frame_start + bitstream_trace_nframes)
572 		visl_trace_bitstream(ctx, &run);
573 
574 	/* Complete request(s) controls if needed. */
575 	if (src_req)
576 		v4l2_ctrl_request_complete(src_req, &ctx->hdl);
577 
578 	if (visl_transtime_ms)
579 		usleep_range(visl_transtime_ms * 1000, 2 * visl_transtime_ms * 1000);
580 
581 	v4l2_m2m_buf_done_and_job_finish(ctx->dev->m2m_dev,
582 					 ctx->fh.m2m_ctx, VB2_BUF_STATE_DONE);
583 }
584