Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / platform / s5p-mfc / s5p_mfc_enc.c
blob0d5d465561becbf587f9aaf3ea84ffbb7fab8d66
1 /*
2 * linux/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c
4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * Jeongtae Park <jtp.park@samsung.com>
8 * Kamil Debski <k.debski@samsung.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/clk.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
22 #include <linux/videodev2.h>
23 #include <media/v4l2-event.h>
24 #include <linux/workqueue.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/videobuf2-v4l2.h>
27 #include "s5p_mfc_common.h"
28 #include "s5p_mfc_ctrl.h"
29 #include "s5p_mfc_debug.h"
30 #include "s5p_mfc_enc.h"
31 #include "s5p_mfc_intr.h"
32 #include "s5p_mfc_opr.h"
34 #define DEF_SRC_FMT_ENC V4L2_PIX_FMT_NV12M
35 #define DEF_DST_FMT_ENC V4L2_PIX_FMT_H264
37 static struct s5p_mfc_fmt formats[] = {
39 .name = "4:2:0 2 Planes 16x16 Tiles",
40 .fourcc = V4L2_PIX_FMT_NV12MT_16X16,
41 .codec_mode = S5P_MFC_CODEC_NONE,
42 .type = MFC_FMT_RAW,
43 .num_planes = 2,
44 .versions = MFC_V6_BIT | MFC_V7_BIT,
47 .name = "4:2:0 2 Planes 64x32 Tiles",
48 .fourcc = V4L2_PIX_FMT_NV12MT,
49 .codec_mode = S5P_MFC_CODEC_NONE,
50 .type = MFC_FMT_RAW,
51 .num_planes = 2,
52 .versions = MFC_V5_BIT,
55 .name = "4:2:0 2 Planes Y/CbCr",
56 .fourcc = V4L2_PIX_FMT_NV12M,
57 .codec_mode = S5P_MFC_CODEC_NONE,
58 .type = MFC_FMT_RAW,
59 .num_planes = 2,
60 .versions = MFC_V5_BIT | MFC_V6_BIT | MFC_V7_BIT |
61 MFC_V8_BIT,
64 .name = "4:2:0 2 Planes Y/CrCb",
65 .fourcc = V4L2_PIX_FMT_NV21M,
66 .codec_mode = S5P_MFC_CODEC_NONE,
67 .type = MFC_FMT_RAW,
68 .num_planes = 2,
69 .versions = MFC_V6_BIT | MFC_V7_BIT | MFC_V8_BIT,
72 .name = "H264 Encoded Stream",
73 .fourcc = V4L2_PIX_FMT_H264,
74 .codec_mode = S5P_MFC_CODEC_H264_ENC,
75 .type = MFC_FMT_ENC,
76 .num_planes = 1,
77 .versions = MFC_V5_BIT | MFC_V6_BIT | MFC_V7_BIT |
78 MFC_V8_BIT,
81 .name = "MPEG4 Encoded Stream",
82 .fourcc = V4L2_PIX_FMT_MPEG4,
83 .codec_mode = S5P_MFC_CODEC_MPEG4_ENC,
84 .type = MFC_FMT_ENC,
85 .num_planes = 1,
86 .versions = MFC_V5_BIT | MFC_V6_BIT | MFC_V7_BIT |
87 MFC_V8_BIT,
90 .name = "H263 Encoded Stream",
91 .fourcc = V4L2_PIX_FMT_H263,
92 .codec_mode = S5P_MFC_CODEC_H263_ENC,
93 .type = MFC_FMT_ENC,
94 .num_planes = 1,
95 .versions = MFC_V5_BIT | MFC_V6_BIT | MFC_V7_BIT |
96 MFC_V8_BIT,
99 .name = "VP8 Encoded Stream",
100 .fourcc = V4L2_PIX_FMT_VP8,
101 .codec_mode = S5P_MFC_CODEC_VP8_ENC,
102 .type = MFC_FMT_ENC,
103 .num_planes = 1,
104 .versions = MFC_V7_BIT | MFC_V8_BIT,
108 #define NUM_FORMATS ARRAY_SIZE(formats)
109 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
111 unsigned int i;
113 for (i = 0; i < NUM_FORMATS; i++) {
114 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
115 formats[i].type == t)
116 return &formats[i];
118 return NULL;
121 static struct mfc_control controls[] = {
123 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
124 .type = V4L2_CTRL_TYPE_INTEGER,
125 .minimum = 0,
126 .maximum = (1 << 16) - 1,
127 .step = 1,
128 .default_value = 12,
131 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
132 .type = V4L2_CTRL_TYPE_MENU,
133 .minimum = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
134 .maximum = V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES,
135 .default_value = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
136 .menu_skip_mask = 0,
139 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB,
140 .type = V4L2_CTRL_TYPE_INTEGER,
141 .minimum = 1,
142 .maximum = (1 << 16) - 1,
143 .step = 1,
144 .default_value = 1,
147 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES,
148 .type = V4L2_CTRL_TYPE_INTEGER,
149 .minimum = 1900,
150 .maximum = (1 << 30) - 1,
151 .step = 1,
152 .default_value = 1900,
155 .id = V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
156 .type = V4L2_CTRL_TYPE_INTEGER,
157 .minimum = 0,
158 .maximum = (1 << 16) - 1,
159 .step = 1,
160 .default_value = 0,
163 .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING,
164 .type = V4L2_CTRL_TYPE_BOOLEAN,
165 .name = "Padding Control Enable",
166 .minimum = 0,
167 .maximum = 1,
168 .step = 1,
169 .default_value = 0,
172 .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV,
173 .type = V4L2_CTRL_TYPE_INTEGER,
174 .name = "Padding Color YUV Value",
175 .minimum = 0,
176 .maximum = (1 << 25) - 1,
177 .step = 1,
178 .default_value = 0,
181 .id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
182 .type = V4L2_CTRL_TYPE_BOOLEAN,
183 .minimum = 0,
184 .maximum = 1,
185 .step = 1,
186 .default_value = 0,
189 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
190 .type = V4L2_CTRL_TYPE_INTEGER,
191 .minimum = 1,
192 .maximum = (1 << 30) - 1,
193 .step = 1,
194 .default_value = 1,
197 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
198 .type = V4L2_CTRL_TYPE_INTEGER,
199 .name = "Rate Control Reaction Coeff.",
200 .minimum = 1,
201 .maximum = (1 << 16) - 1,
202 .step = 1,
203 .default_value = 1,
206 .id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE,
207 .type = V4L2_CTRL_TYPE_MENU,
208 .name = "Force frame type",
209 .minimum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
210 .maximum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED,
211 .default_value = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
212 .menu_skip_mask = 0,
215 .id = V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
216 .type = V4L2_CTRL_TYPE_BUTTON,
217 .minimum = 0,
218 .maximum = 0,
219 .step = 0,
220 .default_value = 0,
223 .id = V4L2_CID_MPEG_VIDEO_VBV_SIZE,
224 .type = V4L2_CTRL_TYPE_INTEGER,
225 .minimum = 0,
226 .maximum = (1 << 16) - 1,
227 .step = 1,
228 .default_value = 0,
231 .id = V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE,
232 .type = V4L2_CTRL_TYPE_INTEGER,
233 .name = "Horizontal MV Search Range",
234 .minimum = 16,
235 .maximum = 128,
236 .step = 16,
237 .default_value = 32,
240 .id = V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE,
241 .type = V4L2_CTRL_TYPE_INTEGER,
242 .name = "Vertical MV Search Range",
243 .minimum = 16,
244 .maximum = 128,
245 .step = 16,
246 .default_value = 32,
249 .id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
250 .type = V4L2_CTRL_TYPE_INTEGER,
251 .minimum = 0,
252 .maximum = (1 << 16) - 1,
253 .step = 1,
254 .default_value = 0,
257 .id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
258 .type = V4L2_CTRL_TYPE_MENU,
259 .minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
260 .maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
261 .default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
262 .menu_skip_mask = 0,
265 .id = V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE,
266 .type = V4L2_CTRL_TYPE_MENU,
267 .name = "Frame Skip Enable",
268 .minimum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
269 .maximum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
270 .menu_skip_mask = 0,
271 .default_value = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
274 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
275 .type = V4L2_CTRL_TYPE_BOOLEAN,
276 .name = "Fixed Target Bit Enable",
277 .minimum = 0,
278 .maximum = 1,
279 .default_value = 0,
280 .step = 1,
281 .menu_skip_mask = 0,
284 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
285 .type = V4L2_CTRL_TYPE_INTEGER,
286 .minimum = 0,
287 .maximum = 2,
288 .step = 1,
289 .default_value = 0,
292 .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
293 .type = V4L2_CTRL_TYPE_MENU,
294 .minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
295 .maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
296 .default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
297 .menu_skip_mask = ~(
298 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
299 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
300 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
304 .id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
305 .type = V4L2_CTRL_TYPE_MENU,
306 .minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
307 .maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
308 .default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
311 .id = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
312 .type = V4L2_CTRL_TYPE_MENU,
313 .minimum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
314 .maximum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
315 .default_value = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
316 .menu_skip_mask = 0,
319 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
320 .type = V4L2_CTRL_TYPE_MENU,
321 .minimum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
322 .maximum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
323 .default_value = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
324 .menu_skip_mask = 0,
327 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA,
328 .type = V4L2_CTRL_TYPE_INTEGER,
329 .minimum = -6,
330 .maximum = 6,
331 .step = 1,
332 .default_value = 0,
335 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA,
336 .type = V4L2_CTRL_TYPE_INTEGER,
337 .minimum = -6,
338 .maximum = 6,
339 .step = 1,
340 .default_value = 0,
343 .id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
344 .type = V4L2_CTRL_TYPE_MENU,
345 .minimum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
346 .maximum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
347 .default_value = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
348 .menu_skip_mask = 0,
351 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P,
352 .type = V4L2_CTRL_TYPE_INTEGER,
353 .name = "The Number of Ref. Pic for P",
354 .minimum = 1,
355 .maximum = 2,
356 .step = 1,
357 .default_value = 1,
360 .id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
361 .type = V4L2_CTRL_TYPE_BOOLEAN,
362 .minimum = 0,
363 .maximum = 1,
364 .step = 1,
365 .default_value = 0,
368 .id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
369 .type = V4L2_CTRL_TYPE_BOOLEAN,
370 .minimum = 0,
371 .maximum = 1,
372 .step = 1,
373 .default_value = 0,
376 .id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
377 .type = V4L2_CTRL_TYPE_INTEGER,
378 .minimum = 0,
379 .maximum = 51,
380 .step = 1,
381 .default_value = 1,
384 .id = V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
385 .type = V4L2_CTRL_TYPE_INTEGER,
386 .minimum = 0,
387 .maximum = 51,
388 .step = 1,
389 .default_value = 1,
392 .id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
393 .type = V4L2_CTRL_TYPE_INTEGER,
394 .minimum = 0,
395 .maximum = 51,
396 .step = 1,
397 .default_value = 51,
400 .id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
401 .type = V4L2_CTRL_TYPE_INTEGER,
402 .minimum = 0,
403 .maximum = 51,
404 .step = 1,
405 .default_value = 1,
408 .id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
409 .type = V4L2_CTRL_TYPE_INTEGER,
410 .minimum = 0,
411 .maximum = 51,
412 .step = 1,
413 .default_value = 1,
416 .id = V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP,
417 .type = V4L2_CTRL_TYPE_INTEGER,
418 .name = "H263 I-Frame QP value",
419 .minimum = 1,
420 .maximum = 31,
421 .step = 1,
422 .default_value = 1,
425 .id = V4L2_CID_MPEG_VIDEO_H263_MIN_QP,
426 .type = V4L2_CTRL_TYPE_INTEGER,
427 .name = "H263 Minimum QP value",
428 .minimum = 1,
429 .maximum = 31,
430 .step = 1,
431 .default_value = 1,
434 .id = V4L2_CID_MPEG_VIDEO_H263_MAX_QP,
435 .type = V4L2_CTRL_TYPE_INTEGER,
436 .name = "H263 Maximum QP value",
437 .minimum = 1,
438 .maximum = 31,
439 .step = 1,
440 .default_value = 31,
443 .id = V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP,
444 .type = V4L2_CTRL_TYPE_INTEGER,
445 .name = "H263 P frame QP value",
446 .minimum = 1,
447 .maximum = 31,
448 .step = 1,
449 .default_value = 1,
452 .id = V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP,
453 .type = V4L2_CTRL_TYPE_INTEGER,
454 .name = "H263 B frame QP value",
455 .minimum = 1,
456 .maximum = 31,
457 .step = 1,
458 .default_value = 1,
461 .id = V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP,
462 .type = V4L2_CTRL_TYPE_INTEGER,
463 .name = "MPEG4 I-Frame QP value",
464 .minimum = 1,
465 .maximum = 31,
466 .step = 1,
467 .default_value = 1,
470 .id = V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP,
471 .type = V4L2_CTRL_TYPE_INTEGER,
472 .name = "MPEG4 Minimum QP value",
473 .minimum = 1,
474 .maximum = 31,
475 .step = 1,
476 .default_value = 1,
479 .id = V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP,
480 .type = V4L2_CTRL_TYPE_INTEGER,
481 .name = "MPEG4 Maximum QP value",
482 .minimum = 0,
483 .maximum = 51,
484 .step = 1,
485 .default_value = 51,
488 .id = V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP,
489 .type = V4L2_CTRL_TYPE_INTEGER,
490 .name = "MPEG4 P frame QP value",
491 .minimum = 1,
492 .maximum = 31,
493 .step = 1,
494 .default_value = 1,
497 .id = V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP,
498 .type = V4L2_CTRL_TYPE_INTEGER,
499 .name = "MPEG4 B frame QP value",
500 .minimum = 1,
501 .maximum = 31,
502 .step = 1,
503 .default_value = 1,
506 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK,
507 .type = V4L2_CTRL_TYPE_BOOLEAN,
508 .name = "H264 Dark Reg Adaptive RC",
509 .minimum = 0,
510 .maximum = 1,
511 .step = 1,
512 .default_value = 0,
515 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH,
516 .type = V4L2_CTRL_TYPE_BOOLEAN,
517 .name = "H264 Smooth Reg Adaptive RC",
518 .minimum = 0,
519 .maximum = 1,
520 .step = 1,
521 .default_value = 0,
524 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC,
525 .type = V4L2_CTRL_TYPE_BOOLEAN,
526 .name = "H264 Static Reg Adaptive RC",
527 .minimum = 0,
528 .maximum = 1,
529 .step = 1,
530 .default_value = 0,
533 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY,
534 .type = V4L2_CTRL_TYPE_BOOLEAN,
535 .name = "H264 Activity Reg Adaptive RC",
536 .minimum = 0,
537 .maximum = 1,
538 .step = 1,
539 .default_value = 0,
542 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
543 .type = V4L2_CTRL_TYPE_BOOLEAN,
544 .minimum = 0,
545 .maximum = 1,
546 .step = 1,
547 .default_value = 0,
550 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
551 .type = V4L2_CTRL_TYPE_MENU,
552 .minimum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
553 .maximum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
554 .default_value = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
555 .menu_skip_mask = 0,
558 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
559 .type = V4L2_CTRL_TYPE_INTEGER,
560 .minimum = 0,
561 .maximum = (1 << 16) - 1,
562 .step = 1,
563 .default_value = 0,
566 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
567 .type = V4L2_CTRL_TYPE_INTEGER,
568 .minimum = 0,
569 .maximum = (1 << 16) - 1,
570 .step = 1,
571 .default_value = 0,
574 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
575 .type = V4L2_CTRL_TYPE_BOOLEAN,
576 .minimum = 0,
577 .maximum = 1,
578 .step = 1,
579 .default_value = 1,
582 .id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
583 .type = V4L2_CTRL_TYPE_INTEGER,
584 .minimum = 0,
585 .maximum = (1 << 16) - 1,
586 .step = 1,
587 .default_value = 0,
590 .id = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
591 .type = V4L2_CTRL_TYPE_MENU,
592 .minimum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
593 .maximum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE,
594 .default_value = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
595 .menu_skip_mask = 0,
598 .id = V4L2_CID_MPEG_VIDEO_MPEG4_QPEL,
599 .type = V4L2_CTRL_TYPE_BOOLEAN,
600 .minimum = 0,
601 .maximum = 1,
602 .step = 1,
603 .default_value = 0,
606 .id = V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS,
607 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
608 .maximum = V4L2_CID_MPEG_VIDEO_VPX_8_PARTITIONS,
609 .default_value = V4L2_CID_MPEG_VIDEO_VPX_1_PARTITION,
610 .menu_skip_mask = 0,
613 .id = V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4,
614 .type = V4L2_CTRL_TYPE_BOOLEAN,
615 .minimum = 0,
616 .maximum = 1,
617 .step = 1,
618 .default_value = 0,
621 .id = V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES,
622 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
623 .maximum = V4L2_CID_MPEG_VIDEO_VPX_2_REF_FRAME,
624 .default_value = V4L2_CID_MPEG_VIDEO_VPX_1_REF_FRAME,
625 .menu_skip_mask = 0,
628 .id = V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL,
629 .type = V4L2_CTRL_TYPE_INTEGER,
630 .minimum = 0,
631 .maximum = 63,
632 .step = 1,
633 .default_value = 0,
636 .id = V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS,
637 .type = V4L2_CTRL_TYPE_INTEGER,
638 .minimum = 0,
639 .maximum = 7,
640 .step = 1,
641 .default_value = 0,
644 .id = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD,
645 .type = V4L2_CTRL_TYPE_INTEGER,
646 .minimum = 0,
647 .maximum = (1 << 16) - 1,
648 .step = 1,
649 .default_value = 0,
652 .id = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL,
653 .type = V4L2_CTRL_TYPE_MENU,
654 .minimum = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV,
655 .maximum = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_REF_PERIOD,
656 .default_value = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV,
657 .menu_skip_mask = 0,
660 .id = V4L2_CID_MPEG_VIDEO_VPX_MAX_QP,
661 .type = V4L2_CTRL_TYPE_INTEGER,
662 .minimum = 0,
663 .maximum = 127,
664 .step = 1,
665 .default_value = 127,
668 .id = V4L2_CID_MPEG_VIDEO_VPX_MIN_QP,
669 .type = V4L2_CTRL_TYPE_INTEGER,
670 .minimum = 0,
671 .maximum = 11,
672 .step = 1,
673 .default_value = 0,
676 .id = V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP,
677 .type = V4L2_CTRL_TYPE_INTEGER,
678 .minimum = 0,
679 .maximum = 127,
680 .step = 1,
681 .default_value = 10,
684 .id = V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP,
685 .type = V4L2_CTRL_TYPE_INTEGER,
686 .minimum = 0,
687 .maximum = 127,
688 .step = 1,
689 .default_value = 10,
692 .id = V4L2_CID_MPEG_VIDEO_VPX_PROFILE,
693 .type = V4L2_CTRL_TYPE_INTEGER,
694 .minimum = 0,
695 .maximum = 3,
696 .step = 1,
697 .default_value = 0,
700 .id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
701 .type = V4L2_CTRL_TYPE_INTEGER,
702 .name = "Minimum number of output bufs",
703 .minimum = 1,
704 .maximum = 32,
705 .step = 1,
706 .default_value = 1,
707 .is_volatile = 1,
711 #define NUM_CTRLS ARRAY_SIZE(controls)
712 static const char * const *mfc51_get_menu(u32 id)
714 static const char * const mfc51_video_frame_skip[] = {
715 "Disabled",
716 "Level Limit",
717 "VBV/CPB Limit",
718 NULL,
720 static const char * const mfc51_video_force_frame[] = {
721 "Disabled",
722 "I Frame",
723 "Not Coded",
724 NULL,
726 switch (id) {
727 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
728 return mfc51_video_frame_skip;
729 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
730 return mfc51_video_force_frame;
732 return NULL;
735 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
737 mfc_debug(2, "src=%d, dst=%d, state=%d\n",
738 ctx->src_queue_cnt, ctx->dst_queue_cnt, ctx->state);
739 /* context is ready to make header */
740 if (ctx->state == MFCINST_GOT_INST && ctx->dst_queue_cnt >= 1)
741 return 1;
742 /* context is ready to encode a frame */
743 if ((ctx->state == MFCINST_RUNNING ||
744 ctx->state == MFCINST_HEAD_PRODUCED) &&
745 ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
746 return 1;
747 /* context is ready to encode remaining frames */
748 if (ctx->state == MFCINST_FINISHING &&
749 ctx->dst_queue_cnt >= 1)
750 return 1;
751 mfc_debug(2, "ctx is not ready\n");
752 return 0;
755 static void cleanup_ref_queue(struct s5p_mfc_ctx *ctx)
757 struct s5p_mfc_buf *mb_entry;
759 /* move buffers in ref queue to src queue */
760 while (!list_empty(&ctx->ref_queue)) {
761 mb_entry = list_entry((&ctx->ref_queue)->next,
762 struct s5p_mfc_buf, list);
763 list_del(&mb_entry->list);
764 ctx->ref_queue_cnt--;
765 list_add_tail(&mb_entry->list, &ctx->src_queue);
766 ctx->src_queue_cnt++;
768 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
769 ctx->src_queue_cnt, ctx->ref_queue_cnt);
770 INIT_LIST_HEAD(&ctx->ref_queue);
771 ctx->ref_queue_cnt = 0;
774 static int enc_pre_seq_start(struct s5p_mfc_ctx *ctx)
776 struct s5p_mfc_dev *dev = ctx->dev;
777 struct s5p_mfc_buf *dst_mb;
778 unsigned long dst_addr;
779 unsigned int dst_size;
781 dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
782 dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
783 dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
784 s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
785 dst_size);
786 return 0;
789 static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
791 struct s5p_mfc_dev *dev = ctx->dev;
792 struct s5p_mfc_enc_params *p = &ctx->enc_params;
793 struct s5p_mfc_buf *dst_mb;
794 unsigned int enc_pb_count;
796 if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
797 if (!list_empty(&ctx->dst_queue)) {
798 dst_mb = list_entry(ctx->dst_queue.next,
799 struct s5p_mfc_buf, list);
800 list_del(&dst_mb->list);
801 ctx->dst_queue_cnt--;
802 vb2_set_plane_payload(&dst_mb->b->vb2_buf, 0,
803 s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size,
804 dev));
805 vb2_buffer_done(&dst_mb->b->vb2_buf,
806 VB2_BUF_STATE_DONE);
810 if (!IS_MFCV6_PLUS(dev)) {
811 ctx->state = MFCINST_RUNNING;
812 if (s5p_mfc_ctx_ready(ctx))
813 set_work_bit_irqsave(ctx);
814 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
815 } else {
816 enc_pb_count = s5p_mfc_hw_call(dev->mfc_ops,
817 get_enc_dpb_count, dev);
818 if (ctx->pb_count < enc_pb_count)
819 ctx->pb_count = enc_pb_count;
820 ctx->state = MFCINST_HEAD_PRODUCED;
823 return 0;
826 static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
828 struct s5p_mfc_dev *dev = ctx->dev;
829 struct s5p_mfc_buf *dst_mb;
830 struct s5p_mfc_buf *src_mb;
831 unsigned long src_y_addr, src_c_addr, dst_addr;
832 unsigned int dst_size;
834 src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
835 src_y_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 0);
836 src_c_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 1);
837 s5p_mfc_hw_call(dev->mfc_ops, set_enc_frame_buffer, ctx,
838 src_y_addr, src_c_addr);
840 dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
841 dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
842 dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
843 s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
844 dst_size);
846 return 0;
849 static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
851 struct s5p_mfc_dev *dev = ctx->dev;
852 struct s5p_mfc_buf *mb_entry;
853 unsigned long enc_y_addr, enc_c_addr;
854 unsigned long mb_y_addr, mb_c_addr;
855 int slice_type;
856 unsigned int strm_size;
858 slice_type = s5p_mfc_hw_call(dev->mfc_ops, get_enc_slice_type, dev);
859 strm_size = s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev);
860 mfc_debug(2, "Encoded slice type: %d\n", slice_type);
861 mfc_debug(2, "Encoded stream size: %d\n", strm_size);
862 mfc_debug(2, "Display order: %d\n",
863 mfc_read(dev, S5P_FIMV_ENC_SI_PIC_CNT));
864 if (slice_type >= 0) {
865 s5p_mfc_hw_call(dev->mfc_ops, get_enc_frame_buffer, ctx,
866 &enc_y_addr, &enc_c_addr);
867 list_for_each_entry(mb_entry, &ctx->src_queue, list) {
868 mb_y_addr = vb2_dma_contig_plane_dma_addr(
869 &mb_entry->b->vb2_buf, 0);
870 mb_c_addr = vb2_dma_contig_plane_dma_addr(
871 &mb_entry->b->vb2_buf, 1);
872 if ((enc_y_addr == mb_y_addr) &&
873 (enc_c_addr == mb_c_addr)) {
874 list_del(&mb_entry->list);
875 ctx->src_queue_cnt--;
876 vb2_buffer_done(&mb_entry->b->vb2_buf,
877 VB2_BUF_STATE_DONE);
878 break;
881 list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
882 mb_y_addr = vb2_dma_contig_plane_dma_addr(
883 &mb_entry->b->vb2_buf, 0);
884 mb_c_addr = vb2_dma_contig_plane_dma_addr(
885 &mb_entry->b->vb2_buf, 1);
886 if ((enc_y_addr == mb_y_addr) &&
887 (enc_c_addr == mb_c_addr)) {
888 list_del(&mb_entry->list);
889 ctx->ref_queue_cnt--;
890 vb2_buffer_done(&mb_entry->b->vb2_buf,
891 VB2_BUF_STATE_DONE);
892 break;
896 if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
897 mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
898 list);
899 if (mb_entry->flags & MFC_BUF_FLAG_USED) {
900 list_del(&mb_entry->list);
901 ctx->src_queue_cnt--;
902 list_add_tail(&mb_entry->list, &ctx->ref_queue);
903 ctx->ref_queue_cnt++;
906 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
907 ctx->src_queue_cnt, ctx->ref_queue_cnt);
908 if ((ctx->dst_queue_cnt > 0) && (strm_size > 0)) {
909 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
910 list);
911 list_del(&mb_entry->list);
912 ctx->dst_queue_cnt--;
913 switch (slice_type) {
914 case S5P_FIMV_ENC_SI_SLICE_TYPE_I:
915 mb_entry->b->flags |= V4L2_BUF_FLAG_KEYFRAME;
916 break;
917 case S5P_FIMV_ENC_SI_SLICE_TYPE_P:
918 mb_entry->b->flags |= V4L2_BUF_FLAG_PFRAME;
919 break;
920 case S5P_FIMV_ENC_SI_SLICE_TYPE_B:
921 mb_entry->b->flags |= V4L2_BUF_FLAG_BFRAME;
922 break;
924 vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, strm_size);
925 vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
927 if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0))
928 clear_work_bit(ctx);
930 return 0;
933 static const struct s5p_mfc_codec_ops encoder_codec_ops = {
934 .pre_seq_start = enc_pre_seq_start,
935 .post_seq_start = enc_post_seq_start,
936 .pre_frame_start = enc_pre_frame_start,
937 .post_frame_start = enc_post_frame_start,
940 /* Query capabilities of the device */
941 static int vidioc_querycap(struct file *file, void *priv,
942 struct v4l2_capability *cap)
944 struct s5p_mfc_dev *dev = video_drvdata(file);
946 strncpy(cap->driver, S5P_MFC_NAME, sizeof(cap->driver) - 1);
947 strncpy(cap->card, dev->vfd_enc->name, sizeof(cap->card) - 1);
948 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
949 dev_name(&dev->plat_dev->dev));
951 * This is only a mem-to-mem video device. The capture and output
952 * device capability flags are left only for backward compatibility
953 * and are scheduled for removal.
955 cap->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
956 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
957 return 0;
960 static int vidioc_enum_fmt(struct file *file, struct v4l2_fmtdesc *f,
961 bool out)
963 struct s5p_mfc_dev *dev = video_drvdata(file);
964 struct s5p_mfc_fmt *fmt;
965 int i, j = 0;
967 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
968 if (out && formats[i].type != MFC_FMT_RAW)
969 continue;
970 else if (!out && formats[i].type != MFC_FMT_ENC)
971 continue;
972 else if ((dev->variant->version_bit & formats[i].versions) == 0)
973 continue;
975 if (j == f->index) {
976 fmt = &formats[i];
977 strlcpy(f->description, fmt->name,
978 sizeof(f->description));
979 f->pixelformat = fmt->fourcc;
980 return 0;
982 ++j;
984 return -EINVAL;
987 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
988 struct v4l2_fmtdesc *f)
990 return vidioc_enum_fmt(file, f, false);
993 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
994 struct v4l2_fmtdesc *f)
996 return vidioc_enum_fmt(file, f, true);
999 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1001 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1002 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1004 mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
1005 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1006 /* This is run on output (encoder dest) */
1007 pix_fmt_mp->width = 0;
1008 pix_fmt_mp->height = 0;
1009 pix_fmt_mp->field = V4L2_FIELD_NONE;
1010 pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
1011 pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
1013 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
1014 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
1015 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1016 /* This is run on capture (encoder src) */
1017 pix_fmt_mp->width = ctx->img_width;
1018 pix_fmt_mp->height = ctx->img_height;
1020 pix_fmt_mp->field = V4L2_FIELD_NONE;
1021 pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
1022 pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
1024 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1025 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1026 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1027 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1028 } else {
1029 mfc_err("invalid buf type\n");
1030 return -EINVAL;
1032 return 0;
1035 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1037 struct s5p_mfc_dev *dev = video_drvdata(file);
1038 struct s5p_mfc_fmt *fmt;
1039 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1041 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1042 fmt = find_format(f, MFC_FMT_ENC);
1043 if (!fmt) {
1044 mfc_err("failed to try output format\n");
1045 return -EINVAL;
1047 if ((dev->variant->version_bit & fmt->versions) == 0) {
1048 mfc_err("Unsupported format by this MFC version.\n");
1049 return -EINVAL;
1052 pix_fmt_mp->plane_fmt[0].bytesperline =
1053 pix_fmt_mp->plane_fmt[0].sizeimage;
1054 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1055 fmt = find_format(f, MFC_FMT_RAW);
1056 if (!fmt) {
1057 mfc_err("failed to try output format\n");
1058 return -EINVAL;
1060 if ((dev->variant->version_bit & fmt->versions) == 0) {
1061 mfc_err("Unsupported format by this MFC version.\n");
1062 return -EINVAL;
1065 v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
1066 &pix_fmt_mp->height, 4, 1080, 1, 0);
1067 } else {
1068 mfc_err("invalid buf type\n");
1069 return -EINVAL;
1071 return 0;
1074 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
1076 struct s5p_mfc_dev *dev = video_drvdata(file);
1077 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1078 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1079 int ret = 0;
1081 ret = vidioc_try_fmt(file, priv, f);
1082 if (ret)
1083 return ret;
1084 if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
1085 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
1086 ret = -EBUSY;
1087 goto out;
1089 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1090 /* dst_fmt is validated by call to vidioc_try_fmt */
1091 ctx->dst_fmt = find_format(f, MFC_FMT_ENC);
1092 ctx->state = MFCINST_INIT;
1093 ctx->codec_mode = ctx->dst_fmt->codec_mode;
1094 ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
1095 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
1096 ctx->dst_bufs_cnt = 0;
1097 ctx->capture_state = QUEUE_FREE;
1098 ret = s5p_mfc_open_mfc_inst(dev, ctx);
1099 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1100 /* src_fmt is validated by call to vidioc_try_fmt */
1101 ctx->src_fmt = find_format(f, MFC_FMT_RAW);
1102 ctx->img_width = pix_fmt_mp->width;
1103 ctx->img_height = pix_fmt_mp->height;
1104 mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
1105 mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1106 pix_fmt_mp->width, pix_fmt_mp->height,
1107 ctx->img_width, ctx->img_height);
1109 s5p_mfc_hw_call(dev->mfc_ops, enc_calc_src_size, ctx);
1110 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1111 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1112 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1113 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1115 ctx->src_bufs_cnt = 0;
1116 ctx->output_state = QUEUE_FREE;
1117 } else {
1118 mfc_err("invalid buf type\n");
1119 ret = -EINVAL;
1121 out:
1122 mfc_debug_leave();
1123 return ret;
1126 static int vidioc_reqbufs(struct file *file, void *priv,
1127 struct v4l2_requestbuffers *reqbufs)
1129 struct s5p_mfc_dev *dev = video_drvdata(file);
1130 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1131 int ret = 0;
1133 /* if memory is not mmp or userptr return error */
1134 if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1135 (reqbufs->memory != V4L2_MEMORY_USERPTR))
1136 return -EINVAL;
1137 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1138 if (reqbufs->count == 0) {
1139 mfc_debug(2, "Freeing buffers\n");
1140 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1141 s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
1142 ctx);
1143 ctx->capture_state = QUEUE_FREE;
1144 return ret;
1146 if (ctx->capture_state != QUEUE_FREE) {
1147 mfc_err("invalid capture state: %d\n",
1148 ctx->capture_state);
1149 return -EINVAL;
1151 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1152 if (ret != 0) {
1153 mfc_err("error in vb2_reqbufs() for E(D)\n");
1154 return ret;
1156 ctx->capture_state = QUEUE_BUFS_REQUESTED;
1158 ret = s5p_mfc_hw_call(ctx->dev->mfc_ops,
1159 alloc_codec_buffers, ctx);
1160 if (ret) {
1161 mfc_err("Failed to allocate encoding buffers\n");
1162 reqbufs->count = 0;
1163 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1164 return -ENOMEM;
1166 } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1167 if (reqbufs->count == 0) {
1168 mfc_debug(2, "Freeing buffers\n");
1169 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1170 s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
1171 ctx);
1172 ctx->output_state = QUEUE_FREE;
1173 return ret;
1175 if (ctx->output_state != QUEUE_FREE) {
1176 mfc_err("invalid output state: %d\n",
1177 ctx->output_state);
1178 return -EINVAL;
1181 if (IS_MFCV6_PLUS(dev)) {
1182 /* Check for min encoder buffers */
1183 if (ctx->pb_count &&
1184 (reqbufs->count < ctx->pb_count)) {
1185 reqbufs->count = ctx->pb_count;
1186 mfc_debug(2, "Minimum %d output buffers needed\n",
1187 ctx->pb_count);
1188 } else {
1189 ctx->pb_count = reqbufs->count;
1193 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1194 if (ret != 0) {
1195 mfc_err("error in vb2_reqbufs() for E(S)\n");
1196 return ret;
1198 ctx->output_state = QUEUE_BUFS_REQUESTED;
1199 } else {
1200 mfc_err("invalid buf type\n");
1201 return -EINVAL;
1203 return ret;
1206 static int vidioc_querybuf(struct file *file, void *priv,
1207 struct v4l2_buffer *buf)
1209 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1210 int ret = 0;
1212 /* if memory is not mmp or userptr return error */
1213 if ((buf->memory != V4L2_MEMORY_MMAP) &&
1214 (buf->memory != V4L2_MEMORY_USERPTR))
1215 return -EINVAL;
1216 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1217 if (ctx->state != MFCINST_GOT_INST) {
1218 mfc_err("invalid context state: %d\n", ctx->state);
1219 return -EINVAL;
1221 ret = vb2_querybuf(&ctx->vq_dst, buf);
1222 if (ret != 0) {
1223 mfc_err("error in vb2_querybuf() for E(D)\n");
1224 return ret;
1226 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1227 } else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1228 ret = vb2_querybuf(&ctx->vq_src, buf);
1229 if (ret != 0) {
1230 mfc_err("error in vb2_querybuf() for E(S)\n");
1231 return ret;
1233 } else {
1234 mfc_err("invalid buf type\n");
1235 return -EINVAL;
1237 return ret;
1240 /* Queue a buffer */
1241 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1243 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1245 if (ctx->state == MFCINST_ERROR) {
1246 mfc_err("Call on QBUF after unrecoverable error\n");
1247 return -EIO;
1249 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1250 if (ctx->state == MFCINST_FINISHING) {
1251 mfc_err("Call on QBUF after EOS command\n");
1252 return -EIO;
1254 return vb2_qbuf(&ctx->vq_src, buf);
1255 } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1256 return vb2_qbuf(&ctx->vq_dst, buf);
1258 return -EINVAL;
1261 /* Dequeue a buffer */
1262 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1264 const struct v4l2_event ev = {
1265 .type = V4L2_EVENT_EOS
1267 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1268 int ret;
1270 if (ctx->state == MFCINST_ERROR) {
1271 mfc_err_limited("Call on DQBUF after unrecoverable error\n");
1272 return -EIO;
1274 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1275 ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1276 } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1277 ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1278 if (ret == 0 && ctx->state == MFCINST_FINISHED
1279 && list_empty(&ctx->vq_dst.done_list))
1280 v4l2_event_queue_fh(&ctx->fh, &ev);
1281 } else {
1282 ret = -EINVAL;
1285 return ret;
1288 /* Export DMA buffer */
1289 static int vidioc_expbuf(struct file *file, void *priv,
1290 struct v4l2_exportbuffer *eb)
1292 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1294 if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1295 return vb2_expbuf(&ctx->vq_src, eb);
1296 if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1297 return vb2_expbuf(&ctx->vq_dst, eb);
1298 return -EINVAL;
1301 /* Stream on */
1302 static int vidioc_streamon(struct file *file, void *priv,
1303 enum v4l2_buf_type type)
1305 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1307 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1308 return vb2_streamon(&ctx->vq_src, type);
1309 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1310 return vb2_streamon(&ctx->vq_dst, type);
1311 return -EINVAL;
1314 /* Stream off, which equals to a pause */
1315 static int vidioc_streamoff(struct file *file, void *priv,
1316 enum v4l2_buf_type type)
1318 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1320 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1321 return vb2_streamoff(&ctx->vq_src, type);
1322 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1323 return vb2_streamoff(&ctx->vq_dst, type);
1324 return -EINVAL;
1327 static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
1329 static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
1330 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0 */ 10,
1331 /* V4L2_MPEG_VIDEO_H264_LEVEL_1B */ 9,
1332 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1 */ 11,
1333 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2 */ 12,
1334 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3 */ 13,
1335 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0 */ 20,
1336 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1 */ 21,
1337 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2 */ 22,
1338 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0 */ 30,
1339 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1 */ 31,
1340 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2 */ 32,
1341 /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0 */ 40,
1343 return t[lvl];
1346 static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
1348 static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
1349 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 */ 0,
1350 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B */ 9,
1351 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 */ 1,
1352 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 */ 2,
1353 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 */ 3,
1354 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B */ 7,
1355 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 */ 4,
1356 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 */ 5,
1358 return t[lvl];
1361 static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
1363 static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
1364 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED */ 0,
1365 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1 */ 1,
1366 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11 */ 2,
1367 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11 */ 3,
1368 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11 */ 4,
1369 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33 */ 5,
1370 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11 */ 6,
1371 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11 */ 7,
1372 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11 */ 8,
1373 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33 */ 9,
1374 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11 */ 10,
1375 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11 */ 11,
1376 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33 */ 12,
1377 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99 */ 13,
1378 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3 */ 14,
1379 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2 */ 15,
1380 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1 */ 16,
1381 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED */ 255,
1383 return t[sar];
1386 static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
1388 struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1389 struct s5p_mfc_dev *dev = ctx->dev;
1390 struct s5p_mfc_enc_params *p = &ctx->enc_params;
1391 int ret = 0;
1393 switch (ctrl->id) {
1394 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1395 p->gop_size = ctrl->val;
1396 break;
1397 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1398 p->slice_mode = ctrl->val;
1399 break;
1400 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1401 p->slice_mb = ctrl->val;
1402 break;
1403 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1404 p->slice_bit = ctrl->val * 8;
1405 break;
1406 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1407 p->intra_refresh_mb = ctrl->val;
1408 break;
1409 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
1410 p->pad = ctrl->val;
1411 break;
1412 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
1413 p->pad_luma = (ctrl->val >> 16) & 0xff;
1414 p->pad_cb = (ctrl->val >> 8) & 0xff;
1415 p->pad_cr = (ctrl->val >> 0) & 0xff;
1416 break;
1417 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1418 p->rc_frame = ctrl->val;
1419 break;
1420 case V4L2_CID_MPEG_VIDEO_BITRATE:
1421 p->rc_bitrate = ctrl->val;
1422 break;
1423 case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
1424 p->rc_reaction_coeff = ctrl->val;
1425 break;
1426 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1427 ctx->force_frame_type = ctrl->val;
1428 break;
1429 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1430 ctx->force_frame_type =
1431 V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_I_FRAME;
1432 break;
1433 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1434 p->vbv_size = ctrl->val;
1435 break;
1436 case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
1437 p->mv_h_range = ctrl->val;
1438 break;
1439 case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
1440 p->mv_v_range = ctrl->val;
1441 break;
1442 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
1443 p->codec.h264.cpb_size = ctrl->val;
1444 break;
1445 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1446 p->seq_hdr_mode = ctrl->val;
1447 break;
1448 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1449 p->frame_skip_mode = ctrl->val;
1450 break;
1451 case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
1452 p->fixed_target_bit = ctrl->val;
1453 break;
1454 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1455 p->num_b_frame = ctrl->val;
1456 break;
1457 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1458 switch (ctrl->val) {
1459 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
1460 p->codec.h264.profile =
1461 S5P_FIMV_ENC_PROFILE_H264_MAIN;
1462 break;
1463 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
1464 p->codec.h264.profile =
1465 S5P_FIMV_ENC_PROFILE_H264_HIGH;
1466 break;
1467 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1468 p->codec.h264.profile =
1469 S5P_FIMV_ENC_PROFILE_H264_BASELINE;
1470 break;
1471 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
1472 if (IS_MFCV6_PLUS(dev))
1473 p->codec.h264.profile =
1474 S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE;
1475 else
1476 ret = -EINVAL;
1477 break;
1478 default:
1479 ret = -EINVAL;
1481 break;
1482 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1483 p->codec.h264.level_v4l2 = ctrl->val;
1484 p->codec.h264.level = h264_level(ctrl->val);
1485 if (p->codec.h264.level < 0) {
1486 mfc_err("Level number is wrong\n");
1487 ret = p->codec.h264.level;
1489 break;
1490 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1491 p->codec.mpeg4.level_v4l2 = ctrl->val;
1492 p->codec.mpeg4.level = mpeg4_level(ctrl->val);
1493 if (p->codec.mpeg4.level < 0) {
1494 mfc_err("Level number is wrong\n");
1495 ret = p->codec.mpeg4.level;
1497 break;
1498 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1499 p->codec.h264.loop_filter_mode = ctrl->val;
1500 break;
1501 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1502 p->codec.h264.loop_filter_alpha = ctrl->val;
1503 break;
1504 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1505 p->codec.h264.loop_filter_beta = ctrl->val;
1506 break;
1507 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1508 p->codec.h264.entropy_mode = ctrl->val;
1509 break;
1510 case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
1511 p->codec.h264.num_ref_pic_4p = ctrl->val;
1512 break;
1513 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1514 p->codec.h264._8x8_transform = ctrl->val;
1515 break;
1516 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1517 p->rc_mb = ctrl->val;
1518 break;
1519 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1520 p->codec.h264.rc_frame_qp = ctrl->val;
1521 break;
1522 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1523 p->codec.h264.rc_min_qp = ctrl->val;
1524 break;
1525 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1526 p->codec.h264.rc_max_qp = ctrl->val;
1527 break;
1528 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1529 p->codec.h264.rc_p_frame_qp = ctrl->val;
1530 break;
1531 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
1532 p->codec.h264.rc_b_frame_qp = ctrl->val;
1533 break;
1534 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1535 case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
1536 p->codec.mpeg4.rc_frame_qp = ctrl->val;
1537 break;
1538 case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
1539 case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
1540 p->codec.mpeg4.rc_min_qp = ctrl->val;
1541 break;
1542 case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
1543 case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
1544 p->codec.mpeg4.rc_max_qp = ctrl->val;
1545 break;
1546 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1547 case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
1548 p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
1549 break;
1550 case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
1551 case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
1552 p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
1553 break;
1554 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
1555 p->codec.h264.rc_mb_dark = ctrl->val;
1556 break;
1557 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
1558 p->codec.h264.rc_mb_smooth = ctrl->val;
1559 break;
1560 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
1561 p->codec.h264.rc_mb_static = ctrl->val;
1562 break;
1563 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
1564 p->codec.h264.rc_mb_activity = ctrl->val;
1565 break;
1566 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1567 p->codec.h264.vui_sar = ctrl->val;
1568 break;
1569 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1570 p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
1571 break;
1572 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
1573 p->codec.h264.vui_ext_sar_width = ctrl->val;
1574 break;
1575 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
1576 p->codec.h264.vui_ext_sar_height = ctrl->val;
1577 break;
1578 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1579 p->codec.h264.open_gop = !ctrl->val;
1580 break;
1581 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
1582 p->codec.h264.open_gop_size = ctrl->val;
1583 break;
1584 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1585 switch (ctrl->val) {
1586 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
1587 p->codec.mpeg4.profile =
1588 S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
1589 break;
1590 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
1591 p->codec.mpeg4.profile =
1592 S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
1593 break;
1594 default:
1595 ret = -EINVAL;
1597 break;
1598 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1599 p->codec.mpeg4.quarter_pixel = ctrl->val;
1600 break;
1601 case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
1602 p->codec.vp8.num_partitions = ctrl->val;
1603 break;
1604 case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4:
1605 p->codec.vp8.imd_4x4 = ctrl->val;
1606 break;
1607 case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
1608 p->codec.vp8.num_ref = ctrl->val;
1609 break;
1610 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL:
1611 p->codec.vp8.filter_level = ctrl->val;
1612 break;
1613 case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS:
1614 p->codec.vp8.filter_sharpness = ctrl->val;
1615 break;
1616 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD:
1617 p->codec.vp8.golden_frame_ref_period = ctrl->val;
1618 break;
1619 case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
1620 p->codec.vp8.golden_frame_sel = ctrl->val;
1621 break;
1622 case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
1623 p->codec.vp8.rc_min_qp = ctrl->val;
1624 break;
1625 case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
1626 p->codec.vp8.rc_max_qp = ctrl->val;
1627 break;
1628 case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP:
1629 p->codec.vp8.rc_frame_qp = ctrl->val;
1630 break;
1631 case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP:
1632 p->codec.vp8.rc_p_frame_qp = ctrl->val;
1633 break;
1634 case V4L2_CID_MPEG_VIDEO_VPX_PROFILE:
1635 p->codec.vp8.profile = ctrl->val;
1636 break;
1637 default:
1638 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
1639 ctrl->id, ctrl->val);
1640 ret = -EINVAL;
1642 return ret;
1645 static int s5p_mfc_enc_g_v_ctrl(struct v4l2_ctrl *ctrl)
1647 struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1648 struct s5p_mfc_dev *dev = ctx->dev;
1650 switch (ctrl->id) {
1651 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
1652 if (ctx->state >= MFCINST_HEAD_PARSED &&
1653 ctx->state < MFCINST_ABORT) {
1654 ctrl->val = ctx->pb_count;
1655 break;
1656 } else if (ctx->state != MFCINST_INIT) {
1657 v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
1658 return -EINVAL;
1660 /* Should wait for the header to be produced */
1661 s5p_mfc_wait_for_done_ctx(ctx,
1662 S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
1663 if (ctx->state >= MFCINST_HEAD_PARSED &&
1664 ctx->state < MFCINST_ABORT) {
1665 ctrl->val = ctx->pb_count;
1666 } else {
1667 v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
1668 return -EINVAL;
1670 break;
1672 return 0;
1675 static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
1676 .s_ctrl = s5p_mfc_enc_s_ctrl,
1677 .g_volatile_ctrl = s5p_mfc_enc_g_v_ctrl,
1680 static int vidioc_s_parm(struct file *file, void *priv,
1681 struct v4l2_streamparm *a)
1683 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1685 if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1686 ctx->enc_params.rc_framerate_num =
1687 a->parm.output.timeperframe.denominator;
1688 ctx->enc_params.rc_framerate_denom =
1689 a->parm.output.timeperframe.numerator;
1690 } else {
1691 mfc_err("Setting FPS is only possible for the output queue\n");
1692 return -EINVAL;
1694 return 0;
1697 static int vidioc_g_parm(struct file *file, void *priv,
1698 struct v4l2_streamparm *a)
1700 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1702 if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1703 a->parm.output.timeperframe.denominator =
1704 ctx->enc_params.rc_framerate_num;
1705 a->parm.output.timeperframe.numerator =
1706 ctx->enc_params.rc_framerate_denom;
1707 } else {
1708 mfc_err("Setting FPS is only possible for the output queue\n");
1709 return -EINVAL;
1711 return 0;
1714 static int vidioc_encoder_cmd(struct file *file, void *priv,
1715 struct v4l2_encoder_cmd *cmd)
1717 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1718 struct s5p_mfc_dev *dev = ctx->dev;
1719 struct s5p_mfc_buf *buf;
1720 unsigned long flags;
1722 switch (cmd->cmd) {
1723 case V4L2_ENC_CMD_STOP:
1724 if (cmd->flags != 0)
1725 return -EINVAL;
1727 if (!ctx->vq_src.streaming)
1728 return -EINVAL;
1730 spin_lock_irqsave(&dev->irqlock, flags);
1731 if (list_empty(&ctx->src_queue)) {
1732 mfc_debug(2, "EOS: empty src queue, entering finishing state\n");
1733 ctx->state = MFCINST_FINISHING;
1734 if (s5p_mfc_ctx_ready(ctx))
1735 set_work_bit_irqsave(ctx);
1736 spin_unlock_irqrestore(&dev->irqlock, flags);
1737 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1738 } else {
1739 mfc_debug(2, "EOS: marking last buffer of stream\n");
1740 buf = list_entry(ctx->src_queue.prev,
1741 struct s5p_mfc_buf, list);
1742 if (buf->flags & MFC_BUF_FLAG_USED)
1743 ctx->state = MFCINST_FINISHING;
1744 else
1745 buf->flags |= MFC_BUF_FLAG_EOS;
1746 spin_unlock_irqrestore(&dev->irqlock, flags);
1748 break;
1749 default:
1750 return -EINVAL;
1753 return 0;
1756 static int vidioc_subscribe_event(struct v4l2_fh *fh,
1757 const struct v4l2_event_subscription *sub)
1759 switch (sub->type) {
1760 case V4L2_EVENT_EOS:
1761 return v4l2_event_subscribe(fh, sub, 2, NULL);
1762 default:
1763 return -EINVAL;
1767 static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
1768 .vidioc_querycap = vidioc_querycap,
1769 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
1770 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
1771 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1772 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1773 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1774 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1775 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1776 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1777 .vidioc_reqbufs = vidioc_reqbufs,
1778 .vidioc_querybuf = vidioc_querybuf,
1779 .vidioc_qbuf = vidioc_qbuf,
1780 .vidioc_dqbuf = vidioc_dqbuf,
1781 .vidioc_expbuf = vidioc_expbuf,
1782 .vidioc_streamon = vidioc_streamon,
1783 .vidioc_streamoff = vidioc_streamoff,
1784 .vidioc_s_parm = vidioc_s_parm,
1785 .vidioc_g_parm = vidioc_g_parm,
1786 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1787 .vidioc_subscribe_event = vidioc_subscribe_event,
1788 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1791 static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
1793 int i;
1795 if (!fmt)
1796 return -EINVAL;
1797 if (fmt->num_planes != vb->num_planes) {
1798 mfc_err("invalid plane number for the format\n");
1799 return -EINVAL;
1801 for (i = 0; i < fmt->num_planes; i++) {
1802 dma_addr_t dma = vb2_dma_contig_plane_dma_addr(vb, i);
1803 if (!dma) {
1804 mfc_err("failed to get plane cookie\n");
1805 return -EINVAL;
1807 mfc_debug(2, "index: %d, plane[%d] cookie: %pad\n",
1808 vb->index, i, &dma);
1810 return 0;
1813 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
1814 unsigned int *buf_count, unsigned int *plane_count,
1815 unsigned int psize[], struct device *alloc_devs[])
1817 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1818 struct s5p_mfc_dev *dev = ctx->dev;
1820 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1821 if (ctx->state != MFCINST_GOT_INST) {
1822 mfc_err("invalid state: %d\n", ctx->state);
1823 return -EINVAL;
1826 if (ctx->dst_fmt)
1827 *plane_count = ctx->dst_fmt->num_planes;
1828 else
1829 *plane_count = MFC_ENC_CAP_PLANE_COUNT;
1830 if (*buf_count < 1)
1831 *buf_count = 1;
1832 if (*buf_count > MFC_MAX_BUFFERS)
1833 *buf_count = MFC_MAX_BUFFERS;
1834 psize[0] = ctx->enc_dst_buf_size;
1835 alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
1836 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1837 if (ctx->src_fmt)
1838 *plane_count = ctx->src_fmt->num_planes;
1839 else
1840 *plane_count = MFC_ENC_OUT_PLANE_COUNT;
1842 if (*buf_count < 1)
1843 *buf_count = 1;
1844 if (*buf_count > MFC_MAX_BUFFERS)
1845 *buf_count = MFC_MAX_BUFFERS;
1847 psize[0] = ctx->luma_size;
1848 psize[1] = ctx->chroma_size;
1850 if (IS_MFCV6_PLUS(dev)) {
1851 alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
1852 alloc_devs[1] = ctx->dev->mem_dev[BANK_L_CTX];
1853 } else {
1854 alloc_devs[0] = ctx->dev->mem_dev[BANK_R_CTX];
1855 alloc_devs[1] = ctx->dev->mem_dev[BANK_R_CTX];
1857 } else {
1858 mfc_err("invalid queue type: %d\n", vq->type);
1859 return -EINVAL;
1861 return 0;
1864 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
1866 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1867 struct vb2_queue *vq = vb->vb2_queue;
1868 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1869 unsigned int i;
1870 int ret;
1872 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1873 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1874 if (ret < 0)
1875 return ret;
1876 i = vb->index;
1877 ctx->dst_bufs[i].b = vbuf;
1878 ctx->dst_bufs[i].cookie.stream =
1879 vb2_dma_contig_plane_dma_addr(vb, 0);
1880 ctx->dst_bufs_cnt++;
1881 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1882 ret = check_vb_with_fmt(ctx->src_fmt, vb);
1883 if (ret < 0)
1884 return ret;
1885 i = vb->index;
1886 ctx->src_bufs[i].b = vbuf;
1887 ctx->src_bufs[i].cookie.raw.luma =
1888 vb2_dma_contig_plane_dma_addr(vb, 0);
1889 ctx->src_bufs[i].cookie.raw.chroma =
1890 vb2_dma_contig_plane_dma_addr(vb, 1);
1891 ctx->src_bufs_cnt++;
1892 } else {
1893 mfc_err("invalid queue type: %d\n", vq->type);
1894 return -EINVAL;
1896 return 0;
1899 static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
1901 struct vb2_queue *vq = vb->vb2_queue;
1902 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1903 int ret;
1905 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1906 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1907 if (ret < 0)
1908 return ret;
1909 mfc_debug(2, "plane size: %ld, dst size: %zu\n",
1910 vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
1911 if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
1912 mfc_err("plane size is too small for capture\n");
1913 return -EINVAL;
1915 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1916 ret = check_vb_with_fmt(ctx->src_fmt, vb);
1917 if (ret < 0)
1918 return ret;
1919 mfc_debug(2, "plane size: %ld, luma size: %d\n",
1920 vb2_plane_size(vb, 0), ctx->luma_size);
1921 mfc_debug(2, "plane size: %ld, chroma size: %d\n",
1922 vb2_plane_size(vb, 1), ctx->chroma_size);
1923 if (vb2_plane_size(vb, 0) < ctx->luma_size ||
1924 vb2_plane_size(vb, 1) < ctx->chroma_size) {
1925 mfc_err("plane size is too small for output\n");
1926 return -EINVAL;
1928 } else {
1929 mfc_err("invalid queue type: %d\n", vq->type);
1930 return -EINVAL;
1932 return 0;
1935 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
1937 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1938 struct s5p_mfc_dev *dev = ctx->dev;
1940 if (IS_MFCV6_PLUS(dev) &&
1941 (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
1943 if ((ctx->state == MFCINST_GOT_INST) &&
1944 (dev->curr_ctx == ctx->num) && dev->hw_lock) {
1945 s5p_mfc_wait_for_done_ctx(ctx,
1946 S5P_MFC_R2H_CMD_SEQ_DONE_RET,
1950 if (ctx->src_bufs_cnt < ctx->pb_count) {
1951 mfc_err("Need minimum %d OUTPUT buffers\n",
1952 ctx->pb_count);
1953 return -ENOBUFS;
1957 /* If context is ready then dev = work->data;schedule it to run */
1958 if (s5p_mfc_ctx_ready(ctx))
1959 set_work_bit_irqsave(ctx);
1960 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1962 return 0;
1965 static void s5p_mfc_stop_streaming(struct vb2_queue *q)
1967 unsigned long flags;
1968 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1969 struct s5p_mfc_dev *dev = ctx->dev;
1971 if ((ctx->state == MFCINST_FINISHING ||
1972 ctx->state == MFCINST_RUNNING) &&
1973 dev->curr_ctx == ctx->num && dev->hw_lock) {
1974 ctx->state = MFCINST_ABORT;
1975 s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET,
1978 ctx->state = MFCINST_FINISHED;
1979 spin_lock_irqsave(&dev->irqlock, flags);
1980 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1981 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
1982 INIT_LIST_HEAD(&ctx->dst_queue);
1983 ctx->dst_queue_cnt = 0;
1985 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1986 cleanup_ref_queue(ctx);
1987 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
1988 INIT_LIST_HEAD(&ctx->src_queue);
1989 ctx->src_queue_cnt = 0;
1991 spin_unlock_irqrestore(&dev->irqlock, flags);
1994 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1996 struct vb2_queue *vq = vb->vb2_queue;
1997 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1998 struct s5p_mfc_dev *dev = ctx->dev;
1999 unsigned long flags;
2000 struct s5p_mfc_buf *mfc_buf;
2002 if (ctx->state == MFCINST_ERROR) {
2003 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2004 cleanup_ref_queue(ctx);
2005 return;
2007 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2008 mfc_buf = &ctx->dst_bufs[vb->index];
2009 mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2010 /* Mark destination as available for use by MFC */
2011 spin_lock_irqsave(&dev->irqlock, flags);
2012 list_add_tail(&mfc_buf->list, &ctx->dst_queue);
2013 ctx->dst_queue_cnt++;
2014 spin_unlock_irqrestore(&dev->irqlock, flags);
2015 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2016 mfc_buf = &ctx->src_bufs[vb->index];
2017 mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2018 spin_lock_irqsave(&dev->irqlock, flags);
2019 list_add_tail(&mfc_buf->list, &ctx->src_queue);
2020 ctx->src_queue_cnt++;
2021 spin_unlock_irqrestore(&dev->irqlock, flags);
2022 } else {
2023 mfc_err("unsupported buffer type (%d)\n", vq->type);
2025 if (s5p_mfc_ctx_ready(ctx))
2026 set_work_bit_irqsave(ctx);
2027 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2030 static struct vb2_ops s5p_mfc_enc_qops = {
2031 .queue_setup = s5p_mfc_queue_setup,
2032 .wait_prepare = vb2_ops_wait_prepare,
2033 .wait_finish = vb2_ops_wait_finish,
2034 .buf_init = s5p_mfc_buf_init,
2035 .buf_prepare = s5p_mfc_buf_prepare,
2036 .start_streaming = s5p_mfc_start_streaming,
2037 .stop_streaming = s5p_mfc_stop_streaming,
2038 .buf_queue = s5p_mfc_buf_queue,
2041 const struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
2043 return &encoder_codec_ops;
2046 struct vb2_ops *get_enc_queue_ops(void)
2048 return &s5p_mfc_enc_qops;
2051 const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
2053 return &s5p_mfc_enc_ioctl_ops;
2056 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2WHICH(x) == V4L2_CTRL_CLASS_MPEG) \
2057 && V4L2_CTRL_DRIVER_PRIV(x))
2059 int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
2061 struct v4l2_ctrl_config cfg;
2062 int i;
2064 v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
2065 if (ctx->ctrl_handler.error) {
2066 mfc_err("v4l2_ctrl_handler_init failed\n");
2067 return ctx->ctrl_handler.error;
2069 for (i = 0; i < NUM_CTRLS; i++) {
2070 if (IS_MFC51_PRIV(controls[i].id)) {
2071 memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
2072 cfg.ops = &s5p_mfc_enc_ctrl_ops;
2073 cfg.id = controls[i].id;
2074 cfg.min = controls[i].minimum;
2075 cfg.max = controls[i].maximum;
2076 cfg.def = controls[i].default_value;
2077 cfg.name = controls[i].name;
2078 cfg.type = controls[i].type;
2079 cfg.flags = 0;
2081 if (cfg.type == V4L2_CTRL_TYPE_MENU) {
2082 cfg.step = 0;
2083 cfg.menu_skip_mask = controls[i].menu_skip_mask;
2084 cfg.qmenu = mfc51_get_menu(cfg.id);
2085 } else {
2086 cfg.step = controls[i].step;
2087 cfg.menu_skip_mask = 0;
2089 ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
2090 &cfg, NULL);
2091 } else {
2092 if ((controls[i].type == V4L2_CTRL_TYPE_MENU) ||
2093 (controls[i].type ==
2094 V4L2_CTRL_TYPE_INTEGER_MENU)) {
2095 ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
2096 &ctx->ctrl_handler,
2097 &s5p_mfc_enc_ctrl_ops, controls[i].id,
2098 controls[i].maximum, 0,
2099 controls[i].default_value);
2100 } else {
2101 ctx->ctrls[i] = v4l2_ctrl_new_std(
2102 &ctx->ctrl_handler,
2103 &s5p_mfc_enc_ctrl_ops, controls[i].id,
2104 controls[i].minimum,
2105 controls[i].maximum, controls[i].step,
2106 controls[i].default_value);
2109 if (ctx->ctrl_handler.error) {
2110 mfc_err("Adding control (%d) failed\n", i);
2111 return ctx->ctrl_handler.error;
2113 if (controls[i].is_volatile && ctx->ctrls[i])
2114 ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
2116 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
2117 return 0;
2120 void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
2122 int i;
2124 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2125 for (i = 0; i < NUM_CTRLS; i++)
2126 ctx->ctrls[i] = NULL;
2129 void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
2131 struct v4l2_format f;
2132 f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
2133 ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
2134 f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
2135 ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);