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 41namespace android { 42 43 #define ive_api_function ih264e_api_function 44 45template<class T> 46static 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 54struct LevelConversion { 55 OMX_VIDEO_AVCLEVELTYPE omxLevel; 56 WORD32 avcLevel; 57}; 58 59static 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 78static 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 108static 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 121static 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 136static 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 152SoftAVC::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 186SoftAVC::~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 194void 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 225OMX_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 250OMX_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 273OMX_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 299OMX_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 326OMX_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 350OMX_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 374OMX_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 408OMX_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 433OMX_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 458OMX_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 483OMX_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 514OMX_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 540OMX_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 565OMX_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 590void 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 613OMX_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 881OMX_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 925OMX_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 984OMX_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 / avcType->nPFrames; 1020 } 1021 1022 mIInterval = avcType->nPFrames + avcType->nBFrames; 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 return OMX_ErrorUndefined; 1037 } 1038 1039 if (OK != ConvertOmxAvcLevelToAvcSpecLevel(avcType->eLevel, &mAVCEncLevel)) { 1040 return OMX_ErrorUndefined; 1041 } 1042 1043 return OMX_ErrorNone; 1044 } 1045 1046 default: 1047 return SoftVideoEncoderOMXComponent::internalSetParameter(index, params); 1048 } 1049} 1050 1051OMX_ERRORTYPE SoftAVC::getConfig( 1052 OMX_INDEXTYPE index, OMX_PTR _params) { 1053 switch ((int)index) { 1054 case OMX_IndexConfigAndroidIntraRefresh: 1055 { 1056 OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE *intraRefreshParams = 1057 (OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE *)_params; 1058 1059 if (!isValidOMXParam(intraRefreshParams)) { 1060 return OMX_ErrorBadParameter; 1061 } 1062 1063 if (intraRefreshParams->nPortIndex != kOutputPortIndex) { 1064 return OMX_ErrorUndefined; 1065 } 1066 1067 intraRefreshParams->nRefreshPeriod = 1068 (mAIRMode == IVE_AIR_MODE_NONE) ? 0 : mAIRRefreshPeriod; 1069 return OMX_ErrorNone; 1070 } 1071 1072 default: 1073 return SoftVideoEncoderOMXComponent::getConfig(index, _params); 1074 } 1075} 1076 1077OMX_ERRORTYPE SoftAVC::setConfig( 1078 OMX_INDEXTYPE index, const OMX_PTR _params) { 1079 switch ((int)index) { 1080 case OMX_IndexConfigVideoIntraVOPRefresh: 1081 { 1082 OMX_CONFIG_INTRAREFRESHVOPTYPE *params = 1083 (OMX_CONFIG_INTRAREFRESHVOPTYPE *)_params; 1084 1085 if (!isValidOMXParam(params)) { 1086 return OMX_ErrorBadParameter; 1087 } 1088 1089 if (params->nPortIndex != kOutputPortIndex) { 1090 return OMX_ErrorBadPortIndex; 1091 } 1092 1093 if (params->IntraRefreshVOP) { 1094 mUpdateFlag |= kRequestKeyFrame; 1095 } 1096 return OMX_ErrorNone; 1097 } 1098 1099 case OMX_IndexConfigVideoBitrate: 1100 { 1101 OMX_VIDEO_CONFIG_BITRATETYPE *params = 1102 (OMX_VIDEO_CONFIG_BITRATETYPE *)_params; 1103 1104 if (!isValidOMXParam(params)) { 1105 return OMX_ErrorBadParameter; 1106 } 1107 1108 if (params->nPortIndex != kOutputPortIndex) { 1109 return OMX_ErrorBadPortIndex; 1110 } 1111 1112 if (mBitrate != params->nEncodeBitrate) { 1113 mBitrate = params->nEncodeBitrate; 1114 mUpdateFlag |= kUpdateBitrate; 1115 } 1116 return OMX_ErrorNone; 1117 } 1118 1119 case OMX_IndexConfigAndroidIntraRefresh: 1120 { 1121 const OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE *intraRefreshParams = 1122 (const OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE *)_params; 1123 1124 if (!isValidOMXParam(intraRefreshParams)) { 1125 return OMX_ErrorBadParameter; 1126 } 1127 1128 if (intraRefreshParams->nPortIndex != kOutputPortIndex) { 1129 return OMX_ErrorUndefined; 1130 } 1131 1132 if (intraRefreshParams->nRefreshPeriod == 0) { 1133 mAIRMode = IVE_AIR_MODE_NONE; 1134 mAIRRefreshPeriod = 0; 1135 } else if (intraRefreshParams->nRefreshPeriod > 0) { 1136 mAIRMode = IVE_AIR_MODE_CYCLIC; 1137 mAIRRefreshPeriod = intraRefreshParams->nRefreshPeriod; 1138 } 1139 mUpdateFlag |= kUpdateAIRMode; 1140 return OMX_ErrorNone; 1141 } 1142 1143 default: 1144 return SimpleSoftOMXComponent::setConfig(index, _params); 1145 } 1146} 1147 1148OMX_ERRORTYPE SoftAVC::internalSetBitrateParams( 1149 const OMX_VIDEO_PARAM_BITRATETYPE *bitrate) { 1150 if (bitrate->nPortIndex != kOutputPortIndex) { 1151 return OMX_ErrorUnsupportedIndex; 1152 } 1153 1154 mBitrate = bitrate->nTargetBitrate; 1155 mUpdateFlag |= kUpdateBitrate; 1156 1157 return OMX_ErrorNone; 1158} 1159 1160OMX_ERRORTYPE SoftAVC::setEncodeArgs( 1161 ive_video_encode_ip_t *ps_encode_ip, 1162 ive_video_encode_op_t *ps_encode_op, 1163 OMX_BUFFERHEADERTYPE *inputBufferHeader, 1164 OMX_BUFFERHEADERTYPE *outputBufferHeader) { 1165 iv_raw_buf_t *ps_inp_raw_buf; 1166 const uint8_t *source; 1167 UWORD8 *pu1_buf; 1168 1169 ps_inp_raw_buf = &ps_encode_ip->s_inp_buf; 1170 ps_encode_ip->s_out_buf.pv_buf = outputBufferHeader->pBuffer; 1171 ps_encode_ip->s_out_buf.u4_bytes = 0; 1172 ps_encode_ip->s_out_buf.u4_bufsize = outputBufferHeader->nAllocLen; 1173 ps_encode_ip->u4_size = sizeof(ive_video_encode_ip_t); 1174 ps_encode_op->u4_size = sizeof(ive_video_encode_op_t); 1175 1176 ps_encode_ip->e_cmd = IVE_CMD_VIDEO_ENCODE; 1177 ps_encode_ip->pv_bufs = NULL; 1178 ps_encode_ip->pv_mb_info = NULL; 1179 ps_encode_ip->pv_pic_info = NULL; 1180 ps_encode_ip->u4_mb_info_type = 0; 1181 ps_encode_ip->u4_pic_info_type = 0; 1182 ps_encode_op->s_out_buf.pv_buf = NULL; 1183 1184 /* Initialize color formats */ 1185 ps_inp_raw_buf->e_color_fmt = mIvVideoColorFormat; 1186 source = NULL; 1187 if ((inputBufferHeader != NULL) && inputBufferHeader->nFilledLen) { 1188 source = inputBufferHeader->pBuffer + inputBufferHeader->nOffset; 1189 1190 if (mInputDataIsMeta) { 1191 uint8_t *conversionBuffer = NULL; 1192 for (size_t i = 0; i < MAX_CONVERSION_BUFFERS; i++) { 1193 if (mConversionBuffersFree[i]) { 1194 mConversionBuffersFree[i] = 0; 1195 conversionBuffer = mConversionBuffers[i]; 1196 break; 1197 } 1198 } 1199 1200 if (NULL == conversionBuffer) { 1201 ALOGE("No free buffers to hold conversion data"); 1202 return OMX_ErrorUndefined; 1203 } 1204 1205 source = extractGraphicBuffer( 1206 conversionBuffer, (mWidth * mHeight * 3 / 2), source, 1207 inputBufferHeader->nFilledLen, mWidth, mHeight); 1208 1209 if (source == NULL) { 1210 ALOGE("Error in extractGraphicBuffer"); 1211 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 1212 return OMX_ErrorUndefined; 1213 } 1214 } 1215 ps_encode_ip->u4_is_last = 0; 1216 ps_encode_ip->u4_timestamp_high = (inputBufferHeader->nTimeStamp) >> 32; 1217 ps_encode_ip->u4_timestamp_low = (inputBufferHeader->nTimeStamp) & 0xFFFFFFFF; 1218 } 1219 else { 1220 if (mSawInputEOS){ 1221 ps_encode_ip->u4_is_last = 1; 1222 } 1223 memset(ps_inp_raw_buf, 0, sizeof(iv_raw_buf_t)); 1224 ps_inp_raw_buf->e_color_fmt = mIvVideoColorFormat; 1225 ps_inp_raw_buf->u4_size = sizeof(iv_raw_buf_t); 1226 return OMX_ErrorNone; 1227 } 1228 1229 pu1_buf = (UWORD8 *)source; 1230 switch (mIvVideoColorFormat) { 1231 case IV_YUV_420P: 1232 { 1233 ps_inp_raw_buf->apv_bufs[0] = pu1_buf; 1234 pu1_buf += (mStride) * mHeight; 1235 ps_inp_raw_buf->apv_bufs[1] = pu1_buf; 1236 pu1_buf += (mStride / 2) * mHeight / 2; 1237 ps_inp_raw_buf->apv_bufs[2] = pu1_buf; 1238 1239 ps_inp_raw_buf->au4_wd[0] = mWidth; 1240 ps_inp_raw_buf->au4_wd[1] = mWidth / 2; 1241 ps_inp_raw_buf->au4_wd[2] = mWidth / 2; 1242 1243 ps_inp_raw_buf->au4_ht[0] = mHeight; 1244 ps_inp_raw_buf->au4_ht[1] = mHeight / 2; 1245 ps_inp_raw_buf->au4_ht[2] = mHeight / 2; 1246 1247 ps_inp_raw_buf->au4_strd[0] = mStride; 1248 ps_inp_raw_buf->au4_strd[1] = (mStride / 2); 1249 ps_inp_raw_buf->au4_strd[2] = (mStride / 2); 1250 break; 1251 } 1252 1253 case IV_YUV_422ILE: 1254 { 1255 ps_inp_raw_buf->apv_bufs[0] = pu1_buf; 1256 ps_inp_raw_buf->au4_wd[0] = mWidth * 2; 1257 ps_inp_raw_buf->au4_ht[0] = mHeight; 1258 ps_inp_raw_buf->au4_strd[0] = mStride * 2; 1259 break; 1260 } 1261 1262 case IV_YUV_420SP_UV: 1263 case IV_YUV_420SP_VU: 1264 default: 1265 { 1266 ps_inp_raw_buf->apv_bufs[0] = pu1_buf; 1267 pu1_buf += (mStride) * mHeight; 1268 ps_inp_raw_buf->apv_bufs[1] = pu1_buf; 1269 1270 ps_inp_raw_buf->au4_wd[0] = mWidth; 1271 ps_inp_raw_buf->au4_wd[1] = mWidth; 1272 1273 ps_inp_raw_buf->au4_ht[0] = mHeight; 1274 ps_inp_raw_buf->au4_ht[1] = mHeight / 2; 1275 1276 ps_inp_raw_buf->au4_strd[0] = mStride; 1277 ps_inp_raw_buf->au4_strd[1] = mStride; 1278 break; 1279 } 1280 } 1281 return OMX_ErrorNone; 1282} 1283 1284void SoftAVC::onQueueFilled(OMX_U32 portIndex) { 1285 IV_STATUS_T status; 1286 WORD32 timeDelay, timeTaken; 1287 1288 UNUSED(portIndex); 1289 1290 // Initialize encoder if not already initialized 1291 if (mCodecCtx == NULL) { 1292 if (OMX_ErrorNone != initEncoder()) { 1293 ALOGE("Failed to initialize encoder"); 1294 notify(OMX_EventError, OMX_ErrorUndefined, 0 /* arg2 */, NULL /* data */); 1295 return; 1296 } 1297 } 1298 if (mSignalledError) { 1299 return; 1300 } 1301 1302 List<BufferInfo *> &inQueue = getPortQueue(0); 1303 List<BufferInfo *> &outQueue = getPortQueue(1); 1304 1305 while (!mSawOutputEOS && !outQueue.empty()) { 1306 1307 OMX_ERRORTYPE error; 1308 ive_video_encode_ip_t s_encode_ip; 1309 ive_video_encode_op_t s_encode_op; 1310 BufferInfo *outputBufferInfo = *outQueue.begin(); 1311 OMX_BUFFERHEADERTYPE *outputBufferHeader = outputBufferInfo->mHeader; 1312 1313 BufferInfo *inputBufferInfo; 1314 OMX_BUFFERHEADERTYPE *inputBufferHeader; 1315 1316 if (mSawInputEOS) { 1317 inputBufferHeader = NULL; 1318 inputBufferInfo = NULL; 1319 } else if (!inQueue.empty()) { 1320 inputBufferInfo = *inQueue.begin(); 1321 inputBufferHeader = inputBufferInfo->mHeader; 1322 } else { 1323 return; 1324 } 1325 1326 outputBufferHeader->nTimeStamp = 0; 1327 outputBufferHeader->nFlags = 0; 1328 outputBufferHeader->nOffset = 0; 1329 outputBufferHeader->nFilledLen = 0; 1330 outputBufferHeader->nOffset = 0; 1331 1332 if (inputBufferHeader != NULL) { 1333 outputBufferHeader->nFlags = inputBufferHeader->nFlags; 1334 } 1335 1336 uint8_t *outPtr = (uint8_t *)outputBufferHeader->pBuffer; 1337 1338 if (!mSpsPpsHeaderReceived) { 1339 error = setEncodeArgs(&s_encode_ip, &s_encode_op, NULL, outputBufferHeader); 1340 if (error != OMX_ErrorNone) { 1341 mSignalledError = true; 1342 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 1343 return; 1344 } 1345 status = ive_api_function(mCodecCtx, &s_encode_ip, &s_encode_op); 1346 1347 if (IV_SUCCESS != status) { 1348 ALOGE("Encode Frame failed = 0x%x\n", 1349 s_encode_op.u4_error_code); 1350 } else { 1351 ALOGV("Bytes Generated in header %d\n", 1352 s_encode_op.s_out_buf.u4_bytes); 1353 } 1354 1355 mSpsPpsHeaderReceived = true; 1356 1357 outputBufferHeader->nFlags = OMX_BUFFERFLAG_CODECCONFIG; 1358 outputBufferHeader->nFilledLen = s_encode_op.s_out_buf.u4_bytes; 1359 if (inputBufferHeader != NULL) { 1360 outputBufferHeader->nTimeStamp = inputBufferHeader->nTimeStamp; 1361 } 1362 1363 outQueue.erase(outQueue.begin()); 1364 outputBufferInfo->mOwnedByUs = false; 1365 1366 DUMP_TO_FILE( 1367 mOutFile, outputBufferHeader->pBuffer, 1368 outputBufferHeader->nFilledLen); 1369 notifyFillBufferDone(outputBufferHeader); 1370 1371 setEncMode(IVE_ENC_MODE_PICTURE); 1372 return; 1373 } 1374 1375 if (mUpdateFlag) { 1376 if (mUpdateFlag & kUpdateBitrate) { 1377 setBitRate(); 1378 } 1379 if (mUpdateFlag & kRequestKeyFrame) { 1380 setFrameType(IV_IDR_FRAME); 1381 } 1382 if (mUpdateFlag & kUpdateAIRMode) { 1383 setAirParams(); 1384 notify(OMX_EventPortSettingsChanged, kOutputPortIndex, 1385 OMX_IndexConfigAndroidIntraRefresh, NULL); 1386 } 1387 mUpdateFlag = 0; 1388 } 1389 1390 if ((inputBufferHeader != NULL) 1391 && (inputBufferHeader->nFlags & OMX_BUFFERFLAG_EOS)) { 1392 mSawInputEOS = true; 1393 } 1394 1395 /* In normal mode, store inputBufferInfo and this will be returned 1396 when encoder consumes this input */ 1397 if (!mInputDataIsMeta && (inputBufferInfo != NULL)) { 1398 for (size_t i = 0; i < MAX_INPUT_BUFFER_HEADERS; i++) { 1399 if (NULL == mInputBufferInfo[i]) { 1400 mInputBufferInfo[i] = inputBufferInfo; 1401 break; 1402 } 1403 } 1404 } 1405 error = setEncodeArgs( 1406 &s_encode_ip, &s_encode_op, inputBufferHeader, outputBufferHeader); 1407 1408 if (error != OMX_ErrorNone) { 1409 mSignalledError = true; 1410 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 1411 return; 1412 } 1413 1414 DUMP_TO_FILE( 1415 mInFile, s_encode_ip.s_inp_buf.apv_bufs[0], 1416 (mHeight * mStride * 3 / 2)); 1417 1418 GETTIME(&mTimeStart, NULL); 1419 /* Compute time elapsed between end of previous decode() 1420 * to start of current decode() */ 1421 TIME_DIFF(mTimeEnd, mTimeStart, timeDelay); 1422 status = ive_api_function(mCodecCtx, &s_encode_ip, &s_encode_op); 1423 1424 if (IV_SUCCESS != status) { 1425 ALOGE("Encode Frame failed = 0x%x\n", 1426 s_encode_op.u4_error_code); 1427 mSignalledError = true; 1428 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 1429 return; 1430 } 1431 1432 GETTIME(&mTimeEnd, NULL); 1433 /* Compute time taken for decode() */ 1434 TIME_DIFF(mTimeStart, mTimeEnd, timeTaken); 1435 1436 ALOGV("timeTaken=%6d delay=%6d numBytes=%6d", timeTaken, timeDelay, 1437 s_encode_op.s_out_buf.u4_bytes); 1438 1439 /* In encoder frees up an input buffer, mark it as free */ 1440 if (s_encode_op.s_inp_buf.apv_bufs[0] != NULL) { 1441 if (mInputDataIsMeta) { 1442 for (size_t i = 0; i < MAX_CONVERSION_BUFFERS; i++) { 1443 if (mConversionBuffers[i] == s_encode_op.s_inp_buf.apv_bufs[0]) { 1444 mConversionBuffersFree[i] = 1; 1445 break; 1446 } 1447 } 1448 } else { 1449 /* In normal mode, call EBD on inBuffeHeader that is freed by the codec */ 1450 for (size_t i = 0; i < MAX_INPUT_BUFFER_HEADERS; i++) { 1451 uint8_t *buf = NULL; 1452 OMX_BUFFERHEADERTYPE *bufHdr = NULL; 1453 if (mInputBufferInfo[i] != NULL) { 1454 bufHdr = mInputBufferInfo[i]->mHeader; 1455 buf = bufHdr->pBuffer + bufHdr->nOffset; 1456 } 1457 if (s_encode_op.s_inp_buf.apv_bufs[0] == buf) { 1458 mInputBufferInfo[i]->mOwnedByUs = false; 1459 notifyEmptyBufferDone(bufHdr); 1460 mInputBufferInfo[i] = NULL; 1461 break; 1462 } 1463 } 1464 } 1465 } 1466 1467 outputBufferHeader->nFilledLen = s_encode_op.s_out_buf.u4_bytes; 1468 1469 if (IV_IDR_FRAME == s_encode_op.u4_encoded_frame_type) { 1470 outputBufferHeader->nFlags |= OMX_BUFFERFLAG_SYNCFRAME; 1471 } 1472 1473 if (inputBufferHeader != NULL) { 1474 inQueue.erase(inQueue.begin()); 1475 1476 /* If in meta data, call EBD on input */ 1477 /* In case of normal mode, EBD will be done once encoder 1478 releases the input buffer */ 1479 if (mInputDataIsMeta) { 1480 inputBufferInfo->mOwnedByUs = false; 1481 notifyEmptyBufferDone(inputBufferHeader); 1482 } 1483 } 1484 1485 if (s_encode_op.u4_is_last) { 1486 outputBufferHeader->nFlags |= OMX_BUFFERFLAG_EOS; 1487 mSawOutputEOS = true; 1488 } else { 1489 outputBufferHeader->nFlags &= ~OMX_BUFFERFLAG_EOS; 1490 } 1491 1492 if (outputBufferHeader->nFilledLen || s_encode_op.u4_is_last) { 1493 outputBufferHeader->nTimeStamp = s_encode_op.u4_timestamp_high; 1494 outputBufferHeader->nTimeStamp <<= 32; 1495 outputBufferHeader->nTimeStamp |= s_encode_op.u4_timestamp_low; 1496 outputBufferInfo->mOwnedByUs = false; 1497 outQueue.erase(outQueue.begin()); 1498 DUMP_TO_FILE(mOutFile, outputBufferHeader->pBuffer, 1499 outputBufferHeader->nFilledLen); 1500 notifyFillBufferDone(outputBufferHeader); 1501 } 1502 1503 if (s_encode_op.u4_is_last == 1) { 1504 return; 1505 } 1506 } 1507 return; 1508} 1509 1510} // namespace android 1511 1512android::SoftOMXComponent *createSoftOMXComponent( 1513 const char *name, const OMX_CALLBACKTYPE *callbacks, 1514 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 1515 return new android::SoftAVC(name, callbacks, appData, component); 1516} 1517