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