blob: 41a9fe9e38ec88de0deef8021080cd6392236173 [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 Hubere13526a2009-10-22 10:43:34 -0700175static bool IsSoftwareCodec(const char *componentName) {
176 if (!strncmp("OMX.PV.", componentName, 7)) {
177 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700178 }
179
Andreas Hubere13526a2009-10-22 10:43:34 -0700180 return false;
181}
182
183static int CompareSoftwareCodecsFirst(
184 const String8 *elem1, const String8 *elem2) {
185 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
186 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
187
188 if (isSoftwareCodec1) {
189 if (isSoftwareCodec2) { return 0; }
190 return -1;
191 }
192
193 if (isSoftwareCodec2) {
194 return 1;
195 }
196
197 return 0;
198}
199
200// static
201uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700202 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700203
Andreas Huberbe06d262009-08-14 14:37:10 -0700204 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700205 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700206 }
207 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
208 quirks |= kNeedsFlushBeforeDisable;
209 }
210 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
211 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700212 quirks |= kRequiresFlushCompleteEmulation;
Andreas Huberbe06d262009-08-14 14:37:10 -0700213 }
214 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
215 quirks |= kRequiresLoadedToIdleAfterAllocation;
216 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700217 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huberbe06d262009-08-14 14:37:10 -0700218 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700219 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
220 // XXX Required on P....on only.
Andreas Huber6e50a8c2009-10-26 11:11:02 -0700221 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700222 quirks |= kRequiresAllocateBufferOnOutputPorts;
223 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700224
Andreas Huber2dc64d82009-09-11 12:58:53 -0700225 if (!strncmp(componentName, "OMX.TI.", 7)) {
226 // Apparently I must not use OMX_UseBuffer on either input or
227 // output ports on any of the TI components or quote:
228 // "(I) may have unexpected problem (sic) which can be timing related
229 // and hard to reproduce."
230
231 quirks |= kRequiresAllocateBufferOnInputPorts;
232 quirks |= kRequiresAllocateBufferOnOutputPorts;
233 }
234
Andreas Hubere13526a2009-10-22 10:43:34 -0700235 return quirks;
236}
237
238// static
239void OMXCodec::findMatchingCodecs(
240 const char *mime,
241 bool createEncoder, const char *matchComponentName,
242 uint32_t flags,
243 Vector<String8> *matchingCodecs) {
244 matchingCodecs->clear();
245
246 for (int index = 0;; ++index) {
247 const char *componentName;
248
249 if (createEncoder) {
250 componentName = GetCodec(
251 kEncoderInfo,
252 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
253 mime, index);
254 } else {
255 componentName = GetCodec(
256 kDecoderInfo,
257 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
258 mime, index);
259 }
260
261 if (!componentName) {
262 break;
263 }
264
265 // If a specific codec is requested, skip the non-matching ones.
266 if (matchComponentName && strcmp(componentName, matchComponentName)) {
267 continue;
268 }
269
270 matchingCodecs->push(String8(componentName));
271 }
272
273 if (flags & kPreferSoftwareCodecs) {
274 matchingCodecs->sort(CompareSoftwareCodecsFirst);
275 }
276}
277
278// static
279sp<OMXCodec> OMXCodec::Create(
280 const sp<IOMX> &omx,
281 const sp<MetaData> &meta, bool createEncoder,
282 const sp<MediaSource> &source,
283 const char *matchComponentName,
284 uint32_t flags) {
285 const char *mime;
286 bool success = meta->findCString(kKeyMIMEType, &mime);
287 CHECK(success);
288
289 Vector<String8> matchingCodecs;
290 findMatchingCodecs(
291 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
292
293 if (matchingCodecs.isEmpty()) {
294 return NULL;
295 }
296
297 sp<OMXCodecObserver> observer = new OMXCodecObserver;
298 IOMX::node_id node = 0;
299 success = false;
300
301 const char *componentName;
302 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
303 componentName = matchingCodecs[i].string();
304
305 LOGV("Attempting to allocate OMX node '%s'", componentName);
306
307 status_t err = omx->allocateNode(componentName, observer, &node);
308 if (err == OK) {
309 LOGV("Successfully allocated OMX node '%s'", componentName);
310
311 success = true;
312 break;
313 }
314 }
315
316 if (!success) {
317 return NULL;
318 }
319
Andreas Huberbe06d262009-08-14 14:37:10 -0700320 sp<OMXCodec> codec = new OMXCodec(
Andreas Hubere13526a2009-10-22 10:43:34 -0700321 omx, node, getComponentQuirks(componentName),
322 createEncoder, mime, componentName,
Andreas Huberbe06d262009-08-14 14:37:10 -0700323 source);
324
Andreas Huber784202e2009-10-15 13:46:54 -0700325 observer->setCodec(codec);
326
Andreas Huberbe06d262009-08-14 14:37:10 -0700327 uint32_t type;
328 const void *data;
329 size_t size;
330 if (meta->findData(kKeyESDS, &type, &data, &size)) {
331 ESDS esds((const char *)data, size);
332 CHECK_EQ(esds.InitCheck(), OK);
333
334 const void *codec_specific_data;
335 size_t codec_specific_data_size;
336 esds.getCodecSpecificInfo(
337 &codec_specific_data, &codec_specific_data_size);
338
339 printf("found codec-specific data of size %d\n",
340 codec_specific_data_size);
341
342 codec->addCodecSpecificData(
343 codec_specific_data, codec_specific_data_size);
344 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
345 printf("found avcc of size %d\n", size);
346
Andreas Huberebf66ea2009-08-19 13:32:58 -0700347 // Parse the AVCDecoderConfigurationRecord
348
349 const uint8_t *ptr = (const uint8_t *)data;
350
351 CHECK(size >= 7);
352 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
353 uint8_t profile = ptr[1];
354 uint8_t level = ptr[3];
355
356 CHECK((ptr[4] >> 2) == 0x3f); // reserved
357
358 size_t lengthSize = 1 + (ptr[4] & 3);
359
360 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
361 // violates it...
362 // CHECK((ptr[5] >> 5) == 7); // reserved
363
364 size_t numSeqParameterSets = ptr[5] & 31;
365
366 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700367 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700368
369 for (size_t i = 0; i < numSeqParameterSets; ++i) {
370 CHECK(size >= 2);
371 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700372
373 ptr += 2;
374 size -= 2;
375
Andreas Huberbe06d262009-08-14 14:37:10 -0700376 CHECK(size >= length);
377
378 codec->addCodecSpecificData(ptr, length);
379
380 ptr += length;
381 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700382 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700383
Andreas Huberebf66ea2009-08-19 13:32:58 -0700384 CHECK(size >= 1);
385 size_t numPictureParameterSets = *ptr;
386 ++ptr;
387 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700388
Andreas Huberebf66ea2009-08-19 13:32:58 -0700389 for (size_t i = 0; i < numPictureParameterSets; ++i) {
390 CHECK(size >= 2);
391 size_t length = U16_AT(ptr);
392
393 ptr += 2;
394 size -= 2;
395
396 CHECK(size >= length);
397
398 codec->addCodecSpecificData(ptr, length);
399
400 ptr += length;
401 size -= length;
402 }
403
Andreas Huber53a76bd2009-10-06 16:20:44 -0700404 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700405 (int)profile, AVCProfileToString(profile), (int)level / 10);
406
407 if (!strcmp(componentName, "OMX.TI.Video.Decoder")
408 && (profile != kAVCProfileBaseline || level > 39)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700409 // This stream exceeds the decoder's capabilities. The decoder
410 // does not handle this gracefully and would clobber the heap
411 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700412
413 LOGE("Profile and/or level exceed the decoder's capabilities.");
414 return NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -0700415 }
416 }
417
Andreas Hubere6c40962009-09-10 14:13:30 -0700418 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700419 codec->setAMRFormat();
420 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700421 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
Andreas Huberee606e62009-09-08 10:19:21 -0700422 codec->setAMRWBFormat();
423 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700424 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700425 int32_t numChannels, sampleRate;
426 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
427 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
428
429 codec->setAACFormat(numChannels, sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700430 }
431 if (!strncasecmp(mime, "video/", 6)) {
432 int32_t width, height;
433 bool success = meta->findInt32(kKeyWidth, &width);
434 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700435 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700436
437 if (createEncoder) {
438 codec->setVideoInputFormat(mime, width, height);
439 } else {
440 codec->setVideoOutputFormat(mime, width, height);
441 }
442 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700443 if (!strcasecmp(mime, MEDIA_MIMETYPE_IMAGE_JPEG)
Andreas Huberbe06d262009-08-14 14:37:10 -0700444 && !strcmp(componentName, "OMX.TI.JPEG.decode")) {
445 OMX_COLOR_FORMATTYPE format =
446 OMX_COLOR_Format32bitARGB8888;
447 // OMX_COLOR_FormatYUV420PackedPlanar;
448 // OMX_COLOR_FormatCbYCrY;
449 // OMX_COLOR_FormatYUV411Planar;
450
451 int32_t width, height;
452 bool success = meta->findInt32(kKeyWidth, &width);
453 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700454
455 int32_t compressedSize;
456 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700457 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700458
459 CHECK(success);
460 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700461
462 codec->setImageOutputFormat(format, width, height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700463 codec->setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700464 }
465
Andreas Huberda050cf22009-09-02 14:01:43 -0700466 int32_t maxInputSize;
467 if (createEncoder && meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
468 codec->setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
469 }
470
471 if (!strcmp(componentName, "OMX.TI.AMR.encode")
472 || !strcmp(componentName, "OMX.TI.WBAMR.encode")) {
473 codec->setMinBufferSize(kPortIndexOutput, 8192); // XXX
474 }
475
Andreas Huberbe06d262009-08-14 14:37:10 -0700476 codec->initOutputFormat(meta);
477
478 return codec;
479}
480
Andreas Huberda050cf22009-09-02 14:01:43 -0700481void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
482 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700483 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700484 def.nPortIndex = portIndex;
485
Andreas Huber784202e2009-10-15 13:46:54 -0700486 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700487 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
488 CHECK_EQ(err, OK);
489
490 if (def.nBufferSize < size) {
491 def.nBufferSize = size;
492
493 }
494
Andreas Huber784202e2009-10-15 13:46:54 -0700495 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700496 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
497 CHECK_EQ(err, OK);
498}
499
Andreas Huberbe06d262009-08-14 14:37:10 -0700500status_t OMXCodec::setVideoPortFormatType(
501 OMX_U32 portIndex,
502 OMX_VIDEO_CODINGTYPE compressionFormat,
503 OMX_COLOR_FORMATTYPE colorFormat) {
504 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700505 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700506 format.nPortIndex = portIndex;
507 format.nIndex = 0;
508 bool found = false;
509
510 OMX_U32 index = 0;
511 for (;;) {
512 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700513 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700514 mNode, OMX_IndexParamVideoPortFormat,
515 &format, sizeof(format));
516
517 if (err != OK) {
518 return err;
519 }
520
521 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700522 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700523
524#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700525 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700526 portIndex,
527 index, format.eCompressionFormat, format.eColorFormat);
528#endif
529
530 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
531 if (portIndex == kPortIndexInput
532 && colorFormat == format.eColorFormat) {
533 // eCompressionFormat does not seem right.
534 found = true;
535 break;
536 }
537 if (portIndex == kPortIndexOutput
538 && compressionFormat == format.eCompressionFormat) {
539 // eColorFormat does not seem right.
540 found = true;
541 break;
542 }
543 }
544
545 if (format.eCompressionFormat == compressionFormat
546 && format.eColorFormat == colorFormat) {
547 found = true;
548 break;
549 }
550
551 ++index;
552 }
553
554 if (!found) {
555 return UNKNOWN_ERROR;
556 }
557
Andreas Huber53a76bd2009-10-06 16:20:44 -0700558 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700559 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700560 mNode, OMX_IndexParamVideoPortFormat,
561 &format, sizeof(format));
562
563 return err;
564}
565
Andreas Huberb482ce82009-10-29 12:02:48 -0700566static size_t getFrameSize(
567 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
568 switch (colorFormat) {
569 case OMX_COLOR_FormatYCbYCr:
570 case OMX_COLOR_FormatCbYCrY:
571 return width * height * 2;
572
573 case OMX_COLOR_FormatYUV420SemiPlanar:
574 return (width * height * 3) / 2;
575
576 default:
577 CHECK(!"Should not be here. Unsupported color format.");
578 break;
579 }
580}
581
Andreas Huberbe06d262009-08-14 14:37:10 -0700582void OMXCodec::setVideoInputFormat(
583 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700584 CODEC_LOGV("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700585
586 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700587 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700588 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700589 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700590 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700591 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700592 compressionFormat = OMX_VIDEO_CodingH263;
593 } else {
594 LOGE("Not a supported video mime type: %s", mime);
595 CHECK(!"Should not be here. Not a supported video mime type.");
596 }
597
598 OMX_COLOR_FORMATTYPE colorFormat =
599 0 ? OMX_COLOR_FormatYCbYCr : OMX_COLOR_FormatCbYCrY;
600
601 if (!strncmp("OMX.qcom.video.encoder.", mComponentName, 23)) {
602 colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
603 }
604
Andreas Huberb482ce82009-10-29 12:02:48 -0700605 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700606 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700607 colorFormat), OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700608
Andreas Huberb482ce82009-10-29 12:02:48 -0700609 CHECK_EQ(setVideoPortFormatType(
610 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
611 OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700612
613 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700614 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700615 def.nPortIndex = kPortIndexOutput;
616
Andreas Huber4c483422009-09-02 16:05:36 -0700617 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
618
Andreas Huber784202e2009-10-15 13:46:54 -0700619 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700620 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
621
622 CHECK_EQ(err, OK);
623 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
624
625 video_def->nFrameWidth = width;
626 video_def->nFrameHeight = height;
627
628 video_def->eCompressionFormat = compressionFormat;
629 video_def->eColorFormat = OMX_COLOR_FormatUnused;
630
Andreas Huber784202e2009-10-15 13:46:54 -0700631 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700632 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
633 CHECK_EQ(err, OK);
634
635 ////////////////////////////////////////////////////////////////////////////
636
Andreas Huber4c483422009-09-02 16:05:36 -0700637 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700638 def.nPortIndex = kPortIndexInput;
639
Andreas Huber784202e2009-10-15 13:46:54 -0700640 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700641 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
642 CHECK_EQ(err, OK);
643
Andreas Huberb482ce82009-10-29 12:02:48 -0700644 def.nBufferSize = getFrameSize(colorFormat, width, height);
Andreas Huber53a76bd2009-10-06 16:20:44 -0700645 CODEC_LOGV("Setting nBufferSize = %ld", def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700646
647 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
648
649 video_def->nFrameWidth = width;
650 video_def->nFrameHeight = height;
651 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
652 video_def->eColorFormat = colorFormat;
653
Andreas Huberb482ce82009-10-29 12:02:48 -0700654 video_def->xFramerate = 24 << 16; // XXX crucial!
655
Andreas Huber784202e2009-10-15 13:46:54 -0700656 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700657 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
658 CHECK_EQ(err, OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700659
660 switch (compressionFormat) {
661 case OMX_VIDEO_CodingMPEG4:
662 {
663 CHECK_EQ(setupMPEG4EncoderParameters(), OK);
664 break;
665 }
666
667 case OMX_VIDEO_CodingH263:
668 break;
669
670 default:
671 CHECK(!"Support for this compressionFormat to be implemented.");
672 break;
673 }
674}
675
676status_t OMXCodec::setupMPEG4EncoderParameters() {
677 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
678 InitOMXParams(&mpeg4type);
679 mpeg4type.nPortIndex = kPortIndexOutput;
680
681 status_t err = mOMX->getParameter(
682 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
683 CHECK_EQ(err, OK);
684
685 mpeg4type.nSliceHeaderSpacing = 0;
686 mpeg4type.bSVH = OMX_FALSE;
687 mpeg4type.bGov = OMX_FALSE;
688
689 mpeg4type.nAllowedPictureTypes =
690 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
691
692 mpeg4type.nPFrames = 23;
693 mpeg4type.nBFrames = 0;
694
695 mpeg4type.nIDCVLCThreshold = 0;
696 mpeg4type.bACPred = OMX_TRUE;
697 mpeg4type.nMaxPacketSize = 256;
698 mpeg4type.nTimeIncRes = 1000;
699 mpeg4type.nHeaderExtension = 0;
700 mpeg4type.bReversibleVLC = OMX_FALSE;
701
702 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileCore;
703 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2;
704
705 err = mOMX->setParameter(
706 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
707 CHECK_EQ(err, OK);
708
709 // ----------------
710
711 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
712 InitOMXParams(&bitrateType);
713 bitrateType.nPortIndex = kPortIndexOutput;
714
715 err = mOMX->getParameter(
716 mNode, OMX_IndexParamVideoBitrate,
717 &bitrateType, sizeof(bitrateType));
718 CHECK_EQ(err, OK);
719
720 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
721 bitrateType.nTargetBitrate = 1000000;
722
723 err = mOMX->setParameter(
724 mNode, OMX_IndexParamVideoBitrate,
725 &bitrateType, sizeof(bitrateType));
726 CHECK_EQ(err, OK);
727
728 // ----------------
729
730 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
731 InitOMXParams(&errorCorrectionType);
732 errorCorrectionType.nPortIndex = kPortIndexOutput;
733
734 err = mOMX->getParameter(
735 mNode, OMX_IndexParamVideoErrorCorrection,
736 &errorCorrectionType, sizeof(errorCorrectionType));
737 CHECK_EQ(err, OK);
738
739 errorCorrectionType.bEnableHEC = OMX_FALSE;
740 errorCorrectionType.bEnableResync = OMX_TRUE;
741 errorCorrectionType.nResynchMarkerSpacing = 256;
742 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
743 errorCorrectionType.bEnableRVLC = OMX_FALSE;
744
745 err = mOMX->setParameter(
746 mNode, OMX_IndexParamVideoErrorCorrection,
747 &errorCorrectionType, sizeof(errorCorrectionType));
748 CHECK_EQ(err, OK);
749
750 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700751}
752
753void OMXCodec::setVideoOutputFormat(
754 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700755 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700756
Andreas Huberbe06d262009-08-14 14:37:10 -0700757 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700758 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700759 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700760 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700761 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700762 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700763 compressionFormat = OMX_VIDEO_CodingH263;
764 } else {
765 LOGE("Not a supported video mime type: %s", mime);
766 CHECK(!"Should not be here. Not a supported video mime type.");
767 }
768
769 setVideoPortFormatType(
770 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
771
772#if 1
773 {
774 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700775 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700776 format.nPortIndex = kPortIndexOutput;
777 format.nIndex = 0;
778
Andreas Huber784202e2009-10-15 13:46:54 -0700779 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700780 mNode, OMX_IndexParamVideoPortFormat,
781 &format, sizeof(format));
782 CHECK_EQ(err, OK);
783 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
784
785 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
786
787 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
788 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
789 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
790 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
791
Andreas Huber784202e2009-10-15 13:46:54 -0700792 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700793 mNode, OMX_IndexParamVideoPortFormat,
794 &format, sizeof(format));
795 CHECK_EQ(err, OK);
796 }
797#endif
798
799 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700800 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700801 def.nPortIndex = kPortIndexInput;
802
Andreas Huber4c483422009-09-02 16:05:36 -0700803 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
804
Andreas Huber784202e2009-10-15 13:46:54 -0700805 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700806 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
807
808 CHECK_EQ(err, OK);
809
810#if 1
811 // XXX Need a (much) better heuristic to compute input buffer sizes.
812 const size_t X = 64 * 1024;
813 if (def.nBufferSize < X) {
814 def.nBufferSize = X;
815 }
816#endif
817
818 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
819
820 video_def->nFrameWidth = width;
821 video_def->nFrameHeight = height;
822
Andreas Huberb482ce82009-10-29 12:02:48 -0700823 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -0700824 video_def->eColorFormat = OMX_COLOR_FormatUnused;
825
Andreas Huber784202e2009-10-15 13:46:54 -0700826 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700827 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
828 CHECK_EQ(err, OK);
829
830 ////////////////////////////////////////////////////////////////////////////
831
Andreas Huber4c483422009-09-02 16:05:36 -0700832 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700833 def.nPortIndex = kPortIndexOutput;
834
Andreas Huber784202e2009-10-15 13:46:54 -0700835 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700836 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
837 CHECK_EQ(err, OK);
838 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
839
840#if 0
841 def.nBufferSize =
842 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
843#endif
844
845 video_def->nFrameWidth = width;
846 video_def->nFrameHeight = height;
847
Andreas Huber784202e2009-10-15 13:46:54 -0700848 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700849 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
850 CHECK_EQ(err, OK);
851}
852
853
854OMXCodec::OMXCodec(
855 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -0700856 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -0700857 const char *mime,
858 const char *componentName,
859 const sp<MediaSource> &source)
860 : mOMX(omx),
861 mNode(node),
862 mQuirks(quirks),
863 mIsEncoder(isEncoder),
864 mMIME(strdup(mime)),
865 mComponentName(strdup(componentName)),
866 mSource(source),
867 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -0700868 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -0700869 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -0700870 mSignalledEOS(false),
871 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -0700872 mOutputPortSettingsHaveChanged(false),
Andreas Huberbe06d262009-08-14 14:37:10 -0700873 mSeekTimeUs(-1) {
874 mPortStatus[kPortIndexInput] = ENABLED;
875 mPortStatus[kPortIndexOutput] = ENABLED;
876
Andreas Huber4c483422009-09-02 16:05:36 -0700877 setComponentRole();
878}
879
Andreas Hubere6c40962009-09-10 14:13:30 -0700880// static
881void OMXCodec::setComponentRole(
882 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
883 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -0700884 struct MimeToRole {
885 const char *mime;
886 const char *decoderRole;
887 const char *encoderRole;
888 };
889
890 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700891 { MEDIA_MIMETYPE_AUDIO_MPEG,
892 "audio_decoder.mp3", "audio_encoder.mp3" },
893 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
894 "audio_decoder.amrnb", "audio_encoder.amrnb" },
895 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
896 "audio_decoder.amrwb", "audio_encoder.amrwb" },
897 { MEDIA_MIMETYPE_AUDIO_AAC,
898 "audio_decoder.aac", "audio_encoder.aac" },
899 { MEDIA_MIMETYPE_VIDEO_AVC,
900 "video_decoder.avc", "video_encoder.avc" },
901 { MEDIA_MIMETYPE_VIDEO_MPEG4,
902 "video_decoder.mpeg4", "video_encoder.mpeg4" },
903 { MEDIA_MIMETYPE_VIDEO_H263,
904 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -0700905 };
906
907 static const size_t kNumMimeToRole =
908 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
909
910 size_t i;
911 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -0700912 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -0700913 break;
914 }
915 }
916
917 if (i == kNumMimeToRole) {
918 return;
919 }
920
921 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -0700922 isEncoder ? kMimeToRole[i].encoderRole
923 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -0700924
925 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -0700926 OMX_PARAM_COMPONENTROLETYPE roleParams;
927 InitOMXParams(&roleParams);
928
929 strncpy((char *)roleParams.cRole,
930 role, OMX_MAX_STRINGNAME_SIZE - 1);
931
932 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
933
Andreas Huber784202e2009-10-15 13:46:54 -0700934 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -0700935 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -0700936 &roleParams, sizeof(roleParams));
937
938 if (err != OK) {
939 LOGW("Failed to set standard component role '%s'.", role);
940 }
941 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700942}
943
Andreas Hubere6c40962009-09-10 14:13:30 -0700944void OMXCodec::setComponentRole() {
945 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
946}
947
Andreas Huberbe06d262009-08-14 14:37:10 -0700948OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700949 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -0700950
Andreas Huber784202e2009-10-15 13:46:54 -0700951 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -0700952 CHECK_EQ(err, OK);
953
954 mNode = NULL;
955 setState(DEAD);
956
957 clearCodecSpecificData();
958
959 free(mComponentName);
960 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700961
Andreas Huberbe06d262009-08-14 14:37:10 -0700962 free(mMIME);
963 mMIME = NULL;
964}
965
966status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -0700967 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -0700968
969 CHECK_EQ(mState, LOADED);
970
971 status_t err;
972 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700973 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -0700974 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700975 setState(LOADED_TO_IDLE);
976 }
977
978 err = allocateBuffers();
979 CHECK_EQ(err, OK);
980
981 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -0700982 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -0700983 CHECK_EQ(err, OK);
984
985 setState(LOADED_TO_IDLE);
986 }
987
988 while (mState != EXECUTING && mState != ERROR) {
989 mAsyncCompletion.wait(mLock);
990 }
991
992 return mState == ERROR ? UNKNOWN_ERROR : OK;
993}
994
995// static
996bool OMXCodec::isIntermediateState(State state) {
997 return state == LOADED_TO_IDLE
998 || state == IDLE_TO_EXECUTING
999 || state == EXECUTING_TO_IDLE
1000 || state == IDLE_TO_LOADED
1001 || state == RECONFIGURING;
1002}
1003
1004status_t OMXCodec::allocateBuffers() {
1005 status_t err = allocateBuffersOnPort(kPortIndexInput);
1006
1007 if (err != OK) {
1008 return err;
1009 }
1010
1011 return allocateBuffersOnPort(kPortIndexOutput);
1012}
1013
1014status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1015 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001016 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001017 def.nPortIndex = portIndex;
1018
Andreas Huber784202e2009-10-15 13:46:54 -07001019 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001020 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1021
1022 if (err != OK) {
1023 return err;
1024 }
1025
Andreas Huber5c0a9132009-08-20 11:16:40 -07001026 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
1027 mDealer[portIndex] = new MemoryDealer(totalSize);
1028
Andreas Huberbe06d262009-08-14 14:37:10 -07001029 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001030 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001031 CHECK(mem.get() != NULL);
1032
1033 IOMX::buffer_id buffer;
1034 if (portIndex == kPortIndexInput
1035 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001036 err = mOMX->allocateBufferWithBackup(
Andreas Huberbe06d262009-08-14 14:37:10 -07001037 mNode, portIndex, mem, &buffer);
Andreas Huber446f44f2009-08-25 17:23:44 -07001038 } else if (portIndex == kPortIndexOutput
1039 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001040 err = mOMX->allocateBufferWithBackup(
Andreas Huber2dc64d82009-09-11 12:58:53 -07001041 mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001042 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001043 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001044 }
1045
1046 if (err != OK) {
1047 LOGE("allocate_buffer_with_backup failed");
1048 return err;
1049 }
1050
1051 BufferInfo info;
1052 info.mBuffer = buffer;
1053 info.mOwnedByComponent = false;
1054 info.mMem = mem;
1055 info.mMediaBuffer = NULL;
1056
1057 if (portIndex == kPortIndexOutput) {
1058 info.mMediaBuffer = new MediaBuffer(mem->pointer(), mem->size());
1059 info.mMediaBuffer->setObserver(this);
1060 }
1061
1062 mPortBuffers[portIndex].push(info);
1063
Andreas Huber4c483422009-09-02 16:05:36 -07001064 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001065 portIndex == kPortIndexInput ? "input" : "output");
1066 }
1067
1068 dumpPortStatus(portIndex);
1069
1070 return OK;
1071}
1072
1073void OMXCodec::on_message(const omx_message &msg) {
1074 Mutex::Autolock autoLock(mLock);
1075
1076 switch (msg.type) {
1077 case omx_message::EVENT:
1078 {
1079 onEvent(
1080 msg.u.event_data.event, msg.u.event_data.data1,
1081 msg.u.event_data.data2);
1082
1083 break;
1084 }
1085
1086 case omx_message::EMPTY_BUFFER_DONE:
1087 {
1088 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1089
Andreas Huber4c483422009-09-02 16:05:36 -07001090 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001091
1092 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1093 size_t i = 0;
1094 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1095 ++i;
1096 }
1097
1098 CHECK(i < buffers->size());
1099 if (!(*buffers)[i].mOwnedByComponent) {
1100 LOGW("We already own input buffer %p, yet received "
1101 "an EMPTY_BUFFER_DONE.", buffer);
1102 }
1103
1104 buffers->editItemAt(i).mOwnedByComponent = false;
1105
1106 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001107 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001108
1109 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001110 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001111 CHECK_EQ(err, OK);
1112
1113 buffers->removeAt(i);
1114 } else if (mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
1115 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1116 drainInputBuffer(&buffers->editItemAt(i));
1117 }
1118
1119 break;
1120 }
1121
1122 case omx_message::FILL_BUFFER_DONE:
1123 {
1124 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1125 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1126
Andreas Huber4c483422009-09-02 16:05:36 -07001127 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001128 buffer,
1129 msg.u.extended_buffer_data.range_length,
1130 flags);
1131
Andreas Huber4c483422009-09-02 16:05:36 -07001132 CODEC_LOGV("FILL_BUFFER_DONE(timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001133 msg.u.extended_buffer_data.timestamp,
1134 msg.u.extended_buffer_data.timestamp / 1E6);
1135
1136 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1137 size_t i = 0;
1138 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1139 ++i;
1140 }
1141
1142 CHECK(i < buffers->size());
1143 BufferInfo *info = &buffers->editItemAt(i);
1144
1145 if (!info->mOwnedByComponent) {
1146 LOGW("We already own output buffer %p, yet received "
1147 "a FILL_BUFFER_DONE.", buffer);
1148 }
1149
1150 info->mOwnedByComponent = false;
1151
1152 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001153 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001154
1155 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001156 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001157 CHECK_EQ(err, OK);
1158
1159 buffers->removeAt(i);
Andreas Huberd7795892009-08-26 10:33:47 -07001160 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1161 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001162 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001163 mNoMoreOutputData = true;
1164 mBufferFilled.signal();
1165 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1166 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001167
Andreas Huberbe06d262009-08-14 14:37:10 -07001168 MediaBuffer *buffer = info->mMediaBuffer;
1169
1170 buffer->set_range(
1171 msg.u.extended_buffer_data.range_offset,
1172 msg.u.extended_buffer_data.range_length);
1173
1174 buffer->meta_data()->clear();
1175
Andreas Huberfa8de752009-10-08 10:07:49 -07001176 buffer->meta_data()->setInt64(
1177 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001178
1179 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1180 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1181 }
1182
1183 buffer->meta_data()->setPointer(
1184 kKeyPlatformPrivate,
1185 msg.u.extended_buffer_data.platform_private);
1186
1187 buffer->meta_data()->setPointer(
1188 kKeyBufferID,
1189 msg.u.extended_buffer_data.buffer);
1190
1191 mFilledBuffers.push_back(i);
1192 mBufferFilled.signal();
1193 }
1194
1195 break;
1196 }
1197
1198 default:
1199 {
1200 CHECK(!"should not be here.");
1201 break;
1202 }
1203 }
1204}
1205
1206void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1207 switch (event) {
1208 case OMX_EventCmdComplete:
1209 {
1210 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1211 break;
1212 }
1213
1214 case OMX_EventError:
1215 {
1216 LOGE("ERROR(%ld, %ld)", data1, data2);
1217
1218 setState(ERROR);
1219 break;
1220 }
1221
1222 case OMX_EventPortSettingsChanged:
1223 {
1224 onPortSettingsChanged(data1);
1225 break;
1226 }
1227
1228 case OMX_EventBufferFlag:
1229 {
Andreas Huber4c483422009-09-02 16:05:36 -07001230 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001231
1232 if (data1 == kPortIndexOutput) {
1233 mNoMoreOutputData = true;
1234 }
1235 break;
1236 }
1237
1238 default:
1239 {
Andreas Huber4c483422009-09-02 16:05:36 -07001240 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001241 break;
1242 }
1243 }
1244}
1245
Andreas Huberb1678602009-10-19 13:06:40 -07001246// Has the format changed in any way that the client would have to be aware of?
1247static bool formatHasNotablyChanged(
1248 const sp<MetaData> &from, const sp<MetaData> &to) {
1249 if (from.get() == NULL && to.get() == NULL) {
1250 return false;
1251 }
1252
Andreas Huberf68c1682009-10-21 14:01:30 -07001253 if ((from.get() == NULL && to.get() != NULL)
1254 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001255 return true;
1256 }
1257
1258 const char *mime_from, *mime_to;
1259 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1260 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1261
1262 if (strcasecmp(mime_from, mime_to)) {
1263 return true;
1264 }
1265
1266 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1267 int32_t colorFormat_from, colorFormat_to;
1268 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1269 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1270
1271 if (colorFormat_from != colorFormat_to) {
1272 return true;
1273 }
1274
1275 int32_t width_from, width_to;
1276 CHECK(from->findInt32(kKeyWidth, &width_from));
1277 CHECK(to->findInt32(kKeyWidth, &width_to));
1278
1279 if (width_from != width_to) {
1280 return true;
1281 }
1282
1283 int32_t height_from, height_to;
1284 CHECK(from->findInt32(kKeyHeight, &height_from));
1285 CHECK(to->findInt32(kKeyHeight, &height_to));
1286
1287 if (height_from != height_to) {
1288 return true;
1289 }
1290 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1291 int32_t numChannels_from, numChannels_to;
1292 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1293 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1294
1295 if (numChannels_from != numChannels_to) {
1296 return true;
1297 }
1298
1299 int32_t sampleRate_from, sampleRate_to;
1300 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1301 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1302
1303 if (sampleRate_from != sampleRate_to) {
1304 return true;
1305 }
1306 }
1307
1308 return false;
1309}
1310
Andreas Huberbe06d262009-08-14 14:37:10 -07001311void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1312 switch (cmd) {
1313 case OMX_CommandStateSet:
1314 {
1315 onStateChange((OMX_STATETYPE)data);
1316 break;
1317 }
1318
1319 case OMX_CommandPortDisable:
1320 {
1321 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001322 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001323
1324 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1325 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1326 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1327
1328 mPortStatus[portIndex] = DISABLED;
1329
1330 if (mState == RECONFIGURING) {
1331 CHECK_EQ(portIndex, kPortIndexOutput);
1332
Andreas Huberb1678602009-10-19 13:06:40 -07001333 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001334 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001335
1336 // Don't notify clients if the output port settings change
1337 // wasn't of importance to them, i.e. it may be that just the
1338 // number of buffers has changed and nothing else.
1339 mOutputPortSettingsHaveChanged =
1340 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001341
Andreas Huberbe06d262009-08-14 14:37:10 -07001342 enablePortAsync(portIndex);
1343
1344 status_t err = allocateBuffersOnPort(portIndex);
1345 CHECK_EQ(err, OK);
1346 }
1347 break;
1348 }
1349
1350 case OMX_CommandPortEnable:
1351 {
1352 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001353 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001354
1355 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1356 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1357
1358 mPortStatus[portIndex] = ENABLED;
1359
1360 if (mState == RECONFIGURING) {
1361 CHECK_EQ(portIndex, kPortIndexOutput);
1362
1363 setState(EXECUTING);
1364
1365 fillOutputBuffers();
1366 }
1367 break;
1368 }
1369
1370 case OMX_CommandFlush:
1371 {
1372 OMX_U32 portIndex = data;
1373
Andreas Huber4c483422009-09-02 16:05:36 -07001374 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001375
1376 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1377 mPortStatus[portIndex] = ENABLED;
1378
1379 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1380 mPortBuffers[portIndex].size());
1381
1382 if (mState == RECONFIGURING) {
1383 CHECK_EQ(portIndex, kPortIndexOutput);
1384
1385 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001386 } else if (mState == EXECUTING_TO_IDLE) {
1387 if (mPortStatus[kPortIndexInput] == ENABLED
1388 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001389 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001390 "transition from EXECUTING to IDLE.");
1391
1392 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1393 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1394
1395 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001396 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001397 CHECK_EQ(err, OK);
1398 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001399 } else {
1400 // We're flushing both ports in preparation for seeking.
1401
1402 if (mPortStatus[kPortIndexInput] == ENABLED
1403 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001404 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001405 " seek-time.");
1406
Andreas Huber1a77b68e2009-09-17 11:16:52 -07001407 // Clear this flag in case the decoder sent us either
1408 // the EVENT_BUFFER_FLAG(1) or an output buffer with
1409 // the EOS flag set _while_ flushing. Since we're going
1410 // to submit "fresh" input data now, this flag no longer
1411 // applies to our future.
1412 mNoMoreOutputData = false;
1413
Andreas Huberbe06d262009-08-14 14:37:10 -07001414 drainInputBuffers();
1415 fillOutputBuffers();
1416 }
1417 }
1418
1419 break;
1420 }
1421
1422 default:
1423 {
Andreas Huber4c483422009-09-02 16:05:36 -07001424 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001425 break;
1426 }
1427 }
1428}
1429
1430void OMXCodec::onStateChange(OMX_STATETYPE newState) {
1431 switch (newState) {
1432 case OMX_StateIdle:
1433 {
Andreas Huber4c483422009-09-02 16:05:36 -07001434 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001435 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001436 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001437 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1438
1439 CHECK_EQ(err, OK);
1440
1441 setState(IDLE_TO_EXECUTING);
1442 } else {
1443 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1444
1445 CHECK_EQ(
1446 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1447 mPortBuffers[kPortIndexInput].size());
1448
1449 CHECK_EQ(
1450 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1451 mPortBuffers[kPortIndexOutput].size());
1452
Andreas Huber784202e2009-10-15 13:46:54 -07001453 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001454 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1455
1456 CHECK_EQ(err, OK);
1457
1458 err = freeBuffersOnPort(kPortIndexInput);
1459 CHECK_EQ(err, OK);
1460
1461 err = freeBuffersOnPort(kPortIndexOutput);
1462 CHECK_EQ(err, OK);
1463
1464 mPortStatus[kPortIndexInput] = ENABLED;
1465 mPortStatus[kPortIndexOutput] = ENABLED;
1466
1467 setState(IDLE_TO_LOADED);
1468 }
1469 break;
1470 }
1471
1472 case OMX_StateExecuting:
1473 {
1474 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1475
Andreas Huber4c483422009-09-02 16:05:36 -07001476 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001477
1478 setState(EXECUTING);
1479
Andreas Huber42978e52009-08-27 10:08:39 -07001480 // Buffers will be submitted to the component in the first
1481 // call to OMXCodec::read as mInitialBufferSubmit is true at
1482 // this point. This ensures that this on_message call returns,
1483 // releases the lock and ::init can notice the state change and
1484 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001485 break;
1486 }
1487
1488 case OMX_StateLoaded:
1489 {
1490 CHECK_EQ(mState, IDLE_TO_LOADED);
1491
Andreas Huber4c483422009-09-02 16:05:36 -07001492 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001493
1494 setState(LOADED);
1495 break;
1496 }
1497
1498 default:
1499 {
1500 CHECK(!"should not be here.");
1501 break;
1502 }
1503 }
1504}
1505
1506// static
1507size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1508 size_t n = 0;
1509 for (size_t i = 0; i < buffers.size(); ++i) {
1510 if (!buffers[i].mOwnedByComponent) {
1511 ++n;
1512 }
1513 }
1514
1515 return n;
1516}
1517
1518status_t OMXCodec::freeBuffersOnPort(
1519 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1520 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1521
1522 status_t stickyErr = OK;
1523
1524 for (size_t i = buffers->size(); i-- > 0;) {
1525 BufferInfo *info = &buffers->editItemAt(i);
1526
1527 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1528 continue;
1529 }
1530
1531 CHECK_EQ(info->mOwnedByComponent, false);
1532
Andreas Huber92022852009-09-14 15:24:14 -07001533 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1534
Andreas Huberbe06d262009-08-14 14:37:10 -07001535 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001536 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001537
1538 if (err != OK) {
1539 stickyErr = err;
1540 }
1541
1542 if (info->mMediaBuffer != NULL) {
1543 info->mMediaBuffer->setObserver(NULL);
1544
1545 // Make sure nobody but us owns this buffer at this point.
1546 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1547
1548 info->mMediaBuffer->release();
1549 }
1550
1551 buffers->removeAt(i);
1552 }
1553
1554 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1555
1556 return stickyErr;
1557}
1558
1559void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001560 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001561
1562 CHECK_EQ(mState, EXECUTING);
1563 CHECK_EQ(portIndex, kPortIndexOutput);
1564 setState(RECONFIGURING);
1565
1566 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001567 if (!flushPortAsync(portIndex)) {
1568 onCmdComplete(OMX_CommandFlush, portIndex);
1569 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001570 } else {
1571 disablePortAsync(portIndex);
1572 }
1573}
1574
Andreas Huber404cc412009-08-25 14:26:05 -07001575bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001576 CHECK(mState == EXECUTING || mState == RECONFIGURING
1577 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001578
Andreas Huber4c483422009-09-02 16:05:36 -07001579 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001580 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1581 mPortBuffers[portIndex].size());
1582
Andreas Huberbe06d262009-08-14 14:37:10 -07001583 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1584 mPortStatus[portIndex] = SHUTTING_DOWN;
1585
Andreas Huber404cc412009-08-25 14:26:05 -07001586 if ((mQuirks & kRequiresFlushCompleteEmulation)
1587 && countBuffersWeOwn(mPortBuffers[portIndex])
1588 == mPortBuffers[portIndex].size()) {
1589 // No flush is necessary and this component fails to send a
1590 // flush-complete event in this case.
1591
1592 return false;
1593 }
1594
Andreas Huberbe06d262009-08-14 14:37:10 -07001595 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001596 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001597 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001598
1599 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001600}
1601
1602void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1603 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1604
1605 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1606 mPortStatus[portIndex] = DISABLING;
1607
1608 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001609 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001610 CHECK_EQ(err, OK);
1611
1612 freeBuffersOnPort(portIndex, true);
1613}
1614
1615void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1616 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1617
1618 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1619 mPortStatus[portIndex] = ENABLING;
1620
1621 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001622 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001623 CHECK_EQ(err, OK);
1624}
1625
1626void OMXCodec::fillOutputBuffers() {
1627 CHECK_EQ(mState, EXECUTING);
1628
1629 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1630 for (size_t i = 0; i < buffers->size(); ++i) {
1631 fillOutputBuffer(&buffers->editItemAt(i));
1632 }
1633}
1634
1635void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001636 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001637
1638 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1639 for (size_t i = 0; i < buffers->size(); ++i) {
1640 drainInputBuffer(&buffers->editItemAt(i));
1641 }
1642}
1643
1644void OMXCodec::drainInputBuffer(BufferInfo *info) {
1645 CHECK_EQ(info->mOwnedByComponent, false);
1646
1647 if (mSignalledEOS) {
1648 return;
1649 }
1650
1651 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1652 const CodecSpecificData *specific =
1653 mCodecSpecificData[mCodecSpecificDataIndex];
1654
1655 size_t size = specific->mSize;
1656
Andreas Hubere6c40962009-09-10 14:13:30 -07001657 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07001658 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001659 static const uint8_t kNALStartCode[4] =
1660 { 0x00, 0x00, 0x00, 0x01 };
1661
1662 CHECK(info->mMem->size() >= specific->mSize + 4);
1663
1664 size += 4;
1665
1666 memcpy(info->mMem->pointer(), kNALStartCode, 4);
1667 memcpy((uint8_t *)info->mMem->pointer() + 4,
1668 specific->mData, specific->mSize);
1669 } else {
1670 CHECK(info->mMem->size() >= specific->mSize);
1671 memcpy(info->mMem->pointer(), specific->mData, specific->mSize);
1672 }
1673
Andreas Huber784202e2009-10-15 13:46:54 -07001674 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07001675 mNode, info->mBuffer, 0, size,
1676 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1677 0);
Andreas Huber3f427072009-10-08 11:02:27 -07001678 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001679
1680 info->mOwnedByComponent = true;
1681
1682 ++mCodecSpecificDataIndex;
1683 return;
1684 }
1685
1686 MediaBuffer *srcBuffer;
1687 status_t err;
1688 if (mSeekTimeUs >= 0) {
1689 MediaSource::ReadOptions options;
1690 options.setSeekTo(mSeekTimeUs);
1691 mSeekTimeUs = -1;
1692
1693 err = mSource->read(&srcBuffer, &options);
1694 } else {
1695 err = mSource->read(&srcBuffer);
1696 }
1697
1698 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberfa8de752009-10-08 10:07:49 -07001699 OMX_TICKS timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07001700 size_t srcLength = 0;
1701
1702 if (err != OK) {
Andreas Huber4c483422009-09-02 16:05:36 -07001703 CODEC_LOGV("signalling end of input stream.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001704 flags |= OMX_BUFFERFLAG_EOS;
1705
1706 mSignalledEOS = true;
1707 } else {
1708 srcLength = srcBuffer->range_length();
1709
1710 if (info->mMem->size() < srcLength) {
1711 LOGE("info->mMem->size() = %d, srcLength = %d",
1712 info->mMem->size(), srcLength);
1713 }
1714 CHECK(info->mMem->size() >= srcLength);
1715 memcpy(info->mMem->pointer(),
1716 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
1717 srcLength);
1718
Andreas Huberfa8de752009-10-08 10:07:49 -07001719 if (srcBuffer->meta_data()->findInt64(kKeyTime, &timestampUs)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001720 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001721 info->mBuffer, srcLength);
Andreas Huber784202e2009-10-15 13:46:54 -07001722 CODEC_LOGV("Calling emptyBuffer with timestamp %lld us (%.2f secs)",
Andreas Huberfa8de752009-10-08 10:07:49 -07001723 timestampUs, timestampUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07001724 }
1725 }
1726
Andreas Huberbe06d262009-08-14 14:37:10 -07001727 if (srcBuffer != NULL) {
1728 srcBuffer->release();
1729 srcBuffer = NULL;
1730 }
Andreas Huber3f427072009-10-08 11:02:27 -07001731
Andreas Huber784202e2009-10-15 13:46:54 -07001732 err = mOMX->emptyBuffer(
Andreas Huber3f427072009-10-08 11:02:27 -07001733 mNode, info->mBuffer, 0, srcLength,
Andreas Huberfa8de752009-10-08 10:07:49 -07001734 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07001735
1736 if (err != OK) {
1737 setState(ERROR);
1738 return;
1739 }
1740
1741 info->mOwnedByComponent = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001742}
1743
1744void OMXCodec::fillOutputBuffer(BufferInfo *info) {
1745 CHECK_EQ(info->mOwnedByComponent, false);
1746
Andreas Huber404cc412009-08-25 14:26:05 -07001747 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07001748 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07001749 "calling fillOutputBuffer");
1750 return;
1751 }
1752
Andreas Huber4c483422009-09-02 16:05:36 -07001753 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07001754 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber3f427072009-10-08 11:02:27 -07001755 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001756
1757 info->mOwnedByComponent = true;
1758}
1759
1760void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
1761 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1762 for (size_t i = 0; i < buffers->size(); ++i) {
1763 if ((*buffers)[i].mBuffer == buffer) {
1764 drainInputBuffer(&buffers->editItemAt(i));
1765 return;
1766 }
1767 }
1768
1769 CHECK(!"should not be here.");
1770}
1771
1772void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
1773 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1774 for (size_t i = 0; i < buffers->size(); ++i) {
1775 if ((*buffers)[i].mBuffer == buffer) {
1776 fillOutputBuffer(&buffers->editItemAt(i));
1777 return;
1778 }
1779 }
1780
1781 CHECK(!"should not be here.");
1782}
1783
1784void OMXCodec::setState(State newState) {
1785 mState = newState;
1786 mAsyncCompletion.signal();
1787
1788 // This may cause some spurious wakeups but is necessary to
1789 // unblock the reader if we enter ERROR state.
1790 mBufferFilled.signal();
1791}
1792
Andreas Huberda050cf22009-09-02 14:01:43 -07001793void OMXCodec::setRawAudioFormat(
1794 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
1795 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07001796 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07001797 pcmParams.nPortIndex = portIndex;
1798
Andreas Huber784202e2009-10-15 13:46:54 -07001799 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001800 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1801
1802 CHECK_EQ(err, OK);
1803
1804 pcmParams.nChannels = numChannels;
1805 pcmParams.eNumData = OMX_NumericalDataSigned;
1806 pcmParams.bInterleaved = OMX_TRUE;
1807 pcmParams.nBitPerSample = 16;
1808 pcmParams.nSamplingRate = sampleRate;
1809 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
1810
1811 if (numChannels == 1) {
1812 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
1813 } else {
1814 CHECK_EQ(numChannels, 2);
1815
1816 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
1817 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
1818 }
1819
Andreas Huber784202e2009-10-15 13:46:54 -07001820 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001821 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1822
1823 CHECK_EQ(err, OK);
1824}
1825
Andreas Huberbe06d262009-08-14 14:37:10 -07001826void OMXCodec::setAMRFormat() {
1827 if (!mIsEncoder) {
1828 OMX_AUDIO_PARAM_AMRTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001829 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001830 def.nPortIndex = kPortIndexInput;
1831
1832 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001833 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07001834
1835 CHECK_EQ(err, OK);
1836
1837 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1838 def.eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
1839
Andreas Huber784202e2009-10-15 13:46:54 -07001840 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07001841 CHECK_EQ(err, OK);
1842 }
1843
1844 ////////////////////////
1845
1846 if (mIsEncoder) {
1847 sp<MetaData> format = mSource->getFormat();
1848 int32_t sampleRate;
1849 int32_t numChannels;
1850 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1851 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1852
Andreas Huberda050cf22009-09-02 14:01:43 -07001853 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
Andreas Huberbe06d262009-08-14 14:37:10 -07001854 }
1855}
1856
Andreas Huberee606e62009-09-08 10:19:21 -07001857void OMXCodec::setAMRWBFormat() {
1858 if (!mIsEncoder) {
1859 OMX_AUDIO_PARAM_AMRTYPE def;
1860 InitOMXParams(&def);
1861 def.nPortIndex = kPortIndexInput;
1862
1863 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001864 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberee606e62009-09-08 10:19:21 -07001865
1866 CHECK_EQ(err, OK);
1867
1868 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1869 def.eAMRBandMode = OMX_AUDIO_AMRBandModeWB0;
1870
Andreas Huber784202e2009-10-15 13:46:54 -07001871 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberee606e62009-09-08 10:19:21 -07001872 CHECK_EQ(err, OK);
1873 }
1874
1875 ////////////////////////
1876
1877 if (mIsEncoder) {
1878 sp<MetaData> format = mSource->getFormat();
1879 int32_t sampleRate;
1880 int32_t numChannels;
1881 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1882 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1883
1884 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1885 }
1886}
1887
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07001888void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) {
Andreas Huberda050cf22009-09-02 14:01:43 -07001889 if (mIsEncoder) {
1890 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1891 } else {
1892 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07001893 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07001894 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07001895
Andreas Huber784202e2009-10-15 13:46:54 -07001896 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001897 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1898 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001899
Andreas Huberda050cf22009-09-02 14:01:43 -07001900 profile.nChannels = numChannels;
1901 profile.nSampleRate = sampleRate;
1902 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07001903
Andreas Huber784202e2009-10-15 13:46:54 -07001904 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001905 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1906 CHECK_EQ(err, OK);
1907 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001908}
1909
1910void OMXCodec::setImageOutputFormat(
1911 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07001912 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001913
1914#if 0
1915 OMX_INDEXTYPE index;
1916 status_t err = mOMX->get_extension_index(
1917 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
1918 CHECK_EQ(err, OK);
1919
1920 err = mOMX->set_config(mNode, index, &format, sizeof(format));
1921 CHECK_EQ(err, OK);
1922#endif
1923
1924 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001925 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001926 def.nPortIndex = kPortIndexOutput;
1927
Andreas Huber784202e2009-10-15 13:46:54 -07001928 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001929 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1930 CHECK_EQ(err, OK);
1931
1932 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1933
1934 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001935
Andreas Huberbe06d262009-08-14 14:37:10 -07001936 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
1937 imageDef->eColorFormat = format;
1938 imageDef->nFrameWidth = width;
1939 imageDef->nFrameHeight = height;
1940
1941 switch (format) {
1942 case OMX_COLOR_FormatYUV420PackedPlanar:
1943 case OMX_COLOR_FormatYUV411Planar:
1944 {
1945 def.nBufferSize = (width * height * 3) / 2;
1946 break;
1947 }
1948
1949 case OMX_COLOR_FormatCbYCrY:
1950 {
1951 def.nBufferSize = width * height * 2;
1952 break;
1953 }
1954
1955 case OMX_COLOR_Format32bitARGB8888:
1956 {
1957 def.nBufferSize = width * height * 4;
1958 break;
1959 }
1960
Andreas Huber201511c2009-09-08 14:01:44 -07001961 case OMX_COLOR_Format16bitARGB4444:
1962 case OMX_COLOR_Format16bitARGB1555:
1963 case OMX_COLOR_Format16bitRGB565:
1964 case OMX_COLOR_Format16bitBGR565:
1965 {
1966 def.nBufferSize = width * height * 2;
1967 break;
1968 }
1969
Andreas Huberbe06d262009-08-14 14:37:10 -07001970 default:
1971 CHECK(!"Should not be here. Unknown color format.");
1972 break;
1973 }
1974
Andreas Huber5c0a9132009-08-20 11:16:40 -07001975 def.nBufferCountActual = def.nBufferCountMin;
1976
Andreas Huber784202e2009-10-15 13:46:54 -07001977 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001978 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1979 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07001980}
Andreas Huberbe06d262009-08-14 14:37:10 -07001981
Andreas Huber5c0a9132009-08-20 11:16:40 -07001982void OMXCodec::setJPEGInputFormat(
1983 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
1984 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001985 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001986 def.nPortIndex = kPortIndexInput;
1987
Andreas Huber784202e2009-10-15 13:46:54 -07001988 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001989 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1990 CHECK_EQ(err, OK);
1991
Andreas Huber5c0a9132009-08-20 11:16:40 -07001992 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1993 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
1994
Andreas Huberbe06d262009-08-14 14:37:10 -07001995 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
1996 imageDef->nFrameWidth = width;
1997 imageDef->nFrameHeight = height;
1998
Andreas Huber5c0a9132009-08-20 11:16:40 -07001999 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002000 def.nBufferCountActual = def.nBufferCountMin;
2001
Andreas Huber784202e2009-10-15 13:46:54 -07002002 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002003 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2004 CHECK_EQ(err, OK);
2005}
2006
2007void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2008 CodecSpecificData *specific =
2009 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2010
2011 specific->mSize = size;
2012 memcpy(specific->mData, data, size);
2013
2014 mCodecSpecificData.push(specific);
2015}
2016
2017void OMXCodec::clearCodecSpecificData() {
2018 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2019 free(mCodecSpecificData.editItemAt(i));
2020 }
2021 mCodecSpecificData.clear();
2022 mCodecSpecificDataIndex = 0;
2023}
2024
2025status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07002026 Mutex::Autolock autoLock(mLock);
2027
Andreas Huberbe06d262009-08-14 14:37:10 -07002028 if (mState != LOADED) {
2029 return UNKNOWN_ERROR;
2030 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002031
Andreas Huberbe06d262009-08-14 14:37:10 -07002032 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002033 if (mQuirks & kWantsNALFragments) {
2034 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002035 }
2036 status_t err = mSource->start(params.get());
2037
2038 if (err != OK) {
2039 return err;
2040 }
2041
2042 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002043 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002044 mSignalledEOS = false;
2045 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002046 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002047 mSeekTimeUs = -1;
2048 mFilledBuffers.clear();
2049
2050 return init();
2051}
2052
2053status_t OMXCodec::stop() {
Andreas Huber4c483422009-09-02 16:05:36 -07002054 CODEC_LOGV("stop");
Andreas Huberbe06d262009-08-14 14:37:10 -07002055
2056 Mutex::Autolock autoLock(mLock);
2057
2058 while (isIntermediateState(mState)) {
2059 mAsyncCompletion.wait(mLock);
2060 }
2061
2062 switch (mState) {
2063 case LOADED:
2064 case ERROR:
2065 break;
2066
2067 case EXECUTING:
2068 {
2069 setState(EXECUTING_TO_IDLE);
2070
Andreas Huber127fcdc2009-08-26 16:27:02 -07002071 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002072 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002073 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002074
Andreas Huber127fcdc2009-08-26 16:27:02 -07002075 bool emulateInputFlushCompletion =
2076 !flushPortAsync(kPortIndexInput);
2077
2078 bool emulateOutputFlushCompletion =
2079 !flushPortAsync(kPortIndexOutput);
2080
2081 if (emulateInputFlushCompletion) {
2082 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2083 }
2084
2085 if (emulateOutputFlushCompletion) {
2086 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2087 }
2088 } else {
2089 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2090 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2091
2092 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002093 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002094 CHECK_EQ(err, OK);
2095 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002096
2097 while (mState != LOADED && mState != ERROR) {
2098 mAsyncCompletion.wait(mLock);
2099 }
2100
2101 break;
2102 }
2103
2104 default:
2105 {
2106 CHECK(!"should not be here.");
2107 break;
2108 }
2109 }
2110
2111 mSource->stop();
2112
2113 return OK;
2114}
2115
2116sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002117 Mutex::Autolock autoLock(mLock);
2118
Andreas Huberbe06d262009-08-14 14:37:10 -07002119 return mOutputFormat;
2120}
2121
2122status_t OMXCodec::read(
2123 MediaBuffer **buffer, const ReadOptions *options) {
2124 *buffer = NULL;
2125
2126 Mutex::Autolock autoLock(mLock);
2127
Andreas Huberd06e5b82009-08-28 13:18:14 -07002128 if (mState != EXECUTING && mState != RECONFIGURING) {
2129 return UNKNOWN_ERROR;
2130 }
2131
Andreas Hubere981c332009-10-22 13:49:30 -07002132 bool seeking = false;
2133 int64_t seekTimeUs;
2134 if (options && options->getSeekTo(&seekTimeUs)) {
2135 seeking = true;
2136 }
2137
Andreas Huber42978e52009-08-27 10:08:39 -07002138 if (mInitialBufferSubmit) {
2139 mInitialBufferSubmit = false;
2140
Andreas Hubere981c332009-10-22 13:49:30 -07002141 if (seeking) {
2142 CHECK(seekTimeUs >= 0);
2143 mSeekTimeUs = seekTimeUs;
2144
2145 // There's no reason to trigger the code below, there's
2146 // nothing to flush yet.
2147 seeking = false;
2148 }
2149
Andreas Huber42978e52009-08-27 10:08:39 -07002150 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002151
Andreas Huberd06e5b82009-08-28 13:18:14 -07002152 if (mState == EXECUTING) {
2153 // Otherwise mState == RECONFIGURING and this code will trigger
2154 // after the output port is reenabled.
2155 fillOutputBuffers();
2156 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002157 }
2158
Andreas Hubere981c332009-10-22 13:49:30 -07002159 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002160 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002161
2162 mSignalledEOS = false;
2163 mNoMoreOutputData = false;
2164
2165 CHECK(seekTimeUs >= 0);
2166 mSeekTimeUs = seekTimeUs;
2167
2168 mFilledBuffers.clear();
2169
2170 CHECK_EQ(mState, EXECUTING);
2171
Andreas Huber404cc412009-08-25 14:26:05 -07002172 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2173 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2174
2175 if (emulateInputFlushCompletion) {
2176 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2177 }
2178
2179 if (emulateOutputFlushCompletion) {
2180 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2181 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002182 }
2183
2184 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2185 mBufferFilled.wait(mLock);
2186 }
2187
2188 if (mState == ERROR) {
2189 return UNKNOWN_ERROR;
2190 }
2191
2192 if (mFilledBuffers.empty()) {
2193 return ERROR_END_OF_STREAM;
2194 }
2195
Andreas Hubercfd55572009-10-09 14:11:28 -07002196 if (mOutputPortSettingsHaveChanged) {
2197 mOutputPortSettingsHaveChanged = false;
2198
2199 return INFO_FORMAT_CHANGED;
2200 }
2201
Andreas Huberbe06d262009-08-14 14:37:10 -07002202 size_t index = *mFilledBuffers.begin();
2203 mFilledBuffers.erase(mFilledBuffers.begin());
2204
2205 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2206 info->mMediaBuffer->add_ref();
2207 *buffer = info->mMediaBuffer;
2208
2209 return OK;
2210}
2211
2212void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2213 Mutex::Autolock autoLock(mLock);
2214
2215 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2216 for (size_t i = 0; i < buffers->size(); ++i) {
2217 BufferInfo *info = &buffers->editItemAt(i);
2218
2219 if (info->mMediaBuffer == buffer) {
2220 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2221 fillOutputBuffer(info);
2222 return;
2223 }
2224 }
2225
2226 CHECK(!"should not be here.");
2227}
2228
2229static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2230 static const char *kNames[] = {
2231 "OMX_IMAGE_CodingUnused",
2232 "OMX_IMAGE_CodingAutoDetect",
2233 "OMX_IMAGE_CodingJPEG",
2234 "OMX_IMAGE_CodingJPEG2K",
2235 "OMX_IMAGE_CodingEXIF",
2236 "OMX_IMAGE_CodingTIFF",
2237 "OMX_IMAGE_CodingGIF",
2238 "OMX_IMAGE_CodingPNG",
2239 "OMX_IMAGE_CodingLZW",
2240 "OMX_IMAGE_CodingBMP",
2241 };
2242
2243 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2244
2245 if (type < 0 || (size_t)type >= numNames) {
2246 return "UNKNOWN";
2247 } else {
2248 return kNames[type];
2249 }
2250}
2251
2252static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2253 static const char *kNames[] = {
2254 "OMX_COLOR_FormatUnused",
2255 "OMX_COLOR_FormatMonochrome",
2256 "OMX_COLOR_Format8bitRGB332",
2257 "OMX_COLOR_Format12bitRGB444",
2258 "OMX_COLOR_Format16bitARGB4444",
2259 "OMX_COLOR_Format16bitARGB1555",
2260 "OMX_COLOR_Format16bitRGB565",
2261 "OMX_COLOR_Format16bitBGR565",
2262 "OMX_COLOR_Format18bitRGB666",
2263 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002264 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002265 "OMX_COLOR_Format24bitRGB888",
2266 "OMX_COLOR_Format24bitBGR888",
2267 "OMX_COLOR_Format24bitARGB1887",
2268 "OMX_COLOR_Format25bitARGB1888",
2269 "OMX_COLOR_Format32bitBGRA8888",
2270 "OMX_COLOR_Format32bitARGB8888",
2271 "OMX_COLOR_FormatYUV411Planar",
2272 "OMX_COLOR_FormatYUV411PackedPlanar",
2273 "OMX_COLOR_FormatYUV420Planar",
2274 "OMX_COLOR_FormatYUV420PackedPlanar",
2275 "OMX_COLOR_FormatYUV420SemiPlanar",
2276 "OMX_COLOR_FormatYUV422Planar",
2277 "OMX_COLOR_FormatYUV422PackedPlanar",
2278 "OMX_COLOR_FormatYUV422SemiPlanar",
2279 "OMX_COLOR_FormatYCbYCr",
2280 "OMX_COLOR_FormatYCrYCb",
2281 "OMX_COLOR_FormatCbYCrY",
2282 "OMX_COLOR_FormatCrYCbY",
2283 "OMX_COLOR_FormatYUV444Interleaved",
2284 "OMX_COLOR_FormatRawBayer8bit",
2285 "OMX_COLOR_FormatRawBayer10bit",
2286 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002287 "OMX_COLOR_FormatL2",
2288 "OMX_COLOR_FormatL4",
2289 "OMX_COLOR_FormatL8",
2290 "OMX_COLOR_FormatL16",
2291 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002292 "OMX_COLOR_FormatL32",
2293 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2294 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2295 "OMX_COLOR_Format18BitBGR666",
2296 "OMX_COLOR_Format24BitARGB6666",
2297 "OMX_COLOR_Format24BitABGR6666",
2298 };
2299
2300 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2301
Andreas Huberbe06d262009-08-14 14:37:10 -07002302 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2303 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2304 } else if (type < 0 || (size_t)type >= numNames) {
2305 return "UNKNOWN";
2306 } else {
2307 return kNames[type];
2308 }
2309}
2310
2311static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2312 static const char *kNames[] = {
2313 "OMX_VIDEO_CodingUnused",
2314 "OMX_VIDEO_CodingAutoDetect",
2315 "OMX_VIDEO_CodingMPEG2",
2316 "OMX_VIDEO_CodingH263",
2317 "OMX_VIDEO_CodingMPEG4",
2318 "OMX_VIDEO_CodingWMV",
2319 "OMX_VIDEO_CodingRV",
2320 "OMX_VIDEO_CodingAVC",
2321 "OMX_VIDEO_CodingMJPEG",
2322 };
2323
2324 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2325
2326 if (type < 0 || (size_t)type >= numNames) {
2327 return "UNKNOWN";
2328 } else {
2329 return kNames[type];
2330 }
2331}
2332
2333static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2334 static const char *kNames[] = {
2335 "OMX_AUDIO_CodingUnused",
2336 "OMX_AUDIO_CodingAutoDetect",
2337 "OMX_AUDIO_CodingPCM",
2338 "OMX_AUDIO_CodingADPCM",
2339 "OMX_AUDIO_CodingAMR",
2340 "OMX_AUDIO_CodingGSMFR",
2341 "OMX_AUDIO_CodingGSMEFR",
2342 "OMX_AUDIO_CodingGSMHR",
2343 "OMX_AUDIO_CodingPDCFR",
2344 "OMX_AUDIO_CodingPDCEFR",
2345 "OMX_AUDIO_CodingPDCHR",
2346 "OMX_AUDIO_CodingTDMAFR",
2347 "OMX_AUDIO_CodingTDMAEFR",
2348 "OMX_AUDIO_CodingQCELP8",
2349 "OMX_AUDIO_CodingQCELP13",
2350 "OMX_AUDIO_CodingEVRC",
2351 "OMX_AUDIO_CodingSMV",
2352 "OMX_AUDIO_CodingG711",
2353 "OMX_AUDIO_CodingG723",
2354 "OMX_AUDIO_CodingG726",
2355 "OMX_AUDIO_CodingG729",
2356 "OMX_AUDIO_CodingAAC",
2357 "OMX_AUDIO_CodingMP3",
2358 "OMX_AUDIO_CodingSBC",
2359 "OMX_AUDIO_CodingVORBIS",
2360 "OMX_AUDIO_CodingWMA",
2361 "OMX_AUDIO_CodingRA",
2362 "OMX_AUDIO_CodingMIDI",
2363 };
2364
2365 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2366
2367 if (type < 0 || (size_t)type >= numNames) {
2368 return "UNKNOWN";
2369 } else {
2370 return kNames[type];
2371 }
2372}
2373
2374static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2375 static const char *kNames[] = {
2376 "OMX_AUDIO_PCMModeLinear",
2377 "OMX_AUDIO_PCMModeALaw",
2378 "OMX_AUDIO_PCMModeMULaw",
2379 };
2380
2381 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2382
2383 if (type < 0 || (size_t)type >= numNames) {
2384 return "UNKNOWN";
2385 } else {
2386 return kNames[type];
2387 }
2388}
2389
Andreas Huber7ae02c82009-09-09 16:29:47 -07002390static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2391 static const char *kNames[] = {
2392 "OMX_AUDIO_AMRBandModeUnused",
2393 "OMX_AUDIO_AMRBandModeNB0",
2394 "OMX_AUDIO_AMRBandModeNB1",
2395 "OMX_AUDIO_AMRBandModeNB2",
2396 "OMX_AUDIO_AMRBandModeNB3",
2397 "OMX_AUDIO_AMRBandModeNB4",
2398 "OMX_AUDIO_AMRBandModeNB5",
2399 "OMX_AUDIO_AMRBandModeNB6",
2400 "OMX_AUDIO_AMRBandModeNB7",
2401 "OMX_AUDIO_AMRBandModeWB0",
2402 "OMX_AUDIO_AMRBandModeWB1",
2403 "OMX_AUDIO_AMRBandModeWB2",
2404 "OMX_AUDIO_AMRBandModeWB3",
2405 "OMX_AUDIO_AMRBandModeWB4",
2406 "OMX_AUDIO_AMRBandModeWB5",
2407 "OMX_AUDIO_AMRBandModeWB6",
2408 "OMX_AUDIO_AMRBandModeWB7",
2409 "OMX_AUDIO_AMRBandModeWB8",
2410 };
2411
2412 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2413
2414 if (type < 0 || (size_t)type >= numNames) {
2415 return "UNKNOWN";
2416 } else {
2417 return kNames[type];
2418 }
2419}
2420
2421static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2422 static const char *kNames[] = {
2423 "OMX_AUDIO_AMRFrameFormatConformance",
2424 "OMX_AUDIO_AMRFrameFormatIF1",
2425 "OMX_AUDIO_AMRFrameFormatIF2",
2426 "OMX_AUDIO_AMRFrameFormatFSF",
2427 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2428 "OMX_AUDIO_AMRFrameFormatITU",
2429 };
2430
2431 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2432
2433 if (type < 0 || (size_t)type >= numNames) {
2434 return "UNKNOWN";
2435 } else {
2436 return kNames[type];
2437 }
2438}
Andreas Huberbe06d262009-08-14 14:37:10 -07002439
2440void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2441 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002442 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002443 def.nPortIndex = portIndex;
2444
Andreas Huber784202e2009-10-15 13:46:54 -07002445 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002446 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2447 CHECK_EQ(err, OK);
2448
2449 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2450
2451 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2452 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2453
2454 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2455 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2456 printf(" nBufferSize = %ld\n", def.nBufferSize);
2457
2458 switch (def.eDomain) {
2459 case OMX_PortDomainImage:
2460 {
2461 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2462
2463 printf("\n");
2464 printf(" // Image\n");
2465 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2466 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2467 printf(" nStride = %ld\n", imageDef->nStride);
2468
2469 printf(" eCompressionFormat = %s\n",
2470 imageCompressionFormatString(imageDef->eCompressionFormat));
2471
2472 printf(" eColorFormat = %s\n",
2473 colorFormatString(imageDef->eColorFormat));
2474
2475 break;
2476 }
2477
2478 case OMX_PortDomainVideo:
2479 {
2480 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2481
2482 printf("\n");
2483 printf(" // Video\n");
2484 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2485 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2486 printf(" nStride = %ld\n", videoDef->nStride);
2487
2488 printf(" eCompressionFormat = %s\n",
2489 videoCompressionFormatString(videoDef->eCompressionFormat));
2490
2491 printf(" eColorFormat = %s\n",
2492 colorFormatString(videoDef->eColorFormat));
2493
2494 break;
2495 }
2496
2497 case OMX_PortDomainAudio:
2498 {
2499 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2500
2501 printf("\n");
2502 printf(" // Audio\n");
2503 printf(" eEncoding = %s\n",
2504 audioCodingTypeString(audioDef->eEncoding));
2505
2506 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2507 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002508 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002509 params.nPortIndex = portIndex;
2510
Andreas Huber784202e2009-10-15 13:46:54 -07002511 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002512 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2513 CHECK_EQ(err, OK);
2514
2515 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
2516 printf(" nChannels = %ld\n", params.nChannels);
2517 printf(" bInterleaved = %d\n", params.bInterleaved);
2518 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
2519
2520 printf(" eNumData = %s\n",
2521 params.eNumData == OMX_NumericalDataSigned
2522 ? "signed" : "unsigned");
2523
2524 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07002525 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
2526 OMX_AUDIO_PARAM_AMRTYPE amr;
2527 InitOMXParams(&amr);
2528 amr.nPortIndex = portIndex;
2529
Andreas Huber784202e2009-10-15 13:46:54 -07002530 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07002531 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2532 CHECK_EQ(err, OK);
2533
2534 printf(" nChannels = %ld\n", amr.nChannels);
2535 printf(" eAMRBandMode = %s\n",
2536 amrBandModeString(amr.eAMRBandMode));
2537 printf(" eAMRFrameFormat = %s\n",
2538 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07002539 }
2540
2541 break;
2542 }
2543
2544 default:
2545 {
2546 printf(" // Unknown\n");
2547 break;
2548 }
2549 }
2550
2551 printf("}\n");
2552}
2553
2554void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
2555 mOutputFormat = new MetaData;
2556 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
2557
2558 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002559 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002560 def.nPortIndex = kPortIndexOutput;
2561
Andreas Huber784202e2009-10-15 13:46:54 -07002562 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002563 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2564 CHECK_EQ(err, OK);
2565
2566 switch (def.eDomain) {
2567 case OMX_PortDomainImage:
2568 {
2569 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2570 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2571
Andreas Hubere6c40962009-09-10 14:13:30 -07002572 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002573 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
2574 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
2575 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
2576 break;
2577 }
2578
2579 case OMX_PortDomainAudio:
2580 {
2581 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
2582
Andreas Huberda050cf22009-09-02 14:01:43 -07002583 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
2584 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002585 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07002586 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002587
Andreas Huber784202e2009-10-15 13:46:54 -07002588 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002589 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2590 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002591
Andreas Huberda050cf22009-09-02 14:01:43 -07002592 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
2593 CHECK_EQ(params.nBitPerSample, 16);
2594 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07002595
Andreas Huberda050cf22009-09-02 14:01:43 -07002596 int32_t numChannels, sampleRate;
2597 inputFormat->findInt32(kKeyChannelCount, &numChannels);
2598 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07002599
Andreas Huberda050cf22009-09-02 14:01:43 -07002600 if ((OMX_U32)numChannels != params.nChannels) {
2601 LOGW("Codec outputs a different number of channels than "
2602 "the input stream contains.");
2603 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002604
Andreas Hubere6c40962009-09-10 14:13:30 -07002605 mOutputFormat->setCString(
2606 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07002607
2608 // Use the codec-advertised number of channels, as some
2609 // codecs appear to output stereo even if the input data is
2610 // mono.
2611 mOutputFormat->setInt32(kKeyChannelCount, params.nChannels);
2612
2613 // The codec-reported sampleRate is not reliable...
2614 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
2615 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07002616 OMX_AUDIO_PARAM_AMRTYPE amr;
2617 InitOMXParams(&amr);
2618 amr.nPortIndex = kPortIndexOutput;
2619
Andreas Huber784202e2009-10-15 13:46:54 -07002620 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07002621 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2622 CHECK_EQ(err, OK);
2623
2624 CHECK_EQ(amr.nChannels, 1);
2625 mOutputFormat->setInt32(kKeyChannelCount, 1);
2626
2627 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
2628 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002629 mOutputFormat->setCString(
2630 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002631 mOutputFormat->setInt32(kKeySampleRate, 8000);
2632 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
2633 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002634 mOutputFormat->setCString(
2635 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002636 mOutputFormat->setInt32(kKeySampleRate, 16000);
2637 } else {
2638 CHECK(!"Unknown AMR band mode.");
2639 }
Andreas Huberda050cf22009-09-02 14:01:43 -07002640 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002641 mOutputFormat->setCString(
2642 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
Andreas Huberda050cf22009-09-02 14:01:43 -07002643 } else {
2644 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07002645 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002646 break;
2647 }
2648
2649 case OMX_PortDomainVideo:
2650 {
2651 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
2652
2653 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002654 mOutputFormat->setCString(
2655 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002656 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002657 mOutputFormat->setCString(
2658 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002659 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002660 mOutputFormat->setCString(
2661 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07002662 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002663 mOutputFormat->setCString(
2664 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07002665 } else {
2666 CHECK(!"Unknown compression format.");
2667 }
2668
2669 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
2670 // This component appears to be lying to me.
2671 mOutputFormat->setInt32(
2672 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
2673 mOutputFormat->setInt32(
2674 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
2675 } else {
2676 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
2677 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
2678 }
2679
2680 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
2681 break;
2682 }
2683
2684 default:
2685 {
2686 CHECK(!"should not be here, neither audio nor video.");
2687 break;
2688 }
2689 }
2690}
2691
Andreas Hubere6c40962009-09-10 14:13:30 -07002692////////////////////////////////////////////////////////////////////////////////
2693
2694status_t QueryCodecs(
2695 const sp<IOMX> &omx,
2696 const char *mime, bool queryDecoders,
2697 Vector<CodecCapabilities> *results) {
2698 results->clear();
2699
2700 for (int index = 0;; ++index) {
2701 const char *componentName;
2702
2703 if (!queryDecoders) {
2704 componentName = GetCodec(
2705 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
2706 mime, index);
2707 } else {
2708 componentName = GetCodec(
2709 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
2710 mime, index);
2711 }
2712
2713 if (!componentName) {
2714 return OK;
2715 }
2716
Andreas Huber784202e2009-10-15 13:46:54 -07002717 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07002718 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07002719 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07002720
2721 if (err != OK) {
2722 continue;
2723 }
2724
2725 OMXCodec::setComponentRole(omx, node, queryDecoders, mime);
2726
2727 results->push();
2728 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
2729 caps->mComponentName = componentName;
2730
2731 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
2732 InitOMXParams(&param);
2733
2734 param.nPortIndex = queryDecoders ? 0 : 1;
2735
2736 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07002737 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07002738 node, OMX_IndexParamVideoProfileLevelQuerySupported,
2739 &param, sizeof(param));
2740
2741 if (err != OK) {
2742 break;
2743 }
2744
2745 CodecProfileLevel profileLevel;
2746 profileLevel.mProfile = param.eProfile;
2747 profileLevel.mLevel = param.eLevel;
2748
2749 caps->mProfileLevels.push(profileLevel);
2750 }
2751
Andreas Huber784202e2009-10-15 13:46:54 -07002752 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07002753 }
2754}
2755
Andreas Huberbe06d262009-08-14 14:37:10 -07002756} // namespace android