1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Hantro VPU codec driver
4 *
5 * Copyright (C) 2018 Collabora, Ltd.
6 * Copyright (C) 2018 Rockchip Electronics Co., Ltd.
7 * Alpha Lin <[email protected]>
8 * Jeffy Chen <[email protected]>
9 *
10 * Copyright 2018 Google LLC.
11 * Tomasz Figa <[email protected]>
12 *
13 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
14 * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
15 */
16
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/videodev2.h>
22 #include <linux/workqueue.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-mem2mem.h>
26
27 #include "hantro.h"
28 #include "hantro_hw.h"
29 #include "hantro_v4l2.h"
30
31 #define HANTRO_DEFAULT_BIT_DEPTH 8
32
33 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
34 struct v4l2_pix_format_mplane *pix_mp,
35 bool need_postproc);
36 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
37 struct v4l2_pix_format_mplane *pix_mp);
38
39 static const struct hantro_fmt *
hantro_get_formats(const struct hantro_ctx * ctx,unsigned int * num_fmts,bool need_postproc)40 hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts, bool need_postproc)
41 {
42 const struct hantro_fmt *formats;
43
44 if (need_postproc) {
45 *num_fmts = 0;
46 return NULL;
47 }
48
49 if (ctx->is_encoder) {
50 formats = ctx->dev->variant->enc_fmts;
51 *num_fmts = ctx->dev->variant->num_enc_fmts;
52 } else {
53 formats = ctx->dev->variant->dec_fmts;
54 *num_fmts = ctx->dev->variant->num_dec_fmts;
55 }
56
57 return formats;
58 }
59
60 static const struct hantro_fmt *
hantro_get_postproc_formats(const struct hantro_ctx * ctx,unsigned int * num_fmts)61 hantro_get_postproc_formats(const struct hantro_ctx *ctx,
62 unsigned int *num_fmts)
63 {
64 struct hantro_dev *vpu = ctx->dev;
65
66 if (ctx->is_encoder || !vpu->variant->postproc_fmts) {
67 *num_fmts = 0;
68 return NULL;
69 }
70
71 *num_fmts = ctx->dev->variant->num_postproc_fmts;
72 return ctx->dev->variant->postproc_fmts;
73 }
74
hantro_get_format_depth(u32 fourcc)75 int hantro_get_format_depth(u32 fourcc)
76 {
77 switch (fourcc) {
78 case V4L2_PIX_FMT_P010:
79 case V4L2_PIX_FMT_P010_4L4:
80 case V4L2_PIX_FMT_NV15_4L4:
81 return 10;
82 default:
83 return 8;
84 }
85 }
86
87 static bool
hantro_check_depth_match(const struct hantro_fmt * fmt,int bit_depth)88 hantro_check_depth_match(const struct hantro_fmt *fmt, int bit_depth)
89 {
90 int fmt_depth;
91
92 if (!fmt->match_depth && !fmt->postprocessed)
93 return true;
94
95 /* 0 means default depth, which is 8 */
96 if (!bit_depth)
97 bit_depth = HANTRO_DEFAULT_BIT_DEPTH;
98
99 fmt_depth = hantro_get_format_depth(fmt->fourcc);
100
101 /*
102 * Allow only downconversion for postproc formats for now.
103 * It may be possible to relax that on some HW.
104 */
105 if (!fmt->match_depth)
106 return fmt_depth <= bit_depth;
107
108 return fmt_depth == bit_depth;
109 }
110
111 static const struct hantro_fmt *
hantro_find_format(const struct hantro_ctx * ctx,u32 fourcc)112 hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc)
113 {
114 const struct hantro_fmt *formats;
115 unsigned int i, num_fmts;
116
117 formats = hantro_get_formats(ctx, &num_fmts, HANTRO_AUTO_POSTPROC);
118 for (i = 0; i < num_fmts; i++)
119 if (formats[i].fourcc == fourcc)
120 return &formats[i];
121
122 formats = hantro_get_postproc_formats(ctx, &num_fmts);
123 for (i = 0; i < num_fmts; i++)
124 if (formats[i].fourcc == fourcc)
125 return &formats[i];
126 return NULL;
127 }
128
129 static int
hantro_set_reference_frames_format(struct hantro_ctx * ctx)130 hantro_set_reference_frames_format(struct hantro_ctx *ctx)
131 {
132 const struct hantro_fmt *fmt;
133 int dst_bit_depth = hantro_get_format_depth(ctx->vpu_dst_fmt->fourcc);
134
135 fmt = hantro_get_default_fmt(ctx, false, dst_bit_depth, HANTRO_AUTO_POSTPROC);
136 if (!fmt)
137 return -EINVAL;
138
139 ctx->ref_fmt.width = ctx->src_fmt.width;
140 ctx->ref_fmt.height = ctx->src_fmt.height;
141
142 v4l2_apply_frmsize_constraints(&ctx->ref_fmt.width, &ctx->ref_fmt.height, &fmt->frmsize);
143 return v4l2_fill_pixfmt_mp(&ctx->ref_fmt, fmt->fourcc,
144 ctx->ref_fmt.width, ctx->ref_fmt.height);
145 }
146
147 const struct hantro_fmt *
hantro_get_default_fmt(const struct hantro_ctx * ctx,bool bitstream,int bit_depth,bool need_postproc)148 hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream,
149 int bit_depth, bool need_postproc)
150 {
151 const struct hantro_fmt *formats;
152 unsigned int i, num_fmts;
153
154 formats = hantro_get_formats(ctx, &num_fmts, need_postproc);
155 for (i = 0; i < num_fmts; i++) {
156 if (bitstream == (formats[i].codec_mode !=
157 HANTRO_MODE_NONE) &&
158 hantro_check_depth_match(&formats[i], bit_depth))
159 return &formats[i];
160 }
161
162 formats = hantro_get_postproc_formats(ctx, &num_fmts);
163 for (i = 0; i < num_fmts; i++) {
164 if (bitstream == (formats[i].codec_mode !=
165 HANTRO_MODE_NONE) &&
166 hantro_check_depth_match(&formats[i], bit_depth))
167 return &formats[i];
168 }
169
170 return NULL;
171 }
172
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)173 static int vidioc_querycap(struct file *file, void *priv,
174 struct v4l2_capability *cap)
175 {
176 struct hantro_dev *vpu = video_drvdata(file);
177 struct video_device *vdev = video_devdata(file);
178
179 strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
180 strscpy(cap->card, vdev->name, sizeof(cap->card));
181 return 0;
182 }
183
vidioc_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)184 static int vidioc_enum_framesizes(struct file *file, void *priv,
185 struct v4l2_frmsizeenum *fsize)
186 {
187 struct hantro_ctx *ctx = fh_to_ctx(priv);
188 const struct hantro_fmt *fmt;
189
190 fmt = hantro_find_format(ctx, fsize->pixel_format);
191 if (!fmt) {
192 vpu_debug(0, "unsupported bitstream format (%08x)\n",
193 fsize->pixel_format);
194 return -EINVAL;
195 }
196
197 /* For non-coded formats check if postprocessing scaling is possible */
198 if (fmt->codec_mode == HANTRO_MODE_NONE) {
199 if (hantro_needs_postproc(ctx, fmt))
200 return hanto_postproc_enum_framesizes(ctx, fsize);
201 else
202 return -ENOTTY;
203 } else if (fsize->index != 0) {
204 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
205 fsize->index);
206 return -EINVAL;
207 }
208
209 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
210 fsize->stepwise = fmt->frmsize;
211
212 return 0;
213 }
214
vidioc_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f,bool capture)215 static int vidioc_enum_fmt(struct file *file, void *priv,
216 struct v4l2_fmtdesc *f, bool capture)
217
218 {
219 struct hantro_ctx *ctx = fh_to_ctx(priv);
220 const struct hantro_fmt *fmt, *formats;
221 unsigned int num_fmts, i, j = 0;
222 bool skip_mode_none, enum_all_formats;
223 u32 index = f->index & ~V4L2_FMTDESC_FLAG_ENUM_ALL;
224
225 /*
226 * If the V4L2_FMTDESC_FLAG_ENUM_ALL flag is set, we want to enumerate all
227 * hardware supported pixel formats
228 */
229 enum_all_formats = !!(f->index & V4L2_FMTDESC_FLAG_ENUM_ALL);
230 f->index = index;
231
232 /*
233 * When dealing with an encoder:
234 * - on the capture side we want to filter out all MODE_NONE formats.
235 * - on the output side we want to filter out all formats that are
236 * not MODE_NONE.
237 * When dealing with a decoder:
238 * - on the capture side we want to filter out all formats that are
239 * not MODE_NONE.
240 * - on the output side we want to filter out all MODE_NONE formats.
241 */
242 skip_mode_none = capture == ctx->is_encoder;
243
244 formats = hantro_get_formats(ctx, &num_fmts, HANTRO_AUTO_POSTPROC);
245 for (i = 0; i < num_fmts; i++) {
246 bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE;
247 fmt = &formats[i];
248
249 if (skip_mode_none == mode_none)
250 continue;
251 if (!hantro_check_depth_match(fmt, ctx->bit_depth) && !enum_all_formats)
252 continue;
253 if (j == index) {
254 f->pixelformat = fmt->fourcc;
255 return 0;
256 }
257 ++j;
258 }
259
260 /*
261 * Enumerate post-processed formats. As per the specification,
262 * we enumerated these formats after natively decoded formats
263 * as a hint for applications on what's the preferred fomat.
264 */
265 if (!capture)
266 return -EINVAL;
267 formats = hantro_get_postproc_formats(ctx, &num_fmts);
268 for (i = 0; i < num_fmts; i++) {
269 fmt = &formats[i];
270
271 if (!hantro_check_depth_match(fmt, ctx->bit_depth) && !enum_all_formats)
272 continue;
273 if (j == index) {
274 f->pixelformat = fmt->fourcc;
275 return 0;
276 }
277 ++j;
278 }
279
280 return -EINVAL;
281 }
282
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)283 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
284 struct v4l2_fmtdesc *f)
285 {
286 return vidioc_enum_fmt(file, priv, f, true);
287 }
288
vidioc_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)289 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
290 struct v4l2_fmtdesc *f)
291 {
292 return vidioc_enum_fmt(file, priv, f, false);
293 }
294
vidioc_g_fmt_out_mplane(struct file * file,void * priv,struct v4l2_format * f)295 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
296 struct v4l2_format *f)
297 {
298 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
299 struct hantro_ctx *ctx = fh_to_ctx(priv);
300
301 vpu_debug(4, "f->type = %d\n", f->type);
302
303 *pix_mp = ctx->src_fmt;
304
305 return 0;
306 }
307
vidioc_g_fmt_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)308 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
309 struct v4l2_format *f)
310 {
311 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
312 struct hantro_ctx *ctx = fh_to_ctx(priv);
313
314 vpu_debug(4, "f->type = %d\n", f->type);
315
316 *pix_mp = ctx->dst_fmt;
317
318 return 0;
319 }
320
hantro_try_fmt(const struct hantro_ctx * ctx,struct v4l2_pix_format_mplane * pix_mp,enum v4l2_buf_type type)321 static int hantro_try_fmt(const struct hantro_ctx *ctx,
322 struct v4l2_pix_format_mplane *pix_mp,
323 enum v4l2_buf_type type)
324 {
325 const struct hantro_fmt *fmt;
326 const struct hantro_fmt *vpu_fmt;
327 bool capture = V4L2_TYPE_IS_CAPTURE(type);
328 bool coded;
329
330 coded = capture == ctx->is_encoder;
331
332 vpu_debug(4, "trying format %p4cc\n", &pix_mp->pixelformat);
333
334 fmt = hantro_find_format(ctx, pix_mp->pixelformat);
335 if (!fmt) {
336 fmt = hantro_get_default_fmt(ctx, coded, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
337 pix_mp->pixelformat = fmt->fourcc;
338 }
339
340 if (coded) {
341 pix_mp->num_planes = 1;
342 vpu_fmt = fmt;
343 } else if (ctx->is_encoder) {
344 vpu_fmt = hantro_find_format(ctx, ctx->dst_fmt.pixelformat);
345 } else {
346 /*
347 * Width/height on the CAPTURE end of a decoder are ignored and
348 * replaced by the OUTPUT ones.
349 */
350 pix_mp->width = ctx->src_fmt.width;
351 pix_mp->height = ctx->src_fmt.height;
352 vpu_fmt = fmt;
353 }
354
355 pix_mp->field = V4L2_FIELD_NONE;
356
357 v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
358 &vpu_fmt->frmsize);
359
360 if (!coded) {
361 /* Fill remaining fields */
362 v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
363 pix_mp->height);
364 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE &&
365 !hantro_needs_postproc(ctx, fmt))
366 pix_mp->plane_fmt[0].sizeimage +=
367 hantro_h264_mv_size(pix_mp->width,
368 pix_mp->height);
369 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP9_FRAME &&
370 !hantro_needs_postproc(ctx, fmt))
371 pix_mp->plane_fmt[0].sizeimage +=
372 hantro_vp9_mv_size(pix_mp->width,
373 pix_mp->height);
374 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_HEVC_SLICE &&
375 !hantro_needs_postproc(ctx, fmt))
376 pix_mp->plane_fmt[0].sizeimage +=
377 hantro_hevc_mv_size(pix_mp->width,
378 pix_mp->height);
379 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_AV1_FRAME &&
380 !hantro_needs_postproc(ctx, fmt))
381 pix_mp->plane_fmt[0].sizeimage +=
382 hantro_av1_mv_size(pix_mp->width,
383 pix_mp->height);
384 } else if (!pix_mp->plane_fmt[0].sizeimage) {
385 /*
386 * For coded formats the application can specify
387 * sizeimage. If the application passes a zero sizeimage,
388 * let's default to the maximum frame size.
389 */
390 pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
391 pix_mp->width * pix_mp->height * fmt->max_depth;
392 }
393
394 return 0;
395 }
396
vidioc_try_fmt_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)397 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv,
398 struct v4l2_format *f)
399 {
400 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
401 }
402
vidioc_try_fmt_out_mplane(struct file * file,void * priv,struct v4l2_format * f)403 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv,
404 struct v4l2_format *f)
405 {
406 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
407 }
408
409 static void
hantro_reset_fmt(struct v4l2_pix_format_mplane * fmt,const struct hantro_fmt * vpu_fmt)410 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt,
411 const struct hantro_fmt *vpu_fmt)
412 {
413 memset(fmt, 0, sizeof(*fmt));
414
415 fmt->pixelformat = vpu_fmt->fourcc;
416 fmt->field = V4L2_FIELD_NONE;
417 fmt->colorspace = V4L2_COLORSPACE_JPEG;
418 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
419 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
420 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
421 }
422
423 static void
hantro_reset_encoded_fmt(struct hantro_ctx * ctx)424 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
425 {
426 const struct hantro_fmt *vpu_fmt;
427 struct v4l2_pix_format_mplane fmt;
428
429 vpu_fmt = hantro_get_default_fmt(ctx, true, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
430 if (!vpu_fmt)
431 return;
432
433 hantro_reset_fmt(&fmt, vpu_fmt);
434 fmt.width = vpu_fmt->frmsize.min_width;
435 fmt.height = vpu_fmt->frmsize.min_height;
436 if (ctx->is_encoder)
437 hantro_set_fmt_cap(ctx, &fmt);
438 else
439 hantro_set_fmt_out(ctx, &fmt, HANTRO_AUTO_POSTPROC);
440 }
441
442 int
hantro_reset_raw_fmt(struct hantro_ctx * ctx,int bit_depth,bool need_postproc)443 hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth, bool need_postproc)
444 {
445 const struct hantro_fmt *raw_vpu_fmt;
446 struct v4l2_pix_format_mplane raw_fmt, *encoded_fmt;
447 int ret;
448
449 raw_vpu_fmt = hantro_get_default_fmt(ctx, false, bit_depth, need_postproc);
450 if (!raw_vpu_fmt)
451 return -EINVAL;
452
453 if (ctx->is_encoder) {
454 encoded_fmt = &ctx->dst_fmt;
455 ctx->vpu_src_fmt = raw_vpu_fmt;
456 } else {
457 encoded_fmt = &ctx->src_fmt;
458 }
459
460 hantro_reset_fmt(&raw_fmt, raw_vpu_fmt);
461 raw_fmt.width = encoded_fmt->width;
462 raw_fmt.height = encoded_fmt->height;
463 if (ctx->is_encoder)
464 ret = hantro_set_fmt_out(ctx, &raw_fmt, need_postproc);
465 else
466 ret = hantro_set_fmt_cap(ctx, &raw_fmt);
467
468 if (!ret) {
469 ctx->bit_depth = bit_depth;
470 ctx->need_postproc = need_postproc;
471 }
472
473 return ret;
474 }
475
hantro_reset_fmts(struct hantro_ctx * ctx)476 void hantro_reset_fmts(struct hantro_ctx *ctx)
477 {
478 hantro_reset_encoded_fmt(ctx);
479 hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
480 }
481
482 static void
hantro_update_requires_request(struct hantro_ctx * ctx,u32 fourcc)483 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc)
484 {
485 switch (fourcc) {
486 case V4L2_PIX_FMT_JPEG:
487 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false;
488 break;
489 case V4L2_PIX_FMT_MPEG2_SLICE:
490 case V4L2_PIX_FMT_VP8_FRAME:
491 case V4L2_PIX_FMT_H264_SLICE:
492 case V4L2_PIX_FMT_HEVC_SLICE:
493 case V4L2_PIX_FMT_VP9_FRAME:
494 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
495 break;
496 default:
497 break;
498 }
499 }
500
501 static void
hantro_update_requires_hold_capture_buf(struct hantro_ctx * ctx,u32 fourcc)502 hantro_update_requires_hold_capture_buf(struct hantro_ctx *ctx, u32 fourcc)
503 {
504 struct vb2_queue *vq;
505
506 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
507 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
508
509 switch (fourcc) {
510 case V4L2_PIX_FMT_JPEG:
511 case V4L2_PIX_FMT_MPEG2_SLICE:
512 case V4L2_PIX_FMT_VP8_FRAME:
513 case V4L2_PIX_FMT_HEVC_SLICE:
514 case V4L2_PIX_FMT_VP9_FRAME:
515 vq->subsystem_flags &= ~(VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF);
516 break;
517 case V4L2_PIX_FMT_H264_SLICE:
518 vq->subsystem_flags |= VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
519 break;
520 default:
521 break;
522 }
523 }
524
hantro_set_fmt_out(struct hantro_ctx * ctx,struct v4l2_pix_format_mplane * pix_mp,bool need_postproc)525 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
526 struct v4l2_pix_format_mplane *pix_mp,
527 bool need_postproc)
528 {
529 struct vb2_queue *vq;
530 int ret;
531
532 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
533 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
534 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
535 if (ret)
536 return ret;
537
538 if (!ctx->is_encoder) {
539 /*
540 * In order to support dynamic resolution change,
541 * the decoder admits a resolution change, as long
542 * as the pixelformat remains.
543 */
544 if (vb2_is_streaming(vq) && pix_mp->pixelformat != ctx->src_fmt.pixelformat) {
545 return -EBUSY;
546 }
547 } else {
548 /*
549 * The encoder doesn't admit a format change if
550 * there are OUTPUT buffers allocated.
551 */
552 if (vb2_is_busy(vq))
553 return -EBUSY;
554 }
555
556 ctx->vpu_src_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
557 ctx->src_fmt = *pix_mp;
558
559 /*
560 * Current raw format might have become invalid with newly
561 * selected codec, so reset it to default just to be safe and
562 * keep internal driver state sane. User is mandated to set
563 * the raw format again after we return, so we don't need
564 * anything smarter.
565 * Note that hantro_reset_raw_fmt() also propagates size
566 * changes to the raw format.
567 */
568 if (!ctx->is_encoder)
569 hantro_reset_raw_fmt(ctx,
570 hantro_get_format_depth(pix_mp->pixelformat),
571 need_postproc);
572
573 /* Colorimetry information are always propagated. */
574 ctx->dst_fmt.colorspace = pix_mp->colorspace;
575 ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
576 ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
577 ctx->dst_fmt.quantization = pix_mp->quantization;
578
579 hantro_update_requires_request(ctx, pix_mp->pixelformat);
580 hantro_update_requires_hold_capture_buf(ctx, pix_mp->pixelformat);
581
582 vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
583 vpu_debug(0, "fmt - w: %d, h: %d\n",
584 pix_mp->width, pix_mp->height);
585 return 0;
586 }
587
hantro_set_fmt_cap(struct hantro_ctx * ctx,struct v4l2_pix_format_mplane * pix_mp)588 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
589 struct v4l2_pix_format_mplane *pix_mp)
590 {
591 int ret;
592
593 if (ctx->is_encoder) {
594 struct vb2_queue *peer_vq;
595
596 /*
597 * Since format change on the CAPTURE queue will reset
598 * the OUTPUT queue, we can't allow doing so
599 * when the OUTPUT queue has buffers allocated.
600 */
601 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
602 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
603 if (vb2_is_busy(peer_vq) &&
604 (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
605 pix_mp->height != ctx->dst_fmt.height ||
606 pix_mp->width != ctx->dst_fmt.width))
607 return -EBUSY;
608 }
609
610 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
611 if (ret)
612 return ret;
613
614 ctx->vpu_dst_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
615 ctx->dst_fmt = *pix_mp;
616 ret = hantro_set_reference_frames_format(ctx);
617 if (ret)
618 return ret;
619
620 /*
621 * Current raw format might have become invalid with newly
622 * selected codec, so reset it to default just to be safe and
623 * keep internal driver state sane. User is mandated to set
624 * the raw format again after we return, so we don't need
625 * anything smarter.
626 * Note that hantro_reset_raw_fmt() also propagates size
627 * changes to the raw format.
628 */
629 if (ctx->is_encoder)
630 hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
631
632 /* Colorimetry information are always propagated. */
633 ctx->src_fmt.colorspace = pix_mp->colorspace;
634 ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
635 ctx->src_fmt.xfer_func = pix_mp->xfer_func;
636 ctx->src_fmt.quantization = pix_mp->quantization;
637
638 vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
639 vpu_debug(0, "fmt - w: %d, h: %d\n",
640 pix_mp->width, pix_mp->height);
641
642 hantro_update_requires_request(ctx, pix_mp->pixelformat);
643
644 return 0;
645 }
646
647 static int
vidioc_s_fmt_out_mplane(struct file * file,void * priv,struct v4l2_format * f)648 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
649 {
650 return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp, HANTRO_AUTO_POSTPROC);
651 }
652
653 static int
vidioc_s_fmt_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)654 vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
655 {
656 return hantro_set_fmt_cap(fh_to_ctx(priv), &f->fmt.pix_mp);
657 }
658
vidioc_g_selection(struct file * file,void * priv,struct v4l2_selection * sel)659 static int vidioc_g_selection(struct file *file, void *priv,
660 struct v4l2_selection *sel)
661 {
662 struct hantro_ctx *ctx = fh_to_ctx(priv);
663
664 /* Crop only supported on source. */
665 if (!ctx->is_encoder ||
666 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
667 return -EINVAL;
668
669 switch (sel->target) {
670 case V4L2_SEL_TGT_CROP_DEFAULT:
671 case V4L2_SEL_TGT_CROP_BOUNDS:
672 sel->r.top = 0;
673 sel->r.left = 0;
674 sel->r.width = ctx->src_fmt.width;
675 sel->r.height = ctx->src_fmt.height;
676 break;
677 case V4L2_SEL_TGT_CROP:
678 sel->r.top = 0;
679 sel->r.left = 0;
680 sel->r.width = ctx->dst_fmt.width;
681 sel->r.height = ctx->dst_fmt.height;
682 break;
683 default:
684 return -EINVAL;
685 }
686
687 return 0;
688 }
689
vidioc_s_selection(struct file * file,void * priv,struct v4l2_selection * sel)690 static int vidioc_s_selection(struct file *file, void *priv,
691 struct v4l2_selection *sel)
692 {
693 struct hantro_ctx *ctx = fh_to_ctx(priv);
694 struct v4l2_rect *rect = &sel->r;
695 struct vb2_queue *vq;
696
697 /* Crop only supported on source. */
698 if (!ctx->is_encoder ||
699 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
700 return -EINVAL;
701
702 /* Change not allowed if the queue is streaming. */
703 vq = v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx);
704 if (vb2_is_streaming(vq))
705 return -EBUSY;
706
707 if (sel->target != V4L2_SEL_TGT_CROP)
708 return -EINVAL;
709
710 /*
711 * We do not support offsets, and we can crop only inside
712 * right-most or bottom-most macroblocks.
713 */
714 if (rect->left != 0 || rect->top != 0 ||
715 round_up(rect->width, MB_DIM) != ctx->src_fmt.width ||
716 round_up(rect->height, MB_DIM) != ctx->src_fmt.height) {
717 /* Default to full frame for incorrect settings. */
718 rect->left = 0;
719 rect->top = 0;
720 rect->width = ctx->src_fmt.width;
721 rect->height = ctx->src_fmt.height;
722 } else {
723 /* We support widths aligned to 4 pixels and arbitrary heights. */
724 rect->width = round_up(rect->width, 4);
725 }
726
727 ctx->dst_fmt.width = rect->width;
728 ctx->dst_fmt.height = rect->height;
729
730 return 0;
731 }
732
733 static const struct v4l2_event hantro_eos_event = {
734 .type = V4L2_EVENT_EOS
735 };
736
vidioc_encoder_cmd(struct file * file,void * priv,struct v4l2_encoder_cmd * ec)737 static int vidioc_encoder_cmd(struct file *file, void *priv,
738 struct v4l2_encoder_cmd *ec)
739 {
740 struct hantro_ctx *ctx = fh_to_ctx(priv);
741 int ret;
742
743 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec);
744 if (ret < 0)
745 return ret;
746
747 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)) ||
748 !vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)))
749 return 0;
750
751 ret = v4l2_m2m_ioctl_encoder_cmd(file, priv, ec);
752 if (ret < 0)
753 return ret;
754
755 if (ec->cmd == V4L2_ENC_CMD_STOP &&
756 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
757 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
758
759 if (ec->cmd == V4L2_ENC_CMD_START)
760 vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q);
761
762 return 0;
763 }
764
765 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
766 .vidioc_querycap = vidioc_querycap,
767 .vidioc_enum_framesizes = vidioc_enum_framesizes,
768
769 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
770 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
771 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
772 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
773 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
774 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
775 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
776 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
777
778 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
779 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
780 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
781 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
782 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
783 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
784 .vidioc_remove_bufs = v4l2_m2m_ioctl_remove_bufs,
785 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
786
787 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
788 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
789
790 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
791 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
792
793 .vidioc_g_selection = vidioc_g_selection,
794 .vidioc_s_selection = vidioc_s_selection,
795
796 .vidioc_decoder_cmd = v4l2_m2m_ioctl_stateless_decoder_cmd,
797 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_stateless_try_decoder_cmd,
798
799 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
800 .vidioc_encoder_cmd = vidioc_encoder_cmd,
801 };
802
803 static int
hantro_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])804 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
805 unsigned int *num_planes, unsigned int sizes[],
806 struct device *alloc_devs[])
807 {
808 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
809 struct v4l2_pix_format_mplane *pixfmt;
810 int i;
811
812 switch (vq->type) {
813 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
814 pixfmt = &ctx->dst_fmt;
815 break;
816 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
817 pixfmt = &ctx->src_fmt;
818 break;
819 default:
820 vpu_err("invalid queue type: %d\n", vq->type);
821 return -EINVAL;
822 }
823
824 if (*num_planes) {
825 if (*num_planes != pixfmt->num_planes)
826 return -EINVAL;
827 for (i = 0; i < pixfmt->num_planes; ++i)
828 if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
829 return -EINVAL;
830 return 0;
831 }
832
833 *num_planes = pixfmt->num_planes;
834 for (i = 0; i < pixfmt->num_planes; ++i)
835 sizes[i] = pixfmt->plane_fmt[i].sizeimage;
836 return 0;
837 }
838
839 static int
hantro_buf_plane_check(struct vb2_buffer * vb,struct v4l2_pix_format_mplane * pixfmt)840 hantro_buf_plane_check(struct vb2_buffer *vb,
841 struct v4l2_pix_format_mplane *pixfmt)
842 {
843 unsigned int sz;
844 int i;
845
846 for (i = 0; i < pixfmt->num_planes; ++i) {
847 sz = pixfmt->plane_fmt[i].sizeimage;
848 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
849 i, vb2_plane_size(vb, i), sz);
850 if (vb2_plane_size(vb, i) < sz) {
851 vpu_err("plane %d is too small for output\n", i);
852 return -EINVAL;
853 }
854 }
855 return 0;
856 }
857
hantro_buf_prepare(struct vb2_buffer * vb)858 static int hantro_buf_prepare(struct vb2_buffer *vb)
859 {
860 struct vb2_queue *vq = vb->vb2_queue;
861 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
862 struct v4l2_pix_format_mplane *pix_fmt;
863 int ret;
864
865 if (V4L2_TYPE_IS_OUTPUT(vq->type))
866 pix_fmt = &ctx->src_fmt;
867 else
868 pix_fmt = &ctx->dst_fmt;
869 ret = hantro_buf_plane_check(vb, pix_fmt);
870 if (ret)
871 return ret;
872 /*
873 * Buffer's bytesused must be written by driver for CAPTURE buffers.
874 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
875 * it to buffer length).
876 */
877 if (V4L2_TYPE_IS_CAPTURE(vq->type)) {
878 if (ctx->is_encoder)
879 vb2_set_plane_payload(vb, 0, 0);
880 else
881 vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
882 }
883
884 return 0;
885 }
886
hantro_buf_queue(struct vb2_buffer * vb)887 static void hantro_buf_queue(struct vb2_buffer *vb)
888 {
889 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
890 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
891
892 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
893 vb2_is_streaming(vb->vb2_queue) &&
894 v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
895 unsigned int i;
896
897 for (i = 0; i < vb->num_planes; i++)
898 vb2_set_plane_payload(vb, i, 0);
899
900 vbuf->field = V4L2_FIELD_NONE;
901 vbuf->sequence = ctx->sequence_cap++;
902
903 v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
904 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
905 return;
906 }
907
908 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
909 }
910
hantro_vq_is_coded(struct vb2_queue * q)911 static bool hantro_vq_is_coded(struct vb2_queue *q)
912 {
913 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
914
915 return ctx->is_encoder != V4L2_TYPE_IS_OUTPUT(q->type);
916 }
917
hantro_start_streaming(struct vb2_queue * q,unsigned int count)918 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
919 {
920 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
921 int ret = 0;
922
923 v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
924
925 if (V4L2_TYPE_IS_OUTPUT(q->type))
926 ctx->sequence_out = 0;
927 else
928 ctx->sequence_cap = 0;
929
930 if (hantro_vq_is_coded(q)) {
931 enum hantro_codec_mode codec_mode;
932
933 if (V4L2_TYPE_IS_OUTPUT(q->type))
934 codec_mode = ctx->vpu_src_fmt->codec_mode;
935 else
936 codec_mode = ctx->vpu_dst_fmt->codec_mode;
937
938 vpu_debug(4, "Codec mode = %d\n", codec_mode);
939 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
940 if (ctx->codec_ops->init) {
941 ret = ctx->codec_ops->init(ctx);
942 if (ret)
943 return ret;
944 }
945
946 if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) {
947 ret = hantro_postproc_init(ctx);
948 if (ret)
949 goto err_codec_exit;
950 }
951 }
952 return ret;
953
954 err_codec_exit:
955 if (ctx->codec_ops->exit)
956 ctx->codec_ops->exit(ctx);
957 return ret;
958 }
959
960 static void
hantro_return_bufs(struct vb2_queue * q,struct vb2_v4l2_buffer * (* buf_remove)(struct v4l2_m2m_ctx *))961 hantro_return_bufs(struct vb2_queue *q,
962 struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
963 {
964 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
965
966 for (;;) {
967 struct vb2_v4l2_buffer *vbuf;
968
969 vbuf = buf_remove(ctx->fh.m2m_ctx);
970 if (!vbuf)
971 break;
972 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
973 &ctx->ctrl_handler);
974 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
975 }
976 }
977
hantro_stop_streaming(struct vb2_queue * q)978 static void hantro_stop_streaming(struct vb2_queue *q)
979 {
980 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
981
982 if (hantro_vq_is_coded(q)) {
983 hantro_postproc_free(ctx);
984 if (ctx->codec_ops && ctx->codec_ops->exit)
985 ctx->codec_ops->exit(ctx);
986 }
987
988 /*
989 * The mem2mem framework calls v4l2_m2m_cancel_job before
990 * .stop_streaming, so there isn't any job running and
991 * it is safe to return all the buffers.
992 */
993 if (V4L2_TYPE_IS_OUTPUT(q->type))
994 hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
995 else
996 hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
997
998 v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
999
1000 if (V4L2_TYPE_IS_OUTPUT(q->type) &&
1001 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
1002 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
1003 }
1004
hantro_buf_request_complete(struct vb2_buffer * vb)1005 static void hantro_buf_request_complete(struct vb2_buffer *vb)
1006 {
1007 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1008
1009 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
1010 }
1011
hantro_buf_out_validate(struct vb2_buffer * vb)1012 static int hantro_buf_out_validate(struct vb2_buffer *vb)
1013 {
1014 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1015
1016 vbuf->field = V4L2_FIELD_NONE;
1017 return 0;
1018 }
1019
1020 const struct vb2_ops hantro_queue_ops = {
1021 .queue_setup = hantro_queue_setup,
1022 .buf_prepare = hantro_buf_prepare,
1023 .buf_queue = hantro_buf_queue,
1024 .buf_out_validate = hantro_buf_out_validate,
1025 .buf_request_complete = hantro_buf_request_complete,
1026 .start_streaming = hantro_start_streaming,
1027 .stop_streaming = hantro_stop_streaming,
1028 };
1029