blob: 9c19aa8038dd809fbdff061727f04836ac4117be [file] [log] [blame]
Kamil Debskiaf935742011-06-21 10:51:26 -03001/*
2 * linux/drivers/media/video/s5p-mfc/s5p_mfc_enc.c
3 *
4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * Jeongtae Park <jtp.park@samsung.com>
8 * Kamil Debski <k.debski@samsung.com>
9 *
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.
14 */
15
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/version.h>
23#include <linux/videodev2.h>
24#include <linux/workqueue.h>
25#include <media/v4l2-ctrls.h>
26#include <media/videobuf2-core.h>
27#include "regs-mfc.h"
28#include "s5p_mfc_common.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"
33
34static struct s5p_mfc_fmt formats[] = {
35 {
36 .name = "4:2:0 2 Planes 64x32 Tiles",
37 .fourcc = V4L2_PIX_FMT_NV12MT,
38 .codec_mode = S5P_FIMV_CODEC_NONE,
39 .type = MFC_FMT_RAW,
40 .num_planes = 2,
41 },
42 {
43 .name = "4:2:0 2 Planes",
44 .fourcc = V4L2_PIX_FMT_NV12M,
45 .codec_mode = S5P_FIMV_CODEC_NONE,
46 .type = MFC_FMT_RAW,
47 .num_planes = 2,
48 },
49 {
50 .name = "H264 Encoded Stream",
51 .fourcc = V4L2_PIX_FMT_H264,
52 .codec_mode = S5P_FIMV_CODEC_H264_ENC,
53 .type = MFC_FMT_ENC,
54 .num_planes = 1,
55 },
56 {
57 .name = "MPEG4 Encoded Stream",
58 .fourcc = V4L2_PIX_FMT_MPEG4,
59 .codec_mode = S5P_FIMV_CODEC_MPEG4_ENC,
60 .type = MFC_FMT_ENC,
61 .num_planes = 1,
62 },
63 {
Peter Korsgaarda626f392011-12-05 06:12:15 -030064 .name = "H263 Encoded Stream",
Kamil Debskiaf935742011-06-21 10:51:26 -030065 .fourcc = V4L2_PIX_FMT_H263,
66 .codec_mode = S5P_FIMV_CODEC_H263_ENC,
67 .type = MFC_FMT_ENC,
68 .num_planes = 1,
69 },
70};
71
72#define NUM_FORMATS ARRAY_SIZE(formats)
73static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
74{
75 unsigned int i;
76
77 for (i = 0; i < NUM_FORMATS; i++) {
78 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
79 formats[i].type == t)
80 return &formats[i];
81 }
82 return NULL;
83}
84
85static struct mfc_control controls[] = {
86 {
87 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
88 .type = V4L2_CTRL_TYPE_INTEGER,
89 .minimum = 0,
90 .maximum = (1 << 16) - 1,
91 .step = 1,
92 .default_value = 0,
93 },
94 {
95 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
96 .type = V4L2_CTRL_TYPE_MENU,
97 .minimum = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
98 .maximum = V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES,
99 .default_value = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
100 .menu_skip_mask = 0,
101 },
102 {
103 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .minimum = 1,
106 .maximum = (1 << 16) - 1,
107 .step = 1,
108 .default_value = 1,
109 },
110 {
111 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES,
112 .type = V4L2_CTRL_TYPE_INTEGER,
113 .minimum = 1900,
114 .maximum = (1 << 30) - 1,
115 .step = 1,
116 .default_value = 1900,
117 },
118 {
119 .id = V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
120 .type = V4L2_CTRL_TYPE_INTEGER,
121 .minimum = 0,
122 .maximum = (1 << 16) - 1,
123 .step = 1,
124 .default_value = 0,
125 },
126 {
127 .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING,
128 .type = V4L2_CTRL_TYPE_BOOLEAN,
129 .name = "Padding Control Enable",
130 .minimum = 0,
131 .maximum = 1,
132 .step = 1,
133 .default_value = 0,
134 },
135 {
136 .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV,
137 .type = V4L2_CTRL_TYPE_INTEGER,
138 .name = "Padding Color YUV Value",
139 .minimum = 0,
140 .maximum = (1 << 25) - 1,
141 .step = 1,
142 .default_value = 0,
143 },
144 {
145 .id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
146 .type = V4L2_CTRL_TYPE_BOOLEAN,
147 .minimum = 0,
148 .maximum = 1,
149 .step = 1,
150 .default_value = 0,
151 },
152 {
153 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
154 .type = V4L2_CTRL_TYPE_INTEGER,
155 .minimum = 1,
156 .maximum = (1 << 30) - 1,
157 .step = 1,
158 .default_value = 1,
159 },
160 {
161 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
162 .type = V4L2_CTRL_TYPE_INTEGER,
163 .name = "Rate Control Reaction Coeff.",
164 .minimum = 1,
165 .maximum = (1 << 16) - 1,
166 .step = 1,
167 .default_value = 1,
168 },
169 {
170 .id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE,
171 .type = V4L2_CTRL_TYPE_MENU,
172 .name = "Force frame type",
173 .minimum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
174 .maximum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED,
175 .default_value = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
176 .menu_skip_mask = 0,
177 },
178 {
179 .id = V4L2_CID_MPEG_VIDEO_VBV_SIZE,
180 .type = V4L2_CTRL_TYPE_INTEGER,
181 .minimum = 0,
182 .maximum = (1 << 16) - 1,
183 .step = 1,
184 .default_value = 0,
185 },
186 {
187 .id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
188 .type = V4L2_CTRL_TYPE_INTEGER,
189 .minimum = 0,
190 .maximum = (1 << 16) - 1,
191 .step = 1,
192 .default_value = 0,
193 },
194 {
195 .id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
196 .type = V4L2_CTRL_TYPE_MENU,
197 .minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
198 .maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
199 .default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
200 .menu_skip_mask = 0,
201 },
202 {
203 .id = V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE,
204 .type = V4L2_CTRL_TYPE_MENU,
205 .name = "Frame Skip Enable",
206 .minimum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
207 .maximum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
208 .menu_skip_mask = 0,
209 .default_value = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
210 },
211 {
212 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
213 .type = V4L2_CTRL_TYPE_BOOLEAN,
214 .name = "Fixed Target Bit Enable",
215 .minimum = 0,
216 .maximum = 1,
217 .default_value = 0,
218 .menu_skip_mask = 0,
219 },
220 {
221 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
222 .type = V4L2_CTRL_TYPE_INTEGER,
223 .minimum = 0,
224 .maximum = 2,
225 .step = 1,
226 .default_value = 0,
227 },
228 {
229 .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
230 .type = V4L2_CTRL_TYPE_MENU,
231 .minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
232 .maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
233 .default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
234 .menu_skip_mask = ~(
235 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
236 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
237 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
238 ),
239 },
240 {
241 .id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
242 .type = V4L2_CTRL_TYPE_MENU,
243 .minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
244 .maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
245 .default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
Kamil Debskiaf935742011-06-21 10:51:26 -0300246 },
247 {
248 .id = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
249 .type = V4L2_CTRL_TYPE_MENU,
250 .minimum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
251 .maximum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
252 .default_value = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
253 .menu_skip_mask = 0,
254 },
255 {
256 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
257 .type = V4L2_CTRL_TYPE_MENU,
258 .minimum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
259 .maximum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
260 .default_value = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
261 .menu_skip_mask = 0,
262 },
263 {
264 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA,
265 .type = V4L2_CTRL_TYPE_INTEGER,
266 .minimum = -6,
267 .maximum = 6,
268 .step = 1,
269 .default_value = 0,
270 },
271 {
272 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA,
273 .type = V4L2_CTRL_TYPE_INTEGER,
274 .minimum = -6,
275 .maximum = 6,
276 .step = 1,
277 .default_value = 0,
278 },
279 {
280 .id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
281 .type = V4L2_CTRL_TYPE_MENU,
282 .minimum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
283 .maximum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
284 .default_value = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
285 .menu_skip_mask = 0,
286 },
287 {
288 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P,
289 .type = V4L2_CTRL_TYPE_INTEGER,
290 .name = "The Number of Ref. Pic for P",
291 .minimum = 1,
292 .maximum = 2,
293 .step = 1,
294 .default_value = 1,
295 },
296 {
297 .id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
298 .type = V4L2_CTRL_TYPE_BOOLEAN,
299 .minimum = 0,
300 .maximum = 1,
301 .step = 1,
302 .default_value = 0,
303 },
304 {
305 .id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
306 .type = V4L2_CTRL_TYPE_BOOLEAN,
307 .minimum = 0,
308 .maximum = 1,
309 .step = 1,
310 .default_value = 0,
311 },
312 {
313 .id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
314 .type = V4L2_CTRL_TYPE_INTEGER,
315 .minimum = 0,
316 .maximum = 51,
317 .step = 1,
318 .default_value = 1,
319 },
320 {
321 .id = V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
322 .type = V4L2_CTRL_TYPE_INTEGER,
323 .minimum = 0,
324 .maximum = 51,
325 .step = 1,
326 .default_value = 1,
327 },
328 {
329 .id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
330 .type = V4L2_CTRL_TYPE_INTEGER,
331 .minimum = 0,
332 .maximum = 51,
333 .step = 1,
334 .default_value = 1,
335 },
336 {
337 .id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
338 .type = V4L2_CTRL_TYPE_INTEGER,
339 .minimum = 0,
340 .maximum = 51,
341 .step = 1,
342 .default_value = 1,
343 },
344 {
345 .id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
346 .type = V4L2_CTRL_TYPE_INTEGER,
347 .minimum = 0,
348 .maximum = 51,
349 .step = 1,
350 .default_value = 1,
351 },
352 {
353 .id = V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP,
354 .type = V4L2_CTRL_TYPE_INTEGER,
355 .name = "H263 I-Frame QP value",
356 .minimum = 1,
357 .maximum = 31,
358 .step = 1,
359 .default_value = 1,
360 },
361 {
362 .id = V4L2_CID_MPEG_VIDEO_H263_MIN_QP,
363 .type = V4L2_CTRL_TYPE_INTEGER,
364 .name = "H263 Minimum QP value",
365 .minimum = 1,
366 .maximum = 31,
367 .step = 1,
368 .default_value = 1,
369 },
370 {
371 .id = V4L2_CID_MPEG_VIDEO_H263_MAX_QP,
372 .type = V4L2_CTRL_TYPE_INTEGER,
373 .name = "H263 Maximum QP value",
374 .minimum = 1,
375 .maximum = 31,
376 .step = 1,
377 .default_value = 1,
378 },
379 {
380 .id = V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP,
381 .type = V4L2_CTRL_TYPE_INTEGER,
382 .name = "H263 P frame QP value",
383 .minimum = 1,
384 .maximum = 31,
385 .step = 1,
386 .default_value = 1,
387 },
388 {
389 .id = V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP,
390 .type = V4L2_CTRL_TYPE_INTEGER,
391 .name = "H263 B frame QP value",
392 .minimum = 1,
393 .maximum = 31,
394 .step = 1,
395 .default_value = 1,
396 },
397 {
398 .id = V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP,
399 .type = V4L2_CTRL_TYPE_INTEGER,
400 .name = "MPEG4 I-Frame QP value",
401 .minimum = 1,
402 .maximum = 31,
403 .step = 1,
404 .default_value = 1,
405 },
406 {
407 .id = V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP,
408 .type = V4L2_CTRL_TYPE_INTEGER,
409 .name = "MPEG4 Minimum QP value",
410 .minimum = 1,
411 .maximum = 31,
412 .step = 1,
413 .default_value = 1,
414 },
415 {
416 .id = V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP,
417 .type = V4L2_CTRL_TYPE_INTEGER,
418 .name = "MPEG4 Maximum QP value",
419 .minimum = 0,
420 .maximum = 51,
421 .step = 1,
422 .default_value = 1,
423 },
424 {
425 .id = V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP,
426 .type = V4L2_CTRL_TYPE_INTEGER,
427 .name = "MPEG4 P frame QP value",
428 .minimum = 1,
429 .maximum = 31,
430 .step = 1,
431 .default_value = 1,
432 },
433 {
434 .id = V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP,
435 .type = V4L2_CTRL_TYPE_INTEGER,
436 .name = "MPEG4 B frame QP value",
437 .minimum = 1,
438 .maximum = 31,
439 .step = 1,
440 .default_value = 1,
441 },
442 {
443 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK,
444 .type = V4L2_CTRL_TYPE_BOOLEAN,
445 .name = "H264 Dark Reg Adaptive RC",
446 .minimum = 0,
447 .maximum = 1,
448 .step = 1,
449 .default_value = 0,
450 },
451 {
452 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH,
453 .type = V4L2_CTRL_TYPE_BOOLEAN,
454 .name = "H264 Smooth Reg Adaptive RC",
455 .minimum = 0,
456 .maximum = 1,
457 .step = 1,
458 .default_value = 0,
459 },
460 {
461 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC,
462 .type = V4L2_CTRL_TYPE_BOOLEAN,
463 .name = "H264 Static Reg Adaptive RC",
464 .minimum = 0,
465 .maximum = 1,
466 .step = 1,
467 .default_value = 0,
468 },
469 {
470 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY,
471 .type = V4L2_CTRL_TYPE_BOOLEAN,
472 .name = "H264 Activity Reg Adaptive RC",
473 .minimum = 0,
474 .maximum = 1,
475 .step = 1,
476 .default_value = 0,
477 },
478 {
479 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
480 .type = V4L2_CTRL_TYPE_BOOLEAN,
481 .minimum = 0,
482 .maximum = 1,
483 .step = 1,
484 .default_value = 0,
485 },
486 {
487 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
488 .type = V4L2_CTRL_TYPE_MENU,
489 .minimum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
490 .maximum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
Andrzej Hajdaafd14f42012-06-12 07:12:58 -0300491 .default_value = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
Kamil Debskiaf935742011-06-21 10:51:26 -0300492 .menu_skip_mask = 0,
493 },
494 {
495 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
496 .type = V4L2_CTRL_TYPE_INTEGER,
497 .minimum = 0,
498 .maximum = (1 << 16) - 1,
499 .step = 1,
500 .default_value = 0,
501 },
502 {
503 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
504 .type = V4L2_CTRL_TYPE_INTEGER,
505 .minimum = 0,
506 .maximum = (1 << 16) - 1,
507 .step = 1,
508 .default_value = 0,
509 },
510 {
511 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
512 .type = V4L2_CTRL_TYPE_BOOLEAN,
513 .minimum = 0,
514 .maximum = 1,
515 .step = 1,
516 .default_value = 1,
517 },
518 {
519 .id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
520 .type = V4L2_CTRL_TYPE_INTEGER,
521 .minimum = 0,
522 .maximum = (1 << 16) - 1,
523 .step = 1,
524 .default_value = 0,
525 },
526 {
527 .id = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
528 .type = V4L2_CTRL_TYPE_MENU,
529 .minimum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
530 .maximum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE,
Andrzej Hajdaafd14f42012-06-12 07:12:58 -0300531 .default_value = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
Kamil Debskiaf935742011-06-21 10:51:26 -0300532 .menu_skip_mask = 0,
533 },
534 {
535 .id = V4L2_CID_MPEG_VIDEO_MPEG4_QPEL,
536 .type = V4L2_CTRL_TYPE_BOOLEAN,
537 .minimum = 0,
538 .maximum = 1,
539 .step = 1,
540 .default_value = 0,
541 },
542};
543
544#define NUM_CTRLS ARRAY_SIZE(controls)
545static const char * const *mfc51_get_menu(u32 id)
546{
547 static const char * const mfc51_video_frame_skip[] = {
548 "Disabled",
549 "Level Limit",
550 "VBV/CPB Limit",
551 NULL,
552 };
553 static const char * const mfc51_video_force_frame[] = {
554 "Disabled",
555 "I Frame",
556 "Not Coded",
557 NULL,
558 };
559 switch (id) {
560 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
561 return mfc51_video_frame_skip;
562 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
563 return mfc51_video_force_frame;
564 }
565 return NULL;
566}
567
568static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
569{
570 mfc_debug(2, "src=%d, dst=%d, state=%d\n",
571 ctx->src_queue_cnt, ctx->dst_queue_cnt, ctx->state);
572 /* context is ready to make header */
573 if (ctx->state == MFCINST_GOT_INST && ctx->dst_queue_cnt >= 1)
574 return 1;
575 /* context is ready to encode a frame */
576 if (ctx->state == MFCINST_RUNNING &&
577 ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
578 return 1;
579 /* context is ready to encode remain frames */
580 if (ctx->state == MFCINST_FINISHING &&
581 ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
582 return 1;
583 mfc_debug(2, "ctx is not ready\n");
584 return 0;
585}
586
587static void cleanup_ref_queue(struct s5p_mfc_ctx *ctx)
588{
589 struct s5p_mfc_buf *mb_entry;
590 unsigned long mb_y_addr, mb_c_addr;
591
592 /* move buffers in ref queue to src queue */
593 while (!list_empty(&ctx->ref_queue)) {
594 mb_entry = list_entry((&ctx->ref_queue)->next,
595 struct s5p_mfc_buf, list);
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300596 mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
597 mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
Kamil Debskiaf935742011-06-21 10:51:26 -0300598 list_del(&mb_entry->list);
599 ctx->ref_queue_cnt--;
600 list_add_tail(&mb_entry->list, &ctx->src_queue);
601 ctx->src_queue_cnt++;
602 }
603 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
604 ctx->src_queue_cnt, ctx->ref_queue_cnt);
605 INIT_LIST_HEAD(&ctx->ref_queue);
606 ctx->ref_queue_cnt = 0;
607}
608
609static int enc_pre_seq_start(struct s5p_mfc_ctx *ctx)
610{
611 struct s5p_mfc_dev *dev = ctx->dev;
612 struct s5p_mfc_buf *dst_mb;
613 unsigned long dst_addr;
614 unsigned int dst_size;
615 unsigned long flags;
616
617 spin_lock_irqsave(&dev->irqlock, flags);
618 dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300619 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
Kamil Debskiaf935742011-06-21 10:51:26 -0300620 dst_size = vb2_plane_size(dst_mb->b, 0);
621 s5p_mfc_set_enc_stream_buffer(ctx, dst_addr, dst_size);
622 spin_unlock_irqrestore(&dev->irqlock, flags);
623 return 0;
624}
625
626static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
627{
628 struct s5p_mfc_dev *dev = ctx->dev;
629 struct s5p_mfc_enc_params *p = &ctx->enc_params;
630 struct s5p_mfc_buf *dst_mb;
631 unsigned long flags;
632
633 if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
634 spin_lock_irqsave(&dev->irqlock, flags);
635 dst_mb = list_entry(ctx->dst_queue.next,
636 struct s5p_mfc_buf, list);
637 list_del(&dst_mb->list);
638 ctx->dst_queue_cnt--;
639 vb2_set_plane_payload(dst_mb->b, 0,
640 s5p_mfc_get_enc_strm_size());
641 vb2_buffer_done(dst_mb->b, VB2_BUF_STATE_DONE);
642 spin_unlock_irqrestore(&dev->irqlock, flags);
643 }
644 ctx->state = MFCINST_RUNNING;
645 if (s5p_mfc_ctx_ready(ctx)) {
646 spin_lock_irqsave(&dev->condlock, flags);
647 set_bit(ctx->num, &dev->ctx_work_bits);
648 spin_unlock_irqrestore(&dev->condlock, flags);
649 }
650 s5p_mfc_try_run(dev);
651 return 0;
652}
653
654static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
655{
656 struct s5p_mfc_dev *dev = ctx->dev;
657 struct s5p_mfc_buf *dst_mb;
658 struct s5p_mfc_buf *src_mb;
659 unsigned long flags;
660 unsigned long src_y_addr, src_c_addr, dst_addr;
661 unsigned int dst_size;
662
663 spin_lock_irqsave(&dev->irqlock, flags);
664 src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300665 src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 0);
666 src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 1);
Kamil Debskiaf935742011-06-21 10:51:26 -0300667 s5p_mfc_set_enc_frame_buffer(ctx, src_y_addr, src_c_addr);
668 spin_unlock_irqrestore(&dev->irqlock, flags);
669
670 spin_lock_irqsave(&dev->irqlock, flags);
671 dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300672 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
Kamil Debskiaf935742011-06-21 10:51:26 -0300673 dst_size = vb2_plane_size(dst_mb->b, 0);
674 s5p_mfc_set_enc_stream_buffer(ctx, dst_addr, dst_size);
675 spin_unlock_irqrestore(&dev->irqlock, flags);
676
677 return 0;
678}
679
680static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
681{
682 struct s5p_mfc_dev *dev = ctx->dev;
683 struct s5p_mfc_buf *mb_entry;
684 unsigned long enc_y_addr, enc_c_addr;
685 unsigned long mb_y_addr, mb_c_addr;
686 int slice_type;
687 unsigned int strm_size;
688 unsigned long flags;
689
690 slice_type = s5p_mfc_get_enc_slice_type();
691 strm_size = s5p_mfc_get_enc_strm_size();
692 mfc_debug(2, "Encoded slice type: %d", slice_type);
693 mfc_debug(2, "Encoded stream size: %d", strm_size);
694 mfc_debug(2, "Display order: %d",
695 mfc_read(dev, S5P_FIMV_ENC_SI_PIC_CNT));
696 spin_lock_irqsave(&dev->irqlock, flags);
697 if (slice_type >= 0) {
698 s5p_mfc_get_enc_frame_buffer(ctx, &enc_y_addr, &enc_c_addr);
699 list_for_each_entry(mb_entry, &ctx->src_queue, list) {
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300700 mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
701 mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
Kamil Debskiaf935742011-06-21 10:51:26 -0300702 if ((enc_y_addr == mb_y_addr) &&
703 (enc_c_addr == mb_c_addr)) {
704 list_del(&mb_entry->list);
705 ctx->src_queue_cnt--;
706 vb2_buffer_done(mb_entry->b,
707 VB2_BUF_STATE_DONE);
708 break;
709 }
710 }
711 list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300712 mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
713 mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
Kamil Debskiaf935742011-06-21 10:51:26 -0300714 if ((enc_y_addr == mb_y_addr) &&
715 (enc_c_addr == mb_c_addr)) {
716 list_del(&mb_entry->list);
717 ctx->ref_queue_cnt--;
718 vb2_buffer_done(mb_entry->b,
719 VB2_BUF_STATE_DONE);
720 break;
721 }
722 }
723 }
724 if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
725 mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
726 list);
727 if (mb_entry->used) {
728 list_del(&mb_entry->list);
729 ctx->src_queue_cnt--;
730 list_add_tail(&mb_entry->list, &ctx->ref_queue);
731 ctx->ref_queue_cnt++;
732 }
733 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
734 ctx->src_queue_cnt, ctx->ref_queue_cnt);
735 }
736 if (strm_size > 0) {
737 /* at least one more dest. buffers exist always */
738 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
739 list);
740 list_del(&mb_entry->list);
741 ctx->dst_queue_cnt--;
742 switch (slice_type) {
743 case S5P_FIMV_ENC_SI_SLICE_TYPE_I:
744 mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
745 break;
746 case S5P_FIMV_ENC_SI_SLICE_TYPE_P:
747 mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
748 break;
749 case S5P_FIMV_ENC_SI_SLICE_TYPE_B:
750 mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_BFRAME;
751 break;
752 }
753 vb2_set_plane_payload(mb_entry->b, 0, strm_size);
754 vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
755 }
756 spin_unlock_irqrestore(&dev->irqlock, flags);
757 if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0)) {
758 spin_lock(&dev->condlock);
759 clear_bit(ctx->num, &dev->ctx_work_bits);
760 spin_unlock(&dev->condlock);
761 }
762 return 0;
763}
764
765static struct s5p_mfc_codec_ops encoder_codec_ops = {
766 .pre_seq_start = enc_pre_seq_start,
767 .post_seq_start = enc_post_seq_start,
768 .pre_frame_start = enc_pre_frame_start,
769 .post_frame_start = enc_post_frame_start,
770};
771
772/* Query capabilities of the device */
773static int vidioc_querycap(struct file *file, void *priv,
774 struct v4l2_capability *cap)
775{
776 struct s5p_mfc_dev *dev = video_drvdata(file);
777
778 strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
779 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
780 cap->bus_info[0] = 0;
781 cap->version = KERNEL_VERSION(1, 0, 0);
Kamil Debski43defb12011-10-06 11:34:05 -0300782 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE_MPLANE
783 | V4L2_CAP_VIDEO_OUTPUT_MPLANE
Kamil Debskiaf935742011-06-21 10:51:26 -0300784 | V4L2_CAP_STREAMING;
785 return 0;
786}
787
788static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
789{
790 struct s5p_mfc_fmt *fmt;
791 int i, j = 0;
792
793 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
794 if (mplane && formats[i].num_planes == 1)
795 continue;
796 else if (!mplane && formats[i].num_planes > 1)
797 continue;
798 if (out && formats[i].type != MFC_FMT_RAW)
799 continue;
800 else if (!out && formats[i].type != MFC_FMT_ENC)
801 continue;
802 if (j == f->index) {
803 fmt = &formats[i];
804 strlcpy(f->description, fmt->name,
805 sizeof(f->description));
806 f->pixelformat = fmt->fourcc;
807 return 0;
808 }
809 ++j;
810 }
811 return -EINVAL;
812}
813
814static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
815 struct v4l2_fmtdesc *f)
816{
817 return vidioc_enum_fmt(f, false, false);
818}
819
820static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
821 struct v4l2_fmtdesc *f)
822{
823 return vidioc_enum_fmt(f, true, false);
824}
825
826static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
827 struct v4l2_fmtdesc *f)
828{
829 return vidioc_enum_fmt(f, false, true);
830}
831
832static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
833 struct v4l2_fmtdesc *f)
834{
835 return vidioc_enum_fmt(f, true, true);
836}
837
838static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
839{
840 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
841 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
842
843 mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
844 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
845 /* This is run on output (encoder dest) */
846 pix_fmt_mp->width = 0;
847 pix_fmt_mp->height = 0;
848 pix_fmt_mp->field = V4L2_FIELD_NONE;
849 pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
850 pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
851
852 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
853 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
854 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
855 /* This is run on capture (encoder src) */
856 pix_fmt_mp->width = ctx->img_width;
857 pix_fmt_mp->height = ctx->img_height;
858
859 pix_fmt_mp->field = V4L2_FIELD_NONE;
860 pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
861 pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
862
863 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
864 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
865 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
866 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
867 } else {
868 mfc_err("invalid buf type\n");
869 return -EINVAL;
870 }
871 return 0;
872}
873
874static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
875{
876 struct s5p_mfc_fmt *fmt;
877 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
878
879 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
880 fmt = find_format(f, MFC_FMT_ENC);
881 if (!fmt) {
882 mfc_err("failed to try output format\n");
883 return -EINVAL;
884 }
885
886 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
887 mfc_err("must be set encoding output size\n");
888 return -EINVAL;
889 }
890
891 pix_fmt_mp->plane_fmt[0].bytesperline =
892 pix_fmt_mp->plane_fmt[0].sizeimage;
893 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
894 fmt = find_format(f, MFC_FMT_RAW);
895 if (!fmt) {
896 mfc_err("failed to try output format\n");
897 return -EINVAL;
898 }
899
900 if (fmt->num_planes != pix_fmt_mp->num_planes) {
901 mfc_err("failed to try output format\n");
902 return -EINVAL;
903 }
904 } else {
905 mfc_err("invalid buf type\n");
906 return -EINVAL;
907 }
908 return 0;
909}
910
911static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
912{
913 struct s5p_mfc_dev *dev = video_drvdata(file);
914 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
915 struct s5p_mfc_fmt *fmt;
916 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
917 unsigned long flags;
918 int ret = 0;
919
920 ret = vidioc_try_fmt(file, priv, f);
921 if (ret)
922 return ret;
923 if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
924 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
925 ret = -EBUSY;
926 goto out;
927 }
928 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
929 fmt = find_format(f, MFC_FMT_ENC);
930 if (!fmt) {
931 mfc_err("failed to set capture format\n");
932 return -EINVAL;
933 }
934 ctx->state = MFCINST_INIT;
935 ctx->dst_fmt = fmt;
936 ctx->codec_mode = ctx->dst_fmt->codec_mode;
937 ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
938 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
939 ctx->dst_bufs_cnt = 0;
940 ctx->capture_state = QUEUE_FREE;
941 s5p_mfc_alloc_instance_buffer(ctx);
942 spin_lock_irqsave(&dev->condlock, flags);
943 set_bit(ctx->num, &dev->ctx_work_bits);
944 spin_unlock_irqrestore(&dev->condlock, flags);
945 s5p_mfc_clean_ctx_int_flags(ctx);
946 s5p_mfc_try_run(dev);
947 if (s5p_mfc_wait_for_done_ctx(ctx, \
948 S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET, 1)) {
949 /* Error or timeout */
950 mfc_err("Error getting instance from hardware\n");
951 s5p_mfc_release_instance_buffer(ctx);
952 ret = -EIO;
953 goto out;
954 }
955 mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
956 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
957 fmt = find_format(f, MFC_FMT_RAW);
958 if (!fmt) {
959 mfc_err("failed to set output format\n");
960 return -EINVAL;
961 }
962 if (fmt->num_planes != pix_fmt_mp->num_planes) {
963 mfc_err("failed to set output format\n");
964 ret = -EINVAL;
965 goto out;
966 }
967 ctx->src_fmt = fmt;
968 ctx->img_width = pix_fmt_mp->width;
969 ctx->img_height = pix_fmt_mp->height;
970 mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
971 mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
972 pix_fmt_mp->width, pix_fmt_mp->height,
973 ctx->img_width, ctx->img_height);
974 if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M) {
975 ctx->buf_width = ALIGN(ctx->img_width,
976 S5P_FIMV_NV12M_HALIGN);
977 ctx->luma_size = ALIGN(ctx->img_width,
978 S5P_FIMV_NV12M_HALIGN) * ALIGN(ctx->img_height,
979 S5P_FIMV_NV12M_LVALIGN);
980 ctx->chroma_size = ALIGN(ctx->img_width,
981 S5P_FIMV_NV12M_HALIGN) * ALIGN((ctx->img_height
982 >> 1), S5P_FIMV_NV12M_CVALIGN);
983
984 ctx->luma_size = ALIGN(ctx->luma_size,
985 S5P_FIMV_NV12M_SALIGN);
986 ctx->chroma_size = ALIGN(ctx->chroma_size,
987 S5P_FIMV_NV12M_SALIGN);
988
989 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
990 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
991 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
992 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
993
994 } else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
995 ctx->buf_width = ALIGN(ctx->img_width,
996 S5P_FIMV_NV12MT_HALIGN);
997 ctx->luma_size = ALIGN(ctx->img_width,
998 S5P_FIMV_NV12MT_HALIGN) * ALIGN(ctx->img_height,
999 S5P_FIMV_NV12MT_VALIGN);
1000 ctx->chroma_size = ALIGN(ctx->img_width,
1001 S5P_FIMV_NV12MT_HALIGN) * ALIGN((ctx->img_height
1002 >> 1), S5P_FIMV_NV12MT_VALIGN);
1003 ctx->luma_size = ALIGN(ctx->luma_size,
1004 S5P_FIMV_NV12MT_SALIGN);
1005 ctx->chroma_size = ALIGN(ctx->chroma_size,
1006 S5P_FIMV_NV12MT_SALIGN);
1007
1008 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1009 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1010 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1011 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1012 }
1013 ctx->src_bufs_cnt = 0;
1014 ctx->output_state = QUEUE_FREE;
1015 } else {
1016 mfc_err("invalid buf type\n");
1017 return -EINVAL;
1018 }
1019out:
1020 mfc_debug_leave();
1021 return ret;
1022}
1023
1024static int vidioc_reqbufs(struct file *file, void *priv,
1025 struct v4l2_requestbuffers *reqbufs)
1026{
1027 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1028 int ret = 0;
1029
1030 /* if memory is not mmp or userptr return error */
1031 if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1032 (reqbufs->memory != V4L2_MEMORY_USERPTR))
1033 return -EINVAL;
1034 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1035 if (ctx->capture_state != QUEUE_FREE) {
1036 mfc_err("invalid capture state: %d\n",
1037 ctx->capture_state);
1038 return -EINVAL;
1039 }
1040 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1041 if (ret != 0) {
1042 mfc_err("error in vb2_reqbufs() for E(D)\n");
1043 return ret;
1044 }
1045 ctx->capture_state = QUEUE_BUFS_REQUESTED;
1046 ret = s5p_mfc_alloc_codec_buffers(ctx);
1047 if (ret) {
1048 mfc_err("Failed to allocate encoding buffers\n");
1049 reqbufs->count = 0;
1050 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1051 return -ENOMEM;
1052 }
1053 } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1054 if (ctx->output_state != QUEUE_FREE) {
1055 mfc_err("invalid output state: %d\n",
1056 ctx->output_state);
1057 return -EINVAL;
1058 }
1059 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1060 if (ret != 0) {
1061 mfc_err("error in vb2_reqbufs() for E(S)\n");
1062 return ret;
1063 }
1064 ctx->output_state = QUEUE_BUFS_REQUESTED;
1065 } else {
1066 mfc_err("invalid buf type\n");
1067 return -EINVAL;
1068 }
1069 return ret;
1070}
1071
1072static int vidioc_querybuf(struct file *file, void *priv,
1073 struct v4l2_buffer *buf)
1074{
1075 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1076 int ret = 0;
1077
1078 /* if memory is not mmp or userptr return error */
1079 if ((buf->memory != V4L2_MEMORY_MMAP) &&
1080 (buf->memory != V4L2_MEMORY_USERPTR))
1081 return -EINVAL;
1082 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1083 if (ctx->state != MFCINST_GOT_INST) {
1084 mfc_err("invalid context state: %d\n", ctx->state);
1085 return -EINVAL;
1086 }
1087 ret = vb2_querybuf(&ctx->vq_dst, buf);
1088 if (ret != 0) {
1089 mfc_err("error in vb2_querybuf() for E(D)\n");
1090 return ret;
1091 }
1092 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1093 } else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1094 ret = vb2_querybuf(&ctx->vq_src, buf);
1095 if (ret != 0) {
1096 mfc_err("error in vb2_querybuf() for E(S)\n");
1097 return ret;
1098 }
1099 } else {
1100 mfc_err("invalid buf type\n");
1101 return -EINVAL;
1102 }
1103 return ret;
1104}
1105
1106/* Queue a buffer */
1107static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1108{
1109 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1110
1111 if (ctx->state == MFCINST_ERROR) {
1112 mfc_err("Call on QBUF after unrecoverable error\n");
1113 return -EIO;
1114 }
1115 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1116 return vb2_qbuf(&ctx->vq_src, buf);
1117 else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1118 return vb2_qbuf(&ctx->vq_dst, buf);
1119 return -EINVAL;
1120}
1121
1122/* Dequeue a buffer */
1123static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1124{
1125 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1126
1127 if (ctx->state == MFCINST_ERROR) {
1128 mfc_err("Call on DQBUF after unrecoverable error\n");
1129 return -EIO;
1130 }
1131 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1132 return vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1133 else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1134 return vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1135 return -EINVAL;
1136}
1137
1138/* Stream on */
1139static int vidioc_streamon(struct file *file, void *priv,
1140 enum v4l2_buf_type type)
1141{
1142 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1143
1144 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1145 return vb2_streamon(&ctx->vq_src, type);
1146 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1147 return vb2_streamon(&ctx->vq_dst, type);
1148 return -EINVAL;
1149}
1150
1151/* Stream off, which equals to a pause */
1152static int vidioc_streamoff(struct file *file, void *priv,
1153 enum v4l2_buf_type type)
1154{
1155 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1156
1157 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1158 return vb2_streamoff(&ctx->vq_src, type);
1159 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1160 return vb2_streamoff(&ctx->vq_dst, type);
1161 return -EINVAL;
1162}
1163
1164static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
1165{
1166 static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
1167 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0 */ 10,
1168 /* V4L2_MPEG_VIDEO_H264_LEVEL_1B */ 9,
1169 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1 */ 11,
1170 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2 */ 12,
1171 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3 */ 13,
1172 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0 */ 20,
1173 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1 */ 21,
1174 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2 */ 22,
1175 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0 */ 30,
1176 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1 */ 31,
1177 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2 */ 32,
1178 /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0 */ 40,
1179 };
1180 return t[lvl];
1181}
1182
1183static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
1184{
1185 static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
1186 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 */ 0,
1187 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B */ 9,
1188 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 */ 1,
1189 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 */ 2,
1190 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 */ 3,
1191 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B */ 7,
1192 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 */ 4,
1193 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 */ 5,
1194 };
1195 return t[lvl];
1196}
1197
1198static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
1199{
1200 static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
1201 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED */ 0,
1202 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1 */ 1,
1203 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11 */ 2,
1204 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11 */ 3,
1205 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11 */ 4,
1206 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33 */ 5,
1207 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11 */ 6,
1208 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11 */ 7,
1209 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11 */ 8,
1210 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33 */ 9,
1211 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11 */ 10,
1212 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11 */ 11,
1213 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33 */ 12,
1214 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99 */ 13,
1215 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3 */ 14,
1216 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2 */ 15,
1217 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1 */ 16,
1218 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED */ 255,
1219 };
1220 return t[sar];
1221}
1222
1223static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
1224{
1225 struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1226 struct s5p_mfc_dev *dev = ctx->dev;
1227 struct s5p_mfc_enc_params *p = &ctx->enc_params;
1228 int ret = 0;
1229
1230 switch (ctrl->id) {
1231 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1232 p->gop_size = ctrl->val;
1233 break;
1234 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1235 p->slice_mode = ctrl->val;
1236 break;
1237 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1238 p->slice_mb = ctrl->val;
1239 break;
1240 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1241 p->slice_bit = ctrl->val * 8;
1242 break;
1243 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1244 p->intra_refresh_mb = ctrl->val;
1245 break;
1246 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
1247 p->pad = ctrl->val;
1248 break;
1249 case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
1250 p->pad_luma = (ctrl->val >> 16) & 0xff;
1251 p->pad_cb = (ctrl->val >> 8) & 0xff;
1252 p->pad_cr = (ctrl->val >> 0) & 0xff;
1253 break;
1254 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1255 p->rc_frame = ctrl->val;
1256 break;
1257 case V4L2_CID_MPEG_VIDEO_BITRATE:
1258 p->rc_bitrate = ctrl->val;
1259 break;
1260 case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
1261 p->rc_reaction_coeff = ctrl->val;
1262 break;
1263 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1264 ctx->force_frame_type = ctrl->val;
1265 break;
1266 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1267 p->vbv_size = ctrl->val;
1268 break;
1269 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
1270 p->codec.h264.cpb_size = ctrl->val;
1271 break;
1272 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1273 p->seq_hdr_mode = ctrl->val;
1274 break;
1275 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1276 p->frame_skip_mode = ctrl->val;
1277 break;
1278 case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
1279 p->fixed_target_bit = ctrl->val;
1280 break;
1281 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1282 p->num_b_frame = ctrl->val;
1283 break;
1284 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1285 switch (ctrl->val) {
1286 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
1287 p->codec.h264.profile =
1288 S5P_FIMV_ENC_PROFILE_H264_MAIN;
1289 break;
1290 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
1291 p->codec.h264.profile =
1292 S5P_FIMV_ENC_PROFILE_H264_HIGH;
1293 break;
1294 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1295 p->codec.h264.profile =
1296 S5P_FIMV_ENC_PROFILE_H264_BASELINE;
1297 break;
1298 default:
1299 ret = -EINVAL;
1300 }
1301 break;
1302 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1303 p->codec.h264.level_v4l2 = ctrl->val;
1304 p->codec.h264.level = h264_level(ctrl->val);
1305 if (p->codec.h264.level < 0) {
1306 mfc_err("Level number is wrong\n");
1307 ret = p->codec.h264.level;
1308 }
1309 break;
1310 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1311 p->codec.mpeg4.level_v4l2 = ctrl->val;
1312 p->codec.mpeg4.level = mpeg4_level(ctrl->val);
1313 if (p->codec.mpeg4.level < 0) {
1314 mfc_err("Level number is wrong\n");
1315 ret = p->codec.mpeg4.level;
1316 }
1317 break;
1318 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1319 p->codec.h264.loop_filter_mode = ctrl->val;
1320 break;
1321 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1322 p->codec.h264.loop_filter_alpha = ctrl->val;
1323 break;
1324 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1325 p->codec.h264.loop_filter_beta = ctrl->val;
1326 break;
1327 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1328 p->codec.h264.entropy_mode = ctrl->val;
1329 break;
1330 case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
1331 p->codec.h264.num_ref_pic_4p = ctrl->val;
1332 break;
1333 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1334 p->codec.h264._8x8_transform = ctrl->val;
1335 break;
1336 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1337 p->codec.h264.rc_mb = ctrl->val;
1338 break;
1339 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1340 p->codec.h264.rc_frame_qp = ctrl->val;
1341 break;
1342 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1343 p->codec.h264.rc_min_qp = ctrl->val;
1344 break;
1345 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1346 p->codec.h264.rc_max_qp = ctrl->val;
1347 break;
1348 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1349 p->codec.h264.rc_p_frame_qp = ctrl->val;
1350 break;
1351 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
1352 p->codec.h264.rc_b_frame_qp = ctrl->val;
1353 break;
1354 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1355 case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
1356 p->codec.mpeg4.rc_frame_qp = ctrl->val;
1357 break;
1358 case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
1359 case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
1360 p->codec.mpeg4.rc_min_qp = ctrl->val;
1361 break;
1362 case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
1363 case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
1364 p->codec.mpeg4.rc_max_qp = ctrl->val;
1365 break;
1366 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1367 case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
1368 p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
1369 break;
1370 case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
1371 case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
1372 p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
1373 break;
1374 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
1375 p->codec.h264.rc_mb_dark = ctrl->val;
1376 break;
1377 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
1378 p->codec.h264.rc_mb_smooth = ctrl->val;
1379 break;
1380 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
1381 p->codec.h264.rc_mb_static = ctrl->val;
1382 break;
1383 case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
1384 p->codec.h264.rc_mb_activity = ctrl->val;
1385 break;
1386 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1387 p->codec.h264.vui_sar = ctrl->val;
1388 break;
1389 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1390 p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
1391 break;
1392 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
1393 p->codec.h264.vui_ext_sar_width = ctrl->val;
1394 break;
1395 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
1396 p->codec.h264.vui_ext_sar_height = ctrl->val;
1397 break;
1398 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1399 p->codec.h264.open_gop = !ctrl->val;
1400 break;
1401 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
1402 p->codec.h264.open_gop_size = ctrl->val;
1403 break;
1404 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1405 switch (ctrl->val) {
1406 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
1407 p->codec.mpeg4.profile =
1408 S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
1409 break;
1410 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
1411 p->codec.mpeg4.profile =
1412 S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
1413 break;
1414 default:
1415 ret = -EINVAL;
1416 }
1417 break;
1418 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1419 p->codec.mpeg4.quarter_pixel = ctrl->val;
1420 break;
1421 default:
1422 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
1423 ctrl->id, ctrl->val);
1424 ret = -EINVAL;
1425 }
1426 return ret;
1427}
1428
1429static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
1430 .s_ctrl = s5p_mfc_enc_s_ctrl,
1431};
1432
Sachin Kamat3e9095d2012-05-11 04:38:14 -03001433static int vidioc_s_parm(struct file *file, void *priv,
1434 struct v4l2_streamparm *a)
Kamil Debskiaf935742011-06-21 10:51:26 -03001435{
1436 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1437
1438 if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1439 ctx->enc_params.rc_framerate_num =
1440 a->parm.output.timeperframe.denominator;
1441 ctx->enc_params.rc_framerate_denom =
1442 a->parm.output.timeperframe.numerator;
1443 } else {
1444 mfc_err("Setting FPS is only possible for the output queue\n");
1445 return -EINVAL;
1446 }
1447 return 0;
1448}
1449
Sachin Kamat3e9095d2012-05-11 04:38:14 -03001450static int vidioc_g_parm(struct file *file, void *priv,
1451 struct v4l2_streamparm *a)
Kamil Debskiaf935742011-06-21 10:51:26 -03001452{
1453 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1454
1455 if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1456 a->parm.output.timeperframe.denominator =
1457 ctx->enc_params.rc_framerate_num;
1458 a->parm.output.timeperframe.numerator =
1459 ctx->enc_params.rc_framerate_denom;
1460 } else {
1461 mfc_err("Setting FPS is only possible for the output queue\n");
1462 return -EINVAL;
1463 }
1464 return 0;
1465}
1466
1467static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
1468 .vidioc_querycap = vidioc_querycap,
1469 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1470 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
1471 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1472 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
1473 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1474 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1475 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1476 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1477 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1478 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1479 .vidioc_reqbufs = vidioc_reqbufs,
1480 .vidioc_querybuf = vidioc_querybuf,
1481 .vidioc_qbuf = vidioc_qbuf,
1482 .vidioc_dqbuf = vidioc_dqbuf,
1483 .vidioc_streamon = vidioc_streamon,
1484 .vidioc_streamoff = vidioc_streamoff,
1485 .vidioc_s_parm = vidioc_s_parm,
1486 .vidioc_g_parm = vidioc_g_parm,
1487};
1488
1489static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
1490{
1491 int i;
1492
1493 if (!fmt)
1494 return -EINVAL;
1495 if (fmt->num_planes != vb->num_planes) {
1496 mfc_err("invalid plane number for the format\n");
1497 return -EINVAL;
1498 }
1499 for (i = 0; i < fmt->num_planes; i++) {
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -03001500 if (!vb2_dma_contig_plane_dma_addr(vb, i)) {
Kamil Debskiaf935742011-06-21 10:51:26 -03001501 mfc_err("failed to get plane cookie\n");
1502 return -EINVAL;
1503 }
1504 mfc_debug(2, "index: %d, plane[%d] cookie: 0x%08zx",
1505 vb->v4l2_buf.index, i,
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -03001506 vb2_dma_contig_plane_dma_addr(vb, i));
Kamil Debskiaf935742011-06-21 10:51:26 -03001507 }
1508 return 0;
1509}
1510
1511static int s5p_mfc_queue_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -03001512 const struct v4l2_format *fmt,
1513 unsigned int *buf_count, unsigned int *plane_count,
1514 unsigned int psize[], void *allocators[])
Kamil Debskiaf935742011-06-21 10:51:26 -03001515{
1516 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1517
1518 if (ctx->state != MFCINST_GOT_INST) {
1519 mfc_err("inavlid state: %d\n", ctx->state);
1520 return -EINVAL;
1521 }
1522 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1523 if (ctx->dst_fmt)
1524 *plane_count = ctx->dst_fmt->num_planes;
1525 else
1526 *plane_count = MFC_ENC_CAP_PLANE_COUNT;
1527 if (*buf_count < 1)
1528 *buf_count = 1;
1529 if (*buf_count > MFC_MAX_BUFFERS)
1530 *buf_count = MFC_MAX_BUFFERS;
1531 psize[0] = ctx->enc_dst_buf_size;
1532 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1533 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1534 if (ctx->src_fmt)
1535 *plane_count = ctx->src_fmt->num_planes;
1536 else
1537 *plane_count = MFC_ENC_OUT_PLANE_COUNT;
1538
1539 if (*buf_count < 1)
1540 *buf_count = 1;
1541 if (*buf_count > MFC_MAX_BUFFERS)
1542 *buf_count = MFC_MAX_BUFFERS;
1543 psize[0] = ctx->luma_size;
1544 psize[1] = ctx->chroma_size;
1545 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1546 allocators[1] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1547 } else {
1548 mfc_err("inavlid queue type: %d\n", vq->type);
1549 return -EINVAL;
1550 }
1551 return 0;
1552}
1553
1554static void s5p_mfc_unlock(struct vb2_queue *q)
1555{
1556 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1557 struct s5p_mfc_dev *dev = ctx->dev;
1558
1559 mutex_unlock(&dev->mfc_mutex);
1560}
1561
1562static void s5p_mfc_lock(struct vb2_queue *q)
1563{
1564 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1565 struct s5p_mfc_dev *dev = ctx->dev;
1566
1567 mutex_lock(&dev->mfc_mutex);
1568}
1569
1570static int s5p_mfc_buf_init(struct vb2_buffer *vb)
1571{
1572 struct vb2_queue *vq = vb->vb2_queue;
1573 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1574 unsigned int i;
1575 int ret;
1576
1577 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1578 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1579 if (ret < 0)
1580 return ret;
1581 i = vb->v4l2_buf.index;
1582 ctx->dst_bufs[i].b = vb;
1583 ctx->dst_bufs[i].cookie.stream =
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -03001584 vb2_dma_contig_plane_dma_addr(vb, 0);
Kamil Debskiaf935742011-06-21 10:51:26 -03001585 ctx->dst_bufs_cnt++;
1586 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1587 ret = check_vb_with_fmt(ctx->src_fmt, vb);
1588 if (ret < 0)
1589 return ret;
1590 i = vb->v4l2_buf.index;
1591 ctx->src_bufs[i].b = vb;
1592 ctx->src_bufs[i].cookie.raw.luma =
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -03001593 vb2_dma_contig_plane_dma_addr(vb, 0);
Kamil Debskiaf935742011-06-21 10:51:26 -03001594 ctx->src_bufs[i].cookie.raw.chroma =
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -03001595 vb2_dma_contig_plane_dma_addr(vb, 1);
Kamil Debskiaf935742011-06-21 10:51:26 -03001596 ctx->src_bufs_cnt++;
1597 } else {
1598 mfc_err("inavlid queue type: %d\n", vq->type);
1599 return -EINVAL;
1600 }
1601 return 0;
1602}
1603
1604static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
1605{
1606 struct vb2_queue *vq = vb->vb2_queue;
1607 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1608 int ret;
1609
1610 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1611 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1612 if (ret < 0)
1613 return ret;
1614 mfc_debug(2, "plane size: %ld, dst size: %d\n",
1615 vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
1616 if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
1617 mfc_err("plane size is too small for capture\n");
1618 return -EINVAL;
1619 }
1620 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1621 ret = check_vb_with_fmt(ctx->src_fmt, vb);
1622 if (ret < 0)
1623 return ret;
1624 mfc_debug(2, "plane size: %ld, luma size: %d\n",
1625 vb2_plane_size(vb, 0), ctx->luma_size);
1626 mfc_debug(2, "plane size: %ld, chroma size: %d\n",
1627 vb2_plane_size(vb, 1), ctx->chroma_size);
1628 if (vb2_plane_size(vb, 0) < ctx->luma_size ||
1629 vb2_plane_size(vb, 1) < ctx->chroma_size) {
1630 mfc_err("plane size is too small for output\n");
1631 return -EINVAL;
1632 }
1633 } else {
1634 mfc_err("inavlid queue type: %d\n", vq->type);
1635 return -EINVAL;
1636 }
1637 return 0;
1638}
1639
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001640static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
Kamil Debskiaf935742011-06-21 10:51:26 -03001641{
1642 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1643 struct s5p_mfc_dev *dev = ctx->dev;
1644 unsigned long flags;
1645
1646 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1647 /* If context is ready then dev = work->data;schedule it to run */
1648 if (s5p_mfc_ctx_ready(ctx)) {
1649 spin_lock_irqsave(&dev->condlock, flags);
1650 set_bit(ctx->num, &dev->ctx_work_bits);
1651 spin_unlock_irqrestore(&dev->condlock, flags);
1652 }
1653 s5p_mfc_try_run(dev);
1654 return 0;
1655}
1656
1657static int s5p_mfc_stop_streaming(struct vb2_queue *q)
1658{
1659 unsigned long flags;
1660 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1661 struct s5p_mfc_dev *dev = ctx->dev;
1662
1663 if ((ctx->state == MFCINST_FINISHING ||
1664 ctx->state == MFCINST_RUNNING) &&
1665 dev->curr_ctx == ctx->num && dev->hw_lock) {
1666 ctx->state = MFCINST_ABORT;
1667 s5p_mfc_wait_for_done_ctx(ctx, S5P_FIMV_R2H_CMD_FRAME_DONE_RET,
1668 0);
1669 }
1670 ctx->state = MFCINST_FINISHED;
1671 spin_lock_irqsave(&dev->irqlock, flags);
1672 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1673 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
1674 INIT_LIST_HEAD(&ctx->dst_queue);
1675 ctx->dst_queue_cnt = 0;
1676 }
1677 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1678 cleanup_ref_queue(ctx);
1679 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
1680 INIT_LIST_HEAD(&ctx->src_queue);
1681 ctx->src_queue_cnt = 0;
1682 }
1683 spin_unlock_irqrestore(&dev->irqlock, flags);
1684 return 0;
1685}
1686
1687static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1688{
1689 struct vb2_queue *vq = vb->vb2_queue;
1690 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1691 struct s5p_mfc_dev *dev = ctx->dev;
1692 unsigned long flags;
1693 struct s5p_mfc_buf *mfc_buf;
1694
1695 if (ctx->state == MFCINST_ERROR) {
1696 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
1697 cleanup_ref_queue(ctx);
1698 return;
1699 }
1700 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1701 mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1702 mfc_buf->used = 0;
1703 /* Mark destination as available for use by MFC */
1704 spin_lock_irqsave(&dev->irqlock, flags);
1705 list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1706 ctx->dst_queue_cnt++;
1707 spin_unlock_irqrestore(&dev->irqlock, flags);
1708 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1709 mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1710 mfc_buf->used = 0;
1711 spin_lock_irqsave(&dev->irqlock, flags);
1712 if (vb->v4l2_planes[0].bytesused == 0) {
1713 mfc_debug(1, "change state to FINISHING\n");
1714 ctx->state = MFCINST_FINISHING;
1715 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1716 cleanup_ref_queue(ctx);
1717 } else {
1718 list_add_tail(&mfc_buf->list, &ctx->src_queue);
1719 ctx->src_queue_cnt++;
1720 }
1721 spin_unlock_irqrestore(&dev->irqlock, flags);
1722 } else {
1723 mfc_err("unsupported buffer type (%d)\n", vq->type);
1724 }
1725 if (s5p_mfc_ctx_ready(ctx)) {
1726 spin_lock_irqsave(&dev->condlock, flags);
1727 set_bit(ctx->num, &dev->ctx_work_bits);
1728 spin_unlock_irqrestore(&dev->condlock, flags);
1729 }
1730 s5p_mfc_try_run(dev);
1731}
1732
1733static struct vb2_ops s5p_mfc_enc_qops = {
1734 .queue_setup = s5p_mfc_queue_setup,
1735 .wait_prepare = s5p_mfc_unlock,
1736 .wait_finish = s5p_mfc_lock,
1737 .buf_init = s5p_mfc_buf_init,
1738 .buf_prepare = s5p_mfc_buf_prepare,
1739 .start_streaming = s5p_mfc_start_streaming,
1740 .stop_streaming = s5p_mfc_stop_streaming,
1741 .buf_queue = s5p_mfc_buf_queue,
1742};
1743
1744struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
1745{
1746 return &encoder_codec_ops;
1747}
1748
1749struct vb2_ops *get_enc_queue_ops(void)
1750{
1751 return &s5p_mfc_enc_qops;
1752}
1753
1754const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
1755{
1756 return &s5p_mfc_enc_ioctl_ops;
1757}
1758
1759#define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1760 && V4L2_CTRL_DRIVER_PRIV(x))
1761
1762int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
1763{
1764 struct v4l2_ctrl_config cfg;
1765 int i;
1766
1767 v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1768 if (ctx->ctrl_handler.error) {
1769 mfc_err("v4l2_ctrl_handler_init failed\n");
1770 return ctx->ctrl_handler.error;
1771 }
1772 for (i = 0; i < NUM_CTRLS; i++) {
1773 if (IS_MFC51_PRIV(controls[i].id)) {
1774 cfg.ops = &s5p_mfc_enc_ctrl_ops;
1775 cfg.id = controls[i].id;
1776 cfg.min = controls[i].minimum;
1777 cfg.max = controls[i].maximum;
1778 cfg.def = controls[i].default_value;
1779 cfg.name = controls[i].name;
1780 cfg.type = controls[i].type;
1781 cfg.flags = 0;
1782
1783 if (cfg.type == V4L2_CTRL_TYPE_MENU) {
1784 cfg.step = 0;
1785 cfg.menu_skip_mask = cfg.menu_skip_mask;
1786 cfg.qmenu = mfc51_get_menu(cfg.id);
1787 } else {
1788 cfg.step = controls[i].step;
1789 cfg.menu_skip_mask = 0;
1790 }
1791 ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1792 &cfg, NULL);
1793 } else {
1794 if (controls[i].type == V4L2_CTRL_TYPE_MENU) {
1795 ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
1796 &ctx->ctrl_handler,
1797 &s5p_mfc_enc_ctrl_ops, controls[i].id,
1798 controls[i].maximum, 0,
1799 controls[i].default_value);
1800 } else {
1801 ctx->ctrls[i] = v4l2_ctrl_new_std(
1802 &ctx->ctrl_handler,
1803 &s5p_mfc_enc_ctrl_ops, controls[i].id,
1804 controls[i].minimum,
1805 controls[i].maximum, controls[i].step,
1806 controls[i].default_value);
1807 }
1808 }
1809 if (ctx->ctrl_handler.error) {
1810 mfc_err("Adding control (%d) failed\n", i);
1811 return ctx->ctrl_handler.error;
1812 }
1813 if (controls[i].is_volatile && ctx->ctrls[i])
Hans Verkuil88365102011-08-26 07:35:14 -03001814 ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
Kamil Debskiaf935742011-06-21 10:51:26 -03001815 }
1816 return 0;
1817}
1818
1819void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
1820{
1821 int i;
1822
1823 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1824 for (i = 0; i < NUM_CTRLS; i++)
1825 ctx->ctrls[i] = NULL;
1826}