blob: 5201c5a679049f399803cf661f0a7cc65be8e430 [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
Andreas Huberbd7b43b2009-10-13 10:22:55 -070021#include "include/ESDS.h"
22
Andreas Huberbe06d262009-08-14 14:37:10 -070023#include <binder/IServiceManager.h>
24#include <binder/MemoryDealer.h>
25#include <binder/ProcessState.h>
26#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070027#include <media/stagefright/MediaBuffer.h>
28#include <media/stagefright/MediaBufferGroup.h>
29#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070030#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070031#include <media/stagefright/MediaExtractor.h>
32#include <media/stagefright/MetaData.h>
33#include <media/stagefright/MmapSource.h>
34#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070035#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070036#include <utils/Vector.h>
37
38#include <OMX_Audio.h>
39#include <OMX_Component.h>
40
41namespace android {
42
Andreas Huber8b432b12009-10-07 13:36:52 -070043static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
44
Andreas Huberbe06d262009-08-14 14:37:10 -070045struct CodecInfo {
46 const char *mime;
47 const char *codec;
48};
49
50static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -070051 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
52 { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
53 { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
54 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
55 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
56 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
57 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
58 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
59 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
60 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
61 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
62 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
63 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
64 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.Decoder" },
65 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
66 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
67 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
68 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huberbe06d262009-08-14 14:37:10 -070069};
70
71static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -070072 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
73 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrencnb" },
74 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
75 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
76 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
77 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
78 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
79 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
80 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
81 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
82 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
83 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
84 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -070085};
86
Andreas Hubere0873732009-09-10 09:57:53 -070087#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -070088#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
89
Andreas Huberbe06d262009-08-14 14:37:10 -070090struct OMXCodecObserver : public BnOMXObserver {
91 OMXCodecObserver(const wp<OMXCodec> &target)
92 : mTarget(target) {
93 }
94
95 // from IOMXObserver
96 virtual void on_message(const omx_message &msg) {
97 sp<OMXCodec> codec = mTarget.promote();
98
99 if (codec.get() != NULL) {
100 codec->on_message(msg);
101 }
102 }
103
104protected:
105 virtual ~OMXCodecObserver() {}
106
107private:
108 wp<OMXCodec> mTarget;
109
110 OMXCodecObserver(const OMXCodecObserver &);
111 OMXCodecObserver &operator=(const OMXCodecObserver &);
112};
113
114static const char *GetCodec(const CodecInfo *info, size_t numInfos,
115 const char *mime, int index) {
116 CHECK(index >= 0);
117 for(size_t i = 0; i < numInfos; ++i) {
118 if (!strcasecmp(mime, info[i].mime)) {
119 if (index == 0) {
120 return info[i].codec;
121 }
122
123 --index;
124 }
125 }
126
127 return NULL;
128}
129
Andreas Huberebf66ea2009-08-19 13:32:58 -0700130enum {
131 kAVCProfileBaseline = 0x42,
132 kAVCProfileMain = 0x4d,
133 kAVCProfileExtended = 0x58,
134 kAVCProfileHigh = 0x64,
135 kAVCProfileHigh10 = 0x6e,
136 kAVCProfileHigh422 = 0x7a,
137 kAVCProfileHigh444 = 0xf4,
138 kAVCProfileCAVLC444Intra = 0x2c
139};
140
141static const char *AVCProfileToString(uint8_t profile) {
142 switch (profile) {
143 case kAVCProfileBaseline:
144 return "Baseline";
145 case kAVCProfileMain:
146 return "Main";
147 case kAVCProfileExtended:
148 return "Extended";
149 case kAVCProfileHigh:
150 return "High";
151 case kAVCProfileHigh10:
152 return "High 10";
153 case kAVCProfileHigh422:
154 return "High 422";
155 case kAVCProfileHigh444:
156 return "High 444";
157 case kAVCProfileCAVLC444Intra:
158 return "CAVLC 444 Intra";
159 default: return "Unknown";
160 }
161}
162
Andreas Huber4c483422009-09-02 16:05:36 -0700163template<class T>
164static void InitOMXParams(T *params) {
165 params->nSize = sizeof(T);
166 params->nVersion.s.nVersionMajor = 1;
167 params->nVersion.s.nVersionMinor = 0;
168 params->nVersion.s.nRevision = 0;
169 params->nVersion.s.nStep = 0;
170}
171
Andreas Huberbe06d262009-08-14 14:37:10 -0700172// static
173sp<OMXCodec> OMXCodec::Create(
174 const sp<IOMX> &omx,
175 const sp<MetaData> &meta, bool createEncoder,
Andreas Hubere6c40962009-09-10 14:13:30 -0700176 const sp<MediaSource> &source,
177 const char *matchComponentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700178 const char *mime;
179 bool success = meta->findCString(kKeyMIMEType, &mime);
180 CHECK(success);
181
182 const char *componentName = NULL;
183 IOMX::node_id node = 0;
184 for (int index = 0;; ++index) {
185 if (createEncoder) {
186 componentName = GetCodec(
187 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
188 mime, index);
189 } else {
190 componentName = GetCodec(
191 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
192 mime, index);
193 }
194
195 if (!componentName) {
196 return NULL;
197 }
198
Andreas Hubere6c40962009-09-10 14:13:30 -0700199 // If a specific codec is requested, skip the non-matching ones.
200 if (matchComponentName && strcmp(componentName, matchComponentName)) {
201 continue;
202 }
203
Andreas Huberbe06d262009-08-14 14:37:10 -0700204 LOGV("Attempting to allocate OMX node '%s'", componentName);
205
206 status_t err = omx->allocate_node(componentName, &node);
207 if (err == OK) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700208 LOGV("Successfully allocated OMX node '%s'", componentName);
Andreas Huberbe06d262009-08-14 14:37:10 -0700209 break;
210 }
211 }
212
213 uint32_t quirks = 0;
214 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700215 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700216 }
217 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
218 quirks |= kNeedsFlushBeforeDisable;
219 }
220 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
221 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700222 quirks |= kRequiresFlushCompleteEmulation;
Andreas Huberbe06d262009-08-14 14:37:10 -0700223 }
224 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
225 quirks |= kRequiresLoadedToIdleAfterAllocation;
226 quirks |= kRequiresAllocateBufferOnInputPorts;
227 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700228 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
229 // XXX Required on P....on only.
Andreas Huber0446b822009-09-15 15:50:31 -0700230 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700231 quirks |= kRequiresAllocateBufferOnOutputPorts;
232 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700233
Andreas Huber2dc64d82009-09-11 12:58:53 -0700234 if (!strncmp(componentName, "OMX.TI.", 7)) {
235 // Apparently I must not use OMX_UseBuffer on either input or
236 // output ports on any of the TI components or quote:
237 // "(I) may have unexpected problem (sic) which can be timing related
238 // and hard to reproduce."
239
240 quirks |= kRequiresAllocateBufferOnInputPorts;
241 quirks |= kRequiresAllocateBufferOnOutputPorts;
242 }
243
Andreas Huberbe06d262009-08-14 14:37:10 -0700244 sp<OMXCodec> codec = new OMXCodec(
245 omx, node, quirks, createEncoder, mime, componentName,
246 source);
247
248 uint32_t type;
249 const void *data;
250 size_t size;
251 if (meta->findData(kKeyESDS, &type, &data, &size)) {
252 ESDS esds((const char *)data, size);
253 CHECK_EQ(esds.InitCheck(), OK);
254
255 const void *codec_specific_data;
256 size_t codec_specific_data_size;
257 esds.getCodecSpecificInfo(
258 &codec_specific_data, &codec_specific_data_size);
259
260 printf("found codec-specific data of size %d\n",
261 codec_specific_data_size);
262
263 codec->addCodecSpecificData(
264 codec_specific_data, codec_specific_data_size);
265 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
266 printf("found avcc of size %d\n", size);
267
Andreas Huberebf66ea2009-08-19 13:32:58 -0700268 // Parse the AVCDecoderConfigurationRecord
269
270 const uint8_t *ptr = (const uint8_t *)data;
271
272 CHECK(size >= 7);
273 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
274 uint8_t profile = ptr[1];
275 uint8_t level = ptr[3];
276
277 CHECK((ptr[4] >> 2) == 0x3f); // reserved
278
279 size_t lengthSize = 1 + (ptr[4] & 3);
280
281 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
282 // violates it...
283 // CHECK((ptr[5] >> 5) == 7); // reserved
284
285 size_t numSeqParameterSets = ptr[5] & 31;
286
287 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700288 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700289
290 for (size_t i = 0; i < numSeqParameterSets; ++i) {
291 CHECK(size >= 2);
292 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700293
294 ptr += 2;
295 size -= 2;
296
Andreas Huberbe06d262009-08-14 14:37:10 -0700297 CHECK(size >= length);
298
299 codec->addCodecSpecificData(ptr, length);
300
301 ptr += length;
302 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700303 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700304
Andreas Huberebf66ea2009-08-19 13:32:58 -0700305 CHECK(size >= 1);
306 size_t numPictureParameterSets = *ptr;
307 ++ptr;
308 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700309
Andreas Huberebf66ea2009-08-19 13:32:58 -0700310 for (size_t i = 0; i < numPictureParameterSets; ++i) {
311 CHECK(size >= 2);
312 size_t length = U16_AT(ptr);
313
314 ptr += 2;
315 size -= 2;
316
317 CHECK(size >= length);
318
319 codec->addCodecSpecificData(ptr, length);
320
321 ptr += length;
322 size -= length;
323 }
324
Andreas Huber53a76bd2009-10-06 16:20:44 -0700325 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700326 (int)profile, AVCProfileToString(profile), (int)level / 10);
327
Andreas Huber53a76bd2009-10-06 16:20:44 -0700328#if 0
Andreas Huberebf66ea2009-08-19 13:32:58 -0700329 if (!strcmp(componentName, "OMX.TI.Video.Decoder")
330 && (profile != kAVCProfileBaseline || level > 39)) {
331 // This stream exceeds the decoder's capabilities.
332
333 LOGE("Profile and/or level exceed the decoder's capabilities.");
334 return NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -0700335 }
Andreas Huber53a76bd2009-10-06 16:20:44 -0700336#endif
Andreas Huberbe06d262009-08-14 14:37:10 -0700337 }
338
Andreas Hubere6c40962009-09-10 14:13:30 -0700339 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700340 codec->setAMRFormat();
341 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700342 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
Andreas Huberee606e62009-09-08 10:19:21 -0700343 codec->setAMRWBFormat();
344 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700345 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700346 int32_t numChannels, sampleRate;
347 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
348 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
349
350 codec->setAACFormat(numChannels, sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700351 }
352 if (!strncasecmp(mime, "video/", 6)) {
353 int32_t width, height;
354 bool success = meta->findInt32(kKeyWidth, &width);
355 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700356 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700357
358 if (createEncoder) {
359 codec->setVideoInputFormat(mime, width, height);
360 } else {
361 codec->setVideoOutputFormat(mime, width, height);
362 }
363 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700364 if (!strcasecmp(mime, MEDIA_MIMETYPE_IMAGE_JPEG)
Andreas Huberbe06d262009-08-14 14:37:10 -0700365 && !strcmp(componentName, "OMX.TI.JPEG.decode")) {
366 OMX_COLOR_FORMATTYPE format =
367 OMX_COLOR_Format32bitARGB8888;
368 // OMX_COLOR_FormatYUV420PackedPlanar;
369 // OMX_COLOR_FormatCbYCrY;
370 // OMX_COLOR_FormatYUV411Planar;
371
372 int32_t width, height;
373 bool success = meta->findInt32(kKeyWidth, &width);
374 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700375
376 int32_t compressedSize;
377 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700378 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700379
380 CHECK(success);
381 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700382
383 codec->setImageOutputFormat(format, width, height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700384 codec->setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700385 }
386
Andreas Huberda050cf22009-09-02 14:01:43 -0700387 int32_t maxInputSize;
388 if (createEncoder && meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
389 codec->setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
390 }
391
392 if (!strcmp(componentName, "OMX.TI.AMR.encode")
393 || !strcmp(componentName, "OMX.TI.WBAMR.encode")) {
394 codec->setMinBufferSize(kPortIndexOutput, 8192); // XXX
395 }
396
Andreas Huberbe06d262009-08-14 14:37:10 -0700397 codec->initOutputFormat(meta);
398
399 return codec;
400}
401
Andreas Huberda050cf22009-09-02 14:01:43 -0700402void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
403 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700404 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700405 def.nPortIndex = portIndex;
406
407 status_t err = mOMX->get_parameter(
408 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
409 CHECK_EQ(err, OK);
410
411 if (def.nBufferSize < size) {
412 def.nBufferSize = size;
413
414 }
415
416 err = mOMX->set_parameter(
417 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
418 CHECK_EQ(err, OK);
419}
420
Andreas Huberbe06d262009-08-14 14:37:10 -0700421status_t OMXCodec::setVideoPortFormatType(
422 OMX_U32 portIndex,
423 OMX_VIDEO_CODINGTYPE compressionFormat,
424 OMX_COLOR_FORMATTYPE colorFormat) {
425 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700426 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700427 format.nPortIndex = portIndex;
428 format.nIndex = 0;
429 bool found = false;
430
431 OMX_U32 index = 0;
432 for (;;) {
433 format.nIndex = index;
434 status_t err = mOMX->get_parameter(
435 mNode, OMX_IndexParamVideoPortFormat,
436 &format, sizeof(format));
437
438 if (err != OK) {
439 return err;
440 }
441
442 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700443 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700444
445#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700446 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700447 portIndex,
448 index, format.eCompressionFormat, format.eColorFormat);
449#endif
450
451 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
452 if (portIndex == kPortIndexInput
453 && colorFormat == format.eColorFormat) {
454 // eCompressionFormat does not seem right.
455 found = true;
456 break;
457 }
458 if (portIndex == kPortIndexOutput
459 && compressionFormat == format.eCompressionFormat) {
460 // eColorFormat does not seem right.
461 found = true;
462 break;
463 }
464 }
465
466 if (format.eCompressionFormat == compressionFormat
467 && format.eColorFormat == colorFormat) {
468 found = true;
469 break;
470 }
471
472 ++index;
473 }
474
475 if (!found) {
476 return UNKNOWN_ERROR;
477 }
478
Andreas Huber53a76bd2009-10-06 16:20:44 -0700479 CODEC_LOGV("found a match.");
Andreas Huberbe06d262009-08-14 14:37:10 -0700480 status_t err = mOMX->set_parameter(
481 mNode, OMX_IndexParamVideoPortFormat,
482 &format, sizeof(format));
483
484 return err;
485}
486
487void OMXCodec::setVideoInputFormat(
488 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700489 CODEC_LOGV("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700490
491 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700492 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700493 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700494 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700495 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700496 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700497 compressionFormat = OMX_VIDEO_CodingH263;
498 } else {
499 LOGE("Not a supported video mime type: %s", mime);
500 CHECK(!"Should not be here. Not a supported video mime type.");
501 }
502
503 OMX_COLOR_FORMATTYPE colorFormat =
504 0 ? OMX_COLOR_FormatYCbYCr : OMX_COLOR_FormatCbYCrY;
505
506 if (!strncmp("OMX.qcom.video.encoder.", mComponentName, 23)) {
507 colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
508 }
509
510 setVideoPortFormatType(
511 kPortIndexInput, OMX_VIDEO_CodingUnused,
512 colorFormat);
513
514 setVideoPortFormatType(
515 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused);
516
517 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700518 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700519 def.nPortIndex = kPortIndexOutput;
520
Andreas Huber4c483422009-09-02 16:05:36 -0700521 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
522
Andreas Huberbe06d262009-08-14 14:37:10 -0700523 status_t err = mOMX->get_parameter(
524 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
525
526 CHECK_EQ(err, OK);
527 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
528
529 video_def->nFrameWidth = width;
530 video_def->nFrameHeight = height;
531
532 video_def->eCompressionFormat = compressionFormat;
533 video_def->eColorFormat = OMX_COLOR_FormatUnused;
534
535 err = mOMX->set_parameter(
536 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
537 CHECK_EQ(err, OK);
538
539 ////////////////////////////////////////////////////////////////////////////
540
Andreas Huber4c483422009-09-02 16:05:36 -0700541 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700542 def.nPortIndex = kPortIndexInput;
543
544 err = mOMX->get_parameter(
545 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
546 CHECK_EQ(err, OK);
547
548 def.nBufferSize = (width * height * 2); // (width * height * 3) / 2;
Andreas Huber53a76bd2009-10-06 16:20:44 -0700549 CODEC_LOGV("Setting nBufferSize = %ld", def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700550
551 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
552
553 video_def->nFrameWidth = width;
554 video_def->nFrameHeight = height;
555 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
556 video_def->eColorFormat = colorFormat;
557
558 err = mOMX->set_parameter(
559 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
560 CHECK_EQ(err, OK);
561}
562
563void OMXCodec::setVideoOutputFormat(
564 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700565 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700566
Andreas Huberbe06d262009-08-14 14:37:10 -0700567 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700568 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700569 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700570 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700571 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700572 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700573 compressionFormat = OMX_VIDEO_CodingH263;
574 } else {
575 LOGE("Not a supported video mime type: %s", mime);
576 CHECK(!"Should not be here. Not a supported video mime type.");
577 }
578
579 setVideoPortFormatType(
580 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
581
582#if 1
583 {
584 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700585 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700586 format.nPortIndex = kPortIndexOutput;
587 format.nIndex = 0;
588
589 status_t err = mOMX->get_parameter(
590 mNode, OMX_IndexParamVideoPortFormat,
591 &format, sizeof(format));
592 CHECK_EQ(err, OK);
593 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
594
595 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
596
597 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
598 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
599 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
600 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
601
602 err = mOMX->set_parameter(
603 mNode, OMX_IndexParamVideoPortFormat,
604 &format, sizeof(format));
605 CHECK_EQ(err, OK);
606 }
607#endif
608
609 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700610 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700611 def.nPortIndex = kPortIndexInput;
612
Andreas Huber4c483422009-09-02 16:05:36 -0700613 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
614
Andreas Huberbe06d262009-08-14 14:37:10 -0700615 status_t err = mOMX->get_parameter(
616 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
617
618 CHECK_EQ(err, OK);
619
620#if 1
621 // XXX Need a (much) better heuristic to compute input buffer sizes.
622 const size_t X = 64 * 1024;
623 if (def.nBufferSize < X) {
624 def.nBufferSize = X;
625 }
626#endif
627
628 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
629
630 video_def->nFrameWidth = width;
631 video_def->nFrameHeight = height;
632
633 video_def->eColorFormat = OMX_COLOR_FormatUnused;
634
635 err = mOMX->set_parameter(
636 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
637 CHECK_EQ(err, OK);
638
639 ////////////////////////////////////////////////////////////////////////////
640
Andreas Huber4c483422009-09-02 16:05:36 -0700641 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700642 def.nPortIndex = kPortIndexOutput;
643
644 err = mOMX->get_parameter(
645 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
646 CHECK_EQ(err, OK);
647 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
648
649#if 0
650 def.nBufferSize =
651 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
652#endif
653
654 video_def->nFrameWidth = width;
655 video_def->nFrameHeight = height;
656
657 err = mOMX->set_parameter(
658 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
659 CHECK_EQ(err, OK);
660}
661
662
663OMXCodec::OMXCodec(
664 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -0700665 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -0700666 const char *mime,
667 const char *componentName,
668 const sp<MediaSource> &source)
669 : mOMX(omx),
670 mNode(node),
671 mQuirks(quirks),
672 mIsEncoder(isEncoder),
673 mMIME(strdup(mime)),
674 mComponentName(strdup(componentName)),
675 mSource(source),
676 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -0700677 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -0700678 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -0700679 mSignalledEOS(false),
680 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -0700681 mOutputPortSettingsHaveChanged(false),
Andreas Huberbe06d262009-08-14 14:37:10 -0700682 mSeekTimeUs(-1) {
683 mPortStatus[kPortIndexInput] = ENABLED;
684 mPortStatus[kPortIndexOutput] = ENABLED;
685
686 mObserver = new OMXCodecObserver(this);
687 mOMX->observe_node(mNode, mObserver);
Andreas Huber4c483422009-09-02 16:05:36 -0700688
689 setComponentRole();
690}
691
Andreas Hubere6c40962009-09-10 14:13:30 -0700692// static
693void OMXCodec::setComponentRole(
694 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
695 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -0700696 struct MimeToRole {
697 const char *mime;
698 const char *decoderRole;
699 const char *encoderRole;
700 };
701
702 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700703 { MEDIA_MIMETYPE_AUDIO_MPEG,
704 "audio_decoder.mp3", "audio_encoder.mp3" },
705 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
706 "audio_decoder.amrnb", "audio_encoder.amrnb" },
707 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
708 "audio_decoder.amrwb", "audio_encoder.amrwb" },
709 { MEDIA_MIMETYPE_AUDIO_AAC,
710 "audio_decoder.aac", "audio_encoder.aac" },
711 { MEDIA_MIMETYPE_VIDEO_AVC,
712 "video_decoder.avc", "video_encoder.avc" },
713 { MEDIA_MIMETYPE_VIDEO_MPEG4,
714 "video_decoder.mpeg4", "video_encoder.mpeg4" },
715 { MEDIA_MIMETYPE_VIDEO_H263,
716 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -0700717 };
718
719 static const size_t kNumMimeToRole =
720 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
721
722 size_t i;
723 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -0700724 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -0700725 break;
726 }
727 }
728
729 if (i == kNumMimeToRole) {
730 return;
731 }
732
733 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -0700734 isEncoder ? kMimeToRole[i].encoderRole
735 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -0700736
737 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -0700738 OMX_PARAM_COMPONENTROLETYPE roleParams;
739 InitOMXParams(&roleParams);
740
741 strncpy((char *)roleParams.cRole,
742 role, OMX_MAX_STRINGNAME_SIZE - 1);
743
744 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
745
Andreas Hubere6c40962009-09-10 14:13:30 -0700746 status_t err = omx->set_parameter(
747 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -0700748 &roleParams, sizeof(roleParams));
749
750 if (err != OK) {
751 LOGW("Failed to set standard component role '%s'.", role);
752 }
753 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700754}
755
Andreas Hubere6c40962009-09-10 14:13:30 -0700756void OMXCodec::setComponentRole() {
757 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
758}
759
Andreas Huberbe06d262009-08-14 14:37:10 -0700760OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700761 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -0700762
763 status_t err = mOMX->observe_node(mNode, NULL);
764 CHECK_EQ(err, OK);
765
766 err = mOMX->free_node(mNode);
767 CHECK_EQ(err, OK);
768
769 mNode = NULL;
770 setState(DEAD);
771
772 clearCodecSpecificData();
773
774 free(mComponentName);
775 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700776
Andreas Huberbe06d262009-08-14 14:37:10 -0700777 free(mMIME);
778 mMIME = NULL;
779}
780
781status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -0700782 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -0700783
784 CHECK_EQ(mState, LOADED);
785
786 status_t err;
787 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
788 err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
789 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700790 setState(LOADED_TO_IDLE);
791 }
792
793 err = allocateBuffers();
794 CHECK_EQ(err, OK);
795
796 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
797 err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
798 CHECK_EQ(err, OK);
799
800 setState(LOADED_TO_IDLE);
801 }
802
803 while (mState != EXECUTING && mState != ERROR) {
804 mAsyncCompletion.wait(mLock);
805 }
806
807 return mState == ERROR ? UNKNOWN_ERROR : OK;
808}
809
810// static
811bool OMXCodec::isIntermediateState(State state) {
812 return state == LOADED_TO_IDLE
813 || state == IDLE_TO_EXECUTING
814 || state == EXECUTING_TO_IDLE
815 || state == IDLE_TO_LOADED
816 || state == RECONFIGURING;
817}
818
819status_t OMXCodec::allocateBuffers() {
820 status_t err = allocateBuffersOnPort(kPortIndexInput);
821
822 if (err != OK) {
823 return err;
824 }
825
826 return allocateBuffersOnPort(kPortIndexOutput);
827}
828
829status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
830 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700831 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700832 def.nPortIndex = portIndex;
833
834 status_t err = mOMX->get_parameter(
835 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
836
837 if (err != OK) {
838 return err;
839 }
840
Andreas Huber5c0a9132009-08-20 11:16:40 -0700841 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
842 mDealer[portIndex] = new MemoryDealer(totalSize);
843
Andreas Huberbe06d262009-08-14 14:37:10 -0700844 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -0700845 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700846 CHECK(mem.get() != NULL);
847
848 IOMX::buffer_id buffer;
849 if (portIndex == kPortIndexInput
850 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
851 err = mOMX->allocate_buffer_with_backup(
852 mNode, portIndex, mem, &buffer);
Andreas Huber446f44f2009-08-25 17:23:44 -0700853 } else if (portIndex == kPortIndexOutput
854 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huber2dc64d82009-09-11 12:58:53 -0700855 err = mOMX->allocate_buffer_with_backup(
856 mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700857 } else {
858 err = mOMX->use_buffer(mNode, portIndex, mem, &buffer);
859 }
860
861 if (err != OK) {
862 LOGE("allocate_buffer_with_backup failed");
863 return err;
864 }
865
866 BufferInfo info;
867 info.mBuffer = buffer;
868 info.mOwnedByComponent = false;
869 info.mMem = mem;
870 info.mMediaBuffer = NULL;
871
872 if (portIndex == kPortIndexOutput) {
873 info.mMediaBuffer = new MediaBuffer(mem->pointer(), mem->size());
874 info.mMediaBuffer->setObserver(this);
875 }
876
877 mPortBuffers[portIndex].push(info);
878
Andreas Huber4c483422009-09-02 16:05:36 -0700879 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -0700880 portIndex == kPortIndexInput ? "input" : "output");
881 }
882
883 dumpPortStatus(portIndex);
884
885 return OK;
886}
887
888void OMXCodec::on_message(const omx_message &msg) {
889 Mutex::Autolock autoLock(mLock);
890
891 switch (msg.type) {
892 case omx_message::EVENT:
893 {
894 onEvent(
895 msg.u.event_data.event, msg.u.event_data.data1,
896 msg.u.event_data.data2);
897
898 break;
899 }
900
901 case omx_message::EMPTY_BUFFER_DONE:
902 {
903 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
904
Andreas Huber4c483422009-09-02 16:05:36 -0700905 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700906
907 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
908 size_t i = 0;
909 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
910 ++i;
911 }
912
913 CHECK(i < buffers->size());
914 if (!(*buffers)[i].mOwnedByComponent) {
915 LOGW("We already own input buffer %p, yet received "
916 "an EMPTY_BUFFER_DONE.", buffer);
917 }
918
919 buffers->editItemAt(i).mOwnedByComponent = false;
920
921 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -0700922 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700923
924 status_t err =
925 mOMX->free_buffer(mNode, kPortIndexInput, buffer);
926 CHECK_EQ(err, OK);
927
928 buffers->removeAt(i);
929 } else if (mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
930 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
931 drainInputBuffer(&buffers->editItemAt(i));
932 }
933
934 break;
935 }
936
937 case omx_message::FILL_BUFFER_DONE:
938 {
939 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
940 OMX_U32 flags = msg.u.extended_buffer_data.flags;
941
Andreas Huber4c483422009-09-02 16:05:36 -0700942 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx)",
Andreas Huberbe06d262009-08-14 14:37:10 -0700943 buffer,
944 msg.u.extended_buffer_data.range_length,
945 flags);
946
Andreas Huber4c483422009-09-02 16:05:36 -0700947 CODEC_LOGV("FILL_BUFFER_DONE(timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -0700948 msg.u.extended_buffer_data.timestamp,
949 msg.u.extended_buffer_data.timestamp / 1E6);
950
951 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
952 size_t i = 0;
953 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
954 ++i;
955 }
956
957 CHECK(i < buffers->size());
958 BufferInfo *info = &buffers->editItemAt(i);
959
960 if (!info->mOwnedByComponent) {
961 LOGW("We already own output buffer %p, yet received "
962 "a FILL_BUFFER_DONE.", buffer);
963 }
964
965 info->mOwnedByComponent = false;
966
967 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -0700968 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700969
970 status_t err =
971 mOMX->free_buffer(mNode, kPortIndexOutput, buffer);
972 CHECK_EQ(err, OK);
973
974 buffers->removeAt(i);
Andreas Huberd7795892009-08-26 10:33:47 -0700975 } else if (mPortStatus[kPortIndexOutput] == ENABLED
976 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -0700977 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -0700978 mNoMoreOutputData = true;
979 mBufferFilled.signal();
980 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
981 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700982
Andreas Huberbe06d262009-08-14 14:37:10 -0700983 MediaBuffer *buffer = info->mMediaBuffer;
984
985 buffer->set_range(
986 msg.u.extended_buffer_data.range_offset,
987 msg.u.extended_buffer_data.range_length);
988
989 buffer->meta_data()->clear();
990
Andreas Huberfa8de752009-10-08 10:07:49 -0700991 buffer->meta_data()->setInt64(
992 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -0700993
994 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
995 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
996 }
997
998 buffer->meta_data()->setPointer(
999 kKeyPlatformPrivate,
1000 msg.u.extended_buffer_data.platform_private);
1001
1002 buffer->meta_data()->setPointer(
1003 kKeyBufferID,
1004 msg.u.extended_buffer_data.buffer);
1005
1006 mFilledBuffers.push_back(i);
1007 mBufferFilled.signal();
1008 }
1009
1010 break;
1011 }
1012
1013 default:
1014 {
1015 CHECK(!"should not be here.");
1016 break;
1017 }
1018 }
1019}
1020
1021void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1022 switch (event) {
1023 case OMX_EventCmdComplete:
1024 {
1025 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1026 break;
1027 }
1028
1029 case OMX_EventError:
1030 {
1031 LOGE("ERROR(%ld, %ld)", data1, data2);
1032
1033 setState(ERROR);
1034 break;
1035 }
1036
1037 case OMX_EventPortSettingsChanged:
1038 {
1039 onPortSettingsChanged(data1);
1040 break;
1041 }
1042
1043 case OMX_EventBufferFlag:
1044 {
Andreas Huber4c483422009-09-02 16:05:36 -07001045 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001046
1047 if (data1 == kPortIndexOutput) {
1048 mNoMoreOutputData = true;
1049 }
1050 break;
1051 }
1052
1053 default:
1054 {
Andreas Huber4c483422009-09-02 16:05:36 -07001055 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001056 break;
1057 }
1058 }
1059}
1060
1061void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1062 switch (cmd) {
1063 case OMX_CommandStateSet:
1064 {
1065 onStateChange((OMX_STATETYPE)data);
1066 break;
1067 }
1068
1069 case OMX_CommandPortDisable:
1070 {
1071 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001072 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001073
1074 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1075 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1076 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1077
1078 mPortStatus[portIndex] = DISABLED;
1079
1080 if (mState == RECONFIGURING) {
1081 CHECK_EQ(portIndex, kPortIndexOutput);
1082
Andreas Hubercfd55572009-10-09 14:11:28 -07001083 initOutputFormat(mSource->getFormat());
1084 mOutputPortSettingsHaveChanged = true;
1085
Andreas Huberbe06d262009-08-14 14:37:10 -07001086 enablePortAsync(portIndex);
1087
1088 status_t err = allocateBuffersOnPort(portIndex);
1089 CHECK_EQ(err, OK);
1090 }
1091 break;
1092 }
1093
1094 case OMX_CommandPortEnable:
1095 {
1096 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001097 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001098
1099 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1100 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1101
1102 mPortStatus[portIndex] = ENABLED;
1103
1104 if (mState == RECONFIGURING) {
1105 CHECK_EQ(portIndex, kPortIndexOutput);
1106
1107 setState(EXECUTING);
1108
1109 fillOutputBuffers();
1110 }
1111 break;
1112 }
1113
1114 case OMX_CommandFlush:
1115 {
1116 OMX_U32 portIndex = data;
1117
Andreas Huber4c483422009-09-02 16:05:36 -07001118 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001119
1120 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1121 mPortStatus[portIndex] = ENABLED;
1122
1123 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1124 mPortBuffers[portIndex].size());
1125
1126 if (mState == RECONFIGURING) {
1127 CHECK_EQ(portIndex, kPortIndexOutput);
1128
1129 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001130 } else if (mState == EXECUTING_TO_IDLE) {
1131 if (mPortStatus[kPortIndexInput] == ENABLED
1132 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001133 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001134 "transition from EXECUTING to IDLE.");
1135
1136 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1137 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1138
1139 status_t err =
1140 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1141 CHECK_EQ(err, OK);
1142 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001143 } else {
1144 // We're flushing both ports in preparation for seeking.
1145
1146 if (mPortStatus[kPortIndexInput] == ENABLED
1147 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001148 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001149 " seek-time.");
1150
Andreas Huber1a77b68e2009-09-17 11:16:52 -07001151 // Clear this flag in case the decoder sent us either
1152 // the EVENT_BUFFER_FLAG(1) or an output buffer with
1153 // the EOS flag set _while_ flushing. Since we're going
1154 // to submit "fresh" input data now, this flag no longer
1155 // applies to our future.
1156 mNoMoreOutputData = false;
1157
Andreas Huberbe06d262009-08-14 14:37:10 -07001158 drainInputBuffers();
1159 fillOutputBuffers();
1160 }
1161 }
1162
1163 break;
1164 }
1165
1166 default:
1167 {
Andreas Huber4c483422009-09-02 16:05:36 -07001168 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001169 break;
1170 }
1171 }
1172}
1173
1174void OMXCodec::onStateChange(OMX_STATETYPE newState) {
1175 switch (newState) {
1176 case OMX_StateIdle:
1177 {
Andreas Huber4c483422009-09-02 16:05:36 -07001178 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001179 if (mState == LOADED_TO_IDLE) {
1180 status_t err = mOMX->send_command(
1181 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1182
1183 CHECK_EQ(err, OK);
1184
1185 setState(IDLE_TO_EXECUTING);
1186 } else {
1187 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1188
1189 CHECK_EQ(
1190 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1191 mPortBuffers[kPortIndexInput].size());
1192
1193 CHECK_EQ(
1194 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1195 mPortBuffers[kPortIndexOutput].size());
1196
1197 status_t err = mOMX->send_command(
1198 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1199
1200 CHECK_EQ(err, OK);
1201
1202 err = freeBuffersOnPort(kPortIndexInput);
1203 CHECK_EQ(err, OK);
1204
1205 err = freeBuffersOnPort(kPortIndexOutput);
1206 CHECK_EQ(err, OK);
1207
1208 mPortStatus[kPortIndexInput] = ENABLED;
1209 mPortStatus[kPortIndexOutput] = ENABLED;
1210
1211 setState(IDLE_TO_LOADED);
1212 }
1213 break;
1214 }
1215
1216 case OMX_StateExecuting:
1217 {
1218 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1219
Andreas Huber4c483422009-09-02 16:05:36 -07001220 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001221
1222 setState(EXECUTING);
1223
Andreas Huber42978e52009-08-27 10:08:39 -07001224 // Buffers will be submitted to the component in the first
1225 // call to OMXCodec::read as mInitialBufferSubmit is true at
1226 // this point. This ensures that this on_message call returns,
1227 // releases the lock and ::init can notice the state change and
1228 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001229 break;
1230 }
1231
1232 case OMX_StateLoaded:
1233 {
1234 CHECK_EQ(mState, IDLE_TO_LOADED);
1235
Andreas Huber4c483422009-09-02 16:05:36 -07001236 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001237
1238 setState(LOADED);
1239 break;
1240 }
1241
1242 default:
1243 {
1244 CHECK(!"should not be here.");
1245 break;
1246 }
1247 }
1248}
1249
1250// static
1251size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1252 size_t n = 0;
1253 for (size_t i = 0; i < buffers.size(); ++i) {
1254 if (!buffers[i].mOwnedByComponent) {
1255 ++n;
1256 }
1257 }
1258
1259 return n;
1260}
1261
1262status_t OMXCodec::freeBuffersOnPort(
1263 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1264 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1265
1266 status_t stickyErr = OK;
1267
1268 for (size_t i = buffers->size(); i-- > 0;) {
1269 BufferInfo *info = &buffers->editItemAt(i);
1270
1271 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1272 continue;
1273 }
1274
1275 CHECK_EQ(info->mOwnedByComponent, false);
1276
Andreas Huber92022852009-09-14 15:24:14 -07001277 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1278
Andreas Huberbe06d262009-08-14 14:37:10 -07001279 status_t err =
1280 mOMX->free_buffer(mNode, portIndex, info->mBuffer);
1281
1282 if (err != OK) {
1283 stickyErr = err;
1284 }
1285
1286 if (info->mMediaBuffer != NULL) {
1287 info->mMediaBuffer->setObserver(NULL);
1288
1289 // Make sure nobody but us owns this buffer at this point.
1290 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1291
1292 info->mMediaBuffer->release();
1293 }
1294
1295 buffers->removeAt(i);
1296 }
1297
1298 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1299
1300 return stickyErr;
1301}
1302
1303void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001304 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001305
1306 CHECK_EQ(mState, EXECUTING);
1307 CHECK_EQ(portIndex, kPortIndexOutput);
1308 setState(RECONFIGURING);
1309
1310 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001311 if (!flushPortAsync(portIndex)) {
1312 onCmdComplete(OMX_CommandFlush, portIndex);
1313 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001314 } else {
1315 disablePortAsync(portIndex);
1316 }
1317}
1318
Andreas Huber404cc412009-08-25 14:26:05 -07001319bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001320 CHECK(mState == EXECUTING || mState == RECONFIGURING
1321 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001322
Andreas Huber4c483422009-09-02 16:05:36 -07001323 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001324 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1325 mPortBuffers[portIndex].size());
1326
Andreas Huberbe06d262009-08-14 14:37:10 -07001327 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1328 mPortStatus[portIndex] = SHUTTING_DOWN;
1329
Andreas Huber404cc412009-08-25 14:26:05 -07001330 if ((mQuirks & kRequiresFlushCompleteEmulation)
1331 && countBuffersWeOwn(mPortBuffers[portIndex])
1332 == mPortBuffers[portIndex].size()) {
1333 // No flush is necessary and this component fails to send a
1334 // flush-complete event in this case.
1335
1336 return false;
1337 }
1338
Andreas Huberbe06d262009-08-14 14:37:10 -07001339 status_t err =
1340 mOMX->send_command(mNode, OMX_CommandFlush, portIndex);
1341 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001342
1343 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001344}
1345
1346void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1347 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1348
1349 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1350 mPortStatus[portIndex] = DISABLING;
1351
1352 status_t err =
1353 mOMX->send_command(mNode, OMX_CommandPortDisable, portIndex);
1354 CHECK_EQ(err, OK);
1355
1356 freeBuffersOnPort(portIndex, true);
1357}
1358
1359void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1360 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1361
1362 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1363 mPortStatus[portIndex] = ENABLING;
1364
1365 status_t err =
1366 mOMX->send_command(mNode, OMX_CommandPortEnable, portIndex);
1367 CHECK_EQ(err, OK);
1368}
1369
1370void OMXCodec::fillOutputBuffers() {
1371 CHECK_EQ(mState, EXECUTING);
1372
1373 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1374 for (size_t i = 0; i < buffers->size(); ++i) {
1375 fillOutputBuffer(&buffers->editItemAt(i));
1376 }
1377}
1378
1379void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001380 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001381
1382 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1383 for (size_t i = 0; i < buffers->size(); ++i) {
1384 drainInputBuffer(&buffers->editItemAt(i));
1385 }
1386}
1387
1388void OMXCodec::drainInputBuffer(BufferInfo *info) {
1389 CHECK_EQ(info->mOwnedByComponent, false);
1390
1391 if (mSignalledEOS) {
1392 return;
1393 }
1394
1395 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1396 const CodecSpecificData *specific =
1397 mCodecSpecificData[mCodecSpecificDataIndex];
1398
1399 size_t size = specific->mSize;
1400
Andreas Hubere6c40962009-09-10 14:13:30 -07001401 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07001402 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001403 static const uint8_t kNALStartCode[4] =
1404 { 0x00, 0x00, 0x00, 0x01 };
1405
1406 CHECK(info->mMem->size() >= specific->mSize + 4);
1407
1408 size += 4;
1409
1410 memcpy(info->mMem->pointer(), kNALStartCode, 4);
1411 memcpy((uint8_t *)info->mMem->pointer() + 4,
1412 specific->mData, specific->mSize);
1413 } else {
1414 CHECK(info->mMem->size() >= specific->mSize);
1415 memcpy(info->mMem->pointer(), specific->mData, specific->mSize);
1416 }
1417
Andreas Huber3f427072009-10-08 11:02:27 -07001418 status_t err = mOMX->empty_buffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07001419 mNode, info->mBuffer, 0, size,
1420 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1421 0);
Andreas Huber3f427072009-10-08 11:02:27 -07001422 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001423
1424 info->mOwnedByComponent = true;
1425
1426 ++mCodecSpecificDataIndex;
1427 return;
1428 }
1429
1430 MediaBuffer *srcBuffer;
1431 status_t err;
1432 if (mSeekTimeUs >= 0) {
1433 MediaSource::ReadOptions options;
1434 options.setSeekTo(mSeekTimeUs);
1435 mSeekTimeUs = -1;
1436
1437 err = mSource->read(&srcBuffer, &options);
1438 } else {
1439 err = mSource->read(&srcBuffer);
1440 }
1441
1442 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberfa8de752009-10-08 10:07:49 -07001443 OMX_TICKS timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07001444 size_t srcLength = 0;
1445
1446 if (err != OK) {
Andreas Huber4c483422009-09-02 16:05:36 -07001447 CODEC_LOGV("signalling end of input stream.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001448 flags |= OMX_BUFFERFLAG_EOS;
1449
1450 mSignalledEOS = true;
1451 } else {
1452 srcLength = srcBuffer->range_length();
1453
1454 if (info->mMem->size() < srcLength) {
1455 LOGE("info->mMem->size() = %d, srcLength = %d",
1456 info->mMem->size(), srcLength);
1457 }
1458 CHECK(info->mMem->size() >= srcLength);
1459 memcpy(info->mMem->pointer(),
1460 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
1461 srcLength);
1462
Andreas Huberfa8de752009-10-08 10:07:49 -07001463 if (srcBuffer->meta_data()->findInt64(kKeyTime, &timestampUs)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001464 CODEC_LOGV("Calling empty_buffer on buffer %p (length %d)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001465 info->mBuffer, srcLength);
Andreas Huber4c483422009-09-02 16:05:36 -07001466 CODEC_LOGV("Calling empty_buffer with timestamp %lld us (%.2f secs)",
Andreas Huberfa8de752009-10-08 10:07:49 -07001467 timestampUs, timestampUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07001468 }
1469 }
1470
Andreas Huberbe06d262009-08-14 14:37:10 -07001471 if (srcBuffer != NULL) {
1472 srcBuffer->release();
1473 srcBuffer = NULL;
1474 }
Andreas Huber3f427072009-10-08 11:02:27 -07001475
1476 err = mOMX->empty_buffer(
1477 mNode, info->mBuffer, 0, srcLength,
Andreas Huberfa8de752009-10-08 10:07:49 -07001478 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07001479
1480 if (err != OK) {
1481 setState(ERROR);
1482 return;
1483 }
1484
1485 info->mOwnedByComponent = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001486}
1487
1488void OMXCodec::fillOutputBuffer(BufferInfo *info) {
1489 CHECK_EQ(info->mOwnedByComponent, false);
1490
Andreas Huber404cc412009-08-25 14:26:05 -07001491 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07001492 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07001493 "calling fillOutputBuffer");
1494 return;
1495 }
1496
Andreas Huber4c483422009-09-02 16:05:36 -07001497 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber3f427072009-10-08 11:02:27 -07001498 status_t err = mOMX->fill_buffer(mNode, info->mBuffer);
1499 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001500
1501 info->mOwnedByComponent = true;
1502}
1503
1504void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
1505 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1506 for (size_t i = 0; i < buffers->size(); ++i) {
1507 if ((*buffers)[i].mBuffer == buffer) {
1508 drainInputBuffer(&buffers->editItemAt(i));
1509 return;
1510 }
1511 }
1512
1513 CHECK(!"should not be here.");
1514}
1515
1516void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
1517 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1518 for (size_t i = 0; i < buffers->size(); ++i) {
1519 if ((*buffers)[i].mBuffer == buffer) {
1520 fillOutputBuffer(&buffers->editItemAt(i));
1521 return;
1522 }
1523 }
1524
1525 CHECK(!"should not be here.");
1526}
1527
1528void OMXCodec::setState(State newState) {
1529 mState = newState;
1530 mAsyncCompletion.signal();
1531
1532 // This may cause some spurious wakeups but is necessary to
1533 // unblock the reader if we enter ERROR state.
1534 mBufferFilled.signal();
1535}
1536
Andreas Huberda050cf22009-09-02 14:01:43 -07001537void OMXCodec::setRawAudioFormat(
1538 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
1539 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07001540 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07001541 pcmParams.nPortIndex = portIndex;
1542
1543 status_t err = mOMX->get_parameter(
1544 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1545
1546 CHECK_EQ(err, OK);
1547
1548 pcmParams.nChannels = numChannels;
1549 pcmParams.eNumData = OMX_NumericalDataSigned;
1550 pcmParams.bInterleaved = OMX_TRUE;
1551 pcmParams.nBitPerSample = 16;
1552 pcmParams.nSamplingRate = sampleRate;
1553 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
1554
1555 if (numChannels == 1) {
1556 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
1557 } else {
1558 CHECK_EQ(numChannels, 2);
1559
1560 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
1561 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
1562 }
1563
1564 err = mOMX->set_parameter(
1565 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1566
1567 CHECK_EQ(err, OK);
1568}
1569
Andreas Huberbe06d262009-08-14 14:37:10 -07001570void OMXCodec::setAMRFormat() {
1571 if (!mIsEncoder) {
1572 OMX_AUDIO_PARAM_AMRTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001573 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001574 def.nPortIndex = kPortIndexInput;
1575
1576 status_t err =
1577 mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1578
1579 CHECK_EQ(err, OK);
1580
1581 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1582 def.eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
1583
1584 err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1585 CHECK_EQ(err, OK);
1586 }
1587
1588 ////////////////////////
1589
1590 if (mIsEncoder) {
1591 sp<MetaData> format = mSource->getFormat();
1592 int32_t sampleRate;
1593 int32_t numChannels;
1594 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1595 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1596
Andreas Huberda050cf22009-09-02 14:01:43 -07001597 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
Andreas Huberbe06d262009-08-14 14:37:10 -07001598 }
1599}
1600
Andreas Huberee606e62009-09-08 10:19:21 -07001601void OMXCodec::setAMRWBFormat() {
1602 if (!mIsEncoder) {
1603 OMX_AUDIO_PARAM_AMRTYPE def;
1604 InitOMXParams(&def);
1605 def.nPortIndex = kPortIndexInput;
1606
1607 status_t err =
1608 mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1609
1610 CHECK_EQ(err, OK);
1611
1612 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1613 def.eAMRBandMode = OMX_AUDIO_AMRBandModeWB0;
1614
1615 err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1616 CHECK_EQ(err, OK);
1617 }
1618
1619 ////////////////////////
1620
1621 if (mIsEncoder) {
1622 sp<MetaData> format = mSource->getFormat();
1623 int32_t sampleRate;
1624 int32_t numChannels;
1625 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1626 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1627
1628 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1629 }
1630}
1631
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07001632void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) {
Andreas Huberda050cf22009-09-02 14:01:43 -07001633 if (mIsEncoder) {
1634 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1635 } else {
1636 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07001637 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07001638 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07001639
Andreas Huberda050cf22009-09-02 14:01:43 -07001640 status_t err = mOMX->get_parameter(
1641 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1642 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001643
Andreas Huberda050cf22009-09-02 14:01:43 -07001644 profile.nChannels = numChannels;
1645 profile.nSampleRate = sampleRate;
1646 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07001647
Andreas Huberda050cf22009-09-02 14:01:43 -07001648 err = mOMX->set_parameter(
1649 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1650 CHECK_EQ(err, OK);
1651 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001652}
1653
1654void OMXCodec::setImageOutputFormat(
1655 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07001656 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001657
1658#if 0
1659 OMX_INDEXTYPE index;
1660 status_t err = mOMX->get_extension_index(
1661 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
1662 CHECK_EQ(err, OK);
1663
1664 err = mOMX->set_config(mNode, index, &format, sizeof(format));
1665 CHECK_EQ(err, OK);
1666#endif
1667
1668 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001669 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001670 def.nPortIndex = kPortIndexOutput;
1671
1672 status_t err = mOMX->get_parameter(
1673 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1674 CHECK_EQ(err, OK);
1675
1676 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1677
1678 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001679
Andreas Huberbe06d262009-08-14 14:37:10 -07001680 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
1681 imageDef->eColorFormat = format;
1682 imageDef->nFrameWidth = width;
1683 imageDef->nFrameHeight = height;
1684
1685 switch (format) {
1686 case OMX_COLOR_FormatYUV420PackedPlanar:
1687 case OMX_COLOR_FormatYUV411Planar:
1688 {
1689 def.nBufferSize = (width * height * 3) / 2;
1690 break;
1691 }
1692
1693 case OMX_COLOR_FormatCbYCrY:
1694 {
1695 def.nBufferSize = width * height * 2;
1696 break;
1697 }
1698
1699 case OMX_COLOR_Format32bitARGB8888:
1700 {
1701 def.nBufferSize = width * height * 4;
1702 break;
1703 }
1704
Andreas Huber201511c2009-09-08 14:01:44 -07001705 case OMX_COLOR_Format16bitARGB4444:
1706 case OMX_COLOR_Format16bitARGB1555:
1707 case OMX_COLOR_Format16bitRGB565:
1708 case OMX_COLOR_Format16bitBGR565:
1709 {
1710 def.nBufferSize = width * height * 2;
1711 break;
1712 }
1713
Andreas Huberbe06d262009-08-14 14:37:10 -07001714 default:
1715 CHECK(!"Should not be here. Unknown color format.");
1716 break;
1717 }
1718
Andreas Huber5c0a9132009-08-20 11:16:40 -07001719 def.nBufferCountActual = def.nBufferCountMin;
1720
Andreas Huberbe06d262009-08-14 14:37:10 -07001721 err = mOMX->set_parameter(
1722 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1723 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07001724}
Andreas Huberbe06d262009-08-14 14:37:10 -07001725
Andreas Huber5c0a9132009-08-20 11:16:40 -07001726void OMXCodec::setJPEGInputFormat(
1727 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
1728 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001729 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001730 def.nPortIndex = kPortIndexInput;
1731
Andreas Huber5c0a9132009-08-20 11:16:40 -07001732 status_t err = mOMX->get_parameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001733 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1734 CHECK_EQ(err, OK);
1735
Andreas Huber5c0a9132009-08-20 11:16:40 -07001736 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1737 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
1738
Andreas Huberbe06d262009-08-14 14:37:10 -07001739 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
1740 imageDef->nFrameWidth = width;
1741 imageDef->nFrameHeight = height;
1742
Andreas Huber5c0a9132009-08-20 11:16:40 -07001743 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07001744 def.nBufferCountActual = def.nBufferCountMin;
1745
1746 err = mOMX->set_parameter(
1747 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1748 CHECK_EQ(err, OK);
1749}
1750
1751void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
1752 CodecSpecificData *specific =
1753 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
1754
1755 specific->mSize = size;
1756 memcpy(specific->mData, data, size);
1757
1758 mCodecSpecificData.push(specific);
1759}
1760
1761void OMXCodec::clearCodecSpecificData() {
1762 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
1763 free(mCodecSpecificData.editItemAt(i));
1764 }
1765 mCodecSpecificData.clear();
1766 mCodecSpecificDataIndex = 0;
1767}
1768
1769status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07001770 Mutex::Autolock autoLock(mLock);
1771
Andreas Huberbe06d262009-08-14 14:37:10 -07001772 if (mState != LOADED) {
1773 return UNKNOWN_ERROR;
1774 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07001775
Andreas Huberbe06d262009-08-14 14:37:10 -07001776 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07001777 if (mQuirks & kWantsNALFragments) {
1778 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07001779 }
1780 status_t err = mSource->start(params.get());
1781
1782 if (err != OK) {
1783 return err;
1784 }
1785
1786 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07001787 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001788 mSignalledEOS = false;
1789 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07001790 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07001791 mSeekTimeUs = -1;
1792 mFilledBuffers.clear();
1793
1794 return init();
1795}
1796
1797status_t OMXCodec::stop() {
Andreas Huber4c483422009-09-02 16:05:36 -07001798 CODEC_LOGV("stop");
Andreas Huberbe06d262009-08-14 14:37:10 -07001799
1800 Mutex::Autolock autoLock(mLock);
1801
1802 while (isIntermediateState(mState)) {
1803 mAsyncCompletion.wait(mLock);
1804 }
1805
1806 switch (mState) {
1807 case LOADED:
1808 case ERROR:
1809 break;
1810
1811 case EXECUTING:
1812 {
1813 setState(EXECUTING_TO_IDLE);
1814
Andreas Huber127fcdc2009-08-26 16:27:02 -07001815 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07001816 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001817 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07001818
Andreas Huber127fcdc2009-08-26 16:27:02 -07001819 bool emulateInputFlushCompletion =
1820 !flushPortAsync(kPortIndexInput);
1821
1822 bool emulateOutputFlushCompletion =
1823 !flushPortAsync(kPortIndexOutput);
1824
1825 if (emulateInputFlushCompletion) {
1826 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1827 }
1828
1829 if (emulateOutputFlushCompletion) {
1830 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1831 }
1832 } else {
1833 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1834 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1835
1836 status_t err =
1837 mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1838 CHECK_EQ(err, OK);
1839 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001840
1841 while (mState != LOADED && mState != ERROR) {
1842 mAsyncCompletion.wait(mLock);
1843 }
1844
1845 break;
1846 }
1847
1848 default:
1849 {
1850 CHECK(!"should not be here.");
1851 break;
1852 }
1853 }
1854
1855 mSource->stop();
1856
1857 return OK;
1858}
1859
1860sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07001861 Mutex::Autolock autoLock(mLock);
1862
Andreas Huberbe06d262009-08-14 14:37:10 -07001863 return mOutputFormat;
1864}
1865
1866status_t OMXCodec::read(
1867 MediaBuffer **buffer, const ReadOptions *options) {
1868 *buffer = NULL;
1869
1870 Mutex::Autolock autoLock(mLock);
1871
Andreas Huberd06e5b82009-08-28 13:18:14 -07001872 if (mState != EXECUTING && mState != RECONFIGURING) {
1873 return UNKNOWN_ERROR;
1874 }
1875
Andreas Huber42978e52009-08-27 10:08:39 -07001876 if (mInitialBufferSubmit) {
1877 mInitialBufferSubmit = false;
1878
1879 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07001880
Andreas Huberd06e5b82009-08-28 13:18:14 -07001881 if (mState == EXECUTING) {
1882 // Otherwise mState == RECONFIGURING and this code will trigger
1883 // after the output port is reenabled.
1884 fillOutputBuffers();
1885 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001886 }
1887
1888 int64_t seekTimeUs;
1889 if (options && options->getSeekTo(&seekTimeUs)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001890 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07001891
1892 mSignalledEOS = false;
1893 mNoMoreOutputData = false;
1894
1895 CHECK(seekTimeUs >= 0);
1896 mSeekTimeUs = seekTimeUs;
1897
1898 mFilledBuffers.clear();
1899
1900 CHECK_EQ(mState, EXECUTING);
1901
Andreas Huber404cc412009-08-25 14:26:05 -07001902 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
1903 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
1904
1905 if (emulateInputFlushCompletion) {
1906 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1907 }
1908
1909 if (emulateOutputFlushCompletion) {
1910 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1911 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001912 }
1913
1914 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
1915 mBufferFilled.wait(mLock);
1916 }
1917
1918 if (mState == ERROR) {
1919 return UNKNOWN_ERROR;
1920 }
1921
1922 if (mFilledBuffers.empty()) {
1923 return ERROR_END_OF_STREAM;
1924 }
1925
Andreas Hubercfd55572009-10-09 14:11:28 -07001926 if (mOutputPortSettingsHaveChanged) {
1927 mOutputPortSettingsHaveChanged = false;
1928
1929 return INFO_FORMAT_CHANGED;
1930 }
1931
Andreas Huberbe06d262009-08-14 14:37:10 -07001932 size_t index = *mFilledBuffers.begin();
1933 mFilledBuffers.erase(mFilledBuffers.begin());
1934
1935 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
1936 info->mMediaBuffer->add_ref();
1937 *buffer = info->mMediaBuffer;
1938
1939 return OK;
1940}
1941
1942void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
1943 Mutex::Autolock autoLock(mLock);
1944
1945 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1946 for (size_t i = 0; i < buffers->size(); ++i) {
1947 BufferInfo *info = &buffers->editItemAt(i);
1948
1949 if (info->mMediaBuffer == buffer) {
1950 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
1951 fillOutputBuffer(info);
1952 return;
1953 }
1954 }
1955
1956 CHECK(!"should not be here.");
1957}
1958
1959static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
1960 static const char *kNames[] = {
1961 "OMX_IMAGE_CodingUnused",
1962 "OMX_IMAGE_CodingAutoDetect",
1963 "OMX_IMAGE_CodingJPEG",
1964 "OMX_IMAGE_CodingJPEG2K",
1965 "OMX_IMAGE_CodingEXIF",
1966 "OMX_IMAGE_CodingTIFF",
1967 "OMX_IMAGE_CodingGIF",
1968 "OMX_IMAGE_CodingPNG",
1969 "OMX_IMAGE_CodingLZW",
1970 "OMX_IMAGE_CodingBMP",
1971 };
1972
1973 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
1974
1975 if (type < 0 || (size_t)type >= numNames) {
1976 return "UNKNOWN";
1977 } else {
1978 return kNames[type];
1979 }
1980}
1981
1982static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
1983 static const char *kNames[] = {
1984 "OMX_COLOR_FormatUnused",
1985 "OMX_COLOR_FormatMonochrome",
1986 "OMX_COLOR_Format8bitRGB332",
1987 "OMX_COLOR_Format12bitRGB444",
1988 "OMX_COLOR_Format16bitARGB4444",
1989 "OMX_COLOR_Format16bitARGB1555",
1990 "OMX_COLOR_Format16bitRGB565",
1991 "OMX_COLOR_Format16bitBGR565",
1992 "OMX_COLOR_Format18bitRGB666",
1993 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07001994 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07001995 "OMX_COLOR_Format24bitRGB888",
1996 "OMX_COLOR_Format24bitBGR888",
1997 "OMX_COLOR_Format24bitARGB1887",
1998 "OMX_COLOR_Format25bitARGB1888",
1999 "OMX_COLOR_Format32bitBGRA8888",
2000 "OMX_COLOR_Format32bitARGB8888",
2001 "OMX_COLOR_FormatYUV411Planar",
2002 "OMX_COLOR_FormatYUV411PackedPlanar",
2003 "OMX_COLOR_FormatYUV420Planar",
2004 "OMX_COLOR_FormatYUV420PackedPlanar",
2005 "OMX_COLOR_FormatYUV420SemiPlanar",
2006 "OMX_COLOR_FormatYUV422Planar",
2007 "OMX_COLOR_FormatYUV422PackedPlanar",
2008 "OMX_COLOR_FormatYUV422SemiPlanar",
2009 "OMX_COLOR_FormatYCbYCr",
2010 "OMX_COLOR_FormatYCrYCb",
2011 "OMX_COLOR_FormatCbYCrY",
2012 "OMX_COLOR_FormatCrYCbY",
2013 "OMX_COLOR_FormatYUV444Interleaved",
2014 "OMX_COLOR_FormatRawBayer8bit",
2015 "OMX_COLOR_FormatRawBayer10bit",
2016 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002017 "OMX_COLOR_FormatL2",
2018 "OMX_COLOR_FormatL4",
2019 "OMX_COLOR_FormatL8",
2020 "OMX_COLOR_FormatL16",
2021 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002022 "OMX_COLOR_FormatL32",
2023 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2024 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2025 "OMX_COLOR_Format18BitBGR666",
2026 "OMX_COLOR_Format24BitARGB6666",
2027 "OMX_COLOR_Format24BitABGR6666",
2028 };
2029
2030 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2031
Andreas Huberbe06d262009-08-14 14:37:10 -07002032 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2033 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2034 } else if (type < 0 || (size_t)type >= numNames) {
2035 return "UNKNOWN";
2036 } else {
2037 return kNames[type];
2038 }
2039}
2040
2041static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2042 static const char *kNames[] = {
2043 "OMX_VIDEO_CodingUnused",
2044 "OMX_VIDEO_CodingAutoDetect",
2045 "OMX_VIDEO_CodingMPEG2",
2046 "OMX_VIDEO_CodingH263",
2047 "OMX_VIDEO_CodingMPEG4",
2048 "OMX_VIDEO_CodingWMV",
2049 "OMX_VIDEO_CodingRV",
2050 "OMX_VIDEO_CodingAVC",
2051 "OMX_VIDEO_CodingMJPEG",
2052 };
2053
2054 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2055
2056 if (type < 0 || (size_t)type >= numNames) {
2057 return "UNKNOWN";
2058 } else {
2059 return kNames[type];
2060 }
2061}
2062
2063static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2064 static const char *kNames[] = {
2065 "OMX_AUDIO_CodingUnused",
2066 "OMX_AUDIO_CodingAutoDetect",
2067 "OMX_AUDIO_CodingPCM",
2068 "OMX_AUDIO_CodingADPCM",
2069 "OMX_AUDIO_CodingAMR",
2070 "OMX_AUDIO_CodingGSMFR",
2071 "OMX_AUDIO_CodingGSMEFR",
2072 "OMX_AUDIO_CodingGSMHR",
2073 "OMX_AUDIO_CodingPDCFR",
2074 "OMX_AUDIO_CodingPDCEFR",
2075 "OMX_AUDIO_CodingPDCHR",
2076 "OMX_AUDIO_CodingTDMAFR",
2077 "OMX_AUDIO_CodingTDMAEFR",
2078 "OMX_AUDIO_CodingQCELP8",
2079 "OMX_AUDIO_CodingQCELP13",
2080 "OMX_AUDIO_CodingEVRC",
2081 "OMX_AUDIO_CodingSMV",
2082 "OMX_AUDIO_CodingG711",
2083 "OMX_AUDIO_CodingG723",
2084 "OMX_AUDIO_CodingG726",
2085 "OMX_AUDIO_CodingG729",
2086 "OMX_AUDIO_CodingAAC",
2087 "OMX_AUDIO_CodingMP3",
2088 "OMX_AUDIO_CodingSBC",
2089 "OMX_AUDIO_CodingVORBIS",
2090 "OMX_AUDIO_CodingWMA",
2091 "OMX_AUDIO_CodingRA",
2092 "OMX_AUDIO_CodingMIDI",
2093 };
2094
2095 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2096
2097 if (type < 0 || (size_t)type >= numNames) {
2098 return "UNKNOWN";
2099 } else {
2100 return kNames[type];
2101 }
2102}
2103
2104static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2105 static const char *kNames[] = {
2106 "OMX_AUDIO_PCMModeLinear",
2107 "OMX_AUDIO_PCMModeALaw",
2108 "OMX_AUDIO_PCMModeMULaw",
2109 };
2110
2111 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2112
2113 if (type < 0 || (size_t)type >= numNames) {
2114 return "UNKNOWN";
2115 } else {
2116 return kNames[type];
2117 }
2118}
2119
Andreas Huber7ae02c82009-09-09 16:29:47 -07002120static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2121 static const char *kNames[] = {
2122 "OMX_AUDIO_AMRBandModeUnused",
2123 "OMX_AUDIO_AMRBandModeNB0",
2124 "OMX_AUDIO_AMRBandModeNB1",
2125 "OMX_AUDIO_AMRBandModeNB2",
2126 "OMX_AUDIO_AMRBandModeNB3",
2127 "OMX_AUDIO_AMRBandModeNB4",
2128 "OMX_AUDIO_AMRBandModeNB5",
2129 "OMX_AUDIO_AMRBandModeNB6",
2130 "OMX_AUDIO_AMRBandModeNB7",
2131 "OMX_AUDIO_AMRBandModeWB0",
2132 "OMX_AUDIO_AMRBandModeWB1",
2133 "OMX_AUDIO_AMRBandModeWB2",
2134 "OMX_AUDIO_AMRBandModeWB3",
2135 "OMX_AUDIO_AMRBandModeWB4",
2136 "OMX_AUDIO_AMRBandModeWB5",
2137 "OMX_AUDIO_AMRBandModeWB6",
2138 "OMX_AUDIO_AMRBandModeWB7",
2139 "OMX_AUDIO_AMRBandModeWB8",
2140 };
2141
2142 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2143
2144 if (type < 0 || (size_t)type >= numNames) {
2145 return "UNKNOWN";
2146 } else {
2147 return kNames[type];
2148 }
2149}
2150
2151static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2152 static const char *kNames[] = {
2153 "OMX_AUDIO_AMRFrameFormatConformance",
2154 "OMX_AUDIO_AMRFrameFormatIF1",
2155 "OMX_AUDIO_AMRFrameFormatIF2",
2156 "OMX_AUDIO_AMRFrameFormatFSF",
2157 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2158 "OMX_AUDIO_AMRFrameFormatITU",
2159 };
2160
2161 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2162
2163 if (type < 0 || (size_t)type >= numNames) {
2164 return "UNKNOWN";
2165 } else {
2166 return kNames[type];
2167 }
2168}
Andreas Huberbe06d262009-08-14 14:37:10 -07002169
2170void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2171 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002172 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002173 def.nPortIndex = portIndex;
2174
2175 status_t err = mOMX->get_parameter(
2176 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2177 CHECK_EQ(err, OK);
2178
2179 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2180
2181 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2182 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2183
2184 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2185 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2186 printf(" nBufferSize = %ld\n", def.nBufferSize);
2187
2188 switch (def.eDomain) {
2189 case OMX_PortDomainImage:
2190 {
2191 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2192
2193 printf("\n");
2194 printf(" // Image\n");
2195 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2196 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2197 printf(" nStride = %ld\n", imageDef->nStride);
2198
2199 printf(" eCompressionFormat = %s\n",
2200 imageCompressionFormatString(imageDef->eCompressionFormat));
2201
2202 printf(" eColorFormat = %s\n",
2203 colorFormatString(imageDef->eColorFormat));
2204
2205 break;
2206 }
2207
2208 case OMX_PortDomainVideo:
2209 {
2210 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2211
2212 printf("\n");
2213 printf(" // Video\n");
2214 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2215 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2216 printf(" nStride = %ld\n", videoDef->nStride);
2217
2218 printf(" eCompressionFormat = %s\n",
2219 videoCompressionFormatString(videoDef->eCompressionFormat));
2220
2221 printf(" eColorFormat = %s\n",
2222 colorFormatString(videoDef->eColorFormat));
2223
2224 break;
2225 }
2226
2227 case OMX_PortDomainAudio:
2228 {
2229 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2230
2231 printf("\n");
2232 printf(" // Audio\n");
2233 printf(" eEncoding = %s\n",
2234 audioCodingTypeString(audioDef->eEncoding));
2235
2236 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2237 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002238 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002239 params.nPortIndex = portIndex;
2240
2241 err = mOMX->get_parameter(
2242 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2243 CHECK_EQ(err, OK);
2244
2245 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
2246 printf(" nChannels = %ld\n", params.nChannels);
2247 printf(" bInterleaved = %d\n", params.bInterleaved);
2248 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
2249
2250 printf(" eNumData = %s\n",
2251 params.eNumData == OMX_NumericalDataSigned
2252 ? "signed" : "unsigned");
2253
2254 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07002255 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
2256 OMX_AUDIO_PARAM_AMRTYPE amr;
2257 InitOMXParams(&amr);
2258 amr.nPortIndex = portIndex;
2259
2260 err = mOMX->get_parameter(
2261 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2262 CHECK_EQ(err, OK);
2263
2264 printf(" nChannels = %ld\n", amr.nChannels);
2265 printf(" eAMRBandMode = %s\n",
2266 amrBandModeString(amr.eAMRBandMode));
2267 printf(" eAMRFrameFormat = %s\n",
2268 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07002269 }
2270
2271 break;
2272 }
2273
2274 default:
2275 {
2276 printf(" // Unknown\n");
2277 break;
2278 }
2279 }
2280
2281 printf("}\n");
2282}
2283
2284void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
2285 mOutputFormat = new MetaData;
2286 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
2287
2288 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002289 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002290 def.nPortIndex = kPortIndexOutput;
2291
2292 status_t err = mOMX->get_parameter(
2293 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2294 CHECK_EQ(err, OK);
2295
2296 switch (def.eDomain) {
2297 case OMX_PortDomainImage:
2298 {
2299 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2300 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2301
Andreas Hubere6c40962009-09-10 14:13:30 -07002302 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002303 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
2304 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
2305 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
2306 break;
2307 }
2308
2309 case OMX_PortDomainAudio:
2310 {
2311 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
2312
Andreas Huberda050cf22009-09-02 14:01:43 -07002313 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
2314 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002315 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07002316 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002317
Andreas Huberda050cf22009-09-02 14:01:43 -07002318 err = mOMX->get_parameter(
2319 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2320 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002321
Andreas Huberda050cf22009-09-02 14:01:43 -07002322 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
2323 CHECK_EQ(params.nBitPerSample, 16);
2324 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07002325
Andreas Huberda050cf22009-09-02 14:01:43 -07002326 int32_t numChannels, sampleRate;
2327 inputFormat->findInt32(kKeyChannelCount, &numChannels);
2328 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07002329
Andreas Huberda050cf22009-09-02 14:01:43 -07002330 if ((OMX_U32)numChannels != params.nChannels) {
2331 LOGW("Codec outputs a different number of channels than "
2332 "the input stream contains.");
2333 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002334
Andreas Hubere6c40962009-09-10 14:13:30 -07002335 mOutputFormat->setCString(
2336 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07002337
2338 // Use the codec-advertised number of channels, as some
2339 // codecs appear to output stereo even if the input data is
2340 // mono.
2341 mOutputFormat->setInt32(kKeyChannelCount, params.nChannels);
2342
2343 // The codec-reported sampleRate is not reliable...
2344 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
2345 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07002346 OMX_AUDIO_PARAM_AMRTYPE amr;
2347 InitOMXParams(&amr);
2348 amr.nPortIndex = kPortIndexOutput;
2349
2350 err = mOMX->get_parameter(
2351 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2352 CHECK_EQ(err, OK);
2353
2354 CHECK_EQ(amr.nChannels, 1);
2355 mOutputFormat->setInt32(kKeyChannelCount, 1);
2356
2357 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
2358 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002359 mOutputFormat->setCString(
2360 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002361 mOutputFormat->setInt32(kKeySampleRate, 8000);
2362 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
2363 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002364 mOutputFormat->setCString(
2365 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002366 mOutputFormat->setInt32(kKeySampleRate, 16000);
2367 } else {
2368 CHECK(!"Unknown AMR band mode.");
2369 }
Andreas Huberda050cf22009-09-02 14:01:43 -07002370 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002371 mOutputFormat->setCString(
2372 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
Andreas Huberda050cf22009-09-02 14:01:43 -07002373 } else {
2374 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07002375 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002376 break;
2377 }
2378
2379 case OMX_PortDomainVideo:
2380 {
2381 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
2382
2383 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002384 mOutputFormat->setCString(
2385 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002386 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002387 mOutputFormat->setCString(
2388 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002389 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002390 mOutputFormat->setCString(
2391 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07002392 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002393 mOutputFormat->setCString(
2394 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07002395 } else {
2396 CHECK(!"Unknown compression format.");
2397 }
2398
2399 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
2400 // This component appears to be lying to me.
2401 mOutputFormat->setInt32(
2402 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
2403 mOutputFormat->setInt32(
2404 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
2405 } else {
2406 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
2407 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
2408 }
2409
2410 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
2411 break;
2412 }
2413
2414 default:
2415 {
2416 CHECK(!"should not be here, neither audio nor video.");
2417 break;
2418 }
2419 }
2420}
2421
Andreas Hubere6c40962009-09-10 14:13:30 -07002422////////////////////////////////////////////////////////////////////////////////
2423
2424status_t QueryCodecs(
2425 const sp<IOMX> &omx,
2426 const char *mime, bool queryDecoders,
2427 Vector<CodecCapabilities> *results) {
2428 results->clear();
2429
2430 for (int index = 0;; ++index) {
2431 const char *componentName;
2432
2433 if (!queryDecoders) {
2434 componentName = GetCodec(
2435 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
2436 mime, index);
2437 } else {
2438 componentName = GetCodec(
2439 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
2440 mime, index);
2441 }
2442
2443 if (!componentName) {
2444 return OK;
2445 }
2446
2447 IOMX::node_id node;
2448 status_t err = omx->allocate_node(componentName, &node);
2449
2450 if (err != OK) {
2451 continue;
2452 }
2453
2454 OMXCodec::setComponentRole(omx, node, queryDecoders, mime);
2455
2456 results->push();
2457 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
2458 caps->mComponentName = componentName;
2459
2460 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
2461 InitOMXParams(&param);
2462
2463 param.nPortIndex = queryDecoders ? 0 : 1;
2464
2465 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
2466 err = omx->get_parameter(
2467 node, OMX_IndexParamVideoProfileLevelQuerySupported,
2468 &param, sizeof(param));
2469
2470 if (err != OK) {
2471 break;
2472 }
2473
2474 CodecProfileLevel profileLevel;
2475 profileLevel.mProfile = param.eProfile;
2476 profileLevel.mLevel = param.eLevel;
2477
2478 caps->mProfileLevels.push(profileLevel);
2479 }
2480
2481 CHECK_EQ(omx->free_node(node), OK);
2482 }
2483}
2484
Andreas Huberbe06d262009-08-14 14:37:10 -07002485} // namespace android