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