blob: 66011ca92d4dcac16c41ac81e8643f14ce84c72a [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;
350 }
351
Andreas Huberb8de9572010-02-22 14:58:45 -0800352 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
353 quirks |= kInputBufferSizesAreBogus;
354 }
355
Andreas Hubere13526a2009-10-22 10:43:34 -0700356 return quirks;
357}
358
359// static
360void OMXCodec::findMatchingCodecs(
361 const char *mime,
362 bool createEncoder, const char *matchComponentName,
363 uint32_t flags,
364 Vector<String8> *matchingCodecs) {
365 matchingCodecs->clear();
366
367 for (int index = 0;; ++index) {
368 const char *componentName;
369
370 if (createEncoder) {
371 componentName = GetCodec(
372 kEncoderInfo,
373 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
374 mime, index);
375 } else {
376 componentName = GetCodec(
377 kDecoderInfo,
378 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
379 mime, index);
380 }
381
382 if (!componentName) {
383 break;
384 }
385
386 // If a specific codec is requested, skip the non-matching ones.
387 if (matchComponentName && strcmp(componentName, matchComponentName)) {
388 continue;
389 }
390
391 matchingCodecs->push(String8(componentName));
392 }
393
394 if (flags & kPreferSoftwareCodecs) {
395 matchingCodecs->sort(CompareSoftwareCodecsFirst);
396 }
397}
398
399// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800400sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700401 const sp<IOMX> &omx,
402 const sp<MetaData> &meta, bool createEncoder,
403 const sp<MediaSource> &source,
404 const char *matchComponentName,
405 uint32_t flags) {
406 const char *mime;
407 bool success = meta->findCString(kKeyMIMEType, &mime);
408 CHECK(success);
409
410 Vector<String8> matchingCodecs;
411 findMatchingCodecs(
412 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
413
414 if (matchingCodecs.isEmpty()) {
415 return NULL;
416 }
417
418 sp<OMXCodecObserver> observer = new OMXCodecObserver;
419 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700420
421 const char *componentName;
422 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
423 componentName = matchingCodecs[i].string();
424
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800425#if BUILD_WITH_FULL_STAGEFRIGHT
James Dong17299ab2010-05-14 15:45:22 -0700426 sp<MediaSource> softwareCodec = createEncoder?
427 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800428 InstantiateSoftwareCodec(componentName, source);
429
430 if (softwareCodec != NULL) {
431 LOGV("Successfully allocated software codec '%s'", componentName);
432
433 return softwareCodec;
434 }
435#endif
436
Andreas Hubere13526a2009-10-22 10:43:34 -0700437 LOGV("Attempting to allocate OMX node '%s'", componentName);
438
439 status_t err = omx->allocateNode(componentName, observer, &node);
440 if (err == OK) {
441 LOGV("Successfully allocated OMX node '%s'", componentName);
442
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700443 sp<OMXCodec> codec = new OMXCodec(
444 omx, node, getComponentQuirks(componentName),
445 createEncoder, mime, componentName,
446 source);
447
448 observer->setCodec(codec);
449
450 err = codec->configureCodec(meta);
451
452 if (err == OK) {
453 return codec;
454 }
455
456 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700457 }
458 }
459
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700460 return NULL;
461}
Andreas Hubere13526a2009-10-22 10:43:34 -0700462
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700463status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700464 uint32_t type;
465 const void *data;
466 size_t size;
467 if (meta->findData(kKeyESDS, &type, &data, &size)) {
468 ESDS esds((const char *)data, size);
469 CHECK_EQ(esds.InitCheck(), OK);
470
471 const void *codec_specific_data;
472 size_t codec_specific_data_size;
473 esds.getCodecSpecificInfo(
474 &codec_specific_data, &codec_specific_data_size);
475
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700476 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700477 codec_specific_data, codec_specific_data_size);
478 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700479 // Parse the AVCDecoderConfigurationRecord
480
481 const uint8_t *ptr = (const uint8_t *)data;
482
483 CHECK(size >= 7);
484 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
485 uint8_t profile = ptr[1];
486 uint8_t level = ptr[3];
487
Andreas Huber44e15c42009-11-23 14:39:38 -0800488 // There is decodable content out there that fails the following
489 // assertion, let's be lenient for now...
490 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700491
492 size_t lengthSize = 1 + (ptr[4] & 3);
493
494 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
495 // violates it...
496 // CHECK((ptr[5] >> 5) == 7); // reserved
497
498 size_t numSeqParameterSets = ptr[5] & 31;
499
500 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700501 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700502
503 for (size_t i = 0; i < numSeqParameterSets; ++i) {
504 CHECK(size >= 2);
505 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700506
507 ptr += 2;
508 size -= 2;
509
Andreas Huberbe06d262009-08-14 14:37:10 -0700510 CHECK(size >= length);
511
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700512 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700513
514 ptr += length;
515 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700516 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700517
Andreas Huberebf66ea2009-08-19 13:32:58 -0700518 CHECK(size >= 1);
519 size_t numPictureParameterSets = *ptr;
520 ++ptr;
521 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700522
Andreas Huberebf66ea2009-08-19 13:32:58 -0700523 for (size_t i = 0; i < numPictureParameterSets; ++i) {
524 CHECK(size >= 2);
525 size_t length = U16_AT(ptr);
526
527 ptr += 2;
528 size -= 2;
529
530 CHECK(size >= length);
531
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700532 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700533
534 ptr += length;
535 size -= length;
536 }
537
Andreas Huber53a76bd2009-10-06 16:20:44 -0700538 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700539 (int)profile, AVCProfileToString(profile), (int)level / 10);
540
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700541 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Huberebf66ea2009-08-19 13:32:58 -0700542 && (profile != kAVCProfileBaseline || level > 39)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700543 // This stream exceeds the decoder's capabilities. The decoder
544 // does not handle this gracefully and would clobber the heap
545 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700546
547 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700548 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700549 }
550 }
551
James Dong17299ab2010-05-14 15:45:22 -0700552 int32_t bitRate = 0;
553 if (mIsEncoder) {
554 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
555 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700556 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700557 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700558 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700559 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700560 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700561 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700562 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700563 int32_t numChannels, sampleRate;
564 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
565 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
566
James Dong17299ab2010-05-14 15:45:22 -0700567 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700568 }
James Dongabed93a2010-04-22 17:27:04 -0700569
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700570 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700571 int32_t width, height;
572 bool success = meta->findInt32(kKeyWidth, &width);
573 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700574 CHECK(success);
Andreas Huberbe06d262009-08-14 14:37:10 -0700575
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700576 if (mIsEncoder) {
577 setVideoInputFormat(mMIME, width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700578 } else {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700579 status_t err = setVideoOutputFormat(
580 mMIME, width, height);
581
582 if (err != OK) {
583 return err;
584 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700585 }
586 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700587
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700588 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
589 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700590 OMX_COLOR_FORMATTYPE format =
591 OMX_COLOR_Format32bitARGB8888;
592 // OMX_COLOR_FormatYUV420PackedPlanar;
593 // OMX_COLOR_FormatCbYCrY;
594 // OMX_COLOR_FormatYUV411Planar;
595
596 int32_t width, height;
597 bool success = meta->findInt32(kKeyWidth, &width);
598 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700599
600 int32_t compressedSize;
601 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700602 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700603
604 CHECK(success);
605 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700606
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700607 setImageOutputFormat(format, width, height);
608 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700609 }
610
Andreas Huberda050cf22009-09-02 14:01:43 -0700611 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800612 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700613 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700614 }
615
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700616 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700617 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
618 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700619 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700620 }
621
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700622 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700623
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700624 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700625}
626
Andreas Huberda050cf22009-09-02 14:01:43 -0700627void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
628 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700629 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700630 def.nPortIndex = portIndex;
631
Andreas Huber784202e2009-10-15 13:46:54 -0700632 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700633 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
634 CHECK_EQ(err, OK);
635
Andreas Huberb8de9572010-02-22 14:58:45 -0800636 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
637 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700638 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700639 }
640
Andreas Huber784202e2009-10-15 13:46:54 -0700641 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700642 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
643 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800644
645 err = mOMX->getParameter(
646 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
647 CHECK_EQ(err, OK);
648
649 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800650 if (portIndex == kPortIndexInput
651 && (mQuirks & kInputBufferSizesAreBogus)) {
652 CHECK_EQ(def.nBufferSize, size);
653 } else {
654 CHECK(def.nBufferSize >= size);
655 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700656}
657
Andreas Huberbe06d262009-08-14 14:37:10 -0700658status_t OMXCodec::setVideoPortFormatType(
659 OMX_U32 portIndex,
660 OMX_VIDEO_CODINGTYPE compressionFormat,
661 OMX_COLOR_FORMATTYPE colorFormat) {
662 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700663 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700664 format.nPortIndex = portIndex;
665 format.nIndex = 0;
666 bool found = false;
667
668 OMX_U32 index = 0;
669 for (;;) {
670 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700671 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700672 mNode, OMX_IndexParamVideoPortFormat,
673 &format, sizeof(format));
674
675 if (err != OK) {
676 return err;
677 }
678
679 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700680 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700681
682#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700683 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700684 portIndex,
685 index, format.eCompressionFormat, format.eColorFormat);
686#endif
687
688 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
689 if (portIndex == kPortIndexInput
690 && colorFormat == format.eColorFormat) {
691 // eCompressionFormat does not seem right.
692 found = true;
693 break;
694 }
695 if (portIndex == kPortIndexOutput
696 && compressionFormat == format.eCompressionFormat) {
697 // eColorFormat does not seem right.
698 found = true;
699 break;
700 }
701 }
702
703 if (format.eCompressionFormat == compressionFormat
704 && format.eColorFormat == colorFormat) {
705 found = true;
706 break;
707 }
708
709 ++index;
710 }
711
712 if (!found) {
713 return UNKNOWN_ERROR;
714 }
715
Andreas Huber53a76bd2009-10-06 16:20:44 -0700716 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700717 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700718 mNode, OMX_IndexParamVideoPortFormat,
719 &format, sizeof(format));
720
721 return err;
722}
723
Andreas Huberb482ce82009-10-29 12:02:48 -0700724static size_t getFrameSize(
725 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
726 switch (colorFormat) {
727 case OMX_COLOR_FormatYCbYCr:
728 case OMX_COLOR_FormatCbYCrY:
729 return width * height * 2;
730
Andreas Huber71c27d92010-03-19 11:43:15 -0700731 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700732 case OMX_COLOR_FormatYUV420SemiPlanar:
733 return (width * height * 3) / 2;
734
735 default:
736 CHECK(!"Should not be here. Unsupported color format.");
737 break;
738 }
739}
740
Andreas Huberbe06d262009-08-14 14:37:10 -0700741void OMXCodec::setVideoInputFormat(
742 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700743 CODEC_LOGV("setVideoInputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700744
745 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700746 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700747 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700748 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700749 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700750 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700751 compressionFormat = OMX_VIDEO_CodingH263;
752 } else {
753 LOGE("Not a supported video mime type: %s", mime);
754 CHECK(!"Should not be here. Not a supported video mime type.");
755 }
756
Andreas Huberea6a38c2009-11-16 15:43:38 -0800757 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
758 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
James Dongabed93a2010-04-22 17:27:04 -0700759 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700760 }
761
James Dongb00e2462010-04-26 17:48:26 -0700762
763
764 status_t err;
765 OMX_PARAM_PORTDEFINITIONTYPE def;
766 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
767
768 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700769 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700770 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700771 colorFormat), OK);
James Dongb00e2462010-04-26 17:48:26 -0700772 InitOMXParams(&def);
773 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700774
James Dongb00e2462010-04-26 17:48:26 -0700775 err = mOMX->getParameter(
776 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
777 CHECK_EQ(err, OK);
778
779 def.nBufferSize = getFrameSize(colorFormat, width, height);
780
781 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
782
783 video_def->nFrameWidth = width;
784 video_def->nFrameHeight = height;
785 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
786 video_def->eColorFormat = colorFormat;
787
788 video_def->xFramerate = (24 << 16); // Q16 format
789
790 err = mOMX->setParameter(
791 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
792 CHECK_EQ(err, OK);
793
794 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700795 CHECK_EQ(setVideoPortFormatType(
796 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
797 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700798 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700799 def.nPortIndex = kPortIndexOutput;
800
James Dongb00e2462010-04-26 17:48:26 -0700801 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700802 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
803
804 CHECK_EQ(err, OK);
805 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
806
807 video_def->nFrameWidth = width;
808 video_def->nFrameHeight = height;
809
810 video_def->eCompressionFormat = compressionFormat;
811 video_def->eColorFormat = OMX_COLOR_FormatUnused;
812
Andreas Huber784202e2009-10-15 13:46:54 -0700813 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700814 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
815 CHECK_EQ(err, OK);
816
James Dongb00e2462010-04-26 17:48:26 -0700817 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700818 switch (compressionFormat) {
819 case OMX_VIDEO_CodingMPEG4:
820 {
821 CHECK_EQ(setupMPEG4EncoderParameters(), OK);
822 break;
823 }
824
825 case OMX_VIDEO_CodingH263:
826 break;
827
Andreas Huberea6a38c2009-11-16 15:43:38 -0800828 case OMX_VIDEO_CodingAVC:
829 {
830 CHECK_EQ(setupAVCEncoderParameters(), OK);
831 break;
832 }
833
Andreas Huberb482ce82009-10-29 12:02:48 -0700834 default:
835 CHECK(!"Support for this compressionFormat to be implemented.");
836 break;
837 }
838}
839
840status_t OMXCodec::setupMPEG4EncoderParameters() {
841 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
842 InitOMXParams(&mpeg4type);
843 mpeg4type.nPortIndex = kPortIndexOutput;
844
845 status_t err = mOMX->getParameter(
846 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
847 CHECK_EQ(err, OK);
848
849 mpeg4type.nSliceHeaderSpacing = 0;
850 mpeg4type.bSVH = OMX_FALSE;
851 mpeg4type.bGov = OMX_FALSE;
852
853 mpeg4type.nAllowedPictureTypes =
854 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
855
856 mpeg4type.nPFrames = 23;
857 mpeg4type.nBFrames = 0;
858
859 mpeg4type.nIDCVLCThreshold = 0;
860 mpeg4type.bACPred = OMX_TRUE;
861 mpeg4type.nMaxPacketSize = 256;
862 mpeg4type.nTimeIncRes = 1000;
863 mpeg4type.nHeaderExtension = 0;
864 mpeg4type.bReversibleVLC = OMX_FALSE;
865
866 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileCore;
867 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2;
868
869 err = mOMX->setParameter(
870 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
871 CHECK_EQ(err, OK);
872
873 // ----------------
874
875 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
876 InitOMXParams(&bitrateType);
877 bitrateType.nPortIndex = kPortIndexOutput;
878
879 err = mOMX->getParameter(
880 mNode, OMX_IndexParamVideoBitrate,
881 &bitrateType, sizeof(bitrateType));
882 CHECK_EQ(err, OK);
883
884 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
885 bitrateType.nTargetBitrate = 1000000;
886
887 err = mOMX->setParameter(
888 mNode, OMX_IndexParamVideoBitrate,
889 &bitrateType, sizeof(bitrateType));
890 CHECK_EQ(err, OK);
891
892 // ----------------
893
894 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
895 InitOMXParams(&errorCorrectionType);
896 errorCorrectionType.nPortIndex = kPortIndexOutput;
897
898 err = mOMX->getParameter(
899 mNode, OMX_IndexParamVideoErrorCorrection,
900 &errorCorrectionType, sizeof(errorCorrectionType));
901 CHECK_EQ(err, OK);
902
903 errorCorrectionType.bEnableHEC = OMX_FALSE;
904 errorCorrectionType.bEnableResync = OMX_TRUE;
905 errorCorrectionType.nResynchMarkerSpacing = 256;
906 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
907 errorCorrectionType.bEnableRVLC = OMX_FALSE;
908
909 err = mOMX->setParameter(
910 mNode, OMX_IndexParamVideoErrorCorrection,
911 &errorCorrectionType, sizeof(errorCorrectionType));
912 CHECK_EQ(err, OK);
913
914 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700915}
916
Andreas Huberea6a38c2009-11-16 15:43:38 -0800917status_t OMXCodec::setupAVCEncoderParameters() {
918 OMX_VIDEO_PARAM_AVCTYPE h264type;
919 InitOMXParams(&h264type);
920 h264type.nPortIndex = kPortIndexOutput;
921
922 status_t err = mOMX->getParameter(
923 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
924 CHECK_EQ(err, OK);
925
926 h264type.nAllowedPictureTypes =
927 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
928
929 h264type.nSliceHeaderSpacing = 0;
930 h264type.nBFrames = 0;
931 h264type.bUseHadamard = OMX_TRUE;
932 h264type.nRefFrames = 1;
933 h264type.nRefIdx10ActiveMinus1 = 0;
934 h264type.nRefIdx11ActiveMinus1 = 0;
935 h264type.bEnableUEP = OMX_FALSE;
936 h264type.bEnableFMO = OMX_FALSE;
937 h264type.bEnableASO = OMX_FALSE;
938 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -0800939 h264type.bFrameMBsOnly = OMX_TRUE;
940 h264type.bMBAFF = OMX_FALSE;
941 h264type.bEntropyCodingCABAC = OMX_FALSE;
942 h264type.bWeightedPPrediction = OMX_FALSE;
943 h264type.bconstIpred = OMX_FALSE;
944 h264type.bDirect8x8Inference = OMX_FALSE;
945 h264type.bDirectSpatialTemporal = OMX_FALSE;
946 h264type.nCabacInitIdc = 0;
947 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
948
949 err = mOMX->setParameter(
950 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
951 CHECK_EQ(err, OK);
952
953 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
954 InitOMXParams(&bitrateType);
955 bitrateType.nPortIndex = kPortIndexOutput;
956
957 err = mOMX->getParameter(
958 mNode, OMX_IndexParamVideoBitrate,
959 &bitrateType, sizeof(bitrateType));
960 CHECK_EQ(err, OK);
961
962 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
Andreas Huber71c27d92010-03-19 11:43:15 -0700963 bitrateType.nTargetBitrate = 3000000;
Andreas Huberea6a38c2009-11-16 15:43:38 -0800964
965 err = mOMX->setParameter(
966 mNode, OMX_IndexParamVideoBitrate,
967 &bitrateType, sizeof(bitrateType));
968 CHECK_EQ(err, OK);
969
970 return OK;
971}
972
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700973status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -0700974 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -0700975 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -0700976
Andreas Huberbe06d262009-08-14 14:37:10 -0700977 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700978 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700979 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700980 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700981 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700982 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700983 compressionFormat = OMX_VIDEO_CodingH263;
984 } else {
985 LOGE("Not a supported video mime type: %s", mime);
986 CHECK(!"Should not be here. Not a supported video mime type.");
987 }
988
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700989 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700990 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
991
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700992 if (err != OK) {
993 return err;
994 }
995
Andreas Huberbe06d262009-08-14 14:37:10 -0700996#if 1
997 {
998 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700999 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001000 format.nPortIndex = kPortIndexOutput;
1001 format.nIndex = 0;
1002
Andreas Huber784202e2009-10-15 13:46:54 -07001003 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001004 mNode, OMX_IndexParamVideoPortFormat,
1005 &format, sizeof(format));
1006 CHECK_EQ(err, OK);
1007 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1008
1009 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1010
1011 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1012 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1013 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1014 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1015
Andreas Huber784202e2009-10-15 13:46:54 -07001016 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001017 mNode, OMX_IndexParamVideoPortFormat,
1018 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001019
1020 if (err != OK) {
1021 return err;
1022 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001023 }
1024#endif
1025
1026 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001027 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001028 def.nPortIndex = kPortIndexInput;
1029
Andreas Huber4c483422009-09-02 16:05:36 -07001030 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1031
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001032 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001033 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1034
1035 CHECK_EQ(err, OK);
1036
1037#if 1
1038 // XXX Need a (much) better heuristic to compute input buffer sizes.
1039 const size_t X = 64 * 1024;
1040 if (def.nBufferSize < X) {
1041 def.nBufferSize = X;
1042 }
1043#endif
1044
1045 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1046
1047 video_def->nFrameWidth = width;
1048 video_def->nFrameHeight = height;
1049
Andreas Huberb482ce82009-10-29 12:02:48 -07001050 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001051 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1052
Andreas Huber784202e2009-10-15 13:46:54 -07001053 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001054 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001055
1056 if (err != OK) {
1057 return err;
1058 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001059
1060 ////////////////////////////////////////////////////////////////////////////
1061
Andreas Huber4c483422009-09-02 16:05:36 -07001062 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001063 def.nPortIndex = kPortIndexOutput;
1064
Andreas Huber784202e2009-10-15 13:46:54 -07001065 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001066 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1067 CHECK_EQ(err, OK);
1068 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1069
1070#if 0
1071 def.nBufferSize =
1072 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1073#endif
1074
1075 video_def->nFrameWidth = width;
1076 video_def->nFrameHeight = height;
1077
Andreas Huber784202e2009-10-15 13:46:54 -07001078 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001079 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001080
1081 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001082}
1083
Andreas Huberbe06d262009-08-14 14:37:10 -07001084OMXCodec::OMXCodec(
1085 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001086 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001087 const char *mime,
1088 const char *componentName,
1089 const sp<MediaSource> &source)
1090 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001091 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001092 mNode(node),
1093 mQuirks(quirks),
1094 mIsEncoder(isEncoder),
1095 mMIME(strdup(mime)),
1096 mComponentName(strdup(componentName)),
1097 mSource(source),
1098 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001099 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001100 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001101 mSignalledEOS(false),
1102 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001103 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001104 mSeekTimeUs(-1),
1105 mLeftOverBuffer(NULL) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001106 mPortStatus[kPortIndexInput] = ENABLED;
1107 mPortStatus[kPortIndexOutput] = ENABLED;
1108
Andreas Huber4c483422009-09-02 16:05:36 -07001109 setComponentRole();
1110}
1111
Andreas Hubere6c40962009-09-10 14:13:30 -07001112// static
1113void OMXCodec::setComponentRole(
1114 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1115 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001116 struct MimeToRole {
1117 const char *mime;
1118 const char *decoderRole;
1119 const char *encoderRole;
1120 };
1121
1122 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001123 { MEDIA_MIMETYPE_AUDIO_MPEG,
1124 "audio_decoder.mp3", "audio_encoder.mp3" },
1125 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1126 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1127 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1128 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1129 { MEDIA_MIMETYPE_AUDIO_AAC,
1130 "audio_decoder.aac", "audio_encoder.aac" },
1131 { MEDIA_MIMETYPE_VIDEO_AVC,
1132 "video_decoder.avc", "video_encoder.avc" },
1133 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1134 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1135 { MEDIA_MIMETYPE_VIDEO_H263,
1136 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001137 };
1138
1139 static const size_t kNumMimeToRole =
1140 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1141
1142 size_t i;
1143 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001144 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001145 break;
1146 }
1147 }
1148
1149 if (i == kNumMimeToRole) {
1150 return;
1151 }
1152
1153 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001154 isEncoder ? kMimeToRole[i].encoderRole
1155 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001156
1157 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001158 OMX_PARAM_COMPONENTROLETYPE roleParams;
1159 InitOMXParams(&roleParams);
1160
1161 strncpy((char *)roleParams.cRole,
1162 role, OMX_MAX_STRINGNAME_SIZE - 1);
1163
1164 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1165
Andreas Huber784202e2009-10-15 13:46:54 -07001166 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001167 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001168 &roleParams, sizeof(roleParams));
1169
1170 if (err != OK) {
1171 LOGW("Failed to set standard component role '%s'.", role);
1172 }
1173 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001174}
1175
Andreas Hubere6c40962009-09-10 14:13:30 -07001176void OMXCodec::setComponentRole() {
1177 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1178}
1179
Andreas Huberbe06d262009-08-14 14:37:10 -07001180OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001181 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001182
Andreas Huber784202e2009-10-15 13:46:54 -07001183 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001184 CHECK_EQ(err, OK);
1185
1186 mNode = NULL;
1187 setState(DEAD);
1188
1189 clearCodecSpecificData();
1190
1191 free(mComponentName);
1192 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001193
Andreas Huberbe06d262009-08-14 14:37:10 -07001194 free(mMIME);
1195 mMIME = NULL;
1196}
1197
1198status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001199 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001200
1201 CHECK_EQ(mState, LOADED);
1202
1203 status_t err;
1204 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001205 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001206 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001207 setState(LOADED_TO_IDLE);
1208 }
1209
1210 err = allocateBuffers();
1211 CHECK_EQ(err, OK);
1212
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);
1216
1217 setState(LOADED_TO_IDLE);
1218 }
1219
1220 while (mState != EXECUTING && mState != ERROR) {
1221 mAsyncCompletion.wait(mLock);
1222 }
1223
1224 return mState == ERROR ? UNKNOWN_ERROR : OK;
1225}
1226
1227// static
1228bool OMXCodec::isIntermediateState(State state) {
1229 return state == LOADED_TO_IDLE
1230 || state == IDLE_TO_EXECUTING
1231 || state == EXECUTING_TO_IDLE
1232 || state == IDLE_TO_LOADED
1233 || state == RECONFIGURING;
1234}
1235
1236status_t OMXCodec::allocateBuffers() {
1237 status_t err = allocateBuffersOnPort(kPortIndexInput);
1238
1239 if (err != OK) {
1240 return err;
1241 }
1242
1243 return allocateBuffersOnPort(kPortIndexOutput);
1244}
1245
1246status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1247 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001248 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001249 def.nPortIndex = portIndex;
1250
Andreas Huber784202e2009-10-15 13:46:54 -07001251 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001252 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1253
1254 if (err != OK) {
1255 return err;
1256 }
1257
Andreas Huber5c0a9132009-08-20 11:16:40 -07001258 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001259 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001260
Andreas Huberbe06d262009-08-14 14:37:10 -07001261 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001262 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001263 CHECK(mem.get() != NULL);
1264
Andreas Huberc712b9f2010-01-20 15:05:46 -08001265 BufferInfo info;
1266 info.mData = NULL;
1267 info.mSize = def.nBufferSize;
1268
Andreas Huberbe06d262009-08-14 14:37:10 -07001269 IOMX::buffer_id buffer;
1270 if (portIndex == kPortIndexInput
1271 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001272 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001273 mem.clear();
1274
Andreas Huberf1fe0642010-01-15 15:28:19 -08001275 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001276 mNode, portIndex, def.nBufferSize, &buffer,
1277 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001278 } else {
1279 err = mOMX->allocateBufferWithBackup(
1280 mNode, portIndex, mem, &buffer);
1281 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001282 } else if (portIndex == kPortIndexOutput
1283 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001284 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001285 mem.clear();
1286
Andreas Huberf1fe0642010-01-15 15:28:19 -08001287 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001288 mNode, portIndex, def.nBufferSize, &buffer,
1289 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001290 } else {
1291 err = mOMX->allocateBufferWithBackup(
1292 mNode, portIndex, mem, &buffer);
1293 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001294 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001295 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001296 }
1297
1298 if (err != OK) {
1299 LOGE("allocate_buffer_with_backup failed");
1300 return err;
1301 }
1302
Andreas Huberc712b9f2010-01-20 15:05:46 -08001303 if (mem != NULL) {
1304 info.mData = mem->pointer();
1305 }
1306
Andreas Huberbe06d262009-08-14 14:37:10 -07001307 info.mBuffer = buffer;
1308 info.mOwnedByComponent = false;
1309 info.mMem = mem;
1310 info.mMediaBuffer = NULL;
1311
1312 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001313 if (!(mOMXLivesLocally
1314 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1315 && (mQuirks & kDefersOutputBufferAllocation))) {
1316 // If the node does not fill in the buffer ptr at this time,
1317 // we will defer creating the MediaBuffer until receiving
1318 // the first FILL_BUFFER_DONE notification instead.
1319 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1320 info.mMediaBuffer->setObserver(this);
1321 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001322 }
1323
1324 mPortBuffers[portIndex].push(info);
1325
Andreas Huber4c483422009-09-02 16:05:36 -07001326 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001327 portIndex == kPortIndexInput ? "input" : "output");
1328 }
1329
Andreas Huber2ea14e22009-12-16 09:30:55 -08001330 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001331
1332 return OK;
1333}
1334
1335void OMXCodec::on_message(const omx_message &msg) {
1336 Mutex::Autolock autoLock(mLock);
1337
1338 switch (msg.type) {
1339 case omx_message::EVENT:
1340 {
1341 onEvent(
1342 msg.u.event_data.event, msg.u.event_data.data1,
1343 msg.u.event_data.data2);
1344
1345 break;
1346 }
1347
1348 case omx_message::EMPTY_BUFFER_DONE:
1349 {
1350 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1351
Andreas Huber4c483422009-09-02 16:05:36 -07001352 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001353
1354 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1355 size_t i = 0;
1356 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1357 ++i;
1358 }
1359
1360 CHECK(i < buffers->size());
1361 if (!(*buffers)[i].mOwnedByComponent) {
1362 LOGW("We already own input buffer %p, yet received "
1363 "an EMPTY_BUFFER_DONE.", buffer);
1364 }
1365
1366 buffers->editItemAt(i).mOwnedByComponent = false;
1367
1368 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001369 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001370
1371 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001372 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001373 CHECK_EQ(err, OK);
1374
1375 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001376 } else if (mState != ERROR
1377 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001378 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1379 drainInputBuffer(&buffers->editItemAt(i));
1380 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001381 break;
1382 }
1383
1384 case omx_message::FILL_BUFFER_DONE:
1385 {
1386 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1387 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1388
Andreas Huber2ea14e22009-12-16 09:30:55 -08001389 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001390 buffer,
1391 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001392 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001393 msg.u.extended_buffer_data.timestamp,
1394 msg.u.extended_buffer_data.timestamp / 1E6);
1395
1396 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1397 size_t i = 0;
1398 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1399 ++i;
1400 }
1401
1402 CHECK(i < buffers->size());
1403 BufferInfo *info = &buffers->editItemAt(i);
1404
1405 if (!info->mOwnedByComponent) {
1406 LOGW("We already own output buffer %p, yet received "
1407 "a FILL_BUFFER_DONE.", buffer);
1408 }
1409
1410 info->mOwnedByComponent = false;
1411
1412 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001413 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001414
1415 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001416 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001417 CHECK_EQ(err, OK);
1418
1419 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001420#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001421 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1422 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001423 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001424 mNoMoreOutputData = true;
1425 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001426#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001427 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1428 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001429
Andreas Huber52733b82010-01-25 10:41:35 -08001430 if (info->mMediaBuffer == NULL) {
1431 CHECK(mOMXLivesLocally);
1432 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1433 CHECK(mQuirks & kDefersOutputBufferAllocation);
1434
1435 // The qcom video decoders on Nexus don't actually allocate
1436 // output buffer memory on a call to OMX_AllocateBuffer
1437 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1438 // structure is only filled in later.
1439
1440 info->mMediaBuffer = new MediaBuffer(
1441 msg.u.extended_buffer_data.data_ptr,
1442 info->mSize);
1443 info->mMediaBuffer->setObserver(this);
1444 }
1445
Andreas Huberbe06d262009-08-14 14:37:10 -07001446 MediaBuffer *buffer = info->mMediaBuffer;
1447
1448 buffer->set_range(
1449 msg.u.extended_buffer_data.range_offset,
1450 msg.u.extended_buffer_data.range_length);
1451
1452 buffer->meta_data()->clear();
1453
Andreas Huberfa8de752009-10-08 10:07:49 -07001454 buffer->meta_data()->setInt64(
1455 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001456
1457 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1458 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1459 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001460 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1461 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1462 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001463
1464 buffer->meta_data()->setPointer(
1465 kKeyPlatformPrivate,
1466 msg.u.extended_buffer_data.platform_private);
1467
1468 buffer->meta_data()->setPointer(
1469 kKeyBufferID,
1470 msg.u.extended_buffer_data.buffer);
1471
1472 mFilledBuffers.push_back(i);
1473 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001474
1475 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1476 CODEC_LOGV("No more output data.");
1477 mNoMoreOutputData = true;
1478 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001479 }
1480
1481 break;
1482 }
1483
1484 default:
1485 {
1486 CHECK(!"should not be here.");
1487 break;
1488 }
1489 }
1490}
1491
1492void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1493 switch (event) {
1494 case OMX_EventCmdComplete:
1495 {
1496 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1497 break;
1498 }
1499
1500 case OMX_EventError:
1501 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001502 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001503
1504 setState(ERROR);
1505 break;
1506 }
1507
1508 case OMX_EventPortSettingsChanged:
1509 {
1510 onPortSettingsChanged(data1);
1511 break;
1512 }
1513
Andreas Huber2ea14e22009-12-16 09:30:55 -08001514#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001515 case OMX_EventBufferFlag:
1516 {
Andreas Huber4c483422009-09-02 16:05:36 -07001517 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001518
1519 if (data1 == kPortIndexOutput) {
1520 mNoMoreOutputData = true;
1521 }
1522 break;
1523 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001524#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001525
1526 default:
1527 {
Andreas Huber4c483422009-09-02 16:05:36 -07001528 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001529 break;
1530 }
1531 }
1532}
1533
Andreas Huberb1678602009-10-19 13:06:40 -07001534// Has the format changed in any way that the client would have to be aware of?
1535static bool formatHasNotablyChanged(
1536 const sp<MetaData> &from, const sp<MetaData> &to) {
1537 if (from.get() == NULL && to.get() == NULL) {
1538 return false;
1539 }
1540
Andreas Huberf68c1682009-10-21 14:01:30 -07001541 if ((from.get() == NULL && to.get() != NULL)
1542 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001543 return true;
1544 }
1545
1546 const char *mime_from, *mime_to;
1547 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1548 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1549
1550 if (strcasecmp(mime_from, mime_to)) {
1551 return true;
1552 }
1553
1554 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1555 int32_t colorFormat_from, colorFormat_to;
1556 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1557 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1558
1559 if (colorFormat_from != colorFormat_to) {
1560 return true;
1561 }
1562
1563 int32_t width_from, width_to;
1564 CHECK(from->findInt32(kKeyWidth, &width_from));
1565 CHECK(to->findInt32(kKeyWidth, &width_to));
1566
1567 if (width_from != width_to) {
1568 return true;
1569 }
1570
1571 int32_t height_from, height_to;
1572 CHECK(from->findInt32(kKeyHeight, &height_from));
1573 CHECK(to->findInt32(kKeyHeight, &height_to));
1574
1575 if (height_from != height_to) {
1576 return true;
1577 }
1578 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1579 int32_t numChannels_from, numChannels_to;
1580 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1581 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1582
1583 if (numChannels_from != numChannels_to) {
1584 return true;
1585 }
1586
1587 int32_t sampleRate_from, sampleRate_to;
1588 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1589 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1590
1591 if (sampleRate_from != sampleRate_to) {
1592 return true;
1593 }
1594 }
1595
1596 return false;
1597}
1598
Andreas Huberbe06d262009-08-14 14:37:10 -07001599void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1600 switch (cmd) {
1601 case OMX_CommandStateSet:
1602 {
1603 onStateChange((OMX_STATETYPE)data);
1604 break;
1605 }
1606
1607 case OMX_CommandPortDisable:
1608 {
1609 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001610 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001611
1612 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1613 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1614 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1615
1616 mPortStatus[portIndex] = DISABLED;
1617
1618 if (mState == RECONFIGURING) {
1619 CHECK_EQ(portIndex, kPortIndexOutput);
1620
Andreas Huberb1678602009-10-19 13:06:40 -07001621 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001622 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001623
1624 // Don't notify clients if the output port settings change
1625 // wasn't of importance to them, i.e. it may be that just the
1626 // number of buffers has changed and nothing else.
1627 mOutputPortSettingsHaveChanged =
1628 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001629
Andreas Huberbe06d262009-08-14 14:37:10 -07001630 enablePortAsync(portIndex);
1631
1632 status_t err = allocateBuffersOnPort(portIndex);
1633 CHECK_EQ(err, OK);
1634 }
1635 break;
1636 }
1637
1638 case OMX_CommandPortEnable:
1639 {
1640 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001641 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001642
1643 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1644 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1645
1646 mPortStatus[portIndex] = ENABLED;
1647
1648 if (mState == RECONFIGURING) {
1649 CHECK_EQ(portIndex, kPortIndexOutput);
1650
1651 setState(EXECUTING);
1652
1653 fillOutputBuffers();
1654 }
1655 break;
1656 }
1657
1658 case OMX_CommandFlush:
1659 {
1660 OMX_U32 portIndex = data;
1661
Andreas Huber4c483422009-09-02 16:05:36 -07001662 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001663
1664 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1665 mPortStatus[portIndex] = ENABLED;
1666
1667 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1668 mPortBuffers[portIndex].size());
1669
1670 if (mState == RECONFIGURING) {
1671 CHECK_EQ(portIndex, kPortIndexOutput);
1672
1673 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001674 } else if (mState == EXECUTING_TO_IDLE) {
1675 if (mPortStatus[kPortIndexInput] == ENABLED
1676 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001677 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001678 "transition from EXECUTING to IDLE.");
1679
1680 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1681 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1682
1683 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001684 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001685 CHECK_EQ(err, OK);
1686 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001687 } else {
1688 // We're flushing both ports in preparation for seeking.
1689
1690 if (mPortStatus[kPortIndexInput] == ENABLED
1691 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001692 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001693 " seek-time.");
1694
1695 drainInputBuffers();
1696 fillOutputBuffers();
1697 }
1698 }
1699
1700 break;
1701 }
1702
1703 default:
1704 {
Andreas Huber4c483422009-09-02 16:05:36 -07001705 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001706 break;
1707 }
1708 }
1709}
1710
1711void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001712 CODEC_LOGV("onStateChange %d", newState);
1713
Andreas Huberbe06d262009-08-14 14:37:10 -07001714 switch (newState) {
1715 case OMX_StateIdle:
1716 {
Andreas Huber4c483422009-09-02 16:05:36 -07001717 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001718 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001719 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001720 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1721
1722 CHECK_EQ(err, OK);
1723
1724 setState(IDLE_TO_EXECUTING);
1725 } else {
1726 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1727
1728 CHECK_EQ(
1729 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1730 mPortBuffers[kPortIndexInput].size());
1731
1732 CHECK_EQ(
1733 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1734 mPortBuffers[kPortIndexOutput].size());
1735
Andreas Huber784202e2009-10-15 13:46:54 -07001736 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001737 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1738
1739 CHECK_EQ(err, OK);
1740
1741 err = freeBuffersOnPort(kPortIndexInput);
1742 CHECK_EQ(err, OK);
1743
1744 err = freeBuffersOnPort(kPortIndexOutput);
1745 CHECK_EQ(err, OK);
1746
1747 mPortStatus[kPortIndexInput] = ENABLED;
1748 mPortStatus[kPortIndexOutput] = ENABLED;
1749
1750 setState(IDLE_TO_LOADED);
1751 }
1752 break;
1753 }
1754
1755 case OMX_StateExecuting:
1756 {
1757 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1758
Andreas Huber4c483422009-09-02 16:05:36 -07001759 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001760
1761 setState(EXECUTING);
1762
Andreas Huber42978e52009-08-27 10:08:39 -07001763 // Buffers will be submitted to the component in the first
1764 // call to OMXCodec::read as mInitialBufferSubmit is true at
1765 // this point. This ensures that this on_message call returns,
1766 // releases the lock and ::init can notice the state change and
1767 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001768 break;
1769 }
1770
1771 case OMX_StateLoaded:
1772 {
1773 CHECK_EQ(mState, IDLE_TO_LOADED);
1774
Andreas Huber4c483422009-09-02 16:05:36 -07001775 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001776
1777 setState(LOADED);
1778 break;
1779 }
1780
Andreas Huberc712b9f2010-01-20 15:05:46 -08001781 case OMX_StateInvalid:
1782 {
1783 setState(ERROR);
1784 break;
1785 }
1786
Andreas Huberbe06d262009-08-14 14:37:10 -07001787 default:
1788 {
1789 CHECK(!"should not be here.");
1790 break;
1791 }
1792 }
1793}
1794
1795// static
1796size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1797 size_t n = 0;
1798 for (size_t i = 0; i < buffers.size(); ++i) {
1799 if (!buffers[i].mOwnedByComponent) {
1800 ++n;
1801 }
1802 }
1803
1804 return n;
1805}
1806
1807status_t OMXCodec::freeBuffersOnPort(
1808 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1809 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1810
1811 status_t stickyErr = OK;
1812
1813 for (size_t i = buffers->size(); i-- > 0;) {
1814 BufferInfo *info = &buffers->editItemAt(i);
1815
1816 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1817 continue;
1818 }
1819
1820 CHECK_EQ(info->mOwnedByComponent, false);
1821
Andreas Huber92022852009-09-14 15:24:14 -07001822 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1823
Andreas Huberbe06d262009-08-14 14:37:10 -07001824 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001825 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001826
1827 if (err != OK) {
1828 stickyErr = err;
1829 }
1830
1831 if (info->mMediaBuffer != NULL) {
1832 info->mMediaBuffer->setObserver(NULL);
1833
1834 // Make sure nobody but us owns this buffer at this point.
1835 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1836
1837 info->mMediaBuffer->release();
1838 }
1839
1840 buffers->removeAt(i);
1841 }
1842
1843 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1844
1845 return stickyErr;
1846}
1847
1848void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001849 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001850
1851 CHECK_EQ(mState, EXECUTING);
1852 CHECK_EQ(portIndex, kPortIndexOutput);
1853 setState(RECONFIGURING);
1854
1855 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001856 if (!flushPortAsync(portIndex)) {
1857 onCmdComplete(OMX_CommandFlush, portIndex);
1858 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001859 } else {
1860 disablePortAsync(portIndex);
1861 }
1862}
1863
Andreas Huber404cc412009-08-25 14:26:05 -07001864bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001865 CHECK(mState == EXECUTING || mState == RECONFIGURING
1866 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001867
Andreas Huber4c483422009-09-02 16:05:36 -07001868 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001869 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1870 mPortBuffers[portIndex].size());
1871
Andreas Huberbe06d262009-08-14 14:37:10 -07001872 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1873 mPortStatus[portIndex] = SHUTTING_DOWN;
1874
Andreas Huber404cc412009-08-25 14:26:05 -07001875 if ((mQuirks & kRequiresFlushCompleteEmulation)
1876 && countBuffersWeOwn(mPortBuffers[portIndex])
1877 == mPortBuffers[portIndex].size()) {
1878 // No flush is necessary and this component fails to send a
1879 // flush-complete event in this case.
1880
1881 return false;
1882 }
1883
Andreas Huberbe06d262009-08-14 14:37:10 -07001884 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001885 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001886 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001887
1888 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001889}
1890
1891void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1892 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1893
1894 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1895 mPortStatus[portIndex] = DISABLING;
1896
1897 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001898 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001899 CHECK_EQ(err, OK);
1900
1901 freeBuffersOnPort(portIndex, true);
1902}
1903
1904void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1905 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1906
1907 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1908 mPortStatus[portIndex] = ENABLING;
1909
1910 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001911 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001912 CHECK_EQ(err, OK);
1913}
1914
1915void OMXCodec::fillOutputBuffers() {
1916 CHECK_EQ(mState, EXECUTING);
1917
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001918 // This is a workaround for some decoders not properly reporting
1919 // end-of-output-stream. If we own all input buffers and also own
1920 // all output buffers and we already signalled end-of-input-stream,
1921 // the end-of-output-stream is implied.
1922 if (mSignalledEOS
1923 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
1924 == mPortBuffers[kPortIndexInput].size()
1925 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
1926 == mPortBuffers[kPortIndexOutput].size()) {
1927 mNoMoreOutputData = true;
1928 mBufferFilled.signal();
1929
1930 return;
1931 }
1932
Andreas Huberbe06d262009-08-14 14:37:10 -07001933 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1934 for (size_t i = 0; i < buffers->size(); ++i) {
1935 fillOutputBuffer(&buffers->editItemAt(i));
1936 }
1937}
1938
1939void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001940 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001941
1942 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1943 for (size_t i = 0; i < buffers->size(); ++i) {
1944 drainInputBuffer(&buffers->editItemAt(i));
1945 }
1946}
1947
1948void OMXCodec::drainInputBuffer(BufferInfo *info) {
1949 CHECK_EQ(info->mOwnedByComponent, false);
1950
1951 if (mSignalledEOS) {
1952 return;
1953 }
1954
1955 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1956 const CodecSpecificData *specific =
1957 mCodecSpecificData[mCodecSpecificDataIndex];
1958
1959 size_t size = specific->mSize;
1960
Andreas Hubere6c40962009-09-10 14:13:30 -07001961 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07001962 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001963 static const uint8_t kNALStartCode[4] =
1964 { 0x00, 0x00, 0x00, 0x01 };
1965
Andreas Huberc712b9f2010-01-20 15:05:46 -08001966 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07001967
1968 size += 4;
1969
Andreas Huberc712b9f2010-01-20 15:05:46 -08001970 memcpy(info->mData, kNALStartCode, 4);
1971 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07001972 specific->mData, specific->mSize);
1973 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001974 CHECK(info->mSize >= specific->mSize);
1975 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001976 }
1977
Andreas Huber2ea14e22009-12-16 09:30:55 -08001978 mNoMoreOutputData = false;
1979
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001980 CODEC_LOGV("calling emptyBuffer with codec specific data");
1981
Andreas Huber784202e2009-10-15 13:46:54 -07001982 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07001983 mNode, info->mBuffer, 0, size,
1984 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1985 0);
Andreas Huber3f427072009-10-08 11:02:27 -07001986 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001987
1988 info->mOwnedByComponent = true;
1989
1990 ++mCodecSpecificDataIndex;
1991 return;
1992 }
1993
Andreas Huberbe06d262009-08-14 14:37:10 -07001994 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08001995
Andreas Hubera4357ad2010-04-02 12:49:54 -07001996 bool signalEOS = false;
1997 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07001998
Andreas Hubera4357ad2010-04-02 12:49:54 -07001999 size_t offset = 0;
2000 int32_t n = 0;
2001 for (;;) {
2002 MediaBuffer *srcBuffer;
2003 if (mSeekTimeUs >= 0) {
2004 if (mLeftOverBuffer) {
2005 mLeftOverBuffer->release();
2006 mLeftOverBuffer = NULL;
2007 }
2008
2009 MediaSource::ReadOptions options;
2010 options.setSeekTo(mSeekTimeUs);
2011
2012 mSeekTimeUs = -1;
2013 mBufferFilled.signal();
2014
2015 err = mSource->read(&srcBuffer, &options);
2016 } else if (mLeftOverBuffer) {
2017 srcBuffer = mLeftOverBuffer;
2018 mLeftOverBuffer = NULL;
2019
2020 err = OK;
2021 } else {
2022 err = mSource->read(&srcBuffer);
2023 }
2024
2025 if (err != OK) {
2026 signalEOS = true;
2027 mFinalStatus = err;
2028 mSignalledEOS = true;
2029 break;
2030 }
2031
2032 size_t remainingBytes = info->mSize - offset;
2033
2034 if (srcBuffer->range_length() > remainingBytes) {
2035 if (offset == 0) {
2036 CODEC_LOGE(
2037 "Codec's input buffers are too small to accomodate "
2038 "buffer read from source (info->mSize = %d, srcLength = %d)",
2039 info->mSize, srcBuffer->range_length());
2040
2041 srcBuffer->release();
2042 srcBuffer = NULL;
2043
2044 setState(ERROR);
2045 return;
2046 }
2047
2048 mLeftOverBuffer = srcBuffer;
2049 break;
2050 }
2051
2052 memcpy((uint8_t *)info->mData + offset,
2053 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2054 srcBuffer->range_length());
2055
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002056 int64_t lastBufferTimeUs;
2057 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
2058 CHECK(timestampUs >= 0);
2059
Andreas Hubera4357ad2010-04-02 12:49:54 -07002060 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002061 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002062 }
2063
2064 offset += srcBuffer->range_length();
2065
2066 srcBuffer->release();
2067 srcBuffer = NULL;
2068
2069 ++n;
2070
2071 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2072 break;
2073 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002074
2075 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2076
2077 if (coalescedDurationUs > 250000ll) {
2078 // Don't coalesce more than 250ms worth of encoded data at once.
2079 break;
2080 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002081 }
2082
2083 if (n > 1) {
2084 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002085 }
2086
2087 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002088
Andreas Hubera4357ad2010-04-02 12:49:54 -07002089 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002090 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002091 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002092 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002093 }
2094
Andreas Hubera4357ad2010-04-02 12:49:54 -07002095 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2096 "timestamp %lld us (%.2f secs)",
2097 info->mBuffer, offset,
2098 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002099
Andreas Huber784202e2009-10-15 13:46:54 -07002100 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002101 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002102 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002103
2104 if (err != OK) {
2105 setState(ERROR);
2106 return;
2107 }
2108
2109 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002110
2111 // This component does not ever signal the EOS flag on output buffers,
2112 // Thanks for nothing.
2113 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2114 mNoMoreOutputData = true;
2115 mBufferFilled.signal();
2116 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002117}
2118
2119void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2120 CHECK_EQ(info->mOwnedByComponent, false);
2121
Andreas Huber404cc412009-08-25 14:26:05 -07002122 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002123 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002124 "calling fillOutputBuffer");
2125 return;
2126 }
2127
Andreas Huber4c483422009-09-02 16:05:36 -07002128 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002129 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002130
2131 if (err != OK) {
2132 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2133
2134 setState(ERROR);
2135 return;
2136 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002137
2138 info->mOwnedByComponent = true;
2139}
2140
2141void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2142 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2143 for (size_t i = 0; i < buffers->size(); ++i) {
2144 if ((*buffers)[i].mBuffer == buffer) {
2145 drainInputBuffer(&buffers->editItemAt(i));
2146 return;
2147 }
2148 }
2149
2150 CHECK(!"should not be here.");
2151}
2152
2153void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2154 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2155 for (size_t i = 0; i < buffers->size(); ++i) {
2156 if ((*buffers)[i].mBuffer == buffer) {
2157 fillOutputBuffer(&buffers->editItemAt(i));
2158 return;
2159 }
2160 }
2161
2162 CHECK(!"should not be here.");
2163}
2164
2165void OMXCodec::setState(State newState) {
2166 mState = newState;
2167 mAsyncCompletion.signal();
2168
2169 // This may cause some spurious wakeups but is necessary to
2170 // unblock the reader if we enter ERROR state.
2171 mBufferFilled.signal();
2172}
2173
Andreas Huberda050cf22009-09-02 14:01:43 -07002174void OMXCodec::setRawAudioFormat(
2175 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002176
2177 // port definition
2178 OMX_PARAM_PORTDEFINITIONTYPE def;
2179 InitOMXParams(&def);
2180 def.nPortIndex = portIndex;
2181 status_t err = mOMX->getParameter(
2182 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2183 CHECK_EQ(err, OK);
2184 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2185 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2186 &def, sizeof(def)), OK);
2187
2188 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002189 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002190 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002191 pcmParams.nPortIndex = portIndex;
2192
James Dongabed93a2010-04-22 17:27:04 -07002193 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002194 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2195
2196 CHECK_EQ(err, OK);
2197
2198 pcmParams.nChannels = numChannels;
2199 pcmParams.eNumData = OMX_NumericalDataSigned;
2200 pcmParams.bInterleaved = OMX_TRUE;
2201 pcmParams.nBitPerSample = 16;
2202 pcmParams.nSamplingRate = sampleRate;
2203 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2204
2205 if (numChannels == 1) {
2206 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2207 } else {
2208 CHECK_EQ(numChannels, 2);
2209
2210 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2211 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2212 }
2213
Andreas Huber784202e2009-10-15 13:46:54 -07002214 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002215 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2216
2217 CHECK_EQ(err, OK);
2218}
2219
James Dong17299ab2010-05-14 15:45:22 -07002220static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2221 if (isAMRWB) {
2222 if (bps <= 6600) {
2223 return OMX_AUDIO_AMRBandModeWB0;
2224 } else if (bps <= 8850) {
2225 return OMX_AUDIO_AMRBandModeWB1;
2226 } else if (bps <= 12650) {
2227 return OMX_AUDIO_AMRBandModeWB2;
2228 } else if (bps <= 14250) {
2229 return OMX_AUDIO_AMRBandModeWB3;
2230 } else if (bps <= 15850) {
2231 return OMX_AUDIO_AMRBandModeWB4;
2232 } else if (bps <= 18250) {
2233 return OMX_AUDIO_AMRBandModeWB5;
2234 } else if (bps <= 19850) {
2235 return OMX_AUDIO_AMRBandModeWB6;
2236 } else if (bps <= 23050) {
2237 return OMX_AUDIO_AMRBandModeWB7;
2238 }
2239
2240 // 23850 bps
2241 return OMX_AUDIO_AMRBandModeWB8;
2242 } else { // AMRNB
2243 if (bps <= 4750) {
2244 return OMX_AUDIO_AMRBandModeNB0;
2245 } else if (bps <= 5150) {
2246 return OMX_AUDIO_AMRBandModeNB1;
2247 } else if (bps <= 5900) {
2248 return OMX_AUDIO_AMRBandModeNB2;
2249 } else if (bps <= 6700) {
2250 return OMX_AUDIO_AMRBandModeNB3;
2251 } else if (bps <= 7400) {
2252 return OMX_AUDIO_AMRBandModeNB4;
2253 } else if (bps <= 7950) {
2254 return OMX_AUDIO_AMRBandModeNB5;
2255 } else if (bps <= 10200) {
2256 return OMX_AUDIO_AMRBandModeNB6;
2257 }
2258
2259 // 12200 bps
2260 return OMX_AUDIO_AMRBandModeNB7;
2261 }
2262}
2263
2264void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002265 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002266
Andreas Huber8768f2c2009-12-01 15:26:54 -08002267 OMX_AUDIO_PARAM_AMRTYPE def;
2268 InitOMXParams(&def);
2269 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002270
Andreas Huber8768f2c2009-12-01 15:26:54 -08002271 status_t err =
2272 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002273
Andreas Huber8768f2c2009-12-01 15:26:54 -08002274 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002275
Andreas Huber8768f2c2009-12-01 15:26:54 -08002276 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002277
James Dong17299ab2010-05-14 15:45:22 -07002278 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002279 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2280 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002281
2282 ////////////////////////
2283
2284 if (mIsEncoder) {
2285 sp<MetaData> format = mSource->getFormat();
2286 int32_t sampleRate;
2287 int32_t numChannels;
2288 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2289 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2290
2291 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2292 }
2293}
2294
James Dong17299ab2010-05-14 15:45:22 -07002295void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002296 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002297 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002298 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002299 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002300
2301 //////////////// output port ////////////////////
2302 // format
2303 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2304 format.nPortIndex = kPortIndexOutput;
2305 format.nIndex = 0;
2306 status_t err = OMX_ErrorNone;
2307 while (OMX_ErrorNone == err) {
2308 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2309 &format, sizeof(format)), OK);
2310 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2311 break;
2312 }
2313 format.nIndex++;
2314 }
2315 CHECK_EQ(OK, err);
2316 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2317 &format, sizeof(format)), OK);
2318
2319 // port definition
2320 OMX_PARAM_PORTDEFINITIONTYPE def;
2321 InitOMXParams(&def);
2322 def.nPortIndex = kPortIndexOutput;
2323 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2324 &def, sizeof(def)), OK);
2325 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2326 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2327 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2328 &def, sizeof(def)), OK);
2329
2330 // profile
2331 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2332 InitOMXParams(&profile);
2333 profile.nPortIndex = kPortIndexOutput;
2334 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2335 &profile, sizeof(profile)), OK);
2336 profile.nChannels = numChannels;
2337 profile.eChannelMode = (numChannels == 1?
2338 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2339 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002340 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002341 profile.nAudioBandWidth = 0;
2342 profile.nFrameLength = 0;
2343 profile.nAACtools = OMX_AUDIO_AACToolAll;
2344 profile.nAACERtools = OMX_AUDIO_AACERNone;
2345 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2346 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2347 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2348 &profile, sizeof(profile)), OK);
2349
Andreas Huberda050cf22009-09-02 14:01:43 -07002350 } else {
2351 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002352 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002353 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002354
Andreas Huber784202e2009-10-15 13:46:54 -07002355 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002356 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2357 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002358
Andreas Huberda050cf22009-09-02 14:01:43 -07002359 profile.nChannels = numChannels;
2360 profile.nSampleRate = sampleRate;
2361 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002362
Andreas Huber784202e2009-10-15 13:46:54 -07002363 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002364 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2365 CHECK_EQ(err, OK);
2366 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002367}
2368
2369void OMXCodec::setImageOutputFormat(
2370 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002371 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002372
2373#if 0
2374 OMX_INDEXTYPE index;
2375 status_t err = mOMX->get_extension_index(
2376 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2377 CHECK_EQ(err, OK);
2378
2379 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2380 CHECK_EQ(err, OK);
2381#endif
2382
2383 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002384 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002385 def.nPortIndex = kPortIndexOutput;
2386
Andreas Huber784202e2009-10-15 13:46:54 -07002387 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002388 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2389 CHECK_EQ(err, OK);
2390
2391 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2392
2393 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002394
Andreas Huberbe06d262009-08-14 14:37:10 -07002395 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2396 imageDef->eColorFormat = format;
2397 imageDef->nFrameWidth = width;
2398 imageDef->nFrameHeight = height;
2399
2400 switch (format) {
2401 case OMX_COLOR_FormatYUV420PackedPlanar:
2402 case OMX_COLOR_FormatYUV411Planar:
2403 {
2404 def.nBufferSize = (width * height * 3) / 2;
2405 break;
2406 }
2407
2408 case OMX_COLOR_FormatCbYCrY:
2409 {
2410 def.nBufferSize = width * height * 2;
2411 break;
2412 }
2413
2414 case OMX_COLOR_Format32bitARGB8888:
2415 {
2416 def.nBufferSize = width * height * 4;
2417 break;
2418 }
2419
Andreas Huber201511c2009-09-08 14:01:44 -07002420 case OMX_COLOR_Format16bitARGB4444:
2421 case OMX_COLOR_Format16bitARGB1555:
2422 case OMX_COLOR_Format16bitRGB565:
2423 case OMX_COLOR_Format16bitBGR565:
2424 {
2425 def.nBufferSize = width * height * 2;
2426 break;
2427 }
2428
Andreas Huberbe06d262009-08-14 14:37:10 -07002429 default:
2430 CHECK(!"Should not be here. Unknown color format.");
2431 break;
2432 }
2433
Andreas Huber5c0a9132009-08-20 11:16:40 -07002434 def.nBufferCountActual = def.nBufferCountMin;
2435
Andreas Huber784202e2009-10-15 13:46:54 -07002436 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002437 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2438 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002439}
Andreas Huberbe06d262009-08-14 14:37:10 -07002440
Andreas Huber5c0a9132009-08-20 11:16:40 -07002441void OMXCodec::setJPEGInputFormat(
2442 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2443 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002444 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002445 def.nPortIndex = kPortIndexInput;
2446
Andreas Huber784202e2009-10-15 13:46:54 -07002447 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002448 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2449 CHECK_EQ(err, OK);
2450
Andreas Huber5c0a9132009-08-20 11:16:40 -07002451 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2452 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2453
Andreas Huberbe06d262009-08-14 14:37:10 -07002454 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2455 imageDef->nFrameWidth = width;
2456 imageDef->nFrameHeight = height;
2457
Andreas Huber5c0a9132009-08-20 11:16:40 -07002458 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002459 def.nBufferCountActual = def.nBufferCountMin;
2460
Andreas Huber784202e2009-10-15 13:46:54 -07002461 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002462 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2463 CHECK_EQ(err, OK);
2464}
2465
2466void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2467 CodecSpecificData *specific =
2468 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2469
2470 specific->mSize = size;
2471 memcpy(specific->mData, data, size);
2472
2473 mCodecSpecificData.push(specific);
2474}
2475
2476void OMXCodec::clearCodecSpecificData() {
2477 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2478 free(mCodecSpecificData.editItemAt(i));
2479 }
2480 mCodecSpecificData.clear();
2481 mCodecSpecificDataIndex = 0;
2482}
2483
2484status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07002485 Mutex::Autolock autoLock(mLock);
2486
Andreas Huberbe06d262009-08-14 14:37:10 -07002487 if (mState != LOADED) {
2488 return UNKNOWN_ERROR;
2489 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002490
Andreas Huberbe06d262009-08-14 14:37:10 -07002491 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002492 if (mQuirks & kWantsNALFragments) {
2493 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002494 }
2495 status_t err = mSource->start(params.get());
2496
2497 if (err != OK) {
2498 return err;
2499 }
2500
2501 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002502 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002503 mSignalledEOS = false;
2504 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002505 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002506 mSeekTimeUs = -1;
2507 mFilledBuffers.clear();
2508
2509 return init();
2510}
2511
2512status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002513 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002514
2515 Mutex::Autolock autoLock(mLock);
2516
2517 while (isIntermediateState(mState)) {
2518 mAsyncCompletion.wait(mLock);
2519 }
2520
2521 switch (mState) {
2522 case LOADED:
2523 case ERROR:
2524 break;
2525
2526 case EXECUTING:
2527 {
2528 setState(EXECUTING_TO_IDLE);
2529
Andreas Huber127fcdc2009-08-26 16:27:02 -07002530 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002531 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002532 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002533
Andreas Huber127fcdc2009-08-26 16:27:02 -07002534 bool emulateInputFlushCompletion =
2535 !flushPortAsync(kPortIndexInput);
2536
2537 bool emulateOutputFlushCompletion =
2538 !flushPortAsync(kPortIndexOutput);
2539
2540 if (emulateInputFlushCompletion) {
2541 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2542 }
2543
2544 if (emulateOutputFlushCompletion) {
2545 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2546 }
2547 } else {
2548 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2549 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2550
2551 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002552 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002553 CHECK_EQ(err, OK);
2554 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002555
2556 while (mState != LOADED && mState != ERROR) {
2557 mAsyncCompletion.wait(mLock);
2558 }
2559
2560 break;
2561 }
2562
2563 default:
2564 {
2565 CHECK(!"should not be here.");
2566 break;
2567 }
2568 }
2569
Andreas Hubera4357ad2010-04-02 12:49:54 -07002570 if (mLeftOverBuffer) {
2571 mLeftOverBuffer->release();
2572 mLeftOverBuffer = NULL;
2573 }
2574
Andreas Huberbe06d262009-08-14 14:37:10 -07002575 mSource->stop();
2576
Andreas Huber4a9375e2010-02-09 11:54:33 -08002577 CODEC_LOGV("stopped");
2578
Andreas Huberbe06d262009-08-14 14:37:10 -07002579 return OK;
2580}
2581
2582sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002583 Mutex::Autolock autoLock(mLock);
2584
Andreas Huberbe06d262009-08-14 14:37:10 -07002585 return mOutputFormat;
2586}
2587
2588status_t OMXCodec::read(
2589 MediaBuffer **buffer, const ReadOptions *options) {
2590 *buffer = NULL;
2591
2592 Mutex::Autolock autoLock(mLock);
2593
Andreas Huberd06e5b82009-08-28 13:18:14 -07002594 if (mState != EXECUTING && mState != RECONFIGURING) {
2595 return UNKNOWN_ERROR;
2596 }
2597
Andreas Hubere981c332009-10-22 13:49:30 -07002598 bool seeking = false;
2599 int64_t seekTimeUs;
2600 if (options && options->getSeekTo(&seekTimeUs)) {
2601 seeking = true;
2602 }
2603
Andreas Huber42978e52009-08-27 10:08:39 -07002604 if (mInitialBufferSubmit) {
2605 mInitialBufferSubmit = false;
2606
Andreas Hubere981c332009-10-22 13:49:30 -07002607 if (seeking) {
2608 CHECK(seekTimeUs >= 0);
2609 mSeekTimeUs = seekTimeUs;
2610
2611 // There's no reason to trigger the code below, there's
2612 // nothing to flush yet.
2613 seeking = false;
2614 }
2615
Andreas Huber42978e52009-08-27 10:08:39 -07002616 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002617
Andreas Huberd06e5b82009-08-28 13:18:14 -07002618 if (mState == EXECUTING) {
2619 // Otherwise mState == RECONFIGURING and this code will trigger
2620 // after the output port is reenabled.
2621 fillOutputBuffers();
2622 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002623 }
2624
Andreas Hubere981c332009-10-22 13:49:30 -07002625 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002626 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002627
2628 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002629
2630 CHECK(seekTimeUs >= 0);
2631 mSeekTimeUs = seekTimeUs;
2632
2633 mFilledBuffers.clear();
2634
2635 CHECK_EQ(mState, EXECUTING);
2636
Andreas Huber404cc412009-08-25 14:26:05 -07002637 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2638 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2639
2640 if (emulateInputFlushCompletion) {
2641 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2642 }
2643
2644 if (emulateOutputFlushCompletion) {
2645 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2646 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002647
2648 while (mSeekTimeUs >= 0) {
2649 mBufferFilled.wait(mLock);
2650 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002651 }
2652
2653 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2654 mBufferFilled.wait(mLock);
2655 }
2656
2657 if (mState == ERROR) {
2658 return UNKNOWN_ERROR;
2659 }
2660
2661 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002662 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002663 }
2664
Andreas Hubercfd55572009-10-09 14:11:28 -07002665 if (mOutputPortSettingsHaveChanged) {
2666 mOutputPortSettingsHaveChanged = false;
2667
2668 return INFO_FORMAT_CHANGED;
2669 }
2670
Andreas Huberbe06d262009-08-14 14:37:10 -07002671 size_t index = *mFilledBuffers.begin();
2672 mFilledBuffers.erase(mFilledBuffers.begin());
2673
2674 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2675 info->mMediaBuffer->add_ref();
2676 *buffer = info->mMediaBuffer;
2677
2678 return OK;
2679}
2680
2681void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2682 Mutex::Autolock autoLock(mLock);
2683
2684 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2685 for (size_t i = 0; i < buffers->size(); ++i) {
2686 BufferInfo *info = &buffers->editItemAt(i);
2687
2688 if (info->mMediaBuffer == buffer) {
2689 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2690 fillOutputBuffer(info);
2691 return;
2692 }
2693 }
2694
2695 CHECK(!"should not be here.");
2696}
2697
2698static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2699 static const char *kNames[] = {
2700 "OMX_IMAGE_CodingUnused",
2701 "OMX_IMAGE_CodingAutoDetect",
2702 "OMX_IMAGE_CodingJPEG",
2703 "OMX_IMAGE_CodingJPEG2K",
2704 "OMX_IMAGE_CodingEXIF",
2705 "OMX_IMAGE_CodingTIFF",
2706 "OMX_IMAGE_CodingGIF",
2707 "OMX_IMAGE_CodingPNG",
2708 "OMX_IMAGE_CodingLZW",
2709 "OMX_IMAGE_CodingBMP",
2710 };
2711
2712 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2713
2714 if (type < 0 || (size_t)type >= numNames) {
2715 return "UNKNOWN";
2716 } else {
2717 return kNames[type];
2718 }
2719}
2720
2721static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2722 static const char *kNames[] = {
2723 "OMX_COLOR_FormatUnused",
2724 "OMX_COLOR_FormatMonochrome",
2725 "OMX_COLOR_Format8bitRGB332",
2726 "OMX_COLOR_Format12bitRGB444",
2727 "OMX_COLOR_Format16bitARGB4444",
2728 "OMX_COLOR_Format16bitARGB1555",
2729 "OMX_COLOR_Format16bitRGB565",
2730 "OMX_COLOR_Format16bitBGR565",
2731 "OMX_COLOR_Format18bitRGB666",
2732 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002733 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002734 "OMX_COLOR_Format24bitRGB888",
2735 "OMX_COLOR_Format24bitBGR888",
2736 "OMX_COLOR_Format24bitARGB1887",
2737 "OMX_COLOR_Format25bitARGB1888",
2738 "OMX_COLOR_Format32bitBGRA8888",
2739 "OMX_COLOR_Format32bitARGB8888",
2740 "OMX_COLOR_FormatYUV411Planar",
2741 "OMX_COLOR_FormatYUV411PackedPlanar",
2742 "OMX_COLOR_FormatYUV420Planar",
2743 "OMX_COLOR_FormatYUV420PackedPlanar",
2744 "OMX_COLOR_FormatYUV420SemiPlanar",
2745 "OMX_COLOR_FormatYUV422Planar",
2746 "OMX_COLOR_FormatYUV422PackedPlanar",
2747 "OMX_COLOR_FormatYUV422SemiPlanar",
2748 "OMX_COLOR_FormatYCbYCr",
2749 "OMX_COLOR_FormatYCrYCb",
2750 "OMX_COLOR_FormatCbYCrY",
2751 "OMX_COLOR_FormatCrYCbY",
2752 "OMX_COLOR_FormatYUV444Interleaved",
2753 "OMX_COLOR_FormatRawBayer8bit",
2754 "OMX_COLOR_FormatRawBayer10bit",
2755 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002756 "OMX_COLOR_FormatL2",
2757 "OMX_COLOR_FormatL4",
2758 "OMX_COLOR_FormatL8",
2759 "OMX_COLOR_FormatL16",
2760 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002761 "OMX_COLOR_FormatL32",
2762 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2763 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2764 "OMX_COLOR_Format18BitBGR666",
2765 "OMX_COLOR_Format24BitARGB6666",
2766 "OMX_COLOR_Format24BitABGR6666",
2767 };
2768
2769 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2770
Andreas Huberbe06d262009-08-14 14:37:10 -07002771 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2772 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2773 } else if (type < 0 || (size_t)type >= numNames) {
2774 return "UNKNOWN";
2775 } else {
2776 return kNames[type];
2777 }
2778}
2779
2780static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2781 static const char *kNames[] = {
2782 "OMX_VIDEO_CodingUnused",
2783 "OMX_VIDEO_CodingAutoDetect",
2784 "OMX_VIDEO_CodingMPEG2",
2785 "OMX_VIDEO_CodingH263",
2786 "OMX_VIDEO_CodingMPEG4",
2787 "OMX_VIDEO_CodingWMV",
2788 "OMX_VIDEO_CodingRV",
2789 "OMX_VIDEO_CodingAVC",
2790 "OMX_VIDEO_CodingMJPEG",
2791 };
2792
2793 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2794
2795 if (type < 0 || (size_t)type >= numNames) {
2796 return "UNKNOWN";
2797 } else {
2798 return kNames[type];
2799 }
2800}
2801
2802static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2803 static const char *kNames[] = {
2804 "OMX_AUDIO_CodingUnused",
2805 "OMX_AUDIO_CodingAutoDetect",
2806 "OMX_AUDIO_CodingPCM",
2807 "OMX_AUDIO_CodingADPCM",
2808 "OMX_AUDIO_CodingAMR",
2809 "OMX_AUDIO_CodingGSMFR",
2810 "OMX_AUDIO_CodingGSMEFR",
2811 "OMX_AUDIO_CodingGSMHR",
2812 "OMX_AUDIO_CodingPDCFR",
2813 "OMX_AUDIO_CodingPDCEFR",
2814 "OMX_AUDIO_CodingPDCHR",
2815 "OMX_AUDIO_CodingTDMAFR",
2816 "OMX_AUDIO_CodingTDMAEFR",
2817 "OMX_AUDIO_CodingQCELP8",
2818 "OMX_AUDIO_CodingQCELP13",
2819 "OMX_AUDIO_CodingEVRC",
2820 "OMX_AUDIO_CodingSMV",
2821 "OMX_AUDIO_CodingG711",
2822 "OMX_AUDIO_CodingG723",
2823 "OMX_AUDIO_CodingG726",
2824 "OMX_AUDIO_CodingG729",
2825 "OMX_AUDIO_CodingAAC",
2826 "OMX_AUDIO_CodingMP3",
2827 "OMX_AUDIO_CodingSBC",
2828 "OMX_AUDIO_CodingVORBIS",
2829 "OMX_AUDIO_CodingWMA",
2830 "OMX_AUDIO_CodingRA",
2831 "OMX_AUDIO_CodingMIDI",
2832 };
2833
2834 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2835
2836 if (type < 0 || (size_t)type >= numNames) {
2837 return "UNKNOWN";
2838 } else {
2839 return kNames[type];
2840 }
2841}
2842
2843static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2844 static const char *kNames[] = {
2845 "OMX_AUDIO_PCMModeLinear",
2846 "OMX_AUDIO_PCMModeALaw",
2847 "OMX_AUDIO_PCMModeMULaw",
2848 };
2849
2850 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2851
2852 if (type < 0 || (size_t)type >= numNames) {
2853 return "UNKNOWN";
2854 } else {
2855 return kNames[type];
2856 }
2857}
2858
Andreas Huber7ae02c82009-09-09 16:29:47 -07002859static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2860 static const char *kNames[] = {
2861 "OMX_AUDIO_AMRBandModeUnused",
2862 "OMX_AUDIO_AMRBandModeNB0",
2863 "OMX_AUDIO_AMRBandModeNB1",
2864 "OMX_AUDIO_AMRBandModeNB2",
2865 "OMX_AUDIO_AMRBandModeNB3",
2866 "OMX_AUDIO_AMRBandModeNB4",
2867 "OMX_AUDIO_AMRBandModeNB5",
2868 "OMX_AUDIO_AMRBandModeNB6",
2869 "OMX_AUDIO_AMRBandModeNB7",
2870 "OMX_AUDIO_AMRBandModeWB0",
2871 "OMX_AUDIO_AMRBandModeWB1",
2872 "OMX_AUDIO_AMRBandModeWB2",
2873 "OMX_AUDIO_AMRBandModeWB3",
2874 "OMX_AUDIO_AMRBandModeWB4",
2875 "OMX_AUDIO_AMRBandModeWB5",
2876 "OMX_AUDIO_AMRBandModeWB6",
2877 "OMX_AUDIO_AMRBandModeWB7",
2878 "OMX_AUDIO_AMRBandModeWB8",
2879 };
2880
2881 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2882
2883 if (type < 0 || (size_t)type >= numNames) {
2884 return "UNKNOWN";
2885 } else {
2886 return kNames[type];
2887 }
2888}
2889
2890static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2891 static const char *kNames[] = {
2892 "OMX_AUDIO_AMRFrameFormatConformance",
2893 "OMX_AUDIO_AMRFrameFormatIF1",
2894 "OMX_AUDIO_AMRFrameFormatIF2",
2895 "OMX_AUDIO_AMRFrameFormatFSF",
2896 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2897 "OMX_AUDIO_AMRFrameFormatITU",
2898 };
2899
2900 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2901
2902 if (type < 0 || (size_t)type >= numNames) {
2903 return "UNKNOWN";
2904 } else {
2905 return kNames[type];
2906 }
2907}
Andreas Huberbe06d262009-08-14 14:37:10 -07002908
2909void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2910 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002911 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002912 def.nPortIndex = portIndex;
2913
Andreas Huber784202e2009-10-15 13:46:54 -07002914 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002915 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2916 CHECK_EQ(err, OK);
2917
2918 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2919
2920 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2921 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2922
2923 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2924 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2925 printf(" nBufferSize = %ld\n", def.nBufferSize);
2926
2927 switch (def.eDomain) {
2928 case OMX_PortDomainImage:
2929 {
2930 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2931
2932 printf("\n");
2933 printf(" // Image\n");
2934 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2935 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2936 printf(" nStride = %ld\n", imageDef->nStride);
2937
2938 printf(" eCompressionFormat = %s\n",
2939 imageCompressionFormatString(imageDef->eCompressionFormat));
2940
2941 printf(" eColorFormat = %s\n",
2942 colorFormatString(imageDef->eColorFormat));
2943
2944 break;
2945 }
2946
2947 case OMX_PortDomainVideo:
2948 {
2949 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2950
2951 printf("\n");
2952 printf(" // Video\n");
2953 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
2954 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
2955 printf(" nStride = %ld\n", videoDef->nStride);
2956
2957 printf(" eCompressionFormat = %s\n",
2958 videoCompressionFormatString(videoDef->eCompressionFormat));
2959
2960 printf(" eColorFormat = %s\n",
2961 colorFormatString(videoDef->eColorFormat));
2962
2963 break;
2964 }
2965
2966 case OMX_PortDomainAudio:
2967 {
2968 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2969
2970 printf("\n");
2971 printf(" // Audio\n");
2972 printf(" eEncoding = %s\n",
2973 audioCodingTypeString(audioDef->eEncoding));
2974
2975 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2976 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07002977 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07002978 params.nPortIndex = portIndex;
2979
Andreas Huber784202e2009-10-15 13:46:54 -07002980 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002981 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2982 CHECK_EQ(err, OK);
2983
2984 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
2985 printf(" nChannels = %ld\n", params.nChannels);
2986 printf(" bInterleaved = %d\n", params.bInterleaved);
2987 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
2988
2989 printf(" eNumData = %s\n",
2990 params.eNumData == OMX_NumericalDataSigned
2991 ? "signed" : "unsigned");
2992
2993 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07002994 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
2995 OMX_AUDIO_PARAM_AMRTYPE amr;
2996 InitOMXParams(&amr);
2997 amr.nPortIndex = portIndex;
2998
Andreas Huber784202e2009-10-15 13:46:54 -07002999 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003000 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3001 CHECK_EQ(err, OK);
3002
3003 printf(" nChannels = %ld\n", amr.nChannels);
3004 printf(" eAMRBandMode = %s\n",
3005 amrBandModeString(amr.eAMRBandMode));
3006 printf(" eAMRFrameFormat = %s\n",
3007 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003008 }
3009
3010 break;
3011 }
3012
3013 default:
3014 {
3015 printf(" // Unknown\n");
3016 break;
3017 }
3018 }
3019
3020 printf("}\n");
3021}
3022
3023void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3024 mOutputFormat = new MetaData;
3025 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
3026
3027 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003028 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003029 def.nPortIndex = kPortIndexOutput;
3030
Andreas Huber784202e2009-10-15 13:46:54 -07003031 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003032 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3033 CHECK_EQ(err, OK);
3034
3035 switch (def.eDomain) {
3036 case OMX_PortDomainImage:
3037 {
3038 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3039 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3040
Andreas Hubere6c40962009-09-10 14:13:30 -07003041 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003042 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3043 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3044 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3045 break;
3046 }
3047
3048 case OMX_PortDomainAudio:
3049 {
3050 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3051
Andreas Huberda050cf22009-09-02 14:01:43 -07003052 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3053 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003054 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003055 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003056
Andreas Huber784202e2009-10-15 13:46:54 -07003057 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003058 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3059 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003060
Andreas Huberda050cf22009-09-02 14:01:43 -07003061 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3062 CHECK_EQ(params.nBitPerSample, 16);
3063 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003064
Andreas Huberda050cf22009-09-02 14:01:43 -07003065 int32_t numChannels, sampleRate;
3066 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3067 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003068
Andreas Huberda050cf22009-09-02 14:01:43 -07003069 if ((OMX_U32)numChannels != params.nChannels) {
3070 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003071 "the input stream contains (contains %d channels, "
3072 "codec outputs %ld channels).",
3073 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003074 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003075
Andreas Hubere6c40962009-09-10 14:13:30 -07003076 mOutputFormat->setCString(
3077 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003078
3079 // Use the codec-advertised number of channels, as some
3080 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003081 // mono. If we know the codec lies about this information,
3082 // use the actual number of channels instead.
3083 mOutputFormat->setInt32(
3084 kKeyChannelCount,
3085 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3086 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003087
3088 // The codec-reported sampleRate is not reliable...
3089 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3090 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003091 OMX_AUDIO_PARAM_AMRTYPE amr;
3092 InitOMXParams(&amr);
3093 amr.nPortIndex = kPortIndexOutput;
3094
Andreas Huber784202e2009-10-15 13:46:54 -07003095 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003096 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3097 CHECK_EQ(err, OK);
3098
3099 CHECK_EQ(amr.nChannels, 1);
3100 mOutputFormat->setInt32(kKeyChannelCount, 1);
3101
3102 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3103 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003104 mOutputFormat->setCString(
3105 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003106 mOutputFormat->setInt32(kKeySampleRate, 8000);
3107 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3108 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003109 mOutputFormat->setCString(
3110 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003111 mOutputFormat->setInt32(kKeySampleRate, 16000);
3112 } else {
3113 CHECK(!"Unknown AMR band mode.");
3114 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003115 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003116 mOutputFormat->setCString(
3117 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003118 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003119 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3120 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003121 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003122 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3123 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003124 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003125 } else {
3126 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003127 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003128 break;
3129 }
3130
3131 case OMX_PortDomainVideo:
3132 {
3133 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3134
3135 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003136 mOutputFormat->setCString(
3137 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003138 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003139 mOutputFormat->setCString(
3140 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003141 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003142 mOutputFormat->setCString(
3143 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003144 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003145 mOutputFormat->setCString(
3146 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003147 } else {
3148 CHECK(!"Unknown compression format.");
3149 }
3150
3151 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3152 // This component appears to be lying to me.
3153 mOutputFormat->setInt32(
3154 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3155 mOutputFormat->setInt32(
3156 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3157 } else {
3158 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3159 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3160 }
3161
3162 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3163 break;
3164 }
3165
3166 default:
3167 {
3168 CHECK(!"should not be here, neither audio nor video.");
3169 break;
3170 }
3171 }
3172}
3173
Andreas Hubere6c40962009-09-10 14:13:30 -07003174////////////////////////////////////////////////////////////////////////////////
3175
3176status_t QueryCodecs(
3177 const sp<IOMX> &omx,
3178 const char *mime, bool queryDecoders,
3179 Vector<CodecCapabilities> *results) {
3180 results->clear();
3181
3182 for (int index = 0;; ++index) {
3183 const char *componentName;
3184
3185 if (!queryDecoders) {
3186 componentName = GetCodec(
3187 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3188 mime, index);
3189 } else {
3190 componentName = GetCodec(
3191 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3192 mime, index);
3193 }
3194
3195 if (!componentName) {
3196 return OK;
3197 }
3198
Andreas Huber1a189a82010-03-24 13:49:20 -07003199 if (strncmp(componentName, "OMX.", 4)) {
3200 // Not an OpenMax component but a software codec.
3201
3202 results->push();
3203 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3204 caps->mComponentName = componentName;
3205
3206 continue;
3207 }
3208
Andreas Huber784202e2009-10-15 13:46:54 -07003209 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003210 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003211 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003212
3213 if (err != OK) {
3214 continue;
3215 }
3216
James Dong722d5912010-04-13 10:56:59 -07003217 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003218
3219 results->push();
3220 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3221 caps->mComponentName = componentName;
3222
3223 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3224 InitOMXParams(&param);
3225
3226 param.nPortIndex = queryDecoders ? 0 : 1;
3227
3228 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003229 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003230 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3231 &param, sizeof(param));
3232
3233 if (err != OK) {
3234 break;
3235 }
3236
3237 CodecProfileLevel profileLevel;
3238 profileLevel.mProfile = param.eProfile;
3239 profileLevel.mLevel = param.eLevel;
3240
3241 caps->mProfileLevels.push(profileLevel);
3242 }
3243
Andreas Huber784202e2009-10-15 13:46:54 -07003244 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003245 }
3246}
3247
Andreas Huberbe06d262009-08-14 14:37:10 -07003248} // namespace android