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_VIDEO_FRAME_SKIP_MODE
,
266 .type
= V4L2_CTRL_TYPE_MENU
,
267 .maximum
= V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT
,
268 .default_value
= V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED
,
271 .id
= V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT
,
272 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
273 .name
= "Fixed Target Bit Enable",
281 .id
= V4L2_CID_MPEG_VIDEO_B_FRAMES
,
282 .type
= V4L2_CTRL_TYPE_INTEGER
,
289 .id
= V4L2_CID_MPEG_VIDEO_H264_PROFILE
,
290 .type
= V4L2_CTRL_TYPE_MENU
,
291 .minimum
= V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
,
292 .maximum
= V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH
,
293 .default_value
= V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
,
295 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
) |
296 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN
) |
297 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
)
301 .id
= V4L2_CID_MPEG_VIDEO_H264_LEVEL
,
302 .type
= V4L2_CTRL_TYPE_MENU
,
303 .minimum
= V4L2_MPEG_VIDEO_H264_LEVEL_1_0
,
304 .maximum
= V4L2_MPEG_VIDEO_H264_LEVEL_4_0
,
305 .default_value
= V4L2_MPEG_VIDEO_H264_LEVEL_1_0
,
308 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
,
309 .type
= V4L2_CTRL_TYPE_MENU
,
310 .minimum
= V4L2_MPEG_VIDEO_MPEG4_LEVEL_0
,
311 .maximum
= V4L2_MPEG_VIDEO_MPEG4_LEVEL_5
,
312 .default_value
= V4L2_MPEG_VIDEO_MPEG4_LEVEL_0
,
316 .id
= V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
,
317 .type
= V4L2_CTRL_TYPE_MENU
,
318 .minimum
= V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
,
319 .maximum
= V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY
,
320 .default_value
= V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED
,
324 .id
= V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
,
325 .type
= V4L2_CTRL_TYPE_INTEGER
,
332 .id
= V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
,
333 .type
= V4L2_CTRL_TYPE_INTEGER
,
340 .id
= V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
,
341 .type
= V4L2_CTRL_TYPE_MENU
,
342 .minimum
= V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC
,
343 .maximum
= V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC
,
344 .default_value
= V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC
,
348 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P
,
349 .type
= V4L2_CTRL_TYPE_INTEGER
,
350 .name
= "The Number of Ref. Pic for P",
357 .id
= V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM
,
358 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
365 .id
= V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE
,
366 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
373 .id
= V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
,
374 .type
= V4L2_CTRL_TYPE_INTEGER
,
381 .id
= V4L2_CID_MPEG_VIDEO_H264_MIN_QP
,
382 .type
= V4L2_CTRL_TYPE_INTEGER
,
389 .id
= V4L2_CID_MPEG_VIDEO_H264_MAX_QP
,
390 .type
= V4L2_CTRL_TYPE_INTEGER
,
397 .id
= V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
,
398 .type
= V4L2_CTRL_TYPE_INTEGER
,
405 .id
= V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP
,
406 .type
= V4L2_CTRL_TYPE_INTEGER
,
413 .id
= V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP
,
414 .type
= V4L2_CTRL_TYPE_INTEGER
,
415 .name
= "H263 I-Frame QP value",
422 .id
= V4L2_CID_MPEG_VIDEO_H263_MIN_QP
,
423 .type
= V4L2_CTRL_TYPE_INTEGER
,
424 .name
= "H263 Minimum QP value",
431 .id
= V4L2_CID_MPEG_VIDEO_H263_MAX_QP
,
432 .type
= V4L2_CTRL_TYPE_INTEGER
,
433 .name
= "H263 Maximum QP value",
440 .id
= V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP
,
441 .type
= V4L2_CTRL_TYPE_INTEGER
,
442 .name
= "H263 P frame QP value",
449 .id
= V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP
,
450 .type
= V4L2_CTRL_TYPE_INTEGER
,
451 .name
= "H263 B frame QP value",
458 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
,
459 .type
= V4L2_CTRL_TYPE_INTEGER
,
460 .name
= "MPEG4 I-Frame QP value",
467 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP
,
468 .type
= V4L2_CTRL_TYPE_INTEGER
,
469 .name
= "MPEG4 Minimum QP value",
476 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP
,
477 .type
= V4L2_CTRL_TYPE_INTEGER
,
478 .name
= "MPEG4 Maximum QP value",
485 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
,
486 .type
= V4L2_CTRL_TYPE_INTEGER
,
487 .name
= "MPEG4 P frame QP value",
494 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP
,
495 .type
= V4L2_CTRL_TYPE_INTEGER
,
496 .name
= "MPEG4 B frame QP value",
503 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK
,
504 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
505 .name
= "H264 Dark Reg Adaptive RC",
512 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH
,
513 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
514 .name
= "H264 Smooth Reg Adaptive RC",
521 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC
,
522 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
523 .name
= "H264 Static Reg Adaptive RC",
530 .id
= V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY
,
531 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
532 .name
= "H264 Activity Reg Adaptive RC",
539 .id
= V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE
,
540 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
547 .id
= V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC
,
548 .type
= V4L2_CTRL_TYPE_MENU
,
549 .minimum
= V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED
,
550 .maximum
= V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED
,
551 .default_value
= V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED
,
555 .id
= V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH
,
556 .type
= V4L2_CTRL_TYPE_INTEGER
,
558 .maximum
= (1 << 16) - 1,
563 .id
= V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT
,
564 .type
= V4L2_CTRL_TYPE_INTEGER
,
566 .maximum
= (1 << 16) - 1,
571 .id
= V4L2_CID_MPEG_VIDEO_GOP_CLOSURE
,
572 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
579 .id
= V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
,
580 .type
= V4L2_CTRL_TYPE_INTEGER
,
582 .maximum
= (1 << 16) - 1,
587 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
,
588 .type
= V4L2_CTRL_TYPE_MENU
,
589 .minimum
= V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
,
590 .maximum
= V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE
,
591 .default_value
= V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
,
595 .id
= V4L2_CID_MPEG_VIDEO_MPEG4_QPEL
,
596 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
603 .id
= V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS
,
604 .type
= V4L2_CTRL_TYPE_INTEGER_MENU
,
605 .maximum
= V4L2_CID_MPEG_VIDEO_VPX_8_PARTITIONS
,
606 .default_value
= V4L2_CID_MPEG_VIDEO_VPX_1_PARTITION
,
610 .id
= V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4
,
611 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
618 .id
= V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES
,
619 .type
= V4L2_CTRL_TYPE_INTEGER_MENU
,
620 .maximum
= V4L2_CID_MPEG_VIDEO_VPX_2_REF_FRAME
,
621 .default_value
= V4L2_CID_MPEG_VIDEO_VPX_1_REF_FRAME
,
625 .id
= V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL
,
626 .type
= V4L2_CTRL_TYPE_INTEGER
,
633 .id
= V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS
,
634 .type
= V4L2_CTRL_TYPE_INTEGER
,
641 .id
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD
,
642 .type
= V4L2_CTRL_TYPE_INTEGER
,
644 .maximum
= (1 << 16) - 1,
649 .id
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL
,
650 .type
= V4L2_CTRL_TYPE_MENU
,
651 .minimum
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV
,
652 .maximum
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_REF_PERIOD
,
653 .default_value
= V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV
,
657 .id
= V4L2_CID_MPEG_VIDEO_VPX_MAX_QP
,
658 .type
= V4L2_CTRL_TYPE_INTEGER
,
662 .default_value
= 127,
665 .id
= V4L2_CID_MPEG_VIDEO_VPX_MIN_QP
,
666 .type
= V4L2_CTRL_TYPE_INTEGER
,
673 .id
= V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP
,
674 .type
= V4L2_CTRL_TYPE_INTEGER
,
681 .id
= V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP
,
682 .type
= V4L2_CTRL_TYPE_INTEGER
,
689 .id
= V4L2_CID_MPEG_VIDEO_VP8_PROFILE
,
690 .type
= V4L2_CTRL_TYPE_MENU
,
691 .minimum
= V4L2_MPEG_VIDEO_VP8_PROFILE_0
,
692 .maximum
= V4L2_MPEG_VIDEO_VP8_PROFILE_3
,
693 .default_value
= V4L2_MPEG_VIDEO_VP8_PROFILE_0
,
697 .id
= V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP
,
698 .type
= V4L2_CTRL_TYPE_INTEGER
,
699 .name
= "HEVC I Frame QP Value",
706 .id
= V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP
,
707 .type
= V4L2_CTRL_TYPE_INTEGER
,
708 .name
= "HEVC P Frame QP Value",
715 .id
= V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP
,
716 .type
= V4L2_CTRL_TYPE_INTEGER
,
723 .id
= V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP
,
724 .type
= V4L2_CTRL_TYPE_INTEGER
,
731 .id
= V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP
,
732 .type
= V4L2_CTRL_TYPE_INTEGER
,
739 .id
= V4L2_CID_MPEG_VIDEO_HEVC_PROFILE
,
740 .type
= V4L2_CTRL_TYPE_MENU
,
741 .minimum
= V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
,
742 .maximum
= V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE
,
744 .default_value
= V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
,
747 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LEVEL
,
748 .type
= V4L2_CTRL_TYPE_MENU
,
749 .minimum
= V4L2_MPEG_VIDEO_HEVC_LEVEL_1
,
750 .maximum
= V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2
,
752 .default_value
= V4L2_MPEG_VIDEO_HEVC_LEVEL_1
,
755 .id
= V4L2_CID_MPEG_VIDEO_HEVC_TIER
,
756 .type
= V4L2_CTRL_TYPE_MENU
,
757 .minimum
= V4L2_MPEG_VIDEO_HEVC_TIER_MAIN
,
758 .maximum
= V4L2_MPEG_VIDEO_HEVC_TIER_HIGH
,
760 .default_value
= V4L2_MPEG_VIDEO_HEVC_TIER_MAIN
,
763 .id
= V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION
,
764 .type
= V4L2_CTRL_TYPE_INTEGER
,
766 .maximum
= (1 << 16) - 1,
771 .id
= V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH
,
772 .type
= V4L2_CTRL_TYPE_INTEGER
,
779 .id
= V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES
,
780 .type
= V4L2_CTRL_TYPE_INTEGER
,
787 .id
= V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE
,
788 .type
= V4L2_CTRL_TYPE_MENU
,
789 .minimum
= V4L2_MPEG_VIDEO_HEVC_REFRESH_NONE
,
790 .maximum
= V4L2_MPEG_VIDEO_HEVC_REFRESH_IDR
,
792 .default_value
= V4L2_MPEG_VIDEO_HEVC_REFRESH_NONE
,
795 .id
= V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED
,
796 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
803 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU
,
804 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
811 .id
= V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT
,
812 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
819 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE
,
820 .type
= V4L2_CTRL_TYPE_MENU
,
821 .minimum
= V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED
,
822 .maximum
= V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY
,
824 .default_value
= V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED
,
827 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP
,
828 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
835 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE
,
836 .type
= V4L2_CTRL_TYPE_MENU
,
837 .minimum
= V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_B
,
838 .maximum
= V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_P
,
840 .default_value
= V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_B
,
843 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER
,
844 .type
= V4L2_CTRL_TYPE_INTEGER
,
851 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP
,
852 .type
= V4L2_CTRL_TYPE_INTEGER
,
859 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP
,
860 .type
= V4L2_CTRL_TYPE_INTEGER
,
867 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP
,
868 .type
= V4L2_CTRL_TYPE_INTEGER
,
875 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP
,
876 .type
= V4L2_CTRL_TYPE_INTEGER
,
883 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP
,
884 .type
= V4L2_CTRL_TYPE_INTEGER
,
891 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP
,
892 .type
= V4L2_CTRL_TYPE_INTEGER
,
899 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP
,
900 .type
= V4L2_CTRL_TYPE_INTEGER
,
907 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR
,
908 .type
= V4L2_CTRL_TYPE_INTEGER
,
915 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR
,
916 .type
= V4L2_CTRL_TYPE_INTEGER
,
923 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR
,
924 .type
= V4L2_CTRL_TYPE_INTEGER
,
931 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR
,
932 .type
= V4L2_CTRL_TYPE_INTEGER
,
939 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR
,
940 .type
= V4L2_CTRL_TYPE_INTEGER
,
947 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR
,
948 .type
= V4L2_CTRL_TYPE_INTEGER
,
955 .id
= V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR
,
956 .type
= V4L2_CTRL_TYPE_INTEGER
,
963 .id
= V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB
,
964 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
971 .id
= V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID
,
972 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
979 .id
= V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING
,
980 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
987 .id
= V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT
,
988 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
995 .id
= V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION
,
996 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1003 .id
= V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1
,
1004 .type
= V4L2_CTRL_TYPE_INTEGER
,
1011 .id
= V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE
,
1012 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1019 .id
= V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD
,
1020 .type
= V4L2_CTRL_TYPE_INTEGER
,
1022 .maximum
= (1 << 16) - 1,
1027 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2
,
1028 .type
= V4L2_CTRL_TYPE_INTEGER
,
1035 .id
= V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2
,
1036 .type
= V4L2_CTRL_TYPE_INTEGER
,
1043 .id
= V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD
,
1044 .type
= V4L2_CTRL_TYPE_MENU
,
1045 .minimum
= V4L2_MPEG_VIDEO_HEVC_SIZE_0
,
1046 .maximum
= V4L2_MPEG_VIDEO_HEVC_SIZE_4
,
1048 .default_value
= V4L2_MPEG_VIDEO_HEVC_SIZE_0
,
1051 .id
= V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR
,
1052 .type
= V4L2_CTRL_TYPE_INTEGER
,
1059 .id
= V4L2_CID_MIN_BUFFERS_FOR_OUTPUT
,
1060 .type
= V4L2_CTRL_TYPE_INTEGER
,
1061 .name
= "Minimum number of output bufs",
1070 #define NUM_CTRLS ARRAY_SIZE(controls)
1071 static const char * const *mfc51_get_menu(u32 id
)
1073 static const char * const mfc51_video_frame_skip
[] = {
1079 static const char * const mfc51_video_force_frame
[] = {
1086 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE
:
1087 return mfc51_video_frame_skip
;
1088 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE
:
1089 return mfc51_video_force_frame
;
1094 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx
*ctx
)
1096 mfc_debug(2, "src=%d, dst=%d, state=%d\n",
1097 ctx
->src_queue_cnt
, ctx
->dst_queue_cnt
, ctx
->state
);
1098 /* context is ready to make header */
1099 if (ctx
->state
== MFCINST_GOT_INST
&& ctx
->dst_queue_cnt
>= 1)
1101 /* context is ready to encode a frame */
1102 if ((ctx
->state
== MFCINST_RUNNING
||
1103 ctx
->state
== MFCINST_HEAD_PRODUCED
) &&
1104 ctx
->src_queue_cnt
>= 1 && ctx
->dst_queue_cnt
>= 1)
1106 /* context is ready to encode remaining frames */
1107 if (ctx
->state
== MFCINST_FINISHING
&&
1108 ctx
->dst_queue_cnt
>= 1)
1110 mfc_debug(2, "ctx is not ready\n");
1114 static void cleanup_ref_queue(struct s5p_mfc_ctx
*ctx
)
1116 struct s5p_mfc_buf
*mb_entry
;
1118 /* move buffers in ref queue to src queue */
1119 while (!list_empty(&ctx
->ref_queue
)) {
1120 mb_entry
= list_entry((&ctx
->ref_queue
)->next
,
1121 struct s5p_mfc_buf
, list
);
1122 list_del(&mb_entry
->list
);
1123 ctx
->ref_queue_cnt
--;
1124 list_add_tail(&mb_entry
->list
, &ctx
->src_queue
);
1125 ctx
->src_queue_cnt
++;
1127 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
1128 ctx
->src_queue_cnt
, ctx
->ref_queue_cnt
);
1129 INIT_LIST_HEAD(&ctx
->ref_queue
);
1130 ctx
->ref_queue_cnt
= 0;
1133 static int enc_pre_seq_start(struct s5p_mfc_ctx
*ctx
)
1135 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1136 struct s5p_mfc_buf
*dst_mb
;
1137 unsigned long dst_addr
;
1138 unsigned int dst_size
;
1140 dst_mb
= list_entry(ctx
->dst_queue
.next
, struct s5p_mfc_buf
, list
);
1141 dst_addr
= vb2_dma_contig_plane_dma_addr(&dst_mb
->b
->vb2_buf
, 0);
1142 dst_size
= vb2_plane_size(&dst_mb
->b
->vb2_buf
, 0);
1143 s5p_mfc_hw_call(dev
->mfc_ops
, set_enc_stream_buffer
, ctx
, dst_addr
,
1148 static int enc_post_seq_start(struct s5p_mfc_ctx
*ctx
)
1150 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1151 struct s5p_mfc_enc_params
*p
= &ctx
->enc_params
;
1152 struct s5p_mfc_buf
*dst_mb
;
1153 unsigned int enc_pb_count
;
1155 if (p
->seq_hdr_mode
== V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
) {
1156 if (!list_empty(&ctx
->dst_queue
)) {
1157 dst_mb
= list_entry(ctx
->dst_queue
.next
,
1158 struct s5p_mfc_buf
, list
);
1159 list_del(&dst_mb
->list
);
1160 ctx
->dst_queue_cnt
--;
1161 vb2_set_plane_payload(&dst_mb
->b
->vb2_buf
, 0,
1162 s5p_mfc_hw_call(dev
->mfc_ops
, get_enc_strm_size
,
1164 vb2_buffer_done(&dst_mb
->b
->vb2_buf
,
1165 VB2_BUF_STATE_DONE
);
1169 if (!IS_MFCV6_PLUS(dev
)) {
1170 ctx
->state
= MFCINST_RUNNING
;
1171 if (s5p_mfc_ctx_ready(ctx
))
1172 set_work_bit_irqsave(ctx
);
1173 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
1175 enc_pb_count
= s5p_mfc_hw_call(dev
->mfc_ops
,
1176 get_enc_dpb_count
, dev
);
1177 if (ctx
->pb_count
< enc_pb_count
)
1178 ctx
->pb_count
= enc_pb_count
;
1179 if (FW_HAS_E_MIN_SCRATCH_BUF(dev
)) {
1180 ctx
->scratch_buf_size
= s5p_mfc_hw_call(dev
->mfc_ops
,
1181 get_e_min_scratch_buf_size
, dev
);
1182 ctx
->bank1
.size
+= ctx
->scratch_buf_size
;
1184 ctx
->state
= MFCINST_HEAD_PRODUCED
;
1190 static int enc_pre_frame_start(struct s5p_mfc_ctx
*ctx
)
1192 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1193 struct s5p_mfc_buf
*dst_mb
;
1194 struct s5p_mfc_buf
*src_mb
;
1195 unsigned long src_y_addr
, src_c_addr
, dst_addr
;
1196 unsigned int dst_size
;
1198 src_mb
= list_entry(ctx
->src_queue
.next
, struct s5p_mfc_buf
, list
);
1199 src_y_addr
= vb2_dma_contig_plane_dma_addr(&src_mb
->b
->vb2_buf
, 0);
1200 src_c_addr
= vb2_dma_contig_plane_dma_addr(&src_mb
->b
->vb2_buf
, 1);
1201 s5p_mfc_hw_call(dev
->mfc_ops
, set_enc_frame_buffer
, ctx
,
1202 src_y_addr
, src_c_addr
);
1204 dst_mb
= list_entry(ctx
->dst_queue
.next
, struct s5p_mfc_buf
, list
);
1205 dst_addr
= vb2_dma_contig_plane_dma_addr(&dst_mb
->b
->vb2_buf
, 0);
1206 dst_size
= vb2_plane_size(&dst_mb
->b
->vb2_buf
, 0);
1207 s5p_mfc_hw_call(dev
->mfc_ops
, set_enc_stream_buffer
, ctx
, dst_addr
,
1213 static int enc_post_frame_start(struct s5p_mfc_ctx
*ctx
)
1215 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1216 struct s5p_mfc_buf
*mb_entry
;
1217 unsigned long enc_y_addr
= 0, enc_c_addr
= 0;
1218 unsigned long mb_y_addr
, mb_c_addr
;
1220 unsigned int strm_size
;
1222 slice_type
= s5p_mfc_hw_call(dev
->mfc_ops
, get_enc_slice_type
, dev
);
1223 strm_size
= s5p_mfc_hw_call(dev
->mfc_ops
, get_enc_strm_size
, dev
);
1224 mfc_debug(2, "Encoded slice type: %d\n", slice_type
);
1225 mfc_debug(2, "Encoded stream size: %d\n", strm_size
);
1226 mfc_debug(2, "Display order: %d\n",
1227 mfc_read(dev
, S5P_FIMV_ENC_SI_PIC_CNT
));
1228 if (slice_type
>= 0) {
1229 s5p_mfc_hw_call(dev
->mfc_ops
, get_enc_frame_buffer
, ctx
,
1230 &enc_y_addr
, &enc_c_addr
);
1231 list_for_each_entry(mb_entry
, &ctx
->src_queue
, list
) {
1232 mb_y_addr
= vb2_dma_contig_plane_dma_addr(
1233 &mb_entry
->b
->vb2_buf
, 0);
1234 mb_c_addr
= vb2_dma_contig_plane_dma_addr(
1235 &mb_entry
->b
->vb2_buf
, 1);
1236 if ((enc_y_addr
== mb_y_addr
) &&
1237 (enc_c_addr
== mb_c_addr
)) {
1238 list_del(&mb_entry
->list
);
1239 ctx
->src_queue_cnt
--;
1240 vb2_buffer_done(&mb_entry
->b
->vb2_buf
,
1241 VB2_BUF_STATE_DONE
);
1245 list_for_each_entry(mb_entry
, &ctx
->ref_queue
, list
) {
1246 mb_y_addr
= vb2_dma_contig_plane_dma_addr(
1247 &mb_entry
->b
->vb2_buf
, 0);
1248 mb_c_addr
= vb2_dma_contig_plane_dma_addr(
1249 &mb_entry
->b
->vb2_buf
, 1);
1250 if ((enc_y_addr
== mb_y_addr
) &&
1251 (enc_c_addr
== mb_c_addr
)) {
1252 list_del(&mb_entry
->list
);
1253 ctx
->ref_queue_cnt
--;
1254 vb2_buffer_done(&mb_entry
->b
->vb2_buf
,
1255 VB2_BUF_STATE_DONE
);
1260 if ((ctx
->src_queue_cnt
> 0) && (ctx
->state
== MFCINST_RUNNING
)) {
1261 mb_entry
= list_entry(ctx
->src_queue
.next
, struct s5p_mfc_buf
,
1263 if (mb_entry
->flags
& MFC_BUF_FLAG_USED
) {
1264 list_del(&mb_entry
->list
);
1265 ctx
->src_queue_cnt
--;
1266 list_add_tail(&mb_entry
->list
, &ctx
->ref_queue
);
1267 ctx
->ref_queue_cnt
++;
1270 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
1271 ctx
->src_queue_cnt
, ctx
->ref_queue_cnt
);
1272 if ((ctx
->dst_queue_cnt
> 0) && (strm_size
> 0)) {
1273 mb_entry
= list_entry(ctx
->dst_queue
.next
, struct s5p_mfc_buf
,
1275 list_del(&mb_entry
->list
);
1276 ctx
->dst_queue_cnt
--;
1277 switch (slice_type
) {
1278 case S5P_FIMV_ENC_SI_SLICE_TYPE_I
:
1279 mb_entry
->b
->flags
|= V4L2_BUF_FLAG_KEYFRAME
;
1281 case S5P_FIMV_ENC_SI_SLICE_TYPE_P
:
1282 mb_entry
->b
->flags
|= V4L2_BUF_FLAG_PFRAME
;
1284 case S5P_FIMV_ENC_SI_SLICE_TYPE_B
:
1285 mb_entry
->b
->flags
|= V4L2_BUF_FLAG_BFRAME
;
1288 vb2_set_plane_payload(&mb_entry
->b
->vb2_buf
, 0, strm_size
);
1289 vb2_buffer_done(&mb_entry
->b
->vb2_buf
, VB2_BUF_STATE_DONE
);
1291 if ((ctx
->src_queue_cnt
== 0) || (ctx
->dst_queue_cnt
== 0))
1292 clear_work_bit(ctx
);
1297 static const struct s5p_mfc_codec_ops encoder_codec_ops
= {
1298 .pre_seq_start
= enc_pre_seq_start
,
1299 .post_seq_start
= enc_post_seq_start
,
1300 .pre_frame_start
= enc_pre_frame_start
,
1301 .post_frame_start
= enc_post_frame_start
,
1304 /* Query capabilities of the device */
1305 static int vidioc_querycap(struct file
*file
, void *priv
,
1306 struct v4l2_capability
*cap
)
1308 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1310 strscpy(cap
->driver
, S5P_MFC_NAME
, sizeof(cap
->driver
));
1311 strscpy(cap
->card
, dev
->vfd_enc
->name
, sizeof(cap
->card
));
1312 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
1313 dev_name(&dev
->plat_dev
->dev
));
1317 static int vidioc_enum_fmt(struct file
*file
, struct v4l2_fmtdesc
*f
,
1320 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1323 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
1324 if (out
&& formats
[i
].type
!= MFC_FMT_RAW
)
1326 else if (!out
&& formats
[i
].type
!= MFC_FMT_ENC
)
1328 else if ((dev
->variant
->version_bit
& formats
[i
].versions
) == 0)
1331 if (j
== f
->index
) {
1332 f
->pixelformat
= formats
[i
].fourcc
;
1340 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *pirv
,
1341 struct v4l2_fmtdesc
*f
)
1343 return vidioc_enum_fmt(file
, f
, false);
1346 static int vidioc_enum_fmt_vid_out(struct file
*file
, void *priv
,
1347 struct v4l2_fmtdesc
*f
)
1349 return vidioc_enum_fmt(file
, f
, true);
1352 static int vidioc_g_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1354 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1355 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
1357 mfc_debug(2, "f->type = %d ctx->state = %d\n", f
->type
, ctx
->state
);
1358 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1359 /* This is run on output (encoder dest) */
1360 pix_fmt_mp
->width
= 0;
1361 pix_fmt_mp
->height
= 0;
1362 pix_fmt_mp
->field
= V4L2_FIELD_NONE
;
1363 pix_fmt_mp
->pixelformat
= ctx
->dst_fmt
->fourcc
;
1364 pix_fmt_mp
->num_planes
= ctx
->dst_fmt
->num_planes
;
1366 pix_fmt_mp
->plane_fmt
[0].bytesperline
= ctx
->enc_dst_buf_size
;
1367 pix_fmt_mp
->plane_fmt
[0].sizeimage
= ctx
->enc_dst_buf_size
;
1368 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1369 /* This is run on capture (encoder src) */
1370 pix_fmt_mp
->width
= ctx
->img_width
;
1371 pix_fmt_mp
->height
= ctx
->img_height
;
1373 pix_fmt_mp
->field
= V4L2_FIELD_NONE
;
1374 pix_fmt_mp
->pixelformat
= ctx
->src_fmt
->fourcc
;
1375 pix_fmt_mp
->num_planes
= ctx
->src_fmt
->num_planes
;
1377 pix_fmt_mp
->plane_fmt
[0].bytesperline
= ctx
->buf_width
;
1378 pix_fmt_mp
->plane_fmt
[0].sizeimage
= ctx
->luma_size
;
1379 pix_fmt_mp
->plane_fmt
[1].bytesperline
= ctx
->buf_width
;
1380 pix_fmt_mp
->plane_fmt
[1].sizeimage
= ctx
->chroma_size
;
1382 mfc_err("invalid buf type\n");
1388 static int vidioc_try_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1390 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1391 struct s5p_mfc_fmt
*fmt
;
1392 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
1394 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1395 fmt
= find_format(f
, MFC_FMT_ENC
);
1397 mfc_err("failed to try output format\n");
1400 if ((dev
->variant
->version_bit
& fmt
->versions
) == 0) {
1401 mfc_err("Unsupported format by this MFC version.\n");
1405 pix_fmt_mp
->plane_fmt
[0].bytesperline
=
1406 pix_fmt_mp
->plane_fmt
[0].sizeimage
;
1407 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1408 fmt
= find_format(f
, MFC_FMT_RAW
);
1410 mfc_err("failed to try output format\n");
1413 if ((dev
->variant
->version_bit
& fmt
->versions
) == 0) {
1414 mfc_err("Unsupported format by this MFC version.\n");
1418 v4l_bound_align_image(&pix_fmt_mp
->width
, 8, 1920, 1,
1419 &pix_fmt_mp
->height
, 4, 1080, 1, 0);
1421 mfc_err("invalid buf type\n");
1427 static int vidioc_s_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1429 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1430 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1431 struct v4l2_pix_format_mplane
*pix_fmt_mp
= &f
->fmt
.pix_mp
;
1434 ret
= vidioc_try_fmt(file
, priv
, f
);
1437 if (ctx
->vq_src
.streaming
|| ctx
->vq_dst
.streaming
) {
1438 v4l2_err(&dev
->v4l2_dev
, "%s queue busy\n", __func__
);
1442 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1443 /* dst_fmt is validated by call to vidioc_try_fmt */
1444 ctx
->dst_fmt
= find_format(f
, MFC_FMT_ENC
);
1445 ctx
->state
= MFCINST_INIT
;
1446 ctx
->codec_mode
= ctx
->dst_fmt
->codec_mode
;
1447 ctx
->enc_dst_buf_size
= pix_fmt_mp
->plane_fmt
[0].sizeimage
;
1448 pix_fmt_mp
->plane_fmt
[0].bytesperline
= 0;
1449 ctx
->dst_bufs_cnt
= 0;
1450 ctx
->capture_state
= QUEUE_FREE
;
1451 ret
= s5p_mfc_open_mfc_inst(dev
, ctx
);
1452 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1453 /* src_fmt is validated by call to vidioc_try_fmt */
1454 ctx
->src_fmt
= find_format(f
, MFC_FMT_RAW
);
1455 ctx
->img_width
= pix_fmt_mp
->width
;
1456 ctx
->img_height
= pix_fmt_mp
->height
;
1457 mfc_debug(2, "codec number: %d\n", ctx
->src_fmt
->codec_mode
);
1458 mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1459 pix_fmt_mp
->width
, pix_fmt_mp
->height
,
1460 ctx
->img_width
, ctx
->img_height
);
1462 s5p_mfc_hw_call(dev
->mfc_ops
, enc_calc_src_size
, ctx
);
1463 pix_fmt_mp
->plane_fmt
[0].sizeimage
= ctx
->luma_size
;
1464 pix_fmt_mp
->plane_fmt
[0].bytesperline
= ctx
->buf_width
;
1465 pix_fmt_mp
->plane_fmt
[1].sizeimage
= ctx
->chroma_size
;
1466 pix_fmt_mp
->plane_fmt
[1].bytesperline
= ctx
->buf_width
;
1468 ctx
->src_bufs_cnt
= 0;
1469 ctx
->output_state
= QUEUE_FREE
;
1471 mfc_err("invalid buf type\n");
1479 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1480 struct v4l2_requestbuffers
*reqbufs
)
1482 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
1483 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1486 /* if memory is not mmp or userptr return error */
1487 if ((reqbufs
->memory
!= V4L2_MEMORY_MMAP
) &&
1488 (reqbufs
->memory
!= V4L2_MEMORY_USERPTR
))
1490 if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1491 if (reqbufs
->count
== 0) {
1492 mfc_debug(2, "Freeing buffers\n");
1493 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
1494 s5p_mfc_hw_call(dev
->mfc_ops
, release_codec_buffers
,
1496 ctx
->capture_state
= QUEUE_FREE
;
1499 if (ctx
->capture_state
!= QUEUE_FREE
) {
1500 mfc_err("invalid capture state: %d\n",
1501 ctx
->capture_state
);
1504 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
1506 mfc_err("error in vb2_reqbufs() for E(D)\n");
1509 ctx
->capture_state
= QUEUE_BUFS_REQUESTED
;
1511 ret
= s5p_mfc_hw_call(ctx
->dev
->mfc_ops
,
1512 alloc_codec_buffers
, ctx
);
1514 mfc_err("Failed to allocate encoding buffers\n");
1516 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
1519 } else if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1520 if (reqbufs
->count
== 0) {
1521 mfc_debug(2, "Freeing buffers\n");
1522 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
1523 s5p_mfc_hw_call(dev
->mfc_ops
, release_codec_buffers
,
1525 ctx
->output_state
= QUEUE_FREE
;
1528 if (ctx
->output_state
!= QUEUE_FREE
) {
1529 mfc_err("invalid output state: %d\n",
1534 if (IS_MFCV6_PLUS(dev
)) {
1535 /* Check for min encoder buffers */
1536 if (ctx
->pb_count
&&
1537 (reqbufs
->count
< ctx
->pb_count
)) {
1538 reqbufs
->count
= ctx
->pb_count
;
1539 mfc_debug(2, "Minimum %d output buffers needed\n",
1542 ctx
->pb_count
= reqbufs
->count
;
1546 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
1548 mfc_err("error in vb2_reqbufs() for E(S)\n");
1551 ctx
->output_state
= QUEUE_BUFS_REQUESTED
;
1553 mfc_err("invalid buf type\n");
1559 static int vidioc_querybuf(struct file
*file
, void *priv
,
1560 struct v4l2_buffer
*buf
)
1562 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1565 /* if memory is not mmp or userptr return error */
1566 if ((buf
->memory
!= V4L2_MEMORY_MMAP
) &&
1567 (buf
->memory
!= V4L2_MEMORY_USERPTR
))
1569 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1570 if (ctx
->state
!= MFCINST_GOT_INST
) {
1571 mfc_err("invalid context state: %d\n", ctx
->state
);
1574 ret
= vb2_querybuf(&ctx
->vq_dst
, buf
);
1576 mfc_err("error in vb2_querybuf() for E(D)\n");
1579 buf
->m
.planes
[0].m
.mem_offset
+= DST_QUEUE_OFF_BASE
;
1580 } else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1581 ret
= vb2_querybuf(&ctx
->vq_src
, buf
);
1583 mfc_err("error in vb2_querybuf() for E(S)\n");
1587 mfc_err("invalid buf type\n");
1593 /* Queue a buffer */
1594 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
1596 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1598 if (ctx
->state
== MFCINST_ERROR
) {
1599 mfc_err("Call on QBUF after unrecoverable error\n");
1602 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1603 if (ctx
->state
== MFCINST_FINISHING
) {
1604 mfc_err("Call on QBUF after EOS command\n");
1607 return vb2_qbuf(&ctx
->vq_src
, NULL
, buf
);
1608 } else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1609 return vb2_qbuf(&ctx
->vq_dst
, NULL
, buf
);
1614 /* Dequeue a buffer */
1615 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
1617 const struct v4l2_event ev
= {
1618 .type
= V4L2_EVENT_EOS
1620 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1623 if (ctx
->state
== MFCINST_ERROR
) {
1624 mfc_err_limited("Call on DQBUF after unrecoverable error\n");
1627 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1628 ret
= vb2_dqbuf(&ctx
->vq_src
, buf
, file
->f_flags
& O_NONBLOCK
);
1629 } else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1630 ret
= vb2_dqbuf(&ctx
->vq_dst
, buf
, file
->f_flags
& O_NONBLOCK
);
1631 if (ret
== 0 && ctx
->state
== MFCINST_FINISHED
1632 && list_empty(&ctx
->vq_dst
.done_list
))
1633 v4l2_event_queue_fh(&ctx
->fh
, &ev
);
1641 /* Export DMA buffer */
1642 static int vidioc_expbuf(struct file
*file
, void *priv
,
1643 struct v4l2_exportbuffer
*eb
)
1645 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1647 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
1648 return vb2_expbuf(&ctx
->vq_src
, eb
);
1649 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1650 return vb2_expbuf(&ctx
->vq_dst
, eb
);
1655 static int vidioc_streamon(struct file
*file
, void *priv
,
1656 enum v4l2_buf_type type
)
1658 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1660 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
1661 return vb2_streamon(&ctx
->vq_src
, type
);
1662 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1663 return vb2_streamon(&ctx
->vq_dst
, type
);
1667 /* Stream off, which equals to a pause */
1668 static int vidioc_streamoff(struct file
*file
, void *priv
,
1669 enum v4l2_buf_type type
)
1671 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
1673 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
1674 return vb2_streamoff(&ctx
->vq_src
, type
);
1675 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1676 return vb2_streamoff(&ctx
->vq_dst
, type
);
1680 static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl
)
1682 static unsigned int t
[V4L2_MPEG_VIDEO_H264_LEVEL_4_0
+ 1] = {
1683 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0 */ 10,
1684 /* V4L2_MPEG_VIDEO_H264_LEVEL_1B */ 9,
1685 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1 */ 11,
1686 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2 */ 12,
1687 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3 */ 13,
1688 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0 */ 20,
1689 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1 */ 21,
1690 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2 */ 22,
1691 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0 */ 30,
1692 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1 */ 31,
1693 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2 */ 32,
1694 /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0 */ 40,
1699 static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl
)
1701 static unsigned int t
[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5
+ 1] = {
1702 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 */ 0,
1703 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B */ 9,
1704 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 */ 1,
1705 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 */ 2,
1706 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 */ 3,
1707 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B */ 7,
1708 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 */ 4,
1709 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 */ 5,
1714 static inline int hevc_level(enum v4l2_mpeg_video_hevc_level lvl
)
1716 static unsigned int t
[] = {
1717 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_1 */ 10,
1718 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_2 */ 20,
1719 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1 */ 21,
1720 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_3 */ 30,
1721 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1 */ 31,
1722 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_4 */ 40,
1723 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1 */ 41,
1724 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_5 */ 50,
1725 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1 */ 51,
1726 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2 */ 52,
1727 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_6 */ 60,
1728 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1 */ 61,
1729 /* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2 */ 62,
1734 static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar
)
1736 static unsigned int t
[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED
+ 1] = {
1737 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED */ 0,
1738 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1 */ 1,
1739 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11 */ 2,
1740 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11 */ 3,
1741 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11 */ 4,
1742 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33 */ 5,
1743 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11 */ 6,
1744 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11 */ 7,
1745 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11 */ 8,
1746 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33 */ 9,
1747 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11 */ 10,
1748 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11 */ 11,
1749 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33 */ 12,
1750 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99 */ 13,
1751 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3 */ 14,
1752 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2 */ 15,
1753 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1 */ 16,
1754 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED */ 255,
1760 * Update range of all HEVC quantization parameter controls that depend on the
1761 * V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP controls.
1763 static void __enc_update_hevc_qp_ctrls_range(struct s5p_mfc_ctx
*ctx
,
1766 static const int __hevc_qp_ctrls
[] = {
1767 V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP
,
1768 V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP
,
1769 V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP
,
1770 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP
,
1771 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP
,
1772 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP
,
1773 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP
,
1774 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP
,
1775 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP
,
1776 V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP
,
1778 struct v4l2_ctrl
*ctrl
= NULL
;
1781 for (i
= 0; i
< ARRAY_SIZE(__hevc_qp_ctrls
); i
++) {
1782 for (j
= 0; j
< ARRAY_SIZE(ctx
->ctrls
); j
++) {
1783 if (ctx
->ctrls
[j
]->id
== __hevc_qp_ctrls
[i
]) {
1784 ctrl
= ctx
->ctrls
[j
];
1791 __v4l2_ctrl_modify_range(ctrl
, min
, max
, ctrl
->step
, min
);
1795 static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl
*ctrl
)
1797 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
1798 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1799 struct s5p_mfc_enc_params
*p
= &ctx
->enc_params
;
1803 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
1804 p
->gop_size
= ctrl
->val
;
1806 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
1807 p
->slice_mode
= ctrl
->val
;
1809 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
1810 p
->slice_mb
= ctrl
->val
;
1812 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
1813 p
->slice_bit
= ctrl
->val
* 8;
1815 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
:
1816 p
->intra_refresh_mb
= ctrl
->val
;
1818 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING
:
1821 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV
:
1822 p
->pad_luma
= (ctrl
->val
>> 16) & 0xff;
1823 p
->pad_cb
= (ctrl
->val
>> 8) & 0xff;
1824 p
->pad_cr
= (ctrl
->val
>> 0) & 0xff;
1826 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE
:
1827 p
->rc_frame
= ctrl
->val
;
1829 case V4L2_CID_MPEG_VIDEO_BITRATE
:
1830 p
->rc_bitrate
= ctrl
->val
;
1832 case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF
:
1833 p
->rc_reaction_coeff
= ctrl
->val
;
1835 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE
:
1836 ctx
->force_frame_type
= ctrl
->val
;
1838 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME
:
1839 ctx
->force_frame_type
=
1840 V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_I_FRAME
;
1842 case V4L2_CID_MPEG_VIDEO_VBV_SIZE
:
1843 p
->vbv_size
= ctrl
->val
;
1845 case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE
:
1846 p
->mv_h_range
= ctrl
->val
;
1848 case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE
:
1849 p
->mv_v_range
= ctrl
->val
;
1851 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE
:
1852 p
->codec
.h264
.cpb_size
= ctrl
->val
;
1854 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
1855 p
->seq_hdr_mode
= ctrl
->val
;
1857 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE
:
1858 case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE
:
1859 p
->frame_skip_mode
= ctrl
->val
;
1861 case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT
:
1862 p
->fixed_target_bit
= ctrl
->val
;
1864 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
1865 p
->num_b_frame
= ctrl
->val
;
1867 case V4L2_CID_MPEG_VIDEO_H264_PROFILE
:
1868 switch (ctrl
->val
) {
1869 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN
:
1870 p
->codec
.h264
.profile
=
1871 S5P_FIMV_ENC_PROFILE_H264_MAIN
;
1873 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
:
1874 p
->codec
.h264
.profile
=
1875 S5P_FIMV_ENC_PROFILE_H264_HIGH
;
1877 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
:
1878 p
->codec
.h264
.profile
=
1879 S5P_FIMV_ENC_PROFILE_H264_BASELINE
;
1881 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE
:
1882 if (IS_MFCV6_PLUS(dev
))
1883 p
->codec
.h264
.profile
=
1884 S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE
;
1892 case V4L2_CID_MPEG_VIDEO_H264_LEVEL
:
1893 p
->codec
.h264
.level_v4l2
= ctrl
->val
;
1894 p
->codec
.h264
.level
= h264_level(ctrl
->val
);
1895 if (p
->codec
.h264
.level
< 0) {
1896 mfc_err("Level number is wrong\n");
1897 ret
= p
->codec
.h264
.level
;
1900 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
:
1901 p
->codec
.mpeg4
.level_v4l2
= ctrl
->val
;
1902 p
->codec
.mpeg4
.level
= mpeg4_level(ctrl
->val
);
1903 if (p
->codec
.mpeg4
.level
< 0) {
1904 mfc_err("Level number is wrong\n");
1905 ret
= p
->codec
.mpeg4
.level
;
1908 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
:
1909 p
->codec
.h264
.loop_filter_mode
= ctrl
->val
;
1911 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
:
1912 p
->codec
.h264
.loop_filter_alpha
= ctrl
->val
;
1914 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
:
1915 p
->codec
.h264
.loop_filter_beta
= ctrl
->val
;
1917 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
:
1918 p
->codec
.h264
.entropy_mode
= ctrl
->val
;
1920 case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P
:
1921 p
->codec
.h264
.num_ref_pic_4p
= ctrl
->val
;
1923 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM
:
1924 p
->codec
.h264
._8x8_transform
= ctrl
->val
;
1926 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE
:
1927 p
->rc_mb
= ctrl
->val
;
1929 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
1930 p
->codec
.h264
.rc_frame_qp
= ctrl
->val
;
1932 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
1933 p
->codec
.h264
.rc_min_qp
= ctrl
->val
;
1935 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
1936 p
->codec
.h264
.rc_max_qp
= ctrl
->val
;
1938 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
1939 p
->codec
.h264
.rc_p_frame_qp
= ctrl
->val
;
1941 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP
:
1942 p
->codec
.h264
.rc_b_frame_qp
= ctrl
->val
;
1944 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP
:
1945 case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP
:
1946 p
->codec
.mpeg4
.rc_frame_qp
= ctrl
->val
;
1948 case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP
:
1949 case V4L2_CID_MPEG_VIDEO_H263_MIN_QP
:
1950 p
->codec
.mpeg4
.rc_min_qp
= ctrl
->val
;
1952 case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP
:
1953 case V4L2_CID_MPEG_VIDEO_H263_MAX_QP
:
1954 p
->codec
.mpeg4
.rc_max_qp
= ctrl
->val
;
1956 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP
:
1957 case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP
:
1958 p
->codec
.mpeg4
.rc_p_frame_qp
= ctrl
->val
;
1960 case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP
:
1961 case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP
:
1962 p
->codec
.mpeg4
.rc_b_frame_qp
= ctrl
->val
;
1964 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK
:
1965 p
->codec
.h264
.rc_mb_dark
= ctrl
->val
;
1967 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH
:
1968 p
->codec
.h264
.rc_mb_smooth
= ctrl
->val
;
1970 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC
:
1971 p
->codec
.h264
.rc_mb_static
= ctrl
->val
;
1973 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY
:
1974 p
->codec
.h264
.rc_mb_activity
= ctrl
->val
;
1976 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE
:
1977 p
->codec
.h264
.vui_sar
= ctrl
->val
;
1979 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC
:
1980 p
->codec
.h264
.vui_sar_idc
= vui_sar_idc(ctrl
->val
);
1982 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH
:
1983 p
->codec
.h264
.vui_ext_sar_width
= ctrl
->val
;
1985 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT
:
1986 p
->codec
.h264
.vui_ext_sar_height
= ctrl
->val
;
1988 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE
:
1989 p
->codec
.h264
.open_gop
= !ctrl
->val
;
1991 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
:
1992 p
->codec
.h264
.open_gop_size
= ctrl
->val
;
1994 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
:
1995 switch (ctrl
->val
) {
1996 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
:
1997 p
->codec
.mpeg4
.profile
=
1998 S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE
;
2000 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE
:
2001 p
->codec
.mpeg4
.profile
=
2002 S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE
;
2008 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL
:
2009 p
->codec
.mpeg4
.quarter_pixel
= ctrl
->val
;
2011 case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS
:
2012 p
->codec
.vp8
.num_partitions
= ctrl
->val
;
2014 case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4
:
2015 p
->codec
.vp8
.imd_4x4
= ctrl
->val
;
2017 case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES
:
2018 p
->codec
.vp8
.num_ref
= ctrl
->val
;
2020 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL
:
2021 p
->codec
.vp8
.filter_level
= ctrl
->val
;
2023 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS
:
2024 p
->codec
.vp8
.filter_sharpness
= ctrl
->val
;
2026 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD
:
2027 p
->codec
.vp8
.golden_frame_ref_period
= ctrl
->val
;
2029 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL
:
2030 p
->codec
.vp8
.golden_frame_sel
= ctrl
->val
;
2032 case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP
:
2033 p
->codec
.vp8
.rc_min_qp
= ctrl
->val
;
2035 case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP
:
2036 p
->codec
.vp8
.rc_max_qp
= ctrl
->val
;
2038 case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP
:
2039 p
->codec
.vp8
.rc_frame_qp
= ctrl
->val
;
2041 case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP
:
2042 p
->codec
.vp8
.rc_p_frame_qp
= ctrl
->val
;
2044 case V4L2_CID_MPEG_VIDEO_VP8_PROFILE
:
2045 p
->codec
.vp8
.profile
= ctrl
->val
;
2047 case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP
:
2048 p
->codec
.hevc
.rc_frame_qp
= ctrl
->val
;
2050 case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP
:
2051 p
->codec
.hevc
.rc_p_frame_qp
= ctrl
->val
;
2053 case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP
:
2054 p
->codec
.hevc
.rc_b_frame_qp
= ctrl
->val
;
2056 case V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION
:
2057 p
->codec
.hevc
.rc_framerate
= ctrl
->val
;
2059 case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP
:
2060 p
->codec
.hevc
.rc_min_qp
= ctrl
->val
;
2061 __enc_update_hevc_qp_ctrls_range(ctx
, ctrl
->val
,
2062 p
->codec
.hevc
.rc_max_qp
);
2064 case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP
:
2065 p
->codec
.hevc
.rc_max_qp
= ctrl
->val
;
2066 __enc_update_hevc_qp_ctrls_range(ctx
, p
->codec
.hevc
.rc_min_qp
,
2069 case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL
:
2070 p
->codec
.hevc
.level_v4l2
= ctrl
->val
;
2071 p
->codec
.hevc
.level
= hevc_level(ctrl
->val
);
2073 case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE
:
2074 switch (ctrl
->val
) {
2075 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
:
2076 p
->codec
.hevc
.profile
=
2077 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
;
2079 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE
:
2080 p
->codec
.hevc
.profile
=
2081 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE
;
2087 case V4L2_CID_MPEG_VIDEO_HEVC_TIER
:
2088 p
->codec
.hevc
.tier
= ctrl
->val
;
2090 case V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH
:
2091 p
->codec
.hevc
.max_partition_depth
= ctrl
->val
;
2093 case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES
:
2094 p
->codec
.hevc
.num_refs_for_p
= ctrl
->val
;
2096 case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE
:
2097 p
->codec
.hevc
.refreshtype
= ctrl
->val
;
2099 case V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED
:
2100 p
->codec
.hevc
.const_intra_period_enable
= ctrl
->val
;
2102 case V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU
:
2103 p
->codec
.hevc
.lossless_cu_enable
= ctrl
->val
;
2105 case V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT
:
2106 p
->codec
.hevc
.wavefront_enable
= ctrl
->val
;
2108 case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE
:
2109 p
->codec
.hevc
.loopfilter
= ctrl
->val
;
2111 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP
:
2112 p
->codec
.hevc
.hier_qp_enable
= ctrl
->val
;
2114 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE
:
2115 p
->codec
.hevc
.hier_qp_type
= ctrl
->val
;
2117 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER
:
2118 p
->codec
.hevc
.num_hier_layer
= ctrl
->val
;
2120 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP
:
2121 p
->codec
.hevc
.hier_qp_layer
[0] = ctrl
->val
;
2123 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP
:
2124 p
->codec
.hevc
.hier_qp_layer
[1] = ctrl
->val
;
2126 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP
:
2127 p
->codec
.hevc
.hier_qp_layer
[2] = ctrl
->val
;
2129 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP
:
2130 p
->codec
.hevc
.hier_qp_layer
[3] = ctrl
->val
;
2132 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP
:
2133 p
->codec
.hevc
.hier_qp_layer
[4] = ctrl
->val
;
2135 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP
:
2136 p
->codec
.hevc
.hier_qp_layer
[5] = ctrl
->val
;
2138 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP
:
2139 p
->codec
.hevc
.hier_qp_layer
[6] = ctrl
->val
;
2141 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR
:
2142 p
->codec
.hevc
.hier_bit_layer
[0] = ctrl
->val
;
2144 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR
:
2145 p
->codec
.hevc
.hier_bit_layer
[1] = ctrl
->val
;
2147 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR
:
2148 p
->codec
.hevc
.hier_bit_layer
[2] = ctrl
->val
;
2150 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR
:
2151 p
->codec
.hevc
.hier_bit_layer
[3] = ctrl
->val
;
2153 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR
:
2154 p
->codec
.hevc
.hier_bit_layer
[4] = ctrl
->val
;
2156 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR
:
2157 p
->codec
.hevc
.hier_bit_layer
[5] = ctrl
->val
;
2159 case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR
:
2160 p
->codec
.hevc
.hier_bit_layer
[6] = ctrl
->val
;
2162 case V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB
:
2163 p
->codec
.hevc
.general_pb_enable
= ctrl
->val
;
2165 case V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID
:
2166 p
->codec
.hevc
.temporal_id_enable
= ctrl
->val
;
2168 case V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING
:
2169 p
->codec
.hevc
.strong_intra_smooth
= ctrl
->val
;
2171 case V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT
:
2172 p
->codec
.hevc
.intra_pu_split_disable
= ctrl
->val
;
2174 case V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION
:
2175 p
->codec
.hevc
.tmv_prediction_disable
= !ctrl
->val
;
2177 case V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1
:
2178 p
->codec
.hevc
.max_num_merge_mv
= ctrl
->val
;
2180 case V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE
:
2181 p
->codec
.hevc
.encoding_nostartcode_enable
= ctrl
->val
;
2183 case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD
:
2184 p
->codec
.hevc
.refreshperiod
= ctrl
->val
;
2186 case V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2
:
2187 p
->codec
.hevc
.lf_beta_offset_div2
= ctrl
->val
;
2189 case V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2
:
2190 p
->codec
.hevc
.lf_tc_offset_div2
= ctrl
->val
;
2192 case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD
:
2193 p
->codec
.hevc
.size_of_length_field
= ctrl
->val
;
2195 case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR
:
2196 p
->codec
.hevc
.prepend_sps_pps_to_idr
= ctrl
->val
;
2199 v4l2_err(&dev
->v4l2_dev
, "Invalid control, id=%d, val=%d\n",
2200 ctrl
->id
, ctrl
->val
);
2206 static int s5p_mfc_enc_g_v_ctrl(struct v4l2_ctrl
*ctrl
)
2208 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
2209 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2212 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT
:
2213 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
2214 ctx
->state
< MFCINST_ABORT
) {
2215 ctrl
->val
= ctx
->pb_count
;
2217 } else if (ctx
->state
!= MFCINST_INIT
) {
2218 v4l2_err(&dev
->v4l2_dev
, "Encoding not initialised\n");
2221 /* Should wait for the header to be produced */
2222 s5p_mfc_wait_for_done_ctx(ctx
,
2223 S5P_MFC_R2H_CMD_SEQ_DONE_RET
, 0);
2224 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
2225 ctx
->state
< MFCINST_ABORT
) {
2226 ctrl
->val
= ctx
->pb_count
;
2228 v4l2_err(&dev
->v4l2_dev
, "Encoding not initialised\n");
2236 static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops
= {
2237 .s_ctrl
= s5p_mfc_enc_s_ctrl
,
2238 .g_volatile_ctrl
= s5p_mfc_enc_g_v_ctrl
,
2241 static int vidioc_s_parm(struct file
*file
, void *priv
,
2242 struct v4l2_streamparm
*a
)
2244 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
2246 if (a
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2247 ctx
->enc_params
.rc_framerate_num
=
2248 a
->parm
.output
.timeperframe
.denominator
;
2249 ctx
->enc_params
.rc_framerate_denom
=
2250 a
->parm
.output
.timeperframe
.numerator
;
2252 mfc_err("Setting FPS is only possible for the output queue\n");
2258 static int vidioc_g_parm(struct file
*file
, void *priv
,
2259 struct v4l2_streamparm
*a
)
2261 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
2263 if (a
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
2264 a
->parm
.output
.timeperframe
.denominator
=
2265 ctx
->enc_params
.rc_framerate_num
;
2266 a
->parm
.output
.timeperframe
.numerator
=
2267 ctx
->enc_params
.rc_framerate_denom
;
2269 mfc_err("Setting FPS is only possible for the output queue\n");
2275 static int vidioc_encoder_cmd(struct file
*file
, void *priv
,
2276 struct v4l2_encoder_cmd
*cmd
)
2278 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
2279 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2280 struct s5p_mfc_buf
*buf
;
2281 unsigned long flags
;
2284 case V4L2_ENC_CMD_STOP
:
2285 if (cmd
->flags
!= 0)
2288 if (!ctx
->vq_src
.streaming
)
2291 spin_lock_irqsave(&dev
->irqlock
, flags
);
2292 if (list_empty(&ctx
->src_queue
)) {
2293 mfc_debug(2, "EOS: empty src queue, entering finishing state\n");
2294 ctx
->state
= MFCINST_FINISHING
;
2295 if (s5p_mfc_ctx_ready(ctx
))
2296 set_work_bit_irqsave(ctx
);
2297 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2298 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
2300 mfc_debug(2, "EOS: marking last buffer of stream\n");
2301 buf
= list_entry(ctx
->src_queue
.prev
,
2302 struct s5p_mfc_buf
, list
);
2303 if (buf
->flags
& MFC_BUF_FLAG_USED
)
2304 ctx
->state
= MFCINST_FINISHING
;
2306 buf
->flags
|= MFC_BUF_FLAG_EOS
;
2307 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2317 static int vidioc_subscribe_event(struct v4l2_fh
*fh
,
2318 const struct v4l2_event_subscription
*sub
)
2320 switch (sub
->type
) {
2321 case V4L2_EVENT_EOS
:
2322 return v4l2_event_subscribe(fh
, sub
, 2, NULL
);
2328 static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops
= {
2329 .vidioc_querycap
= vidioc_querycap
,
2330 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2331 .vidioc_enum_fmt_vid_out
= vidioc_enum_fmt_vid_out
,
2332 .vidioc_g_fmt_vid_cap_mplane
= vidioc_g_fmt
,
2333 .vidioc_g_fmt_vid_out_mplane
= vidioc_g_fmt
,
2334 .vidioc_try_fmt_vid_cap_mplane
= vidioc_try_fmt
,
2335 .vidioc_try_fmt_vid_out_mplane
= vidioc_try_fmt
,
2336 .vidioc_s_fmt_vid_cap_mplane
= vidioc_s_fmt
,
2337 .vidioc_s_fmt_vid_out_mplane
= vidioc_s_fmt
,
2338 .vidioc_reqbufs
= vidioc_reqbufs
,
2339 .vidioc_querybuf
= vidioc_querybuf
,
2340 .vidioc_qbuf
= vidioc_qbuf
,
2341 .vidioc_dqbuf
= vidioc_dqbuf
,
2342 .vidioc_expbuf
= vidioc_expbuf
,
2343 .vidioc_streamon
= vidioc_streamon
,
2344 .vidioc_streamoff
= vidioc_streamoff
,
2345 .vidioc_s_parm
= vidioc_s_parm
,
2346 .vidioc_g_parm
= vidioc_g_parm
,
2347 .vidioc_encoder_cmd
= vidioc_encoder_cmd
,
2348 .vidioc_subscribe_event
= vidioc_subscribe_event
,
2349 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
2352 static int check_vb_with_fmt(struct s5p_mfc_fmt
*fmt
, struct vb2_buffer
*vb
)
2358 if (fmt
->num_planes
!= vb
->num_planes
) {
2359 mfc_err("invalid plane number for the format\n");
2362 for (i
= 0; i
< fmt
->num_planes
; i
++) {
2363 dma_addr_t dma
= vb2_dma_contig_plane_dma_addr(vb
, i
);
2365 mfc_err("failed to get plane cookie\n");
2368 mfc_debug(2, "index: %d, plane[%d] cookie: %pad\n",
2369 vb
->index
, i
, &dma
);
2374 static int s5p_mfc_queue_setup(struct vb2_queue
*vq
,
2375 unsigned int *buf_count
, unsigned int *plane_count
,
2376 unsigned int psize
[], struct device
*alloc_devs
[])
2378 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
2379 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2381 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2382 if (ctx
->state
!= MFCINST_GOT_INST
) {
2383 mfc_err("invalid state: %d\n", ctx
->state
);
2388 *plane_count
= ctx
->dst_fmt
->num_planes
;
2390 *plane_count
= MFC_ENC_CAP_PLANE_COUNT
;
2393 if (*buf_count
> MFC_MAX_BUFFERS
)
2394 *buf_count
= MFC_MAX_BUFFERS
;
2395 psize
[0] = ctx
->enc_dst_buf_size
;
2396 alloc_devs
[0] = ctx
->dev
->mem_dev
[BANK_L_CTX
];
2397 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2399 *plane_count
= ctx
->src_fmt
->num_planes
;
2401 *plane_count
= MFC_ENC_OUT_PLANE_COUNT
;
2405 if (*buf_count
> MFC_MAX_BUFFERS
)
2406 *buf_count
= MFC_MAX_BUFFERS
;
2408 psize
[0] = ctx
->luma_size
;
2409 psize
[1] = ctx
->chroma_size
;
2411 if (IS_MFCV6_PLUS(dev
)) {
2412 alloc_devs
[0] = ctx
->dev
->mem_dev
[BANK_L_CTX
];
2413 alloc_devs
[1] = ctx
->dev
->mem_dev
[BANK_L_CTX
];
2415 alloc_devs
[0] = ctx
->dev
->mem_dev
[BANK_R_CTX
];
2416 alloc_devs
[1] = ctx
->dev
->mem_dev
[BANK_R_CTX
];
2419 mfc_err("invalid queue type: %d\n", vq
->type
);
2425 static int s5p_mfc_buf_init(struct vb2_buffer
*vb
)
2427 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
2428 struct vb2_queue
*vq
= vb
->vb2_queue
;
2429 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
2433 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2434 ret
= check_vb_with_fmt(ctx
->dst_fmt
, vb
);
2438 ctx
->dst_bufs
[i
].b
= vbuf
;
2439 ctx
->dst_bufs
[i
].cookie
.stream
=
2440 vb2_dma_contig_plane_dma_addr(vb
, 0);
2441 ctx
->dst_bufs_cnt
++;
2442 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2443 ret
= check_vb_with_fmt(ctx
->src_fmt
, vb
);
2447 ctx
->src_bufs
[i
].b
= vbuf
;
2448 ctx
->src_bufs
[i
].cookie
.raw
.luma
=
2449 vb2_dma_contig_plane_dma_addr(vb
, 0);
2450 ctx
->src_bufs
[i
].cookie
.raw
.chroma
=
2451 vb2_dma_contig_plane_dma_addr(vb
, 1);
2452 ctx
->src_bufs_cnt
++;
2454 mfc_err("invalid queue type: %d\n", vq
->type
);
2460 static int s5p_mfc_buf_prepare(struct vb2_buffer
*vb
)
2462 struct vb2_queue
*vq
= vb
->vb2_queue
;
2463 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
2466 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2467 ret
= check_vb_with_fmt(ctx
->dst_fmt
, vb
);
2470 mfc_debug(2, "plane size: %ld, dst size: %zu\n",
2471 vb2_plane_size(vb
, 0), ctx
->enc_dst_buf_size
);
2472 if (vb2_plane_size(vb
, 0) < ctx
->enc_dst_buf_size
) {
2473 mfc_err("plane size is too small for capture\n");
2476 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2477 ret
= check_vb_with_fmt(ctx
->src_fmt
, vb
);
2480 mfc_debug(2, "plane size: %ld, luma size: %d\n",
2481 vb2_plane_size(vb
, 0), ctx
->luma_size
);
2482 mfc_debug(2, "plane size: %ld, chroma size: %d\n",
2483 vb2_plane_size(vb
, 1), ctx
->chroma_size
);
2484 if (vb2_plane_size(vb
, 0) < ctx
->luma_size
||
2485 vb2_plane_size(vb
, 1) < ctx
->chroma_size
) {
2486 mfc_err("plane size is too small for output\n");
2490 mfc_err("invalid queue type: %d\n", vq
->type
);
2496 static int s5p_mfc_start_streaming(struct vb2_queue
*q
, unsigned int count
)
2498 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
2499 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2501 if (IS_MFCV6_PLUS(dev
) &&
2502 (q
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)) {
2504 if ((ctx
->state
== MFCINST_GOT_INST
) &&
2505 (dev
->curr_ctx
== ctx
->num
) && dev
->hw_lock
) {
2506 s5p_mfc_wait_for_done_ctx(ctx
,
2507 S5P_MFC_R2H_CMD_SEQ_DONE_RET
,
2511 if (ctx
->src_bufs_cnt
< ctx
->pb_count
) {
2512 mfc_err("Need minimum %d OUTPUT buffers\n",
2518 /* If context is ready then dev = work->data;schedule it to run */
2519 if (s5p_mfc_ctx_ready(ctx
))
2520 set_work_bit_irqsave(ctx
);
2521 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
2526 static void s5p_mfc_stop_streaming(struct vb2_queue
*q
)
2528 unsigned long flags
;
2529 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
2530 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2532 if ((ctx
->state
== MFCINST_FINISHING
||
2533 ctx
->state
== MFCINST_RUNNING
) &&
2534 dev
->curr_ctx
== ctx
->num
&& dev
->hw_lock
) {
2535 ctx
->state
= MFCINST_ABORT
;
2536 s5p_mfc_wait_for_done_ctx(ctx
, S5P_MFC_R2H_CMD_FRAME_DONE_RET
,
2539 ctx
->state
= MFCINST_FINISHED
;
2540 spin_lock_irqsave(&dev
->irqlock
, flags
);
2541 if (q
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2542 s5p_mfc_cleanup_queue(&ctx
->dst_queue
, &ctx
->vq_dst
);
2543 INIT_LIST_HEAD(&ctx
->dst_queue
);
2544 ctx
->dst_queue_cnt
= 0;
2546 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2547 cleanup_ref_queue(ctx
);
2548 s5p_mfc_cleanup_queue(&ctx
->src_queue
, &ctx
->vq_src
);
2549 INIT_LIST_HEAD(&ctx
->src_queue
);
2550 ctx
->src_queue_cnt
= 0;
2552 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2555 static void s5p_mfc_buf_queue(struct vb2_buffer
*vb
)
2557 struct vb2_queue
*vq
= vb
->vb2_queue
;
2558 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
2559 struct s5p_mfc_dev
*dev
= ctx
->dev
;
2560 unsigned long flags
;
2561 struct s5p_mfc_buf
*mfc_buf
;
2563 if (ctx
->state
== MFCINST_ERROR
) {
2564 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
2565 cleanup_ref_queue(ctx
);
2568 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
2569 mfc_buf
= &ctx
->dst_bufs
[vb
->index
];
2570 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
2571 /* Mark destination as available for use by MFC */
2572 spin_lock_irqsave(&dev
->irqlock
, flags
);
2573 list_add_tail(&mfc_buf
->list
, &ctx
->dst_queue
);
2574 ctx
->dst_queue_cnt
++;
2575 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2576 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
2577 mfc_buf
= &ctx
->src_bufs
[vb
->index
];
2578 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
2579 spin_lock_irqsave(&dev
->irqlock
, flags
);
2580 list_add_tail(&mfc_buf
->list
, &ctx
->src_queue
);
2581 ctx
->src_queue_cnt
++;
2582 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
2584 mfc_err("unsupported buffer type (%d)\n", vq
->type
);
2586 if (s5p_mfc_ctx_ready(ctx
))
2587 set_work_bit_irqsave(ctx
);
2588 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
2591 static struct vb2_ops s5p_mfc_enc_qops
= {
2592 .queue_setup
= s5p_mfc_queue_setup
,
2593 .wait_prepare
= vb2_ops_wait_prepare
,
2594 .wait_finish
= vb2_ops_wait_finish
,
2595 .buf_init
= s5p_mfc_buf_init
,
2596 .buf_prepare
= s5p_mfc_buf_prepare
,
2597 .start_streaming
= s5p_mfc_start_streaming
,
2598 .stop_streaming
= s5p_mfc_stop_streaming
,
2599 .buf_queue
= s5p_mfc_buf_queue
,
2602 const struct s5p_mfc_codec_ops
*get_enc_codec_ops(void)
2604 return &encoder_codec_ops
;
2607 struct vb2_ops
*get_enc_queue_ops(void)
2609 return &s5p_mfc_enc_qops
;
2612 const struct v4l2_ioctl_ops
*get_enc_v4l2_ioctl_ops(void)
2614 return &s5p_mfc_enc_ioctl_ops
;
2617 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2WHICH(x) == V4L2_CTRL_CLASS_CODEC) \
2618 && V4L2_CTRL_DRIVER_PRIV(x))
2620 int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx
*ctx
)
2622 struct v4l2_ctrl_config cfg
;
2625 v4l2_ctrl_handler_init(&ctx
->ctrl_handler
, NUM_CTRLS
);
2626 if (ctx
->ctrl_handler
.error
) {
2627 mfc_err("v4l2_ctrl_handler_init failed\n");
2628 return ctx
->ctrl_handler
.error
;
2630 for (i
= 0; i
< NUM_CTRLS
; i
++) {
2631 if (IS_MFC51_PRIV(controls
[i
].id
)) {
2632 memset(&cfg
, 0, sizeof(struct v4l2_ctrl_config
));
2633 cfg
.ops
= &s5p_mfc_enc_ctrl_ops
;
2634 cfg
.id
= controls
[i
].id
;
2635 cfg
.min
= controls
[i
].minimum
;
2636 cfg
.max
= controls
[i
].maximum
;
2637 cfg
.def
= controls
[i
].default_value
;
2638 cfg
.name
= controls
[i
].name
;
2639 cfg
.type
= controls
[i
].type
;
2642 if (cfg
.type
== V4L2_CTRL_TYPE_MENU
) {
2644 cfg
.menu_skip_mask
= controls
[i
].menu_skip_mask
;
2645 cfg
.qmenu
= mfc51_get_menu(cfg
.id
);
2647 cfg
.step
= controls
[i
].step
;
2648 cfg
.menu_skip_mask
= 0;
2650 ctx
->ctrls
[i
] = v4l2_ctrl_new_custom(&ctx
->ctrl_handler
,
2653 if ((controls
[i
].type
== V4L2_CTRL_TYPE_MENU
) ||
2654 (controls
[i
].type
==
2655 V4L2_CTRL_TYPE_INTEGER_MENU
)) {
2656 ctx
->ctrls
[i
] = v4l2_ctrl_new_std_menu(
2658 &s5p_mfc_enc_ctrl_ops
, controls
[i
].id
,
2659 controls
[i
].maximum
, 0,
2660 controls
[i
].default_value
);
2662 ctx
->ctrls
[i
] = v4l2_ctrl_new_std(
2664 &s5p_mfc_enc_ctrl_ops
, controls
[i
].id
,
2665 controls
[i
].minimum
,
2666 controls
[i
].maximum
, controls
[i
].step
,
2667 controls
[i
].default_value
);
2670 if (ctx
->ctrl_handler
.error
) {
2671 mfc_err("Adding control (%d) failed\n", i
);
2672 return ctx
->ctrl_handler
.error
;
2674 if (controls
[i
].is_volatile
&& ctx
->ctrls
[i
])
2675 ctx
->ctrls
[i
]->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
2677 v4l2_ctrl_handler_setup(&ctx
->ctrl_handler
);
2681 void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx
*ctx
)
2685 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
2686 for (i
= 0; i
< NUM_CTRLS
; i
++)
2687 ctx
->ctrls
[i
] = NULL
;
2690 void s5p_mfc_enc_init(struct s5p_mfc_ctx
*ctx
)
2692 struct v4l2_format f
;
2693 f
.fmt
.pix_mp
.pixelformat
= DEF_SRC_FMT_ENC
;
2694 ctx
->src_fmt
= find_format(&f
, MFC_FMT_RAW
);
2695 f
.fmt
.pix_mp
.pixelformat
= DEF_DST_FMT_ENC
;
2696 ctx
->dst_fmt
= find_format(&f
, MFC_FMT_ENC
);