Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,494 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: PC Chen <pc.chen@mediatek.com>
*/
#include <linux/module.h>
#include <linux/slab.h>
#include "../vdec_drv_if.h"
#include "../mtk_vcodec_dec.h"
#include "../../common/mtk_vcodec_intr.h"
#include "../vdec_vpu_if.h"
#include "../vdec_drv_base.h"
#define NAL_NON_IDR_SLICE 0x01
#define NAL_IDR_SLICE 0x05
#define NAL_H264_PPS 0x08
#define NAL_TYPE(value) ((value) & 0x1F)
#define BUF_PREDICTION_SZ (32 * 1024)
#define MB_UNIT_LEN 16
/* motion vector size (bytes) for every macro block */
#define HW_MB_STORE_SZ 64
#define H264_MAX_FB_NUM 17
#define HDR_PARSING_BUF_SZ 1024
#define DEC_ERR_RET(ret) ((ret) >> 16)
#define H264_ERR_NOT_VALID 3
/**
* struct h264_fb - h264 decode frame buffer information
* @vdec_fb_va : virtual address of struct vdec_fb
* @y_fb_dma : dma address of Y frame buffer (luma)
* @c_fb_dma : dma address of C frame buffer (chroma)
* @poc : picture order count of frame buffer
* @reserved : for 8 bytes alignment
*/
struct h264_fb {
uint64_t vdec_fb_va;
uint64_t y_fb_dma;
uint64_t c_fb_dma;
int32_t poc;
uint32_t reserved;
};
/**
* struct h264_ring_fb_list - ring frame buffer list
* @fb_list : frame buffer array
* @read_idx : read index
* @write_idx : write index
* @count : buffer count in list
* @reserved : for 8 bytes alignment
*/
struct h264_ring_fb_list {
struct h264_fb fb_list[H264_MAX_FB_NUM];
unsigned int read_idx;
unsigned int write_idx;
unsigned int count;
unsigned int reserved;
};
/**
* struct vdec_h264_dec_info - decode information
* @dpb_sz : decoding picture buffer size
* @resolution_changed : resolution change happen
* @realloc_mv_buf : flag to notify driver to re-allocate mv buffer
* @reserved : for 8 bytes alignment
* @bs_dma : Input bit-stream buffer dma address
* @y_fb_dma : Y frame buffer dma address
* @c_fb_dma : C frame buffer dma address
* @vdec_fb_va : VDEC frame buffer struct virtual address
*/
struct vdec_h264_dec_info {
uint32_t dpb_sz;
uint32_t resolution_changed;
uint32_t realloc_mv_buf;
uint32_t reserved;
uint64_t bs_dma;
uint64_t y_fb_dma;
uint64_t c_fb_dma;
uint64_t vdec_fb_va;
};
/**
* struct vdec_h264_vsi - shared memory for decode information exchange
* between VPU and Host.
* The memory is allocated by VPU then mapping to Host
* in vpu_dec_init() and freed in vpu_dec_deinit()
* by VPU.
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @hdr_buf : Header parsing buffer (AP-W, VPU-R)
* @pred_buf_dma : HW working prediction buffer dma address (AP-W, VPU-R)
* @mv_buf_dma : HW working motion vector buffer dma address (AP-W, VPU-R)
* @list_free : free frame buffer ring list (AP-W/R, VPU-W)
* @list_disp : display frame buffer ring list (AP-R, VPU-W)
* @dec : decode information (AP-R, VPU-W)
* @pic : picture information (AP-R, VPU-W)
* @crop : crop information (AP-R, VPU-W)
*/
struct vdec_h264_vsi {
unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
uint64_t pred_buf_dma;
uint64_t mv_buf_dma[H264_MAX_FB_NUM];
struct h264_ring_fb_list list_free;
struct h264_ring_fb_list list_disp;
struct vdec_h264_dec_info dec;
struct vdec_pic_info pic;
struct v4l2_rect crop;
};
/**
* struct vdec_h264_inst - h264 decoder instance
* @num_nalu : how many nalus be decoded
* @ctx : point to mtk_vcodec_dec_ctx
* @pred_buf : HW working prediction buffer
* @mv_buf : HW working motion vector buffer
* @vpu : VPU instance
* @vsi : VPU shared information
*/
struct vdec_h264_inst {
unsigned int num_nalu;
struct mtk_vcodec_dec_ctx *ctx;
struct mtk_vcodec_mem pred_buf;
struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
struct vdec_vpu_inst vpu;
struct vdec_h264_vsi *vsi;
};
static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
{
return HW_MB_STORE_SZ * (width/MB_UNIT_LEN) * (height/MB_UNIT_LEN);
}
static int allocate_prediction_buf(struct vdec_h264_inst *inst)
{
int err = 0;
inst->pred_buf.size = BUF_PREDICTION_SZ;
err = mtk_vcodec_mem_alloc(inst->ctx, &inst->pred_buf);
if (err) {
mtk_vdec_err(inst->ctx, "failed to allocate ppl buf");
return err;
}
inst->vsi->pred_buf_dma = inst->pred_buf.dma_addr;
return 0;
}
static void free_prediction_buf(struct vdec_h264_inst *inst)
{
struct mtk_vcodec_mem *mem = NULL;
inst->vsi->pred_buf_dma = 0;
mem = &inst->pred_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
}
static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
{
int i;
int err;
struct mtk_vcodec_mem *mem = NULL;
unsigned int buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
for (i = 0; i < H264_MAX_FB_NUM; i++) {
mem = &inst->mv_buf[i];
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
mem->size = buf_sz;
err = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (err) {
mtk_vdec_err(inst->ctx, "failed to allocate mv buf");
return err;
}
inst->vsi->mv_buf_dma[i] = mem->dma_addr;
}
return 0;
}
static void free_mv_buf(struct vdec_h264_inst *inst)
{
int i;
struct mtk_vcodec_mem *mem = NULL;
for (i = 0; i < H264_MAX_FB_NUM; i++) {
inst->vsi->mv_buf_dma[i] = 0;
mem = &inst->mv_buf[i];
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
}
}
static int check_list_validity(struct vdec_h264_inst *inst, bool disp_list)
{
struct h264_ring_fb_list *list;
list = disp_list ? &inst->vsi->list_disp : &inst->vsi->list_free;
if (list->count > H264_MAX_FB_NUM ||
list->read_idx >= H264_MAX_FB_NUM ||
list->write_idx >= H264_MAX_FB_NUM) {
mtk_vdec_err(inst->ctx, "%s list err: cnt=%d r_idx=%d w_idx=%d",
disp_list ? "disp" : "free", list->count,
list->read_idx, list->write_idx);
return -EINVAL;
}
return 0;
}
static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
{
struct h264_ring_fb_list *list;
if (fb) {
if (check_list_validity(inst, false))
return;
list = &inst->vsi->list_free;
if (list->count == H264_MAX_FB_NUM) {
mtk_vdec_err(inst->ctx, "[FB] put fb free_list full");
return;
}
mtk_vdec_debug(inst->ctx, "[FB] put fb into free_list @(%p, %llx)",
fb->base_y.va, (u64)fb->base_y.dma_addr);
list->fb_list[list->write_idx].vdec_fb_va = (u64)(uintptr_t)fb;
list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
0 : list->write_idx + 1;
list->count++;
}
}
static void get_pic_info(struct vdec_h264_inst *inst,
struct vdec_pic_info *pic)
{
*pic = inst->vsi->pic;
mtk_vdec_debug(inst->ctx, "pic(%d, %d), buf(%d, %d)",
pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
mtk_vdec_debug(inst->ctx, "fb size: Y(%d), C(%d)", pic->fb_sz[0], pic->fb_sz[1]);
}
static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_rect *cr)
{
cr->left = inst->vsi->crop.left;
cr->top = inst->vsi->crop.top;
cr->width = inst->vsi->crop.width;
cr->height = inst->vsi->crop.height;
mtk_vdec_debug(inst->ctx, "l=%d, t=%d, w=%d, h=%d", cr->left, cr->top,
cr->width, cr->height);
}
static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
{
*dpb_sz = inst->vsi->dec.dpb_sz;
mtk_vdec_debug(inst->ctx, "sz=%d", *dpb_sz);
}
static int vdec_h264_init(struct mtk_vcodec_dec_ctx *ctx)
{
struct vdec_h264_inst *inst = NULL;
int err;
inst = kzalloc_obj(*inst);
if (!inst)
return -ENOMEM;
inst->ctx = ctx;
inst->vpu.id = IPI_VDEC_H264;
inst->vpu.ctx = ctx;
err = vpu_dec_init(&inst->vpu);
if (err) {
mtk_vdec_err(ctx, "vdec_h264 init err=%d", err);
goto error_free_inst;
}
inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
err = allocate_prediction_buf(inst);
if (err)
goto error_deinit;
mtk_vdec_debug(ctx, "H264 Instance >> %p", inst);
ctx->drv_handle = inst;
return 0;
error_deinit:
vpu_dec_deinit(&inst->vpu);
error_free_inst:
kfree(inst);
return err;
}
static void vdec_h264_deinit(void *h_vdec)
{
struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
vpu_dec_deinit(&inst->vpu);
free_prediction_buf(inst);
free_mv_buf(inst);
kfree(inst);
}
static int find_start_code(unsigned char *data, unsigned int data_sz)
{
if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
return 3;
if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
data[3] == 1)
return 4;
return -1;
}
static int vdec_h264_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
struct vdec_fb *fb, bool *res_chg)
{
struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
struct vdec_vpu_inst *vpu = &inst->vpu;
int nal_start_idx = 0;
int err = 0;
unsigned int nal_start;
unsigned int nal_type;
unsigned char *buf;
unsigned int buf_sz;
unsigned int data[2];
uint64_t vdec_fb_va = (u64)(uintptr_t)fb;
uint64_t y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
uint64_t c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
mtk_vdec_debug(inst->ctx, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
++inst->num_nalu, y_fb_dma, c_fb_dma, fb);
/* bs NULL means flush decoder */
if (bs == NULL)
return vpu_dec_reset(vpu);
buf = (unsigned char *)bs->va;
buf_sz = bs->size;
nal_start_idx = find_start_code(buf, buf_sz);
if (nal_start_idx < 0) {
mtk_vdec_err(inst->ctx, "invalid nal start code");
err = -EIO;
goto err_free_fb_out;
}
nal_start = buf[nal_start_idx];
nal_type = NAL_TYPE(buf[nal_start_idx]);
mtk_vdec_debug(inst->ctx, "\n + NALU[%d] type %d +\n", inst->num_nalu,
nal_type);
if (nal_type == NAL_H264_PPS) {
buf_sz -= nal_start_idx;
if (buf_sz > HDR_PARSING_BUF_SZ) {
err = -EILSEQ;
goto err_free_fb_out;
}
memcpy(inst->vsi->hdr_buf, buf + nal_start_idx, buf_sz);
}
inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
inst->vsi->dec.y_fb_dma = y_fb_dma;
inst->vsi->dec.c_fb_dma = c_fb_dma;
inst->vsi->dec.vdec_fb_va = vdec_fb_va;
data[0] = buf_sz;
data[1] = nal_start;
err = vpu_dec_start(vpu, data, 2);
if (err) {
if (err > 0 && (DEC_ERR_RET(err) == H264_ERR_NOT_VALID)) {
mtk_vdec_err(inst->ctx, "- error bitstream - err = %d -", err);
err = -EIO;
}
goto err_free_fb_out;
}
*res_chg = inst->vsi->dec.resolution_changed;
if (*res_chg) {
struct vdec_pic_info pic;
mtk_vdec_debug(inst->ctx, "- resolution changed -");
get_pic_info(inst, &pic);
if (inst->vsi->dec.realloc_mv_buf) {
err = alloc_mv_buf(inst, &pic);
if (err)
goto err_free_fb_out;
}
}
if (nal_type == NAL_NON_IDR_SLICE || nal_type == NAL_IDR_SLICE) {
/* wait decoder done interrupt */
err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
MTK_INST_IRQ_RECEIVED,
WAIT_INTR_TIMEOUT_MS, 0);
if (err)
goto err_free_fb_out;
vpu_dec_end(vpu);
}
mtk_vdec_debug(inst->ctx, "\n - NALU[%d] type=%d -\n", inst->num_nalu, nal_type);
return 0;
err_free_fb_out:
put_fb_to_free(inst, fb);
mtk_vdec_err(inst->ctx, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
return err;
}
static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
struct h264_ring_fb_list *list,
bool disp_list, struct vdec_fb **out_fb)
{
struct vdec_fb *fb;
if (check_list_validity(inst, disp_list))
return;
if (list->count == 0) {
mtk_vdec_debug(inst->ctx, "[FB] there is no %s fb", disp_list ? "disp" : "free");
*out_fb = NULL;
return;
}
fb = (struct vdec_fb *)
(uintptr_t)list->fb_list[list->read_idx].vdec_fb_va;
fb->status |= (disp_list ? FB_ST_DISPLAY : FB_ST_FREE);
*out_fb = fb;
mtk_vdec_debug(inst->ctx, "[FB] get %s fb st=%d poc=%d %llx",
disp_list ? "disp" : "free",
fb->status, list->fb_list[list->read_idx].poc,
list->fb_list[list->read_idx].vdec_fb_va);
list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
0 : list->read_idx + 1;
list->count--;
}
static int vdec_h264_get_param(void *h_vdec, enum vdec_get_param_type type,
void *out)
{
struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
switch (type) {
case GET_PARAM_DISP_FRAME_BUFFER:
vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
break;
case GET_PARAM_FREE_FRAME_BUFFER:
vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
break;
case GET_PARAM_PIC_INFO:
get_pic_info(inst, out);
break;
case GET_PARAM_DPB_SIZE:
get_dpb_size(inst, out);
break;
case GET_PARAM_CROP_INFO:
get_crop_info(inst, out);
break;
default:
mtk_vdec_err(inst->ctx, "invalid get parameter type=%d", type);
return -EINVAL;
}
return 0;
}
const struct vdec_common_if vdec_h264_if = {
.init = vdec_h264_init,
.decode = vdec_h264_decode,
.get_param = vdec_h264_get_param,
.deinit = vdec_h264_deinit,
};
@@ -0,0 +1,322 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2022 MediaTek Inc.
* Author: Yunfei Dong <yunfei.dong@mediatek.com>
*/
#include "vdec_h264_req_common.h"
/* get used parameters for sps/pps */
#define GET_MTK_VDEC_FLAG(cond, flag) \
{ dst_param->cond = ((src_param->flags & flag) ? (1) : (0)); }
#define GET_MTK_VDEC_PARAM(param) \
{ dst_param->param = src_param->param; }
void mtk_vdec_h264_get_ref_list(u8 *ref_list,
const struct v4l2_h264_reference *v4l2_ref_list,
int num_valid)
{
u32 i;
/*
* TODO The firmware does not support field decoding. Future
* implementation must use v4l2_ref_list[i].fields to obtain
* the reference field parity.
*/
for (i = 0; i < num_valid; i++)
ref_list[i] = v4l2_ref_list[i].index;
/*
* The firmware expects unused reflist entries to have the value 0x20.
*/
memset(&ref_list[num_valid], 0x20, 32 - num_valid);
}
void *mtk_vdec_h264_get_ctrl_ptr(struct mtk_vcodec_dec_ctx *ctx, int id)
{
struct v4l2_ctrl *ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, id);
if (!ctrl)
return ERR_PTR(-EINVAL);
return ctrl->p_cur.p;
}
void mtk_vdec_h264_fill_dpb_info(struct mtk_vcodec_dec_ctx *ctx,
struct slice_api_h264_decode_param *decode_params,
struct mtk_h264_dpb_info *h264_dpb_info)
{
const struct slice_h264_dpb_entry *dpb;
struct vb2_queue *vq;
struct vb2_buffer *vb;
struct vb2_v4l2_buffer *vb2_v4l2;
int index;
vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
for (index = 0; index < V4L2_H264_NUM_DPB_ENTRIES; index++) {
dpb = &decode_params->dpb[index];
if (!(dpb->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)) {
h264_dpb_info[index].reference_flag = 0;
continue;
}
vb = vb2_find_buffer(vq, dpb->reference_ts);
if (!vb) {
dev_err(&ctx->dev->plat_dev->dev,
"Reference invalid: dpb_index(%d) reference_ts(%lld)",
index, dpb->reference_ts);
continue;
}
/* 1 for short term reference, 2 for long term reference */
if (!(dpb->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM))
h264_dpb_info[index].reference_flag = 1;
else
h264_dpb_info[index].reference_flag = 2;
vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
h264_dpb_info[index].field = vb2_v4l2->field;
h264_dpb_info[index].y_dma_addr =
vb2_dma_contig_plane_dma_addr(vb, 0);
if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
h264_dpb_info[index].c_dma_addr =
vb2_dma_contig_plane_dma_addr(vb, 1);
else
h264_dpb_info[index].c_dma_addr =
h264_dpb_info[index].y_dma_addr +
ctx->picinfo.fb_sz[0];
}
}
void mtk_vdec_h264_copy_sps_params(struct mtk_h264_sps_param *dst_param,
const struct v4l2_ctrl_h264_sps *src_param)
{
GET_MTK_VDEC_PARAM(chroma_format_idc);
GET_MTK_VDEC_PARAM(bit_depth_luma_minus8);
GET_MTK_VDEC_PARAM(bit_depth_chroma_minus8);
GET_MTK_VDEC_PARAM(log2_max_frame_num_minus4);
GET_MTK_VDEC_PARAM(pic_order_cnt_type);
GET_MTK_VDEC_PARAM(log2_max_pic_order_cnt_lsb_minus4);
GET_MTK_VDEC_PARAM(max_num_ref_frames);
GET_MTK_VDEC_PARAM(pic_width_in_mbs_minus1);
GET_MTK_VDEC_PARAM(pic_height_in_map_units_minus1);
GET_MTK_VDEC_FLAG(separate_colour_plane_flag,
V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE);
GET_MTK_VDEC_FLAG(qpprime_y_zero_transform_bypass_flag,
V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS);
GET_MTK_VDEC_FLAG(delta_pic_order_always_zero_flag,
V4L2_H264_SPS_FLAG_DELTA_PIC_ORDER_ALWAYS_ZERO);
GET_MTK_VDEC_FLAG(frame_mbs_only_flag,
V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY);
GET_MTK_VDEC_FLAG(mb_adaptive_frame_field_flag,
V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD);
GET_MTK_VDEC_FLAG(direct_8x8_inference_flag,
V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE);
}
void mtk_vdec_h264_copy_pps_params(struct mtk_h264_pps_param *dst_param,
const struct v4l2_ctrl_h264_pps *src_param)
{
GET_MTK_VDEC_PARAM(num_ref_idx_l0_default_active_minus1);
GET_MTK_VDEC_PARAM(num_ref_idx_l1_default_active_minus1);
GET_MTK_VDEC_PARAM(weighted_bipred_idc);
GET_MTK_VDEC_PARAM(pic_init_qp_minus26);
GET_MTK_VDEC_PARAM(chroma_qp_index_offset);
GET_MTK_VDEC_PARAM(second_chroma_qp_index_offset);
GET_MTK_VDEC_FLAG(entropy_coding_mode_flag,
V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE);
GET_MTK_VDEC_FLAG(pic_order_present_flag,
V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT);
GET_MTK_VDEC_FLAG(weighted_pred_flag,
V4L2_H264_PPS_FLAG_WEIGHTED_PRED);
GET_MTK_VDEC_FLAG(deblocking_filter_control_present_flag,
V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT);
GET_MTK_VDEC_FLAG(constrained_intra_pred_flag,
V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED);
GET_MTK_VDEC_FLAG(redundant_pic_cnt_present_flag,
V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT);
GET_MTK_VDEC_FLAG(transform_8x8_mode_flag,
V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE);
GET_MTK_VDEC_FLAG(scaling_matrix_present_flag,
V4L2_H264_PPS_FLAG_SCALING_MATRIX_PRESENT);
}
void mtk_vdec_h264_copy_slice_hd_params(struct mtk_h264_slice_hd_param *dst_param,
const struct v4l2_ctrl_h264_slice_params *src_param,
const struct v4l2_ctrl_h264_decode_params *dec_param)
{
int temp;
GET_MTK_VDEC_PARAM(first_mb_in_slice);
GET_MTK_VDEC_PARAM(slice_type);
GET_MTK_VDEC_PARAM(cabac_init_idc);
GET_MTK_VDEC_PARAM(slice_qp_delta);
GET_MTK_VDEC_PARAM(disable_deblocking_filter_idc);
GET_MTK_VDEC_PARAM(slice_alpha_c0_offset_div2);
GET_MTK_VDEC_PARAM(slice_beta_offset_div2);
GET_MTK_VDEC_PARAM(num_ref_idx_l0_active_minus1);
GET_MTK_VDEC_PARAM(num_ref_idx_l1_active_minus1);
dst_param->frame_num = dec_param->frame_num;
dst_param->pic_order_cnt_lsb = dec_param->pic_order_cnt_lsb;
dst_param->delta_pic_order_cnt_bottom =
dec_param->delta_pic_order_cnt_bottom;
dst_param->delta_pic_order_cnt0 =
dec_param->delta_pic_order_cnt0;
dst_param->delta_pic_order_cnt1 =
dec_param->delta_pic_order_cnt1;
temp = dec_param->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC;
dst_param->field_pic_flag = temp ? 1 : 0;
temp = dec_param->flags & V4L2_H264_DECODE_PARAM_FLAG_BOTTOM_FIELD;
dst_param->bottom_field_flag = temp ? 1 : 0;
GET_MTK_VDEC_FLAG(direct_spatial_mv_pred_flag,
V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED);
}
void mtk_vdec_h264_copy_scaling_matrix(struct slice_api_h264_scaling_matrix *dst_matrix,
const struct v4l2_ctrl_h264_scaling_matrix *src_matrix)
{
memcpy(dst_matrix->scaling_list_4x4, src_matrix->scaling_list_4x4,
sizeof(dst_matrix->scaling_list_4x4));
memcpy(dst_matrix->scaling_list_8x8, src_matrix->scaling_list_8x8,
sizeof(dst_matrix->scaling_list_8x8));
}
void
mtk_vdec_h264_copy_decode_params(struct slice_api_h264_decode_param *dst_params,
const struct v4l2_ctrl_h264_decode_params *src_params,
const struct v4l2_h264_dpb_entry dpb[V4L2_H264_NUM_DPB_ENTRIES])
{
struct slice_h264_dpb_entry *dst_entry;
const struct v4l2_h264_dpb_entry *src_entry;
int i;
for (i = 0; i < ARRAY_SIZE(dst_params->dpb); i++) {
dst_entry = &dst_params->dpb[i];
src_entry = &dpb[i];
dst_entry->reference_ts = src_entry->reference_ts;
dst_entry->frame_num = src_entry->frame_num;
dst_entry->pic_num = src_entry->pic_num;
dst_entry->top_field_order_cnt = src_entry->top_field_order_cnt;
dst_entry->bottom_field_order_cnt =
src_entry->bottom_field_order_cnt;
dst_entry->flags = src_entry->flags;
}
/* num_slices is a leftover from the old H.264 support and is ignored
* by the firmware.
*/
dst_params->num_slices = 0;
dst_params->nal_ref_idc = src_params->nal_ref_idc;
dst_params->top_field_order_cnt = src_params->top_field_order_cnt;
dst_params->bottom_field_order_cnt = src_params->bottom_field_order_cnt;
dst_params->flags = src_params->flags;
}
static bool mtk_vdec_h264_dpb_entry_match(const struct v4l2_h264_dpb_entry *a,
const struct v4l2_h264_dpb_entry *b)
{
return a->top_field_order_cnt == b->top_field_order_cnt &&
a->bottom_field_order_cnt == b->bottom_field_order_cnt;
}
/*
* Move DPB entries of dec_param that refer to a frame already existing in dpb
* into the already existing slot in dpb, and move other entries into new slots.
*
* This function is an adaptation of the similarly-named function in
* hantro_h264.c.
*/
void mtk_vdec_h264_update_dpb(const struct v4l2_ctrl_h264_decode_params *dec_param,
struct v4l2_h264_dpb_entry *dpb)
{
DECLARE_BITMAP(new, ARRAY_SIZE(dec_param->dpb)) = { 0, };
DECLARE_BITMAP(in_use, ARRAY_SIZE(dec_param->dpb)) = { 0, };
DECLARE_BITMAP(used, ARRAY_SIZE(dec_param->dpb)) = { 0, };
unsigned int i, j;
/* Disable all entries by default, and mark the ones in use. */
for (i = 0; i < ARRAY_SIZE(dec_param->dpb); i++) {
if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
set_bit(i, in_use);
dpb[i].flags &= ~V4L2_H264_DPB_ENTRY_FLAG_ACTIVE;
}
/* Try to match new DPB entries with existing ones by their POCs. */
for (i = 0; i < ARRAY_SIZE(dec_param->dpb); i++) {
const struct v4l2_h264_dpb_entry *ndpb = &dec_param->dpb[i];
if (!(ndpb->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
continue;
/*
* To cut off some comparisons, iterate only on target DPB
* entries were already used.
*/
for_each_set_bit(j, in_use, ARRAY_SIZE(dec_param->dpb)) {
struct v4l2_h264_dpb_entry *cdpb;
cdpb = &dpb[j];
if (!mtk_vdec_h264_dpb_entry_match(cdpb, ndpb))
continue;
*cdpb = *ndpb;
set_bit(j, used);
/* Don't reiterate on this one. */
clear_bit(j, in_use);
break;
}
if (j == ARRAY_SIZE(dec_param->dpb))
set_bit(i, new);
}
/* For entries that could not be matched, use remaining free slots. */
for_each_set_bit(i, new, ARRAY_SIZE(dec_param->dpb)) {
const struct v4l2_h264_dpb_entry *ndpb = &dec_param->dpb[i];
struct v4l2_h264_dpb_entry *cdpb;
/*
* Both arrays are of the same sizes, so there is no way
* we can end up with no space in target array, unless
* something is buggy.
*/
j = find_first_zero_bit(used, ARRAY_SIZE(dec_param->dpb));
if (WARN_ON(j >= ARRAY_SIZE(dec_param->dpb)))
return;
cdpb = &dpb[j];
*cdpb = *ndpb;
set_bit(j, used);
}
}
unsigned int mtk_vdec_h264_get_mv_buf_size(unsigned int width, unsigned int height)
{
int unit_size = (width / MB_UNIT_LEN) * (height / MB_UNIT_LEN) + 8;
return HW_MB_STORE_SZ * unit_size;
}
int mtk_vdec_h264_find_start_code(unsigned char *data, unsigned int data_sz)
{
if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
return 3;
if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
data[3] == 1)
return 4;
return -1;
}
@@ -0,0 +1,280 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2022 MediaTek Inc.
* Author: Yunfei Dong <yunfei.dong@mediatek.com>
*/
#ifndef _VDEC_H264_REQ_COMMON_H_
#define _VDEC_H264_REQ_COMMON_H_
#include <linux/module.h>
#include <linux/slab.h>
#include <media/v4l2-h264.h>
#include <media/v4l2-mem2mem.h>
#include <media/videobuf2-dma-contig.h>
#include "../mtk_vcodec_dec_drv.h"
#define NAL_NON_IDR_SLICE 0x01
#define NAL_IDR_SLICE 0x05
#define NAL_TYPE(value) ((value) & 0x1F)
#define BUF_PREDICTION_SZ (64 * 4096)
#define MB_UNIT_LEN 16
/* motion vector size (bytes) for every macro block */
#define HW_MB_STORE_SZ 64
#define H264_MAX_MV_NUM 32
/**
* struct mtk_h264_dpb_info - h264 dpb information
*
* @y_dma_addr: Y bitstream physical address
* @c_dma_addr: CbCr bitstream physical address
* @reference_flag: reference picture flag (short/long term reference picture)
* @field: field picture flag
*/
struct mtk_h264_dpb_info {
dma_addr_t y_dma_addr;
dma_addr_t c_dma_addr;
int reference_flag;
int field;
};
/*
* struct mtk_h264_sps_param - parameters for sps
*/
struct mtk_h264_sps_param {
unsigned char chroma_format_idc;
unsigned char bit_depth_luma_minus8;
unsigned char bit_depth_chroma_minus8;
unsigned char log2_max_frame_num_minus4;
unsigned char pic_order_cnt_type;
unsigned char log2_max_pic_order_cnt_lsb_minus4;
unsigned char max_num_ref_frames;
unsigned char separate_colour_plane_flag;
unsigned short pic_width_in_mbs_minus1;
unsigned short pic_height_in_map_units_minus1;
unsigned int max_frame_nums;
unsigned char qpprime_y_zero_transform_bypass_flag;
unsigned char delta_pic_order_always_zero_flag;
unsigned char frame_mbs_only_flag;
unsigned char mb_adaptive_frame_field_flag;
unsigned char direct_8x8_inference_flag;
unsigned char reserved[3];
};
/*
* struct mtk_h264_pps_param - parameters for pps
*/
struct mtk_h264_pps_param {
unsigned char num_ref_idx_l0_default_active_minus1;
unsigned char num_ref_idx_l1_default_active_minus1;
unsigned char weighted_bipred_idc;
char pic_init_qp_minus26;
char chroma_qp_index_offset;
char second_chroma_qp_index_offset;
unsigned char entropy_coding_mode_flag;
unsigned char pic_order_present_flag;
unsigned char deblocking_filter_control_present_flag;
unsigned char constrained_intra_pred_flag;
unsigned char weighted_pred_flag;
unsigned char redundant_pic_cnt_present_flag;
unsigned char transform_8x8_mode_flag;
unsigned char scaling_matrix_present_flag;
unsigned char reserved[2];
};
/*
* struct mtk_h264_slice_hd_param - parameters for slice header
*/
struct mtk_h264_slice_hd_param {
unsigned int first_mb_in_slice;
unsigned int field_pic_flag;
unsigned int slice_type;
unsigned int frame_num;
int pic_order_cnt_lsb;
int delta_pic_order_cnt_bottom;
unsigned int bottom_field_flag;
unsigned int direct_spatial_mv_pred_flag;
int delta_pic_order_cnt0;
int delta_pic_order_cnt1;
unsigned int cabac_init_idc;
int slice_qp_delta;
unsigned int disable_deblocking_filter_idc;
int slice_alpha_c0_offset_div2;
int slice_beta_offset_div2;
unsigned int num_ref_idx_l0_active_minus1;
unsigned int num_ref_idx_l1_active_minus1;
unsigned int reserved;
};
/*
* struct slice_api_h264_scaling_matrix - parameters for scaling list
*/
struct slice_api_h264_scaling_matrix {
unsigned char scaling_list_4x4[6][16];
unsigned char scaling_list_8x8[6][64];
};
/*
* struct slice_h264_dpb_entry - each dpb information
*/
struct slice_h264_dpb_entry {
unsigned long long reference_ts;
unsigned short frame_num;
unsigned short pic_num;
/* Note that field is indicated by v4l2_buffer.field */
int top_field_order_cnt;
int bottom_field_order_cnt;
unsigned int flags;
};
/*
* struct slice_api_h264_decode_param - parameters for decode.
*/
struct slice_api_h264_decode_param {
struct slice_h264_dpb_entry dpb[V4L2_H264_NUM_DPB_ENTRIES];
unsigned short num_slices;
unsigned short nal_ref_idc;
unsigned char ref_pic_list_p0[32];
unsigned char ref_pic_list_b0[32];
unsigned char ref_pic_list_b1[32];
int top_field_order_cnt;
int bottom_field_order_cnt;
unsigned int flags;
};
/**
* struct h264_fb - h264 decode frame buffer information
*
* @vdec_fb_va: virtual address of struct vdec_fb
* @y_fb_dma: dma address of Y frame buffer (luma)
* @c_fb_dma: dma address of C frame buffer (chroma)
* @poc: picture order count of frame buffer
* @reserved: for 8 bytes alignment
*/
struct h264_fb {
u64 vdec_fb_va;
u64 y_fb_dma;
u64 c_fb_dma;
s32 poc;
u32 reserved;
};
/**
* mtk_vdec_h264_get_ref_list - translate V4L2 reference list
*
* @ref_list: Mediatek reference picture list
* @v4l2_ref_list: V4L2 reference picture list
* @num_valid: used reference number
*/
void mtk_vdec_h264_get_ref_list(u8 *ref_list,
const struct v4l2_h264_reference *v4l2_ref_list,
int num_valid);
/**
* mtk_vdec_h264_get_ctrl_ptr - get each CID control address.
*
* @ctx: v4l2 ctx
* @id: CID control ID
*
* Return: returns CID ctrl address.
*/
void *mtk_vdec_h264_get_ctrl_ptr(struct mtk_vcodec_dec_ctx *ctx, int id);
/**
* mtk_vdec_h264_fill_dpb_info - Fill the decoded picture buffer info
*
* @ctx: v4l2 ctx
* @decode_params: slice decode params
* @h264_dpb_info: dpb buffer information
*/
void mtk_vdec_h264_fill_dpb_info(struct mtk_vcodec_dec_ctx *ctx,
struct slice_api_h264_decode_param *decode_params,
struct mtk_h264_dpb_info *h264_dpb_info);
/**
* mtk_vdec_h264_copy_sps_params - get sps params.
*
* @dst_param: sps params for hw decoder
* @src_param: sps params from user driver
*/
void mtk_vdec_h264_copy_sps_params(struct mtk_h264_sps_param *dst_param,
const struct v4l2_ctrl_h264_sps *src_param);
/**
* mtk_vdec_h264_copy_pps_params - get pps params.
*
* @dst_param: pps params for hw decoder
* @src_param: pps params from user driver
*/
void mtk_vdec_h264_copy_pps_params(struct mtk_h264_pps_param *dst_param,
const struct v4l2_ctrl_h264_pps *src_param);
/**
* mtk_vdec_h264_copy_slice_hd_params - get slice header params.
*
* @dst_param: slice params for hw decoder
* @src_param: slice params from user driver
* @dec_param: decode params from user driver
*/
void mtk_vdec_h264_copy_slice_hd_params(struct mtk_h264_slice_hd_param *dst_param,
const struct v4l2_ctrl_h264_slice_params *src_param,
const struct v4l2_ctrl_h264_decode_params *dec_param);
/**
* mtk_vdec_h264_copy_scaling_matrix - Copy scaling matrix from a control to the driver
*
* @dst_matrix: scaling list params for the HW decoder
* @src_matrix: scaling list params from a V4L2 control
*
* This function is used to copy the scaling matrix from a
* v4l2 control into the slice parameters for a decode.
*/
void mtk_vdec_h264_copy_scaling_matrix(struct slice_api_h264_scaling_matrix *dst_matrix,
const struct v4l2_ctrl_h264_scaling_matrix *src_matrix);
/**
* mtk_vdec_h264_copy_decode_params - get decode params.
*
* @dst_params: dst params for hw decoder
* @src_params: decode params from user driver
* @dpb: dpb information
*/
void
mtk_vdec_h264_copy_decode_params(struct slice_api_h264_decode_param *dst_params,
const struct v4l2_ctrl_h264_decode_params *src_params,
const struct v4l2_h264_dpb_entry dpb[V4L2_H264_NUM_DPB_ENTRIES]);
/**
* mtk_vdec_h264_update_dpb - update dpb list.
*
* @dec_param: v4l2 control decode params
* @dpb: dpb entry informaton
*/
void mtk_vdec_h264_update_dpb(const struct v4l2_ctrl_h264_decode_params *dec_param,
struct v4l2_h264_dpb_entry *dpb);
/**
* mtk_vdec_h264_find_start_code - find h264 start code using sofeware.
*
* @data: input buffer address
* @data_sz: input buffer size
*
* Return: returns start code position.
*/
int mtk_vdec_h264_find_start_code(unsigned char *data, unsigned int data_sz);
/**
* mtk_vdec_h264_get_mv_buf_size - get mv buffer size.
*
* @width: picture width
* @height: picture height
*
* Return: returns mv buffer size.
*/
unsigned int mtk_vdec_h264_get_mv_buf_size(unsigned int width, unsigned int height);
#endif
@@ -0,0 +1,449 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/module.h>
#include <linux/slab.h>
#include <media/v4l2-mem2mem.h>
#include <media/v4l2-h264.h>
#include <media/videobuf2-dma-contig.h>
#include "../mtk_vcodec_dec.h"
#include "../../common/mtk_vcodec_intr.h"
#include "../vdec_drv_base.h"
#include "../vdec_drv_if.h"
#include "../vdec_vpu_if.h"
#include "vdec_h264_req_common.h"
/*
* struct mtk_h264_dec_slice_param - parameters for decode current frame
*/
struct mtk_h264_dec_slice_param {
struct mtk_h264_sps_param sps;
struct mtk_h264_pps_param pps;
struct slice_api_h264_scaling_matrix scaling_matrix;
struct slice_api_h264_decode_param decode_params;
struct mtk_h264_dpb_info h264_dpb_info[16];
};
/**
* struct vdec_h264_dec_info - decode information
* @dpb_sz : decoding picture buffer size
* @resolution_changed : flag to notify that a resolution change happened
* @realloc_mv_buf : flag to notify driver to re-allocate mv buffer
* @cap_num_planes : number planes of capture buffer
* @bs_dma : Input bit-stream buffer dma address
* @y_fb_dma : Y frame buffer dma address
* @c_fb_dma : C frame buffer dma address
* @vdec_fb_va : VDEC frame buffer struct virtual address
*/
struct vdec_h264_dec_info {
u32 dpb_sz;
u32 resolution_changed;
u32 realloc_mv_buf;
u32 cap_num_planes;
u64 bs_dma;
u64 y_fb_dma;
u64 c_fb_dma;
u64 vdec_fb_va;
};
/**
* struct vdec_h264_vsi - shared memory for decode information exchange
* between VPU and Host.
* The memory is allocated by VPU then mapping to Host
* in vpu_dec_init() and freed in vpu_dec_deinit()
* by VPU.
* AP-W/R : AP is writer/reader on this item
* VPU-W/R: VPU is write/reader on this item
* @pred_buf_dma : HW working prediction buffer dma address (AP-W, VPU-R)
* @mv_buf_dma : HW working motion vector buffer dma address (AP-W, VPU-R)
* @dec : decode information (AP-R, VPU-W)
* @pic : picture information (AP-R, VPU-W)
* @crop : crop information (AP-R, VPU-W)
* @h264_slice_params : the parameters that hardware use to decode
*/
struct vdec_h264_vsi {
u64 pred_buf_dma;
u64 mv_buf_dma[H264_MAX_MV_NUM];
struct vdec_h264_dec_info dec;
struct vdec_pic_info pic;
struct v4l2_rect crop;
struct mtk_h264_dec_slice_param h264_slice_params;
};
/**
* struct vdec_h264_slice_inst - h264 decoder instance
* @num_nalu : how many nalus be decoded
* @ctx : point to mtk_vcodec_dec_ctx
* @pred_buf : HW working prediction buffer
* @mv_buf : HW working motion vector buffer
* @vpu : VPU instance
* @vsi_ctx : Local VSI data for this decoding context
* @h264_slice_param : the parameters that hardware use to decode
* @dpb : decoded picture buffer used to store reference buffer information
*/
struct vdec_h264_slice_inst {
unsigned int num_nalu;
struct mtk_vcodec_dec_ctx *ctx;
struct mtk_vcodec_mem pred_buf;
struct mtk_vcodec_mem mv_buf[H264_MAX_MV_NUM];
struct vdec_vpu_inst vpu;
struct vdec_h264_vsi vsi_ctx;
struct mtk_h264_dec_slice_param h264_slice_param;
struct v4l2_h264_dpb_entry dpb[16];
};
static int get_vdec_decode_parameters(struct vdec_h264_slice_inst *inst)
{
const struct v4l2_ctrl_h264_decode_params *dec_params;
const struct v4l2_ctrl_h264_sps *sps;
const struct v4l2_ctrl_h264_pps *pps;
const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
struct mtk_h264_dec_slice_param *slice_param = &inst->h264_slice_param;
struct v4l2_h264_reflist_builder reflist_builder;
struct v4l2_h264_reference v4l2_p0_reflist[V4L2_H264_REF_LIST_LEN];
struct v4l2_h264_reference v4l2_b0_reflist[V4L2_H264_REF_LIST_LEN];
struct v4l2_h264_reference v4l2_b1_reflist[V4L2_H264_REF_LIST_LEN];
u8 *p0_reflist = slice_param->decode_params.ref_pic_list_p0;
u8 *b0_reflist = slice_param->decode_params.ref_pic_list_b0;
u8 *b1_reflist = slice_param->decode_params.ref_pic_list_b1;
dec_params =
mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
if (IS_ERR(dec_params))
return PTR_ERR(dec_params);
sps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SPS);
if (IS_ERR(sps))
return PTR_ERR(sps);
pps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_PPS);
if (IS_ERR(pps))
return PTR_ERR(pps);
scaling_matrix =
mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
if (IS_ERR(scaling_matrix))
return PTR_ERR(scaling_matrix);
mtk_vdec_h264_update_dpb(dec_params, inst->dpb);
mtk_vdec_h264_copy_sps_params(&slice_param->sps, sps);
mtk_vdec_h264_copy_pps_params(&slice_param->pps, pps);
mtk_vdec_h264_copy_scaling_matrix(&slice_param->scaling_matrix, scaling_matrix);
mtk_vdec_h264_copy_decode_params(&slice_param->decode_params,
dec_params, inst->dpb);
mtk_vdec_h264_fill_dpb_info(inst->ctx, &slice_param->decode_params,
slice_param->h264_dpb_info);
/* Build the reference lists */
v4l2_h264_init_reflist_builder(&reflist_builder, dec_params, sps,
inst->dpb);
v4l2_h264_build_p_ref_list(&reflist_builder, v4l2_p0_reflist);
v4l2_h264_build_b_ref_lists(&reflist_builder, v4l2_b0_reflist,
v4l2_b1_reflist);
/* Adapt the built lists to the firmware's expectations */
mtk_vdec_h264_get_ref_list(p0_reflist, v4l2_p0_reflist, reflist_builder.num_valid);
mtk_vdec_h264_get_ref_list(b0_reflist, v4l2_b0_reflist, reflist_builder.num_valid);
mtk_vdec_h264_get_ref_list(b1_reflist, v4l2_b1_reflist, reflist_builder.num_valid);
memcpy(&inst->vsi_ctx.h264_slice_params, slice_param,
sizeof(inst->vsi_ctx.h264_slice_params));
return 0;
}
static int allocate_prediction_buf(struct vdec_h264_slice_inst *inst)
{
int err;
inst->pred_buf.size = BUF_PREDICTION_SZ;
err = mtk_vcodec_mem_alloc(inst->ctx, &inst->pred_buf);
if (err) {
mtk_vdec_err(inst->ctx, "failed to allocate ppl buf");
return err;
}
inst->vsi_ctx.pred_buf_dma = inst->pred_buf.dma_addr;
return 0;
}
static void free_prediction_buf(struct vdec_h264_slice_inst *inst)
{
struct mtk_vcodec_mem *mem = &inst->pred_buf;
inst->vsi_ctx.pred_buf_dma = 0;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
}
static int alloc_mv_buf(struct vdec_h264_slice_inst *inst,
struct vdec_pic_info *pic)
{
int i;
int err;
struct mtk_vcodec_mem *mem = NULL;
unsigned int buf_sz = mtk_vdec_h264_get_mv_buf_size(pic->buf_w, pic->buf_h);
mtk_v4l2_vdec_dbg(3, inst->ctx, "size = 0x%x", buf_sz);
for (i = 0; i < H264_MAX_MV_NUM; i++) {
mem = &inst->mv_buf[i];
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
mem->size = buf_sz;
err = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (err) {
mtk_vdec_err(inst->ctx, "failed to allocate mv buf");
return err;
}
inst->vsi_ctx.mv_buf_dma[i] = mem->dma_addr;
}
return 0;
}
static void free_mv_buf(struct vdec_h264_slice_inst *inst)
{
int i;
struct mtk_vcodec_mem *mem;
for (i = 0; i < H264_MAX_MV_NUM; i++) {
inst->vsi_ctx.mv_buf_dma[i] = 0;
mem = &inst->mv_buf[i];
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
}
}
static void get_pic_info(struct vdec_h264_slice_inst *inst,
struct vdec_pic_info *pic)
{
struct mtk_vcodec_dec_ctx *ctx = inst->ctx;
ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, VCODEC_DEC_ALIGNED_64);
ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, VCODEC_DEC_ALIGNED_64);
ctx->picinfo.fb_sz[0] = ctx->picinfo.buf_w * ctx->picinfo.buf_h;
ctx->picinfo.fb_sz[1] = ctx->picinfo.fb_sz[0] >> 1;
inst->vsi_ctx.dec.cap_num_planes =
ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes;
*pic = ctx->picinfo;
mtk_vdec_debug(inst->ctx, "pic(%d, %d), buf(%d, %d)",
ctx->picinfo.pic_w, ctx->picinfo.pic_h,
ctx->picinfo.buf_w, ctx->picinfo.buf_h);
mtk_vdec_debug(inst->ctx, "Y/C(%d, %d)", ctx->picinfo.fb_sz[0],
ctx->picinfo.fb_sz[1]);
if (ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w ||
ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h) {
inst->vsi_ctx.dec.resolution_changed = true;
if (ctx->last_decoded_picinfo.buf_w != ctx->picinfo.buf_w ||
ctx->last_decoded_picinfo.buf_h != ctx->picinfo.buf_h)
inst->vsi_ctx.dec.realloc_mv_buf = true;
mtk_v4l2_vdec_dbg(1, inst->ctx, "ResChg: (%d %d) : old(%d, %d) -> new(%d, %d)",
inst->vsi_ctx.dec.resolution_changed,
inst->vsi_ctx.dec.realloc_mv_buf,
ctx->last_decoded_picinfo.pic_w,
ctx->last_decoded_picinfo.pic_h,
ctx->picinfo.pic_w, ctx->picinfo.pic_h);
}
}
static void get_crop_info(struct vdec_h264_slice_inst *inst, struct v4l2_rect *cr)
{
cr->left = inst->vsi_ctx.crop.left;
cr->top = inst->vsi_ctx.crop.top;
cr->width = inst->vsi_ctx.crop.width;
cr->height = inst->vsi_ctx.crop.height;
mtk_vdec_debug(inst->ctx, "l=%d, t=%d, w=%d, h=%d",
cr->left, cr->top, cr->width, cr->height);
}
static void get_dpb_size(struct vdec_h264_slice_inst *inst, unsigned int *dpb_sz)
{
*dpb_sz = inst->vsi_ctx.dec.dpb_sz;
mtk_vdec_debug(inst->ctx, "sz=%d", *dpb_sz);
}
static int vdec_h264_slice_init(struct mtk_vcodec_dec_ctx *ctx)
{
struct vdec_h264_slice_inst *inst;
int err;
inst = kzalloc_obj(*inst);
if (!inst)
return -ENOMEM;
inst->ctx = ctx;
inst->vpu.id = SCP_IPI_VDEC_H264;
inst->vpu.ctx = ctx;
err = vpu_dec_init(&inst->vpu);
if (err) {
mtk_vdec_err(ctx, "vdec_h264 init err=%d", err);
goto error_free_inst;
}
memcpy(&inst->vsi_ctx, inst->vpu.vsi, sizeof(inst->vsi_ctx));
inst->vsi_ctx.dec.resolution_changed = true;
inst->vsi_ctx.dec.realloc_mv_buf = true;
err = allocate_prediction_buf(inst);
if (err)
goto error_deinit;
mtk_vdec_debug(ctx, "struct size = %zu,%zu,%zu,%zu\n",
sizeof(struct mtk_h264_sps_param),
sizeof(struct mtk_h264_pps_param),
sizeof(struct mtk_h264_dec_slice_param),
sizeof(struct mtk_h264_dpb_info));
mtk_vdec_debug(ctx, "H264 Instance >> %p", inst);
ctx->drv_handle = inst;
return 0;
error_deinit:
vpu_dec_deinit(&inst->vpu);
error_free_inst:
kfree(inst);
return err;
}
static void vdec_h264_slice_deinit(void *h_vdec)
{
struct vdec_h264_slice_inst *inst = h_vdec;
vpu_dec_deinit(&inst->vpu);
free_prediction_buf(inst);
free_mv_buf(inst);
kfree(inst);
}
static int vdec_h264_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
struct vdec_fb *unused, bool *res_chg)
{
struct vdec_h264_slice_inst *inst = h_vdec;
const struct v4l2_ctrl_h264_decode_params *dec_params =
mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
struct vdec_vpu_inst *vpu = &inst->vpu;
struct mtk_video_dec_buf *src_buf_info;
struct mtk_video_dec_buf *dst_buf_info;
struct vdec_fb *fb;
u32 data[2];
u64 y_fb_dma;
u64 c_fb_dma;
int err;
inst->num_nalu++;
/* bs NULL means flush decoder */
if (!bs)
return vpu_dec_reset(vpu);
fb = inst->ctx->dev->vdec_pdata->get_cap_buffer(inst->ctx);
if (!fb) {
mtk_vdec_err(inst->ctx, "fb buffer is NULL");
return -ENOMEM;
}
src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
dst_buf_info = container_of(fb, struct mtk_video_dec_buf, frame_buffer);
y_fb_dma = fb->base_y.dma_addr;
c_fb_dma = fb->base_c.dma_addr;
mtk_vdec_debug(inst->ctx, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
inst->num_nalu, y_fb_dma, c_fb_dma, fb);
inst->vsi_ctx.dec.bs_dma = (uint64_t)bs->dma_addr;
inst->vsi_ctx.dec.y_fb_dma = y_fb_dma;
inst->vsi_ctx.dec.c_fb_dma = c_fb_dma;
inst->vsi_ctx.dec.vdec_fb_va = (u64)(uintptr_t)fb;
v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb,
&dst_buf_info->m2m_buf.vb);
err = get_vdec_decode_parameters(inst);
if (err)
goto err_free_fb_out;
data[0] = bs->size;
/*
* Reconstruct the first byte of the NAL unit, as the firmware requests
* that information to be passed even though it is present in the stream
* itself...
*/
data[1] = (dec_params->nal_ref_idc << 5) |
((dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_IDR_PIC)
? 0x5 : 0x1);
*res_chg = inst->vsi_ctx.dec.resolution_changed;
if (*res_chg) {
mtk_vdec_debug(inst->ctx, "- resolution changed -");
if (inst->vsi_ctx.dec.realloc_mv_buf) {
err = alloc_mv_buf(inst, &inst->ctx->picinfo);
inst->vsi_ctx.dec.realloc_mv_buf = false;
if (err)
goto err_free_fb_out;
}
*res_chg = false;
}
memcpy(inst->vpu.vsi, &inst->vsi_ctx, sizeof(inst->vsi_ctx));
err = vpu_dec_start(vpu, data, 2);
if (err)
goto err_free_fb_out;
/* wait decoder done interrupt */
err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
MTK_INST_IRQ_RECEIVED,
WAIT_INTR_TIMEOUT_MS, 0);
if (err)
goto err_free_fb_out;
vpu_dec_end(vpu);
memcpy(&inst->vsi_ctx, inst->vpu.vsi, sizeof(inst->vsi_ctx));
mtk_vdec_debug(inst->ctx, "\n - NALU[%d]", inst->num_nalu);
return 0;
err_free_fb_out:
mtk_vdec_err(inst->ctx, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
return err;
}
static int vdec_h264_slice_get_param(void *h_vdec, enum vdec_get_param_type type, void *out)
{
struct vdec_h264_slice_inst *inst = h_vdec;
switch (type) {
case GET_PARAM_PIC_INFO:
get_pic_info(inst, out);
break;
case GET_PARAM_DPB_SIZE:
get_dpb_size(inst, out);
break;
case GET_PARAM_CROP_INFO:
get_crop_info(inst, out);
break;
default:
mtk_vdec_err(inst->ctx, "invalid get parameter type=%d", type);
return -EINVAL;
}
return 0;
}
const struct vdec_common_if vdec_h264_slice_if = {
.init = vdec_h264_slice_init,
.decode = vdec_h264_slice_decode,
.get_param = vdec_h264_slice_get_param,
.deinit = vdec_h264_slice_deinit,
};
@@ -0,0 +1,608 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2016 MediaTek Inc.
* Author: Jungchang Tsao <jungchang.tsao@mediatek.com>
* PC Chen <pc.chen@mediatek.com>
*/
#include <linux/slab.h>
#include "../vdec_drv_if.h"
#include "../mtk_vcodec_dec.h"
#include "../../common/mtk_vcodec_intr.h"
#include "../vdec_vpu_if.h"
#include "../vdec_drv_base.h"
/* Decoding picture buffer size (3 reference frames plus current frame) */
#define VP8_DPB_SIZE 4
/* HW working buffer size (bytes) */
#define VP8_WORKING_BUF_SZ (45 * 4096)
/* HW control register address */
#define VP8_SEGID_DRAM_ADDR 0x3c
#define VP8_HW_VLD_ADDR 0x93C
#define VP8_HW_VLD_VALUE 0x940
#define VP8_BSASET 0x100
#define VP8_BSDSET 0x104
#define VP8_RW_CKEN_SET 0x0
#define VP8_RW_DCM_CON 0x18
#define VP8_WO_VLD_SRST 0x108
#define VP8_RW_MISC_SYS_SEL 0x84
#define VP8_RW_MISC_SPEC_CON 0xC8
#define VP8_WO_VLD_SRST 0x108
#define VP8_RW_VP8_CTRL 0xA4
#define VP8_RW_MISC_DCM_CON 0xEC
#define VP8_RW_MISC_SRST 0xF4
#define VP8_RW_MISC_FUNC_CON 0xCC
#define VP8_MAX_FRM_BUF_NUM 5
#define VP8_MAX_FRM_BUF_NODE_NUM (VP8_MAX_FRM_BUF_NUM * 2)
/* required buffer size (bytes) to store decode information */
#define VP8_HW_SEGMENT_DATA_SZ 272
#define VP8_HW_SEGMENT_UINT 4
#define VP8_DEC_TABLE_PROC_LOOP 96
#define VP8_DEC_TABLE_UNIT 3
#define VP8_DEC_TABLE_SZ 300
#define VP8_DEC_TABLE_OFFSET 2
#define VP8_DEC_TABLE_RW_UNIT 4
/**
* struct vdec_vp8_dec_info - decode misc information
* @working_buf_dma : working buffer dma address
* @prev_y_dma : previous decoded frame buffer Y plane address
* @cur_y_fb_dma : current plane Y frame buffer dma address
* @cur_c_fb_dma : current plane C frame buffer dma address
* @bs_dma : bitstream dma address
* @bs_sz : bitstream size
* @resolution_changed: resolution change flag 1 - changed, 0 - not changed
* @show_frame : display this frame or not
* @wait_key_frame : wait key frame coming
*/
struct vdec_vp8_dec_info {
uint64_t working_buf_dma;
uint64_t prev_y_dma;
uint64_t cur_y_fb_dma;
uint64_t cur_c_fb_dma;
uint64_t bs_dma;
uint32_t bs_sz;
uint32_t resolution_changed;
uint32_t show_frame;
uint32_t wait_key_frame;
};
/**
* struct vdec_vp8_vsi - VPU shared information
* @dec : decoding information
* @pic : picture information
* @dec_table : decoder coefficient table
* @segment_buf : segmentation buffer
* @load_data : flag to indicate reload decode data
*/
struct vdec_vp8_vsi {
struct vdec_vp8_dec_info dec;
struct vdec_pic_info pic;
uint32_t dec_table[VP8_DEC_TABLE_SZ];
uint32_t segment_buf[VP8_HW_SEGMENT_DATA_SZ][VP8_HW_SEGMENT_UINT];
uint32_t load_data;
};
/**
* struct vdec_vp8_hw_reg_base - HW register base
* @misc : base address for misc
* @ld : base address for ld
* @top : base address for top
* @cm : base address for cm
* @hwd : base address for hwd
* @hwb : base address for hwb
*/
struct vdec_vp8_hw_reg_base {
void __iomem *misc;
void __iomem *ld;
void __iomem *top;
void __iomem *cm;
void __iomem *hwd;
void __iomem *hwb;
};
/**
* struct vdec_vp8_vpu_inst - VPU instance for VP8 decode
* @wq_hd : Wait queue to wait VPU message ack
* @signaled : 1 - Host has received ack message from VPU, 0 - not received
* @failure : VPU execution result status 0 - success, others - fail
* @inst_addr : VPU decoder instance address
*/
struct vdec_vp8_vpu_inst {
wait_queue_head_t wq_hd;
int signaled;
int failure;
uint32_t inst_addr;
};
/* frame buffer (fb) list
* [available_fb_node_list] - decode fb are initialized to 0 and populated in
* [fb_use_list] - fb is set after decode and is moved to this list
* [fb_free_list] - fb is not needed for reference will be moved from
* [fb_use_list] to [fb_free_list] and
* once user remove fb from [fb_free_list],
* it is circulated back to [available_fb_node_list]
* [fb_disp_list] - fb is set after decode and is moved to this list
* once user remove fb from [fb_disp_list] it is
* circulated back to [available_fb_node_list]
*/
/**
* struct vdec_vp8_inst - VP8 decoder instance
* @cur_fb : current frame buffer
* @dec_fb : decode frame buffer node
* @available_fb_node_list : list to store available frame buffer node
* @fb_use_list : list to store frame buffer in use
* @fb_free_list : list to store free frame buffer
* @fb_disp_list : list to store display ready frame buffer
* @working_buf : HW decoder working buffer
* @reg_base : HW register base address
* @frm_cnt : decode frame count
* @ctx : V4L2 context
* @vpu : VPU instance for decoder
* @vsi : VPU share information
*/
struct vdec_vp8_inst {
struct vdec_fb *cur_fb;
struct vdec_fb_node dec_fb[VP8_MAX_FRM_BUF_NODE_NUM];
struct list_head available_fb_node_list;
struct list_head fb_use_list;
struct list_head fb_free_list;
struct list_head fb_disp_list;
struct mtk_vcodec_mem working_buf;
struct vdec_vp8_hw_reg_base reg_base;
unsigned int frm_cnt;
struct mtk_vcodec_dec_ctx *ctx;
struct vdec_vpu_inst vpu;
struct vdec_vp8_vsi *vsi;
};
static void get_hw_reg_base(struct vdec_vp8_inst *inst)
{
void __iomem **reg_base = inst->ctx->dev->reg_base;
inst->reg_base.top = mtk_vcodec_get_reg_addr(reg_base, VDEC_TOP);
inst->reg_base.cm = mtk_vcodec_get_reg_addr(reg_base, VDEC_CM);
inst->reg_base.hwd = mtk_vcodec_get_reg_addr(reg_base, VDEC_HWD);
inst->reg_base.misc = mtk_vcodec_get_reg_addr(reg_base, VDEC_MISC);
inst->reg_base.ld = mtk_vcodec_get_reg_addr(reg_base, VDEC_LD);
inst->reg_base.hwb = mtk_vcodec_get_reg_addr(reg_base, VDEC_HWB);
}
static void write_hw_segmentation_data(struct vdec_vp8_inst *inst)
{
int i, j;
u32 seg_id_addr;
u32 val;
void __iomem *cm = inst->reg_base.cm;
struct vdec_vp8_vsi *vsi = inst->vsi;
seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
for (i = 0; i < ARRAY_SIZE(vsi->segment_buf); i++) {
for (j = ARRAY_SIZE(vsi->segment_buf[i]) - 1; j >= 0; j--) {
val = (1 << 16) + ((seg_id_addr + i) << 2) + j;
writel(val, cm + VP8_HW_VLD_ADDR);
val = vsi->segment_buf[i][j];
writel(val, cm + VP8_HW_VLD_VALUE);
}
}
}
static void read_hw_segmentation_data(struct vdec_vp8_inst *inst)
{
int i, j;
u32 seg_id_addr;
u32 val;
void __iomem *cm = inst->reg_base.cm;
struct vdec_vp8_vsi *vsi = inst->vsi;
seg_id_addr = readl(inst->reg_base.top + VP8_SEGID_DRAM_ADDR) >> 4;
for (i = 0; i < ARRAY_SIZE(vsi->segment_buf); i++) {
for (j = ARRAY_SIZE(vsi->segment_buf[i]) - 1; j >= 0; j--) {
val = ((seg_id_addr + i) << 2) + j;
writel(val, cm + VP8_HW_VLD_ADDR);
val = readl(cm + VP8_HW_VLD_VALUE);
vsi->segment_buf[i][j] = val;
}
}
}
/* reset HW and enable HW read/write data function */
static void enable_hw_rw_function(struct vdec_vp8_inst *inst)
{
u32 val = 0;
void __iomem *misc = inst->reg_base.misc;
void __iomem *ld = inst->reg_base.ld;
void __iomem *hwb = inst->reg_base.hwb;
void __iomem *hwd = inst->reg_base.hwd;
mtk_vcodec_write_vdecsys(inst->ctx, VP8_RW_CKEN_SET, 0x1);
writel(0x101, ld + VP8_WO_VLD_SRST);
writel(0x101, hwb + VP8_WO_VLD_SRST);
mtk_vcodec_write_vdecsys(inst->ctx, 0, 0x1);
val = readl(misc + VP8_RW_MISC_SRST);
writel((val & 0xFFFFFFFE), misc + VP8_RW_MISC_SRST);
writel(0x1, misc + VP8_RW_MISC_SYS_SEL);
writel(0x17F, misc + VP8_RW_MISC_SPEC_CON);
writel(0x71201100, misc + VP8_RW_MISC_FUNC_CON);
writel(0x0, ld + VP8_WO_VLD_SRST);
writel(0x0, hwb + VP8_WO_VLD_SRST);
mtk_vcodec_write_vdecsys(inst->ctx, VP8_RW_DCM_CON, 0x1);
writel(0x1, misc + VP8_RW_MISC_DCM_CON);
writel(0x1, hwd + VP8_RW_VP8_CTRL);
}
static void store_dec_table(struct vdec_vp8_inst *inst)
{
int i, j;
u32 addr = 0, val = 0;
void __iomem *hwd = inst->reg_base.hwd;
u32 *p = &inst->vsi->dec_table[VP8_DEC_TABLE_OFFSET];
for (i = 0; i < VP8_DEC_TABLE_PROC_LOOP; i++) {
writel(addr, hwd + VP8_BSASET);
for (j = 0; j < VP8_DEC_TABLE_UNIT ; j++) {
val = *p++;
writel(val, hwd + VP8_BSDSET);
}
addr += VP8_DEC_TABLE_RW_UNIT;
}
}
static void load_dec_table(struct vdec_vp8_inst *inst)
{
int i;
u32 addr = 0;
u32 *p = &inst->vsi->dec_table[VP8_DEC_TABLE_OFFSET];
void __iomem *hwd = inst->reg_base.hwd;
for (i = 0; i < VP8_DEC_TABLE_PROC_LOOP; i++) {
writel(addr, hwd + VP8_BSASET);
/* read total 11 bytes */
*p++ = readl(hwd + VP8_BSDSET);
*p++ = readl(hwd + VP8_BSDSET);
*p++ = readl(hwd + VP8_BSDSET) & 0xFFFFFF;
addr += VP8_DEC_TABLE_RW_UNIT;
}
}
static void get_pic_info(struct vdec_vp8_inst *inst, struct vdec_pic_info *pic)
{
*pic = inst->vsi->pic;
mtk_vdec_debug(inst->ctx, "pic(%d, %d), buf(%d, %d)",
pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
mtk_vdec_debug(inst->ctx, "fb size: Y(%d), C(%d)",
pic->fb_sz[0], pic->fb_sz[1]);
}
static void vp8_dec_finish(struct vdec_vp8_inst *inst)
{
struct vdec_fb_node *node;
uint64_t prev_y_dma = inst->vsi->dec.prev_y_dma;
mtk_vdec_debug(inst->ctx, "prev fb base dma=%llx", prev_y_dma);
/* put last decode ok frame to fb_free_list */
if (prev_y_dma != 0) {
list_for_each_entry(node, &inst->fb_use_list, list) {
struct vdec_fb *fb = (struct vdec_fb *)node->fb;
if (prev_y_dma == (uint64_t)fb->base_y.dma_addr) {
list_move_tail(&node->list,
&inst->fb_free_list);
break;
}
}
}
/* available_fb_node_list -> fb_use_list */
node = list_first_entry(&inst->available_fb_node_list,
struct vdec_fb_node, list);
node->fb = inst->cur_fb;
list_move_tail(&node->list, &inst->fb_use_list);
/* available_fb_node_list -> fb_disp_list */
if (inst->vsi->dec.show_frame) {
node = list_first_entry(&inst->available_fb_node_list,
struct vdec_fb_node, list);
node->fb = inst->cur_fb;
list_move_tail(&node->list, &inst->fb_disp_list);
}
}
static void move_fb_list_use_to_free(struct vdec_vp8_inst *inst)
{
struct vdec_fb_node *node, *tmp;
list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list)
list_move_tail(&node->list, &inst->fb_free_list);
}
static void init_list(struct vdec_vp8_inst *inst)
{
int i;
INIT_LIST_HEAD(&inst->available_fb_node_list);
INIT_LIST_HEAD(&inst->fb_use_list);
INIT_LIST_HEAD(&inst->fb_free_list);
INIT_LIST_HEAD(&inst->fb_disp_list);
for (i = 0; i < ARRAY_SIZE(inst->dec_fb); i++) {
INIT_LIST_HEAD(&inst->dec_fb[i].list);
inst->dec_fb[i].fb = NULL;
list_add_tail(&inst->dec_fb[i].list,
&inst->available_fb_node_list);
}
}
static void add_fb_to_free_list(struct vdec_vp8_inst *inst, void *fb)
{
struct vdec_fb_node *node;
if (fb) {
node = list_first_entry(&inst->available_fb_node_list,
struct vdec_fb_node, list);
node->fb = fb;
list_move_tail(&node->list, &inst->fb_free_list);
}
}
static int alloc_working_buf(struct vdec_vp8_inst *inst)
{
int err;
struct mtk_vcodec_mem *mem = &inst->working_buf;
mem->size = VP8_WORKING_BUF_SZ;
err = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (err) {
mtk_vdec_err(inst->ctx, "Cannot allocate working buffer");
return err;
}
inst->vsi->dec.working_buf_dma = (uint64_t)mem->dma_addr;
return 0;
}
static void free_working_buf(struct vdec_vp8_inst *inst)
{
struct mtk_vcodec_mem *mem = &inst->working_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
inst->vsi->dec.working_buf_dma = 0;
}
static int vdec_vp8_init(struct mtk_vcodec_dec_ctx *ctx)
{
struct vdec_vp8_inst *inst;
int err;
inst = kzalloc_obj(*inst);
if (!inst)
return -ENOMEM;
inst->ctx = ctx;
inst->vpu.id = IPI_VDEC_VP8;
inst->vpu.ctx = ctx;
err = vpu_dec_init(&inst->vpu);
if (err) {
mtk_vdec_err(ctx, "vdec_vp8 init err=%d", err);
goto error_free_inst;
}
inst->vsi = (struct vdec_vp8_vsi *)inst->vpu.vsi;
init_list(inst);
err = alloc_working_buf(inst);
if (err)
goto error_deinit;
get_hw_reg_base(inst);
mtk_vdec_debug(ctx, "VP8 Instance >> %p", inst);
ctx->drv_handle = inst;
return 0;
error_deinit:
vpu_dec_deinit(&inst->vpu);
error_free_inst:
kfree(inst);
return err;
}
static int vdec_vp8_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
struct vdec_fb *fb, bool *res_chg)
{
struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
struct vdec_vp8_dec_info *dec = &inst->vsi->dec;
struct vdec_vpu_inst *vpu = &inst->vpu;
unsigned char *bs_va;
unsigned int data;
int err = 0;
uint64_t y_fb_dma;
uint64_t c_fb_dma;
/* bs NULL means flush decoder */
if (bs == NULL) {
move_fb_list_use_to_free(inst);
return vpu_dec_reset(vpu);
}
y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
mtk_vdec_debug(inst->ctx, "+ [%d] FB y_dma=%llx c_dma=%llx fb=%p",
inst->frm_cnt, y_fb_dma, c_fb_dma, fb);
inst->cur_fb = fb;
dec->bs_dma = bs->dma_addr;
dec->bs_sz = bs->size;
dec->cur_y_fb_dma = y_fb_dma;
dec->cur_c_fb_dma = c_fb_dma;
mtk_vdec_debug(inst->ctx, "\n + FRAME[%d] +\n", inst->frm_cnt);
write_hw_segmentation_data(inst);
enable_hw_rw_function(inst);
store_dec_table(inst);
bs_va = (unsigned char *)bs->va;
/* retrieve width/hight and scale info from header */
data = (*(bs_va + 9) << 24) | (*(bs_va + 8) << 16) |
(*(bs_va + 7) << 8) | *(bs_va + 6);
err = vpu_dec_start(vpu, &data, 1);
if (err) {
add_fb_to_free_list(inst, fb);
if (dec->wait_key_frame) {
mtk_vdec_debug(inst->ctx, "wait key frame !");
return 0;
}
goto error;
}
if (dec->resolution_changed) {
mtk_vdec_debug(inst->ctx, "- resolution_changed -");
*res_chg = true;
add_fb_to_free_list(inst, fb);
return 0;
}
/* wait decoder done interrupt */
mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
WAIT_INTR_TIMEOUT_MS, 0);
if (inst->vsi->load_data)
load_dec_table(inst);
vp8_dec_finish(inst);
read_hw_segmentation_data(inst);
err = vpu_dec_end(vpu);
if (err)
goto error;
mtk_vdec_debug(inst->ctx, "\n - FRAME[%d] - show=%d\n", inst->frm_cnt, dec->show_frame);
inst->frm_cnt++;
*res_chg = false;
return 0;
error:
mtk_vdec_err(inst->ctx, "\n - FRAME[%d] - err=%d\n", inst->frm_cnt, err);
return err;
}
static void get_disp_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
{
struct vdec_fb_node *node;
struct vdec_fb *fb;
node = list_first_entry_or_null(&inst->fb_disp_list,
struct vdec_fb_node, list);
if (node) {
list_move_tail(&node->list, &inst->available_fb_node_list);
fb = (struct vdec_fb *)node->fb;
fb->status |= FB_ST_DISPLAY;
mtk_vdec_debug(inst->ctx, "[FB] get disp fb %p st=%d", node->fb, fb->status);
} else {
fb = NULL;
mtk_vdec_debug(inst->ctx, "[FB] there is no disp fb");
}
*out_fb = fb;
}
static void get_free_fb(struct vdec_vp8_inst *inst, struct vdec_fb **out_fb)
{
struct vdec_fb_node *node;
struct vdec_fb *fb;
node = list_first_entry_or_null(&inst->fb_free_list,
struct vdec_fb_node, list);
if (node) {
list_move_tail(&node->list, &inst->available_fb_node_list);
fb = (struct vdec_fb *)node->fb;
fb->status |= FB_ST_FREE;
mtk_vdec_debug(inst->ctx, "[FB] get free fb %p st=%d", node->fb, fb->status);
} else {
fb = NULL;
mtk_vdec_debug(inst->ctx, "[FB] there is no free fb");
}
*out_fb = fb;
}
static void get_crop_info(struct vdec_vp8_inst *inst, struct v4l2_rect *cr)
{
cr->left = 0;
cr->top = 0;
cr->width = inst->vsi->pic.pic_w;
cr->height = inst->vsi->pic.pic_h;
mtk_vdec_debug(inst->ctx, "get crop info l=%d, t=%d, w=%d, h=%d",
cr->left, cr->top, cr->width, cr->height);
}
static int vdec_vp8_get_param(void *h_vdec, enum vdec_get_param_type type,
void *out)
{
struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
switch (type) {
case GET_PARAM_DISP_FRAME_BUFFER:
get_disp_fb(inst, out);
break;
case GET_PARAM_FREE_FRAME_BUFFER:
get_free_fb(inst, out);
break;
case GET_PARAM_PIC_INFO:
get_pic_info(inst, out);
break;
case GET_PARAM_CROP_INFO:
get_crop_info(inst, out);
break;
case GET_PARAM_DPB_SIZE:
*((unsigned int *)out) = VP8_DPB_SIZE;
break;
default:
mtk_vdec_err(inst->ctx, "invalid get parameter type=%d", type);
return -EINVAL;
}
return 0;
}
static void vdec_vp8_deinit(void *h_vdec)
{
struct vdec_vp8_inst *inst = (struct vdec_vp8_inst *)h_vdec;
vpu_dec_deinit(&inst->vpu);
free_working_buf(inst);
kfree(inst);
}
const struct vdec_common_if vdec_vp8_if = {
.init = vdec_vp8_init,
.decode = vdec_vp8_decode,
.get_param = vdec_vp8_get_param,
.deinit = vdec_vp8_deinit,
};
@@ -0,0 +1,436 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2021 MediaTek Inc.
* Author: Yunfei Dong <yunfei.dong@mediatek.com>
*/
#include <linux/slab.h>
#include <media/v4l2-mem2mem.h>
#include <media/videobuf2-dma-contig.h>
#include <uapi/linux/v4l2-controls.h>
#include "../mtk_vcodec_dec.h"
#include "../../common/mtk_vcodec_intr.h"
#include "../vdec_drv_base.h"
#include "../vdec_drv_if.h"
#include "../vdec_vpu_if.h"
/* Decoding picture buffer size (3 reference frames plus current frame) */
#define VP8_DPB_SIZE 4
/* HW working buffer size (bytes) */
#define VP8_SEG_ID_SZ SZ_256K
#define VP8_PP_WRAPY_SZ SZ_64K
#define VP8_PP_WRAPC_SZ SZ_64K
#define VP8_VLD_PRED_SZ SZ_64K
/**
* struct vdec_vp8_slice_info - decode misc information
*
* @vld_wrapper_dma: vld wrapper dma address
* @seg_id_buf_dma: seg id dma address
* @wrap_y_dma: wrap y dma address
* @wrap_c_dma: wrap y dma address
* @cur_y_fb_dma: current plane Y frame buffer dma address
* @cur_c_fb_dma: current plane C frame buffer dma address
* @bs_dma: bitstream dma address
* @bs_sz: bitstream size
* @resolution_changed:resolution change flag 1 - changed, 0 - not changed
* @frame_header_type: current frame header type
* @crc: used to check whether hardware's status is right
* @reserved: reserved, currently unused
*/
struct vdec_vp8_slice_info {
u64 vld_wrapper_dma;
u64 seg_id_buf_dma;
u64 wrap_y_dma;
u64 wrap_c_dma;
u64 cur_y_fb_dma;
u64 cur_c_fb_dma;
u64 bs_dma;
u32 bs_sz;
u32 resolution_changed;
u32 frame_header_type;
u32 crc[8];
u32 reserved;
};
/**
* struct vdec_vp8_slice_dpb_info - vp8 reference information
*
* @y_dma_addr: Y bitstream physical address
* @c_dma_addr: CbCr bitstream physical address
* @reference_flag: reference picture flag
* @reserved: 64bit align
*/
struct vdec_vp8_slice_dpb_info {
dma_addr_t y_dma_addr;
dma_addr_t c_dma_addr;
int reference_flag;
int reserved;
};
/**
* struct vdec_vp8_slice_vsi - VPU shared information
*
* @dec: decoding information
* @pic: picture information
* @vp8_dpb_info: reference buffer information
*/
struct vdec_vp8_slice_vsi {
struct vdec_vp8_slice_info dec;
struct vdec_pic_info pic;
struct vdec_vp8_slice_dpb_info vp8_dpb_info[3];
};
/**
* struct vdec_vp8_slice_inst - VP8 decoder instance
*
* @seg_id_buf: seg buffer
* @wrap_y_buf: wrapper y buffer
* @wrap_c_buf: wrapper c buffer
* @vld_wrapper_buf: vld wrapper buffer
* @ctx: V4L2 context
* @vpu: VPU instance for decoder
* @vsi: VPU share information
*/
struct vdec_vp8_slice_inst {
struct mtk_vcodec_mem seg_id_buf;
struct mtk_vcodec_mem wrap_y_buf;
struct mtk_vcodec_mem wrap_c_buf;
struct mtk_vcodec_mem vld_wrapper_buf;
struct mtk_vcodec_dec_ctx *ctx;
struct vdec_vpu_inst vpu;
struct vdec_vp8_slice_vsi *vsi;
};
static void *vdec_vp8_slice_get_ctrl_ptr(struct mtk_vcodec_dec_ctx *ctx, int id)
{
struct v4l2_ctrl *ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, id);
if (!ctrl)
return ERR_PTR(-EINVAL);
return ctrl->p_cur.p;
}
static void vdec_vp8_slice_get_pic_info(struct vdec_vp8_slice_inst *inst)
{
struct mtk_vcodec_dec_ctx *ctx = inst->ctx;
unsigned int data[3];
data[0] = ctx->picinfo.pic_w;
data[1] = ctx->picinfo.pic_h;
data[2] = ctx->capture_fourcc;
vpu_dec_get_param(&inst->vpu, data, 3, GET_PARAM_PIC_INFO);
ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, 64);
ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, 64);
ctx->picinfo.fb_sz[0] = inst->vpu.fb_sz[0];
ctx->picinfo.fb_sz[1] = inst->vpu.fb_sz[1];
inst->vsi->pic.pic_w = ctx->picinfo.pic_w;
inst->vsi->pic.pic_h = ctx->picinfo.pic_h;
inst->vsi->pic.buf_w = ctx->picinfo.buf_w;
inst->vsi->pic.buf_h = ctx->picinfo.buf_h;
inst->vsi->pic.fb_sz[0] = ctx->picinfo.fb_sz[0];
inst->vsi->pic.fb_sz[1] = ctx->picinfo.fb_sz[1];
mtk_vdec_debug(inst->ctx, "pic(%d, %d), buf(%d, %d)",
ctx->picinfo.pic_w, ctx->picinfo.pic_h,
ctx->picinfo.buf_w, ctx->picinfo.buf_h);
mtk_vdec_debug(inst->ctx, "fb size: Y(%d), C(%d)",
ctx->picinfo.fb_sz[0], ctx->picinfo.fb_sz[1]);
}
static int vdec_vp8_slice_alloc_working_buf(struct vdec_vp8_slice_inst *inst)
{
int err;
struct mtk_vcodec_mem *mem;
mem = &inst->seg_id_buf;
mem->size = VP8_SEG_ID_SZ;
err = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (err) {
mtk_vdec_err(inst->ctx, "Cannot allocate working buffer");
return err;
}
inst->vsi->dec.seg_id_buf_dma = (u64)mem->dma_addr;
mem = &inst->wrap_y_buf;
mem->size = VP8_PP_WRAPY_SZ;
err = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (err) {
mtk_vdec_err(inst->ctx, "cannot allocate WRAP Y buffer");
return err;
}
inst->vsi->dec.wrap_y_dma = (u64)mem->dma_addr;
mem = &inst->wrap_c_buf;
mem->size = VP8_PP_WRAPC_SZ;
err = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (err) {
mtk_vdec_err(inst->ctx, "cannot allocate WRAP C buffer");
return err;
}
inst->vsi->dec.wrap_c_dma = (u64)mem->dma_addr;
mem = &inst->vld_wrapper_buf;
mem->size = VP8_VLD_PRED_SZ;
err = mtk_vcodec_mem_alloc(inst->ctx, mem);
if (err) {
mtk_vdec_err(inst->ctx, "cannot allocate vld wrapper buffer");
return err;
}
inst->vsi->dec.vld_wrapper_dma = (u64)mem->dma_addr;
return 0;
}
static void vdec_vp8_slice_free_working_buf(struct vdec_vp8_slice_inst *inst)
{
struct mtk_vcodec_mem *mem;
mem = &inst->seg_id_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
inst->vsi->dec.seg_id_buf_dma = 0;
mem = &inst->wrap_y_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
inst->vsi->dec.wrap_y_dma = 0;
mem = &inst->wrap_c_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
inst->vsi->dec.wrap_c_dma = 0;
mem = &inst->vld_wrapper_buf;
if (mem->va)
mtk_vcodec_mem_free(inst->ctx, mem);
inst->vsi->dec.vld_wrapper_dma = 0;
}
static u64 vdec_vp8_slice_get_ref_by_ts(const struct v4l2_ctrl_vp8_frame *frame_header,
int index)
{
switch (index) {
case 0:
return frame_header->last_frame_ts;
case 1:
return frame_header->golden_frame_ts;
case 2:
return frame_header->alt_frame_ts;
default:
break;
}
return -1;
}
static int vdec_vp8_slice_get_decode_parameters(struct vdec_vp8_slice_inst *inst)
{
const struct v4l2_ctrl_vp8_frame *frame_header;
struct mtk_vcodec_dec_ctx *ctx = inst->ctx;
struct vb2_queue *vq;
struct vb2_buffer *vb;
u64 referenct_ts;
int index;
frame_header = vdec_vp8_slice_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_VP8_FRAME);
if (IS_ERR(frame_header))
return PTR_ERR(frame_header);
vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
for (index = 0; index < 3; index++) {
referenct_ts = vdec_vp8_slice_get_ref_by_ts(frame_header, index);
vb = vb2_find_buffer(vq, referenct_ts);
if (!vb) {
if (!V4L2_VP8_FRAME_IS_KEY_FRAME(frame_header))
mtk_vdec_err(inst->ctx, "reference invalid: index(%d) ts(%lld)",
index, referenct_ts);
inst->vsi->vp8_dpb_info[index].reference_flag = 0;
continue;
}
inst->vsi->vp8_dpb_info[index].reference_flag = 1;
inst->vsi->vp8_dpb_info[index].y_dma_addr =
vb2_dma_contig_plane_dma_addr(vb, 0);
if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
inst->vsi->vp8_dpb_info[index].c_dma_addr =
vb2_dma_contig_plane_dma_addr(vb, 1);
else
inst->vsi->vp8_dpb_info[index].c_dma_addr =
inst->vsi->vp8_dpb_info[index].y_dma_addr +
ctx->picinfo.fb_sz[0];
}
inst->vsi->dec.frame_header_type = frame_header->flags >> 1;
return 0;
}
static int vdec_vp8_slice_init(struct mtk_vcodec_dec_ctx *ctx)
{
struct vdec_vp8_slice_inst *inst;
int err;
inst = kzalloc_obj(*inst);
if (!inst)
return -ENOMEM;
inst->ctx = ctx;
inst->vpu.id = SCP_IPI_VDEC_LAT;
inst->vpu.core_id = SCP_IPI_VDEC_CORE;
inst->vpu.ctx = ctx;
inst->vpu.codec_type = ctx->current_codec;
inst->vpu.capture_type = ctx->capture_fourcc;
err = vpu_dec_init(&inst->vpu);
if (err) {
mtk_vdec_err(ctx, "vdec_vp8 init err=%d", err);
goto error_free_inst;
}
inst->vsi = inst->vpu.vsi;
err = vdec_vp8_slice_alloc_working_buf(inst);
if (err)
goto error_deinit;
mtk_vdec_debug(ctx, "vp8 struct size = %d vsi: %d\n",
(int)sizeof(struct v4l2_ctrl_vp8_frame),
(int)sizeof(struct vdec_vp8_slice_vsi));
mtk_vdec_debug(ctx, "vp8:%p, codec_type = 0x%x vsi: 0x%p",
inst, inst->vpu.codec_type, inst->vpu.vsi);
ctx->drv_handle = inst;
return 0;
error_deinit:
vpu_dec_deinit(&inst->vpu);
error_free_inst:
kfree(inst);
return err;
}
static int vdec_vp8_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
struct vdec_fb *fb, bool *res_chg)
{
struct vdec_vp8_slice_inst *inst = h_vdec;
struct vdec_vpu_inst *vpu = &inst->vpu;
struct mtk_video_dec_buf *src_buf_info, *dst_buf_info;
unsigned int data;
u64 y_fb_dma, c_fb_dma;
int err, timeout;
/* Resolution changes are never initiated by us */
*res_chg = false;
/* bs NULL means flush decoder */
if (!bs)
return vpu_dec_reset(vpu);
src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
fb = inst->ctx->dev->vdec_pdata->get_cap_buffer(inst->ctx);
if (!fb) {
mtk_vdec_err(inst->ctx, "fb buffer is NULL");
return -ENOMEM;
}
dst_buf_info = container_of(fb, struct mtk_video_dec_buf, frame_buffer);
y_fb_dma = fb->base_y.dma_addr;
if (inst->ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1)
c_fb_dma = y_fb_dma +
inst->ctx->picinfo.buf_w * inst->ctx->picinfo.buf_h;
else
c_fb_dma = fb->base_c.dma_addr;
inst->vsi->dec.bs_dma = (u64)bs->dma_addr;
inst->vsi->dec.bs_sz = bs->size;
inst->vsi->dec.cur_y_fb_dma = y_fb_dma;
inst->vsi->dec.cur_c_fb_dma = c_fb_dma;
mtk_vdec_debug(inst->ctx, "frame[%d] bs(%zu 0x%llx) y/c(0x%llx 0x%llx)",
inst->ctx->decoded_frame_cnt,
bs->size, (u64)bs->dma_addr,
y_fb_dma, c_fb_dma);
v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb,
&dst_buf_info->m2m_buf.vb);
err = vdec_vp8_slice_get_decode_parameters(inst);
if (err)
goto error;
err = vpu_dec_start(vpu, &data, 1);
if (err) {
mtk_vdec_debug(inst->ctx, "vp8 dec start err!");
goto error;
}
if (inst->vsi->dec.resolution_changed) {
mtk_vdec_debug(inst->ctx, "- resolution_changed -");
*res_chg = true;
return 0;
}
/* wait decode done interrupt */
timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
50, MTK_VDEC_CORE);
err = vpu_dec_end(vpu);
if (err || timeout)
mtk_vdec_debug(inst->ctx, "vp8 dec error timeout:%d err: %d pic_%d",
timeout, err, inst->ctx->decoded_frame_cnt);
mtk_vdec_debug(inst->ctx, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
inst->ctx->decoded_frame_cnt,
inst->vsi->dec.crc[0], inst->vsi->dec.crc[1],
inst->vsi->dec.crc[2], inst->vsi->dec.crc[3],
inst->vsi->dec.crc[4], inst->vsi->dec.crc[5],
inst->vsi->dec.crc[6], inst->vsi->dec.crc[7]);
inst->ctx->decoded_frame_cnt++;
error:
return err;
}
static int vdec_vp8_slice_get_param(void *h_vdec, enum vdec_get_param_type type, void *out)
{
struct vdec_vp8_slice_inst *inst = h_vdec;
switch (type) {
case GET_PARAM_PIC_INFO:
vdec_vp8_slice_get_pic_info(inst);
break;
case GET_PARAM_CROP_INFO:
mtk_vdec_debug(inst->ctx, "No need to get vp8 crop information.");
break;
case GET_PARAM_DPB_SIZE:
*((unsigned int *)out) = VP8_DPB_SIZE;
break;
default:
mtk_vdec_err(inst->ctx, "invalid get parameter type=%d", type);
return -EINVAL;
}
return 0;
}
static void vdec_vp8_slice_deinit(void *h_vdec)
{
struct vdec_vp8_slice_inst *inst = h_vdec;
vpu_dec_deinit(&inst->vpu);
vdec_vp8_slice_free_working_buf(inst);
kfree(inst);
}
const struct vdec_common_if vdec_vp8_slice_if = {
.init = vdec_vp8_slice_init,
.decode = vdec_vp8_slice_decode,
.get_param = vdec_vp8_slice_get_param,
.deinit = vdec_vp8_slice_deinit,
};
File diff suppressed because it is too large Load Diff