blob: 034457f2ba349df34d1f693bea5dfb759ce1311c [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>
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 Huberebf66ea2009-08-19 13:32:58 -070033#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070034#include <utils/Vector.h>
35
36#include <OMX_Audio.h>
37#include <OMX_Component.h>
38
39namespace android {
40
41struct CodecInfo {
42 const char *mime;
43 const char *codec;
44};
45
46static 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" },
Andreas Huberee606e62009-09-08 10:19:21 -070052 { "audio/amr-wb", "OMX.TI.WBAMR.decode" },
Andreas Huber5a65a6e2009-09-08 16:07:15 -070053 { "audio/amr-wb", "OMX.PV.amrdec" },
Andreas Huberbe06d262009-08-14 14:37:10 -070054 { "audio/mp4a-latm", "OMX.TI.AAC.decode" },
55 { "audio/mp4a-latm", "OMX.PV.aacdec" },
56 { "video/mp4v-es", "OMX.qcom.video.decoder.mpeg4" },
57 { "video/mp4v-es", "OMX.TI.Video.Decoder" },
58 { "video/mp4v-es", "OMX.PV.mpeg4dec" },
59 { "video/3gpp", "OMX.qcom.video.decoder.h263" },
60 { "video/3gpp", "OMX.TI.Video.Decoder" },
61 { "video/3gpp", "OMX.PV.h263dec" },
62 { "video/avc", "OMX.qcom.video.decoder.avc" },
63 { "video/avc", "OMX.TI.Video.Decoder" },
64 { "video/avc", "OMX.PV.avcdec" },
65};
66
67static const CodecInfo kEncoderInfo[] = {
68 { "audio/3gpp", "OMX.TI.AMR.encode" },
69 { "audio/3gpp", "OMX.PV.amrencnb" },
Andreas Huberee606e62009-09-08 10:19:21 -070070 { "audio/amr-wb", "OMX.TI.WBAMR.encode" },
Andreas Huberbe06d262009-08-14 14:37:10 -070071 { "audio/mp4a-latm", "OMX.TI.AAC.encode" },
72 { "audio/mp4a-latm", "OMX.PV.aacenc" },
73 { "video/mp4v-es", "OMX.qcom.video.encoder.mpeg4" },
74 { "video/mp4v-es", "OMX.TI.Video.encoder" },
75 { "video/mp4v-es", "OMX.PV.mpeg4enc" },
76 { "video/3gpp", "OMX.qcom.video.encoder.h263" },
77 { "video/3gpp", "OMX.TI.Video.encoder" },
78 { "video/3gpp", "OMX.PV.h263enc" },
79 { "video/avc", "OMX.TI.Video.encoder" },
80 { "video/avc", "OMX.PV.avcenc" },
81};
82
Andreas Hubere0873732009-09-10 09:57:53 -070083#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -070084#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
85
Andreas Huberbe06d262009-08-14 14:37:10 -070086struct OMXCodecObserver : public BnOMXObserver {
87 OMXCodecObserver(const wp<OMXCodec> &target)
88 : mTarget(target) {
89 }
90
91 // from IOMXObserver
92 virtual void on_message(const omx_message &msg) {
93 sp<OMXCodec> codec = mTarget.promote();
94
95 if (codec.get() != NULL) {
96 codec->on_message(msg);
97 }
98 }
99
100protected:
101 virtual ~OMXCodecObserver() {}
102
103private:
104 wp<OMXCodec> mTarget;
105
106 OMXCodecObserver(const OMXCodecObserver &);
107 OMXCodecObserver &operator=(const OMXCodecObserver &);
108};
109
110static const char *GetCodec(const CodecInfo *info, size_t numInfos,
111 const char *mime, int index) {
112 CHECK(index >= 0);
113 for(size_t i = 0; i < numInfos; ++i) {
114 if (!strcasecmp(mime, info[i].mime)) {
115 if (index == 0) {
116 return info[i].codec;
117 }
118
119 --index;
120 }
121 }
122
123 return NULL;
124}
125
Andreas Huberebf66ea2009-08-19 13:32:58 -0700126enum {
127 kAVCProfileBaseline = 0x42,
128 kAVCProfileMain = 0x4d,
129 kAVCProfileExtended = 0x58,
130 kAVCProfileHigh = 0x64,
131 kAVCProfileHigh10 = 0x6e,
132 kAVCProfileHigh422 = 0x7a,
133 kAVCProfileHigh444 = 0xf4,
134 kAVCProfileCAVLC444Intra = 0x2c
135};
136
137static const char *AVCProfileToString(uint8_t profile) {
138 switch (profile) {
139 case kAVCProfileBaseline:
140 return "Baseline";
141 case kAVCProfileMain:
142 return "Main";
143 case kAVCProfileExtended:
144 return "Extended";
145 case kAVCProfileHigh:
146 return "High";
147 case kAVCProfileHigh10:
148 return "High 10";
149 case kAVCProfileHigh422:
150 return "High 422";
151 case kAVCProfileHigh444:
152 return "High 444";
153 case kAVCProfileCAVLC444Intra:
154 return "CAVLC 444 Intra";
155 default: return "Unknown";
156 }
157}
158
Andreas Huber4c483422009-09-02 16:05:36 -0700159template<class T>
160static void InitOMXParams(T *params) {
161 params->nSize = sizeof(T);
162 params->nVersion.s.nVersionMajor = 1;
163 params->nVersion.s.nVersionMinor = 0;
164 params->nVersion.s.nRevision = 0;
165 params->nVersion.s.nStep = 0;
166}
167
Andreas Huberbe06d262009-08-14 14:37:10 -0700168// static
169sp<OMXCodec> OMXCodec::Create(
170 const sp<IOMX> &omx,
171 const sp<MetaData> &meta, bool createEncoder,
172 const sp<MediaSource> &source) {
173 const char *mime;
174 bool success = meta->findCString(kKeyMIMEType, &mime);
175 CHECK(success);
176
177 const char *componentName = NULL;
178 IOMX::node_id node = 0;
179 for (int index = 0;; ++index) {
180 if (createEncoder) {
181 componentName = GetCodec(
182 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
183 mime, index);
184 } else {
185 componentName = GetCodec(
186 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
187 mime, index);
188 }
189
190 if (!componentName) {
191 return NULL;
192 }
193
194 LOGV("Attempting to allocate OMX node '%s'", componentName);
195
196 status_t err = omx->allocate_node(componentName, &node);
197 if (err == OK) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700198 LOGI("Successfully allocated OMX node '%s'", componentName);
Andreas Huberbe06d262009-08-14 14:37:10 -0700199 break;
200 }
201 }
202
203 uint32_t quirks = 0;
204 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700205 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700206 }
207 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
208 quirks |= kNeedsFlushBeforeDisable;
209 }
210 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
211 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700212 quirks |= kRequiresFlushCompleteEmulation;
Andreas Huber127fcdc2009-08-26 16:27:02 -0700213
214 // The following is currently necessary for proper shutdown
215 // behaviour, but NOT enabled by default in order to make the
216 // bug reproducible...
217 // quirks |= kRequiresFlushBeforeShutdown;
Andreas Huberbe06d262009-08-14 14:37:10 -0700218 }
219 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
220 quirks |= kRequiresLoadedToIdleAfterAllocation;
221 quirks |= kRequiresAllocateBufferOnInputPorts;
222 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700223 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
224 // XXX Required on P....on only.
225 quirks |= kRequiresAllocateBufferOnOutputPorts;
226 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700227
228 sp<OMXCodec> codec = new OMXCodec(
229 omx, node, quirks, createEncoder, mime, componentName,
230 source);
231
232 uint32_t type;
233 const void *data;
234 size_t size;
235 if (meta->findData(kKeyESDS, &type, &data, &size)) {
236 ESDS esds((const char *)data, size);
237 CHECK_EQ(esds.InitCheck(), OK);
238
239 const void *codec_specific_data;
240 size_t codec_specific_data_size;
241 esds.getCodecSpecificInfo(
242 &codec_specific_data, &codec_specific_data_size);
243
244 printf("found codec-specific data of size %d\n",
245 codec_specific_data_size);
246
247 codec->addCodecSpecificData(
248 codec_specific_data, codec_specific_data_size);
249 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
250 printf("found avcc of size %d\n", size);
251
Andreas Huberebf66ea2009-08-19 13:32:58 -0700252 // Parse the AVCDecoderConfigurationRecord
253
254 const uint8_t *ptr = (const uint8_t *)data;
255
256 CHECK(size >= 7);
257 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
258 uint8_t profile = ptr[1];
259 uint8_t level = ptr[3];
260
261 CHECK((ptr[4] >> 2) == 0x3f); // reserved
262
263 size_t lengthSize = 1 + (ptr[4] & 3);
264
265 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
266 // violates it...
267 // CHECK((ptr[5] >> 5) == 7); // reserved
268
269 size_t numSeqParameterSets = ptr[5] & 31;
270
271 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700272 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700273
274 for (size_t i = 0; i < numSeqParameterSets; ++i) {
275 CHECK(size >= 2);
276 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700277
278 ptr += 2;
279 size -= 2;
280
Andreas Huberbe06d262009-08-14 14:37:10 -0700281 CHECK(size >= length);
282
283 codec->addCodecSpecificData(ptr, length);
284
285 ptr += length;
286 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700287 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700288
Andreas Huberebf66ea2009-08-19 13:32:58 -0700289 CHECK(size >= 1);
290 size_t numPictureParameterSets = *ptr;
291 ++ptr;
292 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700293
Andreas Huberebf66ea2009-08-19 13:32:58 -0700294 for (size_t i = 0; i < numPictureParameterSets; ++i) {
295 CHECK(size >= 2);
296 size_t length = U16_AT(ptr);
297
298 ptr += 2;
299 size -= 2;
300
301 CHECK(size >= length);
302
303 codec->addCodecSpecificData(ptr, length);
304
305 ptr += length;
306 size -= length;
307 }
308
309 LOGI("AVC profile = %d (%s), level = %d",
310 (int)profile, AVCProfileToString(profile), (int)level / 10);
311
312 if (!strcmp(componentName, "OMX.TI.Video.Decoder")
313 && (profile != kAVCProfileBaseline || level > 39)) {
314 // This stream exceeds the decoder's capabilities.
315
316 LOGE("Profile and/or level exceed the decoder's capabilities.");
317 return NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -0700318 }
319 }
320
321 if (!strcasecmp("audio/3gpp", mime)) {
322 codec->setAMRFormat();
323 }
Andreas Huberee606e62009-09-08 10:19:21 -0700324 if (!strcasecmp("audio/amr-wb", mime)) {
325 codec->setAMRWBFormat();
326 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700327 if (!strcasecmp("audio/mp4a-latm", mime)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700328 int32_t numChannels, sampleRate;
329 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
330 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
331
332 codec->setAACFormat(numChannels, sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700333 }
334 if (!strncasecmp(mime, "video/", 6)) {
335 int32_t width, height;
336 bool success = meta->findInt32(kKeyWidth, &width);
337 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700338 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700339
340 if (createEncoder) {
341 codec->setVideoInputFormat(mime, width, height);
342 } else {
343 codec->setVideoOutputFormat(mime, width, height);
344 }
345 }
346 if (!strcasecmp(mime, "image/jpeg")
347 && !strcmp(componentName, "OMX.TI.JPEG.decode")) {
348 OMX_COLOR_FORMATTYPE format =
349 OMX_COLOR_Format32bitARGB8888;
350 // OMX_COLOR_FormatYUV420PackedPlanar;
351 // OMX_COLOR_FormatCbYCrY;
352 // OMX_COLOR_FormatYUV411Planar;
353
354 int32_t width, height;
355 bool success = meta->findInt32(kKeyWidth, &width);
356 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700357
358 int32_t compressedSize;
359 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700360 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700361
362 CHECK(success);
363 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700364
365 codec->setImageOutputFormat(format, width, height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700366 codec->setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700367 }
368
Andreas Huberda050cf22009-09-02 14:01:43 -0700369 int32_t maxInputSize;
370 if (createEncoder && meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
371 codec->setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
372 }
373
374 if (!strcmp(componentName, "OMX.TI.AMR.encode")
375 || !strcmp(componentName, "OMX.TI.WBAMR.encode")) {
376 codec->setMinBufferSize(kPortIndexOutput, 8192); // XXX
377 }
378
Andreas Huberbe06d262009-08-14 14:37:10 -0700379 codec->initOutputFormat(meta);
380
381 return codec;
382}
383
Andreas Huberda050cf22009-09-02 14:01:43 -0700384void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
385 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700386 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700387 def.nPortIndex = portIndex;
388
389 status_t err = mOMX->get_parameter(
390 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
391 CHECK_EQ(err, OK);
392
393 if (def.nBufferSize < size) {
394 def.nBufferSize = size;
395
396 }
397
398 err = mOMX->set_parameter(
399 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
400 CHECK_EQ(err, OK);
401}
402
Andreas Huberbe06d262009-08-14 14:37:10 -0700403status_t OMXCodec::setVideoPortFormatType(
404 OMX_U32 portIndex,
405 OMX_VIDEO_CODINGTYPE compressionFormat,
406 OMX_COLOR_FORMATTYPE colorFormat) {
407 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700408 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700409 format.nPortIndex = portIndex;
410 format.nIndex = 0;
411 bool found = false;
412
413 OMX_U32 index = 0;
414 for (;;) {
415 format.nIndex = index;
416 status_t err = mOMX->get_parameter(
417 mNode, OMX_IndexParamVideoPortFormat,
418 &format, sizeof(format));
419
420 if (err != OK) {
421 return err;
422 }
423
424 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700425 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700426
427#if 1
Andreas Hubere0873732009-09-10 09:57:53 -0700428 CODEC_LOGI("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700429 portIndex,
430 index, format.eCompressionFormat, format.eColorFormat);
431#endif
432
433 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
434 if (portIndex == kPortIndexInput
435 && colorFormat == format.eColorFormat) {
436 // eCompressionFormat does not seem right.
437 found = true;
438 break;
439 }
440 if (portIndex == kPortIndexOutput
441 && compressionFormat == format.eCompressionFormat) {
442 // eColorFormat does not seem right.
443 found = true;
444 break;
445 }
446 }
447
448 if (format.eCompressionFormat == compressionFormat
449 && format.eColorFormat == colorFormat) {
450 found = true;
451 break;
452 }
453
454 ++index;
455 }
456
457 if (!found) {
458 return UNKNOWN_ERROR;
459 }
460
Andreas Hubere0873732009-09-10 09:57:53 -0700461 CODEC_LOGI("found a match.");
Andreas Huberbe06d262009-08-14 14:37:10 -0700462 status_t err = mOMX->set_parameter(
463 mNode, OMX_IndexParamVideoPortFormat,
464 &format, sizeof(format));
465
466 return err;
467}
468
469void OMXCodec::setVideoInputFormat(
470 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Hubere0873732009-09-10 09:57:53 -0700471 CODEC_LOGI("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700472
473 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
474 if (!strcasecmp("video/avc", mime)) {
475 compressionFormat = OMX_VIDEO_CodingAVC;
476 } else if (!strcasecmp("video/mp4v-es", mime)) {
477 compressionFormat = OMX_VIDEO_CodingMPEG4;
478 } else if (!strcasecmp("video/3gpp", mime)) {
479 compressionFormat = OMX_VIDEO_CodingH263;
480 } else {
481 LOGE("Not a supported video mime type: %s", mime);
482 CHECK(!"Should not be here. Not a supported video mime type.");
483 }
484
485 OMX_COLOR_FORMATTYPE colorFormat =
486 0 ? OMX_COLOR_FormatYCbYCr : OMX_COLOR_FormatCbYCrY;
487
488 if (!strncmp("OMX.qcom.video.encoder.", mComponentName, 23)) {
489 colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
490 }
491
492 setVideoPortFormatType(
493 kPortIndexInput, OMX_VIDEO_CodingUnused,
494 colorFormat);
495
496 setVideoPortFormatType(
497 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused);
498
499 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700500 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700501 def.nPortIndex = kPortIndexOutput;
502
Andreas Huber4c483422009-09-02 16:05:36 -0700503 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
504
Andreas Huberbe06d262009-08-14 14:37:10 -0700505 status_t err = mOMX->get_parameter(
506 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
507
508 CHECK_EQ(err, OK);
509 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
510
511 video_def->nFrameWidth = width;
512 video_def->nFrameHeight = height;
513
514 video_def->eCompressionFormat = compressionFormat;
515 video_def->eColorFormat = OMX_COLOR_FormatUnused;
516
517 err = mOMX->set_parameter(
518 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
519 CHECK_EQ(err, OK);
520
521 ////////////////////////////////////////////////////////////////////////////
522
Andreas Huber4c483422009-09-02 16:05:36 -0700523 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700524 def.nPortIndex = kPortIndexInput;
525
526 err = mOMX->get_parameter(
527 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
528 CHECK_EQ(err, OK);
529
530 def.nBufferSize = (width * height * 2); // (width * height * 3) / 2;
Andreas Hubere0873732009-09-10 09:57:53 -0700531 CODEC_LOGI("Setting nBufferSize = %ld", def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700532
533 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
534
535 video_def->nFrameWidth = width;
536 video_def->nFrameHeight = height;
537 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
538 video_def->eColorFormat = colorFormat;
539
540 err = mOMX->set_parameter(
541 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
542 CHECK_EQ(err, OK);
543}
544
545void OMXCodec::setVideoOutputFormat(
546 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Hubere0873732009-09-10 09:57:53 -0700547 CODEC_LOGI("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700548
Andreas Huberbe06d262009-08-14 14:37:10 -0700549 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
550 if (!strcasecmp("video/avc", mime)) {
551 compressionFormat = OMX_VIDEO_CodingAVC;
552 } else if (!strcasecmp("video/mp4v-es", mime)) {
553 compressionFormat = OMX_VIDEO_CodingMPEG4;
554 } else if (!strcasecmp("video/3gpp", mime)) {
555 compressionFormat = OMX_VIDEO_CodingH263;
556 } else {
557 LOGE("Not a supported video mime type: %s", mime);
558 CHECK(!"Should not be here. Not a supported video mime type.");
559 }
560
561 setVideoPortFormatType(
562 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
563
564#if 1
565 {
566 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700567 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700568 format.nPortIndex = kPortIndexOutput;
569 format.nIndex = 0;
570
571 status_t err = mOMX->get_parameter(
572 mNode, OMX_IndexParamVideoPortFormat,
573 &format, sizeof(format));
574 CHECK_EQ(err, OK);
575 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
576
577 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
578
579 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
580 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
581 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
582 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
583
584 err = mOMX->set_parameter(
585 mNode, OMX_IndexParamVideoPortFormat,
586 &format, sizeof(format));
587 CHECK_EQ(err, OK);
588 }
589#endif
590
591 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700592 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700593 def.nPortIndex = kPortIndexInput;
594
Andreas Huber4c483422009-09-02 16:05:36 -0700595 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
596
Andreas Huberbe06d262009-08-14 14:37:10 -0700597 status_t err = mOMX->get_parameter(
598 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
599
600 CHECK_EQ(err, OK);
601
602#if 1
603 // XXX Need a (much) better heuristic to compute input buffer sizes.
604 const size_t X = 64 * 1024;
605 if (def.nBufferSize < X) {
606 def.nBufferSize = X;
607 }
608#endif
609
610 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
611
612 video_def->nFrameWidth = width;
613 video_def->nFrameHeight = height;
614
615 video_def->eColorFormat = OMX_COLOR_FormatUnused;
616
617 err = mOMX->set_parameter(
618 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
619 CHECK_EQ(err, OK);
620
621 ////////////////////////////////////////////////////////////////////////////
622
Andreas Huber4c483422009-09-02 16:05:36 -0700623 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700624 def.nPortIndex = kPortIndexOutput;
625
626 err = mOMX->get_parameter(
627 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
628 CHECK_EQ(err, OK);
629 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
630
631#if 0
632 def.nBufferSize =
633 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
634#endif
635
636 video_def->nFrameWidth = width;
637 video_def->nFrameHeight = height;
638
639 err = mOMX->set_parameter(
640 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
641 CHECK_EQ(err, OK);
642}
643
644
645OMXCodec::OMXCodec(
646 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -0700647 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -0700648 const char *mime,
649 const char *componentName,
650 const sp<MediaSource> &source)
651 : mOMX(omx),
652 mNode(node),
653 mQuirks(quirks),
654 mIsEncoder(isEncoder),
655 mMIME(strdup(mime)),
656 mComponentName(strdup(componentName)),
657 mSource(source),
658 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -0700659 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -0700660 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -0700661 mSignalledEOS(false),
662 mNoMoreOutputData(false),
663 mSeekTimeUs(-1) {
664 mPortStatus[kPortIndexInput] = ENABLED;
665 mPortStatus[kPortIndexOutput] = ENABLED;
666
667 mObserver = new OMXCodecObserver(this);
668 mOMX->observe_node(mNode, mObserver);
Andreas Huber4c483422009-09-02 16:05:36 -0700669
670 setComponentRole();
671}
672
673void OMXCodec::setComponentRole() {
674 struct MimeToRole {
675 const char *mime;
676 const char *decoderRole;
677 const char *encoderRole;
678 };
679
680 static const MimeToRole kMimeToRole[] = {
681 { "audio/mpeg", "audio_decoder.mp3", "audio_encoder.mp3" },
682 { "audio/3gpp", "audio_decoder.amrnb", "audio_encoder.amrnb" },
Andreas Huberee606e62009-09-08 10:19:21 -0700683 { "audio/amr-wb", "audio_decoder.amrwb", "audio_encoder.amrwb" },
Andreas Huber4c483422009-09-02 16:05:36 -0700684 { "audio/mp4a-latm", "audio_decoder.aac", "audio_encoder.aac" },
685 { "video/avc", "video_decoder.avc", "video_encoder.avc" },
686 { "video/mp4v-es", "video_decoder.mpeg4", "video_encoder.mpeg4" },
687 { "video/3gpp", "video_decoder.h263", "video_encoder.h263" },
688 };
689
690 static const size_t kNumMimeToRole =
691 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
692
693 size_t i;
694 for (i = 0; i < kNumMimeToRole; ++i) {
695 if (!strcasecmp(mMIME, kMimeToRole[i].mime)) {
696 break;
697 }
698 }
699
700 if (i == kNumMimeToRole) {
701 return;
702 }
703
704 const char *role =
705 mIsEncoder ? kMimeToRole[i].encoderRole
706 : kMimeToRole[i].decoderRole;
707
708 if (role != NULL) {
709 CODEC_LOGV("Setting component role '%s'.", role);
710
711 OMX_PARAM_COMPONENTROLETYPE roleParams;
712 InitOMXParams(&roleParams);
713
714 strncpy((char *)roleParams.cRole,
715 role, OMX_MAX_STRINGNAME_SIZE - 1);
716
717 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
718
719 status_t err = mOMX->set_parameter(
720 mNode, OMX_IndexParamStandardComponentRole,
721 &roleParams, sizeof(roleParams));
722
723 if (err != OK) {
724 LOGW("Failed to set standard component role '%s'.", role);
725 }
726 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700727}
728
729OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700730 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -0700731
732 status_t err = mOMX->observe_node(mNode, NULL);
733 CHECK_EQ(err, OK);
734
735 err = mOMX->free_node(mNode);
736 CHECK_EQ(err, OK);
737
738 mNode = NULL;
739 setState(DEAD);
740
741 clearCodecSpecificData();
742
743 free(mComponentName);
744 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700745
Andreas Huberbe06d262009-08-14 14:37:10 -0700746 free(mMIME);
747 mMIME = NULL;
748}
749
750status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -0700751 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -0700752
753 CHECK_EQ(mState, LOADED);
754
755 status_t err;
756 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
757 err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
758 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700759 setState(LOADED_TO_IDLE);
760 }
761
762 err = allocateBuffers();
763 CHECK_EQ(err, OK);
764
765 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
766 err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
767 CHECK_EQ(err, OK);
768
769 setState(LOADED_TO_IDLE);
770 }
771
772 while (mState != EXECUTING && mState != ERROR) {
773 mAsyncCompletion.wait(mLock);
774 }
775
776 return mState == ERROR ? UNKNOWN_ERROR : OK;
777}
778
779// static
780bool OMXCodec::isIntermediateState(State state) {
781 return state == LOADED_TO_IDLE
782 || state == IDLE_TO_EXECUTING
783 || state == EXECUTING_TO_IDLE
784 || state == IDLE_TO_LOADED
785 || state == RECONFIGURING;
786}
787
788status_t OMXCodec::allocateBuffers() {
789 status_t err = allocateBuffersOnPort(kPortIndexInput);
790
791 if (err != OK) {
792 return err;
793 }
794
795 return allocateBuffersOnPort(kPortIndexOutput);
796}
797
798status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
799 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700800 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700801 def.nPortIndex = portIndex;
802
803 status_t err = mOMX->get_parameter(
804 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
805
806 if (err != OK) {
807 return err;
808 }
809
Andreas Huber5c0a9132009-08-20 11:16:40 -0700810 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
811 mDealer[portIndex] = new MemoryDealer(totalSize);
812
Andreas Huberbe06d262009-08-14 14:37:10 -0700813 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -0700814 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700815 CHECK(mem.get() != NULL);
816
817 IOMX::buffer_id buffer;
818 if (portIndex == kPortIndexInput
819 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
820 err = mOMX->allocate_buffer_with_backup(
821 mNode, portIndex, mem, &buffer);
Andreas Huber446f44f2009-08-25 17:23:44 -0700822 } else if (portIndex == kPortIndexOutput
823 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
824 err = mOMX->allocate_buffer(
825 mNode, portIndex, def.nBufferSize, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700826 } else {
827 err = mOMX->use_buffer(mNode, portIndex, mem, &buffer);
828 }
829
830 if (err != OK) {
831 LOGE("allocate_buffer_with_backup failed");
832 return err;
833 }
834
835 BufferInfo info;
836 info.mBuffer = buffer;
837 info.mOwnedByComponent = false;
838 info.mMem = mem;
839 info.mMediaBuffer = NULL;
840
841 if (portIndex == kPortIndexOutput) {
842 info.mMediaBuffer = new MediaBuffer(mem->pointer(), mem->size());
843 info.mMediaBuffer->setObserver(this);
844 }
845
846 mPortBuffers[portIndex].push(info);
847
Andreas Huber4c483422009-09-02 16:05:36 -0700848 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -0700849 portIndex == kPortIndexInput ? "input" : "output");
850 }
851
852 dumpPortStatus(portIndex);
853
854 return OK;
855}
856
857void OMXCodec::on_message(const omx_message &msg) {
858 Mutex::Autolock autoLock(mLock);
859
860 switch (msg.type) {
861 case omx_message::EVENT:
862 {
863 onEvent(
864 msg.u.event_data.event, msg.u.event_data.data1,
865 msg.u.event_data.data2);
866
867 break;
868 }
869
870 case omx_message::EMPTY_BUFFER_DONE:
871 {
872 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
873
Andreas Huber4c483422009-09-02 16:05:36 -0700874 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700875
876 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
877 size_t i = 0;
878 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
879 ++i;
880 }
881
882 CHECK(i < buffers->size());
883 if (!(*buffers)[i].mOwnedByComponent) {
884 LOGW("We already own input buffer %p, yet received "
885 "an EMPTY_BUFFER_DONE.", buffer);
886 }
887
888 buffers->editItemAt(i).mOwnedByComponent = false;
889
890 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -0700891 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700892
893 status_t err =
894 mOMX->free_buffer(mNode, kPortIndexInput, buffer);
895 CHECK_EQ(err, OK);
896
897 buffers->removeAt(i);
898 } else if (mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
899 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
900 drainInputBuffer(&buffers->editItemAt(i));
901 }
902
903 break;
904 }
905
906 case omx_message::FILL_BUFFER_DONE:
907 {
908 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
909 OMX_U32 flags = msg.u.extended_buffer_data.flags;
910
Andreas Huber4c483422009-09-02 16:05:36 -0700911 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx)",
Andreas Huberbe06d262009-08-14 14:37:10 -0700912 buffer,
913 msg.u.extended_buffer_data.range_length,
914 flags);
915
Andreas Huber4c483422009-09-02 16:05:36 -0700916 CODEC_LOGV("FILL_BUFFER_DONE(timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -0700917 msg.u.extended_buffer_data.timestamp,
918 msg.u.extended_buffer_data.timestamp / 1E6);
919
920 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
921 size_t i = 0;
922 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
923 ++i;
924 }
925
926 CHECK(i < buffers->size());
927 BufferInfo *info = &buffers->editItemAt(i);
928
929 if (!info->mOwnedByComponent) {
930 LOGW("We already own output buffer %p, yet received "
931 "a FILL_BUFFER_DONE.", buffer);
932 }
933
934 info->mOwnedByComponent = false;
935
936 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -0700937 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700938
939 status_t err =
940 mOMX->free_buffer(mNode, kPortIndexOutput, buffer);
941 CHECK_EQ(err, OK);
942
943 buffers->removeAt(i);
Andreas Huberd7795892009-08-26 10:33:47 -0700944 } else if (mPortStatus[kPortIndexOutput] == ENABLED
945 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -0700946 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -0700947 mNoMoreOutputData = true;
948 mBufferFilled.signal();
949 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
950 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700951
Andreas Huberbe06d262009-08-14 14:37:10 -0700952 MediaBuffer *buffer = info->mMediaBuffer;
953
954 buffer->set_range(
955 msg.u.extended_buffer_data.range_offset,
956 msg.u.extended_buffer_data.range_length);
957
958 buffer->meta_data()->clear();
959
960 buffer->meta_data()->setInt32(
961 kKeyTimeUnits,
962 (msg.u.extended_buffer_data.timestamp + 500) / 1000);
963
964 buffer->meta_data()->setInt32(
965 kKeyTimeScale, 1000);
966
967 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
968 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
969 }
970
971 buffer->meta_data()->setPointer(
972 kKeyPlatformPrivate,
973 msg.u.extended_buffer_data.platform_private);
974
975 buffer->meta_data()->setPointer(
976 kKeyBufferID,
977 msg.u.extended_buffer_data.buffer);
978
979 mFilledBuffers.push_back(i);
980 mBufferFilled.signal();
981 }
982
983 break;
984 }
985
986 default:
987 {
988 CHECK(!"should not be here.");
989 break;
990 }
991 }
992}
993
994void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
995 switch (event) {
996 case OMX_EventCmdComplete:
997 {
998 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
999 break;
1000 }
1001
1002 case OMX_EventError:
1003 {
1004 LOGE("ERROR(%ld, %ld)", data1, data2);
1005
1006 setState(ERROR);
1007 break;
1008 }
1009
1010 case OMX_EventPortSettingsChanged:
1011 {
1012 onPortSettingsChanged(data1);
1013 break;
1014 }
1015
1016 case OMX_EventBufferFlag:
1017 {
Andreas Huber4c483422009-09-02 16:05:36 -07001018 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001019
1020 if (data1 == kPortIndexOutput) {
1021 mNoMoreOutputData = true;
1022 }
1023 break;
1024 }
1025
1026 default:
1027 {
Andreas Huber4c483422009-09-02 16:05:36 -07001028 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001029 break;
1030 }
1031 }
1032}
1033
1034void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1035 switch (cmd) {
1036 case OMX_CommandStateSet:
1037 {
1038 onStateChange((OMX_STATETYPE)data);
1039 break;
1040 }
1041
1042 case OMX_CommandPortDisable:
1043 {
1044 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001045 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001046
1047 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1048 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1049 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1050
1051 mPortStatus[portIndex] = DISABLED;
1052
1053 if (mState == RECONFIGURING) {
1054 CHECK_EQ(portIndex, kPortIndexOutput);
1055
1056 enablePortAsync(portIndex);
1057
1058 status_t err = allocateBuffersOnPort(portIndex);
1059 CHECK_EQ(err, OK);
1060 }
1061 break;
1062 }
1063
1064 case OMX_CommandPortEnable:
1065 {
1066 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001067 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001068
1069 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1070 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1071
1072 mPortStatus[portIndex] = ENABLED;
1073
1074 if (mState == RECONFIGURING) {
1075 CHECK_EQ(portIndex, kPortIndexOutput);
1076
1077 setState(EXECUTING);
1078
1079 fillOutputBuffers();
1080 }
1081 break;
1082 }
1083
1084 case OMX_CommandFlush:
1085 {
1086 OMX_U32 portIndex = data;
1087
Andreas Huber4c483422009-09-02 16:05:36 -07001088 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001089
1090 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1091 mPortStatus[portIndex] = ENABLED;
1092
1093 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1094 mPortBuffers[portIndex].size());
1095
1096 if (mState == RECONFIGURING) {
1097 CHECK_EQ(portIndex, kPortIndexOutput);
1098
1099 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001100 } else if (mState == EXECUTING_TO_IDLE) {
1101 if (mPortStatus[kPortIndexInput] == ENABLED
1102 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001103 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001104 "transition from EXECUTING to IDLE.");
1105
1106 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1107 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1108
1109 status_t err =
1110 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1111 CHECK_EQ(err, OK);
1112 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001113 } else {
1114 // We're flushing both ports in preparation for seeking.
1115
1116 if (mPortStatus[kPortIndexInput] == ENABLED
1117 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001118 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001119 " seek-time.");
1120
1121 drainInputBuffers();
1122 fillOutputBuffers();
1123 }
1124 }
1125
1126 break;
1127 }
1128
1129 default:
1130 {
Andreas Huber4c483422009-09-02 16:05:36 -07001131 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001132 break;
1133 }
1134 }
1135}
1136
1137void OMXCodec::onStateChange(OMX_STATETYPE newState) {
1138 switch (newState) {
1139 case OMX_StateIdle:
1140 {
Andreas Huber4c483422009-09-02 16:05:36 -07001141 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001142 if (mState == LOADED_TO_IDLE) {
1143 status_t err = mOMX->send_command(
1144 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1145
1146 CHECK_EQ(err, OK);
1147
1148 setState(IDLE_TO_EXECUTING);
1149 } else {
1150 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1151
1152 CHECK_EQ(
1153 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1154 mPortBuffers[kPortIndexInput].size());
1155
1156 CHECK_EQ(
1157 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1158 mPortBuffers[kPortIndexOutput].size());
1159
1160 status_t err = mOMX->send_command(
1161 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1162
1163 CHECK_EQ(err, OK);
1164
1165 err = freeBuffersOnPort(kPortIndexInput);
1166 CHECK_EQ(err, OK);
1167
1168 err = freeBuffersOnPort(kPortIndexOutput);
1169 CHECK_EQ(err, OK);
1170
1171 mPortStatus[kPortIndexInput] = ENABLED;
1172 mPortStatus[kPortIndexOutput] = ENABLED;
1173
1174 setState(IDLE_TO_LOADED);
1175 }
1176 break;
1177 }
1178
1179 case OMX_StateExecuting:
1180 {
1181 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1182
Andreas Huber4c483422009-09-02 16:05:36 -07001183 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001184
1185 setState(EXECUTING);
1186
Andreas Huber42978e52009-08-27 10:08:39 -07001187 // Buffers will be submitted to the component in the first
1188 // call to OMXCodec::read as mInitialBufferSubmit is true at
1189 // this point. This ensures that this on_message call returns,
1190 // releases the lock and ::init can notice the state change and
1191 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001192 break;
1193 }
1194
1195 case OMX_StateLoaded:
1196 {
1197 CHECK_EQ(mState, IDLE_TO_LOADED);
1198
Andreas Huber4c483422009-09-02 16:05:36 -07001199 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001200
1201 setState(LOADED);
1202 break;
1203 }
1204
1205 default:
1206 {
1207 CHECK(!"should not be here.");
1208 break;
1209 }
1210 }
1211}
1212
1213// static
1214size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1215 size_t n = 0;
1216 for (size_t i = 0; i < buffers.size(); ++i) {
1217 if (!buffers[i].mOwnedByComponent) {
1218 ++n;
1219 }
1220 }
1221
1222 return n;
1223}
1224
1225status_t OMXCodec::freeBuffersOnPort(
1226 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1227 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1228
1229 status_t stickyErr = OK;
1230
1231 for (size_t i = buffers->size(); i-- > 0;) {
1232 BufferInfo *info = &buffers->editItemAt(i);
1233
1234 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1235 continue;
1236 }
1237
1238 CHECK_EQ(info->mOwnedByComponent, false);
1239
1240 status_t err =
1241 mOMX->free_buffer(mNode, portIndex, info->mBuffer);
1242
1243 if (err != OK) {
1244 stickyErr = err;
1245 }
1246
1247 if (info->mMediaBuffer != NULL) {
1248 info->mMediaBuffer->setObserver(NULL);
1249
1250 // Make sure nobody but us owns this buffer at this point.
1251 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1252
1253 info->mMediaBuffer->release();
1254 }
1255
1256 buffers->removeAt(i);
1257 }
1258
1259 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1260
1261 return stickyErr;
1262}
1263
1264void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001265 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001266
1267 CHECK_EQ(mState, EXECUTING);
1268 CHECK_EQ(portIndex, kPortIndexOutput);
1269 setState(RECONFIGURING);
1270
1271 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001272 if (!flushPortAsync(portIndex)) {
1273 onCmdComplete(OMX_CommandFlush, portIndex);
1274 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001275 } else {
1276 disablePortAsync(portIndex);
1277 }
1278}
1279
Andreas Huber404cc412009-08-25 14:26:05 -07001280bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001281 CHECK(mState == EXECUTING || mState == RECONFIGURING
1282 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001283
Andreas Huber4c483422009-09-02 16:05:36 -07001284 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001285 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1286 mPortBuffers[portIndex].size());
1287
Andreas Huberbe06d262009-08-14 14:37:10 -07001288 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1289 mPortStatus[portIndex] = SHUTTING_DOWN;
1290
Andreas Huber404cc412009-08-25 14:26:05 -07001291 if ((mQuirks & kRequiresFlushCompleteEmulation)
1292 && countBuffersWeOwn(mPortBuffers[portIndex])
1293 == mPortBuffers[portIndex].size()) {
1294 // No flush is necessary and this component fails to send a
1295 // flush-complete event in this case.
1296
1297 return false;
1298 }
1299
Andreas Huberbe06d262009-08-14 14:37:10 -07001300 status_t err =
1301 mOMX->send_command(mNode, OMX_CommandFlush, portIndex);
1302 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001303
1304 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001305}
1306
1307void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1308 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1309
1310 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1311 mPortStatus[portIndex] = DISABLING;
1312
1313 status_t err =
1314 mOMX->send_command(mNode, OMX_CommandPortDisable, portIndex);
1315 CHECK_EQ(err, OK);
1316
1317 freeBuffersOnPort(portIndex, true);
1318}
1319
1320void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1321 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1322
1323 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1324 mPortStatus[portIndex] = ENABLING;
1325
1326 status_t err =
1327 mOMX->send_command(mNode, OMX_CommandPortEnable, portIndex);
1328 CHECK_EQ(err, OK);
1329}
1330
1331void OMXCodec::fillOutputBuffers() {
1332 CHECK_EQ(mState, EXECUTING);
1333
1334 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1335 for (size_t i = 0; i < buffers->size(); ++i) {
1336 fillOutputBuffer(&buffers->editItemAt(i));
1337 }
1338}
1339
1340void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001341 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001342
1343 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1344 for (size_t i = 0; i < buffers->size(); ++i) {
1345 drainInputBuffer(&buffers->editItemAt(i));
1346 }
1347}
1348
1349void OMXCodec::drainInputBuffer(BufferInfo *info) {
1350 CHECK_EQ(info->mOwnedByComponent, false);
1351
1352 if (mSignalledEOS) {
1353 return;
1354 }
1355
1356 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1357 const CodecSpecificData *specific =
1358 mCodecSpecificData[mCodecSpecificDataIndex];
1359
1360 size_t size = specific->mSize;
1361
Andreas Huber4f5e6022009-08-19 09:29:34 -07001362 if (!strcasecmp("video/avc", mMIME)
1363 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001364 static const uint8_t kNALStartCode[4] =
1365 { 0x00, 0x00, 0x00, 0x01 };
1366
1367 CHECK(info->mMem->size() >= specific->mSize + 4);
1368
1369 size += 4;
1370
1371 memcpy(info->mMem->pointer(), kNALStartCode, 4);
1372 memcpy((uint8_t *)info->mMem->pointer() + 4,
1373 specific->mData, specific->mSize);
1374 } else {
1375 CHECK(info->mMem->size() >= specific->mSize);
1376 memcpy(info->mMem->pointer(), specific->mData, specific->mSize);
1377 }
1378
1379 mOMX->empty_buffer(
1380 mNode, info->mBuffer, 0, size,
1381 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1382 0);
1383
1384 info->mOwnedByComponent = true;
1385
1386 ++mCodecSpecificDataIndex;
1387 return;
1388 }
1389
1390 MediaBuffer *srcBuffer;
1391 status_t err;
1392 if (mSeekTimeUs >= 0) {
1393 MediaSource::ReadOptions options;
1394 options.setSeekTo(mSeekTimeUs);
1395 mSeekTimeUs = -1;
1396
1397 err = mSource->read(&srcBuffer, &options);
1398 } else {
1399 err = mSource->read(&srcBuffer);
1400 }
1401
1402 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
1403 OMX_TICKS timestamp = 0;
1404 size_t srcLength = 0;
1405
1406 if (err != OK) {
Andreas Huber4c483422009-09-02 16:05:36 -07001407 CODEC_LOGV("signalling end of input stream.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001408 flags |= OMX_BUFFERFLAG_EOS;
1409
1410 mSignalledEOS = true;
1411 } else {
1412 srcLength = srcBuffer->range_length();
1413
1414 if (info->mMem->size() < srcLength) {
1415 LOGE("info->mMem->size() = %d, srcLength = %d",
1416 info->mMem->size(), srcLength);
1417 }
1418 CHECK(info->mMem->size() >= srcLength);
1419 memcpy(info->mMem->pointer(),
1420 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
1421 srcLength);
1422
1423 int32_t units, scale;
1424 if (srcBuffer->meta_data()->findInt32(kKeyTimeUnits, &units)
1425 && srcBuffer->meta_data()->findInt32(kKeyTimeScale, &scale)) {
1426 timestamp = ((OMX_TICKS)units * 1000000) / scale;
1427
Andreas Huber4c483422009-09-02 16:05:36 -07001428 CODEC_LOGV("Calling empty_buffer on buffer %p (length %d)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001429 info->mBuffer, srcLength);
Andreas Huber4c483422009-09-02 16:05:36 -07001430 CODEC_LOGV("Calling empty_buffer with timestamp %lld us (%.2f secs)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001431 timestamp, timestamp / 1E6);
1432 }
1433 }
1434
1435 mOMX->empty_buffer(
1436 mNode, info->mBuffer, 0, srcLength,
1437 flags, timestamp);
1438
1439 info->mOwnedByComponent = true;
1440
1441 if (srcBuffer != NULL) {
1442 srcBuffer->release();
1443 srcBuffer = NULL;
1444 }
1445}
1446
1447void OMXCodec::fillOutputBuffer(BufferInfo *info) {
1448 CHECK_EQ(info->mOwnedByComponent, false);
1449
Andreas Huber404cc412009-08-25 14:26:05 -07001450 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07001451 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07001452 "calling fillOutputBuffer");
1453 return;
1454 }
1455
Andreas Huber4c483422009-09-02 16:05:36 -07001456 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001457 mOMX->fill_buffer(mNode, info->mBuffer);
1458
1459 info->mOwnedByComponent = true;
1460}
1461
1462void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
1463 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1464 for (size_t i = 0; i < buffers->size(); ++i) {
1465 if ((*buffers)[i].mBuffer == buffer) {
1466 drainInputBuffer(&buffers->editItemAt(i));
1467 return;
1468 }
1469 }
1470
1471 CHECK(!"should not be here.");
1472}
1473
1474void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
1475 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1476 for (size_t i = 0; i < buffers->size(); ++i) {
1477 if ((*buffers)[i].mBuffer == buffer) {
1478 fillOutputBuffer(&buffers->editItemAt(i));
1479 return;
1480 }
1481 }
1482
1483 CHECK(!"should not be here.");
1484}
1485
1486void OMXCodec::setState(State newState) {
1487 mState = newState;
1488 mAsyncCompletion.signal();
1489
1490 // This may cause some spurious wakeups but is necessary to
1491 // unblock the reader if we enter ERROR state.
1492 mBufferFilled.signal();
1493}
1494
Andreas Huberda050cf22009-09-02 14:01:43 -07001495void OMXCodec::setRawAudioFormat(
1496 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
1497 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07001498 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07001499 pcmParams.nPortIndex = portIndex;
1500
1501 status_t err = mOMX->get_parameter(
1502 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1503
1504 CHECK_EQ(err, OK);
1505
1506 pcmParams.nChannels = numChannels;
1507 pcmParams.eNumData = OMX_NumericalDataSigned;
1508 pcmParams.bInterleaved = OMX_TRUE;
1509 pcmParams.nBitPerSample = 16;
1510 pcmParams.nSamplingRate = sampleRate;
1511 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
1512
1513 if (numChannels == 1) {
1514 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
1515 } else {
1516 CHECK_EQ(numChannels, 2);
1517
1518 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
1519 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
1520 }
1521
1522 err = mOMX->set_parameter(
1523 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1524
1525 CHECK_EQ(err, OK);
1526}
1527
Andreas Huberbe06d262009-08-14 14:37:10 -07001528void OMXCodec::setAMRFormat() {
1529 if (!mIsEncoder) {
1530 OMX_AUDIO_PARAM_AMRTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001531 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001532 def.nPortIndex = kPortIndexInput;
1533
1534 status_t err =
1535 mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1536
1537 CHECK_EQ(err, OK);
1538
1539 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1540 def.eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
1541
1542 err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1543 CHECK_EQ(err, OK);
1544 }
1545
1546 ////////////////////////
1547
1548 if (mIsEncoder) {
1549 sp<MetaData> format = mSource->getFormat();
1550 int32_t sampleRate;
1551 int32_t numChannels;
1552 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1553 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1554
Andreas Huberda050cf22009-09-02 14:01:43 -07001555 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
Andreas Huberbe06d262009-08-14 14:37:10 -07001556 }
1557}
1558
Andreas Huberee606e62009-09-08 10:19:21 -07001559void OMXCodec::setAMRWBFormat() {
1560 if (!mIsEncoder) {
1561 OMX_AUDIO_PARAM_AMRTYPE def;
1562 InitOMXParams(&def);
1563 def.nPortIndex = kPortIndexInput;
1564
1565 status_t err =
1566 mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1567
1568 CHECK_EQ(err, OK);
1569
1570 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1571 def.eAMRBandMode = OMX_AUDIO_AMRBandModeWB0;
1572
1573 err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1574 CHECK_EQ(err, OK);
1575 }
1576
1577 ////////////////////////
1578
1579 if (mIsEncoder) {
1580 sp<MetaData> format = mSource->getFormat();
1581 int32_t sampleRate;
1582 int32_t numChannels;
1583 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1584 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1585
1586 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1587 }
1588}
1589
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07001590void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) {
Andreas Huberda050cf22009-09-02 14:01:43 -07001591 if (mIsEncoder) {
1592 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1593 } else {
1594 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07001595 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07001596 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07001597
Andreas Huberda050cf22009-09-02 14:01:43 -07001598 status_t err = mOMX->get_parameter(
1599 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1600 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001601
Andreas Huberda050cf22009-09-02 14:01:43 -07001602 profile.nChannels = numChannels;
1603 profile.nSampleRate = sampleRate;
1604 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07001605
Andreas Huberda050cf22009-09-02 14:01:43 -07001606 err = mOMX->set_parameter(
1607 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1608 CHECK_EQ(err, OK);
1609 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001610}
1611
1612void OMXCodec::setImageOutputFormat(
1613 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07001614 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001615
1616#if 0
1617 OMX_INDEXTYPE index;
1618 status_t err = mOMX->get_extension_index(
1619 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
1620 CHECK_EQ(err, OK);
1621
1622 err = mOMX->set_config(mNode, index, &format, sizeof(format));
1623 CHECK_EQ(err, OK);
1624#endif
1625
1626 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001627 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001628 def.nPortIndex = kPortIndexOutput;
1629
1630 status_t err = mOMX->get_parameter(
1631 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1632 CHECK_EQ(err, OK);
1633
1634 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1635
1636 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001637
Andreas Huberbe06d262009-08-14 14:37:10 -07001638 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
1639 imageDef->eColorFormat = format;
1640 imageDef->nFrameWidth = width;
1641 imageDef->nFrameHeight = height;
1642
1643 switch (format) {
1644 case OMX_COLOR_FormatYUV420PackedPlanar:
1645 case OMX_COLOR_FormatYUV411Planar:
1646 {
1647 def.nBufferSize = (width * height * 3) / 2;
1648 break;
1649 }
1650
1651 case OMX_COLOR_FormatCbYCrY:
1652 {
1653 def.nBufferSize = width * height * 2;
1654 break;
1655 }
1656
1657 case OMX_COLOR_Format32bitARGB8888:
1658 {
1659 def.nBufferSize = width * height * 4;
1660 break;
1661 }
1662
Andreas Huber201511c2009-09-08 14:01:44 -07001663 case OMX_COLOR_Format16bitARGB4444:
1664 case OMX_COLOR_Format16bitARGB1555:
1665 case OMX_COLOR_Format16bitRGB565:
1666 case OMX_COLOR_Format16bitBGR565:
1667 {
1668 def.nBufferSize = width * height * 2;
1669 break;
1670 }
1671
Andreas Huberbe06d262009-08-14 14:37:10 -07001672 default:
1673 CHECK(!"Should not be here. Unknown color format.");
1674 break;
1675 }
1676
Andreas Huber5c0a9132009-08-20 11:16:40 -07001677 def.nBufferCountActual = def.nBufferCountMin;
1678
Andreas Huberbe06d262009-08-14 14:37:10 -07001679 err = mOMX->set_parameter(
1680 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1681 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07001682}
Andreas Huberbe06d262009-08-14 14:37:10 -07001683
Andreas Huber5c0a9132009-08-20 11:16:40 -07001684void OMXCodec::setJPEGInputFormat(
1685 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
1686 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001687 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001688 def.nPortIndex = kPortIndexInput;
1689
Andreas Huber5c0a9132009-08-20 11:16:40 -07001690 status_t err = mOMX->get_parameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001691 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1692 CHECK_EQ(err, OK);
1693
Andreas Huber5c0a9132009-08-20 11:16:40 -07001694 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1695 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
1696
Andreas Huberbe06d262009-08-14 14:37:10 -07001697 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
1698 imageDef->nFrameWidth = width;
1699 imageDef->nFrameHeight = height;
1700
Andreas Huber5c0a9132009-08-20 11:16:40 -07001701 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07001702 def.nBufferCountActual = def.nBufferCountMin;
1703
1704 err = mOMX->set_parameter(
1705 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1706 CHECK_EQ(err, OK);
1707}
1708
1709void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
1710 CodecSpecificData *specific =
1711 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
1712
1713 specific->mSize = size;
1714 memcpy(specific->mData, data, size);
1715
1716 mCodecSpecificData.push(specific);
1717}
1718
1719void OMXCodec::clearCodecSpecificData() {
1720 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
1721 free(mCodecSpecificData.editItemAt(i));
1722 }
1723 mCodecSpecificData.clear();
1724 mCodecSpecificDataIndex = 0;
1725}
1726
1727status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07001728 Mutex::Autolock autoLock(mLock);
1729
Andreas Huberbe06d262009-08-14 14:37:10 -07001730 if (mState != LOADED) {
1731 return UNKNOWN_ERROR;
1732 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07001733
Andreas Huberbe06d262009-08-14 14:37:10 -07001734 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07001735 if (mQuirks & kWantsNALFragments) {
1736 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07001737 }
1738 status_t err = mSource->start(params.get());
1739
1740 if (err != OK) {
1741 return err;
1742 }
1743
1744 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07001745 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001746 mSignalledEOS = false;
1747 mNoMoreOutputData = false;
1748 mSeekTimeUs = -1;
1749 mFilledBuffers.clear();
1750
1751 return init();
1752}
1753
1754status_t OMXCodec::stop() {
Andreas Huber4c483422009-09-02 16:05:36 -07001755 CODEC_LOGV("stop");
Andreas Huberbe06d262009-08-14 14:37:10 -07001756
1757 Mutex::Autolock autoLock(mLock);
1758
1759 while (isIntermediateState(mState)) {
1760 mAsyncCompletion.wait(mLock);
1761 }
1762
1763 switch (mState) {
1764 case LOADED:
1765 case ERROR:
1766 break;
1767
1768 case EXECUTING:
1769 {
1770 setState(EXECUTING_TO_IDLE);
1771
Andreas Huber127fcdc2009-08-26 16:27:02 -07001772 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07001773 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001774 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07001775
Andreas Huber127fcdc2009-08-26 16:27:02 -07001776 bool emulateInputFlushCompletion =
1777 !flushPortAsync(kPortIndexInput);
1778
1779 bool emulateOutputFlushCompletion =
1780 !flushPortAsync(kPortIndexOutput);
1781
1782 if (emulateInputFlushCompletion) {
1783 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1784 }
1785
1786 if (emulateOutputFlushCompletion) {
1787 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1788 }
1789 } else {
1790 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1791 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1792
1793 status_t err =
1794 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1795 CHECK_EQ(err, OK);
1796 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001797
1798 while (mState != LOADED && mState != ERROR) {
1799 mAsyncCompletion.wait(mLock);
1800 }
1801
1802 break;
1803 }
1804
1805 default:
1806 {
1807 CHECK(!"should not be here.");
1808 break;
1809 }
1810 }
1811
1812 mSource->stop();
1813
1814 return OK;
1815}
1816
1817sp<MetaData> OMXCodec::getFormat() {
1818 return mOutputFormat;
1819}
1820
1821status_t OMXCodec::read(
1822 MediaBuffer **buffer, const ReadOptions *options) {
1823 *buffer = NULL;
1824
1825 Mutex::Autolock autoLock(mLock);
1826
Andreas Huberd06e5b82009-08-28 13:18:14 -07001827 if (mState != EXECUTING && mState != RECONFIGURING) {
1828 return UNKNOWN_ERROR;
1829 }
1830
Andreas Huber42978e52009-08-27 10:08:39 -07001831 if (mInitialBufferSubmit) {
1832 mInitialBufferSubmit = false;
1833
1834 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07001835
Andreas Huberd06e5b82009-08-28 13:18:14 -07001836 if (mState == EXECUTING) {
1837 // Otherwise mState == RECONFIGURING and this code will trigger
1838 // after the output port is reenabled.
1839 fillOutputBuffers();
1840 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001841 }
1842
1843 int64_t seekTimeUs;
1844 if (options && options->getSeekTo(&seekTimeUs)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001845 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07001846
1847 mSignalledEOS = false;
1848 mNoMoreOutputData = false;
1849
1850 CHECK(seekTimeUs >= 0);
1851 mSeekTimeUs = seekTimeUs;
1852
1853 mFilledBuffers.clear();
1854
1855 CHECK_EQ(mState, EXECUTING);
1856
Andreas Huber404cc412009-08-25 14:26:05 -07001857 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
1858 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
1859
1860 if (emulateInputFlushCompletion) {
1861 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1862 }
1863
1864 if (emulateOutputFlushCompletion) {
1865 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1866 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001867 }
1868
1869 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
1870 mBufferFilled.wait(mLock);
1871 }
1872
1873 if (mState == ERROR) {
1874 return UNKNOWN_ERROR;
1875 }
1876
1877 if (mFilledBuffers.empty()) {
1878 return ERROR_END_OF_STREAM;
1879 }
1880
1881 size_t index = *mFilledBuffers.begin();
1882 mFilledBuffers.erase(mFilledBuffers.begin());
1883
1884 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
1885 info->mMediaBuffer->add_ref();
1886 *buffer = info->mMediaBuffer;
1887
1888 return OK;
1889}
1890
1891void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
1892 Mutex::Autolock autoLock(mLock);
1893
1894 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1895 for (size_t i = 0; i < buffers->size(); ++i) {
1896 BufferInfo *info = &buffers->editItemAt(i);
1897
1898 if (info->mMediaBuffer == buffer) {
1899 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
1900 fillOutputBuffer(info);
1901 return;
1902 }
1903 }
1904
1905 CHECK(!"should not be here.");
1906}
1907
1908static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
1909 static const char *kNames[] = {
1910 "OMX_IMAGE_CodingUnused",
1911 "OMX_IMAGE_CodingAutoDetect",
1912 "OMX_IMAGE_CodingJPEG",
1913 "OMX_IMAGE_CodingJPEG2K",
1914 "OMX_IMAGE_CodingEXIF",
1915 "OMX_IMAGE_CodingTIFF",
1916 "OMX_IMAGE_CodingGIF",
1917 "OMX_IMAGE_CodingPNG",
1918 "OMX_IMAGE_CodingLZW",
1919 "OMX_IMAGE_CodingBMP",
1920 };
1921
1922 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
1923
1924 if (type < 0 || (size_t)type >= numNames) {
1925 return "UNKNOWN";
1926 } else {
1927 return kNames[type];
1928 }
1929}
1930
1931static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
1932 static const char *kNames[] = {
1933 "OMX_COLOR_FormatUnused",
1934 "OMX_COLOR_FormatMonochrome",
1935 "OMX_COLOR_Format8bitRGB332",
1936 "OMX_COLOR_Format12bitRGB444",
1937 "OMX_COLOR_Format16bitARGB4444",
1938 "OMX_COLOR_Format16bitARGB1555",
1939 "OMX_COLOR_Format16bitRGB565",
1940 "OMX_COLOR_Format16bitBGR565",
1941 "OMX_COLOR_Format18bitRGB666",
1942 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07001943 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07001944 "OMX_COLOR_Format24bitRGB888",
1945 "OMX_COLOR_Format24bitBGR888",
1946 "OMX_COLOR_Format24bitARGB1887",
1947 "OMX_COLOR_Format25bitARGB1888",
1948 "OMX_COLOR_Format32bitBGRA8888",
1949 "OMX_COLOR_Format32bitARGB8888",
1950 "OMX_COLOR_FormatYUV411Planar",
1951 "OMX_COLOR_FormatYUV411PackedPlanar",
1952 "OMX_COLOR_FormatYUV420Planar",
1953 "OMX_COLOR_FormatYUV420PackedPlanar",
1954 "OMX_COLOR_FormatYUV420SemiPlanar",
1955 "OMX_COLOR_FormatYUV422Planar",
1956 "OMX_COLOR_FormatYUV422PackedPlanar",
1957 "OMX_COLOR_FormatYUV422SemiPlanar",
1958 "OMX_COLOR_FormatYCbYCr",
1959 "OMX_COLOR_FormatYCrYCb",
1960 "OMX_COLOR_FormatCbYCrY",
1961 "OMX_COLOR_FormatCrYCbY",
1962 "OMX_COLOR_FormatYUV444Interleaved",
1963 "OMX_COLOR_FormatRawBayer8bit",
1964 "OMX_COLOR_FormatRawBayer10bit",
1965 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07001966 "OMX_COLOR_FormatL2",
1967 "OMX_COLOR_FormatL4",
1968 "OMX_COLOR_FormatL8",
1969 "OMX_COLOR_FormatL16",
1970 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07001971 "OMX_COLOR_FormatL32",
1972 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
1973 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
1974 "OMX_COLOR_Format18BitBGR666",
1975 "OMX_COLOR_Format24BitARGB6666",
1976 "OMX_COLOR_Format24BitABGR6666",
1977 };
1978
1979 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
1980
1981 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1982
1983 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
1984 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
1985 } else if (type < 0 || (size_t)type >= numNames) {
1986 return "UNKNOWN";
1987 } else {
1988 return kNames[type];
1989 }
1990}
1991
1992static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
1993 static const char *kNames[] = {
1994 "OMX_VIDEO_CodingUnused",
1995 "OMX_VIDEO_CodingAutoDetect",
1996 "OMX_VIDEO_CodingMPEG2",
1997 "OMX_VIDEO_CodingH263",
1998 "OMX_VIDEO_CodingMPEG4",
1999 "OMX_VIDEO_CodingWMV",
2000 "OMX_VIDEO_CodingRV",
2001 "OMX_VIDEO_CodingAVC",
2002 "OMX_VIDEO_CodingMJPEG",
2003 };
2004
2005 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2006
2007 if (type < 0 || (size_t)type >= numNames) {
2008 return "UNKNOWN";
2009 } else {
2010 return kNames[type];
2011 }
2012}
2013
2014static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2015 static const char *kNames[] = {
2016 "OMX_AUDIO_CodingUnused",
2017 "OMX_AUDIO_CodingAutoDetect",
2018 "OMX_AUDIO_CodingPCM",
2019 "OMX_AUDIO_CodingADPCM",
2020 "OMX_AUDIO_CodingAMR",
2021 "OMX_AUDIO_CodingGSMFR",
2022 "OMX_AUDIO_CodingGSMEFR",
2023 "OMX_AUDIO_CodingGSMHR",
2024 "OMX_AUDIO_CodingPDCFR",
2025 "OMX_AUDIO_CodingPDCEFR",
2026 "OMX_AUDIO_CodingPDCHR",
2027 "OMX_AUDIO_CodingTDMAFR",
2028 "OMX_AUDIO_CodingTDMAEFR",
2029 "OMX_AUDIO_CodingQCELP8",
2030 "OMX_AUDIO_CodingQCELP13",
2031 "OMX_AUDIO_CodingEVRC",
2032 "OMX_AUDIO_CodingSMV",
2033 "OMX_AUDIO_CodingG711",
2034 "OMX_AUDIO_CodingG723",
2035 "OMX_AUDIO_CodingG726",
2036 "OMX_AUDIO_CodingG729",
2037 "OMX_AUDIO_CodingAAC",
2038 "OMX_AUDIO_CodingMP3",
2039 "OMX_AUDIO_CodingSBC",
2040 "OMX_AUDIO_CodingVORBIS",
2041 "OMX_AUDIO_CodingWMA",
2042 "OMX_AUDIO_CodingRA",
2043 "OMX_AUDIO_CodingMIDI",
2044 };
2045
2046 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2047
2048 if (type < 0 || (size_t)type >= numNames) {
2049 return "UNKNOWN";
2050 } else {
2051 return kNames[type];
2052 }
2053}
2054
2055static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2056 static const char *kNames[] = {
2057 "OMX_AUDIO_PCMModeLinear",
2058 "OMX_AUDIO_PCMModeALaw",
2059 "OMX_AUDIO_PCMModeMULaw",
2060 };
2061
2062 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2063
2064 if (type < 0 || (size_t)type >= numNames) {
2065 return "UNKNOWN";
2066 } else {
2067 return kNames[type];
2068 }
2069}
2070
Andreas Huber7ae02c82009-09-09 16:29:47 -07002071static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2072 static const char *kNames[] = {
2073 "OMX_AUDIO_AMRBandModeUnused",
2074 "OMX_AUDIO_AMRBandModeNB0",
2075 "OMX_AUDIO_AMRBandModeNB1",
2076 "OMX_AUDIO_AMRBandModeNB2",
2077 "OMX_AUDIO_AMRBandModeNB3",
2078 "OMX_AUDIO_AMRBandModeNB4",
2079 "OMX_AUDIO_AMRBandModeNB5",
2080 "OMX_AUDIO_AMRBandModeNB6",
2081 "OMX_AUDIO_AMRBandModeNB7",
2082 "OMX_AUDIO_AMRBandModeWB0",
2083 "OMX_AUDIO_AMRBandModeWB1",
2084 "OMX_AUDIO_AMRBandModeWB2",
2085 "OMX_AUDIO_AMRBandModeWB3",
2086 "OMX_AUDIO_AMRBandModeWB4",
2087 "OMX_AUDIO_AMRBandModeWB5",
2088 "OMX_AUDIO_AMRBandModeWB6",
2089 "OMX_AUDIO_AMRBandModeWB7",
2090 "OMX_AUDIO_AMRBandModeWB8",
2091 };
2092
2093 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2094
2095 if (type < 0 || (size_t)type >= numNames) {
2096 return "UNKNOWN";
2097 } else {
2098 return kNames[type];
2099 }
2100}
2101
2102static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2103 static const char *kNames[] = {
2104 "OMX_AUDIO_AMRFrameFormatConformance",
2105 "OMX_AUDIO_AMRFrameFormatIF1",
2106 "OMX_AUDIO_AMRFrameFormatIF2",
2107 "OMX_AUDIO_AMRFrameFormatFSF",
2108 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2109 "OMX_AUDIO_AMRFrameFormatITU",
2110 };
2111
2112 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2113
2114 if (type < 0 || (size_t)type >= numNames) {
2115 return "UNKNOWN";
2116 } else {
2117 return kNames[type];
2118 }
2119}
Andreas Huberbe06d262009-08-14 14:37:10 -07002120
2121void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2122 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002123 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002124 def.nPortIndex = portIndex;
2125
2126 status_t err = mOMX->get_parameter(
2127 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2128 CHECK_EQ(err, OK);
2129
2130 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2131
2132 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2133 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2134
2135 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2136 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2137 printf(" nBufferSize = %ld\n", def.nBufferSize);
2138
2139 switch (def.eDomain) {
2140 case OMX_PortDomainImage:
2141 {
2142 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2143
2144 printf("\n");
2145 printf(" // Image\n");
2146 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2147 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2148 printf(" nStride = %ld\n", imageDef->nStride);
2149
2150 printf(" eCompressionFormat = %s\n",
2151 imageCompressionFormatString(imageDef->eCompressionFormat));
2152
2153 printf(" eColorFormat = %s\n",
2154 colorFormatString(imageDef->eColorFormat));
2155
2156 break;
2157 }
2158
2159 case OMX_PortDomainVideo:
2160 {
2161 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2162
2163 printf("\n");
2164 printf(" // Video\n");
2165 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2166 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2167 printf(" nStride = %ld\n", videoDef->nStride);
2168
2169 printf(" eCompressionFormat = %s\n",
2170 videoCompressionFormatString(videoDef->eCompressionFormat));
2171
2172 printf(" eColorFormat = %s\n",
2173 colorFormatString(videoDef->eColorFormat));
2174
2175 break;
2176 }
2177
2178 case OMX_PortDomainAudio:
2179 {
2180 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2181
2182 printf("\n");
2183 printf(" // Audio\n");
2184 printf(" eEncoding = %s\n",
2185 audioCodingTypeString(audioDef->eEncoding));
2186
2187 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2188 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002189 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002190 params.nPortIndex = portIndex;
2191
2192 err = mOMX->get_parameter(
2193 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2194 CHECK_EQ(err, OK);
2195
2196 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
2197 printf(" nChannels = %ld\n", params.nChannels);
2198 printf(" bInterleaved = %d\n", params.bInterleaved);
2199 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
2200
2201 printf(" eNumData = %s\n",
2202 params.eNumData == OMX_NumericalDataSigned
2203 ? "signed" : "unsigned");
2204
2205 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07002206 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
2207 OMX_AUDIO_PARAM_AMRTYPE amr;
2208 InitOMXParams(&amr);
2209 amr.nPortIndex = portIndex;
2210
2211 err = mOMX->get_parameter(
2212 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2213 CHECK_EQ(err, OK);
2214
2215 printf(" nChannels = %ld\n", amr.nChannels);
2216 printf(" eAMRBandMode = %s\n",
2217 amrBandModeString(amr.eAMRBandMode));
2218 printf(" eAMRFrameFormat = %s\n",
2219 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07002220 }
2221
2222 break;
2223 }
2224
2225 default:
2226 {
2227 printf(" // Unknown\n");
2228 break;
2229 }
2230 }
2231
2232 printf("}\n");
2233}
2234
2235void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
2236 mOutputFormat = new MetaData;
2237 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
2238
2239 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002240 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002241 def.nPortIndex = kPortIndexOutput;
2242
2243 status_t err = mOMX->get_parameter(
2244 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2245 CHECK_EQ(err, OK);
2246
2247 switch (def.eDomain) {
2248 case OMX_PortDomainImage:
2249 {
2250 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2251 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2252
2253 mOutputFormat->setCString(kKeyMIMEType, "image/raw");
2254 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
2255 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
2256 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
2257 break;
2258 }
2259
2260 case OMX_PortDomainAudio:
2261 {
2262 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
2263
Andreas Huberda050cf22009-09-02 14:01:43 -07002264 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
2265 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002266 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07002267 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002268
Andreas Huberda050cf22009-09-02 14:01:43 -07002269 err = mOMX->get_parameter(
2270 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2271 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002272
Andreas Huberda050cf22009-09-02 14:01:43 -07002273 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
2274 CHECK_EQ(params.nBitPerSample, 16);
2275 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07002276
Andreas Huberda050cf22009-09-02 14:01:43 -07002277 int32_t numChannels, sampleRate;
2278 inputFormat->findInt32(kKeyChannelCount, &numChannels);
2279 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07002280
Andreas Huberda050cf22009-09-02 14:01:43 -07002281 if ((OMX_U32)numChannels != params.nChannels) {
2282 LOGW("Codec outputs a different number of channels than "
2283 "the input stream contains.");
2284 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002285
Andreas Huberda050cf22009-09-02 14:01:43 -07002286 mOutputFormat->setCString(kKeyMIMEType, "audio/raw");
2287
2288 // Use the codec-advertised number of channels, as some
2289 // codecs appear to output stereo even if the input data is
2290 // mono.
2291 mOutputFormat->setInt32(kKeyChannelCount, params.nChannels);
2292
2293 // The codec-reported sampleRate is not reliable...
2294 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
2295 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07002296 OMX_AUDIO_PARAM_AMRTYPE amr;
2297 InitOMXParams(&amr);
2298 amr.nPortIndex = kPortIndexOutput;
2299
2300 err = mOMX->get_parameter(
2301 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2302 CHECK_EQ(err, OK);
2303
2304 CHECK_EQ(amr.nChannels, 1);
2305 mOutputFormat->setInt32(kKeyChannelCount, 1);
2306
2307 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
2308 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
2309 mOutputFormat->setCString(kKeyMIMEType, "audio/3gpp");
2310 mOutputFormat->setInt32(kKeySampleRate, 8000);
2311 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
2312 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
2313 mOutputFormat->setCString(kKeyMIMEType, "audio/amr-wb");
2314 mOutputFormat->setInt32(kKeySampleRate, 16000);
2315 } else {
2316 CHECK(!"Unknown AMR band mode.");
2317 }
Andreas Huberda050cf22009-09-02 14:01:43 -07002318 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
2319 mOutputFormat->setCString(kKeyMIMEType, "audio/mp4a-latm");
2320 } else {
2321 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07002322 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002323 break;
2324 }
2325
2326 case OMX_PortDomainVideo:
2327 {
2328 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
2329
2330 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
2331 mOutputFormat->setCString(kKeyMIMEType, "video/raw");
2332 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
2333 mOutputFormat->setCString(kKeyMIMEType, "video/mp4v-es");
2334 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
2335 mOutputFormat->setCString(kKeyMIMEType, "video/3gpp");
2336 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
2337 mOutputFormat->setCString(kKeyMIMEType, "video/avc");
2338 } else {
2339 CHECK(!"Unknown compression format.");
2340 }
2341
2342 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
2343 // This component appears to be lying to me.
2344 mOutputFormat->setInt32(
2345 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
2346 mOutputFormat->setInt32(
2347 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
2348 } else {
2349 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
2350 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
2351 }
2352
2353 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
2354 break;
2355 }
2356
2357 default:
2358 {
2359 CHECK(!"should not be here, neither audio nor video.");
2360 break;
2361 }
2362 }
2363}
2364
2365} // namespace android