blob: d01d6afcc39290667fbb84bb72094be3957d563e [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" },
52 { "audio/mp4a-latm", "OMX.TI.AAC.decode" },
53 { "audio/mp4a-latm", "OMX.PV.aacdec" },
54 { "video/mp4v-es", "OMX.qcom.video.decoder.mpeg4" },
55 { "video/mp4v-es", "OMX.TI.Video.Decoder" },
56 { "video/mp4v-es", "OMX.PV.mpeg4dec" },
57 { "video/3gpp", "OMX.qcom.video.decoder.h263" },
58 { "video/3gpp", "OMX.TI.Video.Decoder" },
59 { "video/3gpp", "OMX.PV.h263dec" },
60 { "video/avc", "OMX.qcom.video.decoder.avc" },
61 { "video/avc", "OMX.TI.Video.Decoder" },
62 { "video/avc", "OMX.PV.avcdec" },
63};
64
65static const CodecInfo kEncoderInfo[] = {
66 { "audio/3gpp", "OMX.TI.AMR.encode" },
67 { "audio/3gpp", "OMX.PV.amrencnb" },
68 { "audio/mp4a-latm", "OMX.TI.AAC.encode" },
69 { "audio/mp4a-latm", "OMX.PV.aacenc" },
70 { "video/mp4v-es", "OMX.qcom.video.encoder.mpeg4" },
71 { "video/mp4v-es", "OMX.TI.Video.encoder" },
72 { "video/mp4v-es", "OMX.PV.mpeg4enc" },
73 { "video/3gpp", "OMX.qcom.video.encoder.h263" },
74 { "video/3gpp", "OMX.TI.Video.encoder" },
75 { "video/3gpp", "OMX.PV.h263enc" },
76 { "video/avc", "OMX.TI.Video.encoder" },
77 { "video/avc", "OMX.PV.avcenc" },
78};
79
Andreas Huber4c483422009-09-02 16:05:36 -070080#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
81
Andreas Huberbe06d262009-08-14 14:37:10 -070082struct OMXCodecObserver : public BnOMXObserver {
83 OMXCodecObserver(const wp<OMXCodec> &target)
84 : mTarget(target) {
85 }
86
87 // from IOMXObserver
88 virtual void on_message(const omx_message &msg) {
89 sp<OMXCodec> codec = mTarget.promote();
90
91 if (codec.get() != NULL) {
92 codec->on_message(msg);
93 }
94 }
95
96protected:
97 virtual ~OMXCodecObserver() {}
98
99private:
100 wp<OMXCodec> mTarget;
101
102 OMXCodecObserver(const OMXCodecObserver &);
103 OMXCodecObserver &operator=(const OMXCodecObserver &);
104};
105
106static const char *GetCodec(const CodecInfo *info, size_t numInfos,
107 const char *mime, int index) {
108 CHECK(index >= 0);
109 for(size_t i = 0; i < numInfos; ++i) {
110 if (!strcasecmp(mime, info[i].mime)) {
111 if (index == 0) {
112 return info[i].codec;
113 }
114
115 --index;
116 }
117 }
118
119 return NULL;
120}
121
Andreas Huberebf66ea2009-08-19 13:32:58 -0700122enum {
123 kAVCProfileBaseline = 0x42,
124 kAVCProfileMain = 0x4d,
125 kAVCProfileExtended = 0x58,
126 kAVCProfileHigh = 0x64,
127 kAVCProfileHigh10 = 0x6e,
128 kAVCProfileHigh422 = 0x7a,
129 kAVCProfileHigh444 = 0xf4,
130 kAVCProfileCAVLC444Intra = 0x2c
131};
132
133static const char *AVCProfileToString(uint8_t profile) {
134 switch (profile) {
135 case kAVCProfileBaseline:
136 return "Baseline";
137 case kAVCProfileMain:
138 return "Main";
139 case kAVCProfileExtended:
140 return "Extended";
141 case kAVCProfileHigh:
142 return "High";
143 case kAVCProfileHigh10:
144 return "High 10";
145 case kAVCProfileHigh422:
146 return "High 422";
147 case kAVCProfileHigh444:
148 return "High 444";
149 case kAVCProfileCAVLC444Intra:
150 return "CAVLC 444 Intra";
151 default: return "Unknown";
152 }
153}
154
Andreas Huber4c483422009-09-02 16:05:36 -0700155template<class T>
156static void InitOMXParams(T *params) {
157 params->nSize = sizeof(T);
158 params->nVersion.s.nVersionMajor = 1;
159 params->nVersion.s.nVersionMinor = 0;
160 params->nVersion.s.nRevision = 0;
161 params->nVersion.s.nStep = 0;
162}
163
Andreas Huberbe06d262009-08-14 14:37:10 -0700164// static
165sp<OMXCodec> OMXCodec::Create(
166 const sp<IOMX> &omx,
167 const sp<MetaData> &meta, bool createEncoder,
168 const sp<MediaSource> &source) {
169 const char *mime;
170 bool success = meta->findCString(kKeyMIMEType, &mime);
171 CHECK(success);
172
173 const char *componentName = NULL;
174 IOMX::node_id node = 0;
175 for (int index = 0;; ++index) {
176 if (createEncoder) {
177 componentName = GetCodec(
178 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
179 mime, index);
180 } else {
181 componentName = GetCodec(
182 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
183 mime, index);
184 }
185
186 if (!componentName) {
187 return NULL;
188 }
189
190 LOGV("Attempting to allocate OMX node '%s'", componentName);
191
192 status_t err = omx->allocate_node(componentName, &node);
193 if (err == OK) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700194 LOGI("Successfully allocated OMX node '%s'", componentName);
Andreas Huberbe06d262009-08-14 14:37:10 -0700195 break;
196 }
197 }
198
199 uint32_t quirks = 0;
200 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700201 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700202 }
203 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
204 quirks |= kNeedsFlushBeforeDisable;
205 }
206 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
207 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700208 quirks |= kRequiresFlushCompleteEmulation;
Andreas Huber127fcdc2009-08-26 16:27:02 -0700209
210 // The following is currently necessary for proper shutdown
211 // behaviour, but NOT enabled by default in order to make the
212 // bug reproducible...
213 // quirks |= kRequiresFlushBeforeShutdown;
Andreas Huberbe06d262009-08-14 14:37:10 -0700214 }
215 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
216 quirks |= kRequiresLoadedToIdleAfterAllocation;
217 quirks |= kRequiresAllocateBufferOnInputPorts;
218 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700219 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
220 // XXX Required on P....on only.
221 quirks |= kRequiresAllocateBufferOnOutputPorts;
222 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700223
224 sp<OMXCodec> codec = new OMXCodec(
225 omx, node, quirks, createEncoder, mime, componentName,
226 source);
227
228 uint32_t type;
229 const void *data;
230 size_t size;
231 if (meta->findData(kKeyESDS, &type, &data, &size)) {
232 ESDS esds((const char *)data, size);
233 CHECK_EQ(esds.InitCheck(), OK);
234
235 const void *codec_specific_data;
236 size_t codec_specific_data_size;
237 esds.getCodecSpecificInfo(
238 &codec_specific_data, &codec_specific_data_size);
239
240 printf("found codec-specific data of size %d\n",
241 codec_specific_data_size);
242
243 codec->addCodecSpecificData(
244 codec_specific_data, codec_specific_data_size);
245 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
246 printf("found avcc of size %d\n", size);
247
Andreas Huberebf66ea2009-08-19 13:32:58 -0700248 // Parse the AVCDecoderConfigurationRecord
249
250 const uint8_t *ptr = (const uint8_t *)data;
251
252 CHECK(size >= 7);
253 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
254 uint8_t profile = ptr[1];
255 uint8_t level = ptr[3];
256
257 CHECK((ptr[4] >> 2) == 0x3f); // reserved
258
259 size_t lengthSize = 1 + (ptr[4] & 3);
260
261 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
262 // violates it...
263 // CHECK((ptr[5] >> 5) == 7); // reserved
264
265 size_t numSeqParameterSets = ptr[5] & 31;
266
267 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700268 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700269
270 for (size_t i = 0; i < numSeqParameterSets; ++i) {
271 CHECK(size >= 2);
272 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700273
274 ptr += 2;
275 size -= 2;
276
Andreas Huberbe06d262009-08-14 14:37:10 -0700277 CHECK(size >= length);
278
279 codec->addCodecSpecificData(ptr, length);
280
281 ptr += length;
282 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700283 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700284
Andreas Huberebf66ea2009-08-19 13:32:58 -0700285 CHECK(size >= 1);
286 size_t numPictureParameterSets = *ptr;
287 ++ptr;
288 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700289
Andreas Huberebf66ea2009-08-19 13:32:58 -0700290 for (size_t i = 0; i < numPictureParameterSets; ++i) {
291 CHECK(size >= 2);
292 size_t length = U16_AT(ptr);
293
294 ptr += 2;
295 size -= 2;
296
297 CHECK(size >= length);
298
299 codec->addCodecSpecificData(ptr, length);
300
301 ptr += length;
302 size -= length;
303 }
304
305 LOGI("AVC profile = %d (%s), level = %d",
306 (int)profile, AVCProfileToString(profile), (int)level / 10);
307
308 if (!strcmp(componentName, "OMX.TI.Video.Decoder")
309 && (profile != kAVCProfileBaseline || level > 39)) {
310 // This stream exceeds the decoder's capabilities.
311
312 LOGE("Profile and/or level exceed the decoder's capabilities.");
313 return NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -0700314 }
315 }
316
317 if (!strcasecmp("audio/3gpp", mime)) {
318 codec->setAMRFormat();
319 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700320 if (!strcasecmp("audio/mp4a-latm", mime)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700321 int32_t numChannels, sampleRate;
322 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
323 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
324
325 codec->setAACFormat(numChannels, sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700326 }
327 if (!strncasecmp(mime, "video/", 6)) {
328 int32_t width, height;
329 bool success = meta->findInt32(kKeyWidth, &width);
330 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700331 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700332
333 if (createEncoder) {
334 codec->setVideoInputFormat(mime, width, height);
335 } else {
336 codec->setVideoOutputFormat(mime, width, height);
337 }
338 }
339 if (!strcasecmp(mime, "image/jpeg")
340 && !strcmp(componentName, "OMX.TI.JPEG.decode")) {
341 OMX_COLOR_FORMATTYPE format =
342 OMX_COLOR_Format32bitARGB8888;
343 // OMX_COLOR_FormatYUV420PackedPlanar;
344 // OMX_COLOR_FormatCbYCrY;
345 // OMX_COLOR_FormatYUV411Planar;
346
347 int32_t width, height;
348 bool success = meta->findInt32(kKeyWidth, &width);
349 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700350
351 int32_t compressedSize;
352 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700353 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700354
355 CHECK(success);
356 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700357
358 codec->setImageOutputFormat(format, width, height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700359 codec->setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700360 }
361
Andreas Huberda050cf22009-09-02 14:01:43 -0700362 int32_t maxInputSize;
363 if (createEncoder && meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
364 codec->setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
365 }
366
367 if (!strcmp(componentName, "OMX.TI.AMR.encode")
368 || !strcmp(componentName, "OMX.TI.WBAMR.encode")) {
369 codec->setMinBufferSize(kPortIndexOutput, 8192); // XXX
370 }
371
Andreas Huberbe06d262009-08-14 14:37:10 -0700372 codec->initOutputFormat(meta);
373
374 return codec;
375}
376
Andreas Huberda050cf22009-09-02 14:01:43 -0700377void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
378 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700379 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700380 def.nPortIndex = portIndex;
381
382 status_t err = mOMX->get_parameter(
383 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
384 CHECK_EQ(err, OK);
385
386 if (def.nBufferSize < size) {
387 def.nBufferSize = size;
388
389 }
390
391 err = mOMX->set_parameter(
392 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
393 CHECK_EQ(err, OK);
394}
395
Andreas Huberbe06d262009-08-14 14:37:10 -0700396status_t OMXCodec::setVideoPortFormatType(
397 OMX_U32 portIndex,
398 OMX_VIDEO_CODINGTYPE compressionFormat,
399 OMX_COLOR_FORMATTYPE colorFormat) {
400 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700401 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700402 format.nPortIndex = portIndex;
403 format.nIndex = 0;
404 bool found = false;
405
406 OMX_U32 index = 0;
407 for (;;) {
408 format.nIndex = index;
409 status_t err = mOMX->get_parameter(
410 mNode, OMX_IndexParamVideoPortFormat,
411 &format, sizeof(format));
412
413 if (err != OK) {
414 return err;
415 }
416
417 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700418 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700419
420#if 1
421 LOGI("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
422 portIndex,
423 index, format.eCompressionFormat, format.eColorFormat);
424#endif
425
426 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
427 if (portIndex == kPortIndexInput
428 && colorFormat == format.eColorFormat) {
429 // eCompressionFormat does not seem right.
430 found = true;
431 break;
432 }
433 if (portIndex == kPortIndexOutput
434 && compressionFormat == format.eCompressionFormat) {
435 // eColorFormat does not seem right.
436 found = true;
437 break;
438 }
439 }
440
441 if (format.eCompressionFormat == compressionFormat
442 && format.eColorFormat == colorFormat) {
443 found = true;
444 break;
445 }
446
447 ++index;
448 }
449
450 if (!found) {
451 return UNKNOWN_ERROR;
452 }
453
454 LOGI("found a match.");
455 status_t err = mOMX->set_parameter(
456 mNode, OMX_IndexParamVideoPortFormat,
457 &format, sizeof(format));
458
459 return err;
460}
461
462void OMXCodec::setVideoInputFormat(
463 const char *mime, OMX_U32 width, OMX_U32 height) {
464 LOGI("setVideoInputFormat width=%ld, height=%ld", width, height);
465
466 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
467 if (!strcasecmp("video/avc", mime)) {
468 compressionFormat = OMX_VIDEO_CodingAVC;
469 } else if (!strcasecmp("video/mp4v-es", mime)) {
470 compressionFormat = OMX_VIDEO_CodingMPEG4;
471 } else if (!strcasecmp("video/3gpp", mime)) {
472 compressionFormat = OMX_VIDEO_CodingH263;
473 } else {
474 LOGE("Not a supported video mime type: %s", mime);
475 CHECK(!"Should not be here. Not a supported video mime type.");
476 }
477
478 OMX_COLOR_FORMATTYPE colorFormat =
479 0 ? OMX_COLOR_FormatYCbYCr : OMX_COLOR_FormatCbYCrY;
480
481 if (!strncmp("OMX.qcom.video.encoder.", mComponentName, 23)) {
482 colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
483 }
484
485 setVideoPortFormatType(
486 kPortIndexInput, OMX_VIDEO_CodingUnused,
487 colorFormat);
488
489 setVideoPortFormatType(
490 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused);
491
492 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700493 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700494 def.nPortIndex = kPortIndexOutput;
495
Andreas Huber4c483422009-09-02 16:05:36 -0700496 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
497
Andreas Huberbe06d262009-08-14 14:37:10 -0700498 status_t err = mOMX->get_parameter(
499 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
500
501 CHECK_EQ(err, OK);
502 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
503
504 video_def->nFrameWidth = width;
505 video_def->nFrameHeight = height;
506
507 video_def->eCompressionFormat = compressionFormat;
508 video_def->eColorFormat = OMX_COLOR_FormatUnused;
509
510 err = mOMX->set_parameter(
511 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
512 CHECK_EQ(err, OK);
513
514 ////////////////////////////////////////////////////////////////////////////
515
Andreas Huber4c483422009-09-02 16:05:36 -0700516 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700517 def.nPortIndex = kPortIndexInput;
518
519 err = mOMX->get_parameter(
520 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
521 CHECK_EQ(err, OK);
522
523 def.nBufferSize = (width * height * 2); // (width * height * 3) / 2;
Andreas Huber4c483422009-09-02 16:05:36 -0700524 LOGI("Setting nBufferSize = %ld", def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700525
526 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
527
528 video_def->nFrameWidth = width;
529 video_def->nFrameHeight = height;
530 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
531 video_def->eColorFormat = colorFormat;
532
533 err = mOMX->set_parameter(
534 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
535 CHECK_EQ(err, OK);
536}
537
538void OMXCodec::setVideoOutputFormat(
539 const char *mime, OMX_U32 width, OMX_U32 height) {
540 LOGI("setVideoOutputFormat width=%ld, height=%ld", width, height);
541
Andreas Huberbe06d262009-08-14 14:37:10 -0700542 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
543 if (!strcasecmp("video/avc", mime)) {
544 compressionFormat = OMX_VIDEO_CodingAVC;
545 } else if (!strcasecmp("video/mp4v-es", mime)) {
546 compressionFormat = OMX_VIDEO_CodingMPEG4;
547 } else if (!strcasecmp("video/3gpp", mime)) {
548 compressionFormat = OMX_VIDEO_CodingH263;
549 } else {
550 LOGE("Not a supported video mime type: %s", mime);
551 CHECK(!"Should not be here. Not a supported video mime type.");
552 }
553
554 setVideoPortFormatType(
555 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
556
557#if 1
558 {
559 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700560 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700561 format.nPortIndex = kPortIndexOutput;
562 format.nIndex = 0;
563
564 status_t err = mOMX->get_parameter(
565 mNode, OMX_IndexParamVideoPortFormat,
566 &format, sizeof(format));
567 CHECK_EQ(err, OK);
568 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
569
570 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
571
572 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
573 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
574 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
575 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
576
577 err = mOMX->set_parameter(
578 mNode, OMX_IndexParamVideoPortFormat,
579 &format, sizeof(format));
580 CHECK_EQ(err, OK);
581 }
582#endif
583
584 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700585 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700586 def.nPortIndex = kPortIndexInput;
587
Andreas Huber4c483422009-09-02 16:05:36 -0700588 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
589
Andreas Huberbe06d262009-08-14 14:37:10 -0700590 status_t err = mOMX->get_parameter(
591 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
592
593 CHECK_EQ(err, OK);
594
595#if 1
596 // XXX Need a (much) better heuristic to compute input buffer sizes.
597 const size_t X = 64 * 1024;
598 if (def.nBufferSize < X) {
599 def.nBufferSize = X;
600 }
601#endif
602
603 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
604
605 video_def->nFrameWidth = width;
606 video_def->nFrameHeight = height;
607
608 video_def->eColorFormat = OMX_COLOR_FormatUnused;
609
610 err = mOMX->set_parameter(
611 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
612 CHECK_EQ(err, OK);
613
614 ////////////////////////////////////////////////////////////////////////////
615
Andreas Huber4c483422009-09-02 16:05:36 -0700616 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700617 def.nPortIndex = kPortIndexOutput;
618
619 err = mOMX->get_parameter(
620 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
621 CHECK_EQ(err, OK);
622 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
623
624#if 0
625 def.nBufferSize =
626 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
627#endif
628
629 video_def->nFrameWidth = width;
630 video_def->nFrameHeight = height;
631
632 err = mOMX->set_parameter(
633 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
634 CHECK_EQ(err, OK);
635}
636
637
638OMXCodec::OMXCodec(
639 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -0700640 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -0700641 const char *mime,
642 const char *componentName,
643 const sp<MediaSource> &source)
644 : mOMX(omx),
645 mNode(node),
646 mQuirks(quirks),
647 mIsEncoder(isEncoder),
648 mMIME(strdup(mime)),
649 mComponentName(strdup(componentName)),
650 mSource(source),
651 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -0700652 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -0700653 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -0700654 mSignalledEOS(false),
655 mNoMoreOutputData(false),
656 mSeekTimeUs(-1) {
657 mPortStatus[kPortIndexInput] = ENABLED;
658 mPortStatus[kPortIndexOutput] = ENABLED;
659
660 mObserver = new OMXCodecObserver(this);
661 mOMX->observe_node(mNode, mObserver);
Andreas Huber4c483422009-09-02 16:05:36 -0700662
663 setComponentRole();
664}
665
666void OMXCodec::setComponentRole() {
667 struct MimeToRole {
668 const char *mime;
669 const char *decoderRole;
670 const char *encoderRole;
671 };
672
673 static const MimeToRole kMimeToRole[] = {
674 { "audio/mpeg", "audio_decoder.mp3", "audio_encoder.mp3" },
675 { "audio/3gpp", "audio_decoder.amrnb", "audio_encoder.amrnb" },
676 { "audio/mp4a-latm", "audio_decoder.aac", "audio_encoder.aac" },
677 { "video/avc", "video_decoder.avc", "video_encoder.avc" },
678 { "video/mp4v-es", "video_decoder.mpeg4", "video_encoder.mpeg4" },
679 { "video/3gpp", "video_decoder.h263", "video_encoder.h263" },
680 };
681
682 static const size_t kNumMimeToRole =
683 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
684
685 size_t i;
686 for (i = 0; i < kNumMimeToRole; ++i) {
687 if (!strcasecmp(mMIME, kMimeToRole[i].mime)) {
688 break;
689 }
690 }
691
692 if (i == kNumMimeToRole) {
693 return;
694 }
695
696 const char *role =
697 mIsEncoder ? kMimeToRole[i].encoderRole
698 : kMimeToRole[i].decoderRole;
699
700 if (role != NULL) {
701 CODEC_LOGV("Setting component role '%s'.", role);
702
703 OMX_PARAM_COMPONENTROLETYPE roleParams;
704 InitOMXParams(&roleParams);
705
706 strncpy((char *)roleParams.cRole,
707 role, OMX_MAX_STRINGNAME_SIZE - 1);
708
709 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
710
711 status_t err = mOMX->set_parameter(
712 mNode, OMX_IndexParamStandardComponentRole,
713 &roleParams, sizeof(roleParams));
714
715 if (err != OK) {
716 LOGW("Failed to set standard component role '%s'.", role);
717 }
718 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700719}
720
721OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700722 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -0700723
724 status_t err = mOMX->observe_node(mNode, NULL);
725 CHECK_EQ(err, OK);
726
727 err = mOMX->free_node(mNode);
728 CHECK_EQ(err, OK);
729
730 mNode = NULL;
731 setState(DEAD);
732
733 clearCodecSpecificData();
734
735 free(mComponentName);
736 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700737
Andreas Huberbe06d262009-08-14 14:37:10 -0700738 free(mMIME);
739 mMIME = NULL;
740}
741
742status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -0700743 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -0700744
745 CHECK_EQ(mState, LOADED);
746
747 status_t err;
748 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
749 err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
750 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700751 setState(LOADED_TO_IDLE);
752 }
753
754 err = allocateBuffers();
755 CHECK_EQ(err, OK);
756
757 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
758 err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
759 CHECK_EQ(err, OK);
760
761 setState(LOADED_TO_IDLE);
762 }
763
764 while (mState != EXECUTING && mState != ERROR) {
765 mAsyncCompletion.wait(mLock);
766 }
767
768 return mState == ERROR ? UNKNOWN_ERROR : OK;
769}
770
771// static
772bool OMXCodec::isIntermediateState(State state) {
773 return state == LOADED_TO_IDLE
774 || state == IDLE_TO_EXECUTING
775 || state == EXECUTING_TO_IDLE
776 || state == IDLE_TO_LOADED
777 || state == RECONFIGURING;
778}
779
780status_t OMXCodec::allocateBuffers() {
781 status_t err = allocateBuffersOnPort(kPortIndexInput);
782
783 if (err != OK) {
784 return err;
785 }
786
787 return allocateBuffersOnPort(kPortIndexOutput);
788}
789
790status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
791 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700792 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700793 def.nPortIndex = portIndex;
794
795 status_t err = mOMX->get_parameter(
796 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
797
798 if (err != OK) {
799 return err;
800 }
801
Andreas Huber5c0a9132009-08-20 11:16:40 -0700802 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
803 mDealer[portIndex] = new MemoryDealer(totalSize);
804
Andreas Huberbe06d262009-08-14 14:37:10 -0700805 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -0700806 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700807 CHECK(mem.get() != NULL);
808
809 IOMX::buffer_id buffer;
810 if (portIndex == kPortIndexInput
811 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
812 err = mOMX->allocate_buffer_with_backup(
813 mNode, portIndex, mem, &buffer);
Andreas Huber446f44f2009-08-25 17:23:44 -0700814 } else if (portIndex == kPortIndexOutput
815 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
816 err = mOMX->allocate_buffer(
817 mNode, portIndex, def.nBufferSize, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700818 } else {
819 err = mOMX->use_buffer(mNode, portIndex, mem, &buffer);
820 }
821
822 if (err != OK) {
823 LOGE("allocate_buffer_with_backup failed");
824 return err;
825 }
826
827 BufferInfo info;
828 info.mBuffer = buffer;
829 info.mOwnedByComponent = false;
830 info.mMem = mem;
831 info.mMediaBuffer = NULL;
832
833 if (portIndex == kPortIndexOutput) {
834 info.mMediaBuffer = new MediaBuffer(mem->pointer(), mem->size());
835 info.mMediaBuffer->setObserver(this);
836 }
837
838 mPortBuffers[portIndex].push(info);
839
Andreas Huber4c483422009-09-02 16:05:36 -0700840 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -0700841 portIndex == kPortIndexInput ? "input" : "output");
842 }
843
844 dumpPortStatus(portIndex);
845
846 return OK;
847}
848
849void OMXCodec::on_message(const omx_message &msg) {
850 Mutex::Autolock autoLock(mLock);
851
852 switch (msg.type) {
853 case omx_message::EVENT:
854 {
855 onEvent(
856 msg.u.event_data.event, msg.u.event_data.data1,
857 msg.u.event_data.data2);
858
859 break;
860 }
861
862 case omx_message::EMPTY_BUFFER_DONE:
863 {
864 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
865
Andreas Huber4c483422009-09-02 16:05:36 -0700866 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700867
868 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
869 size_t i = 0;
870 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
871 ++i;
872 }
873
874 CHECK(i < buffers->size());
875 if (!(*buffers)[i].mOwnedByComponent) {
876 LOGW("We already own input buffer %p, yet received "
877 "an EMPTY_BUFFER_DONE.", buffer);
878 }
879
880 buffers->editItemAt(i).mOwnedByComponent = false;
881
882 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -0700883 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700884
885 status_t err =
886 mOMX->free_buffer(mNode, kPortIndexInput, buffer);
887 CHECK_EQ(err, OK);
888
889 buffers->removeAt(i);
890 } else if (mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
891 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
892 drainInputBuffer(&buffers->editItemAt(i));
893 }
894
895 break;
896 }
897
898 case omx_message::FILL_BUFFER_DONE:
899 {
900 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
901 OMX_U32 flags = msg.u.extended_buffer_data.flags;
902
Andreas Huber4c483422009-09-02 16:05:36 -0700903 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx)",
Andreas Huberbe06d262009-08-14 14:37:10 -0700904 buffer,
905 msg.u.extended_buffer_data.range_length,
906 flags);
907
Andreas Huber4c483422009-09-02 16:05:36 -0700908 CODEC_LOGV("FILL_BUFFER_DONE(timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -0700909 msg.u.extended_buffer_data.timestamp,
910 msg.u.extended_buffer_data.timestamp / 1E6);
911
912 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
913 size_t i = 0;
914 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
915 ++i;
916 }
917
918 CHECK(i < buffers->size());
919 BufferInfo *info = &buffers->editItemAt(i);
920
921 if (!info->mOwnedByComponent) {
922 LOGW("We already own output buffer %p, yet received "
923 "a FILL_BUFFER_DONE.", buffer);
924 }
925
926 info->mOwnedByComponent = false;
927
928 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -0700929 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700930
931 status_t err =
932 mOMX->free_buffer(mNode, kPortIndexOutput, buffer);
933 CHECK_EQ(err, OK);
934
935 buffers->removeAt(i);
Andreas Huberd7795892009-08-26 10:33:47 -0700936 } else if (mPortStatus[kPortIndexOutput] == ENABLED
937 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -0700938 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -0700939 mNoMoreOutputData = true;
940 mBufferFilled.signal();
941 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
942 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700943
Andreas Huberbe06d262009-08-14 14:37:10 -0700944 MediaBuffer *buffer = info->mMediaBuffer;
945
946 buffer->set_range(
947 msg.u.extended_buffer_data.range_offset,
948 msg.u.extended_buffer_data.range_length);
949
950 buffer->meta_data()->clear();
951
952 buffer->meta_data()->setInt32(
953 kKeyTimeUnits,
954 (msg.u.extended_buffer_data.timestamp + 500) / 1000);
955
956 buffer->meta_data()->setInt32(
957 kKeyTimeScale, 1000);
958
959 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
960 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
961 }
962
963 buffer->meta_data()->setPointer(
964 kKeyPlatformPrivate,
965 msg.u.extended_buffer_data.platform_private);
966
967 buffer->meta_data()->setPointer(
968 kKeyBufferID,
969 msg.u.extended_buffer_data.buffer);
970
971 mFilledBuffers.push_back(i);
972 mBufferFilled.signal();
973 }
974
975 break;
976 }
977
978 default:
979 {
980 CHECK(!"should not be here.");
981 break;
982 }
983 }
984}
985
986void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
987 switch (event) {
988 case OMX_EventCmdComplete:
989 {
990 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
991 break;
992 }
993
994 case OMX_EventError:
995 {
996 LOGE("ERROR(%ld, %ld)", data1, data2);
997
998 setState(ERROR);
999 break;
1000 }
1001
1002 case OMX_EventPortSettingsChanged:
1003 {
1004 onPortSettingsChanged(data1);
1005 break;
1006 }
1007
1008 case OMX_EventBufferFlag:
1009 {
Andreas Huber4c483422009-09-02 16:05:36 -07001010 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001011
1012 if (data1 == kPortIndexOutput) {
1013 mNoMoreOutputData = true;
1014 }
1015 break;
1016 }
1017
1018 default:
1019 {
Andreas Huber4c483422009-09-02 16:05:36 -07001020 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001021 break;
1022 }
1023 }
1024}
1025
1026void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1027 switch (cmd) {
1028 case OMX_CommandStateSet:
1029 {
1030 onStateChange((OMX_STATETYPE)data);
1031 break;
1032 }
1033
1034 case OMX_CommandPortDisable:
1035 {
1036 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001037 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001038
1039 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1040 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1041 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1042
1043 mPortStatus[portIndex] = DISABLED;
1044
1045 if (mState == RECONFIGURING) {
1046 CHECK_EQ(portIndex, kPortIndexOutput);
1047
1048 enablePortAsync(portIndex);
1049
1050 status_t err = allocateBuffersOnPort(portIndex);
1051 CHECK_EQ(err, OK);
1052 }
1053 break;
1054 }
1055
1056 case OMX_CommandPortEnable:
1057 {
1058 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001059 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001060
1061 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1062 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1063
1064 mPortStatus[portIndex] = ENABLED;
1065
1066 if (mState == RECONFIGURING) {
1067 CHECK_EQ(portIndex, kPortIndexOutput);
1068
1069 setState(EXECUTING);
1070
1071 fillOutputBuffers();
1072 }
1073 break;
1074 }
1075
1076 case OMX_CommandFlush:
1077 {
1078 OMX_U32 portIndex = data;
1079
Andreas Huber4c483422009-09-02 16:05:36 -07001080 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001081
1082 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1083 mPortStatus[portIndex] = ENABLED;
1084
1085 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1086 mPortBuffers[portIndex].size());
1087
1088 if (mState == RECONFIGURING) {
1089 CHECK_EQ(portIndex, kPortIndexOutput);
1090
1091 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001092 } else if (mState == EXECUTING_TO_IDLE) {
1093 if (mPortStatus[kPortIndexInput] == ENABLED
1094 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001095 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001096 "transition from EXECUTING to IDLE.");
1097
1098 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1099 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1100
1101 status_t err =
1102 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1103 CHECK_EQ(err, OK);
1104 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001105 } else {
1106 // We're flushing both ports in preparation for seeking.
1107
1108 if (mPortStatus[kPortIndexInput] == ENABLED
1109 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001110 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001111 " seek-time.");
1112
1113 drainInputBuffers();
1114 fillOutputBuffers();
1115 }
1116 }
1117
1118 break;
1119 }
1120
1121 default:
1122 {
Andreas Huber4c483422009-09-02 16:05:36 -07001123 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001124 break;
1125 }
1126 }
1127}
1128
1129void OMXCodec::onStateChange(OMX_STATETYPE newState) {
1130 switch (newState) {
1131 case OMX_StateIdle:
1132 {
Andreas Huber4c483422009-09-02 16:05:36 -07001133 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001134 if (mState == LOADED_TO_IDLE) {
1135 status_t err = mOMX->send_command(
1136 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1137
1138 CHECK_EQ(err, OK);
1139
1140 setState(IDLE_TO_EXECUTING);
1141 } else {
1142 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1143
1144 CHECK_EQ(
1145 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1146 mPortBuffers[kPortIndexInput].size());
1147
1148 CHECK_EQ(
1149 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1150 mPortBuffers[kPortIndexOutput].size());
1151
1152 status_t err = mOMX->send_command(
1153 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1154
1155 CHECK_EQ(err, OK);
1156
1157 err = freeBuffersOnPort(kPortIndexInput);
1158 CHECK_EQ(err, OK);
1159
1160 err = freeBuffersOnPort(kPortIndexOutput);
1161 CHECK_EQ(err, OK);
1162
1163 mPortStatus[kPortIndexInput] = ENABLED;
1164 mPortStatus[kPortIndexOutput] = ENABLED;
1165
1166 setState(IDLE_TO_LOADED);
1167 }
1168 break;
1169 }
1170
1171 case OMX_StateExecuting:
1172 {
1173 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1174
Andreas Huber4c483422009-09-02 16:05:36 -07001175 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001176
1177 setState(EXECUTING);
1178
Andreas Huber42978e52009-08-27 10:08:39 -07001179 // Buffers will be submitted to the component in the first
1180 // call to OMXCodec::read as mInitialBufferSubmit is true at
1181 // this point. This ensures that this on_message call returns,
1182 // releases the lock and ::init can notice the state change and
1183 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001184 break;
1185 }
1186
1187 case OMX_StateLoaded:
1188 {
1189 CHECK_EQ(mState, IDLE_TO_LOADED);
1190
Andreas Huber4c483422009-09-02 16:05:36 -07001191 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001192
1193 setState(LOADED);
1194 break;
1195 }
1196
1197 default:
1198 {
1199 CHECK(!"should not be here.");
1200 break;
1201 }
1202 }
1203}
1204
1205// static
1206size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1207 size_t n = 0;
1208 for (size_t i = 0; i < buffers.size(); ++i) {
1209 if (!buffers[i].mOwnedByComponent) {
1210 ++n;
1211 }
1212 }
1213
1214 return n;
1215}
1216
1217status_t OMXCodec::freeBuffersOnPort(
1218 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1219 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1220
1221 status_t stickyErr = OK;
1222
1223 for (size_t i = buffers->size(); i-- > 0;) {
1224 BufferInfo *info = &buffers->editItemAt(i);
1225
1226 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1227 continue;
1228 }
1229
1230 CHECK_EQ(info->mOwnedByComponent, false);
1231
1232 status_t err =
1233 mOMX->free_buffer(mNode, portIndex, info->mBuffer);
1234
1235 if (err != OK) {
1236 stickyErr = err;
1237 }
1238
1239 if (info->mMediaBuffer != NULL) {
1240 info->mMediaBuffer->setObserver(NULL);
1241
1242 // Make sure nobody but us owns this buffer at this point.
1243 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1244
1245 info->mMediaBuffer->release();
1246 }
1247
1248 buffers->removeAt(i);
1249 }
1250
1251 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1252
1253 return stickyErr;
1254}
1255
1256void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001257 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001258
1259 CHECK_EQ(mState, EXECUTING);
1260 CHECK_EQ(portIndex, kPortIndexOutput);
1261 setState(RECONFIGURING);
1262
1263 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001264 if (!flushPortAsync(portIndex)) {
1265 onCmdComplete(OMX_CommandFlush, portIndex);
1266 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001267 } else {
1268 disablePortAsync(portIndex);
1269 }
1270}
1271
Andreas Huber404cc412009-08-25 14:26:05 -07001272bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001273 CHECK(mState == EXECUTING || mState == RECONFIGURING
1274 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001275
Andreas Huber4c483422009-09-02 16:05:36 -07001276 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001277 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1278 mPortBuffers[portIndex].size());
1279
Andreas Huberbe06d262009-08-14 14:37:10 -07001280 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1281 mPortStatus[portIndex] = SHUTTING_DOWN;
1282
Andreas Huber404cc412009-08-25 14:26:05 -07001283 if ((mQuirks & kRequiresFlushCompleteEmulation)
1284 && countBuffersWeOwn(mPortBuffers[portIndex])
1285 == mPortBuffers[portIndex].size()) {
1286 // No flush is necessary and this component fails to send a
1287 // flush-complete event in this case.
1288
1289 return false;
1290 }
1291
Andreas Huberbe06d262009-08-14 14:37:10 -07001292 status_t err =
1293 mOMX->send_command(mNode, OMX_CommandFlush, portIndex);
1294 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001295
1296 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001297}
1298
1299void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1300 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1301
1302 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1303 mPortStatus[portIndex] = DISABLING;
1304
1305 status_t err =
1306 mOMX->send_command(mNode, OMX_CommandPortDisable, portIndex);
1307 CHECK_EQ(err, OK);
1308
1309 freeBuffersOnPort(portIndex, true);
1310}
1311
1312void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1313 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1314
1315 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1316 mPortStatus[portIndex] = ENABLING;
1317
1318 status_t err =
1319 mOMX->send_command(mNode, OMX_CommandPortEnable, portIndex);
1320 CHECK_EQ(err, OK);
1321}
1322
1323void OMXCodec::fillOutputBuffers() {
1324 CHECK_EQ(mState, EXECUTING);
1325
1326 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1327 for (size_t i = 0; i < buffers->size(); ++i) {
1328 fillOutputBuffer(&buffers->editItemAt(i));
1329 }
1330}
1331
1332void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001333 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001334
1335 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1336 for (size_t i = 0; i < buffers->size(); ++i) {
1337 drainInputBuffer(&buffers->editItemAt(i));
1338 }
1339}
1340
1341void OMXCodec::drainInputBuffer(BufferInfo *info) {
1342 CHECK_EQ(info->mOwnedByComponent, false);
1343
1344 if (mSignalledEOS) {
1345 return;
1346 }
1347
1348 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1349 const CodecSpecificData *specific =
1350 mCodecSpecificData[mCodecSpecificDataIndex];
1351
1352 size_t size = specific->mSize;
1353
Andreas Huber4f5e6022009-08-19 09:29:34 -07001354 if (!strcasecmp("video/avc", mMIME)
1355 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001356 static const uint8_t kNALStartCode[4] =
1357 { 0x00, 0x00, 0x00, 0x01 };
1358
1359 CHECK(info->mMem->size() >= specific->mSize + 4);
1360
1361 size += 4;
1362
1363 memcpy(info->mMem->pointer(), kNALStartCode, 4);
1364 memcpy((uint8_t *)info->mMem->pointer() + 4,
1365 specific->mData, specific->mSize);
1366 } else {
1367 CHECK(info->mMem->size() >= specific->mSize);
1368 memcpy(info->mMem->pointer(), specific->mData, specific->mSize);
1369 }
1370
1371 mOMX->empty_buffer(
1372 mNode, info->mBuffer, 0, size,
1373 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1374 0);
1375
1376 info->mOwnedByComponent = true;
1377
1378 ++mCodecSpecificDataIndex;
1379 return;
1380 }
1381
1382 MediaBuffer *srcBuffer;
1383 status_t err;
1384 if (mSeekTimeUs >= 0) {
1385 MediaSource::ReadOptions options;
1386 options.setSeekTo(mSeekTimeUs);
1387 mSeekTimeUs = -1;
1388
1389 err = mSource->read(&srcBuffer, &options);
1390 } else {
1391 err = mSource->read(&srcBuffer);
1392 }
1393
1394 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
1395 OMX_TICKS timestamp = 0;
1396 size_t srcLength = 0;
1397
1398 if (err != OK) {
Andreas Huber4c483422009-09-02 16:05:36 -07001399 CODEC_LOGV("signalling end of input stream.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001400 flags |= OMX_BUFFERFLAG_EOS;
1401
1402 mSignalledEOS = true;
1403 } else {
1404 srcLength = srcBuffer->range_length();
1405
1406 if (info->mMem->size() < srcLength) {
1407 LOGE("info->mMem->size() = %d, srcLength = %d",
1408 info->mMem->size(), srcLength);
1409 }
1410 CHECK(info->mMem->size() >= srcLength);
1411 memcpy(info->mMem->pointer(),
1412 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
1413 srcLength);
1414
1415 int32_t units, scale;
1416 if (srcBuffer->meta_data()->findInt32(kKeyTimeUnits, &units)
1417 && srcBuffer->meta_data()->findInt32(kKeyTimeScale, &scale)) {
1418 timestamp = ((OMX_TICKS)units * 1000000) / scale;
1419
Andreas Huber4c483422009-09-02 16:05:36 -07001420 CODEC_LOGV("Calling empty_buffer on buffer %p (length %d)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001421 info->mBuffer, srcLength);
Andreas Huber4c483422009-09-02 16:05:36 -07001422 CODEC_LOGV("Calling empty_buffer with timestamp %lld us (%.2f secs)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001423 timestamp, timestamp / 1E6);
1424 }
1425 }
1426
1427 mOMX->empty_buffer(
1428 mNode, info->mBuffer, 0, srcLength,
1429 flags, timestamp);
1430
1431 info->mOwnedByComponent = true;
1432
1433 if (srcBuffer != NULL) {
1434 srcBuffer->release();
1435 srcBuffer = NULL;
1436 }
1437}
1438
1439void OMXCodec::fillOutputBuffer(BufferInfo *info) {
1440 CHECK_EQ(info->mOwnedByComponent, false);
1441
Andreas Huber404cc412009-08-25 14:26:05 -07001442 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07001443 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07001444 "calling fillOutputBuffer");
1445 return;
1446 }
1447
Andreas Huber4c483422009-09-02 16:05:36 -07001448 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001449 mOMX->fill_buffer(mNode, info->mBuffer);
1450
1451 info->mOwnedByComponent = true;
1452}
1453
1454void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
1455 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1456 for (size_t i = 0; i < buffers->size(); ++i) {
1457 if ((*buffers)[i].mBuffer == buffer) {
1458 drainInputBuffer(&buffers->editItemAt(i));
1459 return;
1460 }
1461 }
1462
1463 CHECK(!"should not be here.");
1464}
1465
1466void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
1467 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1468 for (size_t i = 0; i < buffers->size(); ++i) {
1469 if ((*buffers)[i].mBuffer == buffer) {
1470 fillOutputBuffer(&buffers->editItemAt(i));
1471 return;
1472 }
1473 }
1474
1475 CHECK(!"should not be here.");
1476}
1477
1478void OMXCodec::setState(State newState) {
1479 mState = newState;
1480 mAsyncCompletion.signal();
1481
1482 // This may cause some spurious wakeups but is necessary to
1483 // unblock the reader if we enter ERROR state.
1484 mBufferFilled.signal();
1485}
1486
Andreas Huberda050cf22009-09-02 14:01:43 -07001487void OMXCodec::setRawAudioFormat(
1488 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
1489 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07001490 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07001491 pcmParams.nPortIndex = portIndex;
1492
1493 status_t err = mOMX->get_parameter(
1494 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1495
1496 CHECK_EQ(err, OK);
1497
1498 pcmParams.nChannels = numChannels;
1499 pcmParams.eNumData = OMX_NumericalDataSigned;
1500 pcmParams.bInterleaved = OMX_TRUE;
1501 pcmParams.nBitPerSample = 16;
1502 pcmParams.nSamplingRate = sampleRate;
1503 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
1504
1505 if (numChannels == 1) {
1506 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
1507 } else {
1508 CHECK_EQ(numChannels, 2);
1509
1510 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
1511 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
1512 }
1513
1514 err = mOMX->set_parameter(
1515 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1516
1517 CHECK_EQ(err, OK);
1518}
1519
Andreas Huberbe06d262009-08-14 14:37:10 -07001520void OMXCodec::setAMRFormat() {
1521 if (!mIsEncoder) {
1522 OMX_AUDIO_PARAM_AMRTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001523 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001524 def.nPortIndex = kPortIndexInput;
1525
1526 status_t err =
1527 mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1528
1529 CHECK_EQ(err, OK);
1530
1531 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1532 def.eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
1533
1534 err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1535 CHECK_EQ(err, OK);
1536 }
1537
1538 ////////////////////////
1539
1540 if (mIsEncoder) {
1541 sp<MetaData> format = mSource->getFormat();
1542 int32_t sampleRate;
1543 int32_t numChannels;
1544 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1545 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1546
Andreas Huberda050cf22009-09-02 14:01:43 -07001547 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
Andreas Huberbe06d262009-08-14 14:37:10 -07001548 }
1549}
1550
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07001551void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) {
Andreas Huberda050cf22009-09-02 14:01:43 -07001552 if (mIsEncoder) {
1553 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1554 } else {
1555 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07001556 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07001557 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07001558
Andreas Huberda050cf22009-09-02 14:01:43 -07001559 status_t err = mOMX->get_parameter(
1560 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1561 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001562
Andreas Huberda050cf22009-09-02 14:01:43 -07001563 profile.nChannels = numChannels;
1564 profile.nSampleRate = sampleRate;
1565 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07001566
Andreas Huberda050cf22009-09-02 14:01:43 -07001567 err = mOMX->set_parameter(
1568 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1569 CHECK_EQ(err, OK);
1570 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001571}
1572
1573void OMXCodec::setImageOutputFormat(
1574 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07001575 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001576
1577#if 0
1578 OMX_INDEXTYPE index;
1579 status_t err = mOMX->get_extension_index(
1580 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
1581 CHECK_EQ(err, OK);
1582
1583 err = mOMX->set_config(mNode, index, &format, sizeof(format));
1584 CHECK_EQ(err, OK);
1585#endif
1586
1587 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001588 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001589 def.nPortIndex = kPortIndexOutput;
1590
1591 status_t err = mOMX->get_parameter(
1592 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1593 CHECK_EQ(err, OK);
1594
1595 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1596
1597 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001598
Andreas Huberbe06d262009-08-14 14:37:10 -07001599 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
1600 imageDef->eColorFormat = format;
1601 imageDef->nFrameWidth = width;
1602 imageDef->nFrameHeight = height;
1603
1604 switch (format) {
1605 case OMX_COLOR_FormatYUV420PackedPlanar:
1606 case OMX_COLOR_FormatYUV411Planar:
1607 {
1608 def.nBufferSize = (width * height * 3) / 2;
1609 break;
1610 }
1611
1612 case OMX_COLOR_FormatCbYCrY:
1613 {
1614 def.nBufferSize = width * height * 2;
1615 break;
1616 }
1617
1618 case OMX_COLOR_Format32bitARGB8888:
1619 {
1620 def.nBufferSize = width * height * 4;
1621 break;
1622 }
1623
1624 default:
1625 CHECK(!"Should not be here. Unknown color format.");
1626 break;
1627 }
1628
Andreas Huber5c0a9132009-08-20 11:16:40 -07001629 def.nBufferCountActual = def.nBufferCountMin;
1630
Andreas Huberbe06d262009-08-14 14:37:10 -07001631 err = mOMX->set_parameter(
1632 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1633 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07001634}
Andreas Huberbe06d262009-08-14 14:37:10 -07001635
Andreas Huber5c0a9132009-08-20 11:16:40 -07001636void OMXCodec::setJPEGInputFormat(
1637 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
1638 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001639 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001640 def.nPortIndex = kPortIndexInput;
1641
Andreas Huber5c0a9132009-08-20 11:16:40 -07001642 status_t err = mOMX->get_parameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001643 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1644 CHECK_EQ(err, OK);
1645
Andreas Huber5c0a9132009-08-20 11:16:40 -07001646 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1647 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
1648
Andreas Huberbe06d262009-08-14 14:37:10 -07001649 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
1650 imageDef->nFrameWidth = width;
1651 imageDef->nFrameHeight = height;
1652
Andreas Huber5c0a9132009-08-20 11:16:40 -07001653 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07001654 def.nBufferCountActual = def.nBufferCountMin;
1655
1656 err = mOMX->set_parameter(
1657 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1658 CHECK_EQ(err, OK);
1659}
1660
1661void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
1662 CodecSpecificData *specific =
1663 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
1664
1665 specific->mSize = size;
1666 memcpy(specific->mData, data, size);
1667
1668 mCodecSpecificData.push(specific);
1669}
1670
1671void OMXCodec::clearCodecSpecificData() {
1672 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
1673 free(mCodecSpecificData.editItemAt(i));
1674 }
1675 mCodecSpecificData.clear();
1676 mCodecSpecificDataIndex = 0;
1677}
1678
1679status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07001680 Mutex::Autolock autoLock(mLock);
1681
Andreas Huberbe06d262009-08-14 14:37:10 -07001682 if (mState != LOADED) {
1683 return UNKNOWN_ERROR;
1684 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07001685
Andreas Huberbe06d262009-08-14 14:37:10 -07001686 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07001687 if (mQuirks & kWantsNALFragments) {
1688 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07001689 }
1690 status_t err = mSource->start(params.get());
1691
1692 if (err != OK) {
1693 return err;
1694 }
1695
1696 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07001697 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001698 mSignalledEOS = false;
1699 mNoMoreOutputData = false;
1700 mSeekTimeUs = -1;
1701 mFilledBuffers.clear();
1702
1703 return init();
1704}
1705
1706status_t OMXCodec::stop() {
Andreas Huber4c483422009-09-02 16:05:36 -07001707 CODEC_LOGV("stop");
Andreas Huberbe06d262009-08-14 14:37:10 -07001708
1709 Mutex::Autolock autoLock(mLock);
1710
1711 while (isIntermediateState(mState)) {
1712 mAsyncCompletion.wait(mLock);
1713 }
1714
1715 switch (mState) {
1716 case LOADED:
1717 case ERROR:
1718 break;
1719
1720 case EXECUTING:
1721 {
1722 setState(EXECUTING_TO_IDLE);
1723
Andreas Huber127fcdc2009-08-26 16:27:02 -07001724 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07001725 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001726 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07001727
Andreas Huber127fcdc2009-08-26 16:27:02 -07001728 bool emulateInputFlushCompletion =
1729 !flushPortAsync(kPortIndexInput);
1730
1731 bool emulateOutputFlushCompletion =
1732 !flushPortAsync(kPortIndexOutput);
1733
1734 if (emulateInputFlushCompletion) {
1735 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1736 }
1737
1738 if (emulateOutputFlushCompletion) {
1739 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1740 }
1741 } else {
1742 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1743 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1744
1745 status_t err =
1746 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1747 CHECK_EQ(err, OK);
1748 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001749
1750 while (mState != LOADED && mState != ERROR) {
1751 mAsyncCompletion.wait(mLock);
1752 }
1753
1754 break;
1755 }
1756
1757 default:
1758 {
1759 CHECK(!"should not be here.");
1760 break;
1761 }
1762 }
1763
1764 mSource->stop();
1765
1766 return OK;
1767}
1768
1769sp<MetaData> OMXCodec::getFormat() {
1770 return mOutputFormat;
1771}
1772
1773status_t OMXCodec::read(
1774 MediaBuffer **buffer, const ReadOptions *options) {
1775 *buffer = NULL;
1776
1777 Mutex::Autolock autoLock(mLock);
1778
Andreas Huberd06e5b82009-08-28 13:18:14 -07001779 if (mState != EXECUTING && mState != RECONFIGURING) {
1780 return UNKNOWN_ERROR;
1781 }
1782
Andreas Huber42978e52009-08-27 10:08:39 -07001783 if (mInitialBufferSubmit) {
1784 mInitialBufferSubmit = false;
1785
1786 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07001787
Andreas Huberd06e5b82009-08-28 13:18:14 -07001788 if (mState == EXECUTING) {
1789 // Otherwise mState == RECONFIGURING and this code will trigger
1790 // after the output port is reenabled.
1791 fillOutputBuffers();
1792 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001793 }
1794
1795 int64_t seekTimeUs;
1796 if (options && options->getSeekTo(&seekTimeUs)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001797 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07001798
1799 mSignalledEOS = false;
1800 mNoMoreOutputData = false;
1801
1802 CHECK(seekTimeUs >= 0);
1803 mSeekTimeUs = seekTimeUs;
1804
1805 mFilledBuffers.clear();
1806
1807 CHECK_EQ(mState, EXECUTING);
1808
Andreas Huber404cc412009-08-25 14:26:05 -07001809 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
1810 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
1811
1812 if (emulateInputFlushCompletion) {
1813 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1814 }
1815
1816 if (emulateOutputFlushCompletion) {
1817 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1818 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001819 }
1820
1821 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
1822 mBufferFilled.wait(mLock);
1823 }
1824
1825 if (mState == ERROR) {
1826 return UNKNOWN_ERROR;
1827 }
1828
1829 if (mFilledBuffers.empty()) {
1830 return ERROR_END_OF_STREAM;
1831 }
1832
1833 size_t index = *mFilledBuffers.begin();
1834 mFilledBuffers.erase(mFilledBuffers.begin());
1835
1836 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
1837 info->mMediaBuffer->add_ref();
1838 *buffer = info->mMediaBuffer;
1839
1840 return OK;
1841}
1842
1843void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
1844 Mutex::Autolock autoLock(mLock);
1845
1846 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1847 for (size_t i = 0; i < buffers->size(); ++i) {
1848 BufferInfo *info = &buffers->editItemAt(i);
1849
1850 if (info->mMediaBuffer == buffer) {
1851 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
1852 fillOutputBuffer(info);
1853 return;
1854 }
1855 }
1856
1857 CHECK(!"should not be here.");
1858}
1859
1860static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
1861 static const char *kNames[] = {
1862 "OMX_IMAGE_CodingUnused",
1863 "OMX_IMAGE_CodingAutoDetect",
1864 "OMX_IMAGE_CodingJPEG",
1865 "OMX_IMAGE_CodingJPEG2K",
1866 "OMX_IMAGE_CodingEXIF",
1867 "OMX_IMAGE_CodingTIFF",
1868 "OMX_IMAGE_CodingGIF",
1869 "OMX_IMAGE_CodingPNG",
1870 "OMX_IMAGE_CodingLZW",
1871 "OMX_IMAGE_CodingBMP",
1872 };
1873
1874 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
1875
1876 if (type < 0 || (size_t)type >= numNames) {
1877 return "UNKNOWN";
1878 } else {
1879 return kNames[type];
1880 }
1881}
1882
1883static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
1884 static const char *kNames[] = {
1885 "OMX_COLOR_FormatUnused",
1886 "OMX_COLOR_FormatMonochrome",
1887 "OMX_COLOR_Format8bitRGB332",
1888 "OMX_COLOR_Format12bitRGB444",
1889 "OMX_COLOR_Format16bitARGB4444",
1890 "OMX_COLOR_Format16bitARGB1555",
1891 "OMX_COLOR_Format16bitRGB565",
1892 "OMX_COLOR_Format16bitBGR565",
1893 "OMX_COLOR_Format18bitRGB666",
1894 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07001895 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07001896 "OMX_COLOR_Format24bitRGB888",
1897 "OMX_COLOR_Format24bitBGR888",
1898 "OMX_COLOR_Format24bitARGB1887",
1899 "OMX_COLOR_Format25bitARGB1888",
1900 "OMX_COLOR_Format32bitBGRA8888",
1901 "OMX_COLOR_Format32bitARGB8888",
1902 "OMX_COLOR_FormatYUV411Planar",
1903 "OMX_COLOR_FormatYUV411PackedPlanar",
1904 "OMX_COLOR_FormatYUV420Planar",
1905 "OMX_COLOR_FormatYUV420PackedPlanar",
1906 "OMX_COLOR_FormatYUV420SemiPlanar",
1907 "OMX_COLOR_FormatYUV422Planar",
1908 "OMX_COLOR_FormatYUV422PackedPlanar",
1909 "OMX_COLOR_FormatYUV422SemiPlanar",
1910 "OMX_COLOR_FormatYCbYCr",
1911 "OMX_COLOR_FormatYCrYCb",
1912 "OMX_COLOR_FormatCbYCrY",
1913 "OMX_COLOR_FormatCrYCbY",
1914 "OMX_COLOR_FormatYUV444Interleaved",
1915 "OMX_COLOR_FormatRawBayer8bit",
1916 "OMX_COLOR_FormatRawBayer10bit",
1917 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07001918 "OMX_COLOR_FormatL2",
1919 "OMX_COLOR_FormatL4",
1920 "OMX_COLOR_FormatL8",
1921 "OMX_COLOR_FormatL16",
1922 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07001923 "OMX_COLOR_FormatL32",
1924 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
1925 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
1926 "OMX_COLOR_Format18BitBGR666",
1927 "OMX_COLOR_Format24BitARGB6666",
1928 "OMX_COLOR_Format24BitABGR6666",
1929 };
1930
1931 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
1932
1933 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1934
1935 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
1936 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
1937 } else if (type < 0 || (size_t)type >= numNames) {
1938 return "UNKNOWN";
1939 } else {
1940 return kNames[type];
1941 }
1942}
1943
1944static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
1945 static const char *kNames[] = {
1946 "OMX_VIDEO_CodingUnused",
1947 "OMX_VIDEO_CodingAutoDetect",
1948 "OMX_VIDEO_CodingMPEG2",
1949 "OMX_VIDEO_CodingH263",
1950 "OMX_VIDEO_CodingMPEG4",
1951 "OMX_VIDEO_CodingWMV",
1952 "OMX_VIDEO_CodingRV",
1953 "OMX_VIDEO_CodingAVC",
1954 "OMX_VIDEO_CodingMJPEG",
1955 };
1956
1957 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
1958
1959 if (type < 0 || (size_t)type >= numNames) {
1960 return "UNKNOWN";
1961 } else {
1962 return kNames[type];
1963 }
1964}
1965
1966static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
1967 static const char *kNames[] = {
1968 "OMX_AUDIO_CodingUnused",
1969 "OMX_AUDIO_CodingAutoDetect",
1970 "OMX_AUDIO_CodingPCM",
1971 "OMX_AUDIO_CodingADPCM",
1972 "OMX_AUDIO_CodingAMR",
1973 "OMX_AUDIO_CodingGSMFR",
1974 "OMX_AUDIO_CodingGSMEFR",
1975 "OMX_AUDIO_CodingGSMHR",
1976 "OMX_AUDIO_CodingPDCFR",
1977 "OMX_AUDIO_CodingPDCEFR",
1978 "OMX_AUDIO_CodingPDCHR",
1979 "OMX_AUDIO_CodingTDMAFR",
1980 "OMX_AUDIO_CodingTDMAEFR",
1981 "OMX_AUDIO_CodingQCELP8",
1982 "OMX_AUDIO_CodingQCELP13",
1983 "OMX_AUDIO_CodingEVRC",
1984 "OMX_AUDIO_CodingSMV",
1985 "OMX_AUDIO_CodingG711",
1986 "OMX_AUDIO_CodingG723",
1987 "OMX_AUDIO_CodingG726",
1988 "OMX_AUDIO_CodingG729",
1989 "OMX_AUDIO_CodingAAC",
1990 "OMX_AUDIO_CodingMP3",
1991 "OMX_AUDIO_CodingSBC",
1992 "OMX_AUDIO_CodingVORBIS",
1993 "OMX_AUDIO_CodingWMA",
1994 "OMX_AUDIO_CodingRA",
1995 "OMX_AUDIO_CodingMIDI",
1996 };
1997
1998 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
1999
2000 if (type < 0 || (size_t)type >= numNames) {
2001 return "UNKNOWN";
2002 } else {
2003 return kNames[type];
2004 }
2005}
2006
2007static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2008 static const char *kNames[] = {
2009 "OMX_AUDIO_PCMModeLinear",
2010 "OMX_AUDIO_PCMModeALaw",
2011 "OMX_AUDIO_PCMModeMULaw",
2012 };
2013
2014 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2015
2016 if (type < 0 || (size_t)type >= numNames) {
2017 return "UNKNOWN";
2018 } else {
2019 return kNames[type];
2020 }
2021}
2022
2023
2024void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2025 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002026 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002027 def.nPortIndex = portIndex;
2028
2029 status_t err = mOMX->get_parameter(
2030 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2031 CHECK_EQ(err, OK);
2032
2033 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2034
2035 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2036 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2037
2038 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2039 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2040 printf(" nBufferSize = %ld\n", def.nBufferSize);
2041
2042 switch (def.eDomain) {
2043 case OMX_PortDomainImage:
2044 {
2045 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2046
2047 printf("\n");
2048 printf(" // Image\n");
2049 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2050 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2051 printf(" nStride = %ld\n", imageDef->nStride);
2052
2053 printf(" eCompressionFormat = %s\n",
2054 imageCompressionFormatString(imageDef->eCompressionFormat));
2055
2056 printf(" eColorFormat = %s\n",
2057 colorFormatString(imageDef->eColorFormat));
2058
2059 break;
2060 }
2061
2062 case OMX_PortDomainVideo:
2063 {
2064 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2065
2066 printf("\n");
2067 printf(" // Video\n");
2068 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2069 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2070 printf(" nStride = %ld\n", videoDef->nStride);
2071
2072 printf(" eCompressionFormat = %s\n",
2073 videoCompressionFormatString(videoDef->eCompressionFormat));
2074
2075 printf(" eColorFormat = %s\n",
2076 colorFormatString(videoDef->eColorFormat));
2077
2078 break;
2079 }
2080
2081 case OMX_PortDomainAudio:
2082 {
2083 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2084
2085 printf("\n");
2086 printf(" // Audio\n");
2087 printf(" eEncoding = %s\n",
2088 audioCodingTypeString(audioDef->eEncoding));
2089
2090 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2091 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002092 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002093 params.nPortIndex = portIndex;
2094
2095 err = mOMX->get_parameter(
2096 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2097 CHECK_EQ(err, OK);
2098
2099 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
2100 printf(" nChannels = %ld\n", params.nChannels);
2101 printf(" bInterleaved = %d\n", params.bInterleaved);
2102 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
2103
2104 printf(" eNumData = %s\n",
2105 params.eNumData == OMX_NumericalDataSigned
2106 ? "signed" : "unsigned");
2107
2108 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
2109 }
2110
2111 break;
2112 }
2113
2114 default:
2115 {
2116 printf(" // Unknown\n");
2117 break;
2118 }
2119 }
2120
2121 printf("}\n");
2122}
2123
2124void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
2125 mOutputFormat = new MetaData;
2126 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
2127
2128 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002129 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002130 def.nPortIndex = kPortIndexOutput;
2131
2132 status_t err = mOMX->get_parameter(
2133 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2134 CHECK_EQ(err, OK);
2135
2136 switch (def.eDomain) {
2137 case OMX_PortDomainImage:
2138 {
2139 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2140 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2141
2142 mOutputFormat->setCString(kKeyMIMEType, "image/raw");
2143 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
2144 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
2145 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
2146 break;
2147 }
2148
2149 case OMX_PortDomainAudio:
2150 {
2151 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
2152
Andreas Huberda050cf22009-09-02 14:01:43 -07002153 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
2154 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002155 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07002156 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002157
Andreas Huberda050cf22009-09-02 14:01:43 -07002158 err = mOMX->get_parameter(
2159 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2160 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002161
Andreas Huberda050cf22009-09-02 14:01:43 -07002162 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
2163 CHECK_EQ(params.nBitPerSample, 16);
2164 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07002165
Andreas Huberda050cf22009-09-02 14:01:43 -07002166 int32_t numChannels, sampleRate;
2167 inputFormat->findInt32(kKeyChannelCount, &numChannels);
2168 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07002169
Andreas Huberda050cf22009-09-02 14:01:43 -07002170 if ((OMX_U32)numChannels != params.nChannels) {
2171 LOGW("Codec outputs a different number of channels than "
2172 "the input stream contains.");
2173 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002174
Andreas Huberda050cf22009-09-02 14:01:43 -07002175 mOutputFormat->setCString(kKeyMIMEType, "audio/raw");
2176
2177 // Use the codec-advertised number of channels, as some
2178 // codecs appear to output stereo even if the input data is
2179 // mono.
2180 mOutputFormat->setInt32(kKeyChannelCount, params.nChannels);
2181
2182 // The codec-reported sampleRate is not reliable...
2183 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
2184 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
2185 mOutputFormat->setCString(kKeyMIMEType, "audio/3gpp");
2186 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
2187 mOutputFormat->setCString(kKeyMIMEType, "audio/mp4a-latm");
2188 } else {
2189 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07002190 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002191 break;
2192 }
2193
2194 case OMX_PortDomainVideo:
2195 {
2196 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
2197
2198 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
2199 mOutputFormat->setCString(kKeyMIMEType, "video/raw");
2200 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
2201 mOutputFormat->setCString(kKeyMIMEType, "video/mp4v-es");
2202 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
2203 mOutputFormat->setCString(kKeyMIMEType, "video/3gpp");
2204 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
2205 mOutputFormat->setCString(kKeyMIMEType, "video/avc");
2206 } else {
2207 CHECK(!"Unknown compression format.");
2208 }
2209
2210 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
2211 // This component appears to be lying to me.
2212 mOutputFormat->setInt32(
2213 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
2214 mOutputFormat->setInt32(
2215 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
2216 } else {
2217 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
2218 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
2219 }
2220
2221 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
2222 break;
2223 }
2224
2225 default:
2226 {
2227 CHECK(!"should not be here, neither audio nor video.");
2228 break;
2229 }
2230 }
2231}
2232
2233} // namespace android