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