SoftAVCEncoder.cpp revision 2edda09a2ad1d112c52acd37d323f63f0a492d67
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 21#include "avcenc_api.h" 22#include "avcenc_int.h" 23#include "OMX_Video.h" 24 25#include <HardwareAPI.h> 26#include <MetadataBufferType.h> 27#include <media/stagefright/foundation/ADebug.h> 28#include <media/stagefright/MediaDefs.h> 29#include <media/stagefright/MediaErrors.h> 30#include <media/stagefright/MetaData.h> 31#include <media/stagefright/Utils.h> 32#include <ui/Rect.h> 33#include <ui/GraphicBufferMapper.h> 34 35#include "SoftAVCEncoder.h" 36 37#if LOG_NDEBUG 38#define UNUSED_UNLESS_VERBOSE(x) (void)(x) 39#else 40#define UNUSED_UNLESS_VERBOSE(x) 41#endif 42 43namespace android { 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 54typedef struct LevelConversion { 55 OMX_U32 omxLevel; 56 AVCLevel avcLevel; 57} LevelConcersion; 58 59static LevelConversion ConversionTable[] = { 60 { OMX_VIDEO_AVCLevel1, AVC_LEVEL1_B }, 61 { OMX_VIDEO_AVCLevel1b, AVC_LEVEL1 }, 62 { OMX_VIDEO_AVCLevel11, AVC_LEVEL1_1 }, 63 { OMX_VIDEO_AVCLevel12, AVC_LEVEL1_2 }, 64 { OMX_VIDEO_AVCLevel13, AVC_LEVEL1_3 }, 65 { OMX_VIDEO_AVCLevel2, AVC_LEVEL2 }, 66#if 0 67 // encoding speed is very poor if video 68 // resolution is higher than CIF 69 { OMX_VIDEO_AVCLevel21, AVC_LEVEL2_1 }, 70 { OMX_VIDEO_AVCLevel22, AVC_LEVEL2_2 }, 71 { OMX_VIDEO_AVCLevel3, AVC_LEVEL3 }, 72 { OMX_VIDEO_AVCLevel31, AVC_LEVEL3_1 }, 73 { OMX_VIDEO_AVCLevel32, AVC_LEVEL3_2 }, 74 { OMX_VIDEO_AVCLevel4, AVC_LEVEL4 }, 75 { OMX_VIDEO_AVCLevel41, AVC_LEVEL4_1 }, 76 { OMX_VIDEO_AVCLevel42, AVC_LEVEL4_2 }, 77 { OMX_VIDEO_AVCLevel5, AVC_LEVEL5 }, 78 { OMX_VIDEO_AVCLevel51, AVC_LEVEL5_1 }, 79#endif 80}; 81 82static status_t ConvertOmxAvcLevelToAvcSpecLevel( 83 OMX_U32 omxLevel, AVCLevel *avcLevel) { 84 for (size_t i = 0, n = sizeof(ConversionTable)/sizeof(ConversionTable[0]); 85 i < n; ++i) { 86 if (omxLevel == ConversionTable[i].omxLevel) { 87 *avcLevel = ConversionTable[i].avcLevel; 88 return OK; 89 } 90 } 91 92 ALOGE("ConvertOmxAvcLevelToAvcSpecLevel: %d level not supported", 93 (int32_t)omxLevel); 94 95 return BAD_VALUE; 96} 97 98static status_t ConvertAvcSpecLevelToOmxAvcLevel( 99 AVCLevel avcLevel, OMX_U32 *omxLevel) { 100 for (size_t i = 0, n = sizeof(ConversionTable)/sizeof(ConversionTable[0]); 101 i < n; ++i) { 102 if (avcLevel == ConversionTable[i].avcLevel) { 103 *omxLevel = ConversionTable[i].omxLevel; 104 return OK; 105 } 106 } 107 108 ALOGE("ConvertAvcSpecLevelToOmxAvcLevel: %d level not supported", 109 (int32_t) avcLevel); 110 111 return BAD_VALUE; 112} 113 114static void* MallocWrapper( 115 void * /* userData */, int32_t size, int32_t /* attrs */) { 116 void *ptr = malloc(size); 117 if (ptr) 118 memset(ptr, 0, size); 119 return ptr; 120} 121 122static void FreeWrapper(void * /* userData */, void* ptr) { 123 free(ptr); 124} 125 126static int32_t DpbAllocWrapper(void *userData, 127 unsigned int sizeInMbs, unsigned int numBuffers) { 128 SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData); 129 CHECK(encoder != NULL); 130 return encoder->allocOutputBuffers(sizeInMbs, numBuffers); 131} 132 133static int32_t BindFrameWrapper( 134 void *userData, int32_t index, uint8_t **yuv) { 135 SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData); 136 CHECK(encoder != NULL); 137 return encoder->bindOutputBuffer(index, yuv); 138} 139 140static void UnbindFrameWrapper(void *userData, int32_t index) { 141 SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData); 142 CHECK(encoder != NULL); 143 return encoder->unbindOutputBuffer(index); 144} 145 146SoftAVCEncoder::SoftAVCEncoder( 147 const char *name, 148 const OMX_CALLBACKTYPE *callbacks, 149 OMX_PTR appData, 150 OMX_COMPONENTTYPE **component) 151 : SoftVideoEncoderOMXComponent(name, callbacks, appData, component), 152 mVideoWidth(176), 153 mVideoHeight(144), 154 mVideoFrameRate(30), 155 mVideoBitRate(192000), 156 mVideoColorFormat(OMX_COLOR_FormatYUV420Planar), 157 mStoreMetaDataInBuffers(false), 158 mIDRFrameRefreshIntervalInSec(1), 159 mAVCEncProfile(AVC_BASELINE), 160 mAVCEncLevel(AVC_LEVEL2), 161 mNumInputFrames(-1), 162 mPrevTimestampUs(-1), 163 mStarted(false), 164 mSawInputEOS(false), 165 mSignalledError(false), 166 mHandle(new tagAVCHandle), 167 mEncParams(new tagAVCEncParam), 168 mInputFrameData(NULL), 169 mSliceGroup(NULL) { 170 171 initPorts(); 172 ALOGI("Construct SoftAVCEncoder"); 173} 174 175SoftAVCEncoder::~SoftAVCEncoder() { 176 ALOGV("Destruct SoftAVCEncoder"); 177 releaseEncoder(); 178 List<BufferInfo *> &outQueue = getPortQueue(1); 179 List<BufferInfo *> &inQueue = getPortQueue(0); 180 CHECK(outQueue.empty()); 181 CHECK(inQueue.empty()); 182} 183 184OMX_ERRORTYPE SoftAVCEncoder::initEncParams() { 185 CHECK(mHandle != NULL); 186 memset(mHandle, 0, sizeof(tagAVCHandle)); 187 mHandle->AVCObject = NULL; 188 mHandle->userData = this; 189 mHandle->CBAVC_DPBAlloc = DpbAllocWrapper; 190 mHandle->CBAVC_FrameBind = BindFrameWrapper; 191 mHandle->CBAVC_FrameUnbind = UnbindFrameWrapper; 192 mHandle->CBAVC_Malloc = MallocWrapper; 193 mHandle->CBAVC_Free = FreeWrapper; 194 195 CHECK(mEncParams != NULL); 196 memset(mEncParams, 0, sizeof(*mEncParams)); 197 mEncParams->rate_control = AVC_ON; 198 mEncParams->initQP = 0; 199 mEncParams->init_CBP_removal_delay = 1600; 200 201 mEncParams->intramb_refresh = 0; 202 mEncParams->auto_scd = AVC_ON; 203 mEncParams->out_of_band_param_set = AVC_ON; 204 mEncParams->poc_type = 2; 205 mEncParams->log2_max_poc_lsb_minus_4 = 12; 206 mEncParams->delta_poc_zero_flag = 0; 207 mEncParams->offset_poc_non_ref = 0; 208 mEncParams->offset_top_bottom = 0; 209 mEncParams->num_ref_in_cycle = 0; 210 mEncParams->offset_poc_ref = NULL; 211 212 mEncParams->num_ref_frame = 1; 213 mEncParams->num_slice_group = 1; 214 mEncParams->fmo_type = 0; 215 216 mEncParams->db_filter = AVC_ON; 217 mEncParams->disable_db_idc = 0; 218 219 mEncParams->alpha_offset = 0; 220 mEncParams->beta_offset = 0; 221 mEncParams->constrained_intra_pred = AVC_OFF; 222 223 mEncParams->data_par = AVC_OFF; 224 mEncParams->fullsearch = AVC_OFF; 225 mEncParams->search_range = 16; 226 mEncParams->sub_pel = AVC_OFF; 227 mEncParams->submb_pred = AVC_OFF; 228 mEncParams->rdopt_mode = AVC_OFF; 229 mEncParams->bidir_pred = AVC_OFF; 230 231 mEncParams->use_overrun_buffer = AVC_OFF; 232 233 if (mVideoColorFormat != OMX_COLOR_FormatYUV420Planar 234 || mStoreMetaDataInBuffers) { 235 // Color conversion is needed. 236 free(mInputFrameData); 237 mInputFrameData = 238 (uint8_t *) malloc((mVideoWidth * mVideoHeight * 3 ) >> 1); 239 CHECK(mInputFrameData != NULL); 240 } 241 242 // PV's AVC encoder requires the video dimension of multiple 243 if (mVideoWidth % 16 != 0 || mVideoHeight % 16 != 0) { 244 ALOGE("Video frame size %dx%d must be a multiple of 16", 245 mVideoWidth, mVideoHeight); 246 return OMX_ErrorBadParameter; 247 } 248 249 mEncParams->width = mVideoWidth; 250 mEncParams->height = mVideoHeight; 251 mEncParams->bitrate = mVideoBitRate; 252 mEncParams->frame_rate = 1000 * mVideoFrameRate; // In frames/ms! 253 mEncParams->CPB_size = (uint32_t) (mVideoBitRate >> 1); 254 255 int32_t nMacroBlocks = ((((mVideoWidth + 15) >> 4) << 4) * 256 (((mVideoHeight + 15) >> 4) << 4)) >> 8; 257 CHECK(mSliceGroup == NULL); 258 mSliceGroup = (uint32_t *) malloc(sizeof(uint32_t) * nMacroBlocks); 259 CHECK(mSliceGroup != NULL); 260 for (int ii = 0, idx = 0; ii < nMacroBlocks; ++ii) { 261 mSliceGroup[ii] = idx++; 262 if (idx >= mEncParams->num_slice_group) { 263 idx = 0; 264 } 265 } 266 mEncParams->slice_group = mSliceGroup; 267 268 // Set IDR frame refresh interval 269 if (mIDRFrameRefreshIntervalInSec < 0) { 270 mEncParams->idr_period = -1; 271 } else if (mIDRFrameRefreshIntervalInSec == 0) { 272 mEncParams->idr_period = 1; // All I frames 273 } else { 274 mEncParams->idr_period = 275 (mIDRFrameRefreshIntervalInSec * mVideoFrameRate); 276 } 277 278 // Set profile and level 279 mEncParams->profile = mAVCEncProfile; 280 mEncParams->level = mAVCEncLevel; 281 282 return OMX_ErrorNone; 283} 284 285OMX_ERRORTYPE SoftAVCEncoder::initEncoder() { 286 CHECK(!mStarted); 287 288 OMX_ERRORTYPE errType = OMX_ErrorNone; 289 if (OMX_ErrorNone != (errType = initEncParams())) { 290 ALOGE("Failed to initialized encoder params"); 291 mSignalledError = true; 292 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 293 return errType; 294 } 295 296 AVCEnc_Status err; 297 err = PVAVCEncInitialize(mHandle, mEncParams, NULL, NULL); 298 if (err != AVCENC_SUCCESS) { 299 ALOGE("Failed to initialize the encoder: %d", err); 300 mSignalledError = true; 301 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 302 return OMX_ErrorUndefined; 303 } 304 305 mNumInputFrames = -2; // 1st two buffers contain SPS and PPS 306 mSpsPpsHeaderReceived = false; 307 mReadyForNextFrame = true; 308 mIsIDRFrame = false; 309 mStarted = true; 310 311 return OMX_ErrorNone; 312} 313 314OMX_ERRORTYPE SoftAVCEncoder::releaseEncoder() { 315 if (!mStarted) { 316 return OMX_ErrorNone; 317 } 318 319 PVAVCCleanUpEncoder(mHandle); 320 releaseOutputBuffers(); 321 322 free(mInputFrameData); 323 mInputFrameData = NULL; 324 325 free(mSliceGroup); 326 mSliceGroup = NULL; 327 328 delete mEncParams; 329 mEncParams = NULL; 330 331 delete mHandle; 332 mHandle = NULL; 333 334 mStarted = false; 335 336 return OMX_ErrorNone; 337} 338 339void SoftAVCEncoder::releaseOutputBuffers() { 340 for (size_t i = 0; i < mOutputBuffers.size(); ++i) { 341 MediaBuffer *buffer = mOutputBuffers.editItemAt(i); 342 buffer->setObserver(NULL); 343 buffer->release(); 344 } 345 mOutputBuffers.clear(); 346} 347 348void SoftAVCEncoder::initPorts() { 349 OMX_PARAM_PORTDEFINITIONTYPE def; 350 InitOMXParams(&def); 351 352 const size_t kInputBufferSize = (mVideoWidth * mVideoHeight * 3) >> 1; 353 354 // 31584 is PV's magic number. Not sure why. 355 const size_t kOutputBufferSize = 356 (kInputBufferSize > 31584) ? kInputBufferSize: 31584; 357 358 def.nPortIndex = 0; 359 def.eDir = OMX_DirInput; 360 def.nBufferCountMin = kNumBuffers; 361 def.nBufferCountActual = def.nBufferCountMin; 362 def.nBufferSize = kInputBufferSize; 363 def.bEnabled = OMX_TRUE; 364 def.bPopulated = OMX_FALSE; 365 def.eDomain = OMX_PortDomainVideo; 366 def.bBuffersContiguous = OMX_FALSE; 367 def.nBufferAlignment = 1; 368 369 def.format.video.cMIMEType = const_cast<char *>("video/raw"); 370 def.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused; 371 def.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar; 372 def.format.video.xFramerate = (mVideoFrameRate << 16); // Q16 format 373 def.format.video.nBitrate = mVideoBitRate; 374 def.format.video.nFrameWidth = mVideoWidth; 375 def.format.video.nFrameHeight = mVideoHeight; 376 def.format.video.nStride = mVideoWidth; 377 def.format.video.nSliceHeight = mVideoHeight; 378 379 addPort(def); 380 381 def.nPortIndex = 1; 382 def.eDir = OMX_DirOutput; 383 def.nBufferCountMin = kNumBuffers; 384 def.nBufferCountActual = def.nBufferCountMin; 385 def.nBufferSize = kOutputBufferSize; 386 def.bEnabled = OMX_TRUE; 387 def.bPopulated = OMX_FALSE; 388 def.eDomain = OMX_PortDomainVideo; 389 def.bBuffersContiguous = OMX_FALSE; 390 def.nBufferAlignment = 2; 391 392 def.format.video.cMIMEType = const_cast<char *>("video/avc"); 393 def.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC; 394 def.format.video.eColorFormat = OMX_COLOR_FormatUnused; 395 def.format.video.xFramerate = (0 << 16); // Q16 format 396 def.format.video.nBitrate = mVideoBitRate; 397 def.format.video.nFrameWidth = mVideoWidth; 398 def.format.video.nFrameHeight = mVideoHeight; 399 def.format.video.nStride = mVideoWidth; 400 def.format.video.nSliceHeight = mVideoHeight; 401 402 addPort(def); 403} 404 405OMX_ERRORTYPE SoftAVCEncoder::internalGetParameter( 406 OMX_INDEXTYPE index, OMX_PTR params) { 407 switch (index) { 408 case OMX_IndexParamVideoErrorCorrection: 409 { 410 return OMX_ErrorNotImplemented; 411 } 412 413 case OMX_IndexParamVideoBitrate: 414 { 415 OMX_VIDEO_PARAM_BITRATETYPE *bitRate = 416 (OMX_VIDEO_PARAM_BITRATETYPE *) params; 417 418 if (bitRate->nPortIndex != 1) { 419 return OMX_ErrorUndefined; 420 } 421 422 bitRate->eControlRate = OMX_Video_ControlRateVariable; 423 bitRate->nTargetBitrate = mVideoBitRate; 424 return OMX_ErrorNone; 425 } 426 427 case OMX_IndexParamVideoPortFormat: 428 { 429 OMX_VIDEO_PARAM_PORTFORMATTYPE *formatParams = 430 (OMX_VIDEO_PARAM_PORTFORMATTYPE *)params; 431 432 if (formatParams->nPortIndex > 1) { 433 return OMX_ErrorUndefined; 434 } 435 436 if (formatParams->nIndex > 2) { 437 return OMX_ErrorNoMore; 438 } 439 440 if (formatParams->nPortIndex == 0) { 441 formatParams->eCompressionFormat = OMX_VIDEO_CodingUnused; 442 if (formatParams->nIndex == 0) { 443 formatParams->eColorFormat = OMX_COLOR_FormatYUV420Planar; 444 } else if (formatParams->nIndex == 1) { 445 formatParams->eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar; 446 } else { 447 formatParams->eColorFormat = OMX_COLOR_FormatAndroidOpaque; 448 } 449 } else { 450 formatParams->eCompressionFormat = OMX_VIDEO_CodingAVC; 451 formatParams->eColorFormat = OMX_COLOR_FormatUnused; 452 } 453 454 return OMX_ErrorNone; 455 } 456 457 case OMX_IndexParamVideoAvc: 458 { 459 OMX_VIDEO_PARAM_AVCTYPE *avcParams = 460 (OMX_VIDEO_PARAM_AVCTYPE *)params; 461 462 if (avcParams->nPortIndex != 1) { 463 return OMX_ErrorUndefined; 464 } 465 466 avcParams->eProfile = OMX_VIDEO_AVCProfileBaseline; 467 OMX_U32 omxLevel = AVC_LEVEL2; 468 if (OMX_ErrorNone != 469 ConvertAvcSpecLevelToOmxAvcLevel(mAVCEncLevel, &omxLevel)) { 470 return OMX_ErrorUndefined; 471 } 472 473 avcParams->eLevel = (OMX_VIDEO_AVCLEVELTYPE) omxLevel; 474 avcParams->nRefFrames = 1; 475 avcParams->nBFrames = 0; 476 avcParams->bUseHadamard = OMX_TRUE; 477 avcParams->nAllowedPictureTypes = 478 (OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP); 479 avcParams->nRefIdx10ActiveMinus1 = 0; 480 avcParams->nRefIdx11ActiveMinus1 = 0; 481 avcParams->bWeightedPPrediction = OMX_FALSE; 482 avcParams->bEntropyCodingCABAC = OMX_FALSE; 483 avcParams->bconstIpred = OMX_FALSE; 484 avcParams->bDirect8x8Inference = OMX_FALSE; 485 avcParams->bDirectSpatialTemporal = OMX_FALSE; 486 avcParams->nCabacInitIdc = 0; 487 return OMX_ErrorNone; 488 } 489 490 case OMX_IndexParamVideoProfileLevelQuerySupported: 491 { 492 OMX_VIDEO_PARAM_PROFILELEVELTYPE *profileLevel = 493 (OMX_VIDEO_PARAM_PROFILELEVELTYPE *)params; 494 495 if (profileLevel->nPortIndex != 1) { 496 return OMX_ErrorUndefined; 497 } 498 499 const size_t size = 500 sizeof(ConversionTable) / sizeof(ConversionTable[0]); 501 502 if (profileLevel->nProfileIndex >= size) { 503 return OMX_ErrorNoMore; 504 } 505 506 profileLevel->eProfile = OMX_VIDEO_AVCProfileBaseline; 507 profileLevel->eLevel = ConversionTable[profileLevel->nProfileIndex].omxLevel; 508 509 return OMX_ErrorNone; 510 } 511 512 default: 513 return SimpleSoftOMXComponent::internalGetParameter(index, params); 514 } 515} 516 517OMX_ERRORTYPE SoftAVCEncoder::internalSetParameter( 518 OMX_INDEXTYPE index, const OMX_PTR params) { 519 int32_t indexFull = index; 520 521 switch (indexFull) { 522 case OMX_IndexParamVideoErrorCorrection: 523 { 524 return OMX_ErrorNotImplemented; 525 } 526 527 case OMX_IndexParamVideoBitrate: 528 { 529 OMX_VIDEO_PARAM_BITRATETYPE *bitRate = 530 (OMX_VIDEO_PARAM_BITRATETYPE *) params; 531 532 if (bitRate->nPortIndex != 1 || 533 bitRate->eControlRate != OMX_Video_ControlRateVariable) { 534 return OMX_ErrorUndefined; 535 } 536 537 mVideoBitRate = bitRate->nTargetBitrate; 538 return OMX_ErrorNone; 539 } 540 541 case OMX_IndexParamPortDefinition: 542 { 543 OMX_PARAM_PORTDEFINITIONTYPE *def = 544 (OMX_PARAM_PORTDEFINITIONTYPE *)params; 545 if (def->nPortIndex > 1) { 546 return OMX_ErrorUndefined; 547 } 548 549 if (def->nPortIndex == 0) { 550 if (def->format.video.eCompressionFormat != OMX_VIDEO_CodingUnused || 551 (def->format.video.eColorFormat != OMX_COLOR_FormatYUV420Planar && 552 def->format.video.eColorFormat != OMX_COLOR_FormatYUV420SemiPlanar && 553 def->format.video.eColorFormat != OMX_COLOR_FormatAndroidOpaque)) { 554 return OMX_ErrorUndefined; 555 } 556 } else { 557 if (def->format.video.eCompressionFormat != OMX_VIDEO_CodingAVC || 558 (def->format.video.eColorFormat != OMX_COLOR_FormatUnused)) { 559 return OMX_ErrorUndefined; 560 } 561 } 562 563 OMX_ERRORTYPE err = SimpleSoftOMXComponent::internalSetParameter(index, params); 564 if (OMX_ErrorNone != err) { 565 return err; 566 } 567 568 if (def->nPortIndex == 0) { 569 mVideoWidth = def->format.video.nFrameWidth; 570 mVideoHeight = def->format.video.nFrameHeight; 571 mVideoFrameRate = def->format.video.xFramerate >> 16; 572 mVideoColorFormat = def->format.video.eColorFormat; 573 574 OMX_PARAM_PORTDEFINITIONTYPE *portDef = 575 &editPortInfo(0)->mDef; 576 portDef->format.video.nFrameWidth = mVideoWidth; 577 portDef->format.video.nFrameHeight = mVideoHeight; 578 portDef->format.video.xFramerate = def->format.video.xFramerate; 579 portDef->format.video.eColorFormat = 580 (OMX_COLOR_FORMATTYPE) mVideoColorFormat; 581 portDef = &editPortInfo(1)->mDef; 582 portDef->format.video.nFrameWidth = mVideoWidth; 583 portDef->format.video.nFrameHeight = mVideoHeight; 584 } else { 585 mVideoBitRate = def->format.video.nBitrate; 586 } 587 588 return OMX_ErrorNone; 589 } 590 591 case OMX_IndexParamStandardComponentRole: 592 { 593 const OMX_PARAM_COMPONENTROLETYPE *roleParams = 594 (const OMX_PARAM_COMPONENTROLETYPE *)params; 595 596 if (strncmp((const char *)roleParams->cRole, 597 "video_encoder.avc", 598 OMX_MAX_STRINGNAME_SIZE - 1)) { 599 return OMX_ErrorUndefined; 600 } 601 602 return OMX_ErrorNone; 603 } 604 605 case OMX_IndexParamVideoPortFormat: 606 { 607 const OMX_VIDEO_PARAM_PORTFORMATTYPE *formatParams = 608 (const OMX_VIDEO_PARAM_PORTFORMATTYPE *)params; 609 610 if (formatParams->nPortIndex > 1) { 611 return OMX_ErrorUndefined; 612 } 613 614 if (formatParams->nIndex > 2) { 615 return OMX_ErrorNoMore; 616 } 617 618 if (formatParams->nPortIndex == 0) { 619 if (formatParams->eCompressionFormat != OMX_VIDEO_CodingUnused || 620 ((formatParams->nIndex == 0 && 621 formatParams->eColorFormat != OMX_COLOR_FormatYUV420Planar) || 622 (formatParams->nIndex == 1 && 623 formatParams->eColorFormat != OMX_COLOR_FormatYUV420SemiPlanar) || 624 (formatParams->nIndex == 2 && 625 formatParams->eColorFormat != OMX_COLOR_FormatAndroidOpaque) )) { 626 return OMX_ErrorUndefined; 627 } 628 mVideoColorFormat = formatParams->eColorFormat; 629 } else { 630 if (formatParams->eCompressionFormat != OMX_VIDEO_CodingAVC || 631 formatParams->eColorFormat != OMX_COLOR_FormatUnused) { 632 return OMX_ErrorUndefined; 633 } 634 } 635 636 return OMX_ErrorNone; 637 } 638 639 case OMX_IndexParamVideoAvc: 640 { 641 OMX_VIDEO_PARAM_AVCTYPE *avcType = 642 (OMX_VIDEO_PARAM_AVCTYPE *)params; 643 644 if (avcType->nPortIndex != 1) { 645 return OMX_ErrorUndefined; 646 } 647 648 // PV's AVC encoder only supports baseline profile 649 if (avcType->eProfile != OMX_VIDEO_AVCProfileBaseline || 650 avcType->nRefFrames != 1 || 651 avcType->nBFrames != 0 || 652 avcType->bUseHadamard != OMX_TRUE || 653 (avcType->nAllowedPictureTypes & OMX_VIDEO_PictureTypeB) != 0 || 654 avcType->nRefIdx10ActiveMinus1 != 0 || 655 avcType->nRefIdx11ActiveMinus1 != 0 || 656 avcType->bWeightedPPrediction != OMX_FALSE || 657 avcType->bEntropyCodingCABAC != OMX_FALSE || 658 avcType->bconstIpred != OMX_FALSE || 659 avcType->bDirect8x8Inference != OMX_FALSE || 660 avcType->bDirectSpatialTemporal != OMX_FALSE || 661 avcType->nCabacInitIdc != 0) { 662 return OMX_ErrorUndefined; 663 } 664 665 if (OK != ConvertOmxAvcLevelToAvcSpecLevel(avcType->eLevel, &mAVCEncLevel)) { 666 return OMX_ErrorUndefined; 667 } 668 669 return OMX_ErrorNone; 670 } 671 672 case kStoreMetaDataExtensionIndex: 673 { 674 StoreMetaDataInBuffersParams *storeParams = 675 (StoreMetaDataInBuffersParams*)params; 676 if (storeParams->nPortIndex != 0) { 677 ALOGE("%s: StoreMetadataInBuffersParams.nPortIndex not zero!", 678 __FUNCTION__); 679 return OMX_ErrorUndefined; 680 } 681 682 mStoreMetaDataInBuffers = storeParams->bStoreMetaData; 683 ALOGV("StoreMetaDataInBuffers set to: %s", 684 mStoreMetaDataInBuffers ? " true" : "false"); 685 686 if (mStoreMetaDataInBuffers) { 687 mVideoColorFormat = OMX_COLOR_FormatAndroidOpaque; 688 } 689 690 return OMX_ErrorNone; 691 } 692 693 default: 694 return SimpleSoftOMXComponent::internalSetParameter(index, params); 695 } 696} 697 698void SoftAVCEncoder::onQueueFilled(OMX_U32 /* portIndex */) { 699 if (mSignalledError || mSawInputEOS) { 700 return; 701 } 702 703 if (!mStarted) { 704 if (OMX_ErrorNone != initEncoder()) { 705 return; 706 } 707 } 708 709 List<BufferInfo *> &inQueue = getPortQueue(0); 710 List<BufferInfo *> &outQueue = getPortQueue(1); 711 712 while (!mSawInputEOS && !inQueue.empty() && !outQueue.empty()) { 713 BufferInfo *inInfo = *inQueue.begin(); 714 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 715 BufferInfo *outInfo = *outQueue.begin(); 716 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader; 717 718 outHeader->nTimeStamp = 0; 719 outHeader->nFlags = 0; 720 outHeader->nOffset = 0; 721 outHeader->nFilledLen = 0; 722 outHeader->nOffset = 0; 723 724 uint8_t *outPtr = (uint8_t *) outHeader->pBuffer; 725 uint32_t dataLength = outHeader->nAllocLen; 726 727 if (!mSpsPpsHeaderReceived && mNumInputFrames < 0) { 728 // 4 bytes are reserved for holding the start code 0x00000001 729 // of the sequence parameter set at the beginning. 730 outPtr += 4; 731 dataLength -= 4; 732 } 733 734 int32_t type; 735 AVCEnc_Status encoderStatus = AVCENC_SUCCESS; 736 737 // Combine SPS and PPS and place them in the very first output buffer 738 // SPS and PPS are separated by start code 0x00000001 739 // Assume that we have exactly one SPS and exactly one PPS. 740 while (!mSpsPpsHeaderReceived && mNumInputFrames <= 0) { 741 encoderStatus = PVAVCEncodeNAL(mHandle, outPtr, &dataLength, &type); 742 if (encoderStatus == AVCENC_WRONG_STATE) { 743 mSpsPpsHeaderReceived = true; 744 CHECK_EQ(0, mNumInputFrames); // 1st video frame is 0 745 outHeader->nFlags = OMX_BUFFERFLAG_CODECCONFIG; 746 outQueue.erase(outQueue.begin()); 747 outInfo->mOwnedByUs = false; 748 notifyFillBufferDone(outHeader); 749 return; 750 } else { 751 switch (type) { 752 case AVC_NALTYPE_SPS: 753 ++mNumInputFrames; 754 memcpy((uint8_t *)outHeader->pBuffer, "\x00\x00\x00\x01", 4); 755 outHeader->nFilledLen = 4 + dataLength; 756 outPtr += (dataLength + 4); // 4 bytes for next start code 757 dataLength = outHeader->nAllocLen - outHeader->nFilledLen; 758 break; 759 default: 760 CHECK_EQ(AVC_NALTYPE_PPS, type); 761 ++mNumInputFrames; 762 memcpy((uint8_t *) outHeader->pBuffer + outHeader->nFilledLen, 763 "\x00\x00\x00\x01", 4); 764 outHeader->nFilledLen += (dataLength + 4); 765 outPtr += (dataLength + 4); 766 break; 767 } 768 } 769 } 770 771 // Get next input video frame 772 if (mReadyForNextFrame) { 773 // Save the input buffer info so that it can be 774 // passed to an output buffer 775 InputBufferInfo info; 776 info.mTimeUs = inHeader->nTimeStamp; 777 info.mFlags = inHeader->nFlags; 778 mInputBufferInfoVec.push(info); 779 mPrevTimestampUs = inHeader->nTimeStamp; 780 781 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 782 mSawInputEOS = true; 783 } 784 785 if (inHeader->nFilledLen > 0) { 786 AVCFrameIO videoInput; 787 memset(&videoInput, 0, sizeof(videoInput)); 788 videoInput.height = ((mVideoHeight + 15) >> 4) << 4; 789 videoInput.pitch = ((mVideoWidth + 15) >> 4) << 4; 790 videoInput.coding_timestamp = (inHeader->nTimeStamp + 500) / 1000; // in ms 791 const uint8_t *inputData = NULL; 792 if (mStoreMetaDataInBuffers) { 793 if (inHeader->nFilledLen != 8) { 794 ALOGE("MetaData buffer is wrong size! " 795 "(got %u bytes, expected 8)", inHeader->nFilledLen); 796 mSignalledError = true; 797 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 798 return; 799 } 800 inputData = 801 extractGraphicBuffer( 802 mInputFrameData, (mVideoWidth * mVideoHeight * 3) >> 1, 803 inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen, 804 mVideoWidth, mVideoHeight); 805 if (inputData == NULL) { 806 ALOGE("Unable to extract gralloc buffer in metadata mode"); 807 mSignalledError = true; 808 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 809 return; 810 } 811 // TODO: Verify/convert pixel format enum 812 } else { 813 inputData = (const uint8_t *)inHeader->pBuffer + inHeader->nOffset; 814 if (mVideoColorFormat != OMX_COLOR_FormatYUV420Planar) { 815 ConvertYUV420SemiPlanarToYUV420Planar( 816 inputData, mInputFrameData, mVideoWidth, mVideoHeight); 817 inputData = mInputFrameData; 818 } 819 } 820 821 CHECK(inputData != NULL); 822 videoInput.YCbCr[0] = (uint8_t *)inputData; 823 videoInput.YCbCr[1] = videoInput.YCbCr[0] + videoInput.height * videoInput.pitch; 824 videoInput.YCbCr[2] = videoInput.YCbCr[1] + 825 ((videoInput.height * videoInput.pitch) >> 2); 826 videoInput.disp_order = mNumInputFrames; 827 828 encoderStatus = PVAVCEncSetInput(mHandle, &videoInput); 829 if (encoderStatus == AVCENC_SUCCESS || encoderStatus == AVCENC_NEW_IDR) { 830 mReadyForNextFrame = false; 831 ++mNumInputFrames; 832 if (encoderStatus == AVCENC_NEW_IDR) { 833 mIsIDRFrame = 1; 834 } 835 } else { 836 if (encoderStatus < AVCENC_SUCCESS) { 837 ALOGE("encoderStatus = %d at line %d", encoderStatus, __LINE__); 838 mSignalledError = true; 839 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 840 return; 841 } else { 842 ALOGV("encoderStatus = %d at line %d", encoderStatus, __LINE__); 843 inQueue.erase(inQueue.begin()); 844 inInfo->mOwnedByUs = false; 845 notifyEmptyBufferDone(inHeader); 846 return; 847 } 848 } 849 } 850 } 851 852 // Encode an input video frame 853 CHECK(encoderStatus == AVCENC_SUCCESS || encoderStatus == AVCENC_NEW_IDR); 854 dataLength = outHeader->nAllocLen; // Reset the output buffer length 855 if (inHeader->nFilledLen > 0) { 856 if (outHeader->nAllocLen >= 4) { 857 memcpy(outPtr, "\x00\x00\x00\x01", 4); 858 outPtr += 4; 859 dataLength -= 4; 860 } 861 encoderStatus = PVAVCEncodeNAL(mHandle, outPtr, &dataLength, &type); 862 dataLength = outPtr + dataLength - outHeader->pBuffer; 863 if (encoderStatus == AVCENC_SUCCESS) { 864 CHECK(NULL == PVAVCEncGetOverrunBuffer(mHandle)); 865 } else if (encoderStatus == AVCENC_PICTURE_READY) { 866 CHECK(NULL == PVAVCEncGetOverrunBuffer(mHandle)); 867 if (mIsIDRFrame) { 868 outHeader->nFlags |= OMX_BUFFERFLAG_SYNCFRAME; 869 mIsIDRFrame = false; 870 } 871 mReadyForNextFrame = true; 872 AVCFrameIO recon; 873 if (PVAVCEncGetRecon(mHandle, &recon) == AVCENC_SUCCESS) { 874 PVAVCEncReleaseRecon(mHandle, &recon); 875 } 876 } else { 877 dataLength = 0; 878 mReadyForNextFrame = true; 879 } 880 881 if (encoderStatus < AVCENC_SUCCESS) { 882 ALOGE("encoderStatus = %d at line %d", encoderStatus, __LINE__); 883 mSignalledError = true; 884 notify(OMX_EventError, OMX_ErrorUndefined, 0, 0); 885 return; 886 } 887 } else { 888 dataLength = 0; 889 } 890 891 inQueue.erase(inQueue.begin()); 892 inInfo->mOwnedByUs = false; 893 notifyEmptyBufferDone(inHeader); 894 895 outQueue.erase(outQueue.begin()); 896 CHECK(!mInputBufferInfoVec.empty()); 897 InputBufferInfo *inputBufInfo = mInputBufferInfoVec.begin(); 898 outHeader->nTimeStamp = inputBufInfo->mTimeUs; 899 outHeader->nFlags |= (inputBufInfo->mFlags | OMX_BUFFERFLAG_ENDOFFRAME); 900 if (mSawInputEOS) { 901 outHeader->nFlags |= OMX_BUFFERFLAG_EOS; 902 } 903 outHeader->nFilledLen = dataLength; 904 outInfo->mOwnedByUs = false; 905 notifyFillBufferDone(outHeader); 906 mInputBufferInfoVec.erase(mInputBufferInfoVec.begin()); 907 } 908} 909 910int32_t SoftAVCEncoder::allocOutputBuffers( 911 unsigned int sizeInMbs, unsigned int numBuffers) { 912 CHECK(mOutputBuffers.isEmpty()); 913 size_t frameSize = (sizeInMbs << 7) * 3; 914 for (unsigned int i = 0; i < numBuffers; ++i) { 915 MediaBuffer *buffer = new MediaBuffer(frameSize); 916 buffer->setObserver(this); 917 mOutputBuffers.push(buffer); 918 } 919 920 return 1; 921} 922 923void SoftAVCEncoder::unbindOutputBuffer(int32_t index) { 924 CHECK(index >= 0); 925} 926 927int32_t SoftAVCEncoder::bindOutputBuffer(int32_t index, uint8_t **yuv) { 928 CHECK(index >= 0); 929 CHECK(index < (int32_t) mOutputBuffers.size()); 930 *yuv = (uint8_t *) mOutputBuffers[index]->data(); 931 932 return 1; 933} 934 935void SoftAVCEncoder::signalBufferReturned(MediaBuffer *buffer) { 936 UNUSED_UNLESS_VERBOSE(buffer); 937 ALOGV("signalBufferReturned: %p", buffer); 938} 939 940} // namespace android 941 942android::SoftOMXComponent *createSoftOMXComponent( 943 const char *name, const OMX_CALLBACKTYPE *callbacks, 944 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 945 return new android::SoftAVCEncoder(name, callbacks, appData, component); 946} 947