2 * linux/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c
4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * Jeongtae Park <jtp.park@samsung.com>
8 * Kamil Debski <k.debski@samsung.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/clk.h>
17 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
22 #include <linux/videodev2.h>
23 #include <media/v4l2-event.h>
24 #include <linux/workqueue.h>
25 #include <media/v4l2-ctrls.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_enc.h"
31 #include "s5p_mfc_intr.h"
32 #include "s5p_mfc_opr.h"
34 #define DEF_SRC_FMT_ENC V4L2_PIX_FMT_NV12M
35 #define DEF_DST_FMT_ENC V4L2_PIX_FMT_H264
37 static struct s5p_mfc_fmt formats[] = {
39 .name = "4:2:0 2 Planes 16x16 Tiles",
40 .fourcc = V4L2_PIX_FMT_NV12MT_16X16,
41 .codec_mode = S5P_MFC_CODEC_NONE,
44 .versions = MFC_V6_BIT | MFC_V7_BIT,
47 .name = "4:2:0 2 Planes 64x32 Tiles",
48 .fourcc = V4L2_PIX_FMT_NV12MT,
49 .codec_mode = S5P_MFC_CODEC_NONE,
52 .versions = MFC_V5_BIT,
55 .name = "4:2:0 2 Planes Y/CbCr",
56 .fourcc = V4L2_PIX_FMT_NV12M,
57 .codec_mode = S5P_MFC_CODEC_NONE,
60 .versions = MFC_V5_BIT | MFC_V6_BIT | MFC_V7_BIT |
64 .name = "4:2:0 2 Planes Y/CrCb",
65 .fourcc = V4L2_PIX_FMT_NV21M,
66 .codec_mode = S5P_MFC_CODEC_NONE,
69 .versions = MFC_V6_BIT | MFC_V7_BIT | MFC_V8_BIT,
72 .name = "H264 Encoded Stream",
73 .fourcc = V4L2_PIX_FMT_H264,
74 .codec_mode = S5P_MFC_CODEC_H264_ENC,
77 .versions = MFC_V5_BIT | MFC_V6_BIT | MFC_V7_BIT |
81 .name = "MPEG4 Encoded Stream",
82 .fourcc = V4L2_PIX_FMT_MPEG4,
83 .codec_mode = S5P_MFC_CODEC_MPEG4_ENC,
86 .versions = MFC_V5_BIT | MFC_V6_BIT | MFC_V7_BIT |
90 .name = "H263 Encoded Stream",
91 .fourcc = V4L2_PIX_FMT_H263,
92 .codec_mode = S5P_MFC_CODEC_H263_ENC,
95 .versions = MFC_V5_BIT | MFC_V6_BIT | MFC_V7_BIT |
99 .name = "VP8 Encoded Stream",
100 .fourcc = V4L2_PIX_FMT_VP8,
101 .codec_mode = S5P_MFC_CODEC_VP8_ENC,
104 .versions = MFC_V7_BIT | MFC_V8_BIT,
108 #define NUM_FORMATS ARRAY_SIZE(formats)
109 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
113 for (i = 0; i < NUM_FORMATS; i++) {
114 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
115 formats[i].type == t)
121 static struct mfc_control controls[] = {
123 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
124 .type = V4L2_CTRL_TYPE_INTEGER,
126 .maximum = (1 << 16) - 1,
131 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
132 .type = V4L2_CTRL_TYPE_MENU,
133 .minimum = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
134 .maximum = V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES,
135 .default_value = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
139 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB,
140 .type = V4L2_CTRL_TYPE_INTEGER,
142 .maximum = (1 << 16) - 1,
147 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES,
148 .type = V4L2_CTRL_TYPE_INTEGER,
150 .maximum = (1 << 30) - 1,
152 .default_value = 1900,
155 .id = V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
156 .type = V4L2_CTRL_TYPE_INTEGER,
158 .maximum = (1 << 16) - 1,
163 .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING,
164 .type = V4L2_CTRL_TYPE_BOOLEAN,
165 .name = "Padding Control Enable",
172 .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV,
173 .type = V4L2_CTRL_TYPE_INTEGER,
174 .name = "Padding Color YUV Value",
176 .maximum = (1 << 25) - 1,
181 .id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
182 .type = V4L2_CTRL_TYPE_BOOLEAN,
189 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
190 .type = V4L2_CTRL_TYPE_INTEGER,
192 .maximum = (1 << 30) - 1,
197 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
198 .type = V4L2_CTRL_TYPE_INTEGER,
199 .name = "Rate Control Reaction Coeff.",
201 .maximum = (1 << 16) - 1,
206 .id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE,
207 .type = V4L2_CTRL_TYPE_MENU,
208 .name = "Force frame type",
209 .minimum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
210 .maximum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED,
211 .default_value = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
215 .id = V4L2_CID_MPEG_VIDEO_VBV_SIZE,
216 .type = V4L2_CTRL_TYPE_INTEGER,
218 .maximum = (1 << 16) - 1,
223 .id = V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE,
224 .type = V4L2_CTRL_TYPE_INTEGER,
225 .name = "Horizontal MV Search Range",
232 .id = V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE,
233 .type = V4L2_CTRL_TYPE_INTEGER,
234 .name = "Vertical MV Search Range",
241 .id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
242 .type = V4L2_CTRL_TYPE_INTEGER,
244 .maximum = (1 << 16) - 1,
249 .id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
250 .type = V4L2_CTRL_TYPE_MENU,
251 .minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
252 .maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
253 .default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
257 .id = V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE,
258 .type = V4L2_CTRL_TYPE_MENU,
259 .name = "Frame Skip Enable",
260 .minimum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
261 .maximum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
263 .default_value = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
266 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
267 .type = V4L2_CTRL_TYPE_BOOLEAN,
268 .name = "Fixed Target Bit Enable",
276 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
277 .type = V4L2_CTRL_TYPE_INTEGER,
284 .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
285 .type = V4L2_CTRL_TYPE_MENU,
286 .minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
287 .maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
288 .default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
290 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
291 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
292 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
296 .id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
297 .type = V4L2_CTRL_TYPE_MENU,
298 .minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
299 .maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
300 .default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
303 .id = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
304 .type = V4L2_CTRL_TYPE_MENU,
305 .minimum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
306 .maximum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
307 .default_value = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
311 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
312 .type = V4L2_CTRL_TYPE_MENU,
313 .minimum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
314 .maximum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
315 .default_value = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
319 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA,
320 .type = V4L2_CTRL_TYPE_INTEGER,
327 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA,
328 .type = V4L2_CTRL_TYPE_INTEGER,
335 .id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
336 .type = V4L2_CTRL_TYPE_MENU,
337 .minimum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
338 .maximum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
339 .default_value = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
343 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P,
344 .type = V4L2_CTRL_TYPE_INTEGER,
345 .name = "The Number of Ref. Pic for P",
352 .id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
353 .type = V4L2_CTRL_TYPE_BOOLEAN,
360 .id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
361 .type = V4L2_CTRL_TYPE_BOOLEAN,
368 .id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
369 .type = V4L2_CTRL_TYPE_INTEGER,
376 .id = V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
377 .type = V4L2_CTRL_TYPE_INTEGER,
384 .id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
385 .type = V4L2_CTRL_TYPE_INTEGER,
392 .id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
393 .type = V4L2_CTRL_TYPE_INTEGER,
400 .id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
401 .type = V4L2_CTRL_TYPE_INTEGER,
408 .id = V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP,
409 .type = V4L2_CTRL_TYPE_INTEGER,
410 .name = "H263 I-Frame QP value",
417 .id = V4L2_CID_MPEG_VIDEO_H263_MIN_QP,
418 .type = V4L2_CTRL_TYPE_INTEGER,
419 .name = "H263 Minimum QP value",
426 .id = V4L2_CID_MPEG_VIDEO_H263_MAX_QP,
427 .type = V4L2_CTRL_TYPE_INTEGER,
428 .name = "H263 Maximum QP value",
435 .id = V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP,
436 .type = V4L2_CTRL_TYPE_INTEGER,
437 .name = "H263 P frame QP value",
444 .id = V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP,
445 .type = V4L2_CTRL_TYPE_INTEGER,
446 .name = "H263 B frame QP value",
453 .id = V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP,
454 .type = V4L2_CTRL_TYPE_INTEGER,
455 .name = "MPEG4 I-Frame QP value",
462 .id = V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP,
463 .type = V4L2_CTRL_TYPE_INTEGER,
464 .name = "MPEG4 Minimum QP value",
471 .id = V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP,
472 .type = V4L2_CTRL_TYPE_INTEGER,
473 .name = "MPEG4 Maximum QP value",
480 .id = V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP,
481 .type = V4L2_CTRL_TYPE_INTEGER,
482 .name = "MPEG4 P frame QP value",
489 .id = V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP,
490 .type = V4L2_CTRL_TYPE_INTEGER,
491 .name = "MPEG4 B frame QP value",
498 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK,
499 .type = V4L2_CTRL_TYPE_BOOLEAN,
500 .name = "H264 Dark Reg Adaptive RC",
507 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH,
508 .type = V4L2_CTRL_TYPE_BOOLEAN,
509 .name = "H264 Smooth Reg Adaptive RC",
516 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC,
517 .type = V4L2_CTRL_TYPE_BOOLEAN,
518 .name = "H264 Static Reg Adaptive RC",
525 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY,
526 .type = V4L2_CTRL_TYPE_BOOLEAN,
527 .name = "H264 Activity Reg Adaptive RC",
534 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
535 .type = V4L2_CTRL_TYPE_BOOLEAN,
542 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
543 .type = V4L2_CTRL_TYPE_MENU,
544 .minimum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
545 .maximum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
546 .default_value = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
550 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
551 .type = V4L2_CTRL_TYPE_INTEGER,
553 .maximum = (1 << 16) - 1,
558 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
559 .type = V4L2_CTRL_TYPE_INTEGER,
561 .maximum = (1 << 16) - 1,
566 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
567 .type = V4L2_CTRL_TYPE_BOOLEAN,
574 .id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
575 .type = V4L2_CTRL_TYPE_INTEGER,
577 .maximum = (1 << 16) - 1,
582 .id = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
583 .type = V4L2_CTRL_TYPE_MENU,
584 .minimum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
585 .maximum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE,
586 .default_value = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
590 .id = V4L2_CID_MPEG_VIDEO_MPEG4_QPEL,
591 .type = V4L2_CTRL_TYPE_BOOLEAN,
598 .id = V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS,
599 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
600 .maximum = V4L2_CID_MPEG_VIDEO_VPX_8_PARTITIONS,
601 .default_value = V4L2_CID_MPEG_VIDEO_VPX_1_PARTITION,
605 .id = V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4,
606 .type = V4L2_CTRL_TYPE_BOOLEAN,
613 .id = V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES,
614 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
615 .maximum = V4L2_CID_MPEG_VIDEO_VPX_2_REF_FRAME,
616 .default_value = V4L2_CID_MPEG_VIDEO_VPX_1_REF_FRAME,
620 .id = V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL,
621 .type = V4L2_CTRL_TYPE_INTEGER,
628 .id = V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS,
629 .type = V4L2_CTRL_TYPE_INTEGER,
636 .id = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD,
637 .type = V4L2_CTRL_TYPE_INTEGER,
639 .maximum = (1 << 16) - 1,
644 .id = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL,
645 .type = V4L2_CTRL_TYPE_MENU,
646 .minimum = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV,
647 .maximum = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_REF_PERIOD,
648 .default_value = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV,
652 .id = V4L2_CID_MPEG_VIDEO_VPX_MAX_QP,
653 .type = V4L2_CTRL_TYPE_INTEGER,
657 .default_value = 127,
660 .id = V4L2_CID_MPEG_VIDEO_VPX_MIN_QP,
661 .type = V4L2_CTRL_TYPE_INTEGER,
668 .id = V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP,
669 .type = V4L2_CTRL_TYPE_INTEGER,
676 .id = V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP,
677 .type = V4L2_CTRL_TYPE_INTEGER,
684 .id = V4L2_CID_MPEG_VIDEO_VPX_PROFILE,
685 .type = V4L2_CTRL_TYPE_INTEGER,
692 .id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
693 .type = V4L2_CTRL_TYPE_INTEGER,
694 .name = "Minimum number of output bufs",
703 #define NUM_CTRLS ARRAY_SIZE(controls)
704 static const char * const *mfc51_get_menu(u32 id)
706 static const char * const mfc51_video_frame_skip[] = {
712 static const char * const mfc51_video_force_frame[] = {
719 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
720 return mfc51_video_frame_skip;
721 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
722 return mfc51_video_force_frame;
727 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
729 mfc_debug(2, "src=%d, dst=%d, state=%d\n",
730 ctx->src_queue_cnt, ctx->dst_queue_cnt, ctx->state);
731 /* context is ready to make header */
732 if (ctx->state == MFCINST_GOT_INST && ctx->dst_queue_cnt >= 1)
734 /* context is ready to encode a frame */
735 if ((ctx->state == MFCINST_RUNNING ||
736 ctx->state == MFCINST_HEAD_PRODUCED) &&
737 ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
739 /* context is ready to encode remaining frames */
740 if (ctx->state == MFCINST_FINISHING &&
741 ctx->dst_queue_cnt >= 1)
743 mfc_debug(2, "ctx is not ready\n");
747 static void cleanup_ref_queue(struct s5p_mfc_ctx *ctx)
749 struct s5p_mfc_buf *mb_entry;
751 /* move buffers in ref queue to src queue */
752 while (!list_empty(&ctx->ref_queue)) {
753 mb_entry = list_entry((&ctx->ref_queue)->next,
754 struct s5p_mfc_buf, list);
755 list_del(&mb_entry->list);
756 ctx->ref_queue_cnt--;
757 list_add_tail(&mb_entry->list, &ctx->src_queue);
758 ctx->src_queue_cnt++;
760 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
761 ctx->src_queue_cnt, ctx->ref_queue_cnt);
762 INIT_LIST_HEAD(&ctx->ref_queue);
763 ctx->ref_queue_cnt = 0;
766 static int enc_pre_seq_start(struct s5p_mfc_ctx *ctx)
768 struct s5p_mfc_dev *dev = ctx->dev;
769 struct s5p_mfc_buf *dst_mb;
770 unsigned long dst_addr;
771 unsigned int dst_size;
774 spin_lock_irqsave(&dev->irqlock, flags);
775 dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
776 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
777 dst_size = vb2_plane_size(dst_mb->b, 0);
778 s5p_mfc_hw_call_void(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
780 spin_unlock_irqrestore(&dev->irqlock, flags);
784 static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
786 struct s5p_mfc_dev *dev = ctx->dev;
787 struct s5p_mfc_enc_params *p = &ctx->enc_params;
788 struct s5p_mfc_buf *dst_mb;
790 unsigned int enc_pb_count;
792 if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
793 spin_lock_irqsave(&dev->irqlock, flags);
794 if (!list_empty(&ctx->dst_queue)) {
795 dst_mb = list_entry(ctx->dst_queue.next,
796 struct s5p_mfc_buf, list);
797 list_del(&dst_mb->list);
798 ctx->dst_queue_cnt--;
799 vb2_set_plane_payload(dst_mb->b, 0,
800 s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size,
802 vb2_buffer_done(dst_mb->b, VB2_BUF_STATE_DONE);
804 spin_unlock_irqrestore(&dev->irqlock, flags);
807 if (!IS_MFCV6_PLUS(dev)) {
808 ctx->state = MFCINST_RUNNING;
809 if (s5p_mfc_ctx_ready(ctx))
810 set_work_bit_irqsave(ctx);
811 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
813 enc_pb_count = s5p_mfc_hw_call(dev->mfc_ops,
814 get_enc_dpb_count, dev);
815 if (ctx->pb_count < enc_pb_count)
816 ctx->pb_count = enc_pb_count;
817 ctx->state = MFCINST_HEAD_PRODUCED;
823 static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
825 struct s5p_mfc_dev *dev = ctx->dev;
826 struct s5p_mfc_buf *dst_mb;
827 struct s5p_mfc_buf *src_mb;
829 unsigned long src_y_addr, src_c_addr, dst_addr;
830 unsigned int dst_size;
832 spin_lock_irqsave(&dev->irqlock, flags);
833 src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
834 src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 0);
835 src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 1);
836 s5p_mfc_hw_call_void(dev->mfc_ops, set_enc_frame_buffer, ctx,
837 src_y_addr, src_c_addr);
838 spin_unlock_irqrestore(&dev->irqlock, flags);
840 spin_lock_irqsave(&dev->irqlock, flags);
841 dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
842 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
843 dst_size = vb2_plane_size(dst_mb->b, 0);
844 s5p_mfc_hw_call_void(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
846 spin_unlock_irqrestore(&dev->irqlock, flags);
851 static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
853 struct s5p_mfc_dev *dev = ctx->dev;
854 struct s5p_mfc_buf *mb_entry;
855 unsigned long enc_y_addr, enc_c_addr;
856 unsigned long mb_y_addr, mb_c_addr;
858 unsigned int strm_size;
861 slice_type = s5p_mfc_hw_call(dev->mfc_ops, get_enc_slice_type, dev);
862 strm_size = s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev);
863 mfc_debug(2, "Encoded slice type: %d\n", slice_type);
864 mfc_debug(2, "Encoded stream size: %d\n", strm_size);
865 mfc_debug(2, "Display order: %d\n",
866 mfc_read(dev, S5P_FIMV_ENC_SI_PIC_CNT));
867 spin_lock_irqsave(&dev->irqlock, flags);
868 if (slice_type >= 0) {
869 s5p_mfc_hw_call_void(dev->mfc_ops, get_enc_frame_buffer, ctx,
870 &enc_y_addr, &enc_c_addr);
871 list_for_each_entry(mb_entry, &ctx->src_queue, list) {
872 mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
873 mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
874 if ((enc_y_addr == mb_y_addr) &&
875 (enc_c_addr == mb_c_addr)) {
876 list_del(&mb_entry->list);
877 ctx->src_queue_cnt--;
878 vb2_buffer_done(mb_entry->b,
883 list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
884 mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
885 mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
886 if ((enc_y_addr == mb_y_addr) &&
887 (enc_c_addr == mb_c_addr)) {
888 list_del(&mb_entry->list);
889 ctx->ref_queue_cnt--;
890 vb2_buffer_done(mb_entry->b,
896 if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
897 mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
899 if (mb_entry->flags & MFC_BUF_FLAG_USED) {
900 list_del(&mb_entry->list);
901 ctx->src_queue_cnt--;
902 list_add_tail(&mb_entry->list, &ctx->ref_queue);
903 ctx->ref_queue_cnt++;
905 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
906 ctx->src_queue_cnt, ctx->ref_queue_cnt);
908 if ((ctx->dst_queue_cnt > 0) && (strm_size > 0)) {
909 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
911 list_del(&mb_entry->list);
912 ctx->dst_queue_cnt--;
913 switch (slice_type) {
914 case S5P_FIMV_ENC_SI_SLICE_TYPE_I:
915 mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
917 case S5P_FIMV_ENC_SI_SLICE_TYPE_P:
918 mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
920 case S5P_FIMV_ENC_SI_SLICE_TYPE_B:
921 mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_BFRAME;
924 vb2_set_plane_payload(mb_entry->b, 0, strm_size);
925 vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
927 spin_unlock_irqrestore(&dev->irqlock, flags);
928 if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0))
933 static struct s5p_mfc_codec_ops encoder_codec_ops = {
934 .pre_seq_start = enc_pre_seq_start,
935 .post_seq_start = enc_post_seq_start,
936 .pre_frame_start = enc_pre_frame_start,
937 .post_frame_start = enc_post_frame_start,
940 /* Query capabilities of the device */
941 static int vidioc_querycap(struct file *file, void *priv,
942 struct v4l2_capability *cap)
944 struct s5p_mfc_dev *dev = video_drvdata(file);
946 strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
947 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
948 cap->bus_info[0] = 0;
950 * This is only a mem-to-mem video device. The capture and output
951 * device capability flags are left only for backward compatibility
952 * and are scheduled for removal.
954 cap->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
955 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
959 static int vidioc_enum_fmt(struct file *file, struct v4l2_fmtdesc *f,
962 struct s5p_mfc_dev *dev = video_drvdata(file);
963 struct s5p_mfc_fmt *fmt;
966 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
967 if (out && formats[i].type != MFC_FMT_RAW)
969 else if (!out && formats[i].type != MFC_FMT_ENC)
971 else if ((dev->variant->version_bit & formats[i].versions) == 0)
976 strlcpy(f->description, fmt->name,
977 sizeof(f->description));
978 f->pixelformat = fmt->fourcc;
986 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
987 struct v4l2_fmtdesc *f)
989 return vidioc_enum_fmt(file, f, false);
992 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
993 struct v4l2_fmtdesc *f)
995 return vidioc_enum_fmt(file, f, true);
998 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1000 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1001 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1003 mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
1004 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1005 /* This is run on output (encoder dest) */
1006 pix_fmt_mp->width = 0;
1007 pix_fmt_mp->height = 0;
1008 pix_fmt_mp->field = V4L2_FIELD_NONE;
1009 pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
1010 pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
1012 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
1013 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
1014 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1015 /* This is run on capture (encoder src) */
1016 pix_fmt_mp->width = ctx->img_width;
1017 pix_fmt_mp->height = ctx->img_height;
1019 pix_fmt_mp->field = V4L2_FIELD_NONE;
1020 pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
1021 pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
1023 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1024 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1025 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1026 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1028 mfc_err("invalid buf type\n");
1034 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1036 struct s5p_mfc_dev *dev = video_drvdata(file);
1037 struct s5p_mfc_fmt *fmt;
1038 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1040 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1041 fmt = find_format(f, MFC_FMT_ENC);
1043 mfc_err("failed to try output format\n");
1046 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
1047 mfc_err("must be set encoding output size\n");
1050 if ((dev->variant->version_bit & fmt->versions) == 0) {
1051 mfc_err("Unsupported format by this MFC version.\n");
1055 pix_fmt_mp->plane_fmt[0].bytesperline =
1056 pix_fmt_mp->plane_fmt[0].sizeimage;
1057 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1058 fmt = find_format(f, MFC_FMT_RAW);
1060 mfc_err("failed to try output format\n");
1064 if (fmt->num_planes != pix_fmt_mp->num_planes) {
1065 mfc_err("failed to try output format\n");
1068 if ((dev->variant->version_bit & fmt->versions) == 0) {
1069 mfc_err("Unsupported format by this MFC version.\n");
1073 v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
1074 &pix_fmt_mp->height, 4, 1080, 1, 0);
1076 mfc_err("invalid buf type\n");
1082 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
1084 struct s5p_mfc_dev *dev = video_drvdata(file);
1085 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1086 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1089 ret = vidioc_try_fmt(file, priv, f);
1092 if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
1093 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
1097 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1098 /* dst_fmt is validated by call to vidioc_try_fmt */
1099 ctx->dst_fmt = find_format(f, MFC_FMT_ENC);
1100 ctx->state = MFCINST_INIT;
1101 ctx->codec_mode = ctx->dst_fmt->codec_mode;
1102 ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
1103 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
1104 ctx->dst_bufs_cnt = 0;
1105 ctx->capture_state = QUEUE_FREE;
1106 ret = s5p_mfc_open_mfc_inst(dev, ctx);
1107 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1108 /* src_fmt is validated by call to vidioc_try_fmt */
1109 ctx->src_fmt = find_format(f, MFC_FMT_RAW);
1110 ctx->img_width = pix_fmt_mp->width;
1111 ctx->img_height = pix_fmt_mp->height;
1112 mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
1113 mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1114 pix_fmt_mp->width, pix_fmt_mp->height,
1115 ctx->img_width, ctx->img_height);
1117 s5p_mfc_hw_call_void(dev->mfc_ops, enc_calc_src_size, ctx);
1118 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1119 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1120 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1121 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1123 ctx->src_bufs_cnt = 0;
1124 ctx->output_state = QUEUE_FREE;
1126 mfc_err("invalid buf type\n");
1134 static int vidioc_reqbufs(struct file *file, void *priv,
1135 struct v4l2_requestbuffers *reqbufs)
1137 struct s5p_mfc_dev *dev = video_drvdata(file);
1138 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1141 /* if memory is not mmp or userptr return error */
1142 if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1143 (reqbufs->memory != V4L2_MEMORY_USERPTR))
1145 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1146 if (reqbufs->count == 0) {
1147 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1148 ctx->capture_state = QUEUE_FREE;
1151 if (ctx->capture_state != QUEUE_FREE) {
1152 mfc_err("invalid capture state: %d\n",
1153 ctx->capture_state);
1156 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1158 mfc_err("error in vb2_reqbufs() for E(D)\n");
1161 ctx->capture_state = QUEUE_BUFS_REQUESTED;
1163 ret = s5p_mfc_hw_call(ctx->dev->mfc_ops,
1164 alloc_codec_buffers, ctx);
1166 mfc_err("Failed to allocate encoding buffers\n");
1168 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1171 } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1172 if (reqbufs->count == 0) {
1173 mfc_debug(2, "Freeing buffers\n");
1174 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1175 s5p_mfc_hw_call_void(dev->mfc_ops, release_codec_buffers,
1177 ctx->output_state = QUEUE_FREE;
1180 if (ctx->output_state != QUEUE_FREE) {
1181 mfc_err("invalid output state: %d\n",
1186 if (IS_MFCV6_PLUS(dev)) {
1187 /* Check for min encoder buffers */
1188 if (ctx->pb_count &&
1189 (reqbufs->count < ctx->pb_count)) {
1190 reqbufs->count = ctx->pb_count;
1191 mfc_debug(2, "Minimum %d output buffers needed\n",
1194 ctx->pb_count = reqbufs->count;
1198 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1200 mfc_err("error in vb2_reqbufs() for E(S)\n");
1203 ctx->output_state = QUEUE_BUFS_REQUESTED;
1205 mfc_err("invalid buf type\n");
1211 static int vidioc_querybuf(struct file *file, void *priv,
1212 struct v4l2_buffer *buf)
1214 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1217 /* if memory is not mmp or userptr return error */
1218 if ((buf->memory != V4L2_MEMORY_MMAP) &&
1219 (buf->memory != V4L2_MEMORY_USERPTR))
1221 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1222 if (ctx->state != MFCINST_GOT_INST) {
1223 mfc_err("invalid context state: %d\n", ctx->state);
1226 ret = vb2_querybuf(&ctx->vq_dst, buf);
1228 mfc_err("error in vb2_querybuf() for E(D)\n");
1231 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1232 } else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1233 ret = vb2_querybuf(&ctx->vq_src, buf);
1235 mfc_err("error in vb2_querybuf() for E(S)\n");
1239 mfc_err("invalid buf type\n");
1245 /* Queue a buffer */
1246 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1248 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1250 if (ctx->state == MFCINST_ERROR) {
1251 mfc_err("Call on QBUF after unrecoverable error\n");
1254 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1255 if (ctx->state == MFCINST_FINISHING) {
1256 mfc_err("Call on QBUF after EOS command\n");
1259 return vb2_qbuf(&ctx->vq_src, buf);
1260 } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1261 return vb2_qbuf(&ctx->vq_dst, buf);
1266 /* Dequeue a buffer */
1267 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1269 const struct v4l2_event ev = {
1270 .type = V4L2_EVENT_EOS
1272 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1275 if (ctx->state == MFCINST_ERROR) {
1276 mfc_err("Call on DQBUF after unrecoverable error\n");
1279 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1280 ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1281 } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1282 ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1283 if (ret == 0 && ctx->state == MFCINST_FINISHED
1284 && list_empty(&ctx->vq_dst.done_list))
1285 v4l2_event_queue_fh(&ctx->fh, &ev);
1293 /* Export DMA buffer */
1294 static int vidioc_expbuf(struct file *file, void *priv,
1295 struct v4l2_exportbuffer *eb)
1297 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1299 if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1300 return vb2_expbuf(&ctx->vq_src, eb);
1301 if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1302 return vb2_expbuf(&ctx->vq_dst, eb);
1307 static int vidioc_streamon(struct file *file, void *priv,
1308 enum v4l2_buf_type type)
1310 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1312 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1313 return vb2_streamon(&ctx->vq_src, type);
1314 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1315 return vb2_streamon(&ctx->vq_dst, type);
1319 /* Stream off, which equals to a pause */
1320 static int vidioc_streamoff(struct file *file, void *priv,
1321 enum v4l2_buf_type type)
1323 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1325 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1326 return vb2_streamoff(&ctx->vq_src, type);
1327 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1328 return vb2_streamoff(&ctx->vq_dst, type);
1332 static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
1334 static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
1335 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0 */ 10,
1336 /* V4L2_MPEG_VIDEO_H264_LEVEL_1B */ 9,
1337 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1 */ 11,
1338 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2 */ 12,
1339 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3 */ 13,
1340 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0 */ 20,
1341 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1 */ 21,
1342 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2 */ 22,
1343 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0 */ 30,
1344 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1 */ 31,
1345 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2 */ 32,
1346 /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0 */ 40,
1351 static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
1353 static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
1354 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 */ 0,
1355 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B */ 9,
1356 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 */ 1,
1357 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 */ 2,
1358 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 */ 3,
1359 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B */ 7,
1360 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 */ 4,
1361 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 */ 5,
1366 static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
1368 static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
1369 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED */ 0,
1370 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1 */ 1,
1371 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11 */ 2,
1372 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11 */ 3,
1373 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11 */ 4,
1374 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33 */ 5,
1375 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11 */ 6,
1376 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11 */ 7,
1377 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11 */ 8,
1378 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33 */ 9,
1379 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11 */ 10,
1380 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11 */ 11,
1381 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33 */ 12,
1382 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99 */ 13,
1383 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3 */ 14,
1384 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2 */ 15,
1385 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1 */ 16,
1386 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED */ 255,
1391 static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
1393 struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1394 struct s5p_mfc_dev *dev = ctx->dev;
1395 struct s5p_mfc_enc_params *p = &ctx->enc_params;
1399 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1400 p->gop_size = ctrl->val;
1402 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1403 p->slice_mode = ctrl->val;
1405 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1406 p->slice_mb = ctrl->val;
1408 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1409 p->slice_bit = ctrl->val * 8;
1411 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1412 p->intra_refresh_mb = ctrl->val;
1414 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
1417 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
1418 p->pad_luma = (ctrl->val >> 16) & 0xff;
1419 p->pad_cb = (ctrl->val >> 8) & 0xff;
1420 p->pad_cr = (ctrl->val >> 0) & 0xff;
1422 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1423 p->rc_frame = ctrl->val;
1425 case V4L2_CID_MPEG_VIDEO_BITRATE:
1426 p->rc_bitrate = ctrl->val;
1428 case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
1429 p->rc_reaction_coeff = ctrl->val;
1431 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1432 ctx->force_frame_type = ctrl->val;
1434 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1435 p->vbv_size = ctrl->val;
1437 case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
1438 p->mv_h_range = ctrl->val;
1440 case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
1441 p->mv_v_range = ctrl->val;
1443 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
1444 p->codec.h264.cpb_size = ctrl->val;
1446 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1447 p->seq_hdr_mode = ctrl->val;
1449 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1450 p->frame_skip_mode = ctrl->val;
1452 case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
1453 p->fixed_target_bit = ctrl->val;
1455 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1456 p->num_b_frame = ctrl->val;
1458 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1459 switch (ctrl->val) {
1460 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
1461 p->codec.h264.profile =
1462 S5P_FIMV_ENC_PROFILE_H264_MAIN;
1464 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
1465 p->codec.h264.profile =
1466 S5P_FIMV_ENC_PROFILE_H264_HIGH;
1468 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1469 p->codec.h264.profile =
1470 S5P_FIMV_ENC_PROFILE_H264_BASELINE;
1472 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
1473 if (IS_MFCV6_PLUS(dev))
1474 p->codec.h264.profile =
1475 S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE;
1483 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1484 p->codec.h264.level_v4l2 = ctrl->val;
1485 p->codec.h264.level = h264_level(ctrl->val);
1486 if (p->codec.h264.level < 0) {
1487 mfc_err("Level number is wrong\n");
1488 ret = p->codec.h264.level;
1491 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1492 p->codec.mpeg4.level_v4l2 = ctrl->val;
1493 p->codec.mpeg4.level = mpeg4_level(ctrl->val);
1494 if (p->codec.mpeg4.level < 0) {
1495 mfc_err("Level number is wrong\n");
1496 ret = p->codec.mpeg4.level;
1499 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1500 p->codec.h264.loop_filter_mode = ctrl->val;
1502 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1503 p->codec.h264.loop_filter_alpha = ctrl->val;
1505 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1506 p->codec.h264.loop_filter_beta = ctrl->val;
1508 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1509 p->codec.h264.entropy_mode = ctrl->val;
1511 case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
1512 p->codec.h264.num_ref_pic_4p = ctrl->val;
1514 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1515 p->codec.h264._8x8_transform = ctrl->val;
1517 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1518 p->rc_mb = ctrl->val;
1520 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1521 p->codec.h264.rc_frame_qp = ctrl->val;
1523 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1524 p->codec.h264.rc_min_qp = ctrl->val;
1526 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1527 p->codec.h264.rc_max_qp = ctrl->val;
1529 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1530 p->codec.h264.rc_p_frame_qp = ctrl->val;
1532 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
1533 p->codec.h264.rc_b_frame_qp = ctrl->val;
1535 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1536 case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
1537 p->codec.mpeg4.rc_frame_qp = ctrl->val;
1539 case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
1540 case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
1541 p->codec.mpeg4.rc_min_qp = ctrl->val;
1543 case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
1544 case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
1545 p->codec.mpeg4.rc_max_qp = ctrl->val;
1547 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1548 case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
1549 p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
1551 case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
1552 case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
1553 p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
1555 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
1556 p->codec.h264.rc_mb_dark = ctrl->val;
1558 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
1559 p->codec.h264.rc_mb_smooth = ctrl->val;
1561 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
1562 p->codec.h264.rc_mb_static = ctrl->val;
1564 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
1565 p->codec.h264.rc_mb_activity = ctrl->val;
1567 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1568 p->codec.h264.vui_sar = ctrl->val;
1570 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1571 p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
1573 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
1574 p->codec.h264.vui_ext_sar_width = ctrl->val;
1576 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
1577 p->codec.h264.vui_ext_sar_height = ctrl->val;
1579 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1580 p->codec.h264.open_gop = !ctrl->val;
1582 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
1583 p->codec.h264.open_gop_size = ctrl->val;
1585 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1586 switch (ctrl->val) {
1587 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
1588 p->codec.mpeg4.profile =
1589 S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
1591 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
1592 p->codec.mpeg4.profile =
1593 S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
1599 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1600 p->codec.mpeg4.quarter_pixel = ctrl->val;
1602 case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
1603 p->codec.vp8.num_partitions = ctrl->val;
1605 case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4:
1606 p->codec.vp8.imd_4x4 = ctrl->val;
1608 case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
1609 p->codec.vp8.num_ref = ctrl->val;
1611 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL:
1612 p->codec.vp8.filter_level = ctrl->val;
1614 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS:
1615 p->codec.vp8.filter_sharpness = ctrl->val;
1617 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD:
1618 p->codec.vp8.golden_frame_ref_period = ctrl->val;
1620 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
1621 p->codec.vp8.golden_frame_sel = ctrl->val;
1623 case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
1624 p->codec.vp8.rc_min_qp = ctrl->val;
1626 case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
1627 p->codec.vp8.rc_max_qp = ctrl->val;
1629 case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP:
1630 p->codec.vp8.rc_frame_qp = ctrl->val;
1632 case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP:
1633 p->codec.vp8.rc_p_frame_qp = ctrl->val;
1635 case V4L2_CID_MPEG_VIDEO_VPX_PROFILE:
1636 p->codec.vp8.profile = ctrl->val;
1639 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
1640 ctrl->id, ctrl->val);
1646 static int s5p_mfc_enc_g_v_ctrl(struct v4l2_ctrl *ctrl)
1648 struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1649 struct s5p_mfc_dev *dev = ctx->dev;
1652 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
1653 if (ctx->state >= MFCINST_HEAD_PARSED &&
1654 ctx->state < MFCINST_ABORT) {
1655 ctrl->val = ctx->pb_count;
1657 } else if (ctx->state != MFCINST_INIT) {
1658 v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
1661 /* Should wait for the header to be produced */
1662 s5p_mfc_wait_for_done_ctx(ctx,
1663 S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
1664 if (ctx->state >= MFCINST_HEAD_PARSED &&
1665 ctx->state < MFCINST_ABORT) {
1666 ctrl->val = ctx->pb_count;
1668 v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
1676 static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
1677 .s_ctrl = s5p_mfc_enc_s_ctrl,
1678 .g_volatile_ctrl = s5p_mfc_enc_g_v_ctrl,
1681 static int vidioc_s_parm(struct file *file, void *priv,
1682 struct v4l2_streamparm *a)
1684 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1686 if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1687 ctx->enc_params.rc_framerate_num =
1688 a->parm.output.timeperframe.denominator;
1689 ctx->enc_params.rc_framerate_denom =
1690 a->parm.output.timeperframe.numerator;
1692 mfc_err("Setting FPS is only possible for the output queue\n");
1698 static int vidioc_g_parm(struct file *file, void *priv,
1699 struct v4l2_streamparm *a)
1701 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1703 if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1704 a->parm.output.timeperframe.denominator =
1705 ctx->enc_params.rc_framerate_num;
1706 a->parm.output.timeperframe.numerator =
1707 ctx->enc_params.rc_framerate_denom;
1709 mfc_err("Setting FPS is only possible for the output queue\n");
1715 static int vidioc_encoder_cmd(struct file *file, void *priv,
1716 struct v4l2_encoder_cmd *cmd)
1718 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1719 struct s5p_mfc_dev *dev = ctx->dev;
1720 struct s5p_mfc_buf *buf;
1721 unsigned long flags;
1724 case V4L2_ENC_CMD_STOP:
1725 if (cmd->flags != 0)
1728 if (!ctx->vq_src.streaming)
1731 spin_lock_irqsave(&dev->irqlock, flags);
1732 if (list_empty(&ctx->src_queue)) {
1733 mfc_debug(2, "EOS: empty src queue, entering finishing state\n");
1734 ctx->state = MFCINST_FINISHING;
1735 if (s5p_mfc_ctx_ready(ctx))
1736 set_work_bit_irqsave(ctx);
1737 spin_unlock_irqrestore(&dev->irqlock, flags);
1738 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
1740 mfc_debug(2, "EOS: marking last buffer of stream\n");
1741 buf = list_entry(ctx->src_queue.prev,
1742 struct s5p_mfc_buf, list);
1743 if (buf->flags & MFC_BUF_FLAG_USED)
1744 ctx->state = MFCINST_FINISHING;
1746 buf->flags |= MFC_BUF_FLAG_EOS;
1747 spin_unlock_irqrestore(&dev->irqlock, flags);
1757 static int vidioc_subscribe_event(struct v4l2_fh *fh,
1758 const struct v4l2_event_subscription *sub)
1760 switch (sub->type) {
1761 case V4L2_EVENT_EOS:
1762 return v4l2_event_subscribe(fh, sub, 2, NULL);
1768 static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
1769 .vidioc_querycap = vidioc_querycap,
1770 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
1771 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
1772 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1773 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1774 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1775 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1776 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1777 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1778 .vidioc_reqbufs = vidioc_reqbufs,
1779 .vidioc_querybuf = vidioc_querybuf,
1780 .vidioc_qbuf = vidioc_qbuf,
1781 .vidioc_dqbuf = vidioc_dqbuf,
1782 .vidioc_expbuf = vidioc_expbuf,
1783 .vidioc_streamon = vidioc_streamon,
1784 .vidioc_streamoff = vidioc_streamoff,
1785 .vidioc_s_parm = vidioc_s_parm,
1786 .vidioc_g_parm = vidioc_g_parm,
1787 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1788 .vidioc_subscribe_event = vidioc_subscribe_event,
1789 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1792 static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
1798 if (fmt->num_planes != vb->num_planes) {
1799 mfc_err("invalid plane number for the format\n");
1802 for (i = 0; i < fmt->num_planes; i++) {
1803 dma_addr_t dma = vb2_dma_contig_plane_dma_addr(vb, i);
1805 mfc_err("failed to get plane cookie\n");
1808 mfc_debug(2, "index: %d, plane[%d] cookie: %pad\n",
1809 vb->v4l2_buf.index, i, &dma);
1814 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
1815 const struct v4l2_format *fmt,
1816 unsigned int *buf_count, unsigned int *plane_count,
1817 unsigned int psize[], void *allocators[])
1819 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1820 struct s5p_mfc_dev *dev = ctx->dev;
1822 if (ctx->state != MFCINST_GOT_INST) {
1823 mfc_err("inavlid state: %d\n", ctx->state);
1826 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1828 *plane_count = ctx->dst_fmt->num_planes;
1830 *plane_count = MFC_ENC_CAP_PLANE_COUNT;
1833 if (*buf_count > MFC_MAX_BUFFERS)
1834 *buf_count = MFC_MAX_BUFFERS;
1835 psize[0] = ctx->enc_dst_buf_size;
1836 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1837 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1839 *plane_count = ctx->src_fmt->num_planes;
1841 *plane_count = MFC_ENC_OUT_PLANE_COUNT;
1845 if (*buf_count > MFC_MAX_BUFFERS)
1846 *buf_count = MFC_MAX_BUFFERS;
1848 psize[0] = ctx->luma_size;
1849 psize[1] = ctx->chroma_size;
1851 if (IS_MFCV6_PLUS(dev)) {
1853 ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1855 ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1858 ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1860 ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1863 mfc_err("inavlid queue type: %d\n", vq->type);
1869 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
1871 struct vb2_queue *vq = vb->vb2_queue;
1872 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1876 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1877 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1880 i = vb->v4l2_buf.index;
1881 ctx->dst_bufs[i].b = vb;
1882 ctx->dst_bufs[i].cookie.stream =
1883 vb2_dma_contig_plane_dma_addr(vb, 0);
1884 ctx->dst_bufs_cnt++;
1885 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1886 ret = check_vb_with_fmt(ctx->src_fmt, vb);
1889 i = vb->v4l2_buf.index;
1890 ctx->src_bufs[i].b = vb;
1891 ctx->src_bufs[i].cookie.raw.luma =
1892 vb2_dma_contig_plane_dma_addr(vb, 0);
1893 ctx->src_bufs[i].cookie.raw.chroma =
1894 vb2_dma_contig_plane_dma_addr(vb, 1);
1895 ctx->src_bufs_cnt++;
1897 mfc_err("inavlid queue type: %d\n", vq->type);
1903 static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
1905 struct vb2_queue *vq = vb->vb2_queue;
1906 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1909 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1910 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1913 mfc_debug(2, "plane size: %ld, dst size: %zu\n",
1914 vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
1915 if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
1916 mfc_err("plane size is too small for capture\n");
1919 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1920 ret = check_vb_with_fmt(ctx->src_fmt, vb);
1923 mfc_debug(2, "plane size: %ld, luma size: %d\n",
1924 vb2_plane_size(vb, 0), ctx->luma_size);
1925 mfc_debug(2, "plane size: %ld, chroma size: %d\n",
1926 vb2_plane_size(vb, 1), ctx->chroma_size);
1927 if (vb2_plane_size(vb, 0) < ctx->luma_size ||
1928 vb2_plane_size(vb, 1) < ctx->chroma_size) {
1929 mfc_err("plane size is too small for output\n");
1933 mfc_err("inavlid queue type: %d\n", vq->type);
1939 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
1941 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1942 struct s5p_mfc_dev *dev = ctx->dev;
1944 if (IS_MFCV6_PLUS(dev) &&
1945 (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
1947 if ((ctx->state == MFCINST_GOT_INST) &&
1948 (dev->curr_ctx == ctx->num) && dev->hw_lock) {
1949 s5p_mfc_wait_for_done_ctx(ctx,
1950 S5P_MFC_R2H_CMD_SEQ_DONE_RET,
1954 if (ctx->src_bufs_cnt < ctx->pb_count) {
1955 mfc_err("Need minimum %d OUTPUT buffers\n",
1961 /* If context is ready then dev = work->data;schedule it to run */
1962 if (s5p_mfc_ctx_ready(ctx))
1963 set_work_bit_irqsave(ctx);
1964 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
1969 static void s5p_mfc_stop_streaming(struct vb2_queue *q)
1971 unsigned long flags;
1972 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1973 struct s5p_mfc_dev *dev = ctx->dev;
1975 if ((ctx->state == MFCINST_FINISHING ||
1976 ctx->state == MFCINST_RUNNING) &&
1977 dev->curr_ctx == ctx->num && dev->hw_lock) {
1978 ctx->state = MFCINST_ABORT;
1979 s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET,
1982 ctx->state = MFCINST_FINISHED;
1983 spin_lock_irqsave(&dev->irqlock, flags);
1984 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1985 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
1986 &ctx->dst_queue, &ctx->vq_dst);
1987 INIT_LIST_HEAD(&ctx->dst_queue);
1988 ctx->dst_queue_cnt = 0;
1990 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1991 cleanup_ref_queue(ctx);
1992 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
1994 INIT_LIST_HEAD(&ctx->src_queue);
1995 ctx->src_queue_cnt = 0;
1997 spin_unlock_irqrestore(&dev->irqlock, flags);
2000 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
2002 struct vb2_queue *vq = vb->vb2_queue;
2003 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2004 struct s5p_mfc_dev *dev = ctx->dev;
2005 unsigned long flags;
2006 struct s5p_mfc_buf *mfc_buf;
2008 if (ctx->state == MFCINST_ERROR) {
2009 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2010 cleanup_ref_queue(ctx);
2013 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2014 mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
2015 mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2016 /* Mark destination as available for use by MFC */
2017 spin_lock_irqsave(&dev->irqlock, flags);
2018 list_add_tail(&mfc_buf->list, &ctx->dst_queue);
2019 ctx->dst_queue_cnt++;
2020 spin_unlock_irqrestore(&dev->irqlock, flags);
2021 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2022 mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
2023 mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2024 spin_lock_irqsave(&dev->irqlock, flags);
2025 list_add_tail(&mfc_buf->list, &ctx->src_queue);
2026 ctx->src_queue_cnt++;
2027 spin_unlock_irqrestore(&dev->irqlock, flags);
2029 mfc_err("unsupported buffer type (%d)\n", vq->type);
2031 if (s5p_mfc_ctx_ready(ctx))
2032 set_work_bit_irqsave(ctx);
2033 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
2036 static struct vb2_ops s5p_mfc_enc_qops = {
2037 .queue_setup = s5p_mfc_queue_setup,
2038 .wait_prepare = vb2_ops_wait_prepare,
2039 .wait_finish = vb2_ops_wait_finish,
2040 .buf_init = s5p_mfc_buf_init,
2041 .buf_prepare = s5p_mfc_buf_prepare,
2042 .start_streaming = s5p_mfc_start_streaming,
2043 .stop_streaming = s5p_mfc_stop_streaming,
2044 .buf_queue = s5p_mfc_buf_queue,
2047 struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
2049 return &encoder_codec_ops;
2052 struct vb2_ops *get_enc_queue_ops(void)
2054 return &s5p_mfc_enc_qops;
2057 const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
2059 return &s5p_mfc_enc_ioctl_ops;
2062 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
2063 && V4L2_CTRL_DRIVER_PRIV(x))
2065 int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
2067 struct v4l2_ctrl_config cfg;
2070 v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
2071 if (ctx->ctrl_handler.error) {
2072 mfc_err("v4l2_ctrl_handler_init failed\n");
2073 return ctx->ctrl_handler.error;
2075 for (i = 0; i < NUM_CTRLS; i++) {
2076 if (IS_MFC51_PRIV(controls[i].id)) {
2077 memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
2078 cfg.ops = &s5p_mfc_enc_ctrl_ops;
2079 cfg.id = controls[i].id;
2080 cfg.min = controls[i].minimum;
2081 cfg.max = controls[i].maximum;
2082 cfg.def = controls[i].default_value;
2083 cfg.name = controls[i].name;
2084 cfg.type = controls[i].type;
2087 if (cfg.type == V4L2_CTRL_TYPE_MENU) {
2089 cfg.menu_skip_mask = cfg.menu_skip_mask;
2090 cfg.qmenu = mfc51_get_menu(cfg.id);
2092 cfg.step = controls[i].step;
2093 cfg.menu_skip_mask = 0;
2095 ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
2098 if ((controls[i].type == V4L2_CTRL_TYPE_MENU) ||
2099 (controls[i].type ==
2100 V4L2_CTRL_TYPE_INTEGER_MENU)) {
2101 ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
2103 &s5p_mfc_enc_ctrl_ops, controls[i].id,
2104 controls[i].maximum, 0,
2105 controls[i].default_value);
2107 ctx->ctrls[i] = v4l2_ctrl_new_std(
2109 &s5p_mfc_enc_ctrl_ops, controls[i].id,
2110 controls[i].minimum,
2111 controls[i].maximum, controls[i].step,
2112 controls[i].default_value);
2115 if (ctx->ctrl_handler.error) {
2116 mfc_err("Adding control (%d) failed\n", i);
2117 return ctx->ctrl_handler.error;
2119 if (controls[i].is_volatile && ctx->ctrls[i])
2120 ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
2122 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
2126 void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
2130 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2131 for (i = 0; i < NUM_CTRLS; i++)
2132 ctx->ctrls[i] = NULL;
2135 void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
2137 struct v4l2_format f;
2138 f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
2139 ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
2140 f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
2141 ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);