Skip to content

Commit

Permalink
media: hantro: Support color conversion via post-processing
Browse files Browse the repository at this point in the history
The Hantro G1 decoder is able to enable a post-processor
on the decoding pipeline, which can be used to perform
scaling and color conversion.

The post-processor is integrated to the decoder, and it's
possible to use it in a way that is completely transparent
to the user.

This commit enables color conversion via post-processing,
which means the driver now exposes YUV packed, in addition to NV12.

Signed-off-by: Ezequiel Garcia <[email protected]>
Reviewed-by: Boris Brezillon <[email protected]>
Signed-off-by: Hans Verkuil <[email protected]>
Signed-off-by: Mauro Carvalho Chehab <[email protected]>
  • Loading branch information
ezequielgarcia authored and mchehab committed Dec 16, 2019
1 parent 3143f8a commit 8c2d66b
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 11 deletions.
1 change: 1 addition & 0 deletions drivers/staging/media/hantro/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ obj-$(CONFIG_VIDEO_HANTRO) += hantro-vpu.o
hantro-vpu-y += \
hantro_drv.o \
hantro_v4l2.o \
hantro_postproc.o \
hantro_h1_jpeg_enc.o \
hantro_g1_h264_dec.o \
hantro_g1_mpeg2_dec.o \
Expand Down
66 changes: 62 additions & 4 deletions drivers/staging/media/hantro/hantro.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ struct hantro_irq {
* @num_enc_fmts: Number of encoder formats.
* @dec_fmts: Decoder formats.
* @num_dec_fmts: Number of decoder formats.
* @postproc_fmts: Post-processor formats.
* @num_postproc_fmts: Number of post-processor formats.
* @codec: Supported codecs
* @codec_ops: Codec ops.
* @init: Initialize hardware.
Expand All @@ -70,6 +72,7 @@ struct hantro_irq {
* @num_clocks: number of clocks in the array
* @reg_names: array of register range names
* @num_regs: number of register range names in the array
* @postproc_regs: &struct hantro_postproc_regs pointer
*/
struct hantro_variant {
unsigned int enc_offset;
Expand All @@ -78,6 +81,8 @@ struct hantro_variant {
unsigned int num_enc_fmts;
const struct hantro_fmt *dec_fmts;
unsigned int num_dec_fmts;
const struct hantro_fmt *postproc_fmts;
unsigned int num_postproc_fmts;
unsigned int codec;
const struct hantro_codec_ops *codec_ops;
int (*init)(struct hantro_dev *vpu);
Expand All @@ -88,6 +93,7 @@ struct hantro_variant {
int num_clocks;
const char * const *reg_names;
int num_regs;
const struct hantro_postproc_regs *postproc_regs;
};

/**
Expand Down Expand Up @@ -213,6 +219,7 @@ struct hantro_dev {
* context, and it's called right before
* calling v4l2_m2m_job_finish.
* @codec_ops: Set of operations related to codec mode.
* @postproc: Post-processing context.
* @jpeg_enc: JPEG-encoding context.
* @mpeg2_dec: MPEG-2-decoding context.
* @vp8_dec: VP8-decoding context.
Expand All @@ -237,6 +244,7 @@ struct hantro_ctx {
unsigned int bytesused);

const struct hantro_codec_ops *codec_ops;
struct hantro_postproc_ctx postproc;

/* Specific for particular codec modes. */
union {
Expand Down Expand Up @@ -274,6 +282,23 @@ struct hantro_reg {
u32 mask;
};

struct hantro_postproc_regs {
struct hantro_reg pipeline_en;
struct hantro_reg max_burst;
struct hantro_reg clk_gate;
struct hantro_reg out_swap32;
struct hantro_reg out_endian;
struct hantro_reg out_luma_base;
struct hantro_reg input_width;
struct hantro_reg input_height;
struct hantro_reg output_width;
struct hantro_reg output_height;
struct hantro_reg input_fmt;
struct hantro_reg output_fmt;
struct hantro_reg orig_width;
struct hantro_reg display_width;
};

/* Logging helpers */

/**
Expand Down Expand Up @@ -352,16 +377,30 @@ static inline u32 vdpu_read(struct hantro_dev *vpu, u32 reg)
return val;
}

static inline void hantro_reg_write(struct hantro_dev *vpu,
const struct hantro_reg *reg,
u32 val)
static inline u32 vdpu_read_mask(struct hantro_dev *vpu,
const struct hantro_reg *reg,
u32 val)
{
u32 v;

v = vdpu_read(vpu, reg->base);
v &= ~(reg->mask << reg->shift);
v |= ((val & reg->mask) << reg->shift);
vdpu_write_relaxed(vpu, v, reg->base);
return v;
}

static inline void hantro_reg_write(struct hantro_dev *vpu,
const struct hantro_reg *reg,
u32 val)
{
vdpu_write_relaxed(vpu, vdpu_read_mask(vpu, reg, val), reg->base);
}

static inline void hantro_reg_write_s(struct hantro_dev *vpu,
const struct hantro_reg *reg,
u32 val)
{
vdpu_write(vpu, vdpu_read_mask(vpu, reg, val), reg->base);
}

bool hantro_is_encoder_ctx(const struct hantro_ctx *ctx);
Expand All @@ -381,4 +420,23 @@ hantro_get_dst_buf(struct hantro_ctx *ctx)
return v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
}

static inline bool
hantro_needs_postproc(struct hantro_ctx *ctx, const struct hantro_fmt *fmt)
{
return fmt->fourcc != V4L2_PIX_FMT_NV12;
}

static inline dma_addr_t
hantro_get_dec_buf_addr(struct hantro_ctx *ctx, struct vb2_buffer *vb)
{
if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt))
return ctx->postproc.dec_q[vb->index].dma;
return vb2_dma_contig_plane_dma_addr(vb, 0);
}

void hantro_postproc_disable(struct hantro_ctx *ctx);
void hantro_postproc_enable(struct hantro_ctx *ctx);
void hantro_postproc_free(struct hantro_ctx *ctx);
int hantro_postproc_alloc(struct hantro_ctx *ctx);

#endif /* HANTRO_H_ */
7 changes: 6 additions & 1 deletion drivers/staging/media/hantro/hantro_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts)
if (index < 0)
return 0;
buf = vb2_get_buffer(q, index);
return vb2_dma_contig_plane_dma_addr(buf, 0);
return hantro_get_dec_buf_addr(ctx, buf);
}

static int
Expand Down Expand Up @@ -159,6 +159,11 @@ void hantro_start_prepare_run(struct hantro_ctx *ctx)
src_buf = hantro_get_src_buf(ctx);
v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
&ctx->ctrl_handler);

if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt))
hantro_postproc_enable(ctx);
else
hantro_postproc_disable(ctx);
}

void hantro_end_prepare_run(struct hantro_ctx *ctx)
Expand Down
2 changes: 1 addition & 1 deletion drivers/staging/media/hantro/hantro_g1_h264_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static void set_buffers(struct hantro_ctx *ctx)
vdpu_write_relaxed(vpu, src_dma, G1_REG_ADDR_STR);

/* Destination (decoded frame) buffer. */
dst_dma = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
dst_dma = hantro_get_dec_buf_addr(ctx, &dst_buf->vb2_buf);
/* Adjust dma addr to start at second line for bottom field */
if (ctrls->slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)
offset = ALIGN(ctx->src_fmt.width, MB_DIM);
Expand Down
2 changes: 1 addition & 1 deletion drivers/staging/media/hantro/hantro_g1_mpeg2_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ hantro_g1_mpeg2_dec_set_buffers(struct hantro_dev *vpu, struct hantro_ctx *ctx,
vdpu_write_relaxed(vpu, addr, G1_REG_RLC_VLC_BASE);

/* Destination frame buffer */
addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
addr = hantro_get_dec_buf_addr(ctx, dst_buf);
current_addr = addr;

if (picture->picture_structure == PICT_BOTTOM_FIELD)
Expand Down
53 changes: 53 additions & 0 deletions drivers/staging/media/hantro/hantro_g1_regs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef HANTRO_G1_REGS_H_
#define HANTRO_G1_REGS_H_

#define G1_SWREG(nr) ((nr) * 4)

/* Decoder registers. */
#define G1_REG_INTERRUPT 0x004
#define G1_REG_INTERRUPT_DEC_PIC_INF BIT(24)
Expand Down Expand Up @@ -298,4 +300,55 @@
#define G1_REG_REF_BUF_CTRL2_APF_THRESHOLD(x) (((x) & 0x3fff) << 0)
#define G1_REG_SOFT_RESET 0x194

/* Post-processor registers. */
#define G1_REG_PP_INTERRUPT G1_SWREG(60)
#define G1_REG_PP_READY_IRQ BIT(12)
#define G1_REG_PP_IRQ BIT(8)
#define G1_REG_PP_IRQ_DIS BIT(4)
#define G1_REG_PP_PIPELINE_EN BIT(1)
#define G1_REG_PP_EXTERNAL_TRIGGER BIT(0)
#define G1_REG_PP_DEV_CONFIG G1_SWREG(61)
#define G1_REG_PP_AXI_RD_ID(v) (((v) << 24) & GENMASK(31, 24))
#define G1_REG_PP_AXI_WR_ID(v) (((v) << 16) & GENMASK(23, 16))
#define G1_REG_PP_INSWAP32_E(v) ((v) ? BIT(10) : 0)
#define G1_REG_PP_DATA_DISC_E(v) ((v) ? BIT(9) : 0)
#define G1_REG_PP_CLK_GATE_E(v) ((v) ? BIT(8) : 0)
#define G1_REG_PP_IN_ENDIAN(v) ((v) ? BIT(7) : 0)
#define G1_REG_PP_OUT_ENDIAN(v) ((v) ? BIT(6) : 0)
#define G1_REG_PP_OUTSWAP32_E(v) ((v) ? BIT(5) : 0)
#define G1_REG_PP_MAX_BURST(v) (((v) << 0) & GENMASK(4, 0))
#define G1_REG_PP_IN_LUMA_BASE G1_SWREG(63)
#define G1_REG_PP_IN_CB_BASE G1_SWREG(64)
#define G1_REG_PP_IN_CR_BASE G1_SWREG(65)
#define G1_REG_PP_OUT_LUMA_BASE G1_SWREG(66)
#define G1_REG_PP_OUT_CHROMA_BASE G1_SWREG(67)
#define G1_REG_PP_CONTRAST_ADJUST G1_SWREG(68)
#define G1_REG_PP_COLOR_CONVERSION G1_SWREG(69)
#define G1_REG_PP_COLOR_CONVERSION0 G1_SWREG(70)
#define G1_REG_PP_COLOR_CONVERSION1 G1_SWREG(71)
#define G1_REG_PP_INPUT_SIZE G1_SWREG(72)
#define G1_REG_PP_INPUT_SIZE_HEIGHT(v) (((v) << 9) & GENMASK(16, 9))
#define G1_REG_PP_INPUT_SIZE_WIDTH(v) (((v) << 0) & GENMASK(8, 0))
#define G1_REG_PP_SCALING0 G1_SWREG(79)
#define G1_REG_PP_PADD_R(v) (((v) << 23) & GENMASK(27, 23))
#define G1_REG_PP_PADD_G(v) (((v) << 18) & GENMASK(22, 18))
#define G1_REG_PP_RANGEMAP_Y(v) ((v) ? BIT(31) : 0)
#define G1_REG_PP_RANGEMAP_C(v) ((v) ? BIT(30) : 0)
#define G1_REG_PP_YCBCR_RANGE(v) ((v) ? BIT(29) : 0)
#define G1_REG_PP_RGB_16(v) ((v) ? BIT(28) : 0)
#define G1_REG_PP_SCALING1 G1_SWREG(80)
#define G1_REG_PP_PADD_B(v) (((v) << 18) & GENMASK(22, 18))
#define G1_REG_PP_MASK_R G1_SWREG(82)
#define G1_REG_PP_MASK_G G1_SWREG(83)
#define G1_REG_PP_MASK_B G1_SWREG(84)
#define G1_REG_PP_CONTROL G1_SWREG(85)
#define G1_REG_PP_CONTROL_IN_FMT(v) (((v) << 29) & GENMASK(31, 29))
#define G1_REG_PP_CONTROL_OUT_FMT(v) (((v) << 26) & GENMASK(28, 26))
#define G1_REG_PP_CONTROL_OUT_HEIGHT(v) (((v) << 15) & GENMASK(25, 15))
#define G1_REG_PP_CONTROL_OUT_WIDTH(v) (((v) << 4) & GENMASK(14, 4))
#define G1_REG_PP_MASK1_ORIG_WIDTH G1_SWREG(88)
#define G1_REG_PP_ORIG_WIDTH(v) (((v) << 23) & GENMASK(31, 23))
#define G1_REG_PP_DISPLAY_WIDTH G1_SWREG(92)
#define G1_REG_PP_FUSE G1_SWREG(99)

#endif /* HANTRO_G1_REGS_H_ */
2 changes: 1 addition & 1 deletion drivers/staging/media/hantro/hantro_g1_vp8_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ static void cfg_buffers(struct hantro_ctx *ctx,
}
vdpu_write_relaxed(vpu, reg, G1_REG_FWD_PIC(0));

dst_dma = vb2_dma_contig_plane_dma_addr(&vb2_dst->vb2_buf, 0);
dst_dma = hantro_get_dec_buf_addr(ctx, &vb2_dst->vb2_buf);
vdpu_write_relaxed(vpu, dst_dma, G1_REG_ADDR_DST);
}

Expand Down
13 changes: 13 additions & 0 deletions drivers/staging/media/hantro/hantro_hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ struct hantro_variant;
* @cpu: CPU pointer to the buffer.
* @dma: DMA address of the buffer.
* @size: Size of the buffer.
* @attrs: Attributes of the DMA mapping.
*/
struct hantro_aux_buf {
void *cpu;
dma_addr_t dma;
size_t size;
unsigned long attrs;
};

/**
Expand Down Expand Up @@ -106,6 +108,15 @@ struct hantro_vp8_dec_hw_ctx {
struct hantro_aux_buf prob_tbl;
};

/**
* struct hantro_postproc_ctx
*
* @dec_q: References buffers, in decoder format.
*/
struct hantro_postproc_ctx {
struct hantro_aux_buf dec_q[VB2_MAX_FRAME];
};

/**
* struct hantro_codec_ops - codec mode specific operations
*
Expand Down Expand Up @@ -141,6 +152,8 @@ extern const struct hantro_variant rk3399_vpu_variant;
extern const struct hantro_variant rk3328_vpu_variant;
extern const struct hantro_variant rk3288_vpu_variant;

extern const struct hantro_postproc_regs hantro_g1_postproc_regs;

extern const u32 hantro_vp8_dec_mc_filter[8][6];

void hantro_watchdog(struct work_struct *work);
Expand Down
Loading

0 comments on commit 8c2d66b

Please sign in to comment.