OMXCodec.cpp revision 48c948b1137e7bbdb161b51908657ab72ac5e2da
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 42static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00; 43 44struct CodecInfo { 45 const char *mime; 46 const char *codec; 47}; 48 49static const CodecInfo kDecoderInfo[] = { 50 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" }, 51 { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" }, 52 { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" }, 53 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" }, 54 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" }, 55 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" }, 56 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" }, 57 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" }, 58 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" }, 59 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" }, 60 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" }, 61 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" }, 62 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" }, 63 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.Decoder" }, 64 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" }, 65 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" }, 66 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" }, 67 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" }, 68}; 69 70static const CodecInfo kEncoderInfo[] = { 71 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" }, 72 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrencnb" }, 73 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" }, 74 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" }, 75 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" }, 76 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" }, 77 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" }, 78 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" }, 79 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" }, 80 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" }, 81 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" }, 82 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" }, 83 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" }, 84}; 85 86#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__) 87#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__) 88 89struct OMXCodecObserver : public BnOMXObserver { 90 OMXCodecObserver(const wp<OMXCodec> &target) 91 : mTarget(target) { 92 } 93 94 // from IOMXObserver 95 virtual void on_message(const omx_message &msg) { 96 sp<OMXCodec> codec = mTarget.promote(); 97 98 if (codec.get() != NULL) { 99 codec->on_message(msg); 100 } 101 } 102 103protected: 104 virtual ~OMXCodecObserver() {} 105 106private: 107 wp<OMXCodec> mTarget; 108 109 OMXCodecObserver(const OMXCodecObserver &); 110 OMXCodecObserver &operator=(const OMXCodecObserver &); 111}; 112 113static const char *GetCodec(const CodecInfo *info, size_t numInfos, 114 const char *mime, int index) { 115 CHECK(index >= 0); 116 for(size_t i = 0; i < numInfos; ++i) { 117 if (!strcasecmp(mime, info[i].mime)) { 118 if (index == 0) { 119 return info[i].codec; 120 } 121 122 --index; 123 } 124 } 125 126 return NULL; 127} 128 129enum { 130 kAVCProfileBaseline = 0x42, 131 kAVCProfileMain = 0x4d, 132 kAVCProfileExtended = 0x58, 133 kAVCProfileHigh = 0x64, 134 kAVCProfileHigh10 = 0x6e, 135 kAVCProfileHigh422 = 0x7a, 136 kAVCProfileHigh444 = 0xf4, 137 kAVCProfileCAVLC444Intra = 0x2c 138}; 139 140static const char *AVCProfileToString(uint8_t profile) { 141 switch (profile) { 142 case kAVCProfileBaseline: 143 return "Baseline"; 144 case kAVCProfileMain: 145 return "Main"; 146 case kAVCProfileExtended: 147 return "Extended"; 148 case kAVCProfileHigh: 149 return "High"; 150 case kAVCProfileHigh10: 151 return "High 10"; 152 case kAVCProfileHigh422: 153 return "High 422"; 154 case kAVCProfileHigh444: 155 return "High 444"; 156 case kAVCProfileCAVLC444Intra: 157 return "CAVLC 444 Intra"; 158 default: return "Unknown"; 159 } 160} 161 162template<class T> 163static void InitOMXParams(T *params) { 164 params->nSize = sizeof(T); 165 params->nVersion.s.nVersionMajor = 1; 166 params->nVersion.s.nVersionMinor = 0; 167 params->nVersion.s.nRevision = 0; 168 params->nVersion.s.nStep = 0; 169} 170 171// static 172sp<OMXCodec> OMXCodec::Create( 173 const sp<IOMX> &omx, 174 const sp<MetaData> &meta, bool createEncoder, 175 const sp<MediaSource> &source, 176 const char *matchComponentName) { 177 const char *mime; 178 bool success = meta->findCString(kKeyMIMEType, &mime); 179 CHECK(success); 180 181 const char *componentName = NULL; 182 IOMX::node_id node = 0; 183 for (int index = 0;; ++index) { 184 if (createEncoder) { 185 componentName = GetCodec( 186 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]), 187 mime, index); 188 } else { 189 componentName = GetCodec( 190 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]), 191 mime, index); 192 } 193 194 if (!componentName) { 195 return NULL; 196 } 197 198 // If a specific codec is requested, skip the non-matching ones. 199 if (matchComponentName && strcmp(componentName, matchComponentName)) { 200 continue; 201 } 202 203 LOGV("Attempting to allocate OMX node '%s'", componentName); 204 205 status_t err = omx->allocate_node(componentName, &node); 206 if (err == OK) { 207 LOGV("Successfully allocated OMX node '%s'", componentName); 208 break; 209 } 210 } 211 212 uint32_t quirks = 0; 213 if (!strcmp(componentName, "OMX.PV.avcdec")) { 214 quirks |= kWantsNALFragments; 215 } 216 if (!strcmp(componentName, "OMX.TI.MP3.decode")) { 217 quirks |= kNeedsFlushBeforeDisable; 218 } 219 if (!strcmp(componentName, "OMX.TI.AAC.decode")) { 220 quirks |= kNeedsFlushBeforeDisable; 221 quirks |= kRequiresFlushCompleteEmulation; 222 } 223 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) { 224 quirks |= kRequiresLoadedToIdleAfterAllocation; 225 quirks |= kRequiresAllocateBufferOnInputPorts; 226 } 227 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) { 228 // XXX Required on P....on only. 229 quirks |= kRequiresAllocateBufferOnInputPorts; 230 quirks |= kRequiresAllocateBufferOnOutputPorts; 231 } 232 233 if (!strncmp(componentName, "OMX.TI.", 7)) { 234 // Apparently I must not use OMX_UseBuffer on either input or 235 // output ports on any of the TI components or quote: 236 // "(I) may have unexpected problem (sic) which can be timing related 237 // and hard to reproduce." 238 239 quirks |= kRequiresAllocateBufferOnInputPorts; 240 quirks |= kRequiresAllocateBufferOnOutputPorts; 241 } 242 243 sp<OMXCodec> codec = new OMXCodec( 244 omx, node, quirks, createEncoder, mime, componentName, 245 source); 246 247 uint32_t type; 248 const void *data; 249 size_t size; 250 if (meta->findData(kKeyESDS, &type, &data, &size)) { 251 ESDS esds((const char *)data, size); 252 CHECK_EQ(esds.InitCheck(), OK); 253 254 const void *codec_specific_data; 255 size_t codec_specific_data_size; 256 esds.getCodecSpecificInfo( 257 &codec_specific_data, &codec_specific_data_size); 258 259 printf("found codec-specific data of size %d\n", 260 codec_specific_data_size); 261 262 codec->addCodecSpecificData( 263 codec_specific_data, codec_specific_data_size); 264 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) { 265 printf("found avcc of size %d\n", size); 266 267 // Parse the AVCDecoderConfigurationRecord 268 269 const uint8_t *ptr = (const uint8_t *)data; 270 271 CHECK(size >= 7); 272 CHECK_EQ(ptr[0], 1); // configurationVersion == 1 273 uint8_t profile = ptr[1]; 274 uint8_t level = ptr[3]; 275 276 CHECK((ptr[4] >> 2) == 0x3f); // reserved 277 278 size_t lengthSize = 1 + (ptr[4] & 3); 279 280 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp 281 // violates it... 282 // CHECK((ptr[5] >> 5) == 7); // reserved 283 284 size_t numSeqParameterSets = ptr[5] & 31; 285 286 ptr += 6; 287 size -= 6; 288 289 for (size_t i = 0; i < numSeqParameterSets; ++i) { 290 CHECK(size >= 2); 291 size_t length = U16_AT(ptr); 292 293 ptr += 2; 294 size -= 2; 295 296 CHECK(size >= length); 297 298 codec->addCodecSpecificData(ptr, length); 299 300 ptr += length; 301 size -= length; 302 } 303 304 CHECK(size >= 1); 305 size_t numPictureParameterSets = *ptr; 306 ++ptr; 307 --size; 308 309 for (size_t i = 0; i < numPictureParameterSets; ++i) { 310 CHECK(size >= 2); 311 size_t length = U16_AT(ptr); 312 313 ptr += 2; 314 size -= 2; 315 316 CHECK(size >= length); 317 318 codec->addCodecSpecificData(ptr, length); 319 320 ptr += length; 321 size -= length; 322 } 323 324 LOGV("AVC profile = %d (%s), level = %d", 325 (int)profile, AVCProfileToString(profile), (int)level / 10); 326 327#if 0 328 if (!strcmp(componentName, "OMX.TI.Video.Decoder") 329 && (profile != kAVCProfileBaseline || level > 39)) { 330 // This stream exceeds the decoder's capabilities. 331 332 LOGE("Profile and/or level exceed the decoder's capabilities."); 333 return NULL; 334 } 335#endif 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_LOGV("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_LOGV("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_LOGV("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_LOGV("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_LOGV("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()->setInt64( 990 kKeyTime, msg.u.extended_buffer_data.timestamp); 991 992 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) { 993 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true); 994 } 995 996 buffer->meta_data()->setPointer( 997 kKeyPlatformPrivate, 998 msg.u.extended_buffer_data.platform_private); 999 1000 buffer->meta_data()->setPointer( 1001 kKeyBufferID, 1002 msg.u.extended_buffer_data.buffer); 1003 1004 mFilledBuffers.push_back(i); 1005 mBufferFilled.signal(); 1006 } 1007 1008 break; 1009 } 1010 1011 default: 1012 { 1013 CHECK(!"should not be here."); 1014 break; 1015 } 1016 } 1017} 1018 1019void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) { 1020 switch (event) { 1021 case OMX_EventCmdComplete: 1022 { 1023 onCmdComplete((OMX_COMMANDTYPE)data1, data2); 1024 break; 1025 } 1026 1027 case OMX_EventError: 1028 { 1029 LOGE("ERROR(%ld, %ld)", data1, data2); 1030 1031 setState(ERROR); 1032 break; 1033 } 1034 1035 case OMX_EventPortSettingsChanged: 1036 { 1037 onPortSettingsChanged(data1); 1038 break; 1039 } 1040 1041 case OMX_EventBufferFlag: 1042 { 1043 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1); 1044 1045 if (data1 == kPortIndexOutput) { 1046 mNoMoreOutputData = true; 1047 } 1048 break; 1049 } 1050 1051 default: 1052 { 1053 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2); 1054 break; 1055 } 1056 } 1057} 1058 1059void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) { 1060 switch (cmd) { 1061 case OMX_CommandStateSet: 1062 { 1063 onStateChange((OMX_STATETYPE)data); 1064 break; 1065 } 1066 1067 case OMX_CommandPortDisable: 1068 { 1069 OMX_U32 portIndex = data; 1070 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex); 1071 1072 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1073 CHECK_EQ(mPortStatus[portIndex], DISABLING); 1074 CHECK_EQ(mPortBuffers[portIndex].size(), 0); 1075 1076 mPortStatus[portIndex] = DISABLED; 1077 1078 if (mState == RECONFIGURING) { 1079 CHECK_EQ(portIndex, kPortIndexOutput); 1080 1081 enablePortAsync(portIndex); 1082 1083 status_t err = allocateBuffersOnPort(portIndex); 1084 CHECK_EQ(err, OK); 1085 } 1086 break; 1087 } 1088 1089 case OMX_CommandPortEnable: 1090 { 1091 OMX_U32 portIndex = data; 1092 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex); 1093 1094 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1095 CHECK_EQ(mPortStatus[portIndex], ENABLING); 1096 1097 mPortStatus[portIndex] = ENABLED; 1098 1099 if (mState == RECONFIGURING) { 1100 CHECK_EQ(portIndex, kPortIndexOutput); 1101 1102 setState(EXECUTING); 1103 1104 fillOutputBuffers(); 1105 } 1106 break; 1107 } 1108 1109 case OMX_CommandFlush: 1110 { 1111 OMX_U32 portIndex = data; 1112 1113 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex); 1114 1115 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN); 1116 mPortStatus[portIndex] = ENABLED; 1117 1118 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]), 1119 mPortBuffers[portIndex].size()); 1120 1121 if (mState == RECONFIGURING) { 1122 CHECK_EQ(portIndex, kPortIndexOutput); 1123 1124 disablePortAsync(portIndex); 1125 } else if (mState == EXECUTING_TO_IDLE) { 1126 if (mPortStatus[kPortIndexInput] == ENABLED 1127 && mPortStatus[kPortIndexOutput] == ENABLED) { 1128 CODEC_LOGV("Finished flushing both ports, now completing " 1129 "transition from EXECUTING to IDLE."); 1130 1131 mPortStatus[kPortIndexInput] = SHUTTING_DOWN; 1132 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; 1133 1134 status_t err = 1135 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle); 1136 CHECK_EQ(err, OK); 1137 } 1138 } else { 1139 // We're flushing both ports in preparation for seeking. 1140 1141 if (mPortStatus[kPortIndexInput] == ENABLED 1142 && mPortStatus[kPortIndexOutput] == ENABLED) { 1143 CODEC_LOGV("Finished flushing both ports, now continuing from" 1144 " seek-time."); 1145 1146 // Clear this flag in case the decoder sent us either 1147 // the EVENT_BUFFER_FLAG(1) or an output buffer with 1148 // the EOS flag set _while_ flushing. Since we're going 1149 // to submit "fresh" input data now, this flag no longer 1150 // applies to our future. 1151 mNoMoreOutputData = false; 1152 1153 drainInputBuffers(); 1154 fillOutputBuffers(); 1155 } 1156 } 1157 1158 break; 1159 } 1160 1161 default: 1162 { 1163 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data); 1164 break; 1165 } 1166 } 1167} 1168 1169void OMXCodec::onStateChange(OMX_STATETYPE newState) { 1170 switch (newState) { 1171 case OMX_StateIdle: 1172 { 1173 CODEC_LOGV("Now Idle."); 1174 if (mState == LOADED_TO_IDLE) { 1175 status_t err = mOMX->send_command( 1176 mNode, OMX_CommandStateSet, OMX_StateExecuting); 1177 1178 CHECK_EQ(err, OK); 1179 1180 setState(IDLE_TO_EXECUTING); 1181 } else { 1182 CHECK_EQ(mState, EXECUTING_TO_IDLE); 1183 1184 CHECK_EQ( 1185 countBuffersWeOwn(mPortBuffers[kPortIndexInput]), 1186 mPortBuffers[kPortIndexInput].size()); 1187 1188 CHECK_EQ( 1189 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]), 1190 mPortBuffers[kPortIndexOutput].size()); 1191 1192 status_t err = mOMX->send_command( 1193 mNode, OMX_CommandStateSet, OMX_StateLoaded); 1194 1195 CHECK_EQ(err, OK); 1196 1197 err = freeBuffersOnPort(kPortIndexInput); 1198 CHECK_EQ(err, OK); 1199 1200 err = freeBuffersOnPort(kPortIndexOutput); 1201 CHECK_EQ(err, OK); 1202 1203 mPortStatus[kPortIndexInput] = ENABLED; 1204 mPortStatus[kPortIndexOutput] = ENABLED; 1205 1206 setState(IDLE_TO_LOADED); 1207 } 1208 break; 1209 } 1210 1211 case OMX_StateExecuting: 1212 { 1213 CHECK_EQ(mState, IDLE_TO_EXECUTING); 1214 1215 CODEC_LOGV("Now Executing."); 1216 1217 setState(EXECUTING); 1218 1219 // Buffers will be submitted to the component in the first 1220 // call to OMXCodec::read as mInitialBufferSubmit is true at 1221 // this point. This ensures that this on_message call returns, 1222 // releases the lock and ::init can notice the state change and 1223 // itself return. 1224 break; 1225 } 1226 1227 case OMX_StateLoaded: 1228 { 1229 CHECK_EQ(mState, IDLE_TO_LOADED); 1230 1231 CODEC_LOGV("Now Loaded."); 1232 1233 setState(LOADED); 1234 break; 1235 } 1236 1237 default: 1238 { 1239 CHECK(!"should not be here."); 1240 break; 1241 } 1242 } 1243} 1244 1245// static 1246size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) { 1247 size_t n = 0; 1248 for (size_t i = 0; i < buffers.size(); ++i) { 1249 if (!buffers[i].mOwnedByComponent) { 1250 ++n; 1251 } 1252 } 1253 1254 return n; 1255} 1256 1257status_t OMXCodec::freeBuffersOnPort( 1258 OMX_U32 portIndex, bool onlyThoseWeOwn) { 1259 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; 1260 1261 status_t stickyErr = OK; 1262 1263 for (size_t i = buffers->size(); i-- > 0;) { 1264 BufferInfo *info = &buffers->editItemAt(i); 1265 1266 if (onlyThoseWeOwn && info->mOwnedByComponent) { 1267 continue; 1268 } 1269 1270 CHECK_EQ(info->mOwnedByComponent, false); 1271 1272 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex); 1273 1274 status_t err = 1275 mOMX->free_buffer(mNode, portIndex, info->mBuffer); 1276 1277 if (err != OK) { 1278 stickyErr = err; 1279 } 1280 1281 if (info->mMediaBuffer != NULL) { 1282 info->mMediaBuffer->setObserver(NULL); 1283 1284 // Make sure nobody but us owns this buffer at this point. 1285 CHECK_EQ(info->mMediaBuffer->refcount(), 0); 1286 1287 info->mMediaBuffer->release(); 1288 } 1289 1290 buffers->removeAt(i); 1291 } 1292 1293 CHECK(onlyThoseWeOwn || buffers->isEmpty()); 1294 1295 return stickyErr; 1296} 1297 1298void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) { 1299 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex); 1300 1301 CHECK_EQ(mState, EXECUTING); 1302 CHECK_EQ(portIndex, kPortIndexOutput); 1303 setState(RECONFIGURING); 1304 1305 if (mQuirks & kNeedsFlushBeforeDisable) { 1306 if (!flushPortAsync(portIndex)) { 1307 onCmdComplete(OMX_CommandFlush, portIndex); 1308 } 1309 } else { 1310 disablePortAsync(portIndex); 1311 } 1312} 1313 1314bool OMXCodec::flushPortAsync(OMX_U32 portIndex) { 1315 CHECK(mState == EXECUTING || mState == RECONFIGURING 1316 || mState == EXECUTING_TO_IDLE); 1317 1318 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.", 1319 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]), 1320 mPortBuffers[portIndex].size()); 1321 1322 CHECK_EQ(mPortStatus[portIndex], ENABLED); 1323 mPortStatus[portIndex] = SHUTTING_DOWN; 1324 1325 if ((mQuirks & kRequiresFlushCompleteEmulation) 1326 && countBuffersWeOwn(mPortBuffers[portIndex]) 1327 == mPortBuffers[portIndex].size()) { 1328 // No flush is necessary and this component fails to send a 1329 // flush-complete event in this case. 1330 1331 return false; 1332 } 1333 1334 status_t err = 1335 mOMX->send_command(mNode, OMX_CommandFlush, portIndex); 1336 CHECK_EQ(err, OK); 1337 1338 return true; 1339} 1340 1341void OMXCodec::disablePortAsync(OMX_U32 portIndex) { 1342 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1343 1344 CHECK_EQ(mPortStatus[portIndex], ENABLED); 1345 mPortStatus[portIndex] = DISABLING; 1346 1347 status_t err = 1348 mOMX->send_command(mNode, OMX_CommandPortDisable, portIndex); 1349 CHECK_EQ(err, OK); 1350 1351 freeBuffersOnPort(portIndex, true); 1352} 1353 1354void OMXCodec::enablePortAsync(OMX_U32 portIndex) { 1355 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1356 1357 CHECK_EQ(mPortStatus[portIndex], DISABLED); 1358 mPortStatus[portIndex] = ENABLING; 1359 1360 status_t err = 1361 mOMX->send_command(mNode, OMX_CommandPortEnable, portIndex); 1362 CHECK_EQ(err, OK); 1363} 1364 1365void OMXCodec::fillOutputBuffers() { 1366 CHECK_EQ(mState, EXECUTING); 1367 1368 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 1369 for (size_t i = 0; i < buffers->size(); ++i) { 1370 fillOutputBuffer(&buffers->editItemAt(i)); 1371 } 1372} 1373 1374void OMXCodec::drainInputBuffers() { 1375 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1376 1377 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 1378 for (size_t i = 0; i < buffers->size(); ++i) { 1379 drainInputBuffer(&buffers->editItemAt(i)); 1380 } 1381} 1382 1383void OMXCodec::drainInputBuffer(BufferInfo *info) { 1384 CHECK_EQ(info->mOwnedByComponent, false); 1385 1386 if (mSignalledEOS) { 1387 return; 1388 } 1389 1390 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) { 1391 const CodecSpecificData *specific = 1392 mCodecSpecificData[mCodecSpecificDataIndex]; 1393 1394 size_t size = specific->mSize; 1395 1396 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME) 1397 && !(mQuirks & kWantsNALFragments)) { 1398 static const uint8_t kNALStartCode[4] = 1399 { 0x00, 0x00, 0x00, 0x01 }; 1400 1401 CHECK(info->mMem->size() >= specific->mSize + 4); 1402 1403 size += 4; 1404 1405 memcpy(info->mMem->pointer(), kNALStartCode, 4); 1406 memcpy((uint8_t *)info->mMem->pointer() + 4, 1407 specific->mData, specific->mSize); 1408 } else { 1409 CHECK(info->mMem->size() >= specific->mSize); 1410 memcpy(info->mMem->pointer(), specific->mData, specific->mSize); 1411 } 1412 1413 status_t err = mOMX->empty_buffer( 1414 mNode, info->mBuffer, 0, size, 1415 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG, 1416 0); 1417 CHECK_EQ(err, OK); 1418 1419 info->mOwnedByComponent = true; 1420 1421 ++mCodecSpecificDataIndex; 1422 return; 1423 } 1424 1425 MediaBuffer *srcBuffer; 1426 status_t err; 1427 if (mSeekTimeUs >= 0) { 1428 MediaSource::ReadOptions options; 1429 options.setSeekTo(mSeekTimeUs); 1430 mSeekTimeUs = -1; 1431 1432 err = mSource->read(&srcBuffer, &options); 1433 } else { 1434 err = mSource->read(&srcBuffer); 1435 } 1436 1437 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME; 1438 OMX_TICKS timestampUs = 0; 1439 size_t srcLength = 0; 1440 1441 if (err != OK) { 1442 CODEC_LOGV("signalling end of input stream."); 1443 flags |= OMX_BUFFERFLAG_EOS; 1444 1445 mSignalledEOS = true; 1446 } else { 1447 srcLength = srcBuffer->range_length(); 1448 1449 if (info->mMem->size() < srcLength) { 1450 LOGE("info->mMem->size() = %d, srcLength = %d", 1451 info->mMem->size(), srcLength); 1452 } 1453 CHECK(info->mMem->size() >= srcLength); 1454 memcpy(info->mMem->pointer(), 1455 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(), 1456 srcLength); 1457 1458 if (srcBuffer->meta_data()->findInt64(kKeyTime, ×tampUs)) { 1459 CODEC_LOGV("Calling empty_buffer on buffer %p (length %d)", 1460 info->mBuffer, srcLength); 1461 CODEC_LOGV("Calling empty_buffer with timestamp %lld us (%.2f secs)", 1462 timestampUs, timestampUs / 1E6); 1463 } 1464 } 1465 1466 if (srcBuffer != NULL) { 1467 srcBuffer->release(); 1468 srcBuffer = NULL; 1469 } 1470 1471 err = mOMX->empty_buffer( 1472 mNode, info->mBuffer, 0, srcLength, 1473 flags, timestampUs); 1474 1475 if (err != OK) { 1476 setState(ERROR); 1477 return; 1478 } 1479 1480 info->mOwnedByComponent = true; 1481} 1482 1483void OMXCodec::fillOutputBuffer(BufferInfo *info) { 1484 CHECK_EQ(info->mOwnedByComponent, false); 1485 1486 if (mNoMoreOutputData) { 1487 CODEC_LOGV("There is no more output data available, not " 1488 "calling fillOutputBuffer"); 1489 return; 1490 } 1491 1492 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer); 1493 status_t err = mOMX->fill_buffer(mNode, info->mBuffer); 1494 CHECK_EQ(err, OK); 1495 1496 info->mOwnedByComponent = true; 1497} 1498 1499void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) { 1500 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 1501 for (size_t i = 0; i < buffers->size(); ++i) { 1502 if ((*buffers)[i].mBuffer == buffer) { 1503 drainInputBuffer(&buffers->editItemAt(i)); 1504 return; 1505 } 1506 } 1507 1508 CHECK(!"should not be here."); 1509} 1510 1511void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) { 1512 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 1513 for (size_t i = 0; i < buffers->size(); ++i) { 1514 if ((*buffers)[i].mBuffer == buffer) { 1515 fillOutputBuffer(&buffers->editItemAt(i)); 1516 return; 1517 } 1518 } 1519 1520 CHECK(!"should not be here."); 1521} 1522 1523void OMXCodec::setState(State newState) { 1524 mState = newState; 1525 mAsyncCompletion.signal(); 1526 1527 // This may cause some spurious wakeups but is necessary to 1528 // unblock the reader if we enter ERROR state. 1529 mBufferFilled.signal(); 1530} 1531 1532void OMXCodec::setRawAudioFormat( 1533 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) { 1534 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams; 1535 InitOMXParams(&pcmParams); 1536 pcmParams.nPortIndex = portIndex; 1537 1538 status_t err = mOMX->get_parameter( 1539 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); 1540 1541 CHECK_EQ(err, OK); 1542 1543 pcmParams.nChannels = numChannels; 1544 pcmParams.eNumData = OMX_NumericalDataSigned; 1545 pcmParams.bInterleaved = OMX_TRUE; 1546 pcmParams.nBitPerSample = 16; 1547 pcmParams.nSamplingRate = sampleRate; 1548 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear; 1549 1550 if (numChannels == 1) { 1551 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF; 1552 } else { 1553 CHECK_EQ(numChannels, 2); 1554 1555 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF; 1556 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF; 1557 } 1558 1559 err = mOMX->set_parameter( 1560 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); 1561 1562 CHECK_EQ(err, OK); 1563} 1564 1565void OMXCodec::setAMRFormat() { 1566 if (!mIsEncoder) { 1567 OMX_AUDIO_PARAM_AMRTYPE def; 1568 InitOMXParams(&def); 1569 def.nPortIndex = kPortIndexInput; 1570 1571 status_t err = 1572 mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 1573 1574 CHECK_EQ(err, OK); 1575 1576 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF; 1577 def.eAMRBandMode = OMX_AUDIO_AMRBandModeNB0; 1578 1579 err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 1580 CHECK_EQ(err, OK); 1581 } 1582 1583 //////////////////////// 1584 1585 if (mIsEncoder) { 1586 sp<MetaData> format = mSource->getFormat(); 1587 int32_t sampleRate; 1588 int32_t numChannels; 1589 CHECK(format->findInt32(kKeySampleRate, &sampleRate)); 1590 CHECK(format->findInt32(kKeyChannelCount, &numChannels)); 1591 1592 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 1593 } 1594} 1595 1596void OMXCodec::setAMRWBFormat() { 1597 if (!mIsEncoder) { 1598 OMX_AUDIO_PARAM_AMRTYPE def; 1599 InitOMXParams(&def); 1600 def.nPortIndex = kPortIndexInput; 1601 1602 status_t err = 1603 mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 1604 1605 CHECK_EQ(err, OK); 1606 1607 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF; 1608 def.eAMRBandMode = OMX_AUDIO_AMRBandModeWB0; 1609 1610 err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 1611 CHECK_EQ(err, OK); 1612 } 1613 1614 //////////////////////// 1615 1616 if (mIsEncoder) { 1617 sp<MetaData> format = mSource->getFormat(); 1618 int32_t sampleRate; 1619 int32_t numChannels; 1620 CHECK(format->findInt32(kKeySampleRate, &sampleRate)); 1621 CHECK(format->findInt32(kKeyChannelCount, &numChannels)); 1622 1623 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 1624 } 1625} 1626 1627void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) { 1628 if (mIsEncoder) { 1629 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 1630 } else { 1631 OMX_AUDIO_PARAM_AACPROFILETYPE profile; 1632 InitOMXParams(&profile); 1633 profile.nPortIndex = kPortIndexInput; 1634 1635 status_t err = mOMX->get_parameter( 1636 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); 1637 CHECK_EQ(err, OK); 1638 1639 profile.nChannels = numChannels; 1640 profile.nSampleRate = sampleRate; 1641 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS; 1642 1643 err = mOMX->set_parameter( 1644 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); 1645 CHECK_EQ(err, OK); 1646 } 1647} 1648 1649void OMXCodec::setImageOutputFormat( 1650 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) { 1651 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height); 1652 1653#if 0 1654 OMX_INDEXTYPE index; 1655 status_t err = mOMX->get_extension_index( 1656 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index); 1657 CHECK_EQ(err, OK); 1658 1659 err = mOMX->set_config(mNode, index, &format, sizeof(format)); 1660 CHECK_EQ(err, OK); 1661#endif 1662 1663 OMX_PARAM_PORTDEFINITIONTYPE def; 1664 InitOMXParams(&def); 1665 def.nPortIndex = kPortIndexOutput; 1666 1667 status_t err = mOMX->get_parameter( 1668 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1669 CHECK_EQ(err, OK); 1670 1671 CHECK_EQ(def.eDomain, OMX_PortDomainImage); 1672 1673 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 1674 1675 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused); 1676 imageDef->eColorFormat = format; 1677 imageDef->nFrameWidth = width; 1678 imageDef->nFrameHeight = height; 1679 1680 switch (format) { 1681 case OMX_COLOR_FormatYUV420PackedPlanar: 1682 case OMX_COLOR_FormatYUV411Planar: 1683 { 1684 def.nBufferSize = (width * height * 3) / 2; 1685 break; 1686 } 1687 1688 case OMX_COLOR_FormatCbYCrY: 1689 { 1690 def.nBufferSize = width * height * 2; 1691 break; 1692 } 1693 1694 case OMX_COLOR_Format32bitARGB8888: 1695 { 1696 def.nBufferSize = width * height * 4; 1697 break; 1698 } 1699 1700 case OMX_COLOR_Format16bitARGB4444: 1701 case OMX_COLOR_Format16bitARGB1555: 1702 case OMX_COLOR_Format16bitRGB565: 1703 case OMX_COLOR_Format16bitBGR565: 1704 { 1705 def.nBufferSize = width * height * 2; 1706 break; 1707 } 1708 1709 default: 1710 CHECK(!"Should not be here. Unknown color format."); 1711 break; 1712 } 1713 1714 def.nBufferCountActual = def.nBufferCountMin; 1715 1716 err = mOMX->set_parameter( 1717 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1718 CHECK_EQ(err, OK); 1719} 1720 1721void OMXCodec::setJPEGInputFormat( 1722 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) { 1723 OMX_PARAM_PORTDEFINITIONTYPE def; 1724 InitOMXParams(&def); 1725 def.nPortIndex = kPortIndexInput; 1726 1727 status_t err = mOMX->get_parameter( 1728 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1729 CHECK_EQ(err, OK); 1730 1731 CHECK_EQ(def.eDomain, OMX_PortDomainImage); 1732 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 1733 1734 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG); 1735 imageDef->nFrameWidth = width; 1736 imageDef->nFrameHeight = height; 1737 1738 def.nBufferSize = compressedSize; 1739 def.nBufferCountActual = def.nBufferCountMin; 1740 1741 err = mOMX->set_parameter( 1742 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1743 CHECK_EQ(err, OK); 1744} 1745 1746void OMXCodec::addCodecSpecificData(const void *data, size_t size) { 1747 CodecSpecificData *specific = 1748 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1); 1749 1750 specific->mSize = size; 1751 memcpy(specific->mData, data, size); 1752 1753 mCodecSpecificData.push(specific); 1754} 1755 1756void OMXCodec::clearCodecSpecificData() { 1757 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) { 1758 free(mCodecSpecificData.editItemAt(i)); 1759 } 1760 mCodecSpecificData.clear(); 1761 mCodecSpecificDataIndex = 0; 1762} 1763 1764status_t OMXCodec::start(MetaData *) { 1765 Mutex::Autolock autoLock(mLock); 1766 1767 if (mState != LOADED) { 1768 return UNKNOWN_ERROR; 1769 } 1770 1771 sp<MetaData> params = new MetaData; 1772 if (mQuirks & kWantsNALFragments) { 1773 params->setInt32(kKeyWantsNALFragments, true); 1774 } 1775 status_t err = mSource->start(params.get()); 1776 1777 if (err != OK) { 1778 return err; 1779 } 1780 1781 mCodecSpecificDataIndex = 0; 1782 mInitialBufferSubmit = true; 1783 mSignalledEOS = false; 1784 mNoMoreOutputData = false; 1785 mSeekTimeUs = -1; 1786 mFilledBuffers.clear(); 1787 1788 return init(); 1789} 1790 1791status_t OMXCodec::stop() { 1792 CODEC_LOGV("stop"); 1793 1794 Mutex::Autolock autoLock(mLock); 1795 1796 while (isIntermediateState(mState)) { 1797 mAsyncCompletion.wait(mLock); 1798 } 1799 1800 switch (mState) { 1801 case LOADED: 1802 case ERROR: 1803 break; 1804 1805 case EXECUTING: 1806 { 1807 setState(EXECUTING_TO_IDLE); 1808 1809 if (mQuirks & kRequiresFlushBeforeShutdown) { 1810 CODEC_LOGV("This component requires a flush before transitioning " 1811 "from EXECUTING to IDLE..."); 1812 1813 bool emulateInputFlushCompletion = 1814 !flushPortAsync(kPortIndexInput); 1815 1816 bool emulateOutputFlushCompletion = 1817 !flushPortAsync(kPortIndexOutput); 1818 1819 if (emulateInputFlushCompletion) { 1820 onCmdComplete(OMX_CommandFlush, kPortIndexInput); 1821 } 1822 1823 if (emulateOutputFlushCompletion) { 1824 onCmdComplete(OMX_CommandFlush, kPortIndexOutput); 1825 } 1826 } else { 1827 mPortStatus[kPortIndexInput] = SHUTTING_DOWN; 1828 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; 1829 1830 status_t err = 1831 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle); 1832 CHECK_EQ(err, OK); 1833 } 1834 1835 while (mState != LOADED && mState != ERROR) { 1836 mAsyncCompletion.wait(mLock); 1837 } 1838 1839 break; 1840 } 1841 1842 default: 1843 { 1844 CHECK(!"should not be here."); 1845 break; 1846 } 1847 } 1848 1849 mSource->stop(); 1850 1851 return OK; 1852} 1853 1854sp<MetaData> OMXCodec::getFormat() { 1855 return mOutputFormat; 1856} 1857 1858status_t OMXCodec::read( 1859 MediaBuffer **buffer, const ReadOptions *options) { 1860 *buffer = NULL; 1861 1862 Mutex::Autolock autoLock(mLock); 1863 1864 if (mState != EXECUTING && mState != RECONFIGURING) { 1865 return UNKNOWN_ERROR; 1866 } 1867 1868 if (mInitialBufferSubmit) { 1869 mInitialBufferSubmit = false; 1870 1871 drainInputBuffers(); 1872 1873 if (mState == EXECUTING) { 1874 // Otherwise mState == RECONFIGURING and this code will trigger 1875 // after the output port is reenabled. 1876 fillOutputBuffers(); 1877 } 1878 } 1879 1880 int64_t seekTimeUs; 1881 if (options && options->getSeekTo(&seekTimeUs)) { 1882 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6); 1883 1884 mSignalledEOS = false; 1885 mNoMoreOutputData = false; 1886 1887 CHECK(seekTimeUs >= 0); 1888 mSeekTimeUs = seekTimeUs; 1889 1890 mFilledBuffers.clear(); 1891 1892 CHECK_EQ(mState, EXECUTING); 1893 1894 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput); 1895 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput); 1896 1897 if (emulateInputFlushCompletion) { 1898 onCmdComplete(OMX_CommandFlush, kPortIndexInput); 1899 } 1900 1901 if (emulateOutputFlushCompletion) { 1902 onCmdComplete(OMX_CommandFlush, kPortIndexOutput); 1903 } 1904 } 1905 1906 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) { 1907 mBufferFilled.wait(mLock); 1908 } 1909 1910 if (mState == ERROR) { 1911 return UNKNOWN_ERROR; 1912 } 1913 1914 if (mFilledBuffers.empty()) { 1915 return ERROR_END_OF_STREAM; 1916 } 1917 1918 size_t index = *mFilledBuffers.begin(); 1919 mFilledBuffers.erase(mFilledBuffers.begin()); 1920 1921 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index); 1922 info->mMediaBuffer->add_ref(); 1923 *buffer = info->mMediaBuffer; 1924 1925 return OK; 1926} 1927 1928void OMXCodec::signalBufferReturned(MediaBuffer *buffer) { 1929 Mutex::Autolock autoLock(mLock); 1930 1931 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 1932 for (size_t i = 0; i < buffers->size(); ++i) { 1933 BufferInfo *info = &buffers->editItemAt(i); 1934 1935 if (info->mMediaBuffer == buffer) { 1936 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED); 1937 fillOutputBuffer(info); 1938 return; 1939 } 1940 } 1941 1942 CHECK(!"should not be here."); 1943} 1944 1945static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) { 1946 static const char *kNames[] = { 1947 "OMX_IMAGE_CodingUnused", 1948 "OMX_IMAGE_CodingAutoDetect", 1949 "OMX_IMAGE_CodingJPEG", 1950 "OMX_IMAGE_CodingJPEG2K", 1951 "OMX_IMAGE_CodingEXIF", 1952 "OMX_IMAGE_CodingTIFF", 1953 "OMX_IMAGE_CodingGIF", 1954 "OMX_IMAGE_CodingPNG", 1955 "OMX_IMAGE_CodingLZW", 1956 "OMX_IMAGE_CodingBMP", 1957 }; 1958 1959 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 1960 1961 if (type < 0 || (size_t)type >= numNames) { 1962 return "UNKNOWN"; 1963 } else { 1964 return kNames[type]; 1965 } 1966} 1967 1968static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) { 1969 static const char *kNames[] = { 1970 "OMX_COLOR_FormatUnused", 1971 "OMX_COLOR_FormatMonochrome", 1972 "OMX_COLOR_Format8bitRGB332", 1973 "OMX_COLOR_Format12bitRGB444", 1974 "OMX_COLOR_Format16bitARGB4444", 1975 "OMX_COLOR_Format16bitARGB1555", 1976 "OMX_COLOR_Format16bitRGB565", 1977 "OMX_COLOR_Format16bitBGR565", 1978 "OMX_COLOR_Format18bitRGB666", 1979 "OMX_COLOR_Format18bitARGB1665", 1980 "OMX_COLOR_Format19bitARGB1666", 1981 "OMX_COLOR_Format24bitRGB888", 1982 "OMX_COLOR_Format24bitBGR888", 1983 "OMX_COLOR_Format24bitARGB1887", 1984 "OMX_COLOR_Format25bitARGB1888", 1985 "OMX_COLOR_Format32bitBGRA8888", 1986 "OMX_COLOR_Format32bitARGB8888", 1987 "OMX_COLOR_FormatYUV411Planar", 1988 "OMX_COLOR_FormatYUV411PackedPlanar", 1989 "OMX_COLOR_FormatYUV420Planar", 1990 "OMX_COLOR_FormatYUV420PackedPlanar", 1991 "OMX_COLOR_FormatYUV420SemiPlanar", 1992 "OMX_COLOR_FormatYUV422Planar", 1993 "OMX_COLOR_FormatYUV422PackedPlanar", 1994 "OMX_COLOR_FormatYUV422SemiPlanar", 1995 "OMX_COLOR_FormatYCbYCr", 1996 "OMX_COLOR_FormatYCrYCb", 1997 "OMX_COLOR_FormatCbYCrY", 1998 "OMX_COLOR_FormatCrYCbY", 1999 "OMX_COLOR_FormatYUV444Interleaved", 2000 "OMX_COLOR_FormatRawBayer8bit", 2001 "OMX_COLOR_FormatRawBayer10bit", 2002 "OMX_COLOR_FormatRawBayer8bitcompressed", 2003 "OMX_COLOR_FormatL2", 2004 "OMX_COLOR_FormatL4", 2005 "OMX_COLOR_FormatL8", 2006 "OMX_COLOR_FormatL16", 2007 "OMX_COLOR_FormatL24", 2008 "OMX_COLOR_FormatL32", 2009 "OMX_COLOR_FormatYUV420PackedSemiPlanar", 2010 "OMX_COLOR_FormatYUV422PackedSemiPlanar", 2011 "OMX_COLOR_Format18BitBGR666", 2012 "OMX_COLOR_Format24BitARGB6666", 2013 "OMX_COLOR_Format24BitABGR6666", 2014 }; 2015 2016 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2017 2018 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) { 2019 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar"; 2020 } else if (type < 0 || (size_t)type >= numNames) { 2021 return "UNKNOWN"; 2022 } else { 2023 return kNames[type]; 2024 } 2025} 2026 2027static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) { 2028 static const char *kNames[] = { 2029 "OMX_VIDEO_CodingUnused", 2030 "OMX_VIDEO_CodingAutoDetect", 2031 "OMX_VIDEO_CodingMPEG2", 2032 "OMX_VIDEO_CodingH263", 2033 "OMX_VIDEO_CodingMPEG4", 2034 "OMX_VIDEO_CodingWMV", 2035 "OMX_VIDEO_CodingRV", 2036 "OMX_VIDEO_CodingAVC", 2037 "OMX_VIDEO_CodingMJPEG", 2038 }; 2039 2040 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2041 2042 if (type < 0 || (size_t)type >= numNames) { 2043 return "UNKNOWN"; 2044 } else { 2045 return kNames[type]; 2046 } 2047} 2048 2049static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) { 2050 static const char *kNames[] = { 2051 "OMX_AUDIO_CodingUnused", 2052 "OMX_AUDIO_CodingAutoDetect", 2053 "OMX_AUDIO_CodingPCM", 2054 "OMX_AUDIO_CodingADPCM", 2055 "OMX_AUDIO_CodingAMR", 2056 "OMX_AUDIO_CodingGSMFR", 2057 "OMX_AUDIO_CodingGSMEFR", 2058 "OMX_AUDIO_CodingGSMHR", 2059 "OMX_AUDIO_CodingPDCFR", 2060 "OMX_AUDIO_CodingPDCEFR", 2061 "OMX_AUDIO_CodingPDCHR", 2062 "OMX_AUDIO_CodingTDMAFR", 2063 "OMX_AUDIO_CodingTDMAEFR", 2064 "OMX_AUDIO_CodingQCELP8", 2065 "OMX_AUDIO_CodingQCELP13", 2066 "OMX_AUDIO_CodingEVRC", 2067 "OMX_AUDIO_CodingSMV", 2068 "OMX_AUDIO_CodingG711", 2069 "OMX_AUDIO_CodingG723", 2070 "OMX_AUDIO_CodingG726", 2071 "OMX_AUDIO_CodingG729", 2072 "OMX_AUDIO_CodingAAC", 2073 "OMX_AUDIO_CodingMP3", 2074 "OMX_AUDIO_CodingSBC", 2075 "OMX_AUDIO_CodingVORBIS", 2076 "OMX_AUDIO_CodingWMA", 2077 "OMX_AUDIO_CodingRA", 2078 "OMX_AUDIO_CodingMIDI", 2079 }; 2080 2081 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2082 2083 if (type < 0 || (size_t)type >= numNames) { 2084 return "UNKNOWN"; 2085 } else { 2086 return kNames[type]; 2087 } 2088} 2089 2090static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) { 2091 static const char *kNames[] = { 2092 "OMX_AUDIO_PCMModeLinear", 2093 "OMX_AUDIO_PCMModeALaw", 2094 "OMX_AUDIO_PCMModeMULaw", 2095 }; 2096 2097 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2098 2099 if (type < 0 || (size_t)type >= numNames) { 2100 return "UNKNOWN"; 2101 } else { 2102 return kNames[type]; 2103 } 2104} 2105 2106static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) { 2107 static const char *kNames[] = { 2108 "OMX_AUDIO_AMRBandModeUnused", 2109 "OMX_AUDIO_AMRBandModeNB0", 2110 "OMX_AUDIO_AMRBandModeNB1", 2111 "OMX_AUDIO_AMRBandModeNB2", 2112 "OMX_AUDIO_AMRBandModeNB3", 2113 "OMX_AUDIO_AMRBandModeNB4", 2114 "OMX_AUDIO_AMRBandModeNB5", 2115 "OMX_AUDIO_AMRBandModeNB6", 2116 "OMX_AUDIO_AMRBandModeNB7", 2117 "OMX_AUDIO_AMRBandModeWB0", 2118 "OMX_AUDIO_AMRBandModeWB1", 2119 "OMX_AUDIO_AMRBandModeWB2", 2120 "OMX_AUDIO_AMRBandModeWB3", 2121 "OMX_AUDIO_AMRBandModeWB4", 2122 "OMX_AUDIO_AMRBandModeWB5", 2123 "OMX_AUDIO_AMRBandModeWB6", 2124 "OMX_AUDIO_AMRBandModeWB7", 2125 "OMX_AUDIO_AMRBandModeWB8", 2126 }; 2127 2128 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2129 2130 if (type < 0 || (size_t)type >= numNames) { 2131 return "UNKNOWN"; 2132 } else { 2133 return kNames[type]; 2134 } 2135} 2136 2137static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) { 2138 static const char *kNames[] = { 2139 "OMX_AUDIO_AMRFrameFormatConformance", 2140 "OMX_AUDIO_AMRFrameFormatIF1", 2141 "OMX_AUDIO_AMRFrameFormatIF2", 2142 "OMX_AUDIO_AMRFrameFormatFSF", 2143 "OMX_AUDIO_AMRFrameFormatRTPPayload", 2144 "OMX_AUDIO_AMRFrameFormatITU", 2145 }; 2146 2147 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 2148 2149 if (type < 0 || (size_t)type >= numNames) { 2150 return "UNKNOWN"; 2151 } else { 2152 return kNames[type]; 2153 } 2154} 2155 2156void OMXCodec::dumpPortStatus(OMX_U32 portIndex) { 2157 OMX_PARAM_PORTDEFINITIONTYPE def; 2158 InitOMXParams(&def); 2159 def.nPortIndex = portIndex; 2160 2161 status_t err = mOMX->get_parameter( 2162 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2163 CHECK_EQ(err, OK); 2164 2165 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output"); 2166 2167 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput) 2168 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput)); 2169 2170 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual); 2171 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin); 2172 printf(" nBufferSize = %ld\n", def.nBufferSize); 2173 2174 switch (def.eDomain) { 2175 case OMX_PortDomainImage: 2176 { 2177 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 2178 2179 printf("\n"); 2180 printf(" // Image\n"); 2181 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth); 2182 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight); 2183 printf(" nStride = %ld\n", imageDef->nStride); 2184 2185 printf(" eCompressionFormat = %s\n", 2186 imageCompressionFormatString(imageDef->eCompressionFormat)); 2187 2188 printf(" eColorFormat = %s\n", 2189 colorFormatString(imageDef->eColorFormat)); 2190 2191 break; 2192 } 2193 2194 case OMX_PortDomainVideo: 2195 { 2196 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video; 2197 2198 printf("\n"); 2199 printf(" // Video\n"); 2200 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth); 2201 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight); 2202 printf(" nStride = %ld\n", videoDef->nStride); 2203 2204 printf(" eCompressionFormat = %s\n", 2205 videoCompressionFormatString(videoDef->eCompressionFormat)); 2206 2207 printf(" eColorFormat = %s\n", 2208 colorFormatString(videoDef->eColorFormat)); 2209 2210 break; 2211 } 2212 2213 case OMX_PortDomainAudio: 2214 { 2215 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio; 2216 2217 printf("\n"); 2218 printf(" // Audio\n"); 2219 printf(" eEncoding = %s\n", 2220 audioCodingTypeString(audioDef->eEncoding)); 2221 2222 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) { 2223 OMX_AUDIO_PARAM_PCMMODETYPE params; 2224 InitOMXParams(¶ms); 2225 params.nPortIndex = portIndex; 2226 2227 err = mOMX->get_parameter( 2228 mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); 2229 CHECK_EQ(err, OK); 2230 2231 printf(" nSamplingRate = %ld\n", params.nSamplingRate); 2232 printf(" nChannels = %ld\n", params.nChannels); 2233 printf(" bInterleaved = %d\n", params.bInterleaved); 2234 printf(" nBitPerSample = %ld\n", params.nBitPerSample); 2235 2236 printf(" eNumData = %s\n", 2237 params.eNumData == OMX_NumericalDataSigned 2238 ? "signed" : "unsigned"); 2239 2240 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode)); 2241 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) { 2242 OMX_AUDIO_PARAM_AMRTYPE amr; 2243 InitOMXParams(&amr); 2244 amr.nPortIndex = portIndex; 2245 2246 err = mOMX->get_parameter( 2247 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); 2248 CHECK_EQ(err, OK); 2249 2250 printf(" nChannels = %ld\n", amr.nChannels); 2251 printf(" eAMRBandMode = %s\n", 2252 amrBandModeString(amr.eAMRBandMode)); 2253 printf(" eAMRFrameFormat = %s\n", 2254 amrFrameFormatString(amr.eAMRFrameFormat)); 2255 } 2256 2257 break; 2258 } 2259 2260 default: 2261 { 2262 printf(" // Unknown\n"); 2263 break; 2264 } 2265 } 2266 2267 printf("}\n"); 2268} 2269 2270void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) { 2271 mOutputFormat = new MetaData; 2272 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName); 2273 2274 OMX_PARAM_PORTDEFINITIONTYPE def; 2275 InitOMXParams(&def); 2276 def.nPortIndex = kPortIndexOutput; 2277 2278 status_t err = mOMX->get_parameter( 2279 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2280 CHECK_EQ(err, OK); 2281 2282 switch (def.eDomain) { 2283 case OMX_PortDomainImage: 2284 { 2285 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 2286 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused); 2287 2288 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 2289 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat); 2290 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth); 2291 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight); 2292 break; 2293 } 2294 2295 case OMX_PortDomainAudio: 2296 { 2297 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio; 2298 2299 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) { 2300 OMX_AUDIO_PARAM_PCMMODETYPE params; 2301 InitOMXParams(¶ms); 2302 params.nPortIndex = kPortIndexOutput; 2303 2304 err = mOMX->get_parameter( 2305 mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); 2306 CHECK_EQ(err, OK); 2307 2308 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned); 2309 CHECK_EQ(params.nBitPerSample, 16); 2310 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear); 2311 2312 int32_t numChannels, sampleRate; 2313 inputFormat->findInt32(kKeyChannelCount, &numChannels); 2314 inputFormat->findInt32(kKeySampleRate, &sampleRate); 2315 2316 if ((OMX_U32)numChannels != params.nChannels) { 2317 LOGW("Codec outputs a different number of channels than " 2318 "the input stream contains."); 2319 } 2320 2321 mOutputFormat->setCString( 2322 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); 2323 2324 // Use the codec-advertised number of channels, as some 2325 // codecs appear to output stereo even if the input data is 2326 // mono. 2327 mOutputFormat->setInt32(kKeyChannelCount, params.nChannels); 2328 2329 // The codec-reported sampleRate is not reliable... 2330 mOutputFormat->setInt32(kKeySampleRate, sampleRate); 2331 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) { 2332 OMX_AUDIO_PARAM_AMRTYPE amr; 2333 InitOMXParams(&amr); 2334 amr.nPortIndex = kPortIndexOutput; 2335 2336 err = mOMX->get_parameter( 2337 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); 2338 CHECK_EQ(err, OK); 2339 2340 CHECK_EQ(amr.nChannels, 1); 2341 mOutputFormat->setInt32(kKeyChannelCount, 1); 2342 2343 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0 2344 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) { 2345 mOutputFormat->setCString( 2346 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB); 2347 mOutputFormat->setInt32(kKeySampleRate, 8000); 2348 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0 2349 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) { 2350 mOutputFormat->setCString( 2351 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB); 2352 mOutputFormat->setInt32(kKeySampleRate, 16000); 2353 } else { 2354 CHECK(!"Unknown AMR band mode."); 2355 } 2356 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) { 2357 mOutputFormat->setCString( 2358 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC); 2359 } else { 2360 CHECK(!"Should not be here. Unknown audio encoding."); 2361 } 2362 break; 2363 } 2364 2365 case OMX_PortDomainVideo: 2366 { 2367 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 2368 2369 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) { 2370 mOutputFormat->setCString( 2371 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 2372 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) { 2373 mOutputFormat->setCString( 2374 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); 2375 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) { 2376 mOutputFormat->setCString( 2377 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); 2378 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) { 2379 mOutputFormat->setCString( 2380 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 2381 } else { 2382 CHECK(!"Unknown compression format."); 2383 } 2384 2385 if (!strcmp(mComponentName, "OMX.PV.avcdec")) { 2386 // This component appears to be lying to me. 2387 mOutputFormat->setInt32( 2388 kKeyWidth, (video_def->nFrameWidth + 15) & -16); 2389 mOutputFormat->setInt32( 2390 kKeyHeight, (video_def->nFrameHeight + 15) & -16); 2391 } else { 2392 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth); 2393 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight); 2394 } 2395 2396 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat); 2397 break; 2398 } 2399 2400 default: 2401 { 2402 CHECK(!"should not be here, neither audio nor video."); 2403 break; 2404 } 2405 } 2406} 2407 2408//////////////////////////////////////////////////////////////////////////////// 2409 2410status_t QueryCodecs( 2411 const sp<IOMX> &omx, 2412 const char *mime, bool queryDecoders, 2413 Vector<CodecCapabilities> *results) { 2414 results->clear(); 2415 2416 for (int index = 0;; ++index) { 2417 const char *componentName; 2418 2419 if (!queryDecoders) { 2420 componentName = GetCodec( 2421 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]), 2422 mime, index); 2423 } else { 2424 componentName = GetCodec( 2425 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]), 2426 mime, index); 2427 } 2428 2429 if (!componentName) { 2430 return OK; 2431 } 2432 2433 IOMX::node_id node; 2434 status_t err = omx->allocate_node(componentName, &node); 2435 2436 if (err != OK) { 2437 continue; 2438 } 2439 2440 OMXCodec::setComponentRole(omx, node, queryDecoders, mime); 2441 2442 results->push(); 2443 CodecCapabilities *caps = &results->editItemAt(results->size() - 1); 2444 caps->mComponentName = componentName; 2445 2446 OMX_VIDEO_PARAM_PROFILELEVELTYPE param; 2447 InitOMXParams(¶m); 2448 2449 param.nPortIndex = queryDecoders ? 0 : 1; 2450 2451 for (param.nProfileIndex = 0;; ++param.nProfileIndex) { 2452 err = omx->get_parameter( 2453 node, OMX_IndexParamVideoProfileLevelQuerySupported, 2454 ¶m, sizeof(param)); 2455 2456 if (err != OK) { 2457 break; 2458 } 2459 2460 CodecProfileLevel profileLevel; 2461 profileLevel.mProfile = param.eProfile; 2462 profileLevel.mLevel = param.eLevel; 2463 2464 caps->mProfileLevels.push(profileLevel); 2465 } 2466 2467 CHECK_EQ(omx->free_node(node), OK); 2468 } 2469} 2470 2471} // namespace android 2472