SoftAVCEncoder.cpp revision 96a09849ee1729f6aa69da9e60f6b4556c898057
1/* 2 * Copyright (C) 2012 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 "SoftAVCEncoder" 19#include <utils/Log.h> 20#include <utils/misc.h> 21 22#include "avcenc_api.h" 23#include "avcenc_int.h" 24#include "OMX_Video.h" 25 26#include <HardwareAPI.h> 27#include <MetadataBufferType.h> 28#include <media/stagefright/foundation/ADebug.h> 29#include <media/stagefright/foundation/AUtils.h> 30#include <media/stagefright/MediaDefs.h> 31#include <media/stagefright/MediaErrors.h> 32#include <media/stagefright/MetaData.h> 33#include <media/stagefright/Utils.h> 34#include <ui/Rect.h> 35#include <ui/GraphicBufferMapper.h> 36 37#include "SoftAVCEncoder.h" 38 39#if LOG_NDEBUG 40#define UNUSED_UNLESS_VERBOSE(x) (void)(x) 41#else 42#define UNUSED_UNLESS_VERBOSE(x) 43#endif 44 45namespace android { 46 47template<class T> 48static void InitOMXParams(T *params) { 49 params->nSize = sizeof(T); 50 params->nVersion.s.nVersionMajor = 1; 51 params->nVersion.s.nVersionMinor = 0; 52 params->nVersion.s.nRevision = 0; 53 params->nVersion.s.nStep = 0; 54} 55 56static const CodecProfileLevel kProfileLevels[] = { 57 { OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel2 }, 58}; 59 60typedef struct LevelConversion { 61 OMX_U32 omxLevel; 62 AVCLevel avcLevel; 63 uint32_t maxMacroBlocks; 64} LevelConcersion; 65 66static LevelConversion ConversionTable[] = { 67 { OMX_VIDEO_AVCLevel1, AVC_LEVEL1_B, 99 }, 68 { OMX_VIDEO_AVCLevel1b, AVC_LEVEL1, 99 }, 69 { OMX_VIDEO_AVCLevel11, AVC_LEVEL1_1, 396 }, 70 { OMX_VIDEO_AVCLevel12, AVC_LEVEL1_2, 396 }, 71 { OMX_VIDEO_AVCLevel13, AVC_LEVEL1_3, 396 }, 72 { OMX_VIDEO_AVCLevel2, AVC_LEVEL2, 396 }, 73#if 0 74 // encoding speed is very poor if video resolution 75 // is higher than CIF or if level is higher than 2 76 { OMX_VIDEO_AVCLevel21, AVC_LEVEL2_1, 792 }, 77 { OMX_VIDEO_AVCLevel22, AVC_LEVEL2_2, 1620 }, 78 { OMX_VIDEO_AVCLevel3, AVC_LEVEL3, 1620 }, 79 { OMX_VIDEO_AVCLevel31, AVC_LEVEL3_1, 3600 }, 80 { OMX_VIDEO_AVCLevel32, AVC_LEVEL3_2, 5120 }, 81 { OMX_VIDEO_AVCLevel4, AVC_LEVEL4, 8192 }, 82 { OMX_VIDEO_AVCLevel41, AVC_LEVEL4_1, 8192 }, 83 { OMX_VIDEO_AVCLevel42, AVC_LEVEL4_2, 8704 }, 84 { OMX_VIDEO_AVCLevel5, AVC_LEVEL5, 22080 }, 85 { OMX_VIDEO_AVCLevel51, AVC_LEVEL5_1, 36864 }, 86#endif 87}; 88 89static status_t ConvertOmxAvcLevelToAvcSpecLevel( 90 OMX_U32 omxLevel, AVCLevel *avcLevel) { 91 for (size_t i = 0, n = sizeof(ConversionTable)/sizeof(ConversionTable[0]); 92 i < n; ++i) { 93 if (omxLevel == ConversionTable[i].omxLevel) { 94 *avcLevel = ConversionTable[i].avcLevel; 95 return OK; 96 } 97 } 98 99 ALOGE("ConvertOmxAvcLevelToAvcSpecLevel: %d level not supported", 100 (int32_t)omxLevel); 101 102 return BAD_VALUE; 103} 104 105static status_t ConvertAvcSpecLevelToOmxAvcLevel( 106 AVCLevel avcLevel, OMX_U32 *omxLevel) { 107 for (size_t i = 0, n = sizeof(ConversionTable)/sizeof(ConversionTable[0]); 108 i < n; ++i) { 109 if (avcLevel == ConversionTable[i].avcLevel) { 110 *omxLevel = ConversionTable[i].omxLevel; 111 return OK; 112 } 113 } 114 115 ALOGE("ConvertAvcSpecLevelToOmxAvcLevel: %d level not supported", 116 (int32_t) avcLevel); 117 118 return BAD_VALUE; 119} 120 121static void* MallocWrapper( 122 void * /* userData */, int32_t size, int32_t /* attrs */) { 123 void *ptr = malloc(size); 124 if (ptr) 125 memset(ptr, 0, size); 126 return ptr; 127} 128 129static void FreeWrapper(void * /* userData */, void* ptr) { 130 free(ptr); 131} 132 133static int32_t DpbAllocWrapper(void *userData, 134 unsigned int sizeInMbs, unsigned int numBuffers) { 135 SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData); 136 CHECK(encoder != NULL); 137 return encoder->allocOutputBuffers(sizeInMbs, numBuffers); 138} 139 140static int32_t BindFrameWrapper( 141 void *userData, int32_t index, uint8_t **yuv) { 142 SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData); 143 CHECK(encoder != NULL); 144 return encoder->bindOutputBuffer(index, yuv); 145} 146 147static void UnbindFrameWrapper(void *userData, int32_t index) { 148 SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData); 149 CHECK(encoder != NULL); 150 return encoder->unbindOutputBuffer(index); 151} 152 153SoftAVCEncoder::SoftAVCEncoder( 154 const char *name, 155 const OMX_CALLBACKTYPE *callbacks, 156 OMX_PTR appData, 157 OMX_COMPONENTTYPE **component) 158 : SoftVideoEncoderOMXComponent( 159 name, "video_encoder.avc", OMX_VIDEO_CodingAVC, 160 kProfileLevels, NELEM(kProfileLevels), 161 176 /* width */, 144 /* height */, 162 callbacks, appData, component), 163 mIDRFrameRefreshIntervalInSec(1), 164 mAVCEncProfile(AVC_BASELINE), 165 mAVCEncLevel(AVC_LEVEL2), 166 mNumInputFrames(-1), 167 mPrevTimestampUs(-1), 168 mStarted(false), 169 mSawInputEOS(false), 170 mSignalledError(false), 171 mHandle(new tagAVCHandle), 172 mEncParams(new tagAVCEncParam), 173 mInputFrameData(NULL), 174 mSliceGroup(NULL) { 175 176 const size_t kOutputBufferSize = 177 320 * ConversionTable[NELEM(ConversionTable) - 1].maxMacroBlocks; 178 179 initPorts( 180 kNumBuffers, kNumBuffers, kOutputBufferSize, 181 MEDIA_MIMETYPE_VIDEO_AVC, 2 /* minCompressionRatio */); 182 183 ALOGI("Construct SoftAVCEncoder"); 184} 185 186SoftAVCEncoder::~SoftAVCEncoder() { 187 ALOGV("Destruct SoftAVCEncoder"); 188 releaseEncoder(); 189 List<BufferInfo *> &outQueue = getPortQueue(1); 190 List<BufferInfo *> &inQueue = getPortQueue(0); 191 CHECK(outQueue.empty()); 192 CHECK(inQueue.empty()); 193} 194 195OMX_ERRORTYPE SoftAVCEncoder::initEncParams() { 196 CHECK(mHandle != NULL); 197 memset(mHandle, 0, sizeof(tagAVCHandle)); 198 mHandle->AVCObject = NULL; 199 mHandle->userData = this; 200 mHandle->CBAVC_DPBAlloc = DpbAllocWrapper; 201 mHandle->CBAVC_FrameBind = BindFrameWrapper; 202 mHandle->CBAVC_FrameUnbind = UnbindFrameWrapper; 203 mHandle->CBAVC_Malloc = MallocWrapper; 204 mHandle->CBAVC_Free = FreeWrapper; 205 206 CHECK(mEncParams != NULL); 207 memset(mEncParams, 0, sizeof(*mEncParams)); 208 mEncParams->rate_control = AVC_ON; 209 mEncParams->initQP = 0; 210 mEncParams->init_CBP_removal_delay = 1600; 211 212 mEncParams->intramb_refresh = 0; 213 mEncParams->auto_scd = AVC_ON; 214 mEncParams->out_of_band_param_set = AVC_ON; 215 mEncParams->poc_type = 2; 216 mEncParams->log2_max_poc_lsb_minus_4 = 12; 217 mEncParams->delta_poc_zero_flag = 0; 218 mEncParams->offset_poc_non_ref = 0; 219 mEncParams->offset_top_bottom = 0; 220 mEncParams->num_ref_in_cycle = 0; 221 mEncParams->offset_poc_ref = NULL; 222 223 mEncParams->num_ref_frame = 1; 224 mEncParams->num_slice_group = 1; 225 mEncParams->fmo_type = 0; 226 227 mEncParams->db_filter = AVC_ON; 228 mEncParams->disable_db_idc = 0; 229 230 mEncParams->alpha_offset = 0; 231 mEncParams->beta_offset = 0; 232 mEncParams->constrained_intra_pred = AVC_OFF; 233 234 mEncParams->data_par = AVC_OFF; 235 mEncParams->fullsearch = AVC_OFF; 236 mEncParams->search_range = 16; 237 mEncParams->sub_pel = AVC_OFF; 238 mEncParams->submb_pred = AVC_OFF; 239 mEncParams->rdopt_mode = AVC_OFF; 240 mEncParams->bidir_pred = AVC_OFF; 241 242 mEncParams->use_overrun_buffer = AVC_OFF; 243 244 if (mColorFormat != OMX_COLOR_FormatYUV420Planar || mInputDataIsMeta) { 245 // Color conversion is needed. 246 free(mInputFrameData); 247 if (((uint64_t)mVideoWidth * mVideoHeight) > ((uint64_t)INT32_MAX / 3)) { 248 ALOGE("Buffer size is too big."); 249 return OMX_ErrorUndefined; 250 } 251 mInputFrameData = 252 (uint8_t *) malloc((mWidth * mHeight * 3 ) >> 1); 253 CHECK(mInputFrameData != NULL); 254 } 255 256 // PV's AVC encoder requires the video dimension of multiple 257 if (mWidth % 16 != 0 || mHeight % 16 != 0) { 258 ALOGE("Video frame size %dx%d must be a multiple of 16", 259 mWidth, mHeight); 260 return OMX_ErrorBadParameter; 261 } 262 263 mEncParams->width = mWidth; 264 mEncParams->height = mHeight; 265 mEncParams->bitrate = mBitrate; 266 mEncParams->frame_rate = (1000 * mFramerate) >> 16; // In frames/ms!, mFramerate is in Q16 267 mEncParams->CPB_size = (uint32_t) (mBitrate >> 1); 268 269 int32_t nMacroBlocks = divUp(mWidth, 16) * divUp(mHeight, 16); 270 CHECK(mSliceGroup == NULL); 271 if (nMacroBlocks > SIZE_MAX / sizeof(uint32_t)) { 272 ALOGE("requested memory size is too big."); 273 return OMX_ErrorUndefined; 274 } 275 mSliceGroup = (uint32_t *) malloc(sizeof(uint32_t) * nMacroBlocks); 276 CHECK(mSliceGroup != NULL); 277 for (int ii = 0, idx = 0; ii < nMacroBlocks; ++ii) { 278 mSliceGroup[ii] = idx++; 279 if (idx >= mEncParams->num_slice_group) { 280 idx = 0; 281 } 282 } 283 mEncParams->slice_group = mSliceGroup; 284 285 // Set IDR frame refresh interval 286 if (mIDRFrameRefreshIntervalInSec < 0) { 287 mEncParams->idr_period = -1; 288 } else if (mIDRFrameRefreshIntervalInSec == 0) { 289 mEncParams->idr_period = 1; // All I frames 290 } else { 291 mEncParams->idr_period = 292 (mIDRFrameRefreshIntervalInSec * mFramerate) >> 16; // mFramerate is in Q16 293 } 294 295 // Set profile and level 296 mEncParams->profile = mAVCEncProfile; 297 mEncParams->level = mAVCEncLevel; 298 299 return OMX_ErrorNone; 300} 301 302OMX_ERRORTYPE SoftAVCEncoder::initEncoder() { 303 CHECK(!mStarted); 304 305 OMX_ERRORTYPE errType = OMX_ErrorNone; 306 if (OMX_ErrorNone != (errType = initEncParams())) { 307 ALOGE("Failed to initialized encoder params"); 308 mSignalledError = true; 309 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 310 return errType; 311 } 312 313 AVCEnc_Status err; 314 err = PVAVCEncInitialize(mHandle, mEncParams, NULL, NULL); 315 if (err != AVCENC_SUCCESS) { 316 ALOGE("Failed to initialize the encoder: %d", err); 317 mSignalledError = true; 318 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 319 return OMX_ErrorUndefined; 320 } 321 322 mNumInputFrames = -2; // 1st two buffers contain SPS and PPS 323 mSpsPpsHeaderReceived = false; 324 mReadyForNextFrame = true; 325 mIsIDRFrame = false; 326 mStarted = true; 327 328 return OMX_ErrorNone; 329} 330 331OMX_ERRORTYPE SoftAVCEncoder::releaseEncoder() { 332 if (!mStarted) { 333 return OMX_ErrorNone; 334 } 335 336 PVAVCCleanUpEncoder(mHandle); 337 releaseOutputBuffers(); 338 339 free(mInputFrameData); 340 mInputFrameData = NULL; 341 342 free(mSliceGroup); 343 mSliceGroup = NULL; 344 345 delete mEncParams; 346 mEncParams = NULL; 347 348 delete mHandle; 349 mHandle = NULL; 350 351 mStarted = false; 352 353 return OMX_ErrorNone; 354} 355 356void SoftAVCEncoder::releaseOutputBuffers() { 357 for (size_t i = 0; i < mOutputBuffers.size(); ++i) { 358 MediaBuffer *buffer = mOutputBuffers.editItemAt(i); 359 buffer->setObserver(NULL); 360 buffer->release(); 361 } 362 mOutputBuffers.clear(); 363} 364 365OMX_ERRORTYPE SoftAVCEncoder::internalGetParameter( 366 OMX_INDEXTYPE index, OMX_PTR params) { 367 switch (index) { 368 case OMX_IndexParamVideoBitrate: 369 { 370 OMX_VIDEO_PARAM_BITRATETYPE *bitRate = 371 (OMX_VIDEO_PARAM_BITRATETYPE *) params; 372 373 if (bitRate->nPortIndex != 1) { 374 return OMX_ErrorUndefined; 375 } 376 377 bitRate->eControlRate = OMX_Video_ControlRateVariable; 378 bitRate->nTargetBitrate = mBitrate; 379 return OMX_ErrorNone; 380 } 381 382 case OMX_IndexParamVideoAvc: 383 { 384 OMX_VIDEO_PARAM_AVCTYPE *avcParams = 385 (OMX_VIDEO_PARAM_AVCTYPE *)params; 386 387 if (avcParams->nPortIndex != 1) { 388 return OMX_ErrorUndefined; 389 } 390 391 avcParams->eProfile = OMX_VIDEO_AVCProfileBaseline; 392 OMX_U32 omxLevel = AVC_LEVEL2; 393 if (OMX_ErrorNone != 394 ConvertAvcSpecLevelToOmxAvcLevel(mAVCEncLevel, &omxLevel)) { 395 return OMX_ErrorUndefined; 396 } 397 398 avcParams->eLevel = (OMX_VIDEO_AVCLEVELTYPE) omxLevel; 399 avcParams->nRefFrames = 1; 400 avcParams->nBFrames = 0; 401 avcParams->bUseHadamard = OMX_TRUE; 402 avcParams->nAllowedPictureTypes = 403 (OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP); 404 avcParams->nRefIdx10ActiveMinus1 = 0; 405 avcParams->nRefIdx11ActiveMinus1 = 0; 406 avcParams->bWeightedPPrediction = OMX_FALSE; 407 avcParams->bEntropyCodingCABAC = OMX_FALSE; 408 avcParams->bconstIpred = OMX_FALSE; 409 avcParams->bDirect8x8Inference = OMX_FALSE; 410 avcParams->bDirectSpatialTemporal = OMX_FALSE; 411 avcParams->nCabacInitIdc = 0; 412 return OMX_ErrorNone; 413 } 414 415 default: 416 return SoftVideoEncoderOMXComponent::internalGetParameter(index, params); 417 } 418} 419 420OMX_ERRORTYPE SoftAVCEncoder::internalSetParameter( 421 OMX_INDEXTYPE index, const OMX_PTR params) { 422 int32_t indexFull = index; 423 424 switch (indexFull) { 425 case OMX_IndexParamVideoBitrate: 426 { 427 OMX_VIDEO_PARAM_BITRATETYPE *bitRate = 428 (OMX_VIDEO_PARAM_BITRATETYPE *) params; 429 430 if (bitRate->nPortIndex != 1 || 431 bitRate->eControlRate != OMX_Video_ControlRateVariable) { 432 return OMX_ErrorUndefined; 433 } 434 435 mBitrate = bitRate->nTargetBitrate; 436 return OMX_ErrorNone; 437 } 438 439 case OMX_IndexParamVideoAvc: 440 { 441 OMX_VIDEO_PARAM_AVCTYPE *avcType = 442 (OMX_VIDEO_PARAM_AVCTYPE *)params; 443 444 if (avcType->nPortIndex != 1) { 445 return OMX_ErrorUndefined; 446 } 447 448 // PV's AVC encoder only supports baseline profile 449 if (avcType->eProfile != OMX_VIDEO_AVCProfileBaseline || 450 avcType->nRefFrames != 1 || 451 avcType->nBFrames != 0 || 452 avcType->bUseHadamard != OMX_TRUE || 453 (avcType->nAllowedPictureTypes & OMX_VIDEO_PictureTypeB) != 0 || 454 avcType->nRefIdx10ActiveMinus1 != 0 || 455 avcType->nRefIdx11ActiveMinus1 != 0 || 456 avcType->bWeightedPPrediction != OMX_FALSE || 457 avcType->bEntropyCodingCABAC != OMX_FALSE || 458 avcType->bconstIpred != OMX_FALSE || 459 avcType->bDirect8x8Inference != OMX_FALSE || 460 avcType->bDirectSpatialTemporal != OMX_FALSE || 461 avcType->nCabacInitIdc != 0) { 462 return OMX_ErrorUndefined; 463 } 464 465 if (OK != ConvertOmxAvcLevelToAvcSpecLevel(avcType->eLevel, &mAVCEncLevel)) { 466 return OMX_ErrorUndefined; 467 } 468 469 return OMX_ErrorNone; 470 } 471 472 default: 473 return SoftVideoEncoderOMXComponent::internalSetParameter(index, params); 474 } 475} 476 477void SoftAVCEncoder::onQueueFilled(OMX_U32 /* portIndex */) { 478 if (mSignalledError || mSawInputEOS) { 479 return; 480 } 481 482 if (!mStarted) { 483 if (OMX_ErrorNone != initEncoder()) { 484 return; 485 } 486 } 487 488 List<BufferInfo *> &inQueue = getPortQueue(0); 489 List<BufferInfo *> &outQueue = getPortQueue(1); 490 491 while (!mSawInputEOS && !inQueue.empty() && !outQueue.empty()) { 492 BufferInfo *inInfo = *inQueue.begin(); 493 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 494 BufferInfo *outInfo = *outQueue.begin(); 495 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader; 496 497 outHeader->nTimeStamp = 0; 498 outHeader->nFlags = 0; 499 outHeader->nOffset = 0; 500 outHeader->nFilledLen = 0; 501 outHeader->nOffset = 0; 502 503 uint8_t *outPtr = (uint8_t *) outHeader->pBuffer; 504 uint32_t dataLength = outHeader->nAllocLen; 505 506 if (!mSpsPpsHeaderReceived && mNumInputFrames < 0) { 507 // 4 bytes are reserved for holding the start code 0x00000001 508 // of the sequence parameter set at the beginning. 509 outPtr += 4; 510 dataLength -= 4; 511 } 512 513 int32_t type; 514 AVCEnc_Status encoderStatus = AVCENC_SUCCESS; 515 516 // Combine SPS and PPS and place them in the very first output buffer 517 // SPS and PPS are separated by start code 0x00000001 518 // Assume that we have exactly one SPS and exactly one PPS. 519 while (!mSpsPpsHeaderReceived && mNumInputFrames <= 0) { 520 encoderStatus = PVAVCEncodeNAL(mHandle, outPtr, &dataLength, &type); 521 if (encoderStatus == AVCENC_WRONG_STATE) { 522 mSpsPpsHeaderReceived = true; 523 CHECK_EQ(0, mNumInputFrames); // 1st video frame is 0 524 outHeader->nFlags = OMX_BUFFERFLAG_CODECCONFIG; 525 outQueue.erase(outQueue.begin()); 526 outInfo->mOwnedByUs = false; 527 notifyFillBufferDone(outHeader); 528 return; 529 } else { 530 switch (type) { 531 case AVC_NALTYPE_SPS: 532 ++mNumInputFrames; 533 memcpy((uint8_t *)outHeader->pBuffer, "\x00\x00\x00\x01", 4); 534 outHeader->nFilledLen = 4 + dataLength; 535 outPtr += (dataLength + 4); // 4 bytes for next start code 536 dataLength = outHeader->nAllocLen - outHeader->nFilledLen; 537 break; 538 default: 539 CHECK_EQ(AVC_NALTYPE_PPS, type); 540 ++mNumInputFrames; 541 memcpy((uint8_t *) outHeader->pBuffer + outHeader->nFilledLen, 542 "\x00\x00\x00\x01", 4); 543 outHeader->nFilledLen += (dataLength + 4); 544 outPtr += (dataLength + 4); 545 break; 546 } 547 } 548 } 549 550 // Get next input video frame 551 if (mReadyForNextFrame) { 552 // Save the input buffer info so that it can be 553 // passed to an output buffer 554 InputBufferInfo info; 555 info.mTimeUs = inHeader->nTimeStamp; 556 info.mFlags = inHeader->nFlags; 557 mInputBufferInfoVec.push(info); 558 mPrevTimestampUs = inHeader->nTimeStamp; 559 560 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 561 mSawInputEOS = true; 562 } 563 564 if (inHeader->nFilledLen > 0) { 565 AVCFrameIO videoInput; 566 memset(&videoInput, 0, sizeof(videoInput)); 567 videoInput.height = align(mHeight, 16); 568 videoInput.pitch = align(mWidth, 16); 569 videoInput.coding_timestamp = (inHeader->nTimeStamp + 500) / 1000; // in ms 570 const uint8_t *inputData = NULL; 571 if (mInputDataIsMeta) { 572 inputData = 573 extractGraphicBuffer( 574 mInputFrameData, (mWidth * mHeight * 3) >> 1, 575 inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen, 576 mWidth, mHeight); 577 if (inputData == NULL) { 578 ALOGE("Unable to extract gralloc buffer in metadata mode"); 579 mSignalledError = true; 580 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 581 return; 582 } 583 // TODO: Verify/convert pixel format enum 584 } else { 585 inputData = (const uint8_t *)inHeader->pBuffer + inHeader->nOffset; 586 if (mColorFormat != OMX_COLOR_FormatYUV420Planar) { 587 ConvertYUV420SemiPlanarToYUV420Planar( 588 inputData, mInputFrameData, mWidth, mHeight); 589 inputData = mInputFrameData; 590 } 591 } 592 593 CHECK(inputData != NULL); 594 videoInput.YCbCr[0] = (uint8_t *)inputData; 595 videoInput.YCbCr[1] = videoInput.YCbCr[0] + videoInput.height * videoInput.pitch; 596 videoInput.YCbCr[2] = videoInput.YCbCr[1] + 597 ((videoInput.height * videoInput.pitch) >> 2); 598 videoInput.disp_order = mNumInputFrames; 599 600 encoderStatus = PVAVCEncSetInput(mHandle, &videoInput); 601 if (encoderStatus == AVCENC_SUCCESS || encoderStatus == AVCENC_NEW_IDR) { 602 mReadyForNextFrame = false; 603 ++mNumInputFrames; 604 if (encoderStatus == AVCENC_NEW_IDR) { 605 mIsIDRFrame = 1; 606 } 607 } else { 608 if (encoderStatus < AVCENC_SUCCESS) { 609 ALOGE("encoderStatus = %d at line %d", encoderStatus, __LINE__); 610 mSignalledError = true; 611 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 612 return; 613 } else { 614 ALOGV("encoderStatus = %d at line %d", encoderStatus, __LINE__); 615 inQueue.erase(inQueue.begin()); 616 inInfo->mOwnedByUs = false; 617 notifyEmptyBufferDone(inHeader); 618 return; 619 } 620 } 621 } 622 } 623 624 // Encode an input video frame 625 CHECK(encoderStatus == AVCENC_SUCCESS || encoderStatus == AVCENC_NEW_IDR); 626 dataLength = outHeader->nAllocLen; // Reset the output buffer length 627 if (inHeader->nFilledLen > 0) { 628 if (outHeader->nAllocLen >= 4) { 629 memcpy(outPtr, "\x00\x00\x00\x01", 4); 630 outPtr += 4; 631 dataLength -= 4; 632 } 633 encoderStatus = PVAVCEncodeNAL(mHandle, outPtr, &dataLength, &type); 634 dataLength = outPtr + dataLength - outHeader->pBuffer; 635 if (encoderStatus == AVCENC_SUCCESS) { 636 CHECK(NULL == PVAVCEncGetOverrunBuffer(mHandle)); 637 } else if (encoderStatus == AVCENC_PICTURE_READY) { 638 CHECK(NULL == PVAVCEncGetOverrunBuffer(mHandle)); 639 if (mIsIDRFrame) { 640 outHeader->nFlags |= OMX_BUFFERFLAG_SYNCFRAME; 641 mIsIDRFrame = false; 642 } 643 mReadyForNextFrame = true; 644 AVCFrameIO recon; 645 if (PVAVCEncGetRecon(mHandle, &recon) == AVCENC_SUCCESS) { 646 PVAVCEncReleaseRecon(mHandle, &recon); 647 } 648 } else { 649 dataLength = 0; 650 mReadyForNextFrame = true; 651 } 652 653 if (encoderStatus < AVCENC_SUCCESS) { 654 ALOGE("encoderStatus = %d at line %d", encoderStatus, __LINE__); 655 mSignalledError = true; 656 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 657 return; 658 } 659 } else { 660 dataLength = 0; 661 } 662 663 inQueue.erase(inQueue.begin()); 664 inInfo->mOwnedByUs = false; 665 notifyEmptyBufferDone(inHeader); 666 667 outQueue.erase(outQueue.begin()); 668 CHECK(!mInputBufferInfoVec.empty()); 669 InputBufferInfo *inputBufInfo = mInputBufferInfoVec.begin(); 670 outHeader->nTimeStamp = inputBufInfo->mTimeUs; 671 outHeader->nFlags |= (inputBufInfo->mFlags | OMX_BUFFERFLAG_ENDOFFRAME); 672 if (mSawInputEOS) { 673 outHeader->nFlags |= OMX_BUFFERFLAG_EOS; 674 } 675 outHeader->nFilledLen = dataLength; 676 outInfo->mOwnedByUs = false; 677 notifyFillBufferDone(outHeader); 678 mInputBufferInfoVec.erase(mInputBufferInfoVec.begin()); 679 } 680} 681 682int32_t SoftAVCEncoder::allocOutputBuffers( 683 unsigned int sizeInMbs, unsigned int numBuffers) { 684 CHECK(mOutputBuffers.isEmpty()); 685 size_t frameSize = (sizeInMbs << 7) * 3; 686 for (unsigned int i = 0; i < numBuffers; ++i) { 687 MediaBuffer *buffer = new MediaBuffer(frameSize); 688 buffer->setObserver(this); 689 mOutputBuffers.push(buffer); 690 } 691 692 return 1; 693} 694 695void SoftAVCEncoder::unbindOutputBuffer(int32_t index) { 696 CHECK(index >= 0); 697} 698 699int32_t SoftAVCEncoder::bindOutputBuffer(int32_t index, uint8_t **yuv) { 700 CHECK(index >= 0); 701 CHECK(index < (int32_t) mOutputBuffers.size()); 702 *yuv = (uint8_t *) mOutputBuffers[index]->data(); 703 704 return 1; 705} 706 707void SoftAVCEncoder::signalBufferReturned(MediaBuffer *buffer) { 708 UNUSED_UNLESS_VERBOSE(buffer); 709 ALOGV("signalBufferReturned: %p", buffer); 710} 711 712} // namespace android 713 714android::SoftOMXComponent *createSoftOMXComponent( 715 const char *name, const OMX_CALLBACKTYPE *callbacks, 716 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 717 return new android::SoftAVCEncoder(name, callbacks, appData, component); 718} 719