blob: 7c3df3105777431be191e1c712f870c8412e75c0 [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 Huberdacaa732009-12-07 09:56:32 -080021#include "include/AACDecoder.h"
James Dong17299ab2010-05-14 15:45:22 -070022#include "include/AACEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080023#include "include/AMRNBDecoder.h"
Andreas Huberd49b526dd2009-12-11 15:07:25 -080024#include "include/AMRNBEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080025#include "include/AMRWBDecoder.h"
James Dong17299ab2010-05-14 15:45:22 -070026#include "include/AMRWBEncoder.h"
Andreas Huber4a0ec3f2009-12-10 09:44:29 -080027#include "include/AVCDecoder.h"
James Dong02f5b542009-12-15 16:26:55 -080028#include "include/M4vH263Decoder.h"
Andreas Huber250f2432009-12-07 14:22:35 -080029#include "include/MP3Decoder.h"
Andreas Huber388379f2010-05-07 10:35:13 -070030#include "include/VorbisDecoder.h"
Andreas Huber47ba30e2010-05-24 14:38:02 -070031#include "include/VPXDecoder.h"
Andreas Huber8c7ab032009-12-07 11:23:44 -080032
Andreas Huberbd7b43b2009-10-13 10:22:55 -070033#include "include/ESDS.h"
34
Andreas Huberbe06d262009-08-14 14:37:10 -070035#include <binder/IServiceManager.h>
36#include <binder/MemoryDealer.h>
37#include <binder/ProcessState.h>
38#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070039#include <media/stagefright/MediaBuffer.h>
40#include <media/stagefright/MediaBufferGroup.h>
41#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070042#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070043#include <media/stagefright/MediaExtractor.h>
44#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070045#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070046#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070047#include <utils/Vector.h>
48
49#include <OMX_Audio.h>
50#include <OMX_Component.h>
51
52namespace android {
53
Andreas Huber8b432b12009-10-07 13:36:52 -070054static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
55
Andreas Huberbe06d262009-08-14 14:37:10 -070056struct CodecInfo {
57 const char *mime;
58 const char *codec;
59};
60
Andreas Huberfb1c2f82009-12-15 13:25:11 -080061#define FACTORY_CREATE(name) \
62static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
63 return new name(source); \
64}
65
James Dong17299ab2010-05-14 15:45:22 -070066#define FACTORY_CREATE_ENCODER(name) \
67static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
68 return new name(source, meta); \
69}
70
Andreas Huberfb1c2f82009-12-15 13:25:11 -080071#define FACTORY_REF(name) { #name, Make##name },
72
73FACTORY_CREATE(MP3Decoder)
74FACTORY_CREATE(AMRNBDecoder)
75FACTORY_CREATE(AMRWBDecoder)
76FACTORY_CREATE(AACDecoder)
77FACTORY_CREATE(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -080078FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070079FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070080FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070081FACTORY_CREATE_ENCODER(AMRNBEncoder)
82FACTORY_CREATE_ENCODER(AMRWBEncoder)
83FACTORY_CREATE_ENCODER(AACEncoder)
84
85static sp<MediaSource> InstantiateSoftwareEncoder(
86 const char *name, const sp<MediaSource> &source,
87 const sp<MetaData> &meta) {
88 struct FactoryInfo {
89 const char *name;
90 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
91 };
92
93 static const FactoryInfo kFactoryInfo[] = {
94 FACTORY_REF(AMRNBEncoder)
95 FACTORY_REF(AMRWBEncoder)
96 FACTORY_REF(AACEncoder)
97 };
98 for (size_t i = 0;
99 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
100 if (!strcmp(name, kFactoryInfo[i].name)) {
101 return (*kFactoryInfo[i].CreateFunc)(source, meta);
102 }
103 }
104
105 return NULL;
106}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800107
108static sp<MediaSource> InstantiateSoftwareCodec(
109 const char *name, const sp<MediaSource> &source) {
110 struct FactoryInfo {
111 const char *name;
112 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
113 };
114
115 static const FactoryInfo kFactoryInfo[] = {
116 FACTORY_REF(MP3Decoder)
117 FACTORY_REF(AMRNBDecoder)
118 FACTORY_REF(AMRWBDecoder)
119 FACTORY_REF(AACDecoder)
120 FACTORY_REF(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -0800121 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700122 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700123 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800124 };
125 for (size_t i = 0;
126 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
127 if (!strcmp(name, kFactoryInfo[i].name)) {
128 return (*kFactoryInfo[i].CreateFunc)(source);
129 }
130 }
131
132 return NULL;
133}
134
135#undef FACTORY_REF
136#undef FACTORY_CREATE
137
Andreas Huberbe06d262009-08-14 14:37:10 -0700138static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700139 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
James Dong374aee62010-04-26 10:23:30 -0700140// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800141 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700142// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700143// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800144 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700145// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700146 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800147 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700148// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700149 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800150 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700151// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700152 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
153 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800154 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700155// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700156 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800157 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700158// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700159 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
160 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800161 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700162// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huber388379f2010-05-07 10:35:13 -0700163 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700164 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700165};
166
167static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700168 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800169 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700170 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700171 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700172 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700173 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700174 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
175 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
176 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
177 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
178 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
179 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
180 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700181 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700182 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
183 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700184};
185
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800186#undef OPTIONAL
187
Andreas Hubere0873732009-09-10 09:57:53 -0700188#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700189#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800190#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700191
Andreas Huberbe06d262009-08-14 14:37:10 -0700192struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700193 OMXCodecObserver() {
194 }
195
196 void setCodec(const sp<OMXCodec> &target) {
197 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700198 }
199
200 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700201 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700202 sp<OMXCodec> codec = mTarget.promote();
203
204 if (codec.get() != NULL) {
205 codec->on_message(msg);
206 }
207 }
208
209protected:
210 virtual ~OMXCodecObserver() {}
211
212private:
213 wp<OMXCodec> mTarget;
214
215 OMXCodecObserver(const OMXCodecObserver &);
216 OMXCodecObserver &operator=(const OMXCodecObserver &);
217};
218
219static const char *GetCodec(const CodecInfo *info, size_t numInfos,
220 const char *mime, int index) {
221 CHECK(index >= 0);
222 for(size_t i = 0; i < numInfos; ++i) {
223 if (!strcasecmp(mime, info[i].mime)) {
224 if (index == 0) {
225 return info[i].codec;
226 }
227
228 --index;
229 }
230 }
231
232 return NULL;
233}
234
Andreas Huberebf66ea2009-08-19 13:32:58 -0700235enum {
236 kAVCProfileBaseline = 0x42,
237 kAVCProfileMain = 0x4d,
238 kAVCProfileExtended = 0x58,
239 kAVCProfileHigh = 0x64,
240 kAVCProfileHigh10 = 0x6e,
241 kAVCProfileHigh422 = 0x7a,
242 kAVCProfileHigh444 = 0xf4,
243 kAVCProfileCAVLC444Intra = 0x2c
244};
245
246static const char *AVCProfileToString(uint8_t profile) {
247 switch (profile) {
248 case kAVCProfileBaseline:
249 return "Baseline";
250 case kAVCProfileMain:
251 return "Main";
252 case kAVCProfileExtended:
253 return "Extended";
254 case kAVCProfileHigh:
255 return "High";
256 case kAVCProfileHigh10:
257 return "High 10";
258 case kAVCProfileHigh422:
259 return "High 422";
260 case kAVCProfileHigh444:
261 return "High 444";
262 case kAVCProfileCAVLC444Intra:
263 return "CAVLC 444 Intra";
264 default: return "Unknown";
265 }
266}
267
Andreas Huber4c483422009-09-02 16:05:36 -0700268template<class T>
269static void InitOMXParams(T *params) {
270 params->nSize = sizeof(T);
271 params->nVersion.s.nVersionMajor = 1;
272 params->nVersion.s.nVersionMinor = 0;
273 params->nVersion.s.nRevision = 0;
274 params->nVersion.s.nStep = 0;
275}
276
Andreas Hubere13526a2009-10-22 10:43:34 -0700277static bool IsSoftwareCodec(const char *componentName) {
278 if (!strncmp("OMX.PV.", componentName, 7)) {
279 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700280 }
281
Andreas Hubere13526a2009-10-22 10:43:34 -0700282 return false;
283}
284
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800285// A sort order in which non-OMX components are first,
286// followed by software codecs, i.e. OMX.PV.*, followed
287// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700288static int CompareSoftwareCodecsFirst(
289 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800290 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
291 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
292
293 if (isNotOMX1) {
294 if (isNotOMX2) { return 0; }
295 return -1;
296 }
297 if (isNotOMX2) {
298 return 1;
299 }
300
Andreas Hubere13526a2009-10-22 10:43:34 -0700301 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
302 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
303
304 if (isSoftwareCodec1) {
305 if (isSoftwareCodec2) { return 0; }
306 return -1;
307 }
308
309 if (isSoftwareCodec2) {
310 return 1;
311 }
312
313 return 0;
314}
315
316// static
317uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700318 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700319
Andreas Huberbe06d262009-08-14 14:37:10 -0700320 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700321 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700322 }
323 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
324 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800325 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700326 }
327 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
328 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700329 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700330 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700331 }
332 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
333 quirks |= kRequiresLoadedToIdleAfterAllocation;
334 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700335 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huberbe06d262009-08-14 14:37:10 -0700336 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700337 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700338 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800339 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700340 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700341
Andreas Huber2dc64d82009-09-11 12:58:53 -0700342 if (!strncmp(componentName, "OMX.TI.", 7)) {
343 // Apparently I must not use OMX_UseBuffer on either input or
344 // output ports on any of the TI components or quote:
345 // "(I) may have unexpected problem (sic) which can be timing related
346 // and hard to reproduce."
347
348 quirks |= kRequiresAllocateBufferOnInputPorts;
349 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dong4f501f02010-06-07 14:41:41 -0700350 if (!strncmp(componentName, "OMX.TI.video.encoder", 20)) {
351 quirks |= kAvoidMemcopyInputRecordingFrames;
352 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700353 }
354
Andreas Huberb8de9572010-02-22 14:58:45 -0800355 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
356 quirks |= kInputBufferSizesAreBogus;
357 }
358
Andreas Hubere13526a2009-10-22 10:43:34 -0700359 return quirks;
360}
361
362// static
363void OMXCodec::findMatchingCodecs(
364 const char *mime,
365 bool createEncoder, const char *matchComponentName,
366 uint32_t flags,
367 Vector<String8> *matchingCodecs) {
368 matchingCodecs->clear();
369
370 for (int index = 0;; ++index) {
371 const char *componentName;
372
373 if (createEncoder) {
374 componentName = GetCodec(
375 kEncoderInfo,
376 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
377 mime, index);
378 } else {
379 componentName = GetCodec(
380 kDecoderInfo,
381 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
382 mime, index);
383 }
384
385 if (!componentName) {
386 break;
387 }
388
389 // If a specific codec is requested, skip the non-matching ones.
390 if (matchComponentName && strcmp(componentName, matchComponentName)) {
391 continue;
392 }
393
394 matchingCodecs->push(String8(componentName));
395 }
396
397 if (flags & kPreferSoftwareCodecs) {
398 matchingCodecs->sort(CompareSoftwareCodecsFirst);
399 }
400}
401
402// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800403sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700404 const sp<IOMX> &omx,
405 const sp<MetaData> &meta, bool createEncoder,
406 const sp<MediaSource> &source,
407 const char *matchComponentName,
408 uint32_t flags) {
409 const char *mime;
410 bool success = meta->findCString(kKeyMIMEType, &mime);
411 CHECK(success);
412
413 Vector<String8> matchingCodecs;
414 findMatchingCodecs(
415 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
416
417 if (matchingCodecs.isEmpty()) {
418 return NULL;
419 }
420
421 sp<OMXCodecObserver> observer = new OMXCodecObserver;
422 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700423
424 const char *componentName;
425 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
426 componentName = matchingCodecs[i].string();
427
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800428#if BUILD_WITH_FULL_STAGEFRIGHT
James Dong17299ab2010-05-14 15:45:22 -0700429 sp<MediaSource> softwareCodec = createEncoder?
430 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800431 InstantiateSoftwareCodec(componentName, source);
432
433 if (softwareCodec != NULL) {
434 LOGV("Successfully allocated software codec '%s'", componentName);
435
436 return softwareCodec;
437 }
438#endif
439
Andreas Hubere13526a2009-10-22 10:43:34 -0700440 LOGV("Attempting to allocate OMX node '%s'", componentName);
441
442 status_t err = omx->allocateNode(componentName, observer, &node);
443 if (err == OK) {
444 LOGV("Successfully allocated OMX node '%s'", componentName);
445
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700446 sp<OMXCodec> codec = new OMXCodec(
447 omx, node, getComponentQuirks(componentName),
448 createEncoder, mime, componentName,
449 source);
450
451 observer->setCodec(codec);
452
453 err = codec->configureCodec(meta);
454
455 if (err == OK) {
456 return codec;
457 }
458
459 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700460 }
461 }
462
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700463 return NULL;
464}
Andreas Hubere13526a2009-10-22 10:43:34 -0700465
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700466status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700467 uint32_t type;
468 const void *data;
469 size_t size;
470 if (meta->findData(kKeyESDS, &type, &data, &size)) {
471 ESDS esds((const char *)data, size);
472 CHECK_EQ(esds.InitCheck(), OK);
473
474 const void *codec_specific_data;
475 size_t codec_specific_data_size;
476 esds.getCodecSpecificInfo(
477 &codec_specific_data, &codec_specific_data_size);
478
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700479 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700480 codec_specific_data, codec_specific_data_size);
481 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700482 // Parse the AVCDecoderConfigurationRecord
483
484 const uint8_t *ptr = (const uint8_t *)data;
485
486 CHECK(size >= 7);
487 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
488 uint8_t profile = ptr[1];
489 uint8_t level = ptr[3];
490
Andreas Huber44e15c42009-11-23 14:39:38 -0800491 // There is decodable content out there that fails the following
492 // assertion, let's be lenient for now...
493 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700494
495 size_t lengthSize = 1 + (ptr[4] & 3);
496
497 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
498 // violates it...
499 // CHECK((ptr[5] >> 5) == 7); // reserved
500
501 size_t numSeqParameterSets = ptr[5] & 31;
502
503 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700504 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700505
506 for (size_t i = 0; i < numSeqParameterSets; ++i) {
507 CHECK(size >= 2);
508 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700509
510 ptr += 2;
511 size -= 2;
512
Andreas Huberbe06d262009-08-14 14:37:10 -0700513 CHECK(size >= length);
514
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700515 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700516
517 ptr += length;
518 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700519 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700520
Andreas Huberebf66ea2009-08-19 13:32:58 -0700521 CHECK(size >= 1);
522 size_t numPictureParameterSets = *ptr;
523 ++ptr;
524 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700525
Andreas Huberebf66ea2009-08-19 13:32:58 -0700526 for (size_t i = 0; i < numPictureParameterSets; ++i) {
527 CHECK(size >= 2);
528 size_t length = U16_AT(ptr);
529
530 ptr += 2;
531 size -= 2;
532
533 CHECK(size >= length);
534
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700535 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700536
537 ptr += length;
538 size -= length;
539 }
540
Andreas Huber53a76bd2009-10-06 16:20:44 -0700541 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700542 (int)profile, AVCProfileToString(profile), (int)level / 10);
543
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700544 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Huberebf66ea2009-08-19 13:32:58 -0700545 && (profile != kAVCProfileBaseline || level > 39)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700546 // This stream exceeds the decoder's capabilities. The decoder
547 // does not handle this gracefully and would clobber the heap
548 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700549
550 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700551 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700552 }
553 }
554
James Dong17299ab2010-05-14 15:45:22 -0700555 int32_t bitRate = 0;
556 if (mIsEncoder) {
557 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
558 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700559 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700560 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700561 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700562 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700563 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700564 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700565 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700566 int32_t numChannels, sampleRate;
567 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
568 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
569
James Dong17299ab2010-05-14 15:45:22 -0700570 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700571 }
James Dongabed93a2010-04-22 17:27:04 -0700572
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700573 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700574 int32_t width, height;
575 bool success = meta->findInt32(kKeyWidth, &width);
576 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700577 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700578
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700579 if (mIsEncoder) {
James Dong4f501f02010-06-07 14:41:41 -0700580 int32_t frameRate = 25; // XXX
581 int32_t bitRate = 3000000; // bit rate
582 //success = success && meta->findInt32(kKeySampleRate, &frameRate);
583 setVideoInputFormat(mMIME, width, height, frameRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700584 } else {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700585 status_t err = setVideoOutputFormat(
586 mMIME, width, height);
587
588 if (err != OK) {
589 return err;
590 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700591 }
592 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700593
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700594 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
595 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700596 OMX_COLOR_FORMATTYPE format =
597 OMX_COLOR_Format32bitARGB8888;
598 // OMX_COLOR_FormatYUV420PackedPlanar;
599 // OMX_COLOR_FormatCbYCrY;
600 // OMX_COLOR_FormatYUV411Planar;
601
602 int32_t width, height;
603 bool success = meta->findInt32(kKeyWidth, &width);
604 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700605
606 int32_t compressedSize;
607 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700608 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700609
610 CHECK(success);
611 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700612
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700613 setImageOutputFormat(format, width, height);
614 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700615 }
616
Andreas Huberda050cf22009-09-02 14:01:43 -0700617 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800618 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700619 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700620 }
621
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700622 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700623 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
624 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700625 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700626 }
627
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700628 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700629
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700630 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700631}
632
Andreas Huberda050cf22009-09-02 14:01:43 -0700633void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
634 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700635 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700636 def.nPortIndex = portIndex;
637
Andreas Huber784202e2009-10-15 13:46:54 -0700638 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700639 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
640 CHECK_EQ(err, OK);
641
Andreas Huberb8de9572010-02-22 14:58:45 -0800642 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
643 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700644 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700645 }
646
Andreas Huber784202e2009-10-15 13:46:54 -0700647 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700648 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
649 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800650
651 err = mOMX->getParameter(
652 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
653 CHECK_EQ(err, OK);
654
655 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800656 if (portIndex == kPortIndexInput
657 && (mQuirks & kInputBufferSizesAreBogus)) {
658 CHECK_EQ(def.nBufferSize, size);
659 } else {
660 CHECK(def.nBufferSize >= size);
661 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700662}
663
Andreas Huberbe06d262009-08-14 14:37:10 -0700664status_t OMXCodec::setVideoPortFormatType(
665 OMX_U32 portIndex,
666 OMX_VIDEO_CODINGTYPE compressionFormat,
667 OMX_COLOR_FORMATTYPE colorFormat) {
668 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700669 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700670 format.nPortIndex = portIndex;
671 format.nIndex = 0;
672 bool found = false;
673
674 OMX_U32 index = 0;
675 for (;;) {
676 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700677 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700678 mNode, OMX_IndexParamVideoPortFormat,
679 &format, sizeof(format));
680
681 if (err != OK) {
682 return err;
683 }
684
685 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700686 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700687
688#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700689 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700690 portIndex,
691 index, format.eCompressionFormat, format.eColorFormat);
692#endif
693
694 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
695 if (portIndex == kPortIndexInput
696 && colorFormat == format.eColorFormat) {
697 // eCompressionFormat does not seem right.
698 found = true;
699 break;
700 }
701 if (portIndex == kPortIndexOutput
702 && compressionFormat == format.eCompressionFormat) {
703 // eColorFormat does not seem right.
704 found = true;
705 break;
706 }
707 }
708
709 if (format.eCompressionFormat == compressionFormat
710 && format.eColorFormat == colorFormat) {
711 found = true;
712 break;
713 }
714
715 ++index;
716 }
717
718 if (!found) {
719 return UNKNOWN_ERROR;
720 }
721
Andreas Huber53a76bd2009-10-06 16:20:44 -0700722 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700723 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700724 mNode, OMX_IndexParamVideoPortFormat,
725 &format, sizeof(format));
726
727 return err;
728}
729
Andreas Huberb482ce82009-10-29 12:02:48 -0700730static size_t getFrameSize(
731 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
732 switch (colorFormat) {
733 case OMX_COLOR_FormatYCbYCr:
734 case OMX_COLOR_FormatCbYCrY:
735 return width * height * 2;
736
Andreas Huber71c27d92010-03-19 11:43:15 -0700737 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700738 case OMX_COLOR_FormatYUV420SemiPlanar:
739 return (width * height * 3) / 2;
740
741 default:
742 CHECK(!"Should not be here. Unsupported color format.");
743 break;
744 }
745}
746
Andreas Huberbe06d262009-08-14 14:37:10 -0700747void OMXCodec::setVideoInputFormat(
James Dong4f501f02010-06-07 14:41:41 -0700748 const char *mime, OMX_U32 width, OMX_U32 height,
749 OMX_U32 frameRate, OMX_U32 bitRate) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700750 CODEC_LOGV("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700751
752 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700753 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700754 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700755 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700756 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700757 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700758 compressionFormat = OMX_VIDEO_CodingH263;
759 } else {
760 LOGE("Not a supported video mime type: %s", mime);
761 CHECK(!"Should not be here. Not a supported video mime type.");
762 }
763
Andreas Huberea6a38c2009-11-16 15:43:38 -0800764 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
765 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
James Dongabed93a2010-04-22 17:27:04 -0700766 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700767 }
768
James Dongb00e2462010-04-26 17:48:26 -0700769
770
771 status_t err;
772 OMX_PARAM_PORTDEFINITIONTYPE def;
773 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
774
775 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700776 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700777 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700778 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700779
James Dongb00e2462010-04-26 17:48:26 -0700780 InitOMXParams(&def);
781 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700782
James Dongb00e2462010-04-26 17:48:26 -0700783 err = mOMX->getParameter(
784 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
785 CHECK_EQ(err, OK);
786
787 def.nBufferSize = getFrameSize(colorFormat, width, height);
788
789 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
790
791 video_def->nFrameWidth = width;
792 video_def->nFrameHeight = height;
James Dong4f501f02010-06-07 14:41:41 -0700793 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700794 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
795 video_def->eColorFormat = colorFormat;
796
James Dongb00e2462010-04-26 17:48:26 -0700797 err = mOMX->setParameter(
798 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
799 CHECK_EQ(err, OK);
800
801 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700802 CHECK_EQ(setVideoPortFormatType(
803 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
804 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700805 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700806 def.nPortIndex = kPortIndexOutput;
807
James Dongb00e2462010-04-26 17:48:26 -0700808 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700809 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
810
811 CHECK_EQ(err, OK);
812 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
813
814 video_def->nFrameWidth = width;
815 video_def->nFrameHeight = height;
James Dong4f501f02010-06-07 14:41:41 -0700816 video_def->xFramerate = (frameRate << 16); // Q16 format
817 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700818 video_def->eCompressionFormat = compressionFormat;
819 video_def->eColorFormat = OMX_COLOR_FormatUnused;
820
Andreas Huber784202e2009-10-15 13:46:54 -0700821 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700822 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
823 CHECK_EQ(err, OK);
824
James Dongb00e2462010-04-26 17:48:26 -0700825 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700826 switch (compressionFormat) {
827 case OMX_VIDEO_CodingMPEG4:
828 {
829 CHECK_EQ(setupMPEG4EncoderParameters(), OK);
830 break;
831 }
832
833 case OMX_VIDEO_CodingH263:
834 break;
835
Andreas Huberea6a38c2009-11-16 15:43:38 -0800836 case OMX_VIDEO_CodingAVC:
837 {
838 CHECK_EQ(setupAVCEncoderParameters(), OK);
839 break;
840 }
841
Andreas Huberb482ce82009-10-29 12:02:48 -0700842 default:
843 CHECK(!"Support for this compressionFormat to be implemented.");
844 break;
845 }
846}
847
848status_t OMXCodec::setupMPEG4EncoderParameters() {
849 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
850 InitOMXParams(&mpeg4type);
851 mpeg4type.nPortIndex = kPortIndexOutput;
852
853 status_t err = mOMX->getParameter(
854 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
855 CHECK_EQ(err, OK);
856
857 mpeg4type.nSliceHeaderSpacing = 0;
858 mpeg4type.bSVH = OMX_FALSE;
859 mpeg4type.bGov = OMX_FALSE;
860
861 mpeg4type.nAllowedPictureTypes =
862 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
863
864 mpeg4type.nPFrames = 23;
865 mpeg4type.nBFrames = 0;
866
867 mpeg4type.nIDCVLCThreshold = 0;
868 mpeg4type.bACPred = OMX_TRUE;
869 mpeg4type.nMaxPacketSize = 256;
870 mpeg4type.nTimeIncRes = 1000;
871 mpeg4type.nHeaderExtension = 0;
872 mpeg4type.bReversibleVLC = OMX_FALSE;
873
874 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileCore;
875 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2;
876
877 err = mOMX->setParameter(
878 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
879 CHECK_EQ(err, OK);
880
881 // ----------------
882
883 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
884 InitOMXParams(&bitrateType);
885 bitrateType.nPortIndex = kPortIndexOutput;
886
887 err = mOMX->getParameter(
888 mNode, OMX_IndexParamVideoBitrate,
889 &bitrateType, sizeof(bitrateType));
890 CHECK_EQ(err, OK);
891
892 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
893 bitrateType.nTargetBitrate = 1000000;
894
895 err = mOMX->setParameter(
896 mNode, OMX_IndexParamVideoBitrate,
897 &bitrateType, sizeof(bitrateType));
898 CHECK_EQ(err, OK);
899
900 // ----------------
901
902 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
903 InitOMXParams(&errorCorrectionType);
904 errorCorrectionType.nPortIndex = kPortIndexOutput;
905
906 err = mOMX->getParameter(
907 mNode, OMX_IndexParamVideoErrorCorrection,
908 &errorCorrectionType, sizeof(errorCorrectionType));
909 CHECK_EQ(err, OK);
910
911 errorCorrectionType.bEnableHEC = OMX_FALSE;
912 errorCorrectionType.bEnableResync = OMX_TRUE;
913 errorCorrectionType.nResynchMarkerSpacing = 256;
914 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
915 errorCorrectionType.bEnableRVLC = OMX_FALSE;
916
917 err = mOMX->setParameter(
918 mNode, OMX_IndexParamVideoErrorCorrection,
919 &errorCorrectionType, sizeof(errorCorrectionType));
920 CHECK_EQ(err, OK);
921
922 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700923}
924
Andreas Huberea6a38c2009-11-16 15:43:38 -0800925status_t OMXCodec::setupAVCEncoderParameters() {
926 OMX_VIDEO_PARAM_AVCTYPE h264type;
927 InitOMXParams(&h264type);
928 h264type.nPortIndex = kPortIndexOutput;
929
930 status_t err = mOMX->getParameter(
931 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
932 CHECK_EQ(err, OK);
933
934 h264type.nAllowedPictureTypes =
935 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
936
937 h264type.nSliceHeaderSpacing = 0;
938 h264type.nBFrames = 0;
James Dong4f501f02010-06-07 14:41:41 -0700939 h264type.nPFrames = 24; // XXX
Andreas Huberea6a38c2009-11-16 15:43:38 -0800940 h264type.bUseHadamard = OMX_TRUE;
941 h264type.nRefFrames = 1;
942 h264type.nRefIdx10ActiveMinus1 = 0;
943 h264type.nRefIdx11ActiveMinus1 = 0;
944 h264type.bEnableUEP = OMX_FALSE;
945 h264type.bEnableFMO = OMX_FALSE;
946 h264type.bEnableASO = OMX_FALSE;
947 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -0800948 h264type.bFrameMBsOnly = OMX_TRUE;
949 h264type.bMBAFF = OMX_FALSE;
950 h264type.bEntropyCodingCABAC = OMX_FALSE;
951 h264type.bWeightedPPrediction = OMX_FALSE;
952 h264type.bconstIpred = OMX_FALSE;
953 h264type.bDirect8x8Inference = OMX_FALSE;
954 h264type.bDirectSpatialTemporal = OMX_FALSE;
955 h264type.nCabacInitIdc = 0;
956 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
957
958 err = mOMX->setParameter(
959 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
960 CHECK_EQ(err, OK);
961
962 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
963 InitOMXParams(&bitrateType);
964 bitrateType.nPortIndex = kPortIndexOutput;
965
966 err = mOMX->getParameter(
967 mNode, OMX_IndexParamVideoBitrate,
968 &bitrateType, sizeof(bitrateType));
969 CHECK_EQ(err, OK);
970
971 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
James Dong4f501f02010-06-07 14:41:41 -0700972 bitrateType.nTargetBitrate = 3000000; // XXX
Andreas Huberea6a38c2009-11-16 15:43:38 -0800973
974 err = mOMX->setParameter(
975 mNode, OMX_IndexParamVideoBitrate,
976 &bitrateType, sizeof(bitrateType));
977 CHECK_EQ(err, OK);
978
979 return OK;
980}
981
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700982status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -0700983 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700984 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700985
Andreas Huberbe06d262009-08-14 14:37:10 -0700986 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700987 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700988 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700989 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700990 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700991 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700992 compressionFormat = OMX_VIDEO_CodingH263;
993 } else {
994 LOGE("Not a supported video mime type: %s", mime);
995 CHECK(!"Should not be here. Not a supported video mime type.");
996 }
997
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700998 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700999 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1000
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001001 if (err != OK) {
1002 return err;
1003 }
1004
Andreas Huberbe06d262009-08-14 14:37:10 -07001005#if 1
1006 {
1007 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001008 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001009 format.nPortIndex = kPortIndexOutput;
1010 format.nIndex = 0;
1011
Andreas Huber784202e2009-10-15 13:46:54 -07001012 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001013 mNode, OMX_IndexParamVideoPortFormat,
1014 &format, sizeof(format));
1015 CHECK_EQ(err, OK);
1016 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1017
1018 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1019
1020 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1021 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1022 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1023 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1024
Andreas Huber784202e2009-10-15 13:46:54 -07001025 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001026 mNode, OMX_IndexParamVideoPortFormat,
1027 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001028
1029 if (err != OK) {
1030 return err;
1031 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001032 }
1033#endif
1034
1035 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001036 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001037 def.nPortIndex = kPortIndexInput;
1038
Andreas Huber4c483422009-09-02 16:05:36 -07001039 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1040
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001041 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001042 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1043
1044 CHECK_EQ(err, OK);
1045
1046#if 1
1047 // XXX Need a (much) better heuristic to compute input buffer sizes.
1048 const size_t X = 64 * 1024;
1049 if (def.nBufferSize < X) {
1050 def.nBufferSize = X;
1051 }
1052#endif
1053
1054 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1055
1056 video_def->nFrameWidth = width;
1057 video_def->nFrameHeight = height;
1058
Andreas Huberb482ce82009-10-29 12:02:48 -07001059 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001060 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1061
Andreas Huber784202e2009-10-15 13:46:54 -07001062 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001063 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001064
1065 if (err != OK) {
1066 return err;
1067 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001068
1069 ////////////////////////////////////////////////////////////////////////////
1070
Andreas Huber4c483422009-09-02 16:05:36 -07001071 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001072 def.nPortIndex = kPortIndexOutput;
1073
Andreas Huber784202e2009-10-15 13:46:54 -07001074 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001075 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1076 CHECK_EQ(err, OK);
1077 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1078
1079#if 0
1080 def.nBufferSize =
1081 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1082#endif
1083
1084 video_def->nFrameWidth = width;
1085 video_def->nFrameHeight = height;
1086
Andreas Huber784202e2009-10-15 13:46:54 -07001087 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001088 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001089
1090 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001091}
1092
Andreas Huberbe06d262009-08-14 14:37:10 -07001093OMXCodec::OMXCodec(
1094 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001095 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001096 const char *mime,
1097 const char *componentName,
1098 const sp<MediaSource> &source)
1099 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001100 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001101 mNode(node),
1102 mQuirks(quirks),
1103 mIsEncoder(isEncoder),
1104 mMIME(strdup(mime)),
1105 mComponentName(strdup(componentName)),
1106 mSource(source),
1107 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001108 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001109 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001110 mSignalledEOS(false),
1111 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001112 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001113 mSeekTimeUs(-1),
1114 mLeftOverBuffer(NULL) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001115 mPortStatus[kPortIndexInput] = ENABLED;
1116 mPortStatus[kPortIndexOutput] = ENABLED;
1117
Andreas Huber4c483422009-09-02 16:05:36 -07001118 setComponentRole();
1119}
1120
Andreas Hubere6c40962009-09-10 14:13:30 -07001121// static
1122void OMXCodec::setComponentRole(
1123 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1124 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001125 struct MimeToRole {
1126 const char *mime;
1127 const char *decoderRole;
1128 const char *encoderRole;
1129 };
1130
1131 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001132 { MEDIA_MIMETYPE_AUDIO_MPEG,
1133 "audio_decoder.mp3", "audio_encoder.mp3" },
1134 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1135 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1136 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1137 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1138 { MEDIA_MIMETYPE_AUDIO_AAC,
1139 "audio_decoder.aac", "audio_encoder.aac" },
1140 { MEDIA_MIMETYPE_VIDEO_AVC,
1141 "video_decoder.avc", "video_encoder.avc" },
1142 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1143 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1144 { MEDIA_MIMETYPE_VIDEO_H263,
1145 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001146 };
1147
1148 static const size_t kNumMimeToRole =
1149 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1150
1151 size_t i;
1152 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001153 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001154 break;
1155 }
1156 }
1157
1158 if (i == kNumMimeToRole) {
1159 return;
1160 }
1161
1162 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001163 isEncoder ? kMimeToRole[i].encoderRole
1164 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001165
1166 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001167 OMX_PARAM_COMPONENTROLETYPE roleParams;
1168 InitOMXParams(&roleParams);
1169
1170 strncpy((char *)roleParams.cRole,
1171 role, OMX_MAX_STRINGNAME_SIZE - 1);
1172
1173 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1174
Andreas Huber784202e2009-10-15 13:46:54 -07001175 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001176 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001177 &roleParams, sizeof(roleParams));
1178
1179 if (err != OK) {
1180 LOGW("Failed to set standard component role '%s'.", role);
1181 }
1182 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001183}
1184
Andreas Hubere6c40962009-09-10 14:13:30 -07001185void OMXCodec::setComponentRole() {
1186 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1187}
1188
Andreas Huberbe06d262009-08-14 14:37:10 -07001189OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001190 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001191
Andreas Huber784202e2009-10-15 13:46:54 -07001192 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001193 CHECK_EQ(err, OK);
1194
1195 mNode = NULL;
1196 setState(DEAD);
1197
1198 clearCodecSpecificData();
1199
1200 free(mComponentName);
1201 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001202
Andreas Huberbe06d262009-08-14 14:37:10 -07001203 free(mMIME);
1204 mMIME = NULL;
1205}
1206
1207status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001208 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001209
1210 CHECK_EQ(mState, LOADED);
1211
1212 status_t err;
1213 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001214 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001215 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001216 setState(LOADED_TO_IDLE);
1217 }
1218
1219 err = allocateBuffers();
1220 CHECK_EQ(err, OK);
1221
1222 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001223 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001224 CHECK_EQ(err, OK);
1225
1226 setState(LOADED_TO_IDLE);
1227 }
1228
1229 while (mState != EXECUTING && mState != ERROR) {
1230 mAsyncCompletion.wait(mLock);
1231 }
1232
1233 return mState == ERROR ? UNKNOWN_ERROR : OK;
1234}
1235
1236// static
1237bool OMXCodec::isIntermediateState(State state) {
1238 return state == LOADED_TO_IDLE
1239 || state == IDLE_TO_EXECUTING
1240 || state == EXECUTING_TO_IDLE
1241 || state == IDLE_TO_LOADED
1242 || state == RECONFIGURING;
1243}
1244
1245status_t OMXCodec::allocateBuffers() {
1246 status_t err = allocateBuffersOnPort(kPortIndexInput);
1247
1248 if (err != OK) {
1249 return err;
1250 }
1251
1252 return allocateBuffersOnPort(kPortIndexOutput);
1253}
1254
1255status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1256 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001257 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001258 def.nPortIndex = portIndex;
1259
Andreas Huber784202e2009-10-15 13:46:54 -07001260 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001261 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1262
1263 if (err != OK) {
1264 return err;
1265 }
1266
Andreas Huber5c0a9132009-08-20 11:16:40 -07001267 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001268 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001269
Andreas Huberbe06d262009-08-14 14:37:10 -07001270 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001271 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001272 CHECK(mem.get() != NULL);
1273
Andreas Huberc712b9f2010-01-20 15:05:46 -08001274 BufferInfo info;
1275 info.mData = NULL;
1276 info.mSize = def.nBufferSize;
1277
Andreas Huberbe06d262009-08-14 14:37:10 -07001278 IOMX::buffer_id buffer;
1279 if (portIndex == kPortIndexInput
1280 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001281 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001282 mem.clear();
1283
Andreas Huberf1fe0642010-01-15 15:28:19 -08001284 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001285 mNode, portIndex, def.nBufferSize, &buffer,
1286 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001287 } else {
1288 err = mOMX->allocateBufferWithBackup(
1289 mNode, portIndex, mem, &buffer);
1290 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001291 } else if (portIndex == kPortIndexOutput
1292 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001293 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001294 mem.clear();
1295
Andreas Huberf1fe0642010-01-15 15:28:19 -08001296 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001297 mNode, portIndex, def.nBufferSize, &buffer,
1298 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001299 } else {
1300 err = mOMX->allocateBufferWithBackup(
1301 mNode, portIndex, mem, &buffer);
1302 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001303 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001304 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001305 }
1306
1307 if (err != OK) {
1308 LOGE("allocate_buffer_with_backup failed");
1309 return err;
1310 }
1311
Andreas Huberc712b9f2010-01-20 15:05:46 -08001312 if (mem != NULL) {
1313 info.mData = mem->pointer();
1314 }
1315
Andreas Huberbe06d262009-08-14 14:37:10 -07001316 info.mBuffer = buffer;
1317 info.mOwnedByComponent = false;
1318 info.mMem = mem;
1319 info.mMediaBuffer = NULL;
1320
1321 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001322 if (!(mOMXLivesLocally
1323 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1324 && (mQuirks & kDefersOutputBufferAllocation))) {
1325 // If the node does not fill in the buffer ptr at this time,
1326 // we will defer creating the MediaBuffer until receiving
1327 // the first FILL_BUFFER_DONE notification instead.
1328 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1329 info.mMediaBuffer->setObserver(this);
1330 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001331 }
1332
1333 mPortBuffers[portIndex].push(info);
1334
Andreas Huber4c483422009-09-02 16:05:36 -07001335 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001336 portIndex == kPortIndexInput ? "input" : "output");
1337 }
1338
Andreas Huber2ea14e22009-12-16 09:30:55 -08001339 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001340
1341 return OK;
1342}
1343
1344void OMXCodec::on_message(const omx_message &msg) {
1345 Mutex::Autolock autoLock(mLock);
1346
1347 switch (msg.type) {
1348 case omx_message::EVENT:
1349 {
1350 onEvent(
1351 msg.u.event_data.event, msg.u.event_data.data1,
1352 msg.u.event_data.data2);
1353
1354 break;
1355 }
1356
1357 case omx_message::EMPTY_BUFFER_DONE:
1358 {
1359 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1360
Andreas Huber4c483422009-09-02 16:05:36 -07001361 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001362
1363 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1364 size_t i = 0;
1365 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1366 ++i;
1367 }
1368
1369 CHECK(i < buffers->size());
1370 if (!(*buffers)[i].mOwnedByComponent) {
1371 LOGW("We already own input buffer %p, yet received "
1372 "an EMPTY_BUFFER_DONE.", buffer);
1373 }
1374
1375 buffers->editItemAt(i).mOwnedByComponent = false;
1376
1377 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001378 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001379
1380 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001381 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001382 CHECK_EQ(err, OK);
1383
1384 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001385 } else if (mState != ERROR
1386 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001387 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1388 drainInputBuffer(&buffers->editItemAt(i));
1389 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001390 break;
1391 }
1392
1393 case omx_message::FILL_BUFFER_DONE:
1394 {
1395 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1396 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1397
Andreas Huber2ea14e22009-12-16 09:30:55 -08001398 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001399 buffer,
1400 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001401 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001402 msg.u.extended_buffer_data.timestamp,
1403 msg.u.extended_buffer_data.timestamp / 1E6);
1404
1405 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1406 size_t i = 0;
1407 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1408 ++i;
1409 }
1410
1411 CHECK(i < buffers->size());
1412 BufferInfo *info = &buffers->editItemAt(i);
1413
1414 if (!info->mOwnedByComponent) {
1415 LOGW("We already own output buffer %p, yet received "
1416 "a FILL_BUFFER_DONE.", buffer);
1417 }
1418
1419 info->mOwnedByComponent = false;
1420
1421 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001422 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001423
1424 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001425 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001426 CHECK_EQ(err, OK);
1427
1428 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001429#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001430 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1431 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001432 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001433 mNoMoreOutputData = true;
1434 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001435#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001436 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1437 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001438
Andreas Huber52733b82010-01-25 10:41:35 -08001439 if (info->mMediaBuffer == NULL) {
1440 CHECK(mOMXLivesLocally);
1441 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1442 CHECK(mQuirks & kDefersOutputBufferAllocation);
1443
1444 // The qcom video decoders on Nexus don't actually allocate
1445 // output buffer memory on a call to OMX_AllocateBuffer
1446 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1447 // structure is only filled in later.
1448
1449 info->mMediaBuffer = new MediaBuffer(
1450 msg.u.extended_buffer_data.data_ptr,
1451 info->mSize);
1452 info->mMediaBuffer->setObserver(this);
1453 }
1454
Andreas Huberbe06d262009-08-14 14:37:10 -07001455 MediaBuffer *buffer = info->mMediaBuffer;
1456
1457 buffer->set_range(
1458 msg.u.extended_buffer_data.range_offset,
1459 msg.u.extended_buffer_data.range_length);
1460
1461 buffer->meta_data()->clear();
1462
Andreas Huberfa8de752009-10-08 10:07:49 -07001463 buffer->meta_data()->setInt64(
1464 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001465
1466 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1467 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1468 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001469 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1470 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1471 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001472
1473 buffer->meta_data()->setPointer(
1474 kKeyPlatformPrivate,
1475 msg.u.extended_buffer_data.platform_private);
1476
1477 buffer->meta_data()->setPointer(
1478 kKeyBufferID,
1479 msg.u.extended_buffer_data.buffer);
1480
1481 mFilledBuffers.push_back(i);
1482 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001483
1484 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1485 CODEC_LOGV("No more output data.");
1486 mNoMoreOutputData = true;
1487 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001488 }
1489
1490 break;
1491 }
1492
1493 default:
1494 {
1495 CHECK(!"should not be here.");
1496 break;
1497 }
1498 }
1499}
1500
1501void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1502 switch (event) {
1503 case OMX_EventCmdComplete:
1504 {
1505 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1506 break;
1507 }
1508
1509 case OMX_EventError:
1510 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001511 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001512
1513 setState(ERROR);
1514 break;
1515 }
1516
1517 case OMX_EventPortSettingsChanged:
1518 {
1519 onPortSettingsChanged(data1);
1520 break;
1521 }
1522
Andreas Huber2ea14e22009-12-16 09:30:55 -08001523#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001524 case OMX_EventBufferFlag:
1525 {
Andreas Huber4c483422009-09-02 16:05:36 -07001526 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001527
1528 if (data1 == kPortIndexOutput) {
1529 mNoMoreOutputData = true;
1530 }
1531 break;
1532 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001533#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001534
1535 default:
1536 {
Andreas Huber4c483422009-09-02 16:05:36 -07001537 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001538 break;
1539 }
1540 }
1541}
1542
Andreas Huberb1678602009-10-19 13:06:40 -07001543// Has the format changed in any way that the client would have to be aware of?
1544static bool formatHasNotablyChanged(
1545 const sp<MetaData> &from, const sp<MetaData> &to) {
1546 if (from.get() == NULL && to.get() == NULL) {
1547 return false;
1548 }
1549
Andreas Huberf68c1682009-10-21 14:01:30 -07001550 if ((from.get() == NULL && to.get() != NULL)
1551 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001552 return true;
1553 }
1554
1555 const char *mime_from, *mime_to;
1556 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1557 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1558
1559 if (strcasecmp(mime_from, mime_to)) {
1560 return true;
1561 }
1562
1563 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1564 int32_t colorFormat_from, colorFormat_to;
1565 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1566 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1567
1568 if (colorFormat_from != colorFormat_to) {
1569 return true;
1570 }
1571
1572 int32_t width_from, width_to;
1573 CHECK(from->findInt32(kKeyWidth, &width_from));
1574 CHECK(to->findInt32(kKeyWidth, &width_to));
1575
1576 if (width_from != width_to) {
1577 return true;
1578 }
1579
1580 int32_t height_from, height_to;
1581 CHECK(from->findInt32(kKeyHeight, &height_from));
1582 CHECK(to->findInt32(kKeyHeight, &height_to));
1583
1584 if (height_from != height_to) {
1585 return true;
1586 }
1587 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1588 int32_t numChannels_from, numChannels_to;
1589 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1590 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1591
1592 if (numChannels_from != numChannels_to) {
1593 return true;
1594 }
1595
1596 int32_t sampleRate_from, sampleRate_to;
1597 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1598 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1599
1600 if (sampleRate_from != sampleRate_to) {
1601 return true;
1602 }
1603 }
1604
1605 return false;
1606}
1607
Andreas Huberbe06d262009-08-14 14:37:10 -07001608void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1609 switch (cmd) {
1610 case OMX_CommandStateSet:
1611 {
1612 onStateChange((OMX_STATETYPE)data);
1613 break;
1614 }
1615
1616 case OMX_CommandPortDisable:
1617 {
1618 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001619 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001620
1621 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1622 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1623 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1624
1625 mPortStatus[portIndex] = DISABLED;
1626
1627 if (mState == RECONFIGURING) {
1628 CHECK_EQ(portIndex, kPortIndexOutput);
1629
Andreas Huberb1678602009-10-19 13:06:40 -07001630 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001631 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001632
1633 // Don't notify clients if the output port settings change
1634 // wasn't of importance to them, i.e. it may be that just the
1635 // number of buffers has changed and nothing else.
1636 mOutputPortSettingsHaveChanged =
1637 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001638
Andreas Huberbe06d262009-08-14 14:37:10 -07001639 enablePortAsync(portIndex);
1640
1641 status_t err = allocateBuffersOnPort(portIndex);
1642 CHECK_EQ(err, OK);
1643 }
1644 break;
1645 }
1646
1647 case OMX_CommandPortEnable:
1648 {
1649 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001650 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001651
1652 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1653 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1654
1655 mPortStatus[portIndex] = ENABLED;
1656
1657 if (mState == RECONFIGURING) {
1658 CHECK_EQ(portIndex, kPortIndexOutput);
1659
1660 setState(EXECUTING);
1661
1662 fillOutputBuffers();
1663 }
1664 break;
1665 }
1666
1667 case OMX_CommandFlush:
1668 {
1669 OMX_U32 portIndex = data;
1670
Andreas Huber4c483422009-09-02 16:05:36 -07001671 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001672
1673 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1674 mPortStatus[portIndex] = ENABLED;
1675
1676 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1677 mPortBuffers[portIndex].size());
1678
1679 if (mState == RECONFIGURING) {
1680 CHECK_EQ(portIndex, kPortIndexOutput);
1681
1682 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001683 } else if (mState == EXECUTING_TO_IDLE) {
1684 if (mPortStatus[kPortIndexInput] == ENABLED
1685 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001686 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001687 "transition from EXECUTING to IDLE.");
1688
1689 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1690 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1691
1692 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001693 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001694 CHECK_EQ(err, OK);
1695 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001696 } else {
1697 // We're flushing both ports in preparation for seeking.
1698
1699 if (mPortStatus[kPortIndexInput] == ENABLED
1700 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001701 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001702 " seek-time.");
1703
1704 drainInputBuffers();
1705 fillOutputBuffers();
1706 }
1707 }
1708
1709 break;
1710 }
1711
1712 default:
1713 {
Andreas Huber4c483422009-09-02 16:05:36 -07001714 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001715 break;
1716 }
1717 }
1718}
1719
1720void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001721 CODEC_LOGV("onStateChange %d", newState);
1722
Andreas Huberbe06d262009-08-14 14:37:10 -07001723 switch (newState) {
1724 case OMX_StateIdle:
1725 {
Andreas Huber4c483422009-09-02 16:05:36 -07001726 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001727 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001728 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001729 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1730
1731 CHECK_EQ(err, OK);
1732
1733 setState(IDLE_TO_EXECUTING);
1734 } else {
1735 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1736
1737 CHECK_EQ(
1738 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1739 mPortBuffers[kPortIndexInput].size());
1740
1741 CHECK_EQ(
1742 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1743 mPortBuffers[kPortIndexOutput].size());
1744
Andreas Huber784202e2009-10-15 13:46:54 -07001745 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001746 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1747
1748 CHECK_EQ(err, OK);
1749
1750 err = freeBuffersOnPort(kPortIndexInput);
1751 CHECK_EQ(err, OK);
1752
1753 err = freeBuffersOnPort(kPortIndexOutput);
1754 CHECK_EQ(err, OK);
1755
1756 mPortStatus[kPortIndexInput] = ENABLED;
1757 mPortStatus[kPortIndexOutput] = ENABLED;
1758
1759 setState(IDLE_TO_LOADED);
1760 }
1761 break;
1762 }
1763
1764 case OMX_StateExecuting:
1765 {
1766 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1767
Andreas Huber4c483422009-09-02 16:05:36 -07001768 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001769
1770 setState(EXECUTING);
1771
Andreas Huber42978e52009-08-27 10:08:39 -07001772 // Buffers will be submitted to the component in the first
1773 // call to OMXCodec::read as mInitialBufferSubmit is true at
1774 // this point. This ensures that this on_message call returns,
1775 // releases the lock and ::init can notice the state change and
1776 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001777 break;
1778 }
1779
1780 case OMX_StateLoaded:
1781 {
1782 CHECK_EQ(mState, IDLE_TO_LOADED);
1783
Andreas Huber4c483422009-09-02 16:05:36 -07001784 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001785
1786 setState(LOADED);
1787 break;
1788 }
1789
Andreas Huberc712b9f2010-01-20 15:05:46 -08001790 case OMX_StateInvalid:
1791 {
1792 setState(ERROR);
1793 break;
1794 }
1795
Andreas Huberbe06d262009-08-14 14:37:10 -07001796 default:
1797 {
1798 CHECK(!"should not be here.");
1799 break;
1800 }
1801 }
1802}
1803
1804// static
1805size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1806 size_t n = 0;
1807 for (size_t i = 0; i < buffers.size(); ++i) {
1808 if (!buffers[i].mOwnedByComponent) {
1809 ++n;
1810 }
1811 }
1812
1813 return n;
1814}
1815
1816status_t OMXCodec::freeBuffersOnPort(
1817 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1818 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1819
1820 status_t stickyErr = OK;
1821
1822 for (size_t i = buffers->size(); i-- > 0;) {
1823 BufferInfo *info = &buffers->editItemAt(i);
1824
1825 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1826 continue;
1827 }
1828
1829 CHECK_EQ(info->mOwnedByComponent, false);
1830
Andreas Huber92022852009-09-14 15:24:14 -07001831 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1832
Andreas Huberbe06d262009-08-14 14:37:10 -07001833 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001834 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001835
1836 if (err != OK) {
1837 stickyErr = err;
1838 }
1839
1840 if (info->mMediaBuffer != NULL) {
1841 info->mMediaBuffer->setObserver(NULL);
1842
1843 // Make sure nobody but us owns this buffer at this point.
1844 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1845
1846 info->mMediaBuffer->release();
1847 }
1848
1849 buffers->removeAt(i);
1850 }
1851
1852 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1853
1854 return stickyErr;
1855}
1856
1857void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001858 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001859
1860 CHECK_EQ(mState, EXECUTING);
1861 CHECK_EQ(portIndex, kPortIndexOutput);
1862 setState(RECONFIGURING);
1863
1864 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001865 if (!flushPortAsync(portIndex)) {
1866 onCmdComplete(OMX_CommandFlush, portIndex);
1867 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001868 } else {
1869 disablePortAsync(portIndex);
1870 }
1871}
1872
Andreas Huber404cc412009-08-25 14:26:05 -07001873bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001874 CHECK(mState == EXECUTING || mState == RECONFIGURING
1875 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001876
Andreas Huber4c483422009-09-02 16:05:36 -07001877 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001878 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1879 mPortBuffers[portIndex].size());
1880
Andreas Huberbe06d262009-08-14 14:37:10 -07001881 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1882 mPortStatus[portIndex] = SHUTTING_DOWN;
1883
Andreas Huber404cc412009-08-25 14:26:05 -07001884 if ((mQuirks & kRequiresFlushCompleteEmulation)
1885 && countBuffersWeOwn(mPortBuffers[portIndex])
1886 == mPortBuffers[portIndex].size()) {
1887 // No flush is necessary and this component fails to send a
1888 // flush-complete event in this case.
1889
1890 return false;
1891 }
1892
Andreas Huberbe06d262009-08-14 14:37:10 -07001893 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001894 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001895 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001896
1897 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001898}
1899
1900void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1901 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1902
1903 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1904 mPortStatus[portIndex] = DISABLING;
1905
1906 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001907 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001908 CHECK_EQ(err, OK);
1909
1910 freeBuffersOnPort(portIndex, true);
1911}
1912
1913void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1914 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1915
1916 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1917 mPortStatus[portIndex] = ENABLING;
1918
1919 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001920 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001921 CHECK_EQ(err, OK);
1922}
1923
1924void OMXCodec::fillOutputBuffers() {
1925 CHECK_EQ(mState, EXECUTING);
1926
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001927 // This is a workaround for some decoders not properly reporting
1928 // end-of-output-stream. If we own all input buffers and also own
1929 // all output buffers and we already signalled end-of-input-stream,
1930 // the end-of-output-stream is implied.
1931 if (mSignalledEOS
1932 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
1933 == mPortBuffers[kPortIndexInput].size()
1934 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
1935 == mPortBuffers[kPortIndexOutput].size()) {
1936 mNoMoreOutputData = true;
1937 mBufferFilled.signal();
1938
1939 return;
1940 }
1941
Andreas Huberbe06d262009-08-14 14:37:10 -07001942 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1943 for (size_t i = 0; i < buffers->size(); ++i) {
1944 fillOutputBuffer(&buffers->editItemAt(i));
1945 }
1946}
1947
1948void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001949 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001950
1951 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1952 for (size_t i = 0; i < buffers->size(); ++i) {
1953 drainInputBuffer(&buffers->editItemAt(i));
1954 }
1955}
1956
1957void OMXCodec::drainInputBuffer(BufferInfo *info) {
1958 CHECK_EQ(info->mOwnedByComponent, false);
1959
1960 if (mSignalledEOS) {
1961 return;
1962 }
1963
1964 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1965 const CodecSpecificData *specific =
1966 mCodecSpecificData[mCodecSpecificDataIndex];
1967
1968 size_t size = specific->mSize;
1969
Andreas Hubere6c40962009-09-10 14:13:30 -07001970 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07001971 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001972 static const uint8_t kNALStartCode[4] =
1973 { 0x00, 0x00, 0x00, 0x01 };
1974
Andreas Huberc712b9f2010-01-20 15:05:46 -08001975 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07001976
1977 size += 4;
1978
Andreas Huberc712b9f2010-01-20 15:05:46 -08001979 memcpy(info->mData, kNALStartCode, 4);
1980 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07001981 specific->mData, specific->mSize);
1982 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001983 CHECK(info->mSize >= specific->mSize);
1984 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001985 }
1986
Andreas Huber2ea14e22009-12-16 09:30:55 -08001987 mNoMoreOutputData = false;
1988
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001989 CODEC_LOGV("calling emptyBuffer with codec specific data");
1990
Andreas Huber784202e2009-10-15 13:46:54 -07001991 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07001992 mNode, info->mBuffer, 0, size,
1993 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1994 0);
Andreas Huber3f427072009-10-08 11:02:27 -07001995 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001996
1997 info->mOwnedByComponent = true;
1998
1999 ++mCodecSpecificDataIndex;
2000 return;
2001 }
2002
Andreas Huberbe06d262009-08-14 14:37:10 -07002003 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002004
Andreas Hubera4357ad2010-04-02 12:49:54 -07002005 bool signalEOS = false;
2006 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002007
Andreas Hubera4357ad2010-04-02 12:49:54 -07002008 size_t offset = 0;
2009 int32_t n = 0;
2010 for (;;) {
2011 MediaBuffer *srcBuffer;
2012 if (mSeekTimeUs >= 0) {
2013 if (mLeftOverBuffer) {
2014 mLeftOverBuffer->release();
2015 mLeftOverBuffer = NULL;
2016 }
2017
2018 MediaSource::ReadOptions options;
2019 options.setSeekTo(mSeekTimeUs);
2020
2021 mSeekTimeUs = -1;
2022 mBufferFilled.signal();
2023
2024 err = mSource->read(&srcBuffer, &options);
2025 } else if (mLeftOverBuffer) {
2026 srcBuffer = mLeftOverBuffer;
2027 mLeftOverBuffer = NULL;
2028
2029 err = OK;
2030 } else {
2031 err = mSource->read(&srcBuffer);
2032 }
2033
2034 if (err != OK) {
2035 signalEOS = true;
2036 mFinalStatus = err;
2037 mSignalledEOS = true;
2038 break;
2039 }
2040
2041 size_t remainingBytes = info->mSize - offset;
2042
2043 if (srcBuffer->range_length() > remainingBytes) {
2044 if (offset == 0) {
2045 CODEC_LOGE(
2046 "Codec's input buffers are too small to accomodate "
2047 "buffer read from source (info->mSize = %d, srcLength = %d)",
2048 info->mSize, srcBuffer->range_length());
2049
2050 srcBuffer->release();
2051 srcBuffer = NULL;
2052
2053 setState(ERROR);
2054 return;
2055 }
2056
2057 mLeftOverBuffer = srcBuffer;
2058 break;
2059 }
2060
James Dong4f501f02010-06-07 14:41:41 -07002061 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2062 CHECK(mOMXLivesLocally && offset == 0);
2063 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2064 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2065 } else {
2066 memcpy((uint8_t *)info->mData + offset,
2067 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2068 srcBuffer->range_length());
2069 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002070
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002071 int64_t lastBufferTimeUs;
2072 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
2073 CHECK(timestampUs >= 0);
2074
Andreas Hubera4357ad2010-04-02 12:49:54 -07002075 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002076 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002077 }
2078
2079 offset += srcBuffer->range_length();
2080
2081 srcBuffer->release();
2082 srcBuffer = NULL;
2083
2084 ++n;
2085
2086 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2087 break;
2088 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002089
2090 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2091
2092 if (coalescedDurationUs > 250000ll) {
2093 // Don't coalesce more than 250ms worth of encoded data at once.
2094 break;
2095 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002096 }
2097
2098 if (n > 1) {
2099 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002100 }
2101
2102 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002103
Andreas Hubera4357ad2010-04-02 12:49:54 -07002104 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002105 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002106 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002107 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002108 }
2109
Andreas Hubera4357ad2010-04-02 12:49:54 -07002110 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2111 "timestamp %lld us (%.2f secs)",
2112 info->mBuffer, offset,
2113 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002114
Andreas Huber784202e2009-10-15 13:46:54 -07002115 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002116 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002117 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002118
2119 if (err != OK) {
2120 setState(ERROR);
2121 return;
2122 }
2123
2124 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002125
2126 // This component does not ever signal the EOS flag on output buffers,
2127 // Thanks for nothing.
2128 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2129 mNoMoreOutputData = true;
2130 mBufferFilled.signal();
2131 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002132}
2133
2134void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2135 CHECK_EQ(info->mOwnedByComponent, false);
2136
Andreas Huber404cc412009-08-25 14:26:05 -07002137 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002138 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002139 "calling fillOutputBuffer");
2140 return;
2141 }
2142
Andreas Huber4c483422009-09-02 16:05:36 -07002143 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002144 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002145
2146 if (err != OK) {
2147 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2148
2149 setState(ERROR);
2150 return;
2151 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002152
2153 info->mOwnedByComponent = true;
2154}
2155
2156void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2157 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2158 for (size_t i = 0; i < buffers->size(); ++i) {
2159 if ((*buffers)[i].mBuffer == buffer) {
2160 drainInputBuffer(&buffers->editItemAt(i));
2161 return;
2162 }
2163 }
2164
2165 CHECK(!"should not be here.");
2166}
2167
2168void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2169 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2170 for (size_t i = 0; i < buffers->size(); ++i) {
2171 if ((*buffers)[i].mBuffer == buffer) {
2172 fillOutputBuffer(&buffers->editItemAt(i));
2173 return;
2174 }
2175 }
2176
2177 CHECK(!"should not be here.");
2178}
2179
2180void OMXCodec::setState(State newState) {
2181 mState = newState;
2182 mAsyncCompletion.signal();
2183
2184 // This may cause some spurious wakeups but is necessary to
2185 // unblock the reader if we enter ERROR state.
2186 mBufferFilled.signal();
2187}
2188
Andreas Huberda050cf22009-09-02 14:01:43 -07002189void OMXCodec::setRawAudioFormat(
2190 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002191
2192 // port definition
2193 OMX_PARAM_PORTDEFINITIONTYPE def;
2194 InitOMXParams(&def);
2195 def.nPortIndex = portIndex;
2196 status_t err = mOMX->getParameter(
2197 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2198 CHECK_EQ(err, OK);
2199 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2200 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2201 &def, sizeof(def)), OK);
2202
2203 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002204 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002205 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002206 pcmParams.nPortIndex = portIndex;
2207
James Dongabed93a2010-04-22 17:27:04 -07002208 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002209 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2210
2211 CHECK_EQ(err, OK);
2212
2213 pcmParams.nChannels = numChannels;
2214 pcmParams.eNumData = OMX_NumericalDataSigned;
2215 pcmParams.bInterleaved = OMX_TRUE;
2216 pcmParams.nBitPerSample = 16;
2217 pcmParams.nSamplingRate = sampleRate;
2218 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2219
2220 if (numChannels == 1) {
2221 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2222 } else {
2223 CHECK_EQ(numChannels, 2);
2224
2225 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2226 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2227 }
2228
Andreas Huber784202e2009-10-15 13:46:54 -07002229 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002230 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2231
2232 CHECK_EQ(err, OK);
2233}
2234
James Dong17299ab2010-05-14 15:45:22 -07002235static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2236 if (isAMRWB) {
2237 if (bps <= 6600) {
2238 return OMX_AUDIO_AMRBandModeWB0;
2239 } else if (bps <= 8850) {
2240 return OMX_AUDIO_AMRBandModeWB1;
2241 } else if (bps <= 12650) {
2242 return OMX_AUDIO_AMRBandModeWB2;
2243 } else if (bps <= 14250) {
2244 return OMX_AUDIO_AMRBandModeWB3;
2245 } else if (bps <= 15850) {
2246 return OMX_AUDIO_AMRBandModeWB4;
2247 } else if (bps <= 18250) {
2248 return OMX_AUDIO_AMRBandModeWB5;
2249 } else if (bps <= 19850) {
2250 return OMX_AUDIO_AMRBandModeWB6;
2251 } else if (bps <= 23050) {
2252 return OMX_AUDIO_AMRBandModeWB7;
2253 }
2254
2255 // 23850 bps
2256 return OMX_AUDIO_AMRBandModeWB8;
2257 } else { // AMRNB
2258 if (bps <= 4750) {
2259 return OMX_AUDIO_AMRBandModeNB0;
2260 } else if (bps <= 5150) {
2261 return OMX_AUDIO_AMRBandModeNB1;
2262 } else if (bps <= 5900) {
2263 return OMX_AUDIO_AMRBandModeNB2;
2264 } else if (bps <= 6700) {
2265 return OMX_AUDIO_AMRBandModeNB3;
2266 } else if (bps <= 7400) {
2267 return OMX_AUDIO_AMRBandModeNB4;
2268 } else if (bps <= 7950) {
2269 return OMX_AUDIO_AMRBandModeNB5;
2270 } else if (bps <= 10200) {
2271 return OMX_AUDIO_AMRBandModeNB6;
2272 }
2273
2274 // 12200 bps
2275 return OMX_AUDIO_AMRBandModeNB7;
2276 }
2277}
2278
2279void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002280 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002281
Andreas Huber8768f2c2009-12-01 15:26:54 -08002282 OMX_AUDIO_PARAM_AMRTYPE def;
2283 InitOMXParams(&def);
2284 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002285
Andreas Huber8768f2c2009-12-01 15:26:54 -08002286 status_t err =
2287 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002288
Andreas Huber8768f2c2009-12-01 15:26:54 -08002289 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002290
Andreas Huber8768f2c2009-12-01 15:26:54 -08002291 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002292
James Dong17299ab2010-05-14 15:45:22 -07002293 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002294 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2295 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002296
2297 ////////////////////////
2298
2299 if (mIsEncoder) {
2300 sp<MetaData> format = mSource->getFormat();
2301 int32_t sampleRate;
2302 int32_t numChannels;
2303 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2304 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2305
2306 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2307 }
2308}
2309
James Dong17299ab2010-05-14 15:45:22 -07002310void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002311 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002312 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002313 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002314 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002315
2316 //////////////// output port ////////////////////
2317 // format
2318 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2319 format.nPortIndex = kPortIndexOutput;
2320 format.nIndex = 0;
2321 status_t err = OMX_ErrorNone;
2322 while (OMX_ErrorNone == err) {
2323 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2324 &format, sizeof(format)), OK);
2325 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2326 break;
2327 }
2328 format.nIndex++;
2329 }
2330 CHECK_EQ(OK, err);
2331 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2332 &format, sizeof(format)), OK);
2333
2334 // port definition
2335 OMX_PARAM_PORTDEFINITIONTYPE def;
2336 InitOMXParams(&def);
2337 def.nPortIndex = kPortIndexOutput;
2338 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2339 &def, sizeof(def)), OK);
2340 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2341 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2342 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2343 &def, sizeof(def)), OK);
2344
2345 // profile
2346 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2347 InitOMXParams(&profile);
2348 profile.nPortIndex = kPortIndexOutput;
2349 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2350 &profile, sizeof(profile)), OK);
2351 profile.nChannels = numChannels;
2352 profile.eChannelMode = (numChannels == 1?
2353 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2354 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002355 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002356 profile.nAudioBandWidth = 0;
2357 profile.nFrameLength = 0;
2358 profile.nAACtools = OMX_AUDIO_AACToolAll;
2359 profile.nAACERtools = OMX_AUDIO_AACERNone;
2360 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2361 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2362 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2363 &profile, sizeof(profile)), OK);
2364
Andreas Huberda050cf22009-09-02 14:01:43 -07002365 } else {
2366 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002367 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002368 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002369
Andreas Huber784202e2009-10-15 13:46:54 -07002370 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002371 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2372 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002373
Andreas Huberda050cf22009-09-02 14:01:43 -07002374 profile.nChannels = numChannels;
2375 profile.nSampleRate = sampleRate;
2376 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002377
Andreas Huber784202e2009-10-15 13:46:54 -07002378 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002379 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2380 CHECK_EQ(err, OK);
2381 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002382}
2383
2384void OMXCodec::setImageOutputFormat(
2385 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002386 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002387
2388#if 0
2389 OMX_INDEXTYPE index;
2390 status_t err = mOMX->get_extension_index(
2391 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2392 CHECK_EQ(err, OK);
2393
2394 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2395 CHECK_EQ(err, OK);
2396#endif
2397
2398 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002399 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002400 def.nPortIndex = kPortIndexOutput;
2401
Andreas Huber784202e2009-10-15 13:46:54 -07002402 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002403 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2404 CHECK_EQ(err, OK);
2405
2406 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2407
2408 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002409
Andreas Huberbe06d262009-08-14 14:37:10 -07002410 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2411 imageDef->eColorFormat = format;
2412 imageDef->nFrameWidth = width;
2413 imageDef->nFrameHeight = height;
2414
2415 switch (format) {
2416 case OMX_COLOR_FormatYUV420PackedPlanar:
2417 case OMX_COLOR_FormatYUV411Planar:
2418 {
2419 def.nBufferSize = (width * height * 3) / 2;
2420 break;
2421 }
2422
2423 case OMX_COLOR_FormatCbYCrY:
2424 {
2425 def.nBufferSize = width * height * 2;
2426 break;
2427 }
2428
2429 case OMX_COLOR_Format32bitARGB8888:
2430 {
2431 def.nBufferSize = width * height * 4;
2432 break;
2433 }
2434
Andreas Huber201511c2009-09-08 14:01:44 -07002435 case OMX_COLOR_Format16bitARGB4444:
2436 case OMX_COLOR_Format16bitARGB1555:
2437 case OMX_COLOR_Format16bitRGB565:
2438 case OMX_COLOR_Format16bitBGR565:
2439 {
2440 def.nBufferSize = width * height * 2;
2441 break;
2442 }
2443
Andreas Huberbe06d262009-08-14 14:37:10 -07002444 default:
2445 CHECK(!"Should not be here. Unknown color format.");
2446 break;
2447 }
2448
Andreas Huber5c0a9132009-08-20 11:16:40 -07002449 def.nBufferCountActual = def.nBufferCountMin;
2450
Andreas Huber784202e2009-10-15 13:46:54 -07002451 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002452 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2453 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002454}
Andreas Huberbe06d262009-08-14 14:37:10 -07002455
Andreas Huber5c0a9132009-08-20 11:16:40 -07002456void OMXCodec::setJPEGInputFormat(
2457 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2458 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002459 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002460 def.nPortIndex = kPortIndexInput;
2461
Andreas Huber784202e2009-10-15 13:46:54 -07002462 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002463 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2464 CHECK_EQ(err, OK);
2465
Andreas Huber5c0a9132009-08-20 11:16:40 -07002466 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2467 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2468
Andreas Huberbe06d262009-08-14 14:37:10 -07002469 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2470 imageDef->nFrameWidth = width;
2471 imageDef->nFrameHeight = height;
2472
Andreas Huber5c0a9132009-08-20 11:16:40 -07002473 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002474 def.nBufferCountActual = def.nBufferCountMin;
2475
Andreas Huber784202e2009-10-15 13:46:54 -07002476 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002477 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2478 CHECK_EQ(err, OK);
2479}
2480
2481void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2482 CodecSpecificData *specific =
2483 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2484
2485 specific->mSize = size;
2486 memcpy(specific->mData, data, size);
2487
2488 mCodecSpecificData.push(specific);
2489}
2490
2491void OMXCodec::clearCodecSpecificData() {
2492 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2493 free(mCodecSpecificData.editItemAt(i));
2494 }
2495 mCodecSpecificData.clear();
2496 mCodecSpecificDataIndex = 0;
2497}
2498
2499status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07002500 Mutex::Autolock autoLock(mLock);
2501
Andreas Huberbe06d262009-08-14 14:37:10 -07002502 if (mState != LOADED) {
2503 return UNKNOWN_ERROR;
2504 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002505
Andreas Huberbe06d262009-08-14 14:37:10 -07002506 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002507 if (mQuirks & kWantsNALFragments) {
2508 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002509 }
2510 status_t err = mSource->start(params.get());
2511
2512 if (err != OK) {
2513 return err;
2514 }
2515
2516 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002517 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002518 mSignalledEOS = false;
2519 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002520 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002521 mSeekTimeUs = -1;
2522 mFilledBuffers.clear();
2523
2524 return init();
2525}
2526
2527status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002528 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002529
2530 Mutex::Autolock autoLock(mLock);
2531
2532 while (isIntermediateState(mState)) {
2533 mAsyncCompletion.wait(mLock);
2534 }
2535
2536 switch (mState) {
2537 case LOADED:
2538 case ERROR:
2539 break;
2540
2541 case EXECUTING:
2542 {
2543 setState(EXECUTING_TO_IDLE);
2544
Andreas Huber127fcdc2009-08-26 16:27:02 -07002545 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002546 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002547 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002548
Andreas Huber127fcdc2009-08-26 16:27:02 -07002549 bool emulateInputFlushCompletion =
2550 !flushPortAsync(kPortIndexInput);
2551
2552 bool emulateOutputFlushCompletion =
2553 !flushPortAsync(kPortIndexOutput);
2554
2555 if (emulateInputFlushCompletion) {
2556 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2557 }
2558
2559 if (emulateOutputFlushCompletion) {
2560 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2561 }
2562 } else {
2563 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2564 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2565
2566 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002567 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002568 CHECK_EQ(err, OK);
2569 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002570
2571 while (mState != LOADED && mState != ERROR) {
2572 mAsyncCompletion.wait(mLock);
2573 }
2574
2575 break;
2576 }
2577
2578 default:
2579 {
2580 CHECK(!"should not be here.");
2581 break;
2582 }
2583 }
2584
Andreas Hubera4357ad2010-04-02 12:49:54 -07002585 if (mLeftOverBuffer) {
2586 mLeftOverBuffer->release();
2587 mLeftOverBuffer = NULL;
2588 }
2589
Andreas Huberbe06d262009-08-14 14:37:10 -07002590 mSource->stop();
2591
Andreas Huber4a9375e2010-02-09 11:54:33 -08002592 CODEC_LOGV("stopped");
2593
Andreas Huberbe06d262009-08-14 14:37:10 -07002594 return OK;
2595}
2596
2597sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002598 Mutex::Autolock autoLock(mLock);
2599
Andreas Huberbe06d262009-08-14 14:37:10 -07002600 return mOutputFormat;
2601}
2602
2603status_t OMXCodec::read(
2604 MediaBuffer **buffer, const ReadOptions *options) {
2605 *buffer = NULL;
2606
2607 Mutex::Autolock autoLock(mLock);
2608
Andreas Huberd06e5b82009-08-28 13:18:14 -07002609 if (mState != EXECUTING && mState != RECONFIGURING) {
2610 return UNKNOWN_ERROR;
2611 }
2612
Andreas Hubere981c332009-10-22 13:49:30 -07002613 bool seeking = false;
2614 int64_t seekTimeUs;
2615 if (options && options->getSeekTo(&seekTimeUs)) {
2616 seeking = true;
2617 }
2618
Andreas Huber42978e52009-08-27 10:08:39 -07002619 if (mInitialBufferSubmit) {
2620 mInitialBufferSubmit = false;
2621
Andreas Hubere981c332009-10-22 13:49:30 -07002622 if (seeking) {
2623 CHECK(seekTimeUs >= 0);
2624 mSeekTimeUs = seekTimeUs;
2625
2626 // There's no reason to trigger the code below, there's
2627 // nothing to flush yet.
2628 seeking = false;
2629 }
2630
Andreas Huber42978e52009-08-27 10:08:39 -07002631 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002632
Andreas Huberd06e5b82009-08-28 13:18:14 -07002633 if (mState == EXECUTING) {
2634 // Otherwise mState == RECONFIGURING and this code will trigger
2635 // after the output port is reenabled.
2636 fillOutputBuffers();
2637 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002638 }
2639
Andreas Hubere981c332009-10-22 13:49:30 -07002640 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002641 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002642
2643 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002644
2645 CHECK(seekTimeUs >= 0);
2646 mSeekTimeUs = seekTimeUs;
2647
2648 mFilledBuffers.clear();
2649
2650 CHECK_EQ(mState, EXECUTING);
2651
Andreas Huber404cc412009-08-25 14:26:05 -07002652 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2653 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2654
2655 if (emulateInputFlushCompletion) {
2656 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2657 }
2658
2659 if (emulateOutputFlushCompletion) {
2660 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2661 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002662
2663 while (mSeekTimeUs >= 0) {
2664 mBufferFilled.wait(mLock);
2665 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002666 }
2667
2668 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2669 mBufferFilled.wait(mLock);
2670 }
2671
2672 if (mState == ERROR) {
2673 return UNKNOWN_ERROR;
2674 }
2675
2676 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002677 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002678 }
2679
Andreas Hubercfd55572009-10-09 14:11:28 -07002680 if (mOutputPortSettingsHaveChanged) {
2681 mOutputPortSettingsHaveChanged = false;
2682
2683 return INFO_FORMAT_CHANGED;
2684 }
2685
Andreas Huberbe06d262009-08-14 14:37:10 -07002686 size_t index = *mFilledBuffers.begin();
2687 mFilledBuffers.erase(mFilledBuffers.begin());
2688
2689 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2690 info->mMediaBuffer->add_ref();
2691 *buffer = info->mMediaBuffer;
2692
2693 return OK;
2694}
2695
2696void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2697 Mutex::Autolock autoLock(mLock);
2698
2699 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2700 for (size_t i = 0; i < buffers->size(); ++i) {
2701 BufferInfo *info = &buffers->editItemAt(i);
2702
2703 if (info->mMediaBuffer == buffer) {
2704 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2705 fillOutputBuffer(info);
2706 return;
2707 }
2708 }
2709
2710 CHECK(!"should not be here.");
2711}
2712
2713static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2714 static const char *kNames[] = {
2715 "OMX_IMAGE_CodingUnused",
2716 "OMX_IMAGE_CodingAutoDetect",
2717 "OMX_IMAGE_CodingJPEG",
2718 "OMX_IMAGE_CodingJPEG2K",
2719 "OMX_IMAGE_CodingEXIF",
2720 "OMX_IMAGE_CodingTIFF",
2721 "OMX_IMAGE_CodingGIF",
2722 "OMX_IMAGE_CodingPNG",
2723 "OMX_IMAGE_CodingLZW",
2724 "OMX_IMAGE_CodingBMP",
2725 };
2726
2727 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2728
2729 if (type < 0 || (size_t)type >= numNames) {
2730 return "UNKNOWN";
2731 } else {
2732 return kNames[type];
2733 }
2734}
2735
2736static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2737 static const char *kNames[] = {
2738 "OMX_COLOR_FormatUnused",
2739 "OMX_COLOR_FormatMonochrome",
2740 "OMX_COLOR_Format8bitRGB332",
2741 "OMX_COLOR_Format12bitRGB444",
2742 "OMX_COLOR_Format16bitARGB4444",
2743 "OMX_COLOR_Format16bitARGB1555",
2744 "OMX_COLOR_Format16bitRGB565",
2745 "OMX_COLOR_Format16bitBGR565",
2746 "OMX_COLOR_Format18bitRGB666",
2747 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002748 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002749 "OMX_COLOR_Format24bitRGB888",
2750 "OMX_COLOR_Format24bitBGR888",
2751 "OMX_COLOR_Format24bitARGB1887",
2752 "OMX_COLOR_Format25bitARGB1888",
2753 "OMX_COLOR_Format32bitBGRA8888",
2754 "OMX_COLOR_Format32bitARGB8888",
2755 "OMX_COLOR_FormatYUV411Planar",
2756 "OMX_COLOR_FormatYUV411PackedPlanar",
2757 "OMX_COLOR_FormatYUV420Planar",
2758 "OMX_COLOR_FormatYUV420PackedPlanar",
2759 "OMX_COLOR_FormatYUV420SemiPlanar",
2760 "OMX_COLOR_FormatYUV422Planar",
2761 "OMX_COLOR_FormatYUV422PackedPlanar",
2762 "OMX_COLOR_FormatYUV422SemiPlanar",
2763 "OMX_COLOR_FormatYCbYCr",
2764 "OMX_COLOR_FormatYCrYCb",
2765 "OMX_COLOR_FormatCbYCrY",
2766 "OMX_COLOR_FormatCrYCbY",
2767 "OMX_COLOR_FormatYUV444Interleaved",
2768 "OMX_COLOR_FormatRawBayer8bit",
2769 "OMX_COLOR_FormatRawBayer10bit",
2770 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002771 "OMX_COLOR_FormatL2",
2772 "OMX_COLOR_FormatL4",
2773 "OMX_COLOR_FormatL8",
2774 "OMX_COLOR_FormatL16",
2775 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002776 "OMX_COLOR_FormatL32",
2777 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2778 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2779 "OMX_COLOR_Format18BitBGR666",
2780 "OMX_COLOR_Format24BitARGB6666",
2781 "OMX_COLOR_Format24BitABGR6666",
2782 };
2783
2784 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2785
Andreas Huberbe06d262009-08-14 14:37:10 -07002786 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2787 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2788 } else if (type < 0 || (size_t)type >= numNames) {
2789 return "UNKNOWN";
2790 } else {
2791 return kNames[type];
2792 }
2793}
2794
2795static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2796 static const char *kNames[] = {
2797 "OMX_VIDEO_CodingUnused",
2798 "OMX_VIDEO_CodingAutoDetect",
2799 "OMX_VIDEO_CodingMPEG2",
2800 "OMX_VIDEO_CodingH263",
2801 "OMX_VIDEO_CodingMPEG4",
2802 "OMX_VIDEO_CodingWMV",
2803 "OMX_VIDEO_CodingRV",
2804 "OMX_VIDEO_CodingAVC",
2805 "OMX_VIDEO_CodingMJPEG",
2806 };
2807
2808 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2809
2810 if (type < 0 || (size_t)type >= numNames) {
2811 return "UNKNOWN";
2812 } else {
2813 return kNames[type];
2814 }
2815}
2816
2817static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2818 static const char *kNames[] = {
2819 "OMX_AUDIO_CodingUnused",
2820 "OMX_AUDIO_CodingAutoDetect",
2821 "OMX_AUDIO_CodingPCM",
2822 "OMX_AUDIO_CodingADPCM",
2823 "OMX_AUDIO_CodingAMR",
2824 "OMX_AUDIO_CodingGSMFR",
2825 "OMX_AUDIO_CodingGSMEFR",
2826 "OMX_AUDIO_CodingGSMHR",
2827 "OMX_AUDIO_CodingPDCFR",
2828 "OMX_AUDIO_CodingPDCEFR",
2829 "OMX_AUDIO_CodingPDCHR",
2830 "OMX_AUDIO_CodingTDMAFR",
2831 "OMX_AUDIO_CodingTDMAEFR",
2832 "OMX_AUDIO_CodingQCELP8",
2833 "OMX_AUDIO_CodingQCELP13",
2834 "OMX_AUDIO_CodingEVRC",
2835 "OMX_AUDIO_CodingSMV",
2836 "OMX_AUDIO_CodingG711",
2837 "OMX_AUDIO_CodingG723",
2838 "OMX_AUDIO_CodingG726",
2839 "OMX_AUDIO_CodingG729",
2840 "OMX_AUDIO_CodingAAC",
2841 "OMX_AUDIO_CodingMP3",
2842 "OMX_AUDIO_CodingSBC",
2843 "OMX_AUDIO_CodingVORBIS",
2844 "OMX_AUDIO_CodingWMA",
2845 "OMX_AUDIO_CodingRA",
2846 "OMX_AUDIO_CodingMIDI",
2847 };
2848
2849 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2850
2851 if (type < 0 || (size_t)type >= numNames) {
2852 return "UNKNOWN";
2853 } else {
2854 return kNames[type];
2855 }
2856}
2857
2858static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2859 static const char *kNames[] = {
2860 "OMX_AUDIO_PCMModeLinear",
2861 "OMX_AUDIO_PCMModeALaw",
2862 "OMX_AUDIO_PCMModeMULaw",
2863 };
2864
2865 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2866
2867 if (type < 0 || (size_t)type >= numNames) {
2868 return "UNKNOWN";
2869 } else {
2870 return kNames[type];
2871 }
2872}
2873
Andreas Huber7ae02c82009-09-09 16:29:47 -07002874static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2875 static const char *kNames[] = {
2876 "OMX_AUDIO_AMRBandModeUnused",
2877 "OMX_AUDIO_AMRBandModeNB0",
2878 "OMX_AUDIO_AMRBandModeNB1",
2879 "OMX_AUDIO_AMRBandModeNB2",
2880 "OMX_AUDIO_AMRBandModeNB3",
2881 "OMX_AUDIO_AMRBandModeNB4",
2882 "OMX_AUDIO_AMRBandModeNB5",
2883 "OMX_AUDIO_AMRBandModeNB6",
2884 "OMX_AUDIO_AMRBandModeNB7",
2885 "OMX_AUDIO_AMRBandModeWB0",
2886 "OMX_AUDIO_AMRBandModeWB1",
2887 "OMX_AUDIO_AMRBandModeWB2",
2888 "OMX_AUDIO_AMRBandModeWB3",
2889 "OMX_AUDIO_AMRBandModeWB4",
2890 "OMX_AUDIO_AMRBandModeWB5",
2891 "OMX_AUDIO_AMRBandModeWB6",
2892 "OMX_AUDIO_AMRBandModeWB7",
2893 "OMX_AUDIO_AMRBandModeWB8",
2894 };
2895
2896 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2897
2898 if (type < 0 || (size_t)type >= numNames) {
2899 return "UNKNOWN";
2900 } else {
2901 return kNames[type];
2902 }
2903}
2904
2905static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2906 static const char *kNames[] = {
2907 "OMX_AUDIO_AMRFrameFormatConformance",
2908 "OMX_AUDIO_AMRFrameFormatIF1",
2909 "OMX_AUDIO_AMRFrameFormatIF2",
2910 "OMX_AUDIO_AMRFrameFormatFSF",
2911 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2912 "OMX_AUDIO_AMRFrameFormatITU",
2913 };
2914
2915 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2916
2917 if (type < 0 || (size_t)type >= numNames) {
2918 return "UNKNOWN";
2919 } else {
2920 return kNames[type];
2921 }
2922}
Andreas Huberbe06d262009-08-14 14:37:10 -07002923
2924void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2925 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002926 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002927 def.nPortIndex = portIndex;
2928
Andreas Huber784202e2009-10-15 13:46:54 -07002929 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002930 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2931 CHECK_EQ(err, OK);
2932
2933 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2934
2935 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2936 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2937
2938 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2939 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2940 printf(" nBufferSize = %ld\n", def.nBufferSize);
2941
2942 switch (def.eDomain) {
2943 case OMX_PortDomainImage:
2944 {
2945 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2946
2947 printf("\n");
2948 printf(" // Image\n");
2949 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2950 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2951 printf(" nStride = %ld\n", imageDef->nStride);
2952
2953 printf(" eCompressionFormat = %s\n",
2954 imageCompressionFormatString(imageDef->eCompressionFormat));
2955
2956 printf(" eColorFormat = %s\n",
2957 colorFormatString(imageDef->eColorFormat));
2958
2959 break;
2960 }
2961
2962 case OMX_PortDomainVideo:
2963 {
2964 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2965
2966 printf("\n");
2967 printf(" // Video\n");
2968 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2969 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2970 printf(" nStride = %ld\n", videoDef->nStride);
2971
2972 printf(" eCompressionFormat = %s\n",
2973 videoCompressionFormatString(videoDef->eCompressionFormat));
2974
2975 printf(" eColorFormat = %s\n",
2976 colorFormatString(videoDef->eColorFormat));
2977
2978 break;
2979 }
2980
2981 case OMX_PortDomainAudio:
2982 {
2983 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2984
2985 printf("\n");
2986 printf(" // Audio\n");
2987 printf(" eEncoding = %s\n",
2988 audioCodingTypeString(audioDef->eEncoding));
2989
2990 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2991 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002992 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002993 params.nPortIndex = portIndex;
2994
Andreas Huber784202e2009-10-15 13:46:54 -07002995 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002996 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2997 CHECK_EQ(err, OK);
2998
2999 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3000 printf(" nChannels = %ld\n", params.nChannels);
3001 printf(" bInterleaved = %d\n", params.bInterleaved);
3002 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3003
3004 printf(" eNumData = %s\n",
3005 params.eNumData == OMX_NumericalDataSigned
3006 ? "signed" : "unsigned");
3007
3008 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003009 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3010 OMX_AUDIO_PARAM_AMRTYPE amr;
3011 InitOMXParams(&amr);
3012 amr.nPortIndex = portIndex;
3013
Andreas Huber784202e2009-10-15 13:46:54 -07003014 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003015 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3016 CHECK_EQ(err, OK);
3017
3018 printf(" nChannels = %ld\n", amr.nChannels);
3019 printf(" eAMRBandMode = %s\n",
3020 amrBandModeString(amr.eAMRBandMode));
3021 printf(" eAMRFrameFormat = %s\n",
3022 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003023 }
3024
3025 break;
3026 }
3027
3028 default:
3029 {
3030 printf(" // Unknown\n");
3031 break;
3032 }
3033 }
3034
3035 printf("}\n");
3036}
3037
3038void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3039 mOutputFormat = new MetaData;
3040 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
3041
3042 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003043 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003044 def.nPortIndex = kPortIndexOutput;
3045
Andreas Huber784202e2009-10-15 13:46:54 -07003046 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003047 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3048 CHECK_EQ(err, OK);
3049
3050 switch (def.eDomain) {
3051 case OMX_PortDomainImage:
3052 {
3053 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3054 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3055
Andreas Hubere6c40962009-09-10 14:13:30 -07003056 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003057 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3058 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3059 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3060 break;
3061 }
3062
3063 case OMX_PortDomainAudio:
3064 {
3065 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3066
Andreas Huberda050cf22009-09-02 14:01:43 -07003067 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3068 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003069 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003070 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003071
Andreas Huber784202e2009-10-15 13:46:54 -07003072 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003073 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3074 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003075
Andreas Huberda050cf22009-09-02 14:01:43 -07003076 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3077 CHECK_EQ(params.nBitPerSample, 16);
3078 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003079
Andreas Huberda050cf22009-09-02 14:01:43 -07003080 int32_t numChannels, sampleRate;
3081 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3082 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003083
Andreas Huberda050cf22009-09-02 14:01:43 -07003084 if ((OMX_U32)numChannels != params.nChannels) {
3085 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003086 "the input stream contains (contains %d channels, "
3087 "codec outputs %ld channels).",
3088 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003089 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003090
Andreas Hubere6c40962009-09-10 14:13:30 -07003091 mOutputFormat->setCString(
3092 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003093
3094 // Use the codec-advertised number of channels, as some
3095 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003096 // mono. If we know the codec lies about this information,
3097 // use the actual number of channels instead.
3098 mOutputFormat->setInt32(
3099 kKeyChannelCount,
3100 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3101 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003102
3103 // The codec-reported sampleRate is not reliable...
3104 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3105 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003106 OMX_AUDIO_PARAM_AMRTYPE amr;
3107 InitOMXParams(&amr);
3108 amr.nPortIndex = kPortIndexOutput;
3109
Andreas Huber784202e2009-10-15 13:46:54 -07003110 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003111 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3112 CHECK_EQ(err, OK);
3113
3114 CHECK_EQ(amr.nChannels, 1);
3115 mOutputFormat->setInt32(kKeyChannelCount, 1);
3116
3117 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3118 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003119 mOutputFormat->setCString(
3120 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003121 mOutputFormat->setInt32(kKeySampleRate, 8000);
3122 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3123 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003124 mOutputFormat->setCString(
3125 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003126 mOutputFormat->setInt32(kKeySampleRate, 16000);
3127 } else {
3128 CHECK(!"Unknown AMR band mode.");
3129 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003130 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003131 mOutputFormat->setCString(
3132 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003133 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003134 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3135 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003136 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003137 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3138 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003139 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003140 } else {
3141 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003142 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003143 break;
3144 }
3145
3146 case OMX_PortDomainVideo:
3147 {
3148 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3149
3150 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003151 mOutputFormat->setCString(
3152 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003153 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003154 mOutputFormat->setCString(
3155 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003156 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003157 mOutputFormat->setCString(
3158 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003159 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003160 mOutputFormat->setCString(
3161 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003162 } else {
3163 CHECK(!"Unknown compression format.");
3164 }
3165
3166 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3167 // This component appears to be lying to me.
3168 mOutputFormat->setInt32(
3169 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3170 mOutputFormat->setInt32(
3171 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3172 } else {
3173 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3174 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3175 }
3176
3177 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3178 break;
3179 }
3180
3181 default:
3182 {
3183 CHECK(!"should not be here, neither audio nor video.");
3184 break;
3185 }
3186 }
3187}
3188
Andreas Hubere6c40962009-09-10 14:13:30 -07003189////////////////////////////////////////////////////////////////////////////////
3190
3191status_t QueryCodecs(
3192 const sp<IOMX> &omx,
3193 const char *mime, bool queryDecoders,
3194 Vector<CodecCapabilities> *results) {
3195 results->clear();
3196
3197 for (int index = 0;; ++index) {
3198 const char *componentName;
3199
3200 if (!queryDecoders) {
3201 componentName = GetCodec(
3202 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3203 mime, index);
3204 } else {
3205 componentName = GetCodec(
3206 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3207 mime, index);
3208 }
3209
3210 if (!componentName) {
3211 return OK;
3212 }
3213
Andreas Huber1a189a82010-03-24 13:49:20 -07003214 if (strncmp(componentName, "OMX.", 4)) {
3215 // Not an OpenMax component but a software codec.
3216
3217 results->push();
3218 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3219 caps->mComponentName = componentName;
3220
3221 continue;
3222 }
3223
Andreas Huber784202e2009-10-15 13:46:54 -07003224 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003225 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003226 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003227
3228 if (err != OK) {
3229 continue;
3230 }
3231
James Dong722d5912010-04-13 10:56:59 -07003232 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003233
3234 results->push();
3235 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3236 caps->mComponentName = componentName;
3237
3238 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3239 InitOMXParams(&param);
3240
3241 param.nPortIndex = queryDecoders ? 0 : 1;
3242
3243 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003244 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003245 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3246 &param, sizeof(param));
3247
3248 if (err != OK) {
3249 break;
3250 }
3251
3252 CodecProfileLevel profileLevel;
3253 profileLevel.mProfile = param.eProfile;
3254 profileLevel.mLevel = param.eLevel;
3255
3256 caps->mProfileLevels.push(profileLevel);
3257 }
3258
Andreas Huber784202e2009-10-15 13:46:54 -07003259 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003260 }
3261}
3262
Andreas Huberbe06d262009-08-14 14:37:10 -07003263} // namespace android