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