blob: 8979c3bc14626d5e5ebe011b2bcc4a51bfad623e [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 Dongdca66e12010-06-14 11:14:38 -0700350 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700351 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 Hubere2f85072010-06-10 09:51:27 -0700541 CODEC_LOGV(
542 "AVC profile = %d (%s), level = %d",
543 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700544
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700545 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Hubere2f85072010-06-10 09:51:27 -0700546 && (profile != kAVCProfileBaseline || level > 30)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700547 // This stream exceeds the decoder's capabilities. The decoder
548 // does not handle this gracefully and would clobber the heap
549 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700550
551 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700552 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700553 }
554 }
555
James Dong17299ab2010-05-14 15:45:22 -0700556 int32_t bitRate = 0;
557 if (mIsEncoder) {
558 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
559 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700560 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700561 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700562 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700563 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700564 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700565 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700566 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700567 int32_t numChannels, sampleRate;
568 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
569 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
570
James Dong17299ab2010-05-14 15:45:22 -0700571 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700572 }
James Dongabed93a2010-04-22 17:27:04 -0700573
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700574 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700575
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700576 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700577 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700578 } else {
James Dong1244eab2010-06-08 11:58:53 -0700579 int32_t width, height;
580 bool success = meta->findInt32(kKeyWidth, &width);
581 success = success && meta->findInt32(kKeyHeight, &height);
582 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700583 status_t err = setVideoOutputFormat(
584 mMIME, width, height);
585
586 if (err != OK) {
587 return err;
588 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700589 }
590 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700591
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700592 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
593 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700594 OMX_COLOR_FORMATTYPE format =
595 OMX_COLOR_Format32bitARGB8888;
596 // OMX_COLOR_FormatYUV420PackedPlanar;
597 // OMX_COLOR_FormatCbYCrY;
598 // OMX_COLOR_FormatYUV411Planar;
599
600 int32_t width, height;
601 bool success = meta->findInt32(kKeyWidth, &width);
602 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700603
604 int32_t compressedSize;
605 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700606 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700607
608 CHECK(success);
609 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700610
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700611 setImageOutputFormat(format, width, height);
612 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700613 }
614
Andreas Huberda050cf22009-09-02 14:01:43 -0700615 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800616 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700617 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700618 }
619
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700620 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700621 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
622 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700623 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700624 }
625
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700626 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700627
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700628 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700629}
630
Andreas Huberda050cf22009-09-02 14:01:43 -0700631void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
632 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700633 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700634 def.nPortIndex = portIndex;
635
Andreas Huber784202e2009-10-15 13:46:54 -0700636 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700637 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
638 CHECK_EQ(err, OK);
639
Andreas Huberb8de9572010-02-22 14:58:45 -0800640 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
641 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700642 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700643 }
644
Andreas Huber784202e2009-10-15 13:46:54 -0700645 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700646 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
647 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800648
649 err = mOMX->getParameter(
650 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
651 CHECK_EQ(err, OK);
652
653 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800654 if (portIndex == kPortIndexInput
655 && (mQuirks & kInputBufferSizesAreBogus)) {
656 CHECK_EQ(def.nBufferSize, size);
657 } else {
658 CHECK(def.nBufferSize >= size);
659 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700660}
661
Andreas Huberbe06d262009-08-14 14:37:10 -0700662status_t OMXCodec::setVideoPortFormatType(
663 OMX_U32 portIndex,
664 OMX_VIDEO_CODINGTYPE compressionFormat,
665 OMX_COLOR_FORMATTYPE colorFormat) {
666 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700667 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700668 format.nPortIndex = portIndex;
669 format.nIndex = 0;
670 bool found = false;
671
672 OMX_U32 index = 0;
673 for (;;) {
674 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700675 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700676 mNode, OMX_IndexParamVideoPortFormat,
677 &format, sizeof(format));
678
679 if (err != OK) {
680 return err;
681 }
682
683 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700684 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700685
686#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700687 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700688 portIndex,
689 index, format.eCompressionFormat, format.eColorFormat);
690#endif
691
692 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
693 if (portIndex == kPortIndexInput
694 && colorFormat == format.eColorFormat) {
695 // eCompressionFormat does not seem right.
696 found = true;
697 break;
698 }
699 if (portIndex == kPortIndexOutput
700 && compressionFormat == format.eCompressionFormat) {
701 // eColorFormat does not seem right.
702 found = true;
703 break;
704 }
705 }
706
707 if (format.eCompressionFormat == compressionFormat
708 && format.eColorFormat == colorFormat) {
709 found = true;
710 break;
711 }
712
713 ++index;
714 }
715
716 if (!found) {
717 return UNKNOWN_ERROR;
718 }
719
Andreas Huber53a76bd2009-10-06 16:20:44 -0700720 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700721 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700722 mNode, OMX_IndexParamVideoPortFormat,
723 &format, sizeof(format));
724
725 return err;
726}
727
Andreas Huberb482ce82009-10-29 12:02:48 -0700728static size_t getFrameSize(
729 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
730 switch (colorFormat) {
731 case OMX_COLOR_FormatYCbYCr:
732 case OMX_COLOR_FormatCbYCrY:
733 return width * height * 2;
734
Andreas Huber71c27d92010-03-19 11:43:15 -0700735 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700736 case OMX_COLOR_FormatYUV420SemiPlanar:
737 return (width * height * 3) / 2;
738
739 default:
740 CHECK(!"Should not be here. Unsupported color format.");
741 break;
742 }
743}
744
Andreas Huberbe06d262009-08-14 14:37:10 -0700745void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700746 const char *mime, const sp<MetaData>& meta) {
747
748 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
749 bool success = meta->findInt32(kKeyWidth, &width);
750 success = success && meta->findInt32(kKeyHeight, &height);
751 success = success && meta->findInt32(kKeySampleRate, &frameRate);
752 success = success && meta->findInt32(kKeyBitRate, &bitRate);
753 success = success && meta->findInt32(kKeyStride, &stride);
754 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
755 CHECK(success);
756 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700757
758 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700759 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700760 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700761 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700762 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700763 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700764 compressionFormat = OMX_VIDEO_CodingH263;
765 } else {
766 LOGE("Not a supported video mime type: %s", mime);
767 CHECK(!"Should not be here. Not a supported video mime type.");
768 }
769
Andreas Huberea6a38c2009-11-16 15:43:38 -0800770 OMX_COLOR_FORMATTYPE colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
771 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
James Dongabed93a2010-04-22 17:27:04 -0700772 colorFormat = OMX_COLOR_FormatYCbYCr;
Andreas Huberbe06d262009-08-14 14:37:10 -0700773 }
774
James Dongb00e2462010-04-26 17:48:26 -0700775 status_t err;
776 OMX_PARAM_PORTDEFINITIONTYPE def;
777 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
778
779 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700780 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700781 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700782 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700783
James Dongb00e2462010-04-26 17:48:26 -0700784 InitOMXParams(&def);
785 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700786
James Dongb00e2462010-04-26 17:48:26 -0700787 err = mOMX->getParameter(
788 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
789 CHECK_EQ(err, OK);
790
James Dong1244eab2010-06-08 11:58:53 -0700791 def.nBufferSize = getFrameSize(colorFormat,
792 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700793
794 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
795
796 video_def->nFrameWidth = width;
797 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700798 video_def->nStride = stride;
799 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700800 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700801 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
802 video_def->eColorFormat = colorFormat;
803
James Dongb00e2462010-04-26 17:48:26 -0700804 err = mOMX->setParameter(
805 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
806 CHECK_EQ(err, OK);
807
808 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700809 CHECK_EQ(setVideoPortFormatType(
810 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
811 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700812 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700813 def.nPortIndex = kPortIndexOutput;
814
James Dongb00e2462010-04-26 17:48:26 -0700815 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700816 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
817
818 CHECK_EQ(err, OK);
819 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
820
821 video_def->nFrameWidth = width;
822 video_def->nFrameHeight = height;
James Dong4f501f02010-06-07 14:41:41 -0700823 video_def->xFramerate = (frameRate << 16); // Q16 format
824 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700825 video_def->eCompressionFormat = compressionFormat;
826 video_def->eColorFormat = OMX_COLOR_FormatUnused;
827
Andreas Huber784202e2009-10-15 13:46:54 -0700828 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700829 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
830 CHECK_EQ(err, OK);
831
James Dongb00e2462010-04-26 17:48:26 -0700832 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700833 switch (compressionFormat) {
834 case OMX_VIDEO_CodingMPEG4:
835 {
James Dong1244eab2010-06-08 11:58:53 -0700836 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700837 break;
838 }
839
840 case OMX_VIDEO_CodingH263:
841 break;
842
Andreas Huberea6a38c2009-11-16 15:43:38 -0800843 case OMX_VIDEO_CodingAVC:
844 {
James Dong1244eab2010-06-08 11:58:53 -0700845 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -0800846 break;
847 }
848
Andreas Huberb482ce82009-10-29 12:02:48 -0700849 default:
850 CHECK(!"Support for this compressionFormat to be implemented.");
851 break;
852 }
853}
854
James Dong1244eab2010-06-08 11:58:53 -0700855static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
856 if (iFramesInterval < 0) {
857 return 0xFFFFFFFF;
858 } else if (iFramesInterval == 0) {
859 return 0;
860 }
861 OMX_U32 ret = frameRate * iFramesInterval;
862 CHECK(ret > 1);
863 return ret;
864}
865
866status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
867 int32_t iFramesInterval, frameRate, bitRate;
868 bool success = meta->findInt32(kKeyBitRate, &bitRate);
869 success = success && meta->findInt32(kKeySampleRate, &frameRate);
870 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
871 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -0700872 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
873 InitOMXParams(&mpeg4type);
874 mpeg4type.nPortIndex = kPortIndexOutput;
875
876 status_t err = mOMX->getParameter(
877 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
878 CHECK_EQ(err, OK);
879
880 mpeg4type.nSliceHeaderSpacing = 0;
881 mpeg4type.bSVH = OMX_FALSE;
882 mpeg4type.bGov = OMX_FALSE;
883
884 mpeg4type.nAllowedPictureTypes =
885 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
886
James Dong1244eab2010-06-08 11:58:53 -0700887 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
888 if (mpeg4type.nPFrames == 0) {
889 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
890 }
Andreas Huberb482ce82009-10-29 12:02:48 -0700891 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -0700892 mpeg4type.nIDCVLCThreshold = 0;
893 mpeg4type.bACPred = OMX_TRUE;
894 mpeg4type.nMaxPacketSize = 256;
895 mpeg4type.nTimeIncRes = 1000;
896 mpeg4type.nHeaderExtension = 0;
897 mpeg4type.bReversibleVLC = OMX_FALSE;
898
899 mpeg4type.eProfile = OMX_VIDEO_MPEG4ProfileCore;
900 mpeg4type.eLevel = OMX_VIDEO_MPEG4Level2;
901
902 err = mOMX->setParameter(
903 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
904 CHECK_EQ(err, OK);
905
906 // ----------------
907
908 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
909 InitOMXParams(&bitrateType);
910 bitrateType.nPortIndex = kPortIndexOutput;
911
912 err = mOMX->getParameter(
913 mNode, OMX_IndexParamVideoBitrate,
914 &bitrateType, sizeof(bitrateType));
915 CHECK_EQ(err, OK);
916
917 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
James Dong1244eab2010-06-08 11:58:53 -0700918 bitrateType.nTargetBitrate = bitRate;
Andreas Huberb482ce82009-10-29 12:02:48 -0700919
920 err = mOMX->setParameter(
921 mNode, OMX_IndexParamVideoBitrate,
922 &bitrateType, sizeof(bitrateType));
923 CHECK_EQ(err, OK);
924
925 // ----------------
926
927 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
928 InitOMXParams(&errorCorrectionType);
929 errorCorrectionType.nPortIndex = kPortIndexOutput;
930
931 err = mOMX->getParameter(
932 mNode, OMX_IndexParamVideoErrorCorrection,
933 &errorCorrectionType, sizeof(errorCorrectionType));
934 CHECK_EQ(err, OK);
935
936 errorCorrectionType.bEnableHEC = OMX_FALSE;
937 errorCorrectionType.bEnableResync = OMX_TRUE;
938 errorCorrectionType.nResynchMarkerSpacing = 256;
939 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
940 errorCorrectionType.bEnableRVLC = OMX_FALSE;
941
942 err = mOMX->setParameter(
943 mNode, OMX_IndexParamVideoErrorCorrection,
944 &errorCorrectionType, sizeof(errorCorrectionType));
945 CHECK_EQ(err, OK);
946
947 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700948}
949
James Dong1244eab2010-06-08 11:58:53 -0700950status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
951 int32_t iFramesInterval, frameRate, bitRate;
952 bool success = meta->findInt32(kKeyBitRate, &bitRate);
953 success = success && meta->findInt32(kKeySampleRate, &frameRate);
954 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
955 CHECK(success);
956
Andreas Huberea6a38c2009-11-16 15:43:38 -0800957 OMX_VIDEO_PARAM_AVCTYPE h264type;
958 InitOMXParams(&h264type);
959 h264type.nPortIndex = kPortIndexOutput;
960
961 status_t err = mOMX->getParameter(
962 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
963 CHECK_EQ(err, OK);
964
965 h264type.nAllowedPictureTypes =
966 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
967
968 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -0700969 h264type.nBFrames = 0; // No B frames support yet
970 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
971 if (h264type.nPFrames == 0) {
972 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
973 }
Andreas Huberea6a38c2009-11-16 15:43:38 -0800974 h264type.bUseHadamard = OMX_TRUE;
975 h264type.nRefFrames = 1;
976 h264type.nRefIdx10ActiveMinus1 = 0;
977 h264type.nRefIdx11ActiveMinus1 = 0;
978 h264type.bEnableUEP = OMX_FALSE;
979 h264type.bEnableFMO = OMX_FALSE;
980 h264type.bEnableASO = OMX_FALSE;
981 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -0800982 h264type.bFrameMBsOnly = OMX_TRUE;
983 h264type.bMBAFF = OMX_FALSE;
984 h264type.bEntropyCodingCABAC = OMX_FALSE;
985 h264type.bWeightedPPrediction = OMX_FALSE;
986 h264type.bconstIpred = OMX_FALSE;
987 h264type.bDirect8x8Inference = OMX_FALSE;
988 h264type.bDirectSpatialTemporal = OMX_FALSE;
989 h264type.nCabacInitIdc = 0;
990 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
991
992 err = mOMX->setParameter(
993 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
994 CHECK_EQ(err, OK);
995
996 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
997 InitOMXParams(&bitrateType);
998 bitrateType.nPortIndex = kPortIndexOutput;
999
1000 err = mOMX->getParameter(
1001 mNode, OMX_IndexParamVideoBitrate,
1002 &bitrateType, sizeof(bitrateType));
1003 CHECK_EQ(err, OK);
1004
1005 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
James Dong1244eab2010-06-08 11:58:53 -07001006 bitrateType.nTargetBitrate = bitRate;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001007
1008 err = mOMX->setParameter(
1009 mNode, OMX_IndexParamVideoBitrate,
1010 &bitrateType, sizeof(bitrateType));
1011 CHECK_EQ(err, OK);
1012
1013 return OK;
1014}
1015
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001016status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001017 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001018 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001019
Andreas Huberbe06d262009-08-14 14:37:10 -07001020 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001021 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001022 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001023 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001024 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001025 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001026 compressionFormat = OMX_VIDEO_CodingH263;
1027 } else {
1028 LOGE("Not a supported video mime type: %s", mime);
1029 CHECK(!"Should not be here. Not a supported video mime type.");
1030 }
1031
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001032 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001033 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1034
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001035 if (err != OK) {
1036 return err;
1037 }
1038
Andreas Huberbe06d262009-08-14 14:37:10 -07001039#if 1
1040 {
1041 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001042 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001043 format.nPortIndex = kPortIndexOutput;
1044 format.nIndex = 0;
1045
Andreas Huber784202e2009-10-15 13:46:54 -07001046 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001047 mNode, OMX_IndexParamVideoPortFormat,
1048 &format, sizeof(format));
1049 CHECK_EQ(err, OK);
1050 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1051
1052 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1053
1054 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1055 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1056 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1057 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1058
Andreas Huber784202e2009-10-15 13:46:54 -07001059 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001060 mNode, OMX_IndexParamVideoPortFormat,
1061 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001062
1063 if (err != OK) {
1064 return err;
1065 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001066 }
1067#endif
1068
1069 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001070 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001071 def.nPortIndex = kPortIndexInput;
1072
Andreas Huber4c483422009-09-02 16:05:36 -07001073 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1074
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001075 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001076 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1077
1078 CHECK_EQ(err, OK);
1079
1080#if 1
1081 // XXX Need a (much) better heuristic to compute input buffer sizes.
1082 const size_t X = 64 * 1024;
1083 if (def.nBufferSize < X) {
1084 def.nBufferSize = X;
1085 }
1086#endif
1087
1088 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1089
1090 video_def->nFrameWidth = width;
1091 video_def->nFrameHeight = height;
1092
Andreas Huberb482ce82009-10-29 12:02:48 -07001093 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001094 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1095
Andreas Huber784202e2009-10-15 13:46:54 -07001096 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001097 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001098
1099 if (err != OK) {
1100 return err;
1101 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001102
1103 ////////////////////////////////////////////////////////////////////////////
1104
Andreas Huber4c483422009-09-02 16:05:36 -07001105 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001106 def.nPortIndex = kPortIndexOutput;
1107
Andreas Huber784202e2009-10-15 13:46:54 -07001108 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001109 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1110 CHECK_EQ(err, OK);
1111 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1112
1113#if 0
1114 def.nBufferSize =
1115 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1116#endif
1117
1118 video_def->nFrameWidth = width;
1119 video_def->nFrameHeight = height;
1120
Andreas Huber784202e2009-10-15 13:46:54 -07001121 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001122 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001123
1124 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001125}
1126
Andreas Huberbe06d262009-08-14 14:37:10 -07001127OMXCodec::OMXCodec(
1128 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001129 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001130 const char *mime,
1131 const char *componentName,
1132 const sp<MediaSource> &source)
1133 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001134 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001135 mNode(node),
1136 mQuirks(quirks),
1137 mIsEncoder(isEncoder),
1138 mMIME(strdup(mime)),
1139 mComponentName(strdup(componentName)),
1140 mSource(source),
1141 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001142 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001143 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001144 mSignalledEOS(false),
1145 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001146 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001147 mSeekTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001148 mLeftOverBuffer(NULL),
1149 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001150 mPortStatus[kPortIndexInput] = ENABLED;
1151 mPortStatus[kPortIndexOutput] = ENABLED;
1152
Andreas Huber4c483422009-09-02 16:05:36 -07001153 setComponentRole();
1154}
1155
Andreas Hubere6c40962009-09-10 14:13:30 -07001156// static
1157void OMXCodec::setComponentRole(
1158 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1159 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001160 struct MimeToRole {
1161 const char *mime;
1162 const char *decoderRole;
1163 const char *encoderRole;
1164 };
1165
1166 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001167 { MEDIA_MIMETYPE_AUDIO_MPEG,
1168 "audio_decoder.mp3", "audio_encoder.mp3" },
1169 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1170 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1171 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1172 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1173 { MEDIA_MIMETYPE_AUDIO_AAC,
1174 "audio_decoder.aac", "audio_encoder.aac" },
1175 { MEDIA_MIMETYPE_VIDEO_AVC,
1176 "video_decoder.avc", "video_encoder.avc" },
1177 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1178 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1179 { MEDIA_MIMETYPE_VIDEO_H263,
1180 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001181 };
1182
1183 static const size_t kNumMimeToRole =
1184 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1185
1186 size_t i;
1187 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001188 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001189 break;
1190 }
1191 }
1192
1193 if (i == kNumMimeToRole) {
1194 return;
1195 }
1196
1197 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001198 isEncoder ? kMimeToRole[i].encoderRole
1199 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001200
1201 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001202 OMX_PARAM_COMPONENTROLETYPE roleParams;
1203 InitOMXParams(&roleParams);
1204
1205 strncpy((char *)roleParams.cRole,
1206 role, OMX_MAX_STRINGNAME_SIZE - 1);
1207
1208 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1209
Andreas Huber784202e2009-10-15 13:46:54 -07001210 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001211 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001212 &roleParams, sizeof(roleParams));
1213
1214 if (err != OK) {
1215 LOGW("Failed to set standard component role '%s'.", role);
1216 }
1217 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001218}
1219
Andreas Hubere6c40962009-09-10 14:13:30 -07001220void OMXCodec::setComponentRole() {
1221 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1222}
1223
Andreas Huberbe06d262009-08-14 14:37:10 -07001224OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001225 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001226
Andreas Huber784202e2009-10-15 13:46:54 -07001227 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001228 CHECK_EQ(err, OK);
1229
1230 mNode = NULL;
1231 setState(DEAD);
1232
1233 clearCodecSpecificData();
1234
1235 free(mComponentName);
1236 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001237
Andreas Huberbe06d262009-08-14 14:37:10 -07001238 free(mMIME);
1239 mMIME = NULL;
1240}
1241
1242status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001243 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001244
1245 CHECK_EQ(mState, LOADED);
1246
1247 status_t err;
1248 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001249 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001250 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001251 setState(LOADED_TO_IDLE);
1252 }
1253
1254 err = allocateBuffers();
1255 CHECK_EQ(err, OK);
1256
1257 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001258 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001259 CHECK_EQ(err, OK);
1260
1261 setState(LOADED_TO_IDLE);
1262 }
1263
1264 while (mState != EXECUTING && mState != ERROR) {
1265 mAsyncCompletion.wait(mLock);
1266 }
1267
1268 return mState == ERROR ? UNKNOWN_ERROR : OK;
1269}
1270
1271// static
1272bool OMXCodec::isIntermediateState(State state) {
1273 return state == LOADED_TO_IDLE
1274 || state == IDLE_TO_EXECUTING
1275 || state == EXECUTING_TO_IDLE
1276 || state == IDLE_TO_LOADED
1277 || state == RECONFIGURING;
1278}
1279
1280status_t OMXCodec::allocateBuffers() {
1281 status_t err = allocateBuffersOnPort(kPortIndexInput);
1282
1283 if (err != OK) {
1284 return err;
1285 }
1286
1287 return allocateBuffersOnPort(kPortIndexOutput);
1288}
1289
1290status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1291 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001292 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001293 def.nPortIndex = portIndex;
1294
Andreas Huber784202e2009-10-15 13:46:54 -07001295 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001296 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1297
1298 if (err != OK) {
1299 return err;
1300 }
1301
Andreas Huber5c0a9132009-08-20 11:16:40 -07001302 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001303 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001304
Andreas Huberbe06d262009-08-14 14:37:10 -07001305 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001306 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001307 CHECK(mem.get() != NULL);
1308
Andreas Huberc712b9f2010-01-20 15:05:46 -08001309 BufferInfo info;
1310 info.mData = NULL;
1311 info.mSize = def.nBufferSize;
1312
Andreas Huberbe06d262009-08-14 14:37:10 -07001313 IOMX::buffer_id buffer;
1314 if (portIndex == kPortIndexInput
1315 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001316 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001317 mem.clear();
1318
Andreas Huberf1fe0642010-01-15 15:28:19 -08001319 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001320 mNode, portIndex, def.nBufferSize, &buffer,
1321 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001322 } else {
1323 err = mOMX->allocateBufferWithBackup(
1324 mNode, portIndex, mem, &buffer);
1325 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001326 } else if (portIndex == kPortIndexOutput
1327 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001328 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001329 mem.clear();
1330
Andreas Huberf1fe0642010-01-15 15:28:19 -08001331 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001332 mNode, portIndex, def.nBufferSize, &buffer,
1333 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001334 } else {
1335 err = mOMX->allocateBufferWithBackup(
1336 mNode, portIndex, mem, &buffer);
1337 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001338 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001339 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001340 }
1341
1342 if (err != OK) {
1343 LOGE("allocate_buffer_with_backup failed");
1344 return err;
1345 }
1346
Andreas Huberc712b9f2010-01-20 15:05:46 -08001347 if (mem != NULL) {
1348 info.mData = mem->pointer();
1349 }
1350
Andreas Huberbe06d262009-08-14 14:37:10 -07001351 info.mBuffer = buffer;
1352 info.mOwnedByComponent = false;
1353 info.mMem = mem;
1354 info.mMediaBuffer = NULL;
1355
1356 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001357 if (!(mOMXLivesLocally
1358 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1359 && (mQuirks & kDefersOutputBufferAllocation))) {
1360 // If the node does not fill in the buffer ptr at this time,
1361 // we will defer creating the MediaBuffer until receiving
1362 // the first FILL_BUFFER_DONE notification instead.
1363 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1364 info.mMediaBuffer->setObserver(this);
1365 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001366 }
1367
1368 mPortBuffers[portIndex].push(info);
1369
Andreas Huber4c483422009-09-02 16:05:36 -07001370 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001371 portIndex == kPortIndexInput ? "input" : "output");
1372 }
1373
Andreas Huber2ea14e22009-12-16 09:30:55 -08001374 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001375
1376 return OK;
1377}
1378
1379void OMXCodec::on_message(const omx_message &msg) {
1380 Mutex::Autolock autoLock(mLock);
1381
1382 switch (msg.type) {
1383 case omx_message::EVENT:
1384 {
1385 onEvent(
1386 msg.u.event_data.event, msg.u.event_data.data1,
1387 msg.u.event_data.data2);
1388
1389 break;
1390 }
1391
1392 case omx_message::EMPTY_BUFFER_DONE:
1393 {
1394 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1395
Andreas Huber4c483422009-09-02 16:05:36 -07001396 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001397
1398 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1399 size_t i = 0;
1400 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1401 ++i;
1402 }
1403
1404 CHECK(i < buffers->size());
1405 if (!(*buffers)[i].mOwnedByComponent) {
1406 LOGW("We already own input buffer %p, yet received "
1407 "an EMPTY_BUFFER_DONE.", buffer);
1408 }
1409
1410 buffers->editItemAt(i).mOwnedByComponent = false;
1411
1412 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001413 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001414
1415 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001416 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001417 CHECK_EQ(err, OK);
1418
1419 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001420 } else if (mState != ERROR
1421 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001422 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1423 drainInputBuffer(&buffers->editItemAt(i));
1424 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001425 break;
1426 }
1427
1428 case omx_message::FILL_BUFFER_DONE:
1429 {
1430 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1431 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1432
Andreas Huber2ea14e22009-12-16 09:30:55 -08001433 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001434 buffer,
1435 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001436 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001437 msg.u.extended_buffer_data.timestamp,
1438 msg.u.extended_buffer_data.timestamp / 1E6);
1439
1440 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1441 size_t i = 0;
1442 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1443 ++i;
1444 }
1445
1446 CHECK(i < buffers->size());
1447 BufferInfo *info = &buffers->editItemAt(i);
1448
1449 if (!info->mOwnedByComponent) {
1450 LOGW("We already own output buffer %p, yet received "
1451 "a FILL_BUFFER_DONE.", buffer);
1452 }
1453
1454 info->mOwnedByComponent = false;
1455
1456 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001457 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001458
1459 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001460 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001461 CHECK_EQ(err, OK);
1462
1463 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001464#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001465 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1466 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001467 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001468 mNoMoreOutputData = true;
1469 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001470#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001471 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1472 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001473
Andreas Huber52733b82010-01-25 10:41:35 -08001474 if (info->mMediaBuffer == NULL) {
1475 CHECK(mOMXLivesLocally);
1476 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1477 CHECK(mQuirks & kDefersOutputBufferAllocation);
1478
1479 // The qcom video decoders on Nexus don't actually allocate
1480 // output buffer memory on a call to OMX_AllocateBuffer
1481 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1482 // structure is only filled in later.
1483
1484 info->mMediaBuffer = new MediaBuffer(
1485 msg.u.extended_buffer_data.data_ptr,
1486 info->mSize);
1487 info->mMediaBuffer->setObserver(this);
1488 }
1489
Andreas Huberbe06d262009-08-14 14:37:10 -07001490 MediaBuffer *buffer = info->mMediaBuffer;
1491
1492 buffer->set_range(
1493 msg.u.extended_buffer_data.range_offset,
1494 msg.u.extended_buffer_data.range_length);
1495
1496 buffer->meta_data()->clear();
1497
Andreas Huberfa8de752009-10-08 10:07:49 -07001498 buffer->meta_data()->setInt64(
1499 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001500
1501 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1502 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1503 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001504 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1505 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1506 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001507
1508 buffer->meta_data()->setPointer(
1509 kKeyPlatformPrivate,
1510 msg.u.extended_buffer_data.platform_private);
1511
1512 buffer->meta_data()->setPointer(
1513 kKeyBufferID,
1514 msg.u.extended_buffer_data.buffer);
1515
1516 mFilledBuffers.push_back(i);
1517 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001518
1519 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1520 CODEC_LOGV("No more output data.");
1521 mNoMoreOutputData = true;
1522 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001523 }
1524
1525 break;
1526 }
1527
1528 default:
1529 {
1530 CHECK(!"should not be here.");
1531 break;
1532 }
1533 }
1534}
1535
1536void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1537 switch (event) {
1538 case OMX_EventCmdComplete:
1539 {
1540 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1541 break;
1542 }
1543
1544 case OMX_EventError:
1545 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001546 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001547
1548 setState(ERROR);
1549 break;
1550 }
1551
1552 case OMX_EventPortSettingsChanged:
1553 {
1554 onPortSettingsChanged(data1);
1555 break;
1556 }
1557
Andreas Huber2ea14e22009-12-16 09:30:55 -08001558#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001559 case OMX_EventBufferFlag:
1560 {
Andreas Huber4c483422009-09-02 16:05:36 -07001561 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001562
1563 if (data1 == kPortIndexOutput) {
1564 mNoMoreOutputData = true;
1565 }
1566 break;
1567 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001568#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001569
1570 default:
1571 {
Andreas Huber4c483422009-09-02 16:05:36 -07001572 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001573 break;
1574 }
1575 }
1576}
1577
Andreas Huberb1678602009-10-19 13:06:40 -07001578// Has the format changed in any way that the client would have to be aware of?
1579static bool formatHasNotablyChanged(
1580 const sp<MetaData> &from, const sp<MetaData> &to) {
1581 if (from.get() == NULL && to.get() == NULL) {
1582 return false;
1583 }
1584
Andreas Huberf68c1682009-10-21 14:01:30 -07001585 if ((from.get() == NULL && to.get() != NULL)
1586 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001587 return true;
1588 }
1589
1590 const char *mime_from, *mime_to;
1591 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1592 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1593
1594 if (strcasecmp(mime_from, mime_to)) {
1595 return true;
1596 }
1597
1598 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1599 int32_t colorFormat_from, colorFormat_to;
1600 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1601 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1602
1603 if (colorFormat_from != colorFormat_to) {
1604 return true;
1605 }
1606
1607 int32_t width_from, width_to;
1608 CHECK(from->findInt32(kKeyWidth, &width_from));
1609 CHECK(to->findInt32(kKeyWidth, &width_to));
1610
1611 if (width_from != width_to) {
1612 return true;
1613 }
1614
1615 int32_t height_from, height_to;
1616 CHECK(from->findInt32(kKeyHeight, &height_from));
1617 CHECK(to->findInt32(kKeyHeight, &height_to));
1618
1619 if (height_from != height_to) {
1620 return true;
1621 }
1622 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1623 int32_t numChannels_from, numChannels_to;
1624 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1625 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1626
1627 if (numChannels_from != numChannels_to) {
1628 return true;
1629 }
1630
1631 int32_t sampleRate_from, sampleRate_to;
1632 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1633 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1634
1635 if (sampleRate_from != sampleRate_to) {
1636 return true;
1637 }
1638 }
1639
1640 return false;
1641}
1642
Andreas Huberbe06d262009-08-14 14:37:10 -07001643void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1644 switch (cmd) {
1645 case OMX_CommandStateSet:
1646 {
1647 onStateChange((OMX_STATETYPE)data);
1648 break;
1649 }
1650
1651 case OMX_CommandPortDisable:
1652 {
1653 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001654 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001655
1656 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1657 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1658 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1659
1660 mPortStatus[portIndex] = DISABLED;
1661
1662 if (mState == RECONFIGURING) {
1663 CHECK_EQ(portIndex, kPortIndexOutput);
1664
Andreas Huberb1678602009-10-19 13:06:40 -07001665 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001666 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001667
1668 // Don't notify clients if the output port settings change
1669 // wasn't of importance to them, i.e. it may be that just the
1670 // number of buffers has changed and nothing else.
1671 mOutputPortSettingsHaveChanged =
1672 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001673
Andreas Huberbe06d262009-08-14 14:37:10 -07001674 enablePortAsync(portIndex);
1675
1676 status_t err = allocateBuffersOnPort(portIndex);
1677 CHECK_EQ(err, OK);
1678 }
1679 break;
1680 }
1681
1682 case OMX_CommandPortEnable:
1683 {
1684 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001685 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001686
1687 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1688 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1689
1690 mPortStatus[portIndex] = ENABLED;
1691
1692 if (mState == RECONFIGURING) {
1693 CHECK_EQ(portIndex, kPortIndexOutput);
1694
1695 setState(EXECUTING);
1696
1697 fillOutputBuffers();
1698 }
1699 break;
1700 }
1701
1702 case OMX_CommandFlush:
1703 {
1704 OMX_U32 portIndex = data;
1705
Andreas Huber4c483422009-09-02 16:05:36 -07001706 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001707
1708 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1709 mPortStatus[portIndex] = ENABLED;
1710
1711 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1712 mPortBuffers[portIndex].size());
1713
1714 if (mState == RECONFIGURING) {
1715 CHECK_EQ(portIndex, kPortIndexOutput);
1716
1717 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001718 } else if (mState == EXECUTING_TO_IDLE) {
1719 if (mPortStatus[kPortIndexInput] == ENABLED
1720 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001721 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001722 "transition from EXECUTING to IDLE.");
1723
1724 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1725 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1726
1727 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001728 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001729 CHECK_EQ(err, OK);
1730 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001731 } else {
1732 // We're flushing both ports in preparation for seeking.
1733
1734 if (mPortStatus[kPortIndexInput] == ENABLED
1735 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001736 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001737 " seek-time.");
1738
Andreas Huber1f24b302010-06-10 11:12:39 -07001739 // We implicitly resume pulling on our upstream source.
1740 mPaused = false;
1741
Andreas Huberbe06d262009-08-14 14:37:10 -07001742 drainInputBuffers();
1743 fillOutputBuffers();
1744 }
1745 }
1746
1747 break;
1748 }
1749
1750 default:
1751 {
Andreas Huber4c483422009-09-02 16:05:36 -07001752 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07001753 break;
1754 }
1755 }
1756}
1757
1758void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001759 CODEC_LOGV("onStateChange %d", newState);
1760
Andreas Huberbe06d262009-08-14 14:37:10 -07001761 switch (newState) {
1762 case OMX_StateIdle:
1763 {
Andreas Huber4c483422009-09-02 16:05:36 -07001764 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001765 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07001766 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001767 mNode, OMX_CommandStateSet, OMX_StateExecuting);
1768
1769 CHECK_EQ(err, OK);
1770
1771 setState(IDLE_TO_EXECUTING);
1772 } else {
1773 CHECK_EQ(mState, EXECUTING_TO_IDLE);
1774
1775 CHECK_EQ(
1776 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1777 mPortBuffers[kPortIndexInput].size());
1778
1779 CHECK_EQ(
1780 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1781 mPortBuffers[kPortIndexOutput].size());
1782
Andreas Huber784202e2009-10-15 13:46:54 -07001783 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07001784 mNode, OMX_CommandStateSet, OMX_StateLoaded);
1785
1786 CHECK_EQ(err, OK);
1787
1788 err = freeBuffersOnPort(kPortIndexInput);
1789 CHECK_EQ(err, OK);
1790
1791 err = freeBuffersOnPort(kPortIndexOutput);
1792 CHECK_EQ(err, OK);
1793
1794 mPortStatus[kPortIndexInput] = ENABLED;
1795 mPortStatus[kPortIndexOutput] = ENABLED;
1796
1797 setState(IDLE_TO_LOADED);
1798 }
1799 break;
1800 }
1801
1802 case OMX_StateExecuting:
1803 {
1804 CHECK_EQ(mState, IDLE_TO_EXECUTING);
1805
Andreas Huber4c483422009-09-02 16:05:36 -07001806 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001807
1808 setState(EXECUTING);
1809
Andreas Huber42978e52009-08-27 10:08:39 -07001810 // Buffers will be submitted to the component in the first
1811 // call to OMXCodec::read as mInitialBufferSubmit is true at
1812 // this point. This ensures that this on_message call returns,
1813 // releases the lock and ::init can notice the state change and
1814 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07001815 break;
1816 }
1817
1818 case OMX_StateLoaded:
1819 {
1820 CHECK_EQ(mState, IDLE_TO_LOADED);
1821
Andreas Huber4c483422009-09-02 16:05:36 -07001822 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001823
1824 setState(LOADED);
1825 break;
1826 }
1827
Andreas Huberc712b9f2010-01-20 15:05:46 -08001828 case OMX_StateInvalid:
1829 {
1830 setState(ERROR);
1831 break;
1832 }
1833
Andreas Huberbe06d262009-08-14 14:37:10 -07001834 default:
1835 {
1836 CHECK(!"should not be here.");
1837 break;
1838 }
1839 }
1840}
1841
1842// static
1843size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1844 size_t n = 0;
1845 for (size_t i = 0; i < buffers.size(); ++i) {
1846 if (!buffers[i].mOwnedByComponent) {
1847 ++n;
1848 }
1849 }
1850
1851 return n;
1852}
1853
1854status_t OMXCodec::freeBuffersOnPort(
1855 OMX_U32 portIndex, bool onlyThoseWeOwn) {
1856 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1857
1858 status_t stickyErr = OK;
1859
1860 for (size_t i = buffers->size(); i-- > 0;) {
1861 BufferInfo *info = &buffers->editItemAt(i);
1862
1863 if (onlyThoseWeOwn && info->mOwnedByComponent) {
1864 continue;
1865 }
1866
1867 CHECK_EQ(info->mOwnedByComponent, false);
1868
Andreas Huber92022852009-09-14 15:24:14 -07001869 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1870
Andreas Huberbe06d262009-08-14 14:37:10 -07001871 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001872 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001873
1874 if (err != OK) {
1875 stickyErr = err;
1876 }
1877
1878 if (info->mMediaBuffer != NULL) {
1879 info->mMediaBuffer->setObserver(NULL);
1880
1881 // Make sure nobody but us owns this buffer at this point.
1882 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1883
1884 info->mMediaBuffer->release();
1885 }
1886
1887 buffers->removeAt(i);
1888 }
1889
1890 CHECK(onlyThoseWeOwn || buffers->isEmpty());
1891
1892 return stickyErr;
1893}
1894
1895void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07001896 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001897
1898 CHECK_EQ(mState, EXECUTING);
1899 CHECK_EQ(portIndex, kPortIndexOutput);
1900 setState(RECONFIGURING);
1901
1902 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07001903 if (!flushPortAsync(portIndex)) {
1904 onCmdComplete(OMX_CommandFlush, portIndex);
1905 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001906 } else {
1907 disablePortAsync(portIndex);
1908 }
1909}
1910
Andreas Huber404cc412009-08-25 14:26:05 -07001911bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07001912 CHECK(mState == EXECUTING || mState == RECONFIGURING
1913 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07001914
Andreas Huber4c483422009-09-02 16:05:36 -07001915 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07001916 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1917 mPortBuffers[portIndex].size());
1918
Andreas Huberbe06d262009-08-14 14:37:10 -07001919 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1920 mPortStatus[portIndex] = SHUTTING_DOWN;
1921
Andreas Huber404cc412009-08-25 14:26:05 -07001922 if ((mQuirks & kRequiresFlushCompleteEmulation)
1923 && countBuffersWeOwn(mPortBuffers[portIndex])
1924 == mPortBuffers[portIndex].size()) {
1925 // No flush is necessary and this component fails to send a
1926 // flush-complete event in this case.
1927
1928 return false;
1929 }
1930
Andreas Huberbe06d262009-08-14 14:37:10 -07001931 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001932 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001933 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07001934
1935 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07001936}
1937
1938void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1939 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1940
1941 CHECK_EQ(mPortStatus[portIndex], ENABLED);
1942 mPortStatus[portIndex] = DISABLING;
1943
1944 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001945 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001946 CHECK_EQ(err, OK);
1947
1948 freeBuffersOnPort(portIndex, true);
1949}
1950
1951void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1952 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1953
1954 CHECK_EQ(mPortStatus[portIndex], DISABLED);
1955 mPortStatus[portIndex] = ENABLING;
1956
1957 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001958 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001959 CHECK_EQ(err, OK);
1960}
1961
1962void OMXCodec::fillOutputBuffers() {
1963 CHECK_EQ(mState, EXECUTING);
1964
Andreas Huberdbcb2c62010-01-14 11:32:13 -08001965 // This is a workaround for some decoders not properly reporting
1966 // end-of-output-stream. If we own all input buffers and also own
1967 // all output buffers and we already signalled end-of-input-stream,
1968 // the end-of-output-stream is implied.
1969 if (mSignalledEOS
1970 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
1971 == mPortBuffers[kPortIndexInput].size()
1972 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
1973 == mPortBuffers[kPortIndexOutput].size()) {
1974 mNoMoreOutputData = true;
1975 mBufferFilled.signal();
1976
1977 return;
1978 }
1979
Andreas Huberbe06d262009-08-14 14:37:10 -07001980 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1981 for (size_t i = 0; i < buffers->size(); ++i) {
1982 fillOutputBuffer(&buffers->editItemAt(i));
1983 }
1984}
1985
1986void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07001987 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07001988
1989 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1990 for (size_t i = 0; i < buffers->size(); ++i) {
1991 drainInputBuffer(&buffers->editItemAt(i));
1992 }
1993}
1994
1995void OMXCodec::drainInputBuffer(BufferInfo *info) {
1996 CHECK_EQ(info->mOwnedByComponent, false);
1997
1998 if (mSignalledEOS) {
1999 return;
2000 }
2001
2002 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2003 const CodecSpecificData *specific =
2004 mCodecSpecificData[mCodecSpecificDataIndex];
2005
2006 size_t size = specific->mSize;
2007
Andreas Hubere6c40962009-09-10 14:13:30 -07002008 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002009 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002010 static const uint8_t kNALStartCode[4] =
2011 { 0x00, 0x00, 0x00, 0x01 };
2012
Andreas Huberc712b9f2010-01-20 15:05:46 -08002013 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002014
2015 size += 4;
2016
Andreas Huberc712b9f2010-01-20 15:05:46 -08002017 memcpy(info->mData, kNALStartCode, 4);
2018 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002019 specific->mData, specific->mSize);
2020 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002021 CHECK(info->mSize >= specific->mSize);
2022 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002023 }
2024
Andreas Huber2ea14e22009-12-16 09:30:55 -08002025 mNoMoreOutputData = false;
2026
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002027 CODEC_LOGV("calling emptyBuffer with codec specific data");
2028
Andreas Huber784202e2009-10-15 13:46:54 -07002029 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002030 mNode, info->mBuffer, 0, size,
2031 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2032 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002033 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002034
2035 info->mOwnedByComponent = true;
2036
2037 ++mCodecSpecificDataIndex;
2038 return;
2039 }
2040
Andreas Huber1f24b302010-06-10 11:12:39 -07002041 if (mPaused) {
2042 return;
2043 }
2044
Andreas Huberbe06d262009-08-14 14:37:10 -07002045 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002046
Andreas Hubera4357ad2010-04-02 12:49:54 -07002047 bool signalEOS = false;
2048 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002049
Andreas Hubera4357ad2010-04-02 12:49:54 -07002050 size_t offset = 0;
2051 int32_t n = 0;
2052 for (;;) {
2053 MediaBuffer *srcBuffer;
2054 if (mSeekTimeUs >= 0) {
2055 if (mLeftOverBuffer) {
2056 mLeftOverBuffer->release();
2057 mLeftOverBuffer = NULL;
2058 }
2059
2060 MediaSource::ReadOptions options;
2061 options.setSeekTo(mSeekTimeUs);
2062
2063 mSeekTimeUs = -1;
2064 mBufferFilled.signal();
2065
2066 err = mSource->read(&srcBuffer, &options);
2067 } else if (mLeftOverBuffer) {
2068 srcBuffer = mLeftOverBuffer;
2069 mLeftOverBuffer = NULL;
2070
2071 err = OK;
2072 } else {
2073 err = mSource->read(&srcBuffer);
2074 }
2075
2076 if (err != OK) {
2077 signalEOS = true;
2078 mFinalStatus = err;
2079 mSignalledEOS = true;
2080 break;
2081 }
2082
2083 size_t remainingBytes = info->mSize - offset;
2084
2085 if (srcBuffer->range_length() > remainingBytes) {
2086 if (offset == 0) {
2087 CODEC_LOGE(
2088 "Codec's input buffers are too small to accomodate "
2089 "buffer read from source (info->mSize = %d, srcLength = %d)",
2090 info->mSize, srcBuffer->range_length());
2091
2092 srcBuffer->release();
2093 srcBuffer = NULL;
2094
2095 setState(ERROR);
2096 return;
2097 }
2098
2099 mLeftOverBuffer = srcBuffer;
2100 break;
2101 }
2102
James Dong4f501f02010-06-07 14:41:41 -07002103 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2104 CHECK(mOMXLivesLocally && offset == 0);
2105 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2106 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2107 } else {
2108 memcpy((uint8_t *)info->mData + offset,
2109 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2110 srcBuffer->range_length());
2111 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002112
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002113 int64_t lastBufferTimeUs;
2114 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
2115 CHECK(timestampUs >= 0);
2116
Andreas Hubera4357ad2010-04-02 12:49:54 -07002117 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002118 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002119 }
2120
2121 offset += srcBuffer->range_length();
2122
2123 srcBuffer->release();
2124 srcBuffer = NULL;
2125
2126 ++n;
2127
2128 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2129 break;
2130 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002131
2132 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2133
2134 if (coalescedDurationUs > 250000ll) {
2135 // Don't coalesce more than 250ms worth of encoded data at once.
2136 break;
2137 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002138 }
2139
2140 if (n > 1) {
2141 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002142 }
2143
2144 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002145
Andreas Hubera4357ad2010-04-02 12:49:54 -07002146 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002147 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002148 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002149 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002150 }
2151
Andreas Hubera4357ad2010-04-02 12:49:54 -07002152 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2153 "timestamp %lld us (%.2f secs)",
2154 info->mBuffer, offset,
2155 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002156
Andreas Huber784202e2009-10-15 13:46:54 -07002157 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002158 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002159 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002160
2161 if (err != OK) {
2162 setState(ERROR);
2163 return;
2164 }
2165
2166 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002167
2168 // This component does not ever signal the EOS flag on output buffers,
2169 // Thanks for nothing.
2170 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2171 mNoMoreOutputData = true;
2172 mBufferFilled.signal();
2173 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002174}
2175
2176void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2177 CHECK_EQ(info->mOwnedByComponent, false);
2178
Andreas Huber404cc412009-08-25 14:26:05 -07002179 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002180 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002181 "calling fillOutputBuffer");
2182 return;
2183 }
2184
Andreas Huber4c483422009-09-02 16:05:36 -07002185 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002186 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002187
2188 if (err != OK) {
2189 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2190
2191 setState(ERROR);
2192 return;
2193 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002194
2195 info->mOwnedByComponent = true;
2196}
2197
2198void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2199 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2200 for (size_t i = 0; i < buffers->size(); ++i) {
2201 if ((*buffers)[i].mBuffer == buffer) {
2202 drainInputBuffer(&buffers->editItemAt(i));
2203 return;
2204 }
2205 }
2206
2207 CHECK(!"should not be here.");
2208}
2209
2210void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2211 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2212 for (size_t i = 0; i < buffers->size(); ++i) {
2213 if ((*buffers)[i].mBuffer == buffer) {
2214 fillOutputBuffer(&buffers->editItemAt(i));
2215 return;
2216 }
2217 }
2218
2219 CHECK(!"should not be here.");
2220}
2221
2222void OMXCodec::setState(State newState) {
2223 mState = newState;
2224 mAsyncCompletion.signal();
2225
2226 // This may cause some spurious wakeups but is necessary to
2227 // unblock the reader if we enter ERROR state.
2228 mBufferFilled.signal();
2229}
2230
Andreas Huberda050cf22009-09-02 14:01:43 -07002231void OMXCodec::setRawAudioFormat(
2232 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002233
2234 // port definition
2235 OMX_PARAM_PORTDEFINITIONTYPE def;
2236 InitOMXParams(&def);
2237 def.nPortIndex = portIndex;
2238 status_t err = mOMX->getParameter(
2239 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2240 CHECK_EQ(err, OK);
2241 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2242 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2243 &def, sizeof(def)), OK);
2244
2245 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002246 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002247 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002248 pcmParams.nPortIndex = portIndex;
2249
James Dongabed93a2010-04-22 17:27:04 -07002250 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002251 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2252
2253 CHECK_EQ(err, OK);
2254
2255 pcmParams.nChannels = numChannels;
2256 pcmParams.eNumData = OMX_NumericalDataSigned;
2257 pcmParams.bInterleaved = OMX_TRUE;
2258 pcmParams.nBitPerSample = 16;
2259 pcmParams.nSamplingRate = sampleRate;
2260 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2261
2262 if (numChannels == 1) {
2263 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2264 } else {
2265 CHECK_EQ(numChannels, 2);
2266
2267 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2268 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2269 }
2270
Andreas Huber784202e2009-10-15 13:46:54 -07002271 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002272 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2273
2274 CHECK_EQ(err, OK);
2275}
2276
James Dong17299ab2010-05-14 15:45:22 -07002277static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2278 if (isAMRWB) {
2279 if (bps <= 6600) {
2280 return OMX_AUDIO_AMRBandModeWB0;
2281 } else if (bps <= 8850) {
2282 return OMX_AUDIO_AMRBandModeWB1;
2283 } else if (bps <= 12650) {
2284 return OMX_AUDIO_AMRBandModeWB2;
2285 } else if (bps <= 14250) {
2286 return OMX_AUDIO_AMRBandModeWB3;
2287 } else if (bps <= 15850) {
2288 return OMX_AUDIO_AMRBandModeWB4;
2289 } else if (bps <= 18250) {
2290 return OMX_AUDIO_AMRBandModeWB5;
2291 } else if (bps <= 19850) {
2292 return OMX_AUDIO_AMRBandModeWB6;
2293 } else if (bps <= 23050) {
2294 return OMX_AUDIO_AMRBandModeWB7;
2295 }
2296
2297 // 23850 bps
2298 return OMX_AUDIO_AMRBandModeWB8;
2299 } else { // AMRNB
2300 if (bps <= 4750) {
2301 return OMX_AUDIO_AMRBandModeNB0;
2302 } else if (bps <= 5150) {
2303 return OMX_AUDIO_AMRBandModeNB1;
2304 } else if (bps <= 5900) {
2305 return OMX_AUDIO_AMRBandModeNB2;
2306 } else if (bps <= 6700) {
2307 return OMX_AUDIO_AMRBandModeNB3;
2308 } else if (bps <= 7400) {
2309 return OMX_AUDIO_AMRBandModeNB4;
2310 } else if (bps <= 7950) {
2311 return OMX_AUDIO_AMRBandModeNB5;
2312 } else if (bps <= 10200) {
2313 return OMX_AUDIO_AMRBandModeNB6;
2314 }
2315
2316 // 12200 bps
2317 return OMX_AUDIO_AMRBandModeNB7;
2318 }
2319}
2320
2321void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002322 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002323
Andreas Huber8768f2c2009-12-01 15:26:54 -08002324 OMX_AUDIO_PARAM_AMRTYPE def;
2325 InitOMXParams(&def);
2326 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002327
Andreas Huber8768f2c2009-12-01 15:26:54 -08002328 status_t err =
2329 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002330
Andreas Huber8768f2c2009-12-01 15:26:54 -08002331 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002332
Andreas Huber8768f2c2009-12-01 15:26:54 -08002333 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002334
James Dong17299ab2010-05-14 15:45:22 -07002335 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002336 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2337 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002338
2339 ////////////////////////
2340
2341 if (mIsEncoder) {
2342 sp<MetaData> format = mSource->getFormat();
2343 int32_t sampleRate;
2344 int32_t numChannels;
2345 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2346 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2347
2348 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2349 }
2350}
2351
James Dong17299ab2010-05-14 15:45:22 -07002352void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002353 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002354 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002355 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002356 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002357
2358 //////////////// output port ////////////////////
2359 // format
2360 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2361 format.nPortIndex = kPortIndexOutput;
2362 format.nIndex = 0;
2363 status_t err = OMX_ErrorNone;
2364 while (OMX_ErrorNone == err) {
2365 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2366 &format, sizeof(format)), OK);
2367 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2368 break;
2369 }
2370 format.nIndex++;
2371 }
2372 CHECK_EQ(OK, err);
2373 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2374 &format, sizeof(format)), OK);
2375
2376 // port definition
2377 OMX_PARAM_PORTDEFINITIONTYPE def;
2378 InitOMXParams(&def);
2379 def.nPortIndex = kPortIndexOutput;
2380 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2381 &def, sizeof(def)), OK);
2382 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2383 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2384 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2385 &def, sizeof(def)), OK);
2386
2387 // profile
2388 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2389 InitOMXParams(&profile);
2390 profile.nPortIndex = kPortIndexOutput;
2391 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2392 &profile, sizeof(profile)), OK);
2393 profile.nChannels = numChannels;
2394 profile.eChannelMode = (numChannels == 1?
2395 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2396 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002397 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002398 profile.nAudioBandWidth = 0;
2399 profile.nFrameLength = 0;
2400 profile.nAACtools = OMX_AUDIO_AACToolAll;
2401 profile.nAACERtools = OMX_AUDIO_AACERNone;
2402 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2403 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2404 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2405 &profile, sizeof(profile)), OK);
2406
Andreas Huberda050cf22009-09-02 14:01:43 -07002407 } else {
2408 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002409 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002410 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002411
Andreas Huber784202e2009-10-15 13:46:54 -07002412 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002413 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2414 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002415
Andreas Huberda050cf22009-09-02 14:01:43 -07002416 profile.nChannels = numChannels;
2417 profile.nSampleRate = sampleRate;
2418 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002419
Andreas Huber784202e2009-10-15 13:46:54 -07002420 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002421 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2422 CHECK_EQ(err, OK);
2423 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002424}
2425
2426void OMXCodec::setImageOutputFormat(
2427 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002428 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002429
2430#if 0
2431 OMX_INDEXTYPE index;
2432 status_t err = mOMX->get_extension_index(
2433 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2434 CHECK_EQ(err, OK);
2435
2436 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2437 CHECK_EQ(err, OK);
2438#endif
2439
2440 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002441 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002442 def.nPortIndex = kPortIndexOutput;
2443
Andreas Huber784202e2009-10-15 13:46:54 -07002444 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002445 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2446 CHECK_EQ(err, OK);
2447
2448 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2449
2450 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002451
Andreas Huberbe06d262009-08-14 14:37:10 -07002452 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2453 imageDef->eColorFormat = format;
2454 imageDef->nFrameWidth = width;
2455 imageDef->nFrameHeight = height;
2456
2457 switch (format) {
2458 case OMX_COLOR_FormatYUV420PackedPlanar:
2459 case OMX_COLOR_FormatYUV411Planar:
2460 {
2461 def.nBufferSize = (width * height * 3) / 2;
2462 break;
2463 }
2464
2465 case OMX_COLOR_FormatCbYCrY:
2466 {
2467 def.nBufferSize = width * height * 2;
2468 break;
2469 }
2470
2471 case OMX_COLOR_Format32bitARGB8888:
2472 {
2473 def.nBufferSize = width * height * 4;
2474 break;
2475 }
2476
Andreas Huber201511c2009-09-08 14:01:44 -07002477 case OMX_COLOR_Format16bitARGB4444:
2478 case OMX_COLOR_Format16bitARGB1555:
2479 case OMX_COLOR_Format16bitRGB565:
2480 case OMX_COLOR_Format16bitBGR565:
2481 {
2482 def.nBufferSize = width * height * 2;
2483 break;
2484 }
2485
Andreas Huberbe06d262009-08-14 14:37:10 -07002486 default:
2487 CHECK(!"Should not be here. Unknown color format.");
2488 break;
2489 }
2490
Andreas Huber5c0a9132009-08-20 11:16:40 -07002491 def.nBufferCountActual = def.nBufferCountMin;
2492
Andreas Huber784202e2009-10-15 13:46:54 -07002493 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002494 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2495 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002496}
Andreas Huberbe06d262009-08-14 14:37:10 -07002497
Andreas Huber5c0a9132009-08-20 11:16:40 -07002498void OMXCodec::setJPEGInputFormat(
2499 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2500 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002501 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002502 def.nPortIndex = kPortIndexInput;
2503
Andreas Huber784202e2009-10-15 13:46:54 -07002504 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002505 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2506 CHECK_EQ(err, OK);
2507
Andreas Huber5c0a9132009-08-20 11:16:40 -07002508 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2509 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2510
Andreas Huberbe06d262009-08-14 14:37:10 -07002511 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2512 imageDef->nFrameWidth = width;
2513 imageDef->nFrameHeight = height;
2514
Andreas Huber5c0a9132009-08-20 11:16:40 -07002515 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002516 def.nBufferCountActual = def.nBufferCountMin;
2517
Andreas Huber784202e2009-10-15 13:46:54 -07002518 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002519 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2520 CHECK_EQ(err, OK);
2521}
2522
2523void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2524 CodecSpecificData *specific =
2525 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2526
2527 specific->mSize = size;
2528 memcpy(specific->mData, data, size);
2529
2530 mCodecSpecificData.push(specific);
2531}
2532
2533void OMXCodec::clearCodecSpecificData() {
2534 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2535 free(mCodecSpecificData.editItemAt(i));
2536 }
2537 mCodecSpecificData.clear();
2538 mCodecSpecificDataIndex = 0;
2539}
2540
James Dong36e573b2010-06-19 09:04:18 -07002541status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002542 Mutex::Autolock autoLock(mLock);
2543
Andreas Huberbe06d262009-08-14 14:37:10 -07002544 if (mState != LOADED) {
2545 return UNKNOWN_ERROR;
2546 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002547
Andreas Huberbe06d262009-08-14 14:37:10 -07002548 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002549 if (mQuirks & kWantsNALFragments) {
2550 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002551 }
James Dong36e573b2010-06-19 09:04:18 -07002552 if (meta) {
2553 int64_t startTimeUs = 0;
2554 int64_t timeUs;
2555 if (meta->findInt64(kKeyTime, &timeUs)) {
2556 startTimeUs = timeUs;
2557 }
2558 params->setInt64(kKeyTime, startTimeUs);
2559 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002560 status_t err = mSource->start(params.get());
2561
2562 if (err != OK) {
2563 return err;
2564 }
2565
2566 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002567 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002568 mSignalledEOS = false;
2569 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002570 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002571 mSeekTimeUs = -1;
2572 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002573 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002574
2575 return init();
2576}
2577
2578status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002579 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002580
2581 Mutex::Autolock autoLock(mLock);
2582
2583 while (isIntermediateState(mState)) {
2584 mAsyncCompletion.wait(mLock);
2585 }
2586
2587 switch (mState) {
2588 case LOADED:
2589 case ERROR:
2590 break;
2591
2592 case EXECUTING:
2593 {
2594 setState(EXECUTING_TO_IDLE);
2595
Andreas Huber127fcdc2009-08-26 16:27:02 -07002596 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002597 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002598 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002599
Andreas Huber127fcdc2009-08-26 16:27:02 -07002600 bool emulateInputFlushCompletion =
2601 !flushPortAsync(kPortIndexInput);
2602
2603 bool emulateOutputFlushCompletion =
2604 !flushPortAsync(kPortIndexOutput);
2605
2606 if (emulateInputFlushCompletion) {
2607 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2608 }
2609
2610 if (emulateOutputFlushCompletion) {
2611 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2612 }
2613 } else {
2614 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2615 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2616
2617 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002618 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002619 CHECK_EQ(err, OK);
2620 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002621
2622 while (mState != LOADED && mState != ERROR) {
2623 mAsyncCompletion.wait(mLock);
2624 }
2625
2626 break;
2627 }
2628
2629 default:
2630 {
2631 CHECK(!"should not be here.");
2632 break;
2633 }
2634 }
2635
Andreas Hubera4357ad2010-04-02 12:49:54 -07002636 if (mLeftOverBuffer) {
2637 mLeftOverBuffer->release();
2638 mLeftOverBuffer = NULL;
2639 }
2640
Andreas Huberbe06d262009-08-14 14:37:10 -07002641 mSource->stop();
2642
Andreas Huber4a9375e2010-02-09 11:54:33 -08002643 CODEC_LOGV("stopped");
2644
Andreas Huberbe06d262009-08-14 14:37:10 -07002645 return OK;
2646}
2647
2648sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002649 Mutex::Autolock autoLock(mLock);
2650
Andreas Huberbe06d262009-08-14 14:37:10 -07002651 return mOutputFormat;
2652}
2653
2654status_t OMXCodec::read(
2655 MediaBuffer **buffer, const ReadOptions *options) {
2656 *buffer = NULL;
2657
2658 Mutex::Autolock autoLock(mLock);
2659
Andreas Huberd06e5b82009-08-28 13:18:14 -07002660 if (mState != EXECUTING && mState != RECONFIGURING) {
2661 return UNKNOWN_ERROR;
2662 }
2663
Andreas Hubere981c332009-10-22 13:49:30 -07002664 bool seeking = false;
2665 int64_t seekTimeUs;
2666 if (options && options->getSeekTo(&seekTimeUs)) {
2667 seeking = true;
2668 }
2669
Andreas Huber42978e52009-08-27 10:08:39 -07002670 if (mInitialBufferSubmit) {
2671 mInitialBufferSubmit = false;
2672
Andreas Hubere981c332009-10-22 13:49:30 -07002673 if (seeking) {
2674 CHECK(seekTimeUs >= 0);
2675 mSeekTimeUs = seekTimeUs;
2676
2677 // There's no reason to trigger the code below, there's
2678 // nothing to flush yet.
2679 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002680 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002681 }
2682
Andreas Huber42978e52009-08-27 10:08:39 -07002683 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002684
Andreas Huberd06e5b82009-08-28 13:18:14 -07002685 if (mState == EXECUTING) {
2686 // Otherwise mState == RECONFIGURING and this code will trigger
2687 // after the output port is reenabled.
2688 fillOutputBuffers();
2689 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002690 }
2691
Andreas Hubere981c332009-10-22 13:49:30 -07002692 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002693 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002694
2695 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002696
2697 CHECK(seekTimeUs >= 0);
2698 mSeekTimeUs = seekTimeUs;
2699
2700 mFilledBuffers.clear();
2701
2702 CHECK_EQ(mState, EXECUTING);
2703
Andreas Huber404cc412009-08-25 14:26:05 -07002704 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2705 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2706
2707 if (emulateInputFlushCompletion) {
2708 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2709 }
2710
2711 if (emulateOutputFlushCompletion) {
2712 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2713 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002714
2715 while (mSeekTimeUs >= 0) {
2716 mBufferFilled.wait(mLock);
2717 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002718 }
2719
2720 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2721 mBufferFilled.wait(mLock);
2722 }
2723
2724 if (mState == ERROR) {
2725 return UNKNOWN_ERROR;
2726 }
2727
2728 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08002729 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07002730 }
2731
Andreas Hubercfd55572009-10-09 14:11:28 -07002732 if (mOutputPortSettingsHaveChanged) {
2733 mOutputPortSettingsHaveChanged = false;
2734
2735 return INFO_FORMAT_CHANGED;
2736 }
2737
Andreas Huberbe06d262009-08-14 14:37:10 -07002738 size_t index = *mFilledBuffers.begin();
2739 mFilledBuffers.erase(mFilledBuffers.begin());
2740
2741 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
2742 info->mMediaBuffer->add_ref();
2743 *buffer = info->mMediaBuffer;
2744
2745 return OK;
2746}
2747
2748void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
2749 Mutex::Autolock autoLock(mLock);
2750
2751 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2752 for (size_t i = 0; i < buffers->size(); ++i) {
2753 BufferInfo *info = &buffers->editItemAt(i);
2754
2755 if (info->mMediaBuffer == buffer) {
2756 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
2757 fillOutputBuffer(info);
2758 return;
2759 }
2760 }
2761
2762 CHECK(!"should not be here.");
2763}
2764
2765static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
2766 static const char *kNames[] = {
2767 "OMX_IMAGE_CodingUnused",
2768 "OMX_IMAGE_CodingAutoDetect",
2769 "OMX_IMAGE_CodingJPEG",
2770 "OMX_IMAGE_CodingJPEG2K",
2771 "OMX_IMAGE_CodingEXIF",
2772 "OMX_IMAGE_CodingTIFF",
2773 "OMX_IMAGE_CodingGIF",
2774 "OMX_IMAGE_CodingPNG",
2775 "OMX_IMAGE_CodingLZW",
2776 "OMX_IMAGE_CodingBMP",
2777 };
2778
2779 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2780
2781 if (type < 0 || (size_t)type >= numNames) {
2782 return "UNKNOWN";
2783 } else {
2784 return kNames[type];
2785 }
2786}
2787
2788static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
2789 static const char *kNames[] = {
2790 "OMX_COLOR_FormatUnused",
2791 "OMX_COLOR_FormatMonochrome",
2792 "OMX_COLOR_Format8bitRGB332",
2793 "OMX_COLOR_Format12bitRGB444",
2794 "OMX_COLOR_Format16bitARGB4444",
2795 "OMX_COLOR_Format16bitARGB1555",
2796 "OMX_COLOR_Format16bitRGB565",
2797 "OMX_COLOR_Format16bitBGR565",
2798 "OMX_COLOR_Format18bitRGB666",
2799 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002800 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07002801 "OMX_COLOR_Format24bitRGB888",
2802 "OMX_COLOR_Format24bitBGR888",
2803 "OMX_COLOR_Format24bitARGB1887",
2804 "OMX_COLOR_Format25bitARGB1888",
2805 "OMX_COLOR_Format32bitBGRA8888",
2806 "OMX_COLOR_Format32bitARGB8888",
2807 "OMX_COLOR_FormatYUV411Planar",
2808 "OMX_COLOR_FormatYUV411PackedPlanar",
2809 "OMX_COLOR_FormatYUV420Planar",
2810 "OMX_COLOR_FormatYUV420PackedPlanar",
2811 "OMX_COLOR_FormatYUV420SemiPlanar",
2812 "OMX_COLOR_FormatYUV422Planar",
2813 "OMX_COLOR_FormatYUV422PackedPlanar",
2814 "OMX_COLOR_FormatYUV422SemiPlanar",
2815 "OMX_COLOR_FormatYCbYCr",
2816 "OMX_COLOR_FormatYCrYCb",
2817 "OMX_COLOR_FormatCbYCrY",
2818 "OMX_COLOR_FormatCrYCbY",
2819 "OMX_COLOR_FormatYUV444Interleaved",
2820 "OMX_COLOR_FormatRawBayer8bit",
2821 "OMX_COLOR_FormatRawBayer10bit",
2822 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07002823 "OMX_COLOR_FormatL2",
2824 "OMX_COLOR_FormatL4",
2825 "OMX_COLOR_FormatL8",
2826 "OMX_COLOR_FormatL16",
2827 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07002828 "OMX_COLOR_FormatL32",
2829 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2830 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2831 "OMX_COLOR_Format18BitBGR666",
2832 "OMX_COLOR_Format24BitARGB6666",
2833 "OMX_COLOR_Format24BitABGR6666",
2834 };
2835
2836 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2837
Andreas Huberbe06d262009-08-14 14:37:10 -07002838 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2839 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2840 } else if (type < 0 || (size_t)type >= numNames) {
2841 return "UNKNOWN";
2842 } else {
2843 return kNames[type];
2844 }
2845}
2846
2847static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2848 static const char *kNames[] = {
2849 "OMX_VIDEO_CodingUnused",
2850 "OMX_VIDEO_CodingAutoDetect",
2851 "OMX_VIDEO_CodingMPEG2",
2852 "OMX_VIDEO_CodingH263",
2853 "OMX_VIDEO_CodingMPEG4",
2854 "OMX_VIDEO_CodingWMV",
2855 "OMX_VIDEO_CodingRV",
2856 "OMX_VIDEO_CodingAVC",
2857 "OMX_VIDEO_CodingMJPEG",
2858 };
2859
2860 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2861
2862 if (type < 0 || (size_t)type >= numNames) {
2863 return "UNKNOWN";
2864 } else {
2865 return kNames[type];
2866 }
2867}
2868
2869static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2870 static const char *kNames[] = {
2871 "OMX_AUDIO_CodingUnused",
2872 "OMX_AUDIO_CodingAutoDetect",
2873 "OMX_AUDIO_CodingPCM",
2874 "OMX_AUDIO_CodingADPCM",
2875 "OMX_AUDIO_CodingAMR",
2876 "OMX_AUDIO_CodingGSMFR",
2877 "OMX_AUDIO_CodingGSMEFR",
2878 "OMX_AUDIO_CodingGSMHR",
2879 "OMX_AUDIO_CodingPDCFR",
2880 "OMX_AUDIO_CodingPDCEFR",
2881 "OMX_AUDIO_CodingPDCHR",
2882 "OMX_AUDIO_CodingTDMAFR",
2883 "OMX_AUDIO_CodingTDMAEFR",
2884 "OMX_AUDIO_CodingQCELP8",
2885 "OMX_AUDIO_CodingQCELP13",
2886 "OMX_AUDIO_CodingEVRC",
2887 "OMX_AUDIO_CodingSMV",
2888 "OMX_AUDIO_CodingG711",
2889 "OMX_AUDIO_CodingG723",
2890 "OMX_AUDIO_CodingG726",
2891 "OMX_AUDIO_CodingG729",
2892 "OMX_AUDIO_CodingAAC",
2893 "OMX_AUDIO_CodingMP3",
2894 "OMX_AUDIO_CodingSBC",
2895 "OMX_AUDIO_CodingVORBIS",
2896 "OMX_AUDIO_CodingWMA",
2897 "OMX_AUDIO_CodingRA",
2898 "OMX_AUDIO_CodingMIDI",
2899 };
2900
2901 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2902
2903 if (type < 0 || (size_t)type >= numNames) {
2904 return "UNKNOWN";
2905 } else {
2906 return kNames[type];
2907 }
2908}
2909
2910static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2911 static const char *kNames[] = {
2912 "OMX_AUDIO_PCMModeLinear",
2913 "OMX_AUDIO_PCMModeALaw",
2914 "OMX_AUDIO_PCMModeMULaw",
2915 };
2916
2917 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2918
2919 if (type < 0 || (size_t)type >= numNames) {
2920 return "UNKNOWN";
2921 } else {
2922 return kNames[type];
2923 }
2924}
2925
Andreas Huber7ae02c82009-09-09 16:29:47 -07002926static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2927 static const char *kNames[] = {
2928 "OMX_AUDIO_AMRBandModeUnused",
2929 "OMX_AUDIO_AMRBandModeNB0",
2930 "OMX_AUDIO_AMRBandModeNB1",
2931 "OMX_AUDIO_AMRBandModeNB2",
2932 "OMX_AUDIO_AMRBandModeNB3",
2933 "OMX_AUDIO_AMRBandModeNB4",
2934 "OMX_AUDIO_AMRBandModeNB5",
2935 "OMX_AUDIO_AMRBandModeNB6",
2936 "OMX_AUDIO_AMRBandModeNB7",
2937 "OMX_AUDIO_AMRBandModeWB0",
2938 "OMX_AUDIO_AMRBandModeWB1",
2939 "OMX_AUDIO_AMRBandModeWB2",
2940 "OMX_AUDIO_AMRBandModeWB3",
2941 "OMX_AUDIO_AMRBandModeWB4",
2942 "OMX_AUDIO_AMRBandModeWB5",
2943 "OMX_AUDIO_AMRBandModeWB6",
2944 "OMX_AUDIO_AMRBandModeWB7",
2945 "OMX_AUDIO_AMRBandModeWB8",
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}
2956
2957static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2958 static const char *kNames[] = {
2959 "OMX_AUDIO_AMRFrameFormatConformance",
2960 "OMX_AUDIO_AMRFrameFormatIF1",
2961 "OMX_AUDIO_AMRFrameFormatIF2",
2962 "OMX_AUDIO_AMRFrameFormatFSF",
2963 "OMX_AUDIO_AMRFrameFormatRTPPayload",
2964 "OMX_AUDIO_AMRFrameFormatITU",
2965 };
2966
2967 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2968
2969 if (type < 0 || (size_t)type >= numNames) {
2970 return "UNKNOWN";
2971 } else {
2972 return kNames[type];
2973 }
2974}
Andreas Huberbe06d262009-08-14 14:37:10 -07002975
2976void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2977 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002978 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002979 def.nPortIndex = portIndex;
2980
Andreas Huber784202e2009-10-15 13:46:54 -07002981 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002982 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2983 CHECK_EQ(err, OK);
2984
2985 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2986
2987 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2988 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2989
2990 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
2991 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
2992 printf(" nBufferSize = %ld\n", def.nBufferSize);
2993
2994 switch (def.eDomain) {
2995 case OMX_PortDomainImage:
2996 {
2997 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2998
2999 printf("\n");
3000 printf(" // Image\n");
3001 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3002 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3003 printf(" nStride = %ld\n", imageDef->nStride);
3004
3005 printf(" eCompressionFormat = %s\n",
3006 imageCompressionFormatString(imageDef->eCompressionFormat));
3007
3008 printf(" eColorFormat = %s\n",
3009 colorFormatString(imageDef->eColorFormat));
3010
3011 break;
3012 }
3013
3014 case OMX_PortDomainVideo:
3015 {
3016 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3017
3018 printf("\n");
3019 printf(" // Video\n");
3020 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3021 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3022 printf(" nStride = %ld\n", videoDef->nStride);
3023
3024 printf(" eCompressionFormat = %s\n",
3025 videoCompressionFormatString(videoDef->eCompressionFormat));
3026
3027 printf(" eColorFormat = %s\n",
3028 colorFormatString(videoDef->eColorFormat));
3029
3030 break;
3031 }
3032
3033 case OMX_PortDomainAudio:
3034 {
3035 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3036
3037 printf("\n");
3038 printf(" // Audio\n");
3039 printf(" eEncoding = %s\n",
3040 audioCodingTypeString(audioDef->eEncoding));
3041
3042 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3043 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003044 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003045 params.nPortIndex = portIndex;
3046
Andreas Huber784202e2009-10-15 13:46:54 -07003047 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003048 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3049 CHECK_EQ(err, OK);
3050
3051 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3052 printf(" nChannels = %ld\n", params.nChannels);
3053 printf(" bInterleaved = %d\n", params.bInterleaved);
3054 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3055
3056 printf(" eNumData = %s\n",
3057 params.eNumData == OMX_NumericalDataSigned
3058 ? "signed" : "unsigned");
3059
3060 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003061 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3062 OMX_AUDIO_PARAM_AMRTYPE amr;
3063 InitOMXParams(&amr);
3064 amr.nPortIndex = portIndex;
3065
Andreas Huber784202e2009-10-15 13:46:54 -07003066 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003067 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3068 CHECK_EQ(err, OK);
3069
3070 printf(" nChannels = %ld\n", amr.nChannels);
3071 printf(" eAMRBandMode = %s\n",
3072 amrBandModeString(amr.eAMRBandMode));
3073 printf(" eAMRFrameFormat = %s\n",
3074 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003075 }
3076
3077 break;
3078 }
3079
3080 default:
3081 {
3082 printf(" // Unknown\n");
3083 break;
3084 }
3085 }
3086
3087 printf("}\n");
3088}
3089
3090void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3091 mOutputFormat = new MetaData;
3092 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
3093
3094 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003095 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003096 def.nPortIndex = kPortIndexOutput;
3097
Andreas Huber784202e2009-10-15 13:46:54 -07003098 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003099 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3100 CHECK_EQ(err, OK);
3101
3102 switch (def.eDomain) {
3103 case OMX_PortDomainImage:
3104 {
3105 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3106 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3107
Andreas Hubere6c40962009-09-10 14:13:30 -07003108 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003109 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3110 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3111 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3112 break;
3113 }
3114
3115 case OMX_PortDomainAudio:
3116 {
3117 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3118
Andreas Huberda050cf22009-09-02 14:01:43 -07003119 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3120 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003121 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003122 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003123
Andreas Huber784202e2009-10-15 13:46:54 -07003124 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003125 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3126 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003127
Andreas Huberda050cf22009-09-02 14:01:43 -07003128 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3129 CHECK_EQ(params.nBitPerSample, 16);
3130 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003131
Andreas Huberda050cf22009-09-02 14:01:43 -07003132 int32_t numChannels, sampleRate;
3133 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3134 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003135
Andreas Huberda050cf22009-09-02 14:01:43 -07003136 if ((OMX_U32)numChannels != params.nChannels) {
3137 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003138 "the input stream contains (contains %d channels, "
3139 "codec outputs %ld channels).",
3140 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003141 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003142
Andreas Hubere6c40962009-09-10 14:13:30 -07003143 mOutputFormat->setCString(
3144 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003145
3146 // Use the codec-advertised number of channels, as some
3147 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003148 // mono. If we know the codec lies about this information,
3149 // use the actual number of channels instead.
3150 mOutputFormat->setInt32(
3151 kKeyChannelCount,
3152 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3153 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003154
3155 // The codec-reported sampleRate is not reliable...
3156 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3157 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003158 OMX_AUDIO_PARAM_AMRTYPE amr;
3159 InitOMXParams(&amr);
3160 amr.nPortIndex = kPortIndexOutput;
3161
Andreas Huber784202e2009-10-15 13:46:54 -07003162 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003163 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3164 CHECK_EQ(err, OK);
3165
3166 CHECK_EQ(amr.nChannels, 1);
3167 mOutputFormat->setInt32(kKeyChannelCount, 1);
3168
3169 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3170 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003171 mOutputFormat->setCString(
3172 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003173 mOutputFormat->setInt32(kKeySampleRate, 8000);
3174 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3175 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003176 mOutputFormat->setCString(
3177 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003178 mOutputFormat->setInt32(kKeySampleRate, 16000);
3179 } else {
3180 CHECK(!"Unknown AMR band mode.");
3181 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003182 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003183 mOutputFormat->setCString(
3184 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003185 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003186 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3187 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003188 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003189 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3190 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003191 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003192 } else {
3193 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003194 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003195 break;
3196 }
3197
3198 case OMX_PortDomainVideo:
3199 {
3200 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3201
3202 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003203 mOutputFormat->setCString(
3204 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003205 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003206 mOutputFormat->setCString(
3207 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003208 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003209 mOutputFormat->setCString(
3210 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003211 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003212 mOutputFormat->setCString(
3213 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003214 } else {
3215 CHECK(!"Unknown compression format.");
3216 }
3217
3218 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3219 // This component appears to be lying to me.
3220 mOutputFormat->setInt32(
3221 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3222 mOutputFormat->setInt32(
3223 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3224 } else {
3225 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3226 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3227 }
3228
3229 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3230 break;
3231 }
3232
3233 default:
3234 {
3235 CHECK(!"should not be here, neither audio nor video.");
3236 break;
3237 }
3238 }
3239}
3240
Andreas Huber1f24b302010-06-10 11:12:39 -07003241status_t OMXCodec::pause() {
3242 Mutex::Autolock autoLock(mLock);
3243
3244 mPaused = true;
3245
3246 return OK;
3247}
3248
Andreas Hubere6c40962009-09-10 14:13:30 -07003249////////////////////////////////////////////////////////////////////////////////
3250
3251status_t QueryCodecs(
3252 const sp<IOMX> &omx,
3253 const char *mime, bool queryDecoders,
3254 Vector<CodecCapabilities> *results) {
3255 results->clear();
3256
3257 for (int index = 0;; ++index) {
3258 const char *componentName;
3259
3260 if (!queryDecoders) {
3261 componentName = GetCodec(
3262 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3263 mime, index);
3264 } else {
3265 componentName = GetCodec(
3266 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3267 mime, index);
3268 }
3269
3270 if (!componentName) {
3271 return OK;
3272 }
3273
Andreas Huber1a189a82010-03-24 13:49:20 -07003274 if (strncmp(componentName, "OMX.", 4)) {
3275 // Not an OpenMax component but a software codec.
3276
3277 results->push();
3278 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3279 caps->mComponentName = componentName;
3280
3281 continue;
3282 }
3283
Andreas Huber784202e2009-10-15 13:46:54 -07003284 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003285 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003286 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003287
3288 if (err != OK) {
3289 continue;
3290 }
3291
James Dong722d5912010-04-13 10:56:59 -07003292 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003293
3294 results->push();
3295 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3296 caps->mComponentName = componentName;
3297
3298 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3299 InitOMXParams(&param);
3300
3301 param.nPortIndex = queryDecoders ? 0 : 1;
3302
3303 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003304 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003305 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3306 &param, sizeof(param));
3307
3308 if (err != OK) {
3309 break;
3310 }
3311
3312 CodecProfileLevel profileLevel;
3313 profileLevel.mProfile = param.eProfile;
3314 profileLevel.mLevel = param.eLevel;
3315
3316 caps->mProfileLevels.push(profileLevel);
3317 }
3318
Andreas Huber784202e2009-10-15 13:46:54 -07003319 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003320 }
3321}
3322
Andreas Huberbe06d262009-08-14 14:37:10 -07003323} // namespace android