1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c
5 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
8 * Jeongtae Park <jtp.park@samsung.com>
9 * Kamil Debski <k.debski@samsung.com>
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/sched.h>
18 #include <linux/videodev2.h>
19 #include <media/v4l2-event.h>
20 #include <linux/workqueue.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/videobuf2-v4l2.h>
23 #include "s5p_mfc_common.h"
24 #include "s5p_mfc_ctrl.h"
25 #include "s5p_mfc_debug.h"
26 #include "s5p_mfc_enc.h"
27 #include "s5p_mfc_intr.h"
28 #include "s5p_mfc_opr.h"
30 #define DEF_SRC_FMT_ENC V4L2_PIX_FMT_NV12M
31 #define DEF_DST_FMT_ENC V4L2_PIX_FMT_H264
33 static struct s5p_mfc_fmt formats
[] = {
35 .fourcc
= V4L2_PIX_FMT_NV12MT_16X16
,
36 .codec_mode
= S5P_MFC_CODEC_NONE
,
39 .versions
= MFC_V6_BIT
| MFC_V7_BIT
,
42 .fourcc
= V4L2_PIX_FMT_NV12MT
,
43 .codec_mode
= S5P_MFC_CODEC_NONE
,
46 .versions
= MFC_V5_BIT
,
49 .fourcc
= V4L2_PIX_FMT_NV12M
,
50 .codec_mode
= S5P_MFC_CODEC_NONE
,
53 .versions
= MFC_V5PLUS_BITS
,
56 .fourcc
= V4L2_PIX_FMT_NV21M
,
57 .codec_mode
= S5P_MFC_CODEC_NONE
,
60 .versions
= MFC_V6PLUS_BITS
,
63 .fourcc
= V4L2_PIX_FMT_H264
,
64 .codec_mode
= S5P_MFC_CODEC_H264_ENC
,
67 .versions
= MFC_V5PLUS_BITS
,
70 .fourcc
= V4L2_PIX_FMT_MPEG4
,
71 .codec_mode
= S5P_MFC_CODEC_MPEG4_ENC
,
74 .versions
= MFC_V5PLUS_BITS
,
77 .fourcc
= V4L2_PIX_FMT_H263
,
78 .codec_mode
= S5P_MFC_CODEC_H263_ENC
,
81 .versions
= MFC_V5PLUS_BITS
,
84 .fourcc
= V4L2_PIX_FMT_VP8
,
85 .codec_mode
= S5P_MFC_CODEC_VP8_ENC
,
88 .versions
= MFC_V7PLUS_BITS
,
91 .fourcc
= V4L2_PIX_FMT_HEVC
,
92 .codec_mode
= S5P_FIMV_CODEC_HEVC_ENC
,
95 .versions
= MFC_V10_BIT
,
99 #define NUM_FORMATS ARRAY_SIZE(formats)
100 static struct s5p_mfc_fmt
*find_format(struct v4l2_format
*f
, unsigned int t
)
104 for (i
= 0; i
< NUM_FORMATS
; i
++) {
105 if (formats
[i
].fourcc
== f
->fmt
.pix_mp
.pixelformat
&&
106 formats
[i
].type
== t
)
112 static struct mfc_control controls
[] = {
114 .id
= V4L2_CID_MPEG_VIDEO_GOP_SIZE
,
115 .type
= V4L2_CTRL_TYPE_INTEGER
,
117 .maximum
= (1 << 16) - 1,
122 .id
= V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
,
123 .type
= V4L2_CTRL_TYPE_MENU
,
124 .minimum
= V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
,
125 .maximum
= V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES
,
126 .default_value
= V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
,
130 .id
= V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
,
131 .type
= V4L2_CTRL_TYPE_INTEGER
,
133 .maximum
= (1 << 16) - 1,
138 .id
= V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
,
139 .type
= V4L2_CTRL_TYPE_INTEGER
,
141 .maximum
= (1 << 30) - 1,
143 .default_value
= 1900,
146 .id
= V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
,
147 .type
= V4L2_CTRL_TYPE_INTEGER
,
149 .maximum
= (1 << 16) - 1,
154 .id
= V4L2_CID_MPEG_MFC51_VIDEO_PADDING
,
155 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
156 .name
= "Padding Control Enable",
163 .id
= V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV
,
164 .type
= V4L2_CTRL_TYPE_INTEGER
,
165 .name
= "Padding Color YUV Value",
167 .maximum
= (1 << 25) - 1,
172 .id
= V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE
,
173 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
180 .id
= V4L2_CID_MPEG_VIDEO_BITRATE
,
181 .type
= V4L2_CTRL_TYPE_INTEGER
,
183 .maximum
= (1 << 30) - 1,
188 .id
= V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF
,
189 .type
= V4L2_CTRL_TYPE_INTEGER
,
190 .name
= "Rate Control Reaction Coeff.",
192 .maximum
= (1 << 16) - 1,
197 .id
= V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE
,
198 .type
= V4L2_CTRL_TYPE_MENU
,
199 .name
= "Force frame type",
200 .minimum
= V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED
,
201 .maximum
= V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED
,
202 .default_value
= V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED
,
206 .id
= V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME
,
207 .type
= V4L2_CTRL_TYPE_BUTTON
,
214 .id
= V4L2_CID_MPEG_VIDEO_VBV_SIZE
,
215 .type
= V4L2_CTRL_TYPE_INTEGER
,
217 .maximum
= (1 << 16) - 1,
222 .id
= V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE
,
223 .type
= V4L2_CTRL_TYPE_INTEGER
,
224 .name
= "Horizontal MV Search Range",
231 .id
= V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE
,
232 .type
= V4L2_CTRL_TYPE_INTEGER
,
233 .name
= "Vertical MV Search Range",
240 .id
= V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE
,
241 .type
= V4L2_CTRL_TYPE_INTEGER
,
243 .maximum
= (1 << 16) - 1,
248 .id
= V4L2_CID_MPEG_VIDEO_HEADER_MODE
,
249 .type
= V4L2_CTRL_TYPE_MENU
,
250 .minimum
= V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
,
251 .maximum
= V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
252 .default_value
= V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
,
256 .id
= V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE
,
257 .type
= V4L2_CTRL_TYPE_MENU
,
258 .name
= "Frame Skip Enable",
259 .minimum
= V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED
,
260 .maximum
= V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT
,
262 .default_value
= V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED
,
265 .id
= V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT
,
266 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
267 .name
= "Fixed Target Bit Enable",
275 .id
= V4L2_CID_MPEG_VIDEO_B_FRAMES
,
276 .type
= V4L2_CTRL_TYPE_INTEGER
,
283 .id
= V4L2_CID_MPEG_VIDEO_H264_PROFILE
,
284 .type
= V4L2_CTRL_TYPE_MENU
,
285 .minimum
= V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
,
286 .maximum
= V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH
,
287 .default_value
= V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
,
289 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
) |
290 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN
) |
291 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
)
295 .id
= V4L2_CID_MPEG_VIDEO_H264_LEVEL
,
296 .type
= V4L2_CTRL_TYPE_MENU
,
297 .minimum
= V4L2_MPEG_VIDEO_H264_LEVEL_1_0
,
298 .maximum
= V4L2_MPEG_VIDEO_H264_LEVEL_4_0
,
299 .default_value
= V4L2_MPEG_VIDEO_H264_LEVEL_1_0
,
302 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
,
303 .type
= V4L2_CTRL_TYPE_MENU
,
304 .minimum
= V4L2_MPEG_VIDEO_MPEG4_LEVEL_0
,
305 .maximum
= V4L2_MPEG_VIDEO_MPEG4_LEVEL_5
,
306 .default_value
= V4L2_MPEG_VIDEO_MPEG4_LEVEL_0
,
310 .id
= V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
,
311 .type
= V4L2_CTRL_TYPE_MENU
,
312 .minimum
= V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
,
313 .maximum
= V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY
,
314 .default_value
= V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
,
318 .id
= V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
,
319 .type
= V4L2_CTRL_TYPE_INTEGER
,
326 .id
= V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
,
327 .type
= V4L2_CTRL_TYPE_INTEGER
,
334 .id
= V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
,
335 .type
= V4L2_CTRL_TYPE_MENU
,
336 .minimum
= V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC
,
337 .maximum
= V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC
,
338 .default_value
= V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC
,
342 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P
,
343 .type
= V4L2_CTRL_TYPE_INTEGER
,
344 .name
= "The Number of Ref. Pic for P",
351 .id
= V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM
,
352 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
359 .id
= V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE
,
360 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
367 .id
= V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
,
368 .type
= V4L2_CTRL_TYPE_INTEGER
,
375 .id
= V4L2_CID_MPEG_VIDEO_H264_MIN_QP
,
376 .type
= V4L2_CTRL_TYPE_INTEGER
,
383 .id
= V4L2_CID_MPEG_VIDEO_H264_MAX_QP
,
384 .type
= V4L2_CTRL_TYPE_INTEGER
,
391 .id
= V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
,
392 .type
= V4L2_CTRL_TYPE_INTEGER
,
399 .id
= V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP
,
400 .type
= V4L2_CTRL_TYPE_INTEGER
,
407 .id
= V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP
,
408 .type
= V4L2_CTRL_TYPE_INTEGER
,
409 .name
= "H263 I-Frame QP value",
416 .id
= V4L2_CID_MPEG_VIDEO_H263_MIN_QP
,
417 .type
= V4L2_CTRL_TYPE_INTEGER
,
418 .name
= "H263 Minimum QP value",
425 .id
= V4L2_CID_MPEG_VIDEO_H263_MAX_QP
,
426 .type
= V4L2_CTRL_TYPE_INTEGER
,
427 .name
= "H263 Maximum QP value",
434 .id
= V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP
,
435 .type
= V4L2_CTRL_TYPE_INTEGER
,
436 .name
= "H263 P frame QP value",
443 .id
= V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP
,
444 .type
= V4L2_CTRL_TYPE_INTEGER
,
445 .name
= "H263 B frame QP value",
452 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
,
453 .type
= V4L2_CTRL_TYPE_INTEGER
,
454 .name
= "MPEG4 I-Frame QP value",
461 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP
,
462 .type
= V4L2_CTRL_TYPE_INTEGER
,
463 .name
= "MPEG4 Minimum QP value",
470 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP
,
471 .type
= V4L2_CTRL_TYPE_INTEGER
,
472 .name
= "MPEG4 Maximum QP value",
479 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
,
480 .type
= V4L2_CTRL_TYPE_INTEGER
,
481 .name
= "MPEG4 P frame QP value",
488 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP
,
489 .type
= V4L2_CTRL_TYPE_INTEGER
,
490 .name
= "MPEG4 B frame QP value",
497 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK
,
498 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
499 .name
= "H264 Dark Reg Adaptive RC",
506 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH
,
507 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
508 .name
= "H264 Smooth Reg Adaptive RC",
515 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC
,
516 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
517 .name
= "H264 Static Reg Adaptive RC",
524 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY
,
525 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
526 .name
= "H264 Activity Reg Adaptive RC",
533 .id
= V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE
,
534 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
541 .id
= V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC
,
542 .type
= V4L2_CTRL_TYPE_MENU
,
543 .minimum
= V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED
,
544 .maximum
= V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED
,
545 .default_value
= V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED
,
549 .id
= V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH
,
550 .type
= V4L2_CTRL_TYPE_INTEGER
,
552 .maximum
= (1 << 16) - 1,
557 .id
= V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT
,
558 .type
= V4L2_CTRL_TYPE_INTEGER
,
560 .maximum
= (1 << 16) - 1,
565 .id
= V4L2_CID_MPEG_VIDEO_GOP_CLOSURE
,
566 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
573 .id
= V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
,
574 .type
= V4L2_CTRL_TYPE_INTEGER
,
576 .maximum
= (1 << 16) - 1,
581 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
,
582 .type
= V4L2_CTRL_TYPE_MENU
,
583 .minimum
= V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
,
584 .maximum
= V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE
,
585 .default_value
= V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
,
589 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_QPEL
,
590 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
597 .id
= V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS
,
598 .type
= V4L2_CTRL_TYPE_INTEGER_MENU
,
599 .maximum
= V4L2_CID_MPEG_VIDEO_VPX_8_PARTITIONS
,
600 .default_value
= V4L2_CID_MPEG_VIDEO_VPX_1_PARTITION
,
604 .id
= V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4
,
605 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
612 .id
= V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES
,
613 .type
= V4L2_CTRL_TYPE_INTEGER_MENU
,
614 .maximum
= V4L2_CID_MPEG_VIDEO_VPX_2_REF_FRAME
,
615 .default_value
= V4L2_CID_MPEG_VIDEO_VPX_1_REF_FRAME
,
619 .id
= V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL
,
620 .type
= V4L2_CTRL_TYPE_INTEGER
,
627 .id
= V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS
,
628 .type
= V4L2_CTRL_TYPE_INTEGER
,
635 .id
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD
,
636 .type
= V4L2_CTRL_TYPE_INTEGER
,
638 .maximum
= (1 << 16) - 1,
643 .id
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL
,
644 .type
= V4L2_CTRL_TYPE_MENU
,
645 .minimum
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV
,
646 .maximum
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_REF_PERIOD
,
647 .default_value
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV
,
651 .id
= V4L2_CID_MPEG_VIDEO_VPX_MAX_QP
,
652 .type
= V4L2_CTRL_TYPE_INTEGER
,
656 .default_value
= 127,
659 .id
= V4L2_CID_MPEG_VIDEO_VPX_MIN_QP
,
660 .type
= V4L2_CTRL_TYPE_INTEGER
,
667 .id
= V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP
,
668 .type
= V4L2_CTRL_TYPE_INTEGER
,
675 .id
= V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP
,
676 .type
= V4L2_CTRL_TYPE_INTEGER
,
683 .id
= V4L2_CID_MPEG_VIDEO_VP8_PROFILE
,
684 .type
= V4L2_CTRL_TYPE_MENU
,
685 .minimum
= V4L2_MPEG_VIDEO_VP8_PROFILE_0
,
686 .maximum
= V4L2_MPEG_VIDEO_VP8_PROFILE_3
,
687 .default_value
= V4L2_MPEG_VIDEO_VP8_PROFILE_0
,
691 .id
= V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP
,
692 .type
= V4L2_CTRL_TYPE_INTEGER
,
693 .name
= "HEVC I Frame QP Value",
700 .id
= V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP
,
701 .type
= V4L2_CTRL_TYPE_INTEGER
,
702 .name
= "HEVC P Frame QP Value",
709 .id
= V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP
,
710 .type
= V4L2_CTRL_TYPE_INTEGER
,
717 .id
= V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP
,
718 .type
= V4L2_CTRL_TYPE_INTEGER
,
725 .id
= V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP
,
726 .type
= V4L2_CTRL_TYPE_INTEGER
,
733 .id
= V4L2_CID_MPEG_VIDEO_HEVC_PROFILE
,
734 .type
= V4L2_CTRL_TYPE_MENU
,
735 .minimum
= V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
,
736 .maximum
= V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE
,
738 .default_value
= V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
,
741 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LEVEL
,
742 .type
= V4L2_CTRL_TYPE_MENU
,
743 .minimum
= V4L2_MPEG_VIDEO_HEVC_LEVEL_1
,
744 .maximum
= V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2
,
746 .default_value
= V4L2_MPEG_VIDEO_HEVC_LEVEL_1
,
749 .id
= V4L2_CID_MPEG_VIDEO_HEVC_TIER
,
750 .type
= V4L2_CTRL_TYPE_MENU
,
751 .minimum
= V4L2_MPEG_VIDEO_HEVC_TIER_MAIN
,
752 .maximum
= V4L2_MPEG_VIDEO_HEVC_TIER_HIGH
,
754 .default_value
= V4L2_MPEG_VIDEO_HEVC_TIER_MAIN
,
757 .id
= V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION
,
758 .type
= V4L2_CTRL_TYPE_INTEGER
,
760 .maximum
= (1 << 16) - 1,
765 .id
= V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH
,
766 .type
= V4L2_CTRL_TYPE_INTEGER
,
773 .id
= V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES
,
774 .type
= V4L2_CTRL_TYPE_INTEGER
,
781 .id
= V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE
,
782 .type
= V4L2_CTRL_TYPE_MENU
,
783 .minimum
= V4L2_MPEG_VIDEO_HEVC_REFRESH_NONE
,
784 .maximum
= V4L2_MPEG_VIDEO_HEVC_REFRESH_IDR
,
786 .default_value
= V4L2_MPEG_VIDEO_HEVC_REFRESH_NONE
,
789 .id
= V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED
,
790 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
797 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU
,
798 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
805 .id
= V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT
,
806 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
813 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE
,
814 .type
= V4L2_CTRL_TYPE_MENU
,
815 .minimum
= V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED
,
816 .maximum
= V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY
,
818 .default_value
= V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED
,
821 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP
,
822 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
829 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE
,
830 .type
= V4L2_CTRL_TYPE_MENU
,
831 .minimum
= V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_B
,
832 .maximum
= V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_P
,
834 .default_value
= V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_B
,
837 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER
,
838 .type
= V4L2_CTRL_TYPE_INTEGER
,
845 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP
,
846 .type
= V4L2_CTRL_TYPE_INTEGER
,
853 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP
,
854 .type
= V4L2_CTRL_TYPE_INTEGER
,
861 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP
,
862 .type
= V4L2_CTRL_TYPE_INTEGER
,
869 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP
,
870 .type
= V4L2_CTRL_TYPE_INTEGER
,
877 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP
,
878 .type
= V4L2_CTRL_TYPE_INTEGER
,
885 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP
,
886 .type
= V4L2_CTRL_TYPE_INTEGER
,
893 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP
,
894 .type
= V4L2_CTRL_TYPE_INTEGER
,
901 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR
,
902 .type
= V4L2_CTRL_TYPE_INTEGER
,
909 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR
,
910 .type
= V4L2_CTRL_TYPE_INTEGER
,
917 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR
,
918 .type
= V4L2_CTRL_TYPE_INTEGER
,
925 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR
,
926 .type
= V4L2_CTRL_TYPE_INTEGER
,
933 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR
,
934 .type
= V4L2_CTRL_TYPE_INTEGER
,
941 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR
,
942 .type
= V4L2_CTRL_TYPE_INTEGER
,
949 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR
,
950 .type
= V4L2_CTRL_TYPE_INTEGER
,
957 .id
= V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB
,
958 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
965 .id
= V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID
,
966 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
973 .id
= V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING
,
974 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
981 .id
= V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT
,
982 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
989 .id
= V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION
,
990 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
997 .id
= V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1
,
998 .type
= V4L2_CTRL_TYPE_INTEGER
,
1005 .id
= V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE
,
1006 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1013 .id
= V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD
,
1014 .type
= V4L2_CTRL_TYPE_INTEGER
,
1016 .maximum
= (1 << 16) - 1,
1021 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2
,
1022 .type
= V4L2_CTRL_TYPE_INTEGER
,
1029 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2
,
1030 .type
= V4L2_CTRL_TYPE_INTEGER
,
1037 .id
= V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD
,
1038 .type
= V4L2_CTRL_TYPE_MENU
,
1039 .minimum
= V4L2_MPEG_VIDEO_HEVC_SIZE_0
,
1040 .maximum
= V4L2_MPEG_VIDEO_HEVC_SIZE_4
,
1042 .default_value
= V4L2_MPEG_VIDEO_HEVC_SIZE_0
,
1045 .id
= V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR
,
1046 .type
= V4L2_CTRL_TYPE_INTEGER
,
1053 .id
= V4L2_CID_MIN_BUFFERS_FOR_OUTPUT
,
1054 .type
= V4L2_CTRL_TYPE_INTEGER
,
1055 .name
= "Minimum number of output bufs",
1064 #define NUM_CTRLS ARRAY_SIZE(controls)
1065 static const char * const *mfc51_get_menu(u32 id
)
1067 static const char * const mfc51_video_frame_skip
[] = {
1073 static const char * const mfc51_video_force_frame
[] = {
1080 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE
:
1081 return mfc51_video_frame_skip
;
1082 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE
:
1083 return mfc51_video_force_frame
;
1088 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx
*ctx
)
1090 mfc_debug(2, "src=%d, dst=%d, state=%d\n",
1091 ctx
->src_queue_cnt
, ctx
->dst_queue_cnt
, ctx
->state
);
1092 /* context is ready to make header */
1093 if (ctx
->state
== MFCINST_GOT_INST
&& ctx
->dst_queue_cnt
>= 1)
1095 /* context is ready to encode a frame */
1096 if ((ctx
->state
== MFCINST_RUNNING
||
1097 ctx
->state
== MFCINST_HEAD_PRODUCED
) &&
1098 ctx
->src_queue_cnt
>= 1 && ctx
->dst_queue_cnt
>= 1)
1100 /* context is ready to encode remaining frames */
1101 if (ctx
->state
== MFCINST_FINISHING
&&
1102 ctx
->dst_queue_cnt
>= 1)
1104 mfc_debug(2, "ctx is not ready\n");
1108 static void cleanup_ref_queue(struct s5p_mfc_ctx
*ctx
)
1110 struct s5p_mfc_buf
*mb_entry
;
1112 /* move buffers in ref queue to src queue */
1113 while (!list_empty(&ctx
->ref_queue
)) {
1114 mb_entry
= list_entry((&ctx
->ref_queue
)->next
,
1115 struct s5p_mfc_buf
, list
);
1116 list_del(&mb_entry
->list
);
1117 ctx
->ref_queue_cnt
--;
1118 list_add_tail(&mb_entry
->list
, &ctx
->src_queue
);
1119 ctx
->src_queue_cnt
++;
1121 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
1122 ctx
->src_queue_cnt
, ctx
->ref_queue_cnt
);
1123 INIT_LIST_HEAD(&ctx
->ref_queue
);
1124 ctx
->ref_queue_cnt
= 0;
1127 static int enc_pre_seq_start(struct s5p_mfc_ctx
*ctx
)
1129 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1130 struct s5p_mfc_buf
*dst_mb
;
1131 unsigned long dst_addr
;
1132 unsigned int dst_size
;
1134 dst_mb
= list_entry(ctx
->dst_queue
.next
, struct s5p_mfc_buf
, list
);
1135 dst_addr
= vb2_dma_contig_plane_dma_addr(&dst_mb
->b
->vb2_buf
, 0);
1136 dst_size
= vb2_plane_size(&dst_mb
->b
->vb2_buf
, 0);
1137 s5p_mfc_hw_call(dev
->mfc_ops
, set_enc_stream_buffer
, ctx
, dst_addr
,
1142 static int enc_post_seq_start(struct s5p_mfc_ctx
*ctx
)
1144 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1145 struct s5p_mfc_enc_params
*p
= &ctx
->enc_params
;
1146 struct s5p_mfc_buf
*dst_mb
;
1147 unsigned int enc_pb_count
;
1149 if (p
->seq_hdr_mode
== V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
) {
1150 if (!list_empty(&ctx
->dst_queue
)) {
1151 dst_mb
= list_entry(ctx
->dst_queue
.next
,
1152 struct s5p_mfc_buf
, list
);
1153 list_del(&dst_mb
->list
);
1154 ctx
->dst_queue_cnt
--;
1155 vb2_set_plane_payload(&dst_mb
->b
->vb2_buf
, 0,
1156 s5p_mfc_hw_call(dev
->mfc_ops
, get_enc_strm_size
,
1158 vb2_buffer_done(&dst_mb
->b
->vb2_buf
,
1159 VB2_BUF_STATE_DONE
);
1163 if (!IS_MFCV6_PLUS(dev
)) {
1164 ctx
->state
= MFCINST_RUNNING
;
1165 if (s5p_mfc_ctx_ready(ctx
))
1166 set_work_bit_irqsave(ctx
);
1167 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
1169 enc_pb_count
= s5p_mfc_hw_call(dev
->mfc_ops
,
1170 get_enc_dpb_count
, dev
);
1171 if (ctx
->pb_count
< enc_pb_count
)
1172 ctx
->pb_count
= enc_pb_count
;
1173 if (FW_HAS_E_MIN_SCRATCH_BUF(dev
)) {
1174 ctx
->scratch_buf_size
= s5p_mfc_hw_call(dev
->mfc_ops
,
1175 get_e_min_scratch_buf_size
, dev
);
1176 ctx
->bank1
.size
+= ctx
->scratch_buf_size
;
1178 ctx
->state
= MFCINST_HEAD_PRODUCED
;
1184 static int enc_pre_frame_start(struct s5p_mfc_ctx
*ctx
)
1186 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1187 struct s5p_mfc_buf
*dst_mb
;
1188 struct s5p_mfc_buf
*src_mb
;
1189 unsigned long src_y_addr
, src_c_addr
, dst_addr
;
1190 unsigned int dst_size
;
1192 src_mb
= list_entry(ctx
->src_queue
.next
, struct s5p_mfc_buf
, list
);
1193 src_y_addr
= vb2_dma_contig_plane_dma_addr(&src_mb
->b
->vb2_buf
, 0);
1194 src_c_addr
= vb2_dma_contig_plane_dma_addr(&src_mb
->b
->vb2_buf
, 1);
1195 s5p_mfc_hw_call(dev
->mfc_ops
, set_enc_frame_buffer
, ctx
,
1196 src_y_addr
, src_c_addr
);
1198 dst_mb
= list_entry(ctx
->dst_queue
.next
, struct s5p_mfc_buf
, list
);
1199 dst_addr
= vb2_dma_contig_plane_dma_addr(&dst_mb
->b
->vb2_buf
, 0);
1200 dst_size
= vb2_plane_size(&dst_mb
->b
->vb2_buf
, 0);
1201 s5p_mfc_hw_call(dev
->mfc_ops
, set_enc_stream_buffer
, ctx
, dst_addr
,
1207 static int enc_post_frame_start(struct s5p_mfc_ctx
*ctx
)
1209 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1210 struct s5p_mfc_buf
*mb_entry
;
1211 unsigned long enc_y_addr
= 0, enc_c_addr
= 0;
1212 unsigned long mb_y_addr
, mb_c_addr
;
1214 unsigned int strm_size
;
1216 slice_type
= s5p_mfc_hw_call(dev
->mfc_ops
, get_enc_slice_type
, dev
);
1217 strm_size
= s5p_mfc_hw_call(dev
->mfc_ops
, get_enc_strm_size
, dev
);
1218 mfc_debug(2, "Encoded slice type: %d\n", slice_type
);
1219 mfc_debug(2, "Encoded stream size: %d\n", strm_size
);
1220 mfc_debug(2, "Display order: %d\n",
1221 mfc_read(dev
, S5P_FIMV_ENC_SI_PIC_CNT
));
1222 if (slice_type
>= 0) {
1223 s5p_mfc_hw_call(dev
->mfc_ops
, get_enc_frame_buffer
, ctx
,
1224 &enc_y_addr
, &enc_c_addr
);
1225 list_for_each_entry(mb_entry
, &ctx
->src_queue
, list
) {
1226 mb_y_addr
= vb2_dma_contig_plane_dma_addr(
1227 &mb_entry
->b
->vb2_buf
, 0);
1228 mb_c_addr
= vb2_dma_contig_plane_dma_addr(
1229 &mb_entry
->b
->vb2_buf
, 1);
1230 if ((enc_y_addr
== mb_y_addr
) &&
1231 (enc_c_addr
== mb_c_addr
)) {
1232 list_del(&mb_entry
->list
);
1233 ctx
->src_queue_cnt
--;
1234 vb2_buffer_done(&mb_entry
->b
->vb2_buf
,
1235 VB2_BUF_STATE_DONE
);
1239 list_for_each_entry(mb_entry
, &ctx
->ref_queue
, list
) {
1240 mb_y_addr
= vb2_dma_contig_plane_dma_addr(
1241 &mb_entry
->b
->vb2_buf
, 0);
1242 mb_c_addr
= vb2_dma_contig_plane_dma_addr(
1243 &mb_entry
->b
->vb2_buf
, 1);
1244 if ((enc_y_addr
== mb_y_addr
) &&
1245 (enc_c_addr
== mb_c_addr
)) {
1246 list_del(&mb_entry
->list
);
1247 ctx
->ref_queue_cnt
--;
1248 vb2_buffer_done(&mb_entry
->b
->vb2_buf
,
1249 VB2_BUF_STATE_DONE
);
1254 if ((ctx
->src_queue_cnt
> 0) && (ctx
->state
== MFCINST_RUNNING
)) {
1255 mb_entry
= list_entry(ctx
->src_queue
.next
, struct s5p_mfc_buf
,
1257 if (mb_entry
->flags
& MFC_BUF_FLAG_USED
) {
1258 list_del(&mb_entry
->list
);
1259 ctx
->src_queue_cnt
--;
1260 list_add_tail(&mb_entry
->list
, &ctx
->ref_queue
);
1261 ctx
->ref_queue_cnt
++;
1264 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
1265 ctx
->src_queue_cnt
, ctx
->ref_queue_cnt
);
1266 if ((ctx
->dst_queue_cnt
> 0) && (strm_size
> 0)) {
1267 mb_entry
= list_entry(ctx
->dst_queue
.next
, struct s5p_mfc_buf
,
1269 list_del(&mb_entry
->list
);
1270 ctx
->dst_queue_cnt
--;
1271 switch (slice_type
) {
1272 case S5P_FIMV_ENC_SI_SLICE_TYPE_I
:
1273 mb_entry
->b
->flags
|= V4L2_BUF_FLAG_KEYFRAME
;
1275 case S5P_FIMV_ENC_SI_SLICE_TYPE_P
:
1276 mb_entry
->b
->flags
|= V4L2_BUF_FLAG_PFRAME
;
1278 case S5P_FIMV_ENC_SI_SLICE_TYPE_B
:
1279 mb_entry
->b
->flags
|= V4L2_BUF_FLAG_BFRAME
;
1282 vb2_set_plane_payload(&mb_entry
->b
->vb2_buf
, 0, strm_size
);
1283 vb2_buffer_done(&mb_entry
->b
->vb2_buf
, VB2_BUF_STATE_DONE
);
1285 if ((ctx
->src_queue_cnt
== 0) || (ctx
->dst_queue_cnt
== 0))
1286 clear_work_bit(ctx
);
1291 static const struct s5p_mfc_codec_ops encoder_codec_ops
= {
1292 .pre_seq_start
= enc_pre_seq_start
,
1293 .post_seq_start
= enc_post_seq_start
,
1294 .pre_frame_start
= enc_pre_frame_start
,
1295 .post_frame_start
= enc_post_frame_start
,
1298 /* Query capabilities of the device */
1299 static int vidioc_querycap(struct file
*file
, void *priv
,
1300 struct v4l2_capability
*cap
)
1302 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1304 strscpy(cap
->driver
, S5P_MFC_NAME
, sizeof(cap
->driver
));
1305 strscpy(cap
->card
, dev
->vfd_enc
->name
, sizeof(cap
->card
));
1306 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
1307 dev_name(&dev
->plat_dev
->dev
));
1311 static int vidioc_enum_fmt(struct file
*file
, struct v4l2_fmtdesc
*f
,
1314 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1317 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
1318 if (out
&& formats
[i
].type
!= MFC_FMT_RAW
)
1320 else if (!out
&& formats
[i
].type
!= MFC_FMT_ENC
)
1322 else if ((dev
->variant
->version_bit
& formats
[i
].versions
) == 0)
1325 if (j
== f
->index
) {
1326 f
->pixelformat
= formats
[i
].fourcc
;
1334 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *pirv
,
1335 struct v4l2_fmtdesc
*f
)
1337 return vidioc_enum_fmt(file
, f
, false);
1340 static int vidioc_enum_fmt_vid_out(struct file
*file
, void *priv
,
1341 struct v4l2_fmtdesc
*f
)
1343 return vidioc_enum_fmt(file
, f
, true);
1346 static int vidioc_g_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1348 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1349 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
1351 mfc_debug(2, "f->type = %d ctx->state = %d\n", f
->type
, ctx
->state
);
1352 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1353 /* This is run on output (encoder dest) */
1354 pix_fmt_mp
->width
= 0;
1355 pix_fmt_mp
->height
= 0;
1356 pix_fmt_mp
->field
= V4L2_FIELD_NONE
;
1357 pix_fmt_mp
->pixelformat
= ctx
->dst_fmt
->fourcc
;
1358 pix_fmt_mp
->num_planes
= ctx
->dst_fmt
->num_planes
;
1360 pix_fmt_mp
->plane_fmt
[0].bytesperline
= ctx
->enc_dst_buf_size
;
1361 pix_fmt_mp
->plane_fmt
[0].sizeimage
= ctx
->enc_dst_buf_size
;
1362 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1363 /* This is run on capture (encoder src) */
1364 pix_fmt_mp
->width
= ctx
->img_width
;
1365 pix_fmt_mp
->height
= ctx
->img_height
;
1367 pix_fmt_mp
->field
= V4L2_FIELD_NONE
;
1368 pix_fmt_mp
->pixelformat
= ctx
->src_fmt
->fourcc
;
1369 pix_fmt_mp
->num_planes
= ctx
->src_fmt
->num_planes
;
1371 pix_fmt_mp
->plane_fmt
[0].bytesperline
= ctx
->buf_width
;
1372 pix_fmt_mp
->plane_fmt
[0].sizeimage
= ctx
->luma_size
;
1373 pix_fmt_mp
->plane_fmt
[1].bytesperline
= ctx
->buf_width
;
1374 pix_fmt_mp
->plane_fmt
[1].sizeimage
= ctx
->chroma_size
;
1376 mfc_err("invalid buf type\n");
1382 static int vidioc_try_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1384 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1385 struct s5p_mfc_fmt
*fmt
;
1386 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
1388 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1389 fmt
= find_format(f
, MFC_FMT_ENC
);
1391 mfc_err("failed to try output format\n");
1394 if ((dev
->variant
->version_bit
& fmt
->versions
) == 0) {
1395 mfc_err("Unsupported format by this MFC version.\n");
1399 pix_fmt_mp
->plane_fmt
[0].bytesperline
=
1400 pix_fmt_mp
->plane_fmt
[0].sizeimage
;
1401 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1402 fmt
= find_format(f
, MFC_FMT_RAW
);
1404 mfc_err("failed to try output format\n");
1407 if ((dev
->variant
->version_bit
& fmt
->versions
) == 0) {
1408 mfc_err("Unsupported format by this MFC version.\n");
1412 v4l_bound_align_image(&pix_fmt_mp
->width
, 8, 1920, 1,
1413 &pix_fmt_mp
->height
, 4, 1080, 1, 0);
1415 mfc_err("invalid buf type\n");
1421 static int vidioc_s_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1423 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1424 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1425 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
1428 ret
= vidioc_try_fmt(file
, priv
, f
);
1431 if (ctx
->vq_src
.streaming
|| ctx
->vq_dst
.streaming
) {
1432 v4l2_err(&dev
->v4l2_dev
, "%s queue busy\n", __func__
);
1436 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1437 /* dst_fmt is validated by call to vidioc_try_fmt */
1438 ctx
->dst_fmt
= find_format(f
, MFC_FMT_ENC
);
1439 ctx
->state
= MFCINST_INIT
;
1440 ctx
->codec_mode
= ctx
->dst_fmt
->codec_mode
;
1441 ctx
->enc_dst_buf_size
= pix_fmt_mp
->plane_fmt
[0].sizeimage
;
1442 pix_fmt_mp
->plane_fmt
[0].bytesperline
= 0;
1443 ctx
->dst_bufs_cnt
= 0;
1444 ctx
->capture_state
= QUEUE_FREE
;
1445 ret
= s5p_mfc_open_mfc_inst(dev
, ctx
);
1446 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1447 /* src_fmt is validated by call to vidioc_try_fmt */
1448 ctx
->src_fmt
= find_format(f
, MFC_FMT_RAW
);
1449 ctx
->img_width
= pix_fmt_mp
->width
;
1450 ctx
->img_height
= pix_fmt_mp
->height
;
1451 mfc_debug(2, "codec number: %d\n", ctx
->src_fmt
->codec_mode
);
1452 mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1453 pix_fmt_mp
->width
, pix_fmt_mp
->height
,
1454 ctx
->img_width
, ctx
->img_height
);
1456 s5p_mfc_hw_call(dev
->mfc_ops
, enc_calc_src_size
, ctx
);
1457 pix_fmt_mp
->plane_fmt
[0].sizeimage
= ctx
->luma_size
;
1458 pix_fmt_mp
->plane_fmt
[0].bytesperline
= ctx
->buf_width
;
1459 pix_fmt_mp
->plane_fmt
[1].sizeimage
= ctx
->chroma_size
;
1460 pix_fmt_mp
->plane_fmt
[1].bytesperline
= ctx
->buf_width
;
1462 ctx
->src_bufs_cnt
= 0;
1463 ctx
->output_state
= QUEUE_FREE
;
1465 mfc_err("invalid buf type\n");
1473 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1474 struct v4l2_requestbuffers
*reqbufs
)
1476 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1477 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1480 /* if memory is not mmp or userptr return error */
1481 if ((reqbufs
->memory
!= V4L2_MEMORY_MMAP
) &&
1482 (reqbufs
->memory
!= V4L2_MEMORY_USERPTR
))
1484 if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1485 if (reqbufs
->count
== 0) {
1486 mfc_debug(2, "Freeing buffers\n");
1487 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
1488 s5p_mfc_hw_call(dev
->mfc_ops
, release_codec_buffers
,
1490 ctx
->capture_state
= QUEUE_FREE
;
1493 if (ctx
->capture_state
!= QUEUE_FREE
) {
1494 mfc_err("invalid capture state: %d\n",
1495 ctx
->capture_state
);
1498 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
1500 mfc_err("error in vb2_reqbufs() for E(D)\n");
1503 ctx
->capture_state
= QUEUE_BUFS_REQUESTED
;
1505 ret
= s5p_mfc_hw_call(ctx
->dev
->mfc_ops
,
1506 alloc_codec_buffers
, ctx
);
1508 mfc_err("Failed to allocate encoding buffers\n");
1510 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
1513 } else if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1514 if (reqbufs
->count
== 0) {
1515 mfc_debug(2, "Freeing buffers\n");
1516 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
1517 s5p_mfc_hw_call(dev
->mfc_ops
, release_codec_buffers
,
1519 ctx
->output_state
= QUEUE_FREE
;
1522 if (ctx
->output_state
!= QUEUE_FREE
) {
1523 mfc_err("invalid output state: %d\n",
1528 if (IS_MFCV6_PLUS(dev
)) {
1529 /* Check for min encoder buffers */
1530 if (ctx
->pb_count
&&
1531 (reqbufs
->count
< ctx
->pb_count
)) {
1532 reqbufs
->count
= ctx
->pb_count
;
1533 mfc_debug(2, "Minimum %d output buffers needed\n",
1536 ctx
->pb_count
= reqbufs
->count
;
1540 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
1542 mfc_err("error in vb2_reqbufs() for E(S)\n");
1545 ctx
->output_state
= QUEUE_BUFS_REQUESTED
;
1547 mfc_err("invalid buf type\n");
1553 static int vidioc_querybuf(struct file
*file
, void *priv
,
1554 struct v4l2_buffer
*buf
)
1556 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1559 /* if memory is not mmp or userptr return error */
1560 if ((buf
->memory
!= V4L2_MEMORY_MMAP
) &&
1561 (buf
->memory
!= V4L2_MEMORY_USERPTR
))
1563 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1564 if (ctx
->state
!= MFCINST_GOT_INST
) {
1565 mfc_err("invalid context state: %d\n", ctx
->state
);
1568 ret
= vb2_querybuf(&ctx
->vq_dst
, buf
);
1570 mfc_err("error in vb2_querybuf() for E(D)\n");
1573 buf
->m
.planes
[0].m
.mem_offset
+= DST_QUEUE_OFF_BASE
;
1574 } else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1575 ret
= vb2_querybuf(&ctx
->vq_src
, buf
);
1577 mfc_err("error in vb2_querybuf() for E(S)\n");
1581 mfc_err("invalid buf type\n");
1587 /* Queue a buffer */
1588 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
1590 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1592 if (ctx
->state
== MFCINST_ERROR
) {
1593 mfc_err("Call on QBUF after unrecoverable error\n");
1596 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1597 if (ctx
->state
== MFCINST_FINISHING
) {
1598 mfc_err("Call on QBUF after EOS command\n");
1601 return vb2_qbuf(&ctx
->vq_src
, NULL
, buf
);
1602 } else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1603 return vb2_qbuf(&ctx
->vq_dst
, NULL
, buf
);
1608 /* Dequeue a buffer */
1609 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
1611 const struct v4l2_event ev
= {
1612 .type
= V4L2_EVENT_EOS
1614 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1617 if (ctx
->state
== MFCINST_ERROR
) {
1618 mfc_err_limited("Call on DQBUF after unrecoverable error\n");
1621 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1622 ret
= vb2_dqbuf(&ctx
->vq_src
, buf
, file
->f_flags
& O_NONBLOCK
);
1623 } else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1624 ret
= vb2_dqbuf(&ctx
->vq_dst
, buf
, file
->f_flags
& O_NONBLOCK
);
1625 if (ret
== 0 && ctx
->state
== MFCINST_FINISHED
1626 && list_empty(&ctx
->vq_dst
.done_list
))
1627 v4l2_event_queue_fh(&ctx
->fh
, &ev
);
1635 /* Export DMA buffer */
1636 static int vidioc_expbuf(struct file
*file
, void *priv
,
1637 struct v4l2_exportbuffer
*eb
)
1639 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1641 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
1642 return vb2_expbuf(&ctx
->vq_src
, eb
);
1643 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1644 return vb2_expbuf(&ctx
->vq_dst
, eb
);
1649 static int vidioc_streamon(struct file
*file
, void *priv
,
1650 enum v4l2_buf_type type
)
1652 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1654 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
1655 return vb2_streamon(&ctx
->vq_src
, type
);
1656 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1657 return vb2_streamon(&ctx
->vq_dst
, type
);
1661 /* Stream off, which equals to a pause */
1662 static int vidioc_streamoff(struct file
*file
, void *priv
,
1663 enum v4l2_buf_type type
)
1665 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1667 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
1668 return vb2_streamoff(&ctx
->vq_src
, type
);
1669 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1670 return vb2_streamoff(&ctx
->vq_dst
, type
);
1674 static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl
)
1676 static unsigned int t
[V4L2_MPEG_VIDEO_H264_LEVEL_4_0
+ 1] = {
1677 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0 */ 10,
1678 /* V4L2_MPEG_VIDEO_H264_LEVEL_1B */ 9,
1679 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1 */ 11,
1680 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2 */ 12,
1681 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3 */ 13,
1682 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0 */ 20,
1683 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1 */ 21,
1684 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2 */ 22,
1685 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0 */ 30,
1686 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1 */ 31,
1687 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2 */ 32,
1688 /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0 */ 40,
1693 static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl
)
1695 static unsigned int t
[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5
+ 1] = {
1696 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 */ 0,
1697 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B */ 9,
1698 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 */ 1,
1699 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 */ 2,
1700 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 */ 3,
1701 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B */ 7,
1702 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 */ 4,
1703 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 */ 5,
1708 static inline int hevc_level(enum v4l2_mpeg_video_hevc_level lvl
)
1710 static unsigned int t
[] = {
1711 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_1 */ 10,
1712 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_2 */ 20,
1713 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1 */ 21,
1714 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_3 */ 30,
1715 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1 */ 31,
1716 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_4 */ 40,
1717 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1 */ 41,
1718 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_5 */ 50,
1719 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1 */ 51,
1720 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2 */ 52,
1721 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_6 */ 60,
1722 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1 */ 61,
1723 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2 */ 62,
1728 static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar
)
1730 static unsigned int t
[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED
+ 1] = {
1731 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED */ 0,
1732 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1 */ 1,
1733 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11 */ 2,
1734 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11 */ 3,
1735 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11 */ 4,
1736 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33 */ 5,
1737 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11 */ 6,
1738 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11 */ 7,
1739 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11 */ 8,
1740 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33 */ 9,
1741 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11 */ 10,
1742 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11 */ 11,
1743 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33 */ 12,
1744 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99 */ 13,
1745 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3 */ 14,
1746 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2 */ 15,
1747 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1 */ 16,
1748 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED */ 255,
1754 * Update range of all HEVC quantization parameter controls that depend on the
1755 * V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP controls.
1757 static void __enc_update_hevc_qp_ctrls_range(struct s5p_mfc_ctx
*ctx
,
1760 static const int __hevc_qp_ctrls
[] = {
1761 V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP
,
1762 V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP
,
1763 V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP
,
1764 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP
,
1765 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP
,
1766 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP
,
1767 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP
,
1768 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP
,
1769 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP
,
1770 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP
,
1772 struct v4l2_ctrl
*ctrl
= NULL
;
1775 for (i
= 0; i
< ARRAY_SIZE(__hevc_qp_ctrls
); i
++) {
1776 for (j
= 0; j
< ARRAY_SIZE(ctx
->ctrls
); j
++) {
1777 if (ctx
->ctrls
[j
]->id
== __hevc_qp_ctrls
[i
]) {
1778 ctrl
= ctx
->ctrls
[j
];
1785 __v4l2_ctrl_modify_range(ctrl
, min
, max
, ctrl
->step
, min
);
1789 static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl
*ctrl
)
1791 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
1792 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1793 struct s5p_mfc_enc_params
*p
= &ctx
->enc_params
;
1797 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
1798 p
->gop_size
= ctrl
->val
;
1800 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
1801 p
->slice_mode
= ctrl
->val
;
1803 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
1804 p
->slice_mb
= ctrl
->val
;
1806 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
1807 p
->slice_bit
= ctrl
->val
* 8;
1809 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
:
1810 p
->intra_refresh_mb
= ctrl
->val
;
1812 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING
:
1815 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV
:
1816 p
->pad_luma
= (ctrl
->val
>> 16) & 0xff;
1817 p
->pad_cb
= (ctrl
->val
>> 8) & 0xff;
1818 p
->pad_cr
= (ctrl
->val
>> 0) & 0xff;
1820 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE
:
1821 p
->rc_frame
= ctrl
->val
;
1823 case V4L2_CID_MPEG_VIDEO_BITRATE
:
1824 p
->rc_bitrate
= ctrl
->val
;
1826 case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF
:
1827 p
->rc_reaction_coeff
= ctrl
->val
;
1829 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE
:
1830 ctx
->force_frame_type
= ctrl
->val
;
1832 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME
:
1833 ctx
->force_frame_type
=
1834 V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_I_FRAME
;
1836 case V4L2_CID_MPEG_VIDEO_VBV_SIZE
:
1837 p
->vbv_size
= ctrl
->val
;
1839 case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE
:
1840 p
->mv_h_range
= ctrl
->val
;
1842 case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE
:
1843 p
->mv_v_range
= ctrl
->val
;
1845 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE
:
1846 p
->codec
.h264
.cpb_size
= ctrl
->val
;
1848 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
1849 p
->seq_hdr_mode
= ctrl
->val
;
1851 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE
:
1852 p
->frame_skip_mode
= ctrl
->val
;
1854 case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT
:
1855 p
->fixed_target_bit
= ctrl
->val
;
1857 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
1858 p
->num_b_frame
= ctrl
->val
;
1860 case V4L2_CID_MPEG_VIDEO_H264_PROFILE
:
1861 switch (ctrl
->val
) {
1862 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN
:
1863 p
->codec
.h264
.profile
=
1864 S5P_FIMV_ENC_PROFILE_H264_MAIN
;
1866 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
:
1867 p
->codec
.h264
.profile
=
1868 S5P_FIMV_ENC_PROFILE_H264_HIGH
;
1870 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
:
1871 p
->codec
.h264
.profile
=
1872 S5P_FIMV_ENC_PROFILE_H264_BASELINE
;
1874 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE
:
1875 if (IS_MFCV6_PLUS(dev
))
1876 p
->codec
.h264
.profile
=
1877 S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE
;
1885 case V4L2_CID_MPEG_VIDEO_H264_LEVEL
:
1886 p
->codec
.h264
.level_v4l2
= ctrl
->val
;
1887 p
->codec
.h264
.level
= h264_level(ctrl
->val
);
1888 if (p
->codec
.h264
.level
< 0) {
1889 mfc_err("Level number is wrong\n");
1890 ret
= p
->codec
.h264
.level
;
1893 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
:
1894 p
->codec
.mpeg4
.level_v4l2
= ctrl
->val
;
1895 p
->codec
.mpeg4
.level
= mpeg4_level(ctrl
->val
);
1896 if (p
->codec
.mpeg4
.level
< 0) {
1897 mfc_err("Level number is wrong\n");
1898 ret
= p
->codec
.mpeg4
.level
;
1901 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
:
1902 p
->codec
.h264
.loop_filter_mode
= ctrl
->val
;
1904 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
:
1905 p
->codec
.h264
.loop_filter_alpha
= ctrl
->val
;
1907 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
:
1908 p
->codec
.h264
.loop_filter_beta
= ctrl
->val
;
1910 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
:
1911 p
->codec
.h264
.entropy_mode
= ctrl
->val
;
1913 case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P
:
1914 p
->codec
.h264
.num_ref_pic_4p
= ctrl
->val
;
1916 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM
:
1917 p
->codec
.h264
._8x8_transform
= ctrl
->val
;
1919 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE
:
1920 p
->rc_mb
= ctrl
->val
;
1922 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
1923 p
->codec
.h264
.rc_frame_qp
= ctrl
->val
;
1925 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
1926 p
->codec
.h264
.rc_min_qp
= ctrl
->val
;
1928 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
1929 p
->codec
.h264
.rc_max_qp
= ctrl
->val
;
1931 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
1932 p
->codec
.h264
.rc_p_frame_qp
= ctrl
->val
;
1934 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP
:
1935 p
->codec
.h264
.rc_b_frame_qp
= ctrl
->val
;
1937 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
:
1938 case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP
:
1939 p
->codec
.mpeg4
.rc_frame_qp
= ctrl
->val
;
1941 case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP
:
1942 case V4L2_CID_MPEG_VIDEO_H263_MIN_QP
:
1943 p
->codec
.mpeg4
.rc_min_qp
= ctrl
->val
;
1945 case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP
:
1946 case V4L2_CID_MPEG_VIDEO_H263_MAX_QP
:
1947 p
->codec
.mpeg4
.rc_max_qp
= ctrl
->val
;
1949 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
:
1950 case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP
:
1951 p
->codec
.mpeg4
.rc_p_frame_qp
= ctrl
->val
;
1953 case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP
:
1954 case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP
:
1955 p
->codec
.mpeg4
.rc_b_frame_qp
= ctrl
->val
;
1957 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK
:
1958 p
->codec
.h264
.rc_mb_dark
= ctrl
->val
;
1960 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH
:
1961 p
->codec
.h264
.rc_mb_smooth
= ctrl
->val
;
1963 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC
:
1964 p
->codec
.h264
.rc_mb_static
= ctrl
->val
;
1966 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY
:
1967 p
->codec
.h264
.rc_mb_activity
= ctrl
->val
;
1969 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE
:
1970 p
->codec
.h264
.vui_sar
= ctrl
->val
;
1972 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC
:
1973 p
->codec
.h264
.vui_sar_idc
= vui_sar_idc(ctrl
->val
);
1975 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH
:
1976 p
->codec
.h264
.vui_ext_sar_width
= ctrl
->val
;
1978 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT
:
1979 p
->codec
.h264
.vui_ext_sar_height
= ctrl
->val
;
1981 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE
:
1982 p
->codec
.h264
.open_gop
= !ctrl
->val
;
1984 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
:
1985 p
->codec
.h264
.open_gop_size
= ctrl
->val
;
1987 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
:
1988 switch (ctrl
->val
) {
1989 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
:
1990 p
->codec
.mpeg4
.profile
=
1991 S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE
;
1993 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE
:
1994 p
->codec
.mpeg4
.profile
=
1995 S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE
;
2001 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL
:
2002 p
->codec
.mpeg4
.quarter_pixel
= ctrl
->val
;
2004 case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS
:
2005 p
->codec
.vp8
.num_partitions
= ctrl
->val
;
2007 case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4
:
2008 p
->codec
.vp8
.imd_4x4
= ctrl
->val
;
2010 case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES
:
2011 p
->codec
.vp8
.num_ref
= ctrl
->val
;
2013 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL
:
2014 p
->codec
.vp8
.filter_level
= ctrl
->val
;
2016 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS
:
2017 p
->codec
.vp8
.filter_sharpness
= ctrl
->val
;
2019 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD
:
2020 p
->codec
.vp8
.golden_frame_ref_period
= ctrl
->val
;
2022 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL
:
2023 p
->codec
.vp8
.golden_frame_sel
= ctrl
->val
;
2025 case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP
:
2026 p
->codec
.vp8
.rc_min_qp
= ctrl
->val
;
2028 case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP
:
2029 p
->codec
.vp8
.rc_max_qp
= ctrl
->val
;
2031 case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP
:
2032 p
->codec
.vp8
.rc_frame_qp
= ctrl
->val
;
2034 case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP
:
2035 p
->codec
.vp8
.rc_p_frame_qp
= ctrl
->val
;
2037 case V4L2_CID_MPEG_VIDEO_VP8_PROFILE
:
2038 p
->codec
.vp8
.profile
= ctrl
->val
;
2040 case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP
:
2041 p
->codec
.hevc
.rc_frame_qp
= ctrl
->val
;
2043 case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP
:
2044 p
->codec
.hevc
.rc_p_frame_qp
= ctrl
->val
;
2046 case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP
:
2047 p
->codec
.hevc
.rc_b_frame_qp
= ctrl
->val
;
2049 case V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION
:
2050 p
->codec
.hevc
.rc_framerate
= ctrl
->val
;
2052 case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP
:
2053 p
->codec
.hevc
.rc_min_qp
= ctrl
->val
;
2054 __enc_update_hevc_qp_ctrls_range(ctx
, ctrl
->val
,
2055 p
->codec
.hevc
.rc_max_qp
);
2057 case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP
:
2058 p
->codec
.hevc
.rc_max_qp
= ctrl
->val
;
2059 __enc_update_hevc_qp_ctrls_range(ctx
, p
->codec
.hevc
.rc_min_qp
,
2062 case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL
:
2063 p
->codec
.hevc
.level_v4l2
= ctrl
->val
;
2064 p
->codec
.hevc
.level
= hevc_level(ctrl
->val
);
2066 case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE
:
2067 switch (ctrl
->val
) {
2068 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
:
2069 p
->codec
.hevc
.profile
=
2070 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
;
2072 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE
:
2073 p
->codec
.hevc
.profile
=
2074 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE
;
2080 case V4L2_CID_MPEG_VIDEO_HEVC_TIER
:
2081 p
->codec
.hevc
.tier
= ctrl
->val
;
2083 case V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH
:
2084 p
->codec
.hevc
.max_partition_depth
= ctrl
->val
;
2086 case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES
:
2087 p
->codec
.hevc
.num_refs_for_p
= ctrl
->val
;
2089 case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE
:
2090 p
->codec
.hevc
.refreshtype
= ctrl
->val
;
2092 case V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED
:
2093 p
->codec
.hevc
.const_intra_period_enable
= ctrl
->val
;
2095 case V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU
:
2096 p
->codec
.hevc
.lossless_cu_enable
= ctrl
->val
;
2098 case V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT
:
2099 p
->codec
.hevc
.wavefront_enable
= ctrl
->val
;
2101 case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE
:
2102 p
->codec
.hevc
.loopfilter
= ctrl
->val
;
2104 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP
:
2105 p
->codec
.hevc
.hier_qp_enable
= ctrl
->val
;
2107 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE
:
2108 p
->codec
.hevc
.hier_qp_type
= ctrl
->val
;
2110 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER
:
2111 p
->codec
.hevc
.num_hier_layer
= ctrl
->val
;
2113 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP
:
2114 p
->codec
.hevc
.hier_qp_layer
[0] = ctrl
->val
;
2116 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP
:
2117 p
->codec
.hevc
.hier_qp_layer
[1] = ctrl
->val
;
2119 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP
:
2120 p
->codec
.hevc
.hier_qp_layer
[2] = ctrl
->val
;
2122 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP
:
2123 p
->codec
.hevc
.hier_qp_layer
[3] = ctrl
->val
;
2125 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP
:
2126 p
->codec
.hevc
.hier_qp_layer
[4] = ctrl
->val
;
2128 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP
:
2129 p
->codec
.hevc
.hier_qp_layer
[5] = ctrl
->val
;
2131 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP
:
2132 p
->codec
.hevc
.hier_qp_layer
[6] = ctrl
->val
;
2134 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR
:
2135 p
->codec
.hevc
.hier_bit_layer
[0] = ctrl
->val
;
2137 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR
:
2138 p
->codec
.hevc
.hier_bit_layer
[1] = ctrl
->val
;
2140 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR
:
2141 p
->codec
.hevc
.hier_bit_layer
[2] = ctrl
->val
;
2143 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR
:
2144 p
->codec
.hevc
.hier_bit_layer
[3] = ctrl
->val
;
2146 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR
:
2147 p
->codec
.hevc
.hier_bit_layer
[4] = ctrl
->val
;
2149 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR
:
2150 p
->codec
.hevc
.hier_bit_layer
[5] = ctrl
->val
;
2152 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR
:
2153 p
->codec
.hevc
.hier_bit_layer
[6] = ctrl
->val
;
2155 case V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB
:
2156 p
->codec
.hevc
.general_pb_enable
= ctrl
->val
;
2158 case V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID
:
2159 p
->codec
.hevc
.temporal_id_enable
= ctrl
->val
;
2161 case V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING
:
2162 p
->codec
.hevc
.strong_intra_smooth
= ctrl
->val
;
2164 case V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT
:
2165 p
->codec
.hevc
.intra_pu_split_disable
= ctrl
->val
;
2167 case V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION
:
2168 p
->codec
.hevc
.tmv_prediction_disable
= !ctrl
->val
;
2170 case V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1
:
2171 p
->codec
.hevc
.max_num_merge_mv
= ctrl
->val
;
2173 case V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE
:
2174 p
->codec
.hevc
.encoding_nostartcode_enable
= ctrl
->val
;
2176 case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD
:
2177 p
->codec
.hevc
.refreshperiod
= ctrl
->val
;
2179 case V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2
:
2180 p
->codec
.hevc
.lf_beta_offset_div2
= ctrl
->val
;
2182 case V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2
:
2183 p
->codec
.hevc
.lf_tc_offset_div2
= ctrl
->val
;
2185 case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD
:
2186 p
->codec
.hevc
.size_of_length_field
= ctrl
->val
;
2188 case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR
:
2189 p
->codec
.hevc
.prepend_sps_pps_to_idr
= ctrl
->val
;
2192 v4l2_err(&dev
->v4l2_dev
, "Invalid control, id=%d, val=%d\n",
2193 ctrl
->id
, ctrl
->val
);
2199 static int s5p_mfc_enc_g_v_ctrl(struct v4l2_ctrl
*ctrl
)
2201 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
2202 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2205 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT
:
2206 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
2207 ctx
->state
< MFCINST_ABORT
) {
2208 ctrl
->val
= ctx
->pb_count
;
2210 } else if (ctx
->state
!= MFCINST_INIT
) {
2211 v4l2_err(&dev
->v4l2_dev
, "Encoding not initialised\n");
2214 /* Should wait for the header to be produced */
2215 s5p_mfc_wait_for_done_ctx(ctx
,
2216 S5P_MFC_R2H_CMD_SEQ_DONE_RET
, 0);
2217 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
2218 ctx
->state
< MFCINST_ABORT
) {
2219 ctrl
->val
= ctx
->pb_count
;
2221 v4l2_err(&dev
->v4l2_dev
, "Encoding not initialised\n");
2229 static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops
= {
2230 .s_ctrl
= s5p_mfc_enc_s_ctrl
,
2231 .g_volatile_ctrl
= s5p_mfc_enc_g_v_ctrl
,
2234 static int vidioc_s_parm(struct file
*file
, void *priv
,
2235 struct v4l2_streamparm
*a
)
2237 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
2239 if (a
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2240 ctx
->enc_params
.rc_framerate_num
=
2241 a
->parm
.output
.timeperframe
.denominator
;
2242 ctx
->enc_params
.rc_framerate_denom
=
2243 a
->parm
.output
.timeperframe
.numerator
;
2245 mfc_err("Setting FPS is only possible for the output queue\n");
2251 static int vidioc_g_parm(struct file
*file
, void *priv
,
2252 struct v4l2_streamparm
*a
)
2254 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
2256 if (a
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
2257 a
->parm
.output
.timeperframe
.denominator
=
2258 ctx
->enc_params
.rc_framerate_num
;
2259 a
->parm
.output
.timeperframe
.numerator
=
2260 ctx
->enc_params
.rc_framerate_denom
;
2262 mfc_err("Setting FPS is only possible for the output queue\n");
2268 static int vidioc_encoder_cmd(struct file
*file
, void *priv
,
2269 struct v4l2_encoder_cmd
*cmd
)
2271 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
2272 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2273 struct s5p_mfc_buf
*buf
;
2274 unsigned long flags
;
2277 case V4L2_ENC_CMD_STOP
:
2278 if (cmd
->flags
!= 0)
2281 if (!ctx
->vq_src
.streaming
)
2284 spin_lock_irqsave(&dev
->irqlock
, flags
);
2285 if (list_empty(&ctx
->src_queue
)) {
2286 mfc_debug(2, "EOS: empty src queue, entering finishing state\n");
2287 ctx
->state
= MFCINST_FINISHING
;
2288 if (s5p_mfc_ctx_ready(ctx
))
2289 set_work_bit_irqsave(ctx
);
2290 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2291 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
2293 mfc_debug(2, "EOS: marking last buffer of stream\n");
2294 buf
= list_entry(ctx
->src_queue
.prev
,
2295 struct s5p_mfc_buf
, list
);
2296 if (buf
->flags
& MFC_BUF_FLAG_USED
)
2297 ctx
->state
= MFCINST_FINISHING
;
2299 buf
->flags
|= MFC_BUF_FLAG_EOS
;
2300 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2310 static int vidioc_subscribe_event(struct v4l2_fh
*fh
,
2311 const struct v4l2_event_subscription
*sub
)
2313 switch (sub
->type
) {
2314 case V4L2_EVENT_EOS
:
2315 return v4l2_event_subscribe(fh
, sub
, 2, NULL
);
2321 static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops
= {
2322 .vidioc_querycap
= vidioc_querycap
,
2323 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2324 .vidioc_enum_fmt_vid_out
= vidioc_enum_fmt_vid_out
,
2325 .vidioc_g_fmt_vid_cap_mplane
= vidioc_g_fmt
,
2326 .vidioc_g_fmt_vid_out_mplane
= vidioc_g_fmt
,
2327 .vidioc_try_fmt_vid_cap_mplane
= vidioc_try_fmt
,
2328 .vidioc_try_fmt_vid_out_mplane
= vidioc_try_fmt
,
2329 .vidioc_s_fmt_vid_cap_mplane
= vidioc_s_fmt
,
2330 .vidioc_s_fmt_vid_out_mplane
= vidioc_s_fmt
,
2331 .vidioc_reqbufs
= vidioc_reqbufs
,
2332 .vidioc_querybuf
= vidioc_querybuf
,
2333 .vidioc_qbuf
= vidioc_qbuf
,
2334 .vidioc_dqbuf
= vidioc_dqbuf
,
2335 .vidioc_expbuf
= vidioc_expbuf
,
2336 .vidioc_streamon
= vidioc_streamon
,
2337 .vidioc_streamoff
= vidioc_streamoff
,
2338 .vidioc_s_parm
= vidioc_s_parm
,
2339 .vidioc_g_parm
= vidioc_g_parm
,
2340 .vidioc_encoder_cmd
= vidioc_encoder_cmd
,
2341 .vidioc_subscribe_event
= vidioc_subscribe_event
,
2342 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
2345 static int check_vb_with_fmt(struct s5p_mfc_fmt
*fmt
, struct vb2_buffer
*vb
)
2351 if (fmt
->num_planes
!= vb
->num_planes
) {
2352 mfc_err("invalid plane number for the format\n");
2355 for (i
= 0; i
< fmt
->num_planes
; i
++) {
2356 dma_addr_t dma
= vb2_dma_contig_plane_dma_addr(vb
, i
);
2358 mfc_err("failed to get plane cookie\n");
2361 mfc_debug(2, "index: %d, plane[%d] cookie: %pad\n",
2362 vb
->index
, i
, &dma
);
2367 static int s5p_mfc_queue_setup(struct vb2_queue
*vq
,
2368 unsigned int *buf_count
, unsigned int *plane_count
,
2369 unsigned int psize
[], struct device
*alloc_devs
[])
2371 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
2372 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2374 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2375 if (ctx
->state
!= MFCINST_GOT_INST
) {
2376 mfc_err("invalid state: %d\n", ctx
->state
);
2381 *plane_count
= ctx
->dst_fmt
->num_planes
;
2383 *plane_count
= MFC_ENC_CAP_PLANE_COUNT
;
2386 if (*buf_count
> MFC_MAX_BUFFERS
)
2387 *buf_count
= MFC_MAX_BUFFERS
;
2388 psize
[0] = ctx
->enc_dst_buf_size
;
2389 alloc_devs
[0] = ctx
->dev
->mem_dev
[BANK_L_CTX
];
2390 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2392 *plane_count
= ctx
->src_fmt
->num_planes
;
2394 *plane_count
= MFC_ENC_OUT_PLANE_COUNT
;
2398 if (*buf_count
> MFC_MAX_BUFFERS
)
2399 *buf_count
= MFC_MAX_BUFFERS
;
2401 psize
[0] = ctx
->luma_size
;
2402 psize
[1] = ctx
->chroma_size
;
2404 if (IS_MFCV6_PLUS(dev
)) {
2405 alloc_devs
[0] = ctx
->dev
->mem_dev
[BANK_L_CTX
];
2406 alloc_devs
[1] = ctx
->dev
->mem_dev
[BANK_L_CTX
];
2408 alloc_devs
[0] = ctx
->dev
->mem_dev
[BANK_R_CTX
];
2409 alloc_devs
[1] = ctx
->dev
->mem_dev
[BANK_R_CTX
];
2412 mfc_err("invalid queue type: %d\n", vq
->type
);
2418 static int s5p_mfc_buf_init(struct vb2_buffer
*vb
)
2420 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
2421 struct vb2_queue
*vq
= vb
->vb2_queue
;
2422 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
2426 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2427 ret
= check_vb_with_fmt(ctx
->dst_fmt
, vb
);
2431 ctx
->dst_bufs
[i
].b
= vbuf
;
2432 ctx
->dst_bufs
[i
].cookie
.stream
=
2433 vb2_dma_contig_plane_dma_addr(vb
, 0);
2434 ctx
->dst_bufs_cnt
++;
2435 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2436 ret
= check_vb_with_fmt(ctx
->src_fmt
, vb
);
2440 ctx
->src_bufs
[i
].b
= vbuf
;
2441 ctx
->src_bufs
[i
].cookie
.raw
.luma
=
2442 vb2_dma_contig_plane_dma_addr(vb
, 0);
2443 ctx
->src_bufs
[i
].cookie
.raw
.chroma
=
2444 vb2_dma_contig_plane_dma_addr(vb
, 1);
2445 ctx
->src_bufs_cnt
++;
2447 mfc_err("invalid queue type: %d\n", vq
->type
);
2453 static int s5p_mfc_buf_prepare(struct vb2_buffer
*vb
)
2455 struct vb2_queue
*vq
= vb
->vb2_queue
;
2456 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
2459 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2460 ret
= check_vb_with_fmt(ctx
->dst_fmt
, vb
);
2463 mfc_debug(2, "plane size: %ld, dst size: %zu\n",
2464 vb2_plane_size(vb
, 0), ctx
->enc_dst_buf_size
);
2465 if (vb2_plane_size(vb
, 0) < ctx
->enc_dst_buf_size
) {
2466 mfc_err("plane size is too small for capture\n");
2469 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2470 ret
= check_vb_with_fmt(ctx
->src_fmt
, vb
);
2473 mfc_debug(2, "plane size: %ld, luma size: %d\n",
2474 vb2_plane_size(vb
, 0), ctx
->luma_size
);
2475 mfc_debug(2, "plane size: %ld, chroma size: %d\n",
2476 vb2_plane_size(vb
, 1), ctx
->chroma_size
);
2477 if (vb2_plane_size(vb
, 0) < ctx
->luma_size
||
2478 vb2_plane_size(vb
, 1) < ctx
->chroma_size
) {
2479 mfc_err("plane size is too small for output\n");
2483 mfc_err("invalid queue type: %d\n", vq
->type
);
2489 static int s5p_mfc_start_streaming(struct vb2_queue
*q
, unsigned int count
)
2491 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
2492 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2494 if (IS_MFCV6_PLUS(dev
) &&
2495 (q
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)) {
2497 if ((ctx
->state
== MFCINST_GOT_INST
) &&
2498 (dev
->curr_ctx
== ctx
->num
) && dev
->hw_lock
) {
2499 s5p_mfc_wait_for_done_ctx(ctx
,
2500 S5P_MFC_R2H_CMD_SEQ_DONE_RET
,
2504 if (ctx
->src_bufs_cnt
< ctx
->pb_count
) {
2505 mfc_err("Need minimum %d OUTPUT buffers\n",
2511 /* If context is ready then dev = work->data;schedule it to run */
2512 if (s5p_mfc_ctx_ready(ctx
))
2513 set_work_bit_irqsave(ctx
);
2514 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
2519 static void s5p_mfc_stop_streaming(struct vb2_queue
*q
)
2521 unsigned long flags
;
2522 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
2523 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2525 if ((ctx
->state
== MFCINST_FINISHING
||
2526 ctx
->state
== MFCINST_RUNNING
) &&
2527 dev
->curr_ctx
== ctx
->num
&& dev
->hw_lock
) {
2528 ctx
->state
= MFCINST_ABORT
;
2529 s5p_mfc_wait_for_done_ctx(ctx
, S5P_MFC_R2H_CMD_FRAME_DONE_RET
,
2532 ctx
->state
= MFCINST_FINISHED
;
2533 spin_lock_irqsave(&dev
->irqlock
, flags
);
2534 if (q
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2535 s5p_mfc_cleanup_queue(&ctx
->dst_queue
, &ctx
->vq_dst
);
2536 INIT_LIST_HEAD(&ctx
->dst_queue
);
2537 ctx
->dst_queue_cnt
= 0;
2539 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2540 cleanup_ref_queue(ctx
);
2541 s5p_mfc_cleanup_queue(&ctx
->src_queue
, &ctx
->vq_src
);
2542 INIT_LIST_HEAD(&ctx
->src_queue
);
2543 ctx
->src_queue_cnt
= 0;
2545 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2548 static void s5p_mfc_buf_queue(struct vb2_buffer
*vb
)
2550 struct vb2_queue
*vq
= vb
->vb2_queue
;
2551 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
2552 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2553 unsigned long flags
;
2554 struct s5p_mfc_buf
*mfc_buf
;
2556 if (ctx
->state
== MFCINST_ERROR
) {
2557 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
2558 cleanup_ref_queue(ctx
);
2561 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2562 mfc_buf
= &ctx
->dst_bufs
[vb
->index
];
2563 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
2564 /* Mark destination as available for use by MFC */
2565 spin_lock_irqsave(&dev
->irqlock
, flags
);
2566 list_add_tail(&mfc_buf
->list
, &ctx
->dst_queue
);
2567 ctx
->dst_queue_cnt
++;
2568 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2569 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2570 mfc_buf
= &ctx
->src_bufs
[vb
->index
];
2571 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
2572 spin_lock_irqsave(&dev
->irqlock
, flags
);
2573 list_add_tail(&mfc_buf
->list
, &ctx
->src_queue
);
2574 ctx
->src_queue_cnt
++;
2575 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2577 mfc_err("unsupported buffer type (%d)\n", vq
->type
);
2579 if (s5p_mfc_ctx_ready(ctx
))
2580 set_work_bit_irqsave(ctx
);
2581 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
2584 static struct vb2_ops s5p_mfc_enc_qops
= {
2585 .queue_setup
= s5p_mfc_queue_setup
,
2586 .wait_prepare
= vb2_ops_wait_prepare
,
2587 .wait_finish
= vb2_ops_wait_finish
,
2588 .buf_init
= s5p_mfc_buf_init
,
2589 .buf_prepare
= s5p_mfc_buf_prepare
,
2590 .start_streaming
= s5p_mfc_start_streaming
,
2591 .stop_streaming
= s5p_mfc_stop_streaming
,
2592 .buf_queue
= s5p_mfc_buf_queue
,
2595 const struct s5p_mfc_codec_ops
*get_enc_codec_ops(void)
2597 return &encoder_codec_ops
;
2600 struct vb2_ops
*get_enc_queue_ops(void)
2602 return &s5p_mfc_enc_qops
;
2605 const struct v4l2_ioctl_ops
*get_enc_v4l2_ioctl_ops(void)
2607 return &s5p_mfc_enc_ioctl_ops
;
2610 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2WHICH(x) == V4L2_CTRL_CLASS_MPEG) \
2611 && V4L2_CTRL_DRIVER_PRIV(x))
2613 int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx
*ctx
)
2615 struct v4l2_ctrl_config cfg
;
2618 v4l2_ctrl_handler_init(&ctx
->ctrl_handler
, NUM_CTRLS
);
2619 if (ctx
->ctrl_handler
.error
) {
2620 mfc_err("v4l2_ctrl_handler_init failed\n");
2621 return ctx
->ctrl_handler
.error
;
2623 for (i
= 0; i
< NUM_CTRLS
; i
++) {
2624 if (IS_MFC51_PRIV(controls
[i
].id
)) {
2625 memset(&cfg
, 0, sizeof(struct v4l2_ctrl_config
));
2626 cfg
.ops
= &s5p_mfc_enc_ctrl_ops
;
2627 cfg
.id
= controls
[i
].id
;
2628 cfg
.min
= controls
[i
].minimum
;
2629 cfg
.max
= controls
[i
].maximum
;
2630 cfg
.def
= controls
[i
].default_value
;
2631 cfg
.name
= controls
[i
].name
;
2632 cfg
.type
= controls
[i
].type
;
2635 if (cfg
.type
== V4L2_CTRL_TYPE_MENU
) {
2637 cfg
.menu_skip_mask
= controls
[i
].menu_skip_mask
;
2638 cfg
.qmenu
= mfc51_get_menu(cfg
.id
);
2640 cfg
.step
= controls
[i
].step
;
2641 cfg
.menu_skip_mask
= 0;
2643 ctx
->ctrls
[i
] = v4l2_ctrl_new_custom(&ctx
->ctrl_handler
,
2646 if ((controls
[i
].type
== V4L2_CTRL_TYPE_MENU
) ||
2647 (controls
[i
].type
==
2648 V4L2_CTRL_TYPE_INTEGER_MENU
)) {
2649 ctx
->ctrls
[i
] = v4l2_ctrl_new_std_menu(
2651 &s5p_mfc_enc_ctrl_ops
, controls
[i
].id
,
2652 controls
[i
].maximum
, 0,
2653 controls
[i
].default_value
);
2655 ctx
->ctrls
[i
] = v4l2_ctrl_new_std(
2657 &s5p_mfc_enc_ctrl_ops
, controls
[i
].id
,
2658 controls
[i
].minimum
,
2659 controls
[i
].maximum
, controls
[i
].step
,
2660 controls
[i
].default_value
);
2663 if (ctx
->ctrl_handler
.error
) {
2664 mfc_err("Adding control (%d) failed\n", i
);
2665 return ctx
->ctrl_handler
.error
;
2667 if (controls
[i
].is_volatile
&& ctx
->ctrls
[i
])
2668 ctx
->ctrls
[i
]->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
2670 v4l2_ctrl_handler_setup(&ctx
->ctrl_handler
);
2674 void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx
*ctx
)
2678 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
2679 for (i
= 0; i
< NUM_CTRLS
; i
++)
2680 ctx
->ctrls
[i
] = NULL
;
2683 void s5p_mfc_enc_init(struct s5p_mfc_ctx
*ctx
)
2685 struct v4l2_format f
;
2686 f
.fmt
.pix_mp
.pixelformat
= DEF_SRC_FMT_ENC
;
2687 ctx
->src_fmt
= find_format(&f
, MFC_FMT_RAW
);
2688 f
.fmt
.pix_mp
.pixelformat
= DEF_DST_FMT_ENC
;
2689 ctx
->dst_fmt
= find_format(&f
, MFC_FMT_ENC
);