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] %s -> %s\n", inst->id,
303 vpu_codec_state_name(pre_state), vpu_codec_state_name(inst->state));
305 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
306 vdec_handle_resolution_change(inst);
311 static void vdec_set_last_buffer_dequeued(struct vpu_inst *inst)
313 struct vdec_t *vdec = inst->priv;
315 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
318 if (vdec->eos_received) {
319 if (!vpu_set_last_buffer_dequeued(inst, true)) {
320 vdec->eos_received--;
321 vdec_update_state(inst, VPU_CODEC_STATE_DRAIN, 0);
326 static int vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
328 strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
329 strscpy(cap->card, "amphion vpu decoder", sizeof(cap->card));
330 strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
335 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
337 struct vpu_inst *inst = to_inst(file);
338 struct vdec_t *vdec = inst->priv;
339 const struct vpu_format *fmt;
343 if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
344 fmt = vpu_get_format(inst, f->type);
346 fmt = vpu_helper_find_sibling(inst, f->type, fmt->pixfmt);
350 fmt = vpu_helper_enum_format(inst, f->type, f->index);
355 memset(f->reserved, 0, sizeof(f->reserved));
356 f->pixelformat = fmt->pixfmt;
357 f->flags = fmt->flags;
360 vpu_inst_unlock(inst);
364 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
366 struct vpu_inst *inst = to_inst(file);
367 struct vdec_t *vdec = inst->priv;
368 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
369 struct vpu_format *cur_fmt;
373 cur_fmt = vpu_get_format(inst, f->type);
375 pixmp->pixelformat = cur_fmt->pixfmt;
376 pixmp->num_planes = cur_fmt->mem_planes;
377 pixmp->width = cur_fmt->width;
378 pixmp->height = cur_fmt->height;
379 pixmp->field = cur_fmt->field;
380 pixmp->flags = cur_fmt->flags;
381 for (i = 0; i < pixmp->num_planes; i++) {
382 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
383 pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
386 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
387 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
388 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
389 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
390 vpu_inst_unlock(inst);
395 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
397 struct vpu_inst *inst = to_inst(file);
398 struct vdec_t *vdec = inst->priv;
399 struct vpu_format fmt;
402 if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
403 struct vpu_format *cap_fmt = vpu_get_format(inst, f->type);
405 if (!vpu_helper_match_format(inst, cap_fmt->type, cap_fmt->pixfmt,
406 f->fmt.pix_mp.pixelformat))
407 f->fmt.pix_mp.pixelformat = cap_fmt->pixfmt;
410 vpu_try_fmt_common(inst, f, &fmt);
412 if (vdec->fixed_fmt) {
413 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
414 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
415 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
416 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
418 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
419 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
420 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
421 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
423 vpu_inst_unlock(inst);
428 static int vdec_s_fmt_common(struct vpu_inst *inst, struct v4l2_format *f)
430 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
431 struct vpu_format fmt;
432 struct vpu_format *cur_fmt;
434 struct vdec_t *vdec = inst->priv;
437 if (!inst->fh.m2m_ctx)
440 q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
446 if (vpu_try_fmt_common(inst, f, &fmt))
449 cur_fmt = vpu_get_format(inst, f->type);
450 if (V4L2_TYPE_IS_OUTPUT(f->type) && inst->state != VPU_CODEC_STATE_DEINIT) {
451 if (cur_fmt->pixfmt != fmt.pixfmt) {
452 vdec->reset_codec = true;
453 vdec->fixed_fmt = false;
456 if (V4L2_TYPE_IS_OUTPUT(f->type) || !vdec->fixed_fmt) {
457 memcpy(cur_fmt, &fmt, sizeof(*cur_fmt));
459 if (vpu_helper_match_format(inst, f->type, cur_fmt->pixfmt, pixmp->pixelformat)) {
460 cur_fmt->pixfmt = fmt.pixfmt;
461 cur_fmt->mem_planes = fmt.mem_planes;
463 pixmp->pixelformat = cur_fmt->pixfmt;
464 pixmp->num_planes = cur_fmt->mem_planes;
465 pixmp->width = cur_fmt->width;
466 pixmp->height = cur_fmt->height;
467 for (i = 0; i < pixmp->num_planes; i++) {
468 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
469 pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
471 pixmp->field = cur_fmt->field;
474 if (!vdec->fixed_fmt) {
475 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
476 vdec->params.codec_format = cur_fmt->pixfmt;
477 vdec->codec_info.color_primaries = f->fmt.pix_mp.colorspace;
478 vdec->codec_info.transfer_chars = f->fmt.pix_mp.xfer_func;
479 vdec->codec_info.matrix_coeffs = f->fmt.pix_mp.ycbcr_enc;
480 vdec->codec_info.full_range = f->fmt.pix_mp.quantization;
482 vdec->params.output_format = cur_fmt->pixfmt;
485 inst->crop.width = cur_fmt->width;
486 inst->crop.height = cur_fmt->height;
490 vpu_trace(inst->dev, "[%d] %c%c%c%c %dx%d\n", inst->id,
491 f->fmt.pix_mp.pixelformat,
492 f->fmt.pix_mp.pixelformat >> 8,
493 f->fmt.pix_mp.pixelformat >> 16,
494 f->fmt.pix_mp.pixelformat >> 24,
496 f->fmt.pix_mp.height);
501 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
503 struct vpu_inst *inst = to_inst(file);
504 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
505 struct vdec_t *vdec = inst->priv;
509 ret = vdec_s_fmt_common(inst, f);
513 if (V4L2_TYPE_IS_OUTPUT(f->type) && !vdec->fixed_fmt) {
514 struct v4l2_format fc;
516 memset(&fc, 0, sizeof(fc));
517 fc.type = inst->cap_format.type;
518 fc.fmt.pix_mp.pixelformat = inst->cap_format.pixfmt;
519 fc.fmt.pix_mp.width = pixmp->width;
520 fc.fmt.pix_mp.height = pixmp->height;
521 vdec_s_fmt_common(inst, &fc);
524 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
525 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
526 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
527 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
530 vpu_inst_unlock(inst);
534 static int vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
536 struct vpu_inst *inst = to_inst(file);
538 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
542 case V4L2_SEL_TGT_COMPOSE:
543 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
544 case V4L2_SEL_TGT_COMPOSE_PADDED:
547 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
550 s->r.width = inst->cap_format.width;
551 s->r.height = inst->cap_format.height;
560 static int vdec_drain(struct vpu_inst *inst)
562 struct vdec_t *vdec = inst->priv;
564 if (!inst->fh.m2m_ctx)
570 if (!vpu_is_source_empty(inst))
573 if (!vdec->params.frame_count) {
574 vpu_set_last_buffer_dequeued(inst, true);
578 vpu_iface_add_scode(inst, SCODE_PADDING_EOS);
579 vdec->params.end_flag = 1;
580 vpu_iface_set_decode_params(inst, &vdec->params, 1);
582 vpu_trace(inst->dev, "[%d] frame_count = %d\n", inst->id, vdec->params.frame_count);
587 static int vdec_cmd_start(struct vpu_inst *inst)
589 struct vdec_t *vdec = inst->priv;
591 switch (inst->state) {
592 case VPU_CODEC_STATE_STARTED:
593 case VPU_CODEC_STATE_DRAIN:
594 case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE:
595 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
600 vpu_process_capture_buffer(inst);
601 if (vdec->eos_received)
602 vdec_set_last_buffer_dequeued(inst);
606 static int vdec_cmd_stop(struct vpu_inst *inst)
608 struct vdec_t *vdec = inst->priv;
610 vpu_trace(inst->dev, "[%d]\n", inst->id);
612 if (inst->state == VPU_CODEC_STATE_DEINIT) {
613 vpu_set_last_buffer_dequeued(inst, true);
622 static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
624 struct vpu_inst *inst = to_inst(file);
627 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
633 case V4L2_DEC_CMD_START:
634 vdec_cmd_start(inst);
636 case V4L2_DEC_CMD_STOP:
642 vpu_inst_unlock(inst);
647 static int vdec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
651 return v4l2_event_subscribe(fh, sub, 0, NULL);
652 case V4L2_EVENT_SOURCE_CHANGE:
653 return v4l2_src_change_event_subscribe(fh, sub);
654 case V4L2_EVENT_CTRL:
655 return v4l2_ctrl_subscribe_event(fh, sub);
663 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
664 .vidioc_querycap = vdec_querycap,
665 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
666 .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
667 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
668 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
669 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
670 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
671 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
672 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
673 .vidioc_g_selection = vdec_g_selection,
674 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
675 .vidioc_decoder_cmd = vdec_decoder_cmd,
676 .vidioc_subscribe_event = vdec_subscribe_event,
677 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
678 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
679 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
680 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
681 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
682 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
683 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
684 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
685 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
686 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
689 static bool vdec_check_ready(struct vpu_inst *inst, unsigned int type)
691 struct vdec_t *vdec = inst->priv;
693 if (V4L2_TYPE_IS_OUTPUT(type))
696 if (vdec->req_frame_count)
702 static struct vb2_v4l2_buffer *vdec_get_src_buffer(struct vpu_inst *inst, u32 count)
705 vpu_skip_frame(inst, count - 1);
707 return vpu_next_src_buf(inst);
710 static int vdec_frame_decoded(struct vpu_inst *inst, void *arg)
712 struct vdec_t *vdec = inst->priv;
713 struct vpu_dec_pic_info *info = arg;
714 struct vpu_vb2_buffer *vpu_buf;
715 struct vb2_v4l2_buffer *vbuf;
716 struct vb2_v4l2_buffer *src_buf;
719 if (!info || info->id >= ARRAY_SIZE(vdec->slots))
723 vpu_buf = vdec->slots[info->id];
725 dev_err(inst->dev, "[%d] decoded invalid frame[%d]\n", inst->id, info->id);
729 vbuf = &vpu_buf->m2m_buf.vb;
730 src_buf = vdec_get_src_buffer(inst, info->consumed_count);
732 v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
733 if (info->consumed_count) {
734 v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
735 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
736 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
738 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_DECODED);
741 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED)
742 dev_info(inst->dev, "[%d] buf[%d] has been decoded\n", inst->id, info->id);
743 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_DECODED);
744 vdec->decoded_frame_count++;
745 if (vdec->params.display_delay_enable) {
746 struct vpu_format *cur_fmt;
748 cur_fmt = vpu_get_format(inst, inst->cap_format.type);
749 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY);
750 for (int i = 0; i < vbuf->vb2_buf.num_planes; i++)
751 vb2_set_plane_payload(&vbuf->vb2_buf,
752 i, vpu_get_fmt_plane_size(cur_fmt, i));
753 vbuf->field = cur_fmt->field;
754 vbuf->sequence = vdec->sequence++;
755 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
757 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
758 vdec->display_frame_count++;
761 vpu_inst_unlock(inst);
766 static struct vpu_vb2_buffer *vdec_find_buffer(struct vpu_inst *inst, u32 luma)
768 struct vdec_t *vdec = inst->priv;
771 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
774 if (luma == vdec->slots[i]->luma)
775 return vdec->slots[i];
781 static void vdec_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame)
783 struct vdec_t *vdec = inst->priv;
784 struct vpu_format *cur_fmt;
785 struct vpu_vb2_buffer *vpu_buf;
786 struct vb2_v4l2_buffer *vbuf;
793 if (!vdec->params.display_delay_enable)
795 vpu_buf = vdec_find_buffer(inst, frame->luma);
796 vpu_inst_unlock(inst);
798 dev_err(inst->dev, "[%d] can't find buffer, id = %d, addr = 0x%x\n",
799 inst->id, frame->id, frame->luma);
802 if (frame->skipped) {
803 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
807 cur_fmt = vpu_get_format(inst, inst->cap_format.type);
808 vbuf = &vpu_buf->m2m_buf.vb;
809 if (vbuf->vb2_buf.index != frame->id)
810 dev_err(inst->dev, "[%d] buffer id(%d, %d) dismatch\n",
811 inst->id, vbuf->vb2_buf.index, frame->id);
813 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_READY && vdec->params.display_delay_enable)
816 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_DECODED)
817 dev_err(inst->dev, "[%d] buffer(%d) ready without decoded\n", inst->id, frame->id);
819 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY);
820 for (i = 0; i < vbuf->vb2_buf.num_planes; i++)
821 vb2_set_plane_payload(&vbuf->vb2_buf, i, vpu_get_fmt_plane_size(cur_fmt, i));
822 vbuf->field = cur_fmt->field;
823 vbuf->sequence = vdec->sequence;
824 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
826 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
828 vdec->display_frame_count++;
829 vpu_inst_unlock(inst);
830 dev_dbg(inst->dev, "[%d] decoded : %d, display : %d, sequence : %d\n",
831 inst->id, vdec->decoded_frame_count, vdec->display_frame_count, vdec->sequence);
834 static void vdec_stop_done(struct vpu_inst *inst)
836 struct vdec_t *vdec = inst->priv;
839 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 0);
840 vdec->seq_hdr_found = 0;
841 vdec->req_frame_count = 0;
842 vdec->reset_codec = false;
843 vdec->fixed_fmt = false;
844 vdec->params.end_flag = 0;
846 vdec->params.frame_count = 0;
847 vdec->decoded_frame_count = 0;
848 vdec->display_frame_count = 0;
850 vdec->eos_received = 0;
851 vdec->is_source_changed = false;
852 vdec->source_change = 0;
853 inst->total_input_count = 0;
854 vpu_inst_unlock(inst);
857 static bool vdec_check_source_change(struct vpu_inst *inst)
859 struct vdec_t *vdec = inst->priv;
860 const struct vpu_format *sibling;
862 if (!inst->fh.m2m_ctx)
865 if (vdec->reset_codec)
868 sibling = vpu_helper_find_sibling(inst, inst->cap_format.type, inst->cap_format.pixfmt);
869 if (sibling && vdec->codec_info.pixfmt == sibling->pixfmt)
870 vdec->codec_info.pixfmt = inst->cap_format.pixfmt;
872 if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)))
874 if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
876 if (inst->cap_format.width != vdec->codec_info.decoded_width)
878 if (inst->cap_format.height != vdec->codec_info.decoded_height)
880 if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap)
882 if (inst->crop.left != vdec->codec_info.offset_x)
884 if (inst->crop.top != vdec->codec_info.offset_y)
886 if (inst->crop.width != vdec->codec_info.width)
888 if (inst->crop.height != vdec->codec_info.height)
894 static void vdec_init_fmt(struct vpu_inst *inst)
896 struct vdec_t *vdec = inst->priv;
897 struct v4l2_format f;
899 memset(&f, 0, sizeof(f));
900 f.type = inst->cap_format.type;
901 f.fmt.pix_mp.pixelformat = vdec->codec_info.pixfmt;
902 f.fmt.pix_mp.width = vdec->codec_info.decoded_width;
903 f.fmt.pix_mp.height = vdec->codec_info.decoded_height;
904 if (vdec->codec_info.progressive)
905 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
907 f.fmt.pix_mp.field = V4L2_FIELD_SEQ_TB;
908 vpu_try_fmt_common(inst, &f, &inst->cap_format);
910 inst->out_format.width = vdec->codec_info.width;
911 inst->out_format.height = vdec->codec_info.height;
914 static void vdec_init_crop(struct vpu_inst *inst)
916 struct vdec_t *vdec = inst->priv;
918 inst->crop.left = vdec->codec_info.offset_x;
919 inst->crop.top = vdec->codec_info.offset_y;
920 inst->crop.width = vdec->codec_info.width;
921 inst->crop.height = vdec->codec_info.height;
924 static void vdec_init_mbi(struct vpu_inst *inst)
926 struct vdec_t *vdec = inst->priv;
928 vdec->mbi.size = vdec->codec_info.mbi_size;
929 vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer);
930 scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi");
931 vdec->mbi.type = MEM_RES_MBI;
932 vdec->mbi.tag = vdec->seq_tag;
935 static void vdec_init_dcp(struct vpu_inst *inst)
937 struct vdec_t *vdec = inst->priv;
939 vdec->dcp.size = vdec->codec_info.dcp_size;
940 vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer);
941 scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp");
942 vdec->dcp.type = MEM_RES_DCP;
943 vdec->dcp.tag = vdec->seq_tag;
946 static void vdec_request_one_fs(struct vdec_fs_info *fs)
949 if (fs->req_count > fs->max_count)
950 fs->req_count = fs->max_count;
953 static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs)
955 struct vpu_buffer *buffer;
960 if (fs->count >= fs->req_count)
963 buffer = &fs->buffer[fs->count];
964 if (buffer->virt && buffer->length >= fs->size)
967 vpu_free_dma(buffer);
968 buffer->length = fs->size;
969 return vpu_alloc_dma(inst->core, buffer);
972 static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
976 while (fs->count < fs->req_count) {
977 ret = vdec_alloc_fs_buffer(inst, fs);
984 static void vdec_clear_fs(struct vdec_fs_info *fs)
991 for (i = 0; i < ARRAY_SIZE(fs->buffer); i++)
992 vpu_free_dma(&fs->buffer[i]);
993 memset(fs, 0, sizeof(*fs));
996 static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
998 struct vpu_fs_info info;
1001 if (fs->index >= fs->count)
1004 memset(&info, 0, sizeof(info));
1005 info.id = fs->index;
1006 info.type = fs->type;
1008 info.luma_addr = fs->buffer[fs->index].phys;
1009 info.luma_size = fs->buffer[fs->index].length;
1010 ret = vpu_session_alloc_fs(inst, &info);
1018 static int vdec_response_frame_abnormal(struct vpu_inst *inst)
1020 struct vdec_t *vdec = inst->priv;
1021 struct vpu_fs_info info;
1024 if (!vdec->req_frame_count)
1027 memset(&info, 0, sizeof(info));
1028 info.type = MEM_RES_FRAME;
1029 info.tag = vdec->seq_tag + 0xf0;
1030 ret = vpu_session_alloc_fs(inst, &info);
1033 vdec->req_frame_count--;
1038 static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1040 struct vdec_t *vdec = inst->priv;
1041 struct vpu_vb2_buffer *vpu_buf;
1042 struct vpu_fs_info info;
1045 if (inst->state != VPU_CODEC_STATE_ACTIVE)
1051 if (!vdec->req_frame_count)
1057 if (vdec->slots[vbuf->vb2_buf.index]) {
1058 dev_err(inst->dev, "[%d] repeat alloc fs %d\n",
1059 inst->id, vbuf->vb2_buf.index);
1063 dev_dbg(inst->dev, "[%d] state = %s, alloc fs %d, tag = 0x%x\n",
1064 inst->id, vpu_codec_state_name(inst->state), vbuf->vb2_buf.index, vdec->seq_tag);
1065 vpu_buf = to_vpu_vb2_buffer(vbuf);
1067 memset(&info, 0, sizeof(info));
1068 info.id = vbuf->vb2_buf.index;
1069 info.type = MEM_RES_FRAME;
1070 info.tag = vdec->seq_tag;
1071 info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0);
1072 info.luma_size = inst->cap_format.sizeimage[0];
1073 if (vbuf->vb2_buf.num_planes > 1)
1074 info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1);
1076 info.chroma_addr = info.luma_addr + info.luma_size;
1077 info.chromau_size = inst->cap_format.sizeimage[1];
1078 info.bytesperline = inst->cap_format.bytesperline[0];
1079 ret = vpu_session_alloc_fs(inst, &info);
1083 vpu_buf->tag = info.tag;
1084 vpu_buf->luma = info.luma_addr;
1085 vpu_buf->chroma_u = info.chroma_addr;
1086 vpu_buf->chroma_v = 0;
1087 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1088 vdec->slots[info.id] = vpu_buf;
1089 vdec->req_frame_count--;
1094 static void vdec_response_fs_request(struct vpu_inst *inst, bool force)
1096 struct vdec_t *vdec = inst->priv;
1101 for (i = vdec->req_frame_count; i > 0; i--)
1102 vdec_response_frame_abnormal(inst);
1106 for (i = vdec->req_frame_count; i > 0; i--) {
1107 ret = vpu_process_capture_buffer(inst);
1110 if (vdec->eos_received)
1114 for (i = vdec->mbi.index; i < vdec->mbi.count; i++) {
1115 if (vdec_response_fs(inst, &vdec->mbi))
1117 if (vdec->eos_received)
1120 for (i = vdec->dcp.index; i < vdec->dcp.count; i++) {
1121 if (vdec_response_fs(inst, &vdec->dcp))
1123 if (vdec->eos_received)
1128 static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag)
1130 struct vpu_fs_info info;
1132 memset(&info, 0, sizeof(info));
1135 vpu_session_release_fs(inst, &info);
1138 static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1140 if (!inst->fh.m2m_ctx)
1142 if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE)
1144 if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index))
1146 v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
1149 static void vdec_clear_slots(struct vpu_inst *inst)
1151 struct vdec_t *vdec = inst->priv;
1152 struct vpu_vb2_buffer *vpu_buf;
1153 struct vb2_v4l2_buffer *vbuf;
1156 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
1157 if (!vdec->slots[i])
1160 vpu_buf = vdec->slots[i];
1161 vbuf = &vpu_buf->m2m_buf.vb;
1163 vpu_trace(inst->dev, "clear slot %d\n", i);
1164 vdec_response_fs_release(inst, i, vpu_buf->tag);
1165 vdec_recycle_buffer(inst, vbuf);
1166 vdec->slots[i]->state = VPU_BUF_STATE_IDLE;
1167 vdec->slots[i] = NULL;
1171 static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)
1173 struct vdec_t *vdec = inst->priv;
1175 vpu_inst_lock(inst);
1176 memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
1178 vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
1180 vdec->codec_info.decoded_width,
1181 vdec->codec_info.decoded_height,
1182 vdec->codec_info.offset_x,
1183 vdec->codec_info.offset_y,
1184 vdec->codec_info.width,
1185 vdec->codec_info.height,
1188 inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;
1189 vdec->is_source_changed = vdec_check_source_change(inst);
1190 vdec_init_fmt(inst);
1191 vdec_init_crop(inst);
1192 vdec_init_mbi(inst);
1193 vdec_init_dcp(inst);
1194 if (!vdec->seq_hdr_found) {
1195 vdec->seq_tag = vdec->codec_info.tag;
1196 if (vdec->is_source_changed) {
1197 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1198 vdec->source_change++;
1199 vdec_handle_resolution_change(inst);
1200 vdec->is_source_changed = false;
1203 if (vdec->seq_tag != vdec->codec_info.tag) {
1204 vdec_response_fs_request(inst, true);
1205 vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n",
1206 inst->id, vdec->seq_tag, vdec->codec_info.tag);
1208 vdec->seq_hdr_found++;
1209 vdec->fixed_fmt = true;
1210 vpu_inst_unlock(inst);
1213 static void vdec_event_resolution_change(struct vpu_inst *inst)
1215 struct vdec_t *vdec = inst->priv;
1217 vpu_trace(inst->dev, "[%d]\n", inst->id);
1218 vpu_inst_lock(inst);
1219 vdec->seq_tag = vdec->codec_info.tag;
1220 vdec_clear_fs(&vdec->mbi);
1221 vdec_clear_fs(&vdec->dcp);
1222 vdec_clear_slots(inst);
1223 vdec_init_mbi(inst);
1224 vdec_init_dcp(inst);
1225 if (vdec->is_source_changed) {
1226 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1227 vdec->source_change++;
1228 vdec_handle_resolution_change(inst);
1229 vdec->is_source_changed = false;
1231 vpu_inst_unlock(inst);
1234 static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1236 struct vdec_t *vdec = inst->priv;
1241 vpu_inst_lock(inst);
1245 vdec->req_frame_count++;
1248 vdec_request_one_fs(&vdec->mbi);
1251 vdec_request_one_fs(&vdec->dcp);
1257 vdec_alloc_fs(inst, &vdec->mbi);
1258 vdec_alloc_fs(inst, &vdec->dcp);
1260 vdec_response_fs_request(inst, false);
1262 vpu_inst_unlock(inst);
1265 static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1267 struct vdec_t *vdec = inst->priv;
1268 struct vpu_vb2_buffer *vpu_buf;
1269 struct vb2_v4l2_buffer *vbuf;
1271 if (!fs || fs->id >= ARRAY_SIZE(vdec->slots))
1273 if (fs->type != MEM_RES_FRAME)
1276 if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) {
1277 dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id);
1281 vpu_inst_lock(inst);
1282 vpu_buf = vdec->slots[fs->id];
1283 vdec->slots[fs->id] = NULL;
1286 dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id);
1290 vbuf = &vpu_buf->m2m_buf.vb;
1291 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) {
1292 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
1296 vdec_response_fs_release(inst, fs->id, vpu_buf->tag);
1297 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY)
1298 vdec_recycle_buffer(inst, vbuf);
1300 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
1301 vpu_process_capture_buffer(inst);
1304 vpu_inst_unlock(inst);
1307 static void vdec_event_eos(struct vpu_inst *inst)
1309 struct vdec_t *vdec = inst->priv;
1311 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1313 vdec->params.frame_count,
1314 vdec->decoded_frame_count,
1315 vdec->display_frame_count,
1317 vpu_inst_lock(inst);
1318 vdec->eos_received++;
1319 vdec->fixed_fmt = false;
1320 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1321 vdec_set_last_buffer_dequeued(inst);
1322 vpu_inst_unlock(inst);
1325 static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data)
1328 case VPU_MSG_ID_SEQ_HDR_FOUND:
1329 vdec_event_seq_hdr(inst, data);
1331 case VPU_MSG_ID_RES_CHANGE:
1332 vdec_event_resolution_change(inst);
1334 case VPU_MSG_ID_FRAME_REQ:
1335 vdec_event_req_fs(inst, data);
1337 case VPU_MSG_ID_FRAME_RELEASE:
1338 vdec_evnet_rel_fs(inst, data);
1340 case VPU_MSG_ID_PIC_EOS:
1341 vdec_event_eos(inst);
1348 static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1350 struct vdec_t *vdec = inst->priv;
1351 struct vb2_v4l2_buffer *vbuf;
1352 struct vpu_rpc_buffer_desc desc;
1356 vbuf = to_vb2_v4l2_buffer(vb);
1357 dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n",
1358 inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0));
1360 if (inst->state == VPU_CODEC_STATE_DEINIT)
1362 if (vdec->reset_codec)
1365 if (inst->state == VPU_CODEC_STATE_STARTED)
1366 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
1368 ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1372 free_space = vpu_helper_get_free_space(inst);
1373 if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000)
1376 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1377 ret = vpu_iface_input_frame(inst, vb);
1381 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp);
1382 vdec->params.frame_count++;
1390 static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1392 struct vdec_t *vdec = inst->priv;
1393 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1396 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
1398 if (vdec->reset_codec)
1401 ret = vdec_response_frame(inst, vbuf);
1404 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1408 static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type)
1410 struct vdec_t *vdec = inst->priv;
1412 if (V4L2_TYPE_IS_OUTPUT(type))
1415 vdec_handle_resolution_change(inst);
1416 if (vdec->eos_received)
1417 vdec_set_last_buffer_dequeued(inst);
1420 static void vdec_abort(struct vpu_inst *inst)
1422 struct vdec_t *vdec = inst->priv;
1423 struct vpu_rpc_buffer_desc desc;
1426 vpu_trace(inst->dev, "[%d] state = %s\n", inst->id, vpu_codec_state_name(inst->state));
1428 vdec->aborting = true;
1429 vpu_iface_add_scode(inst, SCODE_PADDING_ABORT);
1430 vdec->params.end_flag = 1;
1431 vpu_iface_set_decode_params(inst, &vdec->params, 1);
1433 vpu_session_abort(inst);
1435 ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1437 vpu_iface_update_stream_buffer(inst, desc.rptr, 1);
1439 vpu_session_rst_buf(inst);
1440 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1442 vdec->params.frame_count,
1443 vdec->decoded_frame_count,
1444 vdec->display_frame_count,
1446 if (!vdec->seq_hdr_found)
1447 vdec->reset_codec = true;
1448 vdec->params.end_flag = 0;
1450 vdec->params.frame_count = 0;
1451 vdec->decoded_frame_count = 0;
1452 vdec->display_frame_count = 0;
1454 vdec->aborting = false;
1455 inst->extra_size = 0;
1458 static void vdec_stop(struct vpu_inst *inst, bool free)
1460 struct vdec_t *vdec = inst->priv;
1462 vdec_clear_slots(inst);
1463 if (inst->state != VPU_CODEC_STATE_DEINIT)
1464 vpu_session_stop(inst);
1465 vdec_clear_fs(&vdec->mbi);
1466 vdec_clear_fs(&vdec->dcp);
1468 vpu_free_dma(&vdec->udata);
1469 vpu_free_dma(&inst->stream_buffer);
1471 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1);
1472 vdec->reset_codec = false;
1475 static void vdec_release(struct vpu_inst *inst)
1477 if (inst->id != VPU_INST_NULL_ID)
1478 vpu_trace(inst->dev, "[%d]\n", inst->id);
1479 vdec_stop(inst, true);
1482 static void vdec_cleanup(struct vpu_inst *inst)
1484 struct vdec_t *vdec;
1495 static void vdec_init_params(struct vdec_t *vdec)
1497 vdec->params.frame_count = 0;
1498 vdec->params.end_flag = 0;
1501 static int vdec_start(struct vpu_inst *inst)
1503 struct vdec_t *vdec = inst->priv;
1504 int stream_buffer_size;
1507 if (inst->state != VPU_CODEC_STATE_DEINIT)
1510 vpu_trace(inst->dev, "[%d]\n", inst->id);
1511 if (!vdec->udata.virt) {
1512 vdec->udata.length = 0x1000;
1513 ret = vpu_alloc_dma(inst->core, &vdec->udata);
1515 dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id);
1520 if (!inst->stream_buffer.virt) {
1521 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
1522 if (stream_buffer_size > 0) {
1523 inst->stream_buffer.length = stream_buffer_size;
1524 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
1526 dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id);
1529 inst->use_stream_buffer = true;
1533 if (inst->use_stream_buffer)
1534 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
1535 vpu_iface_init_instance(inst);
1536 vdec->params.udata.base = vdec->udata.phys;
1537 vdec->params.udata.size = vdec->udata.length;
1538 ret = vpu_iface_set_decode_params(inst, &vdec->params, 0);
1540 dev_err(inst->dev, "[%d] set decode params fail\n", inst->id);
1544 vdec_init_params(vdec);
1545 ret = vpu_session_start(inst);
1547 dev_err(inst->dev, "[%d] start fail\n", inst->id);
1551 vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0);
1555 vpu_free_dma(&vdec->udata);
1556 vpu_free_dma(&inst->stream_buffer);
1560 static int vdec_start_session(struct vpu_inst *inst, u32 type)
1562 struct vdec_t *vdec = inst->priv;
1565 if (V4L2_TYPE_IS_OUTPUT(type)) {
1566 if (vdec->reset_codec)
1567 vdec_stop(inst, false);
1568 if (inst->state == VPU_CODEC_STATE_DEINIT) {
1569 ret = vdec_start(inst);
1575 if (V4L2_TYPE_IS_OUTPUT(type)) {
1576 vdec_update_state(inst, vdec->state, 1);
1577 vdec->eos_received = 0;
1578 vpu_process_output_buffer(inst);
1580 vdec_cmd_start(inst);
1582 if (inst->state == VPU_CODEC_STATE_ACTIVE)
1583 vdec_response_fs_request(inst, false);
1588 static int vdec_stop_session(struct vpu_inst *inst, u32 type)
1590 struct vdec_t *vdec = inst->priv;
1592 if (inst->state == VPU_CODEC_STATE_DEINIT)
1595 if (V4L2_TYPE_IS_OUTPUT(type)) {
1596 vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0);
1599 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) {
1601 vdec->eos_received = 0;
1603 vdec_clear_slots(inst);
1609 static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1611 struct vdec_t *vdec = inst->priv;
1616 num = scnprintf(str, size,
1617 "req_frame_count = %d\ninterlaced = %d\n",
1618 vdec->req_frame_count,
1619 vdec->codec_info.progressive ? 0 : 1);
1622 num = scnprintf(str, size,
1623 "mbi: size = 0x%x request = %d, alloc = %d, response = %d\n",
1625 vdec->mbi.req_count,
1630 num = scnprintf(str, size,
1631 "dcp: size = 0x%x request = %d, alloc = %d, response = %d\n",
1633 vdec->dcp.req_count,
1638 num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count);
1641 num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count);
1644 num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count);
1647 num = scnprintf(str, size, "sequence = %d\n", vdec->sequence);
1650 num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n",
1651 vdec->drain, vdec->eos_received, vdec->source_change);
1654 num = scnprintf(str, size, "fps = %d/%d\n",
1655 vdec->codec_info.frame_rate.numerator,
1656 vdec->codec_info.frame_rate.denominator);
1659 num = scnprintf(str, size, "colorspace: %d, %d, %d, %d (%d)\n",
1660 vdec->codec_info.color_primaries,
1661 vdec->codec_info.transfer_chars,
1662 vdec->codec_info.matrix_coeffs,
1663 vdec->codec_info.full_range,
1664 vdec->codec_info.vui_present);
1673 static struct vpu_inst_ops vdec_inst_ops = {
1674 .ctrl_init = vdec_ctrl_init,
1675 .check_ready = vdec_check_ready,
1676 .buf_done = vdec_buf_done,
1677 .get_one_frame = vdec_frame_decoded,
1678 .stop_done = vdec_stop_done,
1679 .event_notify = vdec_event_notify,
1680 .release = vdec_release,
1681 .cleanup = vdec_cleanup,
1682 .start = vdec_start_session,
1683 .stop = vdec_stop_session,
1684 .process_output = vdec_process_output,
1685 .process_capture = vdec_process_capture,
1686 .on_queue_empty = vdec_on_queue_empty,
1687 .get_debug_info = vdec_get_debug_info,
1688 .wait_prepare = vpu_inst_unlock,
1689 .wait_finish = vpu_inst_lock,
1692 static void vdec_init(struct file *file)
1694 struct vpu_inst *inst = to_inst(file);
1695 struct v4l2_format f;
1697 memset(&f, 0, sizeof(f));
1698 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1699 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1700 f.fmt.pix_mp.width = 1280;
1701 f.fmt.pix_mp.height = 720;
1702 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1703 vdec_s_fmt(file, &inst->fh, &f);
1705 memset(&f, 0, sizeof(f));
1706 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1707 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M_8L128;
1708 f.fmt.pix_mp.width = 1280;
1709 f.fmt.pix_mp.height = 720;
1710 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1711 vdec_s_fmt(file, &inst->fh, &f);
1714 static int vdec_open(struct file *file)
1716 struct vpu_inst *inst;
1717 struct vdec_t *vdec;
1720 inst = vzalloc(sizeof(*inst));
1724 vdec = vzalloc(sizeof(*vdec));
1730 inst->ops = &vdec_inst_ops;
1731 inst->formats = vdec_formats;
1732 inst->type = VPU_CORE_TYPE_DEC;
1735 ret = vpu_v4l2_open(file, inst);
1739 vdec->fixed_fmt = false;
1740 vdec->state = VPU_CODEC_STATE_ACTIVE;
1741 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1742 inst->min_buffer_out = VDEC_MIN_BUFFER_OUT;
1748 static const struct v4l2_file_operations vdec_fops = {
1749 .owner = THIS_MODULE,
1751 .release = vpu_v4l2_close,
1752 .unlocked_ioctl = video_ioctl2,
1753 .poll = v4l2_m2m_fop_poll,
1754 .mmap = v4l2_m2m_fop_mmap,
1757 const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void)
1759 return &vdec_ioctl_ops;
1762 const struct v4l2_file_operations *vdec_get_fops(void)