blob: d36653ed514f1604ee235158cfc91f44b9f7a7b9 [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.
221 quirks |= kRequiresAllocateBufferOnOutputPorts;
222 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700223
Andreas Huber2dc64d82009-09-11 12:58:53 -0700224 if (!strncmp(componentName, "OMX.TI.", 7)) {
225 // Apparently I must not use OMX_UseBuffer on either input or
226 // output ports on any of the TI components or quote:
227 // "(I) may have unexpected problem (sic) which can be timing related
228 // and hard to reproduce."
229
230 quirks |= kRequiresAllocateBufferOnInputPorts;
231 quirks |= kRequiresAllocateBufferOnOutputPorts;
232 }
233
Andreas Hubere13526a2009-10-22 10:43:34 -0700234 return quirks;
235}
236
237// static
238void OMXCodec::findMatchingCodecs(
239 const char *mime,
240 bool createEncoder, const char *matchComponentName,
241 uint32_t flags,
242 Vector<String8> *matchingCodecs) {
243 matchingCodecs->clear();
244
245 for (int index = 0;; ++index) {
246 const char *componentName;
247
248 if (createEncoder) {
249 componentName = GetCodec(
250 kEncoderInfo,
251 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
252 mime, index);
253 } else {
254 componentName = GetCodec(
255 kDecoderInfo,
256 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
257 mime, index);
258 }
259
260 if (!componentName) {
261 break;
262 }
263
264 // If a specific codec is requested, skip the non-matching ones.
265 if (matchComponentName && strcmp(componentName, matchComponentName)) {
266 continue;
267 }
268
269 matchingCodecs->push(String8(componentName));
270 }
271
272 if (flags & kPreferSoftwareCodecs) {
273 matchingCodecs->sort(CompareSoftwareCodecsFirst);
274 }
275}
276
277// static
278sp<OMXCodec> OMXCodec::Create(
279 const sp<IOMX> &omx,
280 const sp<MetaData> &meta, bool createEncoder,
281 const sp<MediaSource> &source,
282 const char *matchComponentName,
283 uint32_t flags) {
284 const char *mime;
285 bool success = meta->findCString(kKeyMIMEType, &mime);
286 CHECK(success);
287
288 Vector<String8> matchingCodecs;
289 findMatchingCodecs(
290 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
291
292 if (matchingCodecs.isEmpty()) {
293 return NULL;
294 }
295
296 sp<OMXCodecObserver> observer = new OMXCodecObserver;
297 IOMX::node_id node = 0;
298 success = false;
299
300 const char *componentName;
301 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
302 componentName = matchingCodecs[i].string();
303
304 LOGV("Attempting to allocate OMX node '%s'", componentName);
305
306 status_t err = omx->allocateNode(componentName, observer, &node);
307 if (err == OK) {
308 LOGV("Successfully allocated OMX node '%s'", componentName);
309
310 success = true;
311 break;
312 }
313 }
314
315 if (!success) {
316 return NULL;
317 }
318
Andreas Huberbe06d262009-08-14 14:37:10 -0700319 sp<OMXCodec> codec = new OMXCodec(
Andreas Hubere13526a2009-10-22 10:43:34 -0700320 omx, node, getComponentQuirks(componentName),
321 createEncoder, mime, componentName,
Andreas Huberbe06d262009-08-14 14:37:10 -0700322 source);
323
Andreas Huber784202e2009-10-15 13:46:54 -0700324 observer->setCodec(codec);
325
Andreas Huberbe06d262009-08-14 14:37:10 -0700326 uint32_t type;
327 const void *data;
328 size_t size;
329 if (meta->findData(kKeyESDS, &type, &data, &size)) {
330 ESDS esds((const char *)data, size);
331 CHECK_EQ(esds.InitCheck(), OK);
332
333 const void *codec_specific_data;
334 size_t codec_specific_data_size;
335 esds.getCodecSpecificInfo(
336 &codec_specific_data, &codec_specific_data_size);
337
338 printf("found codec-specific data of size %d\n",
339 codec_specific_data_size);
340
341 codec->addCodecSpecificData(
342 codec_specific_data, codec_specific_data_size);
343 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
344 printf("found avcc of size %d\n", size);
345
Andreas Huberebf66ea2009-08-19 13:32:58 -0700346 // Parse the AVCDecoderConfigurationRecord
347
348 const uint8_t *ptr = (const uint8_t *)data;
349
350 CHECK(size >= 7);
351 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
352 uint8_t profile = ptr[1];
353 uint8_t level = ptr[3];
354
355 CHECK((ptr[4] >> 2) == 0x3f); // reserved
356
357 size_t lengthSize = 1 + (ptr[4] & 3);
358
359 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
360 // violates it...
361 // CHECK((ptr[5] >> 5) == 7); // reserved
362
363 size_t numSeqParameterSets = ptr[5] & 31;
364
365 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700366 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700367
368 for (size_t i = 0; i < numSeqParameterSets; ++i) {
369 CHECK(size >= 2);
370 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700371
372 ptr += 2;
373 size -= 2;
374
Andreas Huberbe06d262009-08-14 14:37:10 -0700375 CHECK(size >= length);
376
377 codec->addCodecSpecificData(ptr, length);
378
379 ptr += length;
380 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700381 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700382
Andreas Huberebf66ea2009-08-19 13:32:58 -0700383 CHECK(size >= 1);
384 size_t numPictureParameterSets = *ptr;
385 ++ptr;
386 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700387
Andreas Huberebf66ea2009-08-19 13:32:58 -0700388 for (size_t i = 0; i < numPictureParameterSets; ++i) {
389 CHECK(size >= 2);
390 size_t length = U16_AT(ptr);
391
392 ptr += 2;
393 size -= 2;
394
395 CHECK(size >= length);
396
397 codec->addCodecSpecificData(ptr, length);
398
399 ptr += length;
400 size -= length;
401 }
402
Andreas Huber53a76bd2009-10-06 16:20:44 -0700403 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700404 (int)profile, AVCProfileToString(profile), (int)level / 10);
405
406 if (!strcmp(componentName, "OMX.TI.Video.Decoder")
407 && (profile != kAVCProfileBaseline || level > 39)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700408 // This stream exceeds the decoder's capabilities. The decoder
409 // does not handle this gracefully and would clobber the heap
410 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700411
412 LOGE("Profile and/or level exceed the decoder's capabilities.");
413 return NULL;
Andreas Huberbe06d262009-08-14 14:37:10 -0700414 }
415 }
416
Andreas Hubere6c40962009-09-10 14:13:30 -0700417 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700418 codec->setAMRFormat();
419 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700420 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
Andreas Huberee606e62009-09-08 10:19:21 -0700421 codec->setAMRWBFormat();
422 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700423 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700424 int32_t numChannels, sampleRate;
425 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
426 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
427
428 codec->setAACFormat(numChannels, sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700429 }
430 if (!strncasecmp(mime, "video/", 6)) {
431 int32_t width, height;
432 bool success = meta->findInt32(kKeyWidth, &width);
433 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700434 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700435
436 if (createEncoder) {
437 codec->setVideoInputFormat(mime, width, height);
438 } else {
439 codec->setVideoOutputFormat(mime, width, height);
440 }
441 }
Andreas Hubere6c40962009-09-10 14:13:30 -0700442 if (!strcasecmp(mime, MEDIA_MIMETYPE_IMAGE_JPEG)
Andreas Huberbe06d262009-08-14 14:37:10 -0700443 && !strcmp(componentName, "OMX.TI.JPEG.decode")) {
444 OMX_COLOR_FORMATTYPE format =
445 OMX_COLOR_Format32bitARGB8888;
446 // OMX_COLOR_FormatYUV420PackedPlanar;
447 // OMX_COLOR_FormatCbYCrY;
448 // OMX_COLOR_FormatYUV411Planar;
449
450 int32_t width, height;
451 bool success = meta->findInt32(kKeyWidth, &width);
452 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700453
454 int32_t compressedSize;
455 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700456 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700457
458 CHECK(success);
459 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700460
461 codec->setImageOutputFormat(format, width, height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700462 codec->setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700463 }
464
Andreas Huberda050cf22009-09-02 14:01:43 -0700465 int32_t maxInputSize;
466 if (createEncoder && meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
467 codec->setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
468 }
469
470 if (!strcmp(componentName, "OMX.TI.AMR.encode")
471 || !strcmp(componentName, "OMX.TI.WBAMR.encode")) {
472 codec->setMinBufferSize(kPortIndexOutput, 8192); // XXX
473 }
474
Andreas Huberbe06d262009-08-14 14:37:10 -0700475 codec->initOutputFormat(meta);
476
477 return codec;
478}
479
Andreas Huberda050cf22009-09-02 14:01:43 -0700480void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
481 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700482 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700483 def.nPortIndex = portIndex;
484
Andreas Huber784202e2009-10-15 13:46:54 -0700485 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700486 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
487 CHECK_EQ(err, OK);
488
489 if (def.nBufferSize < size) {
490 def.nBufferSize = size;
491
492 }
493
Andreas Huber784202e2009-10-15 13:46:54 -0700494 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700495 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
496 CHECK_EQ(err, OK);
497}
498
Andreas Huberbe06d262009-08-14 14:37:10 -0700499status_t OMXCodec::setVideoPortFormatType(
500 OMX_U32 portIndex,
501 OMX_VIDEO_CODINGTYPE compressionFormat,
502 OMX_COLOR_FORMATTYPE colorFormat) {
503 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700504 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700505 format.nPortIndex = portIndex;
506 format.nIndex = 0;
507 bool found = false;
508
509 OMX_U32 index = 0;
510 for (;;) {
511 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700512 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700513 mNode, OMX_IndexParamVideoPortFormat,
514 &format, sizeof(format));
515
516 if (err != OK) {
517 return err;
518 }
519
520 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700521 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700522
523#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700524 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700525 portIndex,
526 index, format.eCompressionFormat, format.eColorFormat);
527#endif
528
529 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
530 if (portIndex == kPortIndexInput
531 && colorFormat == format.eColorFormat) {
532 // eCompressionFormat does not seem right.
533 found = true;
534 break;
535 }
536 if (portIndex == kPortIndexOutput
537 && compressionFormat == format.eCompressionFormat) {
538 // eColorFormat does not seem right.
539 found = true;
540 break;
541 }
542 }
543
544 if (format.eCompressionFormat == compressionFormat
545 && format.eColorFormat == colorFormat) {
546 found = true;
547 break;
548 }
549
550 ++index;
551 }
552
553 if (!found) {
554 return UNKNOWN_ERROR;
555 }
556
Andreas Huber53a76bd2009-10-06 16:20:44 -0700557 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700558 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700559 mNode, OMX_IndexParamVideoPortFormat,
560 &format, sizeof(format));
561
562 return err;
563}
564
Andreas Huberb482ce82009-10-29 12:02:48 -0700565static size_t getFrameSize(
566 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
567 switch (colorFormat) {
568 case OMX_COLOR_FormatYCbYCr:
569 case OMX_COLOR_FormatCbYCrY:
570 return width * height * 2;
571
572 case OMX_COLOR_FormatYUV420SemiPlanar:
573 return (width * height * 3) / 2;
574
575 default:
576 CHECK(!"Should not be here. Unsupported color format.");
577 break;
578 }
579}
580
Andreas Huberbe06d262009-08-14 14:37:10 -0700581void OMXCodec::setVideoInputFormat(
582 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700583 CODEC_LOGV("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700584
585 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700586 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700587 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700588 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700589 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700590 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700591 compressionFormat = OMX_VIDEO_CodingH263;
592 } else {
593 LOGE("Not a supported video mime type: %s", mime);
594 CHECK(!"Should not be here. Not a supported video mime type.");
595 }
596
Andreas Huberea6a38c2009-11-16 15:43:38 -0800597 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
598 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
599 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700600 }
601
Andreas Huberb482ce82009-10-29 12:02:48 -0700602 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700603 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700604 colorFormat), OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700605
Andreas Huberb482ce82009-10-29 12:02:48 -0700606 CHECK_EQ(setVideoPortFormatType(
607 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
608 OK);
Andreas Huberbe06d262009-08-14 14:37:10 -0700609
610 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700611 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700612 def.nPortIndex = kPortIndexOutput;
613
Andreas Huber4c483422009-09-02 16:05:36 -0700614 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
615
Andreas Huber784202e2009-10-15 13:46:54 -0700616 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700617 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
618
619 CHECK_EQ(err, OK);
620 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
621
622 video_def->nFrameWidth = width;
623 video_def->nFrameHeight = height;
624
625 video_def->eCompressionFormat = compressionFormat;
626 video_def->eColorFormat = OMX_COLOR_FormatUnused;
627
Andreas Huber784202e2009-10-15 13:46:54 -0700628 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700629 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
630 CHECK_EQ(err, OK);
631
632 ////////////////////////////////////////////////////////////////////////////
633
Andreas Huber4c483422009-09-02 16:05:36 -0700634 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700635 def.nPortIndex = kPortIndexInput;
636
Andreas Huber784202e2009-10-15 13:46:54 -0700637 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700638 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
639 CHECK_EQ(err, OK);
640
Andreas Huberb482ce82009-10-29 12:02:48 -0700641 def.nBufferSize = getFrameSize(colorFormat, width, height);
Andreas Huber53a76bd2009-10-06 16:20:44 -0700642 CODEC_LOGV("Setting nBufferSize = %ld", def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700643
644 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
645
646 video_def->nFrameWidth = width;
647 video_def->nFrameHeight = height;
648 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
649 video_def->eColorFormat = colorFormat;
650
Andreas Huberb482ce82009-10-29 12:02:48 -0700651 video_def->xFramerate = 24 << 16; // XXX crucial!
652
Andreas Huber784202e2009-10-15 13:46:54 -0700653 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700654 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
655 CHECK_EQ(err, OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700656
657 switch (compressionFormat) {
658 case OMX_VIDEO_CodingMPEG4:
659 {
660 CHECK_EQ(setupMPEG4EncoderParameters(), OK);
661 break;
662 }
663
664 case OMX_VIDEO_CodingH263:
665 break;
666
Andreas Huberea6a38c2009-11-16 15:43:38 -0800667 case OMX_VIDEO_CodingAVC:
668 {
669 CHECK_EQ(setupAVCEncoderParameters(), OK);
670 break;
671 }
672
Andreas Huberb482ce82009-10-29 12:02:48 -0700673 default:
674 CHECK(!"Support for this compressionFormat to be implemented.");
675 break;
676 }
677}
678
679status_t OMXCodec::setupMPEG4EncoderParameters() {
680 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
681 InitOMXParams(&mpeg4type);
682 mpeg4type.nPortIndex = kPortIndexOutput;
683
684 status_t err = mOMX->getParameter(
685 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
686 CHECK_EQ(err, OK);
687
688 mpeg4type.nSliceHeaderSpacing = 0;
689 mpeg4type.bSVH = OMX_FALSE;
690 mpeg4type.bGov = OMX_FALSE;
691
692 mpeg4type.nAllowedPictureTypes =
693 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
694
695 mpeg4type.nPFrames = 23;
696 mpeg4type.nBFrames = 0;
697
698 mpeg4type.nIDCVLCThreshold = 0;
699 mpeg4type.bACPred = OMX_TRUE;
700 mpeg4type.nMaxPacketSize = 256;
701 mpeg4type.nTimeIncRes = 1000;
702 mpeg4type.nHeaderExtension = 0;
703 mpeg4type.bReversibleVLC = OMX_FALSE;
704
705 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileCore;
706 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2;
707
708 err = mOMX->setParameter(
709 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
710 CHECK_EQ(err, OK);
711
712 // ----------------
713
714 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
715 InitOMXParams(&bitrateType);
716 bitrateType.nPortIndex = kPortIndexOutput;
717
718 err = mOMX->getParameter(
719 mNode, OMX_IndexParamVideoBitrate,
720 &bitrateType, sizeof(bitrateType));
721 CHECK_EQ(err, OK);
722
723 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
724 bitrateType.nTargetBitrate = 1000000;
725
726 err = mOMX->setParameter(
727 mNode, OMX_IndexParamVideoBitrate,
728 &bitrateType, sizeof(bitrateType));
729 CHECK_EQ(err, OK);
730
731 // ----------------
732
733 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
734 InitOMXParams(&errorCorrectionType);
735 errorCorrectionType.nPortIndex = kPortIndexOutput;
736
737 err = mOMX->getParameter(
738 mNode, OMX_IndexParamVideoErrorCorrection,
739 &errorCorrectionType, sizeof(errorCorrectionType));
740 CHECK_EQ(err, OK);
741
742 errorCorrectionType.bEnableHEC = OMX_FALSE;
743 errorCorrectionType.bEnableResync = OMX_TRUE;
744 errorCorrectionType.nResynchMarkerSpacing = 256;
745 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
746 errorCorrectionType.bEnableRVLC = OMX_FALSE;
747
748 err = mOMX->setParameter(
749 mNode, OMX_IndexParamVideoErrorCorrection,
750 &errorCorrectionType, sizeof(errorCorrectionType));
751 CHECK_EQ(err, OK);
752
753 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700754}
755
Andreas Huberea6a38c2009-11-16 15:43:38 -0800756status_t OMXCodec::setupAVCEncoderParameters() {
757 OMX_VIDEO_PARAM_AVCTYPE h264type;
758 InitOMXParams(&h264type);
759 h264type.nPortIndex = kPortIndexOutput;
760
761 status_t err = mOMX->getParameter(
762 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
763 CHECK_EQ(err, OK);
764
765 h264type.nAllowedPictureTypes =
766 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
767
768 h264type.nSliceHeaderSpacing = 0;
769 h264type.nBFrames = 0;
770 h264type.bUseHadamard = OMX_TRUE;
771 h264type.nRefFrames = 1;
772 h264type.nRefIdx10ActiveMinus1 = 0;
773 h264type.nRefIdx11ActiveMinus1 = 0;
774 h264type.bEnableUEP = OMX_FALSE;
775 h264type.bEnableFMO = OMX_FALSE;
776 h264type.bEnableASO = OMX_FALSE;
777 h264type.bEnableRS = OMX_FALSE;
778 h264type.eProfile = OMX_VIDEO_AVCProfileBaseline;
779 h264type.eLevel = OMX_VIDEO_AVCLevel1b;
780 h264type.bFrameMBsOnly = OMX_TRUE;
781 h264type.bMBAFF = OMX_FALSE;
782 h264type.bEntropyCodingCABAC = OMX_FALSE;
783 h264type.bWeightedPPrediction = OMX_FALSE;
784 h264type.bconstIpred = OMX_FALSE;
785 h264type.bDirect8x8Inference = OMX_FALSE;
786 h264type.bDirectSpatialTemporal = OMX_FALSE;
787 h264type.nCabacInitIdc = 0;
788 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
789
790 err = mOMX->setParameter(
791 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
792 CHECK_EQ(err, OK);
793
794 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
795 InitOMXParams(&bitrateType);
796 bitrateType.nPortIndex = kPortIndexOutput;
797
798 err = mOMX->getParameter(
799 mNode, OMX_IndexParamVideoBitrate,
800 &bitrateType, sizeof(bitrateType));
801 CHECK_EQ(err, OK);
802
803 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
804 bitrateType.nTargetBitrate = 1000000;
805
806 err = mOMX->setParameter(
807 mNode, OMX_IndexParamVideoBitrate,
808 &bitrateType, sizeof(bitrateType));
809 CHECK_EQ(err, OK);
810
811 return OK;
812}
813
Andreas Huberbe06d262009-08-14 14:37:10 -0700814void OMXCodec::setVideoOutputFormat(
815 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700816 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700817
Andreas Huberbe06d262009-08-14 14:37:10 -0700818 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700819 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700820 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700821 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700822 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700823 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700824 compressionFormat = OMX_VIDEO_CodingH263;
825 } else {
826 LOGE("Not a supported video mime type: %s", mime);
827 CHECK(!"Should not be here. Not a supported video mime type.");
828 }
829
830 setVideoPortFormatType(
831 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
832
833#if 1
834 {
835 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700836 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700837 format.nPortIndex = kPortIndexOutput;
838 format.nIndex = 0;
839
Andreas Huber784202e2009-10-15 13:46:54 -0700840 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700841 mNode, OMX_IndexParamVideoPortFormat,
842 &format, sizeof(format));
843 CHECK_EQ(err, OK);
844 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
845
846 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
847
848 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
849 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
850 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
851 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
852
Andreas Huber784202e2009-10-15 13:46:54 -0700853 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700854 mNode, OMX_IndexParamVideoPortFormat,
855 &format, sizeof(format));
856 CHECK_EQ(err, OK);
857 }
858#endif
859
860 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700861 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700862 def.nPortIndex = kPortIndexInput;
863
Andreas Huber4c483422009-09-02 16:05:36 -0700864 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
865
Andreas Huber784202e2009-10-15 13:46:54 -0700866 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700867 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
868
869 CHECK_EQ(err, OK);
870
871#if 1
872 // XXX Need a (much) better heuristic to compute input buffer sizes.
873 const size_t X = 64 * 1024;
874 if (def.nBufferSize < X) {
875 def.nBufferSize = X;
876 }
877#endif
878
879 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
880
881 video_def->nFrameWidth = width;
882 video_def->nFrameHeight = height;
883
Andreas Huberb482ce82009-10-29 12:02:48 -0700884 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -0700885 video_def->eColorFormat = OMX_COLOR_FormatUnused;
886
Andreas Huber784202e2009-10-15 13:46:54 -0700887 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700888 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
889 CHECK_EQ(err, OK);
890
891 ////////////////////////////////////////////////////////////////////////////
892
Andreas Huber4c483422009-09-02 16:05:36 -0700893 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700894 def.nPortIndex = kPortIndexOutput;
895
Andreas Huber784202e2009-10-15 13:46:54 -0700896 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700897 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
898 CHECK_EQ(err, OK);
899 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
900
901#if 0
902 def.nBufferSize =
903 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
904#endif
905
906 video_def->nFrameWidth = width;
907 video_def->nFrameHeight = height;
908
Andreas Huber784202e2009-10-15 13:46:54 -0700909 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700910 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
911 CHECK_EQ(err, OK);
912}
913
Andreas Huberbe06d262009-08-14 14:37:10 -0700914OMXCodec::OMXCodec(
915 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -0700916 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -0700917 const char *mime,
918 const char *componentName,
919 const sp<MediaSource> &source)
920 : mOMX(omx),
921 mNode(node),
922 mQuirks(quirks),
923 mIsEncoder(isEncoder),
924 mMIME(strdup(mime)),
925 mComponentName(strdup(componentName)),
926 mSource(source),
927 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -0700928 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -0700929 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -0700930 mSignalledEOS(false),
931 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -0700932 mOutputPortSettingsHaveChanged(false),
Andreas Huberbe06d262009-08-14 14:37:10 -0700933 mSeekTimeUs(-1) {
934 mPortStatus[kPortIndexInput] = ENABLED;
935 mPortStatus[kPortIndexOutput] = ENABLED;
936
Andreas Huber4c483422009-09-02 16:05:36 -0700937 setComponentRole();
938}
939
Andreas Hubere6c40962009-09-10 14:13:30 -0700940// static
941void OMXCodec::setComponentRole(
942 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
943 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -0700944 struct MimeToRole {
945 const char *mime;
946 const char *decoderRole;
947 const char *encoderRole;
948 };
949
950 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700951 { MEDIA_MIMETYPE_AUDIO_MPEG,
952 "audio_decoder.mp3", "audio_encoder.mp3" },
953 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
954 "audio_decoder.amrnb", "audio_encoder.amrnb" },
955 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
956 "audio_decoder.amrwb", "audio_encoder.amrwb" },
957 { MEDIA_MIMETYPE_AUDIO_AAC,
958 "audio_decoder.aac", "audio_encoder.aac" },
959 { MEDIA_MIMETYPE_VIDEO_AVC,
960 "video_decoder.avc", "video_encoder.avc" },
961 { MEDIA_MIMETYPE_VIDEO_MPEG4,
962 "video_decoder.mpeg4", "video_encoder.mpeg4" },
963 { MEDIA_MIMETYPE_VIDEO_H263,
964 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -0700965 };
966
967 static const size_t kNumMimeToRole =
968 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
969
970 size_t i;
971 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -0700972 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -0700973 break;
974 }
975 }
976
977 if (i == kNumMimeToRole) {
978 return;
979 }
980
981 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -0700982 isEncoder ? kMimeToRole[i].encoderRole
983 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -0700984
985 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -0700986 OMX_PARAM_COMPONENTROLETYPE roleParams;
987 InitOMXParams(&roleParams);
988
989 strncpy((char *)roleParams.cRole,
990 role, OMX_MAX_STRINGNAME_SIZE - 1);
991
992 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
993
Andreas Huber784202e2009-10-15 13:46:54 -0700994 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -0700995 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -0700996 &roleParams, sizeof(roleParams));
997
998 if (err != OK) {
999 LOGW("Failed to set standard component role '%s'.", role);
1000 }
1001 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001002}
1003
Andreas Hubere6c40962009-09-10 14:13:30 -07001004void OMXCodec::setComponentRole() {
1005 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1006}
1007
Andreas Huberbe06d262009-08-14 14:37:10 -07001008OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001009 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001010
Andreas Huber784202e2009-10-15 13:46:54 -07001011 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001012 CHECK_EQ(err, OK);
1013
1014 mNode = NULL;
1015 setState(DEAD);
1016
1017 clearCodecSpecificData();
1018
1019 free(mComponentName);
1020 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001021
Andreas Huberbe06d262009-08-14 14:37:10 -07001022 free(mMIME);
1023 mMIME = NULL;
1024}
1025
1026status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001027 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001028
1029 CHECK_EQ(mState, LOADED);
1030
1031 status_t err;
1032 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001033 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001034 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001035 setState(LOADED_TO_IDLE);
1036 }
1037
1038 err = allocateBuffers();
1039 CHECK_EQ(err, OK);
1040
1041 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001042 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001043 CHECK_EQ(err, OK);
1044
1045 setState(LOADED_TO_IDLE);
1046 }
1047
1048 while (mState != EXECUTING && mState != ERROR) {
1049 mAsyncCompletion.wait(mLock);
1050 }
1051
1052 return mState == ERROR ? UNKNOWN_ERROR : OK;
1053}
1054
1055// static
1056bool OMXCodec::isIntermediateState(State state) {
1057 return state == LOADED_TO_IDLE
1058 || state == IDLE_TO_EXECUTING
1059 || state == EXECUTING_TO_IDLE
1060 || state == IDLE_TO_LOADED
1061 || state == RECONFIGURING;
1062}
1063
1064status_t OMXCodec::allocateBuffers() {
1065 status_t err = allocateBuffersOnPort(kPortIndexInput);
1066
1067 if (err != OK) {
1068 return err;
1069 }
1070
1071 return allocateBuffersOnPort(kPortIndexOutput);
1072}
1073
1074status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1075 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001076 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001077 def.nPortIndex = portIndex;
1078
Andreas Huber784202e2009-10-15 13:46:54 -07001079 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001080 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1081
1082 if (err != OK) {
1083 return err;
1084 }
1085
Andreas Huber5c0a9132009-08-20 11:16:40 -07001086 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
1087 mDealer[portIndex] = new MemoryDealer(totalSize);
1088
Andreas Huberbe06d262009-08-14 14:37:10 -07001089 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001090 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001091 CHECK(mem.get() != NULL);
1092
1093 IOMX::buffer_id buffer;
1094 if (portIndex == kPortIndexInput
1095 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001096 err = mOMX->allocateBufferWithBackup(
Andreas Huberbe06d262009-08-14 14:37:10 -07001097 mNode, portIndex, mem, &buffer);
Andreas Huber446f44f2009-08-25 17:23:44 -07001098 } else if (portIndex == kPortIndexOutput
1099 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001100 err = mOMX->allocateBufferWithBackup(
Andreas Huber2dc64d82009-09-11 12:58:53 -07001101 mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001102 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001103 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001104 }
1105
1106 if (err != OK) {
1107 LOGE("allocate_buffer_with_backup failed");
1108 return err;
1109 }
1110
1111 BufferInfo info;
1112 info.mBuffer = buffer;
1113 info.mOwnedByComponent = false;
1114 info.mMem = mem;
1115 info.mMediaBuffer = NULL;
1116
1117 if (portIndex == kPortIndexOutput) {
1118 info.mMediaBuffer = new MediaBuffer(mem->pointer(), mem->size());
1119 info.mMediaBuffer->setObserver(this);
1120 }
1121
1122 mPortBuffers[portIndex].push(info);
1123
Andreas Huber4c483422009-09-02 16:05:36 -07001124 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001125 portIndex == kPortIndexInput ? "input" : "output");
1126 }
1127
1128 dumpPortStatus(portIndex);
1129
1130 return OK;
1131}
1132
1133void OMXCodec::on_message(const omx_message &msg) {
1134 Mutex::Autolock autoLock(mLock);
1135
1136 switch (msg.type) {
1137 case omx_message::EVENT:
1138 {
1139 onEvent(
1140 msg.u.event_data.event, msg.u.event_data.data1,
1141 msg.u.event_data.data2);
1142
1143 break;
1144 }
1145
1146 case omx_message::EMPTY_BUFFER_DONE:
1147 {
1148 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1149
Andreas Huber4c483422009-09-02 16:05:36 -07001150 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001151
1152 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1153 size_t i = 0;
1154 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1155 ++i;
1156 }
1157
1158 CHECK(i < buffers->size());
1159 if (!(*buffers)[i].mOwnedByComponent) {
1160 LOGW("We already own input buffer %p, yet received "
1161 "an EMPTY_BUFFER_DONE.", buffer);
1162 }
1163
1164 buffers->editItemAt(i).mOwnedByComponent = false;
1165
1166 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001167 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001168
1169 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001170 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001171 CHECK_EQ(err, OK);
1172
1173 buffers->removeAt(i);
1174 } else if (mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
1175 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1176 drainInputBuffer(&buffers->editItemAt(i));
1177 }
1178
1179 break;
1180 }
1181
1182 case omx_message::FILL_BUFFER_DONE:
1183 {
1184 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1185 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1186
Andreas Huber4c483422009-09-02 16:05:36 -07001187 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001188 buffer,
1189 msg.u.extended_buffer_data.range_length,
1190 flags);
1191
Andreas Huber4c483422009-09-02 16:05:36 -07001192 CODEC_LOGV("FILL_BUFFER_DONE(timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001193 msg.u.extended_buffer_data.timestamp,
1194 msg.u.extended_buffer_data.timestamp / 1E6);
1195
1196 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1197 size_t i = 0;
1198 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1199 ++i;
1200 }
1201
1202 CHECK(i < buffers->size());
1203 BufferInfo *info = &buffers->editItemAt(i);
1204
1205 if (!info->mOwnedByComponent) {
1206 LOGW("We already own output buffer %p, yet received "
1207 "a FILL_BUFFER_DONE.", buffer);
1208 }
1209
1210 info->mOwnedByComponent = false;
1211
1212 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001213 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001214
1215 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001216 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001217 CHECK_EQ(err, OK);
1218
1219 buffers->removeAt(i);
Andreas Huberd7795892009-08-26 10:33:47 -07001220 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1221 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001222 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001223 mNoMoreOutputData = true;
1224 mBufferFilled.signal();
1225 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1226 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001227
Andreas Huberbe06d262009-08-14 14:37:10 -07001228 MediaBuffer *buffer = info->mMediaBuffer;
1229
1230 buffer->set_range(
1231 msg.u.extended_buffer_data.range_offset,
1232 msg.u.extended_buffer_data.range_length);
1233
1234 buffer->meta_data()->clear();
1235
Andreas Huberfa8de752009-10-08 10:07:49 -07001236 buffer->meta_data()->setInt64(
1237 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001238
1239 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1240 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1241 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001242 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1243 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1244 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001245
1246 buffer->meta_data()->setPointer(
1247 kKeyPlatformPrivate,
1248 msg.u.extended_buffer_data.platform_private);
1249
1250 buffer->meta_data()->setPointer(
1251 kKeyBufferID,
1252 msg.u.extended_buffer_data.buffer);
1253
1254 mFilledBuffers.push_back(i);
1255 mBufferFilled.signal();
1256 }
1257
1258 break;
1259 }
1260
1261 default:
1262 {
1263 CHECK(!"should not be here.");
1264 break;
1265 }
1266 }
1267}
1268
1269void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1270 switch (event) {
1271 case OMX_EventCmdComplete:
1272 {
1273 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1274 break;
1275 }
1276
1277 case OMX_EventError:
1278 {
1279 LOGE("ERROR(%ld, %ld)", data1, data2);
1280
1281 setState(ERROR);
1282 break;
1283 }
1284
1285 case OMX_EventPortSettingsChanged:
1286 {
1287 onPortSettingsChanged(data1);
1288 break;
1289 }
1290
1291 case OMX_EventBufferFlag:
1292 {
Andreas Huber4c483422009-09-02 16:05:36 -07001293 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001294
1295 if (data1 == kPortIndexOutput) {
1296 mNoMoreOutputData = true;
1297 }
1298 break;
1299 }
1300
1301 default:
1302 {
Andreas Huber4c483422009-09-02 16:05:36 -07001303 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001304 break;
1305 }
1306 }
1307}
1308
Andreas Huberb1678602009-10-19 13:06:40 -07001309// Has the format changed in any way that the client would have to be aware of?
1310static bool formatHasNotablyChanged(
1311 const sp<MetaData> &from, const sp<MetaData> &to) {
1312 if (from.get() == NULL && to.get() == NULL) {
1313 return false;
1314 }
1315
Andreas Huberf68c1682009-10-21 14:01:30 -07001316 if ((from.get() == NULL && to.get() != NULL)
1317 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001318 return true;
1319 }
1320
1321 const char *mime_from, *mime_to;
1322 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1323 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1324
1325 if (strcasecmp(mime_from, mime_to)) {
1326 return true;
1327 }
1328
1329 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1330 int32_t colorFormat_from, colorFormat_to;
1331 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1332 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1333
1334 if (colorFormat_from != colorFormat_to) {
1335 return true;
1336 }
1337
1338 int32_t width_from, width_to;
1339 CHECK(from->findInt32(kKeyWidth, &width_from));
1340 CHECK(to->findInt32(kKeyWidth, &width_to));
1341
1342 if (width_from != width_to) {
1343 return true;
1344 }
1345
1346 int32_t height_from, height_to;
1347 CHECK(from->findInt32(kKeyHeight, &height_from));
1348 CHECK(to->findInt32(kKeyHeight, &height_to));
1349
1350 if (height_from != height_to) {
1351 return true;
1352 }
1353 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1354 int32_t numChannels_from, numChannels_to;
1355 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1356 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1357
1358 if (numChannels_from != numChannels_to) {
1359 return true;
1360 }
1361
1362 int32_t sampleRate_from, sampleRate_to;
1363 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1364 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1365
1366 if (sampleRate_from != sampleRate_to) {
1367 return true;
1368 }
1369 }
1370
1371 return false;
1372}
1373
Andreas Huberbe06d262009-08-14 14:37:10 -07001374void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1375 switch (cmd) {
1376 case OMX_CommandStateSet:
1377 {
1378 onStateChange((OMX_STATETYPE)data);
1379 break;
1380 }
1381
1382 case OMX_CommandPortDisable:
1383 {
1384 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001385 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001386
1387 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1388 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1389 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1390
1391 mPortStatus[portIndex] = DISABLED;
1392
1393 if (mState == RECONFIGURING) {
1394 CHECK_EQ(portIndex, kPortIndexOutput);
1395
Andreas Huberb1678602009-10-19 13:06:40 -07001396 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001397 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001398
1399 // Don't notify clients if the output port settings change
1400 // wasn't of importance to them, i.e. it may be that just the
1401 // number of buffers has changed and nothing else.
1402 mOutputPortSettingsHaveChanged =
1403 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001404
Andreas Huberbe06d262009-08-14 14:37:10 -07001405 enablePortAsync(portIndex);
1406
1407 status_t err = allocateBuffersOnPort(portIndex);
1408 CHECK_EQ(err, OK);
1409 }
1410 break;
1411 }
1412
1413 case OMX_CommandPortEnable:
1414 {
1415 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001416 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001417
1418 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1419 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1420
1421 mPortStatus[portIndex] = ENABLED;
1422
1423 if (mState == RECONFIGURING) {
1424 CHECK_EQ(portIndex, kPortIndexOutput);
1425
1426 setState(EXECUTING);
1427
1428 fillOutputBuffers();
1429 }
1430 break;
1431 }
1432
1433 case OMX_CommandFlush:
1434 {
1435 OMX_U32 portIndex = data;
1436
Andreas Huber4c483422009-09-02 16:05:36 -07001437 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001438
1439 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1440 mPortStatus[portIndex] = ENABLED;
1441
1442 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1443 mPortBuffers[portIndex].size());
1444
1445 if (mState == RECONFIGURING) {
1446 CHECK_EQ(portIndex, kPortIndexOutput);
1447
1448 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001449 } else if (mState == EXECUTING_TO_IDLE) {
1450 if (mPortStatus[kPortIndexInput] == ENABLED
1451 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001452 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001453 "transition from EXECUTING to IDLE.");
1454
1455 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1456 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1457
1458 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001459 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001460 CHECK_EQ(err, OK);
1461 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001462 } else {
1463 // We're flushing both ports in preparation for seeking.
1464
1465 if (mPortStatus[kPortIndexInput] == ENABLED
1466 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001467 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001468 " seek-time.");
1469
Andreas Huber1a77b68e2009-09-17 11:16:52 -07001470 // Clear this flag in case the decoder sent us either
1471 // the EVENT_BUFFER_FLAG(1) or an output buffer with
1472 // the EOS flag set _while_ flushing. Since we're going
1473 // to submit "fresh" input data now, this flag no longer
1474 // applies to our future.
1475 mNoMoreOutputData = false;
1476
Andreas Huberbe06d262009-08-14 14:37:10 -07001477 drainInputBuffers();
1478 fillOutputBuffers();
1479 }
1480 }
1481
1482 break;
1483 }
1484
1485 default:
1486 {
Andreas Huber4c483422009-09-02 16:05:36 -07001487 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001488 break;
1489 }
1490 }
1491}
1492
1493void OMXCodec::onStateChange(OMX_STATETYPE newState) {
1494 switch (newState) {
1495 case OMX_StateIdle:
1496 {
Andreas Huber4c483422009-09-02 16:05:36 -07001497 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001498 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001499 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001500 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1501
1502 CHECK_EQ(err, OK);
1503
1504 setState(IDLE_TO_EXECUTING);
1505 } else {
1506 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1507
1508 CHECK_EQ(
1509 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1510 mPortBuffers[kPortIndexInput].size());
1511
1512 CHECK_EQ(
1513 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1514 mPortBuffers[kPortIndexOutput].size());
1515
Andreas Huber784202e2009-10-15 13:46:54 -07001516 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001517 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1518
1519 CHECK_EQ(err, OK);
1520
1521 err = freeBuffersOnPort(kPortIndexInput);
1522 CHECK_EQ(err, OK);
1523
1524 err = freeBuffersOnPort(kPortIndexOutput);
1525 CHECK_EQ(err, OK);
1526
1527 mPortStatus[kPortIndexInput] = ENABLED;
1528 mPortStatus[kPortIndexOutput] = ENABLED;
1529
1530 setState(IDLE_TO_LOADED);
1531 }
1532 break;
1533 }
1534
1535 case OMX_StateExecuting:
1536 {
1537 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1538
Andreas Huber4c483422009-09-02 16:05:36 -07001539 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001540
1541 setState(EXECUTING);
1542
Andreas Huber42978e52009-08-27 10:08:39 -07001543 // Buffers will be submitted to the component in the first
1544 // call to OMXCodec::read as mInitialBufferSubmit is true at
1545 // this point. This ensures that this on_message call returns,
1546 // releases the lock and ::init can notice the state change and
1547 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001548 break;
1549 }
1550
1551 case OMX_StateLoaded:
1552 {
1553 CHECK_EQ(mState, IDLE_TO_LOADED);
1554
Andreas Huber4c483422009-09-02 16:05:36 -07001555 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001556
1557 setState(LOADED);
1558 break;
1559 }
1560
1561 default:
1562 {
1563 CHECK(!"should not be here.");
1564 break;
1565 }
1566 }
1567}
1568
1569// static
1570size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1571 size_t n = 0;
1572 for (size_t i = 0; i < buffers.size(); ++i) {
1573 if (!buffers[i].mOwnedByComponent) {
1574 ++n;
1575 }
1576 }
1577
1578 return n;
1579}
1580
1581status_t OMXCodec::freeBuffersOnPort(
1582 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1583 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1584
1585 status_t stickyErr = OK;
1586
1587 for (size_t i = buffers->size(); i-- > 0;) {
1588 BufferInfo *info = &buffers->editItemAt(i);
1589
1590 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1591 continue;
1592 }
1593
1594 CHECK_EQ(info->mOwnedByComponent, false);
1595
Andreas Huber92022852009-09-14 15:24:14 -07001596 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1597
Andreas Huberbe06d262009-08-14 14:37:10 -07001598 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001599 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001600
1601 if (err != OK) {
1602 stickyErr = err;
1603 }
1604
1605 if (info->mMediaBuffer != NULL) {
1606 info->mMediaBuffer->setObserver(NULL);
1607
1608 // Make sure nobody but us owns this buffer at this point.
1609 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1610
1611 info->mMediaBuffer->release();
1612 }
1613
1614 buffers->removeAt(i);
1615 }
1616
1617 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1618
1619 return stickyErr;
1620}
1621
1622void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001623 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001624
1625 CHECK_EQ(mState, EXECUTING);
1626 CHECK_EQ(portIndex, kPortIndexOutput);
1627 setState(RECONFIGURING);
1628
1629 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001630 if (!flushPortAsync(portIndex)) {
1631 onCmdComplete(OMX_CommandFlush, portIndex);
1632 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001633 } else {
1634 disablePortAsync(portIndex);
1635 }
1636}
1637
Andreas Huber404cc412009-08-25 14:26:05 -07001638bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001639 CHECK(mState == EXECUTING || mState == RECONFIGURING
1640 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001641
Andreas Huber4c483422009-09-02 16:05:36 -07001642 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001643 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1644 mPortBuffers[portIndex].size());
1645
Andreas Huberbe06d262009-08-14 14:37:10 -07001646 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1647 mPortStatus[portIndex] = SHUTTING_DOWN;
1648
Andreas Huber404cc412009-08-25 14:26:05 -07001649 if ((mQuirks & kRequiresFlushCompleteEmulation)
1650 && countBuffersWeOwn(mPortBuffers[portIndex])
1651 == mPortBuffers[portIndex].size()) {
1652 // No flush is necessary and this component fails to send a
1653 // flush-complete event in this case.
1654
1655 return false;
1656 }
1657
Andreas Huberbe06d262009-08-14 14:37:10 -07001658 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001659 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001660 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001661
1662 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001663}
1664
1665void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1666 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1667
1668 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1669 mPortStatus[portIndex] = DISABLING;
1670
1671 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001672 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001673 CHECK_EQ(err, OK);
1674
1675 freeBuffersOnPort(portIndex, true);
1676}
1677
1678void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1679 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1680
1681 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1682 mPortStatus[portIndex] = ENABLING;
1683
1684 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001685 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001686 CHECK_EQ(err, OK);
1687}
1688
1689void OMXCodec::fillOutputBuffers() {
1690 CHECK_EQ(mState, EXECUTING);
1691
1692 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1693 for (size_t i = 0; i < buffers->size(); ++i) {
1694 fillOutputBuffer(&buffers->editItemAt(i));
1695 }
1696}
1697
1698void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001699 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001700
1701 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1702 for (size_t i = 0; i < buffers->size(); ++i) {
1703 drainInputBuffer(&buffers->editItemAt(i));
1704 }
1705}
1706
1707void OMXCodec::drainInputBuffer(BufferInfo *info) {
1708 CHECK_EQ(info->mOwnedByComponent, false);
1709
1710 if (mSignalledEOS) {
1711 return;
1712 }
1713
1714 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1715 const CodecSpecificData *specific =
1716 mCodecSpecificData[mCodecSpecificDataIndex];
1717
1718 size_t size = specific->mSize;
1719
Andreas Hubere6c40962009-09-10 14:13:30 -07001720 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07001721 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001722 static const uint8_t kNALStartCode[4] =
1723 { 0x00, 0x00, 0x00, 0x01 };
1724
1725 CHECK(info->mMem->size() >= specific->mSize + 4);
1726
1727 size += 4;
1728
1729 memcpy(info->mMem->pointer(), kNALStartCode, 4);
1730 memcpy((uint8_t *)info->mMem->pointer() + 4,
1731 specific->mData, specific->mSize);
1732 } else {
1733 CHECK(info->mMem->size() >= specific->mSize);
1734 memcpy(info->mMem->pointer(), specific->mData, specific->mSize);
1735 }
1736
Andreas Huber784202e2009-10-15 13:46:54 -07001737 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07001738 mNode, info->mBuffer, 0, size,
1739 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1740 0);
Andreas Huber3f427072009-10-08 11:02:27 -07001741 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001742
1743 info->mOwnedByComponent = true;
1744
1745 ++mCodecSpecificDataIndex;
1746 return;
1747 }
1748
1749 MediaBuffer *srcBuffer;
1750 status_t err;
1751 if (mSeekTimeUs >= 0) {
1752 MediaSource::ReadOptions options;
1753 options.setSeekTo(mSeekTimeUs);
1754 mSeekTimeUs = -1;
1755
1756 err = mSource->read(&srcBuffer, &options);
1757 } else {
1758 err = mSource->read(&srcBuffer);
1759 }
1760
1761 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberfa8de752009-10-08 10:07:49 -07001762 OMX_TICKS timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07001763 size_t srcLength = 0;
1764
1765 if (err != OK) {
Andreas Huber4c483422009-09-02 16:05:36 -07001766 CODEC_LOGV("signalling end of input stream.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001767 flags |= OMX_BUFFERFLAG_EOS;
1768
1769 mSignalledEOS = true;
1770 } else {
1771 srcLength = srcBuffer->range_length();
1772
1773 if (info->mMem->size() < srcLength) {
1774 LOGE("info->mMem->size() = %d, srcLength = %d",
1775 info->mMem->size(), srcLength);
1776 }
1777 CHECK(info->mMem->size() >= srcLength);
1778 memcpy(info->mMem->pointer(),
1779 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
1780 srcLength);
1781
Andreas Huberfa8de752009-10-08 10:07:49 -07001782 if (srcBuffer->meta_data()->findInt64(kKeyTime, &timestampUs)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001783 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d)",
Andreas Huberbe06d262009-08-14 14:37:10 -07001784 info->mBuffer, srcLength);
Andreas Huber784202e2009-10-15 13:46:54 -07001785 CODEC_LOGV("Calling emptyBuffer with timestamp %lld us (%.2f secs)",
Andreas Huberfa8de752009-10-08 10:07:49 -07001786 timestampUs, timestampUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07001787 }
1788 }
1789
Andreas Huberbe06d262009-08-14 14:37:10 -07001790 if (srcBuffer != NULL) {
1791 srcBuffer->release();
1792 srcBuffer = NULL;
1793 }
Andreas Huber3f427072009-10-08 11:02:27 -07001794
Andreas Huber784202e2009-10-15 13:46:54 -07001795 err = mOMX->emptyBuffer(
Andreas Huber3f427072009-10-08 11:02:27 -07001796 mNode, info->mBuffer, 0, srcLength,
Andreas Huberfa8de752009-10-08 10:07:49 -07001797 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07001798
1799 if (err != OK) {
1800 setState(ERROR);
1801 return;
1802 }
1803
1804 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001805
1806 // This component does not ever signal the EOS flag on output buffers,
1807 // Thanks for nothing.
1808 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
1809 mNoMoreOutputData = true;
1810 mBufferFilled.signal();
1811 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001812}
1813
1814void OMXCodec::fillOutputBuffer(BufferInfo *info) {
1815 CHECK_EQ(info->mOwnedByComponent, false);
1816
Andreas Huber404cc412009-08-25 14:26:05 -07001817 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07001818 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07001819 "calling fillOutputBuffer");
1820 return;
1821 }
1822
Andreas Huber4c483422009-09-02 16:05:36 -07001823 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07001824 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber3f427072009-10-08 11:02:27 -07001825 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001826
1827 info->mOwnedByComponent = true;
1828}
1829
1830void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
1831 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1832 for (size_t i = 0; i < buffers->size(); ++i) {
1833 if ((*buffers)[i].mBuffer == buffer) {
1834 drainInputBuffer(&buffers->editItemAt(i));
1835 return;
1836 }
1837 }
1838
1839 CHECK(!"should not be here.");
1840}
1841
1842void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
1843 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1844 for (size_t i = 0; i < buffers->size(); ++i) {
1845 if ((*buffers)[i].mBuffer == buffer) {
1846 fillOutputBuffer(&buffers->editItemAt(i));
1847 return;
1848 }
1849 }
1850
1851 CHECK(!"should not be here.");
1852}
1853
1854void OMXCodec::setState(State newState) {
1855 mState = newState;
1856 mAsyncCompletion.signal();
1857
1858 // This may cause some spurious wakeups but is necessary to
1859 // unblock the reader if we enter ERROR state.
1860 mBufferFilled.signal();
1861}
1862
Andreas Huberda050cf22009-09-02 14:01:43 -07001863void OMXCodec::setRawAudioFormat(
1864 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
1865 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07001866 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07001867 pcmParams.nPortIndex = portIndex;
1868
Andreas Huber784202e2009-10-15 13:46:54 -07001869 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001870 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1871
1872 CHECK_EQ(err, OK);
1873
1874 pcmParams.nChannels = numChannels;
1875 pcmParams.eNumData = OMX_NumericalDataSigned;
1876 pcmParams.bInterleaved = OMX_TRUE;
1877 pcmParams.nBitPerSample = 16;
1878 pcmParams.nSamplingRate = sampleRate;
1879 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
1880
1881 if (numChannels == 1) {
1882 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
1883 } else {
1884 CHECK_EQ(numChannels, 2);
1885
1886 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
1887 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
1888 }
1889
Andreas Huber784202e2009-10-15 13:46:54 -07001890 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001891 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1892
1893 CHECK_EQ(err, OK);
1894}
1895
Andreas Huberbe06d262009-08-14 14:37:10 -07001896void OMXCodec::setAMRFormat() {
1897 if (!mIsEncoder) {
1898 OMX_AUDIO_PARAM_AMRTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001899 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001900 def.nPortIndex = kPortIndexInput;
1901
1902 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001903 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07001904
1905 CHECK_EQ(err, OK);
1906
1907 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1908 def.eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
1909
Andreas Huber784202e2009-10-15 13:46:54 -07001910 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07001911 CHECK_EQ(err, OK);
1912 }
1913
1914 ////////////////////////
1915
1916 if (mIsEncoder) {
1917 sp<MetaData> format = mSource->getFormat();
1918 int32_t sampleRate;
1919 int32_t numChannels;
1920 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1921 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1922
Andreas Huberda050cf22009-09-02 14:01:43 -07001923 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
Andreas Huberbe06d262009-08-14 14:37:10 -07001924 }
1925}
1926
Andreas Huberee606e62009-09-08 10:19:21 -07001927void OMXCodec::setAMRWBFormat() {
1928 if (!mIsEncoder) {
1929 OMX_AUDIO_PARAM_AMRTYPE def;
1930 InitOMXParams(&def);
1931 def.nPortIndex = kPortIndexInput;
1932
1933 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001934 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberee606e62009-09-08 10:19:21 -07001935
1936 CHECK_EQ(err, OK);
1937
1938 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1939 def.eAMRBandMode = OMX_AUDIO_AMRBandModeWB0;
1940
Andreas Huber784202e2009-10-15 13:46:54 -07001941 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberee606e62009-09-08 10:19:21 -07001942 CHECK_EQ(err, OK);
1943 }
1944
1945 ////////////////////////
1946
1947 if (mIsEncoder) {
1948 sp<MetaData> format = mSource->getFormat();
1949 int32_t sampleRate;
1950 int32_t numChannels;
1951 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1952 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1953
1954 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1955 }
1956}
1957
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07001958void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) {
Andreas Huberda050cf22009-09-02 14:01:43 -07001959 if (mIsEncoder) {
1960 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1961 } else {
1962 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07001963 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07001964 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07001965
Andreas Huber784202e2009-10-15 13:46:54 -07001966 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001967 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1968 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001969
Andreas Huberda050cf22009-09-02 14:01:43 -07001970 profile.nChannels = numChannels;
1971 profile.nSampleRate = sampleRate;
1972 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07001973
Andreas Huber784202e2009-10-15 13:46:54 -07001974 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07001975 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1976 CHECK_EQ(err, OK);
1977 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001978}
1979
1980void OMXCodec::setImageOutputFormat(
1981 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07001982 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001983
1984#if 0
1985 OMX_INDEXTYPE index;
1986 status_t err = mOMX->get_extension_index(
1987 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
1988 CHECK_EQ(err, OK);
1989
1990 err = mOMX->set_config(mNode, index, &format, sizeof(format));
1991 CHECK_EQ(err, OK);
1992#endif
1993
1994 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001995 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001996 def.nPortIndex = kPortIndexOutput;
1997
Andreas Huber784202e2009-10-15 13:46:54 -07001998 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001999 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2000 CHECK_EQ(err, OK);
2001
2002 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2003
2004 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002005
Andreas Huberbe06d262009-08-14 14:37:10 -07002006 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2007 imageDef->eColorFormat = format;
2008 imageDef->nFrameWidth = width;
2009 imageDef->nFrameHeight = height;
2010
2011 switch (format) {
2012 case OMX_COLOR_FormatYUV420PackedPlanar:
2013 case OMX_COLOR_FormatYUV411Planar:
2014 {
2015 def.nBufferSize = (width * height * 3) / 2;
2016 break;
2017 }
2018
2019 case OMX_COLOR_FormatCbYCrY:
2020 {
2021 def.nBufferSize = width * height * 2;
2022 break;
2023 }
2024
2025 case OMX_COLOR_Format32bitARGB8888:
2026 {
2027 def.nBufferSize = width * height * 4;
2028 break;
2029 }
2030
Andreas Huber201511c2009-09-08 14:01:44 -07002031 case OMX_COLOR_Format16bitARGB4444:
2032 case OMX_COLOR_Format16bitARGB1555:
2033 case OMX_COLOR_Format16bitRGB565:
2034 case OMX_COLOR_Format16bitBGR565:
2035 {
2036 def.nBufferSize = width * height * 2;
2037 break;
2038 }
2039
Andreas Huberbe06d262009-08-14 14:37:10 -07002040 default:
2041 CHECK(!"Should not be here. Unknown color format.");
2042 break;
2043 }
2044
Andreas Huber5c0a9132009-08-20 11:16:40 -07002045 def.nBufferCountActual = def.nBufferCountMin;
2046
Andreas Huber784202e2009-10-15 13:46:54 -07002047 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002048 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2049 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002050}
Andreas Huberbe06d262009-08-14 14:37:10 -07002051
Andreas Huber5c0a9132009-08-20 11:16:40 -07002052void OMXCodec::setJPEGInputFormat(
2053 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2054 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002055 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002056 def.nPortIndex = kPortIndexInput;
2057
Andreas Huber784202e2009-10-15 13:46:54 -07002058 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002059 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2060 CHECK_EQ(err, OK);
2061
Andreas Huber5c0a9132009-08-20 11:16:40 -07002062 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2063 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2064
Andreas Huberbe06d262009-08-14 14:37:10 -07002065 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2066 imageDef->nFrameWidth = width;
2067 imageDef->nFrameHeight = height;
2068
Andreas Huber5c0a9132009-08-20 11:16:40 -07002069 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002070 def.nBufferCountActual = def.nBufferCountMin;
2071
Andreas Huber784202e2009-10-15 13:46:54 -07002072 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002073 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2074 CHECK_EQ(err, OK);
2075}
2076
2077void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2078 CodecSpecificData *specific =
2079 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2080
2081 specific->mSize = size;
2082 memcpy(specific->mData, data, size);
2083
2084 mCodecSpecificData.push(specific);
2085}
2086
2087void OMXCodec::clearCodecSpecificData() {
2088 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2089 free(mCodecSpecificData.editItemAt(i));
2090 }
2091 mCodecSpecificData.clear();
2092 mCodecSpecificDataIndex = 0;
2093}
2094
2095status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07002096 Mutex::Autolock autoLock(mLock);
2097
Andreas Huberbe06d262009-08-14 14:37:10 -07002098 if (mState != LOADED) {
2099 return UNKNOWN_ERROR;
2100 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002101
Andreas Huberbe06d262009-08-14 14:37:10 -07002102 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002103 if (mQuirks & kWantsNALFragments) {
2104 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002105 }
2106 status_t err = mSource->start(params.get());
2107
2108 if (err != OK) {
2109 return err;
2110 }
2111
2112 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002113 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002114 mSignalledEOS = false;
2115 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002116 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002117 mSeekTimeUs = -1;
2118 mFilledBuffers.clear();
2119
2120 return init();
2121}
2122
2123status_t OMXCodec::stop() {
Andreas Huber4c483422009-09-02 16:05:36 -07002124 CODEC_LOGV("stop");
Andreas Huberbe06d262009-08-14 14:37:10 -07002125
2126 Mutex::Autolock autoLock(mLock);
2127
2128 while (isIntermediateState(mState)) {
2129 mAsyncCompletion.wait(mLock);
2130 }
2131
2132 switch (mState) {
2133 case LOADED:
2134 case ERROR:
2135 break;
2136
2137 case EXECUTING:
2138 {
2139 setState(EXECUTING_TO_IDLE);
2140
Andreas Huber127fcdc2009-08-26 16:27:02 -07002141 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002142 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002143 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002144
Andreas Huber127fcdc2009-08-26 16:27:02 -07002145 bool emulateInputFlushCompletion =
2146 !flushPortAsync(kPortIndexInput);
2147
2148 bool emulateOutputFlushCompletion =
2149 !flushPortAsync(kPortIndexOutput);
2150
2151 if (emulateInputFlushCompletion) {
2152 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2153 }
2154
2155 if (emulateOutputFlushCompletion) {
2156 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2157 }
2158 } else {
2159 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2160 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2161
2162 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002163 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002164 CHECK_EQ(err, OK);
2165 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002166
2167 while (mState != LOADED && mState != ERROR) {
2168 mAsyncCompletion.wait(mLock);
2169 }
2170
2171 break;
2172 }
2173
2174 default:
2175 {
2176 CHECK(!"should not be here.");
2177 break;
2178 }
2179 }
2180
2181 mSource->stop();
2182
2183 return OK;
2184}
2185
2186sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002187 Mutex::Autolock autoLock(mLock);
2188
Andreas Huberbe06d262009-08-14 14:37:10 -07002189 return mOutputFormat;
2190}
2191
2192status_t OMXCodec::read(
2193 MediaBuffer **buffer, const ReadOptions *options) {
2194 *buffer = NULL;
2195
2196 Mutex::Autolock autoLock(mLock);
2197
Andreas Huberd06e5b82009-08-28 13:18:14 -07002198 if (mState != EXECUTING && mState != RECONFIGURING) {
2199 return UNKNOWN_ERROR;
2200 }
2201
Andreas Hubere981c332009-10-22 13:49:30 -07002202 bool seeking = false;
2203 int64_t seekTimeUs;
2204 if (options && options->getSeekTo(&seekTimeUs)) {
2205 seeking = true;
2206 }
2207
Andreas Huber42978e52009-08-27 10:08:39 -07002208 if (mInitialBufferSubmit) {
2209 mInitialBufferSubmit = false;
2210
Andreas Hubere981c332009-10-22 13:49:30 -07002211 if (seeking) {
2212 CHECK(seekTimeUs >= 0);
2213 mSeekTimeUs = seekTimeUs;
2214
2215 // There's no reason to trigger the code below, there's
2216 // nothing to flush yet.
2217 seeking = false;
2218 }
2219
Andreas Huber42978e52009-08-27 10:08:39 -07002220 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002221
Andreas Huberd06e5b82009-08-28 13:18:14 -07002222 if (mState == EXECUTING) {
2223 // Otherwise mState == RECONFIGURING and this code will trigger
2224 // after the output port is reenabled.
2225 fillOutputBuffers();
2226 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002227 }
2228
Andreas Hubere981c332009-10-22 13:49:30 -07002229 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002230 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002231
2232 mSignalledEOS = false;
2233 mNoMoreOutputData = false;
2234
2235 CHECK(seekTimeUs >= 0);
2236 mSeekTimeUs = seekTimeUs;
2237
2238 mFilledBuffers.clear();
2239
2240 CHECK_EQ(mState, EXECUTING);
2241
Andreas Huber404cc412009-08-25 14:26:05 -07002242 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2243 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2244
2245 if (emulateInputFlushCompletion) {
2246 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2247 }
2248
2249 if (emulateOutputFlushCompletion) {
2250 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2251 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002252 }
2253
2254 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2255 mBufferFilled.wait(mLock);
2256 }
2257
2258 if (mState == ERROR) {
2259 return UNKNOWN_ERROR;
2260 }
2261
2262 if (mFilledBuffers.empty()) {
2263 return ERROR_END_OF_STREAM;
2264 }
2265
Andreas Hubercfd55572009-10-09 14:11:28 -07002266 if (mOutputPortSettingsHaveChanged) {
2267 mOutputPortSettingsHaveChanged = false;
2268
2269 return INFO_FORMAT_CHANGED;
2270 }
2271
Andreas Huberbe06d262009-08-14 14:37:10 -07002272 size_t index = *mFilledBuffers.begin();
2273 mFilledBuffers.erase(mFilledBuffers.begin());
2274
2275 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2276 info->mMediaBuffer->add_ref();
2277 *buffer = info->mMediaBuffer;
2278
2279 return OK;
2280}
2281
2282void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2283 Mutex::Autolock autoLock(mLock);
2284
2285 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2286 for (size_t i = 0; i < buffers->size(); ++i) {
2287 BufferInfo *info = &buffers->editItemAt(i);
2288
2289 if (info->mMediaBuffer == buffer) {
2290 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2291 fillOutputBuffer(info);
2292 return;
2293 }
2294 }
2295
2296 CHECK(!"should not be here.");
2297}
2298
2299static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2300 static const char *kNames[] = {
2301 "OMX_IMAGE_CodingUnused",
2302 "OMX_IMAGE_CodingAutoDetect",
2303 "OMX_IMAGE_CodingJPEG",
2304 "OMX_IMAGE_CodingJPEG2K",
2305 "OMX_IMAGE_CodingEXIF",
2306 "OMX_IMAGE_CodingTIFF",
2307 "OMX_IMAGE_CodingGIF",
2308 "OMX_IMAGE_CodingPNG",
2309 "OMX_IMAGE_CodingLZW",
2310 "OMX_IMAGE_CodingBMP",
2311 };
2312
2313 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2314
2315 if (type < 0 || (size_t)type >= numNames) {
2316 return "UNKNOWN";
2317 } else {
2318 return kNames[type];
2319 }
2320}
2321
2322static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2323 static const char *kNames[] = {
2324 "OMX_COLOR_FormatUnused",
2325 "OMX_COLOR_FormatMonochrome",
2326 "OMX_COLOR_Format8bitRGB332",
2327 "OMX_COLOR_Format12bitRGB444",
2328 "OMX_COLOR_Format16bitARGB4444",
2329 "OMX_COLOR_Format16bitARGB1555",
2330 "OMX_COLOR_Format16bitRGB565",
2331 "OMX_COLOR_Format16bitBGR565",
2332 "OMX_COLOR_Format18bitRGB666",
2333 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002334 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002335 "OMX_COLOR_Format24bitRGB888",
2336 "OMX_COLOR_Format24bitBGR888",
2337 "OMX_COLOR_Format24bitARGB1887",
2338 "OMX_COLOR_Format25bitARGB1888",
2339 "OMX_COLOR_Format32bitBGRA8888",
2340 "OMX_COLOR_Format32bitARGB8888",
2341 "OMX_COLOR_FormatYUV411Planar",
2342 "OMX_COLOR_FormatYUV411PackedPlanar",
2343 "OMX_COLOR_FormatYUV420Planar",
2344 "OMX_COLOR_FormatYUV420PackedPlanar",
2345 "OMX_COLOR_FormatYUV420SemiPlanar",
2346 "OMX_COLOR_FormatYUV422Planar",
2347 "OMX_COLOR_FormatYUV422PackedPlanar",
2348 "OMX_COLOR_FormatYUV422SemiPlanar",
2349 "OMX_COLOR_FormatYCbYCr",
2350 "OMX_COLOR_FormatYCrYCb",
2351 "OMX_COLOR_FormatCbYCrY",
2352 "OMX_COLOR_FormatCrYCbY",
2353 "OMX_COLOR_FormatYUV444Interleaved",
2354 "OMX_COLOR_FormatRawBayer8bit",
2355 "OMX_COLOR_FormatRawBayer10bit",
2356 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002357 "OMX_COLOR_FormatL2",
2358 "OMX_COLOR_FormatL4",
2359 "OMX_COLOR_FormatL8",
2360 "OMX_COLOR_FormatL16",
2361 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002362 "OMX_COLOR_FormatL32",
2363 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2364 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2365 "OMX_COLOR_Format18BitBGR666",
2366 "OMX_COLOR_Format24BitARGB6666",
2367 "OMX_COLOR_Format24BitABGR6666",
2368 };
2369
2370 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2371
Andreas Huberbe06d262009-08-14 14:37:10 -07002372 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2373 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2374 } else if (type < 0 || (size_t)type >= numNames) {
2375 return "UNKNOWN";
2376 } else {
2377 return kNames[type];
2378 }
2379}
2380
2381static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2382 static const char *kNames[] = {
2383 "OMX_VIDEO_CodingUnused",
2384 "OMX_VIDEO_CodingAutoDetect",
2385 "OMX_VIDEO_CodingMPEG2",
2386 "OMX_VIDEO_CodingH263",
2387 "OMX_VIDEO_CodingMPEG4",
2388 "OMX_VIDEO_CodingWMV",
2389 "OMX_VIDEO_CodingRV",
2390 "OMX_VIDEO_CodingAVC",
2391 "OMX_VIDEO_CodingMJPEG",
2392 };
2393
2394 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2395
2396 if (type < 0 || (size_t)type >= numNames) {
2397 return "UNKNOWN";
2398 } else {
2399 return kNames[type];
2400 }
2401}
2402
2403static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2404 static const char *kNames[] = {
2405 "OMX_AUDIO_CodingUnused",
2406 "OMX_AUDIO_CodingAutoDetect",
2407 "OMX_AUDIO_CodingPCM",
2408 "OMX_AUDIO_CodingADPCM",
2409 "OMX_AUDIO_CodingAMR",
2410 "OMX_AUDIO_CodingGSMFR",
2411 "OMX_AUDIO_CodingGSMEFR",
2412 "OMX_AUDIO_CodingGSMHR",
2413 "OMX_AUDIO_CodingPDCFR",
2414 "OMX_AUDIO_CodingPDCEFR",
2415 "OMX_AUDIO_CodingPDCHR",
2416 "OMX_AUDIO_CodingTDMAFR",
2417 "OMX_AUDIO_CodingTDMAEFR",
2418 "OMX_AUDIO_CodingQCELP8",
2419 "OMX_AUDIO_CodingQCELP13",
2420 "OMX_AUDIO_CodingEVRC",
2421 "OMX_AUDIO_CodingSMV",
2422 "OMX_AUDIO_CodingG711",
2423 "OMX_AUDIO_CodingG723",
2424 "OMX_AUDIO_CodingG726",
2425 "OMX_AUDIO_CodingG729",
2426 "OMX_AUDIO_CodingAAC",
2427 "OMX_AUDIO_CodingMP3",
2428 "OMX_AUDIO_CodingSBC",
2429 "OMX_AUDIO_CodingVORBIS",
2430 "OMX_AUDIO_CodingWMA",
2431 "OMX_AUDIO_CodingRA",
2432 "OMX_AUDIO_CodingMIDI",
2433 };
2434
2435 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2436
2437 if (type < 0 || (size_t)type >= numNames) {
2438 return "UNKNOWN";
2439 } else {
2440 return kNames[type];
2441 }
2442}
2443
2444static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2445 static const char *kNames[] = {
2446 "OMX_AUDIO_PCMModeLinear",
2447 "OMX_AUDIO_PCMModeALaw",
2448 "OMX_AUDIO_PCMModeMULaw",
2449 };
2450
2451 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2452
2453 if (type < 0 || (size_t)type >= numNames) {
2454 return "UNKNOWN";
2455 } else {
2456 return kNames[type];
2457 }
2458}
2459
Andreas Huber7ae02c82009-09-09 16:29:47 -07002460static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2461 static const char *kNames[] = {
2462 "OMX_AUDIO_AMRBandModeUnused",
2463 "OMX_AUDIO_AMRBandModeNB0",
2464 "OMX_AUDIO_AMRBandModeNB1",
2465 "OMX_AUDIO_AMRBandModeNB2",
2466 "OMX_AUDIO_AMRBandModeNB3",
2467 "OMX_AUDIO_AMRBandModeNB4",
2468 "OMX_AUDIO_AMRBandModeNB5",
2469 "OMX_AUDIO_AMRBandModeNB6",
2470 "OMX_AUDIO_AMRBandModeNB7",
2471 "OMX_AUDIO_AMRBandModeWB0",
2472 "OMX_AUDIO_AMRBandModeWB1",
2473 "OMX_AUDIO_AMRBandModeWB2",
2474 "OMX_AUDIO_AMRBandModeWB3",
2475 "OMX_AUDIO_AMRBandModeWB4",
2476 "OMX_AUDIO_AMRBandModeWB5",
2477 "OMX_AUDIO_AMRBandModeWB6",
2478 "OMX_AUDIO_AMRBandModeWB7",
2479 "OMX_AUDIO_AMRBandModeWB8",
2480 };
2481
2482 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2483
2484 if (type < 0 || (size_t)type >= numNames) {
2485 return "UNKNOWN";
2486 } else {
2487 return kNames[type];
2488 }
2489}
2490
2491static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2492 static const char *kNames[] = {
2493 "OMX_AUDIO_AMRFrameFormatConformance",
2494 "OMX_AUDIO_AMRFrameFormatIF1",
2495 "OMX_AUDIO_AMRFrameFormatIF2",
2496 "OMX_AUDIO_AMRFrameFormatFSF",
2497 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2498 "OMX_AUDIO_AMRFrameFormatITU",
2499 };
2500
2501 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2502
2503 if (type < 0 || (size_t)type >= numNames) {
2504 return "UNKNOWN";
2505 } else {
2506 return kNames[type];
2507 }
2508}
Andreas Huberbe06d262009-08-14 14:37:10 -07002509
2510void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2511 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002512 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002513 def.nPortIndex = portIndex;
2514
Andreas Huber784202e2009-10-15 13:46:54 -07002515 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002516 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2517 CHECK_EQ(err, OK);
2518
2519 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2520
2521 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2522 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2523
2524 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2525 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2526 printf(" nBufferSize = %ld\n", def.nBufferSize);
2527
2528 switch (def.eDomain) {
2529 case OMX_PortDomainImage:
2530 {
2531 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2532
2533 printf("\n");
2534 printf(" // Image\n");
2535 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2536 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2537 printf(" nStride = %ld\n", imageDef->nStride);
2538
2539 printf(" eCompressionFormat = %s\n",
2540 imageCompressionFormatString(imageDef->eCompressionFormat));
2541
2542 printf(" eColorFormat = %s\n",
2543 colorFormatString(imageDef->eColorFormat));
2544
2545 break;
2546 }
2547
2548 case OMX_PortDomainVideo:
2549 {
2550 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2551
2552 printf("\n");
2553 printf(" // Video\n");
2554 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2555 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2556 printf(" nStride = %ld\n", videoDef->nStride);
2557
2558 printf(" eCompressionFormat = %s\n",
2559 videoCompressionFormatString(videoDef->eCompressionFormat));
2560
2561 printf(" eColorFormat = %s\n",
2562 colorFormatString(videoDef->eColorFormat));
2563
2564 break;
2565 }
2566
2567 case OMX_PortDomainAudio:
2568 {
2569 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2570
2571 printf("\n");
2572 printf(" // Audio\n");
2573 printf(" eEncoding = %s\n",
2574 audioCodingTypeString(audioDef->eEncoding));
2575
2576 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2577 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002578 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002579 params.nPortIndex = portIndex;
2580
Andreas Huber784202e2009-10-15 13:46:54 -07002581 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002582 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2583 CHECK_EQ(err, OK);
2584
2585 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
2586 printf(" nChannels = %ld\n", params.nChannels);
2587 printf(" bInterleaved = %d\n", params.bInterleaved);
2588 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
2589
2590 printf(" eNumData = %s\n",
2591 params.eNumData == OMX_NumericalDataSigned
2592 ? "signed" : "unsigned");
2593
2594 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07002595 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
2596 OMX_AUDIO_PARAM_AMRTYPE amr;
2597 InitOMXParams(&amr);
2598 amr.nPortIndex = portIndex;
2599
Andreas Huber784202e2009-10-15 13:46:54 -07002600 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07002601 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2602 CHECK_EQ(err, OK);
2603
2604 printf(" nChannels = %ld\n", amr.nChannels);
2605 printf(" eAMRBandMode = %s\n",
2606 amrBandModeString(amr.eAMRBandMode));
2607 printf(" eAMRFrameFormat = %s\n",
2608 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07002609 }
2610
2611 break;
2612 }
2613
2614 default:
2615 {
2616 printf(" // Unknown\n");
2617 break;
2618 }
2619 }
2620
2621 printf("}\n");
2622}
2623
2624void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
2625 mOutputFormat = new MetaData;
2626 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
2627
2628 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002629 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002630 def.nPortIndex = kPortIndexOutput;
2631
Andreas Huber784202e2009-10-15 13:46:54 -07002632 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002633 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2634 CHECK_EQ(err, OK);
2635
2636 switch (def.eDomain) {
2637 case OMX_PortDomainImage:
2638 {
2639 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2640 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2641
Andreas Hubere6c40962009-09-10 14:13:30 -07002642 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002643 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
2644 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
2645 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
2646 break;
2647 }
2648
2649 case OMX_PortDomainAudio:
2650 {
2651 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
2652
Andreas Huberda050cf22009-09-02 14:01:43 -07002653 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
2654 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002655 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07002656 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002657
Andreas Huber784202e2009-10-15 13:46:54 -07002658 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002659 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2660 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002661
Andreas Huberda050cf22009-09-02 14:01:43 -07002662 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
2663 CHECK_EQ(params.nBitPerSample, 16);
2664 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07002665
Andreas Huberda050cf22009-09-02 14:01:43 -07002666 int32_t numChannels, sampleRate;
2667 inputFormat->findInt32(kKeyChannelCount, &numChannels);
2668 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07002669
Andreas Huberda050cf22009-09-02 14:01:43 -07002670 if ((OMX_U32)numChannels != params.nChannels) {
2671 LOGW("Codec outputs a different number of channels than "
2672 "the input stream contains.");
2673 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002674
Andreas Hubere6c40962009-09-10 14:13:30 -07002675 mOutputFormat->setCString(
2676 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07002677
2678 // Use the codec-advertised number of channels, as some
2679 // codecs appear to output stereo even if the input data is
2680 // mono.
2681 mOutputFormat->setInt32(kKeyChannelCount, params.nChannels);
2682
2683 // The codec-reported sampleRate is not reliable...
2684 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
2685 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07002686 OMX_AUDIO_PARAM_AMRTYPE amr;
2687 InitOMXParams(&amr);
2688 amr.nPortIndex = kPortIndexOutput;
2689
Andreas Huber784202e2009-10-15 13:46:54 -07002690 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07002691 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2692 CHECK_EQ(err, OK);
2693
2694 CHECK_EQ(amr.nChannels, 1);
2695 mOutputFormat->setInt32(kKeyChannelCount, 1);
2696
2697 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
2698 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002699 mOutputFormat->setCString(
2700 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002701 mOutputFormat->setInt32(kKeySampleRate, 8000);
2702 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
2703 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002704 mOutputFormat->setCString(
2705 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07002706 mOutputFormat->setInt32(kKeySampleRate, 16000);
2707 } else {
2708 CHECK(!"Unknown AMR band mode.");
2709 }
Andreas Huberda050cf22009-09-02 14:01:43 -07002710 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002711 mOutputFormat->setCString(
2712 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
Andreas Huberda050cf22009-09-02 14:01:43 -07002713 } else {
2714 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07002715 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002716 break;
2717 }
2718
2719 case OMX_PortDomainVideo:
2720 {
2721 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
2722
2723 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002724 mOutputFormat->setCString(
2725 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07002726 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002727 mOutputFormat->setCString(
2728 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002729 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002730 mOutputFormat->setCString(
2731 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07002732 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07002733 mOutputFormat->setCString(
2734 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07002735 } else {
2736 CHECK(!"Unknown compression format.");
2737 }
2738
2739 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
2740 // This component appears to be lying to me.
2741 mOutputFormat->setInt32(
2742 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
2743 mOutputFormat->setInt32(
2744 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
2745 } else {
2746 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
2747 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
2748 }
2749
2750 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
2751 break;
2752 }
2753
2754 default:
2755 {
2756 CHECK(!"should not be here, neither audio nor video.");
2757 break;
2758 }
2759 }
2760}
2761
Andreas Hubere6c40962009-09-10 14:13:30 -07002762////////////////////////////////////////////////////////////////////////////////
2763
2764status_t QueryCodecs(
2765 const sp<IOMX> &omx,
2766 const char *mime, bool queryDecoders,
2767 Vector<CodecCapabilities> *results) {
2768 results->clear();
2769
2770 for (int index = 0;; ++index) {
2771 const char *componentName;
2772
2773 if (!queryDecoders) {
2774 componentName = GetCodec(
2775 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
2776 mime, index);
2777 } else {
2778 componentName = GetCodec(
2779 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
2780 mime, index);
2781 }
2782
2783 if (!componentName) {
2784 return OK;
2785 }
2786
Andreas Huber784202e2009-10-15 13:46:54 -07002787 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07002788 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07002789 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07002790
2791 if (err != OK) {
2792 continue;
2793 }
2794
2795 OMXCodec::setComponentRole(omx, node, queryDecoders, mime);
2796
2797 results->push();
2798 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
2799 caps->mComponentName = componentName;
2800
2801 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
2802 InitOMXParams(&param);
2803
2804 param.nPortIndex = queryDecoders ? 0 : 1;
2805
2806 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07002807 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07002808 node, OMX_IndexParamVideoProfileLevelQuerySupported,
2809 &param, sizeof(param));
2810
2811 if (err != OK) {
2812 break;
2813 }
2814
2815 CodecProfileLevel profileLevel;
2816 profileLevel.mProfile = param.eProfile;
2817 profileLevel.mLevel = param.eLevel;
2818
2819 caps->mProfileLevels.push(profileLevel);
2820 }
2821
Andreas Huber784202e2009-10-15 13:46:54 -07002822 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07002823 }
2824}
2825
Andreas Huberbe06d262009-08-14 14:37:10 -07002826} // namespace android