Lines Matching +full:top +full:- +full:ctrl
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020-2021 NXP
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-event.h>
19 #include <media/v4l2-mem2mem.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/videobuf2-v4l2.h>
22 #include <media/videobuf2-dma-contig.h>
23 #include <media/videobuf2-vmalloc.h>
97 strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver)); in venc_querycap()
98 strscpy(cap->card, "amphion vpu encoder", sizeof(cap->card)); in venc_querycap()
99 strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info)); in venc_querycap()
109 memset(f->reserved, 0, sizeof(f->reserved)); in venc_enum_fmt()
110 fmt = vpu_helper_enum_format(inst, f->type, f->index); in venc_enum_fmt()
112 return -EINVAL; in venc_enum_fmt()
114 f->pixelformat = fmt->pixfmt; in venc_enum_fmt()
115 f->flags = fmt->flags; in venc_enum_fmt()
125 if (!fsize || fsize->index) in venc_enum_framesizes()
126 return -EINVAL; in venc_enum_framesizes()
128 if (!vpu_helper_find_format(inst, 0, fsize->pixel_format)) in venc_enum_framesizes()
129 return -EINVAL; in venc_enum_framesizes()
133 return -EINVAL; in venc_enum_framesizes()
134 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; in venc_enum_framesizes()
135 fsize->stepwise.max_width = res->max_width; in venc_enum_framesizes()
136 fsize->stepwise.max_height = res->max_height; in venc_enum_framesizes()
137 fsize->stepwise.min_width = res->min_width; in venc_enum_framesizes()
138 fsize->stepwise.min_height = res->min_height; in venc_enum_framesizes()
139 fsize->stepwise.step_width = res->step_width; in venc_enum_framesizes()
140 fsize->stepwise.step_height = res->step_height; in venc_enum_framesizes()
150 if (!fival || fival->index) in venc_enum_frameintervals()
151 return -EINVAL; in venc_enum_frameintervals()
153 if (!vpu_helper_find_format(inst, 0, fival->pixel_format)) in venc_enum_frameintervals()
154 return -EINVAL; in venc_enum_frameintervals()
156 if (!fival->width || !fival->height) in venc_enum_frameintervals()
157 return -EINVAL; in venc_enum_frameintervals()
161 return -EINVAL; in venc_enum_frameintervals()
162 if (fival->width < res->min_width || fival->width > res->max_width || in venc_enum_frameintervals()
163 fival->height < res->min_height || fival->height > res->max_height) in venc_enum_frameintervals()
164 return -EINVAL; in venc_enum_frameintervals()
166 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS; in venc_enum_frameintervals()
167 fival->stepwise.min.numerator = 1; in venc_enum_frameintervals()
168 fival->stepwise.min.denominator = USHRT_MAX; in venc_enum_frameintervals()
169 fival->stepwise.max.numerator = USHRT_MAX; in venc_enum_frameintervals()
170 fival->stepwise.max.denominator = 1; in venc_enum_frameintervals()
171 fival->stepwise.step.numerator = 1; in venc_enum_frameintervals()
172 fival->stepwise.step.denominator = 1; in venc_enum_frameintervals()
180 struct venc_t *venc = inst->priv; in venc_g_fmt()
181 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; in venc_g_fmt()
185 cur_fmt = vpu_get_format(inst, f->type); in venc_g_fmt()
187 pixmp->pixelformat = cur_fmt->pixfmt; in venc_g_fmt()
188 pixmp->num_planes = cur_fmt->mem_planes; in venc_g_fmt()
189 pixmp->width = cur_fmt->width; in venc_g_fmt()
190 pixmp->height = cur_fmt->height; in venc_g_fmt()
191 pixmp->field = cur_fmt->field; in venc_g_fmt()
192 pixmp->flags = cur_fmt->flags; in venc_g_fmt()
193 for (i = 0; i < pixmp->num_planes; i++) { in venc_g_fmt()
194 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i]; in venc_g_fmt()
195 pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i); in venc_g_fmt()
198 f->fmt.pix_mp.colorspace = venc->params.color.primaries; in venc_g_fmt()
199 f->fmt.pix_mp.xfer_func = venc->params.color.transfer; in venc_g_fmt()
200 f->fmt.pix_mp.ycbcr_enc = venc->params.color.matrix; in venc_g_fmt()
201 f->fmt.pix_mp.quantization = venc->params.color.full_range; in venc_g_fmt()
222 struct venc_t *venc = inst->priv; in venc_s_fmt()
223 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; in venc_s_fmt()
225 q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type); in venc_s_fmt()
227 return -EINVAL; in venc_s_fmt()
229 return -EBUSY; in venc_s_fmt()
232 return -EINVAL; in venc_s_fmt()
234 cur_fmt = vpu_get_format(inst, f->type); in venc_s_fmt()
238 if (V4L2_TYPE_IS_OUTPUT(f->type)) { in venc_s_fmt()
239 venc->params.input_format = cur_fmt->pixfmt; in venc_s_fmt()
240 venc->params.src_stride = cur_fmt->bytesperline[0]; in venc_s_fmt()
241 venc->params.src_width = cur_fmt->width; in venc_s_fmt()
242 venc->params.src_height = cur_fmt->height; in venc_s_fmt()
243 venc->params.crop.left = 0; in venc_s_fmt()
244 venc->params.crop.top = 0; in venc_s_fmt()
245 venc->params.crop.width = cur_fmt->width; in venc_s_fmt()
246 venc->params.crop.height = cur_fmt->height; in venc_s_fmt()
248 venc->params.codec_format = cur_fmt->pixfmt; in venc_s_fmt()
249 venc->params.out_width = cur_fmt->width; in venc_s_fmt()
250 venc->params.out_height = cur_fmt->height; in venc_s_fmt()
253 if (V4L2_TYPE_IS_OUTPUT(f->type)) { in venc_s_fmt()
254 venc->params.color.primaries = pix_mp->colorspace; in venc_s_fmt()
255 venc->params.color.transfer = pix_mp->xfer_func; in venc_s_fmt()
256 venc->params.color.matrix = pix_mp->ycbcr_enc; in venc_s_fmt()
257 venc->params.color.full_range = pix_mp->quantization; in venc_s_fmt()
260 pix_mp->colorspace = venc->params.color.primaries; in venc_s_fmt()
261 pix_mp->xfer_func = venc->params.color.transfer; in venc_s_fmt()
262 pix_mp->ycbcr_enc = venc->params.color.matrix; in venc_s_fmt()
263 pix_mp->quantization = venc->params.color.full_range; in venc_s_fmt()
271 struct venc_t *venc = inst->priv; in venc_g_parm()
275 return -EINVAL; in venc_g_parm()
277 if (!V4L2_TYPE_IS_OUTPUT(parm->type)) in venc_g_parm()
278 return -EINVAL; in venc_g_parm()
280 if (!vpu_helper_check_type(inst, parm->type)) in venc_g_parm()
281 return -EINVAL; in venc_g_parm()
283 timeperframe = &parm->parm.capture.timeperframe; in venc_g_parm()
284 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; in venc_g_parm()
285 parm->parm.capture.readbuffers = 0; in venc_g_parm()
286 timeperframe->numerator = venc->params.frame_rate.numerator; in venc_g_parm()
287 timeperframe->denominator = venc->params.frame_rate.denominator; in venc_g_parm()
295 struct venc_t *venc = inst->priv; in venc_s_parm()
300 return -EINVAL; in venc_s_parm()
302 if (!V4L2_TYPE_IS_OUTPUT(parm->type)) in venc_s_parm()
303 return -EINVAL; in venc_s_parm()
305 if (!vpu_helper_check_type(inst, parm->type)) in venc_s_parm()
306 return -EINVAL; in venc_s_parm()
308 timeperframe = &parm->parm.capture.timeperframe; in venc_s_parm()
309 if (!timeperframe->numerator) in venc_s_parm()
310 timeperframe->numerator = venc->params.frame_rate.numerator; in venc_s_parm()
311 if (!timeperframe->denominator) in venc_s_parm()
312 timeperframe->denominator = venc->params.frame_rate.denominator; in venc_s_parm()
314 venc->params.frame_rate.numerator = timeperframe->numerator; in venc_s_parm()
315 venc->params.frame_rate.denominator = timeperframe->denominator; in venc_s_parm()
317 rational_best_approximation(venc->params.frame_rate.numerator, in venc_s_parm()
318 venc->params.frame_rate.denominator, in venc_s_parm()
319 venc->params.frame_rate.numerator, in venc_s_parm()
320 venc->params.frame_rate.denominator, in venc_s_parm()
322 venc->params.frame_rate.numerator = n; in venc_s_parm()
323 venc->params.frame_rate.denominator = d; in venc_s_parm()
325 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; in venc_s_parm()
326 memset(parm->parm.capture.reserved, 0, sizeof(parm->parm.capture.reserved)); in venc_s_parm()
334 struct venc_t *venc = inst->priv; in venc_g_selection()
336 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) in venc_g_selection()
337 return -EINVAL; in venc_g_selection()
339 switch (s->target) { in venc_g_selection()
342 s->r.left = 0; in venc_g_selection()
343 s->r.top = 0; in venc_g_selection()
344 s->r.width = inst->out_format.width; in venc_g_selection()
345 s->r.height = inst->out_format.height; in venc_g_selection()
348 s->r = venc->params.crop; in venc_g_selection()
351 return -EINVAL; in venc_g_selection()
365 rect = &venc->params.crop; in venc_valid_crop()
366 min_width = res->min_width; in venc_valid_crop()
367 min_height = res->min_height; in venc_valid_crop()
368 src_width = venc->params.src_width; in venc_valid_crop()
369 src_height = venc->params.src_height; in venc_valid_crop()
371 if (rect->width == 0 || rect->height == 0) in venc_valid_crop()
372 return -EINVAL; in venc_valid_crop()
373 if (rect->left > src_width - min_width || rect->top > src_height - min_height) in venc_valid_crop()
374 return -EINVAL; in venc_valid_crop()
376 rect->width = min(rect->width, src_width - rect->left); in venc_valid_crop()
377 rect->width = max_t(u32, rect->width, min_width); in venc_valid_crop()
379 rect->height = min(rect->height, src_height - rect->top); in venc_valid_crop()
380 rect->height = max_t(u32, rect->height, min_height); in venc_valid_crop()
389 struct venc_t *venc = inst->priv; in venc_s_selection()
393 return -EINVAL; in venc_s_selection()
395 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) in venc_s_selection()
396 return -EINVAL; in venc_s_selection()
397 if (s->target != V4L2_SEL_TGT_CROP) in venc_s_selection()
398 return -EINVAL; in venc_s_selection()
400 venc->params.crop.left = ALIGN(s->r.left, res->step_width); in venc_s_selection()
401 venc->params.crop.top = ALIGN(s->r.top, res->step_height); in venc_s_selection()
402 venc->params.crop.width = ALIGN(s->r.width, res->step_width); in venc_s_selection()
403 venc->params.crop.height = ALIGN(s->r.height, res->step_height); in venc_s_selection()
405 venc->params.crop.left = 0; in venc_s_selection()
406 venc->params.crop.top = 0; in venc_s_selection()
407 venc->params.crop.width = venc->params.src_width; in venc_s_selection()
408 venc->params.crop.height = venc->params.src_height; in venc_s_selection()
411 inst->crop = venc->params.crop; in venc_s_selection()
418 struct venc_t *venc = inst->priv; in venc_drain()
421 if (!inst->fh.m2m_ctx) in venc_drain()
424 if (inst->state != VPU_CODEC_STATE_DRAIN) in venc_drain()
430 if (!venc->input_ready) in venc_drain()
433 venc->input_ready = false; in venc_drain()
434 vpu_trace(inst->dev, "[%d]\n", inst->id); in venc_drain()
438 inst->state = VPU_CODEC_STATE_STOP; in venc_drain()
439 wake_up_all(&venc->wq); in venc_drain()
446 inst->state = VPU_CODEC_STATE_DRAIN; in venc_request_eos()
462 if (cmd->cmd == V4L2_ENC_CMD_STOP) { in venc_encoder_cmd()
463 if (inst->state == VPU_CODEC_STATE_DEINIT) in venc_encoder_cmd()
475 switch (sub->type) { in venc_subscribe_event()
481 return -EINVAL; in venc_subscribe_event()
516 static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl) in venc_op_s_ctrl() argument
518 struct vpu_inst *inst = ctrl_to_inst(ctrl); in venc_op_s_ctrl()
519 struct venc_t *venc = inst->priv; in venc_op_s_ctrl()
522 switch (ctrl->id) { in venc_op_s_ctrl()
524 venc->params.profile = ctrl->val; in venc_op_s_ctrl()
527 venc->params.level = ctrl->val; in venc_op_s_ctrl()
530 venc->params.rc_enable = ctrl->val; in venc_op_s_ctrl()
533 venc->params.rc_mode = ctrl->val; in venc_op_s_ctrl()
536 if (ctrl->val != venc->params.bitrate) in venc_op_s_ctrl()
537 venc->bitrate_change = true; in venc_op_s_ctrl()
538 venc->params.bitrate = ctrl->val; in venc_op_s_ctrl()
541 venc->params.bitrate_max = ctrl->val; in venc_op_s_ctrl()
544 venc->params.gop_length = ctrl->val; in venc_op_s_ctrl()
547 venc->params.bframes = ctrl->val; in venc_op_s_ctrl()
550 venc->params.i_frame_qp = ctrl->val; in venc_op_s_ctrl()
553 venc->params.p_frame_qp = ctrl->val; in venc_op_s_ctrl()
556 venc->params.b_frame_qp = ctrl->val; in venc_op_s_ctrl()
559 venc->request_key_frame = 1; in venc_op_s_ctrl()
562 venc->cpb_size = ctrl->val * 1024; in venc_op_s_ctrl()
565 venc->params.sar.enable = ctrl->val; in venc_op_s_ctrl()
568 venc->params.sar.idc = ctrl->val; in venc_op_s_ctrl()
571 venc->params.sar.width = ctrl->val; in venc_op_s_ctrl()
574 venc->params.sar.height = ctrl->val; in venc_op_s_ctrl()
579 ret = -EINVAL; in venc_op_s_ctrl()
593 struct v4l2_ctrl *ctrl; in venc_ctrl_init() local
596 ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20); in venc_ctrl_init()
600 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
608 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
614 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
617 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
624 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
631 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
637 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
640 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
643 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
645 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
647 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
649 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
651 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
653 if (ctrl) in venc_ctrl_init()
654 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; in venc_ctrl_init()
655 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
657 if (ctrl) in venc_ctrl_init()
658 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; in venc_ctrl_init()
660 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
663 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
665 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
670 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
673 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
676 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, in venc_ctrl_init()
682 v4l2_ctrl_new_std(&inst->ctrl_handler, NULL, in venc_ctrl_init()
685 if (inst->ctrl_handler.error) { in venc_ctrl_init()
686 ret = inst->ctrl_handler.error; in venc_ctrl_init()
687 v4l2_ctrl_handler_free(&inst->ctrl_handler); in venc_ctrl_init()
691 ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler); in venc_ctrl_init()
693 dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret); in venc_ctrl_init()
694 v4l2_ctrl_handler_free(&inst->ctrl_handler); in venc_ctrl_init()
703 struct venc_t *venc = inst->priv; in venc_check_ready()
706 if (vpu_helper_get_free_space(inst) < venc->cpb_size) in venc_check_ready()
708 return venc->input_ready; in venc_check_ready()
711 if (list_empty(&venc->frames)) in venc_check_ready()
729 venc->enable |= mask; in venc_set_enable()
731 venc->enable &= ~mask; in venc_set_enable()
736 return venc->enable & venc_get_enable_mask(type); in venc_get_enable()
741 struct venc_t *venc = inst->priv; in venc_input_done()
744 venc->input_ready = true; in venc_input_done()
746 if (inst->state == VPU_CODEC_STATE_DRAIN) in venc_input_done()
762 if (!frame || !frame->bytesused) in venc_precheck_encoded_frame()
763 return -EINVAL; in venc_precheck_encoded_frame()
765 venc = inst->priv; in venc_precheck_encoded_frame()
766 skipped = vpu_helper_find_startcode(&inst->stream_buffer, in venc_precheck_encoded_frame()
767 inst->cap_format.pixfmt, in venc_precheck_encoded_frame()
768 frame->info.wptr - inst->stream_buffer.phys, in venc_precheck_encoded_frame()
769 frame->bytesused); in venc_precheck_encoded_frame()
771 frame->bytesused -= skipped; in venc_precheck_encoded_frame()
772 frame->info.wptr = vpu_helper_step_walk(&inst->stream_buffer, in venc_precheck_encoded_frame()
773 frame->info.wptr, skipped); in venc_precheck_encoded_frame()
774 venc->skipped_bytes += skipped; in venc_precheck_encoded_frame()
775 venc->skipped_count++; in venc_precheck_encoded_frame()
785 struct venc_t *venc = inst->priv; in venc_get_one_encoded_frame()
789 return -EAGAIN; in venc_get_one_encoded_frame()
791 src_buf = vpu_find_buf_by_sequence(inst, inst->out_format.type, frame->info.frame_id); in venc_get_one_encoded_frame()
795 v4l2_m2m_src_buf_remove_by_buf(inst->fh.m2m_ctx, src_buf); in venc_get_one_encoded_frame()
798 vbuf->vb2_buf.timestamp = frame->info.timestamp; in venc_get_one_encoded_frame()
800 if (!venc_get_enable(inst->priv, vbuf->vb2_buf.type)) { in venc_get_one_encoded_frame()
804 if (frame->bytesused > vbuf->vb2_buf.planes[0].length) { in venc_get_one_encoded_frame()
806 return -ENOMEM; in venc_get_one_encoded_frame()
811 if (frame->bytesused) { in venc_get_one_encoded_frame()
812 u32 rptr = frame->info.wptr; in venc_get_one_encoded_frame()
813 void *dst = vb2_plane_vaddr(&vbuf->vb2_buf, 0); in venc_get_one_encoded_frame()
815 vpu_helper_copy_from_stream_buffer(&inst->stream_buffer, in venc_get_one_encoded_frame()
816 &rptr, frame->bytesused, dst); in venc_get_one_encoded_frame()
819 vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->bytesused); in venc_get_one_encoded_frame()
820 vbuf->sequence = frame->info.frame_id; in venc_get_one_encoded_frame()
821 vbuf->field = inst->cap_format.field; in venc_get_one_encoded_frame()
822 vbuf->flags |= frame->info.pic_type; in venc_get_one_encoded_frame()
824 vpu_set_buffer_average_qp(vbuf, frame->info.average_qp); in venc_get_one_encoded_frame()
825 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp); in venc_get_one_encoded_frame()
827 venc->ready_count++; in venc_get_one_encoded_frame()
829 if (vbuf->flags & V4L2_BUF_FLAG_KEYFRAME) in venc_get_one_encoded_frame()
830 dev_dbg(inst->dev, "[%d][%d]key frame\n", inst->id, frame->info.frame_id); in venc_get_one_encoded_frame()
841 if (!inst->fh.m2m_ctx) in venc_get_encoded_frames()
843 venc = inst->priv; in venc_get_encoded_frames()
844 list_for_each_entry_safe(frame, tmp, &venc->frames, list) { in venc_get_encoded_frames()
846 v4l2_m2m_dst_buf_remove(inst->fh.m2m_ctx))) in venc_get_encoded_frames()
848 list_del_init(&frame->list); in venc_get_encoded_frames()
863 return -EINVAL; in venc_frame_encoded()
864 venc = inst->priv; in venc_frame_encoded()
867 return -ENOMEM; in venc_frame_encoded()
869 memcpy(&frame->info, info, sizeof(frame->info)); in venc_frame_encoded()
870 frame->bytesused = info->frame_size; in venc_frame_encoded()
873 list_add_tail(&frame->list, &venc->frames); in venc_frame_encoded()
874 venc->encode_count++; in venc_frame_encoded()
883 struct venc_t *venc = inst->priv; in venc_set_last_buffer_dequeued()
885 if (venc->stopped && list_empty(&venc->frames)) in venc_set_last_buffer_dequeued()
891 struct venc_t *venc = inst->priv; in venc_stop_done()
894 venc->stopped = true; in venc_stop_done()
898 wake_up_all(&venc->wq); in venc_stop_done()
916 venc = inst->priv; in venc_cleanup()
918 inst->priv = NULL; in venc_cleanup()
924 struct venc_t *venc = inst->priv; in venc_start_session()
929 if ((venc->enable & VENC_ENABLE_MASK) != VENC_ENABLE_MASK) in venc_start_session()
933 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core); in venc_start_session()
935 inst->stream_buffer.length = max_t(u32, stream_buffer_size, venc->cpb_size * 3); in venc_start_session()
936 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer); in venc_start_session()
940 inst->use_stream_buffer = true; in venc_start_session()
941 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer); in venc_start_session()
944 ret = vpu_iface_set_encode_params(inst, &venc->params, 0); in venc_start_session()
948 venc->memory_resource_configured = false; in venc_start_session()
953 if (!venc->memory_resource_configured) { in venc_start_session()
954 vb2_queue_error(v4l2_m2m_get_src_vq(inst->fh.m2m_ctx)); in venc_start_session()
955 vb2_queue_error(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)); in venc_start_session()
956 ret = -ENOMEM; in venc_start_session()
960 inst->state = VPU_CODEC_STATE_CONFIGURED; in venc_start_session()
964 ret = vpu_iface_set_encode_params(inst, &venc->params, 1); in venc_start_session()
971 inst->state = VPU_CODEC_STATE_STARTED; in venc_start_session()
973 venc->bitrate_change = false; in venc_start_session()
974 venc->input_ready = true; in venc_start_session()
975 venc->frame_count = 0; in venc_start_session()
976 venc->encode_count = 0; in venc_start_session()
977 venc->ready_count = 0; in venc_start_session()
978 venc->stopped = false; in venc_start_session()
980 if (venc->frame_count == 0) in venc_start_session()
981 dev_err(inst->dev, "[%d] there is no input when starting\n", inst->id); in venc_start_session()
986 inst->state = VPU_CODEC_STATE_DEINIT; in venc_start_session()
988 vpu_free_dma(&inst->stream_buffer); in venc_start_session()
997 venc = inst->priv; in venc_cleanup_mem_resource()
998 venc->memory_resource_configured = false; in venc_cleanup_mem_resource()
1000 for (i = 0; i < ARRAY_SIZE(venc->enc); i++) in venc_cleanup_mem_resource()
1001 vpu_free_dma(&venc->enc[i]); in venc_cleanup_mem_resource()
1002 for (i = 0; i < ARRAY_SIZE(venc->ref); i++) in venc_cleanup_mem_resource()
1003 vpu_free_dma(&venc->ref[i]); in venc_cleanup_mem_resource()
1018 venc = inst->priv; in venc_request_mem_resource()
1019 if (enc_frame_num > ARRAY_SIZE(venc->enc)) { in venc_request_mem_resource()
1020 dev_err(inst->dev, "[%d] enc num(%d) is out of range\n", inst->id, enc_frame_num); in venc_request_mem_resource()
1023 if (ref_frame_num > ARRAY_SIZE(venc->ref)) { in venc_request_mem_resource()
1024 dev_err(inst->dev, "[%d] ref num(%d) is out of range\n", inst->id, ref_frame_num); in venc_request_mem_resource()
1027 if (act_frame_num > ARRAY_SIZE(venc->act)) { in venc_request_mem_resource()
1028 dev_err(inst->dev, "[%d] act num(%d) is out of range\n", inst->id, act_frame_num); in venc_request_mem_resource()
1033 venc->enc[i].length = enc_frame_size; in venc_request_mem_resource()
1034 ret = vpu_alloc_dma(inst->core, &venc->enc[i]); in venc_request_mem_resource()
1041 venc->ref[i].length = ref_frame_size; in venc_request_mem_resource()
1042 ret = vpu_alloc_dma(inst->core, &venc->ref[i]); in venc_request_mem_resource()
1048 if (act_frame_num != 1 || act_frame_size > inst->act.length) { in venc_request_mem_resource()
1052 venc->act[0].length = act_frame_size; in venc_request_mem_resource()
1053 venc->act[0].phys = inst->act.phys; in venc_request_mem_resource()
1054 venc->act[0].virt = inst->act.virt; in venc_request_mem_resource()
1057 vpu_iface_config_memory_resource(inst, MEM_RES_ENC, i, &venc->enc[i]); in venc_request_mem_resource()
1059 vpu_iface_config_memory_resource(inst, MEM_RES_REF, i, &venc->ref[i]); in venc_request_mem_resource()
1061 vpu_iface_config_memory_resource(inst, MEM_RES_ACT, i, &venc->act[i]); in venc_request_mem_resource()
1062 venc->memory_resource_configured = true; in venc_request_mem_resource()
1070 list_for_each_entry_safe(frame, tmp, &venc->frames, list) { in venc_cleanup_frames()
1071 list_del_init(&frame->list); in venc_cleanup_frames()
1078 struct venc_t *venc = inst->priv; in venc_stop_session()
1081 if (venc->enable & VENC_ENABLE_MASK) in venc_stop_session()
1084 if (inst->state == VPU_CODEC_STATE_DEINIT) in venc_stop_session()
1087 if (inst->state != VPU_CODEC_STATE_STOP) in venc_stop_session()
1091 if (!wait_event_timeout(venc->wq, venc->stopped, VPU_TIMEOUT)) { in venc_stop_session()
1092 set_bit(inst->id, &inst->core->hang_mask); in venc_stop_session()
1097 inst->state = VPU_CODEC_STATE_DEINIT; in venc_stop_session()
1098 venc_cleanup_frames(inst->priv); in venc_stop_session()
1099 vpu_free_dma(&inst->stream_buffer); in venc_stop_session()
1107 struct venc_t *venc = inst->priv; in venc_process_output()
1111 if (inst->state == VPU_CODEC_STATE_DEINIT) in venc_process_output()
1112 return -EINVAL; in venc_process_output()
1115 if (inst->state == VPU_CODEC_STATE_STARTED) in venc_process_output()
1116 inst->state = VPU_CODEC_STATE_ACTIVE; in venc_process_output()
1118 flags = vbuf->flags; in venc_process_output()
1119 if (venc->request_key_frame) { in venc_process_output()
1120 vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME; in venc_process_output()
1121 venc->request_key_frame = 0; in venc_process_output()
1123 if (venc->bitrate_change) { in venc_process_output()
1124 vpu_session_update_parameters(inst, &venc->params); in venc_process_output()
1125 venc->bitrate_change = false; in venc_process_output()
1127 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp); in venc_process_output()
1129 vbuf->flags = flags; in venc_process_output()
1130 venc->input_ready = false; in venc_process_output()
1131 venc->frame_count++; in venc_process_output()
1144 venc = inst->priv; in venc_process_capture()
1145 if (list_empty(&venc->frames)) in venc_process_capture()
1146 return -EINVAL; in venc_process_capture()
1148 frame = list_first_entry(&venc->frames, struct venc_frame_t, list); in venc_process_capture()
1150 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf); in venc_process_capture()
1155 list_del_init(&frame->list); in venc_process_capture()
1162 struct venc_t *venc = inst->priv; in venc_on_queue_empty()
1167 if (venc->stopped) in venc_on_queue_empty()
1173 struct venc_t *venc = inst->priv; in venc_get_debug_info()
1174 int num = -1; in venc_get_debug_info()
1178 num = scnprintf(str, size, "profile = %d\n", venc->params.profile); in venc_get_debug_info()
1181 num = scnprintf(str, size, "level = %d\n", venc->params.level); in venc_get_debug_info()
1185 venc->params.frame_rate.numerator, in venc_get_debug_info()
1186 venc->params.frame_rate.denominator); in venc_get_debug_info()
1189 num = scnprintf(str, size, "%d x %d -> %d x %d\n", in venc_get_debug_info()
1190 venc->params.src_width, in venc_get_debug_info()
1191 venc->params.src_height, in venc_get_debug_info()
1192 venc->params.out_width, in venc_get_debug_info()
1193 venc->params.out_height); in venc_get_debug_info()
1197 venc->params.crop.left, in venc_get_debug_info()
1198 venc->params.crop.top, in venc_get_debug_info()
1199 venc->params.crop.width, in venc_get_debug_info()
1200 venc->params.crop.height); in venc_get_debug_info()
1205 venc->enable, in venc_get_debug_info()
1206 venc->frame_count, venc->encode_count, in venc_get_debug_info()
1207 venc->ready_count, in venc_get_debug_info()
1208 venc->stopped); in venc_get_debug_info()
1211 num = scnprintf(str, size, "gop = %d\n", venc->params.gop_length); in venc_get_debug_info()
1214 num = scnprintf(str, size, "bframes = %d\n", venc->params.bframes); in venc_get_debug_info()
1218 venc->params.rc_enable ? "enable" : "disable", in venc_get_debug_info()
1219 venc->params.rc_mode, in venc_get_debug_info()
1220 venc->params.bitrate, in venc_get_debug_info()
1221 venc->params.bitrate_max, in venc_get_debug_info()
1222 venc->params.i_frame_qp); in venc_get_debug_info()
1226 venc->params.sar.enable, in venc_get_debug_info()
1227 venc->params.sar.idc, in venc_get_debug_info()
1228 venc->params.sar.width, in venc_get_debug_info()
1229 venc->params.sar.height); in venc_get_debug_info()
1235 venc->params.color.primaries, in venc_get_debug_info()
1236 venc->params.color.transfer, in venc_get_debug_info()
1237 venc->params.color.matrix, in venc_get_debug_info()
1238 venc->params.color.full_range); in venc_get_debug_info()
1242 venc->skipped_count, venc->skipped_bytes); in venc_get_debug_info()
1278 venc = inst->priv; in venc_init()
1279 venc->params.qp_min = 1; in venc_init()
1280 venc->params.qp_max = 51; in venc_init()
1281 venc->params.qp_min_i = 1; in venc_init()
1282 venc->params.qp_max_i = 51; in venc_init()
1283 venc->params.bitrate_min = BITRATE_MIN; in venc_init()
1291 venc_s_fmt(file, &inst->fh, &f); in venc_init()
1299 venc_s_fmt(file, &inst->fh, &f); in venc_init()
1305 venc_s_parm(file, &inst->fh, &parm); in venc_init()
1316 return -ENOMEM; in venc_open()
1321 return -ENOMEM; in venc_open()
1324 inst->ops = &venc_inst_ops; in venc_open()
1325 inst->formats = venc_formats; in venc_open()
1326 inst->type = VPU_CORE_TYPE_ENC; in venc_open()
1327 inst->priv = venc; in venc_open()
1328 INIT_LIST_HEAD(&venc->frames); in venc_open()
1329 init_waitqueue_head(&venc->wq); in venc_open()
1335 inst->min_buffer_out = VENC_MIN_BUFFER_OUT; in venc_open()
1336 inst->min_buffer_cap = VENC_MIN_BUFFER_CAP; in venc_open()