2 * linux/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c
4 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 * Kamil Debski, <k.debski@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/clk.h>
15 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/version.h>
22 #include <linux/videodev2.h>
23 #include <linux/workqueue.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-event.h>
26 #include <media/videobuf2-core.h>
27 #include "s5p_mfc_common.h"
28 #include "s5p_mfc_ctrl.h"
29 #include "s5p_mfc_debug.h"
30 #include "s5p_mfc_dec.h"
31 #include "s5p_mfc_intr.h"
32 #include "s5p_mfc_opr.h"
33 #include "s5p_mfc_pm.h"
35 static struct s5p_mfc_fmt formats
[] = {
37 .name
= "4:2:0 2 Planes 16x16 Tiles",
38 .fourcc
= V4L2_PIX_FMT_NV12MT_16X16
,
39 .codec_mode
= S5P_MFC_CODEC_NONE
,
42 .versions
= MFC_V6_BIT
| MFC_V7_BIT
,
45 .name
= "4:2:0 2 Planes 64x32 Tiles",
46 .fourcc
= V4L2_PIX_FMT_NV12MT
,
47 .codec_mode
= S5P_MFC_CODEC_NONE
,
50 .versions
= MFC_V5_BIT
,
53 .name
= "4:2:0 2 Planes Y/CbCr",
54 .fourcc
= V4L2_PIX_FMT_NV12M
,
55 .codec_mode
= S5P_MFC_CODEC_NONE
,
58 .versions
= MFC_V6_BIT
| MFC_V7_BIT
| MFC_V8_BIT
,
61 .name
= "4:2:0 2 Planes Y/CrCb",
62 .fourcc
= V4L2_PIX_FMT_NV21M
,
63 .codec_mode
= S5P_MFC_CODEC_NONE
,
66 .versions
= MFC_V6_BIT
| MFC_V7_BIT
| MFC_V8_BIT
,
69 .name
= "H264 Encoded Stream",
70 .fourcc
= V4L2_PIX_FMT_H264
,
71 .codec_mode
= S5P_MFC_CODEC_H264_DEC
,
74 .versions
= MFC_V5_BIT
| MFC_V6_BIT
| MFC_V7_BIT
|
78 .name
= "H264/MVC Encoded Stream",
79 .fourcc
= V4L2_PIX_FMT_H264_MVC
,
80 .codec_mode
= S5P_MFC_CODEC_H264_MVC_DEC
,
83 .versions
= MFC_V6_BIT
| MFC_V7_BIT
| MFC_V8_BIT
,
86 .name
= "H263 Encoded Stream",
87 .fourcc
= V4L2_PIX_FMT_H263
,
88 .codec_mode
= S5P_MFC_CODEC_H263_DEC
,
91 .versions
= MFC_V5_BIT
| MFC_V6_BIT
| MFC_V7_BIT
|
95 .name
= "MPEG1 Encoded Stream",
96 .fourcc
= V4L2_PIX_FMT_MPEG1
,
97 .codec_mode
= S5P_MFC_CODEC_MPEG2_DEC
,
100 .versions
= MFC_V5_BIT
| MFC_V6_BIT
| MFC_V7_BIT
|
104 .name
= "MPEG2 Encoded Stream",
105 .fourcc
= V4L2_PIX_FMT_MPEG2
,
106 .codec_mode
= S5P_MFC_CODEC_MPEG2_DEC
,
109 .versions
= MFC_V5_BIT
| MFC_V6_BIT
| MFC_V7_BIT
|
113 .name
= "MPEG4 Encoded Stream",
114 .fourcc
= V4L2_PIX_FMT_MPEG4
,
115 .codec_mode
= S5P_MFC_CODEC_MPEG4_DEC
,
118 .versions
= MFC_V5_BIT
| MFC_V6_BIT
| MFC_V7_BIT
|
122 .name
= "XviD Encoded Stream",
123 .fourcc
= V4L2_PIX_FMT_XVID
,
124 .codec_mode
= S5P_MFC_CODEC_MPEG4_DEC
,
127 .versions
= MFC_V5_BIT
| MFC_V6_BIT
| MFC_V7_BIT
|
131 .name
= "VC1 Encoded Stream",
132 .fourcc
= V4L2_PIX_FMT_VC1_ANNEX_G
,
133 .codec_mode
= S5P_MFC_CODEC_VC1_DEC
,
136 .versions
= MFC_V5_BIT
| MFC_V6_BIT
| MFC_V7_BIT
|
140 .name
= "VC1 RCV Encoded Stream",
141 .fourcc
= V4L2_PIX_FMT_VC1_ANNEX_L
,
142 .codec_mode
= S5P_MFC_CODEC_VC1RCV_DEC
,
145 .versions
= MFC_V5_BIT
| MFC_V6_BIT
| MFC_V7_BIT
|
149 .name
= "VP8 Encoded Stream",
150 .fourcc
= V4L2_PIX_FMT_VP8
,
151 .codec_mode
= S5P_MFC_CODEC_VP8_DEC
,
154 .versions
= MFC_V6_BIT
| MFC_V7_BIT
| MFC_V8_BIT
,
158 #define NUM_FORMATS ARRAY_SIZE(formats)
160 /* Find selected format description */
161 static struct s5p_mfc_fmt
*find_format(struct v4l2_format
*f
, unsigned int t
)
165 for (i
= 0; i
< NUM_FORMATS
; i
++) {
166 if (formats
[i
].fourcc
== f
->fmt
.pix_mp
.pixelformat
&&
167 formats
[i
].type
== t
)
173 static struct mfc_control controls
[] = {
175 .id
= V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY
,
176 .type
= V4L2_CTRL_TYPE_INTEGER
,
177 .name
= "H264 Display Delay",
184 .id
= V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE
,
185 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
186 .name
= "H264 Display Delay Enable",
193 .id
= V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER
,
194 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
195 .name
= "Mpeg4 Loop Filter Enable",
202 .id
= V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE
,
203 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
204 .name
= "Slice Interface Enable",
211 .id
= V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
,
212 .type
= V4L2_CTRL_TYPE_INTEGER
,
213 .name
= "Minimum number of cap bufs",
222 #define NUM_CTRLS ARRAY_SIZE(controls)
224 /* Check whether a context should be run on hardware */
225 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx
*ctx
)
227 /* Context is to parse header */
228 if (ctx
->src_queue_cnt
>= 1 && ctx
->state
== MFCINST_GOT_INST
)
230 /* Context is to decode a frame */
231 if (ctx
->src_queue_cnt
>= 1 &&
232 ctx
->state
== MFCINST_RUNNING
&&
233 ctx
->dst_queue_cnt
>= ctx
->pb_count
)
235 /* Context is to return last frame */
236 if (ctx
->state
== MFCINST_FINISHING
&&
237 ctx
->dst_queue_cnt
>= ctx
->pb_count
)
239 /* Context is to set buffers */
240 if (ctx
->src_queue_cnt
>= 1 &&
241 ctx
->state
== MFCINST_HEAD_PARSED
&&
242 ctx
->capture_state
== QUEUE_BUFS_MMAPED
)
244 /* Resolution change */
245 if ((ctx
->state
== MFCINST_RES_CHANGE_INIT
||
246 ctx
->state
== MFCINST_RES_CHANGE_FLUSH
) &&
247 ctx
->dst_queue_cnt
>= ctx
->pb_count
)
249 if (ctx
->state
== MFCINST_RES_CHANGE_END
&&
250 ctx
->src_queue_cnt
>= 1)
252 mfc_debug(2, "ctx is not ready\n");
256 static struct s5p_mfc_codec_ops decoder_codec_ops
= {
257 .pre_seq_start
= NULL
,
258 .post_seq_start
= NULL
,
259 .pre_frame_start
= NULL
,
260 .post_frame_start
= NULL
,
263 /* Query capabilities of the device */
264 static int vidioc_querycap(struct file
*file
, void *priv
,
265 struct v4l2_capability
*cap
)
267 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
269 strncpy(cap
->driver
, dev
->plat_dev
->name
, sizeof(cap
->driver
) - 1);
270 strncpy(cap
->card
, dev
->plat_dev
->name
, sizeof(cap
->card
) - 1);
271 cap
->bus_info
[0] = 0;
273 * This is only a mem-to-mem video device. The capture and output
274 * device capability flags are left only for backward compatibility
275 * and are scheduled for removal.
277 cap
->device_caps
= V4L2_CAP_VIDEO_M2M_MPLANE
| V4L2_CAP_STREAMING
;
278 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
282 /* Enumerate format */
283 static int vidioc_enum_fmt(struct file
*file
, struct v4l2_fmtdesc
*f
,
286 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
287 struct s5p_mfc_fmt
*fmt
;
290 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
291 if (out
&& formats
[i
].type
!= MFC_FMT_DEC
)
293 else if (!out
&& formats
[i
].type
!= MFC_FMT_RAW
)
295 else if ((dev
->variant
->version_bit
& formats
[i
].versions
) == 0)
302 if (i
== ARRAY_SIZE(formats
))
305 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
306 f
->pixelformat
= fmt
->fourcc
;
310 static int vidioc_enum_fmt_vid_cap_mplane(struct file
*file
, void *pirv
,
311 struct v4l2_fmtdesc
*f
)
313 return vidioc_enum_fmt(file
, f
, false);
316 static int vidioc_enum_fmt_vid_out_mplane(struct file
*file
, void *priv
,
317 struct v4l2_fmtdesc
*f
)
319 return vidioc_enum_fmt(file
, f
, true);
323 static int vidioc_g_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
325 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
326 struct v4l2_pix_format_mplane
*pix_mp
;
329 pix_mp
= &f
->fmt
.pix_mp
;
330 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
&&
331 (ctx
->state
== MFCINST_GOT_INST
|| ctx
->state
==
332 MFCINST_RES_CHANGE_END
)) {
333 /* If the MFC is parsing the header,
334 * so wait until it is finished */
335 s5p_mfc_wait_for_done_ctx(ctx
, S5P_MFC_R2H_CMD_SEQ_DONE_RET
,
338 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
&&
339 ctx
->state
>= MFCINST_HEAD_PARSED
&&
340 ctx
->state
< MFCINST_ABORT
) {
341 /* This is run on CAPTURE (decode output) */
342 /* Width and height are set to the dimensions
343 of the movie, the buffer is bigger and
344 further processing stages should crop to this
346 pix_mp
->width
= ctx
->buf_width
;
347 pix_mp
->height
= ctx
->buf_height
;
348 pix_mp
->field
= V4L2_FIELD_NONE
;
349 pix_mp
->num_planes
= 2;
350 /* Set pixelformat to the format in which MFC
351 outputs the decoded frame */
352 pix_mp
->pixelformat
= ctx
->dst_fmt
->fourcc
;
353 pix_mp
->plane_fmt
[0].bytesperline
= ctx
->buf_width
;
354 pix_mp
->plane_fmt
[0].sizeimage
= ctx
->luma_size
;
355 pix_mp
->plane_fmt
[1].bytesperline
= ctx
->buf_width
;
356 pix_mp
->plane_fmt
[1].sizeimage
= ctx
->chroma_size
;
357 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
358 /* This is run on OUTPUT
359 The buffer contains compressed image
360 so width and height have no meaning */
363 pix_mp
->field
= V4L2_FIELD_NONE
;
364 pix_mp
->plane_fmt
[0].bytesperline
= ctx
->dec_src_buf_size
;
365 pix_mp
->plane_fmt
[0].sizeimage
= ctx
->dec_src_buf_size
;
366 pix_mp
->pixelformat
= ctx
->src_fmt
->fourcc
;
367 pix_mp
->num_planes
= ctx
->src_fmt
->num_planes
;
369 mfc_err("Format could not be read\n");
370 mfc_debug(2, "%s-- with error\n", __func__
);
378 static int vidioc_try_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
380 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
381 struct s5p_mfc_fmt
*fmt
;
383 mfc_debug(2, "Type is %d\n", f
->type
);
384 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
385 fmt
= find_format(f
, MFC_FMT_DEC
);
387 mfc_err("Unsupported format for source.\n");
390 if (fmt
->codec_mode
== S5P_FIMV_CODEC_NONE
) {
391 mfc_err("Unknown codec\n");
394 if ((dev
->variant
->version_bit
& fmt
->versions
) == 0) {
395 mfc_err("Unsupported format by this MFC version.\n");
398 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
399 fmt
= find_format(f
, MFC_FMT_RAW
);
401 mfc_err("Unsupported format for destination.\n");
404 if ((dev
->variant
->version_bit
& fmt
->versions
) == 0) {
405 mfc_err("Unsupported format by this MFC version.\n");
414 static int vidioc_s_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
416 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
417 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
419 struct v4l2_pix_format_mplane
*pix_mp
;
420 struct s5p_mfc_buf_size
*buf_size
= dev
->variant
->buf_size
;
423 ret
= vidioc_try_fmt(file
, priv
, f
);
424 pix_mp
= &f
->fmt
.pix_mp
;
427 if (ctx
->vq_src
.streaming
|| ctx
->vq_dst
.streaming
) {
428 v4l2_err(&dev
->v4l2_dev
, "%s queue busy\n", __func__
);
432 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
433 /* dst_fmt is validated by call to vidioc_try_fmt */
434 ctx
->dst_fmt
= find_format(f
, MFC_FMT_RAW
);
437 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
438 /* src_fmt is validated by call to vidioc_try_fmt */
439 ctx
->src_fmt
= find_format(f
, MFC_FMT_DEC
);
440 ctx
->codec_mode
= ctx
->src_fmt
->codec_mode
;
441 mfc_debug(2, "The codec number is: %d\n", ctx
->codec_mode
);
444 if (pix_mp
->plane_fmt
[0].sizeimage
== 0)
445 pix_mp
->plane_fmt
[0].sizeimage
= ctx
->dec_src_buf_size
=
447 else if (pix_mp
->plane_fmt
[0].sizeimage
> buf_size
->cpb
)
448 ctx
->dec_src_buf_size
= buf_size
->cpb
;
450 ctx
->dec_src_buf_size
= pix_mp
->plane_fmt
[0].sizeimage
;
451 pix_mp
->plane_fmt
[0].bytesperline
= 0;
452 ctx
->state
= MFCINST_INIT
;
456 mfc_err("Wrong type error for S_FMT : %d", f
->type
);
466 static int reqbufs_output(struct s5p_mfc_dev
*dev
, struct s5p_mfc_ctx
*ctx
,
467 struct v4l2_requestbuffers
*reqbufs
)
473 if (reqbufs
->count
== 0) {
474 mfc_debug(2, "Freeing buffers\n");
475 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
478 s5p_mfc_close_mfc_inst(dev
, ctx
);
479 ctx
->src_bufs_cnt
= 0;
480 ctx
->output_state
= QUEUE_FREE
;
481 } else if (ctx
->output_state
== QUEUE_FREE
) {
482 /* Can only request buffers when we have a valid format set. */
483 WARN_ON(ctx
->src_bufs_cnt
!= 0);
484 if (ctx
->state
!= MFCINST_INIT
) {
485 mfc_err("Reqbufs called in an invalid state\n");
490 mfc_debug(2, "Allocating %d buffers for OUTPUT queue\n",
492 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
496 ret
= s5p_mfc_open_mfc_inst(dev
, ctx
);
499 vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
503 ctx
->output_state
= QUEUE_BUFS_REQUESTED
;
505 mfc_err("Buffers have already been requested\n");
511 mfc_err("Failed allocating buffers for OUTPUT queue\n");
515 static int reqbufs_capture(struct s5p_mfc_dev
*dev
, struct s5p_mfc_ctx
*ctx
,
516 struct v4l2_requestbuffers
*reqbufs
)
522 if (reqbufs
->count
== 0) {
523 mfc_debug(2, "Freeing buffers\n");
524 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
527 s5p_mfc_hw_call_void(dev
->mfc_ops
, release_codec_buffers
, ctx
);
528 ctx
->dst_bufs_cnt
= 0;
529 } else if (ctx
->capture_state
== QUEUE_FREE
) {
530 WARN_ON(ctx
->dst_bufs_cnt
!= 0);
531 mfc_debug(2, "Allocating %d buffers for CAPTURE queue\n",
533 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
537 ctx
->capture_state
= QUEUE_BUFS_REQUESTED
;
538 ctx
->total_dpb_count
= reqbufs
->count
;
540 ret
= s5p_mfc_hw_call(dev
->mfc_ops
, alloc_codec_buffers
, ctx
);
542 mfc_err("Failed to allocate decoding buffers\n");
544 vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
546 ctx
->capture_state
= QUEUE_FREE
;
550 WARN_ON(ctx
->dst_bufs_cnt
!= ctx
->total_dpb_count
);
551 ctx
->capture_state
= QUEUE_BUFS_MMAPED
;
553 if (s5p_mfc_ctx_ready(ctx
))
554 set_work_bit_irqsave(ctx
);
555 s5p_mfc_hw_call_void(dev
->mfc_ops
, try_run
, dev
);
556 s5p_mfc_wait_for_done_ctx(ctx
, S5P_MFC_R2H_CMD_INIT_BUFFERS_RET
,
559 mfc_err("Buffers have already been requested\n");
565 mfc_err("Failed allocating buffers for CAPTURE queue\n");
569 /* Reqeust buffers */
570 static int vidioc_reqbufs(struct file
*file
, void *priv
,
571 struct v4l2_requestbuffers
*reqbufs
)
573 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
574 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
576 if (reqbufs
->memory
!= V4L2_MEMORY_MMAP
) {
577 mfc_err("Only V4L2_MEMORY_MAP is supported\n");
581 if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
582 return reqbufs_output(dev
, ctx
, reqbufs
);
583 } else if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
584 return reqbufs_capture(dev
, ctx
, reqbufs
);
586 mfc_err("Invalid type requested\n");
592 static int vidioc_querybuf(struct file
*file
, void *priv
,
593 struct v4l2_buffer
*buf
)
595 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
599 if (buf
->memory
!= V4L2_MEMORY_MMAP
) {
600 mfc_err("Only mmaped buffers can be used\n");
603 mfc_debug(2, "State: %d, buf->type: %d\n", ctx
->state
, buf
->type
);
604 if (ctx
->state
== MFCINST_GOT_INST
&&
605 buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
606 ret
= vb2_querybuf(&ctx
->vq_src
, buf
);
607 } else if (ctx
->state
== MFCINST_RUNNING
&&
608 buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
609 ret
= vb2_querybuf(&ctx
->vq_dst
, buf
);
610 for (i
= 0; i
< buf
->length
; i
++)
611 buf
->m
.planes
[i
].m
.mem_offset
+= DST_QUEUE_OFF_BASE
;
613 mfc_err("vidioc_querybuf called in an inappropriate state\n");
621 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
623 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
625 if (ctx
->state
== MFCINST_ERROR
) {
626 mfc_err("Call on QBUF after unrecoverable error\n");
629 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
630 return vb2_qbuf(&ctx
->vq_src
, buf
);
631 else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
632 return vb2_qbuf(&ctx
->vq_dst
, buf
);
636 /* Dequeue a buffer */
637 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
639 const struct v4l2_event ev
= {
640 .type
= V4L2_EVENT_EOS
642 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
645 if (ctx
->state
== MFCINST_ERROR
) {
646 mfc_err("Call on DQBUF after unrecoverable error\n");
649 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
650 ret
= vb2_dqbuf(&ctx
->vq_src
, buf
, file
->f_flags
& O_NONBLOCK
);
651 else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
652 ret
= vb2_dqbuf(&ctx
->vq_dst
, buf
, file
->f_flags
& O_NONBLOCK
);
653 if (ret
== 0 && ctx
->state
== MFCINST_FINISHED
&&
654 list_empty(&ctx
->vq_dst
.done_list
))
655 v4l2_event_queue_fh(&ctx
->fh
, &ev
);
662 /* Export DMA buffer */
663 static int vidioc_expbuf(struct file
*file
, void *priv
,
664 struct v4l2_exportbuffer
*eb
)
666 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
668 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
669 return vb2_expbuf(&ctx
->vq_src
, eb
);
670 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
671 return vb2_expbuf(&ctx
->vq_dst
, eb
);
676 static int vidioc_streamon(struct file
*file
, void *priv
,
677 enum v4l2_buf_type type
)
679 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
683 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
684 ret
= vb2_streamon(&ctx
->vq_src
, type
);
685 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
686 ret
= vb2_streamon(&ctx
->vq_dst
, type
);
691 /* Stream off, which equals to a pause */
692 static int vidioc_streamoff(struct file
*file
, void *priv
,
693 enum v4l2_buf_type type
)
695 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
697 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
698 return vb2_streamoff(&ctx
->vq_src
, type
);
699 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
700 return vb2_streamoff(&ctx
->vq_dst
, type
);
704 /* Set controls - v4l2 control framework */
705 static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl
*ctrl
)
707 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
710 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY
:
711 ctx
->display_delay
= ctrl
->val
;
713 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE
:
714 ctx
->display_delay_enable
= ctrl
->val
;
716 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER
:
717 ctx
->loop_filter_mpeg4
= ctrl
->val
;
719 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE
:
720 ctx
->slice_interface
= ctrl
->val
;
723 mfc_err("Invalid control 0x%08x\n", ctrl
->id
);
729 static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl
*ctrl
)
731 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
732 struct s5p_mfc_dev
*dev
= ctx
->dev
;
735 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
:
736 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
737 ctx
->state
< MFCINST_ABORT
) {
738 ctrl
->val
= ctx
->pb_count
;
740 } else if (ctx
->state
!= MFCINST_INIT
&&
741 ctx
->state
!= MFCINST_RES_CHANGE_END
) {
742 v4l2_err(&dev
->v4l2_dev
, "Decoding not initialised\n");
745 /* Should wait for the header to be parsed */
746 s5p_mfc_wait_for_done_ctx(ctx
,
747 S5P_MFC_R2H_CMD_SEQ_DONE_RET
, 0);
748 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
749 ctx
->state
< MFCINST_ABORT
) {
750 ctrl
->val
= ctx
->pb_count
;
752 v4l2_err(&dev
->v4l2_dev
, "Decoding not initialised\n");
761 static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops
= {
762 .s_ctrl
= s5p_mfc_dec_s_ctrl
,
763 .g_volatile_ctrl
= s5p_mfc_dec_g_v_ctrl
,
766 /* Get cropping information */
767 static int vidioc_g_crop(struct file
*file
, void *priv
,
768 struct v4l2_crop
*cr
)
770 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
771 struct s5p_mfc_dev
*dev
= ctx
->dev
;
772 u32 left
, right
, top
, bottom
;
774 if (ctx
->state
!= MFCINST_HEAD_PARSED
&&
775 ctx
->state
!= MFCINST_RUNNING
&& ctx
->state
!= MFCINST_FINISHING
776 && ctx
->state
!= MFCINST_FINISHED
) {
777 mfc_err("Cannont set crop\n");
780 if (ctx
->src_fmt
->fourcc
== V4L2_PIX_FMT_H264
) {
781 left
= s5p_mfc_hw_call(dev
->mfc_ops
, get_crop_info_h
, ctx
);
782 right
= left
>> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT
;
783 left
= left
& S5P_FIMV_SHARED_CROP_LEFT_MASK
;
784 top
= s5p_mfc_hw_call(dev
->mfc_ops
, get_crop_info_v
, ctx
);
785 bottom
= top
>> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT
;
786 top
= top
& S5P_FIMV_SHARED_CROP_TOP_MASK
;
789 cr
->c
.width
= ctx
->img_width
- left
- right
;
790 cr
->c
.height
= ctx
->img_height
- top
- bottom
;
791 mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
792 "w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left
, top
,
793 cr
->c
.width
, cr
->c
.height
, right
, bottom
,
794 ctx
->buf_width
, ctx
->buf_height
);
798 cr
->c
.width
= ctx
->img_width
;
799 cr
->c
.height
= ctx
->img_height
;
800 mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
801 "fh=%d\n", cr
->c
.width
, cr
->c
.height
, ctx
->buf_width
,
807 static int vidioc_decoder_cmd(struct file
*file
, void *priv
,
808 struct v4l2_decoder_cmd
*cmd
)
810 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
811 struct s5p_mfc_dev
*dev
= ctx
->dev
;
812 struct s5p_mfc_buf
*buf
;
816 case V4L2_ENC_CMD_STOP
:
820 if (!ctx
->vq_src
.streaming
)
823 spin_lock_irqsave(&dev
->irqlock
, flags
);
824 if (list_empty(&ctx
->src_queue
)) {
825 mfc_err("EOS: empty src queue, entering finishing state");
826 ctx
->state
= MFCINST_FINISHING
;
827 if (s5p_mfc_ctx_ready(ctx
))
828 set_work_bit_irqsave(ctx
);
829 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
830 s5p_mfc_hw_call_void(dev
->mfc_ops
, try_run
, dev
);
832 mfc_err("EOS: marking last buffer of stream");
833 buf
= list_entry(ctx
->src_queue
.prev
,
834 struct s5p_mfc_buf
, list
);
835 if (buf
->flags
& MFC_BUF_FLAG_USED
)
836 ctx
->state
= MFCINST_FINISHING
;
838 buf
->flags
|= MFC_BUF_FLAG_EOS
;
839 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
848 static int vidioc_subscribe_event(struct v4l2_fh
*fh
,
849 const struct v4l2_event_subscription
*sub
)
853 return v4l2_event_subscribe(fh
, sub
, 2, NULL
);
854 case V4L2_EVENT_SOURCE_CHANGE
:
855 return v4l2_src_change_event_subscribe(fh
, sub
);
863 static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops
= {
864 .vidioc_querycap
= vidioc_querycap
,
865 .vidioc_enum_fmt_vid_cap_mplane
= vidioc_enum_fmt_vid_cap_mplane
,
866 .vidioc_enum_fmt_vid_out_mplane
= vidioc_enum_fmt_vid_out_mplane
,
867 .vidioc_g_fmt_vid_cap_mplane
= vidioc_g_fmt
,
868 .vidioc_g_fmt_vid_out_mplane
= vidioc_g_fmt
,
869 .vidioc_try_fmt_vid_cap_mplane
= vidioc_try_fmt
,
870 .vidioc_try_fmt_vid_out_mplane
= vidioc_try_fmt
,
871 .vidioc_s_fmt_vid_cap_mplane
= vidioc_s_fmt
,
872 .vidioc_s_fmt_vid_out_mplane
= vidioc_s_fmt
,
873 .vidioc_reqbufs
= vidioc_reqbufs
,
874 .vidioc_querybuf
= vidioc_querybuf
,
875 .vidioc_qbuf
= vidioc_qbuf
,
876 .vidioc_dqbuf
= vidioc_dqbuf
,
877 .vidioc_expbuf
= vidioc_expbuf
,
878 .vidioc_streamon
= vidioc_streamon
,
879 .vidioc_streamoff
= vidioc_streamoff
,
880 .vidioc_g_crop
= vidioc_g_crop
,
881 .vidioc_decoder_cmd
= vidioc_decoder_cmd
,
882 .vidioc_subscribe_event
= vidioc_subscribe_event
,
883 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
886 static int s5p_mfc_queue_setup(struct vb2_queue
*vq
,
887 const struct v4l2_format
*fmt
, unsigned int *buf_count
,
888 unsigned int *plane_count
, unsigned int psize
[],
891 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
892 struct s5p_mfc_dev
*dev
= ctx
->dev
;
894 /* Video output for decoding (source)
895 * this can be set after getting an instance */
896 if (ctx
->state
== MFCINST_INIT
&&
897 vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
898 /* A single plane is required for input */
902 if (*buf_count
> MFC_MAX_BUFFERS
)
903 *buf_count
= MFC_MAX_BUFFERS
;
904 /* Video capture for decoding (destination)
905 * this can be set after the header was parsed */
906 } else if (ctx
->state
== MFCINST_HEAD_PARSED
&&
907 vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
908 /* Output plane count is 2 - one for Y and one for CbCr */
910 /* Setup buffer count */
911 if (*buf_count
< ctx
->pb_count
)
912 *buf_count
= ctx
->pb_count
;
913 if (*buf_count
> ctx
->pb_count
+ MFC_MAX_EXTRA_DPB
)
914 *buf_count
= ctx
->pb_count
+ MFC_MAX_EXTRA_DPB
;
915 if (*buf_count
> MFC_MAX_BUFFERS
)
916 *buf_count
= MFC_MAX_BUFFERS
;
918 mfc_err("State seems invalid. State = %d, vq->type = %d\n",
919 ctx
->state
, vq
->type
);
922 mfc_debug(2, "Buffer count=%d, plane count=%d\n",
923 *buf_count
, *plane_count
);
924 if (ctx
->state
== MFCINST_HEAD_PARSED
&&
925 vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
926 psize
[0] = ctx
->luma_size
;
927 psize
[1] = ctx
->chroma_size
;
929 if (IS_MFCV6_PLUS(dev
))
931 ctx
->dev
->alloc_ctx
[MFC_BANK1_ALLOC_CTX
];
934 ctx
->dev
->alloc_ctx
[MFC_BANK2_ALLOC_CTX
];
935 allocators
[1] = ctx
->dev
->alloc_ctx
[MFC_BANK1_ALLOC_CTX
];
936 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
&&
937 ctx
->state
== MFCINST_INIT
) {
938 psize
[0] = ctx
->dec_src_buf_size
;
939 allocators
[0] = ctx
->dev
->alloc_ctx
[MFC_BANK1_ALLOC_CTX
];
941 mfc_err("This video node is dedicated to decoding. Decoding not initialized\n");
947 static int s5p_mfc_buf_init(struct vb2_buffer
*vb
)
949 struct vb2_queue
*vq
= vb
->vb2_queue
;
950 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
953 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
954 if (ctx
->capture_state
== QUEUE_BUFS_MMAPED
)
956 for (i
= 0; i
< ctx
->dst_fmt
->num_planes
; i
++) {
957 if (IS_ERR_OR_NULL(ERR_PTR(
958 vb2_dma_contig_plane_dma_addr(vb
, i
)))) {
959 mfc_err("Plane mem not allocated\n");
963 if (vb2_plane_size(vb
, 0) < ctx
->luma_size
||
964 vb2_plane_size(vb
, 1) < ctx
->chroma_size
) {
965 mfc_err("Plane buffer (CAPTURE) is too small\n");
968 i
= vb
->v4l2_buf
.index
;
969 ctx
->dst_bufs
[i
].b
= vb
;
970 ctx
->dst_bufs
[i
].cookie
.raw
.luma
=
971 vb2_dma_contig_plane_dma_addr(vb
, 0);
972 ctx
->dst_bufs
[i
].cookie
.raw
.chroma
=
973 vb2_dma_contig_plane_dma_addr(vb
, 1);
975 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
976 if (IS_ERR_OR_NULL(ERR_PTR(
977 vb2_dma_contig_plane_dma_addr(vb
, 0)))) {
978 mfc_err("Plane memory not allocated\n");
981 if (vb2_plane_size(vb
, 0) < ctx
->dec_src_buf_size
) {
982 mfc_err("Plane buffer (OUTPUT) is too small\n");
986 i
= vb
->v4l2_buf
.index
;
987 ctx
->src_bufs
[i
].b
= vb
;
988 ctx
->src_bufs
[i
].cookie
.stream
=
989 vb2_dma_contig_plane_dma_addr(vb
, 0);
992 mfc_err("s5p_mfc_buf_init: unknown queue type\n");
998 static int s5p_mfc_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1000 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
1001 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1003 v4l2_ctrl_handler_setup(&ctx
->ctrl_handler
);
1004 if (ctx
->state
== MFCINST_FINISHING
||
1005 ctx
->state
== MFCINST_FINISHED
)
1006 ctx
->state
= MFCINST_RUNNING
;
1007 /* If context is ready then dev = work->data;schedule it to run */
1008 if (s5p_mfc_ctx_ready(ctx
))
1009 set_work_bit_irqsave(ctx
);
1010 s5p_mfc_hw_call_void(dev
->mfc_ops
, try_run
, dev
);
1014 static void s5p_mfc_stop_streaming(struct vb2_queue
*q
)
1016 unsigned long flags
;
1017 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
1018 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1021 if ((ctx
->state
== MFCINST_FINISHING
||
1022 ctx
->state
== MFCINST_RUNNING
) &&
1023 dev
->curr_ctx
== ctx
->num
&& dev
->hw_lock
) {
1024 ctx
->state
= MFCINST_ABORT
;
1025 s5p_mfc_wait_for_done_ctx(ctx
,
1026 S5P_MFC_R2H_CMD_FRAME_DONE_RET
, 0);
1029 if (q
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1030 spin_lock_irqsave(&dev
->irqlock
, flags
);
1031 s5p_mfc_hw_call_void(dev
->mfc_ops
, cleanup_queue
,
1032 &ctx
->dst_queue
, &ctx
->vq_dst
);
1033 INIT_LIST_HEAD(&ctx
->dst_queue
);
1034 ctx
->dst_queue_cnt
= 0;
1035 ctx
->dpb_flush_flag
= 1;
1036 ctx
->dec_dst_flag
= 0;
1037 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1038 if (IS_MFCV6_PLUS(dev
) && (ctx
->state
== MFCINST_RUNNING
)) {
1039 ctx
->state
= MFCINST_FLUSH
;
1040 set_work_bit_irqsave(ctx
);
1041 s5p_mfc_hw_call_void(dev
->mfc_ops
, try_run
, dev
);
1042 if (s5p_mfc_wait_for_done_ctx(ctx
,
1043 S5P_MFC_R2H_CMD_DPB_FLUSH_RET
, 0))
1044 mfc_err("Err flushing buffers\n");
1047 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1048 spin_lock_irqsave(&dev
->irqlock
, flags
);
1049 s5p_mfc_hw_call_void(dev
->mfc_ops
, cleanup_queue
,
1050 &ctx
->src_queue
, &ctx
->vq_src
);
1051 INIT_LIST_HEAD(&ctx
->src_queue
);
1052 ctx
->src_queue_cnt
= 0;
1053 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1056 ctx
->state
= MFCINST_RUNNING
;
1060 static void s5p_mfc_buf_queue(struct vb2_buffer
*vb
)
1062 struct vb2_queue
*vq
= vb
->vb2_queue
;
1063 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
1064 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1065 unsigned long flags
;
1066 struct s5p_mfc_buf
*mfc_buf
;
1068 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1069 mfc_buf
= &ctx
->src_bufs
[vb
->v4l2_buf
.index
];
1070 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
1071 spin_lock_irqsave(&dev
->irqlock
, flags
);
1072 list_add_tail(&mfc_buf
->list
, &ctx
->src_queue
);
1073 ctx
->src_queue_cnt
++;
1074 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1075 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1076 mfc_buf
= &ctx
->dst_bufs
[vb
->v4l2_buf
.index
];
1077 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
1078 /* Mark destination as available for use by MFC */
1079 spin_lock_irqsave(&dev
->irqlock
, flags
);
1080 set_bit(vb
->v4l2_buf
.index
, &ctx
->dec_dst_flag
);
1081 list_add_tail(&mfc_buf
->list
, &ctx
->dst_queue
);
1082 ctx
->dst_queue_cnt
++;
1083 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1085 mfc_err("Unsupported buffer type (%d)\n", vq
->type
);
1087 if (s5p_mfc_ctx_ready(ctx
))
1088 set_work_bit_irqsave(ctx
);
1089 s5p_mfc_hw_call_void(dev
->mfc_ops
, try_run
, dev
);
1092 static struct vb2_ops s5p_mfc_dec_qops
= {
1093 .queue_setup
= s5p_mfc_queue_setup
,
1094 .wait_prepare
= vb2_ops_wait_prepare
,
1095 .wait_finish
= vb2_ops_wait_finish
,
1096 .buf_init
= s5p_mfc_buf_init
,
1097 .start_streaming
= s5p_mfc_start_streaming
,
1098 .stop_streaming
= s5p_mfc_stop_streaming
,
1099 .buf_queue
= s5p_mfc_buf_queue
,
1102 struct s5p_mfc_codec_ops
*get_dec_codec_ops(void)
1104 return &decoder_codec_ops
;
1107 struct vb2_ops
*get_dec_queue_ops(void)
1109 return &s5p_mfc_dec_qops
;
1112 const struct v4l2_ioctl_ops
*get_dec_v4l2_ioctl_ops(void)
1114 return &s5p_mfc_dec_ioctl_ops
;
1117 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1118 && V4L2_CTRL_DRIVER_PRIV(x))
1120 int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx
*ctx
)
1122 struct v4l2_ctrl_config cfg
;
1125 v4l2_ctrl_handler_init(&ctx
->ctrl_handler
, NUM_CTRLS
);
1126 if (ctx
->ctrl_handler
.error
) {
1127 mfc_err("v4l2_ctrl_handler_init failed\n");
1128 return ctx
->ctrl_handler
.error
;
1131 for (i
= 0; i
< NUM_CTRLS
; i
++) {
1132 if (IS_MFC51_PRIV(controls
[i
].id
)) {
1133 memset(&cfg
, 0, sizeof(struct v4l2_ctrl_config
));
1134 cfg
.ops
= &s5p_mfc_dec_ctrl_ops
;
1135 cfg
.id
= controls
[i
].id
;
1136 cfg
.min
= controls
[i
].minimum
;
1137 cfg
.max
= controls
[i
].maximum
;
1138 cfg
.def
= controls
[i
].default_value
;
1139 cfg
.name
= controls
[i
].name
;
1140 cfg
.type
= controls
[i
].type
;
1142 cfg
.step
= controls
[i
].step
;
1143 cfg
.menu_skip_mask
= 0;
1145 ctx
->ctrls
[i
] = v4l2_ctrl_new_custom(&ctx
->ctrl_handler
,
1148 ctx
->ctrls
[i
] = v4l2_ctrl_new_std(&ctx
->ctrl_handler
,
1149 &s5p_mfc_dec_ctrl_ops
,
1150 controls
[i
].id
, controls
[i
].minimum
,
1151 controls
[i
].maximum
, controls
[i
].step
,
1152 controls
[i
].default_value
);
1154 if (ctx
->ctrl_handler
.error
) {
1155 mfc_err("Adding control (%d) failed\n", i
);
1156 return ctx
->ctrl_handler
.error
;
1158 if (controls
[i
].is_volatile
&& ctx
->ctrls
[i
])
1159 ctx
->ctrls
[i
]->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
1164 void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx
*ctx
)
1168 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
1169 for (i
= 0; i
< NUM_CTRLS
; i
++)
1170 ctx
->ctrls
[i
] = NULL
;
1173 void s5p_mfc_dec_init(struct s5p_mfc_ctx
*ctx
)
1175 struct v4l2_format f
;
1176 f
.fmt
.pix_mp
.pixelformat
= V4L2_PIX_FMT_H264
;
1177 ctx
->src_fmt
= find_format(&f
, MFC_FMT_DEC
);
1178 if (IS_MFCV8(ctx
->dev
))
1179 f
.fmt
.pix_mp
.pixelformat
= V4L2_PIX_FMT_NV12M
;
1180 else if (IS_MFCV6_PLUS(ctx
->dev
))
1181 f
.fmt
.pix_mp
.pixelformat
= V4L2_PIX_FMT_NV12MT_16X16
;
1183 f
.fmt
.pix_mp
.pixelformat
= V4L2_PIX_FMT_NV12MT
;
1184 ctx
->dst_fmt
= find_format(&f
, MFC_FMT_RAW
);
1185 mfc_debug(2, "Default src_fmt is %p, dest_fmt is %p\n",
1186 ctx
->src_fmt
, ctx
->dst_fmt
);