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