OMXCodec.cpp revision ddcc4a66d848deef6fb4689e64e30cd9bd2684fe
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//#define LOG_NDEBUG 0 18#define LOG_TAG "OMXCodec" 19#include <utils/Log.h> 20 21#include "include/AACDecoder.h" 22#include "include/AACEncoder.h" 23#include "include/AMRNBDecoder.h" 24#include "include/AMRNBEncoder.h" 25#include "include/AMRWBDecoder.h" 26#include "include/AMRWBEncoder.h" 27#include "include/AVCDecoder.h" 28#include "include/M4vH263Decoder.h" 29#include "include/MP3Decoder.h" 30#include "include/VorbisDecoder.h" 31#include "include/VPXDecoder.h" 32 33#include "include/ESDS.h" 34 35#include <binder/IServiceManager.h> 36#include <binder/MemoryDealer.h> 37#include <binder/ProcessState.h> 38#include <media/IMediaPlayerService.h> 39#include <media/stagefright/MediaBuffer.h> 40#include <media/stagefright/MediaBufferGroup.h> 41#include <media/stagefright/MediaDebug.h> 42#include <media/stagefright/MediaDefs.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 <utils/Vector.h> 48 49#include <OMX_Audio.h> 50#include <OMX_Component.h> 51 52namespace android { 53 54static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00; 55 56struct CodecInfo { 57 const char *mime; 58 const char *codec; 59}; 60 61#define FACTORY_CREATE(name) \ 62static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \ 63 return new name(source); \ 64} 65 66#define FACTORY_CREATE_ENCODER(name) \ 67static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \ 68 return new name(source, meta); \ 69} 70 71#define FACTORY_REF(name) { #name, Make##name }, 72 73FACTORY_CREATE(MP3Decoder) 74FACTORY_CREATE(AMRNBDecoder) 75FACTORY_CREATE(AMRWBDecoder) 76FACTORY_CREATE(AACDecoder) 77FACTORY_CREATE(AVCDecoder) 78FACTORY_CREATE(M4vH263Decoder) 79FACTORY_CREATE(VorbisDecoder) 80FACTORY_CREATE(VPXDecoder) 81FACTORY_CREATE_ENCODER(AMRNBEncoder) 82FACTORY_CREATE_ENCODER(AMRWBEncoder) 83FACTORY_CREATE_ENCODER(AACEncoder) 84 85static sp<MediaSource> InstantiateSoftwareEncoder( 86 const char *name, const sp<MediaSource> &source, 87 const sp<MetaData> &meta) { 88 struct FactoryInfo { 89 const char *name; 90 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &); 91 }; 92 93 static const FactoryInfo kFactoryInfo[] = { 94 FACTORY_REF(AMRNBEncoder) 95 FACTORY_REF(AMRWBEncoder) 96 FACTORY_REF(AACEncoder) 97 }; 98 for (size_t i = 0; 99 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) { 100 if (!strcmp(name, kFactoryInfo[i].name)) { 101 return (*kFactoryInfo[i].CreateFunc)(source, meta); 102 } 103 } 104 105 return NULL; 106} 107 108static sp<MediaSource> InstantiateSoftwareCodec( 109 const char *name, const sp<MediaSource> &source) { 110 struct FactoryInfo { 111 const char *name; 112 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &); 113 }; 114 115 static const FactoryInfo kFactoryInfo[] = { 116 FACTORY_REF(MP3Decoder) 117 FACTORY_REF(AMRNBDecoder) 118 FACTORY_REF(AMRWBDecoder) 119 FACTORY_REF(AACDecoder) 120 FACTORY_REF(AVCDecoder) 121 FACTORY_REF(M4vH263Decoder) 122 FACTORY_REF(VorbisDecoder) 123 FACTORY_REF(VPXDecoder) 124 }; 125 for (size_t i = 0; 126 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) { 127 if (!strcmp(name, kFactoryInfo[i].name)) { 128 return (*kFactoryInfo[i].CreateFunc)(source); 129 } 130 } 131 132 return NULL; 133} 134 135#undef FACTORY_REF 136#undef FACTORY_CREATE 137 138static const CodecInfo kDecoderInfo[] = { 139 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" }, 140// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" }, 141 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" }, 142// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" }, 143// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" }, 144 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" }, 145// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" }, 146 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" }, 147 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" }, 148// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" }, 149 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" }, 150 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" }, 151// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" }, 152 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" }, 153 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" }, 154 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" }, 155// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" }, 156 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" }, 157 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" }, 158// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" }, 159 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" }, 160 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" }, 161 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" }, 162// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" }, 163 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" }, 164 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" }, 165}; 166 167static const CodecInfo kEncoderInfo[] = { 168 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" }, 169 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" }, 170 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" }, 171 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" }, 172 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" }, 173 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" }, 174 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" }, 175 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" }, 176 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" }, 177 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" }, 178 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" }, 179 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" }, 180 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" }, 181 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" }, 182 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" }, 183 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" }, 184}; 185 186#undef OPTIONAL 187 188#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__) 189#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__) 190#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__) 191 192struct OMXCodecObserver : public BnOMXObserver { 193 OMXCodecObserver() { 194 } 195 196 void setCodec(const sp<OMXCodec> &target) { 197 mTarget = target; 198 } 199 200 // from IOMXObserver 201 virtual void onMessage(const omx_message &msg) { 202 sp<OMXCodec> codec = mTarget.promote(); 203 204 if (codec.get() != NULL) { 205 codec->on_message(msg); 206 } 207 } 208 209protected: 210 virtual ~OMXCodecObserver() {} 211 212private: 213 wp<OMXCodec> mTarget; 214 215 OMXCodecObserver(const OMXCodecObserver &); 216 OMXCodecObserver &operator=(const OMXCodecObserver &); 217}; 218 219static const char *GetCodec(const CodecInfo *info, size_t numInfos, 220 const char *mime, int index) { 221 CHECK(index >= 0); 222 for(size_t i = 0; i < numInfos; ++i) { 223 if (!strcasecmp(mime, info[i].mime)) { 224 if (index == 0) { 225 return info[i].codec; 226 } 227 228 --index; 229 } 230 } 231 232 return NULL; 233} 234 235enum { 236 kAVCProfileBaseline = 0x42, 237 kAVCProfileMain = 0x4d, 238 kAVCProfileExtended = 0x58, 239 kAVCProfileHigh = 0x64, 240 kAVCProfileHigh10 = 0x6e, 241 kAVCProfileHigh422 = 0x7a, 242 kAVCProfileHigh444 = 0xf4, 243 kAVCProfileCAVLC444Intra = 0x2c 244}; 245 246static const char *AVCProfileToString(uint8_t profile) { 247 switch (profile) { 248 case kAVCProfileBaseline: 249 return "Baseline"; 250 case kAVCProfileMain: 251 return "Main"; 252 case kAVCProfileExtended: 253 return "Extended"; 254 case kAVCProfileHigh: 255 return "High"; 256 case kAVCProfileHigh10: 257 return "High 10"; 258 case kAVCProfileHigh422: 259 return "High 422"; 260 case kAVCProfileHigh444: 261 return "High 444"; 262 case kAVCProfileCAVLC444Intra: 263 return "CAVLC 444 Intra"; 264 default: return "Unknown"; 265 } 266} 267 268template<class T> 269static void InitOMXParams(T *params) { 270 params->nSize = sizeof(T); 271 params->nVersion.s.nVersionMajor = 1; 272 params->nVersion.s.nVersionMinor = 0; 273 params->nVersion.s.nRevision = 0; 274 params->nVersion.s.nStep = 0; 275} 276 277static bool IsSoftwareCodec(const char *componentName) { 278 if (!strncmp("OMX.PV.", componentName, 7)) { 279 return true; 280 } 281 282 return false; 283} 284 285// A sort order in which non-OMX components are first, 286// followed by software codecs, i.e. OMX.PV.*, followed 287// by all the others. 288static int CompareSoftwareCodecsFirst( 289 const String8 *elem1, const String8 *elem2) { 290 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4); 291 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4); 292 293 if (isNotOMX1) { 294 if (isNotOMX2) { return 0; } 295 return -1; 296 } 297 if (isNotOMX2) { 298 return 1; 299 } 300 301 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string()); 302 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string()); 303 304 if (isSoftwareCodec1) { 305 if (isSoftwareCodec2) { return 0; } 306 return -1; 307 } 308 309 if (isSoftwareCodec2) { 310 return 1; 311 } 312 313 return 0; 314} 315 316// static 317uint32_t OMXCodec::getComponentQuirks(const char *componentName) { 318 uint32_t quirks = 0; 319 320 if (!strcmp(componentName, "OMX.PV.avcdec")) { 321 quirks |= kWantsNALFragments; 322 } 323 if (!strcmp(componentName, "OMX.TI.MP3.decode")) { 324 quirks |= kNeedsFlushBeforeDisable; 325 quirks |= kDecoderLiesAboutNumberOfChannels; 326 } 327 if (!strcmp(componentName, "OMX.TI.AAC.decode")) { 328 quirks |= kNeedsFlushBeforeDisable; 329 quirks |= kRequiresFlushCompleteEmulation; 330 quirks |= kSupportsMultipleFramesPerInputBuffer; 331 } 332 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) { 333 quirks |= kRequiresLoadedToIdleAfterAllocation; 334 quirks |= kRequiresAllocateBufferOnInputPorts; 335 quirks |= kRequiresAllocateBufferOnOutputPorts; 336 } 337 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) { 338 quirks |= kRequiresAllocateBufferOnOutputPorts; 339 quirks |= kDefersOutputBufferAllocation; 340 } 341 342 if (!strncmp(componentName, "OMX.TI.", 7)) { 343 // Apparently I must not use OMX_UseBuffer on either input or 344 // output ports on any of the TI components or quote: 345 // "(I) may have unexpected problem (sic) which can be timing related 346 // and hard to reproduce." 347 348 quirks |= kRequiresAllocateBufferOnInputPorts; 349 quirks |= kRequiresAllocateBufferOnOutputPorts; 350 if (!strncmp(componentName, "OMX.TI.video.encoder", 20)) { 351 quirks |= kAvoidMemcopyInputRecordingFrames; 352 } 353 } 354 355 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) { 356 quirks |= kInputBufferSizesAreBogus; 357 } 358 359 return quirks; 360} 361 362// static 363void OMXCodec::findMatchingCodecs( 364 const char *mime, 365 bool createEncoder, const char *matchComponentName, 366 uint32_t flags, 367 Vector<String8> *matchingCodecs) { 368 matchingCodecs->clear(); 369 370 for (int index = 0;; ++index) { 371 const char *componentName; 372 373 if (createEncoder) { 374 componentName = GetCodec( 375 kEncoderInfo, 376 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]), 377 mime, index); 378 } else { 379 componentName = GetCodec( 380 kDecoderInfo, 381 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]), 382 mime, index); 383 } 384 385 if (!componentName) { 386 break; 387 } 388 389 // If a specific codec is requested, skip the non-matching ones. 390 if (matchComponentName && strcmp(componentName, matchComponentName)) { 391 continue; 392 } 393 394 matchingCodecs->push(String8(componentName)); 395 } 396 397 if (flags & kPreferSoftwareCodecs) { 398 matchingCodecs->sort(CompareSoftwareCodecsFirst); 399 } 400} 401 402// static 403sp<MediaSource> OMXCodec::Create( 404 const sp<IOMX> &omx, 405 const sp<MetaData> &meta, bool createEncoder, 406 const sp<MediaSource> &source, 407 const char *matchComponentName, 408 uint32_t flags) { 409 const char *mime; 410 bool success = meta->findCString(kKeyMIMEType, &mime); 411 CHECK(success); 412 413 Vector<String8> matchingCodecs; 414 findMatchingCodecs( 415 mime, createEncoder, matchComponentName, flags, &matchingCodecs); 416 417 if (matchingCodecs.isEmpty()) { 418 return NULL; 419 } 420 421 sp<OMXCodecObserver> observer = new OMXCodecObserver; 422 IOMX::node_id node = 0; 423 424 const char *componentName; 425 for (size_t i = 0; i < matchingCodecs.size(); ++i) { 426 componentName = matchingCodecs[i].string(); 427 428#if BUILD_WITH_FULL_STAGEFRIGHT 429 sp<MediaSource> softwareCodec = createEncoder? 430 InstantiateSoftwareEncoder(componentName, source, meta): 431 InstantiateSoftwareCodec(componentName, source); 432 433 if (softwareCodec != NULL) { 434 LOGV("Successfully allocated software codec '%s'", componentName); 435 436 return softwareCodec; 437 } 438#endif 439 440 LOGV("Attempting to allocate OMX node '%s'", componentName); 441 442 status_t err = omx->allocateNode(componentName, observer, &node); 443 if (err == OK) { 444 LOGV("Successfully allocated OMX node '%s'", componentName); 445 446 sp<OMXCodec> codec = new OMXCodec( 447 omx, node, getComponentQuirks(componentName), 448 createEncoder, mime, componentName, 449 source); 450 451 observer->setCodec(codec); 452 453 err = codec->configureCodec(meta); 454 455 if (err == OK) { 456 return codec; 457 } 458 459 LOGV("Failed to configure codec '%s'", componentName); 460 } 461 } 462 463 return NULL; 464} 465 466status_t OMXCodec::configureCodec(const sp<MetaData> &meta) { 467 uint32_t type; 468 const void *data; 469 size_t size; 470 if (meta->findData(kKeyESDS, &type, &data, &size)) { 471 ESDS esds((const char *)data, size); 472 CHECK_EQ(esds.InitCheck(), OK); 473 474 const void *codec_specific_data; 475 size_t codec_specific_data_size; 476 esds.getCodecSpecificInfo( 477 &codec_specific_data, &codec_specific_data_size); 478 479 addCodecSpecificData( 480 codec_specific_data, codec_specific_data_size); 481 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) { 482 // Parse the AVCDecoderConfigurationRecord 483 484 const uint8_t *ptr = (const uint8_t *)data; 485 486 CHECK(size >= 7); 487 CHECK_EQ(ptr[0], 1); // configurationVersion == 1 488 uint8_t profile = ptr[1]; 489 uint8_t level = ptr[3]; 490 491 // There is decodable content out there that fails the following 492 // assertion, let's be lenient for now... 493 // CHECK((ptr[4] >> 2) == 0x3f); // reserved 494 495 size_t lengthSize = 1 + (ptr[4] & 3); 496 497 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp 498 // violates it... 499 // CHECK((ptr[5] >> 5) == 7); // reserved 500 501 size_t numSeqParameterSets = ptr[5] & 31; 502 503 ptr += 6; 504 size -= 6; 505 506 for (size_t i = 0; i < numSeqParameterSets; ++i) { 507 CHECK(size >= 2); 508 size_t length = U16_AT(ptr); 509 510 ptr += 2; 511 size -= 2; 512 513 CHECK(size >= length); 514 515 addCodecSpecificData(ptr, length); 516 517 ptr += length; 518 size -= length; 519 } 520 521 CHECK(size >= 1); 522 size_t numPictureParameterSets = *ptr; 523 ++ptr; 524 --size; 525 526 for (size_t i = 0; i < numPictureParameterSets; ++i) { 527 CHECK(size >= 2); 528 size_t length = U16_AT(ptr); 529 530 ptr += 2; 531 size -= 2; 532 533 CHECK(size >= length); 534 535 addCodecSpecificData(ptr, length); 536 537 ptr += length; 538 size -= length; 539 } 540 541 LOGV("AVC profile = %d (%s), level = %d", 542 (int)profile, AVCProfileToString(profile), (int)level / 10); 543 544 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder") 545 && (profile != kAVCProfileBaseline || level > 39)) { 546 // This stream exceeds the decoder's capabilities. The decoder 547 // does not handle this gracefully and would clobber the heap 548 // and wreak havoc instead... 549 550 LOGE("Profile and/or level exceed the decoder's capabilities."); 551 return ERROR_UNSUPPORTED; 552 } 553 } 554 555 int32_t bitRate = 0; 556 if (mIsEncoder) { 557 CHECK(meta->findInt32(kKeyBitRate, &bitRate)); 558 } 559 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) { 560 setAMRFormat(false /* isWAMR */, bitRate); 561 } 562 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) { 563 setAMRFormat(true /* isWAMR */, bitRate); 564 } 565 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) { 566 int32_t numChannels, sampleRate; 567 CHECK(meta->findInt32(kKeyChannelCount, &numChannels)); 568 CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); 569 570 setAACFormat(numChannels, sampleRate, bitRate); 571 } 572 573 if (!strncasecmp(mMIME, "video/", 6)) { 574 575 if (mIsEncoder) { 576 setVideoInputFormat(mMIME, meta); 577 } else { 578 int32_t width, height; 579 bool success = meta->findInt32(kKeyWidth, &width); 580 success = success && meta->findInt32(kKeyHeight, &height); 581 CHECK(success); 582 status_t err = setVideoOutputFormat( 583 mMIME, width, height); 584 585 if (err != OK) { 586 return err; 587 } 588 } 589 } 590 591 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG) 592 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) { 593 OMX_COLOR_FORMATTYPE format = 594 OMX_COLOR_Format32bitARGB8888; 595 // OMX_COLOR_FormatYUV420PackedPlanar; 596 // OMX_COLOR_FormatCbYCrY; 597 // OMX_COLOR_FormatYUV411Planar; 598 599 int32_t width, height; 600 bool success = meta->findInt32(kKeyWidth, &width); 601 success = success && meta->findInt32(kKeyHeight, &height); 602 603 int32_t compressedSize; 604 success = success && meta->findInt32( 605 kKeyMaxInputSize, &compressedSize); 606 607 CHECK(success); 608 CHECK(compressedSize > 0); 609 610 setImageOutputFormat(format, width, height); 611 setJPEGInputFormat(width, height, (OMX_U32)compressedSize); 612 } 613 614 int32_t maxInputSize; 615 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) { 616 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize); 617 } 618 619 if (!strcmp(mComponentName, "OMX.TI.AMR.encode") 620 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode") 621 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) { 622 setMinBufferSize(kPortIndexOutput, 8192); // XXX 623 } 624 625 initOutputFormat(meta); 626 627 return OK; 628} 629 630void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) { 631 OMX_PARAM_PORTDEFINITIONTYPE def; 632 InitOMXParams(&def); 633 def.nPortIndex = portIndex; 634 635 status_t err = mOMX->getParameter( 636 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 637 CHECK_EQ(err, OK); 638 639 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus)) 640 || (def.nBufferSize < size)) { 641 def.nBufferSize = size; 642 } 643 644 err = mOMX->setParameter( 645 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 646 CHECK_EQ(err, OK); 647 648 err = mOMX->getParameter( 649 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 650 CHECK_EQ(err, OK); 651 652 // Make sure the setting actually stuck. 653 if (portIndex == kPortIndexInput 654 && (mQuirks & kInputBufferSizesAreBogus)) { 655 CHECK_EQ(def.nBufferSize, size); 656 } else { 657 CHECK(def.nBufferSize >= size); 658 } 659} 660 661status_t OMXCodec::setVideoPortFormatType( 662 OMX_U32 portIndex, 663 OMX_VIDEO_CODINGTYPE compressionFormat, 664 OMX_COLOR_FORMATTYPE colorFormat) { 665 OMX_VIDEO_PARAM_PORTFORMATTYPE format; 666 InitOMXParams(&format); 667 format.nPortIndex = portIndex; 668 format.nIndex = 0; 669 bool found = false; 670 671 OMX_U32 index = 0; 672 for (;;) { 673 format.nIndex = index; 674 status_t err = mOMX->getParameter( 675 mNode, OMX_IndexParamVideoPortFormat, 676 &format, sizeof(format)); 677 678 if (err != OK) { 679 return err; 680 } 681 682 // The following assertion is violated by TI's video decoder. 683 // CHECK_EQ(format.nIndex, index); 684 685#if 1 686 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d", 687 portIndex, 688 index, format.eCompressionFormat, format.eColorFormat); 689#endif 690 691 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) { 692 if (portIndex == kPortIndexInput 693 && colorFormat == format.eColorFormat) { 694 // eCompressionFormat does not seem right. 695 found = true; 696 break; 697 } 698 if (portIndex == kPortIndexOutput 699 && compressionFormat == format.eCompressionFormat) { 700 // eColorFormat does not seem right. 701 found = true; 702 break; 703 } 704 } 705 706 if (format.eCompressionFormat == compressionFormat 707 && format.eColorFormat == colorFormat) { 708 found = true; 709 break; 710 } 711 712 ++index; 713 } 714 715 if (!found) { 716 return UNKNOWN_ERROR; 717 } 718 719 CODEC_LOGV("found a match."); 720 status_t err = mOMX->setParameter( 721 mNode, OMX_IndexParamVideoPortFormat, 722 &format, sizeof(format)); 723 724 return err; 725} 726 727static size_t getFrameSize( 728 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) { 729 switch (colorFormat) { 730 case OMX_COLOR_FormatYCbYCr: 731 case OMX_COLOR_FormatCbYCrY: 732 return width * height * 2; 733 734 case OMX_COLOR_FormatYUV420Planar: 735 case OMX_COLOR_FormatYUV420SemiPlanar: 736 return (width * height * 3) / 2; 737 738 default: 739 CHECK(!"Should not be here. Unsupported color format."); 740 break; 741 } 742} 743 744void OMXCodec::setVideoInputFormat( 745 const char *mime, const sp<MetaData>& meta) { 746 747 int32_t width, height, frameRate, bitRate, stride, sliceHeight; 748 bool success = meta->findInt32(kKeyWidth, &width); 749 success = success && meta->findInt32(kKeyHeight, &height); 750 success = success && meta->findInt32(kKeySampleRate, &frameRate); 751 success = success && meta->findInt32(kKeyBitRate, &bitRate); 752 success = success && meta->findInt32(kKeyStride, &stride); 753 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight); 754 CHECK(success); 755 CHECK(stride != 0); 756 757 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused; 758 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 759 compressionFormat = OMX_VIDEO_CodingAVC; 760 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 761 compressionFormat = OMX_VIDEO_CodingMPEG4; 762 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 763 compressionFormat = OMX_VIDEO_CodingH263; 764 } else { 765 LOGE("Not a supported video mime type: %s", mime); 766 CHECK(!"Should not be here. Not a supported video mime type."); 767 } 768 769 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar; 770 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) { 771 colorFormat = OMX_COLOR_FormatYCbYCr; 772 } 773 774 status_t err; 775 OMX_PARAM_PORTDEFINITIONTYPE def; 776 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 777 778 //////////////////////// Input port ///////////////////////// 779 CHECK_EQ(setVideoPortFormatType( 780 kPortIndexInput, OMX_VIDEO_CodingUnused, 781 colorFormat), OK); 782 783 InitOMXParams(&def); 784 def.nPortIndex = kPortIndexInput; 785 786 err = mOMX->getParameter( 787 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 788 CHECK_EQ(err, OK); 789 790 def.nBufferSize = getFrameSize(colorFormat, 791 stride > 0? stride: -stride, sliceHeight); 792 793 CHECK_EQ(def.eDomain, OMX_PortDomainVideo); 794 795 video_def->nFrameWidth = width; 796 video_def->nFrameHeight = height; 797 video_def->nStride = stride; 798 video_def->nSliceHeight = sliceHeight; 799 video_def->xFramerate = (frameRate << 16); // Q16 format 800 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused; 801 video_def->eColorFormat = colorFormat; 802 803 err = mOMX->setParameter( 804 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 805 CHECK_EQ(err, OK); 806 807 //////////////////////// Output port ///////////////////////// 808 CHECK_EQ(setVideoPortFormatType( 809 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused), 810 OK); 811 InitOMXParams(&def); 812 def.nPortIndex = kPortIndexOutput; 813 814 err = mOMX->getParameter( 815 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 816 817 CHECK_EQ(err, OK); 818 CHECK_EQ(def.eDomain, OMX_PortDomainVideo); 819 820 video_def->nFrameWidth = width; 821 video_def->nFrameHeight = height; 822 video_def->xFramerate = (frameRate << 16); // Q16 format 823 video_def->nBitrate = bitRate; // Q16 format 824 video_def->eCompressionFormat = compressionFormat; 825 video_def->eColorFormat = OMX_COLOR_FormatUnused; 826 827 err = mOMX->setParameter( 828 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 829 CHECK_EQ(err, OK); 830 831 /////////////////// Codec-specific //////////////////////// 832 switch (compressionFormat) { 833 case OMX_VIDEO_CodingMPEG4: 834 { 835 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK); 836 break; 837 } 838 839 case OMX_VIDEO_CodingH263: 840 break; 841 842 case OMX_VIDEO_CodingAVC: 843 { 844 CHECK_EQ(setupAVCEncoderParameters(meta), OK); 845 break; 846 } 847 848 default: 849 CHECK(!"Support for this compressionFormat to be implemented."); 850 break; 851 } 852} 853 854static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) { 855 if (iFramesInterval < 0) { 856 return 0xFFFFFFFF; 857 } else if (iFramesInterval == 0) { 858 return 0; 859 } 860 OMX_U32 ret = frameRate * iFramesInterval; 861 CHECK(ret > 1); 862 return ret; 863} 864 865status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) { 866 int32_t iFramesInterval, frameRate, bitRate; 867 bool success = meta->findInt32(kKeyBitRate, &bitRate); 868 success = success && meta->findInt32(kKeySampleRate, &frameRate); 869 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); 870 CHECK(success); 871 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type; 872 InitOMXParams(&mpeg4type); 873 mpeg4type.nPortIndex = kPortIndexOutput; 874 875 status_t err = mOMX->getParameter( 876 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type)); 877 CHECK_EQ(err, OK); 878 879 mpeg4type.nSliceHeaderSpacing = 0; 880 mpeg4type.bSVH = OMX_FALSE; 881 mpeg4type.bGov = OMX_FALSE; 882 883 mpeg4type.nAllowedPictureTypes = 884 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; 885 886 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); 887 if (mpeg4type.nPFrames == 0) { 888 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; 889 } 890 mpeg4type.nBFrames = 0; 891 mpeg4type.nIDCVLCThreshold = 0; 892 mpeg4type.bACPred = OMX_TRUE; 893 mpeg4type.nMaxPacketSize = 256; 894 mpeg4type.nTimeIncRes = 1000; 895 mpeg4type.nHeaderExtension = 0; 896 mpeg4type.bReversibleVLC = OMX_FALSE; 897 898 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileCore; 899 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2; 900 901 err = mOMX->setParameter( 902 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type)); 903 CHECK_EQ(err, OK); 904 905 // ---------------- 906 907 OMX_VIDEO_PARAM_BITRATETYPE bitrateType; 908 InitOMXParams(&bitrateType); 909 bitrateType.nPortIndex = kPortIndexOutput; 910 911 err = mOMX->getParameter( 912 mNode, OMX_IndexParamVideoBitrate, 913 &bitrateType, sizeof(bitrateType)); 914 CHECK_EQ(err, OK); 915 916 bitrateType.eControlRate = OMX_Video_ControlRateVariable; 917 bitrateType.nTargetBitrate = bitRate; 918 919 err = mOMX->setParameter( 920 mNode, OMX_IndexParamVideoBitrate, 921 &bitrateType, sizeof(bitrateType)); 922 CHECK_EQ(err, OK); 923 924 // ---------------- 925 926 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType; 927 InitOMXParams(&errorCorrectionType); 928 errorCorrectionType.nPortIndex = kPortIndexOutput; 929 930 err = mOMX->getParameter( 931 mNode, OMX_IndexParamVideoErrorCorrection, 932 &errorCorrectionType, sizeof(errorCorrectionType)); 933 CHECK_EQ(err, OK); 934 935 errorCorrectionType.bEnableHEC = OMX_FALSE; 936 errorCorrectionType.bEnableResync = OMX_TRUE; 937 errorCorrectionType.nResynchMarkerSpacing = 256; 938 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE; 939 errorCorrectionType.bEnableRVLC = OMX_FALSE; 940 941 err = mOMX->setParameter( 942 mNode, OMX_IndexParamVideoErrorCorrection, 943 &errorCorrectionType, sizeof(errorCorrectionType)); 944 CHECK_EQ(err, OK); 945 946 return OK; 947} 948 949status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) { 950 int32_t iFramesInterval, frameRate, bitRate; 951 bool success = meta->findInt32(kKeyBitRate, &bitRate); 952 success = success && meta->findInt32(kKeySampleRate, &frameRate); 953 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); 954 CHECK(success); 955 956 OMX_VIDEO_PARAM_AVCTYPE h264type; 957 InitOMXParams(&h264type); 958 h264type.nPortIndex = kPortIndexOutput; 959 960 status_t err = mOMX->getParameter( 961 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type)); 962 CHECK_EQ(err, OK); 963 964 h264type.nAllowedPictureTypes = 965 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; 966 967 h264type.nSliceHeaderSpacing = 0; 968 h264type.nBFrames = 0; // No B frames support yet 969 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); 970 if (h264type.nPFrames == 0) { 971 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; 972 } 973 h264type.bUseHadamard = OMX_TRUE; 974 h264type.nRefFrames = 1; 975 h264type.nRefIdx10ActiveMinus1 = 0; 976 h264type.nRefIdx11ActiveMinus1 = 0; 977 h264type.bEnableUEP = OMX_FALSE; 978 h264type.bEnableFMO = OMX_FALSE; 979 h264type.bEnableASO = OMX_FALSE; 980 h264type.bEnableRS = OMX_FALSE; 981 h264type.bFrameMBsOnly = OMX_TRUE; 982 h264type.bMBAFF = OMX_FALSE; 983 h264type.bEntropyCodingCABAC = OMX_FALSE; 984 h264type.bWeightedPPrediction = OMX_FALSE; 985 h264type.bconstIpred = OMX_FALSE; 986 h264type.bDirect8x8Inference = OMX_FALSE; 987 h264type.bDirectSpatialTemporal = OMX_FALSE; 988 h264type.nCabacInitIdc = 0; 989 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable; 990 991 err = mOMX->setParameter( 992 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type)); 993 CHECK_EQ(err, OK); 994 995 OMX_VIDEO_PARAM_BITRATETYPE bitrateType; 996 InitOMXParams(&bitrateType); 997 bitrateType.nPortIndex = kPortIndexOutput; 998 999 err = mOMX->getParameter( 1000 mNode, OMX_IndexParamVideoBitrate, 1001 &bitrateType, sizeof(bitrateType)); 1002 CHECK_EQ(err, OK); 1003 1004 bitrateType.eControlRate = OMX_Video_ControlRateVariable; 1005 bitrateType.nTargetBitrate = bitRate; 1006 1007 err = mOMX->setParameter( 1008 mNode, OMX_IndexParamVideoBitrate, 1009 &bitrateType, sizeof(bitrateType)); 1010 CHECK_EQ(err, OK); 1011 1012 return OK; 1013} 1014 1015status_t OMXCodec::setVideoOutputFormat( 1016 const char *mime, OMX_U32 width, OMX_U32 height) { 1017 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height); 1018 1019 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused; 1020 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 1021 compressionFormat = OMX_VIDEO_CodingAVC; 1022 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 1023 compressionFormat = OMX_VIDEO_CodingMPEG4; 1024 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 1025 compressionFormat = OMX_VIDEO_CodingH263; 1026 } else { 1027 LOGE("Not a supported video mime type: %s", mime); 1028 CHECK(!"Should not be here. Not a supported video mime type."); 1029 } 1030 1031 status_t err = setVideoPortFormatType( 1032 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused); 1033 1034 if (err != OK) { 1035 return err; 1036 } 1037 1038#if 1 1039 { 1040 OMX_VIDEO_PARAM_PORTFORMATTYPE format; 1041 InitOMXParams(&format); 1042 format.nPortIndex = kPortIndexOutput; 1043 format.nIndex = 0; 1044 1045 status_t err = mOMX->getParameter( 1046 mNode, OMX_IndexParamVideoPortFormat, 1047 &format, sizeof(format)); 1048 CHECK_EQ(err, OK); 1049 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused); 1050 1051 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00; 1052 1053 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar 1054 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar 1055 || format.eColorFormat == OMX_COLOR_FormatCbYCrY 1056 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar); 1057 1058 err = mOMX->setParameter( 1059 mNode, OMX_IndexParamVideoPortFormat, 1060 &format, sizeof(format)); 1061 1062 if (err != OK) { 1063 return err; 1064 } 1065 } 1066#endif 1067 1068 OMX_PARAM_PORTDEFINITIONTYPE def; 1069 InitOMXParams(&def); 1070 def.nPortIndex = kPortIndexInput; 1071 1072 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 1073 1074 err = mOMX->getParameter( 1075 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1076 1077 CHECK_EQ(err, OK); 1078 1079#if 1 1080 // XXX Need a (much) better heuristic to compute input buffer sizes. 1081 const size_t X = 64 * 1024; 1082 if (def.nBufferSize < X) { 1083 def.nBufferSize = X; 1084 } 1085#endif 1086 1087 CHECK_EQ(def.eDomain, OMX_PortDomainVideo); 1088 1089 video_def->nFrameWidth = width; 1090 video_def->nFrameHeight = height; 1091 1092 video_def->eCompressionFormat = compressionFormat; 1093 video_def->eColorFormat = OMX_COLOR_FormatUnused; 1094 1095 err = mOMX->setParameter( 1096 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1097 1098 if (err != OK) { 1099 return err; 1100 } 1101 1102 //////////////////////////////////////////////////////////////////////////// 1103 1104 InitOMXParams(&def); 1105 def.nPortIndex = kPortIndexOutput; 1106 1107 err = mOMX->getParameter( 1108 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1109 CHECK_EQ(err, OK); 1110 CHECK_EQ(def.eDomain, OMX_PortDomainVideo); 1111 1112#if 0 1113 def.nBufferSize = 1114 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420 1115#endif 1116 1117 video_def->nFrameWidth = width; 1118 video_def->nFrameHeight = height; 1119 1120 err = mOMX->setParameter( 1121 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1122 1123 return err; 1124} 1125 1126OMXCodec::OMXCodec( 1127 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks, 1128 bool isEncoder, 1129 const char *mime, 1130 const char *componentName, 1131 const sp<MediaSource> &source) 1132 : mOMX(omx), 1133 mOMXLivesLocally(omx->livesLocally(getpid())), 1134 mNode(node), 1135 mQuirks(quirks), 1136 mIsEncoder(isEncoder), 1137 mMIME(strdup(mime)), 1138 mComponentName(strdup(componentName)), 1139 mSource(source), 1140 mCodecSpecificDataIndex(0), 1141 mState(LOADED), 1142 mInitialBufferSubmit(true), 1143 mSignalledEOS(false), 1144 mNoMoreOutputData(false), 1145 mOutputPortSettingsHaveChanged(false), 1146 mSeekTimeUs(-1), 1147 mLeftOverBuffer(NULL) { 1148 mPortStatus[kPortIndexInput] = ENABLED; 1149 mPortStatus[kPortIndexOutput] = ENABLED; 1150 1151 setComponentRole(); 1152} 1153 1154// static 1155void OMXCodec::setComponentRole( 1156 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder, 1157 const char *mime) { 1158 struct MimeToRole { 1159 const char *mime; 1160 const char *decoderRole; 1161 const char *encoderRole; 1162 }; 1163 1164 static const MimeToRole kMimeToRole[] = { 1165 { MEDIA_MIMETYPE_AUDIO_MPEG, 1166 "audio_decoder.mp3", "audio_encoder.mp3" }, 1167 { MEDIA_MIMETYPE_AUDIO_AMR_NB, 1168 "audio_decoder.amrnb", "audio_encoder.amrnb" }, 1169 { MEDIA_MIMETYPE_AUDIO_AMR_WB, 1170 "audio_decoder.amrwb", "audio_encoder.amrwb" }, 1171 { MEDIA_MIMETYPE_AUDIO_AAC, 1172 "audio_decoder.aac", "audio_encoder.aac" }, 1173 { MEDIA_MIMETYPE_VIDEO_AVC, 1174 "video_decoder.avc", "video_encoder.avc" }, 1175 { MEDIA_MIMETYPE_VIDEO_MPEG4, 1176 "video_decoder.mpeg4", "video_encoder.mpeg4" }, 1177 { MEDIA_MIMETYPE_VIDEO_H263, 1178 "video_decoder.h263", "video_encoder.h263" }, 1179 }; 1180 1181 static const size_t kNumMimeToRole = 1182 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]); 1183 1184 size_t i; 1185 for (i = 0; i < kNumMimeToRole; ++i) { 1186 if (!strcasecmp(mime, kMimeToRole[i].mime)) { 1187 break; 1188 } 1189 } 1190 1191 if (i == kNumMimeToRole) { 1192 return; 1193 } 1194 1195 const char *role = 1196 isEncoder ? kMimeToRole[i].encoderRole 1197 : kMimeToRole[i].decoderRole; 1198 1199 if (role != NULL) { 1200 OMX_PARAM_COMPONENTROLETYPE roleParams; 1201 InitOMXParams(&roleParams); 1202 1203 strncpy((char *)roleParams.cRole, 1204 role, OMX_MAX_STRINGNAME_SIZE - 1); 1205 1206 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0'; 1207 1208 status_t err = omx->setParameter( 1209 node, OMX_IndexParamStandardComponentRole, 1210 &roleParams, sizeof(roleParams)); 1211 1212 if (err != OK) { 1213 LOGW("Failed to set standard component role '%s'.", role); 1214 } 1215 } 1216} 1217 1218void OMXCodec::setComponentRole() { 1219 setComponentRole(mOMX, mNode, mIsEncoder, mMIME); 1220} 1221 1222OMXCodec::~OMXCodec() { 1223 CHECK(mState == LOADED || mState == ERROR); 1224 1225 status_t err = mOMX->freeNode(mNode); 1226 CHECK_EQ(err, OK); 1227 1228 mNode = NULL; 1229 setState(DEAD); 1230 1231 clearCodecSpecificData(); 1232 1233 free(mComponentName); 1234 mComponentName = NULL; 1235 1236 free(mMIME); 1237 mMIME = NULL; 1238} 1239 1240status_t OMXCodec::init() { 1241 // mLock is held. 1242 1243 CHECK_EQ(mState, LOADED); 1244 1245 status_t err; 1246 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) { 1247 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 1248 CHECK_EQ(err, OK); 1249 setState(LOADED_TO_IDLE); 1250 } 1251 1252 err = allocateBuffers(); 1253 CHECK_EQ(err, OK); 1254 1255 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) { 1256 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 1257 CHECK_EQ(err, OK); 1258 1259 setState(LOADED_TO_IDLE); 1260 } 1261 1262 while (mState != EXECUTING && mState != ERROR) { 1263 mAsyncCompletion.wait(mLock); 1264 } 1265 1266 return mState == ERROR ? UNKNOWN_ERROR : OK; 1267} 1268 1269// static 1270bool OMXCodec::isIntermediateState(State state) { 1271 return state == LOADED_TO_IDLE 1272 || state == IDLE_TO_EXECUTING 1273 || state == EXECUTING_TO_IDLE 1274 || state == IDLE_TO_LOADED 1275 || state == RECONFIGURING; 1276} 1277 1278status_t OMXCodec::allocateBuffers() { 1279 status_t err = allocateBuffersOnPort(kPortIndexInput); 1280 1281 if (err != OK) { 1282 return err; 1283 } 1284 1285 return allocateBuffersOnPort(kPortIndexOutput); 1286} 1287 1288status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) { 1289 OMX_PARAM_PORTDEFINITIONTYPE def; 1290 InitOMXParams(&def); 1291 def.nPortIndex = portIndex; 1292 1293 status_t err = mOMX->getParameter( 1294 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1295 1296 if (err != OK) { 1297 return err; 1298 } 1299 1300 size_t totalSize = def.nBufferCountActual * def.nBufferSize; 1301 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec"); 1302 1303 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) { 1304 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize); 1305 CHECK(mem.get() != NULL); 1306 1307 BufferInfo info; 1308 info.mData = NULL; 1309 info.mSize = def.nBufferSize; 1310 1311 IOMX::buffer_id buffer; 1312 if (portIndex == kPortIndexInput 1313 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) { 1314 if (mOMXLivesLocally) { 1315 mem.clear(); 1316 1317 err = mOMX->allocateBuffer( 1318 mNode, portIndex, def.nBufferSize, &buffer, 1319 &info.mData); 1320 } else { 1321 err = mOMX->allocateBufferWithBackup( 1322 mNode, portIndex, mem, &buffer); 1323 } 1324 } else if (portIndex == kPortIndexOutput 1325 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) { 1326 if (mOMXLivesLocally) { 1327 mem.clear(); 1328 1329 err = mOMX->allocateBuffer( 1330 mNode, portIndex, def.nBufferSize, &buffer, 1331 &info.mData); 1332 } else { 1333 err = mOMX->allocateBufferWithBackup( 1334 mNode, portIndex, mem, &buffer); 1335 } 1336 } else { 1337 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer); 1338 } 1339 1340 if (err != OK) { 1341 LOGE("allocate_buffer_with_backup failed"); 1342 return err; 1343 } 1344 1345 if (mem != NULL) { 1346 info.mData = mem->pointer(); 1347 } 1348 1349 info.mBuffer = buffer; 1350 info.mOwnedByComponent = false; 1351 info.mMem = mem; 1352 info.mMediaBuffer = NULL; 1353 1354 if (portIndex == kPortIndexOutput) { 1355 if (!(mOMXLivesLocally 1356 && (mQuirks & kRequiresAllocateBufferOnOutputPorts) 1357 && (mQuirks & kDefersOutputBufferAllocation))) { 1358 // If the node does not fill in the buffer ptr at this time, 1359 // we will defer creating the MediaBuffer until receiving 1360 // the first FILL_BUFFER_DONE notification instead. 1361 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize); 1362 info.mMediaBuffer->setObserver(this); 1363 } 1364 } 1365 1366 mPortBuffers[portIndex].push(info); 1367 1368 CODEC_LOGV("allocated buffer %p on %s port", buffer, 1369 portIndex == kPortIndexInput ? "input" : "output"); 1370 } 1371 1372 // dumpPortStatus(portIndex); 1373 1374 return OK; 1375} 1376 1377void OMXCodec::on_message(const omx_message &msg) { 1378 Mutex::Autolock autoLock(mLock); 1379 1380 switch (msg.type) { 1381 case omx_message::EVENT: 1382 { 1383 onEvent( 1384 msg.u.event_data.event, msg.u.event_data.data1, 1385 msg.u.event_data.data2); 1386 1387 break; 1388 } 1389 1390 case omx_message::EMPTY_BUFFER_DONE: 1391 { 1392 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer; 1393 1394 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer); 1395 1396 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 1397 size_t i = 0; 1398 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) { 1399 ++i; 1400 } 1401 1402 CHECK(i < buffers->size()); 1403 if (!(*buffers)[i].mOwnedByComponent) { 1404 LOGW("We already own input buffer %p, yet received " 1405 "an EMPTY_BUFFER_DONE.", buffer); 1406 } 1407 1408 buffers->editItemAt(i).mOwnedByComponent = false; 1409 1410 if (mPortStatus[kPortIndexInput] == DISABLING) { 1411 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer); 1412 1413 status_t err = 1414 mOMX->freeBuffer(mNode, kPortIndexInput, buffer); 1415 CHECK_EQ(err, OK); 1416 1417 buffers->removeAt(i); 1418 } else if (mState != ERROR 1419 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) { 1420 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED); 1421 drainInputBuffer(&buffers->editItemAt(i)); 1422 } 1423 break; 1424 } 1425 1426 case omx_message::FILL_BUFFER_DONE: 1427 { 1428 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer; 1429 OMX_U32 flags = msg.u.extended_buffer_data.flags; 1430 1431 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))", 1432 buffer, 1433 msg.u.extended_buffer_data.range_length, 1434 flags, 1435 msg.u.extended_buffer_data.timestamp, 1436 msg.u.extended_buffer_data.timestamp / 1E6); 1437 1438 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 1439 size_t i = 0; 1440 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) { 1441 ++i; 1442 } 1443 1444 CHECK(i < buffers->size()); 1445 BufferInfo *info = &buffers->editItemAt(i); 1446 1447 if (!info->mOwnedByComponent) { 1448 LOGW("We already own output buffer %p, yet received " 1449 "a FILL_BUFFER_DONE.", buffer); 1450 } 1451 1452 info->mOwnedByComponent = false; 1453 1454 if (mPortStatus[kPortIndexOutput] == DISABLING) { 1455 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer); 1456 1457 status_t err = 1458 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer); 1459 CHECK_EQ(err, OK); 1460 1461 buffers->removeAt(i); 1462#if 0 1463 } else if (mPortStatus[kPortIndexOutput] == ENABLED 1464 && (flags & OMX_BUFFERFLAG_EOS)) { 1465 CODEC_LOGV("No more output data."); 1466 mNoMoreOutputData = true; 1467 mBufferFilled.signal(); 1468#endif 1469 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) { 1470 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED); 1471 1472 if (info->mMediaBuffer == NULL) { 1473 CHECK(mOMXLivesLocally); 1474 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts); 1475 CHECK(mQuirks & kDefersOutputBufferAllocation); 1476 1477 // The qcom video decoders on Nexus don't actually allocate 1478 // output buffer memory on a call to OMX_AllocateBuffer 1479 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE 1480 // structure is only filled in later. 1481 1482 info->mMediaBuffer = new MediaBuffer( 1483 msg.u.extended_buffer_data.data_ptr, 1484 info->mSize); 1485 info->mMediaBuffer->setObserver(this); 1486 } 1487 1488 MediaBuffer *buffer = info->mMediaBuffer; 1489 1490 buffer->set_range( 1491 msg.u.extended_buffer_data.range_offset, 1492 msg.u.extended_buffer_data.range_length); 1493 1494 buffer->meta_data()->clear(); 1495 1496 buffer->meta_data()->setInt64( 1497 kKeyTime, msg.u.extended_buffer_data.timestamp); 1498 1499 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) { 1500 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true); 1501 } 1502 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) { 1503 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true); 1504 } 1505 1506 buffer->meta_data()->setPointer( 1507 kKeyPlatformPrivate, 1508 msg.u.extended_buffer_data.platform_private); 1509 1510 buffer->meta_data()->setPointer( 1511 kKeyBufferID, 1512 msg.u.extended_buffer_data.buffer); 1513 1514 mFilledBuffers.push_back(i); 1515 mBufferFilled.signal(); 1516 1517 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) { 1518 CODEC_LOGV("No more output data."); 1519 mNoMoreOutputData = true; 1520 } 1521 } 1522 1523 break; 1524 } 1525 1526 default: 1527 { 1528 CHECK(!"should not be here."); 1529 break; 1530 } 1531 } 1532} 1533 1534void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) { 1535 switch (event) { 1536 case OMX_EventCmdComplete: 1537 { 1538 onCmdComplete((OMX_COMMANDTYPE)data1, data2); 1539 break; 1540 } 1541 1542 case OMX_EventError: 1543 { 1544 LOGE("ERROR(0x%08lx, %ld)", data1, data2); 1545 1546 setState(ERROR); 1547 break; 1548 } 1549 1550 case OMX_EventPortSettingsChanged: 1551 { 1552 onPortSettingsChanged(data1); 1553 break; 1554 } 1555 1556#if 0 1557 case OMX_EventBufferFlag: 1558 { 1559 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1); 1560 1561 if (data1 == kPortIndexOutput) { 1562 mNoMoreOutputData = true; 1563 } 1564 break; 1565 } 1566#endif 1567 1568 default: 1569 { 1570 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2); 1571 break; 1572 } 1573 } 1574} 1575 1576// Has the format changed in any way that the client would have to be aware of? 1577static bool formatHasNotablyChanged( 1578 const sp<MetaData> &from, const sp<MetaData> &to) { 1579 if (from.get() == NULL && to.get() == NULL) { 1580 return false; 1581 } 1582 1583 if ((from.get() == NULL && to.get() != NULL) 1584 || (from.get() != NULL && to.get() == NULL)) { 1585 return true; 1586 } 1587 1588 const char *mime_from, *mime_to; 1589 CHECK(from->findCString(kKeyMIMEType, &mime_from)); 1590 CHECK(to->findCString(kKeyMIMEType, &mime_to)); 1591 1592 if (strcasecmp(mime_from, mime_to)) { 1593 return true; 1594 } 1595 1596 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) { 1597 int32_t colorFormat_from, colorFormat_to; 1598 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from)); 1599 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to)); 1600 1601 if (colorFormat_from != colorFormat_to) { 1602 return true; 1603 } 1604 1605 int32_t width_from, width_to; 1606 CHECK(from->findInt32(kKeyWidth, &width_from)); 1607 CHECK(to->findInt32(kKeyWidth, &width_to)); 1608 1609 if (width_from != width_to) { 1610 return true; 1611 } 1612 1613 int32_t height_from, height_to; 1614 CHECK(from->findInt32(kKeyHeight, &height_from)); 1615 CHECK(to->findInt32(kKeyHeight, &height_to)); 1616 1617 if (height_from != height_to) { 1618 return true; 1619 } 1620 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) { 1621 int32_t numChannels_from, numChannels_to; 1622 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from)); 1623 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to)); 1624 1625 if (numChannels_from != numChannels_to) { 1626 return true; 1627 } 1628 1629 int32_t sampleRate_from, sampleRate_to; 1630 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from)); 1631 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to)); 1632 1633 if (sampleRate_from != sampleRate_to) { 1634 return true; 1635 } 1636 } 1637 1638 return false; 1639} 1640 1641void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) { 1642 switch (cmd) { 1643 case OMX_CommandStateSet: 1644 { 1645 onStateChange((OMX_STATETYPE)data); 1646 break; 1647 } 1648 1649 case OMX_CommandPortDisable: 1650 { 1651 OMX_U32 portIndex = data; 1652 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex); 1653 1654 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1655 CHECK_EQ(mPortStatus[portIndex], DISABLING); 1656 CHECK_EQ(mPortBuffers[portIndex].size(), 0); 1657 1658 mPortStatus[portIndex] = DISABLED; 1659 1660 if (mState == RECONFIGURING) { 1661 CHECK_EQ(portIndex, kPortIndexOutput); 1662 1663 sp<MetaData> oldOutputFormat = mOutputFormat; 1664 initOutputFormat(mSource->getFormat()); 1665 1666 // Don't notify clients if the output port settings change 1667 // wasn't of importance to them, i.e. it may be that just the 1668 // number of buffers has changed and nothing else. 1669 mOutputPortSettingsHaveChanged = 1670 formatHasNotablyChanged(oldOutputFormat, mOutputFormat); 1671 1672 enablePortAsync(portIndex); 1673 1674 status_t err = allocateBuffersOnPort(portIndex); 1675 CHECK_EQ(err, OK); 1676 } 1677 break; 1678 } 1679 1680 case OMX_CommandPortEnable: 1681 { 1682 OMX_U32 portIndex = data; 1683 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex); 1684 1685 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1686 CHECK_EQ(mPortStatus[portIndex], ENABLING); 1687 1688 mPortStatus[portIndex] = ENABLED; 1689 1690 if (mState == RECONFIGURING) { 1691 CHECK_EQ(portIndex, kPortIndexOutput); 1692 1693 setState(EXECUTING); 1694 1695 fillOutputBuffers(); 1696 } 1697 break; 1698 } 1699 1700 case OMX_CommandFlush: 1701 { 1702 OMX_U32 portIndex = data; 1703 1704 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex); 1705 1706 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN); 1707 mPortStatus[portIndex] = ENABLED; 1708 1709 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]), 1710 mPortBuffers[portIndex].size()); 1711 1712 if (mState == RECONFIGURING) { 1713 CHECK_EQ(portIndex, kPortIndexOutput); 1714 1715 disablePortAsync(portIndex); 1716 } else if (mState == EXECUTING_TO_IDLE) { 1717 if (mPortStatus[kPortIndexInput] == ENABLED 1718 && mPortStatus[kPortIndexOutput] == ENABLED) { 1719 CODEC_LOGV("Finished flushing both ports, now completing " 1720 "transition from EXECUTING to IDLE."); 1721 1722 mPortStatus[kPortIndexInput] = SHUTTING_DOWN; 1723 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; 1724 1725 status_t err = 1726 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 1727 CHECK_EQ(err, OK); 1728 } 1729 } else { 1730 // We're flushing both ports in preparation for seeking. 1731 1732 if (mPortStatus[kPortIndexInput] == ENABLED 1733 && mPortStatus[kPortIndexOutput] == ENABLED) { 1734 CODEC_LOGV("Finished flushing both ports, now continuing from" 1735 " seek-time."); 1736 1737 drainInputBuffers(); 1738 fillOutputBuffers(); 1739 } 1740 } 1741 1742 break; 1743 } 1744 1745 default: 1746 { 1747 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data); 1748 break; 1749 } 1750 } 1751} 1752 1753void OMXCodec::onStateChange(OMX_STATETYPE newState) { 1754 CODEC_LOGV("onStateChange %d", newState); 1755 1756 switch (newState) { 1757 case OMX_StateIdle: 1758 { 1759 CODEC_LOGV("Now Idle."); 1760 if (mState == LOADED_TO_IDLE) { 1761 status_t err = mOMX->sendCommand( 1762 mNode, OMX_CommandStateSet, OMX_StateExecuting); 1763 1764 CHECK_EQ(err, OK); 1765 1766 setState(IDLE_TO_EXECUTING); 1767 } else { 1768 CHECK_EQ(mState, EXECUTING_TO_IDLE); 1769 1770 CHECK_EQ( 1771 countBuffersWeOwn(mPortBuffers[kPortIndexInput]), 1772 mPortBuffers[kPortIndexInput].size()); 1773 1774 CHECK_EQ( 1775 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]), 1776 mPortBuffers[kPortIndexOutput].size()); 1777 1778 status_t err = mOMX->sendCommand( 1779 mNode, OMX_CommandStateSet, OMX_StateLoaded); 1780 1781 CHECK_EQ(err, OK); 1782 1783 err = freeBuffersOnPort(kPortIndexInput); 1784 CHECK_EQ(err, OK); 1785 1786 err = freeBuffersOnPort(kPortIndexOutput); 1787 CHECK_EQ(err, OK); 1788 1789 mPortStatus[kPortIndexInput] = ENABLED; 1790 mPortStatus[kPortIndexOutput] = ENABLED; 1791 1792 setState(IDLE_TO_LOADED); 1793 } 1794 break; 1795 } 1796 1797 case OMX_StateExecuting: 1798 { 1799 CHECK_EQ(mState, IDLE_TO_EXECUTING); 1800 1801 CODEC_LOGV("Now Executing."); 1802 1803 setState(EXECUTING); 1804 1805 // Buffers will be submitted to the component in the first 1806 // call to OMXCodec::read as mInitialBufferSubmit is true at 1807 // this point. This ensures that this on_message call returns, 1808 // releases the lock and ::init can notice the state change and 1809 // itself return. 1810 break; 1811 } 1812 1813 case OMX_StateLoaded: 1814 { 1815 CHECK_EQ(mState, IDLE_TO_LOADED); 1816 1817 CODEC_LOGV("Now Loaded."); 1818 1819 setState(LOADED); 1820 break; 1821 } 1822 1823 case OMX_StateInvalid: 1824 { 1825 setState(ERROR); 1826 break; 1827 } 1828 1829 default: 1830 { 1831 CHECK(!"should not be here."); 1832 break; 1833 } 1834 } 1835} 1836 1837// static 1838size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) { 1839 size_t n = 0; 1840 for (size_t i = 0; i < buffers.size(); ++i) { 1841 if (!buffers[i].mOwnedByComponent) { 1842 ++n; 1843 } 1844 } 1845 1846 return n; 1847} 1848 1849status_t OMXCodec::freeBuffersOnPort( 1850 OMX_U32 portIndex, bool onlyThoseWeOwn) { 1851 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; 1852 1853 status_t stickyErr = OK; 1854 1855 for (size_t i = buffers->size(); i-- > 0;) { 1856 BufferInfo *info = &buffers->editItemAt(i); 1857 1858 if (onlyThoseWeOwn && info->mOwnedByComponent) { 1859 continue; 1860 } 1861 1862 CHECK_EQ(info->mOwnedByComponent, false); 1863 1864 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex); 1865 1866 status_t err = 1867 mOMX->freeBuffer(mNode, portIndex, info->mBuffer); 1868 1869 if (err != OK) { 1870 stickyErr = err; 1871 } 1872 1873 if (info->mMediaBuffer != NULL) { 1874 info->mMediaBuffer->setObserver(NULL); 1875 1876 // Make sure nobody but us owns this buffer at this point. 1877 CHECK_EQ(info->mMediaBuffer->refcount(), 0); 1878 1879 info->mMediaBuffer->release(); 1880 } 1881 1882 buffers->removeAt(i); 1883 } 1884 1885 CHECK(onlyThoseWeOwn || buffers->isEmpty()); 1886 1887 return stickyErr; 1888} 1889 1890void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) { 1891 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex); 1892 1893 CHECK_EQ(mState, EXECUTING); 1894 CHECK_EQ(portIndex, kPortIndexOutput); 1895 setState(RECONFIGURING); 1896 1897 if (mQuirks & kNeedsFlushBeforeDisable) { 1898 if (!flushPortAsync(portIndex)) { 1899 onCmdComplete(OMX_CommandFlush, portIndex); 1900 } 1901 } else { 1902 disablePortAsync(portIndex); 1903 } 1904} 1905 1906bool OMXCodec::flushPortAsync(OMX_U32 portIndex) { 1907 CHECK(mState == EXECUTING || mState == RECONFIGURING 1908 || mState == EXECUTING_TO_IDLE); 1909 1910 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.", 1911 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]), 1912 mPortBuffers[portIndex].size()); 1913 1914 CHECK_EQ(mPortStatus[portIndex], ENABLED); 1915 mPortStatus[portIndex] = SHUTTING_DOWN; 1916 1917 if ((mQuirks & kRequiresFlushCompleteEmulation) 1918 && countBuffersWeOwn(mPortBuffers[portIndex]) 1919 == mPortBuffers[portIndex].size()) { 1920 // No flush is necessary and this component fails to send a 1921 // flush-complete event in this case. 1922 1923 return false; 1924 } 1925 1926 status_t err = 1927 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex); 1928 CHECK_EQ(err, OK); 1929 1930 return true; 1931} 1932 1933void OMXCodec::disablePortAsync(OMX_U32 portIndex) { 1934 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1935 1936 CHECK_EQ(mPortStatus[portIndex], ENABLED); 1937 mPortStatus[portIndex] = DISABLING; 1938 1939 status_t err = 1940 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex); 1941 CHECK_EQ(err, OK); 1942 1943 freeBuffersOnPort(portIndex, true); 1944} 1945 1946void OMXCodec::enablePortAsync(OMX_U32 portIndex) { 1947 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1948 1949 CHECK_EQ(mPortStatus[portIndex], DISABLED); 1950 mPortStatus[portIndex] = ENABLING; 1951 1952 status_t err = 1953 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex); 1954 CHECK_EQ(err, OK); 1955} 1956 1957void OMXCodec::fillOutputBuffers() { 1958 CHECK_EQ(mState, EXECUTING); 1959 1960 // This is a workaround for some decoders not properly reporting 1961 // end-of-output-stream. If we own all input buffers and also own 1962 // all output buffers and we already signalled end-of-input-stream, 1963 // the end-of-output-stream is implied. 1964 if (mSignalledEOS 1965 && countBuffersWeOwn(mPortBuffers[kPortIndexInput]) 1966 == mPortBuffers[kPortIndexInput].size() 1967 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput]) 1968 == mPortBuffers[kPortIndexOutput].size()) { 1969 mNoMoreOutputData = true; 1970 mBufferFilled.signal(); 1971 1972 return; 1973 } 1974 1975 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 1976 for (size_t i = 0; i < buffers->size(); ++i) { 1977 fillOutputBuffer(&buffers->editItemAt(i)); 1978 } 1979} 1980 1981void OMXCodec::drainInputBuffers() { 1982 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1983 1984 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 1985 for (size_t i = 0; i < buffers->size(); ++i) { 1986 drainInputBuffer(&buffers->editItemAt(i)); 1987 } 1988} 1989 1990void OMXCodec::drainInputBuffer(BufferInfo *info) { 1991 CHECK_EQ(info->mOwnedByComponent, false); 1992 1993 if (mSignalledEOS) { 1994 return; 1995 } 1996 1997 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) { 1998 const CodecSpecificData *specific = 1999 mCodecSpecificData[mCodecSpecificDataIndex]; 2000 2001 size_t size = specific->mSize; 2002 2003 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME) 2004 && !(mQuirks & kWantsNALFragments)) { 2005 static const uint8_t kNALStartCode[4] = 2006 { 0x00, 0x00, 0x00, 0x01 }; 2007 2008 CHECK(info->mSize >= specific->mSize + 4); 2009 2010 size += 4; 2011 2012 memcpy(info->mData, kNALStartCode, 4); 2013 memcpy((uint8_t *)info->mData + 4, 2014 specific->mData, specific->mSize); 2015 } else { 2016 CHECK(info->mSize >= specific->mSize); 2017 memcpy(info->mData, specific->mData, specific->mSize); 2018 } 2019 2020 mNoMoreOutputData = false; 2021 2022 CODEC_LOGV("calling emptyBuffer with codec specific data"); 2023 2024 status_t err = mOMX->emptyBuffer( 2025 mNode, info->mBuffer, 0, size, 2026 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG, 2027 0); 2028 CHECK_EQ(err, OK); 2029 2030 info->mOwnedByComponent = true; 2031 2032 ++mCodecSpecificDataIndex; 2033 return; 2034 } 2035 2036 status_t err; 2037 2038 bool signalEOS = false; 2039 int64_t timestampUs = 0; 2040 2041 size_t offset = 0; 2042 int32_t n = 0; 2043 for (;;) { 2044 MediaBuffer *srcBuffer; 2045 if (mSeekTimeUs >= 0) { 2046 if (mLeftOverBuffer) { 2047 mLeftOverBuffer->release(); 2048 mLeftOverBuffer = NULL; 2049 } 2050 2051 MediaSource::ReadOptions options; 2052 options.setSeekTo(mSeekTimeUs); 2053 2054 mSeekTimeUs = -1; 2055 mBufferFilled.signal(); 2056 2057 err = mSource->read(&srcBuffer, &options); 2058 } else if (mLeftOverBuffer) { 2059 srcBuffer = mLeftOverBuffer; 2060 mLeftOverBuffer = NULL; 2061 2062 err = OK; 2063 } else { 2064 err = mSource->read(&srcBuffer); 2065 } 2066 2067 if (err != OK) { 2068 signalEOS = true; 2069 mFinalStatus = err; 2070 mSignalledEOS = true; 2071 break; 2072 } 2073 2074 size_t remainingBytes = info->mSize - offset; 2075 2076 if (srcBuffer->range_length() > remainingBytes) { 2077 if (offset == 0) { 2078 CODEC_LOGE( 2079 "Codec's input buffers are too small to accomodate " 2080 "buffer read from source (info->mSize = %d, srcLength = %d)", 2081 info->mSize, srcBuffer->range_length()); 2082 2083 srcBuffer->release(); 2084 srcBuffer = NULL; 2085 2086 setState(ERROR); 2087 return; 2088 } 2089 2090 mLeftOverBuffer = srcBuffer; 2091 break; 2092 } 2093 2094 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) { 2095 CHECK(mOMXLivesLocally && offset == 0); 2096 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer; 2097 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset(); 2098 } else { 2099 memcpy((uint8_t *)info->mData + offset, 2100 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(), 2101 srcBuffer->range_length()); 2102 } 2103 2104 int64_t lastBufferTimeUs; 2105 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs)); 2106 CHECK(timestampUs >= 0); 2107 2108 if (offset == 0) { 2109 timestampUs = lastBufferTimeUs; 2110 } 2111 2112 offset += srcBuffer->range_length(); 2113 2114 srcBuffer->release(); 2115 srcBuffer = NULL; 2116 2117 ++n; 2118 2119 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) { 2120 break; 2121 } 2122 2123 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs; 2124 2125 if (coalescedDurationUs > 250000ll) { 2126 // Don't coalesce more than 250ms worth of encoded data at once. 2127 break; 2128 } 2129 } 2130 2131 if (n > 1) { 2132 LOGV("coalesced %d frames into one input buffer", n); 2133 } 2134 2135 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME; 2136 2137 if (signalEOS) { 2138 flags |= OMX_BUFFERFLAG_EOS; 2139 } else { 2140 mNoMoreOutputData = false; 2141 } 2142 2143 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), " 2144 "timestamp %lld us (%.2f secs)", 2145 info->mBuffer, offset, 2146 timestampUs, timestampUs / 1E6); 2147 2148 err = mOMX->emptyBuffer( 2149 mNode, info->mBuffer, 0, offset, 2150 flags, timestampUs); 2151 2152 if (err != OK) { 2153 setState(ERROR); 2154 return; 2155 } 2156 2157 info->mOwnedByComponent = true; 2158 2159 // This component does not ever signal the EOS flag on output buffers, 2160 // Thanks for nothing. 2161 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) { 2162 mNoMoreOutputData = true; 2163 mBufferFilled.signal(); 2164 } 2165} 2166 2167void OMXCodec::fillOutputBuffer(BufferInfo *info) { 2168 CHECK_EQ(info->mOwnedByComponent, false); 2169 2170 if (mNoMoreOutputData) { 2171 CODEC_LOGV("There is no more output data available, not " 2172 "calling fillOutputBuffer"); 2173 return; 2174 } 2175 2176 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer); 2177 status_t err = mOMX->fillBuffer(mNode, info->mBuffer); 2178 2179 if (err != OK) { 2180 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err); 2181 2182 setState(ERROR); 2183 return; 2184 } 2185 2186 info->mOwnedByComponent = true; 2187} 2188 2189void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) { 2190 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 2191 for (size_t i = 0; i < buffers->size(); ++i) { 2192 if ((*buffers)[i].mBuffer == buffer) { 2193 drainInputBuffer(&buffers->editItemAt(i)); 2194 return; 2195 } 2196 } 2197 2198 CHECK(!"should not be here."); 2199} 2200 2201void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) { 2202 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 2203 for (size_t i = 0; i < buffers->size(); ++i) { 2204 if ((*buffers)[i].mBuffer == buffer) { 2205 fillOutputBuffer(&buffers->editItemAt(i)); 2206 return; 2207 } 2208 } 2209 2210 CHECK(!"should not be here."); 2211} 2212 2213void OMXCodec::setState(State newState) { 2214 mState = newState; 2215 mAsyncCompletion.signal(); 2216 2217 // This may cause some spurious wakeups but is necessary to 2218 // unblock the reader if we enter ERROR state. 2219 mBufferFilled.signal(); 2220} 2221 2222void OMXCodec::setRawAudioFormat( 2223 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) { 2224 2225 // port definition 2226 OMX_PARAM_PORTDEFINITIONTYPE def; 2227 InitOMXParams(&def); 2228 def.nPortIndex = portIndex; 2229 status_t err = mOMX->getParameter( 2230 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2231 CHECK_EQ(err, OK); 2232 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM; 2233 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition, 2234 &def, sizeof(def)), OK); 2235 2236 // pcm param 2237 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams; 2238 InitOMXParams(&pcmParams); 2239 pcmParams.nPortIndex = portIndex; 2240 2241 err = mOMX->getParameter( 2242 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); 2243 2244 CHECK_EQ(err, OK); 2245 2246 pcmParams.nChannels = numChannels; 2247 pcmParams.eNumData = OMX_NumericalDataSigned; 2248 pcmParams.bInterleaved = OMX_TRUE; 2249 pcmParams.nBitPerSample = 16; 2250 pcmParams.nSamplingRate = sampleRate; 2251 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear; 2252 2253 if (numChannels == 1) { 2254 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF; 2255 } else { 2256 CHECK_EQ(numChannels, 2); 2257 2258 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF; 2259 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF; 2260 } 2261 2262 err = mOMX->setParameter( 2263 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); 2264 2265 CHECK_EQ(err, OK); 2266} 2267 2268static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) { 2269 if (isAMRWB) { 2270 if (bps <= 6600) { 2271 return OMX_AUDIO_AMRBandModeWB0; 2272 } else if (bps <= 8850) { 2273 return OMX_AUDIO_AMRBandModeWB1; 2274 } else if (bps <= 12650) { 2275 return OMX_AUDIO_AMRBandModeWB2; 2276 } else if (bps <= 14250) { 2277 return OMX_AUDIO_AMRBandModeWB3; 2278 } else if (bps <= 15850) { 2279 return OMX_AUDIO_AMRBandModeWB4; 2280 } else if (bps <= 18250) { 2281 return OMX_AUDIO_AMRBandModeWB5; 2282 } else if (bps <= 19850) { 2283 return OMX_AUDIO_AMRBandModeWB6; 2284 } else if (bps <= 23050) { 2285 return OMX_AUDIO_AMRBandModeWB7; 2286 } 2287 2288 // 23850 bps 2289 return OMX_AUDIO_AMRBandModeWB8; 2290 } else { // AMRNB 2291 if (bps <= 4750) { 2292 return OMX_AUDIO_AMRBandModeNB0; 2293 } else if (bps <= 5150) { 2294 return OMX_AUDIO_AMRBandModeNB1; 2295 } else if (bps <= 5900) { 2296 return OMX_AUDIO_AMRBandModeNB2; 2297 } else if (bps <= 6700) { 2298 return OMX_AUDIO_AMRBandModeNB3; 2299 } else if (bps <= 7400) { 2300 return OMX_AUDIO_AMRBandModeNB4; 2301 } else if (bps <= 7950) { 2302 return OMX_AUDIO_AMRBandModeNB5; 2303 } else if (bps <= 10200) { 2304 return OMX_AUDIO_AMRBandModeNB6; 2305 } 2306 2307 // 12200 bps 2308 return OMX_AUDIO_AMRBandModeNB7; 2309 } 2310} 2311 2312void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) { 2313 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput; 2314 2315 OMX_AUDIO_PARAM_AMRTYPE def; 2316 InitOMXParams(&def); 2317 def.nPortIndex = portIndex; 2318 2319 status_t err = 2320 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 2321 2322 CHECK_EQ(err, OK); 2323 2324 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF; 2325 2326 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate); 2327 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 2328 CHECK_EQ(err, OK); 2329 2330 //////////////////////// 2331 2332 if (mIsEncoder) { 2333 sp<MetaData> format = mSource->getFormat(); 2334 int32_t sampleRate; 2335 int32_t numChannels; 2336 CHECK(format->findInt32(kKeySampleRate, &sampleRate)); 2337 CHECK(format->findInt32(kKeyChannelCount, &numChannels)); 2338 2339 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 2340 } 2341} 2342 2343void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) { 2344 CHECK(numChannels == 1 || numChannels == 2); 2345 if (mIsEncoder) { 2346 //////////////// input port //////////////////// 2347 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 2348 2349 //////////////// output port //////////////////// 2350 // format 2351 OMX_AUDIO_PARAM_PORTFORMATTYPE format; 2352 format.nPortIndex = kPortIndexOutput; 2353 format.nIndex = 0; 2354 status_t err = OMX_ErrorNone; 2355 while (OMX_ErrorNone == err) { 2356 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat, 2357 &format, sizeof(format)), OK); 2358 if (format.eEncoding == OMX_AUDIO_CodingAAC) { 2359 break; 2360 } 2361 format.nIndex++; 2362 } 2363 CHECK_EQ(OK, err); 2364 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat, 2365 &format, sizeof(format)), OK); 2366 2367 // port definition 2368 OMX_PARAM_PORTDEFINITIONTYPE def; 2369 InitOMXParams(&def); 2370 def.nPortIndex = kPortIndexOutput; 2371 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition, 2372 &def, sizeof(def)), OK); 2373 def.format.audio.bFlagErrorConcealment = OMX_TRUE; 2374 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC; 2375 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition, 2376 &def, sizeof(def)), OK); 2377 2378 // profile 2379 OMX_AUDIO_PARAM_AACPROFILETYPE profile; 2380 InitOMXParams(&profile); 2381 profile.nPortIndex = kPortIndexOutput; 2382 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac, 2383 &profile, sizeof(profile)), OK); 2384 profile.nChannels = numChannels; 2385 profile.eChannelMode = (numChannels == 1? 2386 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo); 2387 profile.nSampleRate = sampleRate; 2388 profile.nBitRate = bitRate; 2389 profile.nAudioBandWidth = 0; 2390 profile.nFrameLength = 0; 2391 profile.nAACtools = OMX_AUDIO_AACToolAll; 2392 profile.nAACERtools = OMX_AUDIO_AACERNone; 2393 profile.eAACProfile = OMX_AUDIO_AACObjectLC; 2394 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF; 2395 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac, 2396 &profile, sizeof(profile)), OK); 2397 2398 } else { 2399 OMX_AUDIO_PARAM_AACPROFILETYPE profile; 2400 InitOMXParams(&profile); 2401 profile.nPortIndex = kPortIndexInput; 2402 2403 status_t err = mOMX->getParameter( 2404 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); 2405 CHECK_EQ(err, OK); 2406 2407 profile.nChannels = numChannels; 2408 profile.nSampleRate = sampleRate; 2409 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS; 2410 2411 err = mOMX->setParameter( 2412 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); 2413 CHECK_EQ(err, OK); 2414 } 2415} 2416 2417void OMXCodec::setImageOutputFormat( 2418 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) { 2419 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height); 2420 2421#if 0 2422 OMX_INDEXTYPE index; 2423 status_t err = mOMX->get_extension_index( 2424 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index); 2425 CHECK_EQ(err, OK); 2426 2427 err = mOMX->set_config(mNode, index, &format, sizeof(format)); 2428 CHECK_EQ(err, OK); 2429#endif 2430 2431 OMX_PARAM_PORTDEFINITIONTYPE def; 2432 InitOMXParams(&def); 2433 def.nPortIndex = kPortIndexOutput; 2434 2435 status_t err = mOMX->getParameter( 2436 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2437 CHECK_EQ(err, OK); 2438 2439 CHECK_EQ(def.eDomain, OMX_PortDomainImage); 2440 2441 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 2442 2443 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused); 2444 imageDef->eColorFormat = format; 2445 imageDef->nFrameWidth = width; 2446 imageDef->nFrameHeight = height; 2447 2448 switch (format) { 2449 case OMX_COLOR_FormatYUV420PackedPlanar: 2450 case OMX_COLOR_FormatYUV411Planar: 2451 { 2452 def.nBufferSize = (width * height * 3) / 2; 2453 break; 2454 } 2455 2456 case OMX_COLOR_FormatCbYCrY: 2457 { 2458 def.nBufferSize = width * height * 2; 2459 break; 2460 } 2461 2462 case OMX_COLOR_Format32bitARGB8888: 2463 { 2464 def.nBufferSize = width * height * 4; 2465 break; 2466 } 2467 2468 case OMX_COLOR_Format16bitARGB4444: 2469 case OMX_COLOR_Format16bitARGB1555: 2470 case OMX_COLOR_Format16bitRGB565: 2471 case OMX_COLOR_Format16bitBGR565: 2472 { 2473 def.nBufferSize = width * height * 2; 2474 break; 2475 } 2476 2477 default: 2478 CHECK(!"Should not be here. Unknown color format."); 2479 break; 2480 } 2481 2482 def.nBufferCountActual = def.nBufferCountMin; 2483 2484 err = mOMX->setParameter( 2485 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2486 CHECK_EQ(err, OK); 2487} 2488 2489void OMXCodec::setJPEGInputFormat( 2490 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) { 2491 OMX_PARAM_PORTDEFINITIONTYPE def; 2492 InitOMXParams(&def); 2493 def.nPortIndex = kPortIndexInput; 2494 2495 status_t err = mOMX->getParameter( 2496 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2497 CHECK_EQ(err, OK); 2498 2499 CHECK_EQ(def.eDomain, OMX_PortDomainImage); 2500 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 2501 2502 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG); 2503 imageDef->nFrameWidth = width; 2504 imageDef->nFrameHeight = height; 2505 2506 def.nBufferSize = compressedSize; 2507 def.nBufferCountActual = def.nBufferCountMin; 2508 2509 err = mOMX->setParameter( 2510 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2511 CHECK_EQ(err, OK); 2512} 2513 2514void OMXCodec::addCodecSpecificData(const void *data, size_t size) { 2515 CodecSpecificData *specific = 2516 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1); 2517 2518 specific->mSize = size; 2519 memcpy(specific->mData, data, size); 2520 2521 mCodecSpecificData.push(specific); 2522} 2523 2524void OMXCodec::clearCodecSpecificData() { 2525 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) { 2526 free(mCodecSpecificData.editItemAt(i)); 2527 } 2528 mCodecSpecificData.clear(); 2529 mCodecSpecificDataIndex = 0; 2530} 2531 2532status_t OMXCodec::start(MetaData *) { 2533 Mutex::Autolock autoLock(mLock); 2534 2535 if (mState != LOADED) { 2536 return UNKNOWN_ERROR; 2537 } 2538 2539 sp<MetaData> params = new MetaData; 2540 if (mQuirks & kWantsNALFragments) { 2541 params->setInt32(kKeyWantsNALFragments, true); 2542 } 2543 status_t err = mSource->start(params.get()); 2544 2545 if (err != OK) { 2546 return err; 2547 } 2548 2549 mCodecSpecificDataIndex = 0; 2550 mInitialBufferSubmit = true; 2551 mSignalledEOS = false; 2552 mNoMoreOutputData = false; 2553 mOutputPortSettingsHaveChanged = false; 2554 mSeekTimeUs = -1; 2555 mFilledBuffers.clear(); 2556 2557 return init(); 2558} 2559 2560status_t OMXCodec::stop() { 2561 CODEC_LOGV("stop mState=%d", mState); 2562 2563 Mutex::Autolock autoLock(mLock); 2564 2565 while (isIntermediateState(mState)) { 2566 mAsyncCompletion.wait(mLock); 2567 } 2568 2569 switch (mState) { 2570 case LOADED: 2571 case ERROR: 2572 break; 2573 2574 case EXECUTING: 2575 { 2576 setState(EXECUTING_TO_IDLE); 2577 2578 if (mQuirks & kRequiresFlushBeforeShutdown) { 2579 CODEC_LOGV("This component requires a flush before transitioning " 2580 "from EXECUTING to IDLE..."); 2581 2582 bool emulateInputFlushCompletion = 2583 !flushPortAsync(kPortIndexInput); 2584 2585 bool emulateOutputFlushCompletion = 2586 !flushPortAsync(kPortIndexOutput); 2587 2588 if (emulateInputFlushCompletion) { 2589 onCmdComplete(OMX_CommandFlush, kPortIndexInput); 2590 } 2591 2592 if (emulateOutputFlushCompletion) { 2593 onCmdComplete(OMX_CommandFlush, kPortIndexOutput); 2594 } 2595 } else { 2596 mPortStatus[kPortIndexInput] = SHUTTING_DOWN; 2597 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; 2598 2599 status_t err = 2600 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 2601 CHECK_EQ(err, OK); 2602 } 2603 2604 while (mState != LOADED && mState != ERROR) { 2605 mAsyncCompletion.wait(mLock); 2606 } 2607 2608 break; 2609 } 2610 2611 default: 2612 { 2613 CHECK(!"should not be here."); 2614 break; 2615 } 2616 } 2617 2618 if (mLeftOverBuffer) { 2619 mLeftOverBuffer->release(); 2620 mLeftOverBuffer = NULL; 2621 } 2622 2623 mSource->stop(); 2624 2625 CODEC_LOGV("stopped"); 2626 2627 return OK; 2628} 2629 2630sp<MetaData> OMXCodec::getFormat() { 2631 Mutex::Autolock autoLock(mLock); 2632 2633 return mOutputFormat; 2634} 2635 2636status_t OMXCodec::read( 2637 MediaBuffer **buffer, const ReadOptions *options) { 2638 *buffer = NULL; 2639 2640 Mutex::Autolock autoLock(mLock); 2641 2642 if (mState != EXECUTING && mState != RECONFIGURING) { 2643 return UNKNOWN_ERROR; 2644 } 2645 2646 bool seeking = false; 2647 int64_t seekTimeUs; 2648 if (options && options->getSeekTo(&seekTimeUs)) { 2649 seeking = true; 2650 } 2651 2652 if (mInitialBufferSubmit) { 2653 mInitialBufferSubmit = false; 2654 2655 if (seeking) { 2656 CHECK(seekTimeUs >= 0); 2657 mSeekTimeUs = seekTimeUs; 2658 2659 // There's no reason to trigger the code below, there's 2660 // nothing to flush yet. 2661 seeking = false; 2662 } 2663 2664 drainInputBuffers(); 2665 2666 if (mState == EXECUTING) { 2667 // Otherwise mState == RECONFIGURING and this code will trigger 2668 // after the output port is reenabled. 2669 fillOutputBuffers(); 2670 } 2671 } 2672 2673 if (seeking) { 2674 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6); 2675 2676 mSignalledEOS = false; 2677 2678 CHECK(seekTimeUs >= 0); 2679 mSeekTimeUs = seekTimeUs; 2680 2681 mFilledBuffers.clear(); 2682 2683 CHECK_EQ(mState, EXECUTING); 2684 2685 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput); 2686 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput); 2687 2688 if (emulateInputFlushCompletion) { 2689 onCmdComplete(OMX_CommandFlush, kPortIndexInput); 2690 } 2691 2692 if (emulateOutputFlushCompletion) { 2693 onCmdComplete(OMX_CommandFlush, kPortIndexOutput); 2694 } 2695 2696 while (mSeekTimeUs >= 0) { 2697 mBufferFilled.wait(mLock); 2698 } 2699 } 2700 2701 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) { 2702 mBufferFilled.wait(mLock); 2703 } 2704 2705 if (mState == ERROR) { 2706 return UNKNOWN_ERROR; 2707 } 2708 2709 if (mFilledBuffers.empty()) { 2710 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM; 2711 } 2712 2713 if (mOutputPortSettingsHaveChanged) { 2714 mOutputPortSettingsHaveChanged = false; 2715 2716 return INFO_FORMAT_CHANGED; 2717 } 2718 2719 size_t index = *mFilledBuffers.begin(); 2720 mFilledBuffers.erase(mFilledBuffers.begin()); 2721 2722 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index); 2723 info->mMediaBuffer->add_ref(); 2724 *buffer = info->mMediaBuffer; 2725 2726 return OK; 2727} 2728 2729void OMXCodec::signalBufferReturned(MediaBuffer *buffer) { 2730 Mutex::Autolock autoLock(mLock); 2731 2732 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 2733 for (size_t i = 0; i < buffers->size(); ++i) { 2734 BufferInfo *info = &buffers->editItemAt(i); 2735 2736 if (info->mMediaBuffer == buffer) { 2737 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED); 2738 fillOutputBuffer(info); 2739 return; 2740 } 2741 } 2742 2743 CHECK(!"should not be here."); 2744} 2745 2746static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) { 2747 static const char *kNames[] = { 2748 "OMX_IMAGE_CodingUnused", 2749 "OMX_IMAGE_CodingAutoDetect", 2750 "OMX_IMAGE_CodingJPEG", 2751 "OMX_IMAGE_CodingJPEG2K", 2752 "OMX_IMAGE_CodingEXIF", 2753 "OMX_IMAGE_CodingTIFF", 2754 "OMX_IMAGE_CodingGIF", 2755 "OMX_IMAGE_CodingPNG", 2756 "OMX_IMAGE_CodingLZW", 2757 "OMX_IMAGE_CodingBMP", 2758 }; 2759 2760 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2761 2762 if (type < 0 || (size_t)type >= numNames) { 2763 return "UNKNOWN"; 2764 } else { 2765 return kNames[type]; 2766 } 2767} 2768 2769static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) { 2770 static const char *kNames[] = { 2771 "OMX_COLOR_FormatUnused", 2772 "OMX_COLOR_FormatMonochrome", 2773 "OMX_COLOR_Format8bitRGB332", 2774 "OMX_COLOR_Format12bitRGB444", 2775 "OMX_COLOR_Format16bitARGB4444", 2776 "OMX_COLOR_Format16bitARGB1555", 2777 "OMX_COLOR_Format16bitRGB565", 2778 "OMX_COLOR_Format16bitBGR565", 2779 "OMX_COLOR_Format18bitRGB666", 2780 "OMX_COLOR_Format18bitARGB1665", 2781 "OMX_COLOR_Format19bitARGB1666", 2782 "OMX_COLOR_Format24bitRGB888", 2783 "OMX_COLOR_Format24bitBGR888", 2784 "OMX_COLOR_Format24bitARGB1887", 2785 "OMX_COLOR_Format25bitARGB1888", 2786 "OMX_COLOR_Format32bitBGRA8888", 2787 "OMX_COLOR_Format32bitARGB8888", 2788 "OMX_COLOR_FormatYUV411Planar", 2789 "OMX_COLOR_FormatYUV411PackedPlanar", 2790 "OMX_COLOR_FormatYUV420Planar", 2791 "OMX_COLOR_FormatYUV420PackedPlanar", 2792 "OMX_COLOR_FormatYUV420SemiPlanar", 2793 "OMX_COLOR_FormatYUV422Planar", 2794 "OMX_COLOR_FormatYUV422PackedPlanar", 2795 "OMX_COLOR_FormatYUV422SemiPlanar", 2796 "OMX_COLOR_FormatYCbYCr", 2797 "OMX_COLOR_FormatYCrYCb", 2798 "OMX_COLOR_FormatCbYCrY", 2799 "OMX_COLOR_FormatCrYCbY", 2800 "OMX_COLOR_FormatYUV444Interleaved", 2801 "OMX_COLOR_FormatRawBayer8bit", 2802 "OMX_COLOR_FormatRawBayer10bit", 2803 "OMX_COLOR_FormatRawBayer8bitcompressed", 2804 "OMX_COLOR_FormatL2", 2805 "OMX_COLOR_FormatL4", 2806 "OMX_COLOR_FormatL8", 2807 "OMX_COLOR_FormatL16", 2808 "OMX_COLOR_FormatL24", 2809 "OMX_COLOR_FormatL32", 2810 "OMX_COLOR_FormatYUV420PackedSemiPlanar", 2811 "OMX_COLOR_FormatYUV422PackedSemiPlanar", 2812 "OMX_COLOR_Format18BitBGR666", 2813 "OMX_COLOR_Format24BitARGB6666", 2814 "OMX_COLOR_Format24BitABGR6666", 2815 }; 2816 2817 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2818 2819 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) { 2820 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar"; 2821 } else if (type < 0 || (size_t)type >= numNames) { 2822 return "UNKNOWN"; 2823 } else { 2824 return kNames[type]; 2825 } 2826} 2827 2828static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) { 2829 static const char *kNames[] = { 2830 "OMX_VIDEO_CodingUnused", 2831 "OMX_VIDEO_CodingAutoDetect", 2832 "OMX_VIDEO_CodingMPEG2", 2833 "OMX_VIDEO_CodingH263", 2834 "OMX_VIDEO_CodingMPEG4", 2835 "OMX_VIDEO_CodingWMV", 2836 "OMX_VIDEO_CodingRV", 2837 "OMX_VIDEO_CodingAVC", 2838 "OMX_VIDEO_CodingMJPEG", 2839 }; 2840 2841 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2842 2843 if (type < 0 || (size_t)type >= numNames) { 2844 return "UNKNOWN"; 2845 } else { 2846 return kNames[type]; 2847 } 2848} 2849 2850static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) { 2851 static const char *kNames[] = { 2852 "OMX_AUDIO_CodingUnused", 2853 "OMX_AUDIO_CodingAutoDetect", 2854 "OMX_AUDIO_CodingPCM", 2855 "OMX_AUDIO_CodingADPCM", 2856 "OMX_AUDIO_CodingAMR", 2857 "OMX_AUDIO_CodingGSMFR", 2858 "OMX_AUDIO_CodingGSMEFR", 2859 "OMX_AUDIO_CodingGSMHR", 2860 "OMX_AUDIO_CodingPDCFR", 2861 "OMX_AUDIO_CodingPDCEFR", 2862 "OMX_AUDIO_CodingPDCHR", 2863 "OMX_AUDIO_CodingTDMAFR", 2864 "OMX_AUDIO_CodingTDMAEFR", 2865 "OMX_AUDIO_CodingQCELP8", 2866 "OMX_AUDIO_CodingQCELP13", 2867 "OMX_AUDIO_CodingEVRC", 2868 "OMX_AUDIO_CodingSMV", 2869 "OMX_AUDIO_CodingG711", 2870 "OMX_AUDIO_CodingG723", 2871 "OMX_AUDIO_CodingG726", 2872 "OMX_AUDIO_CodingG729", 2873 "OMX_AUDIO_CodingAAC", 2874 "OMX_AUDIO_CodingMP3", 2875 "OMX_AUDIO_CodingSBC", 2876 "OMX_AUDIO_CodingVORBIS", 2877 "OMX_AUDIO_CodingWMA", 2878 "OMX_AUDIO_CodingRA", 2879 "OMX_AUDIO_CodingMIDI", 2880 }; 2881 2882 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2883 2884 if (type < 0 || (size_t)type >= numNames) { 2885 return "UNKNOWN"; 2886 } else { 2887 return kNames[type]; 2888 } 2889} 2890 2891static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) { 2892 static const char *kNames[] = { 2893 "OMX_AUDIO_PCMModeLinear", 2894 "OMX_AUDIO_PCMModeALaw", 2895 "OMX_AUDIO_PCMModeMULaw", 2896 }; 2897 2898 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2899 2900 if (type < 0 || (size_t)type >= numNames) { 2901 return "UNKNOWN"; 2902 } else { 2903 return kNames[type]; 2904 } 2905} 2906 2907static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) { 2908 static const char *kNames[] = { 2909 "OMX_AUDIO_AMRBandModeUnused", 2910 "OMX_AUDIO_AMRBandModeNB0", 2911 "OMX_AUDIO_AMRBandModeNB1", 2912 "OMX_AUDIO_AMRBandModeNB2", 2913 "OMX_AUDIO_AMRBandModeNB3", 2914 "OMX_AUDIO_AMRBandModeNB4", 2915 "OMX_AUDIO_AMRBandModeNB5", 2916 "OMX_AUDIO_AMRBandModeNB6", 2917 "OMX_AUDIO_AMRBandModeNB7", 2918 "OMX_AUDIO_AMRBandModeWB0", 2919 "OMX_AUDIO_AMRBandModeWB1", 2920 "OMX_AUDIO_AMRBandModeWB2", 2921 "OMX_AUDIO_AMRBandModeWB3", 2922 "OMX_AUDIO_AMRBandModeWB4", 2923 "OMX_AUDIO_AMRBandModeWB5", 2924 "OMX_AUDIO_AMRBandModeWB6", 2925 "OMX_AUDIO_AMRBandModeWB7", 2926 "OMX_AUDIO_AMRBandModeWB8", 2927 }; 2928 2929 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2930 2931 if (type < 0 || (size_t)type >= numNames) { 2932 return "UNKNOWN"; 2933 } else { 2934 return kNames[type]; 2935 } 2936} 2937 2938static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) { 2939 static const char *kNames[] = { 2940 "OMX_AUDIO_AMRFrameFormatConformance", 2941 "OMX_AUDIO_AMRFrameFormatIF1", 2942 "OMX_AUDIO_AMRFrameFormatIF2", 2943 "OMX_AUDIO_AMRFrameFormatFSF", 2944 "OMX_AUDIO_AMRFrameFormatRTPPayload", 2945 "OMX_AUDIO_AMRFrameFormatITU", 2946 }; 2947 2948 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2949 2950 if (type < 0 || (size_t)type >= numNames) { 2951 return "UNKNOWN"; 2952 } else { 2953 return kNames[type]; 2954 } 2955} 2956 2957void OMXCodec::dumpPortStatus(OMX_U32 portIndex) { 2958 OMX_PARAM_PORTDEFINITIONTYPE def; 2959 InitOMXParams(&def); 2960 def.nPortIndex = portIndex; 2961 2962 status_t err = mOMX->getParameter( 2963 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2964 CHECK_EQ(err, OK); 2965 2966 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output"); 2967 2968 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput) 2969 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput)); 2970 2971 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual); 2972 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin); 2973 printf(" nBufferSize = %ld\n", def.nBufferSize); 2974 2975 switch (def.eDomain) { 2976 case OMX_PortDomainImage: 2977 { 2978 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 2979 2980 printf("\n"); 2981 printf(" // Image\n"); 2982 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth); 2983 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight); 2984 printf(" nStride = %ld\n", imageDef->nStride); 2985 2986 printf(" eCompressionFormat = %s\n", 2987 imageCompressionFormatString(imageDef->eCompressionFormat)); 2988 2989 printf(" eColorFormat = %s\n", 2990 colorFormatString(imageDef->eColorFormat)); 2991 2992 break; 2993 } 2994 2995 case OMX_PortDomainVideo: 2996 { 2997 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video; 2998 2999 printf("\n"); 3000 printf(" // Video\n"); 3001 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth); 3002 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight); 3003 printf(" nStride = %ld\n", videoDef->nStride); 3004 3005 printf(" eCompressionFormat = %s\n", 3006 videoCompressionFormatString(videoDef->eCompressionFormat)); 3007 3008 printf(" eColorFormat = %s\n", 3009 colorFormatString(videoDef->eColorFormat)); 3010 3011 break; 3012 } 3013 3014 case OMX_PortDomainAudio: 3015 { 3016 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio; 3017 3018 printf("\n"); 3019 printf(" // Audio\n"); 3020 printf(" eEncoding = %s\n", 3021 audioCodingTypeString(audioDef->eEncoding)); 3022 3023 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) { 3024 OMX_AUDIO_PARAM_PCMMODETYPE params; 3025 InitOMXParams(¶ms); 3026 params.nPortIndex = portIndex; 3027 3028 err = mOMX->getParameter( 3029 mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); 3030 CHECK_EQ(err, OK); 3031 3032 printf(" nSamplingRate = %ld\n", params.nSamplingRate); 3033 printf(" nChannels = %ld\n", params.nChannels); 3034 printf(" bInterleaved = %d\n", params.bInterleaved); 3035 printf(" nBitPerSample = %ld\n", params.nBitPerSample); 3036 3037 printf(" eNumData = %s\n", 3038 params.eNumData == OMX_NumericalDataSigned 3039 ? "signed" : "unsigned"); 3040 3041 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode)); 3042 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) { 3043 OMX_AUDIO_PARAM_AMRTYPE amr; 3044 InitOMXParams(&amr); 3045 amr.nPortIndex = portIndex; 3046 3047 err = mOMX->getParameter( 3048 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); 3049 CHECK_EQ(err, OK); 3050 3051 printf(" nChannels = %ld\n", amr.nChannels); 3052 printf(" eAMRBandMode = %s\n", 3053 amrBandModeString(amr.eAMRBandMode)); 3054 printf(" eAMRFrameFormat = %s\n", 3055 amrFrameFormatString(amr.eAMRFrameFormat)); 3056 } 3057 3058 break; 3059 } 3060 3061 default: 3062 { 3063 printf(" // Unknown\n"); 3064 break; 3065 } 3066 } 3067 3068 printf("}\n"); 3069} 3070 3071void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) { 3072 mOutputFormat = new MetaData; 3073 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName); 3074 3075 OMX_PARAM_PORTDEFINITIONTYPE def; 3076 InitOMXParams(&def); 3077 def.nPortIndex = kPortIndexOutput; 3078 3079 status_t err = mOMX->getParameter( 3080 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 3081 CHECK_EQ(err, OK); 3082 3083 switch (def.eDomain) { 3084 case OMX_PortDomainImage: 3085 { 3086 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 3087 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused); 3088 3089 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 3090 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat); 3091 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth); 3092 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight); 3093 break; 3094 } 3095 3096 case OMX_PortDomainAudio: 3097 { 3098 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio; 3099 3100 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) { 3101 OMX_AUDIO_PARAM_PCMMODETYPE params; 3102 InitOMXParams(¶ms); 3103 params.nPortIndex = kPortIndexOutput; 3104 3105 err = mOMX->getParameter( 3106 mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); 3107 CHECK_EQ(err, OK); 3108 3109 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned); 3110 CHECK_EQ(params.nBitPerSample, 16); 3111 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear); 3112 3113 int32_t numChannels, sampleRate; 3114 inputFormat->findInt32(kKeyChannelCount, &numChannels); 3115 inputFormat->findInt32(kKeySampleRate, &sampleRate); 3116 3117 if ((OMX_U32)numChannels != params.nChannels) { 3118 LOGW("Codec outputs a different number of channels than " 3119 "the input stream contains (contains %d channels, " 3120 "codec outputs %ld channels).", 3121 numChannels, params.nChannels); 3122 } 3123 3124 mOutputFormat->setCString( 3125 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); 3126 3127 // Use the codec-advertised number of channels, as some 3128 // codecs appear to output stereo even if the input data is 3129 // mono. If we know the codec lies about this information, 3130 // use the actual number of channels instead. 3131 mOutputFormat->setInt32( 3132 kKeyChannelCount, 3133 (mQuirks & kDecoderLiesAboutNumberOfChannels) 3134 ? numChannels : params.nChannels); 3135 3136 // The codec-reported sampleRate is not reliable... 3137 mOutputFormat->setInt32(kKeySampleRate, sampleRate); 3138 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) { 3139 OMX_AUDIO_PARAM_AMRTYPE amr; 3140 InitOMXParams(&amr); 3141 amr.nPortIndex = kPortIndexOutput; 3142 3143 err = mOMX->getParameter( 3144 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); 3145 CHECK_EQ(err, OK); 3146 3147 CHECK_EQ(amr.nChannels, 1); 3148 mOutputFormat->setInt32(kKeyChannelCount, 1); 3149 3150 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0 3151 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) { 3152 mOutputFormat->setCString( 3153 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB); 3154 mOutputFormat->setInt32(kKeySampleRate, 8000); 3155 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0 3156 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) { 3157 mOutputFormat->setCString( 3158 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB); 3159 mOutputFormat->setInt32(kKeySampleRate, 16000); 3160 } else { 3161 CHECK(!"Unknown AMR band mode."); 3162 } 3163 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) { 3164 mOutputFormat->setCString( 3165 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC); 3166 int32_t numChannels, sampleRate, bitRate; 3167 inputFormat->findInt32(kKeyChannelCount, &numChannels); 3168 inputFormat->findInt32(kKeySampleRate, &sampleRate); 3169 inputFormat->findInt32(kKeyBitRate, &bitRate); 3170 mOutputFormat->setInt32(kKeyChannelCount, numChannels); 3171 mOutputFormat->setInt32(kKeySampleRate, sampleRate); 3172 mOutputFormat->setInt32(kKeyBitRate, bitRate); 3173 } else { 3174 CHECK(!"Should not be here. Unknown audio encoding."); 3175 } 3176 break; 3177 } 3178 3179 case OMX_PortDomainVideo: 3180 { 3181 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 3182 3183 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) { 3184 mOutputFormat->setCString( 3185 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 3186 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) { 3187 mOutputFormat->setCString( 3188 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); 3189 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) { 3190 mOutputFormat->setCString( 3191 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); 3192 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) { 3193 mOutputFormat->setCString( 3194 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 3195 } else { 3196 CHECK(!"Unknown compression format."); 3197 } 3198 3199 if (!strcmp(mComponentName, "OMX.PV.avcdec")) { 3200 // This component appears to be lying to me. 3201 mOutputFormat->setInt32( 3202 kKeyWidth, (video_def->nFrameWidth + 15) & -16); 3203 mOutputFormat->setInt32( 3204 kKeyHeight, (video_def->nFrameHeight + 15) & -16); 3205 } else { 3206 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth); 3207 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight); 3208 } 3209 3210 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat); 3211 break; 3212 } 3213 3214 default: 3215 { 3216 CHECK(!"should not be here, neither audio nor video."); 3217 break; 3218 } 3219 } 3220} 3221 3222//////////////////////////////////////////////////////////////////////////////// 3223 3224status_t QueryCodecs( 3225 const sp<IOMX> &omx, 3226 const char *mime, bool queryDecoders, 3227 Vector<CodecCapabilities> *results) { 3228 results->clear(); 3229 3230 for (int index = 0;; ++index) { 3231 const char *componentName; 3232 3233 if (!queryDecoders) { 3234 componentName = GetCodec( 3235 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]), 3236 mime, index); 3237 } else { 3238 componentName = GetCodec( 3239 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]), 3240 mime, index); 3241 } 3242 3243 if (!componentName) { 3244 return OK; 3245 } 3246 3247 if (strncmp(componentName, "OMX.", 4)) { 3248 // Not an OpenMax component but a software codec. 3249 3250 results->push(); 3251 CodecCapabilities *caps = &results->editItemAt(results->size() - 1); 3252 caps->mComponentName = componentName; 3253 3254 continue; 3255 } 3256 3257 sp<OMXCodecObserver> observer = new OMXCodecObserver; 3258 IOMX::node_id node; 3259 status_t err = omx->allocateNode(componentName, observer, &node); 3260 3261 if (err != OK) { 3262 continue; 3263 } 3264 3265 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime); 3266 3267 results->push(); 3268 CodecCapabilities *caps = &results->editItemAt(results->size() - 1); 3269 caps->mComponentName = componentName; 3270 3271 OMX_VIDEO_PARAM_PROFILELEVELTYPE param; 3272 InitOMXParams(¶m); 3273 3274 param.nPortIndex = queryDecoders ? 0 : 1; 3275 3276 for (param.nProfileIndex = 0;; ++param.nProfileIndex) { 3277 err = omx->getParameter( 3278 node, OMX_IndexParamVideoProfileLevelQuerySupported, 3279 ¶m, sizeof(param)); 3280 3281 if (err != OK) { 3282 break; 3283 } 3284 3285 CodecProfileLevel profileLevel; 3286 profileLevel.mProfile = param.eProfile; 3287 profileLevel.mLevel = param.eLevel; 3288 3289 caps->mProfileLevels.push(profileLevel); 3290 } 3291 3292 CHECK_EQ(omx->freeNode(node), OK); 3293 } 3294} 3295 3296} // namespace android 3297