1 /* 2 * Copyright (C) 2009 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 #include <inttypes.h> 18 19 //#define LOG_NDEBUG 0 20 #define LOG_TAG "OMXCodec" 21 22 #ifdef __LP64__ 23 #define OMX_ANDROID_COMPILE_AS_32BIT_ON_64BIT_PLATFORMS 24 #endif 25 26 #include <utils/Log.h> 27 28 #include "include/AACEncoder.h" 29 30 #include "include/ESDS.h" 31 32 #include <binder/IServiceManager.h> 33 #include <binder/MemoryDealer.h> 34 #include <binder/ProcessState.h> 35 #include <HardwareAPI.h> 36 #include <media/stagefright/foundation/ADebug.h> 37 #include <media/IMediaPlayerService.h> 38 #include <media/stagefright/ACodec.h> 39 #include <media/stagefright/MediaBuffer.h> 40 #include <media/stagefright/MediaBufferGroup.h> 41 #include <media/stagefright/MediaDefs.h> 42 #include <media/stagefright/MediaCodecList.h> 43 #include <media/stagefright/MediaExtractor.h> 44 #include <media/stagefright/MetaData.h> 45 #include <media/stagefright/OMXCodec.h> 46 #include <media/stagefright/Utils.h> 47 #include <media/stagefright/SkipCutBuffer.h> 48 #include <utils/Vector.h> 49 50 #include <OMX_Audio.h> 51 #include <OMX_AudioExt.h> 52 #include <OMX_Component.h> 53 #include <OMX_IndexExt.h> 54 55 #include "include/avc_utils.h" 56 57 namespace android { 58 59 // Treat time out as an error if we have not received any output 60 // buffers after 3 seconds. 61 const static int64_t kBufferFilledEventTimeOutNs = 3000000000LL; 62 63 // OMX Spec defines less than 50 color formats. If the query for 64 // color format is executed for more than kMaxColorFormatSupported, 65 // the query will fail to avoid looping forever. 66 // 1000 is more than enough for us to tell whether the omx 67 // component in question is buggy or not. 68 const static uint32_t kMaxColorFormatSupported = 1000; 69 70 #define FACTORY_CREATE_ENCODER(name) \ 71 static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \ 72 return new name(source, meta); \ 73 } 74 75 #define FACTORY_REF(name) { #name, Make##name }, 76 77 FACTORY_CREATE_ENCODER(AACEncoder) 78 79 static sp<MediaSource> InstantiateSoftwareEncoder( 80 const char *name, const sp<MediaSource> &source, 81 const sp<MetaData> &meta) { 82 struct FactoryInfo { 83 const char *name; 84 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &); 85 }; 86 87 static const FactoryInfo kFactoryInfo[] = { 88 FACTORY_REF(AACEncoder) 89 }; 90 for (size_t i = 0; 91 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) { 92 if (!strcmp(name, kFactoryInfo[i].name)) { 93 return (*kFactoryInfo[i].CreateFunc)(source, meta); 94 } 95 } 96 97 return NULL; 98 } 99 100 #undef FACTORY_CREATE_ENCODER 101 #undef FACTORY_REF 102 103 #define CODEC_LOGI(x, ...) ALOGI("[%s] "x, mComponentName, ##__VA_ARGS__) 104 #define CODEC_LOGV(x, ...) ALOGV("[%s] "x, mComponentName, ##__VA_ARGS__) 105 #define CODEC_LOGW(x, ...) ALOGW("[%s] "x, mComponentName, ##__VA_ARGS__) 106 #define CODEC_LOGE(x, ...) ALOGE("[%s] "x, mComponentName, ##__VA_ARGS__) 107 108 struct OMXCodecObserver : public BnOMXObserver { 109 OMXCodecObserver() { 110 } 111 112 void setCodec(const sp<OMXCodec> &target) { 113 mTarget = target; 114 } 115 116 // from IOMXObserver 117 virtual void onMessage(const omx_message &msg) { 118 sp<OMXCodec> codec = mTarget.promote(); 119 120 if (codec.get() != NULL) { 121 Mutex::Autolock autoLock(codec->mLock); 122 codec->on_message(msg); 123 codec.clear(); 124 } 125 } 126 127 protected: 128 virtual ~OMXCodecObserver() {} 129 130 private: 131 wp<OMXCodec> mTarget; 132 133 OMXCodecObserver(const OMXCodecObserver &); 134 OMXCodecObserver &operator=(const OMXCodecObserver &); 135 }; 136 137 template<class T> 138 static void InitOMXParams(T *params) { 139 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(sizeof(OMX_PTR) == 4); // check OMX_PTR is 4 bytes. 140 params->nSize = sizeof(T); 141 params->nVersion.s.nVersionMajor = 1; 142 params->nVersion.s.nVersionMinor = 0; 143 params->nVersion.s.nRevision = 0; 144 params->nVersion.s.nStep = 0; 145 } 146 147 static bool IsSoftwareCodec(const char *componentName) { 148 if (!strncmp("OMX.google.", componentName, 11)) { 149 return true; 150 } 151 152 if (!strncmp("OMX.", componentName, 4)) { 153 return false; 154 } 155 156 return true; 157 } 158 159 // A sort order in which OMX software codecs are first, followed 160 // by other (non-OMX) software codecs, followed by everything else. 161 static int CompareSoftwareCodecsFirst( 162 const OMXCodec::CodecNameAndQuirks *elem1, 163 const OMXCodec::CodecNameAndQuirks *elem2) { 164 bool isOMX1 = !strncmp(elem1->mName.string(), "OMX.", 4); 165 bool isOMX2 = !strncmp(elem2->mName.string(), "OMX.", 4); 166 167 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->mName.string()); 168 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->mName.string()); 169 170 if (isSoftwareCodec1) { 171 if (!isSoftwareCodec2) { return -1; } 172 173 if (isOMX1) { 174 if (isOMX2) { return 0; } 175 176 return -1; 177 } else { 178 if (isOMX2) { return 0; } 179 180 return 1; 181 } 182 183 return -1; 184 } 185 186 if (isSoftwareCodec2) { 187 return 1; 188 } 189 190 return 0; 191 } 192 193 // static 194 void OMXCodec::findMatchingCodecs( 195 const char *mime, 196 bool createEncoder, const char *matchComponentName, 197 uint32_t flags, 198 Vector<CodecNameAndQuirks> *matchingCodecs) { 199 matchingCodecs->clear(); 200 201 const sp<IMediaCodecList> list = MediaCodecList::getInstance(); 202 if (list == NULL) { 203 return; 204 } 205 206 size_t index = 0; 207 for (;;) { 208 ssize_t matchIndex = 209 list->findCodecByType(mime, createEncoder, index); 210 211 if (matchIndex < 0) { 212 break; 213 } 214 215 index = matchIndex + 1; 216 217 const sp<MediaCodecInfo> info = list->getCodecInfo(matchIndex); 218 CHECK(info != NULL); 219 const char *componentName = info->getCodecName(); 220 221 // If a specific codec is requested, skip the non-matching ones. 222 if (matchComponentName && strcmp(componentName, matchComponentName)) { 223 continue; 224 } 225 226 // When requesting software-only codecs, only push software codecs 227 // When requesting hardware-only codecs, only push hardware codecs 228 // When there is request neither for software-only nor for 229 // hardware-only codecs, push all codecs 230 if (((flags & kSoftwareCodecsOnly) && IsSoftwareCodec(componentName)) || 231 ((flags & kHardwareCodecsOnly) && !IsSoftwareCodec(componentName)) || 232 (!(flags & (kSoftwareCodecsOnly | kHardwareCodecsOnly)))) { 233 234 ssize_t index = matchingCodecs->add(); 235 CodecNameAndQuirks *entry = &matchingCodecs->editItemAt(index); 236 entry->mName = String8(componentName); 237 entry->mQuirks = getComponentQuirks(info); 238 239 ALOGV("matching '%s' quirks 0x%08x", 240 entry->mName.string(), entry->mQuirks); 241 } 242 } 243 244 if (flags & kPreferSoftwareCodecs) { 245 matchingCodecs->sort(CompareSoftwareCodecsFirst); 246 } 247 } 248 249 // static 250 uint32_t OMXCodec::getComponentQuirks( 251 const sp<MediaCodecInfo> &info) { 252 uint32_t quirks = 0; 253 if (info->hasQuirk("requires-allocate-on-input-ports")) { 254 quirks |= kRequiresAllocateBufferOnInputPorts; 255 } 256 if (info->hasQuirk("requires-allocate-on-output-ports")) { 257 quirks |= kRequiresAllocateBufferOnOutputPorts; 258 } 259 if (info->hasQuirk("output-buffers-are-unreadable")) { 260 quirks |= kOutputBuffersAreUnreadable; 261 } 262 263 return quirks; 264 } 265 266 // static 267 bool OMXCodec::findCodecQuirks(const char *componentName, uint32_t *quirks) { 268 const sp<IMediaCodecList> list = MediaCodecList::getInstance(); 269 if (list == NULL) { 270 return false; 271 } 272 273 ssize_t index = list->findCodecByName(componentName); 274 275 if (index < 0) { 276 return false; 277 } 278 279 const sp<MediaCodecInfo> info = list->getCodecInfo(index); 280 CHECK(info != NULL); 281 *quirks = getComponentQuirks(info); 282 283 return true; 284 } 285 286 // static 287 sp<MediaSource> OMXCodec::Create( 288 const sp<IOMX> &omx, 289 const sp<MetaData> &meta, bool createEncoder, 290 const sp<MediaSource> &source, 291 const char *matchComponentName, 292 uint32_t flags, 293 const sp<ANativeWindow> &nativeWindow) { 294 int32_t requiresSecureBuffers; 295 if (source->getFormat()->findInt32( 296 kKeyRequiresSecureBuffers, 297 &requiresSecureBuffers) 298 && requiresSecureBuffers) { 299 flags |= kIgnoreCodecSpecificData; 300 flags |= kUseSecureInputBuffers; 301 } 302 303 const char *mime; 304 bool success = meta->findCString(kKeyMIMEType, &mime); 305 CHECK(success); 306 307 Vector<CodecNameAndQuirks> matchingCodecs; 308 findMatchingCodecs( 309 mime, createEncoder, matchComponentName, flags, &matchingCodecs); 310 311 if (matchingCodecs.isEmpty()) { 312 ALOGV("No matching codecs! (mime: %s, createEncoder: %s, " 313 "matchComponentName: %s, flags: 0x%x)", 314 mime, createEncoder ? "true" : "false", matchComponentName, flags); 315 return NULL; 316 } 317 318 sp<OMXCodecObserver> observer = new OMXCodecObserver; 319 IOMX::node_id node = 0; 320 321 for (size_t i = 0; i < matchingCodecs.size(); ++i) { 322 const char *componentNameBase = matchingCodecs[i].mName.string(); 323 uint32_t quirks = matchingCodecs[i].mQuirks; 324 const char *componentName = componentNameBase; 325 326 AString tmp; 327 if (flags & kUseSecureInputBuffers) { 328 tmp = componentNameBase; 329 tmp.append(".secure"); 330 331 componentName = tmp.c_str(); 332 } 333 334 if (createEncoder) { 335 sp<MediaSource> softwareCodec = 336 InstantiateSoftwareEncoder(componentName, source, meta); 337 338 if (softwareCodec != NULL) { 339 ALOGV("Successfully allocated software codec '%s'", componentName); 340 341 return softwareCodec; 342 } 343 } 344 345 ALOGV("Attempting to allocate OMX node '%s'", componentName); 346 347 if (!createEncoder 348 && (quirks & kOutputBuffersAreUnreadable) 349 && (flags & kClientNeedsFramebuffer)) { 350 if (strncmp(componentName, "OMX.SEC.", 8)) { 351 // For OMX.SEC.* decoders we can enable a special mode that 352 // gives the client access to the framebuffer contents. 353 354 ALOGW("Component '%s' does not give the client access to " 355 "the framebuffer contents. Skipping.", 356 componentName); 357 358 continue; 359 } 360 } 361 362 status_t err = omx->allocateNode(componentName, observer, &node); 363 if (err == OK) { 364 ALOGV("Successfully allocated OMX node '%s'", componentName); 365 366 sp<OMXCodec> codec = new OMXCodec( 367 omx, node, quirks, flags, 368 createEncoder, mime, componentName, 369 source, nativeWindow); 370 371 observer->setCodec(codec); 372 373 err = codec->configureCodec(meta); 374 if (err == OK) { 375 return codec; 376 } 377 378 ALOGV("Failed to configure codec '%s'", componentName); 379 } 380 } 381 382 return NULL; 383 } 384 385 status_t OMXCodec::parseHEVCCodecSpecificData( 386 const void *data, size_t size, 387 unsigned *profile, unsigned *level) { 388 const uint8_t *ptr = (const uint8_t *)data; 389 390 // verify minimum size and configurationVersion == 1. 391 if (size < 7 || ptr[0] != 1) { 392 return ERROR_MALFORMED; 393 } 394 395 *profile = (ptr[1] & 31); 396 *level = ptr[12]; 397 398 ptr += 22; 399 size -= 22; 400 401 size_t numofArrays = (char)ptr[0]; 402 ptr += 1; 403 size -= 1; 404 size_t j = 0, i = 0; 405 for (i = 0; i < numofArrays; i++) { 406 ptr += 1; 407 size -= 1; 408 409 // Num of nals 410 size_t numofNals = U16_AT(ptr); 411 ptr += 2; 412 size -= 2; 413 414 for (j = 0;j < numofNals;j++) { 415 if (size < 2) { 416 return ERROR_MALFORMED; 417 } 418 419 size_t length = U16_AT(ptr); 420 421 ptr += 2; 422 size -= 2; 423 424 if (size < length) { 425 return ERROR_MALFORMED; 426 } 427 addCodecSpecificData(ptr, length); 428 429 ptr += length; 430 size -= length; 431 } 432 } 433 return OK; 434 } 435 436 status_t OMXCodec::parseAVCCodecSpecificData( 437 const void *data, size_t size, 438 unsigned *profile, unsigned *level) { 439 const uint8_t *ptr = (const uint8_t *)data; 440 441 // verify minimum size and configurationVersion == 1. 442 if (size < 7 || ptr[0] != 1) { 443 return ERROR_MALFORMED; 444 } 445 446 *profile = ptr[1]; 447 *level = ptr[3]; 448 449 // There is decodable content out there that fails the following 450 // assertion, let's be lenient for now... 451 // CHECK((ptr[4] >> 2) == 0x3f); // reserved 452 453 size_t lengthSize = 1 + (ptr[4] & 3); 454 455 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp 456 // violates it... 457 // CHECK((ptr[5] >> 5) == 7); // reserved 458 459 size_t numSeqParameterSets = ptr[5] & 31; 460 461 ptr += 6; 462 size -= 6; 463 464 for (size_t i = 0; i < numSeqParameterSets; ++i) { 465 if (size < 2) { 466 return ERROR_MALFORMED; 467 } 468 469 size_t length = U16_AT(ptr); 470 471 ptr += 2; 472 size -= 2; 473 474 if (size < length) { 475 return ERROR_MALFORMED; 476 } 477 478 addCodecSpecificData(ptr, length); 479 480 ptr += length; 481 size -= length; 482 } 483 484 if (size < 1) { 485 return ERROR_MALFORMED; 486 } 487 488 size_t numPictureParameterSets = *ptr; 489 ++ptr; 490 --size; 491 492 for (size_t i = 0; i < numPictureParameterSets; ++i) { 493 if (size < 2) { 494 return ERROR_MALFORMED; 495 } 496 497 size_t length = U16_AT(ptr); 498 499 ptr += 2; 500 size -= 2; 501 502 if (size < length) { 503 return ERROR_MALFORMED; 504 } 505 506 addCodecSpecificData(ptr, length); 507 508 ptr += length; 509 size -= length; 510 } 511 512 return OK; 513 } 514 515 status_t OMXCodec::configureCodec(const sp<MetaData> &meta) { 516 ALOGV("configureCodec protected=%d", 517 (mFlags & kEnableGrallocUsageProtected) ? 1 : 0); 518 519 if (!(mFlags & kIgnoreCodecSpecificData)) { 520 uint32_t type; 521 const void *data; 522 size_t size; 523 if (meta->findData(kKeyESDS, &type, &data, &size)) { 524 ESDS esds((const char *)data, size); 525 CHECK_EQ(esds.InitCheck(), (status_t)OK); 526 527 const void *codec_specific_data; 528 size_t codec_specific_data_size; 529 esds.getCodecSpecificInfo( 530 &codec_specific_data, &codec_specific_data_size); 531 532 addCodecSpecificData( 533 codec_specific_data, codec_specific_data_size); 534 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) { 535 // Parse the AVCDecoderConfigurationRecord 536 537 unsigned profile, level; 538 status_t err; 539 if ((err = parseAVCCodecSpecificData( 540 data, size, &profile, &level)) != OK) { 541 ALOGE("Malformed AVC codec specific data."); 542 return err; 543 } 544 545 CODEC_LOGI( 546 "AVC profile = %u (%s), level = %u", 547 profile, AVCProfileToString(profile), level); 548 } else if (meta->findData(kKeyHVCC, &type, &data, &size)) { 549 // Parse the HEVCDecoderConfigurationRecord 550 551 unsigned profile, level; 552 status_t err; 553 if ((err = parseHEVCCodecSpecificData( 554 data, size, &profile, &level)) != OK) { 555 ALOGE("Malformed HEVC codec specific data."); 556 return err; 557 } 558 559 CODEC_LOGI( 560 "HEVC profile = %u , level = %u", 561 profile, level); 562 } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) { 563 addCodecSpecificData(data, size); 564 565 CHECK(meta->findData(kKeyVorbisBooks, &type, &data, &size)); 566 addCodecSpecificData(data, size); 567 } else if (meta->findData(kKeyOpusHeader, &type, &data, &size)) { 568 addCodecSpecificData(data, size); 569 570 CHECK(meta->findData(kKeyOpusCodecDelay, &type, &data, &size)); 571 addCodecSpecificData(data, size); 572 CHECK(meta->findData(kKeyOpusSeekPreRoll, &type, &data, &size)); 573 addCodecSpecificData(data, size); 574 } 575 } 576 577 int32_t bitRate = 0; 578 if (mIsEncoder) { 579 CHECK(meta->findInt32(kKeyBitRate, &bitRate)); 580 } 581 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) { 582 setAMRFormat(false /* isWAMR */, bitRate); 583 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) { 584 setAMRFormat(true /* isWAMR */, bitRate); 585 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) { 586 int32_t numChannels, sampleRate, aacProfile; 587 CHECK(meta->findInt32(kKeyChannelCount, &numChannels)); 588 CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); 589 590 if (!meta->findInt32(kKeyAACProfile, &aacProfile)) { 591 aacProfile = OMX_AUDIO_AACObjectNull; 592 } 593 594 int32_t isADTS; 595 if (!meta->findInt32(kKeyIsADTS, &isADTS)) { 596 isADTS = false; 597 } 598 599 status_t err = setAACFormat(numChannels, sampleRate, bitRate, aacProfile, isADTS); 600 if (err != OK) { 601 CODEC_LOGE("setAACFormat() failed (err = %d)", err); 602 return err; 603 } 604 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_MPEG, mMIME)) { 605 int32_t numChannels, sampleRate; 606 if (meta->findInt32(kKeyChannelCount, &numChannels) 607 && meta->findInt32(kKeySampleRate, &sampleRate)) { 608 // Since we did not always check for these, leave them optional 609 // and have the decoder figure it all out. 610 setRawAudioFormat( 611 mIsEncoder ? kPortIndexInput : kPortIndexOutput, 612 sampleRate, 613 numChannels); 614 } 615 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AC3, mMIME)) { 616 int32_t numChannels; 617 int32_t sampleRate; 618 CHECK(meta->findInt32(kKeyChannelCount, &numChannels)); 619 CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); 620 621 status_t err = setAC3Format(numChannels, sampleRate); 622 if (err != OK) { 623 CODEC_LOGE("setAC3Format() failed (err = %d)", err); 624 return err; 625 } 626 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_G711_ALAW, mMIME) 627 || !strcasecmp(MEDIA_MIMETYPE_AUDIO_G711_MLAW, mMIME)) { 628 // These are PCM-like formats with a fixed sample rate but 629 // a variable number of channels. 630 631 int32_t numChannels; 632 CHECK(meta->findInt32(kKeyChannelCount, &numChannels)); 633 634 setG711Format(numChannels); 635 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mMIME)) { 636 CHECK(!mIsEncoder); 637 638 int32_t numChannels, sampleRate; 639 CHECK(meta->findInt32(kKeyChannelCount, &numChannels)); 640 CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); 641 642 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 643 } 644 645 if (!strncasecmp(mMIME, "video/", 6)) { 646 647 if (mIsEncoder) { 648 setVideoInputFormat(mMIME, meta); 649 } else { 650 status_t err = setVideoOutputFormat( 651 mMIME, meta); 652 653 if (err != OK) { 654 return err; 655 } 656 } 657 } 658 659 int32_t maxInputSize; 660 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) { 661 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize); 662 } 663 664 initOutputFormat(meta); 665 666 if ((mFlags & kClientNeedsFramebuffer) 667 && !strncmp(mComponentName, "OMX.SEC.", 8)) { 668 // This appears to no longer be needed??? 669 670 OMX_INDEXTYPE index; 671 672 status_t err = 673 mOMX->getExtensionIndex( 674 mNode, 675 "OMX.SEC.index.ThumbnailMode", 676 &index); 677 678 if (err != OK) { 679 return err; 680 } 681 682 OMX_BOOL enable = OMX_TRUE; 683 err = mOMX->setConfig(mNode, index, &enable, sizeof(enable)); 684 685 if (err != OK) { 686 CODEC_LOGE("setConfig('OMX.SEC.index.ThumbnailMode') " 687 "returned error 0x%08x", err); 688 689 return err; 690 } 691 692 mQuirks &= ~kOutputBuffersAreUnreadable; 693 } 694 695 if (mNativeWindow != NULL 696 && !mIsEncoder 697 && !strncasecmp(mMIME, "video/", 6) 698 && !strncmp(mComponentName, "OMX.", 4)) { 699 status_t err = initNativeWindow(); 700 if (err != OK) { 701 return err; 702 } 703 } 704 705 return OK; 706 } 707 708 void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) { 709 OMX_PARAM_PORTDEFINITIONTYPE def; 710 InitOMXParams(&def); 711 def.nPortIndex = portIndex; 712 713 status_t err = mOMX->getParameter( 714 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 715 CHECK_EQ(err, (status_t)OK); 716 717 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus)) 718 || (def.nBufferSize < size)) { 719 def.nBufferSize = size; 720 } 721 722 err = mOMX->setParameter( 723 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 724 CHECK_EQ(err, (status_t)OK); 725 726 err = mOMX->getParameter( 727 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 728 CHECK_EQ(err, (status_t)OK); 729 730 // Make sure the setting actually stuck. 731 if (portIndex == kPortIndexInput 732 && (mQuirks & kInputBufferSizesAreBogus)) { 733 CHECK_EQ(def.nBufferSize, size); 734 } else { 735 CHECK(def.nBufferSize >= size); 736 } 737 } 738 739 status_t OMXCodec::setVideoPortFormatType( 740 OMX_U32 portIndex, 741 OMX_VIDEO_CODINGTYPE compressionFormat, 742 OMX_COLOR_FORMATTYPE colorFormat) { 743 OMX_VIDEO_PARAM_PORTFORMATTYPE format; 744 InitOMXParams(&format); 745 format.nPortIndex = portIndex; 746 format.nIndex = 0; 747 bool found = false; 748 749 OMX_U32 index = 0; 750 for (;;) { 751 format.nIndex = index; 752 status_t err = mOMX->getParameter( 753 mNode, OMX_IndexParamVideoPortFormat, 754 &format, sizeof(format)); 755 756 if (err != OK) { 757 return err; 758 } 759 760 // The following assertion is violated by TI's video decoder. 761 // CHECK_EQ(format.nIndex, index); 762 763 #if 1 764 CODEC_LOGV("portIndex: %u, index: %u, eCompressionFormat=%d eColorFormat=%d", 765 portIndex, 766 index, format.eCompressionFormat, format.eColorFormat); 767 #endif 768 769 if (format.eCompressionFormat == compressionFormat 770 && format.eColorFormat == colorFormat) { 771 found = true; 772 break; 773 } 774 775 ++index; 776 if (index >= kMaxColorFormatSupported) { 777 CODEC_LOGE("color format %d or compression format %d is not supported", 778 colorFormat, compressionFormat); 779 return UNKNOWN_ERROR; 780 } 781 } 782 783 if (!found) { 784 return UNKNOWN_ERROR; 785 } 786 787 CODEC_LOGV("found a match."); 788 status_t err = mOMX->setParameter( 789 mNode, OMX_IndexParamVideoPortFormat, 790 &format, sizeof(format)); 791 792 return err; 793 } 794 795 static size_t getFrameSize( 796 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) { 797 switch (colorFormat) { 798 case OMX_COLOR_FormatYCbYCr: 799 case OMX_COLOR_FormatCbYCrY: 800 return width * height * 2; 801 802 case OMX_COLOR_FormatYUV420Planar: 803 case OMX_COLOR_FormatYUV420SemiPlanar: 804 case OMX_TI_COLOR_FormatYUV420PackedSemiPlanar: 805 /* 806 * FIXME: For the Opaque color format, the frame size does not 807 * need to be (w*h*3)/2. It just needs to 808 * be larger than certain minimum buffer size. However, 809 * currently, this opaque foramt has been tested only on 810 * YUV420 formats. If that is changed, then we need to revisit 811 * this part in the future 812 */ 813 case OMX_COLOR_FormatAndroidOpaque: 814 return (width * height * 3) / 2; 815 816 default: 817 CHECK(!"Should not be here. Unsupported color format."); 818 break; 819 } 820 } 821 822 status_t OMXCodec::findTargetColorFormat( 823 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) { 824 ALOGV("findTargetColorFormat"); 825 CHECK(mIsEncoder); 826 827 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar; 828 int32_t targetColorFormat; 829 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) { 830 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat; 831 } 832 833 // Check whether the target color format is supported. 834 return isColorFormatSupported(*colorFormat, kPortIndexInput); 835 } 836 837 status_t OMXCodec::isColorFormatSupported( 838 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) { 839 ALOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat)); 840 841 // Enumerate all the color formats supported by 842 // the omx component to see whether the given 843 // color format is supported. 844 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat; 845 InitOMXParams(&portFormat); 846 portFormat.nPortIndex = portIndex; 847 OMX_U32 index = 0; 848 portFormat.nIndex = index; 849 while (true) { 850 if (OMX_ErrorNone != mOMX->getParameter( 851 mNode, OMX_IndexParamVideoPortFormat, 852 &portFormat, sizeof(portFormat))) { 853 break; 854 } 855 // Make sure that omx component does not overwrite 856 // the incremented index (bug 2897413). 857 CHECK_EQ(index, portFormat.nIndex); 858 if (portFormat.eColorFormat == colorFormat) { 859 CODEC_LOGV("Found supported color format: %d", portFormat.eColorFormat); 860 return OK; // colorFormat is supported! 861 } 862 ++index; 863 portFormat.nIndex = index; 864 865 if (index >= kMaxColorFormatSupported) { 866 CODEC_LOGE("More than %u color formats are supported???", index); 867 break; 868 } 869 } 870 871 CODEC_LOGE("color format %d is not supported", colorFormat); 872 return UNKNOWN_ERROR; 873 } 874 875 void OMXCodec::setVideoInputFormat( 876 const char *mime, const sp<MetaData>& meta) { 877 878 int32_t width, height, frameRate, bitRate, stride, sliceHeight; 879 bool success = meta->findInt32(kKeyWidth, &width); 880 success = success && meta->findInt32(kKeyHeight, &height); 881 success = success && meta->findInt32(kKeyFrameRate, &frameRate); 882 success = success && meta->findInt32(kKeyBitRate, &bitRate); 883 success = success && meta->findInt32(kKeyStride, &stride); 884 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight); 885 CHECK(success); 886 CHECK(stride != 0); 887 888 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused; 889 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 890 compressionFormat = OMX_VIDEO_CodingAVC; 891 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_HEVC, mime)) { 892 compressionFormat = OMX_VIDEO_CodingHEVC; 893 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 894 compressionFormat = OMX_VIDEO_CodingMPEG4; 895 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 896 compressionFormat = OMX_VIDEO_CodingH263; 897 } else { 898 ALOGE("Not a supported video mime type: %s", mime); 899 CHECK(!"Should not be here. Not a supported video mime type."); 900 } 901 902 OMX_COLOR_FORMATTYPE colorFormat; 903 CHECK_EQ((status_t)OK, findTargetColorFormat(meta, &colorFormat)); 904 905 status_t err; 906 OMX_PARAM_PORTDEFINITIONTYPE def; 907 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 908 909 //////////////////////// Input port ///////////////////////// 910 CHECK_EQ(setVideoPortFormatType( 911 kPortIndexInput, OMX_VIDEO_CodingUnused, 912 colorFormat), (status_t)OK); 913 914 InitOMXParams(&def); 915 def.nPortIndex = kPortIndexInput; 916 917 err = mOMX->getParameter( 918 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 919 CHECK_EQ(err, (status_t)OK); 920 921 def.nBufferSize = getFrameSize(colorFormat, 922 stride > 0? stride: -stride, sliceHeight); 923 924 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo); 925 926 video_def->nFrameWidth = width; 927 video_def->nFrameHeight = height; 928 video_def->nStride = stride; 929 video_def->nSliceHeight = sliceHeight; 930 video_def->xFramerate = (frameRate << 16); // Q16 format 931 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused; 932 video_def->eColorFormat = colorFormat; 933 934 err = mOMX->setParameter( 935 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 936 CHECK_EQ(err, (status_t)OK); 937 938 //////////////////////// Output port ///////////////////////// 939 CHECK_EQ(setVideoPortFormatType( 940 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused), 941 (status_t)OK); 942 InitOMXParams(&def); 943 def.nPortIndex = kPortIndexOutput; 944 945 err = mOMX->getParameter( 946 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 947 948 CHECK_EQ(err, (status_t)OK); 949 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo); 950 951 video_def->nFrameWidth = width; 952 video_def->nFrameHeight = height; 953 video_def->xFramerate = 0; // No need for output port 954 video_def->nBitrate = bitRate; // Q16 format 955 video_def->eCompressionFormat = compressionFormat; 956 video_def->eColorFormat = OMX_COLOR_FormatUnused; 957 if (mQuirks & kRequiresLargerEncoderOutputBuffer) { 958 // Increases the output buffer size 959 def.nBufferSize = ((def.nBufferSize * 3) >> 1); 960 } 961 962 err = mOMX->setParameter( 963 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 964 CHECK_EQ(err, (status_t)OK); 965 966 /////////////////// Codec-specific //////////////////////// 967 switch (compressionFormat) { 968 case OMX_VIDEO_CodingMPEG4: 969 { 970 CHECK_EQ(setupMPEG4EncoderParameters(meta), (status_t)OK); 971 break; 972 } 973 974 case OMX_VIDEO_CodingH263: 975 CHECK_EQ(setupH263EncoderParameters(meta), (status_t)OK); 976 break; 977 978 case OMX_VIDEO_CodingAVC: 979 { 980 CHECK_EQ(setupAVCEncoderParameters(meta), (status_t)OK); 981 break; 982 } 983 984 default: 985 CHECK(!"Support for this compressionFormat to be implemented."); 986 break; 987 } 988 } 989 990 static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) { 991 if (iFramesInterval < 0) { 992 return 0xFFFFFFFF; 993 } else if (iFramesInterval == 0) { 994 return 0; 995 } 996 OMX_U32 ret = frameRate * iFramesInterval - 1; 997 return ret; 998 } 999 1000 status_t OMXCodec::setupErrorCorrectionParameters() { 1001 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType; 1002 InitOMXParams(&errorCorrectionType); 1003 errorCorrectionType.nPortIndex = kPortIndexOutput; 1004 1005 status_t err = mOMX->getParameter( 1006 mNode, OMX_IndexParamVideoErrorCorrection, 1007 &errorCorrectionType, sizeof(errorCorrectionType)); 1008 if (err != OK) { 1009 ALOGW("Error correction param query is not supported"); 1010 return OK; // Optional feature. Ignore this failure 1011 } 1012 1013 errorCorrectionType.bEnableHEC = OMX_FALSE; 1014 errorCorrectionType.bEnableResync = OMX_TRUE; 1015 errorCorrectionType.nResynchMarkerSpacing = 256; 1016 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE; 1017 errorCorrectionType.bEnableRVLC = OMX_FALSE; 1018 1019 err = mOMX->setParameter( 1020 mNode, OMX_IndexParamVideoErrorCorrection, 1021 &errorCorrectionType, sizeof(errorCorrectionType)); 1022 if (err != OK) { 1023 ALOGW("Error correction param configuration is not supported"); 1024 } 1025 1026 // Optional feature. Ignore the failure. 1027 return OK; 1028 } 1029 1030 status_t OMXCodec::setupBitRate(int32_t bitRate) { 1031 OMX_VIDEO_PARAM_BITRATETYPE bitrateType; 1032 InitOMXParams(&bitrateType); 1033 bitrateType.nPortIndex = kPortIndexOutput; 1034 1035 status_t err = mOMX->getParameter( 1036 mNode, OMX_IndexParamVideoBitrate, 1037 &bitrateType, sizeof(bitrateType)); 1038 CHECK_EQ(err, (status_t)OK); 1039 1040 bitrateType.eControlRate = OMX_Video_ControlRateVariable; 1041 bitrateType.nTargetBitrate = bitRate; 1042 1043 err = mOMX->setParameter( 1044 mNode, OMX_IndexParamVideoBitrate, 1045 &bitrateType, sizeof(bitrateType)); 1046 CHECK_EQ(err, (status_t)OK); 1047 return OK; 1048 } 1049 1050 status_t OMXCodec::getVideoProfileLevel( 1051 const sp<MetaData>& meta, 1052 const CodecProfileLevel& defaultProfileLevel, 1053 CodecProfileLevel &profileLevel) { 1054 CODEC_LOGV("Default profile: %ld, level %ld", 1055 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel); 1056 1057 // Are the default profile and level overwriten? 1058 int32_t profile, level; 1059 if (!meta->findInt32(kKeyVideoProfile, &profile)) { 1060 profile = defaultProfileLevel.mProfile; 1061 } 1062 if (!meta->findInt32(kKeyVideoLevel, &level)) { 1063 level = defaultProfileLevel.mLevel; 1064 } 1065 CODEC_LOGV("Target profile: %d, level: %d", profile, level); 1066 1067 // Are the target profile and level supported by the encoder? 1068 OMX_VIDEO_PARAM_PROFILELEVELTYPE param; 1069 InitOMXParams(¶m); 1070 param.nPortIndex = kPortIndexOutput; 1071 for (param.nProfileIndex = 0;; ++param.nProfileIndex) { 1072 status_t err = mOMX->getParameter( 1073 mNode, OMX_IndexParamVideoProfileLevelQuerySupported, 1074 ¶m, sizeof(param)); 1075 1076 if (err != OK) break; 1077 1078 int32_t supportedProfile = static_cast<int32_t>(param.eProfile); 1079 int32_t supportedLevel = static_cast<int32_t>(param.eLevel); 1080 CODEC_LOGV("Supported profile: %d, level %d", 1081 supportedProfile, supportedLevel); 1082 1083 if (profile == supportedProfile && 1084 level <= supportedLevel) { 1085 // We can further check whether the level is a valid 1086 // value; but we will leave that to the omx encoder component 1087 // via OMX_SetParameter call. 1088 profileLevel.mProfile = profile; 1089 profileLevel.mLevel = level; 1090 return OK; 1091 } 1092 } 1093 1094 CODEC_LOGE("Target profile (%d) and level (%d) is not supported", 1095 profile, level); 1096 return BAD_VALUE; 1097 } 1098 1099 status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) { 1100 int32_t iFramesInterval, frameRate, bitRate; 1101 bool success = meta->findInt32(kKeyBitRate, &bitRate); 1102 success = success && meta->findInt32(kKeyFrameRate, &frameRate); 1103 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); 1104 CHECK(success); 1105 OMX_VIDEO_PARAM_H263TYPE h263type; 1106 InitOMXParams(&h263type); 1107 h263type.nPortIndex = kPortIndexOutput; 1108 1109 status_t err = mOMX->getParameter( 1110 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type)); 1111 CHECK_EQ(err, (status_t)OK); 1112 1113 h263type.nAllowedPictureTypes = 1114 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; 1115 1116 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); 1117 if (h263type.nPFrames == 0) { 1118 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; 1119 } 1120 h263type.nBFrames = 0; 1121 1122 // Check profile and level parameters 1123 CodecProfileLevel defaultProfileLevel, profileLevel; 1124 defaultProfileLevel.mProfile = h263type.eProfile; 1125 defaultProfileLevel.mLevel = h263type.eLevel; 1126 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel); 1127 if (err != OK) return err; 1128 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile); 1129 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel); 1130 1131 h263type.bPLUSPTYPEAllowed = OMX_FALSE; 1132 h263type.bForceRoundingTypeToZero = OMX_FALSE; 1133 h263type.nPictureHeaderRepetition = 0; 1134 h263type.nGOBHeaderInterval = 0; 1135 1136 err = mOMX->setParameter( 1137 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type)); 1138 CHECK_EQ(err, (status_t)OK); 1139 1140 CHECK_EQ(setupBitRate(bitRate), (status_t)OK); 1141 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK); 1142 1143 return OK; 1144 } 1145 1146 status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) { 1147 int32_t iFramesInterval, frameRate, bitRate; 1148 bool success = meta->findInt32(kKeyBitRate, &bitRate); 1149 success = success && meta->findInt32(kKeyFrameRate, &frameRate); 1150 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); 1151 CHECK(success); 1152 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type; 1153 InitOMXParams(&mpeg4type); 1154 mpeg4type.nPortIndex = kPortIndexOutput; 1155 1156 status_t err = mOMX->getParameter( 1157 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type)); 1158 CHECK_EQ(err, (status_t)OK); 1159 1160 mpeg4type.nSliceHeaderSpacing = 0; 1161 mpeg4type.bSVH = OMX_FALSE; 1162 mpeg4type.bGov = OMX_FALSE; 1163 1164 mpeg4type.nAllowedPictureTypes = 1165 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; 1166 1167 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); 1168 if (mpeg4type.nPFrames == 0) { 1169 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; 1170 } 1171 mpeg4type.nBFrames = 0; 1172 mpeg4type.nIDCVLCThreshold = 0; 1173 mpeg4type.bACPred = OMX_TRUE; 1174 mpeg4type.nMaxPacketSize = 256; 1175 mpeg4type.nTimeIncRes = 1000; 1176 mpeg4type.nHeaderExtension = 0; 1177 mpeg4type.bReversibleVLC = OMX_FALSE; 1178 1179 // Check profile and level parameters 1180 CodecProfileLevel defaultProfileLevel, profileLevel; 1181 defaultProfileLevel.mProfile = mpeg4type.eProfile; 1182 defaultProfileLevel.mLevel = mpeg4type.eLevel; 1183 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel); 1184 if (err != OK) return err; 1185 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile); 1186 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel); 1187 1188 err = mOMX->setParameter( 1189 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type)); 1190 CHECK_EQ(err, (status_t)OK); 1191 1192 CHECK_EQ(setupBitRate(bitRate), (status_t)OK); 1193 CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK); 1194 1195 return OK; 1196 } 1197 1198 status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) { 1199 int32_t iFramesInterval, frameRate, bitRate; 1200 bool success = meta->findInt32(kKeyBitRate, &bitRate); 1201 success = success && meta->findInt32(kKeyFrameRate, &frameRate); 1202 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); 1203 CHECK(success); 1204 1205 OMX_VIDEO_PARAM_AVCTYPE h264type; 1206 InitOMXParams(&h264type); 1207 h264type.nPortIndex = kPortIndexOutput; 1208 1209 status_t err = mOMX->getParameter( 1210 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type)); 1211 CHECK_EQ(err, (status_t)OK); 1212 1213 h264type.nAllowedPictureTypes = 1214 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; 1215 1216 // Check profile and level parameters 1217 CodecProfileLevel defaultProfileLevel, profileLevel; 1218 defaultProfileLevel.mProfile = h264type.eProfile; 1219 defaultProfileLevel.mLevel = h264type.eLevel; 1220 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel); 1221 if (err != OK) return err; 1222 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile); 1223 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel); 1224 1225 // XXX 1226 if (h264type.eProfile != OMX_VIDEO_AVCProfileBaseline) { 1227 ALOGW("Use baseline profile instead of %d for AVC recording", 1228 h264type.eProfile); 1229 h264type.eProfile = OMX_VIDEO_AVCProfileBaseline; 1230 } 1231 1232 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) { 1233 h264type.nSliceHeaderSpacing = 0; 1234 h264type.bUseHadamard = OMX_TRUE; 1235 h264type.nRefFrames = 1; 1236 h264type.nBFrames = 0; 1237 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); 1238 if (h264type.nPFrames == 0) { 1239 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; 1240 } 1241 h264type.nRefIdx10ActiveMinus1 = 0; 1242 h264type.nRefIdx11ActiveMinus1 = 0; 1243 h264type.bEntropyCodingCABAC = OMX_FALSE; 1244 h264type.bWeightedPPrediction = OMX_FALSE; 1245 h264type.bconstIpred = OMX_FALSE; 1246 h264type.bDirect8x8Inference = OMX_FALSE; 1247 h264type.bDirectSpatialTemporal = OMX_FALSE; 1248 h264type.nCabacInitIdc = 0; 1249 } 1250 1251 if (h264type.nBFrames != 0) { 1252 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB; 1253 } 1254 1255 h264type.bEnableUEP = OMX_FALSE; 1256 h264type.bEnableFMO = OMX_FALSE; 1257 h264type.bEnableASO = OMX_FALSE; 1258 h264type.bEnableRS = OMX_FALSE; 1259 h264type.bFrameMBsOnly = OMX_TRUE; 1260 h264type.bMBAFF = OMX_FALSE; 1261 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable; 1262 1263 err = mOMX->setParameter( 1264 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type)); 1265 CHECK_EQ(err, (status_t)OK); 1266 1267 CHECK_EQ(setupBitRate(bitRate), (status_t)OK); 1268 1269 return OK; 1270 } 1271 1272 status_t OMXCodec::setVideoOutputFormat( 1273 const char *mime, const sp<MetaData>& meta) { 1274 1275 int32_t width, height; 1276 bool success = meta->findInt32(kKeyWidth, &width); 1277 success = success && meta->findInt32(kKeyHeight, &height); 1278 CHECK(success); 1279 1280 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height); 1281 1282 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused; 1283 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 1284 compressionFormat = OMX_VIDEO_CodingAVC; 1285 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 1286 compressionFormat = OMX_VIDEO_CodingMPEG4; 1287 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_HEVC, mime)) { 1288 compressionFormat = OMX_VIDEO_CodingHEVC; 1289 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 1290 compressionFormat = OMX_VIDEO_CodingH263; 1291 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_VP8, mime)) { 1292 compressionFormat = OMX_VIDEO_CodingVP8; 1293 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_VP9, mime)) { 1294 compressionFormat = OMX_VIDEO_CodingVP9; 1295 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG2, mime)) { 1296 compressionFormat = OMX_VIDEO_CodingMPEG2; 1297 } else { 1298 ALOGE("Not a supported video mime type: %s", mime); 1299 CHECK(!"Should not be here. Not a supported video mime type."); 1300 } 1301 1302 status_t err = setVideoPortFormatType( 1303 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused); 1304 1305 if (err != OK) { 1306 return err; 1307 } 1308 1309 #if 1 1310 { 1311 OMX_VIDEO_PARAM_PORTFORMATTYPE format; 1312 InitOMXParams(&format); 1313 format.nPortIndex = kPortIndexOutput; 1314 format.nIndex = 0; 1315 1316 status_t err = mOMX->getParameter( 1317 mNode, OMX_IndexParamVideoPortFormat, 1318 &format, sizeof(format)); 1319 CHECK_EQ(err, (status_t)OK); 1320 CHECK_EQ((int)format.eCompressionFormat, (int)OMX_VIDEO_CodingUnused); 1321 1322 int32_t colorFormat; 1323 if (meta->findInt32(kKeyColorFormat, &colorFormat) 1324 && colorFormat != OMX_COLOR_FormatUnused 1325 && colorFormat != format.eColorFormat) { 1326 1327 while (OMX_ErrorNoMore != err) { 1328 format.nIndex++; 1329 err = mOMX->getParameter( 1330 mNode, OMX_IndexParamVideoPortFormat, 1331 &format, sizeof(format)); 1332 if (format.eColorFormat == colorFormat) { 1333 break; 1334 } 1335 } 1336 if (format.eColorFormat != colorFormat) { 1337 CODEC_LOGE("Color format %d is not supported", colorFormat); 1338 return ERROR_UNSUPPORTED; 1339 } 1340 } 1341 1342 err = mOMX->setParameter( 1343 mNode, OMX_IndexParamVideoPortFormat, 1344 &format, sizeof(format)); 1345 1346 if (err != OK) { 1347 return err; 1348 } 1349 } 1350 #endif 1351 1352 OMX_PARAM_PORTDEFINITIONTYPE def; 1353 InitOMXParams(&def); 1354 def.nPortIndex = kPortIndexInput; 1355 1356 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 1357 1358 err = mOMX->getParameter( 1359 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1360 1361 CHECK_EQ(err, (status_t)OK); 1362 1363 #if 1 1364 // XXX Need a (much) better heuristic to compute input buffer sizes. 1365 const size_t X = 64 * 1024; 1366 if (def.nBufferSize < X) { 1367 def.nBufferSize = X; 1368 } 1369 #endif 1370 1371 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo); 1372 1373 video_def->nFrameWidth = width; 1374 video_def->nFrameHeight = height; 1375 1376 video_def->eCompressionFormat = compressionFormat; 1377 video_def->eColorFormat = OMX_COLOR_FormatUnused; 1378 1379 err = mOMX->setParameter( 1380 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1381 1382 if (err != OK) { 1383 return err; 1384 } 1385 1386 //////////////////////////////////////////////////////////////////////////// 1387 1388 InitOMXParams(&def); 1389 def.nPortIndex = kPortIndexOutput; 1390 1391 err = mOMX->getParameter( 1392 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1393 CHECK_EQ(err, (status_t)OK); 1394 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo); 1395 1396 #if 0 1397 def.nBufferSize = 1398 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420 1399 #endif 1400 1401 video_def->nFrameWidth = width; 1402 video_def->nFrameHeight = height; 1403 1404 err = mOMX->setParameter( 1405 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1406 1407 return err; 1408 } 1409 1410 OMXCodec::OMXCodec( 1411 const sp<IOMX> &omx, IOMX::node_id node, 1412 uint32_t quirks, uint32_t flags, 1413 bool isEncoder, 1414 const char *mime, 1415 const char *componentName, 1416 const sp<MediaSource> &source, 1417 const sp<ANativeWindow> &nativeWindow) 1418 : mOMX(omx), 1419 mOMXLivesLocally(omx->livesLocally(node, getpid())), 1420 mNode(node), 1421 mQuirks(quirks), 1422 mFlags(flags), 1423 mIsEncoder(isEncoder), 1424 mIsVideo(!strncasecmp("video/", mime, 6)), 1425 mMIME(strdup(mime)), 1426 mComponentName(strdup(componentName)), 1427 mSource(source), 1428 mCodecSpecificDataIndex(0), 1429 mState(LOADED), 1430 mInitialBufferSubmit(true), 1431 mSignalledEOS(false), 1432 mNoMoreOutputData(false), 1433 mOutputPortSettingsHaveChanged(false), 1434 mSeekTimeUs(-1), 1435 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC), 1436 mTargetTimeUs(-1), 1437 mOutputPortSettingsChangedPending(false), 1438 mSkipCutBuffer(NULL), 1439 mLeftOverBuffer(NULL), 1440 mPaused(false), 1441 mNativeWindow( 1442 (!strncmp(componentName, "OMX.google.", 11)) 1443 ? NULL : nativeWindow) { 1444 mPortStatus[kPortIndexInput] = ENABLED; 1445 mPortStatus[kPortIndexOutput] = ENABLED; 1446 1447 setComponentRole(); 1448 } 1449 1450 // static 1451 void OMXCodec::setComponentRole( 1452 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder, 1453 const char *mime) { 1454 struct MimeToRole { 1455 const char *mime; 1456 const char *decoderRole; 1457 const char *encoderRole; 1458 }; 1459 1460 static const MimeToRole kMimeToRole[] = { 1461 { MEDIA_MIMETYPE_AUDIO_MPEG, 1462 "audio_decoder.mp3", "audio_encoder.mp3" }, 1463 { MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I, 1464 "audio_decoder.mp1", "audio_encoder.mp1" }, 1465 { MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II, 1466 "audio_decoder.mp2", "audio_encoder.mp2" }, 1467 { MEDIA_MIMETYPE_AUDIO_AMR_NB, 1468 "audio_decoder.amrnb", "audio_encoder.amrnb" }, 1469 { MEDIA_MIMETYPE_AUDIO_AMR_WB, 1470 "audio_decoder.amrwb", "audio_encoder.amrwb" }, 1471 { MEDIA_MIMETYPE_AUDIO_AAC, 1472 "audio_decoder.aac", "audio_encoder.aac" }, 1473 { MEDIA_MIMETYPE_AUDIO_VORBIS, 1474 "audio_decoder.vorbis", "audio_encoder.vorbis" }, 1475 { MEDIA_MIMETYPE_AUDIO_OPUS, 1476 "audio_decoder.opus", "audio_encoder.opus" }, 1477 { MEDIA_MIMETYPE_AUDIO_G711_MLAW, 1478 "audio_decoder.g711mlaw", "audio_encoder.g711mlaw" }, 1479 { MEDIA_MIMETYPE_AUDIO_G711_ALAW, 1480 "audio_decoder.g711alaw", "audio_encoder.g711alaw" }, 1481 { MEDIA_MIMETYPE_VIDEO_AVC, 1482 "video_decoder.avc", "video_encoder.avc" }, 1483 { MEDIA_MIMETYPE_VIDEO_HEVC, 1484 "video_decoder.hevc", "video_encoder.hevc" }, 1485 { MEDIA_MIMETYPE_VIDEO_MPEG4, 1486 "video_decoder.mpeg4", "video_encoder.mpeg4" }, 1487 { MEDIA_MIMETYPE_VIDEO_H263, 1488 "video_decoder.h263", "video_encoder.h263" }, 1489 { MEDIA_MIMETYPE_VIDEO_VP8, 1490 "video_decoder.vp8", "video_encoder.vp8" }, 1491 { MEDIA_MIMETYPE_VIDEO_VP9, 1492 "video_decoder.vp9", "video_encoder.vp9" }, 1493 { MEDIA_MIMETYPE_AUDIO_RAW, 1494 "audio_decoder.raw", "audio_encoder.raw" }, 1495 { MEDIA_MIMETYPE_AUDIO_FLAC, 1496 "audio_decoder.flac", "audio_encoder.flac" }, 1497 { MEDIA_MIMETYPE_AUDIO_MSGSM, 1498 "audio_decoder.gsm", "audio_encoder.gsm" }, 1499 { MEDIA_MIMETYPE_VIDEO_MPEG2, 1500 "video_decoder.mpeg2", "video_encoder.mpeg2" }, 1501 { MEDIA_MIMETYPE_AUDIO_AC3, 1502 "audio_decoder.ac3", "audio_encoder.ac3" }, 1503 }; 1504 1505 static const size_t kNumMimeToRole = 1506 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]); 1507 1508 size_t i; 1509 for (i = 0; i < kNumMimeToRole; ++i) { 1510 if (!strcasecmp(mime, kMimeToRole[i].mime)) { 1511 break; 1512 } 1513 } 1514 1515 if (i == kNumMimeToRole) { 1516 return; 1517 } 1518 1519 const char *role = 1520 isEncoder ? kMimeToRole[i].encoderRole 1521 : kMimeToRole[i].decoderRole; 1522 1523 if (role != NULL) { 1524 OMX_PARAM_COMPONENTROLETYPE roleParams; 1525 InitOMXParams(&roleParams); 1526 1527 strncpy((char *)roleParams.cRole, 1528 role, OMX_MAX_STRINGNAME_SIZE - 1); 1529 1530 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0'; 1531 1532 status_t err = omx->setParameter( 1533 node, OMX_IndexParamStandardComponentRole, 1534 &roleParams, sizeof(roleParams)); 1535 1536 if (err != OK) { 1537 ALOGW("Failed to set standard component role '%s'.", role); 1538 } 1539 } 1540 } 1541 1542 void OMXCodec::setComponentRole() { 1543 setComponentRole(mOMX, mNode, mIsEncoder, mMIME); 1544 } 1545 1546 OMXCodec::~OMXCodec() { 1547 mSource.clear(); 1548 1549 CHECK(mState == LOADED || mState == ERROR || mState == LOADED_TO_IDLE); 1550 1551 status_t err = mOMX->freeNode(mNode); 1552 CHECK_EQ(err, (status_t)OK); 1553 1554 mNode = 0; 1555 setState(DEAD); 1556 1557 clearCodecSpecificData(); 1558 1559 free(mComponentName); 1560 mComponentName = NULL; 1561 1562 free(mMIME); 1563 mMIME = NULL; 1564 } 1565 1566 status_t OMXCodec::init() { 1567 // mLock is held. 1568 1569 CHECK_EQ((int)mState, (int)LOADED); 1570 1571 status_t err; 1572 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) { 1573 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 1574 CHECK_EQ(err, (status_t)OK); 1575 setState(LOADED_TO_IDLE); 1576 } 1577 1578 err = allocateBuffers(); 1579 if (err != (status_t)OK) { 1580 return err; 1581 } 1582 1583 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) { 1584 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 1585 CHECK_EQ(err, (status_t)OK); 1586 1587 setState(LOADED_TO_IDLE); 1588 } 1589 1590 while (mState != EXECUTING && mState != ERROR) { 1591 mAsyncCompletion.wait(mLock); 1592 } 1593 1594 return mState == ERROR ? UNKNOWN_ERROR : OK; 1595 } 1596 1597 // static 1598 bool OMXCodec::isIntermediateState(State state) { 1599 return state == LOADED_TO_IDLE 1600 || state == IDLE_TO_EXECUTING 1601 || state == EXECUTING_TO_IDLE 1602 || state == IDLE_TO_LOADED 1603 || state == RECONFIGURING; 1604 } 1605 1606 status_t OMXCodec::allocateBuffers() { 1607 status_t err = allocateBuffersOnPort(kPortIndexInput); 1608 1609 if (err != OK) { 1610 return err; 1611 } 1612 1613 return allocateBuffersOnPort(kPortIndexOutput); 1614 } 1615 1616 status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) { 1617 if (mNativeWindow != NULL && portIndex == kPortIndexOutput) { 1618 return allocateOutputBuffersFromNativeWindow(); 1619 } 1620 1621 if ((mFlags & kEnableGrallocUsageProtected) && portIndex == kPortIndexOutput) { 1622 ALOGE("protected output buffers must be stent to an ANativeWindow"); 1623 return PERMISSION_DENIED; 1624 } 1625 1626 status_t err = OK; 1627 if ((mFlags & kStoreMetaDataInVideoBuffers) 1628 && portIndex == kPortIndexInput) { 1629 err = mOMX->storeMetaDataInBuffers(mNode, kPortIndexInput, OMX_TRUE); 1630 if (err != OK) { 1631 ALOGE("Storing meta data in video buffers is not supported"); 1632 return err; 1633 } 1634 } 1635 1636 OMX_PARAM_PORTDEFINITIONTYPE def; 1637 InitOMXParams(&def); 1638 def.nPortIndex = portIndex; 1639 1640 err = mOMX->getParameter( 1641 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1642 1643 if (err != OK) { 1644 return err; 1645 } 1646 1647 CODEC_LOGV("allocating %lu buffers of size %lu on %s port", 1648 def.nBufferCountActual, def.nBufferSize, 1649 portIndex == kPortIndexInput ? "input" : "output"); 1650 1651 size_t totalSize = def.nBufferCountActual * def.nBufferSize; 1652 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec"); 1653 1654 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) { 1655 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize); 1656 CHECK(mem.get() != NULL); 1657 1658 BufferInfo info; 1659 info.mData = NULL; 1660 info.mSize = def.nBufferSize; 1661 1662 IOMX::buffer_id buffer; 1663 if (portIndex == kPortIndexInput 1664 && ((mQuirks & kRequiresAllocateBufferOnInputPorts) 1665 || (mFlags & kUseSecureInputBuffers))) { 1666 if (mOMXLivesLocally) { 1667 mem.clear(); 1668 1669 err = mOMX->allocateBuffer( 1670 mNode, portIndex, def.nBufferSize, &buffer, 1671 &info.mData); 1672 } else { 1673 err = mOMX->allocateBufferWithBackup( 1674 mNode, portIndex, mem, &buffer); 1675 } 1676 } else if (portIndex == kPortIndexOutput 1677 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) { 1678 if (mOMXLivesLocally) { 1679 mem.clear(); 1680 1681 err = mOMX->allocateBuffer( 1682 mNode, portIndex, def.nBufferSize, &buffer, 1683 &info.mData); 1684 } else { 1685 err = mOMX->allocateBufferWithBackup( 1686 mNode, portIndex, mem, &buffer); 1687 } 1688 } else { 1689 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer); 1690 } 1691 1692 if (err != OK) { 1693 ALOGE("allocate_buffer_with_backup failed"); 1694 return err; 1695 } 1696 1697 if (mem != NULL) { 1698 info.mData = mem->pointer(); 1699 } 1700 1701 info.mBuffer = buffer; 1702 info.mStatus = OWNED_BY_US; 1703 info.mMem = mem; 1704 info.mMediaBuffer = NULL; 1705 1706 if (portIndex == kPortIndexOutput) { 1707 // Fail deferred MediaBuffer creation until FILL_BUFFER_DONE; 1708 // this legacy mode is no longer supported. 1709 LOG_ALWAYS_FATAL_IF((mOMXLivesLocally 1710 && (mQuirks & kRequiresAllocateBufferOnOutputPorts) 1711 && (mQuirks & kDefersOutputBufferAllocation)), 1712 "allocateBuffersOnPort cannot defer buffer allocation"); 1713 1714 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize); 1715 info.mMediaBuffer->setObserver(this); 1716 } 1717 1718 mPortBuffers[portIndex].push(info); 1719 1720 CODEC_LOGV("allocated buffer %p on %s port", buffer, 1721 portIndex == kPortIndexInput ? "input" : "output"); 1722 } 1723 1724 if (portIndex == kPortIndexOutput) { 1725 1726 sp<MetaData> meta = mSource->getFormat(); 1727 int32_t delay = 0; 1728 if (!meta->findInt32(kKeyEncoderDelay, &delay)) { 1729 delay = 0; 1730 } 1731 int32_t padding = 0; 1732 if (!meta->findInt32(kKeyEncoderPadding, &padding)) { 1733 padding = 0; 1734 } 1735 int32_t numchannels = 0; 1736 if (delay + padding) { 1737 if (mOutputFormat->findInt32(kKeyChannelCount, &numchannels)) { 1738 size_t frameSize = numchannels * sizeof(int16_t); 1739 if (mSkipCutBuffer != NULL) { 1740 size_t prevbuffersize = mSkipCutBuffer->size(); 1741 if (prevbuffersize != 0) { 1742 ALOGW("Replacing SkipCutBuffer holding %d bytes", prevbuffersize); 1743 } 1744 } 1745 mSkipCutBuffer = new SkipCutBuffer(delay * frameSize, padding * frameSize); 1746 } 1747 } 1748 } 1749 1750 // dumpPortStatus(portIndex); 1751 1752 if (portIndex == kPortIndexInput && (mFlags & kUseSecureInputBuffers)) { 1753 Vector<MediaBuffer *> buffers; 1754 for (size_t i = 0; i < def.nBufferCountActual; ++i) { 1755 const BufferInfo &info = mPortBuffers[kPortIndexInput].itemAt(i); 1756 1757 MediaBuffer *mbuf = new MediaBuffer(info.mData, info.mSize); 1758 buffers.push(mbuf); 1759 } 1760 1761 status_t err = mSource->setBuffers(buffers); 1762 1763 if (err != OK) { 1764 for (size_t i = 0; i < def.nBufferCountActual; ++i) { 1765 buffers.editItemAt(i)->release(); 1766 } 1767 buffers.clear(); 1768 1769 CODEC_LOGE( 1770 "Codec requested to use secure input buffers but " 1771 "upstream source didn't support that."); 1772 1773 return err; 1774 } 1775 } 1776 1777 return OK; 1778 } 1779 1780 status_t OMXCodec::applyRotation() { 1781 sp<MetaData> meta = mSource->getFormat(); 1782 1783 int32_t rotationDegrees; 1784 if (!meta->findInt32(kKeyRotation, &rotationDegrees)) { 1785 rotationDegrees = 0; 1786 } 1787 1788 uint32_t transform; 1789 switch (rotationDegrees) { 1790 case 0: transform = 0; break; 1791 case 90: transform = HAL_TRANSFORM_ROT_90; break; 1792 case 180: transform = HAL_TRANSFORM_ROT_180; break; 1793 case 270: transform = HAL_TRANSFORM_ROT_270; break; 1794 default: transform = 0; break; 1795 } 1796 1797 status_t err = OK; 1798 1799 if (transform) { 1800 err = native_window_set_buffers_transform( 1801 mNativeWindow.get(), transform); 1802 ALOGE("native_window_set_buffers_transform failed: %s (%d)", 1803 strerror(-err), -err); 1804 } 1805 1806 return err; 1807 } 1808 1809 status_t OMXCodec::allocateOutputBuffersFromNativeWindow() { 1810 // Get the number of buffers needed. 1811 OMX_PARAM_PORTDEFINITIONTYPE def; 1812 InitOMXParams(&def); 1813 def.nPortIndex = kPortIndexOutput; 1814 1815 status_t err = mOMX->getParameter( 1816 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1817 if (err != OK) { 1818 CODEC_LOGE("getParameter failed: %d", err); 1819 return err; 1820 } 1821 1822 err = native_window_set_buffers_geometry( 1823 mNativeWindow.get(), 1824 def.format.video.nFrameWidth, 1825 def.format.video.nFrameHeight, 1826 def.format.video.eColorFormat); 1827 1828 if (err != 0) { 1829 ALOGE("native_window_set_buffers_geometry failed: %s (%d)", 1830 strerror(-err), -err); 1831 return err; 1832 } 1833 1834 err = applyRotation(); 1835 if (err != OK) { 1836 return err; 1837 } 1838 1839 // Set up the native window. 1840 OMX_U32 usage = 0; 1841 err = mOMX->getGraphicBufferUsage(mNode, kPortIndexOutput, &usage); 1842 if (err != 0) { 1843 ALOGW("querying usage flags from OMX IL component failed: %d", err); 1844 // XXX: Currently this error is logged, but not fatal. 1845 usage = 0; 1846 } 1847 if (mFlags & kEnableGrallocUsageProtected) { 1848 usage |= GRALLOC_USAGE_PROTECTED; 1849 } 1850 1851 // Make sure to check whether either Stagefright or the video decoder 1852 // requested protected buffers. 1853 if (usage & GRALLOC_USAGE_PROTECTED) { 1854 // Verify that the ANativeWindow sends images directly to 1855 // SurfaceFlinger. 1856 int queuesToNativeWindow = 0; 1857 err = mNativeWindow->query( 1858 mNativeWindow.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER, 1859 &queuesToNativeWindow); 1860 if (err != 0) { 1861 ALOGE("error authenticating native window: %d", err); 1862 return err; 1863 } 1864 if (queuesToNativeWindow != 1) { 1865 ALOGE("native window could not be authenticated"); 1866 return PERMISSION_DENIED; 1867 } 1868 } 1869 1870 ALOGV("native_window_set_usage usage=0x%lx", usage); 1871 err = native_window_set_usage( 1872 mNativeWindow.get(), usage | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP); 1873 if (err != 0) { 1874 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err); 1875 return err; 1876 } 1877 1878 int minUndequeuedBufs = 0; 1879 err = mNativeWindow->query(mNativeWindow.get(), 1880 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufs); 1881 if (err != 0) { 1882 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d)", 1883 strerror(-err), -err); 1884 return err; 1885 } 1886 // FIXME: assume that surface is controlled by app (native window 1887 // returns the number for the case when surface is not controlled by app) 1888 // FIXME2: This means that minUndeqeueudBufs can be 1 larger than reported 1889 // For now, try to allocate 1 more buffer, but don't fail if unsuccessful 1890 1891 // Use conservative allocation while also trying to reduce starvation 1892 // 1893 // 1. allocate at least nBufferCountMin + minUndequeuedBuffers - that is the 1894 // minimum needed for the consumer to be able to work 1895 // 2. try to allocate two (2) additional buffers to reduce starvation from 1896 // the consumer 1897 // plus an extra buffer to account for incorrect minUndequeuedBufs 1898 CODEC_LOGI("OMX-buffers: min=%u actual=%u undeq=%d+1", 1899 def.nBufferCountMin, def.nBufferCountActual, minUndequeuedBufs); 1900 1901 for (OMX_U32 extraBuffers = 2 + 1; /* condition inside loop */; extraBuffers--) { 1902 OMX_U32 newBufferCount = 1903 def.nBufferCountMin + minUndequeuedBufs + extraBuffers; 1904 def.nBufferCountActual = newBufferCount; 1905 err = mOMX->setParameter( 1906 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1907 1908 if (err == OK) { 1909 minUndequeuedBufs += extraBuffers; 1910 break; 1911 } 1912 1913 CODEC_LOGW("setting nBufferCountActual to %u failed: %d", 1914 newBufferCount, err); 1915 /* exit condition */ 1916 if (extraBuffers == 0) { 1917 return err; 1918 } 1919 } 1920 CODEC_LOGI("OMX-buffers: min=%u actual=%u undeq=%d+1", 1921 def.nBufferCountMin, def.nBufferCountActual, minUndequeuedBufs); 1922 1923 err = native_window_set_buffer_count( 1924 mNativeWindow.get(), def.nBufferCountActual); 1925 if (err != 0) { 1926 ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err), 1927 -err); 1928 return err; 1929 } 1930 1931 CODEC_LOGV("allocating %u buffers from a native window of size %u on " 1932 "output port", def.nBufferCountActual, def.nBufferSize); 1933 1934 // Dequeue buffers and send them to OMX 1935 for (OMX_U32 i = 0; i < def.nBufferCountActual; i++) { 1936 ANativeWindowBuffer* buf; 1937 err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(), &buf); 1938 if (err != 0) { 1939 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err); 1940 break; 1941 } 1942 1943 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false)); 1944 BufferInfo info; 1945 info.mData = NULL; 1946 info.mSize = def.nBufferSize; 1947 info.mStatus = OWNED_BY_US; 1948 info.mMem = NULL; 1949 info.mMediaBuffer = new MediaBuffer(graphicBuffer); 1950 info.mMediaBuffer->setObserver(this); 1951 mPortBuffers[kPortIndexOutput].push(info); 1952 1953 IOMX::buffer_id bufferId; 1954 err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer, 1955 &bufferId); 1956 if (err != 0) { 1957 CODEC_LOGE("registering GraphicBuffer with OMX IL component " 1958 "failed: %d", err); 1959 break; 1960 } 1961 1962 mPortBuffers[kPortIndexOutput].editItemAt(i).mBuffer = bufferId; 1963 1964 CODEC_LOGV("registered graphic buffer with ID %u (pointer = %p)", 1965 bufferId, graphicBuffer.get()); 1966 } 1967 1968 OMX_U32 cancelStart; 1969 OMX_U32 cancelEnd; 1970 if (err != 0) { 1971 // If an error occurred while dequeuing we need to cancel any buffers 1972 // that were dequeued. 1973 cancelStart = 0; 1974 cancelEnd = mPortBuffers[kPortIndexOutput].size(); 1975 } else { 1976 // Return the last two buffers to the native window. 1977 cancelStart = def.nBufferCountActual - minUndequeuedBufs; 1978 cancelEnd = def.nBufferCountActual; 1979 } 1980 1981 for (OMX_U32 i = cancelStart; i < cancelEnd; i++) { 1982 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(i); 1983 cancelBufferToNativeWindow(info); 1984 } 1985 1986 return err; 1987 } 1988 1989 status_t OMXCodec::cancelBufferToNativeWindow(BufferInfo *info) { 1990 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US); 1991 CODEC_LOGV("Calling cancelBuffer on buffer %u", info->mBuffer); 1992 int err = mNativeWindow->cancelBuffer( 1993 mNativeWindow.get(), info->mMediaBuffer->graphicBuffer().get(), -1); 1994 if (err != 0) { 1995 CODEC_LOGE("cancelBuffer failed w/ error 0x%08x", err); 1996 1997 setState(ERROR); 1998 return err; 1999 } 2000 info->mStatus = OWNED_BY_NATIVE_WINDOW; 2001 return OK; 2002 } 2003 2004 OMXCodec::BufferInfo* OMXCodec::dequeueBufferFromNativeWindow() { 2005 // Dequeue the next buffer from the native window. 2006 ANativeWindowBuffer* buf; 2007 int fenceFd = -1; 2008 int err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(), &buf); 2009 if (err != 0) { 2010 CODEC_LOGE("dequeueBuffer failed w/ error 0x%08x", err); 2011 2012 setState(ERROR); 2013 return 0; 2014 } 2015 2016 // Determine which buffer we just dequeued. 2017 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 2018 BufferInfo *bufInfo = 0; 2019 for (size_t i = 0; i < buffers->size(); i++) { 2020 sp<GraphicBuffer> graphicBuffer = buffers->itemAt(i). 2021 mMediaBuffer->graphicBuffer(); 2022 if (graphicBuffer->handle == buf->handle) { 2023 bufInfo = &buffers->editItemAt(i); 2024 break; 2025 } 2026 } 2027 2028 if (bufInfo == 0) { 2029 CODEC_LOGE("dequeued unrecognized buffer: %p", buf); 2030 2031 setState(ERROR); 2032 return 0; 2033 } 2034 2035 // The native window no longer owns the buffer. 2036 CHECK_EQ((int)bufInfo->mStatus, (int)OWNED_BY_NATIVE_WINDOW); 2037 bufInfo->mStatus = OWNED_BY_US; 2038 2039 return bufInfo; 2040 } 2041 2042 status_t OMXCodec::pushBlankBuffersToNativeWindow() { 2043 status_t err = NO_ERROR; 2044 ANativeWindowBuffer* anb = NULL; 2045 int numBufs = 0; 2046 int minUndequeuedBufs = 0; 2047 2048 // We need to reconnect to the ANativeWindow as a CPU client to ensure that 2049 // no frames get dropped by SurfaceFlinger assuming that these are video 2050 // frames. 2051 err = native_window_api_disconnect(mNativeWindow.get(), 2052 NATIVE_WINDOW_API_MEDIA); 2053 if (err != NO_ERROR) { 2054 ALOGE("error pushing blank frames: api_disconnect failed: %s (%d)", 2055 strerror(-err), -err); 2056 return err; 2057 } 2058 2059 err = native_window_api_connect(mNativeWindow.get(), 2060 NATIVE_WINDOW_API_CPU); 2061 if (err != NO_ERROR) { 2062 ALOGE("error pushing blank frames: api_connect failed: %s (%d)", 2063 strerror(-err), -err); 2064 return err; 2065 } 2066 2067 err = native_window_set_buffers_geometry(mNativeWindow.get(), 1, 1, 2068 HAL_PIXEL_FORMAT_RGBX_8888); 2069 if (err != NO_ERROR) { 2070 ALOGE("error pushing blank frames: set_buffers_geometry failed: %s (%d)", 2071 strerror(-err), -err); 2072 goto error; 2073 } 2074 2075 err = native_window_set_usage(mNativeWindow.get(), 2076 GRALLOC_USAGE_SW_WRITE_OFTEN); 2077 if (err != NO_ERROR) { 2078 ALOGE("error pushing blank frames: set_usage failed: %s (%d)", 2079 strerror(-err), -err); 2080 goto error; 2081 } 2082 2083 err = native_window_set_scaling_mode(mNativeWindow.get(), 2084 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); 2085 if (err != OK) { 2086 ALOGE("error pushing blank frames: set_scaling_mode failed: %s (%d)", 2087 strerror(-err), -err); 2088 goto error; 2089 } 2090 2091 err = mNativeWindow->query(mNativeWindow.get(), 2092 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufs); 2093 if (err != NO_ERROR) { 2094 ALOGE("error pushing blank frames: MIN_UNDEQUEUED_BUFFERS query " 2095 "failed: %s (%d)", strerror(-err), -err); 2096 goto error; 2097 } 2098 2099 numBufs = minUndequeuedBufs + 1; 2100 err = native_window_set_buffer_count(mNativeWindow.get(), numBufs); 2101 if (err != NO_ERROR) { 2102 ALOGE("error pushing blank frames: set_buffer_count failed: %s (%d)", 2103 strerror(-err), -err); 2104 goto error; 2105 } 2106 2107 // We push numBufs + 1 buffers to ensure that we've drawn into the same 2108 // buffer twice. This should guarantee that the buffer has been displayed 2109 // on the screen and then been replaced, so an previous video frames are 2110 // guaranteed NOT to be currently displayed. 2111 for (int i = 0; i < numBufs + 1; i++) { 2112 int fenceFd = -1; 2113 err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(), &anb); 2114 if (err != NO_ERROR) { 2115 ALOGE("error pushing blank frames: dequeueBuffer failed: %s (%d)", 2116 strerror(-err), -err); 2117 goto error; 2118 } 2119 2120 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 2121 2122 // Fill the buffer with the a 1x1 checkerboard pattern ;) 2123 uint32_t* img = NULL; 2124 err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 2125 if (err != NO_ERROR) { 2126 ALOGE("error pushing blank frames: lock failed: %s (%d)", 2127 strerror(-err), -err); 2128 goto error; 2129 } 2130 2131 *img = 0; 2132 2133 err = buf->unlock(); 2134 if (err != NO_ERROR) { 2135 ALOGE("error pushing blank frames: unlock failed: %s (%d)", 2136 strerror(-err), -err); 2137 goto error; 2138 } 2139 2140 err = mNativeWindow->queueBuffer(mNativeWindow.get(), 2141 buf->getNativeBuffer(), -1); 2142 if (err != NO_ERROR) { 2143 ALOGE("error pushing blank frames: queueBuffer failed: %s (%d)", 2144 strerror(-err), -err); 2145 goto error; 2146 } 2147 2148 anb = NULL; 2149 } 2150 2151 error: 2152 2153 if (err != NO_ERROR) { 2154 // Clean up after an error. 2155 if (anb != NULL) { 2156 mNativeWindow->cancelBuffer(mNativeWindow.get(), anb, -1); 2157 } 2158 2159 native_window_api_disconnect(mNativeWindow.get(), 2160 NATIVE_WINDOW_API_CPU); 2161 native_window_api_connect(mNativeWindow.get(), 2162 NATIVE_WINDOW_API_MEDIA); 2163 2164 return err; 2165 } else { 2166 // Clean up after success. 2167 err = native_window_api_disconnect(mNativeWindow.get(), 2168 NATIVE_WINDOW_API_CPU); 2169 if (err != NO_ERROR) { 2170 ALOGE("error pushing blank frames: api_disconnect failed: %s (%d)", 2171 strerror(-err), -err); 2172 return err; 2173 } 2174 2175 err = native_window_api_connect(mNativeWindow.get(), 2176 NATIVE_WINDOW_API_MEDIA); 2177 if (err != NO_ERROR) { 2178 ALOGE("error pushing blank frames: api_connect failed: %s (%d)", 2179 strerror(-err), -err); 2180 return err; 2181 } 2182 2183 return NO_ERROR; 2184 } 2185 } 2186 2187 int64_t OMXCodec::getDecodingTimeUs() { 2188 CHECK(mIsEncoder && mIsVideo); 2189 2190 if (mDecodingTimeList.empty()) { 2191 CHECK(mSignalledEOS || mNoMoreOutputData); 2192 // No corresponding input frame available. 2193 // This could happen when EOS is reached. 2194 return 0; 2195 } 2196 2197 List<int64_t>::iterator it = mDecodingTimeList.begin(); 2198 int64_t timeUs = *it; 2199 mDecodingTimeList.erase(it); 2200 return timeUs; 2201 } 2202 2203 void OMXCodec::on_message(const omx_message &msg) { 2204 if (mState == ERROR) { 2205 /* 2206 * only drop EVENT messages, EBD and FBD are still 2207 * processed for bookkeeping purposes 2208 */ 2209 if (msg.type == omx_message::EVENT) { 2210 ALOGW("Dropping OMX EVENT message - we're in ERROR state."); 2211 return; 2212 } 2213 } 2214 2215 switch (msg.type) { 2216 case omx_message::EVENT: 2217 { 2218 onEvent( 2219 msg.u.event_data.event, msg.u.event_data.data1, 2220 msg.u.event_data.data2); 2221 2222 break; 2223 } 2224 2225 case omx_message::EMPTY_BUFFER_DONE: 2226 { 2227 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer; 2228 2229 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %u)", buffer); 2230 2231 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 2232 size_t i = 0; 2233 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) { 2234 ++i; 2235 } 2236 2237 CHECK(i < buffers->size()); 2238 if ((*buffers)[i].mStatus != OWNED_BY_COMPONENT) { 2239 ALOGW("We already own input buffer %u, yet received " 2240 "an EMPTY_BUFFER_DONE.", buffer); 2241 } 2242 2243 BufferInfo* info = &buffers->editItemAt(i); 2244 info->mStatus = OWNED_BY_US; 2245 2246 // Buffer could not be released until empty buffer done is called. 2247 if (info->mMediaBuffer != NULL) { 2248 info->mMediaBuffer->release(); 2249 info->mMediaBuffer = NULL; 2250 } 2251 2252 if (mPortStatus[kPortIndexInput] == DISABLING) { 2253 CODEC_LOGV("Port is disabled, freeing buffer %u", buffer); 2254 2255 status_t err = freeBuffer(kPortIndexInput, i); 2256 CHECK_EQ(err, (status_t)OK); 2257 } else if (mState != ERROR 2258 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) { 2259 CHECK_EQ((int)mPortStatus[kPortIndexInput], (int)ENABLED); 2260 2261 if (mFlags & kUseSecureInputBuffers) { 2262 drainAnyInputBuffer(); 2263 } else { 2264 drainInputBuffer(&buffers->editItemAt(i)); 2265 } 2266 } 2267 break; 2268 } 2269 2270 case omx_message::FILL_BUFFER_DONE: 2271 { 2272 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer; 2273 OMX_U32 flags = msg.u.extended_buffer_data.flags; 2274 2275 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %u, size: %u, flags: 0x%08x, timestamp: %lld us (%.2f secs))", 2276 buffer, 2277 msg.u.extended_buffer_data.range_length, 2278 flags, 2279 msg.u.extended_buffer_data.timestamp, 2280 msg.u.extended_buffer_data.timestamp / 1E6); 2281 2282 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 2283 size_t i = 0; 2284 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) { 2285 ++i; 2286 } 2287 2288 CHECK(i < buffers->size()); 2289 BufferInfo *info = &buffers->editItemAt(i); 2290 2291 if (info->mStatus != OWNED_BY_COMPONENT) { 2292 ALOGW("We already own output buffer %u, yet received " 2293 "a FILL_BUFFER_DONE.", buffer); 2294 } 2295 2296 info->mStatus = OWNED_BY_US; 2297 2298 if (mPortStatus[kPortIndexOutput] == DISABLING) { 2299 CODEC_LOGV("Port is disabled, freeing buffer %u", buffer); 2300 2301 status_t err = freeBuffer(kPortIndexOutput, i); 2302 CHECK_EQ(err, (status_t)OK); 2303 2304 #if 0 2305 } else if (mPortStatus[kPortIndexOutput] == ENABLED 2306 && (flags & OMX_BUFFERFLAG_EOS)) { 2307 CODEC_LOGV("No more output data."); 2308 mNoMoreOutputData = true; 2309 mBufferFilled.signal(); 2310 #endif 2311 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) { 2312 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED); 2313 2314 MediaBuffer *buffer = info->mMediaBuffer; 2315 bool isGraphicBuffer = buffer->graphicBuffer() != NULL; 2316 2317 if (!isGraphicBuffer 2318 && msg.u.extended_buffer_data.range_offset 2319 + msg.u.extended_buffer_data.range_length 2320 > buffer->size()) { 2321 CODEC_LOGE( 2322 "Codec lied about its buffer size requirements, " 2323 "sending a buffer larger than the originally " 2324 "advertised size in FILL_BUFFER_DONE!"); 2325 } 2326 buffer->set_range( 2327 msg.u.extended_buffer_data.range_offset, 2328 msg.u.extended_buffer_data.range_length); 2329 2330 buffer->meta_data()->clear(); 2331 2332 buffer->meta_data()->setInt64( 2333 kKeyTime, msg.u.extended_buffer_data.timestamp); 2334 2335 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) { 2336 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true); 2337 } 2338 bool isCodecSpecific = false; 2339 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) { 2340 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true); 2341 isCodecSpecific = true; 2342 } 2343 2344 if (isGraphicBuffer || mQuirks & kOutputBuffersAreUnreadable) { 2345 buffer->meta_data()->setInt32(kKeyIsUnreadable, true); 2346 } 2347 2348 buffer->meta_data()->setInt32( 2349 kKeyBufferID, 2350 msg.u.extended_buffer_data.buffer); 2351 2352 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) { 2353 CODEC_LOGV("No more output data."); 2354 mNoMoreOutputData = true; 2355 } 2356 2357 if (mIsEncoder && mIsVideo) { 2358 int64_t decodingTimeUs = isCodecSpecific? 0: getDecodingTimeUs(); 2359 buffer->meta_data()->setInt64(kKeyDecodingTime, decodingTimeUs); 2360 } 2361 2362 if (mTargetTimeUs >= 0) { 2363 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs); 2364 2365 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) { 2366 CODEC_LOGV( 2367 "skipping output buffer at timestamp %lld us", 2368 msg.u.extended_buffer_data.timestamp); 2369 2370 fillOutputBuffer(info); 2371 break; 2372 } 2373 2374 CODEC_LOGV( 2375 "returning output buffer at target timestamp " 2376 "%lld us", 2377 msg.u.extended_buffer_data.timestamp); 2378 2379 mTargetTimeUs = -1; 2380 } 2381 2382 mFilledBuffers.push_back(i); 2383 mBufferFilled.signal(); 2384 if (mIsEncoder) { 2385 sched_yield(); 2386 } 2387 } 2388 2389 break; 2390 } 2391 2392 default: 2393 { 2394 CHECK(!"should not be here."); 2395 break; 2396 } 2397 } 2398 } 2399 2400 // Has the format changed in any way that the client would have to be aware of? 2401 static bool formatHasNotablyChanged( 2402 const sp<MetaData> &from, const sp<MetaData> &to) { 2403 if (from.get() == NULL && to.get() == NULL) { 2404 return false; 2405 } 2406 2407 if ((from.get() == NULL && to.get() != NULL) 2408 || (from.get() != NULL && to.get() == NULL)) { 2409 return true; 2410 } 2411 2412 const char *mime_from, *mime_to; 2413 CHECK(from->findCString(kKeyMIMEType, &mime_from)); 2414 CHECK(to->findCString(kKeyMIMEType, &mime_to)); 2415 2416 if (strcasecmp(mime_from, mime_to)) { 2417 return true; 2418 } 2419 2420 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) { 2421 int32_t colorFormat_from, colorFormat_to; 2422 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from)); 2423 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to)); 2424 2425 if (colorFormat_from != colorFormat_to) { 2426 return true; 2427 } 2428 2429 int32_t width_from, width_to; 2430 CHECK(from->findInt32(kKeyWidth, &width_from)); 2431 CHECK(to->findInt32(kKeyWidth, &width_to)); 2432 2433 if (width_from != width_to) { 2434 return true; 2435 } 2436 2437 int32_t height_from, height_to; 2438 CHECK(from->findInt32(kKeyHeight, &height_from)); 2439 CHECK(to->findInt32(kKeyHeight, &height_to)); 2440 2441 if (height_from != height_to) { 2442 return true; 2443 } 2444 2445 int32_t left_from, top_from, right_from, bottom_from; 2446 CHECK(from->findRect( 2447 kKeyCropRect, 2448 &left_from, &top_from, &right_from, &bottom_from)); 2449 2450 int32_t left_to, top_to, right_to, bottom_to; 2451 CHECK(to->findRect( 2452 kKeyCropRect, 2453 &left_to, &top_to, &right_to, &bottom_to)); 2454 2455 if (left_to != left_from || top_to != top_from 2456 || right_to != right_from || bottom_to != bottom_from) { 2457 return true; 2458 } 2459 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) { 2460 int32_t numChannels_from, numChannels_to; 2461 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from)); 2462 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to)); 2463 2464 if (numChannels_from != numChannels_to) { 2465 return true; 2466 } 2467 2468 int32_t sampleRate_from, sampleRate_to; 2469 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from)); 2470 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to)); 2471 2472 if (sampleRate_from != sampleRate_to) { 2473 return true; 2474 } 2475 } 2476 2477 return false; 2478 } 2479 2480 void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) { 2481 switch (event) { 2482 case OMX_EventCmdComplete: 2483 { 2484 onCmdComplete((OMX_COMMANDTYPE)data1, data2); 2485 break; 2486 } 2487 2488 case OMX_EventError: 2489 { 2490 CODEC_LOGE("OMX_EventError(0x%08x, %u)", data1, data2); 2491 2492 setState(ERROR); 2493 break; 2494 } 2495 2496 case OMX_EventPortSettingsChanged: 2497 { 2498 CODEC_LOGV("OMX_EventPortSettingsChanged(port=%u, data2=0x%08x)", 2499 data1, data2); 2500 2501 if (data2 == 0 || data2 == OMX_IndexParamPortDefinition) { 2502 onPortSettingsChanged(data1); 2503 } else if (data1 == kPortIndexOutput && 2504 (data2 == OMX_IndexConfigCommonOutputCrop || 2505 data2 == OMX_IndexConfigCommonScale)) { 2506 2507 sp<MetaData> oldOutputFormat = mOutputFormat; 2508 initOutputFormat(mSource->getFormat()); 2509 2510 if (data2 == OMX_IndexConfigCommonOutputCrop && 2511 formatHasNotablyChanged(oldOutputFormat, mOutputFormat)) { 2512 mOutputPortSettingsHaveChanged = true; 2513 2514 } else if (data2 == OMX_IndexConfigCommonScale) { 2515 OMX_CONFIG_SCALEFACTORTYPE scale; 2516 InitOMXParams(&scale); 2517 scale.nPortIndex = kPortIndexOutput; 2518 2519 // Change display dimension only when necessary. 2520 if (OK == mOMX->getConfig( 2521 mNode, 2522 OMX_IndexConfigCommonScale, 2523 &scale, sizeof(scale))) { 2524 int32_t left, top, right, bottom; 2525 CHECK(mOutputFormat->findRect(kKeyCropRect, 2526 &left, &top, 2527 &right, &bottom)); 2528 2529 // The scale is in 16.16 format. 2530 // scale 1.0 = 0x010000. When there is no 2531 // need to change the display, skip it. 2532 ALOGV("Get OMX_IndexConfigScale: 0x%x/0x%x", 2533 scale.xWidth, scale.xHeight); 2534 2535 if (scale.xWidth != 0x010000) { 2536 mOutputFormat->setInt32(kKeyDisplayWidth, 2537 ((right - left + 1) * scale.xWidth) >> 16); 2538 mOutputPortSettingsHaveChanged = true; 2539 } 2540 2541 if (scale.xHeight != 0x010000) { 2542 mOutputFormat->setInt32(kKeyDisplayHeight, 2543 ((bottom - top + 1) * scale.xHeight) >> 16); 2544 mOutputPortSettingsHaveChanged = true; 2545 } 2546 } 2547 } 2548 } 2549 break; 2550 } 2551 2552 #if 0 2553 case OMX_EventBufferFlag: 2554 { 2555 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1); 2556 2557 if (data1 == kPortIndexOutput) { 2558 mNoMoreOutputData = true; 2559 } 2560 break; 2561 } 2562 #endif 2563 2564 default: 2565 { 2566 CODEC_LOGV("EVENT(%d, %u, %u)", event, data1, data2); 2567 break; 2568 } 2569 } 2570 } 2571 2572 void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) { 2573 switch (cmd) { 2574 case OMX_CommandStateSet: 2575 { 2576 onStateChange((OMX_STATETYPE)data); 2577 break; 2578 } 2579 2580 case OMX_CommandPortDisable: 2581 { 2582 OMX_U32 portIndex = data; 2583 CODEC_LOGV("PORT_DISABLED(%u)", portIndex); 2584 2585 CHECK(mState == EXECUTING || mState == RECONFIGURING); 2586 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLING); 2587 CHECK_EQ(mPortBuffers[portIndex].size(), 0u); 2588 2589 mPortStatus[portIndex] = DISABLED; 2590 2591 if (mState == RECONFIGURING) { 2592 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); 2593 2594 sp<MetaData> oldOutputFormat = mOutputFormat; 2595 initOutputFormat(mSource->getFormat()); 2596 2597 // Don't notify clients if the output port settings change 2598 // wasn't of importance to them, i.e. it may be that just the 2599 // number of buffers has changed and nothing else. 2600 bool formatChanged = formatHasNotablyChanged(oldOutputFormat, mOutputFormat); 2601 if (!mOutputPortSettingsHaveChanged) { 2602 mOutputPortSettingsHaveChanged = formatChanged; 2603 } 2604 2605 status_t err = enablePortAsync(portIndex); 2606 if (err != OK) { 2607 CODEC_LOGE("enablePortAsync(%u) failed (err = %d)", portIndex, err); 2608 setState(ERROR); 2609 } else { 2610 err = allocateBuffersOnPort(portIndex); 2611 if (err != OK) { 2612 CODEC_LOGE("allocateBuffersOnPort (%s) failed " 2613 "(err = %d)", 2614 portIndex == kPortIndexInput 2615 ? "input" : "output", 2616 err); 2617 2618 setState(ERROR); 2619 } 2620 } 2621 } 2622 break; 2623 } 2624 2625 case OMX_CommandPortEnable: 2626 { 2627 OMX_U32 portIndex = data; 2628 CODEC_LOGV("PORT_ENABLED(%u)", portIndex); 2629 2630 CHECK(mState == EXECUTING || mState == RECONFIGURING); 2631 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLING); 2632 2633 mPortStatus[portIndex] = ENABLED; 2634 2635 if (mState == RECONFIGURING) { 2636 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); 2637 2638 setState(EXECUTING); 2639 2640 fillOutputBuffers(); 2641 } 2642 break; 2643 } 2644 2645 case OMX_CommandFlush: 2646 { 2647 OMX_U32 portIndex = data; 2648 2649 CODEC_LOGV("FLUSH_DONE(%u)", portIndex); 2650 2651 CHECK_EQ((int)mPortStatus[portIndex], (int)SHUTTING_DOWN); 2652 mPortStatus[portIndex] = ENABLED; 2653 2654 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]), 2655 mPortBuffers[portIndex].size()); 2656 2657 if (mSkipCutBuffer != NULL && mPortStatus[kPortIndexOutput] == ENABLED) { 2658 mSkipCutBuffer->clear(); 2659 } 2660 2661 if (mState == RECONFIGURING) { 2662 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); 2663 2664 disablePortAsync(portIndex); 2665 } else if (mState == EXECUTING_TO_IDLE) { 2666 if (mPortStatus[kPortIndexInput] == ENABLED 2667 && mPortStatus[kPortIndexOutput] == ENABLED) { 2668 CODEC_LOGV("Finished flushing both ports, now completing " 2669 "transition from EXECUTING to IDLE."); 2670 2671 mPortStatus[kPortIndexInput] = SHUTTING_DOWN; 2672 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; 2673 2674 status_t err = 2675 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 2676 CHECK_EQ(err, (status_t)OK); 2677 } 2678 } else { 2679 // We're flushing both ports in preparation for seeking. 2680 2681 if (mPortStatus[kPortIndexInput] == ENABLED 2682 && mPortStatus[kPortIndexOutput] == ENABLED) { 2683 CODEC_LOGV("Finished flushing both ports, now continuing from" 2684 " seek-time."); 2685 2686 // We implicitly resume pulling on our upstream source. 2687 mPaused = false; 2688 2689 drainInputBuffers(); 2690 fillOutputBuffers(); 2691 } 2692 2693 if (mOutputPortSettingsChangedPending) { 2694 CODEC_LOGV( 2695 "Honoring deferred output port settings change."); 2696 2697 mOutputPortSettingsChangedPending = false; 2698 onPortSettingsChanged(kPortIndexOutput); 2699 } 2700 } 2701 2702 break; 2703 } 2704 2705 default: 2706 { 2707 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data); 2708 break; 2709 } 2710 } 2711 } 2712 2713 void OMXCodec::onStateChange(OMX_STATETYPE newState) { 2714 CODEC_LOGV("onStateChange %d", newState); 2715 2716 switch (newState) { 2717 case OMX_StateIdle: 2718 { 2719 CODEC_LOGV("Now Idle."); 2720 if (mState == LOADED_TO_IDLE) { 2721 status_t err = mOMX->sendCommand( 2722 mNode, OMX_CommandStateSet, OMX_StateExecuting); 2723 2724 CHECK_EQ(err, (status_t)OK); 2725 2726 setState(IDLE_TO_EXECUTING); 2727 } else { 2728 CHECK_EQ((int)mState, (int)EXECUTING_TO_IDLE); 2729 2730 if (countBuffersWeOwn(mPortBuffers[kPortIndexInput]) != 2731 mPortBuffers[kPortIndexInput].size()) { 2732 ALOGE("Codec did not return all input buffers " 2733 "(received %d / %d)", 2734 countBuffersWeOwn(mPortBuffers[kPortIndexInput]), 2735 mPortBuffers[kPortIndexInput].size()); 2736 TRESPASS(); 2737 } 2738 2739 if (countBuffersWeOwn(mPortBuffers[kPortIndexOutput]) != 2740 mPortBuffers[kPortIndexOutput].size()) { 2741 ALOGE("Codec did not return all output buffers " 2742 "(received %d / %d)", 2743 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]), 2744 mPortBuffers[kPortIndexOutput].size()); 2745 TRESPASS(); 2746 } 2747 2748 status_t err = mOMX->sendCommand( 2749 mNode, OMX_CommandStateSet, OMX_StateLoaded); 2750 2751 CHECK_EQ(err, (status_t)OK); 2752 2753 err = freeBuffersOnPort(kPortIndexInput); 2754 CHECK_EQ(err, (status_t)OK); 2755 2756 err = freeBuffersOnPort(kPortIndexOutput); 2757 CHECK_EQ(err, (status_t)OK); 2758 2759 mPortStatus[kPortIndexInput] = ENABLED; 2760 mPortStatus[kPortIndexOutput] = ENABLED; 2761 2762 if ((mFlags & kEnableGrallocUsageProtected) && 2763 mNativeWindow != NULL) { 2764 // We push enough 1x1 blank buffers to ensure that one of 2765 // them has made it to the display. This allows the OMX 2766 // component teardown to zero out any protected buffers 2767 // without the risk of scanning out one of those buffers. 2768 pushBlankBuffersToNativeWindow(); 2769 } 2770 2771 setState(IDLE_TO_LOADED); 2772 } 2773 break; 2774 } 2775 2776 case OMX_StateExecuting: 2777 { 2778 CHECK_EQ((int)mState, (int)IDLE_TO_EXECUTING); 2779 2780 CODEC_LOGV("Now Executing."); 2781 2782 mOutputPortSettingsChangedPending = false; 2783 2784 setState(EXECUTING); 2785 2786 // Buffers will be submitted to the component in the first 2787 // call to OMXCodec::read as mInitialBufferSubmit is true at 2788 // this point. This ensures that this on_message call returns, 2789 // releases the lock and ::init can notice the state change and 2790 // itself return. 2791 break; 2792 } 2793 2794 case OMX_StateLoaded: 2795 { 2796 CHECK_EQ((int)mState, (int)IDLE_TO_LOADED); 2797 2798 CODEC_LOGV("Now Loaded."); 2799 2800 setState(LOADED); 2801 break; 2802 } 2803 2804 case OMX_StateInvalid: 2805 { 2806 setState(ERROR); 2807 break; 2808 } 2809 2810 default: 2811 { 2812 CHECK(!"should not be here."); 2813 break; 2814 } 2815 } 2816 } 2817 2818 // static 2819 size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) { 2820 size_t n = 0; 2821 for (size_t i = 0; i < buffers.size(); ++i) { 2822 if (buffers[i].mStatus != OWNED_BY_COMPONENT) { 2823 ++n; 2824 } 2825 } 2826 2827 return n; 2828 } 2829 2830 status_t OMXCodec::freeBuffersOnPort( 2831 OMX_U32 portIndex, bool onlyThoseWeOwn) { 2832 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; 2833 2834 status_t stickyErr = OK; 2835 2836 for (size_t i = buffers->size(); i-- > 0;) { 2837 BufferInfo *info = &buffers->editItemAt(i); 2838 2839 if (onlyThoseWeOwn && info->mStatus == OWNED_BY_COMPONENT) { 2840 continue; 2841 } 2842 2843 CHECK(info->mStatus == OWNED_BY_US 2844 || info->mStatus == OWNED_BY_NATIVE_WINDOW); 2845 2846 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex); 2847 2848 status_t err = freeBuffer(portIndex, i); 2849 2850 if (err != OK) { 2851 stickyErr = err; 2852 } 2853 2854 } 2855 2856 CHECK(onlyThoseWeOwn || buffers->isEmpty()); 2857 2858 return stickyErr; 2859 } 2860 2861 status_t OMXCodec::freeBuffer(OMX_U32 portIndex, size_t bufIndex) { 2862 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; 2863 2864 BufferInfo *info = &buffers->editItemAt(bufIndex); 2865 2866 status_t err = mOMX->freeBuffer(mNode, portIndex, info->mBuffer); 2867 2868 if (err == OK && info->mMediaBuffer != NULL) { 2869 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); 2870 info->mMediaBuffer->setObserver(NULL); 2871 2872 // Make sure nobody but us owns this buffer at this point. 2873 CHECK_EQ(info->mMediaBuffer->refcount(), 0); 2874 2875 // Cancel the buffer if it belongs to an ANativeWindow. 2876 sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer(); 2877 if (info->mStatus == OWNED_BY_US && graphicBuffer != 0) { 2878 err = cancelBufferToNativeWindow(info); 2879 } 2880 2881 info->mMediaBuffer->release(); 2882 info->mMediaBuffer = NULL; 2883 } 2884 2885 if (err == OK) { 2886 buffers->removeAt(bufIndex); 2887 } 2888 2889 return err; 2890 } 2891 2892 void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) { 2893 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex); 2894 2895 CHECK(mState == EXECUTING || mState == EXECUTING_TO_IDLE); 2896 CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); 2897 CHECK(!mOutputPortSettingsChangedPending); 2898 2899 if (mPortStatus[kPortIndexOutput] != ENABLED) { 2900 CODEC_LOGV("Deferring output port settings change."); 2901 mOutputPortSettingsChangedPending = true; 2902 return; 2903 } 2904 2905 setState(RECONFIGURING); 2906 2907 if (mQuirks & kNeedsFlushBeforeDisable) { 2908 if (!flushPortAsync(portIndex)) { 2909 onCmdComplete(OMX_CommandFlush, portIndex); 2910 } 2911 } else { 2912 disablePortAsync(portIndex); 2913 } 2914 } 2915 2916 bool OMXCodec::flushPortAsync(OMX_U32 portIndex) { 2917 CHECK(mState == EXECUTING || mState == RECONFIGURING 2918 || mState == EXECUTING_TO_IDLE); 2919 2920 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.", 2921 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]), 2922 mPortBuffers[portIndex].size()); 2923 2924 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED); 2925 mPortStatus[portIndex] = SHUTTING_DOWN; 2926 2927 if ((mQuirks & kRequiresFlushCompleteEmulation) 2928 && countBuffersWeOwn(mPortBuffers[portIndex]) 2929 == mPortBuffers[portIndex].size()) { 2930 // No flush is necessary and this component fails to send a 2931 // flush-complete event in this case. 2932 2933 return false; 2934 } 2935 2936 status_t err = 2937 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex); 2938 CHECK_EQ(err, (status_t)OK); 2939 2940 return true; 2941 } 2942 2943 void OMXCodec::disablePortAsync(OMX_U32 portIndex) { 2944 CHECK(mState == EXECUTING || mState == RECONFIGURING); 2945 2946 CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED); 2947 mPortStatus[portIndex] = DISABLING; 2948 2949 CODEC_LOGV("sending OMX_CommandPortDisable(%ld)", portIndex); 2950 status_t err = 2951 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex); 2952 CHECK_EQ(err, (status_t)OK); 2953 2954 freeBuffersOnPort(portIndex, true); 2955 } 2956 2957 status_t OMXCodec::enablePortAsync(OMX_U32 portIndex) { 2958 CHECK(mState == EXECUTING || mState == RECONFIGURING); 2959 2960 CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLED); 2961 mPortStatus[portIndex] = ENABLING; 2962 2963 CODEC_LOGV("sending OMX_CommandPortEnable(%ld)", portIndex); 2964 return mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex); 2965 } 2966 2967 void OMXCodec::fillOutputBuffers() { 2968 CHECK_EQ((int)mState, (int)EXECUTING); 2969 2970 // This is a workaround for some decoders not properly reporting 2971 // end-of-output-stream. If we own all input buffers and also own 2972 // all output buffers and we already signalled end-of-input-stream, 2973 // the end-of-output-stream is implied. 2974 if (mSignalledEOS 2975 && countBuffersWeOwn(mPortBuffers[kPortIndexInput]) 2976 == mPortBuffers[kPortIndexInput].size() 2977 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput]) 2978 == mPortBuffers[kPortIndexOutput].size()) { 2979 mNoMoreOutputData = true; 2980 mBufferFilled.signal(); 2981 2982 return; 2983 } 2984 2985 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 2986 for (size_t i = 0; i < buffers->size(); ++i) { 2987 BufferInfo *info = &buffers->editItemAt(i); 2988 if (info->mStatus == OWNED_BY_US) { 2989 fillOutputBuffer(&buffers->editItemAt(i)); 2990 } 2991 } 2992 } 2993 2994 void OMXCodec::drainInputBuffers() { 2995 CHECK(mState == EXECUTING || mState == RECONFIGURING); 2996 2997 if (mFlags & kUseSecureInputBuffers) { 2998 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 2999 for (size_t i = 0; i < buffers->size(); ++i) { 3000 if (!drainAnyInputBuffer() 3001 || (mFlags & kOnlySubmitOneInputBufferAtOneTime)) { 3002 break; 3003 } 3004 } 3005 } else { 3006 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 3007 for (size_t i = 0; i < buffers->size(); ++i) { 3008 BufferInfo *info = &buffers->editItemAt(i); 3009 3010 if (info->mStatus != OWNED_BY_US) { 3011 continue; 3012 } 3013 3014 if (!drainInputBuffer(info)) { 3015 break; 3016 } 3017 3018 if (mFlags & kOnlySubmitOneInputBufferAtOneTime) { 3019 break; 3020 } 3021 } 3022 } 3023 } 3024 3025 bool OMXCodec::drainAnyInputBuffer() { 3026 return drainInputBuffer((BufferInfo *)NULL); 3027 } 3028 3029 OMXCodec::BufferInfo *OMXCodec::findInputBufferByDataPointer(void *ptr) { 3030 Vector<BufferInfo> *infos = &mPortBuffers[kPortIndexInput]; 3031 for (size_t i = 0; i < infos->size(); ++i) { 3032 BufferInfo *info = &infos->editItemAt(i); 3033 3034 if (info->mData == ptr) { 3035 CODEC_LOGV( 3036 "input buffer data ptr = %p, buffer_id = %p", 3037 ptr, 3038 info->mBuffer); 3039 3040 return info; 3041 } 3042 } 3043 3044 TRESPASS(); 3045 } 3046 3047 OMXCodec::BufferInfo *OMXCodec::findEmptyInputBuffer() { 3048 Vector<BufferInfo> *infos = &mPortBuffers[kPortIndexInput]; 3049 for (size_t i = 0; i < infos->size(); ++i) { 3050 BufferInfo *info = &infos->editItemAt(i); 3051 3052 if (info->mStatus == OWNED_BY_US) { 3053 return info; 3054 } 3055 } 3056 3057 TRESPASS(); 3058 } 3059 3060 bool OMXCodec::drainInputBuffer(BufferInfo *info) { 3061 if (info != NULL) { 3062 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US); 3063 } 3064 3065 if (mSignalledEOS) { 3066 return false; 3067 } 3068 3069 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) { 3070 CHECK(!(mFlags & kUseSecureInputBuffers)); 3071 3072 const CodecSpecificData *specific = 3073 mCodecSpecificData[mCodecSpecificDataIndex]; 3074 3075 size_t size = specific->mSize; 3076 3077 if ((!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME) || 3078 !strcasecmp(MEDIA_MIMETYPE_VIDEO_HEVC, mMIME)) 3079 && !(mQuirks & kWantsNALFragments)) { 3080 static const uint8_t kNALStartCode[4] = 3081 { 0x00, 0x00, 0x00, 0x01 }; 3082 3083 CHECK(info->mSize >= specific->mSize + 4); 3084 3085 size += 4; 3086 3087 memcpy(info->mData, kNALStartCode, 4); 3088 memcpy((uint8_t *)info->mData + 4, 3089 specific->mData, specific->mSize); 3090 } else { 3091 CHECK(info->mSize >= specific->mSize); 3092 memcpy(info->mData, specific->mData, specific->mSize); 3093 } 3094 3095 mNoMoreOutputData = false; 3096 3097 CODEC_LOGV("calling emptyBuffer with codec specific data"); 3098 3099 status_t err = mOMX->emptyBuffer( 3100 mNode, info->mBuffer, 0, size, 3101 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG, 3102 0); 3103 CHECK_EQ(err, (status_t)OK); 3104 3105 info->mStatus = OWNED_BY_COMPONENT; 3106 3107 ++mCodecSpecificDataIndex; 3108 return true; 3109 } 3110 3111 if (mPaused) { 3112 return false; 3113 } 3114 3115 status_t err; 3116 3117 bool signalEOS = false; 3118 int64_t timestampUs = 0; 3119 3120 size_t offset = 0; 3121 int32_t n = 0; 3122 3123 3124 for (;;) { 3125 MediaBuffer *srcBuffer; 3126 if (mSeekTimeUs >= 0) { 3127 if (mLeftOverBuffer) { 3128 mLeftOverBuffer->release(); 3129 mLeftOverBuffer = NULL; 3130 } 3131 3132 MediaSource::ReadOptions options; 3133 options.setSeekTo(mSeekTimeUs, mSeekMode); 3134 3135 mSeekTimeUs = -1; 3136 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC; 3137 mBufferFilled.signal(); 3138 3139 err = mSource->read(&srcBuffer, &options); 3140 3141 if (err == OK) { 3142 int64_t targetTimeUs; 3143 if (srcBuffer->meta_data()->findInt64( 3144 kKeyTargetTime, &targetTimeUs) 3145 && targetTimeUs >= 0) { 3146 CODEC_LOGV("targetTimeUs = %lld us", targetTimeUs); 3147 mTargetTimeUs = targetTimeUs; 3148 } else { 3149 mTargetTimeUs = -1; 3150 } 3151 } 3152 } else if (mLeftOverBuffer) { 3153 srcBuffer = mLeftOverBuffer; 3154 mLeftOverBuffer = NULL; 3155 3156 err = OK; 3157 } else { 3158 err = mSource->read(&srcBuffer); 3159 } 3160 3161 if (err != OK) { 3162 signalEOS = true; 3163 mFinalStatus = err; 3164 mSignalledEOS = true; 3165 mBufferFilled.signal(); 3166 break; 3167 } 3168 3169 if (mFlags & kUseSecureInputBuffers) { 3170 info = findInputBufferByDataPointer(srcBuffer->data()); 3171 CHECK(info != NULL); 3172 } 3173 3174 size_t remainingBytes = info->mSize - offset; 3175 3176 if (srcBuffer->range_length() > remainingBytes) { 3177 if (offset == 0) { 3178 CODEC_LOGE( 3179 "Codec's input buffers are too small to accomodate " 3180 "buffer read from source (info->mSize = %d, srcLength = %d)", 3181 info->mSize, srcBuffer->range_length()); 3182 3183 srcBuffer->release(); 3184 srcBuffer = NULL; 3185 3186 setState(ERROR); 3187 return false; 3188 } 3189 3190 mLeftOverBuffer = srcBuffer; 3191 break; 3192 } 3193 3194 bool releaseBuffer = true; 3195 if (mFlags & kStoreMetaDataInVideoBuffers) { 3196 releaseBuffer = false; 3197 info->mMediaBuffer = srcBuffer; 3198 } 3199 3200 if (mFlags & kUseSecureInputBuffers) { 3201 // Data in "info" is already provided at this time. 3202 3203 releaseBuffer = false; 3204 3205 CHECK(info->mMediaBuffer == NULL); 3206 info->mMediaBuffer = srcBuffer; 3207 } else { 3208 CHECK(srcBuffer->data() != NULL) ; 3209 memcpy((uint8_t *)info->mData + offset, 3210 (const uint8_t *)srcBuffer->data() 3211 + srcBuffer->range_offset(), 3212 srcBuffer->range_length()); 3213 } 3214 3215 int64_t lastBufferTimeUs; 3216 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs)); 3217 CHECK(lastBufferTimeUs >= 0); 3218 if (mIsEncoder && mIsVideo) { 3219 mDecodingTimeList.push_back(lastBufferTimeUs); 3220 } 3221 3222 if (offset == 0) { 3223 timestampUs = lastBufferTimeUs; 3224 } 3225 3226 offset += srcBuffer->range_length(); 3227 3228 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_VORBIS, mMIME)) { 3229 CHECK(!(mQuirks & kSupportsMultipleFramesPerInputBuffer)); 3230 CHECK_GE(info->mSize, offset + sizeof(int32_t)); 3231 3232 int32_t numPageSamples; 3233 if (!srcBuffer->meta_data()->findInt32( 3234 kKeyValidSamples, &numPageSamples)) { 3235 numPageSamples = -1; 3236 } 3237 3238 memcpy((uint8_t *)info->mData + offset, 3239 &numPageSamples, 3240 sizeof(numPageSamples)); 3241 3242 offset += sizeof(numPageSamples); 3243 } 3244 3245 if (releaseBuffer) { 3246 srcBuffer->release(); 3247 srcBuffer = NULL; 3248 } 3249 3250 ++n; 3251 3252 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) { 3253 break; 3254 } 3255 3256 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs; 3257 3258 if (coalescedDurationUs > 250000ll) { 3259 // Don't coalesce more than 250ms worth of encoded data at once. 3260 break; 3261 } 3262 } 3263 3264 if (n > 1) { 3265 ALOGV("coalesced %d frames into one input buffer", n); 3266 } 3267 3268 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME; 3269 3270 if (signalEOS) { 3271 flags |= OMX_BUFFERFLAG_EOS; 3272 } else { 3273 mNoMoreOutputData = false; 3274 } 3275 3276 if (info == NULL) { 3277 CHECK(mFlags & kUseSecureInputBuffers); 3278 CHECK(signalEOS); 3279 3280 // This is fishy, there's still a MediaBuffer corresponding to this 3281 // info available to the source at this point even though we're going 3282 // to use it to signal EOS to the codec. 3283 info = findEmptyInputBuffer(); 3284 } 3285 3286 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), " 3287 "timestamp %lld us (%.2f secs)", 3288 info->mBuffer, offset, 3289 timestampUs, timestampUs / 1E6); 3290 3291 err = mOMX->emptyBuffer( 3292 mNode, info->mBuffer, 0, offset, 3293 flags, timestampUs); 3294 3295 if (err != OK) { 3296 setState(ERROR); 3297 return false; 3298 } 3299 3300 info->mStatus = OWNED_BY_COMPONENT; 3301 3302 return true; 3303 } 3304 3305 void OMXCodec::fillOutputBuffer(BufferInfo *info) { 3306 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US); 3307 3308 if (mNoMoreOutputData) { 3309 CODEC_LOGV("There is no more output data available, not " 3310 "calling fillOutputBuffer"); 3311 return; 3312 } 3313 3314 CODEC_LOGV("Calling fillBuffer on buffer %p", info->mBuffer); 3315 status_t err = mOMX->fillBuffer(mNode, info->mBuffer); 3316 3317 if (err != OK) { 3318 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err); 3319 3320 setState(ERROR); 3321 return; 3322 } 3323 3324 info->mStatus = OWNED_BY_COMPONENT; 3325 } 3326 3327 bool OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) { 3328 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 3329 for (size_t i = 0; i < buffers->size(); ++i) { 3330 if ((*buffers)[i].mBuffer == buffer) { 3331 return drainInputBuffer(&buffers->editItemAt(i)); 3332 } 3333 } 3334 3335 CHECK(!"should not be here."); 3336 3337 return false; 3338 } 3339 3340 void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) { 3341 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 3342 for (size_t i = 0; i < buffers->size(); ++i) { 3343 if ((*buffers)[i].mBuffer == buffer) { 3344 fillOutputBuffer(&buffers->editItemAt(i)); 3345 return; 3346 } 3347 } 3348 3349 CHECK(!"should not be here."); 3350 } 3351 3352 void OMXCodec::setState(State newState) { 3353 mState = newState; 3354 mAsyncCompletion.signal(); 3355 3356 // This may cause some spurious wakeups but is necessary to 3357 // unblock the reader if we enter ERROR state. 3358 mBufferFilled.signal(); 3359 } 3360 3361 status_t OMXCodec::waitForBufferFilled_l() { 3362 3363 if (mIsEncoder) { 3364 // For timelapse video recording, the timelapse video recording may 3365 // not send an input frame for a _long_ time. Do not use timeout 3366 // for video encoding. 3367 return mBufferFilled.wait(mLock); 3368 } 3369 status_t err = mBufferFilled.waitRelative(mLock, kBufferFilledEventTimeOutNs); 3370 if (err != OK) { 3371 CODEC_LOGE("Timed out waiting for output buffers: %d/%d", 3372 countBuffersWeOwn(mPortBuffers[kPortIndexInput]), 3373 countBuffersWeOwn(mPortBuffers[kPortIndexOutput])); 3374 } 3375 return err; 3376 } 3377 3378 void OMXCodec::setRawAudioFormat( 3379 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) { 3380 3381 // port definition 3382 OMX_PARAM_PORTDEFINITIONTYPE def; 3383 InitOMXParams(&def); 3384 def.nPortIndex = portIndex; 3385 status_t err = mOMX->getParameter( 3386 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 3387 CHECK_EQ(err, (status_t)OK); 3388 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM; 3389 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition, 3390 &def, sizeof(def)), (status_t)OK); 3391 3392 // pcm param 3393 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams; 3394 InitOMXParams(&pcmParams); 3395 pcmParams.nPortIndex = portIndex; 3396 3397 err = mOMX->getParameter( 3398 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); 3399 3400 CHECK_EQ(err, (status_t)OK); 3401 3402 pcmParams.nChannels = numChannels; 3403 pcmParams.eNumData = OMX_NumericalDataSigned; 3404 pcmParams.bInterleaved = OMX_TRUE; 3405 pcmParams.nBitPerSample = 16; 3406 pcmParams.nSamplingRate = sampleRate; 3407 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear; 3408 3409 CHECK_EQ(getOMXChannelMapping( 3410 numChannels, pcmParams.eChannelMapping), (status_t)OK); 3411 3412 err = mOMX->setParameter( 3413 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); 3414 3415 CHECK_EQ(err, (status_t)OK); 3416 } 3417 3418 static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) { 3419 if (isAMRWB) { 3420 if (bps <= 6600) { 3421 return OMX_AUDIO_AMRBandModeWB0; 3422 } else if (bps <= 8850) { 3423 return OMX_AUDIO_AMRBandModeWB1; 3424 } else if (bps <= 12650) { 3425 return OMX_AUDIO_AMRBandModeWB2; 3426 } else if (bps <= 14250) { 3427 return OMX_AUDIO_AMRBandModeWB3; 3428 } else if (bps <= 15850) { 3429 return OMX_AUDIO_AMRBandModeWB4; 3430 } else if (bps <= 18250) { 3431 return OMX_AUDIO_AMRBandModeWB5; 3432 } else if (bps <= 19850) { 3433 return OMX_AUDIO_AMRBandModeWB6; 3434 } else if (bps <= 23050) { 3435 return OMX_AUDIO_AMRBandModeWB7; 3436 } 3437 3438 // 23850 bps 3439 return OMX_AUDIO_AMRBandModeWB8; 3440 } else { // AMRNB 3441 if (bps <= 4750) { 3442 return OMX_AUDIO_AMRBandModeNB0; 3443 } else if (bps <= 5150) { 3444 return OMX_AUDIO_AMRBandModeNB1; 3445 } else if (bps <= 5900) { 3446 return OMX_AUDIO_AMRBandModeNB2; 3447 } else if (bps <= 6700) { 3448 return OMX_AUDIO_AMRBandModeNB3; 3449 } else if (bps <= 7400) { 3450 return OMX_AUDIO_AMRBandModeNB4; 3451 } else if (bps <= 7950) { 3452 return OMX_AUDIO_AMRBandModeNB5; 3453 } else if (bps <= 10200) { 3454 return OMX_AUDIO_AMRBandModeNB6; 3455 } 3456 3457 // 12200 bps 3458 return OMX_AUDIO_AMRBandModeNB7; 3459 } 3460 } 3461 3462 void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) { 3463 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput; 3464 3465 OMX_AUDIO_PARAM_AMRTYPE def; 3466 InitOMXParams(&def); 3467 def.nPortIndex = portIndex; 3468 3469 status_t err = 3470 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 3471 3472 CHECK_EQ(err, (status_t)OK); 3473 3474 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF; 3475 3476 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate); 3477 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 3478 CHECK_EQ(err, (status_t)OK); 3479 3480 //////////////////////// 3481 3482 if (mIsEncoder) { 3483 sp<MetaData> format = mSource->getFormat(); 3484 int32_t sampleRate; 3485 int32_t numChannels; 3486 CHECK(format->findInt32(kKeySampleRate, &sampleRate)); 3487 CHECK(format->findInt32(kKeyChannelCount, &numChannels)); 3488 3489 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 3490 } 3491 } 3492 3493 status_t OMXCodec::setAACFormat( 3494 int32_t numChannels, int32_t sampleRate, int32_t bitRate, int32_t aacProfile, bool isADTS) { 3495 if (numChannels > 2) { 3496 ALOGW("Number of channels: (%d) \n", numChannels); 3497 } 3498 3499 if (mIsEncoder) { 3500 if (isADTS) { 3501 return -EINVAL; 3502 } 3503 3504 //////////////// input port //////////////////// 3505 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 3506 3507 //////////////// output port //////////////////// 3508 // format 3509 OMX_AUDIO_PARAM_PORTFORMATTYPE format; 3510 InitOMXParams(&format); 3511 format.nPortIndex = kPortIndexOutput; 3512 format.nIndex = 0; 3513 status_t err = OMX_ErrorNone; 3514 while (OMX_ErrorNone == err) { 3515 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat, 3516 &format, sizeof(format)), (status_t)OK); 3517 if (format.eEncoding == OMX_AUDIO_CodingAAC) { 3518 break; 3519 } 3520 format.nIndex++; 3521 } 3522 CHECK_EQ((status_t)OK, err); 3523 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat, 3524 &format, sizeof(format)), (status_t)OK); 3525 3526 // port definition 3527 OMX_PARAM_PORTDEFINITIONTYPE def; 3528 InitOMXParams(&def); 3529 def.nPortIndex = kPortIndexOutput; 3530 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition, 3531 &def, sizeof(def)), (status_t)OK); 3532 def.format.audio.bFlagErrorConcealment = OMX_TRUE; 3533 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC; 3534 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition, 3535 &def, sizeof(def)), (status_t)OK); 3536 3537 // profile 3538 OMX_AUDIO_PARAM_AACPROFILETYPE profile; 3539 InitOMXParams(&profile); 3540 profile.nPortIndex = kPortIndexOutput; 3541 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac, 3542 &profile, sizeof(profile)), (status_t)OK); 3543 profile.nChannels = numChannels; 3544 profile.eChannelMode = (numChannels == 1? 3545 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo); 3546 profile.nSampleRate = sampleRate; 3547 profile.nBitRate = bitRate; 3548 profile.nAudioBandWidth = 0; 3549 profile.nFrameLength = 0; 3550 profile.nAACtools = OMX_AUDIO_AACToolAll; 3551 profile.nAACERtools = OMX_AUDIO_AACERNone; 3552 profile.eAACProfile = (OMX_AUDIO_AACPROFILETYPE) aacProfile; 3553 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF; 3554 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAac, 3555 &profile, sizeof(profile)); 3556 3557 if (err != OK) { 3558 CODEC_LOGE("setParameter('OMX_IndexParamAudioAac') failed " 3559 "(err = %d)", 3560 err); 3561 return err; 3562 } 3563 } else { 3564 OMX_AUDIO_PARAM_AACPROFILETYPE profile; 3565 InitOMXParams(&profile); 3566 profile.nPortIndex = kPortIndexInput; 3567 3568 status_t err = mOMX->getParameter( 3569 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); 3570 CHECK_EQ(err, (status_t)OK); 3571 3572 profile.nChannels = numChannels; 3573 profile.nSampleRate = sampleRate; 3574 3575 profile.eAACStreamFormat = 3576 isADTS 3577 ? OMX_AUDIO_AACStreamFormatMP4ADTS 3578 : OMX_AUDIO_AACStreamFormatMP4FF; 3579 3580 err = mOMX->setParameter( 3581 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); 3582 3583 if (err != OK) { 3584 CODEC_LOGE("setParameter('OMX_IndexParamAudioAac') failed " 3585 "(err = %d)", 3586 err); 3587 return err; 3588 } 3589 } 3590 3591 return OK; 3592 } 3593 3594 status_t OMXCodec::setAC3Format(int32_t numChannels, int32_t sampleRate) { 3595 OMX_AUDIO_PARAM_ANDROID_AC3TYPE def; 3596 InitOMXParams(&def); 3597 def.nPortIndex = kPortIndexInput; 3598 3599 status_t err = mOMX->getParameter( 3600 mNode, 3601 (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidAc3, 3602 &def, 3603 sizeof(def)); 3604 3605 if (err != OK) { 3606 return err; 3607 } 3608 3609 def.nChannels = numChannels; 3610 def.nSampleRate = sampleRate; 3611 3612 return mOMX->setParameter( 3613 mNode, 3614 (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidAc3, 3615 &def, 3616 sizeof(def)); 3617 } 3618 3619 void OMXCodec::setG711Format(int32_t numChannels) { 3620 CHECK(!mIsEncoder); 3621 setRawAudioFormat(kPortIndexInput, 8000, numChannels); 3622 } 3623 3624 void OMXCodec::setImageOutputFormat( 3625 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) { 3626 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height); 3627 3628 #if 0 3629 OMX_INDEXTYPE index; 3630 status_t err = mOMX->get_extension_index( 3631 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index); 3632 CHECK_EQ(err, (status_t)OK); 3633 3634 err = mOMX->set_config(mNode, index, &format, sizeof(format)); 3635 CHECK_EQ(err, (status_t)OK); 3636 #endif 3637 3638 OMX_PARAM_PORTDEFINITIONTYPE def; 3639 InitOMXParams(&def); 3640 def.nPortIndex = kPortIndexOutput; 3641 3642 status_t err = mOMX->getParameter( 3643 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 3644 CHECK_EQ(err, (status_t)OK); 3645 3646 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage); 3647 3648 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 3649 3650 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingUnused); 3651 imageDef->eColorFormat = format; 3652 imageDef->nFrameWidth = width; 3653 imageDef->nFrameHeight = height; 3654 3655 switch (format) { 3656 case OMX_COLOR_FormatYUV420PackedPlanar: 3657 case OMX_COLOR_FormatYUV411Planar: 3658 { 3659 def.nBufferSize = (width * height * 3) / 2; 3660 break; 3661 } 3662 3663 case OMX_COLOR_FormatCbYCrY: 3664 { 3665 def.nBufferSize = width * height * 2; 3666 break; 3667 } 3668 3669 case OMX_COLOR_Format32bitARGB8888: 3670 { 3671 def.nBufferSize = width * height * 4; 3672 break; 3673 } 3674 3675 case OMX_COLOR_Format16bitARGB4444: 3676 case OMX_COLOR_Format16bitARGB1555: 3677 case OMX_COLOR_Format16bitRGB565: 3678 case OMX_COLOR_Format16bitBGR565: 3679 { 3680 def.nBufferSize = width * height * 2; 3681 break; 3682 } 3683 3684 default: 3685 CHECK(!"Should not be here. Unknown color format."); 3686 break; 3687 } 3688 3689 def.nBufferCountActual = def.nBufferCountMin; 3690 3691 err = mOMX->setParameter( 3692 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 3693 CHECK_EQ(err, (status_t)OK); 3694 } 3695 3696 void OMXCodec::setJPEGInputFormat( 3697 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) { 3698 OMX_PARAM_PORTDEFINITIONTYPE def; 3699 InitOMXParams(&def); 3700 def.nPortIndex = kPortIndexInput; 3701 3702 status_t err = mOMX->getParameter( 3703 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 3704 CHECK_EQ(err, (status_t)OK); 3705 3706 CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage); 3707 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 3708 3709 CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingJPEG); 3710 imageDef->nFrameWidth = width; 3711 imageDef->nFrameHeight = height; 3712 3713 def.nBufferSize = compressedSize; 3714 def.nBufferCountActual = def.nBufferCountMin; 3715 3716 err = mOMX->setParameter( 3717 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 3718 CHECK_EQ(err, (status_t)OK); 3719 } 3720 3721 void OMXCodec::addCodecSpecificData(const void *data, size_t size) { 3722 CodecSpecificData *specific = 3723 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1); 3724 3725 specific->mSize = size; 3726 memcpy(specific->mData, data, size); 3727 3728 mCodecSpecificData.push(specific); 3729 } 3730 3731 void OMXCodec::clearCodecSpecificData() { 3732 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) { 3733 free(mCodecSpecificData.editItemAt(i)); 3734 } 3735 mCodecSpecificData.clear(); 3736 mCodecSpecificDataIndex = 0; 3737 } 3738 3739 status_t OMXCodec::start(MetaData *meta) { 3740 Mutex::Autolock autoLock(mLock); 3741 3742 if (mState != LOADED) { 3743 CODEC_LOGE("called start in the unexpected state: %d", mState); 3744 return UNKNOWN_ERROR; 3745 } 3746 3747 sp<MetaData> params = new MetaData; 3748 if (mQuirks & kWantsNALFragments) { 3749 params->setInt32(kKeyWantsNALFragments, true); 3750 } 3751 if (meta) { 3752 int64_t startTimeUs = 0; 3753 int64_t timeUs; 3754 if (meta->findInt64(kKeyTime, &timeUs)) { 3755 startTimeUs = timeUs; 3756 } 3757 params->setInt64(kKeyTime, startTimeUs); 3758 } 3759 3760 mCodecSpecificDataIndex = 0; 3761 mInitialBufferSubmit = true; 3762 mSignalledEOS = false; 3763 mNoMoreOutputData = false; 3764 mOutputPortSettingsHaveChanged = false; 3765 mSeekTimeUs = -1; 3766 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC; 3767 mTargetTimeUs = -1; 3768 mFilledBuffers.clear(); 3769 mPaused = false; 3770 3771 status_t err; 3772 if (mIsEncoder) { 3773 // Calling init() before starting its source so that we can configure, 3774 // if supported, the source to use exactly the same number of input 3775 // buffers as requested by the encoder. 3776 if ((err = init()) != OK) { 3777 CODEC_LOGE("init failed: %d", err); 3778 return err; 3779 } 3780 3781 params->setInt32(kKeyNumBuffers, mPortBuffers[kPortIndexInput].size()); 3782 err = mSource->start(params.get()); 3783 if (err != OK) { 3784 CODEC_LOGE("source failed to start: %d", err); 3785 stopOmxComponent_l(); 3786 } 3787 return err; 3788 } 3789 3790 // Decoder case 3791 if ((err = mSource->start(params.get())) != OK) { 3792 CODEC_LOGE("source failed to start: %d", err); 3793 return err; 3794 } 3795 return init(); 3796 } 3797 3798 status_t OMXCodec::stop() { 3799 CODEC_LOGV("stop mState=%d", mState); 3800 Mutex::Autolock autoLock(mLock); 3801 status_t err = stopOmxComponent_l(); 3802 mSource->stop(); 3803 3804 CODEC_LOGV("stopped in state %d", mState); 3805 return err; 3806 } 3807 3808 status_t OMXCodec::stopOmxComponent_l() { 3809 CODEC_LOGV("stopOmxComponent_l mState=%d", mState); 3810 3811 while (isIntermediateState(mState)) { 3812 mAsyncCompletion.wait(mLock); 3813 } 3814 3815 bool isError = false; 3816 switch (mState) { 3817 case LOADED: 3818 break; 3819 3820 case ERROR: 3821 { 3822 if (mPortStatus[kPortIndexOutput] == ENABLING) { 3823 // Codec is in a wedged state (technical term) 3824 // We've seen an output port settings change from the codec, 3825 // We've disabled the output port, then freed the output 3826 // buffers, initiated re-enabling the output port but 3827 // failed to reallocate the output buffers. 3828 // There doesn't seem to be a way to orderly transition 3829 // from executing->idle and idle->loaded now that the 3830 // output port hasn't been reenabled yet... 3831 // Simply free as many resources as we can and pretend 3832 // that we're in LOADED state so that the destructor 3833 // will free the component instance without asserting. 3834 freeBuffersOnPort(kPortIndexInput, true /* onlyThoseWeOwn */); 3835 freeBuffersOnPort(kPortIndexOutput, true /* onlyThoseWeOwn */); 3836 setState(LOADED); 3837 break; 3838 } else { 3839 OMX_STATETYPE state = OMX_StateInvalid; 3840 status_t err = mOMX->getState(mNode, &state); 3841 CHECK_EQ(err, (status_t)OK); 3842 3843 if (state != OMX_StateExecuting) { 3844 break; 3845 } 3846 // else fall through to the idling code 3847 } 3848 3849 isError = true; 3850 } 3851 3852 case EXECUTING: 3853 { 3854 setState(EXECUTING_TO_IDLE); 3855 3856 if (mQuirks & kRequiresFlushBeforeShutdown) { 3857 CODEC_LOGV("This component requires a flush before transitioning " 3858 "from EXECUTING to IDLE..."); 3859 3860 bool emulateInputFlushCompletion = 3861 !flushPortAsync(kPortIndexInput); 3862 3863 bool emulateOutputFlushCompletion = 3864 !flushPortAsync(kPortIndexOutput); 3865 3866 if (emulateInputFlushCompletion) { 3867 onCmdComplete(OMX_CommandFlush, kPortIndexInput); 3868 } 3869 3870 if (emulateOutputFlushCompletion) { 3871 onCmdComplete(OMX_CommandFlush, kPortIndexOutput); 3872 } 3873 } else { 3874 mPortStatus[kPortIndexInput] = SHUTTING_DOWN; 3875 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; 3876 3877 status_t err = 3878 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 3879 CHECK_EQ(err, (status_t)OK); 3880 } 3881 3882 while (mState != LOADED && mState != ERROR) { 3883 mAsyncCompletion.wait(mLock); 3884 } 3885 3886 if (isError) { 3887 // We were in the ERROR state coming in, so restore that now 3888 // that we've idled the OMX component. 3889 setState(ERROR); 3890 } 3891 3892 break; 3893 } 3894 3895 default: 3896 { 3897 CHECK(!"should not be here."); 3898 break; 3899 } 3900 } 3901 3902 if (mLeftOverBuffer) { 3903 mLeftOverBuffer->release(); 3904 mLeftOverBuffer = NULL; 3905 } 3906 3907 return OK; 3908 } 3909 3910 sp<MetaData> OMXCodec::getFormat() { 3911 Mutex::Autolock autoLock(mLock); 3912 3913 return mOutputFormat; 3914 } 3915 3916 status_t OMXCodec::read( 3917 MediaBuffer **buffer, const ReadOptions *options) { 3918 status_t err = OK; 3919 *buffer = NULL; 3920 3921 Mutex::Autolock autoLock(mLock); 3922 3923 if (mState != EXECUTING && mState != RECONFIGURING) { 3924 return UNKNOWN_ERROR; 3925 } 3926 3927 bool seeking = false; 3928 int64_t seekTimeUs; 3929 ReadOptions::SeekMode seekMode; 3930 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) { 3931 seeking = true; 3932 } 3933 3934 if (mInitialBufferSubmit) { 3935 mInitialBufferSubmit = false; 3936 3937 if (seeking) { 3938 CHECK(seekTimeUs >= 0); 3939 mSeekTimeUs = seekTimeUs; 3940 mSeekMode = seekMode; 3941 3942 // There's no reason to trigger the code below, there's 3943 // nothing to flush yet. 3944 seeking = false; 3945 mPaused = false; 3946 } 3947 3948 drainInputBuffers(); 3949 3950 if (mState == EXECUTING) { 3951 // Otherwise mState == RECONFIGURING and this code will trigger 3952 // after the output port is reenabled. 3953 fillOutputBuffers(); 3954 } 3955 } 3956 3957 if (seeking) { 3958 while (mState == RECONFIGURING) { 3959 if ((err = waitForBufferFilled_l()) != OK) { 3960 return err; 3961 } 3962 } 3963 3964 if (mState != EXECUTING) { 3965 return UNKNOWN_ERROR; 3966 } 3967 3968 CODEC_LOGV("seeking to %" PRId64 " us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6); 3969 3970 mSignalledEOS = false; 3971 3972 CHECK(seekTimeUs >= 0); 3973 mSeekTimeUs = seekTimeUs; 3974 mSeekMode = seekMode; 3975 3976 mFilledBuffers.clear(); 3977 3978 CHECK_EQ((int)mState, (int)EXECUTING); 3979 3980 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput); 3981 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput); 3982 3983 if (emulateInputFlushCompletion) { 3984 onCmdComplete(OMX_CommandFlush, kPortIndexInput); 3985 } 3986 3987 if (emulateOutputFlushCompletion) { 3988 onCmdComplete(OMX_CommandFlush, kPortIndexOutput); 3989 } 3990 3991 while (mSeekTimeUs >= 0) { 3992 if ((err = waitForBufferFilled_l()) != OK) { 3993 return err; 3994 } 3995 } 3996 } 3997 3998 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) { 3999 if ((err = waitForBufferFilled_l()) != OK) { 4000 return err; 4001 } 4002 } 4003 4004 if (mState == ERROR) { 4005 return UNKNOWN_ERROR; 4006 } 4007 4008 if (mFilledBuffers.empty()) { 4009 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM; 4010 } 4011 4012 if (mOutputPortSettingsHaveChanged) { 4013 mOutputPortSettingsHaveChanged = false; 4014 4015 return INFO_FORMAT_CHANGED; 4016 } 4017 4018 size_t index = *mFilledBuffers.begin(); 4019 mFilledBuffers.erase(mFilledBuffers.begin()); 4020 4021 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index); 4022 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US); 4023 info->mStatus = OWNED_BY_CLIENT; 4024 4025 info->mMediaBuffer->add_ref(); 4026 if (mSkipCutBuffer != NULL) { 4027 mSkipCutBuffer->submit(info->mMediaBuffer); 4028 } 4029 *buffer = info->mMediaBuffer; 4030 4031 return OK; 4032 } 4033 4034 void OMXCodec::signalBufferReturned(MediaBuffer *buffer) { 4035 Mutex::Autolock autoLock(mLock); 4036 4037 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 4038 for (size_t i = 0; i < buffers->size(); ++i) { 4039 BufferInfo *info = &buffers->editItemAt(i); 4040 4041 if (info->mMediaBuffer == buffer) { 4042 CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED); 4043 CHECK_EQ((int)info->mStatus, (int)OWNED_BY_CLIENT); 4044 4045 info->mStatus = OWNED_BY_US; 4046 4047 if (buffer->graphicBuffer() == 0) { 4048 fillOutputBuffer(info); 4049 } else { 4050 sp<MetaData> metaData = info->mMediaBuffer->meta_data(); 4051 int32_t rendered = 0; 4052 if (!metaData->findInt32(kKeyRendered, &rendered)) { 4053 rendered = 0; 4054 } 4055 if (!rendered) { 4056 status_t err = cancelBufferToNativeWindow(info); 4057 if (err < 0) { 4058 return; 4059 } 4060 } 4061 4062 info->mStatus = OWNED_BY_NATIVE_WINDOW; 4063 4064 // Dequeue the next buffer from the native window. 4065 BufferInfo *nextBufInfo = dequeueBufferFromNativeWindow(); 4066 if (nextBufInfo == 0) { 4067 return; 4068 } 4069 4070 // Give the buffer to the OMX node to fill. 4071 fillOutputBuffer(nextBufInfo); 4072 } 4073 return; 4074 } 4075 } 4076 4077 CHECK(!"should not be here."); 4078 } 4079 4080 static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) { 4081 static const char *kNames[] = { 4082 "OMX_IMAGE_CodingUnused", 4083 "OMX_IMAGE_CodingAutoDetect", 4084 "OMX_IMAGE_CodingJPEG", 4085 "OMX_IMAGE_CodingJPEG2K", 4086 "OMX_IMAGE_CodingEXIF", 4087 "OMX_IMAGE_CodingTIFF", 4088 "OMX_IMAGE_CodingGIF", 4089 "OMX_IMAGE_CodingPNG", 4090 "OMX_IMAGE_CodingLZW", 4091 "OMX_IMAGE_CodingBMP", 4092 }; 4093 4094 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 4095 4096 if (type < 0 || (size_t)type >= numNames) { 4097 return "UNKNOWN"; 4098 } else { 4099 return kNames[type]; 4100 } 4101 } 4102 4103 static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) { 4104 static const char *kNames[] = { 4105 "OMX_COLOR_FormatUnused", 4106 "OMX_COLOR_FormatMonochrome", 4107 "OMX_COLOR_Format8bitRGB332", 4108 "OMX_COLOR_Format12bitRGB444", 4109 "OMX_COLOR_Format16bitARGB4444", 4110 "OMX_COLOR_Format16bitARGB1555", 4111 "OMX_COLOR_Format16bitRGB565", 4112 "OMX_COLOR_Format16bitBGR565", 4113 "OMX_COLOR_Format18bitRGB666", 4114 "OMX_COLOR_Format18bitARGB1665", 4115 "OMX_COLOR_Format19bitARGB1666", 4116 "OMX_COLOR_Format24bitRGB888", 4117 "OMX_COLOR_Format24bitBGR888", 4118 "OMX_COLOR_Format24bitARGB1887", 4119 "OMX_COLOR_Format25bitARGB1888", 4120 "OMX_COLOR_Format32bitBGRA8888", 4121 "OMX_COLOR_Format32bitARGB8888", 4122 "OMX_COLOR_FormatYUV411Planar", 4123 "OMX_COLOR_FormatYUV411PackedPlanar", 4124 "OMX_COLOR_FormatYUV420Planar", 4125 "OMX_COLOR_FormatYUV420PackedPlanar", 4126 "OMX_COLOR_FormatYUV420SemiPlanar", 4127 "OMX_COLOR_FormatYUV422Planar", 4128 "OMX_COLOR_FormatYUV422PackedPlanar", 4129 "OMX_COLOR_FormatYUV422SemiPlanar", 4130 "OMX_COLOR_FormatYCbYCr", 4131 "OMX_COLOR_FormatYCrYCb", 4132 "OMX_COLOR_FormatCbYCrY", 4133 "OMX_COLOR_FormatCrYCbY", 4134 "OMX_COLOR_FormatYUV444Interleaved", 4135 "OMX_COLOR_FormatRawBayer8bit", 4136 "OMX_COLOR_FormatRawBayer10bit", 4137 "OMX_COLOR_FormatRawBayer8bitcompressed", 4138 "OMX_COLOR_FormatL2", 4139 "OMX_COLOR_FormatL4", 4140 "OMX_COLOR_FormatL8", 4141 "OMX_COLOR_FormatL16", 4142 "OMX_COLOR_FormatL24", 4143 "OMX_COLOR_FormatL32", 4144 "OMX_COLOR_FormatYUV420PackedSemiPlanar", 4145 "OMX_COLOR_FormatYUV422PackedSemiPlanar", 4146 "OMX_COLOR_Format18BitBGR666", 4147 "OMX_COLOR_Format24BitARGB6666", 4148 "OMX_COLOR_Format24BitABGR6666", 4149 }; 4150 4151 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 4152 4153 if (type == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar) { 4154 return "OMX_TI_COLOR_FormatYUV420PackedSemiPlanar"; 4155 } else if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) { 4156 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar"; 4157 } else if (type < 0 || (size_t)type >= numNames) { 4158 return "UNKNOWN"; 4159 } else { 4160 return kNames[type]; 4161 } 4162 } 4163 4164 static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) { 4165 static const char *kNames[] = { 4166 "OMX_VIDEO_CodingUnused", 4167 "OMX_VIDEO_CodingAutoDetect", 4168 "OMX_VIDEO_CodingMPEG2", 4169 "OMX_VIDEO_CodingH263", 4170 "OMX_VIDEO_CodingMPEG4", 4171 "OMX_VIDEO_CodingWMV", 4172 "OMX_VIDEO_CodingRV", 4173 "OMX_VIDEO_CodingAVC", 4174 "OMX_VIDEO_CodingMJPEG", 4175 }; 4176 4177 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 4178 4179 if (type < 0 || (size_t)type >= numNames) { 4180 return "UNKNOWN"; 4181 } else { 4182 return kNames[type]; 4183 } 4184 } 4185 4186 static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) { 4187 static const char *kNames[] = { 4188 "OMX_AUDIO_CodingUnused", 4189 "OMX_AUDIO_CodingAutoDetect", 4190 "OMX_AUDIO_CodingPCM", 4191 "OMX_AUDIO_CodingADPCM", 4192 "OMX_AUDIO_CodingAMR", 4193 "OMX_AUDIO_CodingGSMFR", 4194 "OMX_AUDIO_CodingGSMEFR", 4195 "OMX_AUDIO_CodingGSMHR", 4196 "OMX_AUDIO_CodingPDCFR", 4197 "OMX_AUDIO_CodingPDCEFR", 4198 "OMX_AUDIO_CodingPDCHR", 4199 "OMX_AUDIO_CodingTDMAFR", 4200 "OMX_AUDIO_CodingTDMAEFR", 4201 "OMX_AUDIO_CodingQCELP8", 4202 "OMX_AUDIO_CodingQCELP13", 4203 "OMX_AUDIO_CodingEVRC", 4204 "OMX_AUDIO_CodingSMV", 4205 "OMX_AUDIO_CodingG711", 4206 "OMX_AUDIO_CodingG723", 4207 "OMX_AUDIO_CodingG726", 4208 "OMX_AUDIO_CodingG729", 4209 "OMX_AUDIO_CodingAAC", 4210 "OMX_AUDIO_CodingMP3", 4211 "OMX_AUDIO_CodingSBC", 4212 "OMX_AUDIO_CodingVORBIS", 4213 "OMX_AUDIO_CodingOPUS", 4214 "OMX_AUDIO_CodingWMA", 4215 "OMX_AUDIO_CodingRA", 4216 "OMX_AUDIO_CodingMIDI", 4217 }; 4218 4219 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 4220 4221 if (type < 0 || (size_t)type >= numNames) { 4222 return "UNKNOWN"; 4223 } else { 4224 return kNames[type]; 4225 } 4226 } 4227 4228 static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) { 4229 static const char *kNames[] = { 4230 "OMX_AUDIO_PCMModeLinear", 4231 "OMX_AUDIO_PCMModeALaw", 4232 "OMX_AUDIO_PCMModeMULaw", 4233 }; 4234 4235 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 4236 4237 if (type < 0 || (size_t)type >= numNames) { 4238 return "UNKNOWN"; 4239 } else { 4240 return kNames[type]; 4241 } 4242 } 4243 4244 static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) { 4245 static const char *kNames[] = { 4246 "OMX_AUDIO_AMRBandModeUnused", 4247 "OMX_AUDIO_AMRBandModeNB0", 4248 "OMX_AUDIO_AMRBandModeNB1", 4249 "OMX_AUDIO_AMRBandModeNB2", 4250 "OMX_AUDIO_AMRBandModeNB3", 4251 "OMX_AUDIO_AMRBandModeNB4", 4252 "OMX_AUDIO_AMRBandModeNB5", 4253 "OMX_AUDIO_AMRBandModeNB6", 4254 "OMX_AUDIO_AMRBandModeNB7", 4255 "OMX_AUDIO_AMRBandModeWB0", 4256 "OMX_AUDIO_AMRBandModeWB1", 4257 "OMX_AUDIO_AMRBandModeWB2", 4258 "OMX_AUDIO_AMRBandModeWB3", 4259 "OMX_AUDIO_AMRBandModeWB4", 4260 "OMX_AUDIO_AMRBandModeWB5", 4261 "OMX_AUDIO_AMRBandModeWB6", 4262 "OMX_AUDIO_AMRBandModeWB7", 4263 "OMX_AUDIO_AMRBandModeWB8", 4264 }; 4265 4266 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 4267 4268 if (type < 0 || (size_t)type >= numNames) { 4269 return "UNKNOWN"; 4270 } else { 4271 return kNames[type]; 4272 } 4273 } 4274 4275 static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) { 4276 static const char *kNames[] = { 4277 "OMX_AUDIO_AMRFrameFormatConformance", 4278 "OMX_AUDIO_AMRFrameFormatIF1", 4279 "OMX_AUDIO_AMRFrameFormatIF2", 4280 "OMX_AUDIO_AMRFrameFormatFSF", 4281 "OMX_AUDIO_AMRFrameFormatRTPPayload", 4282 "OMX_AUDIO_AMRFrameFormatITU", 4283 }; 4284 4285 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 4286 4287 if (type < 0 || (size_t)type >= numNames) { 4288 return "UNKNOWN"; 4289 } else { 4290 return kNames[type]; 4291 } 4292 } 4293 4294 void OMXCodec::dumpPortStatus(OMX_U32 portIndex) { 4295 OMX_PARAM_PORTDEFINITIONTYPE def; 4296 InitOMXParams(&def); 4297 def.nPortIndex = portIndex; 4298 4299 status_t err = mOMX->getParameter( 4300 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 4301 CHECK_EQ(err, (status_t)OK); 4302 4303 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output"); 4304 4305 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput) 4306 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput)); 4307 4308 printf(" nBufferCountActual = %" PRIu32 "\n", def.nBufferCountActual); 4309 printf(" nBufferCountMin = %" PRIu32 "\n", def.nBufferCountMin); 4310 printf(" nBufferSize = %" PRIu32 "\n", def.nBufferSize); 4311 4312 switch (def.eDomain) { 4313 case OMX_PortDomainImage: 4314 { 4315 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 4316 4317 printf("\n"); 4318 printf(" // Image\n"); 4319 printf(" nFrameWidth = %" PRIu32 "\n", imageDef->nFrameWidth); 4320 printf(" nFrameHeight = %" PRIu32 "\n", imageDef->nFrameHeight); 4321 printf(" nStride = %" PRIu32 "\n", imageDef->nStride); 4322 4323 printf(" eCompressionFormat = %s\n", 4324 imageCompressionFormatString(imageDef->eCompressionFormat)); 4325 4326 printf(" eColorFormat = %s\n", 4327 colorFormatString(imageDef->eColorFormat)); 4328 4329 break; 4330 } 4331 4332 case OMX_PortDomainVideo: 4333 { 4334 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video; 4335 4336 printf("\n"); 4337 printf(" // Video\n"); 4338 printf(" nFrameWidth = %" PRIu32 "\n", videoDef->nFrameWidth); 4339 printf(" nFrameHeight = %" PRIu32 "\n", videoDef->nFrameHeight); 4340 printf(" nStride = %" PRIu32 "\n", videoDef->nStride); 4341 4342 printf(" eCompressionFormat = %s\n", 4343 videoCompressionFormatString(videoDef->eCompressionFormat)); 4344 4345 printf(" eColorFormat = %s\n", 4346 colorFormatString(videoDef->eColorFormat)); 4347 4348 break; 4349 } 4350 4351 case OMX_PortDomainAudio: 4352 { 4353 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio; 4354 4355 printf("\n"); 4356 printf(" // Audio\n"); 4357 printf(" eEncoding = %s\n", 4358 audioCodingTypeString(audioDef->eEncoding)); 4359 4360 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) { 4361 OMX_AUDIO_PARAM_PCMMODETYPE params; 4362 InitOMXParams(¶ms); 4363 params.nPortIndex = portIndex; 4364 4365 err = mOMX->getParameter( 4366 mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); 4367 CHECK_EQ(err, (status_t)OK); 4368 4369 printf(" nSamplingRate = %" PRIu32 "\n", params.nSamplingRate); 4370 printf(" nChannels = %" PRIu32 "\n", params.nChannels); 4371 printf(" bInterleaved = %d\n", params.bInterleaved); 4372 printf(" nBitPerSample = %" PRIu32 "\n", params.nBitPerSample); 4373 4374 printf(" eNumData = %s\n", 4375 params.eNumData == OMX_NumericalDataSigned 4376 ? "signed" : "unsigned"); 4377 4378 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode)); 4379 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) { 4380 OMX_AUDIO_PARAM_AMRTYPE amr; 4381 InitOMXParams(&amr); 4382 amr.nPortIndex = portIndex; 4383 4384 err = mOMX->getParameter( 4385 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); 4386 CHECK_EQ(err, (status_t)OK); 4387 4388 printf(" nChannels = %" PRIu32 "\n", amr.nChannels); 4389 printf(" eAMRBandMode = %s\n", 4390 amrBandModeString(amr.eAMRBandMode)); 4391 printf(" eAMRFrameFormat = %s\n", 4392 amrFrameFormatString(amr.eAMRFrameFormat)); 4393 } 4394 4395 break; 4396 } 4397 4398 default: 4399 { 4400 printf(" // Unknown\n"); 4401 break; 4402 } 4403 } 4404 4405 printf("}\n"); 4406 } 4407 4408 status_t OMXCodec::initNativeWindow() { 4409 // Enable use of a GraphicBuffer as the output for this node. This must 4410 // happen before getting the IndexParamPortDefinition parameter because it 4411 // will affect the pixel format that the node reports. 4412 status_t err = mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_TRUE); 4413 if (err != 0) { 4414 return err; 4415 } 4416 4417 return OK; 4418 } 4419 4420 void OMXCodec::initNativeWindowCrop() { 4421 int32_t left, top, right, bottom; 4422 4423 CHECK(mOutputFormat->findRect( 4424 kKeyCropRect, 4425 &left, &top, &right, &bottom)); 4426 4427 android_native_rect_t crop; 4428 crop.left = left; 4429 crop.top = top; 4430 crop.right = right + 1; 4431 crop.bottom = bottom + 1; 4432 4433 // We'll ignore any errors here, if the surface is 4434 // already invalid, we'll know soon enough. 4435 native_window_set_crop(mNativeWindow.get(), &crop); 4436 } 4437 4438 void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) { 4439 mOutputFormat = new MetaData; 4440 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName); 4441 if (mIsEncoder) { 4442 int32_t timeScale; 4443 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) { 4444 mOutputFormat->setInt32(kKeyTimeScale, timeScale); 4445 } 4446 } 4447 4448 OMX_PARAM_PORTDEFINITIONTYPE def; 4449 InitOMXParams(&def); 4450 def.nPortIndex = kPortIndexOutput; 4451 4452 status_t err = mOMX->getParameter( 4453 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 4454 CHECK_EQ(err, (status_t)OK); 4455 4456 switch (def.eDomain) { 4457 case OMX_PortDomainImage: 4458 { 4459 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 4460 CHECK_EQ((int)imageDef->eCompressionFormat, 4461 (int)OMX_IMAGE_CodingUnused); 4462 4463 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 4464 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat); 4465 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth); 4466 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight); 4467 break; 4468 } 4469 4470 case OMX_PortDomainAudio: 4471 { 4472 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio; 4473 4474 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) { 4475 OMX_AUDIO_PARAM_PCMMODETYPE params; 4476 InitOMXParams(¶ms); 4477 params.nPortIndex = kPortIndexOutput; 4478 4479 err = mOMX->getParameter( 4480 mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); 4481 CHECK_EQ(err, (status_t)OK); 4482 4483 CHECK_EQ((int)params.eNumData, (int)OMX_NumericalDataSigned); 4484 CHECK_EQ(params.nBitPerSample, 16u); 4485 CHECK_EQ((int)params.ePCMMode, (int)OMX_AUDIO_PCMModeLinear); 4486 4487 int32_t numChannels, sampleRate; 4488 inputFormat->findInt32(kKeyChannelCount, &numChannels); 4489 inputFormat->findInt32(kKeySampleRate, &sampleRate); 4490 4491 if ((OMX_U32)numChannels != params.nChannels) { 4492 ALOGV("Codec outputs a different number of channels than " 4493 "the input stream contains (contains %d channels, " 4494 "codec outputs %ld channels).", 4495 numChannels, params.nChannels); 4496 } 4497 4498 if (sampleRate != (int32_t)params.nSamplingRate) { 4499 ALOGV("Codec outputs at different sampling rate than " 4500 "what the input stream contains (contains data at " 4501 "%d Hz, codec outputs %lu Hz)", 4502 sampleRate, params.nSamplingRate); 4503 } 4504 4505 mOutputFormat->setCString( 4506 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); 4507 4508 // Use the codec-advertised number of channels, as some 4509 // codecs appear to output stereo even if the input data is 4510 // mono. If we know the codec lies about this information, 4511 // use the actual number of channels instead. 4512 mOutputFormat->setInt32( 4513 kKeyChannelCount, 4514 (mQuirks & kDecoderLiesAboutNumberOfChannels) 4515 ? numChannels : params.nChannels); 4516 4517 mOutputFormat->setInt32(kKeySampleRate, params.nSamplingRate); 4518 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) { 4519 OMX_AUDIO_PARAM_AMRTYPE amr; 4520 InitOMXParams(&amr); 4521 amr.nPortIndex = kPortIndexOutput; 4522 4523 err = mOMX->getParameter( 4524 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); 4525 CHECK_EQ(err, (status_t)OK); 4526 4527 CHECK_EQ(amr.nChannels, 1u); 4528 mOutputFormat->setInt32(kKeyChannelCount, 1); 4529 4530 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0 4531 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) { 4532 mOutputFormat->setCString( 4533 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB); 4534 mOutputFormat->setInt32(kKeySampleRate, 8000); 4535 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0 4536 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) { 4537 mOutputFormat->setCString( 4538 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB); 4539 mOutputFormat->setInt32(kKeySampleRate, 16000); 4540 } else { 4541 CHECK(!"Unknown AMR band mode."); 4542 } 4543 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) { 4544 mOutputFormat->setCString( 4545 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC); 4546 int32_t numChannels, sampleRate, bitRate; 4547 inputFormat->findInt32(kKeyChannelCount, &numChannels); 4548 inputFormat->findInt32(kKeySampleRate, &sampleRate); 4549 inputFormat->findInt32(kKeyBitRate, &bitRate); 4550 mOutputFormat->setInt32(kKeyChannelCount, numChannels); 4551 mOutputFormat->setInt32(kKeySampleRate, sampleRate); 4552 mOutputFormat->setInt32(kKeyBitRate, bitRate); 4553 } else if (audio_def->eEncoding == 4554 (OMX_AUDIO_CODINGTYPE)OMX_AUDIO_CodingAndroidAC3) { 4555 mOutputFormat->setCString( 4556 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3); 4557 int32_t numChannels, sampleRate, bitRate; 4558 inputFormat->findInt32(kKeyChannelCount, &numChannels); 4559 inputFormat->findInt32(kKeySampleRate, &sampleRate); 4560 inputFormat->findInt32(kKeyBitRate, &bitRate); 4561 mOutputFormat->setInt32(kKeyChannelCount, numChannels); 4562 mOutputFormat->setInt32(kKeySampleRate, sampleRate); 4563 mOutputFormat->setInt32(kKeyBitRate, bitRate); 4564 } else { 4565 CHECK(!"Should not be here. Unknown audio encoding."); 4566 } 4567 break; 4568 } 4569 4570 case OMX_PortDomainVideo: 4571 { 4572 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 4573 4574 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) { 4575 mOutputFormat->setCString( 4576 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 4577 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) { 4578 mOutputFormat->setCString( 4579 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); 4580 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) { 4581 mOutputFormat->setCString( 4582 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); 4583 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) { 4584 mOutputFormat->setCString( 4585 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 4586 } else { 4587 CHECK(!"Unknown compression format."); 4588 } 4589 4590 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth); 4591 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight); 4592 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat); 4593 4594 if (!mIsEncoder) { 4595 OMX_CONFIG_RECTTYPE rect; 4596 InitOMXParams(&rect); 4597 rect.nPortIndex = kPortIndexOutput; 4598 status_t err = 4599 mOMX->getConfig( 4600 mNode, OMX_IndexConfigCommonOutputCrop, 4601 &rect, sizeof(rect)); 4602 4603 CODEC_LOGI( 4604 "video dimensions are %ld x %ld", 4605 video_def->nFrameWidth, video_def->nFrameHeight); 4606 4607 if (err == OK) { 4608 CHECK_GE(rect.nLeft, 0); 4609 CHECK_GE(rect.nTop, 0); 4610 CHECK_GE(rect.nWidth, 0u); 4611 CHECK_GE(rect.nHeight, 0u); 4612 CHECK_LE(rect.nLeft + rect.nWidth - 1, video_def->nFrameWidth); 4613 CHECK_LE(rect.nTop + rect.nHeight - 1, video_def->nFrameHeight); 4614 4615 mOutputFormat->setRect( 4616 kKeyCropRect, 4617 rect.nLeft, 4618 rect.nTop, 4619 rect.nLeft + rect.nWidth - 1, 4620 rect.nTop + rect.nHeight - 1); 4621 4622 CODEC_LOGI( 4623 "Crop rect is %ld x %ld @ (%ld, %ld)", 4624 rect.nWidth, rect.nHeight, rect.nLeft, rect.nTop); 4625 } else { 4626 mOutputFormat->setRect( 4627 kKeyCropRect, 4628 0, 0, 4629 video_def->nFrameWidth - 1, 4630 video_def->nFrameHeight - 1); 4631 } 4632 4633 if (mNativeWindow != NULL) { 4634 initNativeWindowCrop(); 4635 } 4636 } 4637 break; 4638 } 4639 4640 default: 4641 { 4642 CHECK(!"should not be here, neither audio nor video."); 4643 break; 4644 } 4645 } 4646 4647 // If the input format contains rotation information, flag the output 4648 // format accordingly. 4649 4650 int32_t rotationDegrees; 4651 if (mSource->getFormat()->findInt32(kKeyRotation, &rotationDegrees)) { 4652 mOutputFormat->setInt32(kKeyRotation, rotationDegrees); 4653 } 4654 } 4655 4656 status_t OMXCodec::pause() { 4657 Mutex::Autolock autoLock(mLock); 4658 4659 mPaused = true; 4660 4661 return OK; 4662 } 4663 4664 //////////////////////////////////////////////////////////////////////////////// 4665 4666 status_t QueryCodecs( 4667 const sp<IOMX> &omx, 4668 const char *mime, bool queryDecoders, bool hwCodecOnly, 4669 Vector<CodecCapabilities> *results) { 4670 Vector<OMXCodec::CodecNameAndQuirks> matchingCodecs; 4671 results->clear(); 4672 4673 OMXCodec::findMatchingCodecs(mime, 4674 !queryDecoders /*createEncoder*/, 4675 NULL /*matchComponentName*/, 4676 hwCodecOnly ? OMXCodec::kHardwareCodecsOnly : 0 /*flags*/, 4677 &matchingCodecs); 4678 4679 for (size_t c = 0; c < matchingCodecs.size(); c++) { 4680 const char *componentName = matchingCodecs.itemAt(c).mName.string(); 4681 4682 results->push(); 4683 CodecCapabilities *caps = &results->editItemAt(results->size() - 1); 4684 4685 status_t err = 4686 QueryCodec(omx, componentName, mime, !queryDecoders, caps); 4687 4688 if (err != OK) { 4689 results->removeAt(results->size() - 1); 4690 } 4691 } 4692 4693 return OK; 4694 } 4695 4696 status_t QueryCodec( 4697 const sp<IOMX> &omx, 4698 const char *componentName, const char *mime, 4699 bool isEncoder, 4700 CodecCapabilities *caps) { 4701 if (strncmp(componentName, "OMX.", 4)) { 4702 // Not an OpenMax component but a software codec. 4703 caps->mFlags = 0; 4704 caps->mComponentName = componentName; 4705 return OK; 4706 } 4707 4708 sp<OMXCodecObserver> observer = new OMXCodecObserver; 4709 IOMX::node_id node; 4710 status_t err = omx->allocateNode(componentName, observer, &node); 4711 4712 if (err != OK) { 4713 return err; 4714 } 4715 4716 OMXCodec::setComponentRole(omx, node, isEncoder, mime); 4717 4718 caps->mFlags = 0; 4719 caps->mComponentName = componentName; 4720 4721 OMX_VIDEO_PARAM_PROFILELEVELTYPE param; 4722 InitOMXParams(¶m); 4723 4724 param.nPortIndex = !isEncoder ? 0 : 1; 4725 4726 for (param.nProfileIndex = 0;; ++param.nProfileIndex) { 4727 err = omx->getParameter( 4728 node, OMX_IndexParamVideoProfileLevelQuerySupported, 4729 ¶m, sizeof(param)); 4730 4731 if (err != OK) { 4732 break; 4733 } 4734 4735 CodecProfileLevel profileLevel; 4736 profileLevel.mProfile = param.eProfile; 4737 profileLevel.mLevel = param.eLevel; 4738 4739 caps->mProfileLevels.push(profileLevel); 4740 } 4741 4742 // Color format query 4743 // return colors in the order reported by the OMX component 4744 // prefix "flexible" standard ones with the flexible equivalent 4745 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat; 4746 InitOMXParams(&portFormat); 4747 portFormat.nPortIndex = !isEncoder ? 1 : 0; 4748 for (portFormat.nIndex = 0;; ++portFormat.nIndex) { 4749 err = omx->getParameter( 4750 node, OMX_IndexParamVideoPortFormat, 4751 &portFormat, sizeof(portFormat)); 4752 if (err != OK) { 4753 break; 4754 } 4755 4756 OMX_U32 flexibleEquivalent; 4757 if (ACodec::isFlexibleColorFormat( 4758 omx, node, portFormat.eColorFormat, &flexibleEquivalent)) { 4759 bool marked = false; 4760 for (size_t i = 0; i < caps->mColorFormats.size(); i++) { 4761 if (caps->mColorFormats.itemAt(i) == flexibleEquivalent) { 4762 marked = true; 4763 break; 4764 } 4765 } 4766 if (!marked) { 4767 caps->mColorFormats.push(flexibleEquivalent); 4768 } 4769 } 4770 caps->mColorFormats.push(portFormat.eColorFormat); 4771 } 4772 4773 if (!isEncoder && !strncmp(mime, "video/", 6)) { 4774 if (omx->storeMetaDataInBuffers( 4775 node, 1 /* port index */, OMX_TRUE) == OK || 4776 omx->prepareForAdaptivePlayback( 4777 node, 1 /* port index */, OMX_TRUE, 4778 1280 /* width */, 720 /* height */) == OK) { 4779 caps->mFlags |= CodecCapabilities::kFlagSupportsAdaptivePlayback; 4780 } 4781 } 4782 4783 CHECK_EQ(omx->freeNode(node), (status_t)OK); 4784 4785 return OK; 4786 } 4787 4788 status_t QueryCodecs( 4789 const sp<IOMX> &omx, 4790 const char *mimeType, bool queryDecoders, 4791 Vector<CodecCapabilities> *results) { 4792 return QueryCodecs(omx, mimeType, queryDecoders, false /*hwCodecOnly*/, results); 4793 } 4794 4795 // These are supposed be equivalent to the logic in 4796 // "audio_channel_out_mask_from_count". 4797 status_t getOMXChannelMapping(size_t numChannels, OMX_AUDIO_CHANNELTYPE map[]) { 4798 switch (numChannels) { 4799 case 1: 4800 map[0] = OMX_AUDIO_ChannelCF; 4801 break; 4802 case 2: 4803 map[0] = OMX_AUDIO_ChannelLF; 4804 map[1] = OMX_AUDIO_ChannelRF; 4805 break; 4806 case 3: 4807 map[0] = OMX_AUDIO_ChannelLF; 4808 map[1] = OMX_AUDIO_ChannelRF; 4809 map[2] = OMX_AUDIO_ChannelCF; 4810 break; 4811 case 4: 4812 map[0] = OMX_AUDIO_ChannelLF; 4813 map[1] = OMX_AUDIO_ChannelRF; 4814 map[2] = OMX_AUDIO_ChannelLR; 4815 map[3] = OMX_AUDIO_ChannelRR; 4816 break; 4817 case 5: 4818 map[0] = OMX_AUDIO_ChannelLF; 4819 map[1] = OMX_AUDIO_ChannelRF; 4820 map[2] = OMX_AUDIO_ChannelCF; 4821 map[3] = OMX_AUDIO_ChannelLR; 4822 map[4] = OMX_AUDIO_ChannelRR; 4823 break; 4824 case 6: 4825 map[0] = OMX_AUDIO_ChannelLF; 4826 map[1] = OMX_AUDIO_ChannelRF; 4827 map[2] = OMX_AUDIO_ChannelCF; 4828 map[3] = OMX_AUDIO_ChannelLFE; 4829 map[4] = OMX_AUDIO_ChannelLR; 4830 map[5] = OMX_AUDIO_ChannelRR; 4831 break; 4832 case 7: 4833 map[0] = OMX_AUDIO_ChannelLF; 4834 map[1] = OMX_AUDIO_ChannelRF; 4835 map[2] = OMX_AUDIO_ChannelCF; 4836 map[3] = OMX_AUDIO_ChannelLFE; 4837 map[4] = OMX_AUDIO_ChannelLR; 4838 map[5] = OMX_AUDIO_ChannelRR; 4839 map[6] = OMX_AUDIO_ChannelCS; 4840 break; 4841 case 8: 4842 map[0] = OMX_AUDIO_ChannelLF; 4843 map[1] = OMX_AUDIO_ChannelRF; 4844 map[2] = OMX_AUDIO_ChannelCF; 4845 map[3] = OMX_AUDIO_ChannelLFE; 4846 map[4] = OMX_AUDIO_ChannelLR; 4847 map[5] = OMX_AUDIO_ChannelRR; 4848 map[6] = OMX_AUDIO_ChannelLS; 4849 map[7] = OMX_AUDIO_ChannelRS; 4850 break; 4851 default: 4852 return -EINVAL; 4853 } 4854 4855 return OK; 4856 } 4857 4858 } // namespace android 4859