blob: ba357486c952c1ada9e857443c6dcb949873d218 [file] [log] [blame]
Andreas Huberbe06d262009-08-14 14:37:10 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "OMXCodec"
19#include <utils/Log.h>
20
Andreas Huberdacaa732009-12-07 09:56:32 -080021#include "include/AACDecoder.h"
James Dong17299ab2010-05-14 15:45:22 -070022#include "include/AACEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080023#include "include/AMRNBDecoder.h"
Andreas Huberd49b526dd2009-12-11 15:07:25 -080024#include "include/AMRNBEncoder.h"
Andreas Hubera30d4002009-12-08 15:40:06 -080025#include "include/AMRWBDecoder.h"
James Dong17299ab2010-05-14 15:45:22 -070026#include "include/AMRWBEncoder.h"
Andreas Huber4a0ec3f2009-12-10 09:44:29 -080027#include "include/AVCDecoder.h"
James Dong02f5b542009-12-15 16:26:55 -080028#include "include/M4vH263Decoder.h"
Andreas Huber250f2432009-12-07 14:22:35 -080029#include "include/MP3Decoder.h"
Andreas Huber388379f2010-05-07 10:35:13 -070030#include "include/VorbisDecoder.h"
Andreas Huber47ba30e2010-05-24 14:38:02 -070031#include "include/VPXDecoder.h"
Andreas Huber8c7ab032009-12-07 11:23:44 -080032
Andreas Huberbd7b43b2009-10-13 10:22:55 -070033#include "include/ESDS.h"
34
Andreas Huberbe06d262009-08-14 14:37:10 -070035#include <binder/IServiceManager.h>
36#include <binder/MemoryDealer.h>
37#include <binder/ProcessState.h>
38#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070039#include <media/stagefright/MediaBuffer.h>
40#include <media/stagefright/MediaBufferGroup.h>
41#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070042#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070043#include <media/stagefright/MediaExtractor.h>
44#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070045#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070046#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070047#include <utils/Vector.h>
48
49#include <OMX_Audio.h>
50#include <OMX_Component.h>
51
52namespace android {
53
Andreas Huber8b432b12009-10-07 13:36:52 -070054static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
55
Andreas Huberbe06d262009-08-14 14:37:10 -070056struct CodecInfo {
57 const char *mime;
58 const char *codec;
59};
60
Andreas Huberfb1c2f82009-12-15 13:25:11 -080061#define FACTORY_CREATE(name) \
62static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
63 return new name(source); \
64}
65
James Dong17299ab2010-05-14 15:45:22 -070066#define FACTORY_CREATE_ENCODER(name) \
67static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
68 return new name(source, meta); \
69}
70
Andreas Huberfb1c2f82009-12-15 13:25:11 -080071#define FACTORY_REF(name) { #name, Make##name },
72
73FACTORY_CREATE(MP3Decoder)
74FACTORY_CREATE(AMRNBDecoder)
75FACTORY_CREATE(AMRWBDecoder)
76FACTORY_CREATE(AACDecoder)
77FACTORY_CREATE(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -080078FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070079FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070080FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070081FACTORY_CREATE_ENCODER(AMRNBEncoder)
82FACTORY_CREATE_ENCODER(AMRWBEncoder)
83FACTORY_CREATE_ENCODER(AACEncoder)
84
85static sp<MediaSource> InstantiateSoftwareEncoder(
86 const char *name, const sp<MediaSource> &source,
87 const sp<MetaData> &meta) {
88 struct FactoryInfo {
89 const char *name;
90 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
91 };
92
93 static const FactoryInfo kFactoryInfo[] = {
94 FACTORY_REF(AMRNBEncoder)
95 FACTORY_REF(AMRWBEncoder)
96 FACTORY_REF(AACEncoder)
97 };
98 for (size_t i = 0;
99 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
100 if (!strcmp(name, kFactoryInfo[i].name)) {
101 return (*kFactoryInfo[i].CreateFunc)(source, meta);
102 }
103 }
104
105 return NULL;
106}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800107
108static sp<MediaSource> InstantiateSoftwareCodec(
109 const char *name, const sp<MediaSource> &source) {
110 struct FactoryInfo {
111 const char *name;
112 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
113 };
114
115 static const FactoryInfo kFactoryInfo[] = {
116 FACTORY_REF(MP3Decoder)
117 FACTORY_REF(AMRNBDecoder)
118 FACTORY_REF(AMRWBDecoder)
119 FACTORY_REF(AACDecoder)
120 FACTORY_REF(AVCDecoder)
James Dong02f5b542009-12-15 16:26:55 -0800121 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700122 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700123 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800124 };
125 for (size_t i = 0;
126 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
127 if (!strcmp(name, kFactoryInfo[i].name)) {
128 return (*kFactoryInfo[i].CreateFunc)(source);
129 }
130 }
131
132 return NULL;
133}
134
135#undef FACTORY_REF
136#undef FACTORY_CREATE
137
Andreas Huberbe06d262009-08-14 14:37:10 -0700138static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700139 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
James Dong374aee62010-04-26 10:23:30 -0700140// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800141 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700142// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700143// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800144 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700145// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700146 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800147 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700148// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700149 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800150 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700151// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700152 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
153 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800154 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700155// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700156 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800157 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700158// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700159 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
160 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800161 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700162// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huber388379f2010-05-07 10:35:13 -0700163 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700164 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700165};
166
167static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700168 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800169 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700170 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700171 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700172 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700173 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700174 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
175 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
176 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
177 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
178 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
179 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
180 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700181 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700182 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
183 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700184};
185
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800186#undef OPTIONAL
187
Andreas Hubere0873732009-09-10 09:57:53 -0700188#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700189#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800190#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700191
Andreas Huberbe06d262009-08-14 14:37:10 -0700192struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700193 OMXCodecObserver() {
194 }
195
196 void setCodec(const sp<OMXCodec> &target) {
197 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700198 }
199
200 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700201 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700202 sp<OMXCodec> codec = mTarget.promote();
203
204 if (codec.get() != NULL) {
205 codec->on_message(msg);
206 }
207 }
208
209protected:
210 virtual ~OMXCodecObserver() {}
211
212private:
213 wp<OMXCodec> mTarget;
214
215 OMXCodecObserver(const OMXCodecObserver &);
216 OMXCodecObserver &operator=(const OMXCodecObserver &);
217};
218
219static const char *GetCodec(const CodecInfo *info, size_t numInfos,
220 const char *mime, int index) {
221 CHECK(index >= 0);
222 for(size_t i = 0; i < numInfos; ++i) {
223 if (!strcasecmp(mime, info[i].mime)) {
224 if (index == 0) {
225 return info[i].codec;
226 }
227
228 --index;
229 }
230 }
231
232 return NULL;
233}
234
Andreas Huberebf66ea2009-08-19 13:32:58 -0700235enum {
236 kAVCProfileBaseline = 0x42,
237 kAVCProfileMain = 0x4d,
238 kAVCProfileExtended = 0x58,
239 kAVCProfileHigh = 0x64,
240 kAVCProfileHigh10 = 0x6e,
241 kAVCProfileHigh422 = 0x7a,
242 kAVCProfileHigh444 = 0xf4,
243 kAVCProfileCAVLC444Intra = 0x2c
244};
245
246static const char *AVCProfileToString(uint8_t profile) {
247 switch (profile) {
248 case kAVCProfileBaseline:
249 return "Baseline";
250 case kAVCProfileMain:
251 return "Main";
252 case kAVCProfileExtended:
253 return "Extended";
254 case kAVCProfileHigh:
255 return "High";
256 case kAVCProfileHigh10:
257 return "High 10";
258 case kAVCProfileHigh422:
259 return "High 422";
260 case kAVCProfileHigh444:
261 return "High 444";
262 case kAVCProfileCAVLC444Intra:
263 return "CAVLC 444 Intra";
264 default: return "Unknown";
265 }
266}
267
Andreas Huber4c483422009-09-02 16:05:36 -0700268template<class T>
269static void InitOMXParams(T *params) {
270 params->nSize = sizeof(T);
271 params->nVersion.s.nVersionMajor = 1;
272 params->nVersion.s.nVersionMinor = 0;
273 params->nVersion.s.nRevision = 0;
274 params->nVersion.s.nStep = 0;
275}
276
Andreas Hubere13526a2009-10-22 10:43:34 -0700277static bool IsSoftwareCodec(const char *componentName) {
278 if (!strncmp("OMX.PV.", componentName, 7)) {
279 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700280 }
281
Andreas Hubere13526a2009-10-22 10:43:34 -0700282 return false;
283}
284
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800285// A sort order in which non-OMX components are first,
286// followed by software codecs, i.e. OMX.PV.*, followed
287// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700288static int CompareSoftwareCodecsFirst(
289 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800290 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
291 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
292
293 if (isNotOMX1) {
294 if (isNotOMX2) { return 0; }
295 return -1;
296 }
297 if (isNotOMX2) {
298 return 1;
299 }
300
Andreas Hubere13526a2009-10-22 10:43:34 -0700301 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
302 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
303
304 if (isSoftwareCodec1) {
305 if (isSoftwareCodec2) { return 0; }
306 return -1;
307 }
308
309 if (isSoftwareCodec2) {
310 return 1;
311 }
312
313 return 0;
314}
315
316// static
317uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700318 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700319
Andreas Huberbe06d262009-08-14 14:37:10 -0700320 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700321 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700322 }
323 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
324 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800325 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700326 }
327 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
328 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700329 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700330 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700331 }
332 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
333 quirks |= kRequiresLoadedToIdleAfterAllocation;
334 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700335 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huberbe06d262009-08-14 14:37:10 -0700336 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700337 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700338 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800339 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700340 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700341
Andreas Huber2dc64d82009-09-11 12:58:53 -0700342 if (!strncmp(componentName, "OMX.TI.", 7)) {
343 // Apparently I must not use OMX_UseBuffer on either input or
344 // output ports on any of the TI components or quote:
345 // "(I) may have unexpected problem (sic) which can be timing related
346 // and hard to reproduce."
347
348 quirks |= kRequiresAllocateBufferOnInputPorts;
349 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dong4f501f02010-06-07 14:41:41 -0700350 if (!strncmp(componentName, "OMX.TI.video.encoder", 20)) {
351 quirks |= kAvoidMemcopyInputRecordingFrames;
352 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700353 }
354
Andreas Huberb8de9572010-02-22 14:58:45 -0800355 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
356 quirks |= kInputBufferSizesAreBogus;
357 }
358
Andreas Hubere13526a2009-10-22 10:43:34 -0700359 return quirks;
360}
361
362// static
363void OMXCodec::findMatchingCodecs(
364 const char *mime,
365 bool createEncoder, const char *matchComponentName,
366 uint32_t flags,
367 Vector<String8> *matchingCodecs) {
368 matchingCodecs->clear();
369
370 for (int index = 0;; ++index) {
371 const char *componentName;
372
373 if (createEncoder) {
374 componentName = GetCodec(
375 kEncoderInfo,
376 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
377 mime, index);
378 } else {
379 componentName = GetCodec(
380 kDecoderInfo,
381 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
382 mime, index);
383 }
384
385 if (!componentName) {
386 break;
387 }
388
389 // If a specific codec is requested, skip the non-matching ones.
390 if (matchComponentName && strcmp(componentName, matchComponentName)) {
391 continue;
392 }
393
394 matchingCodecs->push(String8(componentName));
395 }
396
397 if (flags & kPreferSoftwareCodecs) {
398 matchingCodecs->sort(CompareSoftwareCodecsFirst);
399 }
400}
401
402// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800403sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700404 const sp<IOMX> &omx,
405 const sp<MetaData> &meta, bool createEncoder,
406 const sp<MediaSource> &source,
407 const char *matchComponentName,
408 uint32_t flags) {
409 const char *mime;
410 bool success = meta->findCString(kKeyMIMEType, &mime);
411 CHECK(success);
412
413 Vector<String8> matchingCodecs;
414 findMatchingCodecs(
415 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
416
417 if (matchingCodecs.isEmpty()) {
418 return NULL;
419 }
420
421 sp<OMXCodecObserver> observer = new OMXCodecObserver;
422 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700423
424 const char *componentName;
425 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
426 componentName = matchingCodecs[i].string();
427
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800428#if BUILD_WITH_FULL_STAGEFRIGHT
James Dong17299ab2010-05-14 15:45:22 -0700429 sp<MediaSource> softwareCodec = createEncoder?
430 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800431 InstantiateSoftwareCodec(componentName, source);
432
433 if (softwareCodec != NULL) {
434 LOGV("Successfully allocated software codec '%s'", componentName);
435
436 return softwareCodec;
437 }
438#endif
439
Andreas Hubere13526a2009-10-22 10:43:34 -0700440 LOGV("Attempting to allocate OMX node '%s'", componentName);
441
442 status_t err = omx->allocateNode(componentName, observer, &node);
443 if (err == OK) {
444 LOGV("Successfully allocated OMX node '%s'", componentName);
445
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700446 sp<OMXCodec> codec = new OMXCodec(
447 omx, node, getComponentQuirks(componentName),
448 createEncoder, mime, componentName,
449 source);
450
451 observer->setCodec(codec);
452
453 err = codec->configureCodec(meta);
454
455 if (err == OK) {
456 return codec;
457 }
458
459 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700460 }
461 }
462
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700463 return NULL;
464}
Andreas Hubere13526a2009-10-22 10:43:34 -0700465
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700466status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700467 uint32_t type;
468 const void *data;
469 size_t size;
470 if (meta->findData(kKeyESDS, &type, &data, &size)) {
471 ESDS esds((const char *)data, size);
472 CHECK_EQ(esds.InitCheck(), OK);
473
474 const void *codec_specific_data;
475 size_t codec_specific_data_size;
476 esds.getCodecSpecificInfo(
477 &codec_specific_data, &codec_specific_data_size);
478
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700479 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700480 codec_specific_data, codec_specific_data_size);
481 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700482 // Parse the AVCDecoderConfigurationRecord
483
484 const uint8_t *ptr = (const uint8_t *)data;
485
486 CHECK(size >= 7);
487 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
488 uint8_t profile = ptr[1];
489 uint8_t level = ptr[3];
490
Andreas Huber44e15c42009-11-23 14:39:38 -0800491 // There is decodable content out there that fails the following
492 // assertion, let's be lenient for now...
493 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700494
495 size_t lengthSize = 1 + (ptr[4] & 3);
496
497 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
498 // violates it...
499 // CHECK((ptr[5] >> 5) == 7); // reserved
500
501 size_t numSeqParameterSets = ptr[5] & 31;
502
503 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700504 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700505
506 for (size_t i = 0; i < numSeqParameterSets; ++i) {
507 CHECK(size >= 2);
508 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700509
510 ptr += 2;
511 size -= 2;
512
Andreas Huberbe06d262009-08-14 14:37:10 -0700513 CHECK(size >= length);
514
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700515 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700516
517 ptr += length;
518 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700519 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700520
Andreas Huberebf66ea2009-08-19 13:32:58 -0700521 CHECK(size >= 1);
522 size_t numPictureParameterSets = *ptr;
523 ++ptr;
524 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700525
Andreas Huberebf66ea2009-08-19 13:32:58 -0700526 for (size_t i = 0; i < numPictureParameterSets; ++i) {
527 CHECK(size >= 2);
528 size_t length = U16_AT(ptr);
529
530 ptr += 2;
531 size -= 2;
532
533 CHECK(size >= length);
534
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700535 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700536
537 ptr += length;
538 size -= length;
539 }
540
Andreas Huber53a76bd2009-10-06 16:20:44 -0700541 LOGV("AVC profile = %d (%s), level = %d",
Andreas Huberebf66ea2009-08-19 13:32:58 -0700542 (int)profile, AVCProfileToString(profile), (int)level / 10);
543
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700544 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Huberebf66ea2009-08-19 13:32:58 -0700545 && (profile != kAVCProfileBaseline || level > 39)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700546 // This stream exceeds the decoder's capabilities. The decoder
547 // does not handle this gracefully and would clobber the heap
548 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700549
550 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700551 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700552 }
553 }
554
James Dong17299ab2010-05-14 15:45:22 -0700555 int32_t bitRate = 0;
556 if (mIsEncoder) {
557 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
558 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700559 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700560 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700561 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700562 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700563 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700564 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700565 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700566 int32_t numChannels, sampleRate;
567 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
568 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
569
James Dong17299ab2010-05-14 15:45:22 -0700570 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700571 }
James Dongabed93a2010-04-22 17:27:04 -0700572
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700573 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700574
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700575 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700576 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700577 } else {
James Dong1244eab2010-06-08 11:58:53 -0700578 int32_t width, height;
579 bool success = meta->findInt32(kKeyWidth, &width);
580 success = success && meta->findInt32(kKeyHeight, &height);
581 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700582 status_t err = setVideoOutputFormat(
583 mMIME, width, height);
584
585 if (err != OK) {
586 return err;
587 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700588 }
589 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700590
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700591 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
592 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700593 OMX_COLOR_FORMATTYPE format =
594 OMX_COLOR_Format32bitARGB8888;
595 // OMX_COLOR_FormatYUV420PackedPlanar;
596 // OMX_COLOR_FormatCbYCrY;
597 // OMX_COLOR_FormatYUV411Planar;
598
599 int32_t width, height;
600 bool success = meta->findInt32(kKeyWidth, &width);
601 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700602
603 int32_t compressedSize;
604 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700605 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700606
607 CHECK(success);
608 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700609
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700610 setImageOutputFormat(format, width, height);
611 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700612 }
613
Andreas Huberda050cf22009-09-02 14:01:43 -0700614 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800615 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700616 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700617 }
618
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700619 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700620 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
621 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700622 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700623 }
624
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700625 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700626
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700627 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700628}
629
Andreas Huberda050cf22009-09-02 14:01:43 -0700630void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
631 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700632 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700633 def.nPortIndex = portIndex;
634
Andreas Huber784202e2009-10-15 13:46:54 -0700635 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700636 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
637 CHECK_EQ(err, OK);
638
Andreas Huberb8de9572010-02-22 14:58:45 -0800639 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
640 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700641 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700642 }
643
Andreas Huber784202e2009-10-15 13:46:54 -0700644 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700645 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
646 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800647
648 err = mOMX->getParameter(
649 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
650 CHECK_EQ(err, OK);
651
652 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800653 if (portIndex == kPortIndexInput
654 && (mQuirks & kInputBufferSizesAreBogus)) {
655 CHECK_EQ(def.nBufferSize, size);
656 } else {
657 CHECK(def.nBufferSize >= size);
658 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700659}
660
Andreas Huberbe06d262009-08-14 14:37:10 -0700661status_t OMXCodec::setVideoPortFormatType(
662 OMX_U32 portIndex,
663 OMX_VIDEO_CODINGTYPE compressionFormat,
664 OMX_COLOR_FORMATTYPE colorFormat) {
665 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700666 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700667 format.nPortIndex = portIndex;
668 format.nIndex = 0;
669 bool found = false;
670
671 OMX_U32 index = 0;
672 for (;;) {
673 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700674 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700675 mNode, OMX_IndexParamVideoPortFormat,
676 &format, sizeof(format));
677
678 if (err != OK) {
679 return err;
680 }
681
682 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700683 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700684
685#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700686 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700687 portIndex,
688 index, format.eCompressionFormat, format.eColorFormat);
689#endif
690
691 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
692 if (portIndex == kPortIndexInput
693 && colorFormat == format.eColorFormat) {
694 // eCompressionFormat does not seem right.
695 found = true;
696 break;
697 }
698 if (portIndex == kPortIndexOutput
699 && compressionFormat == format.eCompressionFormat) {
700 // eColorFormat does not seem right.
701 found = true;
702 break;
703 }
704 }
705
706 if (format.eCompressionFormat == compressionFormat
707 && format.eColorFormat == colorFormat) {
708 found = true;
709 break;
710 }
711
712 ++index;
713 }
714
715 if (!found) {
716 return UNKNOWN_ERROR;
717 }
718
Andreas Huber53a76bd2009-10-06 16:20:44 -0700719 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700720 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700721 mNode, OMX_IndexParamVideoPortFormat,
722 &format, sizeof(format));
723
724 return err;
725}
726
Andreas Huberb482ce82009-10-29 12:02:48 -0700727static size_t getFrameSize(
728 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
729 switch (colorFormat) {
730 case OMX_COLOR_FormatYCbYCr:
731 case OMX_COLOR_FormatCbYCrY:
732 return width * height * 2;
733
Andreas Huber71c27d92010-03-19 11:43:15 -0700734 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700735 case OMX_COLOR_FormatYUV420SemiPlanar:
736 return (width * height * 3) / 2;
737
738 default:
739 CHECK(!"Should not be here. Unsupported color format.");
740 break;
741 }
742}
743
Andreas Huberbe06d262009-08-14 14:37:10 -0700744void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700745 const char *mime, const sp<MetaData>& meta) {
746
747 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
748 bool success = meta->findInt32(kKeyWidth, &width);
749 success = success && meta->findInt32(kKeyHeight, &height);
750 success = success && meta->findInt32(kKeySampleRate, &frameRate);
751 success = success && meta->findInt32(kKeyBitRate, &bitRate);
752 success = success && meta->findInt32(kKeyStride, &stride);
753 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
754 CHECK(success);
755 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700756
757 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700758 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700759 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700760 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700761 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700762 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700763 compressionFormat = OMX_VIDEO_CodingH263;
764 } else {
765 LOGE("Not a supported video mime type: %s", mime);
766 CHECK(!"Should not be here. Not a supported video mime type.");
767 }
768
Andreas Huberea6a38c2009-11-16 15:43:38 -0800769 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
770 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
James Dongabed93a2010-04-22 17:27:04 -0700771 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700772 }
773
James Dongb00e2462010-04-26 17:48:26 -0700774 status_t err;
775 OMX_PARAM_PORTDEFINITIONTYPE def;
776 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
777
778 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700779 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700780 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700781 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700782
James Dongb00e2462010-04-26 17:48:26 -0700783 InitOMXParams(&def);
784 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700785
James Dongb00e2462010-04-26 17:48:26 -0700786 err = mOMX->getParameter(
787 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
788 CHECK_EQ(err, OK);
789
James Dong1244eab2010-06-08 11:58:53 -0700790 def.nBufferSize = getFrameSize(colorFormat,
791 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700792
793 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
794
795 video_def->nFrameWidth = width;
796 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700797 video_def->nStride = stride;
798 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700799 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700800 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
801 video_def->eColorFormat = colorFormat;
802
James Dongb00e2462010-04-26 17:48:26 -0700803 err = mOMX->setParameter(
804 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
805 CHECK_EQ(err, OK);
806
807 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700808 CHECK_EQ(setVideoPortFormatType(
809 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
810 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700811 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700812 def.nPortIndex = kPortIndexOutput;
813
James Dongb00e2462010-04-26 17:48:26 -0700814 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700815 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
816
817 CHECK_EQ(err, OK);
818 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
819
820 video_def->nFrameWidth = width;
821 video_def->nFrameHeight = height;
James Dong4f501f02010-06-07 14:41:41 -0700822 video_def->xFramerate = (frameRate << 16); // Q16 format
823 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700824 video_def->eCompressionFormat = compressionFormat;
825 video_def->eColorFormat = OMX_COLOR_FormatUnused;
826
Andreas Huber784202e2009-10-15 13:46:54 -0700827 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700828 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
829 CHECK_EQ(err, OK);
830
James Dongb00e2462010-04-26 17:48:26 -0700831 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700832 switch (compressionFormat) {
833 case OMX_VIDEO_CodingMPEG4:
834 {
James Dong1244eab2010-06-08 11:58:53 -0700835 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700836 break;
837 }
838
839 case OMX_VIDEO_CodingH263:
840 break;
841
Andreas Huberea6a38c2009-11-16 15:43:38 -0800842 case OMX_VIDEO_CodingAVC:
843 {
James Dong1244eab2010-06-08 11:58:53 -0700844 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -0800845 break;
846 }
847
Andreas Huberb482ce82009-10-29 12:02:48 -0700848 default:
849 CHECK(!"Support for this compressionFormat to be implemented.");
850 break;
851 }
852}
853
James Dong1244eab2010-06-08 11:58:53 -0700854static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
855 if (iFramesInterval < 0) {
856 return 0xFFFFFFFF;
857 } else if (iFramesInterval == 0) {
858 return 0;
859 }
860 OMX_U32 ret = frameRate * iFramesInterval;
861 CHECK(ret > 1);
862 return ret;
863}
864
865status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
866 int32_t iFramesInterval, frameRate, bitRate;
867 bool success = meta->findInt32(kKeyBitRate, &bitRate);
868 success = success && meta->findInt32(kKeySampleRate, &frameRate);
869 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
870 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -0700871 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
872 InitOMXParams(&mpeg4type);
873 mpeg4type.nPortIndex = kPortIndexOutput;
874
875 status_t err = mOMX->getParameter(
876 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
877 CHECK_EQ(err, OK);
878
879 mpeg4type.nSliceHeaderSpacing = 0;
880 mpeg4type.bSVH = OMX_FALSE;
881 mpeg4type.bGov = OMX_FALSE;
882
883 mpeg4type.nAllowedPictureTypes =
884 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
885
James Dong1244eab2010-06-08 11:58:53 -0700886 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
887 if (mpeg4type.nPFrames == 0) {
888 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
889 }
Andreas Huberb482ce82009-10-29 12:02:48 -0700890 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -0700891 mpeg4type.nIDCVLCThreshold = 0;
892 mpeg4type.bACPred = OMX_TRUE;
893 mpeg4type.nMaxPacketSize = 256;
894 mpeg4type.nTimeIncRes = 1000;
895 mpeg4type.nHeaderExtension = 0;
896 mpeg4type.bReversibleVLC = OMX_FALSE;
897
898 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileCore;
899 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2;
900
901 err = mOMX->setParameter(
902 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
903 CHECK_EQ(err, OK);
904
905 // ----------------
906
907 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
908 InitOMXParams(&bitrateType);
909 bitrateType.nPortIndex = kPortIndexOutput;
910
911 err = mOMX->getParameter(
912 mNode, OMX_IndexParamVideoBitrate,
913 &bitrateType, sizeof(bitrateType));
914 CHECK_EQ(err, OK);
915
916 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
James Dong1244eab2010-06-08 11:58:53 -0700917 bitrateType.nTargetBitrate = bitRate;
Andreas Huberb482ce82009-10-29 12:02:48 -0700918
919 err = mOMX->setParameter(
920 mNode, OMX_IndexParamVideoBitrate,
921 &bitrateType, sizeof(bitrateType));
922 CHECK_EQ(err, OK);
923
924 // ----------------
925
926 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
927 InitOMXParams(&errorCorrectionType);
928 errorCorrectionType.nPortIndex = kPortIndexOutput;
929
930 err = mOMX->getParameter(
931 mNode, OMX_IndexParamVideoErrorCorrection,
932 &errorCorrectionType, sizeof(errorCorrectionType));
933 CHECK_EQ(err, OK);
934
935 errorCorrectionType.bEnableHEC = OMX_FALSE;
936 errorCorrectionType.bEnableResync = OMX_TRUE;
937 errorCorrectionType.nResynchMarkerSpacing = 256;
938 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
939 errorCorrectionType.bEnableRVLC = OMX_FALSE;
940
941 err = mOMX->setParameter(
942 mNode, OMX_IndexParamVideoErrorCorrection,
943 &errorCorrectionType, sizeof(errorCorrectionType));
944 CHECK_EQ(err, OK);
945
946 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700947}
948
James Dong1244eab2010-06-08 11:58:53 -0700949status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
950 int32_t iFramesInterval, frameRate, bitRate;
951 bool success = meta->findInt32(kKeyBitRate, &bitRate);
952 success = success && meta->findInt32(kKeySampleRate, &frameRate);
953 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
954 CHECK(success);
955
Andreas Huberea6a38c2009-11-16 15:43:38 -0800956 OMX_VIDEO_PARAM_AVCTYPE h264type;
957 InitOMXParams(&h264type);
958 h264type.nPortIndex = kPortIndexOutput;
959
960 status_t err = mOMX->getParameter(
961 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
962 CHECK_EQ(err, OK);
963
964 h264type.nAllowedPictureTypes =
965 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
966
967 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -0700968 h264type.nBFrames = 0; // No B frames support yet
969 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
970 if (h264type.nPFrames == 0) {
971 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
972 }
Andreas Huberea6a38c2009-11-16 15:43:38 -0800973 h264type.bUseHadamard = OMX_TRUE;
974 h264type.nRefFrames = 1;
975 h264type.nRefIdx10ActiveMinus1 = 0;
976 h264type.nRefIdx11ActiveMinus1 = 0;
977 h264type.bEnableUEP = OMX_FALSE;
978 h264type.bEnableFMO = OMX_FALSE;
979 h264type.bEnableASO = OMX_FALSE;
980 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -0800981 h264type.bFrameMBsOnly = OMX_TRUE;
982 h264type.bMBAFF = OMX_FALSE;
983 h264type.bEntropyCodingCABAC = OMX_FALSE;
984 h264type.bWeightedPPrediction = OMX_FALSE;
985 h264type.bconstIpred = OMX_FALSE;
986 h264type.bDirect8x8Inference = OMX_FALSE;
987 h264type.bDirectSpatialTemporal = OMX_FALSE;
988 h264type.nCabacInitIdc = 0;
989 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
990
991 err = mOMX->setParameter(
992 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
993 CHECK_EQ(err, OK);
994
995 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
996 InitOMXParams(&bitrateType);
997 bitrateType.nPortIndex = kPortIndexOutput;
998
999 err = mOMX->getParameter(
1000 mNode, OMX_IndexParamVideoBitrate,
1001 &bitrateType, sizeof(bitrateType));
1002 CHECK_EQ(err, OK);
1003
1004 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
James Dong1244eab2010-06-08 11:58:53 -07001005 bitrateType.nTargetBitrate = bitRate;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001006
1007 err = mOMX->setParameter(
1008 mNode, OMX_IndexParamVideoBitrate,
1009 &bitrateType, sizeof(bitrateType));
1010 CHECK_EQ(err, OK);
1011
1012 return OK;
1013}
1014
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001015status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001016 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001017 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001018
Andreas Huberbe06d262009-08-14 14:37:10 -07001019 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001020 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001021 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001022 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001023 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001024 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001025 compressionFormat = OMX_VIDEO_CodingH263;
1026 } else {
1027 LOGE("Not a supported video mime type: %s", mime);
1028 CHECK(!"Should not be here. Not a supported video mime type.");
1029 }
1030
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001031 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001032 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1033
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001034 if (err != OK) {
1035 return err;
1036 }
1037
Andreas Huberbe06d262009-08-14 14:37:10 -07001038#if 1
1039 {
1040 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001041 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001042 format.nPortIndex = kPortIndexOutput;
1043 format.nIndex = 0;
1044
Andreas Huber784202e2009-10-15 13:46:54 -07001045 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001046 mNode, OMX_IndexParamVideoPortFormat,
1047 &format, sizeof(format));
1048 CHECK_EQ(err, OK);
1049 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1050
1051 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1052
1053 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1054 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1055 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1056 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1057
Andreas Huber784202e2009-10-15 13:46:54 -07001058 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001059 mNode, OMX_IndexParamVideoPortFormat,
1060 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001061
1062 if (err != OK) {
1063 return err;
1064 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001065 }
1066#endif
1067
1068 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001069 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001070 def.nPortIndex = kPortIndexInput;
1071
Andreas Huber4c483422009-09-02 16:05:36 -07001072 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1073
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001074 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001075 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1076
1077 CHECK_EQ(err, OK);
1078
1079#if 1
1080 // XXX Need a (much) better heuristic to compute input buffer sizes.
1081 const size_t X = 64 * 1024;
1082 if (def.nBufferSize < X) {
1083 def.nBufferSize = X;
1084 }
1085#endif
1086
1087 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1088
1089 video_def->nFrameWidth = width;
1090 video_def->nFrameHeight = height;
1091
Andreas Huberb482ce82009-10-29 12:02:48 -07001092 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001093 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1094
Andreas Huber784202e2009-10-15 13:46:54 -07001095 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001096 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001097
1098 if (err != OK) {
1099 return err;
1100 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001101
1102 ////////////////////////////////////////////////////////////////////////////
1103
Andreas Huber4c483422009-09-02 16:05:36 -07001104 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001105 def.nPortIndex = kPortIndexOutput;
1106
Andreas Huber784202e2009-10-15 13:46:54 -07001107 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001108 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1109 CHECK_EQ(err, OK);
1110 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1111
1112#if 0
1113 def.nBufferSize =
1114 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1115#endif
1116
1117 video_def->nFrameWidth = width;
1118 video_def->nFrameHeight = height;
1119
Andreas Huber784202e2009-10-15 13:46:54 -07001120 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001121 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001122
1123 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001124}
1125
Andreas Huberbe06d262009-08-14 14:37:10 -07001126OMXCodec::OMXCodec(
1127 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001128 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001129 const char *mime,
1130 const char *componentName,
1131 const sp<MediaSource> &source)
1132 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001133 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001134 mNode(node),
1135 mQuirks(quirks),
1136 mIsEncoder(isEncoder),
1137 mMIME(strdup(mime)),
1138 mComponentName(strdup(componentName)),
1139 mSource(source),
1140 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001141 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001142 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001143 mSignalledEOS(false),
1144 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001145 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001146 mSeekTimeUs(-1),
1147 mLeftOverBuffer(NULL) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001148 mPortStatus[kPortIndexInput] = ENABLED;
1149 mPortStatus[kPortIndexOutput] = ENABLED;
1150
Andreas Huber4c483422009-09-02 16:05:36 -07001151 setComponentRole();
1152}
1153
Andreas Hubere6c40962009-09-10 14:13:30 -07001154// static
1155void OMXCodec::setComponentRole(
1156 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1157 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001158 struct MimeToRole {
1159 const char *mime;
1160 const char *decoderRole;
1161 const char *encoderRole;
1162 };
1163
1164 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001165 { MEDIA_MIMETYPE_AUDIO_MPEG,
1166 "audio_decoder.mp3", "audio_encoder.mp3" },
1167 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1168 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1169 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1170 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1171 { MEDIA_MIMETYPE_AUDIO_AAC,
1172 "audio_decoder.aac", "audio_encoder.aac" },
1173 { MEDIA_MIMETYPE_VIDEO_AVC,
1174 "video_decoder.avc", "video_encoder.avc" },
1175 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1176 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1177 { MEDIA_MIMETYPE_VIDEO_H263,
1178 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001179 };
1180
1181 static const size_t kNumMimeToRole =
1182 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1183
1184 size_t i;
1185 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001186 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001187 break;
1188 }
1189 }
1190
1191 if (i == kNumMimeToRole) {
1192 return;
1193 }
1194
1195 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001196 isEncoder ? kMimeToRole[i].encoderRole
1197 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001198
1199 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001200 OMX_PARAM_COMPONENTROLETYPE roleParams;
1201 InitOMXParams(&roleParams);
1202
1203 strncpy((char *)roleParams.cRole,
1204 role, OMX_MAX_STRINGNAME_SIZE - 1);
1205
1206 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1207
Andreas Huber784202e2009-10-15 13:46:54 -07001208 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001209 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001210 &roleParams, sizeof(roleParams));
1211
1212 if (err != OK) {
1213 LOGW("Failed to set standard component role '%s'.", role);
1214 }
1215 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001216}
1217
Andreas Hubere6c40962009-09-10 14:13:30 -07001218void OMXCodec::setComponentRole() {
1219 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1220}
1221
Andreas Huberbe06d262009-08-14 14:37:10 -07001222OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001223 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001224
Andreas Huber784202e2009-10-15 13:46:54 -07001225 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001226 CHECK_EQ(err, OK);
1227
1228 mNode = NULL;
1229 setState(DEAD);
1230
1231 clearCodecSpecificData();
1232
1233 free(mComponentName);
1234 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001235
Andreas Huberbe06d262009-08-14 14:37:10 -07001236 free(mMIME);
1237 mMIME = NULL;
1238}
1239
1240status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001241 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001242
1243 CHECK_EQ(mState, LOADED);
1244
1245 status_t err;
1246 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001247 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001248 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001249 setState(LOADED_TO_IDLE);
1250 }
1251
1252 err = allocateBuffers();
1253 CHECK_EQ(err, OK);
1254
1255 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001256 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001257 CHECK_EQ(err, OK);
1258
1259 setState(LOADED_TO_IDLE);
1260 }
1261
1262 while (mState != EXECUTING && mState != ERROR) {
1263 mAsyncCompletion.wait(mLock);
1264 }
1265
1266 return mState == ERROR ? UNKNOWN_ERROR : OK;
1267}
1268
1269// static
1270bool OMXCodec::isIntermediateState(State state) {
1271 return state == LOADED_TO_IDLE
1272 || state == IDLE_TO_EXECUTING
1273 || state == EXECUTING_TO_IDLE
1274 || state == IDLE_TO_LOADED
1275 || state == RECONFIGURING;
1276}
1277
1278status_t OMXCodec::allocateBuffers() {
1279 status_t err = allocateBuffersOnPort(kPortIndexInput);
1280
1281 if (err != OK) {
1282 return err;
1283 }
1284
1285 return allocateBuffersOnPort(kPortIndexOutput);
1286}
1287
1288status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1289 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001290 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001291 def.nPortIndex = portIndex;
1292
Andreas Huber784202e2009-10-15 13:46:54 -07001293 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001294 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1295
1296 if (err != OK) {
1297 return err;
1298 }
1299
Andreas Huber5c0a9132009-08-20 11:16:40 -07001300 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001301 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001302
Andreas Huberbe06d262009-08-14 14:37:10 -07001303 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001304 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001305 CHECK(mem.get() != NULL);
1306
Andreas Huberc712b9f2010-01-20 15:05:46 -08001307 BufferInfo info;
1308 info.mData = NULL;
1309 info.mSize = def.nBufferSize;
1310
Andreas Huberbe06d262009-08-14 14:37:10 -07001311 IOMX::buffer_id buffer;
1312 if (portIndex == kPortIndexInput
1313 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001314 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001315 mem.clear();
1316
Andreas Huberf1fe0642010-01-15 15:28:19 -08001317 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001318 mNode, portIndex, def.nBufferSize, &buffer,
1319 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001320 } else {
1321 err = mOMX->allocateBufferWithBackup(
1322 mNode, portIndex, mem, &buffer);
1323 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001324 } else if (portIndex == kPortIndexOutput
1325 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001326 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001327 mem.clear();
1328
Andreas Huberf1fe0642010-01-15 15:28:19 -08001329 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001330 mNode, portIndex, def.nBufferSize, &buffer,
1331 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001332 } else {
1333 err = mOMX->allocateBufferWithBackup(
1334 mNode, portIndex, mem, &buffer);
1335 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001336 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001337 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001338 }
1339
1340 if (err != OK) {
1341 LOGE("allocate_buffer_with_backup failed");
1342 return err;
1343 }
1344
Andreas Huberc712b9f2010-01-20 15:05:46 -08001345 if (mem != NULL) {
1346 info.mData = mem->pointer();
1347 }
1348
Andreas Huberbe06d262009-08-14 14:37:10 -07001349 info.mBuffer = buffer;
1350 info.mOwnedByComponent = false;
1351 info.mMem = mem;
1352 info.mMediaBuffer = NULL;
1353
1354 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001355 if (!(mOMXLivesLocally
1356 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1357 && (mQuirks & kDefersOutputBufferAllocation))) {
1358 // If the node does not fill in the buffer ptr at this time,
1359 // we will defer creating the MediaBuffer until receiving
1360 // the first FILL_BUFFER_DONE notification instead.
1361 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1362 info.mMediaBuffer->setObserver(this);
1363 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001364 }
1365
1366 mPortBuffers[portIndex].push(info);
1367
Andreas Huber4c483422009-09-02 16:05:36 -07001368 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001369 portIndex == kPortIndexInput ? "input" : "output");
1370 }
1371
Andreas Huber2ea14e22009-12-16 09:30:55 -08001372 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001373
1374 return OK;
1375}
1376
1377void OMXCodec::on_message(const omx_message &msg) {
1378 Mutex::Autolock autoLock(mLock);
1379
1380 switch (msg.type) {
1381 case omx_message::EVENT:
1382 {
1383 onEvent(
1384 msg.u.event_data.event, msg.u.event_data.data1,
1385 msg.u.event_data.data2);
1386
1387 break;
1388 }
1389
1390 case omx_message::EMPTY_BUFFER_DONE:
1391 {
1392 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1393
Andreas Huber4c483422009-09-02 16:05:36 -07001394 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001395
1396 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1397 size_t i = 0;
1398 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1399 ++i;
1400 }
1401
1402 CHECK(i < buffers->size());
1403 if (!(*buffers)[i].mOwnedByComponent) {
1404 LOGW("We already own input buffer %p, yet received "
1405 "an EMPTY_BUFFER_DONE.", buffer);
1406 }
1407
1408 buffers->editItemAt(i).mOwnedByComponent = false;
1409
1410 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001411 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001412
1413 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001414 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001415 CHECK_EQ(err, OK);
1416
1417 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001418 } else if (mState != ERROR
1419 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001420 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1421 drainInputBuffer(&buffers->editItemAt(i));
1422 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001423 break;
1424 }
1425
1426 case omx_message::FILL_BUFFER_DONE:
1427 {
1428 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1429 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1430
Andreas Huber2ea14e22009-12-16 09:30:55 -08001431 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001432 buffer,
1433 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001434 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001435 msg.u.extended_buffer_data.timestamp,
1436 msg.u.extended_buffer_data.timestamp / 1E6);
1437
1438 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1439 size_t i = 0;
1440 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1441 ++i;
1442 }
1443
1444 CHECK(i < buffers->size());
1445 BufferInfo *info = &buffers->editItemAt(i);
1446
1447 if (!info->mOwnedByComponent) {
1448 LOGW("We already own output buffer %p, yet received "
1449 "a FILL_BUFFER_DONE.", buffer);
1450 }
1451
1452 info->mOwnedByComponent = false;
1453
1454 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001455 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001456
1457 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001458 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001459 CHECK_EQ(err, OK);
1460
1461 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001462#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001463 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1464 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001465 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001466 mNoMoreOutputData = true;
1467 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001468#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001469 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1470 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001471
Andreas Huber52733b82010-01-25 10:41:35 -08001472 if (info->mMediaBuffer == NULL) {
1473 CHECK(mOMXLivesLocally);
1474 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1475 CHECK(mQuirks & kDefersOutputBufferAllocation);
1476
1477 // The qcom video decoders on Nexus don't actually allocate
1478 // output buffer memory on a call to OMX_AllocateBuffer
1479 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1480 // structure is only filled in later.
1481
1482 info->mMediaBuffer = new MediaBuffer(
1483 msg.u.extended_buffer_data.data_ptr,
1484 info->mSize);
1485 info->mMediaBuffer->setObserver(this);
1486 }
1487
Andreas Huberbe06d262009-08-14 14:37:10 -07001488 MediaBuffer *buffer = info->mMediaBuffer;
1489
1490 buffer->set_range(
1491 msg.u.extended_buffer_data.range_offset,
1492 msg.u.extended_buffer_data.range_length);
1493
1494 buffer->meta_data()->clear();
1495
Andreas Huberfa8de752009-10-08 10:07:49 -07001496 buffer->meta_data()->setInt64(
1497 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001498
1499 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1500 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1501 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001502 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1503 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1504 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001505
1506 buffer->meta_data()->setPointer(
1507 kKeyPlatformPrivate,
1508 msg.u.extended_buffer_data.platform_private);
1509
1510 buffer->meta_data()->setPointer(
1511 kKeyBufferID,
1512 msg.u.extended_buffer_data.buffer);
1513
1514 mFilledBuffers.push_back(i);
1515 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001516
1517 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1518 CODEC_LOGV("No more output data.");
1519 mNoMoreOutputData = true;
1520 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001521 }
1522
1523 break;
1524 }
1525
1526 default:
1527 {
1528 CHECK(!"should not be here.");
1529 break;
1530 }
1531 }
1532}
1533
1534void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1535 switch (event) {
1536 case OMX_EventCmdComplete:
1537 {
1538 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1539 break;
1540 }
1541
1542 case OMX_EventError:
1543 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001544 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001545
1546 setState(ERROR);
1547 break;
1548 }
1549
1550 case OMX_EventPortSettingsChanged:
1551 {
1552 onPortSettingsChanged(data1);
1553 break;
1554 }
1555
Andreas Huber2ea14e22009-12-16 09:30:55 -08001556#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001557 case OMX_EventBufferFlag:
1558 {
Andreas Huber4c483422009-09-02 16:05:36 -07001559 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001560
1561 if (data1 == kPortIndexOutput) {
1562 mNoMoreOutputData = true;
1563 }
1564 break;
1565 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001566#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001567
1568 default:
1569 {
Andreas Huber4c483422009-09-02 16:05:36 -07001570 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001571 break;
1572 }
1573 }
1574}
1575
Andreas Huberb1678602009-10-19 13:06:40 -07001576// Has the format changed in any way that the client would have to be aware of?
1577static bool formatHasNotablyChanged(
1578 const sp<MetaData> &from, const sp<MetaData> &to) {
1579 if (from.get() == NULL && to.get() == NULL) {
1580 return false;
1581 }
1582
Andreas Huberf68c1682009-10-21 14:01:30 -07001583 if ((from.get() == NULL && to.get() != NULL)
1584 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001585 return true;
1586 }
1587
1588 const char *mime_from, *mime_to;
1589 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1590 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1591
1592 if (strcasecmp(mime_from, mime_to)) {
1593 return true;
1594 }
1595
1596 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1597 int32_t colorFormat_from, colorFormat_to;
1598 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1599 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1600
1601 if (colorFormat_from != colorFormat_to) {
1602 return true;
1603 }
1604
1605 int32_t width_from, width_to;
1606 CHECK(from->findInt32(kKeyWidth, &width_from));
1607 CHECK(to->findInt32(kKeyWidth, &width_to));
1608
1609 if (width_from != width_to) {
1610 return true;
1611 }
1612
1613 int32_t height_from, height_to;
1614 CHECK(from->findInt32(kKeyHeight, &height_from));
1615 CHECK(to->findInt32(kKeyHeight, &height_to));
1616
1617 if (height_from != height_to) {
1618 return true;
1619 }
1620 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1621 int32_t numChannels_from, numChannels_to;
1622 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1623 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1624
1625 if (numChannels_from != numChannels_to) {
1626 return true;
1627 }
1628
1629 int32_t sampleRate_from, sampleRate_to;
1630 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1631 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1632
1633 if (sampleRate_from != sampleRate_to) {
1634 return true;
1635 }
1636 }
1637
1638 return false;
1639}
1640
Andreas Huberbe06d262009-08-14 14:37:10 -07001641void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1642 switch (cmd) {
1643 case OMX_CommandStateSet:
1644 {
1645 onStateChange((OMX_STATETYPE)data);
1646 break;
1647 }
1648
1649 case OMX_CommandPortDisable:
1650 {
1651 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001652 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001653
1654 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1655 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1656 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1657
1658 mPortStatus[portIndex] = DISABLED;
1659
1660 if (mState == RECONFIGURING) {
1661 CHECK_EQ(portIndex, kPortIndexOutput);
1662
Andreas Huberb1678602009-10-19 13:06:40 -07001663 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001664 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001665
1666 // Don't notify clients if the output port settings change
1667 // wasn't of importance to them, i.e. it may be that just the
1668 // number of buffers has changed and nothing else.
1669 mOutputPortSettingsHaveChanged =
1670 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001671
Andreas Huberbe06d262009-08-14 14:37:10 -07001672 enablePortAsync(portIndex);
1673
1674 status_t err = allocateBuffersOnPort(portIndex);
1675 CHECK_EQ(err, OK);
1676 }
1677 break;
1678 }
1679
1680 case OMX_CommandPortEnable:
1681 {
1682 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001683 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001684
1685 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1686 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1687
1688 mPortStatus[portIndex] = ENABLED;
1689
1690 if (mState == RECONFIGURING) {
1691 CHECK_EQ(portIndex, kPortIndexOutput);
1692
1693 setState(EXECUTING);
1694
1695 fillOutputBuffers();
1696 }
1697 break;
1698 }
1699
1700 case OMX_CommandFlush:
1701 {
1702 OMX_U32 portIndex = data;
1703
Andreas Huber4c483422009-09-02 16:05:36 -07001704 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001705
1706 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1707 mPortStatus[portIndex] = ENABLED;
1708
1709 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1710 mPortBuffers[portIndex].size());
1711
1712 if (mState == RECONFIGURING) {
1713 CHECK_EQ(portIndex, kPortIndexOutput);
1714
1715 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001716 } else if (mState == EXECUTING_TO_IDLE) {
1717 if (mPortStatus[kPortIndexInput] == ENABLED
1718 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001719 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001720 "transition from EXECUTING to IDLE.");
1721
1722 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1723 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1724
1725 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001726 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001727 CHECK_EQ(err, OK);
1728 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001729 } else {
1730 // We're flushing both ports in preparation for seeking.
1731
1732 if (mPortStatus[kPortIndexInput] == ENABLED
1733 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001734 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001735 " seek-time.");
1736
1737 drainInputBuffers();
1738 fillOutputBuffers();
1739 }
1740 }
1741
1742 break;
1743 }
1744
1745 default:
1746 {
Andreas Huber4c483422009-09-02 16:05:36 -07001747 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001748 break;
1749 }
1750 }
1751}
1752
1753void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001754 CODEC_LOGV("onStateChange %d", newState);
1755
Andreas Huberbe06d262009-08-14 14:37:10 -07001756 switch (newState) {
1757 case OMX_StateIdle:
1758 {
Andreas Huber4c483422009-09-02 16:05:36 -07001759 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001760 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001761 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001762 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1763
1764 CHECK_EQ(err, OK);
1765
1766 setState(IDLE_TO_EXECUTING);
1767 } else {
1768 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1769
1770 CHECK_EQ(
1771 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1772 mPortBuffers[kPortIndexInput].size());
1773
1774 CHECK_EQ(
1775 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1776 mPortBuffers[kPortIndexOutput].size());
1777
Andreas Huber784202e2009-10-15 13:46:54 -07001778 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001779 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1780
1781 CHECK_EQ(err, OK);
1782
1783 err = freeBuffersOnPort(kPortIndexInput);
1784 CHECK_EQ(err, OK);
1785
1786 err = freeBuffersOnPort(kPortIndexOutput);
1787 CHECK_EQ(err, OK);
1788
1789 mPortStatus[kPortIndexInput] = ENABLED;
1790 mPortStatus[kPortIndexOutput] = ENABLED;
1791
1792 setState(IDLE_TO_LOADED);
1793 }
1794 break;
1795 }
1796
1797 case OMX_StateExecuting:
1798 {
1799 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1800
Andreas Huber4c483422009-09-02 16:05:36 -07001801 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001802
1803 setState(EXECUTING);
1804
Andreas Huber42978e52009-08-27 10:08:39 -07001805 // Buffers will be submitted to the component in the first
1806 // call to OMXCodec::read as mInitialBufferSubmit is true at
1807 // this point. This ensures that this on_message call returns,
1808 // releases the lock and ::init can notice the state change and
1809 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001810 break;
1811 }
1812
1813 case OMX_StateLoaded:
1814 {
1815 CHECK_EQ(mState, IDLE_TO_LOADED);
1816
Andreas Huber4c483422009-09-02 16:05:36 -07001817 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001818
1819 setState(LOADED);
1820 break;
1821 }
1822
Andreas Huberc712b9f2010-01-20 15:05:46 -08001823 case OMX_StateInvalid:
1824 {
1825 setState(ERROR);
1826 break;
1827 }
1828
Andreas Huberbe06d262009-08-14 14:37:10 -07001829 default:
1830 {
1831 CHECK(!"should not be here.");
1832 break;
1833 }
1834 }
1835}
1836
1837// static
1838size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1839 size_t n = 0;
1840 for (size_t i = 0; i < buffers.size(); ++i) {
1841 if (!buffers[i].mOwnedByComponent) {
1842 ++n;
1843 }
1844 }
1845
1846 return n;
1847}
1848
1849status_t OMXCodec::freeBuffersOnPort(
1850 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1851 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1852
1853 status_t stickyErr = OK;
1854
1855 for (size_t i = buffers->size(); i-- > 0;) {
1856 BufferInfo *info = &buffers->editItemAt(i);
1857
1858 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1859 continue;
1860 }
1861
1862 CHECK_EQ(info->mOwnedByComponent, false);
1863
Andreas Huber92022852009-09-14 15:24:14 -07001864 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1865
Andreas Huberbe06d262009-08-14 14:37:10 -07001866 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001867 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001868
1869 if (err != OK) {
1870 stickyErr = err;
1871 }
1872
1873 if (info->mMediaBuffer != NULL) {
1874 info->mMediaBuffer->setObserver(NULL);
1875
1876 // Make sure nobody but us owns this buffer at this point.
1877 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1878
1879 info->mMediaBuffer->release();
1880 }
1881
1882 buffers->removeAt(i);
1883 }
1884
1885 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1886
1887 return stickyErr;
1888}
1889
1890void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001891 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001892
1893 CHECK_EQ(mState, EXECUTING);
1894 CHECK_EQ(portIndex, kPortIndexOutput);
1895 setState(RECONFIGURING);
1896
1897 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001898 if (!flushPortAsync(portIndex)) {
1899 onCmdComplete(OMX_CommandFlush, portIndex);
1900 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001901 } else {
1902 disablePortAsync(portIndex);
1903 }
1904}
1905
Andreas Huber404cc412009-08-25 14:26:05 -07001906bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001907 CHECK(mState == EXECUTING || mState == RECONFIGURING
1908 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001909
Andreas Huber4c483422009-09-02 16:05:36 -07001910 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001911 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1912 mPortBuffers[portIndex].size());
1913
Andreas Huberbe06d262009-08-14 14:37:10 -07001914 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1915 mPortStatus[portIndex] = SHUTTING_DOWN;
1916
Andreas Huber404cc412009-08-25 14:26:05 -07001917 if ((mQuirks & kRequiresFlushCompleteEmulation)
1918 && countBuffersWeOwn(mPortBuffers[portIndex])
1919 == mPortBuffers[portIndex].size()) {
1920 // No flush is necessary and this component fails to send a
1921 // flush-complete event in this case.
1922
1923 return false;
1924 }
1925
Andreas Huberbe06d262009-08-14 14:37:10 -07001926 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001927 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001928 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001929
1930 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001931}
1932
1933void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1934 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1935
1936 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1937 mPortStatus[portIndex] = DISABLING;
1938
1939 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001940 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001941 CHECK_EQ(err, OK);
1942
1943 freeBuffersOnPort(portIndex, true);
1944}
1945
1946void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1947 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1948
1949 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1950 mPortStatus[portIndex] = ENABLING;
1951
1952 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001953 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001954 CHECK_EQ(err, OK);
1955}
1956
1957void OMXCodec::fillOutputBuffers() {
1958 CHECK_EQ(mState, EXECUTING);
1959
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001960 // This is a workaround for some decoders not properly reporting
1961 // end-of-output-stream. If we own all input buffers and also own
1962 // all output buffers and we already signalled end-of-input-stream,
1963 // the end-of-output-stream is implied.
1964 if (mSignalledEOS
1965 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
1966 == mPortBuffers[kPortIndexInput].size()
1967 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
1968 == mPortBuffers[kPortIndexOutput].size()) {
1969 mNoMoreOutputData = true;
1970 mBufferFilled.signal();
1971
1972 return;
1973 }
1974
Andreas Huberbe06d262009-08-14 14:37:10 -07001975 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1976 for (size_t i = 0; i < buffers->size(); ++i) {
1977 fillOutputBuffer(&buffers->editItemAt(i));
1978 }
1979}
1980
1981void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001982 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001983
1984 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1985 for (size_t i = 0; i < buffers->size(); ++i) {
1986 drainInputBuffer(&buffers->editItemAt(i));
1987 }
1988}
1989
1990void OMXCodec::drainInputBuffer(BufferInfo *info) {
1991 CHECK_EQ(info->mOwnedByComponent, false);
1992
1993 if (mSignalledEOS) {
1994 return;
1995 }
1996
1997 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1998 const CodecSpecificData *specific =
1999 mCodecSpecificData[mCodecSpecificDataIndex];
2000
2001 size_t size = specific->mSize;
2002
Andreas Hubere6c40962009-09-10 14:13:30 -07002003 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002004 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002005 static const uint8_t kNALStartCode[4] =
2006 { 0x00, 0x00, 0x00, 0x01 };
2007
Andreas Huberc712b9f2010-01-20 15:05:46 -08002008 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002009
2010 size += 4;
2011
Andreas Huberc712b9f2010-01-20 15:05:46 -08002012 memcpy(info->mData, kNALStartCode, 4);
2013 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002014 specific->mData, specific->mSize);
2015 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002016 CHECK(info->mSize >= specific->mSize);
2017 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002018 }
2019
Andreas Huber2ea14e22009-12-16 09:30:55 -08002020 mNoMoreOutputData = false;
2021
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002022 CODEC_LOGV("calling emptyBuffer with codec specific data");
2023
Andreas Huber784202e2009-10-15 13:46:54 -07002024 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002025 mNode, info->mBuffer, 0, size,
2026 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2027 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002028 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002029
2030 info->mOwnedByComponent = true;
2031
2032 ++mCodecSpecificDataIndex;
2033 return;
2034 }
2035
Andreas Huberbe06d262009-08-14 14:37:10 -07002036 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002037
Andreas Hubera4357ad2010-04-02 12:49:54 -07002038 bool signalEOS = false;
2039 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002040
Andreas Hubera4357ad2010-04-02 12:49:54 -07002041 size_t offset = 0;
2042 int32_t n = 0;
2043 for (;;) {
2044 MediaBuffer *srcBuffer;
2045 if (mSeekTimeUs >= 0) {
2046 if (mLeftOverBuffer) {
2047 mLeftOverBuffer->release();
2048 mLeftOverBuffer = NULL;
2049 }
2050
2051 MediaSource::ReadOptions options;
2052 options.setSeekTo(mSeekTimeUs);
2053
2054 mSeekTimeUs = -1;
2055 mBufferFilled.signal();
2056
2057 err = mSource->read(&srcBuffer, &options);
2058 } else if (mLeftOverBuffer) {
2059 srcBuffer = mLeftOverBuffer;
2060 mLeftOverBuffer = NULL;
2061
2062 err = OK;
2063 } else {
2064 err = mSource->read(&srcBuffer);
2065 }
2066
2067 if (err != OK) {
2068 signalEOS = true;
2069 mFinalStatus = err;
2070 mSignalledEOS = true;
2071 break;
2072 }
2073
2074 size_t remainingBytes = info->mSize - offset;
2075
2076 if (srcBuffer->range_length() > remainingBytes) {
2077 if (offset == 0) {
2078 CODEC_LOGE(
2079 "Codec's input buffers are too small to accomodate "
2080 "buffer read from source (info->mSize = %d, srcLength = %d)",
2081 info->mSize, srcBuffer->range_length());
2082
2083 srcBuffer->release();
2084 srcBuffer = NULL;
2085
2086 setState(ERROR);
2087 return;
2088 }
2089
2090 mLeftOverBuffer = srcBuffer;
2091 break;
2092 }
2093
James Dong4f501f02010-06-07 14:41:41 -07002094 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2095 CHECK(mOMXLivesLocally && offset == 0);
2096 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2097 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2098 } else {
2099 memcpy((uint8_t *)info->mData + offset,
2100 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2101 srcBuffer->range_length());
2102 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002103
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002104 int64_t lastBufferTimeUs;
2105 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
2106 CHECK(timestampUs >= 0);
2107
Andreas Hubera4357ad2010-04-02 12:49:54 -07002108 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002109 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002110 }
2111
2112 offset += srcBuffer->range_length();
2113
2114 srcBuffer->release();
2115 srcBuffer = NULL;
2116
2117 ++n;
2118
2119 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2120 break;
2121 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002122
2123 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2124
2125 if (coalescedDurationUs > 250000ll) {
2126 // Don't coalesce more than 250ms worth of encoded data at once.
2127 break;
2128 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002129 }
2130
2131 if (n > 1) {
2132 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002133 }
2134
2135 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002136
Andreas Hubera4357ad2010-04-02 12:49:54 -07002137 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002138 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002139 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002140 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002141 }
2142
Andreas Hubera4357ad2010-04-02 12:49:54 -07002143 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2144 "timestamp %lld us (%.2f secs)",
2145 info->mBuffer, offset,
2146 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002147
Andreas Huber784202e2009-10-15 13:46:54 -07002148 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002149 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002150 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002151
2152 if (err != OK) {
2153 setState(ERROR);
2154 return;
2155 }
2156
2157 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002158
2159 // This component does not ever signal the EOS flag on output buffers,
2160 // Thanks for nothing.
2161 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2162 mNoMoreOutputData = true;
2163 mBufferFilled.signal();
2164 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002165}
2166
2167void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2168 CHECK_EQ(info->mOwnedByComponent, false);
2169
Andreas Huber404cc412009-08-25 14:26:05 -07002170 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002171 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002172 "calling fillOutputBuffer");
2173 return;
2174 }
2175
Andreas Huber4c483422009-09-02 16:05:36 -07002176 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002177 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002178
2179 if (err != OK) {
2180 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2181
2182 setState(ERROR);
2183 return;
2184 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002185
2186 info->mOwnedByComponent = true;
2187}
2188
2189void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2190 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2191 for (size_t i = 0; i < buffers->size(); ++i) {
2192 if ((*buffers)[i].mBuffer == buffer) {
2193 drainInputBuffer(&buffers->editItemAt(i));
2194 return;
2195 }
2196 }
2197
2198 CHECK(!"should not be here.");
2199}
2200
2201void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2202 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2203 for (size_t i = 0; i < buffers->size(); ++i) {
2204 if ((*buffers)[i].mBuffer == buffer) {
2205 fillOutputBuffer(&buffers->editItemAt(i));
2206 return;
2207 }
2208 }
2209
2210 CHECK(!"should not be here.");
2211}
2212
2213void OMXCodec::setState(State newState) {
2214 mState = newState;
2215 mAsyncCompletion.signal();
2216
2217 // This may cause some spurious wakeups but is necessary to
2218 // unblock the reader if we enter ERROR state.
2219 mBufferFilled.signal();
2220}
2221
Andreas Huberda050cf22009-09-02 14:01:43 -07002222void OMXCodec::setRawAudioFormat(
2223 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002224
2225 // port definition
2226 OMX_PARAM_PORTDEFINITIONTYPE def;
2227 InitOMXParams(&def);
2228 def.nPortIndex = portIndex;
2229 status_t err = mOMX->getParameter(
2230 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2231 CHECK_EQ(err, OK);
2232 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2233 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2234 &def, sizeof(def)), OK);
2235
2236 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002237 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002238 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002239 pcmParams.nPortIndex = portIndex;
2240
James Dongabed93a2010-04-22 17:27:04 -07002241 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002242 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2243
2244 CHECK_EQ(err, OK);
2245
2246 pcmParams.nChannels = numChannels;
2247 pcmParams.eNumData = OMX_NumericalDataSigned;
2248 pcmParams.bInterleaved = OMX_TRUE;
2249 pcmParams.nBitPerSample = 16;
2250 pcmParams.nSamplingRate = sampleRate;
2251 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2252
2253 if (numChannels == 1) {
2254 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2255 } else {
2256 CHECK_EQ(numChannels, 2);
2257
2258 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2259 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2260 }
2261
Andreas Huber784202e2009-10-15 13:46:54 -07002262 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002263 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2264
2265 CHECK_EQ(err, OK);
2266}
2267
James Dong17299ab2010-05-14 15:45:22 -07002268static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2269 if (isAMRWB) {
2270 if (bps <= 6600) {
2271 return OMX_AUDIO_AMRBandModeWB0;
2272 } else if (bps <= 8850) {
2273 return OMX_AUDIO_AMRBandModeWB1;
2274 } else if (bps <= 12650) {
2275 return OMX_AUDIO_AMRBandModeWB2;
2276 } else if (bps <= 14250) {
2277 return OMX_AUDIO_AMRBandModeWB3;
2278 } else if (bps <= 15850) {
2279 return OMX_AUDIO_AMRBandModeWB4;
2280 } else if (bps <= 18250) {
2281 return OMX_AUDIO_AMRBandModeWB5;
2282 } else if (bps <= 19850) {
2283 return OMX_AUDIO_AMRBandModeWB6;
2284 } else if (bps <= 23050) {
2285 return OMX_AUDIO_AMRBandModeWB7;
2286 }
2287
2288 // 23850 bps
2289 return OMX_AUDIO_AMRBandModeWB8;
2290 } else { // AMRNB
2291 if (bps <= 4750) {
2292 return OMX_AUDIO_AMRBandModeNB0;
2293 } else if (bps <= 5150) {
2294 return OMX_AUDIO_AMRBandModeNB1;
2295 } else if (bps <= 5900) {
2296 return OMX_AUDIO_AMRBandModeNB2;
2297 } else if (bps <= 6700) {
2298 return OMX_AUDIO_AMRBandModeNB3;
2299 } else if (bps <= 7400) {
2300 return OMX_AUDIO_AMRBandModeNB4;
2301 } else if (bps <= 7950) {
2302 return OMX_AUDIO_AMRBandModeNB5;
2303 } else if (bps <= 10200) {
2304 return OMX_AUDIO_AMRBandModeNB6;
2305 }
2306
2307 // 12200 bps
2308 return OMX_AUDIO_AMRBandModeNB7;
2309 }
2310}
2311
2312void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002313 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002314
Andreas Huber8768f2c2009-12-01 15:26:54 -08002315 OMX_AUDIO_PARAM_AMRTYPE def;
2316 InitOMXParams(&def);
2317 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002318
Andreas Huber8768f2c2009-12-01 15:26:54 -08002319 status_t err =
2320 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002321
Andreas Huber8768f2c2009-12-01 15:26:54 -08002322 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002323
Andreas Huber8768f2c2009-12-01 15:26:54 -08002324 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002325
James Dong17299ab2010-05-14 15:45:22 -07002326 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002327 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2328 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002329
2330 ////////////////////////
2331
2332 if (mIsEncoder) {
2333 sp<MetaData> format = mSource->getFormat();
2334 int32_t sampleRate;
2335 int32_t numChannels;
2336 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2337 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2338
2339 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2340 }
2341}
2342
James Dong17299ab2010-05-14 15:45:22 -07002343void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002344 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002345 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002346 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002347 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002348
2349 //////////////// output port ////////////////////
2350 // format
2351 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2352 format.nPortIndex = kPortIndexOutput;
2353 format.nIndex = 0;
2354 status_t err = OMX_ErrorNone;
2355 while (OMX_ErrorNone == err) {
2356 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2357 &format, sizeof(format)), OK);
2358 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2359 break;
2360 }
2361 format.nIndex++;
2362 }
2363 CHECK_EQ(OK, err);
2364 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2365 &format, sizeof(format)), OK);
2366
2367 // port definition
2368 OMX_PARAM_PORTDEFINITIONTYPE def;
2369 InitOMXParams(&def);
2370 def.nPortIndex = kPortIndexOutput;
2371 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2372 &def, sizeof(def)), OK);
2373 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2374 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2375 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2376 &def, sizeof(def)), OK);
2377
2378 // profile
2379 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2380 InitOMXParams(&profile);
2381 profile.nPortIndex = kPortIndexOutput;
2382 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2383 &profile, sizeof(profile)), OK);
2384 profile.nChannels = numChannels;
2385 profile.eChannelMode = (numChannels == 1?
2386 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2387 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002388 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002389 profile.nAudioBandWidth = 0;
2390 profile.nFrameLength = 0;
2391 profile.nAACtools = OMX_AUDIO_AACToolAll;
2392 profile.nAACERtools = OMX_AUDIO_AACERNone;
2393 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2394 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2395 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2396 &profile, sizeof(profile)), OK);
2397
Andreas Huberda050cf22009-09-02 14:01:43 -07002398 } else {
2399 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002400 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002401 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002402
Andreas Huber784202e2009-10-15 13:46:54 -07002403 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002404 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2405 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002406
Andreas Huberda050cf22009-09-02 14:01:43 -07002407 profile.nChannels = numChannels;
2408 profile.nSampleRate = sampleRate;
2409 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002410
Andreas Huber784202e2009-10-15 13:46:54 -07002411 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002412 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2413 CHECK_EQ(err, OK);
2414 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002415}
2416
2417void OMXCodec::setImageOutputFormat(
2418 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002419 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002420
2421#if 0
2422 OMX_INDEXTYPE index;
2423 status_t err = mOMX->get_extension_index(
2424 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2425 CHECK_EQ(err, OK);
2426
2427 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2428 CHECK_EQ(err, OK);
2429#endif
2430
2431 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002432 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002433 def.nPortIndex = kPortIndexOutput;
2434
Andreas Huber784202e2009-10-15 13:46:54 -07002435 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002436 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2437 CHECK_EQ(err, OK);
2438
2439 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2440
2441 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002442
Andreas Huberbe06d262009-08-14 14:37:10 -07002443 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2444 imageDef->eColorFormat = format;
2445 imageDef->nFrameWidth = width;
2446 imageDef->nFrameHeight = height;
2447
2448 switch (format) {
2449 case OMX_COLOR_FormatYUV420PackedPlanar:
2450 case OMX_COLOR_FormatYUV411Planar:
2451 {
2452 def.nBufferSize = (width * height * 3) / 2;
2453 break;
2454 }
2455
2456 case OMX_COLOR_FormatCbYCrY:
2457 {
2458 def.nBufferSize = width * height * 2;
2459 break;
2460 }
2461
2462 case OMX_COLOR_Format32bitARGB8888:
2463 {
2464 def.nBufferSize = width * height * 4;
2465 break;
2466 }
2467
Andreas Huber201511c2009-09-08 14:01:44 -07002468 case OMX_COLOR_Format16bitARGB4444:
2469 case OMX_COLOR_Format16bitARGB1555:
2470 case OMX_COLOR_Format16bitRGB565:
2471 case OMX_COLOR_Format16bitBGR565:
2472 {
2473 def.nBufferSize = width * height * 2;
2474 break;
2475 }
2476
Andreas Huberbe06d262009-08-14 14:37:10 -07002477 default:
2478 CHECK(!"Should not be here. Unknown color format.");
2479 break;
2480 }
2481
Andreas Huber5c0a9132009-08-20 11:16:40 -07002482 def.nBufferCountActual = def.nBufferCountMin;
2483
Andreas Huber784202e2009-10-15 13:46:54 -07002484 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002485 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2486 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002487}
Andreas Huberbe06d262009-08-14 14:37:10 -07002488
Andreas Huber5c0a9132009-08-20 11:16:40 -07002489void OMXCodec::setJPEGInputFormat(
2490 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2491 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002492 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002493 def.nPortIndex = kPortIndexInput;
2494
Andreas Huber784202e2009-10-15 13:46:54 -07002495 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002496 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2497 CHECK_EQ(err, OK);
2498
Andreas Huber5c0a9132009-08-20 11:16:40 -07002499 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2500 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2501
Andreas Huberbe06d262009-08-14 14:37:10 -07002502 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2503 imageDef->nFrameWidth = width;
2504 imageDef->nFrameHeight = height;
2505
Andreas Huber5c0a9132009-08-20 11:16:40 -07002506 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002507 def.nBufferCountActual = def.nBufferCountMin;
2508
Andreas Huber784202e2009-10-15 13:46:54 -07002509 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002510 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2511 CHECK_EQ(err, OK);
2512}
2513
2514void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2515 CodecSpecificData *specific =
2516 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2517
2518 specific->mSize = size;
2519 memcpy(specific->mData, data, size);
2520
2521 mCodecSpecificData.push(specific);
2522}
2523
2524void OMXCodec::clearCodecSpecificData() {
2525 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2526 free(mCodecSpecificData.editItemAt(i));
2527 }
2528 mCodecSpecificData.clear();
2529 mCodecSpecificDataIndex = 0;
2530}
2531
2532status_t OMXCodec::start(MetaData *) {
Andreas Huber42978e52009-08-27 10:08:39 -07002533 Mutex::Autolock autoLock(mLock);
2534
Andreas Huberbe06d262009-08-14 14:37:10 -07002535 if (mState != LOADED) {
2536 return UNKNOWN_ERROR;
2537 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002538
Andreas Huberbe06d262009-08-14 14:37:10 -07002539 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002540 if (mQuirks & kWantsNALFragments) {
2541 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002542 }
2543 status_t err = mSource->start(params.get());
2544
2545 if (err != OK) {
2546 return err;
2547 }
2548
2549 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002550 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002551 mSignalledEOS = false;
2552 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002553 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002554 mSeekTimeUs = -1;
2555 mFilledBuffers.clear();
2556
2557 return init();
2558}
2559
2560status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002561 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002562
2563 Mutex::Autolock autoLock(mLock);
2564
2565 while (isIntermediateState(mState)) {
2566 mAsyncCompletion.wait(mLock);
2567 }
2568
2569 switch (mState) {
2570 case LOADED:
2571 case ERROR:
2572 break;
2573
2574 case EXECUTING:
2575 {
2576 setState(EXECUTING_TO_IDLE);
2577
Andreas Huber127fcdc2009-08-26 16:27:02 -07002578 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002579 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002580 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002581
Andreas Huber127fcdc2009-08-26 16:27:02 -07002582 bool emulateInputFlushCompletion =
2583 !flushPortAsync(kPortIndexInput);
2584
2585 bool emulateOutputFlushCompletion =
2586 !flushPortAsync(kPortIndexOutput);
2587
2588 if (emulateInputFlushCompletion) {
2589 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2590 }
2591
2592 if (emulateOutputFlushCompletion) {
2593 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2594 }
2595 } else {
2596 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2597 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2598
2599 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002600 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002601 CHECK_EQ(err, OK);
2602 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002603
2604 while (mState != LOADED && mState != ERROR) {
2605 mAsyncCompletion.wait(mLock);
2606 }
2607
2608 break;
2609 }
2610
2611 default:
2612 {
2613 CHECK(!"should not be here.");
2614 break;
2615 }
2616 }
2617
Andreas Hubera4357ad2010-04-02 12:49:54 -07002618 if (mLeftOverBuffer) {
2619 mLeftOverBuffer->release();
2620 mLeftOverBuffer = NULL;
2621 }
2622
Andreas Huberbe06d262009-08-14 14:37:10 -07002623 mSource->stop();
2624
Andreas Huber4a9375e2010-02-09 11:54:33 -08002625 CODEC_LOGV("stopped");
2626
Andreas Huberbe06d262009-08-14 14:37:10 -07002627 return OK;
2628}
2629
2630sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002631 Mutex::Autolock autoLock(mLock);
2632
Andreas Huberbe06d262009-08-14 14:37:10 -07002633 return mOutputFormat;
2634}
2635
2636status_t OMXCodec::read(
2637 MediaBuffer **buffer, const ReadOptions *options) {
2638 *buffer = NULL;
2639
2640 Mutex::Autolock autoLock(mLock);
2641
Andreas Huberd06e5b82009-08-28 13:18:14 -07002642 if (mState != EXECUTING && mState != RECONFIGURING) {
2643 return UNKNOWN_ERROR;
2644 }
2645
Andreas Hubere981c332009-10-22 13:49:30 -07002646 bool seeking = false;
2647 int64_t seekTimeUs;
2648 if (options && options->getSeekTo(&seekTimeUs)) {
2649 seeking = true;
2650 }
2651
Andreas Huber42978e52009-08-27 10:08:39 -07002652 if (mInitialBufferSubmit) {
2653 mInitialBufferSubmit = false;
2654
Andreas Hubere981c332009-10-22 13:49:30 -07002655 if (seeking) {
2656 CHECK(seekTimeUs >= 0);
2657 mSeekTimeUs = seekTimeUs;
2658
2659 // There's no reason to trigger the code below, there's
2660 // nothing to flush yet.
2661 seeking = false;
2662 }
2663
Andreas Huber42978e52009-08-27 10:08:39 -07002664 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002665
Andreas Huberd06e5b82009-08-28 13:18:14 -07002666 if (mState == EXECUTING) {
2667 // Otherwise mState == RECONFIGURING and this code will trigger
2668 // after the output port is reenabled.
2669 fillOutputBuffers();
2670 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002671 }
2672
Andreas Hubere981c332009-10-22 13:49:30 -07002673 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002674 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002675
2676 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002677
2678 CHECK(seekTimeUs >= 0);
2679 mSeekTimeUs = seekTimeUs;
2680
2681 mFilledBuffers.clear();
2682
2683 CHECK_EQ(mState, EXECUTING);
2684
Andreas Huber404cc412009-08-25 14:26:05 -07002685 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2686 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2687
2688 if (emulateInputFlushCompletion) {
2689 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2690 }
2691
2692 if (emulateOutputFlushCompletion) {
2693 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2694 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002695
2696 while (mSeekTimeUs >= 0) {
2697 mBufferFilled.wait(mLock);
2698 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002699 }
2700
2701 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2702 mBufferFilled.wait(mLock);
2703 }
2704
2705 if (mState == ERROR) {
2706 return UNKNOWN_ERROR;
2707 }
2708
2709 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002710 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002711 }
2712
Andreas Hubercfd55572009-10-09 14:11:28 -07002713 if (mOutputPortSettingsHaveChanged) {
2714 mOutputPortSettingsHaveChanged = false;
2715
2716 return INFO_FORMAT_CHANGED;
2717 }
2718
Andreas Huberbe06d262009-08-14 14:37:10 -07002719 size_t index = *mFilledBuffers.begin();
2720 mFilledBuffers.erase(mFilledBuffers.begin());
2721
2722 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2723 info->mMediaBuffer->add_ref();
2724 *buffer = info->mMediaBuffer;
2725
2726 return OK;
2727}
2728
2729void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2730 Mutex::Autolock autoLock(mLock);
2731
2732 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2733 for (size_t i = 0; i < buffers->size(); ++i) {
2734 BufferInfo *info = &buffers->editItemAt(i);
2735
2736 if (info->mMediaBuffer == buffer) {
2737 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2738 fillOutputBuffer(info);
2739 return;
2740 }
2741 }
2742
2743 CHECK(!"should not be here.");
2744}
2745
2746static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2747 static const char *kNames[] = {
2748 "OMX_IMAGE_CodingUnused",
2749 "OMX_IMAGE_CodingAutoDetect",
2750 "OMX_IMAGE_CodingJPEG",
2751 "OMX_IMAGE_CodingJPEG2K",
2752 "OMX_IMAGE_CodingEXIF",
2753 "OMX_IMAGE_CodingTIFF",
2754 "OMX_IMAGE_CodingGIF",
2755 "OMX_IMAGE_CodingPNG",
2756 "OMX_IMAGE_CodingLZW",
2757 "OMX_IMAGE_CodingBMP",
2758 };
2759
2760 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2761
2762 if (type < 0 || (size_t)type >= numNames) {
2763 return "UNKNOWN";
2764 } else {
2765 return kNames[type];
2766 }
2767}
2768
2769static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2770 static const char *kNames[] = {
2771 "OMX_COLOR_FormatUnused",
2772 "OMX_COLOR_FormatMonochrome",
2773 "OMX_COLOR_Format8bitRGB332",
2774 "OMX_COLOR_Format12bitRGB444",
2775 "OMX_COLOR_Format16bitARGB4444",
2776 "OMX_COLOR_Format16bitARGB1555",
2777 "OMX_COLOR_Format16bitRGB565",
2778 "OMX_COLOR_Format16bitBGR565",
2779 "OMX_COLOR_Format18bitRGB666",
2780 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002781 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002782 "OMX_COLOR_Format24bitRGB888",
2783 "OMX_COLOR_Format24bitBGR888",
2784 "OMX_COLOR_Format24bitARGB1887",
2785 "OMX_COLOR_Format25bitARGB1888",
2786 "OMX_COLOR_Format32bitBGRA8888",
2787 "OMX_COLOR_Format32bitARGB8888",
2788 "OMX_COLOR_FormatYUV411Planar",
2789 "OMX_COLOR_FormatYUV411PackedPlanar",
2790 "OMX_COLOR_FormatYUV420Planar",
2791 "OMX_COLOR_FormatYUV420PackedPlanar",
2792 "OMX_COLOR_FormatYUV420SemiPlanar",
2793 "OMX_COLOR_FormatYUV422Planar",
2794 "OMX_COLOR_FormatYUV422PackedPlanar",
2795 "OMX_COLOR_FormatYUV422SemiPlanar",
2796 "OMX_COLOR_FormatYCbYCr",
2797 "OMX_COLOR_FormatYCrYCb",
2798 "OMX_COLOR_FormatCbYCrY",
2799 "OMX_COLOR_FormatCrYCbY",
2800 "OMX_COLOR_FormatYUV444Interleaved",
2801 "OMX_COLOR_FormatRawBayer8bit",
2802 "OMX_COLOR_FormatRawBayer10bit",
2803 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002804 "OMX_COLOR_FormatL2",
2805 "OMX_COLOR_FormatL4",
2806 "OMX_COLOR_FormatL8",
2807 "OMX_COLOR_FormatL16",
2808 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002809 "OMX_COLOR_FormatL32",
2810 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2811 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2812 "OMX_COLOR_Format18BitBGR666",
2813 "OMX_COLOR_Format24BitARGB6666",
2814 "OMX_COLOR_Format24BitABGR6666",
2815 };
2816
2817 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2818
Andreas Huberbe06d262009-08-14 14:37:10 -07002819 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2820 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2821 } else if (type < 0 || (size_t)type >= numNames) {
2822 return "UNKNOWN";
2823 } else {
2824 return kNames[type];
2825 }
2826}
2827
2828static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2829 static const char *kNames[] = {
2830 "OMX_VIDEO_CodingUnused",
2831 "OMX_VIDEO_CodingAutoDetect",
2832 "OMX_VIDEO_CodingMPEG2",
2833 "OMX_VIDEO_CodingH263",
2834 "OMX_VIDEO_CodingMPEG4",
2835 "OMX_VIDEO_CodingWMV",
2836 "OMX_VIDEO_CodingRV",
2837 "OMX_VIDEO_CodingAVC",
2838 "OMX_VIDEO_CodingMJPEG",
2839 };
2840
2841 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2842
2843 if (type < 0 || (size_t)type >= numNames) {
2844 return "UNKNOWN";
2845 } else {
2846 return kNames[type];
2847 }
2848}
2849
2850static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2851 static const char *kNames[] = {
2852 "OMX_AUDIO_CodingUnused",
2853 "OMX_AUDIO_CodingAutoDetect",
2854 "OMX_AUDIO_CodingPCM",
2855 "OMX_AUDIO_CodingADPCM",
2856 "OMX_AUDIO_CodingAMR",
2857 "OMX_AUDIO_CodingGSMFR",
2858 "OMX_AUDIO_CodingGSMEFR",
2859 "OMX_AUDIO_CodingGSMHR",
2860 "OMX_AUDIO_CodingPDCFR",
2861 "OMX_AUDIO_CodingPDCEFR",
2862 "OMX_AUDIO_CodingPDCHR",
2863 "OMX_AUDIO_CodingTDMAFR",
2864 "OMX_AUDIO_CodingTDMAEFR",
2865 "OMX_AUDIO_CodingQCELP8",
2866 "OMX_AUDIO_CodingQCELP13",
2867 "OMX_AUDIO_CodingEVRC",
2868 "OMX_AUDIO_CodingSMV",
2869 "OMX_AUDIO_CodingG711",
2870 "OMX_AUDIO_CodingG723",
2871 "OMX_AUDIO_CodingG726",
2872 "OMX_AUDIO_CodingG729",
2873 "OMX_AUDIO_CodingAAC",
2874 "OMX_AUDIO_CodingMP3",
2875 "OMX_AUDIO_CodingSBC",
2876 "OMX_AUDIO_CodingVORBIS",
2877 "OMX_AUDIO_CodingWMA",
2878 "OMX_AUDIO_CodingRA",
2879 "OMX_AUDIO_CodingMIDI",
2880 };
2881
2882 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2883
2884 if (type < 0 || (size_t)type >= numNames) {
2885 return "UNKNOWN";
2886 } else {
2887 return kNames[type];
2888 }
2889}
2890
2891static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2892 static const char *kNames[] = {
2893 "OMX_AUDIO_PCMModeLinear",
2894 "OMX_AUDIO_PCMModeALaw",
2895 "OMX_AUDIO_PCMModeMULaw",
2896 };
2897
2898 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2899
2900 if (type < 0 || (size_t)type >= numNames) {
2901 return "UNKNOWN";
2902 } else {
2903 return kNames[type];
2904 }
2905}
2906
Andreas Huber7ae02c82009-09-09 16:29:47 -07002907static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2908 static const char *kNames[] = {
2909 "OMX_AUDIO_AMRBandModeUnused",
2910 "OMX_AUDIO_AMRBandModeNB0",
2911 "OMX_AUDIO_AMRBandModeNB1",
2912 "OMX_AUDIO_AMRBandModeNB2",
2913 "OMX_AUDIO_AMRBandModeNB3",
2914 "OMX_AUDIO_AMRBandModeNB4",
2915 "OMX_AUDIO_AMRBandModeNB5",
2916 "OMX_AUDIO_AMRBandModeNB6",
2917 "OMX_AUDIO_AMRBandModeNB7",
2918 "OMX_AUDIO_AMRBandModeWB0",
2919 "OMX_AUDIO_AMRBandModeWB1",
2920 "OMX_AUDIO_AMRBandModeWB2",
2921 "OMX_AUDIO_AMRBandModeWB3",
2922 "OMX_AUDIO_AMRBandModeWB4",
2923 "OMX_AUDIO_AMRBandModeWB5",
2924 "OMX_AUDIO_AMRBandModeWB6",
2925 "OMX_AUDIO_AMRBandModeWB7",
2926 "OMX_AUDIO_AMRBandModeWB8",
2927 };
2928
2929 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2930
2931 if (type < 0 || (size_t)type >= numNames) {
2932 return "UNKNOWN";
2933 } else {
2934 return kNames[type];
2935 }
2936}
2937
2938static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2939 static const char *kNames[] = {
2940 "OMX_AUDIO_AMRFrameFormatConformance",
2941 "OMX_AUDIO_AMRFrameFormatIF1",
2942 "OMX_AUDIO_AMRFrameFormatIF2",
2943 "OMX_AUDIO_AMRFrameFormatFSF",
2944 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2945 "OMX_AUDIO_AMRFrameFormatITU",
2946 };
2947
2948 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2949
2950 if (type < 0 || (size_t)type >= numNames) {
2951 return "UNKNOWN";
2952 } else {
2953 return kNames[type];
2954 }
2955}
Andreas Huberbe06d262009-08-14 14:37:10 -07002956
2957void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2958 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002959 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002960 def.nPortIndex = portIndex;
2961
Andreas Huber784202e2009-10-15 13:46:54 -07002962 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002963 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2964 CHECK_EQ(err, OK);
2965
2966 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2967
2968 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2969 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2970
2971 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2972 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2973 printf(" nBufferSize = %ld\n", def.nBufferSize);
2974
2975 switch (def.eDomain) {
2976 case OMX_PortDomainImage:
2977 {
2978 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2979
2980 printf("\n");
2981 printf(" // Image\n");
2982 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
2983 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
2984 printf(" nStride = %ld\n", imageDef->nStride);
2985
2986 printf(" eCompressionFormat = %s\n",
2987 imageCompressionFormatString(imageDef->eCompressionFormat));
2988
2989 printf(" eColorFormat = %s\n",
2990 colorFormatString(imageDef->eColorFormat));
2991
2992 break;
2993 }
2994
2995 case OMX_PortDomainVideo:
2996 {
2997 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2998
2999 printf("\n");
3000 printf(" // Video\n");
3001 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3002 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3003 printf(" nStride = %ld\n", videoDef->nStride);
3004
3005 printf(" eCompressionFormat = %s\n",
3006 videoCompressionFormatString(videoDef->eCompressionFormat));
3007
3008 printf(" eColorFormat = %s\n",
3009 colorFormatString(videoDef->eColorFormat));
3010
3011 break;
3012 }
3013
3014 case OMX_PortDomainAudio:
3015 {
3016 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3017
3018 printf("\n");
3019 printf(" // Audio\n");
3020 printf(" eEncoding = %s\n",
3021 audioCodingTypeString(audioDef->eEncoding));
3022
3023 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3024 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003025 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003026 params.nPortIndex = portIndex;
3027
Andreas Huber784202e2009-10-15 13:46:54 -07003028 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003029 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3030 CHECK_EQ(err, OK);
3031
3032 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3033 printf(" nChannels = %ld\n", params.nChannels);
3034 printf(" bInterleaved = %d\n", params.bInterleaved);
3035 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3036
3037 printf(" eNumData = %s\n",
3038 params.eNumData == OMX_NumericalDataSigned
3039 ? "signed" : "unsigned");
3040
3041 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003042 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3043 OMX_AUDIO_PARAM_AMRTYPE amr;
3044 InitOMXParams(&amr);
3045 amr.nPortIndex = portIndex;
3046
Andreas Huber784202e2009-10-15 13:46:54 -07003047 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003048 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3049 CHECK_EQ(err, OK);
3050
3051 printf(" nChannels = %ld\n", amr.nChannels);
3052 printf(" eAMRBandMode = %s\n",
3053 amrBandModeString(amr.eAMRBandMode));
3054 printf(" eAMRFrameFormat = %s\n",
3055 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003056 }
3057
3058 break;
3059 }
3060
3061 default:
3062 {
3063 printf(" // Unknown\n");
3064 break;
3065 }
3066 }
3067
3068 printf("}\n");
3069}
3070
3071void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3072 mOutputFormat = new MetaData;
3073 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
3074
3075 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003076 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003077 def.nPortIndex = kPortIndexOutput;
3078
Andreas Huber784202e2009-10-15 13:46:54 -07003079 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003080 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3081 CHECK_EQ(err, OK);
3082
3083 switch (def.eDomain) {
3084 case OMX_PortDomainImage:
3085 {
3086 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3087 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3088
Andreas Hubere6c40962009-09-10 14:13:30 -07003089 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003090 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3091 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3092 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3093 break;
3094 }
3095
3096 case OMX_PortDomainAudio:
3097 {
3098 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3099
Andreas Huberda050cf22009-09-02 14:01:43 -07003100 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3101 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003102 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003103 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003104
Andreas Huber784202e2009-10-15 13:46:54 -07003105 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003106 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3107 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003108
Andreas Huberda050cf22009-09-02 14:01:43 -07003109 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3110 CHECK_EQ(params.nBitPerSample, 16);
3111 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003112
Andreas Huberda050cf22009-09-02 14:01:43 -07003113 int32_t numChannels, sampleRate;
3114 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3115 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003116
Andreas Huberda050cf22009-09-02 14:01:43 -07003117 if ((OMX_U32)numChannels != params.nChannels) {
3118 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003119 "the input stream contains (contains %d channels, "
3120 "codec outputs %ld channels).",
3121 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003122 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003123
Andreas Hubere6c40962009-09-10 14:13:30 -07003124 mOutputFormat->setCString(
3125 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003126
3127 // Use the codec-advertised number of channels, as some
3128 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003129 // mono. If we know the codec lies about this information,
3130 // use the actual number of channels instead.
3131 mOutputFormat->setInt32(
3132 kKeyChannelCount,
3133 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3134 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003135
3136 // The codec-reported sampleRate is not reliable...
3137 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3138 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003139 OMX_AUDIO_PARAM_AMRTYPE amr;
3140 InitOMXParams(&amr);
3141 amr.nPortIndex = kPortIndexOutput;
3142
Andreas Huber784202e2009-10-15 13:46:54 -07003143 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003144 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3145 CHECK_EQ(err, OK);
3146
3147 CHECK_EQ(amr.nChannels, 1);
3148 mOutputFormat->setInt32(kKeyChannelCount, 1);
3149
3150 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3151 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003152 mOutputFormat->setCString(
3153 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003154 mOutputFormat->setInt32(kKeySampleRate, 8000);
3155 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3156 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003157 mOutputFormat->setCString(
3158 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003159 mOutputFormat->setInt32(kKeySampleRate, 16000);
3160 } else {
3161 CHECK(!"Unknown AMR band mode.");
3162 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003163 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003164 mOutputFormat->setCString(
3165 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003166 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003167 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3168 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003169 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003170 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3171 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003172 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003173 } else {
3174 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003175 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003176 break;
3177 }
3178
3179 case OMX_PortDomainVideo:
3180 {
3181 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3182
3183 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003184 mOutputFormat->setCString(
3185 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003186 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003187 mOutputFormat->setCString(
3188 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003189 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003190 mOutputFormat->setCString(
3191 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003192 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003193 mOutputFormat->setCString(
3194 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003195 } else {
3196 CHECK(!"Unknown compression format.");
3197 }
3198
3199 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3200 // This component appears to be lying to me.
3201 mOutputFormat->setInt32(
3202 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3203 mOutputFormat->setInt32(
3204 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3205 } else {
3206 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3207 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3208 }
3209
3210 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3211 break;
3212 }
3213
3214 default:
3215 {
3216 CHECK(!"should not be here, neither audio nor video.");
3217 break;
3218 }
3219 }
3220}
3221
Andreas Hubere6c40962009-09-10 14:13:30 -07003222////////////////////////////////////////////////////////////////////////////////
3223
3224status_t QueryCodecs(
3225 const sp<IOMX> &omx,
3226 const char *mime, bool queryDecoders,
3227 Vector<CodecCapabilities> *results) {
3228 results->clear();
3229
3230 for (int index = 0;; ++index) {
3231 const char *componentName;
3232
3233 if (!queryDecoders) {
3234 componentName = GetCodec(
3235 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3236 mime, index);
3237 } else {
3238 componentName = GetCodec(
3239 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3240 mime, index);
3241 }
3242
3243 if (!componentName) {
3244 return OK;
3245 }
3246
Andreas Huber1a189a82010-03-24 13:49:20 -07003247 if (strncmp(componentName, "OMX.", 4)) {
3248 // Not an OpenMax component but a software codec.
3249
3250 results->push();
3251 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3252 caps->mComponentName = componentName;
3253
3254 continue;
3255 }
3256
Andreas Huber784202e2009-10-15 13:46:54 -07003257 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003258 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003259 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003260
3261 if (err != OK) {
3262 continue;
3263 }
3264
James Dong722d5912010-04-13 10:56:59 -07003265 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003266
3267 results->push();
3268 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3269 caps->mComponentName = componentName;
3270
3271 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3272 InitOMXParams(&param);
3273
3274 param.nPortIndex = queryDecoders ? 0 : 1;
3275
3276 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003277 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003278 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3279 &param, sizeof(param));
3280
3281 if (err != OK) {
3282 break;
3283 }
3284
3285 CodecProfileLevel profileLevel;
3286 profileLevel.mProfile = param.eProfile;
3287 profileLevel.mLevel = param.eLevel;
3288
3289 caps->mProfileLevels.push(profileLevel);
3290 }
3291
Andreas Huber784202e2009-10-15 13:46:54 -07003292 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003293 }
3294}
3295
Andreas Huberbe06d262009-08-14 14:37:10 -07003296} // namespace android