blob: 37def3b63542cd29792a50b17d2582e84d392809 [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 Dong1cc31e62010-07-02 17:44:44 -070028#include "include/AVCEncoder.h"
Andreas Huber520b2a72010-08-09 09:54:59 -070029#include "include/G711Decoder.h"
James Dong02f5b542009-12-15 16:26:55 -080030#include "include/M4vH263Decoder.h"
James Dong42ef0c72010-07-12 21:46:25 -070031#include "include/M4vH263Encoder.h"
Andreas Huber250f2432009-12-07 14:22:35 -080032#include "include/MP3Decoder.h"
Andreas Huber388379f2010-05-07 10:35:13 -070033#include "include/VorbisDecoder.h"
Andreas Huber47ba30e2010-05-24 14:38:02 -070034#include "include/VPXDecoder.h"
Andreas Huber8c7ab032009-12-07 11:23:44 -080035
Andreas Huberbd7b43b2009-10-13 10:22:55 -070036#include "include/ESDS.h"
37
Andreas Huberbe06d262009-08-14 14:37:10 -070038#include <binder/IServiceManager.h>
39#include <binder/MemoryDealer.h>
40#include <binder/ProcessState.h>
41#include <media/IMediaPlayerService.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070042#include <media/stagefright/MediaBuffer.h>
43#include <media/stagefright/MediaBufferGroup.h>
44#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070045#include <media/stagefright/MediaDefs.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070046#include <media/stagefright/MediaExtractor.h>
47#include <media/stagefright/MetaData.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070048#include <media/stagefright/OMXCodec.h>
Andreas Huberebf66ea2009-08-19 13:32:58 -070049#include <media/stagefright/Utils.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070050#include <utils/Vector.h>
51
52#include <OMX_Audio.h>
53#include <OMX_Component.h>
54
55namespace android {
56
Andreas Huber8b432b12009-10-07 13:36:52 -070057static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
58
Andreas Huberbe06d262009-08-14 14:37:10 -070059struct CodecInfo {
60 const char *mime;
61 const char *codec;
62};
63
Andreas Huberfb1c2f82009-12-15 13:25:11 -080064#define FACTORY_CREATE(name) \
65static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \
66 return new name(source); \
67}
68
James Dong17299ab2010-05-14 15:45:22 -070069#define FACTORY_CREATE_ENCODER(name) \
70static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \
71 return new name(source, meta); \
72}
73
Andreas Huberfb1c2f82009-12-15 13:25:11 -080074#define FACTORY_REF(name) { #name, Make##name },
75
76FACTORY_CREATE(MP3Decoder)
77FACTORY_CREATE(AMRNBDecoder)
78FACTORY_CREATE(AMRWBDecoder)
79FACTORY_CREATE(AACDecoder)
80FACTORY_CREATE(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -070081FACTORY_CREATE(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -080082FACTORY_CREATE(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -070083FACTORY_CREATE(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -070084FACTORY_CREATE(VPXDecoder)
James Dong17299ab2010-05-14 15:45:22 -070085FACTORY_CREATE_ENCODER(AMRNBEncoder)
86FACTORY_CREATE_ENCODER(AMRWBEncoder)
87FACTORY_CREATE_ENCODER(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -070088FACTORY_CREATE_ENCODER(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -070089FACTORY_CREATE_ENCODER(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -070090
91static sp<MediaSource> InstantiateSoftwareEncoder(
92 const char *name, const sp<MediaSource> &source,
93 const sp<MetaData> &meta) {
94 struct FactoryInfo {
95 const char *name;
96 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &);
97 };
98
99 static const FactoryInfo kFactoryInfo[] = {
100 FACTORY_REF(AMRNBEncoder)
101 FACTORY_REF(AMRWBEncoder)
102 FACTORY_REF(AACEncoder)
James Dong1cc31e62010-07-02 17:44:44 -0700103 FACTORY_REF(AVCEncoder)
James Dong42ef0c72010-07-12 21:46:25 -0700104 FACTORY_REF(M4vH263Encoder)
James Dong17299ab2010-05-14 15:45:22 -0700105 };
106 for (size_t i = 0;
107 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
108 if (!strcmp(name, kFactoryInfo[i].name)) {
109 return (*kFactoryInfo[i].CreateFunc)(source, meta);
110 }
111 }
112
113 return NULL;
114}
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800115
116static sp<MediaSource> InstantiateSoftwareCodec(
117 const char *name, const sp<MediaSource> &source) {
118 struct FactoryInfo {
119 const char *name;
120 sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &);
121 };
122
123 static const FactoryInfo kFactoryInfo[] = {
124 FACTORY_REF(MP3Decoder)
125 FACTORY_REF(AMRNBDecoder)
126 FACTORY_REF(AMRWBDecoder)
127 FACTORY_REF(AACDecoder)
128 FACTORY_REF(AVCDecoder)
Andreas Huber520b2a72010-08-09 09:54:59 -0700129 FACTORY_REF(G711Decoder)
James Dong02f5b542009-12-15 16:26:55 -0800130 FACTORY_REF(M4vH263Decoder)
Andreas Huber388379f2010-05-07 10:35:13 -0700131 FACTORY_REF(VorbisDecoder)
Andreas Huber47ba30e2010-05-24 14:38:02 -0700132 FACTORY_REF(VPXDecoder)
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800133 };
134 for (size_t i = 0;
135 i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) {
136 if (!strcmp(name, kFactoryInfo[i].name)) {
137 return (*kFactoryInfo[i].CreateFunc)(source);
138 }
139 }
140
141 return NULL;
142}
143
144#undef FACTORY_REF
145#undef FACTORY_CREATE
146
Andreas Huberbe06d262009-08-14 14:37:10 -0700147static const CodecInfo kDecoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700148 { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
Andreas Huberd222c842010-08-26 14:29:34 -0700149// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.Nvidia.mp3.decoder" },
James Dong374aee62010-04-26 10:23:30 -0700150// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800151 { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700152// { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
Andreas Hubera4357ad2010-04-02 12:49:54 -0700153// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
Andreas Huberd222c842010-08-26 14:29:34 -0700154// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amr.decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800155 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700156// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
Andreas Huberd222c842010-08-26 14:29:34 -0700157// { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amrwb.decoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700158 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800159 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700160// { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
Andreas Huberd222c842010-08-26 14:29:34 -0700161// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.Nvidia.aac.decoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700162 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800163 { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700164// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
Andreas Huber520b2a72010-08-09 09:54:59 -0700165 { MEDIA_MIMETYPE_AUDIO_G711_ALAW, "G711Decoder" },
166 { MEDIA_MIMETYPE_AUDIO_G711_MLAW, "G711Decoder" },
Andreas Huberd222c842010-08-26 14:29:34 -0700167// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.Nvidia.mp4.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700168 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700169 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
170 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800171 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700172// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
Andreas Huberd222c842010-08-26 14:29:34 -0700173// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.Nvidia.h263.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700174 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700175 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800176 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700177// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
pgudadhe6ad2c352010-07-26 15:04:33 -0700178 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.decode" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700179 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700180 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
181 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800182 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" },
Andreas Huber1a189a82010-03-24 13:49:20 -0700183// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
Andreas Huber388379f2010-05-07 10:35:13 -0700184 { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" },
Andreas Huber47ba30e2010-05-24 14:38:02 -0700185 { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700186};
187
188static const CodecInfo kEncoderInfo[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -0700189 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
Andreas Huberacfbc802010-02-04 10:48:37 -0800190 { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700191 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700192 { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700193 { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
James Dong17299ab2010-05-14 15:45:22 -0700194 { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700195// { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
196 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700197 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
198 { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700199 { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700200// { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
201 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700202 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
203 { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
James Dong42ef0c72010-07-12 21:46:25 -0700204 { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700205// { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
206 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" },
Andreas Huber71c27d92010-03-19 11:43:15 -0700207 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" },
Andreas Hubere6c40962009-09-10 14:13:30 -0700208 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
pgudadhe9c305322010-07-26 13:59:29 -0700209 { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.encoder" },
James Dong1cc31e62010-07-02 17:44:44 -0700210 { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" },
Andreas Huber8ef64c92010-06-29 09:14:00 -0700211// { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
Andreas Huberbe06d262009-08-14 14:37:10 -0700212};
213
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800214#undef OPTIONAL
215
Andreas Hubere0873732009-09-10 09:57:53 -0700216#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700217#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber42c444a2010-02-09 10:20:00 -0800218#define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__)
Andreas Huber4c483422009-09-02 16:05:36 -0700219
Andreas Huberbe06d262009-08-14 14:37:10 -0700220struct OMXCodecObserver : public BnOMXObserver {
Andreas Huber784202e2009-10-15 13:46:54 -0700221 OMXCodecObserver() {
222 }
223
224 void setCodec(const sp<OMXCodec> &target) {
225 mTarget = target;
Andreas Huberbe06d262009-08-14 14:37:10 -0700226 }
227
228 // from IOMXObserver
Andreas Huber784202e2009-10-15 13:46:54 -0700229 virtual void onMessage(const omx_message &msg) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700230 sp<OMXCodec> codec = mTarget.promote();
231
232 if (codec.get() != NULL) {
233 codec->on_message(msg);
234 }
235 }
236
237protected:
238 virtual ~OMXCodecObserver() {}
239
240private:
241 wp<OMXCodec> mTarget;
242
243 OMXCodecObserver(const OMXCodecObserver &);
244 OMXCodecObserver &operator=(const OMXCodecObserver &);
245};
246
247static const char *GetCodec(const CodecInfo *info, size_t numInfos,
248 const char *mime, int index) {
249 CHECK(index >= 0);
250 for(size_t i = 0; i < numInfos; ++i) {
251 if (!strcasecmp(mime, info[i].mime)) {
252 if (index == 0) {
253 return info[i].codec;
254 }
255
256 --index;
257 }
258 }
259
260 return NULL;
261}
262
Andreas Huberebf66ea2009-08-19 13:32:58 -0700263enum {
264 kAVCProfileBaseline = 0x42,
265 kAVCProfileMain = 0x4d,
266 kAVCProfileExtended = 0x58,
267 kAVCProfileHigh = 0x64,
268 kAVCProfileHigh10 = 0x6e,
269 kAVCProfileHigh422 = 0x7a,
270 kAVCProfileHigh444 = 0xf4,
271 kAVCProfileCAVLC444Intra = 0x2c
272};
273
274static const char *AVCProfileToString(uint8_t profile) {
275 switch (profile) {
276 case kAVCProfileBaseline:
277 return "Baseline";
278 case kAVCProfileMain:
279 return "Main";
280 case kAVCProfileExtended:
281 return "Extended";
282 case kAVCProfileHigh:
283 return "High";
284 case kAVCProfileHigh10:
285 return "High 10";
286 case kAVCProfileHigh422:
287 return "High 422";
288 case kAVCProfileHigh444:
289 return "High 444";
290 case kAVCProfileCAVLC444Intra:
291 return "CAVLC 444 Intra";
292 default: return "Unknown";
293 }
294}
295
Andreas Huber4c483422009-09-02 16:05:36 -0700296template<class T>
297static void InitOMXParams(T *params) {
298 params->nSize = sizeof(T);
299 params->nVersion.s.nVersionMajor = 1;
300 params->nVersion.s.nVersionMinor = 0;
301 params->nVersion.s.nRevision = 0;
302 params->nVersion.s.nStep = 0;
303}
304
Andreas Hubere13526a2009-10-22 10:43:34 -0700305static bool IsSoftwareCodec(const char *componentName) {
306 if (!strncmp("OMX.PV.", componentName, 7)) {
307 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -0700308 }
309
Andreas Hubere13526a2009-10-22 10:43:34 -0700310 return false;
311}
312
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800313// A sort order in which non-OMX components are first,
314// followed by software codecs, i.e. OMX.PV.*, followed
315// by all the others.
Andreas Hubere13526a2009-10-22 10:43:34 -0700316static int CompareSoftwareCodecsFirst(
317 const String8 *elem1, const String8 *elem2) {
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800318 bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4);
319 bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4);
320
321 if (isNotOMX1) {
322 if (isNotOMX2) { return 0; }
323 return -1;
324 }
325 if (isNotOMX2) {
326 return 1;
327 }
328
Andreas Hubere13526a2009-10-22 10:43:34 -0700329 bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string());
330 bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string());
331
332 if (isSoftwareCodec1) {
333 if (isSoftwareCodec2) { return 0; }
334 return -1;
335 }
336
337 if (isSoftwareCodec2) {
338 return 1;
339 }
340
341 return 0;
342}
343
344// static
345uint32_t OMXCodec::getComponentQuirks(const char *componentName) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700346 uint32_t quirks = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700347
Dima Zavin30ba6cb2010-08-23 11:10:03 -0700348 if (!strcmp(componentName, "OMX.Nvidia.amr.decoder") ||
349 !strcmp(componentName, "OMX.Nvidia.amrwb.decoder") ||
350 !strcmp(componentName, "OMX.Nvidia.aac.decoder") ||
351 !strcmp(componentName, "OMX.Nvidia.mp3.decoder")) {
352 quirks |= kDecoderLiesAboutNumberOfChannels;
353 }
354
Andreas Huberbe06d262009-08-14 14:37:10 -0700355 if (!strcmp(componentName, "OMX.PV.avcdec")) {
Andreas Huber4f5e6022009-08-19 09:29:34 -0700356 quirks |= kWantsNALFragments;
Andreas Huberbe06d262009-08-14 14:37:10 -0700357 }
358 if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
359 quirks |= kNeedsFlushBeforeDisable;
Andreas Hubere331c7b2010-02-01 10:51:50 -0800360 quirks |= kDecoderLiesAboutNumberOfChannels;
Andreas Huberbe06d262009-08-14 14:37:10 -0700361 }
362 if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
363 quirks |= kNeedsFlushBeforeDisable;
Andreas Huber404cc412009-08-25 14:26:05 -0700364 quirks |= kRequiresFlushCompleteEmulation;
Andreas Hubera4357ad2010-04-02 12:49:54 -0700365 quirks |= kSupportsMultipleFramesPerInputBuffer;
Andreas Huberbe06d262009-08-14 14:37:10 -0700366 }
367 if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
368 quirks |= kRequiresLoadedToIdleAfterAllocation;
369 quirks |= kRequiresAllocateBufferOnInputPorts;
Andreas Huberb482ce82009-10-29 12:02:48 -0700370 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huberbe06d262009-08-14 14:37:10 -0700371 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700372 if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) {
373 }
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700374 if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700375 quirks |= kRequiresAllocateBufferOnOutputPorts;
Andreas Huber52733b82010-01-25 10:41:35 -0800376 quirks |= kDefersOutputBufferAllocation;
Andreas Hubera7d0cf42009-09-04 07:48:51 -0700377 }
Andreas Huber8ef64c92010-06-29 09:14:00 -0700378 if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) {
379 quirks |= kRequiresAllocateBufferOnInputPorts;
380 quirks |= kRequiresAllocateBufferOnOutputPorts;
381 quirks |= kDefersOutputBufferAllocation;
382 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700383
Andreas Huber2dc64d82009-09-11 12:58:53 -0700384 if (!strncmp(componentName, "OMX.TI.", 7)) {
385 // Apparently I must not use OMX_UseBuffer on either input or
386 // output ports on any of the TI components or quote:
387 // "(I) may have unexpected problem (sic) which can be timing related
388 // and hard to reproduce."
389
390 quirks |= kRequiresAllocateBufferOnInputPorts;
391 quirks |= kRequiresAllocateBufferOnOutputPorts;
James Dongdca66e12010-06-14 11:14:38 -0700392 if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) {
James Dong4f501f02010-06-07 14:41:41 -0700393 quirks |= kAvoidMemcopyInputRecordingFrames;
394 }
Andreas Huber2dc64d82009-09-11 12:58:53 -0700395 }
396
Andreas Huberb8de9572010-02-22 14:58:45 -0800397 if (!strcmp(componentName, "OMX.TI.Video.Decoder")) {
398 quirks |= kInputBufferSizesAreBogus;
399 }
400
Andreas Hubere13526a2009-10-22 10:43:34 -0700401 return quirks;
402}
403
404// static
405void OMXCodec::findMatchingCodecs(
406 const char *mime,
407 bool createEncoder, const char *matchComponentName,
408 uint32_t flags,
409 Vector<String8> *matchingCodecs) {
410 matchingCodecs->clear();
411
412 for (int index = 0;; ++index) {
413 const char *componentName;
414
415 if (createEncoder) {
416 componentName = GetCodec(
417 kEncoderInfo,
418 sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
419 mime, index);
420 } else {
421 componentName = GetCodec(
422 kDecoderInfo,
423 sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
424 mime, index);
425 }
426
427 if (!componentName) {
428 break;
429 }
430
431 // If a specific codec is requested, skip the non-matching ones.
432 if (matchComponentName && strcmp(componentName, matchComponentName)) {
433 continue;
434 }
435
436 matchingCodecs->push(String8(componentName));
437 }
438
439 if (flags & kPreferSoftwareCodecs) {
440 matchingCodecs->sort(CompareSoftwareCodecsFirst);
441 }
442}
443
444// static
Andreas Huber91eb0352009-12-07 09:43:00 -0800445sp<MediaSource> OMXCodec::Create(
Andreas Hubere13526a2009-10-22 10:43:34 -0700446 const sp<IOMX> &omx,
447 const sp<MetaData> &meta, bool createEncoder,
448 const sp<MediaSource> &source,
449 const char *matchComponentName,
450 uint32_t flags) {
451 const char *mime;
452 bool success = meta->findCString(kKeyMIMEType, &mime);
453 CHECK(success);
454
455 Vector<String8> matchingCodecs;
456 findMatchingCodecs(
457 mime, createEncoder, matchComponentName, flags, &matchingCodecs);
458
459 if (matchingCodecs.isEmpty()) {
460 return NULL;
461 }
462
463 sp<OMXCodecObserver> observer = new OMXCodecObserver;
464 IOMX::node_id node = 0;
Andreas Hubere13526a2009-10-22 10:43:34 -0700465
466 const char *componentName;
467 for (size_t i = 0; i < matchingCodecs.size(); ++i) {
468 componentName = matchingCodecs[i].string();
469
James Dong17299ab2010-05-14 15:45:22 -0700470 sp<MediaSource> softwareCodec = createEncoder?
471 InstantiateSoftwareEncoder(componentName, source, meta):
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800472 InstantiateSoftwareCodec(componentName, source);
473
474 if (softwareCodec != NULL) {
475 LOGV("Successfully allocated software codec '%s'", componentName);
476
477 return softwareCodec;
478 }
Andreas Huberfb1c2f82009-12-15 13:25:11 -0800479
Andreas Hubere13526a2009-10-22 10:43:34 -0700480 LOGV("Attempting to allocate OMX node '%s'", componentName);
481
482 status_t err = omx->allocateNode(componentName, observer, &node);
483 if (err == OK) {
484 LOGV("Successfully allocated OMX node '%s'", componentName);
485
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700486 sp<OMXCodec> codec = new OMXCodec(
487 omx, node, getComponentQuirks(componentName),
488 createEncoder, mime, componentName,
489 source);
490
491 observer->setCodec(codec);
492
493 err = codec->configureCodec(meta);
494
495 if (err == OK) {
496 return codec;
497 }
498
499 LOGV("Failed to configure codec '%s'", componentName);
Andreas Hubere13526a2009-10-22 10:43:34 -0700500 }
501 }
502
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700503 return NULL;
504}
Andreas Hubere13526a2009-10-22 10:43:34 -0700505
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700506status_t OMXCodec::configureCodec(const sp<MetaData> &meta) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700507 uint32_t type;
508 const void *data;
509 size_t size;
510 if (meta->findData(kKeyESDS, &type, &data, &size)) {
511 ESDS esds((const char *)data, size);
512 CHECK_EQ(esds.InitCheck(), OK);
513
514 const void *codec_specific_data;
515 size_t codec_specific_data_size;
516 esds.getCodecSpecificInfo(
517 &codec_specific_data, &codec_specific_data_size);
518
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700519 addCodecSpecificData(
Andreas Huberbe06d262009-08-14 14:37:10 -0700520 codec_specific_data, codec_specific_data_size);
521 } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
Andreas Huberebf66ea2009-08-19 13:32:58 -0700522 // Parse the AVCDecoderConfigurationRecord
523
524 const uint8_t *ptr = (const uint8_t *)data;
525
526 CHECK(size >= 7);
527 CHECK_EQ(ptr[0], 1); // configurationVersion == 1
528 uint8_t profile = ptr[1];
529 uint8_t level = ptr[3];
530
Andreas Huber44e15c42009-11-23 14:39:38 -0800531 // There is decodable content out there that fails the following
532 // assertion, let's be lenient for now...
533 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
Andreas Huberebf66ea2009-08-19 13:32:58 -0700534
535 size_t lengthSize = 1 + (ptr[4] & 3);
536
537 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
538 // violates it...
539 // CHECK((ptr[5] >> 5) == 7); // reserved
540
541 size_t numSeqParameterSets = ptr[5] & 31;
542
543 ptr += 6;
Andreas Huberbe06d262009-08-14 14:37:10 -0700544 size -= 6;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700545
546 for (size_t i = 0; i < numSeqParameterSets; ++i) {
547 CHECK(size >= 2);
548 size_t length = U16_AT(ptr);
Andreas Huberbe06d262009-08-14 14:37:10 -0700549
550 ptr += 2;
551 size -= 2;
552
Andreas Huberbe06d262009-08-14 14:37:10 -0700553 CHECK(size >= length);
554
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700555 addCodecSpecificData(ptr, length);
Andreas Huberbe06d262009-08-14 14:37:10 -0700556
557 ptr += length;
558 size -= length;
Andreas Huberebf66ea2009-08-19 13:32:58 -0700559 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700560
Andreas Huberebf66ea2009-08-19 13:32:58 -0700561 CHECK(size >= 1);
562 size_t numPictureParameterSets = *ptr;
563 ++ptr;
564 --size;
Andreas Huberbe06d262009-08-14 14:37:10 -0700565
Andreas Huberebf66ea2009-08-19 13:32:58 -0700566 for (size_t i = 0; i < numPictureParameterSets; ++i) {
567 CHECK(size >= 2);
568 size_t length = U16_AT(ptr);
569
570 ptr += 2;
571 size -= 2;
572
573 CHECK(size >= length);
574
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700575 addCodecSpecificData(ptr, length);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700576
577 ptr += length;
578 size -= length;
579 }
580
Andreas Hubere2f85072010-06-10 09:51:27 -0700581 CODEC_LOGV(
582 "AVC profile = %d (%s), level = %d",
583 (int)profile, AVCProfileToString(profile), level);
Andreas Huberebf66ea2009-08-19 13:32:58 -0700584
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700585 if (!strcmp(mComponentName, "OMX.TI.Video.Decoder")
Andreas Hubere2f85072010-06-10 09:51:27 -0700586 && (profile != kAVCProfileBaseline || level > 30)) {
Andreas Huber784202e2009-10-15 13:46:54 -0700587 // This stream exceeds the decoder's capabilities. The decoder
588 // does not handle this gracefully and would clobber the heap
589 // and wreak havoc instead...
Andreas Huberebf66ea2009-08-19 13:32:58 -0700590
591 LOGE("Profile and/or level exceed the decoder's capabilities.");
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700592 return ERROR_UNSUPPORTED;
Andreas Huberbe06d262009-08-14 14:37:10 -0700593 }
594 }
595
James Dong17299ab2010-05-14 15:45:22 -0700596 int32_t bitRate = 0;
597 if (mIsEncoder) {
598 CHECK(meta->findInt32(kKeyBitRate, &bitRate));
599 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700600 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700601 setAMRFormat(false /* isWAMR */, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700602 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700603 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) {
James Dong17299ab2010-05-14 15:45:22 -0700604 setAMRFormat(true /* isWAMR */, bitRate);
Andreas Huberee606e62009-09-08 10:19:21 -0700605 }
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700606 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) {
Andreas Huber43ad6eaf2009-09-01 16:02:43 -0700607 int32_t numChannels, sampleRate;
608 CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
609 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
610
James Dong17299ab2010-05-14 15:45:22 -0700611 setAACFormat(numChannels, sampleRate, bitRate);
Andreas Huberbe06d262009-08-14 14:37:10 -0700612 }
James Dongabed93a2010-04-22 17:27:04 -0700613
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700614 if (!strncasecmp(mMIME, "video/", 6)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700615
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700616 if (mIsEncoder) {
James Dong1244eab2010-06-08 11:58:53 -0700617 setVideoInputFormat(mMIME, meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700618 } else {
James Dong1244eab2010-06-08 11:58:53 -0700619 int32_t width, height;
620 bool success = meta->findInt32(kKeyWidth, &width);
621 success = success && meta->findInt32(kKeyHeight, &height);
622 CHECK(success);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700623 status_t err = setVideoOutputFormat(
624 mMIME, width, height);
625
626 if (err != OK) {
627 return err;
628 }
Andreas Huberbe06d262009-08-14 14:37:10 -0700629 }
630 }
Andreas Hubera4357ad2010-04-02 12:49:54 -0700631
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700632 if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG)
633 && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700634 OMX_COLOR_FORMATTYPE format =
635 OMX_COLOR_Format32bitARGB8888;
636 // OMX_COLOR_FormatYUV420PackedPlanar;
637 // OMX_COLOR_FormatCbYCrY;
638 // OMX_COLOR_FormatYUV411Planar;
639
640 int32_t width, height;
641 bool success = meta->findInt32(kKeyWidth, &width);
642 success = success && meta->findInt32(kKeyHeight, &height);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700643
644 int32_t compressedSize;
645 success = success && meta->findInt32(
Andreas Huberda050cf22009-09-02 14:01:43 -0700646 kKeyMaxInputSize, &compressedSize);
Andreas Huber5c0a9132009-08-20 11:16:40 -0700647
648 CHECK(success);
649 CHECK(compressedSize > 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700650
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700651 setImageOutputFormat(format, width, height);
652 setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
Andreas Huberbe06d262009-08-14 14:37:10 -0700653 }
654
Andreas Huberda050cf22009-09-02 14:01:43 -0700655 int32_t maxInputSize;
Andreas Huber1bceff92009-11-23 14:03:32 -0800656 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700657 setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
Andreas Huberda050cf22009-09-02 14:01:43 -0700658 }
659
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700660 if (!strcmp(mComponentName, "OMX.TI.AMR.encode")
James Dongabed93a2010-04-22 17:27:04 -0700661 || !strcmp(mComponentName, "OMX.TI.WBAMR.encode")
662 || !strcmp(mComponentName, "OMX.TI.AAC.encode")) {
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700663 setMinBufferSize(kPortIndexOutput, 8192); // XXX
Andreas Huberda050cf22009-09-02 14:01:43 -0700664 }
665
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700666 initOutputFormat(meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700667
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700668 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -0700669}
670
Andreas Huberda050cf22009-09-02 14:01:43 -0700671void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
672 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -0700673 InitOMXParams(&def);
Andreas Huberda050cf22009-09-02 14:01:43 -0700674 def.nPortIndex = portIndex;
675
Andreas Huber784202e2009-10-15 13:46:54 -0700676 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700677 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
678 CHECK_EQ(err, OK);
679
Andreas Huberb8de9572010-02-22 14:58:45 -0800680 if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus))
681 || (def.nBufferSize < size)) {
Andreas Huberda050cf22009-09-02 14:01:43 -0700682 def.nBufferSize = size;
Andreas Huberda050cf22009-09-02 14:01:43 -0700683 }
684
Andreas Huber784202e2009-10-15 13:46:54 -0700685 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -0700686 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
687 CHECK_EQ(err, OK);
Andreas Huber1bceff92009-11-23 14:03:32 -0800688
689 err = mOMX->getParameter(
690 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
691 CHECK_EQ(err, OK);
692
693 // Make sure the setting actually stuck.
Andreas Huberb8de9572010-02-22 14:58:45 -0800694 if (portIndex == kPortIndexInput
695 && (mQuirks & kInputBufferSizesAreBogus)) {
696 CHECK_EQ(def.nBufferSize, size);
697 } else {
698 CHECK(def.nBufferSize >= size);
699 }
Andreas Huberda050cf22009-09-02 14:01:43 -0700700}
701
Andreas Huberbe06d262009-08-14 14:37:10 -0700702status_t OMXCodec::setVideoPortFormatType(
703 OMX_U32 portIndex,
704 OMX_VIDEO_CODINGTYPE compressionFormat,
705 OMX_COLOR_FORMATTYPE colorFormat) {
706 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -0700707 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -0700708 format.nPortIndex = portIndex;
709 format.nIndex = 0;
710 bool found = false;
711
712 OMX_U32 index = 0;
713 for (;;) {
714 format.nIndex = index;
Andreas Huber784202e2009-10-15 13:46:54 -0700715 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700716 mNode, OMX_IndexParamVideoPortFormat,
717 &format, sizeof(format));
718
719 if (err != OK) {
720 return err;
721 }
722
723 // The following assertion is violated by TI's video decoder.
Andreas Huber5c0a9132009-08-20 11:16:40 -0700724 // CHECK_EQ(format.nIndex, index);
Andreas Huberbe06d262009-08-14 14:37:10 -0700725
726#if 1
Andreas Huber53a76bd2009-10-06 16:20:44 -0700727 CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
Andreas Huberbe06d262009-08-14 14:37:10 -0700728 portIndex,
729 index, format.eCompressionFormat, format.eColorFormat);
730#endif
731
732 if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
733 if (portIndex == kPortIndexInput
734 && colorFormat == format.eColorFormat) {
735 // eCompressionFormat does not seem right.
736 found = true;
737 break;
738 }
739 if (portIndex == kPortIndexOutput
740 && compressionFormat == format.eCompressionFormat) {
741 // eColorFormat does not seem right.
742 found = true;
743 break;
744 }
745 }
746
747 if (format.eCompressionFormat == compressionFormat
748 && format.eColorFormat == colorFormat) {
749 found = true;
750 break;
751 }
752
753 ++index;
754 }
755
756 if (!found) {
757 return UNKNOWN_ERROR;
758 }
759
Andreas Huber53a76bd2009-10-06 16:20:44 -0700760 CODEC_LOGV("found a match.");
Andreas Huber784202e2009-10-15 13:46:54 -0700761 status_t err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700762 mNode, OMX_IndexParamVideoPortFormat,
763 &format, sizeof(format));
764
765 return err;
766}
767
Andreas Huberb482ce82009-10-29 12:02:48 -0700768static size_t getFrameSize(
769 OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) {
770 switch (colorFormat) {
771 case OMX_COLOR_FormatYCbYCr:
772 case OMX_COLOR_FormatCbYCrY:
773 return width * height * 2;
774
Andreas Huber71c27d92010-03-19 11:43:15 -0700775 case OMX_COLOR_FormatYUV420Planar:
Andreas Huberb482ce82009-10-29 12:02:48 -0700776 case OMX_COLOR_FormatYUV420SemiPlanar:
777 return (width * height * 3) / 2;
778
779 default:
780 CHECK(!"Should not be here. Unsupported color format.");
781 break;
782 }
783}
784
James Dongafd97e82010-08-03 17:19:23 -0700785status_t OMXCodec::findTargetColorFormat(
786 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) {
787 LOGV("findTargetColorFormat");
788 CHECK(mIsEncoder);
789
790 *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
791 int32_t targetColorFormat;
792 if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) {
793 *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat;
794 } else {
795 if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) {
796 *colorFormat = OMX_COLOR_FormatYCbYCr;
797 }
798 }
799
800 // Check whether the target color format is supported.
801 return isColorFormatSupported(*colorFormat, kPortIndexInput);
802}
803
804status_t OMXCodec::isColorFormatSupported(
805 OMX_COLOR_FORMATTYPE colorFormat, int portIndex) {
806 LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat));
807
808 // Enumerate all the color formats supported by
809 // the omx component to see whether the given
810 // color format is supported.
811 OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat;
812 InitOMXParams(&portFormat);
813 portFormat.nPortIndex = portIndex;
814 OMX_U32 index = 0;
815 portFormat.nIndex = index;
816 while (true) {
817 if (OMX_ErrorNone != mOMX->getParameter(
818 mNode, OMX_IndexParamVideoPortFormat,
819 &portFormat, sizeof(portFormat))) {
820
821 return UNKNOWN_ERROR;
822 }
823 // Make sure that omx component does not overwrite
824 // the incremented index (bug 2897413).
825 CHECK_EQ(index, portFormat.nIndex);
826 if ((portFormat.eColorFormat == colorFormat)) {
827 LOGV("Found supported color format: %d", portFormat.eColorFormat);
828 return OK; // colorFormat is supported!
829 }
830 ++index;
831 portFormat.nIndex = index;
832
833 // OMX Spec defines less than 50 color formats
834 // 1000 is more than enough for us to tell whether the omx
835 // component in question is buggy or not.
836 if (index >= 1000) {
837 LOGE("More than %ld color formats are supported???", index);
838 break;
839 }
840 }
841 return UNKNOWN_ERROR;
842}
843
Andreas Huberbe06d262009-08-14 14:37:10 -0700844void OMXCodec::setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700845 const char *mime, const sp<MetaData>& meta) {
846
847 int32_t width, height, frameRate, bitRate, stride, sliceHeight;
848 bool success = meta->findInt32(kKeyWidth, &width);
849 success = success && meta->findInt32(kKeyHeight, &height);
850 success = success && meta->findInt32(kKeySampleRate, &frameRate);
851 success = success && meta->findInt32(kKeyBitRate, &bitRate);
852 success = success && meta->findInt32(kKeyStride, &stride);
853 success = success && meta->findInt32(kKeySliceHeight, &sliceHeight);
854 CHECK(success);
855 CHECK(stride != 0);
Andreas Huberbe06d262009-08-14 14:37:10 -0700856
857 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -0700858 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700859 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -0700860 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700861 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -0700862 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -0700863 compressionFormat = OMX_VIDEO_CodingH263;
864 } else {
865 LOGE("Not a supported video mime type: %s", mime);
866 CHECK(!"Should not be here. Not a supported video mime type.");
867 }
868
James Dongafd97e82010-08-03 17:19:23 -0700869 OMX_COLOR_FORMATTYPE colorFormat;
870 CHECK_EQ(OK, findTargetColorFormat(meta, &colorFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -0700871
pgudadhe9c305322010-07-26 13:59:29 -0700872 if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) {
873 colorFormat = OMX_COLOR_FormatYUV420Planar;
874 }
875
James Dongb00e2462010-04-26 17:48:26 -0700876 status_t err;
877 OMX_PARAM_PORTDEFINITIONTYPE def;
878 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
879
880 //////////////////////// Input port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700881 CHECK_EQ(setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -0700882 kPortIndexInput, OMX_VIDEO_CodingUnused,
Andreas Huberb482ce82009-10-29 12:02:48 -0700883 colorFormat), OK);
James Dong4f501f02010-06-07 14:41:41 -0700884
James Dongb00e2462010-04-26 17:48:26 -0700885 InitOMXParams(&def);
886 def.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -0700887
James Dongb00e2462010-04-26 17:48:26 -0700888 err = mOMX->getParameter(
889 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
890 CHECK_EQ(err, OK);
891
James Dong1244eab2010-06-08 11:58:53 -0700892 def.nBufferSize = getFrameSize(colorFormat,
893 stride > 0? stride: -stride, sliceHeight);
James Dongb00e2462010-04-26 17:48:26 -0700894
895 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
896
897 video_def->nFrameWidth = width;
898 video_def->nFrameHeight = height;
James Dong1244eab2010-06-08 11:58:53 -0700899 video_def->nStride = stride;
900 video_def->nSliceHeight = sliceHeight;
James Dong4f501f02010-06-07 14:41:41 -0700901 video_def->xFramerate = (frameRate << 16); // Q16 format
James Dongb00e2462010-04-26 17:48:26 -0700902 video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
903 video_def->eColorFormat = colorFormat;
904
James Dongb00e2462010-04-26 17:48:26 -0700905 err = mOMX->setParameter(
906 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
907 CHECK_EQ(err, OK);
908
909 //////////////////////// Output port /////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700910 CHECK_EQ(setVideoPortFormatType(
911 kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused),
912 OK);
Andreas Huber4c483422009-09-02 16:05:36 -0700913 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -0700914 def.nPortIndex = kPortIndexOutput;
915
James Dongb00e2462010-04-26 17:48:26 -0700916 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700917 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
918
919 CHECK_EQ(err, OK);
920 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
921
922 video_def->nFrameWidth = width;
923 video_def->nFrameHeight = height;
James Dong81c929a2010-07-01 15:02:14 -0700924 video_def->xFramerate = 0; // No need for output port
James Dong4f501f02010-06-07 14:41:41 -0700925 video_def->nBitrate = bitRate; // Q16 format
Andreas Huberbe06d262009-08-14 14:37:10 -0700926 video_def->eCompressionFormat = compressionFormat;
927 video_def->eColorFormat = OMX_COLOR_FormatUnused;
928
Andreas Huber784202e2009-10-15 13:46:54 -0700929 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -0700930 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
931 CHECK_EQ(err, OK);
932
James Dongb00e2462010-04-26 17:48:26 -0700933 /////////////////// Codec-specific ////////////////////////
Andreas Huberb482ce82009-10-29 12:02:48 -0700934 switch (compressionFormat) {
935 case OMX_VIDEO_CodingMPEG4:
936 {
James Dong1244eab2010-06-08 11:58:53 -0700937 CHECK_EQ(setupMPEG4EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700938 break;
939 }
940
941 case OMX_VIDEO_CodingH263:
James Dongc0ab2a62010-06-29 16:29:19 -0700942 CHECK_EQ(setupH263EncoderParameters(meta), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -0700943 break;
944
Andreas Huberea6a38c2009-11-16 15:43:38 -0800945 case OMX_VIDEO_CodingAVC:
946 {
James Dong1244eab2010-06-08 11:58:53 -0700947 CHECK_EQ(setupAVCEncoderParameters(meta), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -0800948 break;
949 }
950
Andreas Huberb482ce82009-10-29 12:02:48 -0700951 default:
952 CHECK(!"Support for this compressionFormat to be implemented.");
953 break;
954 }
955}
956
James Dong1244eab2010-06-08 11:58:53 -0700957static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
958 if (iFramesInterval < 0) {
959 return 0xFFFFFFFF;
960 } else if (iFramesInterval == 0) {
961 return 0;
962 }
963 OMX_U32 ret = frameRate * iFramesInterval;
964 CHECK(ret > 1);
965 return ret;
966}
967
James Dongc0ab2a62010-06-29 16:29:19 -0700968status_t OMXCodec::setupErrorCorrectionParameters() {
969 OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
970 InitOMXParams(&errorCorrectionType);
971 errorCorrectionType.nPortIndex = kPortIndexOutput;
972
973 status_t err = mOMX->getParameter(
974 mNode, OMX_IndexParamVideoErrorCorrection,
975 &errorCorrectionType, sizeof(errorCorrectionType));
976 CHECK_EQ(err, OK);
977
978 errorCorrectionType.bEnableHEC = OMX_FALSE;
979 errorCorrectionType.bEnableResync = OMX_TRUE;
980 errorCorrectionType.nResynchMarkerSpacing = 256;
981 errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
982 errorCorrectionType.bEnableRVLC = OMX_FALSE;
983
984 err = mOMX->setParameter(
985 mNode, OMX_IndexParamVideoErrorCorrection,
986 &errorCorrectionType, sizeof(errorCorrectionType));
987 CHECK_EQ(err, OK);
988 return OK;
989}
990
991status_t OMXCodec::setupBitRate(int32_t bitRate) {
992 OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
993 InitOMXParams(&bitrateType);
994 bitrateType.nPortIndex = kPortIndexOutput;
995
996 status_t err = mOMX->getParameter(
997 mNode, OMX_IndexParamVideoBitrate,
998 &bitrateType, sizeof(bitrateType));
999 CHECK_EQ(err, OK);
1000
1001 bitrateType.eControlRate = OMX_Video_ControlRateVariable;
1002 bitrateType.nTargetBitrate = bitRate;
1003
1004 err = mOMX->setParameter(
1005 mNode, OMX_IndexParamVideoBitrate,
1006 &bitrateType, sizeof(bitrateType));
1007 CHECK_EQ(err, OK);
1008 return OK;
1009}
1010
James Dong81c929a2010-07-01 15:02:14 -07001011status_t OMXCodec::getVideoProfileLevel(
1012 const sp<MetaData>& meta,
1013 const CodecProfileLevel& defaultProfileLevel,
1014 CodecProfileLevel &profileLevel) {
1015 CODEC_LOGV("Default profile: %ld, level %ld",
1016 defaultProfileLevel.mProfile, defaultProfileLevel.mLevel);
1017
1018 // Are the default profile and level overwriten?
1019 int32_t profile, level;
1020 if (!meta->findInt32(kKeyVideoProfile, &profile)) {
1021 profile = defaultProfileLevel.mProfile;
1022 }
1023 if (!meta->findInt32(kKeyVideoLevel, &level)) {
1024 level = defaultProfileLevel.mLevel;
1025 }
1026 CODEC_LOGV("Target profile: %d, level: %d", profile, level);
1027
1028 // Are the target profile and level supported by the encoder?
1029 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
1030 InitOMXParams(&param);
1031 param.nPortIndex = kPortIndexOutput;
1032 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
1033 status_t err = mOMX->getParameter(
1034 mNode, OMX_IndexParamVideoProfileLevelQuerySupported,
1035 &param, sizeof(param));
1036
1037 if (err != OK) return err;
1038
1039 int32_t supportedProfile = static_cast<int32_t>(param.eProfile);
1040 int32_t supportedLevel = static_cast<int32_t>(param.eLevel);
James Dong929642e2010-07-08 11:16:11 -07001041 CODEC_LOGV("Supported profile: %d, level %d",
James Dong81c929a2010-07-01 15:02:14 -07001042 supportedProfile, supportedLevel);
1043
1044 if (profile == supportedProfile &&
1045 level == supportedLevel) {
1046 profileLevel.mProfile = profile;
1047 profileLevel.mLevel = level;
1048 return OK;
1049 }
1050 }
1051
1052 CODEC_LOGE("Target profile (%d) and level (%d) is not supported",
1053 profile, level);
1054 return BAD_VALUE;
1055}
1056
James Dongc0ab2a62010-06-29 16:29:19 -07001057status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) {
1058 int32_t iFramesInterval, frameRate, bitRate;
1059 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1060 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1061 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1062 CHECK(success);
1063 OMX_VIDEO_PARAM_H263TYPE h263type;
1064 InitOMXParams(&h263type);
1065 h263type.nPortIndex = kPortIndexOutput;
1066
1067 status_t err = mOMX->getParameter(
1068 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1069 CHECK_EQ(err, OK);
1070
1071 h263type.nAllowedPictureTypes =
1072 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1073
1074 h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1075 if (h263type.nPFrames == 0) {
1076 h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1077 }
1078 h263type.nBFrames = 0;
1079
James Dong81c929a2010-07-01 15:02:14 -07001080 // Check profile and level parameters
1081 CodecProfileLevel defaultProfileLevel, profileLevel;
1082 defaultProfileLevel.mProfile = OMX_VIDEO_H263ProfileBaseline;
1083 defaultProfileLevel.mLevel = OMX_VIDEO_H263Level45;
1084 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1085 if (err != OK) return err;
1086 h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile);
1087 h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel);
James Dongc0ab2a62010-06-29 16:29:19 -07001088
1089 h263type.bPLUSPTYPEAllowed = OMX_FALSE;
1090 h263type.bForceRoundingTypeToZero = OMX_FALSE;
1091 h263type.nPictureHeaderRepetition = 0;
1092 h263type.nGOBHeaderInterval = 0;
1093
1094 err = mOMX->setParameter(
1095 mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
1096 CHECK_EQ(err, OK);
1097
1098 CHECK_EQ(setupBitRate(bitRate), OK);
1099 CHECK_EQ(setupErrorCorrectionParameters(), OK);
1100
1101 return OK;
1102}
1103
James Dong1244eab2010-06-08 11:58:53 -07001104status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) {
1105 int32_t iFramesInterval, frameRate, bitRate;
1106 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1107 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1108 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1109 CHECK(success);
Andreas Huberb482ce82009-10-29 12:02:48 -07001110 OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
1111 InitOMXParams(&mpeg4type);
1112 mpeg4type.nPortIndex = kPortIndexOutput;
1113
1114 status_t err = mOMX->getParameter(
1115 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1116 CHECK_EQ(err, OK);
1117
1118 mpeg4type.nSliceHeaderSpacing = 0;
1119 mpeg4type.bSVH = OMX_FALSE;
1120 mpeg4type.bGov = OMX_FALSE;
1121
1122 mpeg4type.nAllowedPictureTypes =
1123 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1124
James Dong1244eab2010-06-08 11:58:53 -07001125 mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1126 if (mpeg4type.nPFrames == 0) {
1127 mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1128 }
Andreas Huberb482ce82009-10-29 12:02:48 -07001129 mpeg4type.nBFrames = 0;
Andreas Huberb482ce82009-10-29 12:02:48 -07001130 mpeg4type.nIDCVLCThreshold = 0;
1131 mpeg4type.bACPred = OMX_TRUE;
1132 mpeg4type.nMaxPacketSize = 256;
1133 mpeg4type.nTimeIncRes = 1000;
1134 mpeg4type.nHeaderExtension = 0;
1135 mpeg4type.bReversibleVLC = OMX_FALSE;
1136
James Dong81c929a2010-07-01 15:02:14 -07001137 // Check profile and level parameters
1138 CodecProfileLevel defaultProfileLevel, profileLevel;
1139 defaultProfileLevel.mProfile = OMX_VIDEO_MPEG4ProfileSimple;
1140 defaultProfileLevel.mLevel = OMX_VIDEO_MPEG4Level2;
1141 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1142 if (err != OK) return err;
1143 mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile);
1144 mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel);
Andreas Huberb482ce82009-10-29 12:02:48 -07001145
1146 err = mOMX->setParameter(
1147 mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
1148 CHECK_EQ(err, OK);
1149
James Dongc0ab2a62010-06-29 16:29:19 -07001150 CHECK_EQ(setupBitRate(bitRate), OK);
1151 CHECK_EQ(setupErrorCorrectionParameters(), OK);
Andreas Huberb482ce82009-10-29 12:02:48 -07001152
1153 return OK;
Andreas Huberbe06d262009-08-14 14:37:10 -07001154}
1155
James Dong1244eab2010-06-08 11:58:53 -07001156status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) {
1157 int32_t iFramesInterval, frameRate, bitRate;
1158 bool success = meta->findInt32(kKeyBitRate, &bitRate);
1159 success = success && meta->findInt32(kKeySampleRate, &frameRate);
1160 success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval);
1161 CHECK(success);
1162
Andreas Huberea6a38c2009-11-16 15:43:38 -08001163 OMX_VIDEO_PARAM_AVCTYPE h264type;
1164 InitOMXParams(&h264type);
1165 h264type.nPortIndex = kPortIndexOutput;
1166
1167 status_t err = mOMX->getParameter(
1168 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1169 CHECK_EQ(err, OK);
1170
1171 h264type.nAllowedPictureTypes =
1172 OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
1173
1174 h264type.nSliceHeaderSpacing = 0;
James Dong1244eab2010-06-08 11:58:53 -07001175 h264type.nBFrames = 0; // No B frames support yet
1176 h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate);
1177 if (h264type.nPFrames == 0) {
1178 h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
1179 }
James Dong81c929a2010-07-01 15:02:14 -07001180
1181 // Check profile and level parameters
1182 CodecProfileLevel defaultProfileLevel, profileLevel;
1183 defaultProfileLevel.mProfile = h264type.eProfile;
1184 defaultProfileLevel.mLevel = h264type.eLevel;
1185 err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel);
1186 if (err != OK) return err;
1187 h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile);
1188 h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel);
1189
1190 if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
1191 h264type.bUseHadamard = OMX_TRUE;
1192 h264type.nRefFrames = 1;
1193 h264type.nRefIdx10ActiveMinus1 = 0;
1194 h264type.nRefIdx11ActiveMinus1 = 0;
1195 h264type.bEntropyCodingCABAC = OMX_FALSE;
1196 h264type.bWeightedPPrediction = OMX_FALSE;
1197 h264type.bconstIpred = OMX_FALSE;
1198 h264type.bDirect8x8Inference = OMX_FALSE;
1199 h264type.bDirectSpatialTemporal = OMX_FALSE;
1200 h264type.nCabacInitIdc = 0;
1201 }
1202
1203 if (h264type.nBFrames != 0) {
1204 h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
1205 }
1206
Andreas Huberea6a38c2009-11-16 15:43:38 -08001207 h264type.bEnableUEP = OMX_FALSE;
1208 h264type.bEnableFMO = OMX_FALSE;
1209 h264type.bEnableASO = OMX_FALSE;
1210 h264type.bEnableRS = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001211 h264type.bFrameMBsOnly = OMX_TRUE;
1212 h264type.bMBAFF = OMX_FALSE;
Andreas Huberea6a38c2009-11-16 15:43:38 -08001213 h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
1214
pgudadhe9c305322010-07-26 13:59:29 -07001215 if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) {
1216 h264type.eLevel = OMX_VIDEO_AVCLevelMax;
1217 }
1218
Andreas Huberea6a38c2009-11-16 15:43:38 -08001219 err = mOMX->setParameter(
1220 mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
1221 CHECK_EQ(err, OK);
1222
James Dongc0ab2a62010-06-29 16:29:19 -07001223 CHECK_EQ(setupBitRate(bitRate), OK);
Andreas Huberea6a38c2009-11-16 15:43:38 -08001224
1225 return OK;
1226}
1227
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001228status_t OMXCodec::setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -07001229 const char *mime, OMX_U32 width, OMX_U32 height) {
Andreas Huber53a76bd2009-10-06 16:20:44 -07001230 CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07001231
Andreas Huberbe06d262009-08-14 14:37:10 -07001232 OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
Andreas Hubere6c40962009-09-10 14:13:30 -07001233 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001234 compressionFormat = OMX_VIDEO_CodingAVC;
Andreas Hubere6c40962009-09-10 14:13:30 -07001235 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001236 compressionFormat = OMX_VIDEO_CodingMPEG4;
Andreas Hubere6c40962009-09-10 14:13:30 -07001237 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001238 compressionFormat = OMX_VIDEO_CodingH263;
1239 } else {
1240 LOGE("Not a supported video mime type: %s", mime);
1241 CHECK(!"Should not be here. Not a supported video mime type.");
1242 }
1243
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001244 status_t err = setVideoPortFormatType(
Andreas Huberbe06d262009-08-14 14:37:10 -07001245 kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1246
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001247 if (err != OK) {
1248 return err;
1249 }
1250
Andreas Huberbe06d262009-08-14 14:37:10 -07001251#if 1
1252 {
1253 OMX_VIDEO_PARAM_PORTFORMATTYPE format;
Andreas Huber4c483422009-09-02 16:05:36 -07001254 InitOMXParams(&format);
Andreas Huberbe06d262009-08-14 14:37:10 -07001255 format.nPortIndex = kPortIndexOutput;
1256 format.nIndex = 0;
1257
Andreas Huber784202e2009-10-15 13:46:54 -07001258 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001259 mNode, OMX_IndexParamVideoPortFormat,
1260 &format, sizeof(format));
1261 CHECK_EQ(err, OK);
1262 CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
1263
1264 static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
1265
1266 CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
1267 || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
1268 || format.eColorFormat == OMX_COLOR_FormatCbYCrY
1269 || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
1270
Andreas Huber784202e2009-10-15 13:46:54 -07001271 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001272 mNode, OMX_IndexParamVideoPortFormat,
1273 &format, sizeof(format));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001274
1275 if (err != OK) {
1276 return err;
1277 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001278 }
1279#endif
1280
1281 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001282 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001283 def.nPortIndex = kPortIndexInput;
1284
Andreas Huber4c483422009-09-02 16:05:36 -07001285 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
1286
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001287 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001288 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1289
1290 CHECK_EQ(err, OK);
1291
1292#if 1
1293 // XXX Need a (much) better heuristic to compute input buffer sizes.
1294 const size_t X = 64 * 1024;
1295 if (def.nBufferSize < X) {
1296 def.nBufferSize = X;
1297 }
1298#endif
1299
1300 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1301
1302 video_def->nFrameWidth = width;
1303 video_def->nFrameHeight = height;
1304
Andreas Huberb482ce82009-10-29 12:02:48 -07001305 video_def->eCompressionFormat = compressionFormat;
Andreas Huberbe06d262009-08-14 14:37:10 -07001306 video_def->eColorFormat = OMX_COLOR_FormatUnused;
1307
Andreas Huber784202e2009-10-15 13:46:54 -07001308 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001309 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001310
1311 if (err != OK) {
1312 return err;
1313 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001314
1315 ////////////////////////////////////////////////////////////////////////////
1316
Andreas Huber4c483422009-09-02 16:05:36 -07001317 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001318 def.nPortIndex = kPortIndexOutput;
1319
Andreas Huber784202e2009-10-15 13:46:54 -07001320 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001321 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1322 CHECK_EQ(err, OK);
1323 CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
1324
1325#if 0
1326 def.nBufferSize =
1327 (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420
1328#endif
1329
1330 video_def->nFrameWidth = width;
1331 video_def->nFrameHeight = height;
1332
Andreas Huber784202e2009-10-15 13:46:54 -07001333 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001334 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
Andreas Huber2a09c7e2010-03-16 11:44:07 -07001335
1336 return err;
Andreas Huberbe06d262009-08-14 14:37:10 -07001337}
1338
Andreas Huberbe06d262009-08-14 14:37:10 -07001339OMXCodec::OMXCodec(
1340 const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
Andreas Huberebf66ea2009-08-19 13:32:58 -07001341 bool isEncoder,
Andreas Huberbe06d262009-08-14 14:37:10 -07001342 const char *mime,
1343 const char *componentName,
1344 const sp<MediaSource> &source)
1345 : mOMX(omx),
Andreas Huberf1fe0642010-01-15 15:28:19 -08001346 mOMXLivesLocally(omx->livesLocally(getpid())),
Andreas Huberbe06d262009-08-14 14:37:10 -07001347 mNode(node),
1348 mQuirks(quirks),
1349 mIsEncoder(isEncoder),
1350 mMIME(strdup(mime)),
1351 mComponentName(strdup(componentName)),
1352 mSource(source),
1353 mCodecSpecificDataIndex(0),
Andreas Huberbe06d262009-08-14 14:37:10 -07001354 mState(LOADED),
Andreas Huber42978e52009-08-27 10:08:39 -07001355 mInitialBufferSubmit(true),
Andreas Huberbe06d262009-08-14 14:37:10 -07001356 mSignalledEOS(false),
1357 mNoMoreOutputData(false),
Andreas Hubercfd55572009-10-09 14:11:28 -07001358 mOutputPortSettingsHaveChanged(false),
Andreas Hubera4357ad2010-04-02 12:49:54 -07001359 mSeekTimeUs(-1),
Andreas Huber6624c9f2010-07-20 15:04:28 -07001360 mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC),
1361 mTargetTimeUs(-1),
James Dong53d4e0d2010-07-21 14:51:35 -07001362 mSkipTimeUs(-1),
Andreas Huber1f24b302010-06-10 11:12:39 -07001363 mLeftOverBuffer(NULL),
1364 mPaused(false) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001365 mPortStatus[kPortIndexInput] = ENABLED;
1366 mPortStatus[kPortIndexOutput] = ENABLED;
1367
Andreas Huber4c483422009-09-02 16:05:36 -07001368 setComponentRole();
1369}
1370
Andreas Hubere6c40962009-09-10 14:13:30 -07001371// static
1372void OMXCodec::setComponentRole(
1373 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
1374 const char *mime) {
Andreas Huber4c483422009-09-02 16:05:36 -07001375 struct MimeToRole {
1376 const char *mime;
1377 const char *decoderRole;
1378 const char *encoderRole;
1379 };
1380
1381 static const MimeToRole kMimeToRole[] = {
Andreas Hubere6c40962009-09-10 14:13:30 -07001382 { MEDIA_MIMETYPE_AUDIO_MPEG,
1383 "audio_decoder.mp3", "audio_encoder.mp3" },
1384 { MEDIA_MIMETYPE_AUDIO_AMR_NB,
1385 "audio_decoder.amrnb", "audio_encoder.amrnb" },
1386 { MEDIA_MIMETYPE_AUDIO_AMR_WB,
1387 "audio_decoder.amrwb", "audio_encoder.amrwb" },
1388 { MEDIA_MIMETYPE_AUDIO_AAC,
1389 "audio_decoder.aac", "audio_encoder.aac" },
1390 { MEDIA_MIMETYPE_VIDEO_AVC,
1391 "video_decoder.avc", "video_encoder.avc" },
1392 { MEDIA_MIMETYPE_VIDEO_MPEG4,
1393 "video_decoder.mpeg4", "video_encoder.mpeg4" },
1394 { MEDIA_MIMETYPE_VIDEO_H263,
1395 "video_decoder.h263", "video_encoder.h263" },
Andreas Huber4c483422009-09-02 16:05:36 -07001396 };
1397
1398 static const size_t kNumMimeToRole =
1399 sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
1400
1401 size_t i;
1402 for (i = 0; i < kNumMimeToRole; ++i) {
Andreas Hubere6c40962009-09-10 14:13:30 -07001403 if (!strcasecmp(mime, kMimeToRole[i].mime)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001404 break;
1405 }
1406 }
1407
1408 if (i == kNumMimeToRole) {
1409 return;
1410 }
1411
1412 const char *role =
Andreas Hubere6c40962009-09-10 14:13:30 -07001413 isEncoder ? kMimeToRole[i].encoderRole
1414 : kMimeToRole[i].decoderRole;
Andreas Huber4c483422009-09-02 16:05:36 -07001415
1416 if (role != NULL) {
Andreas Huber4c483422009-09-02 16:05:36 -07001417 OMX_PARAM_COMPONENTROLETYPE roleParams;
1418 InitOMXParams(&roleParams);
1419
1420 strncpy((char *)roleParams.cRole,
1421 role, OMX_MAX_STRINGNAME_SIZE - 1);
1422
1423 roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
1424
Andreas Huber784202e2009-10-15 13:46:54 -07001425 status_t err = omx->setParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07001426 node, OMX_IndexParamStandardComponentRole,
Andreas Huber4c483422009-09-02 16:05:36 -07001427 &roleParams, sizeof(roleParams));
1428
1429 if (err != OK) {
1430 LOGW("Failed to set standard component role '%s'.", role);
1431 }
1432 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001433}
1434
Andreas Hubere6c40962009-09-10 14:13:30 -07001435void OMXCodec::setComponentRole() {
1436 setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
1437}
1438
Andreas Huberbe06d262009-08-14 14:37:10 -07001439OMXCodec::~OMXCodec() {
Andreas Huber4f5e6022009-08-19 09:29:34 -07001440 CHECK(mState == LOADED || mState == ERROR);
Andreas Huberbe06d262009-08-14 14:37:10 -07001441
Andreas Huber784202e2009-10-15 13:46:54 -07001442 status_t err = mOMX->freeNode(mNode);
Andreas Huberbe06d262009-08-14 14:37:10 -07001443 CHECK_EQ(err, OK);
1444
1445 mNode = NULL;
1446 setState(DEAD);
1447
1448 clearCodecSpecificData();
1449
1450 free(mComponentName);
1451 mComponentName = NULL;
Andreas Huberebf66ea2009-08-19 13:32:58 -07001452
Andreas Huberbe06d262009-08-14 14:37:10 -07001453 free(mMIME);
1454 mMIME = NULL;
1455}
1456
1457status_t OMXCodec::init() {
Andreas Huber42978e52009-08-27 10:08:39 -07001458 // mLock is held.
Andreas Huberbe06d262009-08-14 14:37:10 -07001459
1460 CHECK_EQ(mState, LOADED);
1461
1462 status_t err;
1463 if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
Andreas Huber784202e2009-10-15 13:46:54 -07001464 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001465 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07001466 setState(LOADED_TO_IDLE);
1467 }
1468
1469 err = allocateBuffers();
1470 CHECK_EQ(err, OK);
1471
1472 if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
Andreas Huber784202e2009-10-15 13:46:54 -07001473 err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huberbe06d262009-08-14 14:37:10 -07001474 CHECK_EQ(err, OK);
1475
1476 setState(LOADED_TO_IDLE);
1477 }
1478
1479 while (mState != EXECUTING && mState != ERROR) {
1480 mAsyncCompletion.wait(mLock);
1481 }
1482
1483 return mState == ERROR ? UNKNOWN_ERROR : OK;
1484}
1485
1486// static
1487bool OMXCodec::isIntermediateState(State state) {
1488 return state == LOADED_TO_IDLE
1489 || state == IDLE_TO_EXECUTING
1490 || state == EXECUTING_TO_IDLE
1491 || state == IDLE_TO_LOADED
1492 || state == RECONFIGURING;
1493}
1494
1495status_t OMXCodec::allocateBuffers() {
1496 status_t err = allocateBuffersOnPort(kPortIndexInput);
1497
1498 if (err != OK) {
1499 return err;
1500 }
1501
1502 return allocateBuffersOnPort(kPortIndexOutput);
1503}
1504
1505status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
1506 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07001507 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07001508 def.nPortIndex = portIndex;
1509
Andreas Huber784202e2009-10-15 13:46:54 -07001510 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07001511 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1512
1513 if (err != OK) {
1514 return err;
1515 }
1516
Andreas Huber57648e42010-08-04 10:14:30 -07001517 CODEC_LOGI("allocating %lu buffers of size %lu on %s port",
1518 def.nBufferCountActual, def.nBufferSize,
1519 portIndex == kPortIndexInput ? "input" : "output");
1520
Andreas Huber5c0a9132009-08-20 11:16:40 -07001521 size_t totalSize = def.nBufferCountActual * def.nBufferSize;
Mathias Agopian6faf7892010-01-25 19:00:00 -08001522 mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec");
Andreas Huber5c0a9132009-08-20 11:16:40 -07001523
Andreas Huberbe06d262009-08-14 14:37:10 -07001524 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
Andreas Huber5c0a9132009-08-20 11:16:40 -07001525 sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07001526 CHECK(mem.get() != NULL);
1527
Andreas Huberc712b9f2010-01-20 15:05:46 -08001528 BufferInfo info;
1529 info.mData = NULL;
1530 info.mSize = def.nBufferSize;
1531
Andreas Huberbe06d262009-08-14 14:37:10 -07001532 IOMX::buffer_id buffer;
1533 if (portIndex == kPortIndexInput
1534 && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001535 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001536 mem.clear();
1537
Andreas Huberf1fe0642010-01-15 15:28:19 -08001538 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001539 mNode, portIndex, def.nBufferSize, &buffer,
1540 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001541 } else {
1542 err = mOMX->allocateBufferWithBackup(
1543 mNode, portIndex, mem, &buffer);
1544 }
Andreas Huber446f44f2009-08-25 17:23:44 -07001545 } else if (portIndex == kPortIndexOutput
1546 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
Andreas Huberf1fe0642010-01-15 15:28:19 -08001547 if (mOMXLivesLocally) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08001548 mem.clear();
1549
Andreas Huberf1fe0642010-01-15 15:28:19 -08001550 err = mOMX->allocateBuffer(
Andreas Huberc712b9f2010-01-20 15:05:46 -08001551 mNode, portIndex, def.nBufferSize, &buffer,
1552 &info.mData);
Andreas Huberf1fe0642010-01-15 15:28:19 -08001553 } else {
1554 err = mOMX->allocateBufferWithBackup(
1555 mNode, portIndex, mem, &buffer);
1556 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001557 } else {
Andreas Huber784202e2009-10-15 13:46:54 -07001558 err = mOMX->useBuffer(mNode, portIndex, mem, &buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001559 }
1560
1561 if (err != OK) {
1562 LOGE("allocate_buffer_with_backup failed");
1563 return err;
1564 }
1565
Andreas Huberc712b9f2010-01-20 15:05:46 -08001566 if (mem != NULL) {
1567 info.mData = mem->pointer();
1568 }
1569
Andreas Huberbe06d262009-08-14 14:37:10 -07001570 info.mBuffer = buffer;
1571 info.mOwnedByComponent = false;
1572 info.mMem = mem;
1573 info.mMediaBuffer = NULL;
1574
1575 if (portIndex == kPortIndexOutput) {
Andreas Huber52733b82010-01-25 10:41:35 -08001576 if (!(mOMXLivesLocally
1577 && (mQuirks & kRequiresAllocateBufferOnOutputPorts)
1578 && (mQuirks & kDefersOutputBufferAllocation))) {
1579 // If the node does not fill in the buffer ptr at this time,
1580 // we will defer creating the MediaBuffer until receiving
1581 // the first FILL_BUFFER_DONE notification instead.
1582 info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize);
1583 info.mMediaBuffer->setObserver(this);
1584 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001585 }
1586
1587 mPortBuffers[portIndex].push(info);
1588
Andreas Huber4c483422009-09-02 16:05:36 -07001589 CODEC_LOGV("allocated buffer %p on %s port", buffer,
Andreas Huberbe06d262009-08-14 14:37:10 -07001590 portIndex == kPortIndexInput ? "input" : "output");
1591 }
1592
Andreas Huber2ea14e22009-12-16 09:30:55 -08001593 // dumpPortStatus(portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001594
1595 return OK;
1596}
1597
1598void OMXCodec::on_message(const omx_message &msg) {
1599 Mutex::Autolock autoLock(mLock);
1600
1601 switch (msg.type) {
1602 case omx_message::EVENT:
1603 {
1604 onEvent(
1605 msg.u.event_data.event, msg.u.event_data.data1,
1606 msg.u.event_data.data2);
1607
1608 break;
1609 }
1610
1611 case omx_message::EMPTY_BUFFER_DONE:
1612 {
1613 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1614
Andreas Huber4c483422009-09-02 16:05:36 -07001615 CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001616
1617 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1618 size_t i = 0;
1619 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1620 ++i;
1621 }
1622
1623 CHECK(i < buffers->size());
1624 if (!(*buffers)[i].mOwnedByComponent) {
1625 LOGW("We already own input buffer %p, yet received "
1626 "an EMPTY_BUFFER_DONE.", buffer);
1627 }
1628
1629 buffers->editItemAt(i).mOwnedByComponent = false;
1630
1631 if (mPortStatus[kPortIndexInput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001632 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001633
1634 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001635 mOMX->freeBuffer(mNode, kPortIndexInput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001636 CHECK_EQ(err, OK);
1637
1638 buffers->removeAt(i);
Andreas Huber4a9375e2010-02-09 11:54:33 -08001639 } else if (mState != ERROR
1640 && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
Andreas Huberbe06d262009-08-14 14:37:10 -07001641 CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
1642 drainInputBuffer(&buffers->editItemAt(i));
1643 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001644 break;
1645 }
1646
1647 case omx_message::FILL_BUFFER_DONE:
1648 {
1649 IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
1650 OMX_U32 flags = msg.u.extended_buffer_data.flags;
1651
Andreas Huber2ea14e22009-12-16 09:30:55 -08001652 CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))",
Andreas Huberbe06d262009-08-14 14:37:10 -07001653 buffer,
1654 msg.u.extended_buffer_data.range_length,
Andreas Huber2ea14e22009-12-16 09:30:55 -08001655 flags,
Andreas Huberbe06d262009-08-14 14:37:10 -07001656 msg.u.extended_buffer_data.timestamp,
1657 msg.u.extended_buffer_data.timestamp / 1E6);
1658
1659 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1660 size_t i = 0;
1661 while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
1662 ++i;
1663 }
1664
1665 CHECK(i < buffers->size());
1666 BufferInfo *info = &buffers->editItemAt(i);
1667
1668 if (!info->mOwnedByComponent) {
1669 LOGW("We already own output buffer %p, yet received "
1670 "a FILL_BUFFER_DONE.", buffer);
1671 }
1672
1673 info->mOwnedByComponent = false;
1674
1675 if (mPortStatus[kPortIndexOutput] == DISABLING) {
Andreas Huber4c483422009-09-02 16:05:36 -07001676 CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001677
1678 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001679 mOMX->freeBuffer(mNode, kPortIndexOutput, buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07001680 CHECK_EQ(err, OK);
1681
1682 buffers->removeAt(i);
Andreas Huber2ea14e22009-12-16 09:30:55 -08001683#if 0
Andreas Huberd7795892009-08-26 10:33:47 -07001684 } else if (mPortStatus[kPortIndexOutput] == ENABLED
1685 && (flags & OMX_BUFFERFLAG_EOS)) {
Andreas Huber4c483422009-09-02 16:05:36 -07001686 CODEC_LOGV("No more output data.");
Andreas Huberbe06d262009-08-14 14:37:10 -07001687 mNoMoreOutputData = true;
1688 mBufferFilled.signal();
Andreas Huber2ea14e22009-12-16 09:30:55 -08001689#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001690 } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
1691 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
Andreas Huberebf66ea2009-08-19 13:32:58 -07001692
Andreas Huber52733b82010-01-25 10:41:35 -08001693 if (info->mMediaBuffer == NULL) {
1694 CHECK(mOMXLivesLocally);
1695 CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts);
1696 CHECK(mQuirks & kDefersOutputBufferAllocation);
1697
1698 // The qcom video decoders on Nexus don't actually allocate
1699 // output buffer memory on a call to OMX_AllocateBuffer
1700 // the "pBuffer" member of the OMX_BUFFERHEADERTYPE
1701 // structure is only filled in later.
1702
1703 info->mMediaBuffer = new MediaBuffer(
1704 msg.u.extended_buffer_data.data_ptr,
1705 info->mSize);
1706 info->mMediaBuffer->setObserver(this);
1707 }
1708
Andreas Huberbe06d262009-08-14 14:37:10 -07001709 MediaBuffer *buffer = info->mMediaBuffer;
1710
Andreas Huberf88f8442010-08-10 11:18:36 -07001711 if (msg.u.extended_buffer_data.range_offset
1712 + msg.u.extended_buffer_data.range_length
1713 > buffer->size()) {
1714 CODEC_LOGE(
1715 "Codec lied about its buffer size requirements, "
1716 "sending a buffer larger than the originally "
1717 "advertised size in FILL_BUFFER_DONE!");
1718 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001719 buffer->set_range(
1720 msg.u.extended_buffer_data.range_offset,
1721 msg.u.extended_buffer_data.range_length);
1722
1723 buffer->meta_data()->clear();
1724
Andreas Huberfa8de752009-10-08 10:07:49 -07001725 buffer->meta_data()->setInt64(
1726 kKeyTime, msg.u.extended_buffer_data.timestamp);
Andreas Huberbe06d262009-08-14 14:37:10 -07001727
1728 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
1729 buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
1730 }
Andreas Huberea6a38c2009-11-16 15:43:38 -08001731 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) {
1732 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
1733 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001734
1735 buffer->meta_data()->setPointer(
1736 kKeyPlatformPrivate,
1737 msg.u.extended_buffer_data.platform_private);
1738
1739 buffer->meta_data()->setPointer(
1740 kKeyBufferID,
1741 msg.u.extended_buffer_data.buffer);
1742
Andreas Huber2ea14e22009-12-16 09:30:55 -08001743 if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) {
1744 CODEC_LOGV("No more output data.");
1745 mNoMoreOutputData = true;
1746 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07001747
1748 if (mTargetTimeUs >= 0) {
1749 CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs);
1750
1751 if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) {
1752 CODEC_LOGV(
1753 "skipping output buffer at timestamp %lld us",
1754 msg.u.extended_buffer_data.timestamp);
1755
1756 fillOutputBuffer(info);
1757 break;
1758 }
1759
1760 CODEC_LOGV(
1761 "returning output buffer at target timestamp "
1762 "%lld us",
1763 msg.u.extended_buffer_data.timestamp);
1764
1765 mTargetTimeUs = -1;
1766 }
1767
1768 mFilledBuffers.push_back(i);
1769 mBufferFilled.signal();
Andreas Huberbe06d262009-08-14 14:37:10 -07001770 }
1771
1772 break;
1773 }
1774
1775 default:
1776 {
1777 CHECK(!"should not be here.");
1778 break;
1779 }
1780 }
1781}
1782
1783void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1784 switch (event) {
1785 case OMX_EventCmdComplete:
1786 {
1787 onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1788 break;
1789 }
1790
1791 case OMX_EventError:
1792 {
Andreas Huber2ea14e22009-12-16 09:30:55 -08001793 LOGE("ERROR(0x%08lx, %ld)", data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001794
1795 setState(ERROR);
1796 break;
1797 }
1798
1799 case OMX_EventPortSettingsChanged:
1800 {
1801 onPortSettingsChanged(data1);
1802 break;
1803 }
1804
Andreas Huber2ea14e22009-12-16 09:30:55 -08001805#if 0
Andreas Huberbe06d262009-08-14 14:37:10 -07001806 case OMX_EventBufferFlag:
1807 {
Andreas Huber4c483422009-09-02 16:05:36 -07001808 CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
Andreas Huberbe06d262009-08-14 14:37:10 -07001809
1810 if (data1 == kPortIndexOutput) {
1811 mNoMoreOutputData = true;
1812 }
1813 break;
1814 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08001815#endif
Andreas Huberbe06d262009-08-14 14:37:10 -07001816
1817 default:
1818 {
Andreas Huber4c483422009-09-02 16:05:36 -07001819 CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
Andreas Huberbe06d262009-08-14 14:37:10 -07001820 break;
1821 }
1822 }
1823}
1824
Andreas Huberb1678602009-10-19 13:06:40 -07001825// Has the format changed in any way that the client would have to be aware of?
1826static bool formatHasNotablyChanged(
1827 const sp<MetaData> &from, const sp<MetaData> &to) {
1828 if (from.get() == NULL && to.get() == NULL) {
1829 return false;
1830 }
1831
Andreas Huberf68c1682009-10-21 14:01:30 -07001832 if ((from.get() == NULL && to.get() != NULL)
1833 || (from.get() != NULL && to.get() == NULL)) {
Andreas Huberb1678602009-10-19 13:06:40 -07001834 return true;
1835 }
1836
1837 const char *mime_from, *mime_to;
1838 CHECK(from->findCString(kKeyMIMEType, &mime_from));
1839 CHECK(to->findCString(kKeyMIMEType, &mime_to));
1840
1841 if (strcasecmp(mime_from, mime_to)) {
1842 return true;
1843 }
1844
1845 if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) {
1846 int32_t colorFormat_from, colorFormat_to;
1847 CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from));
1848 CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to));
1849
1850 if (colorFormat_from != colorFormat_to) {
1851 return true;
1852 }
1853
1854 int32_t width_from, width_to;
1855 CHECK(from->findInt32(kKeyWidth, &width_from));
1856 CHECK(to->findInt32(kKeyWidth, &width_to));
1857
1858 if (width_from != width_to) {
1859 return true;
1860 }
1861
1862 int32_t height_from, height_to;
1863 CHECK(from->findInt32(kKeyHeight, &height_from));
1864 CHECK(to->findInt32(kKeyHeight, &height_to));
1865
1866 if (height_from != height_to) {
1867 return true;
1868 }
1869 } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) {
1870 int32_t numChannels_from, numChannels_to;
1871 CHECK(from->findInt32(kKeyChannelCount, &numChannels_from));
1872 CHECK(to->findInt32(kKeyChannelCount, &numChannels_to));
1873
1874 if (numChannels_from != numChannels_to) {
1875 return true;
1876 }
1877
1878 int32_t sampleRate_from, sampleRate_to;
1879 CHECK(from->findInt32(kKeySampleRate, &sampleRate_from));
1880 CHECK(to->findInt32(kKeySampleRate, &sampleRate_to));
1881
1882 if (sampleRate_from != sampleRate_to) {
1883 return true;
1884 }
1885 }
1886
1887 return false;
1888}
1889
Andreas Huberbe06d262009-08-14 14:37:10 -07001890void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1891 switch (cmd) {
1892 case OMX_CommandStateSet:
1893 {
1894 onStateChange((OMX_STATETYPE)data);
1895 break;
1896 }
1897
1898 case OMX_CommandPortDisable:
1899 {
1900 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001901 CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001902
1903 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1904 CHECK_EQ(mPortStatus[portIndex], DISABLING);
1905 CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1906
1907 mPortStatus[portIndex] = DISABLED;
1908
1909 if (mState == RECONFIGURING) {
1910 CHECK_EQ(portIndex, kPortIndexOutput);
1911
Andreas Huberb1678602009-10-19 13:06:40 -07001912 sp<MetaData> oldOutputFormat = mOutputFormat;
Andreas Hubercfd55572009-10-09 14:11:28 -07001913 initOutputFormat(mSource->getFormat());
Andreas Huberb1678602009-10-19 13:06:40 -07001914
1915 // Don't notify clients if the output port settings change
1916 // wasn't of importance to them, i.e. it may be that just the
1917 // number of buffers has changed and nothing else.
1918 mOutputPortSettingsHaveChanged =
1919 formatHasNotablyChanged(oldOutputFormat, mOutputFormat);
Andreas Hubercfd55572009-10-09 14:11:28 -07001920
Andreas Huberbe06d262009-08-14 14:37:10 -07001921 enablePortAsync(portIndex);
1922
1923 status_t err = allocateBuffersOnPort(portIndex);
1924 CHECK_EQ(err, OK);
1925 }
1926 break;
1927 }
1928
1929 case OMX_CommandPortEnable:
1930 {
1931 OMX_U32 portIndex = data;
Andreas Huber4c483422009-09-02 16:05:36 -07001932 CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001933
1934 CHECK(mState == EXECUTING || mState == RECONFIGURING);
1935 CHECK_EQ(mPortStatus[portIndex], ENABLING);
1936
1937 mPortStatus[portIndex] = ENABLED;
1938
1939 if (mState == RECONFIGURING) {
1940 CHECK_EQ(portIndex, kPortIndexOutput);
1941
1942 setState(EXECUTING);
1943
1944 fillOutputBuffers();
1945 }
1946 break;
1947 }
1948
1949 case OMX_CommandFlush:
1950 {
1951 OMX_U32 portIndex = data;
1952
Andreas Huber4c483422009-09-02 16:05:36 -07001953 CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07001954
1955 CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1956 mPortStatus[portIndex] = ENABLED;
1957
1958 CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1959 mPortBuffers[portIndex].size());
1960
1961 if (mState == RECONFIGURING) {
1962 CHECK_EQ(portIndex, kPortIndexOutput);
1963
1964 disablePortAsync(portIndex);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001965 } else if (mState == EXECUTING_TO_IDLE) {
1966 if (mPortStatus[kPortIndexInput] == ENABLED
1967 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001968 CODEC_LOGV("Finished flushing both ports, now completing "
Andreas Huber127fcdc2009-08-26 16:27:02 -07001969 "transition from EXECUTING to IDLE.");
1970
1971 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1972 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1973
1974 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07001975 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07001976 CHECK_EQ(err, OK);
1977 }
Andreas Huberbe06d262009-08-14 14:37:10 -07001978 } else {
1979 // We're flushing both ports in preparation for seeking.
1980
1981 if (mPortStatus[kPortIndexInput] == ENABLED
1982 && mPortStatus[kPortIndexOutput] == ENABLED) {
Andreas Huber4c483422009-09-02 16:05:36 -07001983 CODEC_LOGV("Finished flushing both ports, now continuing from"
Andreas Huberbe06d262009-08-14 14:37:10 -07001984 " seek-time.");
1985
Andreas Huber1f24b302010-06-10 11:12:39 -07001986 // We implicitly resume pulling on our upstream source.
1987 mPaused = false;
1988
Andreas Huberbe06d262009-08-14 14:37:10 -07001989 drainInputBuffers();
1990 fillOutputBuffers();
1991 }
1992 }
1993
1994 break;
1995 }
1996
1997 default:
1998 {
Andreas Huber4c483422009-09-02 16:05:36 -07001999 CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
Andreas Huberbe06d262009-08-14 14:37:10 -07002000 break;
2001 }
2002 }
2003}
2004
2005void OMXCodec::onStateChange(OMX_STATETYPE newState) {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002006 CODEC_LOGV("onStateChange %d", newState);
2007
Andreas Huberbe06d262009-08-14 14:37:10 -07002008 switch (newState) {
2009 case OMX_StateIdle:
2010 {
Andreas Huber4c483422009-09-02 16:05:36 -07002011 CODEC_LOGV("Now Idle.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002012 if (mState == LOADED_TO_IDLE) {
Andreas Huber784202e2009-10-15 13:46:54 -07002013 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002014 mNode, OMX_CommandStateSet, OMX_StateExecuting);
2015
2016 CHECK_EQ(err, OK);
2017
2018 setState(IDLE_TO_EXECUTING);
2019 } else {
2020 CHECK_EQ(mState, EXECUTING_TO_IDLE);
2021
2022 CHECK_EQ(
2023 countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
2024 mPortBuffers[kPortIndexInput].size());
2025
2026 CHECK_EQ(
2027 countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
2028 mPortBuffers[kPortIndexOutput].size());
2029
Andreas Huber784202e2009-10-15 13:46:54 -07002030 status_t err = mOMX->sendCommand(
Andreas Huberbe06d262009-08-14 14:37:10 -07002031 mNode, OMX_CommandStateSet, OMX_StateLoaded);
2032
2033 CHECK_EQ(err, OK);
2034
2035 err = freeBuffersOnPort(kPortIndexInput);
2036 CHECK_EQ(err, OK);
2037
2038 err = freeBuffersOnPort(kPortIndexOutput);
2039 CHECK_EQ(err, OK);
2040
2041 mPortStatus[kPortIndexInput] = ENABLED;
2042 mPortStatus[kPortIndexOutput] = ENABLED;
2043
2044 setState(IDLE_TO_LOADED);
2045 }
2046 break;
2047 }
2048
2049 case OMX_StateExecuting:
2050 {
2051 CHECK_EQ(mState, IDLE_TO_EXECUTING);
2052
Andreas Huber4c483422009-09-02 16:05:36 -07002053 CODEC_LOGV("Now Executing.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002054
2055 setState(EXECUTING);
2056
Andreas Huber42978e52009-08-27 10:08:39 -07002057 // Buffers will be submitted to the component in the first
2058 // call to OMXCodec::read as mInitialBufferSubmit is true at
2059 // this point. This ensures that this on_message call returns,
2060 // releases the lock and ::init can notice the state change and
2061 // itself return.
Andreas Huberbe06d262009-08-14 14:37:10 -07002062 break;
2063 }
2064
2065 case OMX_StateLoaded:
2066 {
2067 CHECK_EQ(mState, IDLE_TO_LOADED);
2068
Andreas Huber4c483422009-09-02 16:05:36 -07002069 CODEC_LOGV("Now Loaded.");
Andreas Huberbe06d262009-08-14 14:37:10 -07002070
2071 setState(LOADED);
2072 break;
2073 }
2074
Andreas Huberc712b9f2010-01-20 15:05:46 -08002075 case OMX_StateInvalid:
2076 {
2077 setState(ERROR);
2078 break;
2079 }
2080
Andreas Huberbe06d262009-08-14 14:37:10 -07002081 default:
2082 {
2083 CHECK(!"should not be here.");
2084 break;
2085 }
2086 }
2087}
2088
2089// static
2090size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
2091 size_t n = 0;
2092 for (size_t i = 0; i < buffers.size(); ++i) {
2093 if (!buffers[i].mOwnedByComponent) {
2094 ++n;
2095 }
2096 }
2097
2098 return n;
2099}
2100
2101status_t OMXCodec::freeBuffersOnPort(
2102 OMX_U32 portIndex, bool onlyThoseWeOwn) {
2103 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
2104
2105 status_t stickyErr = OK;
2106
2107 for (size_t i = buffers->size(); i-- > 0;) {
2108 BufferInfo *info = &buffers->editItemAt(i);
2109
2110 if (onlyThoseWeOwn && info->mOwnedByComponent) {
2111 continue;
2112 }
2113
2114 CHECK_EQ(info->mOwnedByComponent, false);
2115
Andreas Huber92022852009-09-14 15:24:14 -07002116 CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
2117
Andreas Huberbe06d262009-08-14 14:37:10 -07002118 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002119 mOMX->freeBuffer(mNode, portIndex, info->mBuffer);
Andreas Huberbe06d262009-08-14 14:37:10 -07002120
2121 if (err != OK) {
2122 stickyErr = err;
2123 }
2124
2125 if (info->mMediaBuffer != NULL) {
2126 info->mMediaBuffer->setObserver(NULL);
2127
2128 // Make sure nobody but us owns this buffer at this point.
2129 CHECK_EQ(info->mMediaBuffer->refcount(), 0);
2130
2131 info->mMediaBuffer->release();
2132 }
2133
2134 buffers->removeAt(i);
2135 }
2136
2137 CHECK(onlyThoseWeOwn || buffers->isEmpty());
2138
2139 return stickyErr;
2140}
2141
2142void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
Andreas Huber4c483422009-09-02 16:05:36 -07002143 CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002144
2145 CHECK_EQ(mState, EXECUTING);
2146 CHECK_EQ(portIndex, kPortIndexOutput);
2147 setState(RECONFIGURING);
2148
2149 if (mQuirks & kNeedsFlushBeforeDisable) {
Andreas Huber404cc412009-08-25 14:26:05 -07002150 if (!flushPortAsync(portIndex)) {
2151 onCmdComplete(OMX_CommandFlush, portIndex);
2152 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002153 } else {
2154 disablePortAsync(portIndex);
2155 }
2156}
2157
Andreas Huber404cc412009-08-25 14:26:05 -07002158bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
Andreas Huber127fcdc2009-08-26 16:27:02 -07002159 CHECK(mState == EXECUTING || mState == RECONFIGURING
2160 || mState == EXECUTING_TO_IDLE);
Andreas Huberbe06d262009-08-14 14:37:10 -07002161
Andreas Huber4c483422009-09-02 16:05:36 -07002162 CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
Andreas Huber404cc412009-08-25 14:26:05 -07002163 portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
2164 mPortBuffers[portIndex].size());
2165
Andreas Huberbe06d262009-08-14 14:37:10 -07002166 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2167 mPortStatus[portIndex] = SHUTTING_DOWN;
2168
Andreas Huber404cc412009-08-25 14:26:05 -07002169 if ((mQuirks & kRequiresFlushCompleteEmulation)
2170 && countBuffersWeOwn(mPortBuffers[portIndex])
2171 == mPortBuffers[portIndex].size()) {
2172 // No flush is necessary and this component fails to send a
2173 // flush-complete event in this case.
2174
2175 return false;
2176 }
2177
Andreas Huberbe06d262009-08-14 14:37:10 -07002178 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002179 mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002180 CHECK_EQ(err, OK);
Andreas Huber404cc412009-08-25 14:26:05 -07002181
2182 return true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002183}
2184
2185void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
2186 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2187
2188 CHECK_EQ(mPortStatus[portIndex], ENABLED);
2189 mPortStatus[portIndex] = DISABLING;
2190
Andreas Huberd222c842010-08-26 14:29:34 -07002191 CODEC_LOGV("sending OMX_CommandPortDisable(%ld)", portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002192 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002193 mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002194 CHECK_EQ(err, OK);
2195
2196 freeBuffersOnPort(portIndex, true);
2197}
2198
2199void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
2200 CHECK(mState == EXECUTING || mState == RECONFIGURING);
2201
2202 CHECK_EQ(mPortStatus[portIndex], DISABLED);
2203 mPortStatus[portIndex] = ENABLING;
2204
2205 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002206 mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -07002207 CHECK_EQ(err, OK);
2208}
2209
2210void OMXCodec::fillOutputBuffers() {
2211 CHECK_EQ(mState, EXECUTING);
2212
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002213 // This is a workaround for some decoders not properly reporting
2214 // end-of-output-stream. If we own all input buffers and also own
2215 // all output buffers and we already signalled end-of-input-stream,
2216 // the end-of-output-stream is implied.
2217 if (mSignalledEOS
2218 && countBuffersWeOwn(mPortBuffers[kPortIndexInput])
2219 == mPortBuffers[kPortIndexInput].size()
2220 && countBuffersWeOwn(mPortBuffers[kPortIndexOutput])
2221 == mPortBuffers[kPortIndexOutput].size()) {
2222 mNoMoreOutputData = true;
2223 mBufferFilled.signal();
2224
2225 return;
2226 }
2227
Andreas Huberbe06d262009-08-14 14:37:10 -07002228 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2229 for (size_t i = 0; i < buffers->size(); ++i) {
2230 fillOutputBuffer(&buffers->editItemAt(i));
2231 }
2232}
2233
2234void OMXCodec::drainInputBuffers() {
Andreas Huberd06e5b82009-08-28 13:18:14 -07002235 CHECK(mState == EXECUTING || mState == RECONFIGURING);
Andreas Huberbe06d262009-08-14 14:37:10 -07002236
2237 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2238 for (size_t i = 0; i < buffers->size(); ++i) {
2239 drainInputBuffer(&buffers->editItemAt(i));
2240 }
2241}
2242
2243void OMXCodec::drainInputBuffer(BufferInfo *info) {
2244 CHECK_EQ(info->mOwnedByComponent, false);
2245
2246 if (mSignalledEOS) {
2247 return;
2248 }
2249
2250 if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
2251 const CodecSpecificData *specific =
2252 mCodecSpecificData[mCodecSpecificDataIndex];
2253
2254 size_t size = specific->mSize;
2255
Andreas Hubere6c40962009-09-10 14:13:30 -07002256 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
Andreas Huber4f5e6022009-08-19 09:29:34 -07002257 && !(mQuirks & kWantsNALFragments)) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002258 static const uint8_t kNALStartCode[4] =
2259 { 0x00, 0x00, 0x00, 0x01 };
2260
Andreas Huberc712b9f2010-01-20 15:05:46 -08002261 CHECK(info->mSize >= specific->mSize + 4);
Andreas Huberbe06d262009-08-14 14:37:10 -07002262
2263 size += 4;
2264
Andreas Huberc712b9f2010-01-20 15:05:46 -08002265 memcpy(info->mData, kNALStartCode, 4);
2266 memcpy((uint8_t *)info->mData + 4,
Andreas Huberbe06d262009-08-14 14:37:10 -07002267 specific->mData, specific->mSize);
2268 } else {
Andreas Huberc712b9f2010-01-20 15:05:46 -08002269 CHECK(info->mSize >= specific->mSize);
2270 memcpy(info->mData, specific->mData, specific->mSize);
Andreas Huberbe06d262009-08-14 14:37:10 -07002271 }
2272
Andreas Huber2ea14e22009-12-16 09:30:55 -08002273 mNoMoreOutputData = false;
2274
Andreas Huberdbcb2c62010-01-14 11:32:13 -08002275 CODEC_LOGV("calling emptyBuffer with codec specific data");
2276
Andreas Huber784202e2009-10-15 13:46:54 -07002277 status_t err = mOMX->emptyBuffer(
Andreas Huberbe06d262009-08-14 14:37:10 -07002278 mNode, info->mBuffer, 0, size,
2279 OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
2280 0);
Andreas Huber3f427072009-10-08 11:02:27 -07002281 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002282
2283 info->mOwnedByComponent = true;
2284
2285 ++mCodecSpecificDataIndex;
2286 return;
2287 }
2288
Andreas Huber1f24b302010-06-10 11:12:39 -07002289 if (mPaused) {
2290 return;
2291 }
2292
Andreas Huberbe06d262009-08-14 14:37:10 -07002293 status_t err;
Andreas Huber2ea14e22009-12-16 09:30:55 -08002294
Andreas Hubera4357ad2010-04-02 12:49:54 -07002295 bool signalEOS = false;
2296 int64_t timestampUs = 0;
Andreas Huberbe06d262009-08-14 14:37:10 -07002297
Andreas Hubera4357ad2010-04-02 12:49:54 -07002298 size_t offset = 0;
2299 int32_t n = 0;
2300 for (;;) {
2301 MediaBuffer *srcBuffer;
James Dong53d4e0d2010-07-21 14:51:35 -07002302 MediaSource::ReadOptions options;
2303 if (mSkipTimeUs >= 0) {
2304 options.setSkipFrame(mSkipTimeUs);
2305 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002306 if (mSeekTimeUs >= 0) {
2307 if (mLeftOverBuffer) {
2308 mLeftOverBuffer->release();
2309 mLeftOverBuffer = NULL;
2310 }
Andreas Huber6624c9f2010-07-20 15:04:28 -07002311 options.setSeekTo(mSeekTimeUs, mSeekMode);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002312
2313 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002314 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002315 mBufferFilled.signal();
2316
2317 err = mSource->read(&srcBuffer, &options);
Andreas Huber6624c9f2010-07-20 15:04:28 -07002318
2319 if (err == OK) {
2320 int64_t targetTimeUs;
2321 if (srcBuffer->meta_data()->findInt64(
2322 kKeyTargetTime, &targetTimeUs)
2323 && targetTimeUs >= 0) {
2324 mTargetTimeUs = targetTimeUs;
2325 } else {
2326 mTargetTimeUs = -1;
2327 }
2328 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002329 } else if (mLeftOverBuffer) {
2330 srcBuffer = mLeftOverBuffer;
2331 mLeftOverBuffer = NULL;
2332
2333 err = OK;
2334 } else {
James Dong53d4e0d2010-07-21 14:51:35 -07002335 err = mSource->read(&srcBuffer, &options);
Andreas Hubera4357ad2010-04-02 12:49:54 -07002336 }
2337
2338 if (err != OK) {
2339 signalEOS = true;
2340 mFinalStatus = err;
2341 mSignalledEOS = true;
2342 break;
2343 }
2344
2345 size_t remainingBytes = info->mSize - offset;
2346
2347 if (srcBuffer->range_length() > remainingBytes) {
2348 if (offset == 0) {
2349 CODEC_LOGE(
2350 "Codec's input buffers are too small to accomodate "
2351 "buffer read from source (info->mSize = %d, srcLength = %d)",
2352 info->mSize, srcBuffer->range_length());
2353
2354 srcBuffer->release();
2355 srcBuffer = NULL;
2356
2357 setState(ERROR);
2358 return;
2359 }
2360
2361 mLeftOverBuffer = srcBuffer;
2362 break;
2363 }
2364
James Dong4f501f02010-06-07 14:41:41 -07002365 if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) {
2366 CHECK(mOMXLivesLocally && offset == 0);
2367 OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *) info->mBuffer;
2368 header->pBuffer = (OMX_U8 *) srcBuffer->data() + srcBuffer->range_offset();
2369 } else {
2370 memcpy((uint8_t *)info->mData + offset,
2371 (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
2372 srcBuffer->range_length());
2373 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002374
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002375 int64_t lastBufferTimeUs;
2376 CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs));
Andreas Huber6624c9f2010-07-20 15:04:28 -07002377 CHECK(lastBufferTimeUs >= 0);
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002378
Andreas Hubera4357ad2010-04-02 12:49:54 -07002379 if (offset == 0) {
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002380 timestampUs = lastBufferTimeUs;
Andreas Hubera4357ad2010-04-02 12:49:54 -07002381 }
2382
2383 offset += srcBuffer->range_length();
2384
2385 srcBuffer->release();
2386 srcBuffer = NULL;
2387
2388 ++n;
2389
2390 if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) {
2391 break;
2392 }
Andreas Huber2dd8ff82010-04-20 14:26:00 -07002393
2394 int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs;
2395
2396 if (coalescedDurationUs > 250000ll) {
2397 // Don't coalesce more than 250ms worth of encoded data at once.
2398 break;
2399 }
Andreas Hubera4357ad2010-04-02 12:49:54 -07002400 }
2401
2402 if (n > 1) {
2403 LOGV("coalesced %d frames into one input buffer", n);
Andreas Huberbe06d262009-08-14 14:37:10 -07002404 }
2405
2406 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
Andreas Huberbe06d262009-08-14 14:37:10 -07002407
Andreas Hubera4357ad2010-04-02 12:49:54 -07002408 if (signalEOS) {
Andreas Huberbe06d262009-08-14 14:37:10 -07002409 flags |= OMX_BUFFERFLAG_EOS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002410 } else {
Andreas Huber2ea14e22009-12-16 09:30:55 -08002411 mNoMoreOutputData = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002412 }
2413
Andreas Hubera4357ad2010-04-02 12:49:54 -07002414 CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), "
2415 "timestamp %lld us (%.2f secs)",
2416 info->mBuffer, offset,
2417 timestampUs, timestampUs / 1E6);
Andreas Huber3f427072009-10-08 11:02:27 -07002418
Andreas Huber784202e2009-10-15 13:46:54 -07002419 err = mOMX->emptyBuffer(
Andreas Hubera4357ad2010-04-02 12:49:54 -07002420 mNode, info->mBuffer, 0, offset,
Andreas Huberfa8de752009-10-08 10:07:49 -07002421 flags, timestampUs);
Andreas Huber3f427072009-10-08 11:02:27 -07002422
2423 if (err != OK) {
2424 setState(ERROR);
2425 return;
2426 }
2427
2428 info->mOwnedByComponent = true;
Andreas Huberea6a38c2009-11-16 15:43:38 -08002429
2430 // This component does not ever signal the EOS flag on output buffers,
2431 // Thanks for nothing.
2432 if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) {
2433 mNoMoreOutputData = true;
2434 mBufferFilled.signal();
2435 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002436}
2437
2438void OMXCodec::fillOutputBuffer(BufferInfo *info) {
2439 CHECK_EQ(info->mOwnedByComponent, false);
2440
Andreas Huber404cc412009-08-25 14:26:05 -07002441 if (mNoMoreOutputData) {
Andreas Huber4c483422009-09-02 16:05:36 -07002442 CODEC_LOGV("There is no more output data available, not "
Andreas Huber404cc412009-08-25 14:26:05 -07002443 "calling fillOutputBuffer");
2444 return;
2445 }
2446
Andreas Huber4c483422009-09-02 16:05:36 -07002447 CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
Andreas Huber784202e2009-10-15 13:46:54 -07002448 status_t err = mOMX->fillBuffer(mNode, info->mBuffer);
Andreas Huber8f14c552010-04-12 10:20:12 -07002449
2450 if (err != OK) {
2451 CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err);
2452
2453 setState(ERROR);
2454 return;
2455 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002456
2457 info->mOwnedByComponent = true;
2458}
2459
2460void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
2461 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
2462 for (size_t i = 0; i < buffers->size(); ++i) {
2463 if ((*buffers)[i].mBuffer == buffer) {
2464 drainInputBuffer(&buffers->editItemAt(i));
2465 return;
2466 }
2467 }
2468
2469 CHECK(!"should not be here.");
2470}
2471
2472void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
2473 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
2474 for (size_t i = 0; i < buffers->size(); ++i) {
2475 if ((*buffers)[i].mBuffer == buffer) {
2476 fillOutputBuffer(&buffers->editItemAt(i));
2477 return;
2478 }
2479 }
2480
2481 CHECK(!"should not be here.");
2482}
2483
2484void OMXCodec::setState(State newState) {
2485 mState = newState;
2486 mAsyncCompletion.signal();
2487
2488 // This may cause some spurious wakeups but is necessary to
2489 // unblock the reader if we enter ERROR state.
2490 mBufferFilled.signal();
2491}
2492
Andreas Huberda050cf22009-09-02 14:01:43 -07002493void OMXCodec::setRawAudioFormat(
2494 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
James Dongabed93a2010-04-22 17:27:04 -07002495
2496 // port definition
2497 OMX_PARAM_PORTDEFINITIONTYPE def;
2498 InitOMXParams(&def);
2499 def.nPortIndex = portIndex;
2500 status_t err = mOMX->getParameter(
2501 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2502 CHECK_EQ(err, OK);
2503 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2504 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2505 &def, sizeof(def)), OK);
2506
2507 // pcm param
Andreas Huberda050cf22009-09-02 14:01:43 -07002508 OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
Andreas Huber4c483422009-09-02 16:05:36 -07002509 InitOMXParams(&pcmParams);
Andreas Huberda050cf22009-09-02 14:01:43 -07002510 pcmParams.nPortIndex = portIndex;
2511
James Dongabed93a2010-04-22 17:27:04 -07002512 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002513 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2514
2515 CHECK_EQ(err, OK);
2516
2517 pcmParams.nChannels = numChannels;
2518 pcmParams.eNumData = OMX_NumericalDataSigned;
2519 pcmParams.bInterleaved = OMX_TRUE;
2520 pcmParams.nBitPerSample = 16;
2521 pcmParams.nSamplingRate = sampleRate;
2522 pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
2523
2524 if (numChannels == 1) {
2525 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
2526 } else {
2527 CHECK_EQ(numChannels, 2);
2528
2529 pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
2530 pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
2531 }
2532
Andreas Huber784202e2009-10-15 13:46:54 -07002533 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002534 mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
2535
2536 CHECK_EQ(err, OK);
2537}
2538
James Dong17299ab2010-05-14 15:45:22 -07002539static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) {
2540 if (isAMRWB) {
2541 if (bps <= 6600) {
2542 return OMX_AUDIO_AMRBandModeWB0;
2543 } else if (bps <= 8850) {
2544 return OMX_AUDIO_AMRBandModeWB1;
2545 } else if (bps <= 12650) {
2546 return OMX_AUDIO_AMRBandModeWB2;
2547 } else if (bps <= 14250) {
2548 return OMX_AUDIO_AMRBandModeWB3;
2549 } else if (bps <= 15850) {
2550 return OMX_AUDIO_AMRBandModeWB4;
2551 } else if (bps <= 18250) {
2552 return OMX_AUDIO_AMRBandModeWB5;
2553 } else if (bps <= 19850) {
2554 return OMX_AUDIO_AMRBandModeWB6;
2555 } else if (bps <= 23050) {
2556 return OMX_AUDIO_AMRBandModeWB7;
2557 }
2558
2559 // 23850 bps
2560 return OMX_AUDIO_AMRBandModeWB8;
2561 } else { // AMRNB
2562 if (bps <= 4750) {
2563 return OMX_AUDIO_AMRBandModeNB0;
2564 } else if (bps <= 5150) {
2565 return OMX_AUDIO_AMRBandModeNB1;
2566 } else if (bps <= 5900) {
2567 return OMX_AUDIO_AMRBandModeNB2;
2568 } else if (bps <= 6700) {
2569 return OMX_AUDIO_AMRBandModeNB3;
2570 } else if (bps <= 7400) {
2571 return OMX_AUDIO_AMRBandModeNB4;
2572 } else if (bps <= 7950) {
2573 return OMX_AUDIO_AMRBandModeNB5;
2574 } else if (bps <= 10200) {
2575 return OMX_AUDIO_AMRBandModeNB6;
2576 }
2577
2578 // 12200 bps
2579 return OMX_AUDIO_AMRBandModeNB7;
2580 }
2581}
2582
2583void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) {
Andreas Huber8768f2c2009-12-01 15:26:54 -08002584 OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002585
Andreas Huber8768f2c2009-12-01 15:26:54 -08002586 OMX_AUDIO_PARAM_AMRTYPE def;
2587 InitOMXParams(&def);
2588 def.nPortIndex = portIndex;
Andreas Huberbe06d262009-08-14 14:37:10 -07002589
Andreas Huber8768f2c2009-12-01 15:26:54 -08002590 status_t err =
2591 mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
Andreas Huberbe06d262009-08-14 14:37:10 -07002592
Andreas Huber8768f2c2009-12-01 15:26:54 -08002593 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002594
Andreas Huber8768f2c2009-12-01 15:26:54 -08002595 def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
James Dongabed93a2010-04-22 17:27:04 -07002596
James Dong17299ab2010-05-14 15:45:22 -07002597 def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate);
Andreas Huber8768f2c2009-12-01 15:26:54 -08002598 err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
2599 CHECK_EQ(err, OK);
Andreas Huberee606e62009-09-08 10:19:21 -07002600
2601 ////////////////////////
2602
2603 if (mIsEncoder) {
2604 sp<MetaData> format = mSource->getFormat();
2605 int32_t sampleRate;
2606 int32_t numChannels;
2607 CHECK(format->findInt32(kKeySampleRate, &sampleRate));
2608 CHECK(format->findInt32(kKeyChannelCount, &numChannels));
2609
2610 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
2611 }
2612}
2613
James Dong17299ab2010-05-14 15:45:22 -07002614void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) {
James Dongabed93a2010-04-22 17:27:04 -07002615 CHECK(numChannels == 1 || numChannels == 2);
Andreas Huberda050cf22009-09-02 14:01:43 -07002616 if (mIsEncoder) {
James Dongabed93a2010-04-22 17:27:04 -07002617 //////////////// input port ////////////////////
Andreas Huberda050cf22009-09-02 14:01:43 -07002618 setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
James Dongabed93a2010-04-22 17:27:04 -07002619
2620 //////////////// output port ////////////////////
2621 // format
2622 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
2623 format.nPortIndex = kPortIndexOutput;
2624 format.nIndex = 0;
2625 status_t err = OMX_ErrorNone;
2626 while (OMX_ErrorNone == err) {
2627 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat,
2628 &format, sizeof(format)), OK);
2629 if (format.eEncoding == OMX_AUDIO_CodingAAC) {
2630 break;
2631 }
2632 format.nIndex++;
2633 }
2634 CHECK_EQ(OK, err);
2635 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat,
2636 &format, sizeof(format)), OK);
2637
2638 // port definition
2639 OMX_PARAM_PORTDEFINITIONTYPE def;
2640 InitOMXParams(&def);
2641 def.nPortIndex = kPortIndexOutput;
2642 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition,
2643 &def, sizeof(def)), OK);
2644 def.format.audio.bFlagErrorConcealment = OMX_TRUE;
2645 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
2646 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition,
2647 &def, sizeof(def)), OK);
2648
2649 // profile
2650 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
2651 InitOMXParams(&profile);
2652 profile.nPortIndex = kPortIndexOutput;
2653 CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac,
2654 &profile, sizeof(profile)), OK);
2655 profile.nChannels = numChannels;
2656 profile.eChannelMode = (numChannels == 1?
2657 OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo);
2658 profile.nSampleRate = sampleRate;
James Dong17299ab2010-05-14 15:45:22 -07002659 profile.nBitRate = bitRate;
James Dongabed93a2010-04-22 17:27:04 -07002660 profile.nAudioBandWidth = 0;
2661 profile.nFrameLength = 0;
2662 profile.nAACtools = OMX_AUDIO_AACToolAll;
2663 profile.nAACERtools = OMX_AUDIO_AACERNone;
2664 profile.eAACProfile = OMX_AUDIO_AACObjectLC;
2665 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
2666 CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac,
2667 &profile, sizeof(profile)), OK);
2668
Andreas Huberda050cf22009-09-02 14:01:43 -07002669 } else {
2670 OMX_AUDIO_PARAM_AACPROFILETYPE profile;
Andreas Huber4c483422009-09-02 16:05:36 -07002671 InitOMXParams(&profile);
Andreas Huberda050cf22009-09-02 14:01:43 -07002672 profile.nPortIndex = kPortIndexInput;
Andreas Huberbe06d262009-08-14 14:37:10 -07002673
Andreas Huber784202e2009-10-15 13:46:54 -07002674 status_t err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002675 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2676 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07002677
Andreas Huberda050cf22009-09-02 14:01:43 -07002678 profile.nChannels = numChannels;
2679 profile.nSampleRate = sampleRate;
2680 profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
Andreas Huberbe06d262009-08-14 14:37:10 -07002681
Andreas Huber784202e2009-10-15 13:46:54 -07002682 err = mOMX->setParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07002683 mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
2684 CHECK_EQ(err, OK);
2685 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002686}
2687
2688void OMXCodec::setImageOutputFormat(
2689 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
Andreas Huber4c483422009-09-02 16:05:36 -07002690 CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
Andreas Huberbe06d262009-08-14 14:37:10 -07002691
2692#if 0
2693 OMX_INDEXTYPE index;
2694 status_t err = mOMX->get_extension_index(
2695 mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
2696 CHECK_EQ(err, OK);
2697
2698 err = mOMX->set_config(mNode, index, &format, sizeof(format));
2699 CHECK_EQ(err, OK);
2700#endif
2701
2702 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002703 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002704 def.nPortIndex = kPortIndexOutput;
2705
Andreas Huber784202e2009-10-15 13:46:54 -07002706 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002707 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2708 CHECK_EQ(err, OK);
2709
2710 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2711
2712 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
Andreas Huberebf66ea2009-08-19 13:32:58 -07002713
Andreas Huberbe06d262009-08-14 14:37:10 -07002714 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2715 imageDef->eColorFormat = format;
2716 imageDef->nFrameWidth = width;
2717 imageDef->nFrameHeight = height;
2718
2719 switch (format) {
2720 case OMX_COLOR_FormatYUV420PackedPlanar:
2721 case OMX_COLOR_FormatYUV411Planar:
2722 {
2723 def.nBufferSize = (width * height * 3) / 2;
2724 break;
2725 }
2726
2727 case OMX_COLOR_FormatCbYCrY:
2728 {
2729 def.nBufferSize = width * height * 2;
2730 break;
2731 }
2732
2733 case OMX_COLOR_Format32bitARGB8888:
2734 {
2735 def.nBufferSize = width * height * 4;
2736 break;
2737 }
2738
Andreas Huber201511c2009-09-08 14:01:44 -07002739 case OMX_COLOR_Format16bitARGB4444:
2740 case OMX_COLOR_Format16bitARGB1555:
2741 case OMX_COLOR_Format16bitRGB565:
2742 case OMX_COLOR_Format16bitBGR565:
2743 {
2744 def.nBufferSize = width * height * 2;
2745 break;
2746 }
2747
Andreas Huberbe06d262009-08-14 14:37:10 -07002748 default:
2749 CHECK(!"Should not be here. Unknown color format.");
2750 break;
2751 }
2752
Andreas Huber5c0a9132009-08-20 11:16:40 -07002753 def.nBufferCountActual = def.nBufferCountMin;
2754
Andreas Huber784202e2009-10-15 13:46:54 -07002755 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002756 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2757 CHECK_EQ(err, OK);
Andreas Huber5c0a9132009-08-20 11:16:40 -07002758}
Andreas Huberbe06d262009-08-14 14:37:10 -07002759
Andreas Huber5c0a9132009-08-20 11:16:40 -07002760void OMXCodec::setJPEGInputFormat(
2761 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
2762 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07002763 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07002764 def.nPortIndex = kPortIndexInput;
2765
Andreas Huber784202e2009-10-15 13:46:54 -07002766 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002767 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2768 CHECK_EQ(err, OK);
2769
Andreas Huber5c0a9132009-08-20 11:16:40 -07002770 CHECK_EQ(def.eDomain, OMX_PortDomainImage);
2771 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2772
Andreas Huberbe06d262009-08-14 14:37:10 -07002773 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
2774 imageDef->nFrameWidth = width;
2775 imageDef->nFrameHeight = height;
2776
Andreas Huber5c0a9132009-08-20 11:16:40 -07002777 def.nBufferSize = compressedSize;
Andreas Huberbe06d262009-08-14 14:37:10 -07002778 def.nBufferCountActual = def.nBufferCountMin;
2779
Andreas Huber784202e2009-10-15 13:46:54 -07002780 err = mOMX->setParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07002781 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2782 CHECK_EQ(err, OK);
2783}
2784
2785void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
2786 CodecSpecificData *specific =
2787 (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
2788
2789 specific->mSize = size;
2790 memcpy(specific->mData, data, size);
2791
2792 mCodecSpecificData.push(specific);
2793}
2794
2795void OMXCodec::clearCodecSpecificData() {
2796 for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
2797 free(mCodecSpecificData.editItemAt(i));
2798 }
2799 mCodecSpecificData.clear();
2800 mCodecSpecificDataIndex = 0;
2801}
2802
James Dong36e573b2010-06-19 09:04:18 -07002803status_t OMXCodec::start(MetaData *meta) {
Andreas Huber42978e52009-08-27 10:08:39 -07002804 Mutex::Autolock autoLock(mLock);
2805
Andreas Huberbe06d262009-08-14 14:37:10 -07002806 if (mState != LOADED) {
2807 return UNKNOWN_ERROR;
2808 }
Andreas Huberebf66ea2009-08-19 13:32:58 -07002809
Andreas Huberbe06d262009-08-14 14:37:10 -07002810 sp<MetaData> params = new MetaData;
Andreas Huber4f5e6022009-08-19 09:29:34 -07002811 if (mQuirks & kWantsNALFragments) {
2812 params->setInt32(kKeyWantsNALFragments, true);
Andreas Huberbe06d262009-08-14 14:37:10 -07002813 }
James Dong36e573b2010-06-19 09:04:18 -07002814 if (meta) {
2815 int64_t startTimeUs = 0;
2816 int64_t timeUs;
2817 if (meta->findInt64(kKeyTime, &timeUs)) {
2818 startTimeUs = timeUs;
2819 }
2820 params->setInt64(kKeyTime, startTimeUs);
2821 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002822 status_t err = mSource->start(params.get());
2823
2824 if (err != OK) {
2825 return err;
2826 }
2827
2828 mCodecSpecificDataIndex = 0;
Andreas Huber42978e52009-08-27 10:08:39 -07002829 mInitialBufferSubmit = true;
Andreas Huberbe06d262009-08-14 14:37:10 -07002830 mSignalledEOS = false;
2831 mNoMoreOutputData = false;
Andreas Hubercfd55572009-10-09 14:11:28 -07002832 mOutputPortSettingsHaveChanged = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002833 mSeekTimeUs = -1;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002834 mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC;
2835 mTargetTimeUs = -1;
Andreas Huberbe06d262009-08-14 14:37:10 -07002836 mFilledBuffers.clear();
Andreas Huber1f24b302010-06-10 11:12:39 -07002837 mPaused = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002838
2839 return init();
2840}
2841
2842status_t OMXCodec::stop() {
Andreas Huber4a9375e2010-02-09 11:54:33 -08002843 CODEC_LOGV("stop mState=%d", mState);
Andreas Huberbe06d262009-08-14 14:37:10 -07002844
2845 Mutex::Autolock autoLock(mLock);
2846
2847 while (isIntermediateState(mState)) {
2848 mAsyncCompletion.wait(mLock);
2849 }
2850
2851 switch (mState) {
2852 case LOADED:
2853 case ERROR:
2854 break;
2855
2856 case EXECUTING:
2857 {
2858 setState(EXECUTING_TO_IDLE);
2859
Andreas Huber127fcdc2009-08-26 16:27:02 -07002860 if (mQuirks & kRequiresFlushBeforeShutdown) {
Andreas Huber4c483422009-09-02 16:05:36 -07002861 CODEC_LOGV("This component requires a flush before transitioning "
Andreas Huber127fcdc2009-08-26 16:27:02 -07002862 "from EXECUTING to IDLE...");
Andreas Huberbe06d262009-08-14 14:37:10 -07002863
Andreas Huber127fcdc2009-08-26 16:27:02 -07002864 bool emulateInputFlushCompletion =
2865 !flushPortAsync(kPortIndexInput);
2866
2867 bool emulateOutputFlushCompletion =
2868 !flushPortAsync(kPortIndexOutput);
2869
2870 if (emulateInputFlushCompletion) {
2871 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2872 }
2873
2874 if (emulateOutputFlushCompletion) {
2875 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2876 }
2877 } else {
2878 mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
2879 mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
2880
2881 status_t err =
Andreas Huber784202e2009-10-15 13:46:54 -07002882 mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle);
Andreas Huber127fcdc2009-08-26 16:27:02 -07002883 CHECK_EQ(err, OK);
2884 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002885
2886 while (mState != LOADED && mState != ERROR) {
2887 mAsyncCompletion.wait(mLock);
2888 }
2889
2890 break;
2891 }
2892
2893 default:
2894 {
2895 CHECK(!"should not be here.");
2896 break;
2897 }
2898 }
2899
Andreas Hubera4357ad2010-04-02 12:49:54 -07002900 if (mLeftOverBuffer) {
2901 mLeftOverBuffer->release();
2902 mLeftOverBuffer = NULL;
2903 }
2904
Andreas Huberbe06d262009-08-14 14:37:10 -07002905 mSource->stop();
2906
Andreas Huber4a9375e2010-02-09 11:54:33 -08002907 CODEC_LOGV("stopped");
2908
Andreas Huberbe06d262009-08-14 14:37:10 -07002909 return OK;
2910}
2911
2912sp<MetaData> OMXCodec::getFormat() {
Andreas Hubercfd55572009-10-09 14:11:28 -07002913 Mutex::Autolock autoLock(mLock);
2914
Andreas Huberbe06d262009-08-14 14:37:10 -07002915 return mOutputFormat;
2916}
2917
2918status_t OMXCodec::read(
2919 MediaBuffer **buffer, const ReadOptions *options) {
2920 *buffer = NULL;
2921
2922 Mutex::Autolock autoLock(mLock);
2923
Andreas Huberd06e5b82009-08-28 13:18:14 -07002924 if (mState != EXECUTING && mState != RECONFIGURING) {
2925 return UNKNOWN_ERROR;
2926 }
2927
Andreas Hubere981c332009-10-22 13:49:30 -07002928 bool seeking = false;
2929 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002930 ReadOptions::SeekMode seekMode;
2931 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubere981c332009-10-22 13:49:30 -07002932 seeking = true;
2933 }
James Dong53d4e0d2010-07-21 14:51:35 -07002934 int64_t skipTimeUs;
2935 if (options && options->getSkipFrame(&skipTimeUs)) {
2936 mSkipTimeUs = skipTimeUs;
2937 } else {
2938 mSkipTimeUs = -1;
2939 }
Andreas Hubere981c332009-10-22 13:49:30 -07002940
Andreas Huber42978e52009-08-27 10:08:39 -07002941 if (mInitialBufferSubmit) {
2942 mInitialBufferSubmit = false;
2943
Andreas Hubere981c332009-10-22 13:49:30 -07002944 if (seeking) {
2945 CHECK(seekTimeUs >= 0);
2946 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002947 mSeekMode = seekMode;
Andreas Hubere981c332009-10-22 13:49:30 -07002948
2949 // There's no reason to trigger the code below, there's
2950 // nothing to flush yet.
2951 seeking = false;
Andreas Huber1f24b302010-06-10 11:12:39 -07002952 mPaused = false;
Andreas Hubere981c332009-10-22 13:49:30 -07002953 }
2954
Andreas Huber42978e52009-08-27 10:08:39 -07002955 drainInputBuffers();
Andreas Huber42978e52009-08-27 10:08:39 -07002956
Andreas Huberd06e5b82009-08-28 13:18:14 -07002957 if (mState == EXECUTING) {
2958 // Otherwise mState == RECONFIGURING and this code will trigger
2959 // after the output port is reenabled.
2960 fillOutputBuffers();
2961 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002962 }
2963
Andreas Hubere981c332009-10-22 13:49:30 -07002964 if (seeking) {
Andreas Huber4c483422009-09-02 16:05:36 -07002965 CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
Andreas Huberbe06d262009-08-14 14:37:10 -07002966
2967 mSignalledEOS = false;
Andreas Huberbe06d262009-08-14 14:37:10 -07002968
2969 CHECK(seekTimeUs >= 0);
2970 mSeekTimeUs = seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -07002971 mSeekMode = seekMode;
Andreas Huberbe06d262009-08-14 14:37:10 -07002972
2973 mFilledBuffers.clear();
2974
2975 CHECK_EQ(mState, EXECUTING);
2976
Andreas Huber404cc412009-08-25 14:26:05 -07002977 bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
2978 bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
2979
2980 if (emulateInputFlushCompletion) {
2981 onCmdComplete(OMX_CommandFlush, kPortIndexInput);
2982 }
2983
2984 if (emulateOutputFlushCompletion) {
2985 onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
2986 }
Andreas Huber2ea14e22009-12-16 09:30:55 -08002987
2988 while (mSeekTimeUs >= 0) {
2989 mBufferFilled.wait(mLock);
2990 }
Andreas Huberbe06d262009-08-14 14:37:10 -07002991 }
2992
2993 while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
2994 mBufferFilled.wait(mLock);
2995 }
2996
2997 if (mState == ERROR) {
2998 return UNKNOWN_ERROR;
2999 }
3000
3001 if (mFilledBuffers.empty()) {
Andreas Huberd7d22eb2010-02-23 13:45:33 -08003002 return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM;
Andreas Huberbe06d262009-08-14 14:37:10 -07003003 }
3004
Andreas Hubercfd55572009-10-09 14:11:28 -07003005 if (mOutputPortSettingsHaveChanged) {
3006 mOutputPortSettingsHaveChanged = false;
3007
3008 return INFO_FORMAT_CHANGED;
3009 }
3010
Andreas Huberbe06d262009-08-14 14:37:10 -07003011 size_t index = *mFilledBuffers.begin();
3012 mFilledBuffers.erase(mFilledBuffers.begin());
3013
3014 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
3015 info->mMediaBuffer->add_ref();
3016 *buffer = info->mMediaBuffer;
3017
3018 return OK;
3019}
3020
3021void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
3022 Mutex::Autolock autoLock(mLock);
3023
3024 Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
3025 for (size_t i = 0; i < buffers->size(); ++i) {
3026 BufferInfo *info = &buffers->editItemAt(i);
3027
3028 if (info->mMediaBuffer == buffer) {
3029 CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
3030 fillOutputBuffer(info);
3031 return;
3032 }
3033 }
3034
3035 CHECK(!"should not be here.");
3036}
3037
3038static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
3039 static const char *kNames[] = {
3040 "OMX_IMAGE_CodingUnused",
3041 "OMX_IMAGE_CodingAutoDetect",
3042 "OMX_IMAGE_CodingJPEG",
3043 "OMX_IMAGE_CodingJPEG2K",
3044 "OMX_IMAGE_CodingEXIF",
3045 "OMX_IMAGE_CodingTIFF",
3046 "OMX_IMAGE_CodingGIF",
3047 "OMX_IMAGE_CodingPNG",
3048 "OMX_IMAGE_CodingLZW",
3049 "OMX_IMAGE_CodingBMP",
3050 };
3051
3052 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3053
3054 if (type < 0 || (size_t)type >= numNames) {
3055 return "UNKNOWN";
3056 } else {
3057 return kNames[type];
3058 }
3059}
3060
3061static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
3062 static const char *kNames[] = {
3063 "OMX_COLOR_FormatUnused",
3064 "OMX_COLOR_FormatMonochrome",
3065 "OMX_COLOR_Format8bitRGB332",
3066 "OMX_COLOR_Format12bitRGB444",
3067 "OMX_COLOR_Format16bitARGB4444",
3068 "OMX_COLOR_Format16bitARGB1555",
3069 "OMX_COLOR_Format16bitRGB565",
3070 "OMX_COLOR_Format16bitBGR565",
3071 "OMX_COLOR_Format18bitRGB666",
3072 "OMX_COLOR_Format18bitARGB1665",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003073 "OMX_COLOR_Format19bitARGB1666",
Andreas Huberbe06d262009-08-14 14:37:10 -07003074 "OMX_COLOR_Format24bitRGB888",
3075 "OMX_COLOR_Format24bitBGR888",
3076 "OMX_COLOR_Format24bitARGB1887",
3077 "OMX_COLOR_Format25bitARGB1888",
3078 "OMX_COLOR_Format32bitBGRA8888",
3079 "OMX_COLOR_Format32bitARGB8888",
3080 "OMX_COLOR_FormatYUV411Planar",
3081 "OMX_COLOR_FormatYUV411PackedPlanar",
3082 "OMX_COLOR_FormatYUV420Planar",
3083 "OMX_COLOR_FormatYUV420PackedPlanar",
3084 "OMX_COLOR_FormatYUV420SemiPlanar",
3085 "OMX_COLOR_FormatYUV422Planar",
3086 "OMX_COLOR_FormatYUV422PackedPlanar",
3087 "OMX_COLOR_FormatYUV422SemiPlanar",
3088 "OMX_COLOR_FormatYCbYCr",
3089 "OMX_COLOR_FormatYCrYCb",
3090 "OMX_COLOR_FormatCbYCrY",
3091 "OMX_COLOR_FormatCrYCbY",
3092 "OMX_COLOR_FormatYUV444Interleaved",
3093 "OMX_COLOR_FormatRawBayer8bit",
3094 "OMX_COLOR_FormatRawBayer10bit",
3095 "OMX_COLOR_FormatRawBayer8bitcompressed",
Andreas Huberebf66ea2009-08-19 13:32:58 -07003096 "OMX_COLOR_FormatL2",
3097 "OMX_COLOR_FormatL4",
3098 "OMX_COLOR_FormatL8",
3099 "OMX_COLOR_FormatL16",
3100 "OMX_COLOR_FormatL24",
Andreas Huberbe06d262009-08-14 14:37:10 -07003101 "OMX_COLOR_FormatL32",
3102 "OMX_COLOR_FormatYUV420PackedSemiPlanar",
3103 "OMX_COLOR_FormatYUV422PackedSemiPlanar",
3104 "OMX_COLOR_Format18BitBGR666",
3105 "OMX_COLOR_Format24BitARGB6666",
3106 "OMX_COLOR_Format24BitABGR6666",
3107 };
3108
3109 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3110
Andreas Huberbe06d262009-08-14 14:37:10 -07003111 if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
3112 return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
3113 } else if (type < 0 || (size_t)type >= numNames) {
3114 return "UNKNOWN";
3115 } else {
3116 return kNames[type];
3117 }
3118}
3119
3120static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
3121 static const char *kNames[] = {
3122 "OMX_VIDEO_CodingUnused",
3123 "OMX_VIDEO_CodingAutoDetect",
3124 "OMX_VIDEO_CodingMPEG2",
3125 "OMX_VIDEO_CodingH263",
3126 "OMX_VIDEO_CodingMPEG4",
3127 "OMX_VIDEO_CodingWMV",
3128 "OMX_VIDEO_CodingRV",
3129 "OMX_VIDEO_CodingAVC",
3130 "OMX_VIDEO_CodingMJPEG",
3131 };
3132
3133 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3134
3135 if (type < 0 || (size_t)type >= numNames) {
3136 return "UNKNOWN";
3137 } else {
3138 return kNames[type];
3139 }
3140}
3141
3142static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
3143 static const char *kNames[] = {
3144 "OMX_AUDIO_CodingUnused",
3145 "OMX_AUDIO_CodingAutoDetect",
3146 "OMX_AUDIO_CodingPCM",
3147 "OMX_AUDIO_CodingADPCM",
3148 "OMX_AUDIO_CodingAMR",
3149 "OMX_AUDIO_CodingGSMFR",
3150 "OMX_AUDIO_CodingGSMEFR",
3151 "OMX_AUDIO_CodingGSMHR",
3152 "OMX_AUDIO_CodingPDCFR",
3153 "OMX_AUDIO_CodingPDCEFR",
3154 "OMX_AUDIO_CodingPDCHR",
3155 "OMX_AUDIO_CodingTDMAFR",
3156 "OMX_AUDIO_CodingTDMAEFR",
3157 "OMX_AUDIO_CodingQCELP8",
3158 "OMX_AUDIO_CodingQCELP13",
3159 "OMX_AUDIO_CodingEVRC",
3160 "OMX_AUDIO_CodingSMV",
3161 "OMX_AUDIO_CodingG711",
3162 "OMX_AUDIO_CodingG723",
3163 "OMX_AUDIO_CodingG726",
3164 "OMX_AUDIO_CodingG729",
3165 "OMX_AUDIO_CodingAAC",
3166 "OMX_AUDIO_CodingMP3",
3167 "OMX_AUDIO_CodingSBC",
3168 "OMX_AUDIO_CodingVORBIS",
3169 "OMX_AUDIO_CodingWMA",
3170 "OMX_AUDIO_CodingRA",
3171 "OMX_AUDIO_CodingMIDI",
3172 };
3173
3174 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3175
3176 if (type < 0 || (size_t)type >= numNames) {
3177 return "UNKNOWN";
3178 } else {
3179 return kNames[type];
3180 }
3181}
3182
3183static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
3184 static const char *kNames[] = {
3185 "OMX_AUDIO_PCMModeLinear",
3186 "OMX_AUDIO_PCMModeALaw",
3187 "OMX_AUDIO_PCMModeMULaw",
3188 };
3189
3190 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3191
3192 if (type < 0 || (size_t)type >= numNames) {
3193 return "UNKNOWN";
3194 } else {
3195 return kNames[type];
3196 }
3197}
3198
Andreas Huber7ae02c82009-09-09 16:29:47 -07003199static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
3200 static const char *kNames[] = {
3201 "OMX_AUDIO_AMRBandModeUnused",
3202 "OMX_AUDIO_AMRBandModeNB0",
3203 "OMX_AUDIO_AMRBandModeNB1",
3204 "OMX_AUDIO_AMRBandModeNB2",
3205 "OMX_AUDIO_AMRBandModeNB3",
3206 "OMX_AUDIO_AMRBandModeNB4",
3207 "OMX_AUDIO_AMRBandModeNB5",
3208 "OMX_AUDIO_AMRBandModeNB6",
3209 "OMX_AUDIO_AMRBandModeNB7",
3210 "OMX_AUDIO_AMRBandModeWB0",
3211 "OMX_AUDIO_AMRBandModeWB1",
3212 "OMX_AUDIO_AMRBandModeWB2",
3213 "OMX_AUDIO_AMRBandModeWB3",
3214 "OMX_AUDIO_AMRBandModeWB4",
3215 "OMX_AUDIO_AMRBandModeWB5",
3216 "OMX_AUDIO_AMRBandModeWB6",
3217 "OMX_AUDIO_AMRBandModeWB7",
3218 "OMX_AUDIO_AMRBandModeWB8",
3219 };
3220
3221 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3222
3223 if (type < 0 || (size_t)type >= numNames) {
3224 return "UNKNOWN";
3225 } else {
3226 return kNames[type];
3227 }
3228}
3229
3230static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
3231 static const char *kNames[] = {
3232 "OMX_AUDIO_AMRFrameFormatConformance",
3233 "OMX_AUDIO_AMRFrameFormatIF1",
3234 "OMX_AUDIO_AMRFrameFormatIF2",
3235 "OMX_AUDIO_AMRFrameFormatFSF",
3236 "OMX_AUDIO_AMRFrameFormatRTPPayload",
3237 "OMX_AUDIO_AMRFrameFormatITU",
3238 };
3239
3240 size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
3241
3242 if (type < 0 || (size_t)type >= numNames) {
3243 return "UNKNOWN";
3244 } else {
3245 return kNames[type];
3246 }
3247}
Andreas Huberbe06d262009-08-14 14:37:10 -07003248
3249void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
3250 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003251 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003252 def.nPortIndex = portIndex;
3253
Andreas Huber784202e2009-10-15 13:46:54 -07003254 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003255 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3256 CHECK_EQ(err, OK);
3257
3258 printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
3259
3260 CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
3261 || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
3262
3263 printf(" nBufferCountActual = %ld\n", def.nBufferCountActual);
3264 printf(" nBufferCountMin = %ld\n", def.nBufferCountMin);
3265 printf(" nBufferSize = %ld\n", def.nBufferSize);
3266
3267 switch (def.eDomain) {
3268 case OMX_PortDomainImage:
3269 {
3270 const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3271
3272 printf("\n");
3273 printf(" // Image\n");
3274 printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth);
3275 printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight);
3276 printf(" nStride = %ld\n", imageDef->nStride);
3277
3278 printf(" eCompressionFormat = %s\n",
3279 imageCompressionFormatString(imageDef->eCompressionFormat));
3280
3281 printf(" eColorFormat = %s\n",
3282 colorFormatString(imageDef->eColorFormat));
3283
3284 break;
3285 }
3286
3287 case OMX_PortDomainVideo:
3288 {
3289 OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
3290
3291 printf("\n");
3292 printf(" // Video\n");
3293 printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth);
3294 printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight);
3295 printf(" nStride = %ld\n", videoDef->nStride);
3296
3297 printf(" eCompressionFormat = %s\n",
3298 videoCompressionFormatString(videoDef->eCompressionFormat));
3299
3300 printf(" eColorFormat = %s\n",
3301 colorFormatString(videoDef->eColorFormat));
3302
3303 break;
3304 }
3305
3306 case OMX_PortDomainAudio:
3307 {
3308 OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
3309
3310 printf("\n");
3311 printf(" // Audio\n");
3312 printf(" eEncoding = %s\n",
3313 audioCodingTypeString(audioDef->eEncoding));
3314
3315 if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
3316 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003317 InitOMXParams(&params);
Andreas Huberbe06d262009-08-14 14:37:10 -07003318 params.nPortIndex = portIndex;
3319
Andreas Huber784202e2009-10-15 13:46:54 -07003320 err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003321 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3322 CHECK_EQ(err, OK);
3323
3324 printf(" nSamplingRate = %ld\n", params.nSamplingRate);
3325 printf(" nChannels = %ld\n", params.nChannels);
3326 printf(" bInterleaved = %d\n", params.bInterleaved);
3327 printf(" nBitPerSample = %ld\n", params.nBitPerSample);
3328
3329 printf(" eNumData = %s\n",
3330 params.eNumData == OMX_NumericalDataSigned
3331 ? "signed" : "unsigned");
3332
3333 printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
Andreas Huber7ae02c82009-09-09 16:29:47 -07003334 } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
3335 OMX_AUDIO_PARAM_AMRTYPE amr;
3336 InitOMXParams(&amr);
3337 amr.nPortIndex = portIndex;
3338
Andreas Huber784202e2009-10-15 13:46:54 -07003339 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003340 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3341 CHECK_EQ(err, OK);
3342
3343 printf(" nChannels = %ld\n", amr.nChannels);
3344 printf(" eAMRBandMode = %s\n",
3345 amrBandModeString(amr.eAMRBandMode));
3346 printf(" eAMRFrameFormat = %s\n",
3347 amrFrameFormatString(amr.eAMRFrameFormat));
Andreas Huberbe06d262009-08-14 14:37:10 -07003348 }
3349
3350 break;
3351 }
3352
3353 default:
3354 {
3355 printf(" // Unknown\n");
3356 break;
3357 }
3358 }
3359
3360 printf("}\n");
3361}
3362
3363void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
3364 mOutputFormat = new MetaData;
3365 mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
James Dong52d13f02010-07-02 11:39:06 -07003366 if (mIsEncoder) {
3367 int32_t timeScale;
3368 if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) {
3369 mOutputFormat->setInt32(kKeyTimeScale, timeScale);
3370 }
3371 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003372
3373 OMX_PARAM_PORTDEFINITIONTYPE def;
Andreas Huber4c483422009-09-02 16:05:36 -07003374 InitOMXParams(&def);
Andreas Huberbe06d262009-08-14 14:37:10 -07003375 def.nPortIndex = kPortIndexOutput;
3376
Andreas Huber784202e2009-10-15 13:46:54 -07003377 status_t err = mOMX->getParameter(
Andreas Huberbe06d262009-08-14 14:37:10 -07003378 mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
3379 CHECK_EQ(err, OK);
3380
3381 switch (def.eDomain) {
3382 case OMX_PortDomainImage:
3383 {
3384 OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
3385 CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
3386
Andreas Hubere6c40962009-09-10 14:13:30 -07003387 mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003388 mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
3389 mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
3390 mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
3391 break;
3392 }
3393
3394 case OMX_PortDomainAudio:
3395 {
3396 OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
3397
Andreas Huberda050cf22009-09-02 14:01:43 -07003398 if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
3399 OMX_AUDIO_PARAM_PCMMODETYPE params;
Andreas Huber4c483422009-09-02 16:05:36 -07003400 InitOMXParams(&params);
Andreas Huberda050cf22009-09-02 14:01:43 -07003401 params.nPortIndex = kPortIndexOutput;
Andreas Huberbe06d262009-08-14 14:37:10 -07003402
Andreas Huber784202e2009-10-15 13:46:54 -07003403 err = mOMX->getParameter(
Andreas Huberda050cf22009-09-02 14:01:43 -07003404 mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
3405 CHECK_EQ(err, OK);
Andreas Huberbe06d262009-08-14 14:37:10 -07003406
Andreas Huberda050cf22009-09-02 14:01:43 -07003407 CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
3408 CHECK_EQ(params.nBitPerSample, 16);
3409 CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
Andreas Huberbe06d262009-08-14 14:37:10 -07003410
Andreas Huberda050cf22009-09-02 14:01:43 -07003411 int32_t numChannels, sampleRate;
3412 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3413 inputFormat->findInt32(kKeySampleRate, &sampleRate);
Andreas Huberbe06d262009-08-14 14:37:10 -07003414
Andreas Huberda050cf22009-09-02 14:01:43 -07003415 if ((OMX_U32)numChannels != params.nChannels) {
3416 LOGW("Codec outputs a different number of channels than "
Andreas Hubere331c7b2010-02-01 10:51:50 -08003417 "the input stream contains (contains %d channels, "
3418 "codec outputs %ld channels).",
3419 numChannels, params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003420 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003421
Andreas Hubere6c40962009-09-10 14:13:30 -07003422 mOutputFormat->setCString(
3423 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
Andreas Huberda050cf22009-09-02 14:01:43 -07003424
3425 // Use the codec-advertised number of channels, as some
3426 // codecs appear to output stereo even if the input data is
Andreas Hubere331c7b2010-02-01 10:51:50 -08003427 // mono. If we know the codec lies about this information,
3428 // use the actual number of channels instead.
3429 mOutputFormat->setInt32(
3430 kKeyChannelCount,
3431 (mQuirks & kDecoderLiesAboutNumberOfChannels)
3432 ? numChannels : params.nChannels);
Andreas Huberda050cf22009-09-02 14:01:43 -07003433
3434 // The codec-reported sampleRate is not reliable...
3435 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
3436 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
Andreas Huber7ae02c82009-09-09 16:29:47 -07003437 OMX_AUDIO_PARAM_AMRTYPE amr;
3438 InitOMXParams(&amr);
3439 amr.nPortIndex = kPortIndexOutput;
3440
Andreas Huber784202e2009-10-15 13:46:54 -07003441 err = mOMX->getParameter(
Andreas Huber7ae02c82009-09-09 16:29:47 -07003442 mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
3443 CHECK_EQ(err, OK);
3444
3445 CHECK_EQ(amr.nChannels, 1);
3446 mOutputFormat->setInt32(kKeyChannelCount, 1);
3447
3448 if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
3449 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003450 mOutputFormat->setCString(
3451 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003452 mOutputFormat->setInt32(kKeySampleRate, 8000);
3453 } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
3454 && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003455 mOutputFormat->setCString(
3456 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
Andreas Huber7ae02c82009-09-09 16:29:47 -07003457 mOutputFormat->setInt32(kKeySampleRate, 16000);
3458 } else {
3459 CHECK(!"Unknown AMR band mode.");
3460 }
Andreas Huberda050cf22009-09-02 14:01:43 -07003461 } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003462 mOutputFormat->setCString(
3463 kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
James Dong17299ab2010-05-14 15:45:22 -07003464 int32_t numChannels, sampleRate, bitRate;
James Dongabed93a2010-04-22 17:27:04 -07003465 inputFormat->findInt32(kKeyChannelCount, &numChannels);
3466 inputFormat->findInt32(kKeySampleRate, &sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003467 inputFormat->findInt32(kKeyBitRate, &bitRate);
James Dongabed93a2010-04-22 17:27:04 -07003468 mOutputFormat->setInt32(kKeyChannelCount, numChannels);
3469 mOutputFormat->setInt32(kKeySampleRate, sampleRate);
James Dong17299ab2010-05-14 15:45:22 -07003470 mOutputFormat->setInt32(kKeyBitRate, bitRate);
Andreas Huberda050cf22009-09-02 14:01:43 -07003471 } else {
3472 CHECK(!"Should not be here. Unknown audio encoding.");
Andreas Huber43ad6eaf2009-09-01 16:02:43 -07003473 }
Andreas Huberbe06d262009-08-14 14:37:10 -07003474 break;
3475 }
3476
3477 case OMX_PortDomainVideo:
3478 {
3479 OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
3480
3481 if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003482 mOutputFormat->setCString(
3483 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
Andreas Huberbe06d262009-08-14 14:37:10 -07003484 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003485 mOutputFormat->setCString(
3486 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
Andreas Huberbe06d262009-08-14 14:37:10 -07003487 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003488 mOutputFormat->setCString(
3489 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
Andreas Huberbe06d262009-08-14 14:37:10 -07003490 } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
Andreas Hubere6c40962009-09-10 14:13:30 -07003491 mOutputFormat->setCString(
3492 kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
Andreas Huberbe06d262009-08-14 14:37:10 -07003493 } else {
3494 CHECK(!"Unknown compression format.");
3495 }
3496
3497 if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
3498 // This component appears to be lying to me.
3499 mOutputFormat->setInt32(
3500 kKeyWidth, (video_def->nFrameWidth + 15) & -16);
3501 mOutputFormat->setInt32(
3502 kKeyHeight, (video_def->nFrameHeight + 15) & -16);
3503 } else {
3504 mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
3505 mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
3506 }
3507
3508 mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
3509 break;
3510 }
3511
3512 default:
3513 {
3514 CHECK(!"should not be here, neither audio nor video.");
3515 break;
3516 }
3517 }
3518}
3519
Andreas Huber1f24b302010-06-10 11:12:39 -07003520status_t OMXCodec::pause() {
3521 Mutex::Autolock autoLock(mLock);
3522
3523 mPaused = true;
3524
3525 return OK;
3526}
3527
Andreas Hubere6c40962009-09-10 14:13:30 -07003528////////////////////////////////////////////////////////////////////////////////
3529
3530status_t QueryCodecs(
3531 const sp<IOMX> &omx,
3532 const char *mime, bool queryDecoders,
3533 Vector<CodecCapabilities> *results) {
3534 results->clear();
3535
3536 for (int index = 0;; ++index) {
3537 const char *componentName;
3538
3539 if (!queryDecoders) {
3540 componentName = GetCodec(
3541 kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
3542 mime, index);
3543 } else {
3544 componentName = GetCodec(
3545 kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
3546 mime, index);
3547 }
3548
3549 if (!componentName) {
3550 return OK;
3551 }
3552
Andreas Huber1a189a82010-03-24 13:49:20 -07003553 if (strncmp(componentName, "OMX.", 4)) {
3554 // Not an OpenMax component but a software codec.
3555
3556 results->push();
3557 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3558 caps->mComponentName = componentName;
3559
3560 continue;
3561 }
3562
Andreas Huber784202e2009-10-15 13:46:54 -07003563 sp<OMXCodecObserver> observer = new OMXCodecObserver;
Andreas Hubere6c40962009-09-10 14:13:30 -07003564 IOMX::node_id node;
Andreas Huber784202e2009-10-15 13:46:54 -07003565 status_t err = omx->allocateNode(componentName, observer, &node);
Andreas Hubere6c40962009-09-10 14:13:30 -07003566
3567 if (err != OK) {
3568 continue;
3569 }
3570
James Dong722d5912010-04-13 10:56:59 -07003571 OMXCodec::setComponentRole(omx, node, !queryDecoders, mime);
Andreas Hubere6c40962009-09-10 14:13:30 -07003572
3573 results->push();
3574 CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
3575 caps->mComponentName = componentName;
3576
3577 OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
3578 InitOMXParams(&param);
3579
3580 param.nPortIndex = queryDecoders ? 0 : 1;
3581
3582 for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
Andreas Huber784202e2009-10-15 13:46:54 -07003583 err = omx->getParameter(
Andreas Hubere6c40962009-09-10 14:13:30 -07003584 node, OMX_IndexParamVideoProfileLevelQuerySupported,
3585 &param, sizeof(param));
3586
3587 if (err != OK) {
3588 break;
3589 }
3590
3591 CodecProfileLevel profileLevel;
3592 profileLevel.mProfile = param.eProfile;
3593 profileLevel.mLevel = param.eLevel;
3594
3595 caps->mProfileLevels.push(profileLevel);
3596 }
3597
Andreas Huber784202e2009-10-15 13:46:54 -07003598 CHECK_EQ(omx->freeNode(node), OK);
Andreas Hubere6c40962009-09-10 14:13:30 -07003599 }
3600}
3601
Andreas Huberbe06d262009-08-14 14:37:10 -07003602} // namespace android