1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020-2021 NXP
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>
24 #include "vpu_helpers.h"
29 #define VDEC_MIN_BUFFER_CAP 8
30 #define VDEC_MIN_BUFFER_OUT 8
40 struct vpu_buffer buffer[32];
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;
51 struct vpu_vb2_buffer *slots[VB2_MAX_FRAME];
53 struct vdec_fs_info mbi;
54 struct vdec_fs_info dcp;
59 u32 decoded_frame_count;
60 u32 display_frame_count;
63 bool is_source_changed;
69 static const struct vpu_format vdec_formats[] = {
71 .pixfmt = V4L2_PIX_FMT_NV12M_8L128,
74 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
75 .sibling = V4L2_PIX_FMT_NV12_8L128,
78 .pixfmt = V4L2_PIX_FMT_NV12_8L128,
81 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
82 .sibling = V4L2_PIX_FMT_NV12M_8L128,
85 .pixfmt = V4L2_PIX_FMT_NV12M_10BE_8L128,
88 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
89 .sibling = V4L2_PIX_FMT_NV12_10BE_8L128,
92 .pixfmt = V4L2_PIX_FMT_NV12_10BE_8L128,
95 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
96 .sibling = V4L2_PIX_FMT_NV12M_10BE_8L128
99 .pixfmt = V4L2_PIX_FMT_H264,
102 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
103 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
106 .pixfmt = V4L2_PIX_FMT_H264_MVC,
109 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
110 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
113 .pixfmt = V4L2_PIX_FMT_HEVC,
116 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
117 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
120 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
123 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
124 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
127 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
130 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
131 .flags = V4L2_FMT_FLAG_COMPRESSED
134 .pixfmt = V4L2_PIX_FMT_MPEG2,
137 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
138 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
141 .pixfmt = V4L2_PIX_FMT_MPEG4,
144 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
145 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
148 .pixfmt = V4L2_PIX_FMT_XVID,
151 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
152 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
155 .pixfmt = V4L2_PIX_FMT_VP8,
158 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
159 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
162 .pixfmt = V4L2_PIX_FMT_H263,
165 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
166 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
169 .pixfmt = V4L2_PIX_FMT_SPK,
172 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
173 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
176 .pixfmt = V4L2_PIX_FMT_RV30,
179 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
180 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
183 .pixfmt = V4L2_PIX_FMT_RV40,
186 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
187 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
192 static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
194 struct vpu_inst *inst = ctrl_to_inst(ctrl);
195 struct vdec_t *vdec = inst->priv;
200 case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
201 vdec->params.display_delay_enable = ctrl->val;
203 case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:
204 vdec->params.display_delay = ctrl->val;
210 vpu_inst_unlock(inst);
215 static const struct v4l2_ctrl_ops vdec_ctrl_ops = {
216 .s_ctrl = vdec_op_s_ctrl,
217 .g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
220 static int vdec_ctrl_init(struct vpu_inst *inst)
222 struct v4l2_ctrl *ctrl;
225 ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
229 v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
230 V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY,
233 v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
234 V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE,
237 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
238 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
240 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
242 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
243 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
245 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
247 if (inst->ctrl_handler.error) {
248 ret = inst->ctrl_handler.error;
249 v4l2_ctrl_handler_free(&inst->ctrl_handler);
253 ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
255 dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
256 v4l2_ctrl_handler_free(&inst->ctrl_handler);
263 static void vdec_handle_resolution_change(struct vpu_inst *inst)
265 struct vdec_t *vdec = inst->priv;
268 if (!inst->fh.m2m_ctx)
271 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
273 if (!vdec->source_change)
276 q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
277 if (!list_empty(&q->done_list))
280 vdec->source_change--;
281 vpu_notify_source_change(inst);
282 vpu_set_last_buffer_dequeued(inst, false);
285 static int vdec_update_state(struct vpu_inst *inst, enum vpu_codec_state state, u32 force)
287 struct vdec_t *vdec = inst->priv;
288 enum vpu_codec_state pre_state = inst->state;
290 if (state == VPU_CODEC_STATE_SEEK) {
291 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
292 vdec->state = inst->state;
294 vdec->state = VPU_CODEC_STATE_ACTIVE;
296 if (inst->state != VPU_CODEC_STATE_SEEK || force)
298 else if (state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
299 vdec->state = VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE;
301 if (inst->state != pre_state)
302 vpu_trace(inst->dev, "[%d] %d -> %d\n", inst->id, pre_state, inst->state);
304 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
305 vdec_handle_resolution_change(inst);
310 static void vdec_set_last_buffer_dequeued(struct vpu_inst *inst)
312 struct vdec_t *vdec = inst->priv;
314 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
317 if (vdec->eos_received) {
318 if (!vpu_set_last_buffer_dequeued(inst, true)) {
319 vdec->eos_received--;
320 vdec_update_state(inst, VPU_CODEC_STATE_DRAIN, 0);
325 static int vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
327 strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
328 strscpy(cap->card, "amphion vpu decoder", sizeof(cap->card));
329 strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
334 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
336 struct vpu_inst *inst = to_inst(file);
337 struct vdec_t *vdec = inst->priv;
338 const struct vpu_format *fmt;
342 if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
343 fmt = vpu_get_format(inst, f->type);
345 fmt = vpu_helper_find_sibling(inst, f->type, fmt->pixfmt);
349 fmt = vpu_helper_enum_format(inst, f->type, f->index);
354 memset(f->reserved, 0, sizeof(f->reserved));
355 f->pixelformat = fmt->pixfmt;
356 f->flags = fmt->flags;
359 vpu_inst_unlock(inst);
363 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
365 struct vpu_inst *inst = to_inst(file);
366 struct vdec_t *vdec = inst->priv;
367 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
368 struct vpu_format *cur_fmt;
372 cur_fmt = vpu_get_format(inst, f->type);
374 pixmp->pixelformat = cur_fmt->pixfmt;
375 pixmp->num_planes = cur_fmt->mem_planes;
376 pixmp->width = cur_fmt->width;
377 pixmp->height = cur_fmt->height;
378 pixmp->field = cur_fmt->field;
379 pixmp->flags = cur_fmt->flags;
380 for (i = 0; i < pixmp->num_planes; i++) {
381 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
382 pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
385 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
386 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
387 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
388 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
389 vpu_inst_unlock(inst);
394 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
396 struct vpu_inst *inst = to_inst(file);
397 struct vdec_t *vdec = inst->priv;
398 struct vpu_format fmt;
401 if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
402 struct vpu_format *cap_fmt = vpu_get_format(inst, f->type);
404 if (!vpu_helper_match_format(inst, cap_fmt->type, cap_fmt->pixfmt,
405 f->fmt.pix_mp.pixelformat))
406 f->fmt.pix_mp.pixelformat = cap_fmt->pixfmt;
409 vpu_try_fmt_common(inst, f, &fmt);
411 if (vdec->fixed_fmt) {
412 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
413 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
414 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
415 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
417 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
418 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
419 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
420 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
422 vpu_inst_unlock(inst);
427 static int vdec_s_fmt_common(struct vpu_inst *inst, struct v4l2_format *f)
429 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
430 struct vpu_format fmt;
431 struct vpu_format *cur_fmt;
433 struct vdec_t *vdec = inst->priv;
436 if (!inst->fh.m2m_ctx)
439 q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
445 if (vpu_try_fmt_common(inst, f, &fmt))
448 cur_fmt = vpu_get_format(inst, f->type);
449 if (V4L2_TYPE_IS_OUTPUT(f->type) && inst->state != VPU_CODEC_STATE_DEINIT) {
450 if (cur_fmt->pixfmt != fmt.pixfmt) {
451 vdec->reset_codec = true;
452 vdec->fixed_fmt = false;
455 if (V4L2_TYPE_IS_OUTPUT(f->type) || !vdec->fixed_fmt) {
456 memcpy(cur_fmt, &fmt, sizeof(*cur_fmt));
458 if (vpu_helper_match_format(inst, f->type, cur_fmt->pixfmt, pixmp->pixelformat)) {
459 cur_fmt->pixfmt = fmt.pixfmt;
460 cur_fmt->mem_planes = fmt.mem_planes;
462 pixmp->pixelformat = cur_fmt->pixfmt;
463 pixmp->num_planes = cur_fmt->mem_planes;
464 pixmp->width = cur_fmt->width;
465 pixmp->height = cur_fmt->height;
466 for (i = 0; i < pixmp->num_planes; i++) {
467 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
468 pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
470 pixmp->field = cur_fmt->field;
473 if (!vdec->fixed_fmt) {
474 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
475 vdec->params.codec_format = cur_fmt->pixfmt;
476 vdec->codec_info.color_primaries = f->fmt.pix_mp.colorspace;
477 vdec->codec_info.transfer_chars = f->fmt.pix_mp.xfer_func;
478 vdec->codec_info.matrix_coeffs = f->fmt.pix_mp.ycbcr_enc;
479 vdec->codec_info.full_range = f->fmt.pix_mp.quantization;
481 vdec->params.output_format = cur_fmt->pixfmt;
484 inst->crop.width = cur_fmt->width;
485 inst->crop.height = cur_fmt->height;
489 vpu_trace(inst->dev, "[%d] %c%c%c%c %dx%d\n", inst->id,
490 f->fmt.pix_mp.pixelformat,
491 f->fmt.pix_mp.pixelformat >> 8,
492 f->fmt.pix_mp.pixelformat >> 16,
493 f->fmt.pix_mp.pixelformat >> 24,
495 f->fmt.pix_mp.height);
500 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
502 struct vpu_inst *inst = to_inst(file);
503 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
504 struct vdec_t *vdec = inst->priv;
508 ret = vdec_s_fmt_common(inst, f);
512 if (V4L2_TYPE_IS_OUTPUT(f->type) && !vdec->fixed_fmt) {
513 struct v4l2_format fc;
515 memset(&fc, 0, sizeof(fc));
516 fc.type = inst->cap_format.type;
517 fc.fmt.pix_mp.pixelformat = inst->cap_format.pixfmt;
518 fc.fmt.pix_mp.width = pixmp->width;
519 fc.fmt.pix_mp.height = pixmp->height;
520 vdec_s_fmt_common(inst, &fc);
523 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
524 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
525 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
526 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
529 vpu_inst_unlock(inst);
533 static int vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
535 struct vpu_inst *inst = to_inst(file);
537 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
541 case V4L2_SEL_TGT_COMPOSE:
542 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
543 case V4L2_SEL_TGT_COMPOSE_PADDED:
546 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
549 s->r.width = inst->cap_format.width;
550 s->r.height = inst->cap_format.height;
559 static int vdec_drain(struct vpu_inst *inst)
561 struct vdec_t *vdec = inst->priv;
563 if (!inst->fh.m2m_ctx)
569 if (!vpu_is_source_empty(inst))
572 if (!vdec->params.frame_count) {
573 vpu_set_last_buffer_dequeued(inst, true);
577 vpu_iface_add_scode(inst, SCODE_PADDING_EOS);
578 vdec->params.end_flag = 1;
579 vpu_iface_set_decode_params(inst, &vdec->params, 1);
581 vpu_trace(inst->dev, "[%d] frame_count = %d\n", inst->id, vdec->params.frame_count);
586 static int vdec_cmd_start(struct vpu_inst *inst)
588 struct vdec_t *vdec = inst->priv;
590 switch (inst->state) {
591 case VPU_CODEC_STATE_STARTED:
592 case VPU_CODEC_STATE_DRAIN:
593 case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE:
594 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
599 vpu_process_capture_buffer(inst);
600 if (vdec->eos_received)
601 vdec_set_last_buffer_dequeued(inst);
605 static int vdec_cmd_stop(struct vpu_inst *inst)
607 struct vdec_t *vdec = inst->priv;
609 vpu_trace(inst->dev, "[%d]\n", inst->id);
611 if (inst->state == VPU_CODEC_STATE_DEINIT) {
612 vpu_set_last_buffer_dequeued(inst, true);
621 static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
623 struct vpu_inst *inst = to_inst(file);
626 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
632 case V4L2_DEC_CMD_START:
633 vdec_cmd_start(inst);
635 case V4L2_DEC_CMD_STOP:
641 vpu_inst_unlock(inst);
646 static int vdec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
650 return v4l2_event_subscribe(fh, sub, 0, NULL);
651 case V4L2_EVENT_SOURCE_CHANGE:
652 return v4l2_src_change_event_subscribe(fh, sub);
653 case V4L2_EVENT_CTRL:
654 return v4l2_ctrl_subscribe_event(fh, sub);
662 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
663 .vidioc_querycap = vdec_querycap,
664 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
665 .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
666 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
667 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
668 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
669 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
670 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
671 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
672 .vidioc_g_selection = vdec_g_selection,
673 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
674 .vidioc_decoder_cmd = vdec_decoder_cmd,
675 .vidioc_subscribe_event = vdec_subscribe_event,
676 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
677 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
678 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
679 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
680 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
681 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
682 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
683 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
684 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
685 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
688 static bool vdec_check_ready(struct vpu_inst *inst, unsigned int type)
690 struct vdec_t *vdec = inst->priv;
692 if (V4L2_TYPE_IS_OUTPUT(type))
695 if (vdec->req_frame_count)
701 static struct vb2_v4l2_buffer *vdec_get_src_buffer(struct vpu_inst *inst, u32 count)
704 vpu_skip_frame(inst, count - 1);
706 return vpu_next_src_buf(inst);
709 static int vdec_frame_decoded(struct vpu_inst *inst, void *arg)
711 struct vdec_t *vdec = inst->priv;
712 struct vpu_dec_pic_info *info = arg;
713 struct vpu_vb2_buffer *vpu_buf;
714 struct vb2_v4l2_buffer *vbuf;
715 struct vb2_v4l2_buffer *src_buf;
718 if (!info || info->id >= ARRAY_SIZE(vdec->slots))
722 vpu_buf = vdec->slots[info->id];
724 dev_err(inst->dev, "[%d] decoded invalid frame[%d]\n", inst->id, info->id);
728 vbuf = &vpu_buf->m2m_buf.vb;
729 src_buf = vdec_get_src_buffer(inst, info->consumed_count);
731 v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
732 if (info->consumed_count) {
733 v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
734 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
735 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
737 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_DECODED);
740 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED)
741 dev_info(inst->dev, "[%d] buf[%d] has been decoded\n", inst->id, info->id);
742 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_DECODED);
743 vdec->decoded_frame_count++;
745 vpu_inst_unlock(inst);
750 static struct vpu_vb2_buffer *vdec_find_buffer(struct vpu_inst *inst, u32 luma)
752 struct vdec_t *vdec = inst->priv;
755 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
758 if (luma == vdec->slots[i]->luma)
759 return vdec->slots[i];
765 static void vdec_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame)
767 struct vdec_t *vdec = inst->priv;
768 struct vpu_format *cur_fmt;
769 struct vpu_vb2_buffer *vpu_buf;
770 struct vb2_v4l2_buffer *vbuf;
778 sequence = vdec->sequence++;
779 vpu_buf = vdec_find_buffer(inst, frame->luma);
780 vpu_inst_unlock(inst);
782 dev_err(inst->dev, "[%d] can't find buffer, id = %d, addr = 0x%x\n",
783 inst->id, frame->id, frame->luma);
786 if (frame->skipped) {
787 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
791 cur_fmt = vpu_get_format(inst, inst->cap_format.type);
792 vbuf = &vpu_buf->m2m_buf.vb;
793 if (vbuf->vb2_buf.index != frame->id)
794 dev_err(inst->dev, "[%d] buffer id(%d, %d) dismatch\n",
795 inst->id, vbuf->vb2_buf.index, frame->id);
797 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_DECODED)
798 dev_err(inst->dev, "[%d] buffer(%d) ready without decoded\n", inst->id, frame->id);
799 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY);
800 for (i = 0; i < vbuf->vb2_buf.num_planes; i++)
801 vb2_set_plane_payload(&vbuf->vb2_buf, i, vpu_get_fmt_plane_size(cur_fmt, i));
802 vbuf->field = cur_fmt->field;
803 vbuf->sequence = sequence;
804 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
806 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
808 vdec->display_frame_count++;
809 vpu_inst_unlock(inst);
810 dev_dbg(inst->dev, "[%d] decoded : %d, display : %d, sequence : %d\n",
811 inst->id, vdec->decoded_frame_count, vdec->display_frame_count, vdec->sequence);
814 static void vdec_stop_done(struct vpu_inst *inst)
816 struct vdec_t *vdec = inst->priv;
819 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 0);
820 vdec->seq_hdr_found = 0;
821 vdec->req_frame_count = 0;
822 vdec->reset_codec = false;
823 vdec->fixed_fmt = false;
824 vdec->params.end_flag = 0;
826 vdec->params.frame_count = 0;
827 vdec->decoded_frame_count = 0;
828 vdec->display_frame_count = 0;
830 vdec->eos_received = 0;
831 vdec->is_source_changed = false;
832 vdec->source_change = 0;
833 inst->total_input_count = 0;
834 vpu_inst_unlock(inst);
837 static bool vdec_check_source_change(struct vpu_inst *inst)
839 struct vdec_t *vdec = inst->priv;
840 const struct vpu_format *sibling;
842 if (!inst->fh.m2m_ctx)
845 if (vdec->reset_codec)
848 sibling = vpu_helper_find_sibling(inst, inst->cap_format.type, inst->cap_format.pixfmt);
849 if (sibling && vdec->codec_info.pixfmt == sibling->pixfmt)
850 vdec->codec_info.pixfmt = inst->cap_format.pixfmt;
852 if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)))
854 if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
856 if (inst->cap_format.width != vdec->codec_info.decoded_width)
858 if (inst->cap_format.height != vdec->codec_info.decoded_height)
860 if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap)
862 if (inst->crop.left != vdec->codec_info.offset_x)
864 if (inst->crop.top != vdec->codec_info.offset_y)
866 if (inst->crop.width != vdec->codec_info.width)
868 if (inst->crop.height != vdec->codec_info.height)
874 static void vdec_init_fmt(struct vpu_inst *inst)
876 struct vdec_t *vdec = inst->priv;
877 struct v4l2_format f;
879 memset(&f, 0, sizeof(f));
880 f.type = inst->cap_format.type;
881 f.fmt.pix_mp.pixelformat = vdec->codec_info.pixfmt;
882 f.fmt.pix_mp.width = vdec->codec_info.decoded_width;
883 f.fmt.pix_mp.height = vdec->codec_info.decoded_height;
884 if (vdec->codec_info.progressive)
885 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
887 f.fmt.pix_mp.field = V4L2_FIELD_SEQ_TB;
888 vpu_try_fmt_common(inst, &f, &inst->cap_format);
890 inst->out_format.width = vdec->codec_info.width;
891 inst->out_format.height = vdec->codec_info.height;
894 static void vdec_init_crop(struct vpu_inst *inst)
896 struct vdec_t *vdec = inst->priv;
898 inst->crop.left = vdec->codec_info.offset_x;
899 inst->crop.top = vdec->codec_info.offset_y;
900 inst->crop.width = vdec->codec_info.width;
901 inst->crop.height = vdec->codec_info.height;
904 static void vdec_init_mbi(struct vpu_inst *inst)
906 struct vdec_t *vdec = inst->priv;
908 vdec->mbi.size = vdec->codec_info.mbi_size;
909 vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer);
910 scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi");
911 vdec->mbi.type = MEM_RES_MBI;
912 vdec->mbi.tag = vdec->seq_tag;
915 static void vdec_init_dcp(struct vpu_inst *inst)
917 struct vdec_t *vdec = inst->priv;
919 vdec->dcp.size = vdec->codec_info.dcp_size;
920 vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer);
921 scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp");
922 vdec->dcp.type = MEM_RES_DCP;
923 vdec->dcp.tag = vdec->seq_tag;
926 static void vdec_request_one_fs(struct vdec_fs_info *fs)
929 if (fs->req_count > fs->max_count)
930 fs->req_count = fs->max_count;
933 static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs)
935 struct vpu_buffer *buffer;
940 if (fs->count >= fs->req_count)
943 buffer = &fs->buffer[fs->count];
944 if (buffer->virt && buffer->length >= fs->size)
947 vpu_free_dma(buffer);
948 buffer->length = fs->size;
949 return vpu_alloc_dma(inst->core, buffer);
952 static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
956 while (fs->count < fs->req_count) {
957 ret = vdec_alloc_fs_buffer(inst, fs);
964 static void vdec_clear_fs(struct vdec_fs_info *fs)
971 for (i = 0; i < ARRAY_SIZE(fs->buffer); i++)
972 vpu_free_dma(&fs->buffer[i]);
973 memset(fs, 0, sizeof(*fs));
976 static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
978 struct vpu_fs_info info;
981 if (fs->index >= fs->count)
984 memset(&info, 0, sizeof(info));
986 info.type = fs->type;
988 info.luma_addr = fs->buffer[fs->index].phys;
989 info.luma_size = fs->buffer[fs->index].length;
990 ret = vpu_session_alloc_fs(inst, &info);
998 static int vdec_response_frame_abnormal(struct vpu_inst *inst)
1000 struct vdec_t *vdec = inst->priv;
1001 struct vpu_fs_info info;
1003 if (!vdec->req_frame_count)
1006 memset(&info, 0, sizeof(info));
1007 info.type = MEM_RES_FRAME;
1008 info.tag = vdec->seq_tag + 0xf0;
1009 vpu_session_alloc_fs(inst, &info);
1010 vdec->req_frame_count--;
1015 static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1017 struct vdec_t *vdec = inst->priv;
1018 struct vpu_vb2_buffer *vpu_buf;
1019 struct vpu_fs_info info;
1022 if (inst->state != VPU_CODEC_STATE_ACTIVE)
1028 if (!vdec->req_frame_count)
1034 if (vdec->slots[vbuf->vb2_buf.index]) {
1035 dev_err(inst->dev, "[%d] repeat alloc fs %d\n",
1036 inst->id, vbuf->vb2_buf.index);
1040 dev_dbg(inst->dev, "[%d] state = %d, alloc fs %d, tag = 0x%x\n",
1041 inst->id, inst->state, vbuf->vb2_buf.index, vdec->seq_tag);
1042 vpu_buf = to_vpu_vb2_buffer(vbuf);
1044 memset(&info, 0, sizeof(info));
1045 info.id = vbuf->vb2_buf.index;
1046 info.type = MEM_RES_FRAME;
1047 info.tag = vdec->seq_tag;
1048 info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0);
1049 info.luma_size = inst->cap_format.sizeimage[0];
1050 if (vbuf->vb2_buf.num_planes > 1)
1051 info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1);
1053 info.chroma_addr = info.luma_addr + info.luma_size;
1054 info.chromau_size = inst->cap_format.sizeimage[1];
1055 info.bytesperline = inst->cap_format.bytesperline[0];
1056 ret = vpu_session_alloc_fs(inst, &info);
1060 vpu_buf->tag = info.tag;
1061 vpu_buf->luma = info.luma_addr;
1062 vpu_buf->chroma_u = info.chroma_addr;
1063 vpu_buf->chroma_v = 0;
1064 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1065 vdec->slots[info.id] = vpu_buf;
1066 vdec->req_frame_count--;
1071 static void vdec_response_fs_request(struct vpu_inst *inst, bool force)
1073 struct vdec_t *vdec = inst->priv;
1078 for (i = vdec->req_frame_count; i > 0; i--)
1079 vdec_response_frame_abnormal(inst);
1083 for (i = vdec->req_frame_count; i > 0; i--) {
1084 ret = vpu_process_capture_buffer(inst);
1087 if (vdec->eos_received)
1091 for (i = vdec->mbi.index; i < vdec->mbi.count; i++) {
1092 if (vdec_response_fs(inst, &vdec->mbi))
1094 if (vdec->eos_received)
1097 for (i = vdec->dcp.index; i < vdec->dcp.count; i++) {
1098 if (vdec_response_fs(inst, &vdec->dcp))
1100 if (vdec->eos_received)
1105 static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag)
1107 struct vpu_fs_info info;
1109 memset(&info, 0, sizeof(info));
1112 vpu_session_release_fs(inst, &info);
1115 static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1117 if (!inst->fh.m2m_ctx)
1119 if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE)
1121 if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index))
1123 v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
1126 static void vdec_clear_slots(struct vpu_inst *inst)
1128 struct vdec_t *vdec = inst->priv;
1129 struct vpu_vb2_buffer *vpu_buf;
1130 struct vb2_v4l2_buffer *vbuf;
1133 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
1134 if (!vdec->slots[i])
1137 vpu_buf = vdec->slots[i];
1138 vbuf = &vpu_buf->m2m_buf.vb;
1140 vpu_trace(inst->dev, "clear slot %d\n", i);
1141 vdec_response_fs_release(inst, i, vpu_buf->tag);
1142 vdec_recycle_buffer(inst, vbuf);
1143 vdec->slots[i]->state = VPU_BUF_STATE_IDLE;
1144 vdec->slots[i] = NULL;
1148 static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)
1150 struct vdec_t *vdec = inst->priv;
1152 vpu_inst_lock(inst);
1153 memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
1155 vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
1157 vdec->codec_info.decoded_width,
1158 vdec->codec_info.decoded_height,
1159 vdec->codec_info.offset_x,
1160 vdec->codec_info.offset_y,
1161 vdec->codec_info.width,
1162 vdec->codec_info.height,
1165 inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;
1166 vdec->is_source_changed = vdec_check_source_change(inst);
1167 vdec_init_fmt(inst);
1168 vdec_init_crop(inst);
1169 vdec_init_mbi(inst);
1170 vdec_init_dcp(inst);
1171 if (!vdec->seq_hdr_found) {
1172 vdec->seq_tag = vdec->codec_info.tag;
1173 if (vdec->is_source_changed) {
1174 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1175 vdec->source_change++;
1176 vdec_handle_resolution_change(inst);
1177 vdec->is_source_changed = false;
1180 if (vdec->seq_tag != vdec->codec_info.tag) {
1181 vdec_response_fs_request(inst, true);
1182 vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n",
1183 inst->id, vdec->seq_tag, vdec->codec_info.tag);
1185 vdec->seq_hdr_found++;
1186 vdec->fixed_fmt = true;
1187 vpu_inst_unlock(inst);
1190 static void vdec_event_resolution_change(struct vpu_inst *inst)
1192 struct vdec_t *vdec = inst->priv;
1194 vpu_trace(inst->dev, "[%d]\n", inst->id);
1195 vpu_inst_lock(inst);
1196 vdec->seq_tag = vdec->codec_info.tag;
1197 vdec_clear_fs(&vdec->mbi);
1198 vdec_clear_fs(&vdec->dcp);
1199 vdec_clear_slots(inst);
1200 vdec_init_mbi(inst);
1201 vdec_init_dcp(inst);
1202 if (vdec->is_source_changed) {
1203 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1204 vdec->source_change++;
1205 vdec_handle_resolution_change(inst);
1206 vdec->is_source_changed = false;
1208 vpu_inst_unlock(inst);
1211 static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1213 struct vdec_t *vdec = inst->priv;
1218 vpu_inst_lock(inst);
1222 vdec->req_frame_count++;
1225 vdec_request_one_fs(&vdec->mbi);
1228 vdec_request_one_fs(&vdec->dcp);
1234 vdec_alloc_fs(inst, &vdec->mbi);
1235 vdec_alloc_fs(inst, &vdec->dcp);
1237 vdec_response_fs_request(inst, false);
1239 vpu_inst_unlock(inst);
1242 static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1244 struct vdec_t *vdec = inst->priv;
1245 struct vpu_vb2_buffer *vpu_buf;
1246 struct vb2_v4l2_buffer *vbuf;
1248 if (!fs || fs->id >= ARRAY_SIZE(vdec->slots))
1250 if (fs->type != MEM_RES_FRAME)
1253 if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) {
1254 dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id);
1258 vpu_inst_lock(inst);
1259 vpu_buf = vdec->slots[fs->id];
1260 vdec->slots[fs->id] = NULL;
1263 dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id);
1267 vbuf = &vpu_buf->m2m_buf.vb;
1268 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) {
1269 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
1273 vdec_response_fs_release(inst, fs->id, vpu_buf->tag);
1274 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY)
1275 vdec_recycle_buffer(inst, vbuf);
1277 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
1278 vpu_process_capture_buffer(inst);
1281 vpu_inst_unlock(inst);
1284 static void vdec_event_eos(struct vpu_inst *inst)
1286 struct vdec_t *vdec = inst->priv;
1288 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1290 vdec->params.frame_count,
1291 vdec->decoded_frame_count,
1292 vdec->display_frame_count,
1294 vpu_inst_lock(inst);
1295 vdec->eos_received++;
1296 vdec->fixed_fmt = false;
1297 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1298 vdec_set_last_buffer_dequeued(inst);
1299 vpu_inst_unlock(inst);
1302 static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data)
1305 case VPU_MSG_ID_SEQ_HDR_FOUND:
1306 vdec_event_seq_hdr(inst, data);
1308 case VPU_MSG_ID_RES_CHANGE:
1309 vdec_event_resolution_change(inst);
1311 case VPU_MSG_ID_FRAME_REQ:
1312 vdec_event_req_fs(inst, data);
1314 case VPU_MSG_ID_FRAME_RELEASE:
1315 vdec_evnet_rel_fs(inst, data);
1317 case VPU_MSG_ID_PIC_EOS:
1318 vdec_event_eos(inst);
1325 static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1327 struct vdec_t *vdec = inst->priv;
1328 struct vb2_v4l2_buffer *vbuf;
1329 struct vpu_rpc_buffer_desc desc;
1333 vbuf = to_vb2_v4l2_buffer(vb);
1334 dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n",
1335 inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0));
1337 if (inst->state == VPU_CODEC_STATE_DEINIT)
1339 if (vdec->reset_codec)
1342 if (inst->state == VPU_CODEC_STATE_STARTED)
1343 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
1345 ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1349 free_space = vpu_helper_get_free_space(inst);
1350 if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000)
1353 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1354 ret = vpu_iface_input_frame(inst, vb);
1358 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp);
1359 vdec->params.frame_count++;
1367 static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1369 struct vdec_t *vdec = inst->priv;
1370 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1373 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
1375 if (vdec->reset_codec)
1378 ret = vdec_response_frame(inst, vbuf);
1381 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1385 static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type)
1387 struct vdec_t *vdec = inst->priv;
1389 if (V4L2_TYPE_IS_OUTPUT(type))
1392 vdec_handle_resolution_change(inst);
1393 if (vdec->eos_received)
1394 vdec_set_last_buffer_dequeued(inst);
1397 static void vdec_abort(struct vpu_inst *inst)
1399 struct vdec_t *vdec = inst->priv;
1400 struct vpu_rpc_buffer_desc desc;
1403 vpu_trace(inst->dev, "[%d] state = %d\n", inst->id, inst->state);
1405 vdec->aborting = true;
1406 vpu_iface_add_scode(inst, SCODE_PADDING_ABORT);
1407 vdec->params.end_flag = 1;
1408 vpu_iface_set_decode_params(inst, &vdec->params, 1);
1410 vpu_session_abort(inst);
1412 ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1414 vpu_iface_update_stream_buffer(inst, desc.rptr, 1);
1416 vpu_session_rst_buf(inst);
1417 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1419 vdec->params.frame_count,
1420 vdec->decoded_frame_count,
1421 vdec->display_frame_count,
1423 if (!vdec->seq_hdr_found)
1424 vdec->reset_codec = true;
1425 vdec->params.end_flag = 0;
1427 vdec->params.frame_count = 0;
1428 vdec->decoded_frame_count = 0;
1429 vdec->display_frame_count = 0;
1431 vdec->aborting = false;
1432 inst->extra_size = 0;
1435 static void vdec_stop(struct vpu_inst *inst, bool free)
1437 struct vdec_t *vdec = inst->priv;
1439 vdec_clear_slots(inst);
1440 if (inst->state != VPU_CODEC_STATE_DEINIT)
1441 vpu_session_stop(inst);
1442 vdec_clear_fs(&vdec->mbi);
1443 vdec_clear_fs(&vdec->dcp);
1445 vpu_free_dma(&vdec->udata);
1446 vpu_free_dma(&inst->stream_buffer);
1448 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1);
1449 vdec->reset_codec = false;
1452 static void vdec_release(struct vpu_inst *inst)
1454 if (inst->id != VPU_INST_NULL_ID)
1455 vpu_trace(inst->dev, "[%d]\n", inst->id);
1456 vpu_inst_lock(inst);
1457 vdec_stop(inst, true);
1458 vpu_inst_unlock(inst);
1461 static void vdec_cleanup(struct vpu_inst *inst)
1463 struct vdec_t *vdec;
1474 static void vdec_init_params(struct vdec_t *vdec)
1476 vdec->params.frame_count = 0;
1477 vdec->params.end_flag = 0;
1480 static int vdec_start(struct vpu_inst *inst)
1482 struct vdec_t *vdec = inst->priv;
1483 int stream_buffer_size;
1486 if (inst->state != VPU_CODEC_STATE_DEINIT)
1489 vpu_trace(inst->dev, "[%d]\n", inst->id);
1490 if (!vdec->udata.virt) {
1491 vdec->udata.length = 0x1000;
1492 ret = vpu_alloc_dma(inst->core, &vdec->udata);
1494 dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id);
1499 if (!inst->stream_buffer.virt) {
1500 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
1501 if (stream_buffer_size > 0) {
1502 inst->stream_buffer.length = stream_buffer_size;
1503 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
1505 dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id);
1508 inst->use_stream_buffer = true;
1512 if (inst->use_stream_buffer)
1513 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
1514 vpu_iface_init_instance(inst);
1515 vdec->params.udata.base = vdec->udata.phys;
1516 vdec->params.udata.size = vdec->udata.length;
1517 ret = vpu_iface_set_decode_params(inst, &vdec->params, 0);
1519 dev_err(inst->dev, "[%d] set decode params fail\n", inst->id);
1523 vdec_init_params(vdec);
1524 ret = vpu_session_start(inst);
1526 dev_err(inst->dev, "[%d] start fail\n", inst->id);
1530 vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0);
1534 vpu_free_dma(&vdec->udata);
1535 vpu_free_dma(&inst->stream_buffer);
1539 static int vdec_start_session(struct vpu_inst *inst, u32 type)
1541 struct vdec_t *vdec = inst->priv;
1544 if (V4L2_TYPE_IS_OUTPUT(type)) {
1545 if (vdec->reset_codec)
1546 vdec_stop(inst, false);
1547 if (inst->state == VPU_CODEC_STATE_DEINIT) {
1548 ret = vdec_start(inst);
1554 if (V4L2_TYPE_IS_OUTPUT(type)) {
1555 vdec_update_state(inst, vdec->state, 1);
1556 vdec->eos_received = 0;
1557 vpu_process_output_buffer(inst);
1559 vdec_cmd_start(inst);
1561 if (inst->state == VPU_CODEC_STATE_ACTIVE)
1562 vdec_response_fs_request(inst, false);
1567 static int vdec_stop_session(struct vpu_inst *inst, u32 type)
1569 struct vdec_t *vdec = inst->priv;
1571 if (inst->state == VPU_CODEC_STATE_DEINIT)
1574 if (V4L2_TYPE_IS_OUTPUT(type)) {
1575 vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0);
1578 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) {
1580 vdec->eos_received = 0;
1582 vdec_clear_slots(inst);
1588 static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1590 struct vdec_t *vdec = inst->priv;
1595 num = scnprintf(str, size,
1596 "req_frame_count = %d\ninterlaced = %d\n",
1597 vdec->req_frame_count,
1598 vdec->codec_info.progressive ? 0 : 1);
1601 num = scnprintf(str, size,
1602 "mbi: size = 0x%x request = %d, alloc = %d, response = %d\n",
1604 vdec->mbi.req_count,
1609 num = scnprintf(str, size,
1610 "dcp: size = 0x%x request = %d, alloc = %d, response = %d\n",
1612 vdec->dcp.req_count,
1617 num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count);
1620 num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count);
1623 num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count);
1626 num = scnprintf(str, size, "sequence = %d\n", vdec->sequence);
1629 num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n",
1630 vdec->drain, vdec->eos_received, vdec->source_change);
1633 num = scnprintf(str, size, "fps = %d/%d\n",
1634 vdec->codec_info.frame_rate.numerator,
1635 vdec->codec_info.frame_rate.denominator);
1638 num = scnprintf(str, size, "colorspace: %d, %d, %d, %d (%d)\n",
1639 vdec->codec_info.color_primaries,
1640 vdec->codec_info.transfer_chars,
1641 vdec->codec_info.matrix_coeffs,
1642 vdec->codec_info.full_range,
1643 vdec->codec_info.vui_present);
1652 static struct vpu_inst_ops vdec_inst_ops = {
1653 .ctrl_init = vdec_ctrl_init,
1654 .check_ready = vdec_check_ready,
1655 .buf_done = vdec_buf_done,
1656 .get_one_frame = vdec_frame_decoded,
1657 .stop_done = vdec_stop_done,
1658 .event_notify = vdec_event_notify,
1659 .release = vdec_release,
1660 .cleanup = vdec_cleanup,
1661 .start = vdec_start_session,
1662 .stop = vdec_stop_session,
1663 .process_output = vdec_process_output,
1664 .process_capture = vdec_process_capture,
1665 .on_queue_empty = vdec_on_queue_empty,
1666 .get_debug_info = vdec_get_debug_info,
1667 .wait_prepare = vpu_inst_unlock,
1668 .wait_finish = vpu_inst_lock,
1671 static void vdec_init(struct file *file)
1673 struct vpu_inst *inst = to_inst(file);
1674 struct v4l2_format f;
1676 memset(&f, 0, sizeof(f));
1677 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1678 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1679 f.fmt.pix_mp.width = 1280;
1680 f.fmt.pix_mp.height = 720;
1681 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1682 vdec_s_fmt(file, &inst->fh, &f);
1684 memset(&f, 0, sizeof(f));
1685 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1686 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M_8L128;
1687 f.fmt.pix_mp.width = 1280;
1688 f.fmt.pix_mp.height = 720;
1689 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1690 vdec_s_fmt(file, &inst->fh, &f);
1693 static int vdec_open(struct file *file)
1695 struct vpu_inst *inst;
1696 struct vdec_t *vdec;
1699 inst = vzalloc(sizeof(*inst));
1703 vdec = vzalloc(sizeof(*vdec));
1709 inst->ops = &vdec_inst_ops;
1710 inst->formats = vdec_formats;
1711 inst->type = VPU_CORE_TYPE_DEC;
1714 ret = vpu_v4l2_open(file, inst);
1718 vdec->fixed_fmt = false;
1719 vdec->state = VPU_CODEC_STATE_ACTIVE;
1720 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1721 inst->min_buffer_out = VDEC_MIN_BUFFER_OUT;
1727 static const struct v4l2_file_operations vdec_fops = {
1728 .owner = THIS_MODULE,
1730 .release = vpu_v4l2_close,
1731 .unlocked_ioctl = video_ioctl2,
1732 .poll = v4l2_m2m_fop_poll,
1733 .mmap = v4l2_m2m_fop_mmap,
1736 const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void)
1738 return &vdec_ioctl_ops;
1741 const struct v4l2_file_operations *vdec_get_fops(void)