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);
284 static int vdec_update_state(struct vpu_inst *inst, enum vpu_codec_state state, u32 force)
286 struct vdec_t *vdec = inst->priv;
287 enum vpu_codec_state pre_state = inst->state;
289 if (state == VPU_CODEC_STATE_SEEK) {
290 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
291 vdec->state = inst->state;
293 vdec->state = VPU_CODEC_STATE_ACTIVE;
295 if (inst->state != VPU_CODEC_STATE_SEEK || force)
297 else if (state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
298 vdec->state = VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE;
300 if (inst->state != pre_state)
301 vpu_trace(inst->dev, "[%d] %d -> %d\n", inst->id, pre_state, inst->state);
303 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
304 vdec_handle_resolution_change(inst);
309 static void vdec_set_last_buffer_dequeued(struct vpu_inst *inst)
311 struct vdec_t *vdec = inst->priv;
313 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
316 if (vdec->eos_received) {
317 if (!vpu_set_last_buffer_dequeued(inst)) {
318 vdec->eos_received--;
319 vdec_update_state(inst, VPU_CODEC_STATE_DRAIN, 0);
324 static int vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
326 strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
327 strscpy(cap->card, "amphion vpu decoder", sizeof(cap->card));
328 strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
333 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
335 struct vpu_inst *inst = to_inst(file);
336 struct vdec_t *vdec = inst->priv;
337 const struct vpu_format *fmt;
341 if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
342 fmt = vpu_get_format(inst, f->type);
344 fmt = vpu_helper_find_sibling(inst, f->type, fmt->pixfmt);
348 fmt = vpu_helper_enum_format(inst, f->type, f->index);
353 memset(f->reserved, 0, sizeof(f->reserved));
354 f->pixelformat = fmt->pixfmt;
355 f->flags = fmt->flags;
358 vpu_inst_unlock(inst);
362 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
364 struct vpu_inst *inst = to_inst(file);
365 struct vdec_t *vdec = inst->priv;
366 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
367 struct vpu_format *cur_fmt;
371 cur_fmt = vpu_get_format(inst, f->type);
373 pixmp->pixelformat = cur_fmt->pixfmt;
374 pixmp->num_planes = cur_fmt->mem_planes;
375 pixmp->width = cur_fmt->width;
376 pixmp->height = cur_fmt->height;
377 pixmp->field = cur_fmt->field;
378 pixmp->flags = cur_fmt->flags;
379 for (i = 0; i < pixmp->num_planes; i++) {
380 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
381 pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
384 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
385 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
386 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
387 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
388 vpu_inst_unlock(inst);
393 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
395 struct vpu_inst *inst = to_inst(file);
396 struct vdec_t *vdec = inst->priv;
397 struct vpu_format fmt;
400 if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
401 struct vpu_format *cap_fmt = vpu_get_format(inst, f->type);
403 if (!vpu_helper_match_format(inst, cap_fmt->type, cap_fmt->pixfmt,
404 f->fmt.pix_mp.pixelformat))
405 f->fmt.pix_mp.pixelformat = cap_fmt->pixfmt;
408 vpu_try_fmt_common(inst, f, &fmt);
410 if (vdec->fixed_fmt) {
411 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
412 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
413 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
414 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
416 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
417 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
418 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
419 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
421 vpu_inst_unlock(inst);
426 static int vdec_s_fmt_common(struct vpu_inst *inst, struct v4l2_format *f)
428 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
429 struct vpu_format fmt;
430 struct vpu_format *cur_fmt;
432 struct vdec_t *vdec = inst->priv;
435 if (!inst->fh.m2m_ctx)
438 q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
444 if (vpu_try_fmt_common(inst, f, &fmt))
447 cur_fmt = vpu_get_format(inst, f->type);
448 if (V4L2_TYPE_IS_OUTPUT(f->type) && inst->state != VPU_CODEC_STATE_DEINIT) {
449 if (cur_fmt->pixfmt != fmt.pixfmt) {
450 vdec->reset_codec = true;
451 vdec->fixed_fmt = false;
454 if (V4L2_TYPE_IS_OUTPUT(f->type) || !vdec->fixed_fmt) {
455 memcpy(cur_fmt, &fmt, sizeof(*cur_fmt));
457 if (vpu_helper_match_format(inst, f->type, cur_fmt->pixfmt, pixmp->pixelformat)) {
458 cur_fmt->pixfmt = fmt.pixfmt;
459 cur_fmt->mem_planes = fmt.mem_planes;
461 pixmp->pixelformat = cur_fmt->pixfmt;
462 pixmp->num_planes = cur_fmt->mem_planes;
463 pixmp->width = cur_fmt->width;
464 pixmp->height = cur_fmt->height;
465 for (i = 0; i < pixmp->num_planes; i++) {
466 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
467 pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
469 pixmp->field = cur_fmt->field;
472 if (!vdec->fixed_fmt) {
473 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
474 vdec->params.codec_format = cur_fmt->pixfmt;
475 vdec->codec_info.color_primaries = f->fmt.pix_mp.colorspace;
476 vdec->codec_info.transfer_chars = f->fmt.pix_mp.xfer_func;
477 vdec->codec_info.matrix_coeffs = f->fmt.pix_mp.ycbcr_enc;
478 vdec->codec_info.full_range = f->fmt.pix_mp.quantization;
480 vdec->params.output_format = cur_fmt->pixfmt;
483 inst->crop.width = cur_fmt->width;
484 inst->crop.height = cur_fmt->height;
488 vpu_trace(inst->dev, "[%d] %c%c%c%c %dx%d\n", inst->id,
489 f->fmt.pix_mp.pixelformat,
490 f->fmt.pix_mp.pixelformat >> 8,
491 f->fmt.pix_mp.pixelformat >> 16,
492 f->fmt.pix_mp.pixelformat >> 24,
494 f->fmt.pix_mp.height);
499 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
501 struct vpu_inst *inst = to_inst(file);
502 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
503 struct vdec_t *vdec = inst->priv;
507 ret = vdec_s_fmt_common(inst, f);
511 if (V4L2_TYPE_IS_OUTPUT(f->type) && !vdec->fixed_fmt) {
512 struct v4l2_format fc;
514 memset(&fc, 0, sizeof(fc));
515 fc.type = inst->cap_format.type;
516 fc.fmt.pix_mp.pixelformat = inst->cap_format.pixfmt;
517 fc.fmt.pix_mp.width = pixmp->width;
518 fc.fmt.pix_mp.height = pixmp->height;
519 vdec_s_fmt_common(inst, &fc);
522 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
523 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
524 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
525 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
528 vpu_inst_unlock(inst);
532 static int vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
534 struct vpu_inst *inst = to_inst(file);
536 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
540 case V4L2_SEL_TGT_COMPOSE:
541 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
542 case V4L2_SEL_TGT_COMPOSE_PADDED:
545 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
548 s->r.width = inst->cap_format.width;
549 s->r.height = inst->cap_format.height;
558 static int vdec_drain(struct vpu_inst *inst)
560 struct vdec_t *vdec = inst->priv;
562 if (!inst->fh.m2m_ctx)
568 if (!vpu_is_source_empty(inst))
571 if (!vdec->params.frame_count) {
572 vpu_set_last_buffer_dequeued(inst);
576 vpu_iface_add_scode(inst, SCODE_PADDING_EOS);
577 vdec->params.end_flag = 1;
578 vpu_iface_set_decode_params(inst, &vdec->params, 1);
580 vpu_trace(inst->dev, "[%d] frame_count = %d\n", inst->id, vdec->params.frame_count);
585 static int vdec_cmd_start(struct vpu_inst *inst)
587 struct vdec_t *vdec = inst->priv;
589 switch (inst->state) {
590 case VPU_CODEC_STATE_STARTED:
591 case VPU_CODEC_STATE_DRAIN:
592 case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE:
593 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
598 vpu_process_capture_buffer(inst);
599 if (vdec->eos_received)
600 vdec_set_last_buffer_dequeued(inst);
604 static int vdec_cmd_stop(struct vpu_inst *inst)
606 struct vdec_t *vdec = inst->priv;
608 vpu_trace(inst->dev, "[%d]\n", inst->id);
610 if (inst->state == VPU_CODEC_STATE_DEINIT) {
611 vpu_set_last_buffer_dequeued(inst);
620 static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
622 struct vpu_inst *inst = to_inst(file);
625 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
631 case V4L2_DEC_CMD_START:
632 vdec_cmd_start(inst);
634 case V4L2_DEC_CMD_STOP:
640 vpu_inst_unlock(inst);
645 static int vdec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
649 return v4l2_event_subscribe(fh, sub, 0, NULL);
650 case V4L2_EVENT_SOURCE_CHANGE:
651 return v4l2_src_change_event_subscribe(fh, sub);
652 case V4L2_EVENT_CTRL:
653 return v4l2_ctrl_subscribe_event(fh, sub);
661 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
662 .vidioc_querycap = vdec_querycap,
663 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
664 .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
665 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
666 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
667 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
668 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
669 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
670 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
671 .vidioc_g_selection = vdec_g_selection,
672 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
673 .vidioc_decoder_cmd = vdec_decoder_cmd,
674 .vidioc_subscribe_event = vdec_subscribe_event,
675 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
676 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
677 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
678 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
679 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
680 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
681 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
682 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
683 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
684 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
687 static bool vdec_check_ready(struct vpu_inst *inst, unsigned int type)
689 struct vdec_t *vdec = inst->priv;
691 if (V4L2_TYPE_IS_OUTPUT(type))
694 if (vdec->req_frame_count)
700 static struct vb2_v4l2_buffer *vdec_get_src_buffer(struct vpu_inst *inst, u32 count)
703 vpu_skip_frame(inst, count - 1);
705 return vpu_next_src_buf(inst);
708 static int vdec_frame_decoded(struct vpu_inst *inst, void *arg)
710 struct vdec_t *vdec = inst->priv;
711 struct vpu_dec_pic_info *info = arg;
712 struct vpu_vb2_buffer *vpu_buf;
713 struct vb2_v4l2_buffer *vbuf;
714 struct vb2_v4l2_buffer *src_buf;
717 if (!info || info->id >= ARRAY_SIZE(vdec->slots))
721 vpu_buf = vdec->slots[info->id];
723 dev_err(inst->dev, "[%d] decoded invalid frame[%d]\n", inst->id, info->id);
727 vbuf = &vpu_buf->m2m_buf.vb;
728 src_buf = vdec_get_src_buffer(inst, info->consumed_count);
730 v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
731 if (info->consumed_count) {
732 v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
733 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
734 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
736 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_DECODED);
739 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED)
740 dev_info(inst->dev, "[%d] buf[%d] has been decoded\n", inst->id, info->id);
741 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_DECODED);
742 vdec->decoded_frame_count++;
744 vpu_inst_unlock(inst);
749 static struct vpu_vb2_buffer *vdec_find_buffer(struct vpu_inst *inst, u32 luma)
751 struct vdec_t *vdec = inst->priv;
754 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
757 if (luma == vdec->slots[i]->luma)
758 return vdec->slots[i];
764 static void vdec_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame)
766 struct vdec_t *vdec = inst->priv;
767 struct vpu_format *cur_fmt;
768 struct vpu_vb2_buffer *vpu_buf;
769 struct vb2_v4l2_buffer *vbuf;
777 sequence = vdec->sequence++;
778 vpu_buf = vdec_find_buffer(inst, frame->luma);
779 vpu_inst_unlock(inst);
781 dev_err(inst->dev, "[%d] can't find buffer, id = %d, addr = 0x%x\n",
782 inst->id, frame->id, frame->luma);
785 if (frame->skipped) {
786 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
790 cur_fmt = vpu_get_format(inst, inst->cap_format.type);
791 vbuf = &vpu_buf->m2m_buf.vb;
792 if (vbuf->vb2_buf.index != frame->id)
793 dev_err(inst->dev, "[%d] buffer id(%d, %d) dismatch\n",
794 inst->id, vbuf->vb2_buf.index, frame->id);
796 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_DECODED)
797 dev_err(inst->dev, "[%d] buffer(%d) ready without decoded\n", inst->id, frame->id);
798 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY);
799 for (i = 0; i < vbuf->vb2_buf.num_planes; i++)
800 vb2_set_plane_payload(&vbuf->vb2_buf, i, vpu_get_fmt_plane_size(cur_fmt, i));
801 vbuf->field = cur_fmt->field;
802 vbuf->sequence = sequence;
803 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
805 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
807 vdec->display_frame_count++;
808 vpu_inst_unlock(inst);
809 dev_dbg(inst->dev, "[%d] decoded : %d, display : %d, sequence : %d\n",
810 inst->id, vdec->decoded_frame_count, vdec->display_frame_count, vdec->sequence);
813 static void vdec_stop_done(struct vpu_inst *inst)
815 struct vdec_t *vdec = inst->priv;
818 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 0);
819 vdec->seq_hdr_found = 0;
820 vdec->req_frame_count = 0;
821 vdec->reset_codec = false;
822 vdec->fixed_fmt = false;
823 vdec->params.end_flag = 0;
825 vdec->params.frame_count = 0;
826 vdec->decoded_frame_count = 0;
827 vdec->display_frame_count = 0;
829 vdec->eos_received = 0;
830 vdec->is_source_changed = false;
831 vdec->source_change = 0;
832 inst->total_input_count = 0;
833 vpu_inst_unlock(inst);
836 static bool vdec_check_source_change(struct vpu_inst *inst)
838 struct vdec_t *vdec = inst->priv;
839 const struct vpu_format *sibling;
841 if (!inst->fh.m2m_ctx)
844 if (vdec->reset_codec)
847 sibling = vpu_helper_find_sibling(inst, inst->cap_format.type, inst->cap_format.pixfmt);
848 if (sibling && vdec->codec_info.pixfmt == sibling->pixfmt)
849 vdec->codec_info.pixfmt = inst->cap_format.pixfmt;
851 if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)))
853 if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
855 if (inst->cap_format.width != vdec->codec_info.decoded_width)
857 if (inst->cap_format.height != vdec->codec_info.decoded_height)
859 if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap)
861 if (inst->crop.left != vdec->codec_info.offset_x)
863 if (inst->crop.top != vdec->codec_info.offset_y)
865 if (inst->crop.width != vdec->codec_info.width)
867 if (inst->crop.height != vdec->codec_info.height)
873 static void vdec_init_fmt(struct vpu_inst *inst)
875 struct vdec_t *vdec = inst->priv;
876 struct v4l2_format f;
878 memset(&f, 0, sizeof(f));
879 f.type = inst->cap_format.type;
880 f.fmt.pix_mp.pixelformat = vdec->codec_info.pixfmt;
881 f.fmt.pix_mp.width = vdec->codec_info.decoded_width;
882 f.fmt.pix_mp.height = vdec->codec_info.decoded_height;
883 if (vdec->codec_info.progressive)
884 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
886 f.fmt.pix_mp.field = V4L2_FIELD_SEQ_TB;
887 vpu_try_fmt_common(inst, &f, &inst->cap_format);
889 inst->out_format.width = vdec->codec_info.width;
890 inst->out_format.height = vdec->codec_info.height;
893 static void vdec_init_crop(struct vpu_inst *inst)
895 struct vdec_t *vdec = inst->priv;
897 inst->crop.left = vdec->codec_info.offset_x;
898 inst->crop.top = vdec->codec_info.offset_y;
899 inst->crop.width = vdec->codec_info.width;
900 inst->crop.height = vdec->codec_info.height;
903 static void vdec_init_mbi(struct vpu_inst *inst)
905 struct vdec_t *vdec = inst->priv;
907 vdec->mbi.size = vdec->codec_info.mbi_size;
908 vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer);
909 scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi");
910 vdec->mbi.type = MEM_RES_MBI;
911 vdec->mbi.tag = vdec->seq_tag;
914 static void vdec_init_dcp(struct vpu_inst *inst)
916 struct vdec_t *vdec = inst->priv;
918 vdec->dcp.size = vdec->codec_info.dcp_size;
919 vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer);
920 scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp");
921 vdec->dcp.type = MEM_RES_DCP;
922 vdec->dcp.tag = vdec->seq_tag;
925 static void vdec_request_one_fs(struct vdec_fs_info *fs)
928 if (fs->req_count > fs->max_count)
929 fs->req_count = fs->max_count;
932 static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs)
934 struct vpu_buffer *buffer;
939 if (fs->count >= fs->req_count)
942 buffer = &fs->buffer[fs->count];
943 if (buffer->virt && buffer->length >= fs->size)
946 vpu_free_dma(buffer);
947 buffer->length = fs->size;
948 return vpu_alloc_dma(inst->core, buffer);
951 static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
955 while (fs->count < fs->req_count) {
956 ret = vdec_alloc_fs_buffer(inst, fs);
963 static void vdec_clear_fs(struct vdec_fs_info *fs)
970 for (i = 0; i < ARRAY_SIZE(fs->buffer); i++)
971 vpu_free_dma(&fs->buffer[i]);
972 memset(fs, 0, sizeof(*fs));
975 static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
977 struct vpu_fs_info info;
980 if (fs->index >= fs->count)
983 memset(&info, 0, sizeof(info));
985 info.type = fs->type;
987 info.luma_addr = fs->buffer[fs->index].phys;
988 info.luma_size = fs->buffer[fs->index].length;
989 ret = vpu_session_alloc_fs(inst, &info);
997 static int vdec_response_frame_abnormal(struct vpu_inst *inst)
999 struct vdec_t *vdec = inst->priv;
1000 struct vpu_fs_info info;
1002 if (!vdec->req_frame_count)
1005 memset(&info, 0, sizeof(info));
1006 info.type = MEM_RES_FRAME;
1007 info.tag = vdec->seq_tag + 0xf0;
1008 vpu_session_alloc_fs(inst, &info);
1009 vdec->req_frame_count--;
1014 static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1016 struct vdec_t *vdec = inst->priv;
1017 struct vpu_vb2_buffer *vpu_buf;
1018 struct vpu_fs_info info;
1021 if (inst->state != VPU_CODEC_STATE_ACTIVE)
1027 if (!vdec->req_frame_count)
1033 if (vdec->slots[vbuf->vb2_buf.index]) {
1034 dev_err(inst->dev, "[%d] repeat alloc fs %d\n",
1035 inst->id, vbuf->vb2_buf.index);
1039 dev_dbg(inst->dev, "[%d] state = %d, alloc fs %d, tag = 0x%x\n",
1040 inst->id, inst->state, vbuf->vb2_buf.index, vdec->seq_tag);
1041 vpu_buf = to_vpu_vb2_buffer(vbuf);
1043 memset(&info, 0, sizeof(info));
1044 info.id = vbuf->vb2_buf.index;
1045 info.type = MEM_RES_FRAME;
1046 info.tag = vdec->seq_tag;
1047 info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0);
1048 info.luma_size = inst->cap_format.sizeimage[0];
1049 if (vbuf->vb2_buf.num_planes > 1)
1050 info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1);
1052 info.chroma_addr = info.luma_addr + info.luma_size;
1053 info.chromau_size = inst->cap_format.sizeimage[1];
1054 info.bytesperline = inst->cap_format.bytesperline[0];
1055 ret = vpu_session_alloc_fs(inst, &info);
1059 vpu_buf->tag = info.tag;
1060 vpu_buf->luma = info.luma_addr;
1061 vpu_buf->chroma_u = info.chroma_addr;
1062 vpu_buf->chroma_v = 0;
1063 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1064 vdec->slots[info.id] = vpu_buf;
1065 vdec->req_frame_count--;
1070 static void vdec_response_fs_request(struct vpu_inst *inst, bool force)
1072 struct vdec_t *vdec = inst->priv;
1077 for (i = vdec->req_frame_count; i > 0; i--)
1078 vdec_response_frame_abnormal(inst);
1082 for (i = vdec->req_frame_count; i > 0; i--) {
1083 ret = vpu_process_capture_buffer(inst);
1086 if (vdec->eos_received)
1090 for (i = vdec->mbi.index; i < vdec->mbi.count; i++) {
1091 if (vdec_response_fs(inst, &vdec->mbi))
1093 if (vdec->eos_received)
1096 for (i = vdec->dcp.index; i < vdec->dcp.count; i++) {
1097 if (vdec_response_fs(inst, &vdec->dcp))
1099 if (vdec->eos_received)
1104 static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag)
1106 struct vpu_fs_info info;
1108 memset(&info, 0, sizeof(info));
1111 vpu_session_release_fs(inst, &info);
1114 static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1116 if (!inst->fh.m2m_ctx)
1118 if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE)
1120 if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index))
1122 v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
1125 static void vdec_clear_slots(struct vpu_inst *inst)
1127 struct vdec_t *vdec = inst->priv;
1128 struct vpu_vb2_buffer *vpu_buf;
1129 struct vb2_v4l2_buffer *vbuf;
1132 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
1133 if (!vdec->slots[i])
1136 vpu_buf = vdec->slots[i];
1137 vbuf = &vpu_buf->m2m_buf.vb;
1139 vpu_trace(inst->dev, "clear slot %d\n", i);
1140 vdec_response_fs_release(inst, i, vpu_buf->tag);
1141 vdec_recycle_buffer(inst, vbuf);
1142 vdec->slots[i]->state = VPU_BUF_STATE_IDLE;
1143 vdec->slots[i] = NULL;
1147 static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)
1149 struct vdec_t *vdec = inst->priv;
1151 vpu_inst_lock(inst);
1152 memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
1154 vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
1156 vdec->codec_info.decoded_width,
1157 vdec->codec_info.decoded_height,
1158 vdec->codec_info.offset_x,
1159 vdec->codec_info.offset_y,
1160 vdec->codec_info.width,
1161 vdec->codec_info.height,
1164 inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;
1165 vdec->is_source_changed = vdec_check_source_change(inst);
1166 vdec_init_fmt(inst);
1167 vdec_init_crop(inst);
1168 vdec_init_mbi(inst);
1169 vdec_init_dcp(inst);
1170 if (!vdec->seq_hdr_found) {
1171 vdec->seq_tag = vdec->codec_info.tag;
1172 if (vdec->is_source_changed) {
1173 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1174 vdec->source_change++;
1175 vdec_handle_resolution_change(inst);
1176 vdec->is_source_changed = false;
1179 if (vdec->seq_tag != vdec->codec_info.tag) {
1180 vdec_response_fs_request(inst, true);
1181 vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n",
1182 inst->id, vdec->seq_tag, vdec->codec_info.tag);
1184 vdec->seq_hdr_found++;
1185 vdec->fixed_fmt = true;
1186 vpu_inst_unlock(inst);
1189 static void vdec_event_resolution_change(struct vpu_inst *inst)
1191 struct vdec_t *vdec = inst->priv;
1193 vpu_trace(inst->dev, "[%d]\n", inst->id);
1194 vpu_inst_lock(inst);
1195 vdec->seq_tag = vdec->codec_info.tag;
1196 vdec_clear_fs(&vdec->mbi);
1197 vdec_clear_fs(&vdec->dcp);
1198 vdec_clear_slots(inst);
1199 vdec_init_mbi(inst);
1200 vdec_init_dcp(inst);
1201 if (vdec->is_source_changed) {
1202 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1203 vdec->source_change++;
1204 vdec_handle_resolution_change(inst);
1205 vdec->is_source_changed = false;
1207 vpu_inst_unlock(inst);
1210 static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1212 struct vdec_t *vdec = inst->priv;
1217 vpu_inst_lock(inst);
1221 vdec->req_frame_count++;
1224 vdec_request_one_fs(&vdec->mbi);
1227 vdec_request_one_fs(&vdec->dcp);
1233 vdec_alloc_fs(inst, &vdec->mbi);
1234 vdec_alloc_fs(inst, &vdec->dcp);
1236 vdec_response_fs_request(inst, false);
1238 vpu_inst_unlock(inst);
1241 static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1243 struct vdec_t *vdec = inst->priv;
1244 struct vpu_vb2_buffer *vpu_buf;
1245 struct vb2_v4l2_buffer *vbuf;
1247 if (!fs || fs->id >= ARRAY_SIZE(vdec->slots))
1249 if (fs->type != MEM_RES_FRAME)
1252 if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) {
1253 dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id);
1257 vpu_inst_lock(inst);
1258 vpu_buf = vdec->slots[fs->id];
1259 vdec->slots[fs->id] = NULL;
1262 dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id);
1266 vbuf = &vpu_buf->m2m_buf.vb;
1267 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) {
1268 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
1272 vdec_response_fs_release(inst, fs->id, vpu_buf->tag);
1273 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY)
1274 vdec_recycle_buffer(inst, vbuf);
1276 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
1277 vpu_process_capture_buffer(inst);
1280 vpu_inst_unlock(inst);
1283 static void vdec_event_eos(struct vpu_inst *inst)
1285 struct vdec_t *vdec = inst->priv;
1287 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1289 vdec->params.frame_count,
1290 vdec->decoded_frame_count,
1291 vdec->display_frame_count,
1293 vpu_inst_lock(inst);
1294 vdec->eos_received++;
1295 vdec->fixed_fmt = false;
1296 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1297 vdec_set_last_buffer_dequeued(inst);
1298 vpu_inst_unlock(inst);
1301 static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data)
1304 case VPU_MSG_ID_SEQ_HDR_FOUND:
1305 vdec_event_seq_hdr(inst, data);
1307 case VPU_MSG_ID_RES_CHANGE:
1308 vdec_event_resolution_change(inst);
1310 case VPU_MSG_ID_FRAME_REQ:
1311 vdec_event_req_fs(inst, data);
1313 case VPU_MSG_ID_FRAME_RELEASE:
1314 vdec_evnet_rel_fs(inst, data);
1316 case VPU_MSG_ID_PIC_EOS:
1317 vdec_event_eos(inst);
1324 static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1326 struct vdec_t *vdec = inst->priv;
1327 struct vb2_v4l2_buffer *vbuf;
1328 struct vpu_rpc_buffer_desc desc;
1332 vbuf = to_vb2_v4l2_buffer(vb);
1333 dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n",
1334 inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0));
1336 if (inst->state == VPU_CODEC_STATE_DEINIT)
1338 if (vdec->reset_codec)
1341 if (inst->state == VPU_CODEC_STATE_STARTED)
1342 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
1344 ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1348 free_space = vpu_helper_get_free_space(inst);
1349 if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000)
1352 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1353 ret = vpu_iface_input_frame(inst, vb);
1357 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp);
1358 vdec->params.frame_count++;
1366 static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1368 struct vdec_t *vdec = inst->priv;
1369 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1372 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
1374 if (vdec->reset_codec)
1377 ret = vdec_response_frame(inst, vbuf);
1380 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1384 static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type)
1386 struct vdec_t *vdec = inst->priv;
1388 if (V4L2_TYPE_IS_OUTPUT(type))
1391 vdec_handle_resolution_change(inst);
1392 if (vdec->eos_received)
1393 vdec_set_last_buffer_dequeued(inst);
1396 static void vdec_abort(struct vpu_inst *inst)
1398 struct vdec_t *vdec = inst->priv;
1399 struct vpu_rpc_buffer_desc desc;
1402 vpu_trace(inst->dev, "[%d] state = %d\n", inst->id, inst->state);
1404 vdec->aborting = true;
1405 vpu_iface_add_scode(inst, SCODE_PADDING_ABORT);
1406 vdec->params.end_flag = 1;
1407 vpu_iface_set_decode_params(inst, &vdec->params, 1);
1409 vpu_session_abort(inst);
1411 ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1413 vpu_iface_update_stream_buffer(inst, desc.rptr, 1);
1415 vpu_session_rst_buf(inst);
1416 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1418 vdec->params.frame_count,
1419 vdec->decoded_frame_count,
1420 vdec->display_frame_count,
1422 if (!vdec->seq_hdr_found)
1423 vdec->reset_codec = true;
1424 vdec->params.end_flag = 0;
1426 vdec->params.frame_count = 0;
1427 vdec->decoded_frame_count = 0;
1428 vdec->display_frame_count = 0;
1430 vdec->aborting = false;
1431 inst->extra_size = 0;
1434 static void vdec_stop(struct vpu_inst *inst, bool free)
1436 struct vdec_t *vdec = inst->priv;
1438 vdec_clear_slots(inst);
1439 if (inst->state != VPU_CODEC_STATE_DEINIT)
1440 vpu_session_stop(inst);
1441 vdec_clear_fs(&vdec->mbi);
1442 vdec_clear_fs(&vdec->dcp);
1444 vpu_free_dma(&vdec->udata);
1445 vpu_free_dma(&inst->stream_buffer);
1447 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1);
1448 vdec->reset_codec = false;
1451 static void vdec_release(struct vpu_inst *inst)
1453 if (inst->id != VPU_INST_NULL_ID)
1454 vpu_trace(inst->dev, "[%d]\n", inst->id);
1455 vpu_inst_lock(inst);
1456 vdec_stop(inst, true);
1457 vpu_inst_unlock(inst);
1460 static void vdec_cleanup(struct vpu_inst *inst)
1462 struct vdec_t *vdec;
1473 static void vdec_init_params(struct vdec_t *vdec)
1475 vdec->params.frame_count = 0;
1476 vdec->params.end_flag = 0;
1479 static int vdec_start(struct vpu_inst *inst)
1481 struct vdec_t *vdec = inst->priv;
1482 int stream_buffer_size;
1485 if (inst->state != VPU_CODEC_STATE_DEINIT)
1488 vpu_trace(inst->dev, "[%d]\n", inst->id);
1489 if (!vdec->udata.virt) {
1490 vdec->udata.length = 0x1000;
1491 ret = vpu_alloc_dma(inst->core, &vdec->udata);
1493 dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id);
1498 if (!inst->stream_buffer.virt) {
1499 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
1500 if (stream_buffer_size > 0) {
1501 inst->stream_buffer.length = stream_buffer_size;
1502 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
1504 dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id);
1507 inst->use_stream_buffer = true;
1511 if (inst->use_stream_buffer)
1512 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
1513 vpu_iface_init_instance(inst);
1514 vdec->params.udata.base = vdec->udata.phys;
1515 vdec->params.udata.size = vdec->udata.length;
1516 ret = vpu_iface_set_decode_params(inst, &vdec->params, 0);
1518 dev_err(inst->dev, "[%d] set decode params fail\n", inst->id);
1522 vdec_init_params(vdec);
1523 ret = vpu_session_start(inst);
1525 dev_err(inst->dev, "[%d] start fail\n", inst->id);
1529 vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0);
1533 vpu_free_dma(&vdec->udata);
1534 vpu_free_dma(&inst->stream_buffer);
1538 static int vdec_start_session(struct vpu_inst *inst, u32 type)
1540 struct vdec_t *vdec = inst->priv;
1543 if (V4L2_TYPE_IS_OUTPUT(type)) {
1544 if (vdec->reset_codec)
1545 vdec_stop(inst, false);
1546 if (inst->state == VPU_CODEC_STATE_DEINIT) {
1547 ret = vdec_start(inst);
1553 if (V4L2_TYPE_IS_OUTPUT(type)) {
1554 vdec_update_state(inst, vdec->state, 1);
1555 vdec->eos_received = 0;
1556 vpu_process_output_buffer(inst);
1558 vdec_cmd_start(inst);
1560 if (inst->state == VPU_CODEC_STATE_ACTIVE)
1561 vdec_response_fs_request(inst, false);
1566 static int vdec_stop_session(struct vpu_inst *inst, u32 type)
1568 struct vdec_t *vdec = inst->priv;
1570 if (inst->state == VPU_CODEC_STATE_DEINIT)
1573 if (V4L2_TYPE_IS_OUTPUT(type)) {
1574 vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0);
1577 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) {
1579 vdec->eos_received = 0;
1581 vdec_clear_slots(inst);
1587 static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1589 struct vdec_t *vdec = inst->priv;
1594 num = scnprintf(str, size,
1595 "req_frame_count = %d\ninterlaced = %d\n",
1596 vdec->req_frame_count,
1597 vdec->codec_info.progressive ? 0 : 1);
1600 num = scnprintf(str, size,
1601 "mbi: size = 0x%x request = %d, alloc = %d, response = %d\n",
1603 vdec->mbi.req_count,
1608 num = scnprintf(str, size,
1609 "dcp: size = 0x%x request = %d, alloc = %d, response = %d\n",
1611 vdec->dcp.req_count,
1616 num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count);
1619 num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count);
1622 num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count);
1625 num = scnprintf(str, size, "sequence = %d\n", vdec->sequence);
1628 num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n",
1629 vdec->drain, vdec->eos_received, vdec->source_change);
1632 num = scnprintf(str, size, "fps = %d/%d\n",
1633 vdec->codec_info.frame_rate.numerator,
1634 vdec->codec_info.frame_rate.denominator);
1637 num = scnprintf(str, size, "colorspace: %d, %d, %d, %d (%d)\n",
1638 vdec->codec_info.color_primaries,
1639 vdec->codec_info.transfer_chars,
1640 vdec->codec_info.matrix_coeffs,
1641 vdec->codec_info.full_range,
1642 vdec->codec_info.vui_present);
1651 static struct vpu_inst_ops vdec_inst_ops = {
1652 .ctrl_init = vdec_ctrl_init,
1653 .check_ready = vdec_check_ready,
1654 .buf_done = vdec_buf_done,
1655 .get_one_frame = vdec_frame_decoded,
1656 .stop_done = vdec_stop_done,
1657 .event_notify = vdec_event_notify,
1658 .release = vdec_release,
1659 .cleanup = vdec_cleanup,
1660 .start = vdec_start_session,
1661 .stop = vdec_stop_session,
1662 .process_output = vdec_process_output,
1663 .process_capture = vdec_process_capture,
1664 .on_queue_empty = vdec_on_queue_empty,
1665 .get_debug_info = vdec_get_debug_info,
1666 .wait_prepare = vpu_inst_unlock,
1667 .wait_finish = vpu_inst_lock,
1670 static void vdec_init(struct file *file)
1672 struct vpu_inst *inst = to_inst(file);
1673 struct v4l2_format f;
1675 memset(&f, 0, sizeof(f));
1676 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1677 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1678 f.fmt.pix_mp.width = 1280;
1679 f.fmt.pix_mp.height = 720;
1680 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1681 vdec_s_fmt(file, &inst->fh, &f);
1683 memset(&f, 0, sizeof(f));
1684 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1685 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M_8L128;
1686 f.fmt.pix_mp.width = 1280;
1687 f.fmt.pix_mp.height = 720;
1688 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1689 vdec_s_fmt(file, &inst->fh, &f);
1692 static int vdec_open(struct file *file)
1694 struct vpu_inst *inst;
1695 struct vdec_t *vdec;
1698 inst = vzalloc(sizeof(*inst));
1702 vdec = vzalloc(sizeof(*vdec));
1708 inst->ops = &vdec_inst_ops;
1709 inst->formats = vdec_formats;
1710 inst->type = VPU_CORE_TYPE_DEC;
1713 ret = vpu_v4l2_open(file, inst);
1717 vdec->fixed_fmt = false;
1718 vdec->state = VPU_CODEC_STATE_ACTIVE;
1719 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1720 inst->min_buffer_out = VDEC_MIN_BUFFER_OUT;
1726 static const struct v4l2_file_operations vdec_fops = {
1727 .owner = THIS_MODULE,
1729 .release = vpu_v4l2_close,
1730 .unlocked_ioctl = video_ioctl2,
1731 .poll = v4l2_m2m_fop_poll,
1732 .mmap = v4l2_m2m_fop_mmap,
1735 const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void)
1737 return &vdec_ioctl_ops;
1740 const struct v4l2_file_operations *vdec_get_fops(void)