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