int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx, int command, unsigned int timeout_ms) { wait_queue_head_t *waitqueue; long timeout_jiff, ret; int status = 0; waitqueue = (wait_queue_head_t *)&ctx->queue; timeout_jiff = msecs_to_jiffies(timeout_ms); ret = wait_event_interruptible_timeout(*waitqueue, (ctx->int_cond && (ctx->int_type == command)), timeout_jiff); if (!ret) { status = -1; /* timeout */ mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout time=%ums out %d %d!", ctx->id, ctx->type, command, timeout_ms, ctx->int_cond, ctx->int_type); } else if (-ERESTARTSYS == ret) { mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout interrupted by a signal %d %d", ctx->id, ctx->type, command, ctx->int_cond, ctx->int_type); status = -1; } ctx->int_cond = 0; ctx->int_type = 0; return status; }
static int mtk_venc_encode_header(void *priv) { struct mtk_vcodec_ctx *ctx = priv; int ret; struct vb2_v4l2_buffer *src_buf, *dst_buf; struct mtk_vcodec_mem bs_buf; struct venc_done_result enc_result; dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); if (!dst_buf) { mtk_v4l2_debug(1, "No dst buffer"); return -EINVAL; } bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0); bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0); bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length; mtk_v4l2_debug(1, "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu", ctx->id, dst_buf->vb2_buf.index, bs_buf.va, (u64)bs_buf.dma_addr, bs_buf.size); ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_SEQUENCE_HEADER, NULL, &bs_buf, &enc_result); if (ret) { dst_buf->vb2_buf.planes[0].bytesused = 0; ctx->state = MTK_STATE_ABORT; v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); mtk_v4l2_err("venc_if_encode failed=%d", ret); return -EINVAL; } src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx); if (src_buf) { dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp; dst_buf->timecode = src_buf->timecode; } else { mtk_v4l2_err("No timestamp for the header buffer."); } ctx->state = MTK_STATE_HEADER; dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size; v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE); return 0; }
void mtk_vcodec_enc_release(struct mtk_vcodec_ctx *ctx) { int ret = venc_if_deinit(ctx); if (ret) mtk_v4l2_err("venc_if_deinit failed=%d", ret); ctx->state = MTK_STATE_FREE; }
void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data, unsigned int reg_idx) { struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data; if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) { mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx); return NULL; } return ctx->dev->reg_base[reg_idx]; }
static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx) { unsigned int dpbsize = 0; int ret; if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->last_decoded_picinfo)) { mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR", ctx->id); return -EINVAL; } if (ctx->last_decoded_picinfo.pic_w == 0 || ctx->last_decoded_picinfo.pic_h == 0 || ctx->last_decoded_picinfo.buf_w == 0 || ctx->last_decoded_picinfo.buf_h == 0) { mtk_v4l2_err("Cannot get correct pic info"); return -EINVAL; } if ((ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w) || (ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h)) return 0; mtk_v4l2_debug(1, "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)", ctx->id, ctx->last_decoded_picinfo.pic_w, ctx->last_decoded_picinfo.pic_h, ctx->picinfo.pic_w, ctx->picinfo.pic_h, ctx->last_decoded_picinfo.buf_w, ctx->last_decoded_picinfo.buf_h); ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize); if (dpbsize == 0) mtk_v4l2_err("Incorrect dpb size, ret=%d", ret); ctx->dpb_size = dpbsize; return ret; }
static int vidioc_venc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf) { struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); if (ctx->state == MTK_STATE_ABORT) { mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error", ctx->id); return -EIO; } return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf); }
static int vidioc_try_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd) { switch (cmd->cmd) { case V4L2_DEC_CMD_STOP: case V4L2_DEC_CMD_START: if (cmd->flags != 0) { mtk_v4l2_err("cmd->flags=%u", cmd->flags); return -EINVAL; } break; default: return -EINVAL; } return 0; }
static void mtk_venc_set_param(struct mtk_vcodec_ctx *ctx, struct venc_enc_param *param) { struct mtk_q_data *q_data_src = &ctx->q_data[MTK_Q_DATA_SRC]; struct mtk_enc_params *enc_params = &ctx->enc_params; switch (q_data_src->fmt->fourcc) { case V4L2_PIX_FMT_YUV420M: param->input_yuv_fmt = VENC_YUV_FORMAT_I420; break; case V4L2_PIX_FMT_YVU420M: param->input_yuv_fmt = VENC_YUV_FORMAT_YV12; break; case V4L2_PIX_FMT_NV12M: param->input_yuv_fmt = VENC_YUV_FORMAT_NV12; break; case V4L2_PIX_FMT_NV21M: param->input_yuv_fmt = VENC_YUV_FORMAT_NV21; break; default: mtk_v4l2_err("Unsupported fourcc =%d", q_data_src->fmt->fourcc); break; } param->h264_profile = enc_params->h264_profile; param->h264_level = enc_params->h264_level; /* Config visible resolution */ param->width = q_data_src->visible_width; param->height = q_data_src->visible_height; /* Config coded resolution */ param->buf_width = q_data_src->coded_width; param->buf_height = q_data_src->coded_height; param->frm_rate = enc_params->framerate_num / enc_params->framerate_denom; param->intra_period = enc_params->intra_period; param->gop_size = enc_params->gop_size; param->bitrate = enc_params->bitrate; mtk_v4l2_debug(0, "fmt 0x%x, P/L %d/%d, w/h %d/%d, buf %d/%d, fps/bps %d/%d, gop %d, i_period %d", param->input_yuv_fmt, param->h264_profile, param->h264_level, param->width, param->height, param->buf_width, param->buf_height, param->frm_rate, param->bitrate, param->gop_size, param->intra_period); }
static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv, struct v4l2_format *f) { struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp; struct mtk_video_fmt *fmt; fmt = mtk_vdec_find_format(f); if (!fmt) { f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc; fmt = mtk_vdec_find_format(f); } if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) { mtk_v4l2_err("sizeimage of output format must be given"); return -EINVAL; } return vidioc_try_fmt(f, fmt); }
static int vb2ops_venc_buf_prepare(struct vb2_buffer *vb) { struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); struct mtk_q_data *q_data; int i; q_data = mtk_venc_get_q_data(ctx, vb->vb2_queue->type); for (i = 0; i < q_data->fmt->num_planes; i++) { if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) { mtk_v4l2_err("data will not fit into plane %d (%lu < %d)", i, vb2_plane_size(vb, i), q_data->sizeimage[i]); return -EINVAL; } } return 0; }
int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx) { const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops; struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl; v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT); v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE, 1, 4000000, 1, 4000000); v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 2, 1, 0); v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1); v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51); v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, 0, 65535, 1, 0); v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, 65535, 1, 0); v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE, 0, 1, 1, 0); v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0); v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_HEADER_MODE, V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME, 0, V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE); v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 0, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH); v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, V4L2_MPEG_VIDEO_H264_LEVEL_4_2, 0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0); if (handler->error) { mtk_v4l2_err("Init control handler fail %d", handler->error); return handler->error; } v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); return 0; }
/* * This function tries to clean all display buffers, the buffers will return * in display order. * Note the buffers returned from codec driver may still be in driver's * reference list. */ static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx) { struct vdec_fb *disp_frame_buffer = NULL; struct mtk_video_dec_buf *dstbuf; mtk_v4l2_debug(3, "[%d]", ctx->id); if (vdec_if_get_param(ctx, GET_PARAM_DISP_FRAME_BUFFER, &disp_frame_buffer)) { mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER", ctx->id); return NULL; } if (disp_frame_buffer == NULL) { mtk_v4l2_debug(3, "No display frame buffer"); return NULL; } dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf, frame_buffer); mutex_lock(&ctx->lock); if (dstbuf->used) { vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0, ctx->picinfo.y_bs_sz); vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1, ctx->picinfo.c_bs_sz); dstbuf->ready_to_display = true; mtk_v4l2_debug(2, "[%d]status=%x queue id=%d to done_list %d", ctx->id, disp_frame_buffer->status, dstbuf->vb.vb2_buf.index, dstbuf->queued_in_vb2); v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE); ctx->decoded_frame_cnt++; } mutex_unlock(&ctx->lock); return &dstbuf->vb.vb2_buf; }
static int vb2ops_vdec_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) { struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq); struct mtk_q_data *q_data; unsigned int i; q_data = mtk_vdec_get_q_data(ctx, vq->type); if (q_data == NULL) { mtk_v4l2_err("vq->type=%d err\n", vq->type); return -EINVAL; } if (*nplanes) { for (i = 0; i < *nplanes; i++) { if (sizes[i] < q_data->sizeimage[i]) return -EINVAL; } } else { if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) *nplanes = 2; else *nplanes = 1; for (i = 0; i < *nplanes; i++) sizes[i] = q_data->sizeimage[i]; } mtk_v4l2_debug(1, "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ", ctx->id, vq->type, *nplanes, *nbuffers, sizes[0], sizes[1]); return 0; }
void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data, struct mtk_vcodec_mem *mem) { unsigned long size = mem->size; struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data; struct device *dev = &ctx->dev->plat_dev->dev; if (!mem->va) { mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev), size); return; } mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va); mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id, (unsigned long)mem->dma_addr); mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size); dma_free_coherent(dev, size, mem->va, mem->dma_addr); mem->va = NULL; mem->dma_addr = 0; mem->size = 0; }
static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx) { static const struct v4l2_event ev_src_ch = { .type = V4L2_EVENT_SOURCE_CHANGE, .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION, }; mtk_v4l2_debug(1, "[%d]", ctx->id); v4l2_event_queue_fh(&ctx->fh, &ev_src_ch); } static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx) { bool res_chg; int ret = 0; ret = vdec_if_decode(ctx, NULL, NULL, &res_chg); if (ret) mtk_v4l2_err("DecodeFinal failed, ret=%d", ret); clean_display_buffer(ctx); clean_free_buffer(ctx); }
static void vb2ops_venc_stop_streaming(struct vb2_queue *q) { struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q); struct vb2_v4l2_buffer *src_buf, *dst_buf; int ret; mtk_v4l2_debug(2, "[%d]-> type=%d", ctx->id, q->type); if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) { dst_buf->vb2_buf.planes[0].bytesused = 0; v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); } } else { while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR); } if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q)) || (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q))) { mtk_v4l2_debug(1, "[%d]-> q type %d out=%d cap=%d", ctx->id, q->type, vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q), vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q)); return; } /* Release the encoder if both streams are stopped. */ ret = venc_if_deinit(ctx); if (ret) mtk_v4l2_err("venc_if_deinit failed=%d", ret); ctx->state = MTK_STATE_FREE; }
int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data, struct mtk_vcodec_mem *mem) { unsigned long size = mem->size; struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data; struct device *dev = &ctx->dev->plat_dev->dev; mem->va = dma_alloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL); if (!mem->va) { mtk_v4l2_err("%s dma_alloc size=%ld failed!", dev_name(dev), size); return -ENOMEM; } memset(mem->va, 0, size); mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va); mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id, (unsigned long)mem->dma_addr); mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size); return 0; }
static int mtk_venc_param_change(struct mtk_vcodec_ctx *ctx) { struct venc_enc_param enc_prm; struct vb2_v4l2_buffer *vb2_v4l2 = v4l2_m2m_next_src_buf(ctx->m2m_ctx); struct mtk_video_enc_buf *mtk_buf = container_of(vb2_v4l2, struct mtk_video_enc_buf, vb); int ret = 0; memset(&enc_prm, 0, sizeof(enc_prm)); if (mtk_buf->param_change == MTK_ENCODE_PARAM_NONE) return 0; if (mtk_buf->param_change & MTK_ENCODE_PARAM_BITRATE) { enc_prm.bitrate = mtk_buf->enc_params.bitrate; mtk_v4l2_debug(1, "[%d] id=%d, change param br=%d", ctx->id, mtk_buf->vb.vb2_buf.index, enc_prm.bitrate); ret |= venc_if_set_param(ctx, VENC_SET_PARAM_ADJUST_BITRATE, &enc_prm); } if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FRAMERATE) { enc_prm.frm_rate = mtk_buf->enc_params.framerate_num / mtk_buf->enc_params.framerate_denom; mtk_v4l2_debug(1, "[%d] id=%d, change param fr=%d", ctx->id, mtk_buf->vb.vb2_buf.index, enc_prm.frm_rate); ret |= venc_if_set_param(ctx, VENC_SET_PARAM_ADJUST_FRAMERATE, &enc_prm); } if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_GOP_SIZE) { enc_prm.gop_size = mtk_buf->enc_params.gop_size; mtk_v4l2_debug(1, "change param intra period=%d", enc_prm.gop_size); ret |= venc_if_set_param(ctx, VENC_SET_PARAM_GOP_SIZE, &enc_prm); } if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) { mtk_v4l2_debug(1, "[%d] id=%d, change param force I=%d", ctx->id, mtk_buf->vb.vb2_buf.index, mtk_buf->enc_params.force_intra); if (mtk_buf->enc_params.force_intra) ret |= venc_if_set_param(ctx, VENC_SET_PARAM_FORCE_INTRA, NULL); } mtk_buf->param_change = MTK_ENCODE_PARAM_NONE; if (ret) { ctx->state = MTK_STATE_ABORT; mtk_v4l2_err("venc_if_set_param %d failed=%d", mtk_buf->param_change, ret); return -1; } return 0; }
static int vb2ops_venc_start_streaming(struct vb2_queue *q, unsigned int count) { struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q); struct venc_enc_param param; int ret; int i; /* Once state turn into MTK_STATE_ABORT, we need stop_streaming * to clear it */ if ((ctx->state == MTK_STATE_ABORT) || (ctx->state == MTK_STATE_FREE)) { ret = -EIO; goto err_set_param; } /* Do the initialization when both start_streaming have been called */ if (V4L2_TYPE_IS_OUTPUT(q->type)) { if (!vb2_start_streaming_called(&ctx->m2m_ctx->cap_q_ctx.q)) return 0; } else { if (!vb2_start_streaming_called(&ctx->m2m_ctx->out_q_ctx.q)) return 0; } mtk_venc_set_param(ctx, ¶m); ret = venc_if_set_param(ctx, VENC_SET_PARAM_ENC, ¶m); if (ret) { mtk_v4l2_err("venc_if_set_param failed=%d", ret); ctx->state = MTK_STATE_ABORT; goto err_set_param; } ctx->param_change = MTK_ENCODE_PARAM_NONE; if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) && (ctx->enc_params.seq_hdr_mode != V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)) { ret = venc_if_set_param(ctx, VENC_SET_PARAM_PREPEND_HEADER, NULL); if (ret) { mtk_v4l2_err("venc_if_set_param failed=%d", ret); ctx->state = MTK_STATE_ABORT; goto err_set_param; } ctx->state = MTK_STATE_HEADER; } return 0; err_set_param: for (i = 0; i < q->num_buffers; ++i) { if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) { mtk_v4l2_debug(0, "[%d] id=%d, type=%d, %d -> VB2_BUF_STATE_QUEUED", ctx->id, i, q->type, (int)q->bufs[i]->state); v4l2_m2m_buf_done(to_vb2_v4l2_buffer(q->bufs[i]), VB2_BUF_STATE_QUEUED); } } return ret; }
/* * This function tries to clean all capture buffers that are not used as * reference buffers by codec driver any more * In this case, we need re-queue buffer to vb2 buffer if user space * already returns this buffer to v4l2 or this buffer is just the output of * previous sps/pps/resolution change decode, or do nothing if user * space still owns this buffer */ static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx) { struct mtk_video_dec_buf *dstbuf; struct vdec_fb *free_frame_buffer = NULL; if (vdec_if_get_param(ctx, GET_PARAM_FREE_FRAME_BUFFER, &free_frame_buffer)) { mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->id); return NULL; } if (free_frame_buffer == NULL) { mtk_v4l2_debug(3, " No free frame buffer"); return NULL; } mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p", ctx->id, free_frame_buffer); dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf, frame_buffer); mutex_lock(&ctx->lock); if (dstbuf->used) { if ((dstbuf->queued_in_vb2) && (dstbuf->queued_in_v4l2) && (free_frame_buffer->status == FB_ST_FREE)) { /* * After decode sps/pps or non-display buffer, we don't * need to return capture buffer to user space, but * just re-queue this capture buffer to vb2 queue. * This reduce overheads that dq/q unused capture * buffer. In this case, queued_in_vb2 = true. */ mtk_v4l2_debug(2, "[%d]status=%x queue id=%d to rdy_queue %d", ctx->id, free_frame_buffer->status, dstbuf->vb.vb2_buf.index, dstbuf->queued_in_vb2); v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb); } else if ((dstbuf->queued_in_vb2 == false) && (dstbuf->queued_in_v4l2 == true)) { /* * If buffer in v4l2 driver but not in vb2 queue yet, * and we get this buffer from free_list, it means * that codec driver do not use this buffer as * reference buffer anymore. We should q buffer to vb2 * queue, so later work thread could get this buffer * for decode. In this case, queued_in_vb2 = false * means this buffer is not from previous decode * output. */ mtk_v4l2_debug(2, "[%d]status=%x queue id=%d to rdy_queue", ctx->id, free_frame_buffer->status, dstbuf->vb.vb2_buf.index); v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb); dstbuf->queued_in_vb2 = true; } else { /* * Codec driver do not need to reference this capture * buffer and this buffer is not in v4l2 driver. * Then we don't need to do any thing, just add log when * we need to debug buffer flow. * When this buffer q from user space, it could * directly q to vb2 buffer */ mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d", ctx->id, free_frame_buffer->status, dstbuf->vb.vb2_buf.index, dstbuf->queued_in_vb2, dstbuf->queued_in_v4l2); } dstbuf->used = false; } mutex_unlock(&ctx->lock); return &dstbuf->vb.vb2_buf; }
static int vidioc_venc_s_fmt_out(struct file *file, void *priv, struct v4l2_format *f) { struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); struct vb2_queue *vq; struct mtk_q_data *q_data; int ret, i; struct mtk_video_fmt *fmt; struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp; vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); if (!vq) { mtk_v4l2_err("fail to get vq"); return -EINVAL; } if (vb2_is_busy(vq)) { mtk_v4l2_err("queue busy"); return -EBUSY; } q_data = mtk_venc_get_q_data(ctx, f->type); if (!q_data) { mtk_v4l2_err("fail to get q data"); return -EINVAL; } fmt = mtk_venc_find_format(f); if (!fmt) { f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc; fmt = mtk_venc_find_format(f); } pix_fmt_mp->height = clamp(pix_fmt_mp->height, MTK_VENC_MIN_H, MTK_VENC_MAX_H); pix_fmt_mp->width = clamp(pix_fmt_mp->width, MTK_VENC_MIN_W, MTK_VENC_MAX_W); q_data->visible_width = f->fmt.pix_mp.width; q_data->visible_height = f->fmt.pix_mp.height; q_data->fmt = fmt; ret = vidioc_try_fmt(f, q_data->fmt); if (ret) return ret; q_data->coded_width = f->fmt.pix_mp.width; q_data->coded_height = f->fmt.pix_mp.height; q_data->field = f->fmt.pix_mp.field; ctx->colorspace = f->fmt.pix_mp.colorspace; ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc; ctx->quantization = f->fmt.pix_mp.quantization; ctx->xfer_func = f->fmt.pix_mp.xfer_func; for (i = 0; i < f->fmt.pix_mp.num_planes; i++) { struct v4l2_plane_pix_format *plane_fmt; plane_fmt = &f->fmt.pix_mp.plane_fmt[i]; q_data->bytesperline[i] = plane_fmt->bytesperline; q_data->sizeimage[i] = plane_fmt->sizeimage; } return 0; }
static int vidioc_venc_s_fmt_cap(struct file *file, void *priv, struct v4l2_format *f) { struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); struct vb2_queue *vq; struct mtk_q_data *q_data; int i, ret; struct mtk_video_fmt *fmt; vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); if (!vq) { mtk_v4l2_err("fail to get vq"); return -EINVAL; } if (vb2_is_busy(vq)) { mtk_v4l2_err("queue busy"); return -EBUSY; } q_data = mtk_venc_get_q_data(ctx, f->type); if (!q_data) { mtk_v4l2_err("fail to get q data"); return -EINVAL; } fmt = mtk_venc_find_format(f); if (!fmt) { f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc; fmt = mtk_venc_find_format(f); } q_data->fmt = fmt; ret = vidioc_try_fmt(f, q_data->fmt); if (ret) return ret; q_data->coded_width = f->fmt.pix_mp.width; q_data->coded_height = f->fmt.pix_mp.height; q_data->field = f->fmt.pix_mp.field; for (i = 0; i < f->fmt.pix_mp.num_planes; i++) { struct v4l2_plane_pix_format *plane_fmt; plane_fmt = &f->fmt.pix_mp.plane_fmt[i]; q_data->bytesperline[i] = plane_fmt->bytesperline; q_data->sizeimage[i] = plane_fmt->sizeimage; } if (ctx->state == MTK_STATE_FREE) { ret = venc_if_init(ctx, q_data->fmt->fourcc); if (ret) { mtk_v4l2_err("venc_if_init failed=%d, codec type=%x", ret, q_data->fmt->fourcc); return -EBUSY; } ctx->state = MTK_STATE_INIT; } return 0; }
static void mtk_vdec_worker(struct work_struct *work) { struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx, decode_work); struct mtk_vcodec_dev *dev = ctx->dev; struct vb2_buffer *src_buf, *dst_buf; struct mtk_vcodec_mem buf; struct vdec_fb *pfb; bool res_chg = false; int ret; struct mtk_video_dec_buf *dst_buf_info, *src_buf_info; struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2; src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx); if (src_buf == NULL) { v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id); return; } dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); if (dst_buf == NULL) { v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id); return; } src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf); src_buf_info = container_of(src_vb2_v4l2, struct mtk_video_dec_buf, vb); dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf); dst_buf_info = container_of(dst_vb2_v4l2, struct mtk_video_dec_buf, vb); pfb = &dst_buf_info->frame_buffer; pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0); pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0); pfb->base_y.size = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz; pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1); pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1); pfb->base_c.size = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz; pfb->status = 0; mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id); mtk_v4l2_debug(3, "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx", dst_buf->index, pfb, pfb->base_y.va, &pfb->base_y.dma_addr, &pfb->base_c.dma_addr, pfb->base_y.size); if (src_buf_info->lastframe) { mtk_v4l2_debug(1, "Got empty flush input buffer."); src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); /* update dst buf status */ dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); mutex_lock(&ctx->lock); dst_buf_info->used = false; mutex_unlock(&ctx->lock); vdec_if_decode(ctx, NULL, NULL, &res_chg); clean_display_buffer(ctx); vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0); vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0); dst_vb2_v4l2->flags |= V4L2_BUF_FLAG_LAST; v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE); clean_free_buffer(ctx); v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); return; } buf.va = vb2_plane_vaddr(src_buf, 0); buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0); buf.size = (size_t)src_buf->planes[0].bytesused; if (!buf.va) { v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); mtk_v4l2_err("[%d] id=%d src_addr is NULL!!", ctx->id, src_buf->index); return; } mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p", ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf); dst_buf_info->vb.vb2_buf.timestamp = src_buf_info->vb.vb2_buf.timestamp; dst_buf_info->vb.timecode = src_buf_info->vb.timecode; mutex_lock(&ctx->lock); dst_buf_info->used = true; mutex_unlock(&ctx->lock); src_buf_info->used = true; ret = vdec_if_decode(ctx, &buf, pfb, &res_chg); if (ret) { mtk_v4l2_err( " <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>", ctx->id, src_buf->index, buf.size, src_buf_info->vb.vb2_buf.timestamp, dst_buf->index, ret, res_chg); src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_ERROR); } else if (res_chg == false) { /* * we only return src buffer with VB2_BUF_STATE_DONE * when decode success without resolution change */ src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE); } dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); clean_display_buffer(ctx); clean_free_buffer(ctx); if (!ret && res_chg) { mtk_vdec_pic_info_update(ctx); /* * On encountering a resolution change in the stream. * The driver must first process and decode all * remaining buffers from before the resolution change * point, so call flush decode here */ mtk_vdec_flush_decoder(ctx); /* * After all buffers containing decoded frames from * before the resolution change point ready to be * dequeued on the CAPTURE queue, the driver sends a * V4L2_EVENT_SOURCE_CHANGE event for source change * type V4L2_EVENT_SRC_CH_RESOLUTION */ mtk_vdec_queue_res_chg_event(ctx); } v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); }
static int vidioc_vdec_s_fmt(struct file *file, void *priv, struct v4l2_format *f) { struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); struct v4l2_pix_format_mplane *pix_mp; struct mtk_q_data *q_data; int ret = 0; struct mtk_video_fmt *fmt; mtk_v4l2_debug(3, "[%d]", ctx->id); q_data = mtk_vdec_get_q_data(ctx, f->type); if (!q_data) return -EINVAL; pix_mp = &f->fmt.pix_mp; if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) && vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) { mtk_v4l2_err("out_q_ctx buffers already requested"); ret = -EBUSY; } if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) && vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) { mtk_v4l2_err("cap_q_ctx buffers already requested"); ret = -EBUSY; } fmt = mtk_vdec_find_format(f); if (fmt == NULL) { if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc; fmt = mtk_vdec_find_format(f); } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc; fmt = mtk_vdec_find_format(f); } } q_data->fmt = fmt; vidioc_try_fmt(f, q_data->fmt); if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage; q_data->coded_width = pix_mp->width; q_data->coded_height = pix_mp->height; ctx->colorspace = f->fmt.pix_mp.colorspace; ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc; ctx->quantization = f->fmt.pix_mp.quantization; ctx->xfer_func = f->fmt.pix_mp.xfer_func; if (ctx->state == MTK_STATE_FREE) { ret = vdec_if_init(ctx, q_data->fmt->fourcc); if (ret) { mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d", ctx->id, ret); return -EINVAL; } ctx->state = MTK_STATE_INIT; } } return 0; }
static int vidioc_vdec_g_fmt(struct file *file, void *priv, struct v4l2_format *f) { struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; struct vb2_queue *vq; struct mtk_q_data *q_data; vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); if (!vq) { mtk_v4l2_err("no vb2 queue for type=%d", f->type); return -EINVAL; } q_data = mtk_vdec_get_q_data(ctx, f->type); pix_mp->field = V4L2_FIELD_NONE; pix_mp->colorspace = ctx->colorspace; pix_mp->ycbcr_enc = ctx->ycbcr_enc; pix_mp->quantization = ctx->quantization; pix_mp->xfer_func = ctx->xfer_func; if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) && (ctx->state >= MTK_STATE_HEADER)) { /* Until STREAMOFF is called on the CAPTURE queue * (acknowledging the event), the driver operates as if * the resolution hasn't changed yet. * So we just return picinfo yet, and update picinfo in * stop_streaming hook function */ q_data->sizeimage[0] = ctx->picinfo.y_bs_sz + ctx->picinfo.y_len_sz; q_data->sizeimage[1] = ctx->picinfo.c_bs_sz + ctx->picinfo.c_len_sz; q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w; q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w; q_data->coded_width = ctx->picinfo.buf_w; q_data->coded_height = ctx->picinfo.buf_h; /* * Width and height are set to the dimensions * of the movie, the buffer is bigger and * further processing stages should crop to this * rectangle. */ pix_mp->width = q_data->coded_width; pix_mp->height = q_data->coded_height; /* * Set pixelformat to the format in which mt vcodec * outputs the decoded frame */ pix_mp->num_planes = q_data->fmt->num_planes; pix_mp->pixelformat = q_data->fmt->fourcc; pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0]; pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0]; pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1]; pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1]; } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { /* * This is run on OUTPUT * The buffer contains compressed image * so width and height have no meaning. * Assign value here to pass v4l2-compliance test */ pix_mp->width = q_data->visible_width; pix_mp->height = q_data->visible_height; pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0]; pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0]; pix_mp->pixelformat = q_data->fmt->fourcc; pix_mp->num_planes = q_data->fmt->num_planes; } else { pix_mp->width = q_data->coded_width; pix_mp->height = q_data->coded_height; pix_mp->num_planes = q_data->fmt->num_planes; pix_mp->pixelformat = q_data->fmt->fourcc; pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0]; pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0]; pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1]; pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1]; mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!", ctx->id, f->type, ctx->state); } return 0; }
/* * v4l2_m2m_streamoff() holds dev_mutex and waits mtk_venc_worker() * to call v4l2_m2m_job_finish(). * If mtk_venc_worker() tries to acquire dev_mutex, it will deadlock. * So this function must not try to acquire dev->dev_mutex. * This means v4l2 ioctls and mtk_venc_worker() can run at the same time. * mtk_venc_worker() should be carefully implemented to avoid bugs. */ static void mtk_venc_worker(struct work_struct *work) { struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx, encode_work); struct vb2_v4l2_buffer *src_buf, *dst_buf; struct venc_frm_buf frm_buf; struct mtk_vcodec_mem bs_buf; struct venc_done_result enc_result; int ret, i; /* check dst_buf, dst_buf may be removed in device_run * to stored encdoe header so we need check dst_buf and * call job_finish here to prevent recursion */ dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); if (!dst_buf) { v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx); return; } src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); memset(&frm_buf, 0, sizeof(frm_buf)); for (i = 0; i < src_buf->vb2_buf.num_planes ; i++) { frm_buf.fb_addr[i].dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, i); frm_buf.fb_addr[i].size = (size_t)src_buf->vb2_buf.planes[i].length; } bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0); bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0); bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length; mtk_v4l2_debug(2, "Framebuf PA=%llx Size=0x%zx;PA=0x%llx Size=0x%zx;PA=0x%llx Size=%zu", (u64)frm_buf.fb_addr[0].dma_addr, frm_buf.fb_addr[0].size, (u64)frm_buf.fb_addr[1].dma_addr, frm_buf.fb_addr[1].size, (u64)frm_buf.fb_addr[2].dma_addr, frm_buf.fb_addr[2].size); ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME, &frm_buf, &bs_buf, &enc_result); dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp; dst_buf->timecode = src_buf->timecode; if (enc_result.is_key_frm) dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME; if (ret) { v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR); dst_buf->vb2_buf.planes[0].bytesused = 0; v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); mtk_v4l2_err("venc_if_encode failed=%d", ret); } else { v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size; v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE); mtk_v4l2_debug(2, "venc_if_encode bs size=%d", enc_result.bs_size); } v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx); mtk_v4l2_debug(1, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>", src_buf->vb2_buf.index, dst_buf->vb2_buf.index, ret, enc_result.bs_size); }