blob: 7586adad0f95b0165a58fd473dac70e5e0495d54 [file] [log] [blame]
Andreas Huberbe06d262009-08-14 14:37:10 -07001/*
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>
Andreas Hubere6c40962009-09-10 14:13:30 -070029#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070030#include <media/stagefright/MediaExtractor.h>
31#include <media/stagefright/MetaData.h>
32#include <media/stagefright/MmapSource.h>
33#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070034#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070035#include <utils/Vector.h>
36
37#include <OMX_Audio.h>
38#include <OMX_Component.h>
39
40namespace android {
41
Andreas Huber8b432b12009-10-07 13:36:52 -070042static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
43
Andreas Huberbe06d262009-08-14 14:37:10 -070044struct CodecInfo {
45 const char *mime;
46 const char *codec;
47};
48
49static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -070050 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
51 { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
52 { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
53 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
54 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
55 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
56 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
57 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
58 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
59 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
60 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
61 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
62 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
63 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.Decoder" },
64 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
65 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
66 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
67 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huberbe06d262009-08-14 14:37:10 -070068};
69
70static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -070071 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
72 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrencnb" },
73 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
74 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
75 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
76 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
77 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
78 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
79 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
80 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
81 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
82 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
83 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -070084};
85
Andreas Hubere0873732009-09-10 09:57:53 -070086#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -070087#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
88
Andreas Huberbe06d262009-08-14 14:37:10 -070089struct OMXCodecObserver : public BnOMXObserver {
90 OMXCodecObserver(const wp<OMXCodec> &target)
91 : mTarget(target) {
92 }
93
94 // from IOMXObserver
95 virtual void on_message(const omx_message &msg) {
96 sp<OMXCodec> codec = mTarget.promote();
97
98 if (codec.get() != NULL) {
99 codec->on_message(msg);
100 }
101 }
102
103protected:
104 virtual ~OMXCodecObserver() {}
105
106private:
107 wp<OMXCodec> mTarget;
108
109 OMXCodecObserver(const OMXCodecObserver &);
110 OMXCodecObserver &operator=(const OMXCodecObserver &);
111};
112
113static const char *GetCodec(const CodecInfo *info, size_t numInfos,
114 const char *mime, int index) {
115 CHECK(index >= 0);
116 for(size_t i = 0; i < numInfos; ++i) {
117 if (!strcasecmp(mime, info[i].mime)) {
118 if (index == 0) {
119 return info[i].codec;
120 }
121
122 --index;
123 }
124 }
125
126 return NULL;
127}
128
Andreas Huberebf66ea2009-08-19 13:32:58 -0700129enum {
130 kAVCProfileBaseline = 0x42,
131 kAVCProfileMain = 0x4d,
132 kAVCProfileExtended = 0x58,
133 kAVCProfileHigh = 0x64,
134 kAVCProfileHigh10 = 0x6e,
135 kAVCProfileHigh422 = 0x7a,
136 kAVCProfileHigh444 = 0xf4,
137 kAVCProfileCAVLC444Intra = 0x2c
138};
139
140static const char *AVCProfileToString(uint8_t profile) {
141 switch (profile) {
142 case kAVCProfileBaseline:
143 return "Baseline";
144 case kAVCProfileMain:
145 return "Main";
146 case kAVCProfileExtended:
147 return "Extended";
148 case kAVCProfileHigh:
149 return "High";
150 case kAVCProfileHigh10:
151 return "High 10";
152 case kAVCProfileHigh422:
153 return "High 422";
154 case kAVCProfileHigh444:
155 return "High 444";
156 case kAVCProfileCAVLC444Intra:
157 return "CAVLC 444 Intra";
158 default: return "Unknown";
159 }
160}
161
Andreas Huber4c483422009-09-02 16:05:36 -0700162template<class T>
163static void InitOMXParams(T *params) {
164 params->nSize = sizeof(T);
165 params->nVersion.s.nVersionMajor = 1;
166 params->nVersion.s.nVersionMinor = 0;
167 params->nVersion.s.nRevision = 0;
168 params->nVersion.s.nStep = 0;
169}
170
Andreas Huberbe06d262009-08-14 14:37:10 -0700171// static
172sp<OMXCodec> OMXCodec::Create(
173 const sp<IOMX> &omx,
174 const sp<MetaData> &meta, bool createEncoder,
Andreas Hubere6c40962009-09-10 14:13:30 -0700175 const sp<MediaSource> &source,
176 const char *matchComponentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700177 const char *mime;
178 bool success = meta->findCString(kKeyMIMEType, &mime);
179 CHECK(success);
180
181 const char *componentName = NULL;
182 IOMX::node_id node = 0;
183 for (int index = 0;; ++index) {
184 if (createEncoder) {
185 componentName = GetCodec(
186 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
187 mime, index);
188 } else {
189 componentName = GetCodec(
190 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
191 mime, index);
192 }
193
194 if (!componentName) {
195 return NULL;
196 }
197
Andreas Hubere6c40962009-09-10 14:13:30 -0700198 // If a specific codec is requested, skip the non-matching ones.
199 if (matchComponentName && strcmp(componentName, matchComponentName)) {
200 continue;
201 }
202
Andreas Huberbe06d262009-08-14 14:37:10 -0700203 LOGV("Attempting to allocate OMX node '%s'", componentName);
204
205 status_t err = omx->allocate_node(componentName, &node);
206 if (err == OK) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700207 LOGV("Successfully allocated OMX node '%s'", componentName);
Andreas Huberbe06d262009-08-14 14:37:10 -0700208 break;
209 }
210 }
211
212 uint32_t quirks = 0;
213 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700214 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700215 }
216 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
217 quirks |= kNeedsFlushBeforeDisable;
218 }
219 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
220 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700221 quirks |= kRequiresFlushCompleteEmulation;
Andreas Huberbe06d262009-08-14 14:37:10 -0700222 }
223 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
224 quirks |= kRequiresLoadedToIdleAfterAllocation;
225 quirks |= kRequiresAllocateBufferOnInputPorts;
226 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700227 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
228 // XXX Required on P....on only.
Andreas Huber0446b822009-09-15 15:50:31 -0700229 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700230 quirks |= kRequiresAllocateBufferOnOutputPorts;
231 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700232
Andreas Huber2dc64d82009-09-11 12:58:53 -0700233 if (!strncmp(componentName, "OMX.TI.", 7)) {
234 // Apparently I must not use OMX_UseBuffer on either input or
235 // output ports on any of the TI components or quote:
236 // "(I) may have unexpected problem (sic) which can be timing related
237 // and hard to reproduce."
238
239 quirks |= kRequiresAllocateBufferOnInputPorts;
240 quirks |= kRequiresAllocateBufferOnOutputPorts;
241 }
242
Andreas Huberbe06d262009-08-14 14:37:10 -0700243 sp<OMXCodec> codec = new OMXCodec(
244 omx, node, quirks, createEncoder, mime, componentName,
245 source);
246
247 uint32_t type;
248 const void *data;
249 size_t size;
250 if (meta->findData(kKeyESDS, &type, &data, &size)) {
251 ESDS esds((const char *)data, size);
252 CHECK_EQ(esds.InitCheck(), OK);
253
254 const void *codec_specific_data;
255 size_t codec_specific_data_size;
256 esds.getCodecSpecificInfo(
257 &codec_specific_data, &codec_specific_data_size);
258
259 printf("found codec-specific data of size %d\n",
260 codec_specific_data_size);
261
262 codec->addCodecSpecificData(
263 codec_specific_data, codec_specific_data_size);
264 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
265 printf("found avcc of size %d\n", size);
266
Andreas Huberebf66ea2009-08-19 13:32:58 -0700267 // Parse the AVCDecoderConfigurationRecord
268
269 const uint8_t *ptr = (const uint8_t *)data;
270
271 CHECK(size >= 7);
272 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
273 uint8_t profile = ptr[1];
274 uint8_t level = ptr[3];
275
276 CHECK((ptr[4] >> 2) == 0x3f); // reserved
277
278 size_t lengthSize = 1 + (ptr[4] & 3);
279
280 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
281 // violates it...
282 // CHECK((ptr[5] >> 5) == 7); // reserved
283
284 size_t numSeqParameterSets = ptr[5] & 31;
285
286 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700287 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700288
289 for (size_t i = 0; i < numSeqParameterSets; ++i) {
290 CHECK(size >= 2);
291 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700292
293 ptr += 2;
294 size -= 2;
295
Andreas Huberbe06d262009-08-14 14:37:10 -0700296 CHECK(size >= length);
297
298 codec->addCodecSpecificData(ptr, length);
299
300 ptr += length;
301 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700302 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700303
Andreas Huberebf66ea2009-08-19 13:32:58 -0700304 CHECK(size >= 1);
305 size_t numPictureParameterSets = *ptr;
306 ++ptr;
307 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700308
Andreas Huberebf66ea2009-08-19 13:32:58 -0700309 for (size_t i = 0; i < numPictureParameterSets; ++i) {
310 CHECK(size >= 2);
311 size_t length = U16_AT(ptr);
312
313 ptr += 2;
314 size -= 2;
315
316 CHECK(size >= length);
317
318 codec->addCodecSpecificData(ptr, length);
319
320 ptr += length;
321 size -= length;
322 }
323
Andreas Huber53a76bd2009-10-06 16:20:44 -0700324 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700325 (int)profile, AVCProfileToString(profile), (int)level / 10);
326
Andreas Huber53a76bd2009-10-06 16:20:44 -0700327#if 0
Andreas Huberebf66ea2009-08-19 13:32:58 -0700328 if (!strcmp(componentName, "OMX.TI.Video.Decoder")
329 && (profile != kAVCProfileBaseline || level > 39)) {
330 // This stream exceeds the decoder's capabilities.
331
332 LOGE("Profile and/or level exceed the decoder's capabilities.");
333 return NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -0700334 }
Andreas Huber53a76bd2009-10-06 16:20:44 -0700335#endif
Andreas Huberbe06d262009-08-14 14:37:10 -0700336 }
337
Andreas Hubere6c40962009-09-10 14:13:30 -0700338 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700339 codec->setAMRFormat();
340 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700341 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
Andreas Huberee606e62009-09-08 10:19:21 -0700342 codec->setAMRWBFormat();
343 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700344 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700345 int32_t numChannels, sampleRate;
346 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
347 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
348
349 codec->setAACFormat(numChannels, sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700350 }
351 if (!strncasecmp(mime, "video/", 6)) {
352 int32_t width, height;
353 bool success = meta->findInt32(kKeyWidth, &width);
354 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700355 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700356
357 if (createEncoder) {
358 codec->setVideoInputFormat(mime, width, height);
359 } else {
360 codec->setVideoOutputFormat(mime, width, height);
361 }
362 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700363 if (!strcasecmp(mime, MEDIA_MIMETYPE_IMAGE_JPEG)
Andreas Huberbe06d262009-08-14 14:37:10 -0700364 && !strcmp(componentName, "OMX.TI.JPEG.decode")) {
365 OMX_COLOR_FORMATTYPE format =
366 OMX_COLOR_Format32bitARGB8888;
367 // OMX_COLOR_FormatYUV420PackedPlanar;
368 // OMX_COLOR_FormatCbYCrY;
369 // OMX_COLOR_FormatYUV411Planar;
370
371 int32_t width, height;
372 bool success = meta->findInt32(kKeyWidth, &width);
373 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700374
375 int32_t compressedSize;
376 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700377 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700378
379 CHECK(success);
380 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700381
382 codec->setImageOutputFormat(format, width, height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700383 codec->setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700384 }
385
Andreas Huberda050cf22009-09-02 14:01:43 -0700386 int32_t maxInputSize;
387 if (createEncoder && meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
388 codec->setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
389 }
390
391 if (!strcmp(componentName, "OMX.TI.AMR.encode")
392 || !strcmp(componentName, "OMX.TI.WBAMR.encode")) {
393 codec->setMinBufferSize(kPortIndexOutput, 8192); // XXX
394 }
395
Andreas Huberbe06d262009-08-14 14:37:10 -0700396 codec->initOutputFormat(meta);
397
398 return codec;
399}
400
Andreas Huberda050cf22009-09-02 14:01:43 -0700401void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
402 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700403 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700404 def.nPortIndex = portIndex;
405
406 status_t err = mOMX->get_parameter(
407 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
408 CHECK_EQ(err, OK);
409
410 if (def.nBufferSize < size) {
411 def.nBufferSize = size;
412
413 }
414
415 err = mOMX->set_parameter(
416 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
417 CHECK_EQ(err, OK);
418}
419
Andreas Huberbe06d262009-08-14 14:37:10 -0700420status_t OMXCodec::setVideoPortFormatType(
421 OMX_U32 portIndex,
422 OMX_VIDEO_CODINGTYPE compressionFormat,
423 OMX_COLOR_FORMATTYPE colorFormat) {
424 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700425 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700426 format.nPortIndex = portIndex;
427 format.nIndex = 0;
428 bool found = false;
429
430 OMX_U32 index = 0;
431 for (;;) {
432 format.nIndex = index;
433 status_t err = mOMX->get_parameter(
434 mNode, OMX_IndexParamVideoPortFormat,
435 &format, sizeof(format));
436
437 if (err != OK) {
438 return err;
439 }
440
441 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700442 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700443
444#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700445 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700446 portIndex,
447 index, format.eCompressionFormat, format.eColorFormat);
448#endif
449
450 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
451 if (portIndex == kPortIndexInput
452 && colorFormat == format.eColorFormat) {
453 // eCompressionFormat does not seem right.
454 found = true;
455 break;
456 }
457 if (portIndex == kPortIndexOutput
458 && compressionFormat == format.eCompressionFormat) {
459 // eColorFormat does not seem right.
460 found = true;
461 break;
462 }
463 }
464
465 if (format.eCompressionFormat == compressionFormat
466 && format.eColorFormat == colorFormat) {
467 found = true;
468 break;
469 }
470
471 ++index;
472 }
473
474 if (!found) {
475 return UNKNOWN_ERROR;
476 }
477
Andreas Huber53a76bd2009-10-06 16:20:44 -0700478 CODEC_LOGV("found a match.");
Andreas Huberbe06d262009-08-14 14:37:10 -0700479 status_t err = mOMX->set_parameter(
480 mNode, OMX_IndexParamVideoPortFormat,
481 &format, sizeof(format));
482
483 return err;
484}
485
486void OMXCodec::setVideoInputFormat(
487 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700488 CODEC_LOGV("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700489
490 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700491 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700492 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700493 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700494 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700495 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700496 compressionFormat = OMX_VIDEO_CodingH263;
497 } else {
498 LOGE("Not a supported video mime type: %s", mime);
499 CHECK(!"Should not be here. Not a supported video mime type.");
500 }
501
502 OMX_COLOR_FORMATTYPE colorFormat =
503 0 ? OMX_COLOR_FormatYCbYCr : OMX_COLOR_FormatCbYCrY;
504
505 if (!strncmp("OMX.qcom.video.encoder.", mComponentName, 23)) {
506 colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
507 }
508
509 setVideoPortFormatType(
510 kPortIndexInput, OMX_VIDEO_CodingUnused,
511 colorFormat);
512
513 setVideoPortFormatType(
514 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused);
515
516 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700517 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700518 def.nPortIndex = kPortIndexOutput;
519
Andreas Huber4c483422009-09-02 16:05:36 -0700520 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
521
Andreas Huberbe06d262009-08-14 14:37:10 -0700522 status_t err = mOMX->get_parameter(
523 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
524
525 CHECK_EQ(err, OK);
526 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
527
528 video_def->nFrameWidth = width;
529 video_def->nFrameHeight = height;
530
531 video_def->eCompressionFormat = compressionFormat;
532 video_def->eColorFormat = OMX_COLOR_FormatUnused;
533
534 err = mOMX->set_parameter(
535 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
536 CHECK_EQ(err, OK);
537
538 ////////////////////////////////////////////////////////////////////////////
539
Andreas Huber4c483422009-09-02 16:05:36 -0700540 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700541 def.nPortIndex = kPortIndexInput;
542
543 err = mOMX->get_parameter(
544 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
545 CHECK_EQ(err, OK);
546
547 def.nBufferSize = (width * height * 2); // (width * height * 3) / 2;
Andreas Huber53a76bd2009-10-06 16:20:44 -0700548 CODEC_LOGV("Setting nBufferSize = %ld", def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700549
550 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
551
552 video_def->nFrameWidth = width;
553 video_def->nFrameHeight = height;
554 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
555 video_def->eColorFormat = colorFormat;
556
557 err = mOMX->set_parameter(
558 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
559 CHECK_EQ(err, OK);
560}
561
562void OMXCodec::setVideoOutputFormat(
563 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700564 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700565
Andreas Huberbe06d262009-08-14 14:37:10 -0700566 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700567 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700568 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700569 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700570 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700571 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700572 compressionFormat = OMX_VIDEO_CodingH263;
573 } else {
574 LOGE("Not a supported video mime type: %s", mime);
575 CHECK(!"Should not be here. Not a supported video mime type.");
576 }
577
578 setVideoPortFormatType(
579 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
580
581#if 1
582 {
583 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700584 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700585 format.nPortIndex = kPortIndexOutput;
586 format.nIndex = 0;
587
588 status_t err = mOMX->get_parameter(
589 mNode, OMX_IndexParamVideoPortFormat,
590 &format, sizeof(format));
591 CHECK_EQ(err, OK);
592 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
593
594 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
595
596 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
597 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
598 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
599 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
600
601 err = mOMX->set_parameter(
602 mNode, OMX_IndexParamVideoPortFormat,
603 &format, sizeof(format));
604 CHECK_EQ(err, OK);
605 }
606#endif
607
608 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700609 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700610 def.nPortIndex = kPortIndexInput;
611
Andreas Huber4c483422009-09-02 16:05:36 -0700612 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
613
Andreas Huberbe06d262009-08-14 14:37:10 -0700614 status_t err = mOMX->get_parameter(
615 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
616
617 CHECK_EQ(err, OK);
618
619#if 1
620 // XXX Need a (much) better heuristic to compute input buffer sizes.
621 const size_t X = 64 * 1024;
622 if (def.nBufferSize < X) {
623 def.nBufferSize = X;
624 }
625#endif
626
627 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
628
629 video_def->nFrameWidth = width;
630 video_def->nFrameHeight = height;
631
632 video_def->eColorFormat = OMX_COLOR_FormatUnused;
633
634 err = mOMX->set_parameter(
635 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
636 CHECK_EQ(err, OK);
637
638 ////////////////////////////////////////////////////////////////////////////
639
Andreas Huber4c483422009-09-02 16:05:36 -0700640 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700641 def.nPortIndex = kPortIndexOutput;
642
643 err = mOMX->get_parameter(
644 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
645 CHECK_EQ(err, OK);
646 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
647
648#if 0
649 def.nBufferSize =
650 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
651#endif
652
653 video_def->nFrameWidth = width;
654 video_def->nFrameHeight = height;
655
656 err = mOMX->set_parameter(
657 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
658 CHECK_EQ(err, OK);
659}
660
661
662OMXCodec::OMXCodec(
663 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -0700664 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -0700665 const char *mime,
666 const char *componentName,
667 const sp<MediaSource> &source)
668 : mOMX(omx),
669 mNode(node),
670 mQuirks(quirks),
671 mIsEncoder(isEncoder),
672 mMIME(strdup(mime)),
673 mComponentName(strdup(componentName)),
674 mSource(source),
675 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -0700676 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -0700677 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -0700678 mSignalledEOS(false),
679 mNoMoreOutputData(false),
680 mSeekTimeUs(-1) {
681 mPortStatus[kPortIndexInput] = ENABLED;
682 mPortStatus[kPortIndexOutput] = ENABLED;
683
684 mObserver = new OMXCodecObserver(this);
685 mOMX->observe_node(mNode, mObserver);
Andreas Huber4c483422009-09-02 16:05:36 -0700686
687 setComponentRole();
688}
689
Andreas Hubere6c40962009-09-10 14:13:30 -0700690// static
691void OMXCodec::setComponentRole(
692 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
693 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -0700694 struct MimeToRole {
695 const char *mime;
696 const char *decoderRole;
697 const char *encoderRole;
698 };
699
700 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700701 { MEDIA_MIMETYPE_AUDIO_MPEG,
702 "audio_decoder.mp3", "audio_encoder.mp3" },
703 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
704 "audio_decoder.amrnb", "audio_encoder.amrnb" },
705 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
706 "audio_decoder.amrwb", "audio_encoder.amrwb" },
707 { MEDIA_MIMETYPE_AUDIO_AAC,
708 "audio_decoder.aac", "audio_encoder.aac" },
709 { MEDIA_MIMETYPE_VIDEO_AVC,
710 "video_decoder.avc", "video_encoder.avc" },
711 { MEDIA_MIMETYPE_VIDEO_MPEG4,
712 "video_decoder.mpeg4", "video_encoder.mpeg4" },
713 { MEDIA_MIMETYPE_VIDEO_H263,
714 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -0700715 };
716
717 static const size_t kNumMimeToRole =
718 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
719
720 size_t i;
721 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -0700722 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -0700723 break;
724 }
725 }
726
727 if (i == kNumMimeToRole) {
728 return;
729 }
730
731 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -0700732 isEncoder ? kMimeToRole[i].encoderRole
733 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -0700734
735 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -0700736 OMX_PARAM_COMPONENTROLETYPE roleParams;
737 InitOMXParams(&roleParams);
738
739 strncpy((char *)roleParams.cRole,
740 role, OMX_MAX_STRINGNAME_SIZE - 1);
741
742 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
743
Andreas Hubere6c40962009-09-10 14:13:30 -0700744 status_t err = omx->set_parameter(
745 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -0700746 &roleParams, sizeof(roleParams));
747
748 if (err != OK) {
749 LOGW("Failed to set standard component role '%s'.", role);
750 }
751 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700752}
753
Andreas Hubere6c40962009-09-10 14:13:30 -0700754void OMXCodec::setComponentRole() {
755 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
756}
757
Andreas Huberbe06d262009-08-14 14:37:10 -0700758OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700759 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -0700760
761 status_t err = mOMX->observe_node(mNode, NULL);
762 CHECK_EQ(err, OK);
763
764 err = mOMX->free_node(mNode);
765 CHECK_EQ(err, OK);
766
767 mNode = NULL;
768 setState(DEAD);
769
770 clearCodecSpecificData();
771
772 free(mComponentName);
773 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700774
Andreas Huberbe06d262009-08-14 14:37:10 -0700775 free(mMIME);
776 mMIME = NULL;
777}
778
779status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -0700780 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -0700781
782 CHECK_EQ(mState, LOADED);
783
784 status_t err;
785 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
786 err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
787 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700788 setState(LOADED_TO_IDLE);
789 }
790
791 err = allocateBuffers();
792 CHECK_EQ(err, OK);
793
794 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
795 err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
796 CHECK_EQ(err, OK);
797
798 setState(LOADED_TO_IDLE);
799 }
800
801 while (mState != EXECUTING && mState != ERROR) {
802 mAsyncCompletion.wait(mLock);
803 }
804
805 return mState == ERROR ? UNKNOWN_ERROR : OK;
806}
807
808// static
809bool OMXCodec::isIntermediateState(State state) {
810 return state == LOADED_TO_IDLE
811 || state == IDLE_TO_EXECUTING
812 || state == EXECUTING_TO_IDLE
813 || state == IDLE_TO_LOADED
814 || state == RECONFIGURING;
815}
816
817status_t OMXCodec::allocateBuffers() {
818 status_t err = allocateBuffersOnPort(kPortIndexInput);
819
820 if (err != OK) {
821 return err;
822 }
823
824 return allocateBuffersOnPort(kPortIndexOutput);
825}
826
827status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
828 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700829 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700830 def.nPortIndex = portIndex;
831
832 status_t err = mOMX->get_parameter(
833 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
834
835 if (err != OK) {
836 return err;
837 }
838
Andreas Huber5c0a9132009-08-20 11:16:40 -0700839 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
840 mDealer[portIndex] = new MemoryDealer(totalSize);
841
Andreas Huberbe06d262009-08-14 14:37:10 -0700842 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -0700843 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700844 CHECK(mem.get() != NULL);
845
846 IOMX::buffer_id buffer;
847 if (portIndex == kPortIndexInput
848 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
849 err = mOMX->allocate_buffer_with_backup(
850 mNode, portIndex, mem, &buffer);
Andreas Huber446f44f2009-08-25 17:23:44 -0700851 } else if (portIndex == kPortIndexOutput
852 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huber2dc64d82009-09-11 12:58:53 -0700853 err = mOMX->allocate_buffer_with_backup(
854 mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700855 } else {
856 err = mOMX->use_buffer(mNode, portIndex, mem, &buffer);
857 }
858
859 if (err != OK) {
860 LOGE("allocate_buffer_with_backup failed");
861 return err;
862 }
863
864 BufferInfo info;
865 info.mBuffer = buffer;
866 info.mOwnedByComponent = false;
867 info.mMem = mem;
868 info.mMediaBuffer = NULL;
869
870 if (portIndex == kPortIndexOutput) {
871 info.mMediaBuffer = new MediaBuffer(mem->pointer(), mem->size());
872 info.mMediaBuffer->setObserver(this);
873 }
874
875 mPortBuffers[portIndex].push(info);
876
Andreas Huber4c483422009-09-02 16:05:36 -0700877 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -0700878 portIndex == kPortIndexInput ? "input" : "output");
879 }
880
881 dumpPortStatus(portIndex);
882
883 return OK;
884}
885
886void OMXCodec::on_message(const omx_message &msg) {
887 Mutex::Autolock autoLock(mLock);
888
889 switch (msg.type) {
890 case omx_message::EVENT:
891 {
892 onEvent(
893 msg.u.event_data.event, msg.u.event_data.data1,
894 msg.u.event_data.data2);
895
896 break;
897 }
898
899 case omx_message::EMPTY_BUFFER_DONE:
900 {
901 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
902
Andreas Huber4c483422009-09-02 16:05:36 -0700903 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700904
905 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
906 size_t i = 0;
907 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
908 ++i;
909 }
910
911 CHECK(i < buffers->size());
912 if (!(*buffers)[i].mOwnedByComponent) {
913 LOGW("We already own input buffer %p, yet received "
914 "an EMPTY_BUFFER_DONE.", buffer);
915 }
916
917 buffers->editItemAt(i).mOwnedByComponent = false;
918
919 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -0700920 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700921
922 status_t err =
923 mOMX->free_buffer(mNode, kPortIndexInput, buffer);
924 CHECK_EQ(err, OK);
925
926 buffers->removeAt(i);
927 } else if (mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
928 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
929 drainInputBuffer(&buffers->editItemAt(i));
930 }
931
932 break;
933 }
934
935 case omx_message::FILL_BUFFER_DONE:
936 {
937 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
938 OMX_U32 flags = msg.u.extended_buffer_data.flags;
939
Andreas Huber4c483422009-09-02 16:05:36 -0700940 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx)",
Andreas Huberbe06d262009-08-14 14:37:10 -0700941 buffer,
942 msg.u.extended_buffer_data.range_length,
943 flags);
944
Andreas Huber4c483422009-09-02 16:05:36 -0700945 CODEC_LOGV("FILL_BUFFER_DONE(timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -0700946 msg.u.extended_buffer_data.timestamp,
947 msg.u.extended_buffer_data.timestamp / 1E6);
948
949 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
950 size_t i = 0;
951 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
952 ++i;
953 }
954
955 CHECK(i < buffers->size());
956 BufferInfo *info = &buffers->editItemAt(i);
957
958 if (!info->mOwnedByComponent) {
959 LOGW("We already own output buffer %p, yet received "
960 "a FILL_BUFFER_DONE.", buffer);
961 }
962
963 info->mOwnedByComponent = false;
964
965 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -0700966 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700967
968 status_t err =
969 mOMX->free_buffer(mNode, kPortIndexOutput, buffer);
970 CHECK_EQ(err, OK);
971
972 buffers->removeAt(i);
Andreas Huberd7795892009-08-26 10:33:47 -0700973 } else if (mPortStatus[kPortIndexOutput] == ENABLED
974 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -0700975 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -0700976 mNoMoreOutputData = true;
977 mBufferFilled.signal();
978 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
979 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700980
Andreas Huberbe06d262009-08-14 14:37:10 -0700981 MediaBuffer *buffer = info->mMediaBuffer;
982
983 buffer->set_range(
984 msg.u.extended_buffer_data.range_offset,
985 msg.u.extended_buffer_data.range_length);
986
987 buffer->meta_data()->clear();
988
989 buffer->meta_data()->setInt32(
990 kKeyTimeUnits,
991 (msg.u.extended_buffer_data.timestamp + 500) / 1000);
992
993 buffer->meta_data()->setInt32(
994 kKeyTimeScale, 1000);
995
996 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
997 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
998 }
999
1000 buffer->meta_data()->setPointer(
1001 kKeyPlatformPrivate,
1002 msg.u.extended_buffer_data.platform_private);
1003
1004 buffer->meta_data()->setPointer(
1005 kKeyBufferID,
1006 msg.u.extended_buffer_data.buffer);
1007
1008 mFilledBuffers.push_back(i);
1009 mBufferFilled.signal();
1010 }
1011
1012 break;
1013 }
1014
1015 default:
1016 {
1017 CHECK(!"should not be here.");
1018 break;
1019 }
1020 }
1021}
1022
1023void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1024 switch (event) {
1025 case OMX_EventCmdComplete:
1026 {
1027 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1028 break;
1029 }
1030
1031 case OMX_EventError:
1032 {
1033 LOGE("ERROR(%ld, %ld)", data1, data2);
1034
1035 setState(ERROR);
1036 break;
1037 }
1038
1039 case OMX_EventPortSettingsChanged:
1040 {
1041 onPortSettingsChanged(data1);
1042 break;
1043 }
1044
1045 case OMX_EventBufferFlag:
1046 {
Andreas Huber4c483422009-09-02 16:05:36 -07001047 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001048
1049 if (data1 == kPortIndexOutput) {
1050 mNoMoreOutputData = true;
1051 }
1052 break;
1053 }
1054
1055 default:
1056 {
Andreas Huber4c483422009-09-02 16:05:36 -07001057 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001058 break;
1059 }
1060 }
1061}
1062
1063void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1064 switch (cmd) {
1065 case OMX_CommandStateSet:
1066 {
1067 onStateChange((OMX_STATETYPE)data);
1068 break;
1069 }
1070
1071 case OMX_CommandPortDisable:
1072 {
1073 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001074 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001075
1076 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1077 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1078 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1079
1080 mPortStatus[portIndex] = DISABLED;
1081
1082 if (mState == RECONFIGURING) {
1083 CHECK_EQ(portIndex, kPortIndexOutput);
1084
1085 enablePortAsync(portIndex);
1086
1087 status_t err = allocateBuffersOnPort(portIndex);
1088 CHECK_EQ(err, OK);
1089 }
1090 break;
1091 }
1092
1093 case OMX_CommandPortEnable:
1094 {
1095 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001096 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001097
1098 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1099 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1100
1101 mPortStatus[portIndex] = ENABLED;
1102
1103 if (mState == RECONFIGURING) {
1104 CHECK_EQ(portIndex, kPortIndexOutput);
1105
1106 setState(EXECUTING);
1107
1108 fillOutputBuffers();
1109 }
1110 break;
1111 }
1112
1113 case OMX_CommandFlush:
1114 {
1115 OMX_U32 portIndex = data;
1116
Andreas Huber4c483422009-09-02 16:05:36 -07001117 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001118
1119 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1120 mPortStatus[portIndex] = ENABLED;
1121
1122 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1123 mPortBuffers[portIndex].size());
1124
1125 if (mState == RECONFIGURING) {
1126 CHECK_EQ(portIndex, kPortIndexOutput);
1127
1128 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001129 } else if (mState == EXECUTING_TO_IDLE) {
1130 if (mPortStatus[kPortIndexInput] == ENABLED
1131 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001132 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001133 "transition from EXECUTING to IDLE.");
1134
1135 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1136 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1137
1138 status_t err =
1139 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1140 CHECK_EQ(err, OK);
1141 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001142 } else {
1143 // We're flushing both ports in preparation for seeking.
1144
1145 if (mPortStatus[kPortIndexInput] == ENABLED
1146 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001147 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001148 " seek-time.");
1149
Andreas Huber1a77b68e2009-09-17 11:16:52 -07001150 // Clear this flag in case the decoder sent us either
1151 // the EVENT_BUFFER_FLAG(1) or an output buffer with
1152 // the EOS flag set _while_ flushing. Since we're going
1153 // to submit "fresh" input data now, this flag no longer
1154 // applies to our future.
1155 mNoMoreOutputData = false;
1156
Andreas Huberbe06d262009-08-14 14:37:10 -07001157 drainInputBuffers();
1158 fillOutputBuffers();
1159 }
1160 }
1161
1162 break;
1163 }
1164
1165 default:
1166 {
Andreas Huber4c483422009-09-02 16:05:36 -07001167 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001168 break;
1169 }
1170 }
1171}
1172
1173void OMXCodec::onStateChange(OMX_STATETYPE newState) {
1174 switch (newState) {
1175 case OMX_StateIdle:
1176 {
Andreas Huber4c483422009-09-02 16:05:36 -07001177 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001178 if (mState == LOADED_TO_IDLE) {
1179 status_t err = mOMX->send_command(
1180 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1181
1182 CHECK_EQ(err, OK);
1183
1184 setState(IDLE_TO_EXECUTING);
1185 } else {
1186 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1187
1188 CHECK_EQ(
1189 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1190 mPortBuffers[kPortIndexInput].size());
1191
1192 CHECK_EQ(
1193 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1194 mPortBuffers[kPortIndexOutput].size());
1195
1196 status_t err = mOMX->send_command(
1197 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1198
1199 CHECK_EQ(err, OK);
1200
1201 err = freeBuffersOnPort(kPortIndexInput);
1202 CHECK_EQ(err, OK);
1203
1204 err = freeBuffersOnPort(kPortIndexOutput);
1205 CHECK_EQ(err, OK);
1206
1207 mPortStatus[kPortIndexInput] = ENABLED;
1208 mPortStatus[kPortIndexOutput] = ENABLED;
1209
1210 setState(IDLE_TO_LOADED);
1211 }
1212 break;
1213 }
1214
1215 case OMX_StateExecuting:
1216 {
1217 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1218
Andreas Huber4c483422009-09-02 16:05:36 -07001219 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001220
1221 setState(EXECUTING);
1222
Andreas Huber42978e52009-08-27 10:08:39 -07001223 // Buffers will be submitted to the component in the first
1224 // call to OMXCodec::read as mInitialBufferSubmit is true at
1225 // this point. This ensures that this on_message call returns,
1226 // releases the lock and ::init can notice the state change and
1227 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001228 break;
1229 }
1230
1231 case OMX_StateLoaded:
1232 {
1233 CHECK_EQ(mState, IDLE_TO_LOADED);
1234
Andreas Huber4c483422009-09-02 16:05:36 -07001235 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001236
1237 setState(LOADED);
1238 break;
1239 }
1240
1241 default:
1242 {
1243 CHECK(!"should not be here.");
1244 break;
1245 }
1246 }
1247}
1248
1249// static
1250size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1251 size_t n = 0;
1252 for (size_t i = 0; i < buffers.size(); ++i) {
1253 if (!buffers[i].mOwnedByComponent) {
1254 ++n;
1255 }
1256 }
1257
1258 return n;
1259}
1260
1261status_t OMXCodec::freeBuffersOnPort(
1262 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1263 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1264
1265 status_t stickyErr = OK;
1266
1267 for (size_t i = buffers->size(); i-- > 0;) {
1268 BufferInfo *info = &buffers->editItemAt(i);
1269
1270 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1271 continue;
1272 }
1273
1274 CHECK_EQ(info->mOwnedByComponent, false);
1275
Andreas Huber92022852009-09-14 15:24:14 -07001276 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1277
Andreas Huberbe06d262009-08-14 14:37:10 -07001278 status_t err =
1279 mOMX->free_buffer(mNode, portIndex, info->mBuffer);
1280
1281 if (err != OK) {
1282 stickyErr = err;
1283 }
1284
1285 if (info->mMediaBuffer != NULL) {
1286 info->mMediaBuffer->setObserver(NULL);
1287
1288 // Make sure nobody but us owns this buffer at this point.
1289 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1290
1291 info->mMediaBuffer->release();
1292 }
1293
1294 buffers->removeAt(i);
1295 }
1296
1297 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1298
1299 return stickyErr;
1300}
1301
1302void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001303 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001304
1305 CHECK_EQ(mState, EXECUTING);
1306 CHECK_EQ(portIndex, kPortIndexOutput);
1307 setState(RECONFIGURING);
1308
1309 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001310 if (!flushPortAsync(portIndex)) {
1311 onCmdComplete(OMX_CommandFlush, portIndex);
1312 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001313 } else {
1314 disablePortAsync(portIndex);
1315 }
1316}
1317
Andreas Huber404cc412009-08-25 14:26:05 -07001318bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001319 CHECK(mState == EXECUTING || mState == RECONFIGURING
1320 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001321
Andreas Huber4c483422009-09-02 16:05:36 -07001322 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001323 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1324 mPortBuffers[portIndex].size());
1325
Andreas Huberbe06d262009-08-14 14:37:10 -07001326 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1327 mPortStatus[portIndex] = SHUTTING_DOWN;
1328
Andreas Huber404cc412009-08-25 14:26:05 -07001329 if ((mQuirks & kRequiresFlushCompleteEmulation)
1330 && countBuffersWeOwn(mPortBuffers[portIndex])
1331 == mPortBuffers[portIndex].size()) {
1332 // No flush is necessary and this component fails to send a
1333 // flush-complete event in this case.
1334
1335 return false;
1336 }
1337
Andreas Huberbe06d262009-08-14 14:37:10 -07001338 status_t err =
1339 mOMX->send_command(mNode, OMX_CommandFlush, portIndex);
1340 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001341
1342 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001343}
1344
1345void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1346 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1347
1348 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1349 mPortStatus[portIndex] = DISABLING;
1350
1351 status_t err =
1352 mOMX->send_command(mNode, OMX_CommandPortDisable, portIndex);
1353 CHECK_EQ(err, OK);
1354
1355 freeBuffersOnPort(portIndex, true);
1356}
1357
1358void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1359 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1360
1361 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1362 mPortStatus[portIndex] = ENABLING;
1363
1364 status_t err =
1365 mOMX->send_command(mNode, OMX_CommandPortEnable, portIndex);
1366 CHECK_EQ(err, OK);
1367}
1368
1369void OMXCodec::fillOutputBuffers() {
1370 CHECK_EQ(mState, EXECUTING);
1371
1372 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1373 for (size_t i = 0; i < buffers->size(); ++i) {
1374 fillOutputBuffer(&buffers->editItemAt(i));
1375 }
1376}
1377
1378void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001379 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001380
1381 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1382 for (size_t i = 0; i < buffers->size(); ++i) {
1383 drainInputBuffer(&buffers->editItemAt(i));
1384 }
1385}
1386
1387void OMXCodec::drainInputBuffer(BufferInfo *info) {
1388 CHECK_EQ(info->mOwnedByComponent, false);
1389
1390 if (mSignalledEOS) {
1391 return;
1392 }
1393
1394 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1395 const CodecSpecificData *specific =
1396 mCodecSpecificData[mCodecSpecificDataIndex];
1397
1398 size_t size = specific->mSize;
1399
Andreas Hubere6c40962009-09-10 14:13:30 -07001400 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07001401 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001402 static const uint8_t kNALStartCode[4] =
1403 { 0x00, 0x00, 0x00, 0x01 };
1404
1405 CHECK(info->mMem->size() >= specific->mSize + 4);
1406
1407 size += 4;
1408
1409 memcpy(info->mMem->pointer(), kNALStartCode, 4);
1410 memcpy((uint8_t *)info->mMem->pointer() + 4,
1411 specific->mData, specific->mSize);
1412 } else {
1413 CHECK(info->mMem->size() >= specific->mSize);
1414 memcpy(info->mMem->pointer(), specific->mData, specific->mSize);
1415 }
1416
Andreas Huber3f427072009-10-08 11:02:27 -07001417 status_t err = mOMX->empty_buffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07001418 mNode, info->mBuffer, 0, size,
1419 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1420 0);
Andreas Huber3f427072009-10-08 11:02:27 -07001421 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001422
1423 info->mOwnedByComponent = true;
1424
1425 ++mCodecSpecificDataIndex;
1426 return;
1427 }
1428
1429 MediaBuffer *srcBuffer;
1430 status_t err;
1431 if (mSeekTimeUs >= 0) {
1432 MediaSource::ReadOptions options;
1433 options.setSeekTo(mSeekTimeUs);
1434 mSeekTimeUs = -1;
1435
1436 err = mSource->read(&srcBuffer, &options);
1437 } else {
1438 err = mSource->read(&srcBuffer);
1439 }
1440
1441 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
1442 OMX_TICKS timestamp = 0;
1443 size_t srcLength = 0;
1444
1445 if (err != OK) {
Andreas Huber4c483422009-09-02 16:05:36 -07001446 CODEC_LOGV("signalling end of input stream.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001447 flags |= OMX_BUFFERFLAG_EOS;
1448
1449 mSignalledEOS = true;
1450 } else {
1451 srcLength = srcBuffer->range_length();
1452
1453 if (info->mMem->size() < srcLength) {
1454 LOGE("info->mMem->size() = %d, srcLength = %d",
1455 info->mMem->size(), srcLength);
1456 }
1457 CHECK(info->mMem->size() >= srcLength);
1458 memcpy(info->mMem->pointer(),
1459 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
1460 srcLength);
1461
1462 int32_t units, scale;
1463 if (srcBuffer->meta_data()->findInt32(kKeyTimeUnits, &units)
1464 && srcBuffer->meta_data()->findInt32(kKeyTimeScale, &scale)) {
1465 timestamp = ((OMX_TICKS)units * 1000000) / scale;
1466
Andreas Huber4c483422009-09-02 16:05:36 -07001467 CODEC_LOGV("Calling empty_buffer on buffer %p (length %d)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001468 info->mBuffer, srcLength);
Andreas Huber4c483422009-09-02 16:05:36 -07001469 CODEC_LOGV("Calling empty_buffer with timestamp %lld us (%.2f secs)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001470 timestamp, timestamp / 1E6);
1471 }
1472 }
1473
Andreas Huberbe06d262009-08-14 14:37:10 -07001474 if (srcBuffer != NULL) {
1475 srcBuffer->release();
1476 srcBuffer = NULL;
1477 }
Andreas Huber3f427072009-10-08 11:02:27 -07001478
1479 err = mOMX->empty_buffer(
1480 mNode, info->mBuffer, 0, srcLength,
1481 flags, timestamp);
1482
1483 if (err != OK) {
1484 setState(ERROR);
1485 return;
1486 }
1487
1488 info->mOwnedByComponent = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001489}
1490
1491void OMXCodec::fillOutputBuffer(BufferInfo *info) {
1492 CHECK_EQ(info->mOwnedByComponent, false);
1493
Andreas Huber404cc412009-08-25 14:26:05 -07001494 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07001495 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07001496 "calling fillOutputBuffer");
1497 return;
1498 }
1499
Andreas Huber4c483422009-09-02 16:05:36 -07001500 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber3f427072009-10-08 11:02:27 -07001501 status_t err = mOMX->fill_buffer(mNode, info->mBuffer);
1502 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001503
1504 info->mOwnedByComponent = true;
1505}
1506
1507void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
1508 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1509 for (size_t i = 0; i < buffers->size(); ++i) {
1510 if ((*buffers)[i].mBuffer == buffer) {
1511 drainInputBuffer(&buffers->editItemAt(i));
1512 return;
1513 }
1514 }
1515
1516 CHECK(!"should not be here.");
1517}
1518
1519void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
1520 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1521 for (size_t i = 0; i < buffers->size(); ++i) {
1522 if ((*buffers)[i].mBuffer == buffer) {
1523 fillOutputBuffer(&buffers->editItemAt(i));
1524 return;
1525 }
1526 }
1527
1528 CHECK(!"should not be here.");
1529}
1530
1531void OMXCodec::setState(State newState) {
1532 mState = newState;
1533 mAsyncCompletion.signal();
1534
1535 // This may cause some spurious wakeups but is necessary to
1536 // unblock the reader if we enter ERROR state.
1537 mBufferFilled.signal();
1538}
1539
Andreas Huberda050cf22009-09-02 14:01:43 -07001540void OMXCodec::setRawAudioFormat(
1541 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
1542 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07001543 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07001544 pcmParams.nPortIndex = portIndex;
1545
1546 status_t err = mOMX->get_parameter(
1547 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1548
1549 CHECK_EQ(err, OK);
1550
1551 pcmParams.nChannels = numChannels;
1552 pcmParams.eNumData = OMX_NumericalDataSigned;
1553 pcmParams.bInterleaved = OMX_TRUE;
1554 pcmParams.nBitPerSample = 16;
1555 pcmParams.nSamplingRate = sampleRate;
1556 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
1557
1558 if (numChannels == 1) {
1559 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
1560 } else {
1561 CHECK_EQ(numChannels, 2);
1562
1563 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
1564 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
1565 }
1566
1567 err = mOMX->set_parameter(
1568 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1569
1570 CHECK_EQ(err, OK);
1571}
1572
Andreas Huberbe06d262009-08-14 14:37:10 -07001573void OMXCodec::setAMRFormat() {
1574 if (!mIsEncoder) {
1575 OMX_AUDIO_PARAM_AMRTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001576 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001577 def.nPortIndex = kPortIndexInput;
1578
1579 status_t err =
1580 mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1581
1582 CHECK_EQ(err, OK);
1583
1584 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1585 def.eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
1586
1587 err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1588 CHECK_EQ(err, OK);
1589 }
1590
1591 ////////////////////////
1592
1593 if (mIsEncoder) {
1594 sp<MetaData> format = mSource->getFormat();
1595 int32_t sampleRate;
1596 int32_t numChannels;
1597 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1598 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1599
Andreas Huberda050cf22009-09-02 14:01:43 -07001600 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
Andreas Huberbe06d262009-08-14 14:37:10 -07001601 }
1602}
1603
Andreas Huberee606e62009-09-08 10:19:21 -07001604void OMXCodec::setAMRWBFormat() {
1605 if (!mIsEncoder) {
1606 OMX_AUDIO_PARAM_AMRTYPE def;
1607 InitOMXParams(&def);
1608 def.nPortIndex = kPortIndexInput;
1609
1610 status_t err =
1611 mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1612
1613 CHECK_EQ(err, OK);
1614
1615 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1616 def.eAMRBandMode = OMX_AUDIO_AMRBandModeWB0;
1617
1618 err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1619 CHECK_EQ(err, OK);
1620 }
1621
1622 ////////////////////////
1623
1624 if (mIsEncoder) {
1625 sp<MetaData> format = mSource->getFormat();
1626 int32_t sampleRate;
1627 int32_t numChannels;
1628 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1629 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1630
1631 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1632 }
1633}
1634
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07001635void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) {
Andreas Huberda050cf22009-09-02 14:01:43 -07001636 if (mIsEncoder) {
1637 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1638 } else {
1639 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07001640 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07001641 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07001642
Andreas Huberda050cf22009-09-02 14:01:43 -07001643 status_t err = mOMX->get_parameter(
1644 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1645 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001646
Andreas Huberda050cf22009-09-02 14:01:43 -07001647 profile.nChannels = numChannels;
1648 profile.nSampleRate = sampleRate;
1649 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07001650
Andreas Huberda050cf22009-09-02 14:01:43 -07001651 err = mOMX->set_parameter(
1652 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1653 CHECK_EQ(err, OK);
1654 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001655}
1656
1657void OMXCodec::setImageOutputFormat(
1658 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07001659 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001660
1661#if 0
1662 OMX_INDEXTYPE index;
1663 status_t err = mOMX->get_extension_index(
1664 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
1665 CHECK_EQ(err, OK);
1666
1667 err = mOMX->set_config(mNode, index, &format, sizeof(format));
1668 CHECK_EQ(err, OK);
1669#endif
1670
1671 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001672 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001673 def.nPortIndex = kPortIndexOutput;
1674
1675 status_t err = mOMX->get_parameter(
1676 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1677 CHECK_EQ(err, OK);
1678
1679 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1680
1681 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001682
Andreas Huberbe06d262009-08-14 14:37:10 -07001683 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
1684 imageDef->eColorFormat = format;
1685 imageDef->nFrameWidth = width;
1686 imageDef->nFrameHeight = height;
1687
1688 switch (format) {
1689 case OMX_COLOR_FormatYUV420PackedPlanar:
1690 case OMX_COLOR_FormatYUV411Planar:
1691 {
1692 def.nBufferSize = (width * height * 3) / 2;
1693 break;
1694 }
1695
1696 case OMX_COLOR_FormatCbYCrY:
1697 {
1698 def.nBufferSize = width * height * 2;
1699 break;
1700 }
1701
1702 case OMX_COLOR_Format32bitARGB8888:
1703 {
1704 def.nBufferSize = width * height * 4;
1705 break;
1706 }
1707
Andreas Huber201511c2009-09-08 14:01:44 -07001708 case OMX_COLOR_Format16bitARGB4444:
1709 case OMX_COLOR_Format16bitARGB1555:
1710 case OMX_COLOR_Format16bitRGB565:
1711 case OMX_COLOR_Format16bitBGR565:
1712 {
1713 def.nBufferSize = width * height * 2;
1714 break;
1715 }
1716
Andreas Huberbe06d262009-08-14 14:37:10 -07001717 default:
1718 CHECK(!"Should not be here. Unknown color format.");
1719 break;
1720 }
1721
Andreas Huber5c0a9132009-08-20 11:16:40 -07001722 def.nBufferCountActual = def.nBufferCountMin;
1723
Andreas Huberbe06d262009-08-14 14:37:10 -07001724 err = mOMX->set_parameter(
1725 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1726 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07001727}
Andreas Huberbe06d262009-08-14 14:37:10 -07001728
Andreas Huber5c0a9132009-08-20 11:16:40 -07001729void OMXCodec::setJPEGInputFormat(
1730 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
1731 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001732 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001733 def.nPortIndex = kPortIndexInput;
1734
Andreas Huber5c0a9132009-08-20 11:16:40 -07001735 status_t err = mOMX->get_parameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001736 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1737 CHECK_EQ(err, OK);
1738
Andreas Huber5c0a9132009-08-20 11:16:40 -07001739 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1740 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
1741
Andreas Huberbe06d262009-08-14 14:37:10 -07001742 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
1743 imageDef->nFrameWidth = width;
1744 imageDef->nFrameHeight = height;
1745
Andreas Huber5c0a9132009-08-20 11:16:40 -07001746 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07001747 def.nBufferCountActual = def.nBufferCountMin;
1748
1749 err = mOMX->set_parameter(
1750 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1751 CHECK_EQ(err, OK);
1752}
1753
1754void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
1755 CodecSpecificData *specific =
1756 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
1757
1758 specific->mSize = size;
1759 memcpy(specific->mData, data, size);
1760
1761 mCodecSpecificData.push(specific);
1762}
1763
1764void OMXCodec::clearCodecSpecificData() {
1765 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
1766 free(mCodecSpecificData.editItemAt(i));
1767 }
1768 mCodecSpecificData.clear();
1769 mCodecSpecificDataIndex = 0;
1770}
1771
1772status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07001773 Mutex::Autolock autoLock(mLock);
1774
Andreas Huberbe06d262009-08-14 14:37:10 -07001775 if (mState != LOADED) {
1776 return UNKNOWN_ERROR;
1777 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07001778
Andreas Huberbe06d262009-08-14 14:37:10 -07001779 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07001780 if (mQuirks & kWantsNALFragments) {
1781 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07001782 }
1783 status_t err = mSource->start(params.get());
1784
1785 if (err != OK) {
1786 return err;
1787 }
1788
1789 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07001790 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001791 mSignalledEOS = false;
1792 mNoMoreOutputData = false;
1793 mSeekTimeUs = -1;
1794 mFilledBuffers.clear();
1795
1796 return init();
1797}
1798
1799status_t OMXCodec::stop() {
Andreas Huber4c483422009-09-02 16:05:36 -07001800 CODEC_LOGV("stop");
Andreas Huberbe06d262009-08-14 14:37:10 -07001801
1802 Mutex::Autolock autoLock(mLock);
1803
1804 while (isIntermediateState(mState)) {
1805 mAsyncCompletion.wait(mLock);
1806 }
1807
1808 switch (mState) {
1809 case LOADED:
1810 case ERROR:
1811 break;
1812
1813 case EXECUTING:
1814 {
1815 setState(EXECUTING_TO_IDLE);
1816
Andreas Huber127fcdc2009-08-26 16:27:02 -07001817 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07001818 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001819 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07001820
Andreas Huber127fcdc2009-08-26 16:27:02 -07001821 bool emulateInputFlushCompletion =
1822 !flushPortAsync(kPortIndexInput);
1823
1824 bool emulateOutputFlushCompletion =
1825 !flushPortAsync(kPortIndexOutput);
1826
1827 if (emulateInputFlushCompletion) {
1828 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1829 }
1830
1831 if (emulateOutputFlushCompletion) {
1832 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1833 }
1834 } else {
1835 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1836 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1837
1838 status_t err =
1839 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1840 CHECK_EQ(err, OK);
1841 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001842
1843 while (mState != LOADED && mState != ERROR) {
1844 mAsyncCompletion.wait(mLock);
1845 }
1846
1847 break;
1848 }
1849
1850 default:
1851 {
1852 CHECK(!"should not be here.");
1853 break;
1854 }
1855 }
1856
1857 mSource->stop();
1858
1859 return OK;
1860}
1861
1862sp<MetaData> OMXCodec::getFormat() {
1863 return mOutputFormat;
1864}
1865
1866status_t OMXCodec::read(
1867 MediaBuffer **buffer, const ReadOptions *options) {
1868 *buffer = NULL;
1869
1870 Mutex::Autolock autoLock(mLock);
1871
Andreas Huberd06e5b82009-08-28 13:18:14 -07001872 if (mState != EXECUTING && mState != RECONFIGURING) {
1873 return UNKNOWN_ERROR;
1874 }
1875
Andreas Huber42978e52009-08-27 10:08:39 -07001876 if (mInitialBufferSubmit) {
1877 mInitialBufferSubmit = false;
1878
1879 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07001880
Andreas Huberd06e5b82009-08-28 13:18:14 -07001881 if (mState == EXECUTING) {
1882 // Otherwise mState == RECONFIGURING and this code will trigger
1883 // after the output port is reenabled.
1884 fillOutputBuffers();
1885 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001886 }
1887
1888 int64_t seekTimeUs;
1889 if (options && options->getSeekTo(&seekTimeUs)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001890 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07001891
1892 mSignalledEOS = false;
1893 mNoMoreOutputData = false;
1894
1895 CHECK(seekTimeUs >= 0);
1896 mSeekTimeUs = seekTimeUs;
1897
1898 mFilledBuffers.clear();
1899
1900 CHECK_EQ(mState, EXECUTING);
1901
Andreas Huber404cc412009-08-25 14:26:05 -07001902 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
1903 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
1904
1905 if (emulateInputFlushCompletion) {
1906 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1907 }
1908
1909 if (emulateOutputFlushCompletion) {
1910 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1911 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001912 }
1913
1914 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
1915 mBufferFilled.wait(mLock);
1916 }
1917
1918 if (mState == ERROR) {
1919 return UNKNOWN_ERROR;
1920 }
1921
1922 if (mFilledBuffers.empty()) {
1923 return ERROR_END_OF_STREAM;
1924 }
1925
1926 size_t index = *mFilledBuffers.begin();
1927 mFilledBuffers.erase(mFilledBuffers.begin());
1928
1929 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
1930 info->mMediaBuffer->add_ref();
1931 *buffer = info->mMediaBuffer;
1932
1933 return OK;
1934}
1935
1936void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
1937 Mutex::Autolock autoLock(mLock);
1938
1939 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1940 for (size_t i = 0; i < buffers->size(); ++i) {
1941 BufferInfo *info = &buffers->editItemAt(i);
1942
1943 if (info->mMediaBuffer == buffer) {
1944 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
1945 fillOutputBuffer(info);
1946 return;
1947 }
1948 }
1949
1950 CHECK(!"should not be here.");
1951}
1952
1953static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
1954 static const char *kNames[] = {
1955 "OMX_IMAGE_CodingUnused",
1956 "OMX_IMAGE_CodingAutoDetect",
1957 "OMX_IMAGE_CodingJPEG",
1958 "OMX_IMAGE_CodingJPEG2K",
1959 "OMX_IMAGE_CodingEXIF",
1960 "OMX_IMAGE_CodingTIFF",
1961 "OMX_IMAGE_CodingGIF",
1962 "OMX_IMAGE_CodingPNG",
1963 "OMX_IMAGE_CodingLZW",
1964 "OMX_IMAGE_CodingBMP",
1965 };
1966
1967 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
1968
1969 if (type < 0 || (size_t)type >= numNames) {
1970 return "UNKNOWN";
1971 } else {
1972 return kNames[type];
1973 }
1974}
1975
1976static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
1977 static const char *kNames[] = {
1978 "OMX_COLOR_FormatUnused",
1979 "OMX_COLOR_FormatMonochrome",
1980 "OMX_COLOR_Format8bitRGB332",
1981 "OMX_COLOR_Format12bitRGB444",
1982 "OMX_COLOR_Format16bitARGB4444",
1983 "OMX_COLOR_Format16bitARGB1555",
1984 "OMX_COLOR_Format16bitRGB565",
1985 "OMX_COLOR_Format16bitBGR565",
1986 "OMX_COLOR_Format18bitRGB666",
1987 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07001988 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07001989 "OMX_COLOR_Format24bitRGB888",
1990 "OMX_COLOR_Format24bitBGR888",
1991 "OMX_COLOR_Format24bitARGB1887",
1992 "OMX_COLOR_Format25bitARGB1888",
1993 "OMX_COLOR_Format32bitBGRA8888",
1994 "OMX_COLOR_Format32bitARGB8888",
1995 "OMX_COLOR_FormatYUV411Planar",
1996 "OMX_COLOR_FormatYUV411PackedPlanar",
1997 "OMX_COLOR_FormatYUV420Planar",
1998 "OMX_COLOR_FormatYUV420PackedPlanar",
1999 "OMX_COLOR_FormatYUV420SemiPlanar",
2000 "OMX_COLOR_FormatYUV422Planar",
2001 "OMX_COLOR_FormatYUV422PackedPlanar",
2002 "OMX_COLOR_FormatYUV422SemiPlanar",
2003 "OMX_COLOR_FormatYCbYCr",
2004 "OMX_COLOR_FormatYCrYCb",
2005 "OMX_COLOR_FormatCbYCrY",
2006 "OMX_COLOR_FormatCrYCbY",
2007 "OMX_COLOR_FormatYUV444Interleaved",
2008 "OMX_COLOR_FormatRawBayer8bit",
2009 "OMX_COLOR_FormatRawBayer10bit",
2010 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002011 "OMX_COLOR_FormatL2",
2012 "OMX_COLOR_FormatL4",
2013 "OMX_COLOR_FormatL8",
2014 "OMX_COLOR_FormatL16",
2015 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002016 "OMX_COLOR_FormatL32",
2017 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2018 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2019 "OMX_COLOR_Format18BitBGR666",
2020 "OMX_COLOR_Format24BitARGB6666",
2021 "OMX_COLOR_Format24BitABGR6666",
2022 };
2023
2024 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2025
Andreas Huberbe06d262009-08-14 14:37:10 -07002026 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2027 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2028 } else if (type < 0 || (size_t)type >= numNames) {
2029 return "UNKNOWN";
2030 } else {
2031 return kNames[type];
2032 }
2033}
2034
2035static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2036 static const char *kNames[] = {
2037 "OMX_VIDEO_CodingUnused",
2038 "OMX_VIDEO_CodingAutoDetect",
2039 "OMX_VIDEO_CodingMPEG2",
2040 "OMX_VIDEO_CodingH263",
2041 "OMX_VIDEO_CodingMPEG4",
2042 "OMX_VIDEO_CodingWMV",
2043 "OMX_VIDEO_CodingRV",
2044 "OMX_VIDEO_CodingAVC",
2045 "OMX_VIDEO_CodingMJPEG",
2046 };
2047
2048 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2049
2050 if (type < 0 || (size_t)type >= numNames) {
2051 return "UNKNOWN";
2052 } else {
2053 return kNames[type];
2054 }
2055}
2056
2057static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2058 static const char *kNames[] = {
2059 "OMX_AUDIO_CodingUnused",
2060 "OMX_AUDIO_CodingAutoDetect",
2061 "OMX_AUDIO_CodingPCM",
2062 "OMX_AUDIO_CodingADPCM",
2063 "OMX_AUDIO_CodingAMR",
2064 "OMX_AUDIO_CodingGSMFR",
2065 "OMX_AUDIO_CodingGSMEFR",
2066 "OMX_AUDIO_CodingGSMHR",
2067 "OMX_AUDIO_CodingPDCFR",
2068 "OMX_AUDIO_CodingPDCEFR",
2069 "OMX_AUDIO_CodingPDCHR",
2070 "OMX_AUDIO_CodingTDMAFR",
2071 "OMX_AUDIO_CodingTDMAEFR",
2072 "OMX_AUDIO_CodingQCELP8",
2073 "OMX_AUDIO_CodingQCELP13",
2074 "OMX_AUDIO_CodingEVRC",
2075 "OMX_AUDIO_CodingSMV",
2076 "OMX_AUDIO_CodingG711",
2077 "OMX_AUDIO_CodingG723",
2078 "OMX_AUDIO_CodingG726",
2079 "OMX_AUDIO_CodingG729",
2080 "OMX_AUDIO_CodingAAC",
2081 "OMX_AUDIO_CodingMP3",
2082 "OMX_AUDIO_CodingSBC",
2083 "OMX_AUDIO_CodingVORBIS",
2084 "OMX_AUDIO_CodingWMA",
2085 "OMX_AUDIO_CodingRA",
2086 "OMX_AUDIO_CodingMIDI",
2087 };
2088
2089 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2090
2091 if (type < 0 || (size_t)type >= numNames) {
2092 return "UNKNOWN";
2093 } else {
2094 return kNames[type];
2095 }
2096}
2097
2098static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2099 static const char *kNames[] = {
2100 "OMX_AUDIO_PCMModeLinear",
2101 "OMX_AUDIO_PCMModeALaw",
2102 "OMX_AUDIO_PCMModeMULaw",
2103 };
2104
2105 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2106
2107 if (type < 0 || (size_t)type >= numNames) {
2108 return "UNKNOWN";
2109 } else {
2110 return kNames[type];
2111 }
2112}
2113
Andreas Huber7ae02c82009-09-09 16:29:47 -07002114static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2115 static const char *kNames[] = {
2116 "OMX_AUDIO_AMRBandModeUnused",
2117 "OMX_AUDIO_AMRBandModeNB0",
2118 "OMX_AUDIO_AMRBandModeNB1",
2119 "OMX_AUDIO_AMRBandModeNB2",
2120 "OMX_AUDIO_AMRBandModeNB3",
2121 "OMX_AUDIO_AMRBandModeNB4",
2122 "OMX_AUDIO_AMRBandModeNB5",
2123 "OMX_AUDIO_AMRBandModeNB6",
2124 "OMX_AUDIO_AMRBandModeNB7",
2125 "OMX_AUDIO_AMRBandModeWB0",
2126 "OMX_AUDIO_AMRBandModeWB1",
2127 "OMX_AUDIO_AMRBandModeWB2",
2128 "OMX_AUDIO_AMRBandModeWB3",
2129 "OMX_AUDIO_AMRBandModeWB4",
2130 "OMX_AUDIO_AMRBandModeWB5",
2131 "OMX_AUDIO_AMRBandModeWB6",
2132 "OMX_AUDIO_AMRBandModeWB7",
2133 "OMX_AUDIO_AMRBandModeWB8",
2134 };
2135
2136 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2137
2138 if (type < 0 || (size_t)type >= numNames) {
2139 return "UNKNOWN";
2140 } else {
2141 return kNames[type];
2142 }
2143}
2144
2145static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2146 static const char *kNames[] = {
2147 "OMX_AUDIO_AMRFrameFormatConformance",
2148 "OMX_AUDIO_AMRFrameFormatIF1",
2149 "OMX_AUDIO_AMRFrameFormatIF2",
2150 "OMX_AUDIO_AMRFrameFormatFSF",
2151 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2152 "OMX_AUDIO_AMRFrameFormatITU",
2153 };
2154
2155 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2156
2157 if (type < 0 || (size_t)type >= numNames) {
2158 return "UNKNOWN";
2159 } else {
2160 return kNames[type];
2161 }
2162}
Andreas Huberbe06d262009-08-14 14:37:10 -07002163
2164void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2165 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002166 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002167 def.nPortIndex = portIndex;
2168
2169 status_t err = mOMX->get_parameter(
2170 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2171 CHECK_EQ(err, OK);
2172
2173 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2174
2175 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2176 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2177
2178 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2179 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2180 printf(" nBufferSize = %ld\n", def.nBufferSize);
2181
2182 switch (def.eDomain) {
2183 case OMX_PortDomainImage:
2184 {
2185 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2186
2187 printf("\n");
2188 printf(" // Image\n");
2189 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2190 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2191 printf(" nStride = %ld\n", imageDef->nStride);
2192
2193 printf(" eCompressionFormat = %s\n",
2194 imageCompressionFormatString(imageDef->eCompressionFormat));
2195
2196 printf(" eColorFormat = %s\n",
2197 colorFormatString(imageDef->eColorFormat));
2198
2199 break;
2200 }
2201
2202 case OMX_PortDomainVideo:
2203 {
2204 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2205
2206 printf("\n");
2207 printf(" // Video\n");
2208 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2209 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2210 printf(" nStride = %ld\n", videoDef->nStride);
2211
2212 printf(" eCompressionFormat = %s\n",
2213 videoCompressionFormatString(videoDef->eCompressionFormat));
2214
2215 printf(" eColorFormat = %s\n",
2216 colorFormatString(videoDef->eColorFormat));
2217
2218 break;
2219 }
2220
2221 case OMX_PortDomainAudio:
2222 {
2223 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2224
2225 printf("\n");
2226 printf(" // Audio\n");
2227 printf(" eEncoding = %s\n",
2228 audioCodingTypeString(audioDef->eEncoding));
2229
2230 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2231 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002232 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002233 params.nPortIndex = portIndex;
2234
2235 err = mOMX->get_parameter(
2236 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2237 CHECK_EQ(err, OK);
2238
2239 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
2240 printf(" nChannels = %ld\n", params.nChannels);
2241 printf(" bInterleaved = %d\n", params.bInterleaved);
2242 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
2243
2244 printf(" eNumData = %s\n",
2245 params.eNumData == OMX_NumericalDataSigned
2246 ? "signed" : "unsigned");
2247
2248 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07002249 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
2250 OMX_AUDIO_PARAM_AMRTYPE amr;
2251 InitOMXParams(&amr);
2252 amr.nPortIndex = portIndex;
2253
2254 err = mOMX->get_parameter(
2255 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2256 CHECK_EQ(err, OK);
2257
2258 printf(" nChannels = %ld\n", amr.nChannels);
2259 printf(" eAMRBandMode = %s\n",
2260 amrBandModeString(amr.eAMRBandMode));
2261 printf(" eAMRFrameFormat = %s\n",
2262 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07002263 }
2264
2265 break;
2266 }
2267
2268 default:
2269 {
2270 printf(" // Unknown\n");
2271 break;
2272 }
2273 }
2274
2275 printf("}\n");
2276}
2277
2278void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
2279 mOutputFormat = new MetaData;
2280 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
2281
2282 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002283 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002284 def.nPortIndex = kPortIndexOutput;
2285
2286 status_t err = mOMX->get_parameter(
2287 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2288 CHECK_EQ(err, OK);
2289
2290 switch (def.eDomain) {
2291 case OMX_PortDomainImage:
2292 {
2293 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2294 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2295
Andreas Hubere6c40962009-09-10 14:13:30 -07002296 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002297 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
2298 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
2299 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
2300 break;
2301 }
2302
2303 case OMX_PortDomainAudio:
2304 {
2305 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
2306
Andreas Huberda050cf22009-09-02 14:01:43 -07002307 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
2308 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002309 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07002310 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002311
Andreas Huberda050cf22009-09-02 14:01:43 -07002312 err = mOMX->get_parameter(
2313 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2314 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002315
Andreas Huberda050cf22009-09-02 14:01:43 -07002316 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
2317 CHECK_EQ(params.nBitPerSample, 16);
2318 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07002319
Andreas Huberda050cf22009-09-02 14:01:43 -07002320 int32_t numChannels, sampleRate;
2321 inputFormat->findInt32(kKeyChannelCount, &numChannels);
2322 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07002323
Andreas Huberda050cf22009-09-02 14:01:43 -07002324 if ((OMX_U32)numChannels != params.nChannels) {
2325 LOGW("Codec outputs a different number of channels than "
2326 "the input stream contains.");
2327 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002328
Andreas Hubere6c40962009-09-10 14:13:30 -07002329 mOutputFormat->setCString(
2330 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07002331
2332 // Use the codec-advertised number of channels, as some
2333 // codecs appear to output stereo even if the input data is
2334 // mono.
2335 mOutputFormat->setInt32(kKeyChannelCount, params.nChannels);
2336
2337 // The codec-reported sampleRate is not reliable...
2338 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
2339 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07002340 OMX_AUDIO_PARAM_AMRTYPE amr;
2341 InitOMXParams(&amr);
2342 amr.nPortIndex = kPortIndexOutput;
2343
2344 err = mOMX->get_parameter(
2345 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2346 CHECK_EQ(err, OK);
2347
2348 CHECK_EQ(amr.nChannels, 1);
2349 mOutputFormat->setInt32(kKeyChannelCount, 1);
2350
2351 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
2352 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002353 mOutputFormat->setCString(
2354 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002355 mOutputFormat->setInt32(kKeySampleRate, 8000);
2356 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
2357 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002358 mOutputFormat->setCString(
2359 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002360 mOutputFormat->setInt32(kKeySampleRate, 16000);
2361 } else {
2362 CHECK(!"Unknown AMR band mode.");
2363 }
Andreas Huberda050cf22009-09-02 14:01:43 -07002364 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002365 mOutputFormat->setCString(
2366 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
Andreas Huberda050cf22009-09-02 14:01:43 -07002367 } else {
2368 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07002369 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002370 break;
2371 }
2372
2373 case OMX_PortDomainVideo:
2374 {
2375 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
2376
2377 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002378 mOutputFormat->setCString(
2379 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002380 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002381 mOutputFormat->setCString(
2382 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002383 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002384 mOutputFormat->setCString(
2385 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07002386 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002387 mOutputFormat->setCString(
2388 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07002389 } else {
2390 CHECK(!"Unknown compression format.");
2391 }
2392
2393 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
2394 // This component appears to be lying to me.
2395 mOutputFormat->setInt32(
2396 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
2397 mOutputFormat->setInt32(
2398 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
2399 } else {
2400 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
2401 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
2402 }
2403
2404 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
2405 break;
2406 }
2407
2408 default:
2409 {
2410 CHECK(!"should not be here, neither audio nor video.");
2411 break;
2412 }
2413 }
2414}
2415
Andreas Hubere6c40962009-09-10 14:13:30 -07002416////////////////////////////////////////////////////////////////////////////////
2417
2418status_t QueryCodecs(
2419 const sp<IOMX> &omx,
2420 const char *mime, bool queryDecoders,
2421 Vector<CodecCapabilities> *results) {
2422 results->clear();
2423
2424 for (int index = 0;; ++index) {
2425 const char *componentName;
2426
2427 if (!queryDecoders) {
2428 componentName = GetCodec(
2429 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
2430 mime, index);
2431 } else {
2432 componentName = GetCodec(
2433 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
2434 mime, index);
2435 }
2436
2437 if (!componentName) {
2438 return OK;
2439 }
2440
2441 IOMX::node_id node;
2442 status_t err = omx->allocate_node(componentName, &node);
2443
2444 if (err != OK) {
2445 continue;
2446 }
2447
2448 OMXCodec::setComponentRole(omx, node, queryDecoders, mime);
2449
2450 results->push();
2451 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
2452 caps->mComponentName = componentName;
2453
2454 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
2455 InitOMXParams(&param);
2456
2457 param.nPortIndex = queryDecoders ? 0 : 1;
2458
2459 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
2460 err = omx->get_parameter(
2461 node, OMX_IndexParamVideoProfileLevelQuerySupported,
2462 &param, sizeof(param));
2463
2464 if (err != OK) {
2465 break;
2466 }
2467
2468 CodecProfileLevel profileLevel;
2469 profileLevel.mProfile = param.eProfile;
2470 profileLevel.mLevel = param.eLevel;
2471
2472 caps->mProfileLevels.push(profileLevel);
2473 }
2474
2475 CHECK_EQ(omx->free_node(node), OK);
2476 }
2477}
2478
Andreas Huberbe06d262009-08-14 14:37:10 -07002479} // namespace android