2 * linux/drivers/media/video/s5p-mfc/s5p_mfc_enc.c
4 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/sched.h>
15 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/platform_device.h>
19 #include <linux/version.h>
20 #include <linux/workqueue.h>
21 #include <linux/videodev2.h>
22 #include <media/videobuf2-core.h>
26 #include "s5p_mfc_opr.h"
27 #include "s5p_mfc_intr.h"
28 #include "s5p_mfc_mem.h"
29 #include "s5p_mfc_debug.h"
30 #include "s5p_mfc_reg.h"
31 #include "s5p_mfc_enc.h"
32 #include "s5p_mfc_common.h"
37 static struct s5p_mfc_fmt formats[] = {
39 .name = "4:2:0 2 Planes 64x32 Tiles",
40 .fourcc = V4L2_PIX_FMT_NV12MT,
41 .codec_mode = MFC_FORMATS_NO_CODEC,
46 .name = "4:2:0 2 Planes",
47 .fourcc = V4L2_PIX_FMT_NV12M,
48 .codec_mode = MFC_FORMATS_NO_CODEC,
53 .name = "H264 Encoded Stream",
54 .fourcc = V4L2_PIX_FMT_H264,
55 .codec_mode = S5P_FIMV_CODEC_H264_ENC,
60 .name = "MPEG4 Encoded Stream",
61 .fourcc = V4L2_PIX_FMT_MPEG4,
62 .codec_mode = S5P_FIMV_CODEC_MPEG4_ENC,
67 .name = "H264 Encoded Stream",
68 .fourcc = V4L2_PIX_FMT_H263,
69 .codec_mode = S5P_FIMV_CODEC_H263_ENC,
75 #define NUM_FORMATS ARRAY_SIZE(formats)
77 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
81 for (i = 0; i < NUM_FORMATS; i++) {
82 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
84 return (struct s5p_mfc_fmt *)&formats[i];
90 static struct v4l2_queryctrl controls[] = {
92 .id = V4L2_CID_CODEC_MFC5X_ENC_GOP_SIZE,
93 .type = V4L2_CTRL_TYPE_INTEGER,
94 .name = "The period of intra frame",
96 .maximum = (1 << 16) - 1,
101 .id = V4L2_CID_CODEC_MFC5X_ENC_MULTI_SLICE_MODE,
102 .type = V4L2_CTRL_TYPE_INTEGER,
103 .name = "The slice partitioning method",
110 .id = V4L2_CID_CODEC_MFC5X_ENC_MULTI_SLICE_MB,
111 .type = V4L2_CTRL_TYPE_INTEGER,
112 .name = "The number of MB in a slice",
114 .maximum = (1 << 16) - 1,
119 .id = V4L2_CID_CODEC_MFC5X_ENC_MULTI_SLICE_BIT,
120 .type = V4L2_CTRL_TYPE_INTEGER,
121 .name = "The maximum bits per slices",
123 .maximum = (1 << 30) - 1,
125 .default_value = 1900,
128 .id = V4L2_CID_CODEC_MFC5X_ENC_INTRA_REFRESH_MB,
129 .type = V4L2_CTRL_TYPE_INTEGER,
130 .name = "The number of intra refresh MBs",
132 .maximum = (1 << 16) - 1,
137 .id = V4L2_CID_CODEC_MFC5X_ENC_PAD_CTRL_ENABLE,
138 .type = V4L2_CTRL_TYPE_BOOLEAN,
139 .name = "Padding control enable",
146 .id = V4L2_CID_CODEC_MFC5X_ENC_PAD_LUMA_VALUE,
147 .type = V4L2_CTRL_TYPE_INTEGER,
148 .name = "Y image's padding value",
155 .id = V4L2_CID_CODEC_MFC5X_ENC_PAD_CB_VALUE,
156 .type = V4L2_CTRL_TYPE_INTEGER,
157 .name = "Cb image's padding value",
164 .id = V4L2_CID_CODEC_MFC5X_ENC_PAD_CR_VALUE,
165 .type = V4L2_CTRL_TYPE_INTEGER,
166 .name = "Cr image's padding value",
173 .id = V4L2_CID_CODEC_MFC5X_ENC_RC_FRAME_ENABLE,
174 .type = V4L2_CTRL_TYPE_BOOLEAN,
175 .name = "Frame level rate control enable",
182 .id = V4L2_CID_CODEC_MFC5X_ENC_RC_BIT_RATE,
183 .type = V4L2_CTRL_TYPE_INTEGER,
184 .name = "Target bit rate rate-control",
186 .maximum = (1 << 30) - 1,
191 .id = V4L2_CID_CODEC_MFC5X_ENC_RC_REACTION_COEFF,
192 .type = V4L2_CTRL_TYPE_INTEGER,
193 .name = "Rate control reaction coeff.",
195 .maximum = (1 << 16) - 1,
200 .id = V4L2_CID_CODEC_MFC5X_ENC_STREAM_SIZE,
201 .type = V4L2_CTRL_TYPE_INTEGER,
202 .name = "Encoded stream size",
204 .maximum = (1 << 30) - 1,
207 .flags = V4L2_CTRL_FLAG_READ_ONLY,
210 .id = V4L2_CID_CODEC_MFC5X_ENC_FRAME_COUNT,
211 .type = V4L2_CTRL_TYPE_INTEGER,
212 .name = "Encoded frame count",
214 .maximum = (1 << 30) - 1,
217 .flags = V4L2_CTRL_FLAG_READ_ONLY,
220 .id = V4L2_CID_CODEC_MFC5X_ENC_FRAME_TYPE,
221 .type = V4L2_CTRL_TYPE_INTEGER,
222 .name = "Encoded frame type",
227 .flags = V4L2_CTRL_FLAG_READ_ONLY,
230 .id = V4L2_CID_CODEC_MFC5X_ENC_FORCE_FRAME_TYPE,
231 .type = V4L2_CTRL_TYPE_INTEGER,
232 .name = "Force frame type",
239 .id = V4L2_CID_CODEC_MFC5X_ENC_VBV_BUF_SIZE,
240 .type = V4L2_CTRL_TYPE_INTEGER,
241 .name = "VBV buffer size (1Kbits)",
243 .maximum = (1 << 16) - 1,
248 .id = V4L2_CID_CODEC_MFC5X_ENC_SEQ_HDR_MODE,
249 .type = V4L2_CTRL_TYPE_INTEGER,
250 .name = "Sequence header mode",
257 .id = V4L2_CID_CODEC_MFC5X_ENC_FRAME_SKIP_MODE,
258 .type = V4L2_CTRL_TYPE_INTEGER,
259 .name = "Frame skip enable",
266 .id = V4L2_CID_CODEC_MFC5X_ENC_RC_FIXED_TARGET_BIT,
267 .type = V4L2_CTRL_TYPE_BOOLEAN,
268 .name = "Fixed target bit enable",
275 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_B_FRAMES,
276 .type = V4L2_CTRL_TYPE_INTEGER,
277 .name = "The number of B frames",
284 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_PROFILE,
285 .type = V4L2_CTRL_TYPE_INTEGER,
286 .name = "H264 profile",
293 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_LEVEL,
294 .type = V4L2_CTRL_TYPE_INTEGER,
295 .name = "H264 level",
302 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_INTERLACE,
303 .type = V4L2_CTRL_TYPE_BOOLEAN,
304 .name = "H264 interface mode",
311 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_LOOP_FILTER_MODE,
312 .type = V4L2_CTRL_TYPE_INTEGER,
313 .name = "H264 loop filter mode",
320 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_LOOP_FILTER_ALPHA,
321 .type = V4L2_CTRL_TYPE_INTEGER,
322 .name = "H264 loop filter alpha offset",
329 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_LOOP_FILTER_BETA,
330 .type = V4L2_CTRL_TYPE_INTEGER,
331 .name = "H264 loop filter beta offset",
338 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_ENTROPY_MODE,
339 .type = V4L2_CTRL_TYPE_INTEGER,
340 .name = "H264 entorpy mode",
347 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_MAX_REF_PIC,
348 .type = V4L2_CTRL_TYPE_INTEGER,
349 .name = "The max number of ref. picture",
356 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_NUM_REF_PIC_4P,
357 .type = V4L2_CTRL_TYPE_INTEGER,
358 .name = "The number of ref. picture of P",
365 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_8X8_TRANSFORM,
366 .type = V4L2_CTRL_TYPE_BOOLEAN,
367 .name = "H264 8x8 transform enable",
374 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_ENABLE,
375 .type = V4L2_CTRL_TYPE_BOOLEAN,
376 .name = "H264 MB level rate control",
383 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_FRAME_RATE,
384 .type = V4L2_CTRL_TYPE_INTEGER,
385 .name = "Frame rate",
387 .maximum = (1 << 30) - 1,
392 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_FRAME_QP,
393 .type = V4L2_CTRL_TYPE_INTEGER,
394 .name = "Frame QP value",
401 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MIN_QP,
402 .type = V4L2_CTRL_TYPE_INTEGER,
403 .name = "Minimum QP value",
410 /* FIXME: MAX_QP must be greater than or equal to MIN_QP */
411 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MAX_QP,
412 .type = V4L2_CTRL_TYPE_INTEGER,
413 .name = "Maximum QP value",
420 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_DARK,
421 .type = V4L2_CTRL_TYPE_BOOLEAN,
422 .name = "H264 dark region adaptive",
429 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_SMOOTH,
430 .type = V4L2_CTRL_TYPE_BOOLEAN,
431 .name = "H264 smooth region adaptive",
438 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_STATIC,
439 .type = V4L2_CTRL_TYPE_BOOLEAN,
440 .name = "H264 static region adaptive",
447 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_ACTIVITY,
448 .type = V4L2_CTRL_TYPE_BOOLEAN,
449 .name = "H264 MB activity adaptive",
456 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_P_FRAME_QP,
457 .type = V4L2_CTRL_TYPE_INTEGER,
458 .name = "P frame QP value",
465 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_RC_B_FRAME_QP,
466 .type = V4L2_CTRL_TYPE_INTEGER,
467 .name = "B frame QP value",
474 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_AR_VUI_ENABLE,
475 .type = V4L2_CTRL_TYPE_BOOLEAN,
476 .name = "Aspect ratio VUI enable",
483 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_AR_VUI_IDC,
484 .type = V4L2_CTRL_TYPE_INTEGER,
485 .name = "VUI aspect ratio IDC",
492 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_EXT_SAR_WIDTH,
493 .type = V4L2_CTRL_TYPE_INTEGER,
494 .name = "Horizontal size of SAR",
496 .maximum = (1 << 16) - 1,
501 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_EXT_SAR_HEIGHT,
502 .type = V4L2_CTRL_TYPE_INTEGER,
503 .name = "Vertical size of SAR",
505 .maximum = (1 << 16) - 1,
510 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_OPEN_GOP,
511 .type = V4L2_CTRL_TYPE_BOOLEAN,
512 .name = "Open GOP enable (I-picture)",
519 .id = V4L2_CID_CODEC_MFC5X_ENC_H264_I_PERIOD,
520 .type = V4L2_CTRL_TYPE_INTEGER,
521 .name = "H264 I period",
523 .maximum = (1 << 16) - 1,
528 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_B_FRAMES,
529 .type = V4L2_CTRL_TYPE_INTEGER,
530 .name = "The number of B frames",
537 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_PROFILE,
538 .type = V4L2_CTRL_TYPE_INTEGER,
539 .name = "MPEG4 profile",
546 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_LEVEL,
547 .type = V4L2_CTRL_TYPE_INTEGER,
548 .name = "MPEG4 level",
550 .maximum = (1 << 16) - 1,
555 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_FRAME_QP,
556 .type = V4L2_CTRL_TYPE_INTEGER,
557 .name = "Frame QP value",
564 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_MIN_QP,
565 .type = V4L2_CTRL_TYPE_INTEGER,
566 .name = "Minimum QP value",
573 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_MAX_QP,
574 .type = V4L2_CTRL_TYPE_INTEGER,
575 .name = "Minimum QP value",
582 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_QUARTER_PIXEL,
583 .type = V4L2_CTRL_TYPE_BOOLEAN,
584 .name = "Quarter pixel search enable",
591 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_P_FRAME_QP,
592 .type = V4L2_CTRL_TYPE_INTEGER,
593 .name = "P frame QP value",
600 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_B_FRAME_QP,
601 .type = V4L2_CTRL_TYPE_INTEGER,
602 .name = "B frame QP value",
609 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_VOP_TIME_RES,
610 .type = V4L2_CTRL_TYPE_INTEGER,
611 .name = "MPEG4 vop time resolution",
613 .maximum = (1 << 15) - 1,
618 .id = V4L2_CID_CODEC_MFC5X_ENC_MPEG4_VOP_FRM_DELTA,
619 .type = V4L2_CTRL_TYPE_INTEGER,
620 .name = "MPEG4 frame delta",
622 .maximum = (1 << 16) - 1,
627 .id = V4L2_CID_CODEC_MFC5X_ENC_H263_RC_FRAME_RATE,
628 .type = V4L2_CTRL_TYPE_INTEGER,
629 .name = "Frame rate",
631 .maximum = (1 << 30) - 1,
636 .id = V4L2_CID_CODEC_MFC5X_ENC_H263_RC_FRAME_QP,
637 .type = V4L2_CTRL_TYPE_INTEGER,
638 .name = "Frame QP value",
645 .id = V4L2_CID_CODEC_MFC5X_ENC_H263_RC_MIN_QP,
646 .type = V4L2_CTRL_TYPE_INTEGER,
647 .name = "Minimum QP value",
654 .id = V4L2_CID_CODEC_MFC5X_ENC_H263_RC_MAX_QP,
655 .type = V4L2_CTRL_TYPE_INTEGER,
656 .name = "Minimum QP value",
663 .id = V4L2_CID_CODEC_MFC5X_ENC_H263_RC_P_FRAME_QP,
664 .type = V4L2_CTRL_TYPE_INTEGER,
665 .name = "P frame QP value",
673 #define NUM_CTRLS ARRAY_SIZE(controls)
675 static struct v4l2_queryctrl *get_ctrl(int id)
679 for (i = 0; i < NUM_CTRLS; ++i)
680 if (id == controls[i].id)
685 static int check_ctrl_val(struct s5p_mfc_ctx *ctx, struct v4l2_control *ctrl)
687 struct s5p_mfc_dev *dev = ctx->dev;
688 struct v4l2_queryctrl *c;
690 c = get_ctrl(ctrl->id);
693 if (ctrl->value < c->minimum || ctrl->value > c->maximum
694 || (c->step != 0 && ctrl->value % c->step != 0)) {
695 v4l2_err(&dev->v4l2_dev, "Invalid control value\n");
701 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
703 mfc_debug(2, "src=%d, dst=%d, state=%d\n",
704 ctx->src_queue_cnt, ctx->dst_queue_cnt, ctx->state);
706 /* context is ready to make header */
707 if (ctx->state == MFCINST_GOT_INST && ctx->dst_queue_cnt >= 1)
709 /* context is ready to encode a frame */
710 if (ctx->state == MFCINST_RUNNING &&
711 ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
713 /* context is ready to encode remain frames */
714 if (ctx->state == MFCINST_FINISHING &&
715 ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
718 mfc_debug(2, "ctx is not ready.\n");
723 static void cleanup_ref_queue(struct s5p_mfc_ctx *ctx)
725 struct s5p_mfc_buf *mb_entry;
726 unsigned long mb_y_addr, mb_c_addr;
728 /* move buffers in ref queue to src queue */
729 while (!list_empty(&ctx->ref_queue)) {
730 mb_entry = list_entry((&ctx->ref_queue)->next, struct s5p_mfc_buf, list);
732 mb_y_addr = mfc_plane_cookie(mb_entry->b, 0);
733 mb_c_addr = mfc_plane_cookie(mb_entry->b, 1);
735 mfc_debug(2, "enc ref y addr: 0x%08lx", mb_y_addr);
736 mfc_debug(2, "enc ref c addr: 0x%08lx", mb_c_addr);
738 list_del(&mb_entry->list);
739 ctx->ref_queue_cnt--;
741 list_add_tail(&mb_entry->list, &ctx->src_queue);
742 ctx->src_queue_cnt++;
745 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
746 ctx->src_queue_cnt, ctx->ref_queue_cnt);
748 INIT_LIST_HEAD(&ctx->ref_queue);
749 ctx->ref_queue_cnt = 0;
752 static int enc_pre_seq_start(struct s5p_mfc_ctx *ctx)
754 struct s5p_mfc_dev *dev = ctx->dev;
755 struct s5p_mfc_buf *dst_mb;
756 unsigned long dst_addr;
757 unsigned int dst_size;
760 spin_lock_irqsave(&dev->irqlock, flags);
762 dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
763 dst_addr = mfc_plane_cookie(dst_mb->b, 0);
764 dst_size = vb2_plane_size(dst_mb->b, 0);
765 s5p_mfc_set_enc_stream_buffer(ctx, dst_addr, dst_size);
767 spin_unlock_irqrestore(&dev->irqlock, flags);
772 static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
774 struct s5p_mfc_dev *dev = ctx->dev;
775 struct s5p_mfc_enc_params *p = &ctx->enc_params;
776 struct s5p_mfc_buf *dst_mb;
779 mfc_debug(2, "seq header size: %d", s5p_mfc_get_enc_strm_size());
781 if (p->seq_hdr_mode == V4L2_CODEC_MFC5X_ENC_SEQ_HDR_MODE_SEQ) {
782 spin_lock_irqsave(&dev->irqlock, flags);
784 dst_mb = list_entry(ctx->dst_queue.next,
785 struct s5p_mfc_buf, list);
786 list_del(&dst_mb->list);
787 ctx->dst_queue_cnt--;
789 vb2_set_plane_payload(dst_mb->b, 0, s5p_mfc_get_enc_strm_size());
790 vb2_buffer_done(dst_mb->b, VB2_BUF_STATE_DONE);
792 spin_unlock_irqrestore(&dev->irqlock, flags);
795 ctx->state = MFCINST_RUNNING;
797 if (s5p_mfc_ctx_ready(ctx)) {
798 spin_lock_irqsave(&dev->condlock, flags);
799 set_bit(ctx->num, &dev->ctx_work_bits);
800 spin_unlock_irqrestore(&dev->condlock, flags);
803 s5p_mfc_try_run(dev);
808 static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
810 struct s5p_mfc_dev *dev = ctx->dev;
811 struct s5p_mfc_buf *dst_mb;
812 struct s5p_mfc_buf *src_mb;
814 unsigned long src_y_addr, src_c_addr, dst_addr;
815 unsigned int dst_size;
817 spin_lock_irqsave(&dev->irqlock, flags);
819 src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
820 src_y_addr = mfc_plane_cookie(src_mb->b, 0);
821 src_c_addr = mfc_plane_cookie(src_mb->b, 1);
822 s5p_mfc_set_enc_frame_buffer(ctx, src_y_addr, src_c_addr);
824 spin_unlock_irqrestore(&dev->irqlock, flags);
826 mfc_debug(2, "enc src y addr: 0x%08lx", src_y_addr);
827 mfc_debug(2, "enc src c addr: 0x%08lx", src_c_addr);
829 spin_lock_irqsave(&dev->irqlock, flags);
831 dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
832 dst_addr = mfc_plane_cookie(dst_mb->b, 0);
833 dst_size = vb2_plane_size(dst_mb->b, 0);
834 s5p_mfc_set_enc_stream_buffer(ctx, dst_addr, dst_size);
836 spin_unlock_irqrestore(&dev->irqlock, flags);
838 mfc_debug(2, "enc dst addr: 0x%08lx", dst_addr);
843 static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
845 struct s5p_mfc_dev *dev = ctx->dev;
846 struct s5p_mfc_buf *mb_entry;
847 unsigned long enc_y_addr, enc_c_addr;
848 unsigned long mb_y_addr, mb_c_addr;
850 unsigned int strm_size;
853 slice_type = s5p_mfc_get_enc_slice_type();
854 strm_size = s5p_mfc_get_enc_strm_size();
856 mfc_debug(2, "encoded slice type: %d", slice_type);
857 mfc_debug(2, "encoded stream size: %d", strm_size);
858 mfc_debug(2, "display order: %d",
859 s5p_mfc_read_reg(S5P_FIMV_ENC_SI_PIC_CNT));
861 /* FIXME: set it to dest buffer not context */
862 /* set encoded frame type */
863 ctx->frame_type = slice_type;
865 spin_lock_irqsave(&dev->irqlock, flags);
867 if (slice_type >= 0) {
868 s5p_mfc_get_enc_frame_buffer(ctx, &enc_y_addr, &enc_c_addr);
870 mfc_debug(2, "encoded y addr: 0x%08lx", enc_y_addr);
871 mfc_debug(2, "encoded c addr: 0x%08lx", enc_c_addr);
873 list_for_each_entry(mb_entry, &ctx->src_queue, list) {
874 mb_y_addr = mfc_plane_cookie(mb_entry->b, 0);
875 mb_c_addr = mfc_plane_cookie(mb_entry->b, 1);
877 mfc_debug(2, "enc src y addr: 0x%08lx", mb_y_addr);
878 mfc_debug(2, "enc src c addr: 0x%08lx", mb_c_addr);
880 if ((enc_y_addr == mb_y_addr) && (enc_c_addr == mb_c_addr)) {
881 list_del(&mb_entry->list);
882 ctx->src_queue_cnt--;
884 vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
889 list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
890 mb_y_addr = mfc_plane_cookie(mb_entry->b, 0);
891 mb_c_addr = mfc_plane_cookie(mb_entry->b, 1);
893 mfc_debug(2, "enc ref y addr: 0x%08lx", mb_y_addr);
894 mfc_debug(2, "enc ref c addr: 0x%08lx", mb_c_addr);
896 if ((enc_y_addr == mb_y_addr) && (enc_c_addr == mb_c_addr)) {
897 list_del(&mb_entry->list);
898 ctx->ref_queue_cnt--;
900 vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
906 if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
907 mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
909 if (mb_entry->used) {
910 list_del(&mb_entry->list);
911 ctx->src_queue_cnt--;
913 list_add_tail(&mb_entry->list, &ctx->ref_queue);
914 ctx->ref_queue_cnt++;
917 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
918 ctx->src_queue_cnt, ctx->ref_queue_cnt);
922 /* at least one more dest. buffers exist always */
923 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
924 list_del(&mb_entry->list);
925 ctx->dst_queue_cnt--;
926 vb2_set_plane_payload(mb_entry->b, 0, strm_size);
927 vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
930 spin_unlock_irqrestore(&dev->irqlock, flags);
932 if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0))
937 spin_lock(&dev->condlock);
938 clear_bit(ctx->num, &dev->ctx_work_bits);
939 spin_unlock(&dev->condlock);
945 static struct s5p_mfc_codec_ops encoder_codec_ops = {
946 .pre_seq_start = enc_pre_seq_start,
947 .post_seq_start = enc_post_seq_start,
948 .pre_frame_start = enc_pre_frame_start,
949 .post_frame_start = enc_post_frame_start,
952 /* Query capabilities of the device */
953 static int vidioc_querycap(struct file *file, void *priv,
954 struct v4l2_capability *cap)
956 struct s5p_mfc_dev *dev = video_drvdata(file);
958 strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
959 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
960 cap->bus_info[0] = 0;
961 cap->version = KERNEL_VERSION(1, 0, 0);
962 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
963 | V4L2_CAP_VIDEO_OUTPUT
964 | V4L2_CAP_STREAMING;
969 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
971 struct s5p_mfc_fmt *fmt;
974 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
975 if (mplane && formats[i].num_planes == 1)
977 else if (!mplane && formats[i].num_planes > 1)
979 if (out && formats[i].type != MFC_FMT_RAW)
981 else if (!out && formats[i].type != MFC_FMT_ENC)
986 strlcpy(f->description, fmt->name,
987 sizeof(f->description));
988 f->pixelformat = fmt->fourcc;
999 static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
1000 struct v4l2_fmtdesc *f)
1002 return vidioc_enum_fmt(f, false, false);
1005 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
1006 struct v4l2_fmtdesc *f)
1008 return vidioc_enum_fmt(f, true, false);
1011 static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
1012 struct v4l2_fmtdesc *f)
1014 return vidioc_enum_fmt(f, false, true);
1017 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
1018 struct v4l2_fmtdesc *f)
1020 return vidioc_enum_fmt(f, true, true);
1023 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1025 struct s5p_mfc_ctx *ctx = priv;
1026 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1030 mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
1032 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1033 /* This is run on output (encoder dest) */
1034 pix_fmt_mp->width = 0;
1035 pix_fmt_mp->height = 0;
1036 pix_fmt_mp->field = V4L2_FIELD_NONE;
1037 pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
1038 pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
1040 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
1041 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
1042 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1043 /* This is run on capture (encoder src) */
1044 pix_fmt_mp->width = ctx->img_width;
1045 pix_fmt_mp->height = ctx->img_height;
1046 /* FIXME: interlace */
1047 pix_fmt_mp->field = V4L2_FIELD_NONE;
1048 pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
1049 pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
1051 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1052 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1053 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1054 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1056 mfc_err("invalid buf type\n");
1065 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1067 struct s5p_mfc_fmt *fmt;
1068 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1070 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1071 fmt = find_format(f, MFC_FMT_ENC);
1073 mfc_err("failed to try output format\n");
1077 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
1078 mfc_err("must be set encoding output size\n");
1082 pix_fmt_mp->plane_fmt[0].bytesperline =
1083 pix_fmt_mp->plane_fmt[0].sizeimage;
1084 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1085 fmt = find_format(f, MFC_FMT_RAW);
1087 mfc_err("failed to try output format\n");
1091 if (fmt->num_planes != pix_fmt_mp->num_planes) {
1092 mfc_err("failed to try output format\n");
1096 /* FIXME: check below items */
1101 pix_fmt_mp->plane_fmt[0].bytesperline; - buf_width
1102 pix_fmt_mp->plane_fmt[0].sizeimage; - luma
1103 pix_fmt_mp->plane_fmt[1].bytesperline; - buf_width
1104 pix_fmt_mp->plane_fmt[1].sizeimage; - chroma
1107 mfc_err("invalid buf type\n");
1114 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
1116 struct s5p_mfc_dev *dev = video_drvdata(file);
1117 struct s5p_mfc_ctx *ctx = priv;
1118 struct s5p_mfc_fmt *fmt;
1119 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1120 unsigned long flags;
1125 ret = vidioc_try_fmt(file, priv, f);
1129 if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
1130 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
1135 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1136 fmt = find_format(f, MFC_FMT_ENC);
1138 mfc_err("failed to set capture format\n");
1141 ctx->state = MFCINST_INIT;
1144 ctx->codec_mode = ctx->dst_fmt->codec_mode;
1145 mfc_debug(2, "codec number: %d\n", ctx->dst_fmt->codec_mode);
1147 /* CHKME: 2KB aligned, multiple of 4KB - it may be ok with SDVMM */
1148 ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
1149 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
1151 ctx->dst_bufs_cnt = 0;
1152 ctx->capture_state = QUEUE_FREE;
1154 s5p_mfc_alloc_instance_buffer(ctx);
1156 spin_lock_irqsave(&dev->condlock, flags);
1157 set_bit(ctx->num, &dev->ctx_work_bits);
1158 spin_unlock_irqrestore(&dev->condlock, flags);
1160 s5p_mfc_clean_ctx_int_flags(ctx);
1161 s5p_mfc_try_run(dev);
1162 if (s5p_mfc_wait_for_done_ctx(ctx, \
1163 S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET, 1)) {
1164 /* Error or timeout */
1165 mfc_err("Error getting instance from hardware.\n");
1166 s5p_mfc_release_instance_buffer(ctx);
1170 mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
1171 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1172 fmt = find_format(f, MFC_FMT_RAW);
1174 mfc_err("failed to set output format\n");
1178 if (fmt->num_planes != pix_fmt_mp->num_planes) {
1179 mfc_err("failed to set output format\n");
1184 /* FIXME: Can be change source format in encoding? */
1186 ctx->img_width = pix_fmt_mp->width;
1187 ctx->img_height = pix_fmt_mp->height;
1189 mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
1190 mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1191 pix_fmt_mp->width, pix_fmt_mp->height,
1192 ctx->img_width, ctx->img_height);
1194 if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M) {
1195 ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN);
1197 ctx->luma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN)
1198 * ALIGN(ctx->img_height, S5P_FIMV_NV12M_LVALIGN);
1199 ctx->chroma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN)
1200 * ALIGN((ctx->img_height >> 1), S5P_FIMV_NV12M_CVALIGN);
1202 ctx->luma_size = ALIGN(ctx->luma_size, S5P_FIMV_NV12M_SALIGN);
1203 ctx->chroma_size = ALIGN(ctx->chroma_size, S5P_FIMV_NV12M_SALIGN);
1204 } else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
1205 ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN);
1207 ctx->luma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
1208 * ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
1209 ctx->chroma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
1210 * ALIGN((ctx->img_height >> 1), S5P_FIMV_NV12MT_VALIGN);
1212 ctx->luma_size = ALIGN(ctx->luma_size, S5P_FIMV_NV12MT_SALIGN);
1213 ctx->chroma_size = ALIGN(ctx->chroma_size, S5P_FIMV_NV12MT_SALIGN);
1216 ctx->src_bufs_cnt = 0;
1217 ctx->output_state = QUEUE_FREE;
1219 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1220 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1221 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1222 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1224 mfc_err("invalid buf type\n");
1232 static int vidioc_reqbufs(struct file *file, void *priv,
1233 struct v4l2_requestbuffers *reqbufs)
1235 struct s5p_mfc_ctx *ctx = priv;
1240 mfc_debug(2, "type: %d\n", reqbufs->memory);
1242 /* if memory is not mmp or userptr return error */
1243 if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1244 (reqbufs->memory != V4L2_MEMORY_USERPTR))
1247 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1248 /* RMVME: s5p_mfc_buf_negotiate() ctx state checked */
1250 if (ctx->state != MFCINST_GOT_INST) {
1251 mfc_err("invalid context state: %d\n", ctx->state);
1256 if (ctx->capture_state != QUEUE_FREE) {
1257 mfc_err("invalid capture state: %d\n", ctx->capture_state);
1261 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1263 mfc_err("error in vb2_reqbufs() for E(D)\n");
1266 ctx->capture_state = QUEUE_BUFS_REQUESTED;
1268 ret = s5p_mfc_alloc_codec_buffers(ctx);
1270 mfc_err("Failed to allocate encoding buffers.\n");
1272 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1275 } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1276 if (ctx->output_state != QUEUE_FREE) {
1277 mfc_err("invalid output state: %d\n", ctx->output_state);
1281 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1283 mfc_err("error in vb2_reqbufs() for E(S)\n");
1286 ctx->output_state = QUEUE_BUFS_REQUESTED;
1288 mfc_err("invalid buf type\n");
1292 mfc_debug(2, "--\n");
1297 static int vidioc_querybuf(struct file *file, void *priv,
1298 struct v4l2_buffer *buf)
1300 struct s5p_mfc_ctx *ctx = priv;
1305 mfc_debug(2, "type: %d\n", buf->memory);
1307 /* if memory is not mmp or userptr return error */
1308 if ((buf->memory != V4L2_MEMORY_MMAP) &&
1309 (buf->memory != V4L2_MEMORY_USERPTR))
1312 mfc_debug(2, "state: %d, buf->type: %d\n", ctx->state, buf->type);
1314 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1315 if (ctx->state != MFCINST_GOT_INST) {
1316 mfc_err("invalid context state: %d\n", ctx->state);
1321 if (ctx->capture_state != QUEUE_BUFS_REQUESTED) {
1322 mfc_err("invalid capture state: %d\n", ctx->capture_state);
1327 ret = vb2_querybuf(&ctx->vq_dst, buf);
1329 mfc_err("error in vb2_querybuf() for E(D)\n");
1332 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1335 ctx->capture_state = QUEUE_BUFS_QUERIED;
1337 } else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1338 /* FIXME: check context state */
1340 if (ctx->output_state != QUEUE_BUFS_REQUESTED) {
1341 mfc_err("invalid output state: %d\n", ctx->output_state);
1346 ret = vb2_querybuf(&ctx->vq_src, buf);
1348 mfc_err("error in vb2_querybuf() for E(S)\n");
1353 ctx->output_state = QUEUE_BUFS_QUERIED;
1356 mfc_err("invalid buf type\n");
1365 /* Queue a buffer */
1366 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1368 struct s5p_mfc_ctx *ctx = priv;
1371 mfc_debug(2, "Enqueued buf: %d (type = %d)\n", buf->index, buf->type);
1372 if (ctx->state == MFCINST_ERROR) {
1373 mfc_err("Call on QBUF after unrecoverable error.\n");
1376 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1377 return vb2_qbuf(&ctx->vq_src, buf);
1379 return vb2_qbuf(&ctx->vq_dst, buf);
1384 /* Dequeue a buffer */
1385 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1387 struct s5p_mfc_ctx *ctx = priv;
1391 mfc_debug(2, "Addr: %p %p %p Type: %d\n", &ctx->vq_src, buf, buf->m.planes,
1393 if (ctx->state == MFCINST_ERROR) {
1394 mfc_err("Call on DQBUF after unrecoverable error.\n");
1397 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1398 ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1400 ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1406 static int vidioc_streamon(struct file *file, void *priv,
1407 enum v4l2_buf_type type)
1409 struct s5p_mfc_ctx *ctx = priv;
1413 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1414 ret = vb2_streamon(&ctx->vq_src, type);
1416 ret = vb2_streamon(&ctx->vq_dst, type);
1417 mfc_debug(2, "ctx->src_queue_cnt = %d ctx->state = %d "
1418 "ctx->dst_queue_cnt = %d ctx->dpb_count = %d\n",
1419 ctx->src_queue_cnt, ctx->state, ctx->dst_queue_cnt,
1425 /* Stream off, which equals to a pause */
1426 static int vidioc_streamoff(struct file *file, void *priv,
1427 enum v4l2_buf_type type)
1429 struct s5p_mfc_ctx *ctx = priv;
1434 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1435 ret = vb2_streamoff(&ctx->vq_src, type);
1437 ret = vb2_streamoff(&ctx->vq_dst, type);
1443 static int vidioc_queryctrl(struct file *file, void *priv,
1444 struct v4l2_queryctrl *qc)
1446 struct v4l2_queryctrl *c;
1448 c = get_ctrl(qc->id);
1455 static int get_ctrl_val(struct s5p_mfc_ctx *ctx, struct v4l2_control *ctrl)
1458 struct s5p_mfc_dev *dev = ctx->dev;
1461 case V4L2_CID_CODEC_MFC5X_ENC_STREAM_SIZE:
1462 ctrl->value = ctx->enc_dst_buf_size;
1464 case V4L2_CID_CODEC_MFC5X_ENC_FRAME_COUNT:
1465 ctrl->value = ctx->frame_count;
1467 case V4L2_CID_CODEC_MFC5X_ENC_FRAME_TYPE:
1468 ctrl->value = ctx->frame_type;
1471 v4l2_err(&dev->v4l2_dev, "Invalid control\n");
1478 static int vidioc_g_ctrl(struct file *file, void *priv,
1479 struct v4l2_control *ctrl)
1481 struct s5p_mfc_ctx *ctx = priv;
1484 ret = get_ctrl_val(ctx, ctrl);
1491 static int set_ctrl_val(struct s5p_mfc_ctx *ctx, struct v4l2_control *ctrl)
1493 struct s5p_mfc_dev *dev = ctx->dev;
1494 struct s5p_mfc_enc_params *p = &ctx->enc_params;
1498 case V4L2_CID_CODEC_MFC5X_ENC_GOP_SIZE:
1499 p->gop_size = ctrl->value;
1501 case V4L2_CID_CODEC_MFC5X_ENC_MULTI_SLICE_MODE:
1502 p->slice_mode = ctrl->value;
1504 case V4L2_CID_CODEC_MFC5X_ENC_MULTI_SLICE_MB:
1505 p->slice_mb = ctrl->value;
1507 case V4L2_CID_CODEC_MFC5X_ENC_MULTI_SLICE_BIT:
1508 p->slice_bit = ctrl->value;
1510 case V4L2_CID_CODEC_MFC5X_ENC_INTRA_REFRESH_MB:
1511 p->intra_refresh_mb = ctrl->value;
1513 case V4L2_CID_CODEC_MFC5X_ENC_PAD_CTRL_ENABLE:
1514 p->pad = ctrl->value;
1516 case V4L2_CID_CODEC_MFC5X_ENC_PAD_LUMA_VALUE:
1517 p->pad_luma = ctrl->value;
1519 case V4L2_CID_CODEC_MFC5X_ENC_PAD_CB_VALUE:
1520 p->pad_cb = ctrl->value;
1522 case V4L2_CID_CODEC_MFC5X_ENC_PAD_CR_VALUE:
1523 p->pad_cr = ctrl->value;
1525 case V4L2_CID_CODEC_MFC5X_ENC_RC_FRAME_ENABLE:
1526 p->rc_frame = ctrl->value;
1528 case V4L2_CID_CODEC_MFC5X_ENC_RC_BIT_RATE:
1529 p->rc_bitrate = ctrl->value;
1531 case V4L2_CID_CODEC_MFC5X_ENC_RC_REACTION_COEFF:
1532 p->rc_reaction_coeff = ctrl->value;
1534 case V4L2_CID_CODEC_MFC5X_ENC_FORCE_FRAME_TYPE:
1535 ctx->force_frame_type = ctrl->value;
1537 case V4L2_CID_CODEC_MFC5X_ENC_VBV_BUF_SIZE:
1538 p->vbv_buf_size = ctrl->value;
1540 case V4L2_CID_CODEC_MFC5X_ENC_SEQ_HDR_MODE:
1541 p->seq_hdr_mode = ctrl->value;
1543 case V4L2_CID_CODEC_MFC5X_ENC_FRAME_SKIP_MODE:
1544 p->frame_skip_mode = ctrl->value;
1546 case V4L2_CID_CODEC_MFC5X_ENC_RC_FIXED_TARGET_BIT:
1547 p->fixed_target_bit = ctrl->value;
1549 case V4L2_CID_CODEC_MFC5X_ENC_H264_B_FRAMES:
1550 p->codec.h264.num_b_frame = ctrl->value;
1552 case V4L2_CID_CODEC_MFC5X_ENC_H264_PROFILE:
1553 p->codec.h264.profile = ctrl->value;
1555 case V4L2_CID_CODEC_MFC5X_ENC_H264_LEVEL:
1556 p->codec.h264.level = ctrl->value;
1558 case V4L2_CID_CODEC_MFC5X_ENC_H264_INTERLACE:
1559 p->codec.h264.interlace = ctrl->value;
1561 case V4L2_CID_CODEC_MFC5X_ENC_H264_LOOP_FILTER_MODE:
1562 p->codec.h264.loop_filter_mode = ctrl->value;
1564 case V4L2_CID_CODEC_MFC5X_ENC_H264_LOOP_FILTER_ALPHA:
1565 p->codec.h264.loop_filter_alpha = ctrl->value;
1567 case V4L2_CID_CODEC_MFC5X_ENC_H264_LOOP_FILTER_BETA:
1568 p->codec.h264.loop_filter_beta = ctrl->value;
1570 case V4L2_CID_CODEC_MFC5X_ENC_H264_ENTROPY_MODE:
1571 p->codec.h264.entropy_mode = ctrl->value;
1573 case V4L2_CID_CODEC_MFC5X_ENC_H264_MAX_REF_PIC:
1574 p->codec.h264.max_ref_pic = ctrl->value;
1576 case V4L2_CID_CODEC_MFC5X_ENC_H264_NUM_REF_PIC_4P:
1577 p->codec.h264.num_ref_pic_4p = ctrl->value;
1579 case V4L2_CID_CODEC_MFC5X_ENC_H264_8X8_TRANSFORM:
1580 p->codec.h264._8x8_transform = ctrl->value;
1582 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_ENABLE:
1583 p->codec.h264.rc_mb = ctrl->value;
1585 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_FRAME_RATE:
1586 p->codec.h264.rc_framerate = ctrl->value;
1588 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_FRAME_QP:
1589 p->codec.h264.rc_frame_qp = ctrl->value;
1591 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MIN_QP:
1592 p->codec.h264.rc_min_qp = ctrl->value;
1594 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MAX_QP:
1595 p->codec.h264.rc_max_qp = ctrl->value;
1597 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_DARK:
1598 p->codec.h264.rc_mb_dark = ctrl->value;
1600 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_SMOOTH:
1601 p->codec.h264.rc_mb_smooth = ctrl->value;
1603 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_STATIC:
1604 p->codec.h264.rc_mb_static = ctrl->value;
1606 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_MB_ACTIVITY:
1607 p->codec.h264.rc_mb_activity = ctrl->value;
1609 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_P_FRAME_QP:
1610 p->codec.h264.rc_p_frame_qp = ctrl->value;
1612 case V4L2_CID_CODEC_MFC5X_ENC_H264_RC_B_FRAME_QP:
1613 p->codec.h264.rc_b_frame_qp = ctrl->value;
1615 case V4L2_CID_CODEC_MFC5X_ENC_H264_AR_VUI_ENABLE:
1616 p->codec.h264.ar_vui = ctrl->value;
1618 case V4L2_CID_CODEC_MFC5X_ENC_H264_AR_VUI_IDC:
1619 p->codec.h264.ar_vui_idc = ctrl->value;
1621 case V4L2_CID_CODEC_MFC5X_ENC_H264_EXT_SAR_WIDTH:
1622 p->codec.h264.ext_sar_width = ctrl->value;
1624 case V4L2_CID_CODEC_MFC5X_ENC_H264_EXT_SAR_HEIGHT:
1625 p->codec.h264.ext_sar_height = ctrl->value;
1627 case V4L2_CID_CODEC_MFC5X_ENC_H264_OPEN_GOP:
1628 p->codec.h264.open_gop = ctrl->value;
1630 case V4L2_CID_CODEC_MFC5X_ENC_H264_I_PERIOD:
1631 p->codec.h264.open_gop_size = ctrl->value;
1633 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_B_FRAMES:
1634 p->codec.mpeg4.num_b_frame = ctrl->value;
1636 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_PROFILE:
1637 p->codec.mpeg4.profile = ctrl->value;
1639 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_LEVEL:
1640 p->codec.mpeg4.level = ctrl->value;
1642 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_FRAME_QP:
1643 p->codec.mpeg4.rc_frame_qp = ctrl->value;
1645 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_MIN_QP:
1646 p->codec.mpeg4.rc_min_qp = ctrl->value;
1648 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_MAX_QP:
1649 p->codec.mpeg4.rc_max_qp = ctrl->value;
1651 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_QUARTER_PIXEL:
1652 p->codec.mpeg4.quarter_pixel = ctrl->value;
1654 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_P_FRAME_QP:
1655 p->codec.mpeg4.rc_p_frame_qp = ctrl->value;
1657 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_RC_B_FRAME_QP:
1658 p->codec.mpeg4.rc_b_frame_qp = ctrl->value;
1660 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_VOP_TIME_RES:
1661 p->codec.mpeg4.vop_time_res = ctrl->value;
1663 case V4L2_CID_CODEC_MFC5X_ENC_MPEG4_VOP_FRM_DELTA:
1664 p->codec.mpeg4.vop_frm_delta = ctrl->value;
1666 case V4L2_CID_CODEC_MFC5X_ENC_H263_RC_FRAME_RATE:
1667 p->codec.mpeg4.rc_framerate = ctrl->value;
1669 case V4L2_CID_CODEC_MFC5X_ENC_H263_RC_FRAME_QP:
1670 p->codec.mpeg4.rc_frame_qp = ctrl->value;
1672 case V4L2_CID_CODEC_MFC5X_ENC_H263_RC_MIN_QP:
1673 p->codec.mpeg4.rc_min_qp = ctrl->value;
1675 case V4L2_CID_CODEC_MFC5X_ENC_H263_RC_MAX_QP:
1676 p->codec.mpeg4.rc_max_qp = ctrl->value;
1678 case V4L2_CID_CODEC_MFC5X_ENC_H263_RC_P_FRAME_QP:
1679 p->codec.mpeg4.rc_p_frame_qp = ctrl->value;
1682 v4l2_err(&dev->v4l2_dev, "Invalid control\n");
1689 static int vidioc_s_ctrl(struct file *file, void *priv,
1690 struct v4l2_control *ctrl)
1692 struct s5p_mfc_ctx *ctx = priv;
1699 if (s5p_mfc_get_node_type(file) != MFCNODE_ENCODER)
1704 stream_on = ctx->vq_src.streaming || ctx->vq_dst.streaming;
1707 ret = check_ctrl_val(ctx, ctrl);
1711 ret = set_ctrl_val(ctx, ctrl);
1718 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
1719 struct v4l2_ext_controls *f)
1721 struct s5p_mfc_ctx *ctx = priv;
1722 struct v4l2_ext_control *ext_ctrl;
1723 struct v4l2_control ctrl;
1728 if (s5p_mfc_get_node_type(file) != MFCNODE_ENCODER)
1732 if (f->ctrl_class != V4L2_CTRL_CLASS_CODEC)
1735 for (i = 0; i < f->count; i++) {
1736 ext_ctrl = (f->controls + i);
1738 ctrl.id = ext_ctrl->id;
1740 ret = get_ctrl_val(ctx, &ctrl);
1742 ext_ctrl->value = ctrl.value;
1748 mfc_debug(2, "[%d] id: 0x%08x, value: %d", i, ext_ctrl->id, ext_ctrl->value);
1754 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1755 struct v4l2_ext_controls *f)
1757 struct s5p_mfc_ctx *ctx = priv;
1758 struct v4l2_ext_control *ext_ctrl;
1759 struct v4l2_control ctrl;
1764 if (s5p_mfc_get_node_type(file) != MFCNODE_ENCODER)
1768 if (f->ctrl_class != V4L2_CTRL_CLASS_CODEC)
1771 for (i = 0; i < f->count; i++) {
1772 ext_ctrl = (f->controls + i);
1774 ctrl.id = ext_ctrl->id;
1775 ctrl.value = ext_ctrl->value;
1777 ret = check_ctrl_val(ctx, &ctrl);
1783 ret = set_ctrl_val(ctx, &ctrl);
1789 mfc_debug(2, "[%d] id: 0x%08x, value: %d", i, ext_ctrl->id, ext_ctrl->value);
1795 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
1796 struct v4l2_ext_controls *f)
1798 struct s5p_mfc_ctx *ctx = priv;
1799 struct v4l2_ext_control *ext_ctrl;
1800 struct v4l2_control ctrl;
1805 if (s5p_mfc_get_node_type(file) != MFCNODE_ENCODER)
1809 if (f->ctrl_class != V4L2_CTRL_CLASS_CODEC)
1812 for (i = 0; i < f->count; i++) {
1813 ext_ctrl = (f->controls + i);
1815 ctrl.id = ext_ctrl->id;
1816 ctrl.value = ext_ctrl->value;
1818 ret = check_ctrl_val(ctx, &ctrl);
1824 mfc_debug(2, "[%d] id: 0x%08x, value: %d", i, ext_ctrl->id, ext_ctrl->value);
1830 static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
1831 .vidioc_querycap = vidioc_querycap,
1832 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1833 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
1834 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1835 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
1836 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1837 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1838 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1839 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1840 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1841 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1842 .vidioc_reqbufs = vidioc_reqbufs,
1843 .vidioc_querybuf = vidioc_querybuf,
1844 .vidioc_qbuf = vidioc_qbuf,
1845 .vidioc_dqbuf = vidioc_dqbuf,
1846 .vidioc_streamon = vidioc_streamon,
1847 .vidioc_streamoff = vidioc_streamoff,
1848 .vidioc_queryctrl = vidioc_queryctrl,
1849 .vidioc_g_ctrl = vidioc_g_ctrl,
1850 .vidioc_s_ctrl = vidioc_s_ctrl,
1851 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1852 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1853 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1856 static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
1863 if (fmt->num_planes != vb->num_planes) {
1864 mfc_err("invalid plane number for the format\n");
1868 for (i = 0; i < fmt->num_planes; i++) {
1869 if (!mfc_plane_cookie(vb, i)) {
1870 mfc_err("failed to get plane cookie\n");
1874 mfc_debug(2, "index: %d, plane[%d] cookie: 0x%08lx",
1875 vb->v4l2_buf.index, i,
1876 mfc_plane_cookie(vb, i));
1882 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
1883 unsigned int *buf_count, unsigned int *plane_count,
1884 unsigned long psize[], void *allocators[])
1886 struct s5p_mfc_ctx *ctx = vq->drv_priv;
1891 if (ctx->state != MFCINST_GOT_INST) {
1892 mfc_err("inavlid state: %d\n", ctx->state);
1896 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1898 *plane_count = ctx->dst_fmt->num_planes;
1900 *plane_count = MFC_ENC_CAP_PLANE_COUNT;
1904 if (*buf_count > MFC_MAX_BUFFERS)
1905 *buf_count = MFC_MAX_BUFFERS;
1907 psize[0] = ctx->enc_dst_buf_size;
1908 allocators[0] = ctx->dev->alloc_ctx[MFC_CMA_BANK1_ALLOC_CTX];
1909 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1911 *plane_count = ctx->src_fmt->num_planes;
1913 *plane_count = MFC_ENC_OUT_PLANE_COUNT;
1917 if (*buf_count > MFC_MAX_BUFFERS)
1918 *buf_count = MFC_MAX_BUFFERS;
1920 psize[0] = ctx->luma_size;
1921 psize[1] = ctx->chroma_size;
1922 allocators[0] = ctx->dev->alloc_ctx[MFC_CMA_BANK2_ALLOC_CTX];
1923 allocators[1] = ctx->dev->alloc_ctx[MFC_CMA_BANK2_ALLOC_CTX];
1926 mfc_err("inavlid queue type: %d\n", vq->type);
1930 mfc_debug(2, "buf_count: %d, plane_count: %d\n", *buf_count, *plane_count);
1931 for (i = 0; i < *plane_count; i++)
1932 mfc_debug(2, "plane[%d] size=%lu\n", i, psize[i]);
1939 static void s5p_mfc_unlock(struct vb2_queue *q)
1941 struct s5p_mfc_ctx *ctx = q->drv_priv;
1942 struct s5p_mfc_dev *dev = ctx->dev;
1944 mutex_unlock(&dev->mfc_mutex);
1947 static void s5p_mfc_lock(struct vb2_queue *q)
1949 struct s5p_mfc_ctx *ctx = q->drv_priv;
1950 struct s5p_mfc_dev *dev = ctx->dev;
1952 mutex_lock(&dev->mfc_mutex);
1955 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
1957 struct vb2_queue *vq = vb->vb2_queue;
1958 struct s5p_mfc_ctx *ctx = vq->drv_priv;
1964 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1965 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1969 i = vb->v4l2_buf.index;
1970 ctx->dst_bufs[i].b = vb;
1971 ctx->dst_bufs[i].cookie.stream = mfc_plane_cookie(vb, 0);
1972 ctx->dst_bufs_cnt++;
1973 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1974 ret = check_vb_with_fmt(ctx->src_fmt, vb);
1978 i = vb->v4l2_buf.index;
1979 ctx->src_bufs[i].b = vb;
1980 ctx->src_bufs[i].cookie.raw.luma = mfc_plane_cookie(vb, 0);
1981 ctx->src_bufs[i].cookie.raw.chroma = mfc_plane_cookie(vb, 1);
1982 ctx->src_bufs_cnt++;
1984 mfc_err("inavlid queue type: %d\n", vq->type);
1993 static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
1995 struct vb2_queue *vq = vb->vb2_queue;
1996 struct s5p_mfc_ctx *ctx = vq->drv_priv;
2001 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2002 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
2006 mfc_debug(2, "plane size: %ld, dst size: %d\n",
2007 vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
2009 if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
2010 mfc_err("plane size is too small for capture\n");
2013 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2014 ret = check_vb_with_fmt(ctx->src_fmt, vb);
2018 mfc_debug(2, "plane size: %ld, luma size: %d\n",
2019 vb2_plane_size(vb, 0), ctx->luma_size);
2020 mfc_debug(2, "plane size: %ld, chroma size: %d\n",
2021 vb2_plane_size(vb, 1), ctx->chroma_size);
2023 if (vb2_plane_size(vb, 0) < ctx->luma_size ||
2024 vb2_plane_size(vb, 1) < ctx->chroma_size) {
2025 mfc_err("plane size is too small for output\n");
2029 mfc_err("inavlid queue type: %d\n", vq->type);
2038 static int s5p_mfc_start_streaming(struct vb2_queue *q)
2040 struct s5p_mfc_ctx *ctx = q->drv_priv;
2041 struct s5p_mfc_dev *dev = ctx->dev;
2042 unsigned long flags;
2044 /* If context is ready then dev = work->data;schedule it to run */
2045 if (s5p_mfc_ctx_ready(ctx)) {
2046 spin_lock_irqsave(&dev->condlock, flags);
2047 set_bit(ctx->num, &dev->ctx_work_bits);
2048 spin_unlock_irqrestore(&dev->condlock, flags);
2051 s5p_mfc_try_run(dev);
2056 static int s5p_mfc_stop_streaming(struct vb2_queue *q)
2058 unsigned long flags;
2059 struct s5p_mfc_ctx *ctx = q->drv_priv;
2060 struct s5p_mfc_dev *dev = ctx->dev;
2062 if ((ctx->state == MFCINST_FINISHING ||
2063 ctx->state == MFCINST_RUNNING) &&
2064 dev->curr_ctx == ctx->num && dev->hw_lock) {
2065 ctx->state = MFCINST_ABORT;
2066 s5p_mfc_wait_for_done_ctx(ctx, S5P_FIMV_R2H_CMD_FRAME_DONE_RET,
2070 ctx->state = MFCINST_FINISHED;
2072 spin_lock_irqsave(&dev->irqlock, flags);
2074 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2075 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
2076 INIT_LIST_HEAD(&ctx->dst_queue);
2077 ctx->dst_queue_cnt = 0;
2080 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2081 cleanup_ref_queue(ctx);
2083 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
2084 INIT_LIST_HEAD(&ctx->src_queue);
2085 ctx->src_queue_cnt = 0;
2088 spin_unlock_irqrestore(&dev->irqlock, flags);
2093 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
2095 struct vb2_queue *vq = vb->vb2_queue;
2096 struct s5p_mfc_ctx *ctx = vq->drv_priv;
2097 struct s5p_mfc_dev *dev = ctx->dev;
2098 unsigned long flags;
2099 struct s5p_mfc_buf *mfc_buf;
2103 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2104 mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
2106 mfc_debug(2, "dst queue: %p\n", &ctx->dst_queue);
2107 mfc_debug(2, "adding to dst: %p (%08lx, %08x)\n", vb,
2108 mfc_plane_cookie(vb, 0),
2109 ctx->dst_bufs[vb->v4l2_buf.index].cookie.stream);
2111 /* Mark destination as available for use by MFC */
2112 spin_lock_irqsave(&dev->irqlock, flags);
2113 list_add_tail(&mfc_buf->list, &ctx->dst_queue);
2114 ctx->dst_queue_cnt++;
2115 spin_unlock_irqrestore(&dev->irqlock, flags);
2116 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2117 mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
2119 mfc_debug(2, "src queue: %p\n", &ctx->src_queue);
2120 mfc_debug(2, "adding to src: %p (%08lx, %08lx, %08x, %08x)\n", vb,
2121 mfc_plane_cookie(vb, 0),
2122 mfc_plane_cookie(vb, 1),
2123 ctx->src_bufs[vb->v4l2_buf.index].cookie.raw.luma,
2124 ctx->src_bufs[vb->v4l2_buf.index].cookie.raw.chroma);
2126 spin_lock_irqsave(&dev->irqlock, flags);
2128 if (vb->v4l2_planes[0].bytesused == 0) {
2129 mfc_debug(1, "change state to FINISHING\n");
2130 ctx->state = MFCINST_FINISHING;
2132 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2134 cleanup_ref_queue(ctx);
2136 list_add_tail(&mfc_buf->list, &ctx->src_queue);
2137 ctx->src_queue_cnt++;
2140 spin_unlock_irqrestore(&dev->irqlock, flags);
2142 mfc_err("unsupported buffer type (%d)\n", vq->type);
2145 if (s5p_mfc_ctx_ready(ctx)) {
2146 spin_lock_irqsave(&dev->condlock, flags);
2147 set_bit(ctx->num, &dev->ctx_work_bits);
2148 spin_unlock_irqrestore(&dev->condlock, flags);
2150 s5p_mfc_try_run(dev);
2155 static struct vb2_ops s5p_mfc_enc_qops = {
2156 .queue_setup = s5p_mfc_queue_setup,
2157 .wait_prepare = s5p_mfc_unlock,
2158 .wait_finish = s5p_mfc_lock,
2159 .buf_init = s5p_mfc_buf_init,
2160 .buf_prepare = s5p_mfc_buf_prepare,
2161 .start_streaming= s5p_mfc_start_streaming,
2162 .stop_streaming = s5p_mfc_stop_streaming,
2163 .buf_queue = s5p_mfc_buf_queue,
2166 struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
2168 return &encoder_codec_ops;
2171 struct vb2_ops *get_enc_queue_ops(void)
2173 return &s5p_mfc_enc_qops;
2176 const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
2178 return &s5p_mfc_enc_ioctl_ops;
2181 struct s5p_mfc_fmt *get_enc_def_fmt(bool src)
2184 return &formats[DEF_SRC_FMT];
2186 return &formats[DEF_DST_FMT];