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;
68 static const struct vpu_format vdec_formats[] = {
70 .pixfmt = V4L2_PIX_FMT_NV12M_8L128,
72 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
75 .pixfmt = V4L2_PIX_FMT_NV12M_10BE_8L128,
77 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
80 .pixfmt = V4L2_PIX_FMT_H264,
82 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
83 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
86 .pixfmt = V4L2_PIX_FMT_H264_MVC,
88 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
89 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
92 .pixfmt = V4L2_PIX_FMT_HEVC,
94 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
95 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
98 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
100 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
101 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
104 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
106 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
107 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
110 .pixfmt = V4L2_PIX_FMT_MPEG2,
112 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
113 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
116 .pixfmt = V4L2_PIX_FMT_MPEG4,
118 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
119 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
122 .pixfmt = V4L2_PIX_FMT_XVID,
124 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
125 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
128 .pixfmt = V4L2_PIX_FMT_VP8,
130 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
131 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
134 .pixfmt = V4L2_PIX_FMT_H263,
136 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
137 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
142 static const struct v4l2_ctrl_ops vdec_ctrl_ops = {
143 .g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
146 static int vdec_ctrl_init(struct vpu_inst *inst)
148 struct v4l2_ctrl *ctrl;
151 ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
155 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
156 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
158 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
160 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
161 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
163 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
165 if (inst->ctrl_handler.error) {
166 ret = inst->ctrl_handler.error;
167 v4l2_ctrl_handler_free(&inst->ctrl_handler);
171 ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
173 dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
174 v4l2_ctrl_handler_free(&inst->ctrl_handler);
181 static void vdec_set_last_buffer_dequeued(struct vpu_inst *inst)
183 struct vdec_t *vdec = inst->priv;
185 if (vdec->eos_received) {
186 if (!vpu_set_last_buffer_dequeued(inst))
187 vdec->eos_received--;
191 static void vdec_handle_resolution_change(struct vpu_inst *inst)
193 struct vdec_t *vdec = inst->priv;
196 if (!inst->fh.m2m_ctx)
199 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
201 if (!vdec->source_change)
204 q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
205 if (!list_empty(&q->done_list))
208 vdec->source_change--;
209 vpu_notify_source_change(inst);
212 static int vdec_update_state(struct vpu_inst *inst, enum vpu_codec_state state, u32 force)
214 struct vdec_t *vdec = inst->priv;
215 enum vpu_codec_state pre_state = inst->state;
217 if (state == VPU_CODEC_STATE_SEEK) {
218 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
219 vdec->state = inst->state;
221 vdec->state = VPU_CODEC_STATE_ACTIVE;
223 if (inst->state != VPU_CODEC_STATE_SEEK || force)
225 else if (state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
226 vdec->state = VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE;
228 if (inst->state != pre_state)
229 vpu_trace(inst->dev, "[%d] %d -> %d\n", inst->id, pre_state, inst->state);
231 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
232 vdec_handle_resolution_change(inst);
237 static int vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
239 strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
240 strscpy(cap->card, "amphion vpu decoder", sizeof(cap->card));
241 strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
246 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
248 struct vpu_inst *inst = to_inst(file);
249 struct vdec_t *vdec = inst->priv;
250 const struct vpu_format *fmt;
254 if (!V4L2_TYPE_IS_OUTPUT(f->type) && vdec->fixed_fmt) {
256 f->pixelformat = inst->cap_format.pixfmt;
257 f->flags = inst->cap_format.flags;
261 fmt = vpu_helper_enum_format(inst, f->type, f->index);
262 memset(f->reserved, 0, sizeof(f->reserved));
266 f->pixelformat = fmt->pixfmt;
267 f->flags = fmt->flags;
272 vpu_inst_unlock(inst);
276 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
278 struct vpu_inst *inst = to_inst(file);
279 struct vdec_t *vdec = inst->priv;
280 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
281 struct vpu_format *cur_fmt;
284 cur_fmt = vpu_get_format(inst, f->type);
286 pixmp->pixelformat = cur_fmt->pixfmt;
287 pixmp->num_planes = cur_fmt->num_planes;
288 pixmp->width = cur_fmt->width;
289 pixmp->height = cur_fmt->height;
290 pixmp->field = cur_fmt->field;
291 pixmp->flags = cur_fmt->flags;
292 for (i = 0; i < pixmp->num_planes; i++) {
293 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
294 pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i];
297 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
298 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
299 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
300 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
305 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
307 struct vpu_inst *inst = to_inst(file);
308 struct vdec_t *vdec = inst->priv;
310 vpu_try_fmt_common(inst, f);
313 if (vdec->fixed_fmt) {
314 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
315 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
316 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
317 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
319 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
320 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
321 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
322 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
324 vpu_inst_unlock(inst);
329 static int vdec_s_fmt_common(struct vpu_inst *inst, struct v4l2_format *f)
331 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
332 const struct vpu_format *fmt;
333 struct vpu_format *cur_fmt;
335 struct vdec_t *vdec = inst->priv;
338 if (!inst->fh.m2m_ctx)
341 q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
347 fmt = vpu_try_fmt_common(inst, f);
351 cur_fmt = vpu_get_format(inst, f->type);
352 if (V4L2_TYPE_IS_OUTPUT(f->type) && inst->state != VPU_CODEC_STATE_DEINIT) {
353 if (cur_fmt->pixfmt != fmt->pixfmt) {
354 vdec->reset_codec = true;
355 vdec->fixed_fmt = false;
358 cur_fmt->pixfmt = fmt->pixfmt;
359 if (V4L2_TYPE_IS_OUTPUT(f->type) || !vdec->fixed_fmt) {
360 cur_fmt->num_planes = fmt->num_planes;
361 cur_fmt->flags = fmt->flags;
362 cur_fmt->width = pixmp->width;
363 cur_fmt->height = pixmp->height;
364 for (i = 0; i < fmt->num_planes; i++) {
365 cur_fmt->sizeimage[i] = pixmp->plane_fmt[i].sizeimage;
366 cur_fmt->bytesperline[i] = pixmp->plane_fmt[i].bytesperline;
368 if (pixmp->field != V4L2_FIELD_ANY)
369 cur_fmt->field = pixmp->field;
371 pixmp->num_planes = cur_fmt->num_planes;
372 pixmp->width = cur_fmt->width;
373 pixmp->height = cur_fmt->height;
374 for (i = 0; i < pixmp->num_planes; i++) {
375 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
376 pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i];
378 pixmp->field = cur_fmt->field;
381 if (!vdec->fixed_fmt) {
382 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
383 vdec->params.codec_format = cur_fmt->pixfmt;
384 vdec->codec_info.color_primaries = f->fmt.pix_mp.colorspace;
385 vdec->codec_info.transfer_chars = f->fmt.pix_mp.xfer_func;
386 vdec->codec_info.matrix_coeffs = f->fmt.pix_mp.ycbcr_enc;
387 vdec->codec_info.full_range = f->fmt.pix_mp.quantization;
389 vdec->params.output_format = cur_fmt->pixfmt;
392 inst->crop.width = cur_fmt->width;
393 inst->crop.height = cur_fmt->height;
397 vpu_trace(inst->dev, "[%d] %c%c%c%c %dx%d\n", inst->id,
398 f->fmt.pix_mp.pixelformat,
399 f->fmt.pix_mp.pixelformat >> 8,
400 f->fmt.pix_mp.pixelformat >> 16,
401 f->fmt.pix_mp.pixelformat >> 24,
403 f->fmt.pix_mp.height);
408 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
410 struct vpu_inst *inst = to_inst(file);
411 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
412 struct vdec_t *vdec = inst->priv;
416 ret = vdec_s_fmt_common(inst, f);
420 if (V4L2_TYPE_IS_OUTPUT(f->type) && !vdec->fixed_fmt) {
421 struct v4l2_format fc;
423 memset(&fc, 0, sizeof(fc));
424 fc.type = inst->cap_format.type;
425 fc.fmt.pix_mp.pixelformat = inst->cap_format.pixfmt;
426 fc.fmt.pix_mp.width = pixmp->width;
427 fc.fmt.pix_mp.height = pixmp->height;
428 vdec_s_fmt_common(inst, &fc);
431 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
432 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
433 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
434 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
437 vpu_inst_unlock(inst);
441 static int vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
443 struct vpu_inst *inst = to_inst(file);
445 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
449 case V4L2_SEL_TGT_COMPOSE:
450 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
451 case V4L2_SEL_TGT_COMPOSE_PADDED:
454 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
457 s->r.width = inst->cap_format.width;
458 s->r.height = inst->cap_format.height;
467 static int vdec_drain(struct vpu_inst *inst)
469 struct vdec_t *vdec = inst->priv;
471 if (!inst->fh.m2m_ctx)
477 if (!vpu_is_source_empty(inst))
480 if (!vdec->params.frame_count) {
481 vpu_set_last_buffer_dequeued(inst);
485 vpu_iface_add_scode(inst, SCODE_PADDING_EOS);
486 vdec->params.end_flag = 1;
487 vpu_iface_set_decode_params(inst, &vdec->params, 1);
489 vpu_trace(inst->dev, "[%d] frame_count = %d\n", inst->id, vdec->params.frame_count);
494 static int vdec_cmd_start(struct vpu_inst *inst)
496 switch (inst->state) {
497 case VPU_CODEC_STATE_STARTED:
498 case VPU_CODEC_STATE_DRAIN:
499 case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE:
500 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
505 vpu_process_capture_buffer(inst);
509 static int vdec_cmd_stop(struct vpu_inst *inst)
511 struct vdec_t *vdec = inst->priv;
513 vpu_trace(inst->dev, "[%d]\n", inst->id);
515 if (inst->state == VPU_CODEC_STATE_DEINIT) {
516 vpu_set_last_buffer_dequeued(inst);
525 static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
527 struct vpu_inst *inst = to_inst(file);
530 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
536 case V4L2_DEC_CMD_START:
537 vdec_cmd_start(inst);
539 case V4L2_DEC_CMD_STOP:
545 vpu_inst_unlock(inst);
550 static int vdec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
554 return v4l2_event_subscribe(fh, sub, 0, NULL);
555 case V4L2_EVENT_SOURCE_CHANGE:
556 return v4l2_src_change_event_subscribe(fh, sub);
557 case V4L2_EVENT_CTRL:
558 return v4l2_ctrl_subscribe_event(fh, sub);
566 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
567 .vidioc_querycap = vdec_querycap,
568 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
569 .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
570 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
571 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
572 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
573 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
574 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
575 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
576 .vidioc_g_selection = vdec_g_selection,
577 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
578 .vidioc_decoder_cmd = vdec_decoder_cmd,
579 .vidioc_subscribe_event = vdec_subscribe_event,
580 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
581 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
582 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
583 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
584 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
585 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
586 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
587 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
588 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
589 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
592 static bool vdec_check_ready(struct vpu_inst *inst, unsigned int type)
594 struct vdec_t *vdec = inst->priv;
596 if (V4L2_TYPE_IS_OUTPUT(type))
599 if (vdec->req_frame_count)
605 static struct vb2_v4l2_buffer *vdec_get_src_buffer(struct vpu_inst *inst, u32 count)
608 vpu_skip_frame(inst, count - 1);
610 return vpu_next_src_buf(inst);
613 static int vdec_frame_decoded(struct vpu_inst *inst, void *arg)
615 struct vdec_t *vdec = inst->priv;
616 struct vpu_dec_pic_info *info = arg;
617 struct vpu_vb2_buffer *vpu_buf;
618 struct vb2_v4l2_buffer *vbuf;
619 struct vb2_v4l2_buffer *src_buf;
622 if (!info || info->id >= ARRAY_SIZE(vdec->slots))
626 vpu_buf = vdec->slots[info->id];
628 dev_err(inst->dev, "[%d] decoded invalid frame[%d]\n", inst->id, info->id);
632 vbuf = &vpu_buf->m2m_buf.vb;
633 src_buf = vdec_get_src_buffer(inst, info->consumed_count);
635 v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
636 if (info->consumed_count) {
637 v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
638 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
639 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
641 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_DECODED);
644 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED)
645 dev_info(inst->dev, "[%d] buf[%d] has been decoded\n", inst->id, info->id);
646 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_DECODED);
647 vdec->decoded_frame_count++;
649 vpu_inst_unlock(inst);
654 static struct vpu_vb2_buffer *vdec_find_buffer(struct vpu_inst *inst, u32 luma)
656 struct vdec_t *vdec = inst->priv;
659 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
662 if (luma == vdec->slots[i]->luma)
663 return vdec->slots[i];
669 static void vdec_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame)
671 struct vdec_t *vdec = inst->priv;
672 struct vpu_vb2_buffer *vpu_buf;
673 struct vb2_v4l2_buffer *vbuf;
680 sequence = vdec->sequence++;
681 vpu_buf = vdec_find_buffer(inst, frame->luma);
682 vpu_inst_unlock(inst);
684 dev_err(inst->dev, "[%d] can't find buffer, id = %d, addr = 0x%x\n",
685 inst->id, frame->id, frame->luma);
688 if (frame->skipped) {
689 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
693 vbuf = &vpu_buf->m2m_buf.vb;
694 if (vbuf->vb2_buf.index != frame->id)
695 dev_err(inst->dev, "[%d] buffer id(%d, %d) dismatch\n",
696 inst->id, vbuf->vb2_buf.index, frame->id);
698 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_DECODED)
699 dev_err(inst->dev, "[%d] buffer(%d) ready without decoded\n", inst->id, frame->id);
700 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY);
701 vb2_set_plane_payload(&vbuf->vb2_buf, 0, inst->cap_format.sizeimage[0]);
702 vb2_set_plane_payload(&vbuf->vb2_buf, 1, inst->cap_format.sizeimage[1]);
703 vbuf->field = inst->cap_format.field;
704 vbuf->sequence = sequence;
705 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
707 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
709 vdec->display_frame_count++;
710 vpu_inst_unlock(inst);
711 dev_dbg(inst->dev, "[%d] decoded : %d, display : %d, sequence : %d\n",
712 inst->id, vdec->decoded_frame_count, vdec->display_frame_count, vdec->sequence);
715 static void vdec_stop_done(struct vpu_inst *inst)
717 struct vdec_t *vdec = inst->priv;
720 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 0);
721 vdec->seq_hdr_found = 0;
722 vdec->req_frame_count = 0;
723 vdec->reset_codec = false;
724 vdec->fixed_fmt = false;
725 vdec->params.end_flag = 0;
727 vdec->params.frame_count = 0;
728 vdec->decoded_frame_count = 0;
729 vdec->display_frame_count = 0;
731 vdec->eos_received = 0;
732 vdec->is_source_changed = false;
733 vdec->source_change = 0;
734 vpu_inst_unlock(inst);
737 static bool vdec_check_source_change(struct vpu_inst *inst)
739 struct vdec_t *vdec = inst->priv;
740 const struct vpu_format *fmt;
743 if (!inst->fh.m2m_ctx)
746 if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)))
748 fmt = vpu_helper_find_format(inst, inst->cap_format.type, vdec->codec_info.pixfmt);
749 if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
751 if (inst->cap_format.width != vdec->codec_info.decoded_width)
753 if (inst->cap_format.height != vdec->codec_info.decoded_height)
755 if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap)
757 if (inst->crop.left != vdec->codec_info.offset_x)
759 if (inst->crop.top != vdec->codec_info.offset_y)
761 if (inst->crop.width != vdec->codec_info.width)
763 if (inst->crop.height != vdec->codec_info.height)
765 if (fmt && inst->cap_format.num_planes != fmt->num_planes)
767 for (i = 0; i < inst->cap_format.num_planes; i++) {
768 if (inst->cap_format.bytesperline[i] != vdec->codec_info.bytesperline[i])
770 if (inst->cap_format.sizeimage[i] != vdec->codec_info.sizeimage[i])
777 static void vdec_init_fmt(struct vpu_inst *inst)
779 struct vdec_t *vdec = inst->priv;
780 const struct vpu_format *fmt;
783 fmt = vpu_helper_find_format(inst, inst->cap_format.type, vdec->codec_info.pixfmt);
784 inst->out_format.width = vdec->codec_info.width;
785 inst->out_format.height = vdec->codec_info.height;
786 inst->cap_format.width = vdec->codec_info.decoded_width;
787 inst->cap_format.height = vdec->codec_info.decoded_height;
788 inst->cap_format.pixfmt = vdec->codec_info.pixfmt;
790 inst->cap_format.num_planes = fmt->num_planes;
791 inst->cap_format.flags = fmt->flags;
793 for (i = 0; i < inst->cap_format.num_planes; i++) {
794 inst->cap_format.bytesperline[i] = vdec->codec_info.bytesperline[i];
795 inst->cap_format.sizeimage[i] = vdec->codec_info.sizeimage[i];
797 if (vdec->codec_info.progressive)
798 inst->cap_format.field = V4L2_FIELD_NONE;
800 inst->cap_format.field = V4L2_FIELD_SEQ_TB;
801 if (vdec->codec_info.color_primaries == V4L2_COLORSPACE_DEFAULT)
802 vdec->codec_info.color_primaries = V4L2_COLORSPACE_REC709;
803 if (vdec->codec_info.transfer_chars == V4L2_XFER_FUNC_DEFAULT)
804 vdec->codec_info.transfer_chars = V4L2_XFER_FUNC_709;
805 if (vdec->codec_info.matrix_coeffs == V4L2_YCBCR_ENC_DEFAULT)
806 vdec->codec_info.matrix_coeffs = V4L2_YCBCR_ENC_709;
807 if (vdec->codec_info.full_range == V4L2_QUANTIZATION_DEFAULT)
808 vdec->codec_info.full_range = V4L2_QUANTIZATION_LIM_RANGE;
811 static void vdec_init_crop(struct vpu_inst *inst)
813 struct vdec_t *vdec = inst->priv;
815 inst->crop.left = vdec->codec_info.offset_x;
816 inst->crop.top = vdec->codec_info.offset_y;
817 inst->crop.width = vdec->codec_info.width;
818 inst->crop.height = vdec->codec_info.height;
821 static void vdec_init_mbi(struct vpu_inst *inst)
823 struct vdec_t *vdec = inst->priv;
825 vdec->mbi.size = vdec->codec_info.mbi_size;
826 vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer);
827 scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi");
828 vdec->mbi.type = MEM_RES_MBI;
829 vdec->mbi.tag = vdec->seq_tag;
832 static void vdec_init_dcp(struct vpu_inst *inst)
834 struct vdec_t *vdec = inst->priv;
836 vdec->dcp.size = vdec->codec_info.dcp_size;
837 vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer);
838 scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp");
839 vdec->dcp.type = MEM_RES_DCP;
840 vdec->dcp.tag = vdec->seq_tag;
843 static void vdec_request_one_fs(struct vdec_fs_info *fs)
846 if (fs->req_count > fs->max_count)
847 fs->req_count = fs->max_count;
850 static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs)
852 struct vpu_buffer *buffer;
857 if (fs->count >= fs->req_count)
860 buffer = &fs->buffer[fs->count];
861 if (buffer->virt && buffer->length >= fs->size)
864 vpu_free_dma(buffer);
865 buffer->length = fs->size;
866 return vpu_alloc_dma(inst->core, buffer);
869 static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
873 while (fs->count < fs->req_count) {
874 ret = vdec_alloc_fs_buffer(inst, fs);
881 static void vdec_clear_fs(struct vdec_fs_info *fs)
888 for (i = 0; i < ARRAY_SIZE(fs->buffer); i++)
889 vpu_free_dma(&fs->buffer[i]);
890 memset(fs, 0, sizeof(*fs));
893 static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
895 struct vpu_fs_info info;
898 if (fs->index >= fs->count)
901 memset(&info, 0, sizeof(info));
903 info.type = fs->type;
905 info.luma_addr = fs->buffer[fs->index].phys;
906 info.luma_size = fs->buffer[fs->index].length;
907 ret = vpu_session_alloc_fs(inst, &info);
915 static int vdec_response_frame_abnormal(struct vpu_inst *inst)
917 struct vdec_t *vdec = inst->priv;
918 struct vpu_fs_info info;
920 if (!vdec->req_frame_count)
923 memset(&info, 0, sizeof(info));
924 info.type = MEM_RES_FRAME;
925 info.tag = vdec->seq_tag + 0xf0;
926 vpu_session_alloc_fs(inst, &info);
927 vdec->req_frame_count--;
932 static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
934 struct vdec_t *vdec = inst->priv;
935 struct vpu_vb2_buffer *vpu_buf;
936 struct vpu_fs_info info;
939 if (inst->state != VPU_CODEC_STATE_ACTIVE)
942 if (!vdec->req_frame_count)
948 if (vdec->slots[vbuf->vb2_buf.index]) {
949 dev_err(inst->dev, "[%d] repeat alloc fs %d\n",
950 inst->id, vbuf->vb2_buf.index);
954 dev_dbg(inst->dev, "[%d] state = %d, alloc fs %d, tag = 0x%x\n",
955 inst->id, inst->state, vbuf->vb2_buf.index, vdec->seq_tag);
956 vpu_buf = to_vpu_vb2_buffer(vbuf);
958 memset(&info, 0, sizeof(info));
959 info.id = vbuf->vb2_buf.index;
960 info.type = MEM_RES_FRAME;
961 info.tag = vdec->seq_tag;
962 info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0);
963 info.luma_size = inst->cap_format.sizeimage[0];
964 info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1);
965 info.chromau_size = inst->cap_format.sizeimage[1];
966 info.bytesperline = inst->cap_format.bytesperline[0];
967 ret = vpu_session_alloc_fs(inst, &info);
971 vpu_buf->tag = info.tag;
972 vpu_buf->luma = info.luma_addr;
973 vpu_buf->chroma_u = info.chromau_size;
974 vpu_buf->chroma_v = 0;
975 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
976 vdec->slots[info.id] = vpu_buf;
977 vdec->req_frame_count--;
982 static void vdec_response_fs_request(struct vpu_inst *inst, bool force)
984 struct vdec_t *vdec = inst->priv;
989 for (i = vdec->req_frame_count; i > 0; i--)
990 vdec_response_frame_abnormal(inst);
994 for (i = vdec->req_frame_count; i > 0; i--) {
995 ret = vpu_process_capture_buffer(inst);
998 if (vdec->eos_received)
1002 for (i = vdec->mbi.index; i < vdec->mbi.count; i++) {
1003 if (vdec_response_fs(inst, &vdec->mbi))
1005 if (vdec->eos_received)
1008 for (i = vdec->dcp.index; i < vdec->dcp.count; i++) {
1009 if (vdec_response_fs(inst, &vdec->dcp))
1011 if (vdec->eos_received)
1016 static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag)
1018 struct vpu_fs_info info;
1020 memset(&info, 0, sizeof(info));
1023 vpu_session_release_fs(inst, &info);
1026 static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1028 if (!inst->fh.m2m_ctx)
1030 if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE)
1032 if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index))
1034 v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
1037 static void vdec_clear_slots(struct vpu_inst *inst)
1039 struct vdec_t *vdec = inst->priv;
1040 struct vpu_vb2_buffer *vpu_buf;
1041 struct vb2_v4l2_buffer *vbuf;
1044 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
1045 if (!vdec->slots[i])
1048 vpu_buf = vdec->slots[i];
1049 vbuf = &vpu_buf->m2m_buf.vb;
1051 vdec_recycle_buffer(inst, vbuf);
1052 vdec->slots[i]->state = VPU_BUF_STATE_IDLE;
1053 vdec->slots[i] = NULL;
1057 static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)
1059 struct vdec_t *vdec = inst->priv;
1061 vpu_inst_lock(inst);
1062 memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
1064 vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
1066 vdec->codec_info.decoded_width,
1067 vdec->codec_info.decoded_height,
1068 vdec->codec_info.offset_x,
1069 vdec->codec_info.offset_y,
1070 vdec->codec_info.width,
1071 vdec->codec_info.height,
1074 inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;
1075 vdec->is_source_changed = vdec_check_source_change(inst);
1076 vdec_init_fmt(inst);
1077 vdec_init_crop(inst);
1078 vdec_init_mbi(inst);
1079 vdec_init_dcp(inst);
1080 if (!vdec->seq_hdr_found) {
1081 vdec->seq_tag = vdec->codec_info.tag;
1082 if (vdec->is_source_changed) {
1083 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1084 vpu_notify_source_change(inst);
1085 vdec->is_source_changed = false;
1088 if (vdec->seq_tag != vdec->codec_info.tag) {
1089 vdec_response_fs_request(inst, true);
1090 vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n",
1091 inst->id, vdec->seq_tag, vdec->codec_info.tag);
1093 vdec->seq_hdr_found++;
1094 vdec->fixed_fmt = true;
1095 vpu_inst_unlock(inst);
1098 static void vdec_event_resolution_change(struct vpu_inst *inst)
1100 struct vdec_t *vdec = inst->priv;
1102 vpu_trace(inst->dev, "[%d]\n", inst->id);
1103 vpu_inst_lock(inst);
1104 vdec->seq_tag = vdec->codec_info.tag;
1105 vdec_clear_fs(&vdec->mbi);
1106 vdec_clear_fs(&vdec->dcp);
1107 vdec_clear_slots(inst);
1108 vdec_init_mbi(inst);
1109 vdec_init_dcp(inst);
1110 if (vdec->is_source_changed) {
1111 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1112 vdec->source_change++;
1113 vdec_handle_resolution_change(inst);
1114 vdec->is_source_changed = false;
1116 vpu_inst_unlock(inst);
1119 static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1121 struct vdec_t *vdec = inst->priv;
1126 vpu_inst_lock(inst);
1130 vdec->req_frame_count++;
1133 vdec_request_one_fs(&vdec->mbi);
1136 vdec_request_one_fs(&vdec->dcp);
1142 vdec_alloc_fs(inst, &vdec->mbi);
1143 vdec_alloc_fs(inst, &vdec->dcp);
1145 vdec_response_fs_request(inst, false);
1147 vpu_inst_unlock(inst);
1150 static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1152 struct vdec_t *vdec = inst->priv;
1153 struct vpu_vb2_buffer *vpu_buf;
1154 struct vb2_v4l2_buffer *vbuf;
1156 if (!fs || fs->id >= ARRAY_SIZE(vdec->slots))
1158 if (fs->type != MEM_RES_FRAME)
1161 if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) {
1162 dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id);
1166 vpu_inst_lock(inst);
1167 vpu_buf = vdec->slots[fs->id];
1168 vdec->slots[fs->id] = NULL;
1171 dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id);
1175 vbuf = &vpu_buf->m2m_buf.vb;
1176 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) {
1177 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
1181 vdec_response_fs_release(inst, fs->id, vpu_buf->tag);
1182 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY)
1183 vdec_recycle_buffer(inst, vbuf);
1185 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
1186 vpu_process_capture_buffer(inst);
1189 vpu_inst_unlock(inst);
1192 static void vdec_event_eos(struct vpu_inst *inst)
1194 struct vdec_t *vdec = inst->priv;
1196 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1198 vdec->params.frame_count,
1199 vdec->decoded_frame_count,
1200 vdec->display_frame_count,
1202 vpu_inst_lock(inst);
1203 vdec->eos_received++;
1204 vdec->fixed_fmt = false;
1205 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1206 vdec_update_state(inst, VPU_CODEC_STATE_DRAIN, 0);
1207 vdec_set_last_buffer_dequeued(inst);
1208 vpu_inst_unlock(inst);
1211 static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data)
1214 case VPU_MSG_ID_SEQ_HDR_FOUND:
1215 vdec_event_seq_hdr(inst, data);
1217 case VPU_MSG_ID_RES_CHANGE:
1218 vdec_event_resolution_change(inst);
1220 case VPU_MSG_ID_FRAME_REQ:
1221 vdec_event_req_fs(inst, data);
1223 case VPU_MSG_ID_FRAME_RELEASE:
1224 vdec_evnet_rel_fs(inst, data);
1226 case VPU_MSG_ID_PIC_EOS:
1227 vdec_event_eos(inst);
1234 static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1236 struct vdec_t *vdec = inst->priv;
1237 struct vb2_v4l2_buffer *vbuf;
1238 struct vpu_rpc_buffer_desc desc;
1242 vbuf = to_vb2_v4l2_buffer(vb);
1243 dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n",
1244 inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0));
1246 if (inst->state == VPU_CODEC_STATE_DEINIT)
1248 if (vdec->reset_codec)
1251 if (inst->state == VPU_CODEC_STATE_STARTED)
1252 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
1254 ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1258 free_space = vpu_helper_get_free_space(inst);
1259 if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000)
1262 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1263 ret = vpu_iface_input_frame(inst, vb);
1267 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp);
1268 vdec->params.frame_count++;
1276 static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1278 struct vdec_t *vdec = inst->priv;
1279 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1282 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
1284 if (vdec->reset_codec)
1287 ret = vdec_response_frame(inst, vbuf);
1290 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1294 static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type)
1296 struct vdec_t *vdec = inst->priv;
1298 if (V4L2_TYPE_IS_OUTPUT(type))
1301 vdec_handle_resolution_change(inst);
1302 if (vdec->eos_received)
1303 vdec_set_last_buffer_dequeued(inst);
1306 static void vdec_abort(struct vpu_inst *inst)
1308 struct vdec_t *vdec = inst->priv;
1309 struct vpu_rpc_buffer_desc desc;
1312 vpu_trace(inst->dev, "[%d] state = %d\n", inst->id, inst->state);
1313 vpu_iface_add_scode(inst, SCODE_PADDING_ABORT);
1314 vdec->params.end_flag = 1;
1315 vpu_iface_set_decode_params(inst, &vdec->params, 1);
1317 vpu_session_abort(inst);
1319 ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1321 vpu_iface_update_stream_buffer(inst, desc.rptr, 1);
1323 vpu_session_rst_buf(inst);
1324 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1326 vdec->params.frame_count,
1327 vdec->decoded_frame_count,
1328 vdec->display_frame_count,
1330 vdec->params.end_flag = 0;
1332 vdec->params.frame_count = 0;
1333 vdec->decoded_frame_count = 0;
1334 vdec->display_frame_count = 0;
1338 static void vdec_stop(struct vpu_inst *inst, bool free)
1340 struct vdec_t *vdec = inst->priv;
1342 vdec_clear_slots(inst);
1343 if (inst->state != VPU_CODEC_STATE_DEINIT)
1344 vpu_session_stop(inst);
1345 vdec_clear_fs(&vdec->mbi);
1346 vdec_clear_fs(&vdec->dcp);
1348 vpu_free_dma(&vdec->udata);
1349 vpu_free_dma(&inst->stream_buffer);
1351 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1);
1352 vdec->reset_codec = false;
1355 static void vdec_release(struct vpu_inst *inst)
1357 if (inst->id != VPU_INST_NULL_ID)
1358 vpu_trace(inst->dev, "[%d]\n", inst->id);
1359 vpu_inst_lock(inst);
1360 vdec_stop(inst, true);
1361 vpu_inst_unlock(inst);
1364 static void vdec_cleanup(struct vpu_inst *inst)
1366 struct vdec_t *vdec;
1378 static void vdec_init_params(struct vdec_t *vdec)
1380 vdec->params.frame_count = 0;
1381 vdec->params.end_flag = 0;
1384 static int vdec_start(struct vpu_inst *inst)
1386 struct vdec_t *vdec = inst->priv;
1387 int stream_buffer_size;
1390 if (inst->state != VPU_CODEC_STATE_DEINIT)
1393 vpu_trace(inst->dev, "[%d]\n", inst->id);
1394 if (!vdec->udata.virt) {
1395 vdec->udata.length = 0x1000;
1396 ret = vpu_alloc_dma(inst->core, &vdec->udata);
1398 dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id);
1403 if (!inst->stream_buffer.virt) {
1404 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
1405 if (stream_buffer_size > 0) {
1406 inst->stream_buffer.length = stream_buffer_size;
1407 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
1409 dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id);
1412 inst->use_stream_buffer = true;
1416 if (inst->use_stream_buffer)
1417 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
1418 vpu_iface_init_instance(inst);
1419 vdec->params.udata.base = vdec->udata.phys;
1420 vdec->params.udata.size = vdec->udata.length;
1421 ret = vpu_iface_set_decode_params(inst, &vdec->params, 0);
1423 dev_err(inst->dev, "[%d] set decode params fail\n", inst->id);
1427 vdec_init_params(vdec);
1428 ret = vpu_session_start(inst);
1430 dev_err(inst->dev, "[%d] start fail\n", inst->id);
1434 vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0);
1438 vpu_free_dma(&vdec->udata);
1439 vpu_free_dma(&inst->stream_buffer);
1443 static int vdec_start_session(struct vpu_inst *inst, u32 type)
1445 struct vdec_t *vdec = inst->priv;
1448 if (V4L2_TYPE_IS_OUTPUT(type)) {
1449 if (vdec->reset_codec)
1450 vdec_stop(inst, false);
1451 if (inst->state == VPU_CODEC_STATE_DEINIT) {
1452 ret = vdec_start(inst);
1458 if (V4L2_TYPE_IS_OUTPUT(type)) {
1459 if (inst->state == VPU_CODEC_STATE_SEEK)
1460 vdec_update_state(inst, vdec->state, 1);
1461 vdec->eos_received = 0;
1462 vpu_process_output_buffer(inst);
1464 vdec_cmd_start(inst);
1466 if (inst->state == VPU_CODEC_STATE_ACTIVE)
1467 vdec_response_fs_request(inst, false);
1472 static int vdec_stop_session(struct vpu_inst *inst, u32 type)
1474 struct vdec_t *vdec = inst->priv;
1476 if (inst->state == VPU_CODEC_STATE_DEINIT)
1479 if (V4L2_TYPE_IS_OUTPUT(type)) {
1480 vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0);
1483 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
1486 vdec->eos_received = 0;
1487 vdec_clear_slots(inst);
1493 static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1495 struct vdec_t *vdec = inst->priv;
1500 num = scnprintf(str, size,
1501 "req_frame_count = %d\ninterlaced = %d\n",
1502 vdec->req_frame_count,
1503 vdec->codec_info.progressive ? 0 : 1);
1506 num = scnprintf(str, size,
1507 "mbi: size = 0x%x request = %d, alloc = %d, response = %d\n",
1509 vdec->mbi.req_count,
1514 num = scnprintf(str, size,
1515 "dcp: size = 0x%x request = %d, alloc = %d, response = %d\n",
1517 vdec->dcp.req_count,
1522 num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count);
1525 num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count);
1528 num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count);
1531 num = scnprintf(str, size, "sequence = %d\n", vdec->sequence);
1534 num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n",
1535 vdec->drain, vdec->eos_received, vdec->source_change);
1538 num = scnprintf(str, size, "fps = %d/%d\n",
1539 vdec->codec_info.frame_rate.numerator,
1540 vdec->codec_info.frame_rate.denominator);
1549 static struct vpu_inst_ops vdec_inst_ops = {
1550 .ctrl_init = vdec_ctrl_init,
1551 .check_ready = vdec_check_ready,
1552 .buf_done = vdec_buf_done,
1553 .get_one_frame = vdec_frame_decoded,
1554 .stop_done = vdec_stop_done,
1555 .event_notify = vdec_event_notify,
1556 .release = vdec_release,
1557 .cleanup = vdec_cleanup,
1558 .start = vdec_start_session,
1559 .stop = vdec_stop_session,
1560 .process_output = vdec_process_output,
1561 .process_capture = vdec_process_capture,
1562 .on_queue_empty = vdec_on_queue_empty,
1563 .get_debug_info = vdec_get_debug_info,
1564 .wait_prepare = vpu_inst_unlock,
1565 .wait_finish = vpu_inst_lock,
1568 static void vdec_init(struct file *file)
1570 struct vpu_inst *inst = to_inst(file);
1571 struct v4l2_format f;
1573 memset(&f, 0, sizeof(f));
1574 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1575 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1576 f.fmt.pix_mp.width = 1280;
1577 f.fmt.pix_mp.height = 720;
1578 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1579 vdec_s_fmt(file, &inst->fh, &f);
1581 memset(&f, 0, sizeof(f));
1582 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1583 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M_8L128;
1584 f.fmt.pix_mp.width = 1280;
1585 f.fmt.pix_mp.height = 720;
1586 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1587 vdec_s_fmt(file, &inst->fh, &f);
1590 static int vdec_open(struct file *file)
1592 struct vpu_inst *inst;
1593 struct vdec_t *vdec;
1596 inst = vzalloc(sizeof(*inst));
1600 vdec = vzalloc(sizeof(*vdec));
1606 inst->ops = &vdec_inst_ops;
1607 inst->formats = vdec_formats;
1608 inst->type = VPU_CORE_TYPE_DEC;
1611 ret = vpu_v4l2_open(file, inst);
1615 vdec->fixed_fmt = false;
1616 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1617 inst->min_buffer_out = VDEC_MIN_BUFFER_OUT;
1623 static const struct v4l2_file_operations vdec_fops = {
1624 .owner = THIS_MODULE,
1626 .release = vpu_v4l2_close,
1627 .unlocked_ioctl = video_ioctl2,
1628 .poll = v4l2_m2m_fop_poll,
1629 .mmap = v4l2_m2m_fop_mmap,
1632 const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void)
1634 return &vdec_ioctl_ops;
1637 const struct v4l2_file_operations *vdec_get_fops(void)