Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions drivers/media/platform/samsung/s5p-mfc/s5p_mfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,18 @@ static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx)

ctx->state = MFCINST_FINISHED;

/* Called from the interrupt handler, under dev->irqlock */
if (!list_empty(&ctx->dst_queue)) {
mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
list);
list_del(&mb_entry->list);
ctx->dst_queue_cnt--;
vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, 0);
/* Tell the client that the drain is done */
mb_entry->b->flags |= V4L2_BUF_FLAG_LAST;
vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
} else {
ctx->last_pending = true;
}

clear_work_bit(ctx);
Expand Down Expand Up @@ -1470,18 +1475,6 @@ static int s5p_mfc_probe(struct platform_device *pdev)
v4l2_info(&dev->v4l2_dev,
"decoder registered as /dev/video%d\n", dev->vfd_dec->num);

/*
* The v15 encoder's registers differ from v12's (0xF770/0xF774 clash
* with the decoder's) and nothing here programs them yet: no encoder
* node rather than one that hangs the firmware.
*/
if (IS_MFCV15(dev)) {
video_device_release(dev->vfd_enc);
dev->vfd_enc = NULL;
pr_debug("%s--\n", __func__);
return 0;
}

ret = video_register_device(dev->vfd_enc, VFL_TYPE_VIDEO, 0);
if (ret) {
v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
Expand Down
3 changes: 3 additions & 0 deletions drivers/media/platform/samsung/s5p-mfc/s5p_mfc_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ struct s5p_mfc_codec_ops {
* @dst_queue_cnt: number of buffers queued on the dest internal queue
* @type: type of the instance - decoder or encoder
* @state: state of the context
* @last_pending: the encoder drained with no capture buffer queued: the
* next one is returned empty with V4L2_BUF_FLAG_LAST
* @inst_no: number of hw instance associated with the context
* @img_width: width of the image that is decoded or encoded
* @img_height: height of the image that is decoded or encoded
Expand Down Expand Up @@ -662,6 +664,7 @@ struct s5p_mfc_ctx {

enum s5p_mfc_inst_type type;
enum s5p_mfc_inst_state state;
bool last_pending;
int inst_no;

/* Image parameters */
Expand Down
138 changes: 112 additions & 26 deletions drivers/media/platform/samsung/s5p-mfc/s5p_mfc_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ static const struct s5p_mfc_fmt formats[] = {
.codec_mode = S5P_MFC_CODEC_NONE,
.type = MFC_FMT_RAW,
.num_planes = 2,
.versions = MFC_V5PLUS_BITS,
.versions = MFC_V5PLUS_BITS & ~MFC_V15_BIT,
},
{
.fourcc = V4L2_PIX_FMT_NV21M,
.codec_mode = S5P_MFC_CODEC_NONE,
.type = MFC_FMT_RAW,
.num_planes = 2,
.versions = MFC_V6PLUS_BITS,
.versions = MFC_V6PLUS_BITS & ~MFC_V15_BIT,
},
{
.fourcc = V4L2_PIX_FMT_YUV420M,
Expand All @@ -73,6 +73,13 @@ static const struct s5p_mfc_fmt formats[] = {
.num_planes = 3,
.versions = MFC_V12_BIT,
},
{
.fourcc = V4L2_PIX_FMT_NV12,
.codec_mode = S5P_MFC_CODEC_NONE,
.type = MFC_FMT_RAW,
.num_planes = 1,
.versions = MFC_V15_BIT,
},
{
.fourcc = V4L2_PIX_FMT_H264,
.codec_mode = S5P_MFC_CODEC_H264_ENC,
Expand All @@ -85,14 +92,14 @@ static const struct s5p_mfc_fmt formats[] = {
.codec_mode = S5P_MFC_CODEC_MPEG4_ENC,
.type = MFC_FMT_ENC,
.num_planes = 1,
.versions = MFC_V5PLUS_BITS,
.versions = MFC_V5PLUS_BITS & ~MFC_V15_BIT,
},
{
.fourcc = V4L2_PIX_FMT_H263,
.codec_mode = S5P_MFC_CODEC_H263_ENC,
.type = MFC_FMT_ENC,
.num_planes = 1,
.versions = MFC_V5PLUS_BITS,
.versions = MFC_V5PLUS_BITS & ~MFC_V15_BIT,
},
{
.fourcc = V4L2_PIX_FMT_VP8,
Expand Down Expand Up @@ -747,7 +754,7 @@ static struct mfc_control controls[] = {
.minimum = 0,
.maximum = 51,
.step = 1,
.default_value = 0,
.default_value = 51,
},
{
.id = V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
Expand Down Expand Up @@ -1165,7 +1172,13 @@ static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
struct s5p_mfc_enc_params *p = &ctx->enc_params;
struct s5p_mfc_buf *dst_mb;

if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
/*
* VP8 on v15 has no header (no IVF), so the stream size reads 0 here:
* an empty buffer would look like the end of the stream.
*/
if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE &&
(!IS_MFCV15(dev) ||
s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev))) {
if (!list_empty(&ctx->dst_queue)) {
dst_mb = list_entry(ctx->dst_queue.next,
struct s5p_mfc_buf, list);
Expand Down Expand Up @@ -1198,6 +1211,30 @@ static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
return 0;
}

/*
* Bytes of a one-buffer NV12 source: both planes, then room for the rows
* the MFC reads past the chroma (its height rounded up to 16) and the
* 256 bytes it wants after a plane.
*/
static unsigned int enc_nv12_size(struct s5p_mfc_ctx *ctx)
{
return ctx->luma_size + ctx->chroma_size + 8 * ctx->stride[1] +
MFC_CHROMA_PAD_BYTES_V12;
}

/*
* Source plane addresses. NV12 in one buffer has its chroma right after
* the luma's bytesperline * height.
*/
static unsigned long enc_src_addr(struct s5p_mfc_ctx *ctx,
struct vb2_buffer *vb, unsigned int plane)
{
if (ctx->src_fmt->num_planes == 1)
return vb2_dma_contig_plane_dma_addr(vb, 0) +
(plane ? ctx->luma_size : 0);
return vb2_dma_contig_plane_dma_addr(vb, plane);
}

static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
{
struct s5p_mfc_dev *dev = ctx->dev;
Expand All @@ -1207,8 +1244,8 @@ static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
unsigned int dst_size;

src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
src_y_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 0);
src_c_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 1);
src_y_addr = enc_src_addr(ctx, &src_mb->b->vb2_buf, 0);
src_c_addr = enc_src_addr(ctx, &src_mb->b->vb2_buf, 1);
if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_YUV420M || ctx->src_fmt->fourcc ==
V4L2_PIX_FMT_YVU420M)
src_c_1_addr =
Expand Down Expand Up @@ -1236,6 +1273,7 @@ static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
int slice_type;
unsigned int strm_size;
bool src_ready;
u64 timestamp = 0;

slice_type = s5p_mfc_hw_call(dev->mfc_ops, get_enc_slice_type, dev);
strm_size = s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev);
Expand All @@ -1247,10 +1285,8 @@ static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
s5p_mfc_hw_call(dev->mfc_ops, get_enc_frame_buffer, ctx,
&enc_y_addr, &enc_c_addr, &enc_c_1_addr);
list_for_each_entry(mb_entry, &ctx->src_queue, list) {
mb_y_addr = vb2_dma_contig_plane_dma_addr(
&mb_entry->b->vb2_buf, 0);
mb_c_addr = vb2_dma_contig_plane_dma_addr(
&mb_entry->b->vb2_buf, 1);
mb_y_addr = enc_src_addr(ctx, &mb_entry->b->vb2_buf, 0);
mb_c_addr = enc_src_addr(ctx, &mb_entry->b->vb2_buf, 1);
if (ctx->src_fmt->fourcc ==
V4L2_PIX_FMT_YUV420M ||
ctx->src_fmt->fourcc ==
Expand All @@ -1263,16 +1299,15 @@ static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
== mb_c_1_addr) {
list_del(&mb_entry->list);
ctx->src_queue_cnt--;
timestamp = mb_entry->b->vb2_buf.timestamp;
vb2_buffer_done(&mb_entry->b->vb2_buf,
VB2_BUF_STATE_DONE);
break;
}
}
list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
mb_y_addr = vb2_dma_contig_plane_dma_addr(
&mb_entry->b->vb2_buf, 0);
mb_c_addr = vb2_dma_contig_plane_dma_addr(
&mb_entry->b->vb2_buf, 1);
mb_y_addr = enc_src_addr(ctx, &mb_entry->b->vb2_buf, 0);
mb_c_addr = enc_src_addr(ctx, &mb_entry->b->vb2_buf, 1);
if (ctx->src_fmt->fourcc ==
V4L2_PIX_FMT_YUV420M ||
ctx->src_fmt->fourcc == V4L2_PIX_FMT_YVU420M)
Expand All @@ -1284,6 +1319,7 @@ static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
== mb_c_1_addr) {
list_del(&mb_entry->list);
ctx->ref_queue_cnt--;
timestamp = mb_entry->b->vb2_buf.timestamp;
vb2_buffer_done(&mb_entry->b->vb2_buf,
VB2_BUF_STATE_DONE);
break;
Expand Down Expand Up @@ -1319,6 +1355,8 @@ static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
mb_entry->b->flags |= V4L2_BUF_FLAG_BFRAME;
break;
}
/* The frame just encoded is the source buffer found above */
mb_entry->b->vb2_buf.timestamp = timestamp;
vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, strm_size);
vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
}
Expand Down Expand Up @@ -1421,6 +1459,8 @@ static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
pix_fmt_mp->plane_fmt[2].bytesperline = ctx->stride[2];
pix_fmt_mp->plane_fmt[2].sizeimage = ctx->chroma_size_1;
}
if (ctx->src_fmt->num_planes == 1)
pix_fmt_mp->plane_fmt[0].sizeimage = enc_nv12_size(ctx);
} else {
mfc_err("invalid buf type\n");
return -EINVAL;
Expand Down Expand Up @@ -1463,6 +1503,17 @@ static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
else
v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1, &pix_fmt_mp
->height, 4, 1080, 1, 0);
pix_fmt_mp->field = V4L2_FIELD_NONE;
pix_fmt_mp->num_planes = fmt->num_planes;
/* Same layout as enc_calc_src_size() and enc_nv12_size() */
if (fmt->num_planes == 1) {
unsigned int stride = ALIGN(pix_fmt_mp->width, 64);

pix_fmt_mp->plane_fmt[0].bytesperline = stride;
pix_fmt_mp->plane_fmt[0].sizeimage =
stride * pix_fmt_mp->height * 3 / 2 +
8 * stride + MFC_CHROMA_PAD_BYTES_V12;
}
} else {
mfc_err("invalid buf type\n");
return -EINVAL;
Expand All @@ -1480,7 +1531,8 @@ static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
ret = vidioc_try_fmt(file, priv, f);
if (ret)
return ret;
if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ?
ctx->vq_dst.streaming : ctx->vq_src.streaming) {
v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
ret = -EBUSY;
goto out;
Expand All @@ -1490,6 +1542,10 @@ static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
ctx->dst_fmt = find_format(f, MFC_FMT_ENC);
ctx->state = MFCINST_INIT;
ctx->codec_mode = ctx->dst_fmt->codec_mode;
/* A client may leave the bitstream buffer's size to us */
if (!pix_fmt_mp->plane_fmt[0].sizeimage)
pix_fmt_mp->plane_fmt[0].sizeimage =
dev->variant->buf_size->cpb;
ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
pix_fmt_mp->plane_fmt[0].bytesperline = 0;
ctx->dst_bufs_cnt = 0;
Expand All @@ -1515,6 +1571,10 @@ static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
pix_fmt_mp->plane_fmt[2].bytesperline = ctx->stride[2];
pix_fmt_mp->plane_fmt[2].sizeimage = ctx->chroma_size_1;
}
if (ctx->src_fmt->num_planes == 1) {
pix_fmt_mp->num_planes = 1;
pix_fmt_mp->plane_fmt[0].sizeimage = enc_nv12_size(ctx);
}

ctx->src_bufs_cnt = 0;
ctx->output_state = QUEUE_FREE;
Expand Down Expand Up @@ -2287,6 +2347,7 @@ static int vidioc_s_parm(struct file *file, void *priv,
struct s5p_mfc_ctx *ctx = file_to_ctx(file);

if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
ctx->enc_params.rc_framerate_num =
a->parm.output.timeperframe.denominator;
ctx->enc_params.rc_framerate_denom =
Expand All @@ -2303,7 +2364,9 @@ static int vidioc_g_parm(struct file *file, void *priv,
{
struct s5p_mfc_ctx *ctx = file_to_ctx(file);

if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
a->parm.output.timeperframe.denominator =
ctx->enc_params.rc_framerate_num;
a->parm.output.timeperframe.numerator =
Expand Down Expand Up @@ -2450,6 +2513,8 @@ static int s5p_mfc_queue_setup(struct vb2_queue *vq,

psize[0] = ctx->luma_size;
psize[1] = ctx->chroma_size;
if (ctx->src_fmt && ctx->src_fmt->num_planes == 1)
psize[0] = enc_nv12_size(ctx);
if (ctx->src_fmt && (ctx->src_fmt->fourcc ==
V4L2_PIX_FMT_YUV420M || ctx->src_fmt->fourcc ==
V4L2_PIX_FMT_YVU420M))
Expand Down Expand Up @@ -2496,10 +2561,8 @@ static int s5p_mfc_buf_init(struct vb2_buffer *vb)
return ret;
i = vb->index;
ctx->src_bufs[i].b = vbuf;
ctx->src_bufs[i].cookie.raw.luma =
vb2_dma_contig_plane_dma_addr(vb, 0);
ctx->src_bufs[i].cookie.raw.chroma =
vb2_dma_contig_plane_dma_addr(vb, 1);
ctx->src_bufs[i].cookie.raw.luma = enc_src_addr(ctx, vb, 0);
ctx->src_bufs[i].cookie.raw.chroma = enc_src_addr(ctx, vb, 1);
if (ctx->src_fmt->fourcc ==
V4L2_PIX_FMT_YUV420M || ctx->src_fmt->fourcc ==
V4L2_PIX_FMT_YVU420M)
Expand Down Expand Up @@ -2537,7 +2600,9 @@ static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
vb2_plane_size(vb, 0), ctx->luma_size);
mfc_debug(2, "plane size: %ld, chroma size: %d\n",
vb2_plane_size(vb, 1), ctx->chroma_size);
if (vb2_plane_size(vb, 0) < ctx->luma_size ||
if (ctx->src_fmt->num_planes == 1 ?
vb2_plane_size(vb, 0) < enc_nv12_size(ctx) :
vb2_plane_size(vb, 0) < ctx->luma_size ||
vb2_plane_size(vb, 1) < ctx->chroma_size) {
mfc_err("plane size is too small for output\n");
return -EINVAL;
Expand Down Expand Up @@ -2600,6 +2665,7 @@ static void s5p_mfc_stop_streaming(struct vb2_queue *q)
}
ctx->state = MFCINST_FINISHED;
spin_lock_irqsave(&dev->irqlock, flags);
ctx->last_pending = false;
if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
INIT_LIST_HEAD(&ctx->dst_queue);
Expand Down Expand Up @@ -2632,6 +2698,15 @@ static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
/* Mark destination as available for use by MFC */
spin_lock_irqsave(&dev->irqlock, flags);
if (ctx->last_pending) {
/* The drain ended while no buffer was queued */
ctx->last_pending = false;
spin_unlock_irqrestore(&dev->irqlock, flags);
vb2_set_plane_payload(vb, 0, 0);
to_vb2_v4l2_buffer(vb)->flags |= V4L2_BUF_FLAG_LAST;
vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
return;
}
list_add_tail(&mfc_buf->list, &ctx->dst_queue);
ctx->dst_queue_cnt++;
spin_unlock_irqrestore(&dev->irqlock, flags);
Expand Down Expand Up @@ -2713,11 +2788,21 @@ int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
if ((controls[i].type == V4L2_CTRL_TYPE_MENU) ||
(controls[i].type ==
V4L2_CTRL_TYPE_INTEGER_MENU)) {
s32 def = controls[i].default_value;

/*
* On v15 the header comes with the first frame
* by default: GStreamer expects one capture
* buffer per frame and drops the extra one.
*/
if (controls[i].id ==
V4L2_CID_MPEG_VIDEO_HEADER_MODE &&
IS_MFCV15(ctx->dev))
def = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME;
ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
&ctx->ctrl_handler,
&s5p_mfc_enc_ctrl_ops, controls[i].id,
controls[i].maximum, 0,
controls[i].default_value);
controls[i].maximum, 0, def);
} else {
ctx->ctrls[i] = v4l2_ctrl_new_std(
&ctx->ctrl_handler,
Expand Down Expand Up @@ -2750,7 +2835,8 @@ void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
{
struct v4l2_format f;
f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
f.fmt.pix_mp.pixelformat = IS_MFCV15(ctx->dev) ? V4L2_PIX_FMT_NV12 :
DEF_SRC_FMT_ENC;
ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);
Expand Down
Loading