1 /* 2 * Copyright 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 //#define LOG_NDEBUG 0 18 #define LOG_TAG "SoftAVCEnc" 19 #include <utils/Log.h> 20 #include <utils/misc.h> 21 22 #include "OMX_Video.h" 23 24 #include <HardwareAPI.h> 25 #include <MetadataBufferType.h> 26 #include <media/stagefright/foundation/ADebug.h> 27 #include <media/stagefright/MediaDefs.h> 28 #include <media/stagefright/MediaErrors.h> 29 #include <media/stagefright/MetaData.h> 30 #include <media/stagefright/Utils.h> 31 #include <OMX_IndexExt.h> 32 #include <OMX_VideoExt.h> 33 #include <ui/Rect.h> 34 35 #include "ih264_typedefs.h" 36 #include "iv2.h" 37 #include "ive2.h" 38 #include "ih264e.h" 39 #include "SoftAVCEnc.h" 40 41 namespace android { 42 43 #define ive_api_function ih264e_api_function 44 45 template<class T> 46 static void InitOMXParams(T *params) { 47 params->nSize = sizeof(T); 48 params->nVersion.s.nVersionMajor = 1; 49 params->nVersion.s.nVersionMinor = 0; 50 params->nVersion.s.nRevision = 0; 51 params->nVersion.s.nStep = 0; 52 } 53 54 struct LevelConversion { 55 OMX_VIDEO_AVCLEVELTYPE omxLevel; 56 WORD32 avcLevel; 57 }; 58 59 static LevelConversion ConversionTable[] = { 60 { OMX_VIDEO_AVCLevel1, 10 }, 61 { OMX_VIDEO_AVCLevel1b, 9 }, 62 { OMX_VIDEO_AVCLevel11, 11 }, 63 { OMX_VIDEO_AVCLevel12, 12 }, 64 { OMX_VIDEO_AVCLevel13, 13 }, 65 { OMX_VIDEO_AVCLevel2, 20 }, 66 { OMX_VIDEO_AVCLevel21, 21 }, 67 { OMX_VIDEO_AVCLevel22, 22 }, 68 { OMX_VIDEO_AVCLevel3, 30 }, 69 { OMX_VIDEO_AVCLevel31, 31 }, 70 { OMX_VIDEO_AVCLevel32, 32 }, 71 { OMX_VIDEO_AVCLevel4, 40 }, 72 { OMX_VIDEO_AVCLevel41, 41 }, 73 { OMX_VIDEO_AVCLevel42, 42 }, 74 { OMX_VIDEO_AVCLevel5, 50 }, 75 { OMX_VIDEO_AVCLevel51, 51 }, 76 }; 77 78 static const CodecProfileLevel kProfileLevels[] = { 79 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel1 }, 80 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel1b }, 81 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel11 }, 82 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel12 }, 83 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel13 }, 84 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel2 }, 85 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel21 }, 86 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel22 }, 87 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel3 }, 88 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel31 }, 89 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel32 }, 90 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel4 }, 91 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel41 }, 92 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel1 }, 93 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel1b }, 94 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel11 }, 95 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel12 }, 96 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel13 }, 97 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel2 }, 98 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel21 }, 99 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel22 }, 100 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel3 }, 101 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel31 }, 102 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel32 }, 103 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel4 }, 104 { OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel41 }, 105 106 }; 107 108 static size_t GetCPUCoreCount() { 109 long cpuCoreCount = 1; 110 #if defined(_SC_NPROCESSORS_ONLN) 111 cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN); 112 #else 113 // _SC_NPROC_ONLN must be defined... 114 cpuCoreCount = sysconf(_SC_NPROC_ONLN); 115 #endif 116 CHECK(cpuCoreCount >= 1); 117 ALOGV("Number of CPU cores: %ld", cpuCoreCount); 118 return (size_t)cpuCoreCount; 119 } 120 121 static status_t ConvertOmxAvcLevelToAvcSpecLevel( 122 OMX_VIDEO_AVCLEVELTYPE omxLevel, WORD32 *avcLevel) { 123 for (size_t i = 0; i < NELEM(ConversionTable); ++i) { 124 if (omxLevel == ConversionTable[i].omxLevel) { 125 *avcLevel = ConversionTable[i].avcLevel; 126 return OK; 127 } 128 } 129 130 ALOGE("ConvertOmxAvcLevelToAvcSpecLevel: %d level not supported", 131 (int32_t)omxLevel); 132 133 return BAD_VALUE; 134 } 135 136 static status_t ConvertAvcSpecLevelToOmxAvcLevel( 137 WORD32 avcLevel, OMX_VIDEO_AVCLEVELTYPE *omxLevel) { 138 for (size_t i = 0; i < NELEM(ConversionTable); ++i) { 139 if (avcLevel == ConversionTable[i].avcLevel) { 140 *omxLevel = ConversionTable[i].omxLevel; 141 return OK; 142 } 143 } 144 145 ALOGE("ConvertAvcSpecLevelToOmxAvcLevel: %d level not supported", 146 (int32_t)avcLevel); 147 148 return BAD_VALUE; 149 } 150 151 152 SoftAVC::SoftAVC( 153 const char *name, 154 const OMX_CALLBACKTYPE *callbacks, 155 OMX_PTR appData, 156 OMX_COMPONENTTYPE **component) 157 : SoftVideoEncoderOMXComponent( 158 name, "video_encoder.avc", OMX_VIDEO_CodingAVC, 159 kProfileLevels, NELEM(kProfileLevels), 160 176 /* width */, 144 /* height */, 161 callbacks, appData, component), 162 mUpdateFlag(0), 163 mIvVideoColorFormat(IV_YUV_420P), 164 mAVCEncProfile(IV_PROFILE_BASE), 165 mAVCEncLevel(41), 166 mStarted(false), 167 mSawInputEOS(false), 168 mSawOutputEOS(false), 169 mSignalledError(false), 170 mCodecCtx(NULL) { 171 172 initPorts(kNumBuffers, kNumBuffers, ((mWidth * mHeight * 3) >> 1), 173 MEDIA_MIMETYPE_VIDEO_AVC, 2); 174 175 // If dump is enabled, then open create an empty file 176 GENERATE_FILE_NAMES(); 177 CREATE_DUMP_FILE(mInFile); 178 CREATE_DUMP_FILE(mOutFile); 179 memset(mConversionBuffers, 0, sizeof(mConversionBuffers)); 180 memset(mInputBufferInfo, 0, sizeof(mInputBufferInfo)); 181 182 initEncParams(); 183 184 } 185 186 SoftAVC::~SoftAVC() { 187 releaseEncoder(); 188 List<BufferInfo *> &outQueue = getPortQueue(1); 189 List<BufferInfo *> &inQueue = getPortQueue(0); 190 CHECK(outQueue.empty()); 191 CHECK(inQueue.empty()); 192 } 193 194 void SoftAVC::initEncParams() { 195 mCodecCtx = NULL; 196 mMemRecords = NULL; 197 mNumMemRecords = DEFAULT_MEM_REC_CNT; 198 mHeaderGenerated = 0; 199 mNumCores = GetCPUCoreCount(); 200 mArch = DEFAULT_ARCH; 201 mSliceMode = DEFAULT_SLICE_MODE; 202 mSliceParam = DEFAULT_SLICE_PARAM; 203 mHalfPelEnable = DEFAULT_HPEL; 204 mIInterval = DEFAULT_I_INTERVAL; 205 mIDRInterval = DEFAULT_IDR_INTERVAL; 206 mDisableDeblkLevel = DEFAULT_DISABLE_DEBLK_LEVEL; 207 mEnableFastSad = DEFAULT_ENABLE_FAST_SAD; 208 mEnableAltRef = DEFAULT_ENABLE_ALT_REF; 209 mEncSpeed = DEFAULT_ENC_SPEED; 210 mIntra4x4 = DEFAULT_INTRA4x4; 211 mConstrainedIntraFlag = DEFAULT_CONSTRAINED_INTRA; 212 mAIRMode = DEFAULT_AIR; 213 mAIRRefreshPeriod = DEFAULT_AIR_REFRESH_PERIOD; 214 mPSNREnable = DEFAULT_PSNR_ENABLE; 215 mReconEnable = DEFAULT_RECON_ENABLE; 216 mEntropyMode = DEFAULT_ENTROPY_MODE; 217 mBframes = DEFAULT_B_FRAMES; 218 219 gettimeofday(&mTimeStart, NULL); 220 gettimeofday(&mTimeEnd, NULL); 221 222 } 223 224 225 OMX_ERRORTYPE SoftAVC::setDimensions() { 226 ive_ctl_set_dimensions_ip_t s_dimensions_ip; 227 ive_ctl_set_dimensions_op_t s_dimensions_op; 228 IV_STATUS_T status; 229 230 s_dimensions_ip.e_cmd = IVE_CMD_VIDEO_CTL; 231 s_dimensions_ip.e_sub_cmd = IVE_CMD_CTL_SET_DIMENSIONS; 232 s_dimensions_ip.u4_ht = mHeight; 233 s_dimensions_ip.u4_wd = mWidth; 234 235 s_dimensions_ip.u4_timestamp_high = -1; 236 s_dimensions_ip.u4_timestamp_low = -1; 237 238 s_dimensions_ip.u4_size = sizeof(ive_ctl_set_dimensions_ip_t); 239 s_dimensions_op.u4_size = sizeof(ive_ctl_set_dimensions_op_t); 240 241 status = ive_api_function(mCodecCtx, &s_dimensions_ip, &s_dimensions_op); 242 if (status != IV_SUCCESS) { 243 ALOGE("Unable to set frame dimensions = 0x%x\n", 244 s_dimensions_op.u4_error_code); 245 return OMX_ErrorUndefined; 246 } 247 return OMX_ErrorNone; 248 } 249 250 OMX_ERRORTYPE SoftAVC::setNumCores() { 251 IV_STATUS_T status; 252 ive_ctl_set_num_cores_ip_t s_num_cores_ip; 253 ive_ctl_set_num_cores_op_t s_num_cores_op; 254 s_num_cores_ip.e_cmd = IVE_CMD_VIDEO_CTL; 255 s_num_cores_ip.e_sub_cmd = IVE_CMD_CTL_SET_NUM_CORES; 256 s_num_cores_ip.u4_num_cores = MIN(mNumCores, CODEC_MAX_CORES); 257 s_num_cores_ip.u4_timestamp_high = -1; 258 s_num_cores_ip.u4_timestamp_low = -1; 259 s_num_cores_ip.u4_size = sizeof(ive_ctl_set_num_cores_ip_t); 260 261 s_num_cores_op.u4_size = sizeof(ive_ctl_set_num_cores_op_t); 262 263 status = ive_api_function( 264 mCodecCtx, (void *) &s_num_cores_ip, (void *) &s_num_cores_op); 265 if (status != IV_SUCCESS) { 266 ALOGE("Unable to set processor params = 0x%x\n", 267 s_num_cores_op.u4_error_code); 268 return OMX_ErrorUndefined; 269 } 270 return OMX_ErrorNone; 271 } 272 273 OMX_ERRORTYPE SoftAVC::setFrameRate() { 274 ive_ctl_set_frame_rate_ip_t s_frame_rate_ip; 275 ive_ctl_set_frame_rate_op_t s_frame_rate_op; 276 IV_STATUS_T status; 277 278 s_frame_rate_ip.e_cmd = IVE_CMD_VIDEO_CTL; 279 s_frame_rate_ip.e_sub_cmd = IVE_CMD_CTL_SET_FRAMERATE; 280 281 s_frame_rate_ip.u4_src_frame_rate = mFramerate >> 16; 282 s_frame_rate_ip.u4_tgt_frame_rate = mFramerate >> 16; 283 284 s_frame_rate_ip.u4_timestamp_high = -1; 285 s_frame_rate_ip.u4_timestamp_low = -1; 286 287 s_frame_rate_ip.u4_size = sizeof(ive_ctl_set_frame_rate_ip_t); 288 s_frame_rate_op.u4_size = sizeof(ive_ctl_set_frame_rate_op_t); 289 290 status = ive_api_function(mCodecCtx, &s_frame_rate_ip, &s_frame_rate_op); 291 if (status != IV_SUCCESS) { 292 ALOGE("Unable to set frame rate = 0x%x\n", 293 s_frame_rate_op.u4_error_code); 294 return OMX_ErrorUndefined; 295 } 296 return OMX_ErrorNone; 297 } 298 299 OMX_ERRORTYPE SoftAVC::setIpeParams() { 300 ive_ctl_set_ipe_params_ip_t s_ipe_params_ip; 301 ive_ctl_set_ipe_params_op_t s_ipe_params_op; 302 IV_STATUS_T status; 303 304 s_ipe_params_ip.e_cmd = IVE_CMD_VIDEO_CTL; 305 s_ipe_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_IPE_PARAMS; 306 307 s_ipe_params_ip.u4_enable_intra_4x4 = mIntra4x4; 308 s_ipe_params_ip.u4_enc_speed_preset = mEncSpeed; 309 s_ipe_params_ip.u4_constrained_intra_pred = mConstrainedIntraFlag; 310 311 s_ipe_params_ip.u4_timestamp_high = -1; 312 s_ipe_params_ip.u4_timestamp_low = -1; 313 314 s_ipe_params_ip.u4_size = sizeof(ive_ctl_set_ipe_params_ip_t); 315 s_ipe_params_op.u4_size = sizeof(ive_ctl_set_ipe_params_op_t); 316 317 status = ive_api_function(mCodecCtx, &s_ipe_params_ip, &s_ipe_params_op); 318 if (status != IV_SUCCESS) { 319 ALOGE("Unable to set ipe params = 0x%x\n", 320 s_ipe_params_op.u4_error_code); 321 return OMX_ErrorUndefined; 322 } 323 return OMX_ErrorNone; 324 } 325 326 OMX_ERRORTYPE SoftAVC::setBitRate() { 327 ive_ctl_set_bitrate_ip_t s_bitrate_ip; 328 ive_ctl_set_bitrate_op_t s_bitrate_op; 329 IV_STATUS_T status; 330 331 s_bitrate_ip.e_cmd = IVE_CMD_VIDEO_CTL; 332 s_bitrate_ip.e_sub_cmd = IVE_CMD_CTL_SET_BITRATE; 333 334 s_bitrate_ip.u4_target_bitrate = mBitrate; 335 336 s_bitrate_ip.u4_timestamp_high = -1; 337 s_bitrate_ip.u4_timestamp_low = -1; 338 339 s_bitrate_ip.u4_size = sizeof(ive_ctl_set_bitrate_ip_t); 340 s_bitrate_op.u4_size = sizeof(ive_ctl_set_bitrate_op_t); 341 342 status = ive_api_function(mCodecCtx, &s_bitrate_ip, &s_bitrate_op); 343 if (status != IV_SUCCESS) { 344 ALOGE("Unable to set bit rate = 0x%x\n", s_bitrate_op.u4_error_code); 345 return OMX_ErrorUndefined; 346 } 347 return OMX_ErrorNone; 348 } 349 350 OMX_ERRORTYPE SoftAVC::setFrameType(IV_PICTURE_CODING_TYPE_T e_frame_type) { 351 ive_ctl_set_frame_type_ip_t s_frame_type_ip; 352 ive_ctl_set_frame_type_op_t s_frame_type_op; 353 IV_STATUS_T status; 354 s_frame_type_ip.e_cmd = IVE_CMD_VIDEO_CTL; 355 s_frame_type_ip.e_sub_cmd = IVE_CMD_CTL_SET_FRAMETYPE; 356 357 s_frame_type_ip.e_frame_type = e_frame_type; 358 359 s_frame_type_ip.u4_timestamp_high = -1; 360 s_frame_type_ip.u4_timestamp_low = -1; 361 362 s_frame_type_ip.u4_size = sizeof(ive_ctl_set_frame_type_ip_t); 363 s_frame_type_op.u4_size = sizeof(ive_ctl_set_frame_type_op_t); 364 365 status = ive_api_function(mCodecCtx, &s_frame_type_ip, &s_frame_type_op); 366 if (status != IV_SUCCESS) { 367 ALOGE("Unable to set frame type = 0x%x\n", 368 s_frame_type_op.u4_error_code); 369 return OMX_ErrorUndefined; 370 } 371 return OMX_ErrorNone; 372 } 373 374 OMX_ERRORTYPE SoftAVC::setQp() { 375 ive_ctl_set_qp_ip_t s_qp_ip; 376 ive_ctl_set_qp_op_t s_qp_op; 377 IV_STATUS_T status; 378 379 s_qp_ip.e_cmd = IVE_CMD_VIDEO_CTL; 380 s_qp_ip.e_sub_cmd = IVE_CMD_CTL_SET_QP; 381 382 s_qp_ip.u4_i_qp = DEFAULT_I_QP; 383 s_qp_ip.u4_i_qp_max = DEFAULT_QP_MAX; 384 s_qp_ip.u4_i_qp_min = DEFAULT_QP_MIN; 385 386 s_qp_ip.u4_p_qp = DEFAULT_P_QP; 387 s_qp_ip.u4_p_qp_max = DEFAULT_QP_MAX; 388 s_qp_ip.u4_p_qp_min = DEFAULT_QP_MIN; 389 390 s_qp_ip.u4_b_qp = DEFAULT_P_QP; 391 s_qp_ip.u4_b_qp_max = DEFAULT_QP_MAX; 392 s_qp_ip.u4_b_qp_min = DEFAULT_QP_MIN; 393 394 s_qp_ip.u4_timestamp_high = -1; 395 s_qp_ip.u4_timestamp_low = -1; 396 397 s_qp_ip.u4_size = sizeof(ive_ctl_set_qp_ip_t); 398 s_qp_op.u4_size = sizeof(ive_ctl_set_qp_op_t); 399 400 status = ive_api_function(mCodecCtx, &s_qp_ip, &s_qp_op); 401 if (status != IV_SUCCESS) { 402 ALOGE("Unable to set qp 0x%x\n", s_qp_op.u4_error_code); 403 return OMX_ErrorUndefined; 404 } 405 return OMX_ErrorNone; 406 } 407 408 OMX_ERRORTYPE SoftAVC::setEncMode(IVE_ENC_MODE_T e_enc_mode) { 409 IV_STATUS_T status; 410 ive_ctl_set_enc_mode_ip_t s_enc_mode_ip; 411 ive_ctl_set_enc_mode_op_t s_enc_mode_op; 412 413 s_enc_mode_ip.e_cmd = IVE_CMD_VIDEO_CTL; 414 s_enc_mode_ip.e_sub_cmd = IVE_CMD_CTL_SET_ENC_MODE; 415 416 s_enc_mode_ip.e_enc_mode = e_enc_mode; 417 418 s_enc_mode_ip.u4_timestamp_high = -1; 419 s_enc_mode_ip.u4_timestamp_low = -1; 420 421 s_enc_mode_ip.u4_size = sizeof(ive_ctl_set_enc_mode_ip_t); 422 s_enc_mode_op.u4_size = sizeof(ive_ctl_set_enc_mode_op_t); 423 424 status = ive_api_function(mCodecCtx, &s_enc_mode_ip, &s_enc_mode_op); 425 if (status != IV_SUCCESS) { 426 ALOGE("Unable to set in header encode mode = 0x%x\n", 427 s_enc_mode_op.u4_error_code); 428 return OMX_ErrorUndefined; 429 } 430 return OMX_ErrorNone; 431 } 432 433 OMX_ERRORTYPE SoftAVC::setVbvParams() { 434 ive_ctl_set_vbv_params_ip_t s_vbv_ip; 435 ive_ctl_set_vbv_params_op_t s_vbv_op; 436 IV_STATUS_T status; 437 438 s_vbv_ip.e_cmd = IVE_CMD_VIDEO_CTL; 439 s_vbv_ip.e_sub_cmd = IVE_CMD_CTL_SET_VBV_PARAMS; 440 441 s_vbv_ip.u4_vbv_buf_size = 0; 442 s_vbv_ip.u4_vbv_buffer_delay = 1000; 443 444 s_vbv_ip.u4_timestamp_high = -1; 445 s_vbv_ip.u4_timestamp_low = -1; 446 447 s_vbv_ip.u4_size = sizeof(ive_ctl_set_vbv_params_ip_t); 448 s_vbv_op.u4_size = sizeof(ive_ctl_set_vbv_params_op_t); 449 450 status = ive_api_function(mCodecCtx, &s_vbv_ip, &s_vbv_op); 451 if (status != IV_SUCCESS) { 452 ALOGE("Unable to set VBC params = 0x%x\n", s_vbv_op.u4_error_code); 453 return OMX_ErrorUndefined; 454 } 455 return OMX_ErrorNone; 456 } 457 458 OMX_ERRORTYPE SoftAVC::setAirParams() { 459 ive_ctl_set_air_params_ip_t s_air_ip; 460 ive_ctl_set_air_params_op_t s_air_op; 461 IV_STATUS_T status; 462 463 s_air_ip.e_cmd = IVE_CMD_VIDEO_CTL; 464 s_air_ip.e_sub_cmd = IVE_CMD_CTL_SET_AIR_PARAMS; 465 466 s_air_ip.e_air_mode = mAIRMode; 467 s_air_ip.u4_air_refresh_period = mAIRRefreshPeriod; 468 469 s_air_ip.u4_timestamp_high = -1; 470 s_air_ip.u4_timestamp_low = -1; 471 472 s_air_ip.u4_size = sizeof(ive_ctl_set_air_params_ip_t); 473 s_air_op.u4_size = sizeof(ive_ctl_set_air_params_op_t); 474 475 status = ive_api_function(mCodecCtx, &s_air_ip, &s_air_op); 476 if (status != IV_SUCCESS) { 477 ALOGE("Unable to set air params = 0x%x\n", s_air_op.u4_error_code); 478 return OMX_ErrorUndefined; 479 } 480 return OMX_ErrorNone; 481 } 482 483 OMX_ERRORTYPE SoftAVC::setMeParams() { 484 IV_STATUS_T status; 485 ive_ctl_set_me_params_ip_t s_me_params_ip; 486 ive_ctl_set_me_params_op_t s_me_params_op; 487 488 s_me_params_ip.e_cmd = IVE_CMD_VIDEO_CTL; 489 s_me_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_ME_PARAMS; 490 491 s_me_params_ip.u4_enable_fast_sad = mEnableFastSad; 492 s_me_params_ip.u4_enable_alt_ref = mEnableAltRef; 493 494 s_me_params_ip.u4_enable_hpel = mHalfPelEnable; 495 s_me_params_ip.u4_enable_qpel = DEFAULT_QPEL; 496 s_me_params_ip.u4_me_speed_preset = DEFAULT_ME_SPEED; 497 s_me_params_ip.u4_srch_rng_x = DEFAULT_SRCH_RNG_X; 498 s_me_params_ip.u4_srch_rng_y = DEFAULT_SRCH_RNG_Y; 499 500 s_me_params_ip.u4_timestamp_high = -1; 501 s_me_params_ip.u4_timestamp_low = -1; 502 503 s_me_params_ip.u4_size = sizeof(ive_ctl_set_me_params_ip_t); 504 s_me_params_op.u4_size = sizeof(ive_ctl_set_me_params_op_t); 505 506 status = ive_api_function(mCodecCtx, &s_me_params_ip, &s_me_params_op); 507 if (status != IV_SUCCESS) { 508 ALOGE("Unable to set me params = 0x%x\n", s_me_params_op.u4_error_code); 509 return OMX_ErrorUndefined; 510 } 511 return OMX_ErrorNone; 512 } 513 514 OMX_ERRORTYPE SoftAVC::setGopParams() { 515 IV_STATUS_T status; 516 ive_ctl_set_gop_params_ip_t s_gop_params_ip; 517 ive_ctl_set_gop_params_op_t s_gop_params_op; 518 519 s_gop_params_ip.e_cmd = IVE_CMD_VIDEO_CTL; 520 s_gop_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_GOP_PARAMS; 521 522 s_gop_params_ip.u4_i_frm_interval = mIInterval; 523 s_gop_params_ip.u4_idr_frm_interval = mIDRInterval; 524 525 s_gop_params_ip.u4_timestamp_high = -1; 526 s_gop_params_ip.u4_timestamp_low = -1; 527 528 s_gop_params_ip.u4_size = sizeof(ive_ctl_set_gop_params_ip_t); 529 s_gop_params_op.u4_size = sizeof(ive_ctl_set_gop_params_op_t); 530 531 status = ive_api_function(mCodecCtx, &s_gop_params_ip, &s_gop_params_op); 532 if (status != IV_SUCCESS) { 533 ALOGE("Unable to set ME params = 0x%x\n", 534 s_gop_params_op.u4_error_code); 535 return OMX_ErrorUndefined; 536 } 537 return OMX_ErrorNone; 538 } 539 540 OMX_ERRORTYPE SoftAVC::setProfileParams() { 541 IV_STATUS_T status; 542 ive_ctl_set_profile_params_ip_t s_profile_params_ip; 543 ive_ctl_set_profile_params_op_t s_profile_params_op; 544 545 s_profile_params_ip.e_cmd = IVE_CMD_VIDEO_CTL; 546 s_profile_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_PROFILE_PARAMS; 547 548 s_profile_params_ip.e_profile = DEFAULT_EPROFILE; 549 s_profile_params_ip.u4_entropy_coding_mode = mEntropyMode; 550 s_profile_params_ip.u4_timestamp_high = -1; 551 s_profile_params_ip.u4_timestamp_low = -1; 552 553 s_profile_params_ip.u4_size = sizeof(ive_ctl_set_profile_params_ip_t); 554 s_profile_params_op.u4_size = sizeof(ive_ctl_set_profile_params_op_t); 555 556 status = ive_api_function(mCodecCtx, &s_profile_params_ip, &s_profile_params_op); 557 if (status != IV_SUCCESS) { 558 ALOGE("Unable to set profile params = 0x%x\n", 559 s_profile_params_op.u4_error_code); 560 return OMX_ErrorUndefined; 561 } 562 return OMX_ErrorNone; 563 } 564 565 OMX_ERRORTYPE SoftAVC::setDeblockParams() { 566 IV_STATUS_T status; 567 ive_ctl_set_deblock_params_ip_t s_deblock_params_ip; 568 ive_ctl_set_deblock_params_op_t s_deblock_params_op; 569 570 s_deblock_params_ip.e_cmd = IVE_CMD_VIDEO_CTL; 571 s_deblock_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_DEBLOCK_PARAMS; 572 573 s_deblock_params_ip.u4_disable_deblock_level = mDisableDeblkLevel; 574 575 s_deblock_params_ip.u4_timestamp_high = -1; 576 s_deblock_params_ip.u4_timestamp_low = -1; 577 578 s_deblock_params_ip.u4_size = sizeof(ive_ctl_set_deblock_params_ip_t); 579 s_deblock_params_op.u4_size = sizeof(ive_ctl_set_deblock_params_op_t); 580 581 status = ive_api_function(mCodecCtx, &s_deblock_params_ip, &s_deblock_params_op); 582 if (status != IV_SUCCESS) { 583 ALOGE("Unable to enable/disable deblock params = 0x%x\n", 584 s_deblock_params_op.u4_error_code); 585 return OMX_ErrorUndefined; 586 } 587 return OMX_ErrorNone; 588 } 589 590 void SoftAVC::logVersion() { 591 ive_ctl_getversioninfo_ip_t s_ctl_ip; 592 ive_ctl_getversioninfo_op_t s_ctl_op; 593 UWORD8 au1_buf[512]; 594 IV_STATUS_T status; 595 596 s_ctl_ip.e_cmd = IVE_CMD_VIDEO_CTL; 597 s_ctl_ip.e_sub_cmd = IVE_CMD_CTL_GETVERSION; 598 s_ctl_ip.u4_size = sizeof(ive_ctl_getversioninfo_ip_t); 599 s_ctl_op.u4_size = sizeof(ive_ctl_getversioninfo_op_t); 600 s_ctl_ip.pu1_version = au1_buf; 601 s_ctl_ip.u4_version_bufsize = sizeof(au1_buf); 602 603 status = ive_api_function(mCodecCtx, (void *) &s_ctl_ip, (void *) &s_ctl_op); 604 605 if (status != IV_SUCCESS) { 606 ALOGE("Error in getting version: 0x%x", s_ctl_op.u4_error_code); 607 } else { 608 ALOGV("Ittiam encoder version: %s", (char *)s_ctl_ip.pu1_version); 609 } 610 return; 611 } 612 613 OMX_ERRORTYPE SoftAVC::initEncoder() { 614 IV_STATUS_T status; 615 WORD32 level; 616 uint32_t displaySizeY; 617 CHECK(!mStarted); 618 619 OMX_ERRORTYPE errType = OMX_ErrorNone; 620 621 displaySizeY = mWidth * mHeight; 622 if (displaySizeY > (1920 * 1088)) { 623 level = 50; 624 } else if (displaySizeY > (1280 * 720)) { 625 level = 40; 626 } else if (displaySizeY > (720 * 576)) { 627 level = 31; 628 } else if (displaySizeY > (624 * 320)) { 629 level = 30; 630 } else if (displaySizeY > (352 * 288)) { 631 level = 21; 632 } else { 633 level = 20; 634 } 635 mAVCEncLevel = MAX(level, mAVCEncLevel); 636 637 mStride = mWidth; 638 639 if (mInputDataIsMeta) { 640 for (size_t i = 0; i < MAX_CONVERSION_BUFFERS; i++) { 641 if (mConversionBuffers[i] != NULL) { 642 free(mConversionBuffers[i]); 643 mConversionBuffers[i] = 0; 644 } 645 646 if (((uint64_t)mStride * mHeight) > ((uint64_t)INT32_MAX / 3)) { 647 ALOGE("Buffer size is too big."); 648 return OMX_ErrorUndefined; 649 } 650 mConversionBuffers[i] = (uint8_t *)malloc(mStride * mHeight * 3 / 2); 651 652 if (mConversionBuffers[i] == NULL) { 653 ALOGE("Allocating conversion buffer failed."); 654 return OMX_ErrorUndefined; 655 } 656 657 mConversionBuffersFree[i] = 1; 658 } 659 } 660 661 switch (mColorFormat) { 662 case OMX_COLOR_FormatYUV420SemiPlanar: 663 mIvVideoColorFormat = IV_YUV_420SP_UV; 664 ALOGV("colorFormat YUV_420SP"); 665 break; 666 default: 667 case OMX_COLOR_FormatYUV420Planar: 668 mIvVideoColorFormat = IV_YUV_420P; 669 ALOGV("colorFormat YUV_420P"); 670 break; 671 } 672 673 ALOGD("Params width %d height %d level %d colorFormat %d", mWidth, 674 mHeight, mAVCEncLevel, mIvVideoColorFormat); 675 676 /* Getting Number of MemRecords */ 677 { 678 iv_num_mem_rec_ip_t s_num_mem_rec_ip; 679 iv_num_mem_rec_op_t s_num_mem_rec_op; 680 681 s_num_mem_rec_ip.u4_size = sizeof(iv_num_mem_rec_ip_t); 682 s_num_mem_rec_op.u4_size = sizeof(iv_num_mem_rec_op_t); 683 684 s_num_mem_rec_ip.e_cmd = IV_CMD_GET_NUM_MEM_REC; 685 686 status = ive_api_function(0, &s_num_mem_rec_ip, &s_num_mem_rec_op); 687 688 if (status != IV_SUCCESS) { 689 ALOGE("Get number of memory records failed = 0x%x\n", 690 s_num_mem_rec_op.u4_error_code); 691 return OMX_ErrorUndefined; 692 } 693 694 mNumMemRecords = s_num_mem_rec_op.u4_num_mem_rec; 695 } 696 697 /* Allocate array to hold memory records */ 698 if (mNumMemRecords > SIZE_MAX / sizeof(iv_mem_rec_t)) { 699 ALOGE("requested memory size is too big."); 700 return OMX_ErrorUndefined; 701 } 702 mMemRecords = (iv_mem_rec_t *)malloc(mNumMemRecords * sizeof(iv_mem_rec_t)); 703 if (NULL == mMemRecords) { 704 ALOGE("Unable to allocate memory for hold memory records: Size %zu", 705 mNumMemRecords * sizeof(iv_mem_rec_t)); 706 mSignalledError = true; 707 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 708 return OMX_ErrorUndefined; 709 } 710 711 { 712 iv_mem_rec_t *ps_mem_rec; 713 ps_mem_rec = mMemRecords; 714 for (size_t i = 0; i < mNumMemRecords; i++) { 715 ps_mem_rec->u4_size = sizeof(iv_mem_rec_t); 716 ps_mem_rec->pv_base = NULL; 717 ps_mem_rec->u4_mem_size = 0; 718 ps_mem_rec->u4_mem_alignment = 0; 719 ps_mem_rec->e_mem_type = IV_NA_MEM_TYPE; 720 721 ps_mem_rec++; 722 } 723 } 724 725 /* Getting MemRecords Attributes */ 726 { 727 iv_fill_mem_rec_ip_t s_fill_mem_rec_ip; 728 iv_fill_mem_rec_op_t s_fill_mem_rec_op; 729 730 s_fill_mem_rec_ip.u4_size = sizeof(iv_fill_mem_rec_ip_t); 731 s_fill_mem_rec_op.u4_size = sizeof(iv_fill_mem_rec_op_t); 732 733 s_fill_mem_rec_ip.e_cmd = IV_CMD_FILL_NUM_MEM_REC; 734 s_fill_mem_rec_ip.ps_mem_rec = mMemRecords; 735 s_fill_mem_rec_ip.u4_num_mem_rec = mNumMemRecords; 736 s_fill_mem_rec_ip.u4_max_wd = mWidth; 737 s_fill_mem_rec_ip.u4_max_ht = mHeight; 738 s_fill_mem_rec_ip.u4_max_level = mAVCEncLevel; 739 s_fill_mem_rec_ip.e_color_format = DEFAULT_INP_COLOR_FORMAT; 740 s_fill_mem_rec_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM; 741 s_fill_mem_rec_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM; 742 s_fill_mem_rec_ip.u4_max_srch_rng_x = DEFAULT_MAX_SRCH_RANGE_X; 743 s_fill_mem_rec_ip.u4_max_srch_rng_y = DEFAULT_MAX_SRCH_RANGE_Y; 744 745 status = ive_api_function(0, &s_fill_mem_rec_ip, &s_fill_mem_rec_op); 746 747 if (status != IV_SUCCESS) { 748 ALOGE("Fill memory records failed = 0x%x\n", 749 s_fill_mem_rec_op.u4_error_code); 750 mSignalledError = true; 751 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 752 return OMX_ErrorUndefined; 753 } 754 } 755 756 /* Allocating Memory for Mem Records */ 757 { 758 WORD32 total_size; 759 iv_mem_rec_t *ps_mem_rec; 760 total_size = 0; 761 ps_mem_rec = mMemRecords; 762 763 for (size_t i = 0; i < mNumMemRecords; i++) { 764 ps_mem_rec->pv_base = ive_aligned_malloc( 765 ps_mem_rec->u4_mem_alignment, ps_mem_rec->u4_mem_size); 766 if (ps_mem_rec->pv_base == NULL) { 767 ALOGE("Allocation failure for mem record id %zu size %u\n", i, 768 ps_mem_rec->u4_mem_size); 769 mSignalledError = true; 770 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 771 return OMX_ErrorUndefined; 772 773 } 774 total_size += ps_mem_rec->u4_mem_size; 775 776 ps_mem_rec++; 777 } 778 } 779 780 /* Codec Instance Creation */ 781 { 782 ive_init_ip_t s_init_ip; 783 ive_init_op_t s_init_op; 784 785 mCodecCtx = (iv_obj_t *)mMemRecords[0].pv_base; 786 mCodecCtx->u4_size = sizeof(iv_obj_t); 787 mCodecCtx->pv_fxns = (void *)ive_api_function; 788 789 s_init_ip.u4_size = sizeof(ive_init_ip_t); 790 s_init_op.u4_size = sizeof(ive_init_op_t); 791 792 s_init_ip.e_cmd = IV_CMD_INIT; 793 s_init_ip.u4_num_mem_rec = mNumMemRecords; 794 s_init_ip.ps_mem_rec = mMemRecords; 795 s_init_ip.u4_max_wd = mWidth; 796 s_init_ip.u4_max_ht = mHeight; 797 s_init_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM; 798 s_init_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM; 799 s_init_ip.u4_max_level = mAVCEncLevel; 800 s_init_ip.e_inp_color_fmt = mIvVideoColorFormat; 801 802 if (mReconEnable || mPSNREnable) { 803 s_init_ip.u4_enable_recon = 1; 804 } else { 805 s_init_ip.u4_enable_recon = 0; 806 } 807 s_init_ip.e_recon_color_fmt = DEFAULT_RECON_COLOR_FORMAT; 808 s_init_ip.e_rc_mode = DEFAULT_RC_MODE; 809 s_init_ip.u4_max_framerate = DEFAULT_MAX_FRAMERATE; 810 s_init_ip.u4_max_bitrate = DEFAULT_MAX_BITRATE; 811 s_init_ip.u4_num_bframes = mBframes; 812 s_init_ip.e_content_type = IV_PROGRESSIVE; 813 s_init_ip.u4_max_srch_rng_x = DEFAULT_MAX_SRCH_RANGE_X; 814 s_init_ip.u4_max_srch_rng_y = DEFAULT_MAX_SRCH_RANGE_Y; 815 s_init_ip.e_slice_mode = mSliceMode; 816 s_init_ip.u4_slice_param = mSliceParam; 817 s_init_ip.e_arch = mArch; 818 s_init_ip.e_soc = DEFAULT_SOC; 819 820 status = ive_api_function(mCodecCtx, &s_init_ip, &s_init_op); 821 822 if (status != IV_SUCCESS) { 823 ALOGE("Init memory records failed = 0x%x\n", 824 s_init_op.u4_error_code); 825 mSignalledError = true; 826 notify(OMX_EventError, OMX_ErrorUndefined, 0 /* arg2 */, NULL /* data */); 827 return OMX_ErrorUndefined; 828 } 829 } 830 831 /* Get Codec Version */ 832 logVersion(); 833 834 /* set processor details */ 835 setNumCores(); 836 837 /* Video control Set Frame dimensions */ 838 setDimensions(); 839 840 /* Video control Set Frame rates */ 841 setFrameRate(); 842 843 /* Video control Set IPE Params */ 844 setIpeParams(); 845 846 /* Video control Set Bitrate */ 847 setBitRate(); 848 849 /* Video control Set QP */ 850 setQp(); 851 852 /* Video control Set AIR params */ 853 setAirParams(); 854 855 /* Video control Set VBV params */ 856 setVbvParams(); 857 858 /* Video control Set Motion estimation params */ 859 setMeParams(); 860 861 /* Video control Set GOP params */ 862 setGopParams(); 863 864 /* Video control Set Deblock params */ 865 setDeblockParams(); 866 867 /* Video control Set Profile params */ 868 setProfileParams(); 869 870 /* Video control Set in Encode header mode */ 871 setEncMode(IVE_ENC_MODE_HEADER); 872 873 ALOGV("init_codec successfull"); 874 875 mSpsPpsHeaderReceived = false; 876 mStarted = true; 877 878 return OMX_ErrorNone; 879 } 880 881 OMX_ERRORTYPE SoftAVC::releaseEncoder() { 882 IV_STATUS_T status = IV_SUCCESS; 883 iv_retrieve_mem_rec_ip_t s_retrieve_mem_ip; 884 iv_retrieve_mem_rec_op_t s_retrieve_mem_op; 885 iv_mem_rec_t *ps_mem_rec; 886 887 if (!mStarted) { 888 return OMX_ErrorNone; 889 } 890 891 s_retrieve_mem_ip.u4_size = sizeof(iv_retrieve_mem_rec_ip_t); 892 s_retrieve_mem_op.u4_size = sizeof(iv_retrieve_mem_rec_op_t); 893 s_retrieve_mem_ip.e_cmd = IV_CMD_RETRIEVE_MEMREC; 894 s_retrieve_mem_ip.ps_mem_rec = mMemRecords; 895 896 status = ive_api_function(mCodecCtx, &s_retrieve_mem_ip, &s_retrieve_mem_op); 897 898 if (status != IV_SUCCESS) { 899 ALOGE("Unable to retrieve memory records = 0x%x\n", 900 s_retrieve_mem_op.u4_error_code); 901 return OMX_ErrorUndefined; 902 } 903 904 /* Free memory records */ 905 ps_mem_rec = mMemRecords; 906 for (size_t i = 0; i < s_retrieve_mem_op.u4_num_mem_rec_filled; i++) { 907 ive_aligned_free(ps_mem_rec->pv_base); 908 ps_mem_rec++; 909 } 910 911 free(mMemRecords); 912 913 for (size_t i = 0; i < MAX_CONVERSION_BUFFERS; i++) { 914 if (mConversionBuffers[i]) { 915 free(mConversionBuffers[i]); 916 mConversionBuffers[i] = NULL; 917 } 918 } 919 920 mStarted = false; 921 922 return OMX_ErrorNone; 923 } 924 925 OMX_ERRORTYPE SoftAVC::internalGetParameter(OMX_INDEXTYPE index, OMX_PTR params) { 926 switch (index) { 927 case OMX_IndexParamVideoBitrate: 928 { 929 OMX_VIDEO_PARAM_BITRATETYPE *bitRate = 930 (OMX_VIDEO_PARAM_BITRATETYPE *)params; 931 932 if (!isValidOMXParam(bitRate)) { 933 return OMX_ErrorBadParameter; 934 } 935 936 if (bitRate->nPortIndex != 1) { 937 return OMX_ErrorUndefined; 938 } 939 940 bitRate->eControlRate = OMX_Video_ControlRateVariable; 941 bitRate->nTargetBitrate = mBitrate; 942 return OMX_ErrorNone; 943 } 944 945 case OMX_IndexParamVideoAvc: 946 { 947 OMX_VIDEO_PARAM_AVCTYPE *avcParams = (OMX_VIDEO_PARAM_AVCTYPE *)params; 948 949 if (!isValidOMXParam(avcParams)) { 950 return OMX_ErrorBadParameter; 951 } 952 953 if (avcParams->nPortIndex != 1) { 954 return OMX_ErrorUndefined; 955 } 956 957 OMX_VIDEO_AVCLEVELTYPE omxLevel = OMX_VIDEO_AVCLevel41; 958 if (OMX_ErrorNone 959 != ConvertAvcSpecLevelToOmxAvcLevel(mAVCEncLevel, &omxLevel)) { 960 return OMX_ErrorUndefined; 961 } 962 963 avcParams->eProfile = OMX_VIDEO_AVCProfileBaseline; 964 avcParams->eLevel = omxLevel; 965 avcParams->nRefFrames = 1; 966 avcParams->bUseHadamard = OMX_TRUE; 967 avcParams->nAllowedPictureTypes = (OMX_VIDEO_PictureTypeI 968 | OMX_VIDEO_PictureTypeP | OMX_VIDEO_PictureTypeB); 969 avcParams->nRefIdx10ActiveMinus1 = 0; 970 avcParams->nRefIdx11ActiveMinus1 = 0; 971 avcParams->bWeightedPPrediction = OMX_FALSE; 972 avcParams->bconstIpred = OMX_FALSE; 973 avcParams->bDirect8x8Inference = OMX_FALSE; 974 avcParams->bDirectSpatialTemporal = OMX_FALSE; 975 avcParams->nCabacInitIdc = 0; 976 return OMX_ErrorNone; 977 } 978 979 default: 980 return SoftVideoEncoderOMXComponent::internalGetParameter(index, params); 981 } 982 } 983 984 OMX_ERRORTYPE SoftAVC::internalSetParameter(OMX_INDEXTYPE index, const OMX_PTR params) { 985 int32_t indexFull = index; 986 987 switch (indexFull) { 988 case OMX_IndexParamVideoBitrate: 989 { 990 OMX_VIDEO_PARAM_BITRATETYPE *bitRate = 991 (OMX_VIDEO_PARAM_BITRATETYPE *)params; 992 993 if (!isValidOMXParam(bitRate)) { 994 return OMX_ErrorBadParameter; 995 } 996 997 return internalSetBitrateParams(bitRate); 998 } 999 1000 case OMX_IndexParamVideoAvc: 1001 { 1002 OMX_VIDEO_PARAM_AVCTYPE *avcType = (OMX_VIDEO_PARAM_AVCTYPE *)params; 1003 1004 if (!isValidOMXParam(avcType)) { 1005 return OMX_ErrorBadParameter; 1006 } 1007 1008 if (avcType->nPortIndex != 1) { 1009 return OMX_ErrorUndefined; 1010 } 1011 1012 mEntropyMode = 0; 1013 1014 if (OMX_TRUE == avcType->bEntropyCodingCABAC) 1015 mEntropyMode = 1; 1016 1017 if ((avcType->nAllowedPictureTypes & OMX_VIDEO_PictureTypeB) && 1018 avcType->nPFrames) { 1019 mBframes = avcType->nBFrames; 1020 } 1021 1022 mIInterval = (avcType->nPFrames + 1) * (avcType->nBFrames + 1); 1023 mConstrainedIntraFlag = avcType->bconstIpred; 1024 1025 if (OMX_VIDEO_AVCLoopFilterDisable == avcType->eLoopFilterMode) 1026 mDisableDeblkLevel = 4; 1027 1028 if (avcType->nRefFrames != 1 1029 || avcType->bUseHadamard != OMX_TRUE 1030 || avcType->nRefIdx10ActiveMinus1 != 0 1031 || avcType->nRefIdx11ActiveMinus1 != 0 1032 || avcType->bWeightedPPrediction != OMX_FALSE 1033 || avcType->bDirect8x8Inference != OMX_FALSE 1034 || avcType->bDirectSpatialTemporal != OMX_FALSE 1035 || avcType->nCabacInitIdc != 0) { 1036 // OMX does not allow a way to signal what values are wrong, so it's 1037 // best for components to just do best effort in supporting these values 1038 ALOGV("ignoring unsupported settings"); 1039 } 1040 1041 if (OK != ConvertOmxAvcLevelToAvcSpecLevel(avcType->eLevel, &mAVCEncLevel)) { 1042 return OMX_ErrorUndefined; 1043 } 1044 1045 return OMX_ErrorNone; 1046 } 1047 1048 default: 1049 return SoftVideoEncoderOMXComponent::internalSetParameter(index, params); 1050 } 1051 } 1052 1053 OMX_ERRORTYPE SoftAVC::getConfig( 1054 OMX_INDEXTYPE index, OMX_PTR _params) { 1055 switch ((int)index) { 1056 case OMX_IndexConfigAndroidIntraRefresh: 1057 { 1058 OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE *intraRefreshParams = 1059 (OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE *)_params; 1060 1061 if (!isValidOMXParam(intraRefreshParams)) { 1062 return OMX_ErrorBadParameter; 1063 } 1064 1065 if (intraRefreshParams->nPortIndex != kOutputPortIndex) { 1066 return OMX_ErrorUndefined; 1067 } 1068 1069 intraRefreshParams->nRefreshPeriod = 1070 (mAIRMode == IVE_AIR_MODE_NONE) ? 0 : mAIRRefreshPeriod; 1071 return OMX_ErrorNone; 1072 } 1073 1074 default: 1075 return SoftVideoEncoderOMXComponent::getConfig(index, _params); 1076 } 1077 } 1078 1079 OMX_ERRORTYPE SoftAVC::setConfig( 1080 OMX_INDEXTYPE index, const OMX_PTR _params) { 1081 switch ((int)index) { 1082 case OMX_IndexConfigVideoIntraVOPRefresh: 1083 { 1084 OMX_CONFIG_INTRAREFRESHVOPTYPE *params = 1085 (OMX_CONFIG_INTRAREFRESHVOPTYPE *)_params; 1086 1087 if (!isValidOMXParam(params)) { 1088 return OMX_ErrorBadParameter; 1089 } 1090 1091 if (params->nPortIndex != kOutputPortIndex) { 1092 return OMX_ErrorBadPortIndex; 1093 } 1094 1095 if (params->IntraRefreshVOP) { 1096 mUpdateFlag |= kRequestKeyFrame; 1097 } 1098 return OMX_ErrorNone; 1099 } 1100 1101 case OMX_IndexConfigVideoBitrate: 1102 { 1103 OMX_VIDEO_CONFIG_BITRATETYPE *params = 1104 (OMX_VIDEO_CONFIG_BITRATETYPE *)_params; 1105 1106 if (!isValidOMXParam(params)) { 1107 return OMX_ErrorBadParameter; 1108 } 1109 1110 if (params->nPortIndex != kOutputPortIndex) { 1111 return OMX_ErrorBadPortIndex; 1112 } 1113 1114 if (mBitrate != params->nEncodeBitrate) { 1115 mBitrate = params->nEncodeBitrate; 1116 mUpdateFlag |= kUpdateBitrate; 1117 } 1118 return OMX_ErrorNone; 1119 } 1120 1121 case OMX_IndexConfigAndroidIntraRefresh: 1122 { 1123 const OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE *intraRefreshParams = 1124 (const OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE *)_params; 1125 1126 if (!isValidOMXParam(intraRefreshParams)) { 1127 return OMX_ErrorBadParameter; 1128 } 1129 1130 if (intraRefreshParams->nPortIndex != kOutputPortIndex) { 1131 return OMX_ErrorUndefined; 1132 } 1133 1134 if (intraRefreshParams->nRefreshPeriod == 0) { 1135 mAIRMode = IVE_AIR_MODE_NONE; 1136 mAIRRefreshPeriod = 0; 1137 } else if (intraRefreshParams->nRefreshPeriod > 0) { 1138 mAIRMode = IVE_AIR_MODE_CYCLIC; 1139 mAIRRefreshPeriod = intraRefreshParams->nRefreshPeriod; 1140 } 1141 mUpdateFlag |= kUpdateAIRMode; 1142 return OMX_ErrorNone; 1143 } 1144 1145 default: 1146 return SimpleSoftOMXComponent::setConfig(index, _params); 1147 } 1148 } 1149 1150 OMX_ERRORTYPE SoftAVC::internalSetBitrateParams( 1151 const OMX_VIDEO_PARAM_BITRATETYPE *bitrate) { 1152 if (bitrate->nPortIndex != kOutputPortIndex) { 1153 return OMX_ErrorUnsupportedIndex; 1154 } 1155 1156 mBitrate = bitrate->nTargetBitrate; 1157 mUpdateFlag |= kUpdateBitrate; 1158 1159 return OMX_ErrorNone; 1160 } 1161 1162 OMX_ERRORTYPE SoftAVC::setEncodeArgs( 1163 ive_video_encode_ip_t *ps_encode_ip, 1164 ive_video_encode_op_t *ps_encode_op, 1165 OMX_BUFFERHEADERTYPE *inputBufferHeader, 1166 OMX_BUFFERHEADERTYPE *outputBufferHeader) { 1167 iv_raw_buf_t *ps_inp_raw_buf; 1168 const uint8_t *source; 1169 UWORD8 *pu1_buf; 1170 1171 ps_inp_raw_buf = &ps_encode_ip->s_inp_buf; 1172 ps_encode_ip->s_out_buf.pv_buf = outputBufferHeader->pBuffer; 1173 ps_encode_ip->s_out_buf.u4_bytes = 0; 1174 ps_encode_ip->s_out_buf.u4_bufsize = outputBufferHeader->nAllocLen; 1175 ps_encode_ip->u4_size = sizeof(ive_video_encode_ip_t); 1176 ps_encode_op->u4_size = sizeof(ive_video_encode_op_t); 1177 1178 ps_encode_ip->e_cmd = IVE_CMD_VIDEO_ENCODE; 1179 ps_encode_ip->pv_bufs = NULL; 1180 ps_encode_ip->pv_mb_info = NULL; 1181 ps_encode_ip->pv_pic_info = NULL; 1182 ps_encode_ip->u4_mb_info_type = 0; 1183 ps_encode_ip->u4_pic_info_type = 0; 1184 ps_encode_op->s_out_buf.pv_buf = NULL; 1185 1186 /* Initialize color formats */ 1187 ps_inp_raw_buf->e_color_fmt = mIvVideoColorFormat; 1188 source = NULL; 1189 if ((inputBufferHeader != NULL) && inputBufferHeader->nFilledLen) { 1190 source = inputBufferHeader->pBuffer + inputBufferHeader->nOffset; 1191 1192 if (mInputDataIsMeta) { 1193 uint8_t *conversionBuffer = NULL; 1194 for (size_t i = 0; i < MAX_CONVERSION_BUFFERS; i++) { 1195 if (mConversionBuffersFree[i]) { 1196 mConversionBuffersFree[i] = 0; 1197 conversionBuffer = mConversionBuffers[i]; 1198 break; 1199 } 1200 } 1201 1202 if (NULL == conversionBuffer) { 1203 ALOGE("No free buffers to hold conversion data"); 1204 return OMX_ErrorUndefined; 1205 } 1206 1207 source = extractGraphicBuffer( 1208 conversionBuffer, (mWidth * mHeight * 3 / 2), source, 1209 inputBufferHeader->nFilledLen, mWidth, mHeight); 1210 1211 if (source == NULL) { 1212 ALOGE("Error in extractGraphicBuffer"); 1213 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 1214 return OMX_ErrorUndefined; 1215 } 1216 } 1217 ps_encode_ip->u4_is_last = 0; 1218 ps_encode_ip->u4_timestamp_high = (inputBufferHeader->nTimeStamp) >> 32; 1219 ps_encode_ip->u4_timestamp_low = (inputBufferHeader->nTimeStamp) & 0xFFFFFFFF; 1220 } 1221 else { 1222 if (mSawInputEOS){ 1223 ps_encode_ip->u4_is_last = 1; 1224 } 1225 memset(ps_inp_raw_buf, 0, sizeof(iv_raw_buf_t)); 1226 ps_inp_raw_buf->e_color_fmt = mIvVideoColorFormat; 1227 ps_inp_raw_buf->u4_size = sizeof(iv_raw_buf_t); 1228 return OMX_ErrorNone; 1229 } 1230 1231 pu1_buf = (UWORD8 *)source; 1232 switch (mIvVideoColorFormat) { 1233 case IV_YUV_420P: 1234 { 1235 ps_inp_raw_buf->apv_bufs[0] = pu1_buf; 1236 pu1_buf += (mStride) * mHeight; 1237 ps_inp_raw_buf->apv_bufs[1] = pu1_buf; 1238 pu1_buf += (mStride / 2) * mHeight / 2; 1239 ps_inp_raw_buf->apv_bufs[2] = pu1_buf; 1240 1241 ps_inp_raw_buf->au4_wd[0] = mWidth; 1242 ps_inp_raw_buf->au4_wd[1] = mWidth / 2; 1243 ps_inp_raw_buf->au4_wd[2] = mWidth / 2; 1244 1245 ps_inp_raw_buf->au4_ht[0] = mHeight; 1246 ps_inp_raw_buf->au4_ht[1] = mHeight / 2; 1247 ps_inp_raw_buf->au4_ht[2] = mHeight / 2; 1248 1249 ps_inp_raw_buf->au4_strd[0] = mStride; 1250 ps_inp_raw_buf->au4_strd[1] = (mStride / 2); 1251 ps_inp_raw_buf->au4_strd[2] = (mStride / 2); 1252 break; 1253 } 1254 1255 case IV_YUV_422ILE: 1256 { 1257 ps_inp_raw_buf->apv_bufs[0] = pu1_buf; 1258 ps_inp_raw_buf->au4_wd[0] = mWidth * 2; 1259 ps_inp_raw_buf->au4_ht[0] = mHeight; 1260 ps_inp_raw_buf->au4_strd[0] = mStride * 2; 1261 break; 1262 } 1263 1264 case IV_YUV_420SP_UV: 1265 case IV_YUV_420SP_VU: 1266 default: 1267 { 1268 ps_inp_raw_buf->apv_bufs[0] = pu1_buf; 1269 pu1_buf += (mStride) * mHeight; 1270 ps_inp_raw_buf->apv_bufs[1] = pu1_buf; 1271 1272 ps_inp_raw_buf->au4_wd[0] = mWidth; 1273 ps_inp_raw_buf->au4_wd[1] = mWidth; 1274 1275 ps_inp_raw_buf->au4_ht[0] = mHeight; 1276 ps_inp_raw_buf->au4_ht[1] = mHeight / 2; 1277 1278 ps_inp_raw_buf->au4_strd[0] = mStride; 1279 ps_inp_raw_buf->au4_strd[1] = mStride; 1280 break; 1281 } 1282 } 1283 return OMX_ErrorNone; 1284 } 1285 1286 void SoftAVC::onQueueFilled(OMX_U32 portIndex) { 1287 IV_STATUS_T status; 1288 WORD32 timeDelay, timeTaken; 1289 1290 UNUSED(portIndex); 1291 1292 // Initialize encoder if not already initialized 1293 if (mCodecCtx == NULL) { 1294 if (OMX_ErrorNone != initEncoder()) { 1295 ALOGE("Failed to initialize encoder"); 1296 notify(OMX_EventError, OMX_ErrorUndefined, 0 /* arg2 */, NULL /* data */); 1297 return; 1298 } 1299 } 1300 if (mSignalledError) { 1301 return; 1302 } 1303 1304 List<BufferInfo *> &inQueue = getPortQueue(0); 1305 List<BufferInfo *> &outQueue = getPortQueue(1); 1306 1307 while (!mSawOutputEOS && !outQueue.empty()) { 1308 1309 OMX_ERRORTYPE error; 1310 ive_video_encode_ip_t s_encode_ip; 1311 ive_video_encode_op_t s_encode_op; 1312 BufferInfo *outputBufferInfo = *outQueue.begin(); 1313 OMX_BUFFERHEADERTYPE *outputBufferHeader = outputBufferInfo->mHeader; 1314 1315 BufferInfo *inputBufferInfo; 1316 OMX_BUFFERHEADERTYPE *inputBufferHeader; 1317 1318 if (mSawInputEOS) { 1319 inputBufferHeader = NULL; 1320 inputBufferInfo = NULL; 1321 } else if (!inQueue.empty()) { 1322 inputBufferInfo = *inQueue.begin(); 1323 inputBufferHeader = inputBufferInfo->mHeader; 1324 } else { 1325 return; 1326 } 1327 1328 outputBufferHeader->nTimeStamp = 0; 1329 outputBufferHeader->nFlags = 0; 1330 outputBufferHeader->nOffset = 0; 1331 outputBufferHeader->nFilledLen = 0; 1332 outputBufferHeader->nOffset = 0; 1333 1334 if (inputBufferHeader != NULL) { 1335 outputBufferHeader->nFlags = inputBufferHeader->nFlags; 1336 } 1337 1338 uint8_t *outPtr = (uint8_t *)outputBufferHeader->pBuffer; 1339 1340 if (!mSpsPpsHeaderReceived) { 1341 error = setEncodeArgs(&s_encode_ip, &s_encode_op, NULL, outputBufferHeader); 1342 if (error != OMX_ErrorNone) { 1343 mSignalledError = true; 1344 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 1345 return; 1346 } 1347 status = ive_api_function(mCodecCtx, &s_encode_ip, &s_encode_op); 1348 1349 if (IV_SUCCESS != status) { 1350 ALOGE("Encode Frame failed = 0x%x\n", 1351 s_encode_op.u4_error_code); 1352 } else { 1353 ALOGV("Bytes Generated in header %d\n", 1354 s_encode_op.s_out_buf.u4_bytes); 1355 } 1356 1357 mSpsPpsHeaderReceived = true; 1358 1359 outputBufferHeader->nFlags = OMX_BUFFERFLAG_CODECCONFIG; 1360 outputBufferHeader->nFilledLen = s_encode_op.s_out_buf.u4_bytes; 1361 if (inputBufferHeader != NULL) { 1362 outputBufferHeader->nTimeStamp = inputBufferHeader->nTimeStamp; 1363 } 1364 1365 outQueue.erase(outQueue.begin()); 1366 outputBufferInfo->mOwnedByUs = false; 1367 1368 DUMP_TO_FILE( 1369 mOutFile, outputBufferHeader->pBuffer, 1370 outputBufferHeader->nFilledLen); 1371 notifyFillBufferDone(outputBufferHeader); 1372 1373 setEncMode(IVE_ENC_MODE_PICTURE); 1374 return; 1375 } 1376 1377 if (mUpdateFlag) { 1378 if (mUpdateFlag & kUpdateBitrate) { 1379 setBitRate(); 1380 } 1381 if (mUpdateFlag & kRequestKeyFrame) { 1382 setFrameType(IV_IDR_FRAME); 1383 } 1384 if (mUpdateFlag & kUpdateAIRMode) { 1385 setAirParams(); 1386 notify(OMX_EventPortSettingsChanged, kOutputPortIndex, 1387 OMX_IndexConfigAndroidIntraRefresh, NULL); 1388 } 1389 mUpdateFlag = 0; 1390 } 1391 1392 if ((inputBufferHeader != NULL) 1393 && (inputBufferHeader->nFlags & OMX_BUFFERFLAG_EOS)) { 1394 mSawInputEOS = true; 1395 } 1396 1397 /* In normal mode, store inputBufferInfo and this will be returned 1398 when encoder consumes this input */ 1399 if (!mInputDataIsMeta && (inputBufferInfo != NULL)) { 1400 for (size_t i = 0; i < MAX_INPUT_BUFFER_HEADERS; i++) { 1401 if (NULL == mInputBufferInfo[i]) { 1402 mInputBufferInfo[i] = inputBufferInfo; 1403 break; 1404 } 1405 } 1406 } 1407 error = setEncodeArgs( 1408 &s_encode_ip, &s_encode_op, inputBufferHeader, outputBufferHeader); 1409 1410 if (error != OMX_ErrorNone) { 1411 mSignalledError = true; 1412 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 1413 return; 1414 } 1415 1416 DUMP_TO_FILE( 1417 mInFile, s_encode_ip.s_inp_buf.apv_bufs[0], 1418 (mHeight * mStride * 3 / 2)); 1419 1420 GETTIME(&mTimeStart, NULL); 1421 /* Compute time elapsed between end of previous decode() 1422 * to start of current decode() */ 1423 TIME_DIFF(mTimeEnd, mTimeStart, timeDelay); 1424 status = ive_api_function(mCodecCtx, &s_encode_ip, &s_encode_op); 1425 1426 if (IV_SUCCESS != status) { 1427 ALOGE("Encode Frame failed = 0x%x\n", 1428 s_encode_op.u4_error_code); 1429 mSignalledError = true; 1430 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 1431 return; 1432 } 1433 1434 GETTIME(&mTimeEnd, NULL); 1435 /* Compute time taken for decode() */ 1436 TIME_DIFF(mTimeStart, mTimeEnd, timeTaken); 1437 1438 ALOGV("timeTaken=%6d delay=%6d numBytes=%6d", timeTaken, timeDelay, 1439 s_encode_op.s_out_buf.u4_bytes); 1440 1441 /* In encoder frees up an input buffer, mark it as free */ 1442 if (s_encode_op.s_inp_buf.apv_bufs[0] != NULL) { 1443 if (mInputDataIsMeta) { 1444 for (size_t i = 0; i < MAX_CONVERSION_BUFFERS; i++) { 1445 if (mConversionBuffers[i] == s_encode_op.s_inp_buf.apv_bufs[0]) { 1446 mConversionBuffersFree[i] = 1; 1447 break; 1448 } 1449 } 1450 } else { 1451 /* In normal mode, call EBD on inBuffeHeader that is freed by the codec */ 1452 for (size_t i = 0; i < MAX_INPUT_BUFFER_HEADERS; i++) { 1453 uint8_t *buf = NULL; 1454 OMX_BUFFERHEADERTYPE *bufHdr = NULL; 1455 if (mInputBufferInfo[i] != NULL) { 1456 bufHdr = mInputBufferInfo[i]->mHeader; 1457 buf = bufHdr->pBuffer + bufHdr->nOffset; 1458 } 1459 if (s_encode_op.s_inp_buf.apv_bufs[0] == buf) { 1460 mInputBufferInfo[i]->mOwnedByUs = false; 1461 notifyEmptyBufferDone(bufHdr); 1462 mInputBufferInfo[i] = NULL; 1463 break; 1464 } 1465 } 1466 } 1467 } 1468 1469 outputBufferHeader->nFilledLen = s_encode_op.s_out_buf.u4_bytes; 1470 1471 if (IV_IDR_FRAME == s_encode_op.u4_encoded_frame_type) { 1472 outputBufferHeader->nFlags |= OMX_BUFFERFLAG_SYNCFRAME; 1473 } 1474 1475 if (inputBufferHeader != NULL) { 1476 inQueue.erase(inQueue.begin()); 1477 1478 /* If in meta data, call EBD on input */ 1479 /* In case of normal mode, EBD will be done once encoder 1480 releases the input buffer */ 1481 if (mInputDataIsMeta) { 1482 inputBufferInfo->mOwnedByUs = false; 1483 notifyEmptyBufferDone(inputBufferHeader); 1484 } 1485 } 1486 1487 if (s_encode_op.u4_is_last) { 1488 outputBufferHeader->nFlags |= OMX_BUFFERFLAG_EOS; 1489 mSawOutputEOS = true; 1490 } else { 1491 outputBufferHeader->nFlags &= ~OMX_BUFFERFLAG_EOS; 1492 } 1493 1494 if (outputBufferHeader->nFilledLen || s_encode_op.u4_is_last) { 1495 outputBufferHeader->nTimeStamp = s_encode_op.u4_timestamp_high; 1496 outputBufferHeader->nTimeStamp <<= 32; 1497 outputBufferHeader->nTimeStamp |= s_encode_op.u4_timestamp_low; 1498 outputBufferInfo->mOwnedByUs = false; 1499 outQueue.erase(outQueue.begin()); 1500 DUMP_TO_FILE(mOutFile, outputBufferHeader->pBuffer, 1501 outputBufferHeader->nFilledLen); 1502 notifyFillBufferDone(outputBufferHeader); 1503 } 1504 1505 if (s_encode_op.u4_is_last == 1) { 1506 return; 1507 } 1508 } 1509 return; 1510 } 1511 1512 } // namespace android 1513 1514 android::SoftOMXComponent *createSoftOMXComponent( 1515 const char *name, const OMX_CALLBACKTYPE *callbacks, 1516 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 1517 return new android::SoftAVC(name, callbacks, appData, component); 1518 } 1519