1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 //#define LOG_NDEBUG 0 18 #define LOG_TAG "OMXCodec" 19 #include <utils/Log.h> 20 21 #include "include/AACDecoder.h" 22 #include "include/AACEncoder.h" 23 #include "include/AMRNBDecoder.h" 24 #include "include/AMRNBEncoder.h" 25 #include "include/AMRWBDecoder.h" 26 #include "include/AMRWBEncoder.h" 27 #include "include/AVCDecoder.h" 28 #include "include/AVCEncoder.h" 29 #include "include/G711Decoder.h" 30 #include "include/M4vH263Decoder.h" 31 #include "include/M4vH263Encoder.h" 32 #include "include/MP3Decoder.h" 33 #include "include/VorbisDecoder.h" 34 #include "include/VPXDecoder.h" 35 36 #include "include/ESDS.h" 37 38 #include <binder/IServiceManager.h> 39 #include <binder/MemoryDealer.h> 40 #include <binder/ProcessState.h> 41 #include <media/IMediaPlayerService.h> 42 #include <media/stagefright/MediaBuffer.h> 43 #include <media/stagefright/MediaBufferGroup.h> 44 #include <media/stagefright/MediaDebug.h> 45 #include <media/stagefright/MediaDefs.h> 46 #include <media/stagefright/MediaExtractor.h> 47 #include <media/stagefright/MetaData.h> 48 #include <media/stagefright/OMXCodec.h> 49 #include <media/stagefright/Utils.h> 50 #include <utils/Vector.h> 51 52 #include <OMX_Audio.h> 53 #include <OMX_Component.h> 54 55 #include "include/ThreadedSource.h" 56 57 namespace android { 58 59 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00; 60 61 struct CodecInfo { 62 const char *mime; 63 const char *codec; 64 }; 65 66 #define FACTORY_CREATE(name) \ 67 static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \ 68 return new name(source); \ 69 } 70 71 #define FACTORY_CREATE_ENCODER(name) \ 72 static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \ 73 return new name(source, meta); \ 74 } 75 76 #define FACTORY_REF(name) { #name, Make##name }, 77 78 FACTORY_CREATE(MP3Decoder) 79 FACTORY_CREATE(AMRNBDecoder) 80 FACTORY_CREATE(AMRWBDecoder) 81 FACTORY_CREATE(AACDecoder) 82 FACTORY_CREATE(AVCDecoder) 83 FACTORY_CREATE(G711Decoder) 84 FACTORY_CREATE(M4vH263Decoder) 85 FACTORY_CREATE(VorbisDecoder) 86 FACTORY_CREATE(VPXDecoder) 87 FACTORY_CREATE_ENCODER(AMRNBEncoder) 88 FACTORY_CREATE_ENCODER(AMRWBEncoder) 89 FACTORY_CREATE_ENCODER(AACEncoder) 90 FACTORY_CREATE_ENCODER(AVCEncoder) 91 FACTORY_CREATE_ENCODER(M4vH263Encoder) 92 93 static sp<MediaSource> InstantiateSoftwareEncoder( 94 const char *name, const sp<MediaSource> &source, 95 const sp<MetaData> &meta) { 96 struct FactoryInfo { 97 const char *name; 98 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &); 99 }; 100 101 static const FactoryInfo kFactoryInfo[] = { 102 FACTORY_REF(AMRNBEncoder) 103 FACTORY_REF(AMRWBEncoder) 104 FACTORY_REF(AACEncoder) 105 FACTORY_REF(AVCEncoder) 106 FACTORY_REF(M4vH263Encoder) 107 }; 108 for (size_t i = 0; 109 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) { 110 if (!strcmp(name, kFactoryInfo[i].name)) { 111 return (*kFactoryInfo[i].CreateFunc)(source, meta); 112 } 113 } 114 115 return NULL; 116 } 117 118 static sp<MediaSource> InstantiateSoftwareCodec( 119 const char *name, const sp<MediaSource> &source) { 120 struct FactoryInfo { 121 const char *name; 122 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &); 123 }; 124 125 static const FactoryInfo kFactoryInfo[] = { 126 FACTORY_REF(MP3Decoder) 127 FACTORY_REF(AMRNBDecoder) 128 FACTORY_REF(AMRWBDecoder) 129 FACTORY_REF(AACDecoder) 130 FACTORY_REF(AVCDecoder) 131 FACTORY_REF(G711Decoder) 132 FACTORY_REF(M4vH263Decoder) 133 FACTORY_REF(VorbisDecoder) 134 FACTORY_REF(VPXDecoder) 135 }; 136 for (size_t i = 0; 137 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) { 138 if (!strcmp(name, kFactoryInfo[i].name)) { 139 if (!strcmp(name, "VPXDecoder")) { 140 return new ThreadedSource( 141 (*kFactoryInfo[i].CreateFunc)(source)); 142 } 143 return (*kFactoryInfo[i].CreateFunc)(source); 144 } 145 } 146 147 return NULL; 148 } 149 150 #undef FACTORY_REF 151 #undef FACTORY_CREATE 152 153 static const CodecInfo kDecoderInfo[] = { 154 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" }, 155 // { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" }, 156 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" }, 157 // { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" }, 158 // { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" }, 159 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" }, 160 // { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" }, 161 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" }, 162 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" }, 163 // { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" }, 164 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" }, 165 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" }, 166 // { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" }, 167 { MEDIA_MIMETYPE_AUDIO_G711_ALAW, "G711Decoder" }, 168 { MEDIA_MIMETYPE_AUDIO_G711_MLAW, "G711Decoder" }, 169 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" }, 170 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" }, 171 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" }, 172 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Decoder" }, 173 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" }, 174 // { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" }, 175 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" }, 176 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" }, 177 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Decoder" }, 178 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" }, 179 // { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" }, 180 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" }, 181 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" }, 182 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" }, 183 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Decoder" }, 184 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" }, 185 // { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" }, 186 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" }, 187 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" }, 188 }; 189 190 static const CodecInfo kEncoderInfo[] = { 191 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" }, 192 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" }, 193 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" }, 194 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" }, 195 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" }, 196 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" }, 197 // { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" }, 198 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" }, 199 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" }, 200 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" }, 201 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Encoder" }, 202 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" }, 203 // { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" }, 204 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" }, 205 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" }, 206 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" }, 207 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Encoder" }, 208 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" }, 209 // { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" }, 210 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" }, 211 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" }, 212 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" }, 213 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Encoder" }, 214 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" }, 215 // { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" }, 216 }; 217 218 #undef OPTIONAL 219 220 #define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__) 221 #define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__) 222 #define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__) 223 224 struct OMXCodecObserver : public BnOMXObserver { 225 OMXCodecObserver() { 226 } 227 228 void setCodec(const sp<OMXCodec> &target) { 229 mTarget = target; 230 } 231 232 // from IOMXObserver 233 virtual void onMessage(const omx_message &msg) { 234 sp<OMXCodec> codec = mTarget.promote(); 235 236 if (codec.get() != NULL) { 237 codec->on_message(msg); 238 } 239 } 240 241 protected: 242 virtual ~OMXCodecObserver() {} 243 244 private: 245 wp<OMXCodec> mTarget; 246 247 OMXCodecObserver(const OMXCodecObserver &); 248 OMXCodecObserver &operator=(const OMXCodecObserver &); 249 }; 250 251 static const char *GetCodec(const CodecInfo *info, size_t numInfos, 252 const char *mime, int index) { 253 CHECK(index >= 0); 254 for(size_t i = 0; i < numInfos; ++i) { 255 if (!strcasecmp(mime, info[i].mime)) { 256 if (index == 0) { 257 return info[i].codec; 258 } 259 260 --index; 261 } 262 } 263 264 return NULL; 265 } 266 267 enum { 268 kAVCProfileBaseline = 0x42, 269 kAVCProfileMain = 0x4d, 270 kAVCProfileExtended = 0x58, 271 kAVCProfileHigh = 0x64, 272 kAVCProfileHigh10 = 0x6e, 273 kAVCProfileHigh422 = 0x7a, 274 kAVCProfileHigh444 = 0xf4, 275 kAVCProfileCAVLC444Intra = 0x2c 276 }; 277 278 static const char *AVCProfileToString(uint8_t profile) { 279 switch (profile) { 280 case kAVCProfileBaseline: 281 return "Baseline"; 282 case kAVCProfileMain: 283 return "Main"; 284 case kAVCProfileExtended: 285 return "Extended"; 286 case kAVCProfileHigh: 287 return "High"; 288 case kAVCProfileHigh10: 289 return "High 10"; 290 case kAVCProfileHigh422: 291 return "High 422"; 292 case kAVCProfileHigh444: 293 return "High 444"; 294 case kAVCProfileCAVLC444Intra: 295 return "CAVLC 444 Intra"; 296 default: return "Unknown"; 297 } 298 } 299 300 template<class T> 301 static void InitOMXParams(T *params) { 302 params->nSize = sizeof(T); 303 params->nVersion.s.nVersionMajor = 1; 304 params->nVersion.s.nVersionMinor = 0; 305 params->nVersion.s.nRevision = 0; 306 params->nVersion.s.nStep = 0; 307 } 308 309 static bool IsSoftwareCodec(const char *componentName) { 310 if (!strncmp("OMX.PV.", componentName, 7)) { 311 return true; 312 } 313 314 return false; 315 } 316 317 // A sort order in which non-OMX components are first, 318 // followed by software codecs, i.e. OMX.PV.*, followed 319 // by all the others. 320 static int CompareSoftwareCodecsFirst( 321 const String8 *elem1, const String8 *elem2) { 322 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4); 323 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4); 324 325 if (isNotOMX1) { 326 if (isNotOMX2) { return 0; } 327 return -1; 328 } 329 if (isNotOMX2) { 330 return 1; 331 } 332 333 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string()); 334 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string()); 335 336 if (isSoftwareCodec1) { 337 if (isSoftwareCodec2) { return 0; } 338 return -1; 339 } 340 341 if (isSoftwareCodec2) { 342 return 1; 343 } 344 345 return 0; 346 } 347 348 // static 349 uint32_t OMXCodec::getComponentQuirks( 350 const char *componentName, bool isEncoder) { 351 uint32_t quirks = 0; 352 353 if (!strcmp(componentName, "OMX.PV.avcdec")) { 354 quirks |= kWantsNALFragments; 355 } 356 if (!strcmp(componentName, "OMX.TI.MP3.decode")) { 357 quirks |= kNeedsFlushBeforeDisable; 358 quirks |= kDecoderLiesAboutNumberOfChannels; 359 } 360 if (!strcmp(componentName, "OMX.TI.AAC.decode")) { 361 quirks |= kNeedsFlushBeforeDisable; 362 quirks |= kRequiresFlushCompleteEmulation; 363 quirks |= kSupportsMultipleFramesPerInputBuffer; 364 } 365 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) { 366 quirks |= kRequiresLoadedToIdleAfterAllocation; 367 quirks |= kRequiresAllocateBufferOnInputPorts; 368 quirks |= kRequiresAllocateBufferOnOutputPorts; 369 if (!strncmp(componentName, "OMX.qcom.video.encoder.avc", 26)) { 370 371 // The AVC encoder advertises the size of output buffers 372 // based on the input video resolution and assumes 373 // the worst/least compression ratio is 0.5. It is found that 374 // sometimes, the output buffer size is larger than 375 // size advertised by the encoder. 376 quirks |= kRequiresLargerEncoderOutputBuffer; 377 } 378 } 379 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) { 380 } 381 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) { 382 quirks |= kRequiresAllocateBufferOnOutputPorts; 383 quirks |= kDefersOutputBufferAllocation; 384 } 385 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) { 386 quirks |= kRequiresAllocateBufferOnInputPorts; 387 quirks |= kRequiresAllocateBufferOnOutputPorts; 388 quirks |= kDefersOutputBufferAllocation; 389 } 390 391 if (!strncmp(componentName, "OMX.TI.", 7)) { 392 // Apparently I must not use OMX_UseBuffer on either input or 393 // output ports on any of the TI components or quote: 394 // "(I) may have unexpected problem (sic) which can be timing related 395 // and hard to reproduce." 396 397 quirks |= kRequiresAllocateBufferOnInputPorts; 398 quirks |= kRequiresAllocateBufferOnOutputPorts; 399 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) { 400 quirks |= kAvoidMemcopyInputRecordingFrames; 401 } 402 } 403 404 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) { 405 quirks |= kInputBufferSizesAreBogus; 406 } 407 408 if (!strncmp(componentName, "OMX.SEC.", 8) && !isEncoder) { 409 // These output buffers contain no video data, just some 410 // opaque information that allows the overlay to display their 411 // contents. 412 quirks |= kOutputBuffersAreUnreadable; 413 } 414 415 if (!strncmp(componentName, "OMX.SEC.", 8) && isEncoder) { 416 // These input buffers contain meta data (for instance, 417 // information helps locate the actual YUV data, or 418 // the physical address of the YUV data). 419 quirks |= kStoreMetaDataInInputVideoBuffers; 420 } 421 422 return quirks; 423 } 424 425 // static 426 void OMXCodec::findMatchingCodecs( 427 const char *mime, 428 bool createEncoder, const char *matchComponentName, 429 uint32_t flags, 430 Vector<String8> *matchingCodecs) { 431 matchingCodecs->clear(); 432 433 for (int index = 0;; ++index) { 434 const char *componentName; 435 436 if (createEncoder) { 437 componentName = GetCodec( 438 kEncoderInfo, 439 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]), 440 mime, index); 441 } else { 442 componentName = GetCodec( 443 kDecoderInfo, 444 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]), 445 mime, index); 446 } 447 448 if (!componentName) { 449 break; 450 } 451 452 // If a specific codec is requested, skip the non-matching ones. 453 if (matchComponentName && strcmp(componentName, matchComponentName)) { 454 continue; 455 } 456 457 matchingCodecs->push(String8(componentName)); 458 } 459 460 if (flags & kPreferSoftwareCodecs) { 461 matchingCodecs->sort(CompareSoftwareCodecsFirst); 462 } 463 } 464 465 // static 466 sp<MediaSource> OMXCodec::Create( 467 const sp<IOMX> &omx, 468 const sp<MetaData> &meta, bool createEncoder, 469 const sp<MediaSource> &source, 470 const char *matchComponentName, 471 uint32_t flags) { 472 const char *mime; 473 bool success = meta->findCString(kKeyMIMEType, &mime); 474 CHECK(success); 475 476 Vector<String8> matchingCodecs; 477 findMatchingCodecs( 478 mime, createEncoder, matchComponentName, flags, &matchingCodecs); 479 480 if (matchingCodecs.isEmpty()) { 481 return NULL; 482 } 483 484 sp<OMXCodecObserver> observer = new OMXCodecObserver; 485 IOMX::node_id node = 0; 486 487 const char *componentName; 488 for (size_t i = 0; i < matchingCodecs.size(); ++i) { 489 componentName = matchingCodecs[i].string(); 490 491 sp<MediaSource> softwareCodec = createEncoder? 492 InstantiateSoftwareEncoder(componentName, source, meta): 493 InstantiateSoftwareCodec(componentName, source); 494 495 if (softwareCodec != NULL) { 496 LOGV("Successfully allocated software codec '%s'", componentName); 497 498 return softwareCodec; 499 } 500 501 LOGV("Attempting to allocate OMX node '%s'", componentName); 502 503 uint32_t quirks = getComponentQuirks(componentName, createEncoder); 504 505 if (!createEncoder 506 && (quirks & kOutputBuffersAreUnreadable) 507 && (flags & kClientNeedsFramebuffer)) { 508 if (strncmp(componentName, "OMX.SEC.", 8)) { 509 // For OMX.SEC.* decoders we can enable a special mode that 510 // gives the client access to the framebuffer contents. 511 512 LOGW("Component '%s' does not give the client access to " 513 "the framebuffer contents. Skipping.", 514 componentName); 515 516 continue; 517 } 518 } 519 520 status_t err = omx->allocateNode(componentName, observer, &node); 521 if (err == OK) { 522 LOGV("Successfully allocated OMX node '%s'", componentName); 523 524 sp<OMXCodec> codec = new OMXCodec( 525 omx, node, quirks, 526 createEncoder, mime, componentName, 527 source); 528 529 observer->setCodec(codec); 530 531 err = codec->configureCodec(meta, flags); 532 533 if (err == OK) { 534 return codec; 535 } 536 537 LOGV("Failed to configure codec '%s'", componentName); 538 } 539 } 540 541 return NULL; 542 } 543 544 status_t OMXCodec::configureCodec(const sp<MetaData> &meta, uint32_t flags) { 545 if (!(flags & kIgnoreCodecSpecificData)) { 546 uint32_t type; 547 const void *data; 548 size_t size; 549 if (meta->findData(kKeyESDS, &type, &data, &size)) { 550 ESDS esds((const char *)data, size); 551 CHECK_EQ(esds.InitCheck(), OK); 552 553 const void *codec_specific_data; 554 size_t codec_specific_data_size; 555 esds.getCodecSpecificInfo( 556 &codec_specific_data, &codec_specific_data_size); 557 558 addCodecSpecificData( 559 codec_specific_data, codec_specific_data_size); 560 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) { 561 // Parse the AVCDecoderConfigurationRecord 562 563 const uint8_t *ptr = (const uint8_t *)data; 564 565 CHECK(size >= 7); 566 CHECK_EQ(ptr[0], 1); // configurationVersion == 1 567 uint8_t profile = ptr[1]; 568 uint8_t level = ptr[3]; 569 570 // There is decodable content out there that fails the following 571 // assertion, let's be lenient for now... 572 // CHECK((ptr[4] >> 2) == 0x3f); // reserved 573 574 size_t lengthSize = 1 + (ptr[4] & 3); 575 576 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp 577 // violates it... 578 // CHECK((ptr[5] >> 5) == 7); // reserved 579 580 size_t numSeqParameterSets = ptr[5] & 31; 581 582 ptr += 6; 583 size -= 6; 584 585 for (size_t i = 0; i < numSeqParameterSets; ++i) { 586 CHECK(size >= 2); 587 size_t length = U16_AT(ptr); 588 589 ptr += 2; 590 size -= 2; 591 592 CHECK(size >= length); 593 594 addCodecSpecificData(ptr, length); 595 596 ptr += length; 597 size -= length; 598 } 599 600 CHECK(size >= 1); 601 size_t numPictureParameterSets = *ptr; 602 ++ptr; 603 --size; 604 605 for (size_t i = 0; i < numPictureParameterSets; ++i) { 606 CHECK(size >= 2); 607 size_t length = U16_AT(ptr); 608 609 ptr += 2; 610 size -= 2; 611 612 CHECK(size >= length); 613 614 addCodecSpecificData(ptr, length); 615 616 ptr += length; 617 size -= length; 618 } 619 620 CODEC_LOGV( 621 "AVC profile = %d (%s), level = %d", 622 (int)profile, AVCProfileToString(profile), level); 623 624 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder") 625 && (profile != kAVCProfileBaseline || level > 30)) { 626 // This stream exceeds the decoder's capabilities. The decoder 627 // does not handle this gracefully and would clobber the heap 628 // and wreak havoc instead... 629 630 LOGE("Profile and/or level exceed the decoder's capabilities."); 631 return ERROR_UNSUPPORTED; 632 } 633 } 634 } 635 636 int32_t bitRate = 0; 637 if (mIsEncoder) { 638 CHECK(meta->findInt32(kKeyBitRate, &bitRate)); 639 } 640 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) { 641 setAMRFormat(false /* isWAMR */, bitRate); 642 } 643 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) { 644 setAMRFormat(true /* isWAMR */, bitRate); 645 } 646 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) { 647 int32_t numChannels, sampleRate; 648 CHECK(meta->findInt32(kKeyChannelCount, &numChannels)); 649 CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); 650 651 setAACFormat(numChannels, sampleRate, bitRate); 652 } 653 654 if (!strncasecmp(mMIME, "video/", 6)) { 655 656 if (mIsEncoder) { 657 setVideoInputFormat(mMIME, meta); 658 } else { 659 int32_t width, height; 660 bool success = meta->findInt32(kKeyWidth, &width); 661 success = success && meta->findInt32(kKeyHeight, &height); 662 CHECK(success); 663 status_t err = setVideoOutputFormat( 664 mMIME, width, height); 665 666 if (err != OK) { 667 return err; 668 } 669 } 670 } 671 672 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG) 673 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) { 674 OMX_COLOR_FORMATTYPE format = 675 OMX_COLOR_Format32bitARGB8888; 676 // OMX_COLOR_FormatYUV420PackedPlanar; 677 // OMX_COLOR_FormatCbYCrY; 678 // OMX_COLOR_FormatYUV411Planar; 679 680 int32_t width, height; 681 bool success = meta->findInt32(kKeyWidth, &width); 682 success = success && meta->findInt32(kKeyHeight, &height); 683 684 int32_t compressedSize; 685 success = success && meta->findInt32( 686 kKeyMaxInputSize, &compressedSize); 687 688 CHECK(success); 689 CHECK(compressedSize > 0); 690 691 setImageOutputFormat(format, width, height); 692 setJPEGInputFormat(width, height, (OMX_U32)compressedSize); 693 } 694 695 int32_t maxInputSize; 696 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) { 697 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize); 698 } 699 700 if (!strcmp(mComponentName, "OMX.TI.AMR.encode") 701 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode") 702 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) { 703 setMinBufferSize(kPortIndexOutput, 8192); // XXX 704 } 705 706 initOutputFormat(meta); 707 708 if ((flags & kClientNeedsFramebuffer) 709 && !strncmp(mComponentName, "OMX.SEC.", 8)) { 710 OMX_INDEXTYPE index; 711 712 status_t err = 713 mOMX->getExtensionIndex( 714 mNode, 715 "OMX.SEC.index.ThumbnailMode", 716 &index); 717 718 if (err != OK) { 719 return err; 720 } 721 722 OMX_BOOL enable = OMX_TRUE; 723 err = mOMX->setConfig(mNode, index, &enable, sizeof(enable)); 724 725 if (err != OK) { 726 CODEC_LOGE("setConfig('OMX.SEC.index.ThumbnailMode') " 727 "returned error 0x%08x", err); 728 729 return err; 730 } 731 732 mQuirks &= ~kOutputBuffersAreUnreadable; 733 } 734 735 return OK; 736 } 737 738 void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) { 739 OMX_PARAM_PORTDEFINITIONTYPE def; 740 InitOMXParams(&def); 741 def.nPortIndex = portIndex; 742 743 status_t err = mOMX->getParameter( 744 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 745 CHECK_EQ(err, OK); 746 747 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus)) 748 || (def.nBufferSize < size)) { 749 def.nBufferSize = size; 750 } 751 752 err = mOMX->setParameter( 753 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 754 CHECK_EQ(err, OK); 755 756 err = mOMX->getParameter( 757 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 758 CHECK_EQ(err, OK); 759 760 // Make sure the setting actually stuck. 761 if (portIndex == kPortIndexInput 762 && (mQuirks & kInputBufferSizesAreBogus)) { 763 CHECK_EQ(def.nBufferSize, size); 764 } else { 765 CHECK(def.nBufferSize >= size); 766 } 767 } 768 769 status_t OMXCodec::setVideoPortFormatType( 770 OMX_U32 portIndex, 771 OMX_VIDEO_CODINGTYPE compressionFormat, 772 OMX_COLOR_FORMATTYPE colorFormat) { 773 OMX_VIDEO_PARAM_PORTFORMATTYPE format; 774 InitOMXParams(&format); 775 format.nPortIndex = portIndex; 776 format.nIndex = 0; 777 bool found = false; 778 779 OMX_U32 index = 0; 780 for (;;) { 781 format.nIndex = index; 782 status_t err = mOMX->getParameter( 783 mNode, OMX_IndexParamVideoPortFormat, 784 &format, sizeof(format)); 785 786 if (err != OK) { 787 return err; 788 } 789 790 // The following assertion is violated by TI's video decoder. 791 // CHECK_EQ(format.nIndex, index); 792 793 #if 1 794 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d", 795 portIndex, 796 index, format.eCompressionFormat, format.eColorFormat); 797 #endif 798 799 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) { 800 if (portIndex == kPortIndexInput 801 && colorFormat == format.eColorFormat) { 802 // eCompressionFormat does not seem right. 803 found = true; 804 break; 805 } 806 if (portIndex == kPortIndexOutput 807 && compressionFormat == format.eCompressionFormat) { 808 // eColorFormat does not seem right. 809 found = true; 810 break; 811 } 812 } 813 814 if (format.eCompressionFormat == compressionFormat 815 && format.eColorFormat == colorFormat) { 816 found = true; 817 break; 818 } 819 820 ++index; 821 } 822 823 if (!found) { 824 return UNKNOWN_ERROR; 825 } 826 827 CODEC_LOGV("found a match."); 828 status_t err = mOMX->setParameter( 829 mNode, OMX_IndexParamVideoPortFormat, 830 &format, sizeof(format)); 831 832 return err; 833 } 834 835 static size_t getFrameSize( 836 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) { 837 switch (colorFormat) { 838 case OMX_COLOR_FormatYCbYCr: 839 case OMX_COLOR_FormatCbYCrY: 840 return width * height * 2; 841 842 case OMX_COLOR_FormatYUV420Planar: 843 case OMX_COLOR_FormatYUV420SemiPlanar: 844 return (width * height * 3) / 2; 845 846 default: 847 CHECK(!"Should not be here. Unsupported color format."); 848 break; 849 } 850 } 851 852 status_t OMXCodec::findTargetColorFormat( 853 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) { 854 LOGV("findTargetColorFormat"); 855 CHECK(mIsEncoder); 856 857 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar; 858 int32_t targetColorFormat; 859 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) { 860 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat; 861 } else { 862 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) { 863 *colorFormat = OMX_COLOR_FormatYCbYCr; 864 } 865 } 866 867 // Check whether the target color format is supported. 868 return isColorFormatSupported(*colorFormat, kPortIndexInput); 869 } 870 871 status_t OMXCodec::isColorFormatSupported( 872 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) { 873 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat)); 874 875 // Enumerate all the color formats supported by 876 // the omx component to see whether the given 877 // color format is supported. 878 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat; 879 InitOMXParams(&portFormat); 880 portFormat.nPortIndex = portIndex; 881 OMX_U32 index = 0; 882 portFormat.nIndex = index; 883 while (true) { 884 if (OMX_ErrorNone != mOMX->getParameter( 885 mNode, OMX_IndexParamVideoPortFormat, 886 &portFormat, sizeof(portFormat))) { 887 break; 888 } 889 // Make sure that omx component does not overwrite 890 // the incremented index (bug 2897413). 891 CHECK_EQ(index, portFormat.nIndex); 892 if ((portFormat.eColorFormat == colorFormat)) { 893 LOGV("Found supported color format: %d", portFormat.eColorFormat); 894 return OK; // colorFormat is supported! 895 } 896 ++index; 897 portFormat.nIndex = index; 898 899 // OMX Spec defines less than 50 color formats 900 // 1000 is more than enough for us to tell whether the omx 901 // component in question is buggy or not. 902 if (index >= 1000) { 903 LOGE("More than %ld color formats are supported???", index); 904 break; 905 } 906 } 907 908 LOGE("color format %d is not supported", colorFormat); 909 return UNKNOWN_ERROR; 910 } 911 912 void OMXCodec::setVideoInputFormat( 913 const char *mime, const sp<MetaData>& meta) { 914 915 int32_t width, height, frameRate, bitRate, stride, sliceHeight; 916 bool success = meta->findInt32(kKeyWidth, &width); 917 success = success && meta->findInt32(kKeyHeight, &height); 918 success = success && meta->findInt32(kKeySampleRate, &frameRate); 919 success = success && meta->findInt32(kKeyBitRate, &bitRate); 920 success = success && meta->findInt32(kKeyStride, &stride); 921 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight); 922 CHECK(success); 923 CHECK(stride != 0); 924 925 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused; 926 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 927 compressionFormat = OMX_VIDEO_CodingAVC; 928 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 929 compressionFormat = OMX_VIDEO_CodingMPEG4; 930 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 931 compressionFormat = OMX_VIDEO_CodingH263; 932 } else { 933 LOGE("Not a supported video mime type: %s", mime); 934 CHECK(!"Should not be here. Not a supported video mime type."); 935 } 936 937 OMX_COLOR_FORMATTYPE colorFormat; 938 CHECK_EQ(OK, findTargetColorFormat(meta, &colorFormat)); 939 940 status_t err; 941 OMX_PARAM_PORTDEFINITIONTYPE def; 942 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 943 944 //////////////////////// Input port ///////////////////////// 945 CHECK_EQ(setVideoPortFormatType( 946 kPortIndexInput, OMX_VIDEO_CodingUnused, 947 colorFormat), OK); 948 949 InitOMXParams(&def); 950 def.nPortIndex = kPortIndexInput; 951 952 err = mOMX->getParameter( 953 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 954 CHECK_EQ(err, OK); 955 956 def.nBufferSize = getFrameSize(colorFormat, 957 stride > 0? stride: -stride, sliceHeight); 958 959 CHECK_EQ(def.eDomain, OMX_PortDomainVideo); 960 961 video_def->nFrameWidth = width; 962 video_def->nFrameHeight = height; 963 video_def->nStride = stride; 964 video_def->nSliceHeight = sliceHeight; 965 video_def->xFramerate = (frameRate << 16); // Q16 format 966 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused; 967 video_def->eColorFormat = colorFormat; 968 969 err = mOMX->setParameter( 970 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 971 CHECK_EQ(err, OK); 972 973 //////////////////////// Output port ///////////////////////// 974 CHECK_EQ(setVideoPortFormatType( 975 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused), 976 OK); 977 InitOMXParams(&def); 978 def.nPortIndex = kPortIndexOutput; 979 980 err = mOMX->getParameter( 981 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 982 983 CHECK_EQ(err, OK); 984 CHECK_EQ(def.eDomain, OMX_PortDomainVideo); 985 986 video_def->nFrameWidth = width; 987 video_def->nFrameHeight = height; 988 video_def->xFramerate = 0; // No need for output port 989 video_def->nBitrate = bitRate; // Q16 format 990 video_def->eCompressionFormat = compressionFormat; 991 video_def->eColorFormat = OMX_COLOR_FormatUnused; 992 if (mQuirks & kRequiresLargerEncoderOutputBuffer) { 993 // Increases the output buffer size 994 def.nBufferSize = ((def.nBufferSize * 3) >> 1); 995 } 996 997 err = mOMX->setParameter( 998 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 999 CHECK_EQ(err, OK); 1000 1001 /////////////////// Codec-specific //////////////////////// 1002 switch (compressionFormat) { 1003 case OMX_VIDEO_CodingMPEG4: 1004 { 1005 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK); 1006 break; 1007 } 1008 1009 case OMX_VIDEO_CodingH263: 1010 CHECK_EQ(setupH263EncoderParameters(meta), OK); 1011 break; 1012 1013 case OMX_VIDEO_CodingAVC: 1014 { 1015 CHECK_EQ(setupAVCEncoderParameters(meta), OK); 1016 break; 1017 } 1018 1019 default: 1020 CHECK(!"Support for this compressionFormat to be implemented."); 1021 break; 1022 } 1023 } 1024 1025 static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) { 1026 if (iFramesInterval < 0) { 1027 return 0xFFFFFFFF; 1028 } else if (iFramesInterval == 0) { 1029 return 0; 1030 } 1031 OMX_U32 ret = frameRate * iFramesInterval; 1032 CHECK(ret > 1); 1033 return ret; 1034 } 1035 1036 status_t OMXCodec::setupErrorCorrectionParameters() { 1037 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType; 1038 InitOMXParams(&errorCorrectionType); 1039 errorCorrectionType.nPortIndex = kPortIndexOutput; 1040 1041 status_t err = mOMX->getParameter( 1042 mNode, OMX_IndexParamVideoErrorCorrection, 1043 &errorCorrectionType, sizeof(errorCorrectionType)); 1044 if (err != OK) { 1045 LOGW("Error correction param query is not supported"); 1046 return OK; // Optional feature. Ignore this failure 1047 } 1048 1049 errorCorrectionType.bEnableHEC = OMX_FALSE; 1050 errorCorrectionType.bEnableResync = OMX_TRUE; 1051 errorCorrectionType.nResynchMarkerSpacing = 256; 1052 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE; 1053 errorCorrectionType.bEnableRVLC = OMX_FALSE; 1054 1055 err = mOMX->setParameter( 1056 mNode, OMX_IndexParamVideoErrorCorrection, 1057 &errorCorrectionType, sizeof(errorCorrectionType)); 1058 if (err != OK) { 1059 LOGW("Error correction param configuration is not supported"); 1060 } 1061 1062 // Optional feature. Ignore the failure. 1063 return OK; 1064 } 1065 1066 status_t OMXCodec::setupBitRate(int32_t bitRate) { 1067 OMX_VIDEO_PARAM_BITRATETYPE bitrateType; 1068 InitOMXParams(&bitrateType); 1069 bitrateType.nPortIndex = kPortIndexOutput; 1070 1071 status_t err = mOMX->getParameter( 1072 mNode, OMX_IndexParamVideoBitrate, 1073 &bitrateType, sizeof(bitrateType)); 1074 CHECK_EQ(err, OK); 1075 1076 bitrateType.eControlRate = OMX_Video_ControlRateVariable; 1077 bitrateType.nTargetBitrate = bitRate; 1078 1079 err = mOMX->setParameter( 1080 mNode, OMX_IndexParamVideoBitrate, 1081 &bitrateType, sizeof(bitrateType)); 1082 CHECK_EQ(err, OK); 1083 return OK; 1084 } 1085 1086 status_t OMXCodec::getVideoProfileLevel( 1087 const sp<MetaData>& meta, 1088 const CodecProfileLevel& defaultProfileLevel, 1089 CodecProfileLevel &profileLevel) { 1090 CODEC_LOGV("Default profile: %ld, level %ld", 1091 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel); 1092 1093 // Are the default profile and level overwriten? 1094 int32_t profile, level; 1095 if (!meta->findInt32(kKeyVideoProfile, &profile)) { 1096 profile = defaultProfileLevel.mProfile; 1097 } 1098 if (!meta->findInt32(kKeyVideoLevel, &level)) { 1099 level = defaultProfileLevel.mLevel; 1100 } 1101 CODEC_LOGV("Target profile: %d, level: %d", profile, level); 1102 1103 // Are the target profile and level supported by the encoder? 1104 OMX_VIDEO_PARAM_PROFILELEVELTYPE param; 1105 InitOMXParams(¶m); 1106 param.nPortIndex = kPortIndexOutput; 1107 for (param.nProfileIndex = 0;; ++param.nProfileIndex) { 1108 status_t err = mOMX->getParameter( 1109 mNode, OMX_IndexParamVideoProfileLevelQuerySupported, 1110 ¶m, sizeof(param)); 1111 1112 if (err != OK) break; 1113 1114 int32_t supportedProfile = static_cast<int32_t>(param.eProfile); 1115 int32_t supportedLevel = static_cast<int32_t>(param.eLevel); 1116 CODEC_LOGV("Supported profile: %d, level %d", 1117 supportedProfile, supportedLevel); 1118 1119 if (profile == supportedProfile && 1120 level <= supportedLevel) { 1121 // We can further check whether the level is a valid 1122 // value; but we will leave that to the omx encoder component 1123 // via OMX_SetParameter call. 1124 profileLevel.mProfile = profile; 1125 profileLevel.mLevel = level; 1126 return OK; 1127 } 1128 } 1129 1130 CODEC_LOGE("Target profile (%d) and level (%d) is not supported", 1131 profile, level); 1132 return BAD_VALUE; 1133 } 1134 1135 status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) { 1136 int32_t iFramesInterval, frameRate, bitRate; 1137 bool success = meta->findInt32(kKeyBitRate, &bitRate); 1138 success = success && meta->findInt32(kKeySampleRate, &frameRate); 1139 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); 1140 CHECK(success); 1141 OMX_VIDEO_PARAM_H263TYPE h263type; 1142 InitOMXParams(&h263type); 1143 h263type.nPortIndex = kPortIndexOutput; 1144 1145 status_t err = mOMX->getParameter( 1146 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type)); 1147 CHECK_EQ(err, OK); 1148 1149 h263type.nAllowedPictureTypes = 1150 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; 1151 1152 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); 1153 if (h263type.nPFrames == 0) { 1154 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; 1155 } 1156 h263type.nBFrames = 0; 1157 1158 // Check profile and level parameters 1159 CodecProfileLevel defaultProfileLevel, profileLevel; 1160 defaultProfileLevel.mProfile = h263type.eProfile; 1161 defaultProfileLevel.mLevel = h263type.eLevel; 1162 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel); 1163 if (err != OK) return err; 1164 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile); 1165 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel); 1166 1167 h263type.bPLUSPTYPEAllowed = OMX_FALSE; 1168 h263type.bForceRoundingTypeToZero = OMX_FALSE; 1169 h263type.nPictureHeaderRepetition = 0; 1170 h263type.nGOBHeaderInterval = 0; 1171 1172 err = mOMX->setParameter( 1173 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type)); 1174 CHECK_EQ(err, OK); 1175 1176 CHECK_EQ(setupBitRate(bitRate), OK); 1177 CHECK_EQ(setupErrorCorrectionParameters(), OK); 1178 1179 return OK; 1180 } 1181 1182 status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) { 1183 int32_t iFramesInterval, frameRate, bitRate; 1184 bool success = meta->findInt32(kKeyBitRate, &bitRate); 1185 success = success && meta->findInt32(kKeySampleRate, &frameRate); 1186 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); 1187 CHECK(success); 1188 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type; 1189 InitOMXParams(&mpeg4type); 1190 mpeg4type.nPortIndex = kPortIndexOutput; 1191 1192 status_t err = mOMX->getParameter( 1193 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type)); 1194 CHECK_EQ(err, OK); 1195 1196 mpeg4type.nSliceHeaderSpacing = 0; 1197 mpeg4type.bSVH = OMX_FALSE; 1198 mpeg4type.bGov = OMX_FALSE; 1199 1200 mpeg4type.nAllowedPictureTypes = 1201 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; 1202 1203 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); 1204 if (mpeg4type.nPFrames == 0) { 1205 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; 1206 } 1207 mpeg4type.nBFrames = 0; 1208 mpeg4type.nIDCVLCThreshold = 0; 1209 mpeg4type.bACPred = OMX_TRUE; 1210 mpeg4type.nMaxPacketSize = 256; 1211 mpeg4type.nTimeIncRes = 1000; 1212 mpeg4type.nHeaderExtension = 0; 1213 mpeg4type.bReversibleVLC = OMX_FALSE; 1214 1215 // Check profile and level parameters 1216 CodecProfileLevel defaultProfileLevel, profileLevel; 1217 defaultProfileLevel.mProfile = mpeg4type.eProfile; 1218 defaultProfileLevel.mLevel = mpeg4type.eLevel; 1219 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel); 1220 if (err != OK) return err; 1221 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile); 1222 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel); 1223 1224 err = mOMX->setParameter( 1225 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type)); 1226 CHECK_EQ(err, OK); 1227 1228 CHECK_EQ(setupBitRate(bitRate), OK); 1229 CHECK_EQ(setupErrorCorrectionParameters(), OK); 1230 1231 return OK; 1232 } 1233 1234 status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) { 1235 int32_t iFramesInterval, frameRate, bitRate; 1236 bool success = meta->findInt32(kKeyBitRate, &bitRate); 1237 success = success && meta->findInt32(kKeySampleRate, &frameRate); 1238 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); 1239 CHECK(success); 1240 1241 OMX_VIDEO_PARAM_AVCTYPE h264type; 1242 InitOMXParams(&h264type); 1243 h264type.nPortIndex = kPortIndexOutput; 1244 1245 status_t err = mOMX->getParameter( 1246 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type)); 1247 CHECK_EQ(err, OK); 1248 1249 h264type.nAllowedPictureTypes = 1250 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; 1251 1252 h264type.nSliceHeaderSpacing = 0; 1253 h264type.nBFrames = 0; // No B frames support yet 1254 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); 1255 if (h264type.nPFrames == 0) { 1256 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; 1257 } 1258 1259 // Check profile and level parameters 1260 CodecProfileLevel defaultProfileLevel, profileLevel; 1261 defaultProfileLevel.mProfile = h264type.eProfile; 1262 defaultProfileLevel.mLevel = h264type.eLevel; 1263 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel); 1264 if (err != OK) return err; 1265 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile); 1266 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel); 1267 1268 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) { 1269 h264type.bUseHadamard = OMX_TRUE; 1270 h264type.nRefFrames = 1; 1271 h264type.nRefIdx10ActiveMinus1 = 0; 1272 h264type.nRefIdx11ActiveMinus1 = 0; 1273 h264type.bEntropyCodingCABAC = OMX_FALSE; 1274 h264type.bWeightedPPrediction = OMX_FALSE; 1275 h264type.bconstIpred = OMX_FALSE; 1276 h264type.bDirect8x8Inference = OMX_FALSE; 1277 h264type.bDirectSpatialTemporal = OMX_FALSE; 1278 h264type.nCabacInitIdc = 0; 1279 } 1280 1281 if (h264type.nBFrames != 0) { 1282 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB; 1283 } 1284 1285 h264type.bEnableUEP = OMX_FALSE; 1286 h264type.bEnableFMO = OMX_FALSE; 1287 h264type.bEnableASO = OMX_FALSE; 1288 h264type.bEnableRS = OMX_FALSE; 1289 h264type.bFrameMBsOnly = OMX_TRUE; 1290 h264type.bMBAFF = OMX_FALSE; 1291 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable; 1292 1293 err = mOMX->setParameter( 1294 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type)); 1295 CHECK_EQ(err, OK); 1296 1297 CHECK_EQ(setupBitRate(bitRate), OK); 1298 1299 return OK; 1300 } 1301 1302 status_t OMXCodec::setVideoOutputFormat( 1303 const char *mime, OMX_U32 width, OMX_U32 height) { 1304 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height); 1305 1306 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused; 1307 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 1308 compressionFormat = OMX_VIDEO_CodingAVC; 1309 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 1310 compressionFormat = OMX_VIDEO_CodingMPEG4; 1311 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 1312 compressionFormat = OMX_VIDEO_CodingH263; 1313 } else { 1314 LOGE("Not a supported video mime type: %s", mime); 1315 CHECK(!"Should not be here. Not a supported video mime type."); 1316 } 1317 1318 status_t err = setVideoPortFormatType( 1319 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused); 1320 1321 if (err != OK) { 1322 return err; 1323 } 1324 1325 #if 1 1326 { 1327 OMX_VIDEO_PARAM_PORTFORMATTYPE format; 1328 InitOMXParams(&format); 1329 format.nPortIndex = kPortIndexOutput; 1330 format.nIndex = 0; 1331 1332 status_t err = mOMX->getParameter( 1333 mNode, OMX_IndexParamVideoPortFormat, 1334 &format, sizeof(format)); 1335 CHECK_EQ(err, OK); 1336 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused); 1337 1338 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00; 1339 1340 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar 1341 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar 1342 || format.eColorFormat == OMX_COLOR_FormatCbYCrY 1343 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar); 1344 1345 err = mOMX->setParameter( 1346 mNode, OMX_IndexParamVideoPortFormat, 1347 &format, sizeof(format)); 1348 1349 if (err != OK) { 1350 return err; 1351 } 1352 } 1353 #endif 1354 1355 OMX_PARAM_PORTDEFINITIONTYPE def; 1356 InitOMXParams(&def); 1357 def.nPortIndex = kPortIndexInput; 1358 1359 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 1360 1361 err = mOMX->getParameter( 1362 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1363 1364 CHECK_EQ(err, OK); 1365 1366 #if 1 1367 // XXX Need a (much) better heuristic to compute input buffer sizes. 1368 const size_t X = 64 * 1024; 1369 if (def.nBufferSize < X) { 1370 def.nBufferSize = X; 1371 } 1372 #endif 1373 1374 CHECK_EQ(def.eDomain, OMX_PortDomainVideo); 1375 1376 video_def->nFrameWidth = width; 1377 video_def->nFrameHeight = height; 1378 1379 video_def->eCompressionFormat = compressionFormat; 1380 video_def->eColorFormat = OMX_COLOR_FormatUnused; 1381 1382 err = mOMX->setParameter( 1383 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1384 1385 if (err != OK) { 1386 return err; 1387 } 1388 1389 //////////////////////////////////////////////////////////////////////////// 1390 1391 InitOMXParams(&def); 1392 def.nPortIndex = kPortIndexOutput; 1393 1394 err = mOMX->getParameter( 1395 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1396 CHECK_EQ(err, OK); 1397 CHECK_EQ(def.eDomain, OMX_PortDomainVideo); 1398 1399 #if 0 1400 def.nBufferSize = 1401 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420 1402 #endif 1403 1404 video_def->nFrameWidth = width; 1405 video_def->nFrameHeight = height; 1406 1407 err = mOMX->setParameter( 1408 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1409 1410 return err; 1411 } 1412 1413 OMXCodec::OMXCodec( 1414 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks, 1415 bool isEncoder, 1416 const char *mime, 1417 const char *componentName, 1418 const sp<MediaSource> &source) 1419 : mOMX(omx), 1420 mOMXLivesLocally(omx->livesLocally(getpid())), 1421 mNode(node), 1422 mQuirks(quirks), 1423 mIsEncoder(isEncoder), 1424 mMIME(strdup(mime)), 1425 mComponentName(strdup(componentName)), 1426 mSource(source), 1427 mCodecSpecificDataIndex(0), 1428 mState(LOADED), 1429 mInitialBufferSubmit(true), 1430 mSignalledEOS(false), 1431 mNoMoreOutputData(false), 1432 mOutputPortSettingsHaveChanged(false), 1433 mSeekTimeUs(-1), 1434 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC), 1435 mTargetTimeUs(-1), 1436 mSkipTimeUs(-1), 1437 mLeftOverBuffer(NULL), 1438 mPaused(false) { 1439 mPortStatus[kPortIndexInput] = ENABLED; 1440 mPortStatus[kPortIndexOutput] = ENABLED; 1441 1442 setComponentRole(); 1443 } 1444 1445 // static 1446 void OMXCodec::setComponentRole( 1447 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder, 1448 const char *mime) { 1449 struct MimeToRole { 1450 const char *mime; 1451 const char *decoderRole; 1452 const char *encoderRole; 1453 }; 1454 1455 static const MimeToRole kMimeToRole[] = { 1456 { MEDIA_MIMETYPE_AUDIO_MPEG, 1457 "audio_decoder.mp3", "audio_encoder.mp3" }, 1458 { MEDIA_MIMETYPE_AUDIO_AMR_NB, 1459 "audio_decoder.amrnb", "audio_encoder.amrnb" }, 1460 { MEDIA_MIMETYPE_AUDIO_AMR_WB, 1461 "audio_decoder.amrwb", "audio_encoder.amrwb" }, 1462 { MEDIA_MIMETYPE_AUDIO_AAC, 1463 "audio_decoder.aac", "audio_encoder.aac" }, 1464 { MEDIA_MIMETYPE_VIDEO_AVC, 1465 "video_decoder.avc", "video_encoder.avc" }, 1466 { MEDIA_MIMETYPE_VIDEO_MPEG4, 1467 "video_decoder.mpeg4", "video_encoder.mpeg4" }, 1468 { MEDIA_MIMETYPE_VIDEO_H263, 1469 "video_decoder.h263", "video_encoder.h263" }, 1470 }; 1471 1472 static const size_t kNumMimeToRole = 1473 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]); 1474 1475 size_t i; 1476 for (i = 0; i < kNumMimeToRole; ++i) { 1477 if (!strcasecmp(mime, kMimeToRole[i].mime)) { 1478 break; 1479 } 1480 } 1481 1482 if (i == kNumMimeToRole) { 1483 return; 1484 } 1485 1486 const char *role = 1487 isEncoder ? kMimeToRole[i].encoderRole 1488 : kMimeToRole[i].decoderRole; 1489 1490 if (role != NULL) { 1491 OMX_PARAM_COMPONENTROLETYPE roleParams; 1492 InitOMXParams(&roleParams); 1493 1494 strncpy((char *)roleParams.cRole, 1495 role, OMX_MAX_STRINGNAME_SIZE - 1); 1496 1497 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0'; 1498 1499 status_t err = omx->setParameter( 1500 node, OMX_IndexParamStandardComponentRole, 1501 &roleParams, sizeof(roleParams)); 1502 1503 if (err != OK) { 1504 LOGW("Failed to set standard component role '%s'.", role); 1505 } 1506 } 1507 } 1508 1509 void OMXCodec::setComponentRole() { 1510 setComponentRole(mOMX, mNode, mIsEncoder, mMIME); 1511 } 1512 1513 OMXCodec::~OMXCodec() { 1514 mSource.clear(); 1515 1516 CHECK(mState == LOADED || mState == ERROR); 1517 1518 status_t err = mOMX->freeNode(mNode); 1519 CHECK_EQ(err, OK); 1520 1521 mNode = NULL; 1522 setState(DEAD); 1523 1524 clearCodecSpecificData(); 1525 1526 free(mComponentName); 1527 mComponentName = NULL; 1528 1529 free(mMIME); 1530 mMIME = NULL; 1531 } 1532 1533 status_t OMXCodec::init() { 1534 // mLock is held. 1535 1536 CHECK_EQ(mState, LOADED); 1537 1538 status_t err; 1539 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) { 1540 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 1541 CHECK_EQ(err, OK); 1542 setState(LOADED_TO_IDLE); 1543 } 1544 1545 err = allocateBuffers(); 1546 CHECK_EQ(err, OK); 1547 1548 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) { 1549 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 1550 CHECK_EQ(err, OK); 1551 1552 setState(LOADED_TO_IDLE); 1553 } 1554 1555 while (mState != EXECUTING && mState != ERROR) { 1556 mAsyncCompletion.wait(mLock); 1557 } 1558 1559 return mState == ERROR ? UNKNOWN_ERROR : OK; 1560 } 1561 1562 // static 1563 bool OMXCodec::isIntermediateState(State state) { 1564 return state == LOADED_TO_IDLE 1565 || state == IDLE_TO_EXECUTING 1566 || state == EXECUTING_TO_IDLE 1567 || state == IDLE_TO_LOADED 1568 || state == RECONFIGURING; 1569 } 1570 1571 status_t OMXCodec::allocateBuffers() { 1572 status_t err = allocateBuffersOnPort(kPortIndexInput); 1573 1574 if (err != OK) { 1575 return err; 1576 } 1577 1578 return allocateBuffersOnPort(kPortIndexOutput); 1579 } 1580 1581 status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) { 1582 OMX_PARAM_PORTDEFINITIONTYPE def; 1583 InitOMXParams(&def); 1584 def.nPortIndex = portIndex; 1585 1586 status_t err = mOMX->getParameter( 1587 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 1588 1589 if (err != OK) { 1590 return err; 1591 } 1592 1593 CODEC_LOGI("allocating %lu buffers of size %lu on %s port", 1594 def.nBufferCountActual, def.nBufferSize, 1595 portIndex == kPortIndexInput ? "input" : "output"); 1596 1597 size_t totalSize = def.nBufferCountActual * def.nBufferSize; 1598 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec"); 1599 1600 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) { 1601 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize); 1602 CHECK(mem.get() != NULL); 1603 1604 BufferInfo info; 1605 info.mData = NULL; 1606 info.mSize = def.nBufferSize; 1607 1608 IOMX::buffer_id buffer; 1609 if (portIndex == kPortIndexInput 1610 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) { 1611 if (mOMXLivesLocally) { 1612 mem.clear(); 1613 1614 err = mOMX->allocateBuffer( 1615 mNode, portIndex, def.nBufferSize, &buffer, 1616 &info.mData); 1617 } else { 1618 err = mOMX->allocateBufferWithBackup( 1619 mNode, portIndex, mem, &buffer); 1620 } 1621 } else if (portIndex == kPortIndexOutput 1622 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) { 1623 if (mOMXLivesLocally) { 1624 mem.clear(); 1625 1626 err = mOMX->allocateBuffer( 1627 mNode, portIndex, def.nBufferSize, &buffer, 1628 &info.mData); 1629 } else { 1630 err = mOMX->allocateBufferWithBackup( 1631 mNode, portIndex, mem, &buffer); 1632 } 1633 } else { 1634 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer); 1635 } 1636 1637 if (err != OK) { 1638 LOGE("allocate_buffer_with_backup failed"); 1639 return err; 1640 } 1641 1642 if (mem != NULL) { 1643 info.mData = mem->pointer(); 1644 } 1645 1646 info.mBuffer = buffer; 1647 info.mOwnedByComponent = false; 1648 info.mMem = mem; 1649 info.mMediaBuffer = NULL; 1650 1651 if (portIndex == kPortIndexOutput) { 1652 if (!(mOMXLivesLocally 1653 && (mQuirks & kRequiresAllocateBufferOnOutputPorts) 1654 && (mQuirks & kDefersOutputBufferAllocation))) { 1655 // If the node does not fill in the buffer ptr at this time, 1656 // we will defer creating the MediaBuffer until receiving 1657 // the first FILL_BUFFER_DONE notification instead. 1658 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize); 1659 info.mMediaBuffer->setObserver(this); 1660 } 1661 } 1662 1663 mPortBuffers[portIndex].push(info); 1664 1665 CODEC_LOGV("allocated buffer %p on %s port", buffer, 1666 portIndex == kPortIndexInput ? "input" : "output"); 1667 } 1668 1669 // dumpPortStatus(portIndex); 1670 1671 return OK; 1672 } 1673 1674 void OMXCodec::on_message(const omx_message &msg) { 1675 Mutex::Autolock autoLock(mLock); 1676 1677 switch (msg.type) { 1678 case omx_message::EVENT: 1679 { 1680 onEvent( 1681 msg.u.event_data.event, msg.u.event_data.data1, 1682 msg.u.event_data.data2); 1683 1684 break; 1685 } 1686 1687 case omx_message::EMPTY_BUFFER_DONE: 1688 { 1689 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer; 1690 1691 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer); 1692 1693 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 1694 size_t i = 0; 1695 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) { 1696 ++i; 1697 } 1698 1699 CHECK(i < buffers->size()); 1700 if (!(*buffers)[i].mOwnedByComponent) { 1701 LOGW("We already own input buffer %p, yet received " 1702 "an EMPTY_BUFFER_DONE.", buffer); 1703 } 1704 1705 { 1706 BufferInfo *info = &buffers->editItemAt(i); 1707 info->mOwnedByComponent = false; 1708 if (info->mMediaBuffer != NULL) { 1709 // It is time to release the media buffers storing meta data 1710 info->mMediaBuffer->release(); 1711 info->mMediaBuffer = NULL; 1712 } 1713 } 1714 1715 if (mPortStatus[kPortIndexInput] == DISABLING) { 1716 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer); 1717 1718 status_t err = 1719 mOMX->freeBuffer(mNode, kPortIndexInput, buffer); 1720 CHECK_EQ(err, OK); 1721 1722 buffers->removeAt(i); 1723 } else if (mState != ERROR 1724 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) { 1725 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED); 1726 drainInputBuffer(&buffers->editItemAt(i)); 1727 } 1728 break; 1729 } 1730 1731 case omx_message::FILL_BUFFER_DONE: 1732 { 1733 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer; 1734 OMX_U32 flags = msg.u.extended_buffer_data.flags; 1735 1736 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))", 1737 buffer, 1738 msg.u.extended_buffer_data.range_length, 1739 flags, 1740 msg.u.extended_buffer_data.timestamp, 1741 msg.u.extended_buffer_data.timestamp / 1E6); 1742 1743 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 1744 size_t i = 0; 1745 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) { 1746 ++i; 1747 } 1748 1749 CHECK(i < buffers->size()); 1750 BufferInfo *info = &buffers->editItemAt(i); 1751 1752 if (!info->mOwnedByComponent) { 1753 LOGW("We already own output buffer %p, yet received " 1754 "a FILL_BUFFER_DONE.", buffer); 1755 } 1756 1757 info->mOwnedByComponent = false; 1758 1759 if (mPortStatus[kPortIndexOutput] == DISABLING) { 1760 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer); 1761 1762 status_t err = 1763 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer); 1764 CHECK_EQ(err, OK); 1765 1766 buffers->removeAt(i); 1767 #if 0 1768 } else if (mPortStatus[kPortIndexOutput] == ENABLED 1769 && (flags & OMX_BUFFERFLAG_EOS)) { 1770 CODEC_LOGV("No more output data."); 1771 mNoMoreOutputData = true; 1772 mBufferFilled.signal(); 1773 #endif 1774 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) { 1775 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED); 1776 1777 if (info->mMediaBuffer == NULL) { 1778 CHECK(mOMXLivesLocally); 1779 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts); 1780 CHECK(mQuirks & kDefersOutputBufferAllocation); 1781 1782 // The qcom video decoders on Nexus don't actually allocate 1783 // output buffer memory on a call to OMX_AllocateBuffer 1784 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE 1785 // structure is only filled in later. 1786 1787 info->mMediaBuffer = new MediaBuffer( 1788 msg.u.extended_buffer_data.data_ptr, 1789 info->mSize); 1790 info->mMediaBuffer->setObserver(this); 1791 } 1792 1793 MediaBuffer *buffer = info->mMediaBuffer; 1794 1795 if (msg.u.extended_buffer_data.range_offset 1796 + msg.u.extended_buffer_data.range_length 1797 > buffer->size()) { 1798 CODEC_LOGE( 1799 "Codec lied about its buffer size requirements, " 1800 "sending a buffer larger than the originally " 1801 "advertised size in FILL_BUFFER_DONE!"); 1802 } 1803 buffer->set_range( 1804 msg.u.extended_buffer_data.range_offset, 1805 msg.u.extended_buffer_data.range_length); 1806 1807 buffer->meta_data()->clear(); 1808 1809 buffer->meta_data()->setInt64( 1810 kKeyTime, msg.u.extended_buffer_data.timestamp); 1811 1812 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) { 1813 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true); 1814 } 1815 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) { 1816 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true); 1817 } 1818 1819 if (mQuirks & kOutputBuffersAreUnreadable) { 1820 buffer->meta_data()->setInt32(kKeyIsUnreadable, true); 1821 } 1822 1823 buffer->meta_data()->setPointer( 1824 kKeyPlatformPrivate, 1825 msg.u.extended_buffer_data.platform_private); 1826 1827 buffer->meta_data()->setPointer( 1828 kKeyBufferID, 1829 msg.u.extended_buffer_data.buffer); 1830 1831 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) { 1832 CODEC_LOGV("No more output data."); 1833 mNoMoreOutputData = true; 1834 } 1835 1836 if (mTargetTimeUs >= 0) { 1837 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs); 1838 1839 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) { 1840 CODEC_LOGV( 1841 "skipping output buffer at timestamp %lld us", 1842 msg.u.extended_buffer_data.timestamp); 1843 1844 fillOutputBuffer(info); 1845 break; 1846 } 1847 1848 CODEC_LOGV( 1849 "returning output buffer at target timestamp " 1850 "%lld us", 1851 msg.u.extended_buffer_data.timestamp); 1852 1853 mTargetTimeUs = -1; 1854 } 1855 1856 mFilledBuffers.push_back(i); 1857 mBufferFilled.signal(); 1858 } 1859 1860 break; 1861 } 1862 1863 default: 1864 { 1865 CHECK(!"should not be here."); 1866 break; 1867 } 1868 } 1869 } 1870 1871 void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) { 1872 switch (event) { 1873 case OMX_EventCmdComplete: 1874 { 1875 onCmdComplete((OMX_COMMANDTYPE)data1, data2); 1876 break; 1877 } 1878 1879 case OMX_EventError: 1880 { 1881 CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2); 1882 1883 setState(ERROR); 1884 break; 1885 } 1886 1887 case OMX_EventPortSettingsChanged: 1888 { 1889 onPortSettingsChanged(data1); 1890 break; 1891 } 1892 1893 #if 0 1894 case OMX_EventBufferFlag: 1895 { 1896 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1); 1897 1898 if (data1 == kPortIndexOutput) { 1899 mNoMoreOutputData = true; 1900 } 1901 break; 1902 } 1903 #endif 1904 1905 default: 1906 { 1907 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2); 1908 break; 1909 } 1910 } 1911 } 1912 1913 // Has the format changed in any way that the client would have to be aware of? 1914 static bool formatHasNotablyChanged( 1915 const sp<MetaData> &from, const sp<MetaData> &to) { 1916 if (from.get() == NULL && to.get() == NULL) { 1917 return false; 1918 } 1919 1920 if ((from.get() == NULL && to.get() != NULL) 1921 || (from.get() != NULL && to.get() == NULL)) { 1922 return true; 1923 } 1924 1925 const char *mime_from, *mime_to; 1926 CHECK(from->findCString(kKeyMIMEType, &mime_from)); 1927 CHECK(to->findCString(kKeyMIMEType, &mime_to)); 1928 1929 if (strcasecmp(mime_from, mime_to)) { 1930 return true; 1931 } 1932 1933 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) { 1934 int32_t colorFormat_from, colorFormat_to; 1935 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from)); 1936 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to)); 1937 1938 if (colorFormat_from != colorFormat_to) { 1939 return true; 1940 } 1941 1942 int32_t width_from, width_to; 1943 CHECK(from->findInt32(kKeyWidth, &width_from)); 1944 CHECK(to->findInt32(kKeyWidth, &width_to)); 1945 1946 if (width_from != width_to) { 1947 return true; 1948 } 1949 1950 int32_t height_from, height_to; 1951 CHECK(from->findInt32(kKeyHeight, &height_from)); 1952 CHECK(to->findInt32(kKeyHeight, &height_to)); 1953 1954 if (height_from != height_to) { 1955 return true; 1956 } 1957 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) { 1958 int32_t numChannels_from, numChannels_to; 1959 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from)); 1960 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to)); 1961 1962 if (numChannels_from != numChannels_to) { 1963 return true; 1964 } 1965 1966 int32_t sampleRate_from, sampleRate_to; 1967 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from)); 1968 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to)); 1969 1970 if (sampleRate_from != sampleRate_to) { 1971 return true; 1972 } 1973 } 1974 1975 return false; 1976 } 1977 1978 void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) { 1979 switch (cmd) { 1980 case OMX_CommandStateSet: 1981 { 1982 onStateChange((OMX_STATETYPE)data); 1983 break; 1984 } 1985 1986 case OMX_CommandPortDisable: 1987 { 1988 OMX_U32 portIndex = data; 1989 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex); 1990 1991 CHECK(mState == EXECUTING || mState == RECONFIGURING); 1992 CHECK_EQ(mPortStatus[portIndex], DISABLING); 1993 CHECK_EQ(mPortBuffers[portIndex].size(), 0); 1994 1995 mPortStatus[portIndex] = DISABLED; 1996 1997 if (mState == RECONFIGURING) { 1998 CHECK_EQ(portIndex, kPortIndexOutput); 1999 2000 sp<MetaData> oldOutputFormat = mOutputFormat; 2001 initOutputFormat(mSource->getFormat()); 2002 2003 // Don't notify clients if the output port settings change 2004 // wasn't of importance to them, i.e. it may be that just the 2005 // number of buffers has changed and nothing else. 2006 mOutputPortSettingsHaveChanged = 2007 formatHasNotablyChanged(oldOutputFormat, mOutputFormat); 2008 2009 enablePortAsync(portIndex); 2010 2011 status_t err = allocateBuffersOnPort(portIndex); 2012 CHECK_EQ(err, OK); 2013 } 2014 break; 2015 } 2016 2017 case OMX_CommandPortEnable: 2018 { 2019 OMX_U32 portIndex = data; 2020 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex); 2021 2022 CHECK(mState == EXECUTING || mState == RECONFIGURING); 2023 CHECK_EQ(mPortStatus[portIndex], ENABLING); 2024 2025 mPortStatus[portIndex] = ENABLED; 2026 2027 if (mState == RECONFIGURING) { 2028 CHECK_EQ(portIndex, kPortIndexOutput); 2029 2030 setState(EXECUTING); 2031 2032 fillOutputBuffers(); 2033 } 2034 break; 2035 } 2036 2037 case OMX_CommandFlush: 2038 { 2039 OMX_U32 portIndex = data; 2040 2041 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex); 2042 2043 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN); 2044 mPortStatus[portIndex] = ENABLED; 2045 2046 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]), 2047 mPortBuffers[portIndex].size()); 2048 2049 if (mState == RECONFIGURING) { 2050 CHECK_EQ(portIndex, kPortIndexOutput); 2051 2052 disablePortAsync(portIndex); 2053 } else if (mState == EXECUTING_TO_IDLE) { 2054 if (mPortStatus[kPortIndexInput] == ENABLED 2055 && mPortStatus[kPortIndexOutput] == ENABLED) { 2056 CODEC_LOGV("Finished flushing both ports, now completing " 2057 "transition from EXECUTING to IDLE."); 2058 2059 mPortStatus[kPortIndexInput] = SHUTTING_DOWN; 2060 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; 2061 2062 status_t err = 2063 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 2064 CHECK_EQ(err, OK); 2065 } 2066 } else { 2067 // We're flushing both ports in preparation for seeking. 2068 2069 if (mPortStatus[kPortIndexInput] == ENABLED 2070 && mPortStatus[kPortIndexOutput] == ENABLED) { 2071 CODEC_LOGV("Finished flushing both ports, now continuing from" 2072 " seek-time."); 2073 2074 // We implicitly resume pulling on our upstream source. 2075 mPaused = false; 2076 2077 drainInputBuffers(); 2078 fillOutputBuffers(); 2079 } 2080 } 2081 2082 break; 2083 } 2084 2085 default: 2086 { 2087 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data); 2088 break; 2089 } 2090 } 2091 } 2092 2093 void OMXCodec::onStateChange(OMX_STATETYPE newState) { 2094 CODEC_LOGV("onStateChange %d", newState); 2095 2096 switch (newState) { 2097 case OMX_StateIdle: 2098 { 2099 CODEC_LOGV("Now Idle."); 2100 if (mState == LOADED_TO_IDLE) { 2101 status_t err = mOMX->sendCommand( 2102 mNode, OMX_CommandStateSet, OMX_StateExecuting); 2103 2104 CHECK_EQ(err, OK); 2105 2106 setState(IDLE_TO_EXECUTING); 2107 } else { 2108 CHECK_EQ(mState, EXECUTING_TO_IDLE); 2109 2110 CHECK_EQ( 2111 countBuffersWeOwn(mPortBuffers[kPortIndexInput]), 2112 mPortBuffers[kPortIndexInput].size()); 2113 2114 CHECK_EQ( 2115 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]), 2116 mPortBuffers[kPortIndexOutput].size()); 2117 2118 status_t err = mOMX->sendCommand( 2119 mNode, OMX_CommandStateSet, OMX_StateLoaded); 2120 2121 CHECK_EQ(err, OK); 2122 2123 err = freeBuffersOnPort(kPortIndexInput); 2124 CHECK_EQ(err, OK); 2125 2126 err = freeBuffersOnPort(kPortIndexOutput); 2127 CHECK_EQ(err, OK); 2128 2129 mPortStatus[kPortIndexInput] = ENABLED; 2130 mPortStatus[kPortIndexOutput] = ENABLED; 2131 2132 setState(IDLE_TO_LOADED); 2133 } 2134 break; 2135 } 2136 2137 case OMX_StateExecuting: 2138 { 2139 CHECK_EQ(mState, IDLE_TO_EXECUTING); 2140 2141 CODEC_LOGV("Now Executing."); 2142 2143 setState(EXECUTING); 2144 2145 // Buffers will be submitted to the component in the first 2146 // call to OMXCodec::read as mInitialBufferSubmit is true at 2147 // this point. This ensures that this on_message call returns, 2148 // releases the lock and ::init can notice the state change and 2149 // itself return. 2150 break; 2151 } 2152 2153 case OMX_StateLoaded: 2154 { 2155 CHECK_EQ(mState, IDLE_TO_LOADED); 2156 2157 CODEC_LOGV("Now Loaded."); 2158 2159 setState(LOADED); 2160 break; 2161 } 2162 2163 case OMX_StateInvalid: 2164 { 2165 setState(ERROR); 2166 break; 2167 } 2168 2169 default: 2170 { 2171 CHECK(!"should not be here."); 2172 break; 2173 } 2174 } 2175 } 2176 2177 // static 2178 size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) { 2179 size_t n = 0; 2180 for (size_t i = 0; i < buffers.size(); ++i) { 2181 if (!buffers[i].mOwnedByComponent) { 2182 ++n; 2183 } 2184 } 2185 2186 return n; 2187 } 2188 2189 status_t OMXCodec::freeBuffersOnPort( 2190 OMX_U32 portIndex, bool onlyThoseWeOwn) { 2191 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; 2192 2193 status_t stickyErr = OK; 2194 2195 for (size_t i = buffers->size(); i-- > 0;) { 2196 BufferInfo *info = &buffers->editItemAt(i); 2197 2198 if (onlyThoseWeOwn && info->mOwnedByComponent) { 2199 continue; 2200 } 2201 2202 CHECK_EQ(info->mOwnedByComponent, false); 2203 2204 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex); 2205 2206 status_t err = 2207 mOMX->freeBuffer(mNode, portIndex, info->mBuffer); 2208 2209 if (err != OK) { 2210 stickyErr = err; 2211 } 2212 2213 if (info->mMediaBuffer != NULL) { 2214 info->mMediaBuffer->setObserver(NULL); 2215 2216 // Make sure nobody but us owns this buffer at this point. 2217 CHECK_EQ(info->mMediaBuffer->refcount(), 0); 2218 2219 info->mMediaBuffer->release(); 2220 info->mMediaBuffer = NULL; 2221 } 2222 2223 buffers->removeAt(i); 2224 } 2225 2226 CHECK(onlyThoseWeOwn || buffers->isEmpty()); 2227 2228 return stickyErr; 2229 } 2230 2231 void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) { 2232 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex); 2233 2234 CHECK_EQ(mState, EXECUTING); 2235 CHECK_EQ(portIndex, kPortIndexOutput); 2236 setState(RECONFIGURING); 2237 2238 if (mQuirks & kNeedsFlushBeforeDisable) { 2239 if (!flushPortAsync(portIndex)) { 2240 onCmdComplete(OMX_CommandFlush, portIndex); 2241 } 2242 } else { 2243 disablePortAsync(portIndex); 2244 } 2245 } 2246 2247 bool OMXCodec::flushPortAsync(OMX_U32 portIndex) { 2248 CHECK(mState == EXECUTING || mState == RECONFIGURING 2249 || mState == EXECUTING_TO_IDLE); 2250 2251 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.", 2252 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]), 2253 mPortBuffers[portIndex].size()); 2254 2255 CHECK_EQ(mPortStatus[portIndex], ENABLED); 2256 mPortStatus[portIndex] = SHUTTING_DOWN; 2257 2258 if ((mQuirks & kRequiresFlushCompleteEmulation) 2259 && countBuffersWeOwn(mPortBuffers[portIndex]) 2260 == mPortBuffers[portIndex].size()) { 2261 // No flush is necessary and this component fails to send a 2262 // flush-complete event in this case. 2263 2264 return false; 2265 } 2266 2267 status_t err = 2268 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex); 2269 CHECK_EQ(err, OK); 2270 2271 return true; 2272 } 2273 2274 void OMXCodec::disablePortAsync(OMX_U32 portIndex) { 2275 CHECK(mState == EXECUTING || mState == RECONFIGURING); 2276 2277 CHECK_EQ(mPortStatus[portIndex], ENABLED); 2278 mPortStatus[portIndex] = DISABLING; 2279 2280 status_t err = 2281 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex); 2282 CHECK_EQ(err, OK); 2283 2284 freeBuffersOnPort(portIndex, true); 2285 } 2286 2287 void OMXCodec::enablePortAsync(OMX_U32 portIndex) { 2288 CHECK(mState == EXECUTING || mState == RECONFIGURING); 2289 2290 CHECK_EQ(mPortStatus[portIndex], DISABLED); 2291 mPortStatus[portIndex] = ENABLING; 2292 2293 status_t err = 2294 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex); 2295 CHECK_EQ(err, OK); 2296 } 2297 2298 void OMXCodec::fillOutputBuffers() { 2299 CHECK_EQ(mState, EXECUTING); 2300 2301 // This is a workaround for some decoders not properly reporting 2302 // end-of-output-stream. If we own all input buffers and also own 2303 // all output buffers and we already signalled end-of-input-stream, 2304 // the end-of-output-stream is implied. 2305 if (mSignalledEOS 2306 && countBuffersWeOwn(mPortBuffers[kPortIndexInput]) 2307 == mPortBuffers[kPortIndexInput].size() 2308 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput]) 2309 == mPortBuffers[kPortIndexOutput].size()) { 2310 mNoMoreOutputData = true; 2311 mBufferFilled.signal(); 2312 2313 return; 2314 } 2315 2316 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 2317 for (size_t i = 0; i < buffers->size(); ++i) { 2318 fillOutputBuffer(&buffers->editItemAt(i)); 2319 } 2320 } 2321 2322 void OMXCodec::drainInputBuffers() { 2323 CHECK(mState == EXECUTING || mState == RECONFIGURING); 2324 2325 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 2326 for (size_t i = 0; i < buffers->size(); ++i) { 2327 drainInputBuffer(&buffers->editItemAt(i)); 2328 } 2329 } 2330 2331 void OMXCodec::drainInputBuffer(BufferInfo *info) { 2332 CHECK_EQ(info->mOwnedByComponent, false); 2333 2334 if (mSignalledEOS) { 2335 return; 2336 } 2337 2338 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) { 2339 const CodecSpecificData *specific = 2340 mCodecSpecificData[mCodecSpecificDataIndex]; 2341 2342 size_t size = specific->mSize; 2343 2344 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME) 2345 && !(mQuirks & kWantsNALFragments)) { 2346 static const uint8_t kNALStartCode[4] = 2347 { 0x00, 0x00, 0x00, 0x01 }; 2348 2349 CHECK(info->mSize >= specific->mSize + 4); 2350 2351 size += 4; 2352 2353 memcpy(info->mData, kNALStartCode, 4); 2354 memcpy((uint8_t *)info->mData + 4, 2355 specific->mData, specific->mSize); 2356 } else { 2357 CHECK(info->mSize >= specific->mSize); 2358 memcpy(info->mData, specific->mData, specific->mSize); 2359 } 2360 2361 mNoMoreOutputData = false; 2362 2363 CODEC_LOGV("calling emptyBuffer with codec specific data"); 2364 2365 status_t err = mOMX->emptyBuffer( 2366 mNode, info->mBuffer, 0, size, 2367 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG, 2368 0); 2369 CHECK_EQ(err, OK); 2370 2371 info->mOwnedByComponent = true; 2372 2373 ++mCodecSpecificDataIndex; 2374 return; 2375 } 2376 2377 if (mPaused) { 2378 return; 2379 } 2380 2381 status_t err; 2382 2383 bool signalEOS = false; 2384 int64_t timestampUs = 0; 2385 2386 size_t offset = 0; 2387 int32_t n = 0; 2388 for (;;) { 2389 MediaBuffer *srcBuffer; 2390 MediaSource::ReadOptions options; 2391 if (mSkipTimeUs >= 0) { 2392 options.setSkipFrame(mSkipTimeUs); 2393 } 2394 if (mSeekTimeUs >= 0) { 2395 if (mLeftOverBuffer) { 2396 mLeftOverBuffer->release(); 2397 mLeftOverBuffer = NULL; 2398 } 2399 options.setSeekTo(mSeekTimeUs, mSeekMode); 2400 2401 mSeekTimeUs = -1; 2402 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC; 2403 mBufferFilled.signal(); 2404 2405 err = mSource->read(&srcBuffer, &options); 2406 2407 if (err == OK) { 2408 int64_t targetTimeUs; 2409 if (srcBuffer->meta_data()->findInt64( 2410 kKeyTargetTime, &targetTimeUs) 2411 && targetTimeUs >= 0) { 2412 mTargetTimeUs = targetTimeUs; 2413 } else { 2414 mTargetTimeUs = -1; 2415 } 2416 } 2417 } else if (mLeftOverBuffer) { 2418 srcBuffer = mLeftOverBuffer; 2419 mLeftOverBuffer = NULL; 2420 2421 err = OK; 2422 } else { 2423 err = mSource->read(&srcBuffer, &options); 2424 } 2425 2426 if (err != OK) { 2427 signalEOS = true; 2428 mFinalStatus = err; 2429 mSignalledEOS = true; 2430 break; 2431 } 2432 2433 size_t remainingBytes = info->mSize - offset; 2434 2435 if (srcBuffer->range_length() > remainingBytes) { 2436 if (offset == 0) { 2437 CODEC_LOGE( 2438 "Codec's input buffers are too small to accomodate " 2439 "buffer read from source (info->mSize = %d, srcLength = %d)", 2440 info->mSize, srcBuffer->range_length()); 2441 2442 srcBuffer->release(); 2443 srcBuffer = NULL; 2444 2445 setState(ERROR); 2446 return; 2447 } 2448 2449 mLeftOverBuffer = srcBuffer; 2450 break; 2451 } 2452 2453 // Do not release the media buffer if it stores meta data 2454 // instead of YUV data. The release is delayed until 2455 // EMPTY_BUFFER_DONE callback is received. 2456 bool releaseBuffer = true; 2457 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) { 2458 CHECK(mOMXLivesLocally && offset == 0); 2459 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer; 2460 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset(); 2461 } else { 2462 if (mQuirks & kStoreMetaDataInInputVideoBuffers) { 2463 releaseBuffer = false; 2464 info->mMediaBuffer = srcBuffer; 2465 } 2466 memcpy((uint8_t *)info->mData + offset, 2467 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(), 2468 srcBuffer->range_length()); 2469 } 2470 2471 int64_t lastBufferTimeUs; 2472 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs)); 2473 CHECK(lastBufferTimeUs >= 0); 2474 2475 if (offset == 0) { 2476 timestampUs = lastBufferTimeUs; 2477 } 2478 2479 offset += srcBuffer->range_length(); 2480 2481 if (releaseBuffer) { 2482 srcBuffer->release(); 2483 srcBuffer = NULL; 2484 } 2485 2486 ++n; 2487 2488 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) { 2489 break; 2490 } 2491 2492 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs; 2493 2494 if (coalescedDurationUs > 250000ll) { 2495 // Don't coalesce more than 250ms worth of encoded data at once. 2496 break; 2497 } 2498 } 2499 2500 if (n > 1) { 2501 LOGV("coalesced %d frames into one input buffer", n); 2502 } 2503 2504 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME; 2505 2506 if (signalEOS) { 2507 flags |= OMX_BUFFERFLAG_EOS; 2508 } else { 2509 mNoMoreOutputData = false; 2510 } 2511 2512 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), " 2513 "timestamp %lld us (%.2f secs)", 2514 info->mBuffer, offset, 2515 timestampUs, timestampUs / 1E6); 2516 2517 err = mOMX->emptyBuffer( 2518 mNode, info->mBuffer, 0, offset, 2519 flags, timestampUs); 2520 2521 if (err != OK) { 2522 setState(ERROR); 2523 return; 2524 } 2525 2526 info->mOwnedByComponent = true; 2527 2528 // This component does not ever signal the EOS flag on output buffers, 2529 // Thanks for nothing. 2530 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) { 2531 mNoMoreOutputData = true; 2532 mBufferFilled.signal(); 2533 } 2534 } 2535 2536 void OMXCodec::fillOutputBuffer(BufferInfo *info) { 2537 CHECK_EQ(info->mOwnedByComponent, false); 2538 2539 if (mNoMoreOutputData) { 2540 CODEC_LOGV("There is no more output data available, not " 2541 "calling fillOutputBuffer"); 2542 return; 2543 } 2544 2545 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer); 2546 status_t err = mOMX->fillBuffer(mNode, info->mBuffer); 2547 2548 if (err != OK) { 2549 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err); 2550 2551 setState(ERROR); 2552 return; 2553 } 2554 2555 info->mOwnedByComponent = true; 2556 } 2557 2558 void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) { 2559 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; 2560 for (size_t i = 0; i < buffers->size(); ++i) { 2561 if ((*buffers)[i].mBuffer == buffer) { 2562 drainInputBuffer(&buffers->editItemAt(i)); 2563 return; 2564 } 2565 } 2566 2567 CHECK(!"should not be here."); 2568 } 2569 2570 void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) { 2571 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 2572 for (size_t i = 0; i < buffers->size(); ++i) { 2573 if ((*buffers)[i].mBuffer == buffer) { 2574 fillOutputBuffer(&buffers->editItemAt(i)); 2575 return; 2576 } 2577 } 2578 2579 CHECK(!"should not be here."); 2580 } 2581 2582 void OMXCodec::setState(State newState) { 2583 mState = newState; 2584 mAsyncCompletion.signal(); 2585 2586 // This may cause some spurious wakeups but is necessary to 2587 // unblock the reader if we enter ERROR state. 2588 mBufferFilled.signal(); 2589 } 2590 2591 void OMXCodec::setRawAudioFormat( 2592 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) { 2593 2594 // port definition 2595 OMX_PARAM_PORTDEFINITIONTYPE def; 2596 InitOMXParams(&def); 2597 def.nPortIndex = portIndex; 2598 status_t err = mOMX->getParameter( 2599 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2600 CHECK_EQ(err, OK); 2601 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM; 2602 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition, 2603 &def, sizeof(def)), OK); 2604 2605 // pcm param 2606 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams; 2607 InitOMXParams(&pcmParams); 2608 pcmParams.nPortIndex = portIndex; 2609 2610 err = mOMX->getParameter( 2611 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); 2612 2613 CHECK_EQ(err, OK); 2614 2615 pcmParams.nChannels = numChannels; 2616 pcmParams.eNumData = OMX_NumericalDataSigned; 2617 pcmParams.bInterleaved = OMX_TRUE; 2618 pcmParams.nBitPerSample = 16; 2619 pcmParams.nSamplingRate = sampleRate; 2620 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear; 2621 2622 if (numChannels == 1) { 2623 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF; 2624 } else { 2625 CHECK_EQ(numChannels, 2); 2626 2627 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF; 2628 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF; 2629 } 2630 2631 err = mOMX->setParameter( 2632 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); 2633 2634 CHECK_EQ(err, OK); 2635 } 2636 2637 static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) { 2638 if (isAMRWB) { 2639 if (bps <= 6600) { 2640 return OMX_AUDIO_AMRBandModeWB0; 2641 } else if (bps <= 8850) { 2642 return OMX_AUDIO_AMRBandModeWB1; 2643 } else if (bps <= 12650) { 2644 return OMX_AUDIO_AMRBandModeWB2; 2645 } else if (bps <= 14250) { 2646 return OMX_AUDIO_AMRBandModeWB3; 2647 } else if (bps <= 15850) { 2648 return OMX_AUDIO_AMRBandModeWB4; 2649 } else if (bps <= 18250) { 2650 return OMX_AUDIO_AMRBandModeWB5; 2651 } else if (bps <= 19850) { 2652 return OMX_AUDIO_AMRBandModeWB6; 2653 } else if (bps <= 23050) { 2654 return OMX_AUDIO_AMRBandModeWB7; 2655 } 2656 2657 // 23850 bps 2658 return OMX_AUDIO_AMRBandModeWB8; 2659 } else { // AMRNB 2660 if (bps <= 4750) { 2661 return OMX_AUDIO_AMRBandModeNB0; 2662 } else if (bps <= 5150) { 2663 return OMX_AUDIO_AMRBandModeNB1; 2664 } else if (bps <= 5900) { 2665 return OMX_AUDIO_AMRBandModeNB2; 2666 } else if (bps <= 6700) { 2667 return OMX_AUDIO_AMRBandModeNB3; 2668 } else if (bps <= 7400) { 2669 return OMX_AUDIO_AMRBandModeNB4; 2670 } else if (bps <= 7950) { 2671 return OMX_AUDIO_AMRBandModeNB5; 2672 } else if (bps <= 10200) { 2673 return OMX_AUDIO_AMRBandModeNB6; 2674 } 2675 2676 // 12200 bps 2677 return OMX_AUDIO_AMRBandModeNB7; 2678 } 2679 } 2680 2681 void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) { 2682 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput; 2683 2684 OMX_AUDIO_PARAM_AMRTYPE def; 2685 InitOMXParams(&def); 2686 def.nPortIndex = portIndex; 2687 2688 status_t err = 2689 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 2690 2691 CHECK_EQ(err, OK); 2692 2693 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF; 2694 2695 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate); 2696 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); 2697 CHECK_EQ(err, OK); 2698 2699 //////////////////////// 2700 2701 if (mIsEncoder) { 2702 sp<MetaData> format = mSource->getFormat(); 2703 int32_t sampleRate; 2704 int32_t numChannels; 2705 CHECK(format->findInt32(kKeySampleRate, &sampleRate)); 2706 CHECK(format->findInt32(kKeyChannelCount, &numChannels)); 2707 2708 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 2709 } 2710 } 2711 2712 void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) { 2713 CHECK(numChannels == 1 || numChannels == 2); 2714 if (mIsEncoder) { 2715 //////////////// input port //////////////////// 2716 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); 2717 2718 //////////////// output port //////////////////// 2719 // format 2720 OMX_AUDIO_PARAM_PORTFORMATTYPE format; 2721 format.nPortIndex = kPortIndexOutput; 2722 format.nIndex = 0; 2723 status_t err = OMX_ErrorNone; 2724 while (OMX_ErrorNone == err) { 2725 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat, 2726 &format, sizeof(format)), OK); 2727 if (format.eEncoding == OMX_AUDIO_CodingAAC) { 2728 break; 2729 } 2730 format.nIndex++; 2731 } 2732 CHECK_EQ(OK, err); 2733 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat, 2734 &format, sizeof(format)), OK); 2735 2736 // port definition 2737 OMX_PARAM_PORTDEFINITIONTYPE def; 2738 InitOMXParams(&def); 2739 def.nPortIndex = kPortIndexOutput; 2740 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition, 2741 &def, sizeof(def)), OK); 2742 def.format.audio.bFlagErrorConcealment = OMX_TRUE; 2743 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC; 2744 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition, 2745 &def, sizeof(def)), OK); 2746 2747 // profile 2748 OMX_AUDIO_PARAM_AACPROFILETYPE profile; 2749 InitOMXParams(&profile); 2750 profile.nPortIndex = kPortIndexOutput; 2751 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac, 2752 &profile, sizeof(profile)), OK); 2753 profile.nChannels = numChannels; 2754 profile.eChannelMode = (numChannels == 1? 2755 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo); 2756 profile.nSampleRate = sampleRate; 2757 profile.nBitRate = bitRate; 2758 profile.nAudioBandWidth = 0; 2759 profile.nFrameLength = 0; 2760 profile.nAACtools = OMX_AUDIO_AACToolAll; 2761 profile.nAACERtools = OMX_AUDIO_AACERNone; 2762 profile.eAACProfile = OMX_AUDIO_AACObjectLC; 2763 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF; 2764 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac, 2765 &profile, sizeof(profile)), OK); 2766 2767 } else { 2768 OMX_AUDIO_PARAM_AACPROFILETYPE profile; 2769 InitOMXParams(&profile); 2770 profile.nPortIndex = kPortIndexInput; 2771 2772 status_t err = mOMX->getParameter( 2773 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); 2774 CHECK_EQ(err, OK); 2775 2776 profile.nChannels = numChannels; 2777 profile.nSampleRate = sampleRate; 2778 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS; 2779 2780 err = mOMX->setParameter( 2781 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); 2782 CHECK_EQ(err, OK); 2783 } 2784 } 2785 2786 void OMXCodec::setImageOutputFormat( 2787 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) { 2788 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height); 2789 2790 #if 0 2791 OMX_INDEXTYPE index; 2792 status_t err = mOMX->get_extension_index( 2793 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index); 2794 CHECK_EQ(err, OK); 2795 2796 err = mOMX->set_config(mNode, index, &format, sizeof(format)); 2797 CHECK_EQ(err, OK); 2798 #endif 2799 2800 OMX_PARAM_PORTDEFINITIONTYPE def; 2801 InitOMXParams(&def); 2802 def.nPortIndex = kPortIndexOutput; 2803 2804 status_t err = mOMX->getParameter( 2805 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2806 CHECK_EQ(err, OK); 2807 2808 CHECK_EQ(def.eDomain, OMX_PortDomainImage); 2809 2810 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 2811 2812 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused); 2813 imageDef->eColorFormat = format; 2814 imageDef->nFrameWidth = width; 2815 imageDef->nFrameHeight = height; 2816 2817 switch (format) { 2818 case OMX_COLOR_FormatYUV420PackedPlanar: 2819 case OMX_COLOR_FormatYUV411Planar: 2820 { 2821 def.nBufferSize = (width * height * 3) / 2; 2822 break; 2823 } 2824 2825 case OMX_COLOR_FormatCbYCrY: 2826 { 2827 def.nBufferSize = width * height * 2; 2828 break; 2829 } 2830 2831 case OMX_COLOR_Format32bitARGB8888: 2832 { 2833 def.nBufferSize = width * height * 4; 2834 break; 2835 } 2836 2837 case OMX_COLOR_Format16bitARGB4444: 2838 case OMX_COLOR_Format16bitARGB1555: 2839 case OMX_COLOR_Format16bitRGB565: 2840 case OMX_COLOR_Format16bitBGR565: 2841 { 2842 def.nBufferSize = width * height * 2; 2843 break; 2844 } 2845 2846 default: 2847 CHECK(!"Should not be here. Unknown color format."); 2848 break; 2849 } 2850 2851 def.nBufferCountActual = def.nBufferCountMin; 2852 2853 err = mOMX->setParameter( 2854 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2855 CHECK_EQ(err, OK); 2856 } 2857 2858 void OMXCodec::setJPEGInputFormat( 2859 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) { 2860 OMX_PARAM_PORTDEFINITIONTYPE def; 2861 InitOMXParams(&def); 2862 def.nPortIndex = kPortIndexInput; 2863 2864 status_t err = mOMX->getParameter( 2865 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2866 CHECK_EQ(err, OK); 2867 2868 CHECK_EQ(def.eDomain, OMX_PortDomainImage); 2869 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 2870 2871 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG); 2872 imageDef->nFrameWidth = width; 2873 imageDef->nFrameHeight = height; 2874 2875 def.nBufferSize = compressedSize; 2876 def.nBufferCountActual = def.nBufferCountMin; 2877 2878 err = mOMX->setParameter( 2879 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 2880 CHECK_EQ(err, OK); 2881 } 2882 2883 void OMXCodec::addCodecSpecificData(const void *data, size_t size) { 2884 CodecSpecificData *specific = 2885 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1); 2886 2887 specific->mSize = size; 2888 memcpy(specific->mData, data, size); 2889 2890 mCodecSpecificData.push(specific); 2891 } 2892 2893 void OMXCodec::clearCodecSpecificData() { 2894 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) { 2895 free(mCodecSpecificData.editItemAt(i)); 2896 } 2897 mCodecSpecificData.clear(); 2898 mCodecSpecificDataIndex = 0; 2899 } 2900 2901 status_t OMXCodec::start(MetaData *meta) { 2902 Mutex::Autolock autoLock(mLock); 2903 2904 if (mState != LOADED) { 2905 return UNKNOWN_ERROR; 2906 } 2907 2908 sp<MetaData> params = new MetaData; 2909 if (mQuirks & kWantsNALFragments) { 2910 params->setInt32(kKeyWantsNALFragments, true); 2911 } 2912 if (meta) { 2913 int64_t startTimeUs = 0; 2914 int64_t timeUs; 2915 if (meta->findInt64(kKeyTime, &timeUs)) { 2916 startTimeUs = timeUs; 2917 } 2918 params->setInt64(kKeyTime, startTimeUs); 2919 } 2920 status_t err = mSource->start(params.get()); 2921 2922 if (err != OK) { 2923 return err; 2924 } 2925 2926 mCodecSpecificDataIndex = 0; 2927 mInitialBufferSubmit = true; 2928 mSignalledEOS = false; 2929 mNoMoreOutputData = false; 2930 mOutputPortSettingsHaveChanged = false; 2931 mSeekTimeUs = -1; 2932 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC; 2933 mTargetTimeUs = -1; 2934 mFilledBuffers.clear(); 2935 mPaused = false; 2936 2937 return init(); 2938 } 2939 2940 status_t OMXCodec::stop() { 2941 CODEC_LOGV("stop mState=%d", mState); 2942 2943 Mutex::Autolock autoLock(mLock); 2944 2945 while (isIntermediateState(mState)) { 2946 mAsyncCompletion.wait(mLock); 2947 } 2948 2949 switch (mState) { 2950 case LOADED: 2951 case ERROR: 2952 break; 2953 2954 case EXECUTING: 2955 { 2956 setState(EXECUTING_TO_IDLE); 2957 2958 if (mQuirks & kRequiresFlushBeforeShutdown) { 2959 CODEC_LOGV("This component requires a flush before transitioning " 2960 "from EXECUTING to IDLE..."); 2961 2962 bool emulateInputFlushCompletion = 2963 !flushPortAsync(kPortIndexInput); 2964 2965 bool emulateOutputFlushCompletion = 2966 !flushPortAsync(kPortIndexOutput); 2967 2968 if (emulateInputFlushCompletion) { 2969 onCmdComplete(OMX_CommandFlush, kPortIndexInput); 2970 } 2971 2972 if (emulateOutputFlushCompletion) { 2973 onCmdComplete(OMX_CommandFlush, kPortIndexOutput); 2974 } 2975 } else { 2976 mPortStatus[kPortIndexInput] = SHUTTING_DOWN; 2977 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; 2978 2979 status_t err = 2980 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); 2981 CHECK_EQ(err, OK); 2982 } 2983 2984 while (mState != LOADED && mState != ERROR) { 2985 mAsyncCompletion.wait(mLock); 2986 } 2987 2988 break; 2989 } 2990 2991 default: 2992 { 2993 CHECK(!"should not be here."); 2994 break; 2995 } 2996 } 2997 2998 if (mLeftOverBuffer) { 2999 mLeftOverBuffer->release(); 3000 mLeftOverBuffer = NULL; 3001 } 3002 3003 mSource->stop(); 3004 3005 CODEC_LOGV("stopped"); 3006 3007 return OK; 3008 } 3009 3010 sp<MetaData> OMXCodec::getFormat() { 3011 Mutex::Autolock autoLock(mLock); 3012 3013 return mOutputFormat; 3014 } 3015 3016 status_t OMXCodec::read( 3017 MediaBuffer **buffer, const ReadOptions *options) { 3018 *buffer = NULL; 3019 3020 Mutex::Autolock autoLock(mLock); 3021 3022 if (mState != EXECUTING && mState != RECONFIGURING) { 3023 return UNKNOWN_ERROR; 3024 } 3025 3026 bool seeking = false; 3027 int64_t seekTimeUs; 3028 ReadOptions::SeekMode seekMode; 3029 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) { 3030 seeking = true; 3031 } 3032 int64_t skipTimeUs; 3033 if (options && options->getSkipFrame(&skipTimeUs)) { 3034 mSkipTimeUs = skipTimeUs; 3035 } else { 3036 mSkipTimeUs = -1; 3037 } 3038 3039 if (mInitialBufferSubmit) { 3040 mInitialBufferSubmit = false; 3041 3042 if (seeking) { 3043 CHECK(seekTimeUs >= 0); 3044 mSeekTimeUs = seekTimeUs; 3045 mSeekMode = seekMode; 3046 3047 // There's no reason to trigger the code below, there's 3048 // nothing to flush yet. 3049 seeking = false; 3050 mPaused = false; 3051 } 3052 3053 drainInputBuffers(); 3054 3055 if (mState == EXECUTING) { 3056 // Otherwise mState == RECONFIGURING and this code will trigger 3057 // after the output port is reenabled. 3058 fillOutputBuffers(); 3059 } 3060 } 3061 3062 if (seeking) { 3063 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6); 3064 3065 mSignalledEOS = false; 3066 3067 CHECK(seekTimeUs >= 0); 3068 mSeekTimeUs = seekTimeUs; 3069 mSeekMode = seekMode; 3070 3071 mFilledBuffers.clear(); 3072 3073 CHECK_EQ(mState, EXECUTING); 3074 3075 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput); 3076 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput); 3077 3078 if (emulateInputFlushCompletion) { 3079 onCmdComplete(OMX_CommandFlush, kPortIndexInput); 3080 } 3081 3082 if (emulateOutputFlushCompletion) { 3083 onCmdComplete(OMX_CommandFlush, kPortIndexOutput); 3084 } 3085 3086 while (mSeekTimeUs >= 0) { 3087 mBufferFilled.wait(mLock); 3088 } 3089 } 3090 3091 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) { 3092 mBufferFilled.wait(mLock); 3093 } 3094 3095 if (mState == ERROR) { 3096 return UNKNOWN_ERROR; 3097 } 3098 3099 if (mFilledBuffers.empty()) { 3100 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM; 3101 } 3102 3103 if (mOutputPortSettingsHaveChanged) { 3104 mOutputPortSettingsHaveChanged = false; 3105 3106 return INFO_FORMAT_CHANGED; 3107 } 3108 3109 size_t index = *mFilledBuffers.begin(); 3110 mFilledBuffers.erase(mFilledBuffers.begin()); 3111 3112 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index); 3113 info->mMediaBuffer->add_ref(); 3114 *buffer = info->mMediaBuffer; 3115 3116 return OK; 3117 } 3118 3119 void OMXCodec::signalBufferReturned(MediaBuffer *buffer) { 3120 Mutex::Autolock autoLock(mLock); 3121 3122 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; 3123 for (size_t i = 0; i < buffers->size(); ++i) { 3124 BufferInfo *info = &buffers->editItemAt(i); 3125 3126 if (info->mMediaBuffer == buffer) { 3127 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED); 3128 fillOutputBuffer(info); 3129 return; 3130 } 3131 } 3132 3133 CHECK(!"should not be here."); 3134 } 3135 3136 static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) { 3137 static const char *kNames[] = { 3138 "OMX_IMAGE_CodingUnused", 3139 "OMX_IMAGE_CodingAutoDetect", 3140 "OMX_IMAGE_CodingJPEG", 3141 "OMX_IMAGE_CodingJPEG2K", 3142 "OMX_IMAGE_CodingEXIF", 3143 "OMX_IMAGE_CodingTIFF", 3144 "OMX_IMAGE_CodingGIF", 3145 "OMX_IMAGE_CodingPNG", 3146 "OMX_IMAGE_CodingLZW", 3147 "OMX_IMAGE_CodingBMP", 3148 }; 3149 3150 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 3151 3152 if (type < 0 || (size_t)type >= numNames) { 3153 return "UNKNOWN"; 3154 } else { 3155 return kNames[type]; 3156 } 3157 } 3158 3159 static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) { 3160 static const char *kNames[] = { 3161 "OMX_COLOR_FormatUnused", 3162 "OMX_COLOR_FormatMonochrome", 3163 "OMX_COLOR_Format8bitRGB332", 3164 "OMX_COLOR_Format12bitRGB444", 3165 "OMX_COLOR_Format16bitARGB4444", 3166 "OMX_COLOR_Format16bitARGB1555", 3167 "OMX_COLOR_Format16bitRGB565", 3168 "OMX_COLOR_Format16bitBGR565", 3169 "OMX_COLOR_Format18bitRGB666", 3170 "OMX_COLOR_Format18bitARGB1665", 3171 "OMX_COLOR_Format19bitARGB1666", 3172 "OMX_COLOR_Format24bitRGB888", 3173 "OMX_COLOR_Format24bitBGR888", 3174 "OMX_COLOR_Format24bitARGB1887", 3175 "OMX_COLOR_Format25bitARGB1888", 3176 "OMX_COLOR_Format32bitBGRA8888", 3177 "OMX_COLOR_Format32bitARGB8888", 3178 "OMX_COLOR_FormatYUV411Planar", 3179 "OMX_COLOR_FormatYUV411PackedPlanar", 3180 "OMX_COLOR_FormatYUV420Planar", 3181 "OMX_COLOR_FormatYUV420PackedPlanar", 3182 "OMX_COLOR_FormatYUV420SemiPlanar", 3183 "OMX_COLOR_FormatYUV422Planar", 3184 "OMX_COLOR_FormatYUV422PackedPlanar", 3185 "OMX_COLOR_FormatYUV422SemiPlanar", 3186 "OMX_COLOR_FormatYCbYCr", 3187 "OMX_COLOR_FormatYCrYCb", 3188 "OMX_COLOR_FormatCbYCrY", 3189 "OMX_COLOR_FormatCrYCbY", 3190 "OMX_COLOR_FormatYUV444Interleaved", 3191 "OMX_COLOR_FormatRawBayer8bit", 3192 "OMX_COLOR_FormatRawBayer10bit", 3193 "OMX_COLOR_FormatRawBayer8bitcompressed", 3194 "OMX_COLOR_FormatL2", 3195 "OMX_COLOR_FormatL4", 3196 "OMX_COLOR_FormatL8", 3197 "OMX_COLOR_FormatL16", 3198 "OMX_COLOR_FormatL24", 3199 "OMX_COLOR_FormatL32", 3200 "OMX_COLOR_FormatYUV420PackedSemiPlanar", 3201 "OMX_COLOR_FormatYUV422PackedSemiPlanar", 3202 "OMX_COLOR_Format18BitBGR666", 3203 "OMX_COLOR_Format24BitARGB6666", 3204 "OMX_COLOR_Format24BitABGR6666", 3205 }; 3206 3207 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 3208 3209 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) { 3210 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar"; 3211 } else if (type < 0 || (size_t)type >= numNames) { 3212 return "UNKNOWN"; 3213 } else { 3214 return kNames[type]; 3215 } 3216 } 3217 3218 static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) { 3219 static const char *kNames[] = { 3220 "OMX_VIDEO_CodingUnused", 3221 "OMX_VIDEO_CodingAutoDetect", 3222 "OMX_VIDEO_CodingMPEG2", 3223 "OMX_VIDEO_CodingH263", 3224 "OMX_VIDEO_CodingMPEG4", 3225 "OMX_VIDEO_CodingWMV", 3226 "OMX_VIDEO_CodingRV", 3227 "OMX_VIDEO_CodingAVC", 3228 "OMX_VIDEO_CodingMJPEG", 3229 }; 3230 3231 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 3232 3233 if (type < 0 || (size_t)type >= numNames) { 3234 return "UNKNOWN"; 3235 } else { 3236 return kNames[type]; 3237 } 3238 } 3239 3240 static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) { 3241 static const char *kNames[] = { 3242 "OMX_AUDIO_CodingUnused", 3243 "OMX_AUDIO_CodingAutoDetect", 3244 "OMX_AUDIO_CodingPCM", 3245 "OMX_AUDIO_CodingADPCM", 3246 "OMX_AUDIO_CodingAMR", 3247 "OMX_AUDIO_CodingGSMFR", 3248 "OMX_AUDIO_CodingGSMEFR", 3249 "OMX_AUDIO_CodingGSMHR", 3250 "OMX_AUDIO_CodingPDCFR", 3251 "OMX_AUDIO_CodingPDCEFR", 3252 "OMX_AUDIO_CodingPDCHR", 3253 "OMX_AUDIO_CodingTDMAFR", 3254 "OMX_AUDIO_CodingTDMAEFR", 3255 "OMX_AUDIO_CodingQCELP8", 3256 "OMX_AUDIO_CodingQCELP13", 3257 "OMX_AUDIO_CodingEVRC", 3258 "OMX_AUDIO_CodingSMV", 3259 "OMX_AUDIO_CodingG711", 3260 "OMX_AUDIO_CodingG723", 3261 "OMX_AUDIO_CodingG726", 3262 "OMX_AUDIO_CodingG729", 3263 "OMX_AUDIO_CodingAAC", 3264 "OMX_AUDIO_CodingMP3", 3265 "OMX_AUDIO_CodingSBC", 3266 "OMX_AUDIO_CodingVORBIS", 3267 "OMX_AUDIO_CodingWMA", 3268 "OMX_AUDIO_CodingRA", 3269 "OMX_AUDIO_CodingMIDI", 3270 }; 3271 3272 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 3273 3274 if (type < 0 || (size_t)type >= numNames) { 3275 return "UNKNOWN"; 3276 } else { 3277 return kNames[type]; 3278 } 3279 } 3280 3281 static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) { 3282 static const char *kNames[] = { 3283 "OMX_AUDIO_PCMModeLinear", 3284 "OMX_AUDIO_PCMModeALaw", 3285 "OMX_AUDIO_PCMModeMULaw", 3286 }; 3287 3288 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 3289 3290 if (type < 0 || (size_t)type >= numNames) { 3291 return "UNKNOWN"; 3292 } else { 3293 return kNames[type]; 3294 } 3295 } 3296 3297 static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) { 3298 static const char *kNames[] = { 3299 "OMX_AUDIO_AMRBandModeUnused", 3300 "OMX_AUDIO_AMRBandModeNB0", 3301 "OMX_AUDIO_AMRBandModeNB1", 3302 "OMX_AUDIO_AMRBandModeNB2", 3303 "OMX_AUDIO_AMRBandModeNB3", 3304 "OMX_AUDIO_AMRBandModeNB4", 3305 "OMX_AUDIO_AMRBandModeNB5", 3306 "OMX_AUDIO_AMRBandModeNB6", 3307 "OMX_AUDIO_AMRBandModeNB7", 3308 "OMX_AUDIO_AMRBandModeWB0", 3309 "OMX_AUDIO_AMRBandModeWB1", 3310 "OMX_AUDIO_AMRBandModeWB2", 3311 "OMX_AUDIO_AMRBandModeWB3", 3312 "OMX_AUDIO_AMRBandModeWB4", 3313 "OMX_AUDIO_AMRBandModeWB5", 3314 "OMX_AUDIO_AMRBandModeWB6", 3315 "OMX_AUDIO_AMRBandModeWB7", 3316 "OMX_AUDIO_AMRBandModeWB8", 3317 }; 3318 3319 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 3320 3321 if (type < 0 || (size_t)type >= numNames) { 3322 return "UNKNOWN"; 3323 } else { 3324 return kNames[type]; 3325 } 3326 } 3327 3328 static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) { 3329 static const char *kNames[] = { 3330 "OMX_AUDIO_AMRFrameFormatConformance", 3331 "OMX_AUDIO_AMRFrameFormatIF1", 3332 "OMX_AUDIO_AMRFrameFormatIF2", 3333 "OMX_AUDIO_AMRFrameFormatFSF", 3334 "OMX_AUDIO_AMRFrameFormatRTPPayload", 3335 "OMX_AUDIO_AMRFrameFormatITU", 3336 }; 3337 3338 size_t numNames = sizeof(kNames) / sizeof(kNames[0]); 3339 3340 if (type < 0 || (size_t)type >= numNames) { 3341 return "UNKNOWN"; 3342 } else { 3343 return kNames[type]; 3344 } 3345 } 3346 3347 void OMXCodec::dumpPortStatus(OMX_U32 portIndex) { 3348 OMX_PARAM_PORTDEFINITIONTYPE def; 3349 InitOMXParams(&def); 3350 def.nPortIndex = portIndex; 3351 3352 status_t err = mOMX->getParameter( 3353 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 3354 CHECK_EQ(err, OK); 3355 3356 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output"); 3357 3358 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput) 3359 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput)); 3360 3361 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual); 3362 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin); 3363 printf(" nBufferSize = %ld\n", def.nBufferSize); 3364 3365 switch (def.eDomain) { 3366 case OMX_PortDomainImage: 3367 { 3368 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 3369 3370 printf("\n"); 3371 printf(" // Image\n"); 3372 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth); 3373 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight); 3374 printf(" nStride = %ld\n", imageDef->nStride); 3375 3376 printf(" eCompressionFormat = %s\n", 3377 imageCompressionFormatString(imageDef->eCompressionFormat)); 3378 3379 printf(" eColorFormat = %s\n", 3380 colorFormatString(imageDef->eColorFormat)); 3381 3382 break; 3383 } 3384 3385 case OMX_PortDomainVideo: 3386 { 3387 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video; 3388 3389 printf("\n"); 3390 printf(" // Video\n"); 3391 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth); 3392 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight); 3393 printf(" nStride = %ld\n", videoDef->nStride); 3394 3395 printf(" eCompressionFormat = %s\n", 3396 videoCompressionFormatString(videoDef->eCompressionFormat)); 3397 3398 printf(" eColorFormat = %s\n", 3399 colorFormatString(videoDef->eColorFormat)); 3400 3401 break; 3402 } 3403 3404 case OMX_PortDomainAudio: 3405 { 3406 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio; 3407 3408 printf("\n"); 3409 printf(" // Audio\n"); 3410 printf(" eEncoding = %s\n", 3411 audioCodingTypeString(audioDef->eEncoding)); 3412 3413 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) { 3414 OMX_AUDIO_PARAM_PCMMODETYPE params; 3415 InitOMXParams(¶ms); 3416 params.nPortIndex = portIndex; 3417 3418 err = mOMX->getParameter( 3419 mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); 3420 CHECK_EQ(err, OK); 3421 3422 printf(" nSamplingRate = %ld\n", params.nSamplingRate); 3423 printf(" nChannels = %ld\n", params.nChannels); 3424 printf(" bInterleaved = %d\n", params.bInterleaved); 3425 printf(" nBitPerSample = %ld\n", params.nBitPerSample); 3426 3427 printf(" eNumData = %s\n", 3428 params.eNumData == OMX_NumericalDataSigned 3429 ? "signed" : "unsigned"); 3430 3431 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode)); 3432 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) { 3433 OMX_AUDIO_PARAM_AMRTYPE amr; 3434 InitOMXParams(&amr); 3435 amr.nPortIndex = portIndex; 3436 3437 err = mOMX->getParameter( 3438 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); 3439 CHECK_EQ(err, OK); 3440 3441 printf(" nChannels = %ld\n", amr.nChannels); 3442 printf(" eAMRBandMode = %s\n", 3443 amrBandModeString(amr.eAMRBandMode)); 3444 printf(" eAMRFrameFormat = %s\n", 3445 amrFrameFormatString(amr.eAMRFrameFormat)); 3446 } 3447 3448 break; 3449 } 3450 3451 default: 3452 { 3453 printf(" // Unknown\n"); 3454 break; 3455 } 3456 } 3457 3458 printf("}\n"); 3459 } 3460 3461 void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) { 3462 mOutputFormat = new MetaData; 3463 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName); 3464 if (mIsEncoder) { 3465 int32_t timeScale; 3466 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) { 3467 mOutputFormat->setInt32(kKeyTimeScale, timeScale); 3468 } 3469 } 3470 3471 OMX_PARAM_PORTDEFINITIONTYPE def; 3472 InitOMXParams(&def); 3473 def.nPortIndex = kPortIndexOutput; 3474 3475 status_t err = mOMX->getParameter( 3476 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); 3477 CHECK_EQ(err, OK); 3478 3479 switch (def.eDomain) { 3480 case OMX_PortDomainImage: 3481 { 3482 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; 3483 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused); 3484 3485 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 3486 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat); 3487 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth); 3488 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight); 3489 break; 3490 } 3491 3492 case OMX_PortDomainAudio: 3493 { 3494 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio; 3495 3496 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) { 3497 OMX_AUDIO_PARAM_PCMMODETYPE params; 3498 InitOMXParams(¶ms); 3499 params.nPortIndex = kPortIndexOutput; 3500 3501 err = mOMX->getParameter( 3502 mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); 3503 CHECK_EQ(err, OK); 3504 3505 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned); 3506 CHECK_EQ(params.nBitPerSample, 16); 3507 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear); 3508 3509 int32_t numChannels, sampleRate; 3510 inputFormat->findInt32(kKeyChannelCount, &numChannels); 3511 inputFormat->findInt32(kKeySampleRate, &sampleRate); 3512 3513 if ((OMX_U32)numChannels != params.nChannels) { 3514 LOGW("Codec outputs a different number of channels than " 3515 "the input stream contains (contains %d channels, " 3516 "codec outputs %ld channels).", 3517 numChannels, params.nChannels); 3518 } 3519 3520 mOutputFormat->setCString( 3521 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); 3522 3523 // Use the codec-advertised number of channels, as some 3524 // codecs appear to output stereo even if the input data is 3525 // mono. If we know the codec lies about this information, 3526 // use the actual number of channels instead. 3527 mOutputFormat->setInt32( 3528 kKeyChannelCount, 3529 (mQuirks & kDecoderLiesAboutNumberOfChannels) 3530 ? numChannels : params.nChannels); 3531 3532 // The codec-reported sampleRate is not reliable... 3533 mOutputFormat->setInt32(kKeySampleRate, sampleRate); 3534 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) { 3535 OMX_AUDIO_PARAM_AMRTYPE amr; 3536 InitOMXParams(&amr); 3537 amr.nPortIndex = kPortIndexOutput; 3538 3539 err = mOMX->getParameter( 3540 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); 3541 CHECK_EQ(err, OK); 3542 3543 CHECK_EQ(amr.nChannels, 1); 3544 mOutputFormat->setInt32(kKeyChannelCount, 1); 3545 3546 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0 3547 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) { 3548 mOutputFormat->setCString( 3549 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB); 3550 mOutputFormat->setInt32(kKeySampleRate, 8000); 3551 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0 3552 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) { 3553 mOutputFormat->setCString( 3554 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB); 3555 mOutputFormat->setInt32(kKeySampleRate, 16000); 3556 } else { 3557 CHECK(!"Unknown AMR band mode."); 3558 } 3559 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) { 3560 mOutputFormat->setCString( 3561 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC); 3562 int32_t numChannels, sampleRate, bitRate; 3563 inputFormat->findInt32(kKeyChannelCount, &numChannels); 3564 inputFormat->findInt32(kKeySampleRate, &sampleRate); 3565 inputFormat->findInt32(kKeyBitRate, &bitRate); 3566 mOutputFormat->setInt32(kKeyChannelCount, numChannels); 3567 mOutputFormat->setInt32(kKeySampleRate, sampleRate); 3568 mOutputFormat->setInt32(kKeyBitRate, bitRate); 3569 } else { 3570 CHECK(!"Should not be here. Unknown audio encoding."); 3571 } 3572 break; 3573 } 3574 3575 case OMX_PortDomainVideo: 3576 { 3577 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; 3578 3579 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) { 3580 mOutputFormat->setCString( 3581 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 3582 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) { 3583 mOutputFormat->setCString( 3584 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); 3585 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) { 3586 mOutputFormat->setCString( 3587 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); 3588 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) { 3589 mOutputFormat->setCString( 3590 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 3591 } else { 3592 CHECK(!"Unknown compression format."); 3593 } 3594 3595 if (!strcmp(mComponentName, "OMX.PV.avcdec")) { 3596 // This component appears to be lying to me. 3597 mOutputFormat->setInt32( 3598 kKeyWidth, (video_def->nFrameWidth + 15) & -16); 3599 mOutputFormat->setInt32( 3600 kKeyHeight, (video_def->nFrameHeight + 15) & -16); 3601 } else { 3602 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth); 3603 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight); 3604 } 3605 3606 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat); 3607 break; 3608 } 3609 3610 default: 3611 { 3612 CHECK(!"should not be here, neither audio nor video."); 3613 break; 3614 } 3615 } 3616 } 3617 3618 status_t OMXCodec::pause() { 3619 Mutex::Autolock autoLock(mLock); 3620 3621 mPaused = true; 3622 3623 return OK; 3624 } 3625 3626 //////////////////////////////////////////////////////////////////////////////// 3627 3628 status_t QueryCodecs( 3629 const sp<IOMX> &omx, 3630 const char *mime, bool queryDecoders, 3631 Vector<CodecCapabilities> *results) { 3632 results->clear(); 3633 3634 for (int index = 0;; ++index) { 3635 const char *componentName; 3636 3637 if (!queryDecoders) { 3638 componentName = GetCodec( 3639 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]), 3640 mime, index); 3641 } else { 3642 componentName = GetCodec( 3643 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]), 3644 mime, index); 3645 } 3646 3647 if (!componentName) { 3648 return OK; 3649 } 3650 3651 if (strncmp(componentName, "OMX.", 4)) { 3652 // Not an OpenMax component but a software codec. 3653 3654 results->push(); 3655 CodecCapabilities *caps = &results->editItemAt(results->size() - 1); 3656 caps->mComponentName = componentName; 3657 3658 continue; 3659 } 3660 3661 sp<OMXCodecObserver> observer = new OMXCodecObserver; 3662 IOMX::node_id node; 3663 status_t err = omx->allocateNode(componentName, observer, &node); 3664 3665 if (err != OK) { 3666 continue; 3667 } 3668 3669 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime); 3670 3671 results->push(); 3672 CodecCapabilities *caps = &results->editItemAt(results->size() - 1); 3673 caps->mComponentName = componentName; 3674 3675 OMX_VIDEO_PARAM_PROFILELEVELTYPE param; 3676 InitOMXParams(¶m); 3677 3678 param.nPortIndex = queryDecoders ? 0 : 1; 3679 3680 for (param.nProfileIndex = 0;; ++param.nProfileIndex) { 3681 err = omx->getParameter( 3682 node, OMX_IndexParamVideoProfileLevelQuerySupported, 3683 ¶m, sizeof(param)); 3684 3685 if (err != OK) { 3686 break; 3687 } 3688 3689 CodecProfileLevel profileLevel; 3690 profileLevel.mProfile = param.eProfile; 3691 profileLevel.mLevel = param.eLevel; 3692 3693 caps->mProfileLevels.push(profileLevel); 3694 } 3695 3696 CHECK_EQ(omx->freeNode(node), OK); 3697 } 3698 } 3699 3700 } // namespace android 3701