blob: 83d3b13dd6a2b25069e1c2bad2e444bd228d25ad [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 {
Andreas Huber784202e2009-10-15 13:46:54 -070091 OMXCodecObserver() {
92 }
93
94 void setCodec(const sp<OMXCodec> &target) {
95 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -070096 }
97
98 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -070099 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700100 sp<OMXCodec> codec = mTarget.promote();
101
102 if (codec.get() != NULL) {
103 codec->on_message(msg);
104 }
105 }
106
107protected:
108 virtual ~OMXCodecObserver() {}
109
110private:
111 wp<OMXCodec> mTarget;
112
113 OMXCodecObserver(const OMXCodecObserver &);
114 OMXCodecObserver &operator=(const OMXCodecObserver &);
115};
116
117static const char *GetCodec(const CodecInfo *info, size_t numInfos,
118 const char *mime, int index) {
119 CHECK(index >= 0);
120 for(size_t i = 0; i < numInfos; ++i) {
121 if (!strcasecmp(mime, info[i].mime)) {
122 if (index == 0) {
123 return info[i].codec;
124 }
125
126 --index;
127 }
128 }
129
130 return NULL;
131}
132
Andreas Huberebf66ea2009-08-19 13:32:58 -0700133enum {
134 kAVCProfileBaseline = 0x42,
135 kAVCProfileMain = 0x4d,
136 kAVCProfileExtended = 0x58,
137 kAVCProfileHigh = 0x64,
138 kAVCProfileHigh10 = 0x6e,
139 kAVCProfileHigh422 = 0x7a,
140 kAVCProfileHigh444 = 0xf4,
141 kAVCProfileCAVLC444Intra = 0x2c
142};
143
144static const char *AVCProfileToString(uint8_t profile) {
145 switch (profile) {
146 case kAVCProfileBaseline:
147 return "Baseline";
148 case kAVCProfileMain:
149 return "Main";
150 case kAVCProfileExtended:
151 return "Extended";
152 case kAVCProfileHigh:
153 return "High";
154 case kAVCProfileHigh10:
155 return "High 10";
156 case kAVCProfileHigh422:
157 return "High 422";
158 case kAVCProfileHigh444:
159 return "High 444";
160 case kAVCProfileCAVLC444Intra:
161 return "CAVLC 444 Intra";
162 default: return "Unknown";
163 }
164}
165
Andreas Huber4c483422009-09-02 16:05:36 -0700166template<class T>
167static void InitOMXParams(T *params) {
168 params->nSize = sizeof(T);
169 params->nVersion.s.nVersionMajor = 1;
170 params->nVersion.s.nVersionMinor = 0;
171 params->nVersion.s.nRevision = 0;
172 params->nVersion.s.nStep = 0;
173}
174
Andreas Huberbe06d262009-08-14 14:37:10 -0700175// static
176sp<OMXCodec> OMXCodec::Create(
177 const sp<IOMX> &omx,
178 const sp<MetaData> &meta, bool createEncoder,
Andreas Hubere6c40962009-09-10 14:13:30 -0700179 const sp<MediaSource> &source,
180 const char *matchComponentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700181 const char *mime;
182 bool success = meta->findCString(kKeyMIMEType, &mime);
183 CHECK(success);
184
185 const char *componentName = NULL;
Andreas Huber784202e2009-10-15 13:46:54 -0700186 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Huberbe06d262009-08-14 14:37:10 -0700187 IOMX::node_id node = 0;
188 for (int index = 0;; ++index) {
189 if (createEncoder) {
190 componentName = GetCodec(
191 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
192 mime, index);
193 } else {
194 componentName = GetCodec(
195 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
196 mime, index);
197 }
198
199 if (!componentName) {
200 return NULL;
201 }
202
Andreas Hubere6c40962009-09-10 14:13:30 -0700203 // If a specific codec is requested, skip the non-matching ones.
204 if (matchComponentName && strcmp(componentName, matchComponentName)) {
205 continue;
206 }
207
Andreas Huberbe06d262009-08-14 14:37:10 -0700208 LOGV("Attempting to allocate OMX node '%s'", componentName);
209
Andreas Huber784202e2009-10-15 13:46:54 -0700210 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Huberbe06d262009-08-14 14:37:10 -0700211 if (err == OK) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700212 LOGV("Successfully allocated OMX node '%s'", componentName);
Andreas Huberbe06d262009-08-14 14:37:10 -0700213 break;
214 }
215 }
216
217 uint32_t quirks = 0;
218 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700219 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700220 }
221 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
222 quirks |= kNeedsFlushBeforeDisable;
223 }
224 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
225 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700226 quirks |= kRequiresFlushCompleteEmulation;
Andreas Huberbe06d262009-08-14 14:37:10 -0700227 }
228 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
229 quirks |= kRequiresLoadedToIdleAfterAllocation;
230 quirks |= kRequiresAllocateBufferOnInputPorts;
231 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700232 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
233 // XXX Required on P....on only.
Andreas Huber0446b822009-09-15 15:50:31 -0700234 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700235 quirks |= kRequiresAllocateBufferOnOutputPorts;
236 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700237
Andreas Huber2dc64d82009-09-11 12:58:53 -0700238 if (!strncmp(componentName, "OMX.TI.", 7)) {
239 // Apparently I must not use OMX_UseBuffer on either input or
240 // output ports on any of the TI components or quote:
241 // "(I) may have unexpected problem (sic) which can be timing related
242 // and hard to reproduce."
243
244 quirks |= kRequiresAllocateBufferOnInputPorts;
245 quirks |= kRequiresAllocateBufferOnOutputPorts;
246 }
247
Andreas Huberbe06d262009-08-14 14:37:10 -0700248 sp<OMXCodec> codec = new OMXCodec(
249 omx, node, quirks, createEncoder, mime, componentName,
250 source);
251
Andreas Huber784202e2009-10-15 13:46:54 -0700252 observer->setCodec(codec);
253
Andreas Huberbe06d262009-08-14 14:37:10 -0700254 uint32_t type;
255 const void *data;
256 size_t size;
257 if (meta->findData(kKeyESDS, &type, &data, &size)) {
258 ESDS esds((const char *)data, size);
259 CHECK_EQ(esds.InitCheck(), OK);
260
261 const void *codec_specific_data;
262 size_t codec_specific_data_size;
263 esds.getCodecSpecificInfo(
264 &codec_specific_data, &codec_specific_data_size);
265
266 printf("found codec-specific data of size %d\n",
267 codec_specific_data_size);
268
269 codec->addCodecSpecificData(
270 codec_specific_data, codec_specific_data_size);
271 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
272 printf("found avcc of size %d\n", size);
273
Andreas Huberebf66ea2009-08-19 13:32:58 -0700274 // Parse the AVCDecoderConfigurationRecord
275
276 const uint8_t *ptr = (const uint8_t *)data;
277
278 CHECK(size >= 7);
279 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
280 uint8_t profile = ptr[1];
281 uint8_t level = ptr[3];
282
283 CHECK((ptr[4] >> 2) == 0x3f); // reserved
284
285 size_t lengthSize = 1 + (ptr[4] & 3);
286
287 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
288 // violates it...
289 // CHECK((ptr[5] >> 5) == 7); // reserved
290
291 size_t numSeqParameterSets = ptr[5] & 31;
292
293 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700294 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700295
296 for (size_t i = 0; i < numSeqParameterSets; ++i) {
297 CHECK(size >= 2);
298 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700299
300 ptr += 2;
301 size -= 2;
302
Andreas Huberbe06d262009-08-14 14:37:10 -0700303 CHECK(size >= length);
304
305 codec->addCodecSpecificData(ptr, length);
306
307 ptr += length;
308 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700309 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700310
Andreas Huberebf66ea2009-08-19 13:32:58 -0700311 CHECK(size >= 1);
312 size_t numPictureParameterSets = *ptr;
313 ++ptr;
314 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700315
Andreas Huberebf66ea2009-08-19 13:32:58 -0700316 for (size_t i = 0; i < numPictureParameterSets; ++i) {
317 CHECK(size >= 2);
318 size_t length = U16_AT(ptr);
319
320 ptr += 2;
321 size -= 2;
322
323 CHECK(size >= length);
324
325 codec->addCodecSpecificData(ptr, length);
326
327 ptr += length;
328 size -= length;
329 }
330
Andreas Huber53a76bd2009-10-06 16:20:44 -0700331 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700332 (int)profile, AVCProfileToString(profile), (int)level / 10);
333
334 if (!strcmp(componentName, "OMX.TI.Video.Decoder")
335 && (profile != kAVCProfileBaseline || level > 39)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700336 // This stream exceeds the decoder's capabilities. The decoder
337 // does not handle this gracefully and would clobber the heap
338 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700339
340 LOGE("Profile and/or level exceed the decoder's capabilities.");
341 return NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -0700342 }
343 }
344
Andreas Hubere6c40962009-09-10 14:13:30 -0700345 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700346 codec->setAMRFormat();
347 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700348 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
Andreas Huberee606e62009-09-08 10:19:21 -0700349 codec->setAMRWBFormat();
350 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700351 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700352 int32_t numChannels, sampleRate;
353 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
354 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
355
356 codec->setAACFormat(numChannels, sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700357 }
358 if (!strncasecmp(mime, "video/", 6)) {
359 int32_t width, height;
360 bool success = meta->findInt32(kKeyWidth, &width);
361 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700362 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700363
364 if (createEncoder) {
365 codec->setVideoInputFormat(mime, width, height);
366 } else {
367 codec->setVideoOutputFormat(mime, width, height);
368 }
369 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700370 if (!strcasecmp(mime, MEDIA_MIMETYPE_IMAGE_JPEG)
Andreas Huberbe06d262009-08-14 14:37:10 -0700371 && !strcmp(componentName, "OMX.TI.JPEG.decode")) {
372 OMX_COLOR_FORMATTYPE format =
373 OMX_COLOR_Format32bitARGB8888;
374 // OMX_COLOR_FormatYUV420PackedPlanar;
375 // OMX_COLOR_FormatCbYCrY;
376 // OMX_COLOR_FormatYUV411Planar;
377
378 int32_t width, height;
379 bool success = meta->findInt32(kKeyWidth, &width);
380 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700381
382 int32_t compressedSize;
383 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700384 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700385
386 CHECK(success);
387 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700388
389 codec->setImageOutputFormat(format, width, height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700390 codec->setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700391 }
392
Andreas Huberda050cf22009-09-02 14:01:43 -0700393 int32_t maxInputSize;
394 if (createEncoder && meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
395 codec->setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
396 }
397
398 if (!strcmp(componentName, "OMX.TI.AMR.encode")
399 || !strcmp(componentName, "OMX.TI.WBAMR.encode")) {
400 codec->setMinBufferSize(kPortIndexOutput, 8192); // XXX
401 }
402
Andreas Huberbe06d262009-08-14 14:37:10 -0700403 codec->initOutputFormat(meta);
404
405 return codec;
406}
407
Andreas Huberda050cf22009-09-02 14:01:43 -0700408void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
409 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700410 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700411 def.nPortIndex = portIndex;
412
Andreas Huber784202e2009-10-15 13:46:54 -0700413 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700414 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
415 CHECK_EQ(err, OK);
416
417 if (def.nBufferSize < size) {
418 def.nBufferSize = size;
419
420 }
421
Andreas Huber784202e2009-10-15 13:46:54 -0700422 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700423 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
424 CHECK_EQ(err, OK);
425}
426
Andreas Huberbe06d262009-08-14 14:37:10 -0700427status_t OMXCodec::setVideoPortFormatType(
428 OMX_U32 portIndex,
429 OMX_VIDEO_CODINGTYPE compressionFormat,
430 OMX_COLOR_FORMATTYPE colorFormat) {
431 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700432 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700433 format.nPortIndex = portIndex;
434 format.nIndex = 0;
435 bool found = false;
436
437 OMX_U32 index = 0;
438 for (;;) {
439 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700440 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700441 mNode, OMX_IndexParamVideoPortFormat,
442 &format, sizeof(format));
443
444 if (err != OK) {
445 return err;
446 }
447
448 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700449 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700450
451#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700452 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700453 portIndex,
454 index, format.eCompressionFormat, format.eColorFormat);
455#endif
456
457 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
458 if (portIndex == kPortIndexInput
459 && colorFormat == format.eColorFormat) {
460 // eCompressionFormat does not seem right.
461 found = true;
462 break;
463 }
464 if (portIndex == kPortIndexOutput
465 && compressionFormat == format.eCompressionFormat) {
466 // eColorFormat does not seem right.
467 found = true;
468 break;
469 }
470 }
471
472 if (format.eCompressionFormat == compressionFormat
473 && format.eColorFormat == colorFormat) {
474 found = true;
475 break;
476 }
477
478 ++index;
479 }
480
481 if (!found) {
482 return UNKNOWN_ERROR;
483 }
484
Andreas Huber53a76bd2009-10-06 16:20:44 -0700485 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700486 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700487 mNode, OMX_IndexParamVideoPortFormat,
488 &format, sizeof(format));
489
490 return err;
491}
492
493void OMXCodec::setVideoInputFormat(
494 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700495 CODEC_LOGV("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700496
497 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700498 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700499 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700500 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700501 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700502 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700503 compressionFormat = OMX_VIDEO_CodingH263;
504 } else {
505 LOGE("Not a supported video mime type: %s", mime);
506 CHECK(!"Should not be here. Not a supported video mime type.");
507 }
508
509 OMX_COLOR_FORMATTYPE colorFormat =
510 0 ? OMX_COLOR_FormatYCbYCr : OMX_COLOR_FormatCbYCrY;
511
512 if (!strncmp("OMX.qcom.video.encoder.", mComponentName, 23)) {
513 colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
514 }
515
516 setVideoPortFormatType(
517 kPortIndexInput, OMX_VIDEO_CodingUnused,
518 colorFormat);
519
520 setVideoPortFormatType(
521 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused);
522
523 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700524 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700525 def.nPortIndex = kPortIndexOutput;
526
Andreas Huber4c483422009-09-02 16:05:36 -0700527 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
528
Andreas Huber784202e2009-10-15 13:46:54 -0700529 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700530 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
531
532 CHECK_EQ(err, OK);
533 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
534
535 video_def->nFrameWidth = width;
536 video_def->nFrameHeight = height;
537
538 video_def->eCompressionFormat = compressionFormat;
539 video_def->eColorFormat = OMX_COLOR_FormatUnused;
540
Andreas Huber784202e2009-10-15 13:46:54 -0700541 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700542 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
543 CHECK_EQ(err, OK);
544
545 ////////////////////////////////////////////////////////////////////////////
546
Andreas Huber4c483422009-09-02 16:05:36 -0700547 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700548 def.nPortIndex = kPortIndexInput;
549
Andreas Huber784202e2009-10-15 13:46:54 -0700550 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700551 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
552 CHECK_EQ(err, OK);
553
554 def.nBufferSize = (width * height * 2); // (width * height * 3) / 2;
Andreas Huber53a76bd2009-10-06 16:20:44 -0700555 CODEC_LOGV("Setting nBufferSize = %ld", def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700556
557 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
558
559 video_def->nFrameWidth = width;
560 video_def->nFrameHeight = height;
561 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
562 video_def->eColorFormat = colorFormat;
563
Andreas Huber784202e2009-10-15 13:46:54 -0700564 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700565 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
566 CHECK_EQ(err, OK);
567}
568
569void OMXCodec::setVideoOutputFormat(
570 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700571 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700572
Andreas Huberbe06d262009-08-14 14:37:10 -0700573 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700574 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700575 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700576 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700577 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700578 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700579 compressionFormat = OMX_VIDEO_CodingH263;
580 } else {
581 LOGE("Not a supported video mime type: %s", mime);
582 CHECK(!"Should not be here. Not a supported video mime type.");
583 }
584
585 setVideoPortFormatType(
586 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
587
588#if 1
589 {
590 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700591 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700592 format.nPortIndex = kPortIndexOutput;
593 format.nIndex = 0;
594
Andreas Huber784202e2009-10-15 13:46:54 -0700595 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700596 mNode, OMX_IndexParamVideoPortFormat,
597 &format, sizeof(format));
598 CHECK_EQ(err, OK);
599 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
600
601 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
602
603 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
604 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
605 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
606 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
607
Andreas Huber784202e2009-10-15 13:46:54 -0700608 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700609 mNode, OMX_IndexParamVideoPortFormat,
610 &format, sizeof(format));
611 CHECK_EQ(err, OK);
612 }
613#endif
614
615 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700616 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700617 def.nPortIndex = kPortIndexInput;
618
Andreas Huber4c483422009-09-02 16:05:36 -0700619 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
620
Andreas Huber784202e2009-10-15 13:46:54 -0700621 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700622 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
623
624 CHECK_EQ(err, OK);
625
626#if 1
627 // XXX Need a (much) better heuristic to compute input buffer sizes.
628 const size_t X = 64 * 1024;
629 if (def.nBufferSize < X) {
630 def.nBufferSize = X;
631 }
632#endif
633
634 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
635
636 video_def->nFrameWidth = width;
637 video_def->nFrameHeight = height;
638
639 video_def->eColorFormat = OMX_COLOR_FormatUnused;
640
Andreas Huber784202e2009-10-15 13:46:54 -0700641 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700642 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
643 CHECK_EQ(err, OK);
644
645 ////////////////////////////////////////////////////////////////////////////
646
Andreas Huber4c483422009-09-02 16:05:36 -0700647 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700648 def.nPortIndex = kPortIndexOutput;
649
Andreas Huber784202e2009-10-15 13:46:54 -0700650 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700651 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
652 CHECK_EQ(err, OK);
653 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
654
655#if 0
656 def.nBufferSize =
657 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
658#endif
659
660 video_def->nFrameWidth = width;
661 video_def->nFrameHeight = height;
662
Andreas Huber784202e2009-10-15 13:46:54 -0700663 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700664 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
665 CHECK_EQ(err, OK);
666}
667
668
669OMXCodec::OMXCodec(
670 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -0700671 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -0700672 const char *mime,
673 const char *componentName,
674 const sp<MediaSource> &source)
675 : mOMX(omx),
676 mNode(node),
677 mQuirks(quirks),
678 mIsEncoder(isEncoder),
679 mMIME(strdup(mime)),
680 mComponentName(strdup(componentName)),
681 mSource(source),
682 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -0700683 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -0700684 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -0700685 mSignalledEOS(false),
686 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -0700687 mOutputPortSettingsHaveChanged(false),
Andreas Huberbe06d262009-08-14 14:37:10 -0700688 mSeekTimeUs(-1) {
689 mPortStatus[kPortIndexInput] = ENABLED;
690 mPortStatus[kPortIndexOutput] = ENABLED;
691
Andreas Huber4c483422009-09-02 16:05:36 -0700692 setComponentRole();
693}
694
Andreas Hubere6c40962009-09-10 14:13:30 -0700695// static
696void OMXCodec::setComponentRole(
697 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
698 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -0700699 struct MimeToRole {
700 const char *mime;
701 const char *decoderRole;
702 const char *encoderRole;
703 };
704
705 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700706 { MEDIA_MIMETYPE_AUDIO_MPEG,
707 "audio_decoder.mp3", "audio_encoder.mp3" },
708 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
709 "audio_decoder.amrnb", "audio_encoder.amrnb" },
710 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
711 "audio_decoder.amrwb", "audio_encoder.amrwb" },
712 { MEDIA_MIMETYPE_AUDIO_AAC,
713 "audio_decoder.aac", "audio_encoder.aac" },
714 { MEDIA_MIMETYPE_VIDEO_AVC,
715 "video_decoder.avc", "video_encoder.avc" },
716 { MEDIA_MIMETYPE_VIDEO_MPEG4,
717 "video_decoder.mpeg4", "video_encoder.mpeg4" },
718 { MEDIA_MIMETYPE_VIDEO_H263,
719 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -0700720 };
721
722 static const size_t kNumMimeToRole =
723 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
724
725 size_t i;
726 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -0700727 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -0700728 break;
729 }
730 }
731
732 if (i == kNumMimeToRole) {
733 return;
734 }
735
736 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -0700737 isEncoder ? kMimeToRole[i].encoderRole
738 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -0700739
740 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -0700741 OMX_PARAM_COMPONENTROLETYPE roleParams;
742 InitOMXParams(&roleParams);
743
744 strncpy((char *)roleParams.cRole,
745 role, OMX_MAX_STRINGNAME_SIZE - 1);
746
747 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
748
Andreas Huber784202e2009-10-15 13:46:54 -0700749 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -0700750 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -0700751 &roleParams, sizeof(roleParams));
752
753 if (err != OK) {
754 LOGW("Failed to set standard component role '%s'.", role);
755 }
756 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700757}
758
Andreas Hubere6c40962009-09-10 14:13:30 -0700759void OMXCodec::setComponentRole() {
760 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
761}
762
Andreas Huberbe06d262009-08-14 14:37:10 -0700763OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700764 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -0700765
Andreas Huber784202e2009-10-15 13:46:54 -0700766 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -0700767 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)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700788 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -0700789 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) {
Andreas Huber784202e2009-10-15 13:46:54 -0700797 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -0700798 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
Andreas Huber784202e2009-10-15 13:46:54 -0700834 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700835 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)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700851 err = mOMX->allocateBufferWithBackup(
Andreas Huberbe06d262009-08-14 14:37:10 -0700852 mNode, portIndex, mem, &buffer);
Andreas Huber446f44f2009-08-25 17:23:44 -0700853 } else if (portIndex == kPortIndexOutput
854 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700855 err = mOMX->allocateBufferWithBackup(
Andreas Huber2dc64d82009-09-11 12:58:53 -0700856 mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700857 } else {
Andreas Huber784202e2009-10-15 13:46:54 -0700858 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700859 }
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 =
Andreas Huber784202e2009-10-15 13:46:54 -0700925 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700926 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 =
Andreas Huber784202e2009-10-15 13:46:54 -0700971 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700972 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 =
Andreas Huber784202e2009-10-15 13:46:54 -07001140 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001141 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) {
Andreas Huber784202e2009-10-15 13:46:54 -07001180 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001181 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
Andreas Huber784202e2009-10-15 13:46:54 -07001197 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001198 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 =
Andreas Huber784202e2009-10-15 13:46:54 -07001280 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001281
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 =
Andreas Huber784202e2009-10-15 13:46:54 -07001340 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001341 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 =
Andreas Huber784202e2009-10-15 13:46:54 -07001353 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001354 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 =
Andreas Huber784202e2009-10-15 13:46:54 -07001366 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001367 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 Huber784202e2009-10-15 13:46:54 -07001418 status_t err = mOMX->emptyBuffer(
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 Huber784202e2009-10-15 13:46:54 -07001464 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001465 info->mBuffer, srcLength);
Andreas Huber784202e2009-10-15 13:46:54 -07001466 CODEC_LOGV("Calling emptyBuffer 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
Andreas Huber784202e2009-10-15 13:46:54 -07001476 err = mOMX->emptyBuffer(
Andreas Huber3f427072009-10-08 11:02:27 -07001477 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 Huber784202e2009-10-15 13:46:54 -07001498 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber3f427072009-10-08 11:02:27 -07001499 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
Andreas Huber784202e2009-10-15 13:46:54 -07001543 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001544 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
Andreas Huber784202e2009-10-15 13:46:54 -07001564 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001565 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 =
Andreas Huber784202e2009-10-15 13:46:54 -07001577 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07001578
1579 CHECK_EQ(err, OK);
1580
1581 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1582 def.eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
1583
Andreas Huber784202e2009-10-15 13:46:54 -07001584 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07001585 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 =
Andreas Huber784202e2009-10-15 13:46:54 -07001608 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberee606e62009-09-08 10:19:21 -07001609
1610 CHECK_EQ(err, OK);
1611
1612 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1613 def.eAMRBandMode = OMX_AUDIO_AMRBandModeWB0;
1614
Andreas Huber784202e2009-10-15 13:46:54 -07001615 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberee606e62009-09-08 10:19:21 -07001616 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 Huber784202e2009-10-15 13:46:54 -07001640 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001641 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 Huber784202e2009-10-15 13:46:54 -07001648 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001649 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
Andreas Huber784202e2009-10-15 13:46:54 -07001672 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001673 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 Huber784202e2009-10-15 13:46:54 -07001721 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001722 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 Huber784202e2009-10-15 13:46:54 -07001732 status_t err = mOMX->getParameter(
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
Andreas Huber784202e2009-10-15 13:46:54 -07001746 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001747 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 =
Andreas Huber784202e2009-10-15 13:46:54 -07001837 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001838 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
Andreas Huber784202e2009-10-15 13:46:54 -07002175 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002176 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
Andreas Huber784202e2009-10-15 13:46:54 -07002241 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002242 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
Andreas Huber784202e2009-10-15 13:46:54 -07002260 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07002261 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
Andreas Huber784202e2009-10-15 13:46:54 -07002292 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002293 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 Huber784202e2009-10-15 13:46:54 -07002318 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002319 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
Andreas Huber784202e2009-10-15 13:46:54 -07002350 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07002351 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
Andreas Huber784202e2009-10-15 13:46:54 -07002447 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07002448 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07002449 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07002450
2451 if (err != OK) {
2452 continue;
2453 }
2454
2455 OMXCodec::setComponentRole(omx, node, queryDecoders, mime);
2456
2457 results->push();
2458 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
2459 caps->mComponentName = componentName;
2460
2461 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
2462 InitOMXParams(&param);
2463
2464 param.nPortIndex = queryDecoders ? 0 : 1;
2465
2466 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07002467 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07002468 node, OMX_IndexParamVideoProfileLevelQuerySupported,
2469 &param, sizeof(param));
2470
2471 if (err != OK) {
2472 break;
2473 }
2474
2475 CodecProfileLevel profileLevel;
2476 profileLevel.mProfile = param.eProfile;
2477 profileLevel.mLevel = param.eLevel;
2478
2479 caps->mProfileLevels.push(profileLevel);
2480 }
2481
Andreas Huber784202e2009-10-15 13:46:54 -07002482 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07002483 }
2484}
2485
Andreas Huberbe06d262009-08-14 14:37:10 -07002486} // namespace android