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