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